Exemplo n.º 1
0
int
main(int argc, char *argv[])
{
	input_fmt ifmt;
	u_long version;
	int ch, ret, t_ret;
	char *home;

	/* Initialize globals. */
	dbenv = NULL;
	db = NULL;
	if ((progname = strrchr(argv[0], '/')) == NULL)
		progname = argv[0];
	else
		++progname;
	verbose = 0;
	ffp = NULL;

	/* Initialize arguments. */
	home = NULL;
	ifmt = FORMAT_NL;
	version = 1;

	/* Process arguments. */
	while ((ch = getopt(argc, argv, "F:f:h:V:v")) != EOF)
		switch (ch) {
		case 'f':		/* Required argument */
			if ((ffp = freopen(optarg, "r", stdin)) == NULL) {
				fprintf(stderr,
				    "%s: %s\n", optarg, db_strerror(errno));
				return (EXIT_FAILURE);
			}
			break;
		case 'F':
			if (strcasecmp(optarg, "excel") == 0) {
				ifmt = FORMAT_EXCEL;
				break;
			}
			return (usage());
		case 'h':
			home = optarg;
			break;
		case 'V':
			if (strtoul_err(optarg, &version))
				return (EXIT_FAILURE);
			break;
		case 'v':
			++verbose;
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;

	if (*argv != NULL || ffp == NULL)
		return (usage());

	/*
	 * The home directory may not exist -- try and create it.  We don't
	 * bother to distinguish between failure to create it and it already
	 * existing, as the database environment open will fail if we aren't
	 * successful.
	 */
	if (home == NULL)
		home = getenv("DB_HOME");
	if (home != NULL)
		(void)mkdir(home, S_IRWXU);

	/* Create or join the database environment. */
	if (csv_env_open(home, 0) != 0)
		return (EXIT_FAILURE);

	/* Load records into the database. */
	ret = input_load(ifmt, version);

	/* Close the database environment. */
	if ((t_ret = csv_env_close()) != 0 && ret == 0)
		ret = t_ret;

	return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
Exemplo n.º 2
0
int
main(int argc, char *argv[])
{
    int ch, done, ret, t_ret;
    char **clist, **clp, *home;

    /* Initialize globals. */
    dbenv = NULL;
    db = NULL;
    if ((progname = strrchr(argv[0], '/')) == NULL)
        progname = argv[0];
    else
        ++progname;
    verbose = 0;

    /* Initialize arguments. */
    home = NULL;
    ret = 0;

    /* Allocate enough room for command-list arguments. */
    if ((clp = clist =
                   (char **)calloc((size_t)argc + 1, sizeof(char *))) == NULL) {
        fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM));
        return (EXIT_FAILURE);
    }

    /* Process arguments. */
    while ((ch = getopt(argc, argv, "c:h:v")) != EOF)
        switch (ch) {
        case 'c':
            *clp++ = optarg;
            break;
        case 'h':
            home = optarg;
            break;
        case 'v':
            ++verbose;
            break;
        case '?':
        default:
            return (usage());
        }
    argc -= optind;
    argv += optind;

    if (*argv != NULL)
        return (usage());

    /* Create or join the database environment. */
    if (csv_env_open(home, 1) != 0)
        return (EXIT_FAILURE);

    /* Handle the queries. */
    if (clp == clist)
        ret = query_interactive();
    else
        for (clp = clist, done = 0; *clp != NULL && !done; ++clp)
            if ((ret = query(*clp, &done)) != 0)
                break;

    /* Close the database environment. */
    if ((t_ret = csv_env_close()) != 0 && ret == 0)
        ret = t_ret;

    return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}