1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2005,2008 Oracle. All rights reserved. 5 * 6 * $Id: load_main.c,v 1.13 2008/01/08 20:58:23 bostic Exp $ 7 */ 8 9#include "csv.h" 10#include "csv_local.h" 11#include "csv_extern.h" 12 13static int usage(void); 14 15/* 16 * Globals 17 */ 18DB_ENV *dbenv; /* Database environment */ 19DB *db; /* Primary database */ 20DB **secondary; /* Secondaries */ 21int verbose; /* Program verbosity */ 22char *progname; /* Program name */ 23 24int 25main(int argc, char *argv[]) 26{ 27 input_fmt ifmt; 28 u_long version; 29 int ch, ret, t_ret; 30 char *home; 31 32 /* Initialize globals. */ 33 dbenv = NULL; 34 db = NULL; 35 if ((progname = strrchr(argv[0], '/')) == NULL) 36 progname = argv[0]; 37 else 38 ++progname; 39 verbose = 0; 40 41 /* Initialize arguments. */ 42 home = NULL; 43 ifmt = FORMAT_NL; 44 version = 1; 45 46 /* Process arguments. */ 47 while ((ch = getopt(argc, argv, "F:f:h:V:v")) != EOF) 48 switch (ch) { 49 case 'f': 50 if (freopen(optarg, "r", stdin) == NULL) { 51 fprintf(stderr, 52 "%s: %s\n", optarg, db_strerror(errno)); 53 return (EXIT_FAILURE); 54 } 55 break; 56 case 'F': 57 if (strcasecmp(optarg, "excel") == 0) { 58 ifmt = FORMAT_EXCEL; 59 break; 60 } 61 return (usage()); 62 case 'h': 63 home = optarg; 64 break; 65 case 'V': 66 if (strtoul_err(optarg, &version)) 67 return (EXIT_FAILURE); 68 break; 69 case 'v': 70 ++verbose; 71 break; 72 case '?': 73 default: 74 return (usage()); 75 } 76 argc -= optind; 77 argv += optind; 78 79 if (*argv != NULL) 80 return (usage()); 81 82 /* 83 * The home directory may not exist -- try and create it. We don't 84 * bother to distinguish between failure to create it and it already 85 * existing, as the database environment open will fail if we aren't 86 * successful. 87 */ 88 if (home == NULL) 89 home = getenv("DB_HOME"); 90 if (home != NULL) 91 (void)mkdir(home, S_IRWXU); 92 93 /* Create or join the database environment. */ 94 if (csv_env_open(home, 0) != 0) 95 return (EXIT_FAILURE); 96 97 /* Load records into the database. */ 98 ret = input_load(ifmt, version); 99 100 /* Close the database environment. */ 101 if ((t_ret = csv_env_close()) != 0 && ret == 0) 102 ret = t_ret; 103 104 return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 105} 106 107/* 108 * usage -- 109 * Program usage message. 110 */ 111static int 112usage(void) 113{ 114 (void)fprintf(stderr, 115 "usage: %s [-v] [-F excel] [-f csv-file] [-h home]\n", progname); 116 return (EXIT_FAILURE); 117} 118