]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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 | * $Id: getopt.c 6 2005-09-10 01:02:21Z nenolod $ | |
25 | */ | |
26 | ||
27 | #include "stdinc.h" | |
28 | ||
29 | #include "ircd_getopt.h" | |
30 | ||
31 | # define OPTCHAR '-' | |
32 | ||
33 | void | |
34 | parseargs(int *argc, char ***argv, struct lgetopt *opts) | |
35 | { | |
36 | int i; | |
37 | char *progname = (*argv)[0]; | |
38 | ||
39 | /* loop through each argument */ | |
40 | for (;;) | |
41 | { | |
42 | int found = 0; | |
43 | ||
44 | (*argc)--; | |
45 | (*argv)++; | |
46 | ||
47 | if(*argc < 1) | |
48 | { | |
49 | return; | |
50 | } | |
51 | ||
52 | /* check if it *is* an arg.. */ | |
53 | if((*argv)[0][0] != OPTCHAR) | |
54 | { | |
55 | return; | |
56 | } | |
57 | ||
58 | (*argv)[0]++; | |
59 | ||
60 | /* search through our argument list, and see if it matches */ | |
61 | for (i = 0; opts[i].opt; i++) | |
62 | { | |
63 | if(!strcmp(opts[i].opt, (*argv)[0])) | |
64 | { | |
65 | /* found our argument */ | |
66 | found = 1; | |
67 | ||
68 | switch (opts[i].argtype) | |
69 | { | |
70 | case YESNO: | |
71 | *((int *) opts[i].argloc) = 1; | |
72 | break; | |
73 | case INTEGER: | |
74 | if(*argc < 2) | |
75 | { | |
76 | fprintf(stderr, | |
77 | "Error: option '%c%s' requires an argument\n", | |
78 | OPTCHAR, opts[i].opt); | |
79 | usage((*argv)[0]); | |
80 | } | |
81 | ||
82 | *((int *) opts[i].argloc) = atoi((*argv)[1]); | |
83 | ||
84 | (*argc)--; | |
85 | (*argv)++; | |
86 | break; | |
87 | case STRING: | |
88 | if(*argc < 2) | |
89 | { | |
90 | fprintf(stderr, | |
91 | "error: option '%c%s' requires an argument\n", | |
92 | OPTCHAR, opts[i].opt); | |
93 | usage(progname); | |
94 | } | |
95 | ||
96 | *((char **) opts[i].argloc) = | |
97 | malloc(strlen((*argv)[1]) + 1); | |
98 | strcpy(*((char **) opts[i].argloc), (*argv)[1]); | |
99 | ||
100 | (*argc)--; | |
101 | (*argv)++; | |
102 | break; | |
103 | ||
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); | |
111 | } | |
112 | } | |
113 | } | |
114 | if(!found) | |
115 | { | |
116 | fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, (*argv)[0]); | |
117 | usage(progname); | |
118 | } | |
119 | } | |
120 | } | |
121 | ||
122 | void | |
123 | usage(char *name) | |
124 | { | |
125 | int i = 0; | |
126 | ||
127 | fprintf(stderr, "Usage: %s [options]\n", name); | |
128 | fprintf(stderr, "Where valid options are:\n"); | |
129 | ||
130 | for (i = 0; myopts[i].opt; i++) | |
131 | { | |
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); | |
137 | } | |
138 | ||
139 | exit(EXIT_FAILURE); | |
140 | } |