Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

query_main.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: query_main.c,v 1.8 2005/04/04 19:59:21 bostic Exp $
00008  */
00009 
00010 #include "csv.h"
00011 #include "csv_local.h"
00012 #include "csv_extern.h"
00013 
00014 static int usage(void);
00015 
00016 /*
00017  * Globals
00018  */
00019 DB_ENV   *dbenv;                        /* Database environment */
00020 DB       *db;                           /* Primary database */
00021 int       verbose;                      /* Program verbosity */
00022 char     *progname;                     /* Program name */
00023 
00024 int
00025 main(int argc, char *argv[])
00026 {
00027         int ch, done, ret, t_ret;
00028         char **clist, **clp, *home;
00029 
00030         /* Initialize globals. */
00031         dbenv = NULL;
00032         db = NULL;
00033         if ((progname = strrchr(argv[0], '/')) == NULL)
00034                 progname = argv[0];
00035         else
00036                 ++progname;
00037         verbose = 0;
00038 
00039         /* Initialize arguments. */
00040         home = NULL;
00041         ret = 0;
00042 
00043         /* Allocate enough room for command-list arguments. */
00044         if ((clp = clist =
00045             (char **)calloc((size_t)argc + 1, sizeof(char *))) == NULL) {
00046                 fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM));
00047                 return (EXIT_FAILURE);
00048         }
00049 
00050         /* Process arguments. */
00051         while ((ch = getopt(argc, argv, "c:h:v")) != EOF)
00052                 switch (ch) {
00053                 case 'c':
00054                         *clp++ = optarg;
00055                         break;
00056                 case 'h':
00057                         home = optarg;
00058                         break;
00059                 case 'v':
00060                         ++verbose;
00061                         break;
00062                 case '?':
00063                 default:
00064                         return (usage());
00065                 }
00066         argc -= optind;
00067         argv += optind;
00068 
00069         if (*argv != NULL)
00070                 return (usage());
00071 
00072         /* Create or join the database environment. */
00073         if (csv_env_open(home, 1) != 0)
00074                 return (EXIT_FAILURE);
00075 
00076         /* Handle the queries. */
00077         if (clp == clist)
00078                 ret = query_interactive();
00079         else
00080                 for (clp = clist, done = 0; *clp != NULL && !done; ++clp)
00081                         if ((ret = query(*clp, &done)) != 0)
00082                                 break;
00083 
00084         /* Close the database environment. */
00085         if ((t_ret = csv_env_close()) != 0 && ret == 0)
00086                 ret = t_ret;
00087 
00088         return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00089 }
00090 
00091 /*
00092  * usage --
00093  *      Program usage message.
00094  */
00095 static int
00096 usage(void)
00097 {
00098         (void)fprintf(stderr, "usage: %s [-v] [-c cmd] [-h home]\n", progname);
00099         return (EXIT_FAILURE);
00100 }

Generated on Sun Dec 25 12:14:25 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2