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