1 |
adx |
30 |
/* |
2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
|
|
* getopt.c: Uses getopt to fetch the command line options. |
4 |
|
|
* |
5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
|
|
* |
7 |
|
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* This program is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with this program; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
|
|
* USA |
21 |
|
|
* |
22 |
knight |
31 |
* $Id$ |
23 |
adx |
30 |
*/ |
24 |
|
|
|
25 |
|
|
#include "stdinc.h" |
26 |
|
|
#include "ircd_getopt.h" |
27 |
|
|
|
28 |
|
|
#define OPTCHAR '-' |
29 |
|
|
|
30 |
|
|
static void usage(const char *name); |
31 |
|
|
|
32 |
|
|
void |
33 |
|
|
parseargs(int *argc, char ***argv, struct lgetopt *opts) |
34 |
|
|
{ |
35 |
|
|
int i; |
36 |
|
|
char *progname = (*argv)[0]; |
37 |
|
|
|
38 |
|
|
/* loop through each argument */ |
39 |
|
|
for (;;) |
40 |
|
|
{ |
41 |
|
|
int found = 0; |
42 |
|
|
|
43 |
|
|
(*argc)--; |
44 |
|
|
(*argv)++; |
45 |
|
|
|
46 |
|
|
if (*argc < 1) |
47 |
|
|
{ |
48 |
|
|
return; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
/* check if it *is* an arg.. */ |
52 |
|
|
if ((*argv)[0][0] != OPTCHAR) |
53 |
|
|
{ |
54 |
|
|
return; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
(*argv)[0]++; |
58 |
|
|
|
59 |
|
|
/* search through our argument list, and see if it matches */ |
60 |
|
|
for (i = 0; opts[i].opt; i++) |
61 |
|
|
{ |
62 |
|
|
if (!strcmp(opts[i].opt, (*argv)[0])) |
63 |
|
|
{ |
64 |
|
|
/* found our argument */ |
65 |
|
|
found = 1; |
66 |
|
|
|
67 |
|
|
switch (opts[i].argtype) |
68 |
|
|
{ |
69 |
|
|
case YESNO: |
70 |
|
|
*((int *)opts[i].argloc) = 1; |
71 |
|
|
break; |
72 |
|
|
case INTEGER: |
73 |
|
|
if (*argc < 2) |
74 |
|
|
{ |
75 |
|
|
fprintf(stderr, "Error: option '%c%s' requires an argument\n", |
76 |
|
|
OPTCHAR, opts[i].opt); |
77 |
|
|
usage((*argv)[0]); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
*((int *)opts[i].argloc) = atoi((*argv)[1]); |
81 |
michael |
755 |
(*argc)--; |
82 |
|
|
(*argv)++; |
83 |
adx |
30 |
break; |
84 |
|
|
case STRING: |
85 |
|
|
if (*argc < 2) |
86 |
|
|
{ |
87 |
|
|
fprintf(stderr, "error: option '%c%s' requires an argument\n", |
88 |
|
|
OPTCHAR, opts[i].opt); |
89 |
|
|
usage(progname); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
*((char**)opts[i].argloc) = malloc(strlen((*argv)[1]) + 1); |
93 |
|
|
strcpy(*((char**)opts[i].argloc), (*argv)[1]); |
94 |
michael |
755 |
(*argc)--; |
95 |
|
|
(*argv)++; |
96 |
adx |
30 |
break; |
97 |
|
|
|
98 |
|
|
case USAGE: |
99 |
|
|
usage(progname); |
100 |
|
|
/*NOTREACHED*/ |
101 |
|
|
|
102 |
|
|
default: |
103 |
|
|
fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n", |
104 |
|
|
__FILE__, __LINE__); |
105 |
|
|
exit(EXIT_FAILURE); |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
if (!found) |
110 |
|
|
{ |
111 |
|
|
fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, (*argv)[0]); |
112 |
|
|
usage(progname); |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
static void |
118 |
|
|
usage(const char *name) |
119 |
|
|
{ |
120 |
|
|
int i; |
121 |
|
|
|
122 |
|
|
fprintf(stderr, "Usage: %s [options]\n", name); |
123 |
|
|
fprintf(stderr, "Where valid options are:\n"); |
124 |
|
|
|
125 |
|
|
for (i = 0; myopts[i].opt; i++) |
126 |
|
|
{ |
127 |
|
|
fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, myopts[i].opt, |
128 |
|
|
(myopts[i].argtype == YESNO || myopts[i].argtype == USAGE) ? "" : |
129 |
|
|
myopts[i].argtype == INTEGER ? "<number>" : "<string>", |
130 |
|
|
myopts[i].desc); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
exit(EXIT_FAILURE); |
134 |
|
|
} |
135 |
|
|
|