Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / getopt.c
blob9e28ea2f8c9b70a81df9e14ef016b7796a848681
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * getopt.c: Uses getopt to fetch the command line options.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
25 #include "stdinc.h"
27 #include "ircd_getopt.h"
29 # define OPTCHAR '-'
31 void
32 parseargs(int *argc, char ***argv, struct lgetopt *opts)
34 int i;
35 char *progname = (*argv)[0];
37 /* loop through each argument */
38 for (;;)
40 int found = 0;
42 (*argc)--;
43 (*argv)++;
45 if(*argc < 1)
47 return;
50 /* check if it *is* an arg.. */
51 if((*argv)[0][0] != OPTCHAR)
53 return;
56 (*argv)[0]++;
58 /* search through our argument list, and see if it matches */
59 for (i = 0; opts[i].opt; i++)
61 if(!strcmp(opts[i].opt, (*argv)[0]))
63 /* found our argument */
64 found = 1;
66 switch (opts[i].argtype)
68 case YESNO:
69 *((int *) opts[i].argloc) = 1;
70 break;
71 case INTEGER:
72 if(*argc < 2)
74 fprintf(stderr,
75 "Error: option '%c%s' requires an argument\n",
76 OPTCHAR, opts[i].opt);
77 usage((*argv)[0]);
80 *((int *) opts[i].argloc) = atoi((*argv)[1]);
82 (*argc)--;
83 (*argv)++;
84 break;
85 case STRING:
86 if(*argc < 2)
88 fprintf(stderr,
89 "error: option '%c%s' requires an argument\n",
90 OPTCHAR, opts[i].opt);
91 usage(progname);
94 *((char **) opts[i].argloc) =
95 malloc(strlen((*argv)[1]) + 1);
96 strcpy(*((char **) opts[i].argloc), (*argv)[1]);
98 (*argc)--;
99 (*argv)++;
100 break;
102 case USAGE:
103 usage(progname);
104 /*NOTREACHED*/ default:
105 fprintf(stderr,
106 "Error: internal error in parseargs() at %s:%d\n",
107 __FILE__, __LINE__);
108 exit(EXIT_FAILURE);
112 if(!found)
114 fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, (*argv)[0]);
115 usage(progname);
120 void
121 usage(char *name)
123 int i = 0;
125 fprintf(stderr, "Usage: %s [options]\n", name);
126 fprintf(stderr, "Where valid options are:\n");
128 for (i = 0; myopts[i].opt; i++)
130 fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR,
131 myopts[i].opt, (myopts[i].argtype == YESNO
132 || myopts[i].argtype ==
133 USAGE) ? "" : myopts[i].argtype ==
134 INTEGER ? "<number>" : "<string>", myopts[i].desc);
137 exit(EXIT_FAILURE);