| 1 |
adx |
30 |
/* |
| 2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
adx |
30 |
* |
| 4 |
michael |
8279 |
* Copyright (c) 2001-2018 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 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
adx |
30 |
* 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 |
michael |
4470 |
#include "memory.h" |
| 30 |
adx |
30 |
|
| 31 |
|
|
#define OPTCHAR '-' |
| 32 |
|
|
|
| 33 |
|
|
|
| 34 |
michael |
1357 |
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 |
michael |
6383 |
for (; opts->opt; ++opts) |
| 41 |
|
|
fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, opts->opt, |
| 42 |
|
|
(opts->argtype == YESNO || opts->argtype == USAGE) ? "" : |
| 43 |
|
|
opts->argtype == INTEGER ? "<number>" : "<string>", |
| 44 |
|
|
opts->desc); |
| 45 |
michael |
1357 |
|
| 46 |
|
|
exit(EXIT_FAILURE); |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
adx |
30 |
void |
| 50 |
|
|
parseargs(int *argc, char ***argv, struct lgetopt *opts) |
| 51 |
|
|
{ |
| 52 |
michael |
1357 |
const char *progname = (*argv)[0]; |
| 53 |
adx |
30 |
|
| 54 |
michael |
3250 |
/* Loop through each argument */ |
| 55 |
michael |
1357 |
while (1) |
| 56 |
|
|
{ |
| 57 |
michael |
8656 |
bool found = false; |
| 58 |
michael |
4470 |
const char *opt = NULL; |
| 59 |
michael |
1357 |
|
| 60 |
|
|
(*argc)--; |
| 61 |
|
|
(*argv)++; |
| 62 |
|
|
|
| 63 |
|
|
if (*argc < 1 || (*argv)[0][0] != OPTCHAR) |
| 64 |
|
|
return; |
| 65 |
|
|
|
| 66 |
michael |
4470 |
opt = &(*argv)[0][1]; |
| 67 |
michael |
1357 |
|
| 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 |
8603 |
if (strcmp(opts[i].opt, opt) == 0) |
| 72 |
michael |
1357 |
{ |
| 73 |
michael |
3250 |
/* Found our argument */ |
| 74 |
michael |
8656 |
found = true; |
| 75 |
adx |
30 |
|
| 76 |
michael |
1357 |
switch (opts[i].argtype) |
| 77 |
|
|
{ |
| 78 |
|
|
case YESNO: |
| 79 |
michael |
8658 |
*((bool *)opts[i].argloc) = true; |
| 80 |
michael |
1357 |
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 |
michael |
6381 |
fprintf(stderr, "Error: option '%c%s' requires an argument\n", |
| 99 |
michael |
1357 |
OPTCHAR, opts[i].opt); |
| 100 |
|
|
usage(progname, opts); |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
michael |
4470 |
*((char **)opts[i].argloc) = xstrdup((*argv)[1]); |
| 104 |
michael |
755 |
(*argc)--; |
| 105 |
|
|
(*argv)++; |
| 106 |
michael |
1357 |
break; |
| 107 |
adx |
30 |
|
| 108 |
michael |
1357 |
case USAGE: |
| 109 |
|
|
usage(progname, opts); |
| 110 |
|
|
/* NOTREACHED */ |
| 111 |
adx |
30 |
|
| 112 |
michael |
1357 |
default: |
| 113 |
|
|
fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n", |
| 114 |
|
|
__FILE__, __LINE__); |
| 115 |
|
|
exit(EXIT_FAILURE); |
| 116 |
|
|
} |
| 117 |
|
|
} |
| 118 |
adx |
30 |
} |
| 119 |
|
|
|
| 120 |
michael |
8656 |
if (found == false) |
| 121 |
michael |
1357 |
{ |
| 122 |
michael |
6381 |
fprintf(stderr, "Error: unknown argument '%c%s'\n", |
| 123 |
michael |
4470 |
OPTCHAR, opt); |
| 124 |
michael |
1357 |
usage(progname, opts); |
| 125 |
|
|
} |
| 126 |
adx |
30 |
} |
| 127 |
|
|
} |