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