]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * getopt.c: Uses getopt to fetch the command line options. | |
4 | * | |
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 | |
8 | * | |
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. | |
13 | * | |
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. | |
18 | * | |
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 | |
23 | */ | |
24 | ||
25 | #include "stdinc.h" | |
26 | ||
27 | #include "ircd_getopt.h" | |
28 | ||
29 | # define OPTCHAR '-' | |
30 | ||
31 | void | |
32 | parseargs(int *argc, char * const **argv, struct lgetopt *opts) | |
33 | { | |
34 | int i; | |
35 | const char *progname = (*argv)[0]; | |
36 | ||
37 | /* loop through each argument */ | |
38 | for (;;) | |
39 | { | |
40 | int found = 0; | |
41 | ||
42 | (*argc)--; | |
43 | (*argv)++; | |
44 | ||
45 | if(*argc < 1) | |
46 | { | |
47 | return; | |
48 | } | |
49 | ||
50 | /* check if it *is* an arg.. */ | |
51 | if((*argv)[0][0] != OPTCHAR) | |
52 | { | |
53 | return; | |
54 | } | |
55 | ||
56 | /* search through our argument list, and see if it matches */ | |
57 | for (i = 0; opts[i].opt; i++) | |
58 | { | |
59 | if(!strcmp(opts[i].opt, &(*argv)[0][1])) | |
60 | { | |
61 | /* found our argument */ | |
62 | found = 1; | |
63 | ||
64 | switch (opts[i].argtype) | |
65 | { | |
66 | case YESNO: | |
67 | *((int *) opts[i].argloc) = 1; | |
68 | break; | |
69 | case INTEGER: | |
70 | if(*argc < 2) | |
71 | { | |
72 | fprintf(stderr, | |
73 | "Error: option '%c%s' requires an argument\n", | |
74 | OPTCHAR, opts[i].opt); | |
75 | usage(progname); | |
76 | } | |
77 | ||
78 | *((int *) opts[i].argloc) = atoi((*argv)[1]); | |
79 | ||
80 | (*argc)--; | |
81 | (*argv)++; | |
82 | break; | |
83 | case STRING: | |
84 | if(*argc < 2) | |
85 | { | |
86 | fprintf(stderr, | |
87 | "error: option '%c%s' requires an argument\n", | |
88 | OPTCHAR, opts[i].opt); | |
89 | usage(progname); | |
90 | } | |
91 | ||
92 | *((char **) opts[i].argloc) = | |
93 | malloc(strlen((*argv)[1]) + 1); | |
94 | strcpy(*((char **) opts[i].argloc), (*argv)[1]); | |
95 | ||
96 | (*argc)--; | |
97 | (*argv)++; | |
98 | break; | |
99 | ||
100 | case USAGE: | |
101 | usage(progname); | |
102 | /*NOTREACHED*/ default: | |
103 | fprintf(stderr, | |
104 | "Error: internal error in parseargs() at %s:%d\n", | |
105 | __FILE__, __LINE__); | |
106 | exit(EXIT_FAILURE); | |
107 | } | |
108 | } | |
109 | } | |
110 | if(!found) | |
111 | { | |
112 | fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, &(*argv)[0][1]); | |
113 | usage(progname); | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
118 | void | |
119 | usage(const char *name) | |
120 | { | |
121 | int i = 0; | |
122 | ||
123 | fprintf(stderr, "Usage: %s [options]\n", name); | |
124 | fprintf(stderr, "Where valid options are:\n"); | |
125 | ||
126 | for (i = 0; myopts[i].opt; i++) | |
127 | { | |
128 | fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, | |
129 | myopts[i].opt, (myopts[i].argtype == YESNO | |
130 | || myopts[i].argtype == | |
131 | USAGE) ? "" : myopts[i].argtype == | |
132 | INTEGER ? "<number>" : "<string>", myopts[i].desc); | |
133 | } | |
134 | ||
135 | exit(EXIT_FAILURE); | |
136 | } |