1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2001-2020 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 |
fprintf(stderr, "Usage: %s [options]\n", name); |
38 |
fprintf(stderr, "Where valid options are:\n"); |
39 |
|
40 |
for (; opts->opt; ++opts) |
41 |
fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, opts->opt, |
42 |
(opts->argtype == BOOLEAN || opts->argtype == USAGE) ? "" : |
43 |
opts->argtype == INTEGER ? "<number>" : "<string>", |
44 |
opts->desc); |
45 |
|
46 |
exit(EXIT_FAILURE); |
47 |
} |
48 |
|
49 |
void |
50 |
parseargs(int *argc, char ***argv, struct lgetopt *opts) |
51 |
{ |
52 |
const char *progname = (*argv)[0]; |
53 |
|
54 |
/* Loop through each argument */ |
55 |
while (true) |
56 |
{ |
57 |
bool found = false; |
58 |
const char *opt = NULL; |
59 |
|
60 |
(*argc)--; |
61 |
(*argv)++; |
62 |
|
63 |
if (*argc < 1 || (*argv)[0][0] != OPTCHAR) |
64 |
return; |
65 |
|
66 |
opt = &(*argv)[0][1]; |
67 |
|
68 |
/* Search through our argument list, and see if it matches */ |
69 |
for (unsigned int i = 0; opts[i].opt; ++i) |
70 |
{ |
71 |
if (strcmp(opts[i].opt, opt) == 0) |
72 |
{ |
73 |
/* Found our argument */ |
74 |
found = true; |
75 |
|
76 |
switch (opts[i].argtype) |
77 |
{ |
78 |
case BOOLEAN: |
79 |
*((bool *)opts[i].argloc) = true; |
80 |
break; |
81 |
|
82 |
case INTEGER: |
83 |
if (*argc < 2) |
84 |
{ |
85 |
fprintf(stderr, "Error: option '%c%s' requires an argument\n", |
86 |
OPTCHAR, opts[i].opt); |
87 |
usage((*argv)[0], opts); |
88 |
} |
89 |
|
90 |
*((int *)opts[i].argloc) = atoi((*argv)[1]); |
91 |
(*argc)--; |
92 |
(*argv)++; |
93 |
break; |
94 |
|
95 |
case STRING: |
96 |
if (*argc < 2) |
97 |
{ |
98 |
fprintf(stderr, "Error: option '%c%s' requires an argument\n", |
99 |
OPTCHAR, opts[i].opt); |
100 |
usage(progname, opts); |
101 |
} |
102 |
|
103 |
*((char **)opts[i].argloc) = xstrdup((*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 == false) |
121 |
{ |
122 |
fprintf(stderr, "Error: unknown argument '%c%s'\n", |
123 |
OPTCHAR, opt); |
124 |
usage(progname, opts); |
125 |
} |
126 |
} |
127 |
} |