1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
2916 |
* Copyright (c) 2001-2014 ircd-hybrid development team |
5 |
adx |
30 |
* |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file getopt.c |
23 |
|
|
* \brief Uses getopt to fetch the command line options. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
|
|
#include "ircd_getopt.h" |
29 |
|
|
|
30 |
|
|
#define OPTCHAR '-' |
31 |
|
|
|
32 |
|
|
|
33 |
michael |
1357 |
static void |
34 |
|
|
usage(const char *name, const struct lgetopt *opts) |
35 |
|
|
{ |
36 |
|
|
unsigned int i; |
37 |
|
|
|
38 |
|
|
fprintf(stderr, "Usage: %s [options]\n", name); |
39 |
|
|
fprintf(stderr, "Where valid options are:\n"); |
40 |
|
|
|
41 |
|
|
for (i = 0; opts[i].opt; ++i) |
42 |
|
|
fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, opts[i].opt, |
43 |
|
|
(opts[i].argtype == YESNO || opts[i].argtype == USAGE) ? "" : |
44 |
|
|
opts[i].argtype == INTEGER ? "<number>" : "<string>", |
45 |
|
|
opts[i].desc); |
46 |
|
|
|
47 |
|
|
exit(EXIT_FAILURE); |
48 |
|
|
} |
49 |
|
|
|
50 |
adx |
30 |
void |
51 |
|
|
parseargs(int *argc, char ***argv, struct lgetopt *opts) |
52 |
|
|
{ |
53 |
michael |
1357 |
const char *progname = (*argv)[0]; |
54 |
adx |
30 |
|
55 |
michael |
3250 |
/* Loop through each argument */ |
56 |
michael |
1357 |
while (1) |
57 |
|
|
{ |
58 |
|
|
int found = 0; |
59 |
|
|
|
60 |
|
|
(*argc)--; |
61 |
|
|
(*argv)++; |
62 |
|
|
|
63 |
|
|
if (*argc < 1 || (*argv)[0][0] != OPTCHAR) |
64 |
|
|
return; |
65 |
|
|
|
66 |
|
|
(*argv)[0]++; |
67 |
|
|
|
68 |
michael |
3250 |
/* Search through our argument list, and see if it matches */ |
69 |
|
|
for (unsigned int i = 0; opts[i].opt; ++i) |
70 |
adx |
30 |
{ |
71 |
michael |
1357 |
if (!strcmp(opts[i].opt, (*argv)[0])) |
72 |
|
|
{ |
73 |
michael |
3250 |
/* Found our argument */ |
74 |
michael |
1357 |
found = 1; |
75 |
adx |
30 |
|
76 |
michael |
1357 |
switch (opts[i].argtype) |
77 |
|
|
{ |
78 |
|
|
case YESNO: |
79 |
|
|
*((int *)opts[i].argloc) = 1; |
80 |
|
|
break; |
81 |
adx |
30 |
|
82 |
michael |
1357 |
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 |
michael |
2916 |
|
90 |
michael |
1357 |
*((int *)opts[i].argloc) = atoi((*argv)[1]); |
91 |
michael |
755 |
(*argc)--; |
92 |
|
|
(*argv)++; |
93 |
michael |
1357 |
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) = malloc(strlen((*argv)[1]) + 1); |
104 |
|
|
strcpy(*((char**)opts[i].argloc), (*argv)[1]); |
105 |
michael |
755 |
(*argc)--; |
106 |
|
|
(*argv)++; |
107 |
michael |
1357 |
break; |
108 |
adx |
30 |
|
109 |
michael |
1357 |
case USAGE: |
110 |
|
|
usage(progname, opts); |
111 |
|
|
/* NOTREACHED */ |
112 |
adx |
30 |
|
113 |
michael |
1357 |
default: |
114 |
|
|
fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n", |
115 |
|
|
__FILE__, __LINE__); |
116 |
|
|
exit(EXIT_FAILURE); |
117 |
|
|
} |
118 |
|
|
} |
119 |
adx |
30 |
} |
120 |
|
|
|
121 |
michael |
1357 |
if (!found) |
122 |
|
|
{ |
123 |
|
|
fprintf(stderr, "error: unknown argument '%c%s'\n", |
124 |
|
|
OPTCHAR, (*argv)[0]); |
125 |
|
|
usage(progname, opts); |
126 |
|
|
} |
127 |
adx |
30 |
} |
128 |
|
|
} |