Clean.
[seven-1.x.git] / src / getopt.c
blobb536ae94f9ee604f070693e947b3928c1c7e18b1
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * getopt.c: Uses getopt to fetch the command line options.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
24 * $Id: getopt.c 26 2006-09-20 18:02:06Z spb $
27 #include "stdinc.h"
29 #include "ircd_getopt.h"
31 # define OPTCHAR '-'
33 void
34 parseargs(int *argc, char ***argv, struct lgetopt *opts)
36 int i;
37 char *progname = (*argv)[0];
39 /* loop through each argument */
40 for (;;)
42 int found = 0;
44 (*argc)--;
45 (*argv)++;
47 if(*argc < 1)
49 return;
52 /* check if it *is* an arg.. */
53 if((*argv)[0][0] != OPTCHAR)
55 return;
58 (*argv)[0]++;
60 /* search through our argument list, and see if it matches */
61 for (i = 0; opts[i].opt; i++)
63 if(!strcmp(opts[i].opt, (*argv)[0]))
65 /* found our argument */
66 found = 1;
68 switch (opts[i].argtype)
70 case YESNO:
71 *((int *) opts[i].argloc) = 1;
72 break;
73 case INTEGER:
74 if(*argc < 2)
76 fprintf(stderr,
77 "Error: option '%c%s' requires an argument\n",
78 OPTCHAR, opts[i].opt);
79 usage((*argv)[0]);
82 *((int *) opts[i].argloc) = atoi((*argv)[1]);
84 (*argc)--;
85 (*argv)++;
86 break;
87 case STRING:
88 if(*argc < 2)
90 fprintf(stderr,
91 "error: option '%c%s' requires an argument\n",
92 OPTCHAR, opts[i].opt);
93 usage(progname);
96 *((char **) opts[i].argloc) =
97 malloc(strlen((*argv)[1]) + 1);
98 strcpy(*((char **) opts[i].argloc), (*argv)[1]);
100 (*argc)--;
101 (*argv)++;
102 break;
104 case USAGE:
105 usage(progname);
106 /*NOTREACHED*/ default:
107 fprintf(stderr,
108 "Error: internal error in parseargs() at %s:%d\n",
109 __FILE__, __LINE__);
110 exit(EXIT_FAILURE);
114 if(!found)
116 fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, (*argv)[0]);
117 usage(progname);
122 void
123 usage(char *name)
125 int i = 0;
127 fprintf(stderr, "Usage: %s [options]\n", name);
128 fprintf(stderr, "Where valid options are:\n");
130 for (i = 0; myopts[i].opt; i++)
132 fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR,
133 myopts[i].opt, (myopts[i].argtype == YESNO
134 || myopts[i].argtype ==
135 USAGE) ? "" : myopts[i].argtype ==
136 INTEGER ? "<number>" : "<string>", myopts[i].desc);
139 exit(EXIT_FAILURE);