1 /**********************************************************************
2  * options.c                                                   May 1999
3  * Horms                                             horms@verge.net.au
4  *
5  * Parse command line arguments
6  * Code based on man getopt(3), later translated to popt.
7  * Some code based on man popt(3)
8  *
9  * perdition
10  * Mail retrieval proxy server
11  * Copyright (C) 1999-2005  Horms
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of the
16  * License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  **********************************************************************/
28 
29 #include <stdlib.h>
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include "options.h"
36 
37 #ifdef DMALLOC
38 #include <dmalloc.h>
39 #endif
40 
41 
42 /**********************************************************************
43  * makegdbm_options
44  * parse command line arguments
45  **********************************************************************/
46 
makegdbm_options(int argc,char ** argv)47 makegdbm_options_t makegdbm_options(int argc, char **argv){
48   int c=0;
49   char *optarg;
50   poptContext context;
51   makegdbm_options_t opt;
52 
53   static struct poptOption options[] =
54   {
55     {"help",  'h', POPT_ARG_NONE, NULL, 'h', NULL, NULL},
56     {"undo",  'u', POPT_ARG_NONE, NULL, 'u', NULL, NULL},
57     {NULL,    0,   0,             NULL, 0,   NULL, NULL}
58   };
59 
60 
61   /*Defaults*/
62   memset(&opt, 0, sizeof(opt));
63 
64   if(argc==0 || argv==NULL) return(opt);
65 
66   context= poptGetContext("perdition", argc, (const char **)argv, options, 0);
67 
68   while ((c=poptGetNextOpt(context)) >= 0){
69     optarg=(char *)poptGetOptArg(context);
70     switch (c){
71       case 'h':
72         usage(0);
73         break;
74       case 'u':
75         opt.undo=1;
76         break;
77       }
78   }
79 
80   if (c < -1) {
81     fprintf(
82       stderr,
83       "options: %s: %s\n",
84       poptBadOption(context, POPT_BADOPTION_NOALIAS),
85       poptStrerror(c)
86     );
87   }
88 
89   opt.mapname = (char *)poptGetArg(context);
90   if((opt.mapname == NULL) || !(poptPeekArg(context) == NULL)){
91     usage(-1);
92   }
93 
94   return(opt);
95 }
96 
97 
98 /**********************************************************************
99  * usage
100  * Display usage information and exit
101  * Prints to stdout if exit_status=0, stderr otherwise
102  **********************************************************************/
103 
usage(int exit_status)104 void usage(int exit_status){
105   FILE *stream;
106 
107   if(exit_status!=0){
108      stream=stderr;
109   }
110   else{
111      stream=stdout;
112   }
113 
114 
115   fprintf(
116     stream,
117     "perdition version " VERSION " Copyright Horms\n"
118     "\n"
119     "Usage: makegdbm [options] gdbmname\n"
120     "   options: -h, --help: print this message\n"
121     "            -u, --undo: print content of database file, one entry a line\n"
122   );
123 
124   exit(exit_status);
125 }
126