]> jfr.im git - irc/rqf/shadowircd.git/blob - src/getopt.c
Some clang static analyzer fixes.
[irc/rqf/shadowircd.git] / src / getopt.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * getopt.c: Uses getopt to fetch the command line options.
4 *
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
8 *
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.
13 *
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.
18 *
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
23 *
24 */
25
26 #include "stdinc.h"
27
28 #include "ircd_getopt.h"
29
30 # define OPTCHAR '-'
31
32 void
33 parseargs(int *argc, char ***argv, struct lgetopt *opts)
34 {
35 int i;
36 char *progname = (*argv)[0];
37
38 /* loop through each argument */
39 for (;;)
40 {
41 int found = 0;
42
43 (*argc)--;
44 (*argv)++;
45
46 if(*argc < 1)
47 {
48 return;
49 }
50
51 /* check if it *is* an arg.. */
52 if((*argv)[0][0] != OPTCHAR)
53 {
54 return;
55 }
56
57 (*argv)[0]++;
58
59 /* search through our argument list, and see if it matches */
60 for (i = 0; opts[i].opt; i++)
61 {
62 if(!strcmp(opts[i].opt, (*argv)[0]))
63 {
64 /* found our argument */
65 found = 1;
66
67 switch (opts[i].argtype)
68 {
69 case YESNO:
70 *((int *) opts[i].argloc) = 1;
71 break;
72 case INTEGER:
73 if(*argc < 2)
74 {
75 fprintf(stderr,
76 "Error: option '%c%s' requires an argument\n",
77 OPTCHAR, opts[i].opt);
78 usage((*argv)[0]);
79 }
80
81 *((int *) opts[i].argloc) = atoi((*argv)[1]);
82
83 (*argc)--;
84 (*argv)++;
85 break;
86 case STRING:
87 if(*argc < 2)
88 {
89 fprintf(stderr,
90 "error: option '%c%s' requires an argument\n",
91 OPTCHAR, opts[i].opt);
92 usage(progname);
93 }
94
95 *((char **) opts[i].argloc) =
96 malloc(strlen((*argv)[1]) + 1);
97 strcpy(*((char **) opts[i].argloc), (*argv)[1]);
98
99 (*argc)--;
100 (*argv)++;
101 break;
102
103 case USAGE:
104 usage(progname);
105 /*NOTREACHED*/ default:
106 fprintf(stderr,
107 "Error: internal error in parseargs() at %s:%d\n",
108 __FILE__, __LINE__);
109 exit(EXIT_FAILURE);
110 }
111 }
112 }
113 if(!found)
114 {
115 fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, (*argv)[0]);
116 usage(progname);
117 }
118 }
119 }
120
121 void
122 usage(char *name)
123 {
124 int i = 0;
125
126 fprintf(stderr, "Usage: %s [options]\n", name);
127 fprintf(stderr, "Where valid options are:\n");
128
129 for (i = 0; myopts[i].opt; i++)
130 {
131 fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR,
132 myopts[i].opt, (myopts[i].argtype == YESNO
133 || myopts[i].argtype ==
134 USAGE) ? "" : myopts[i].argtype ==
135 INTEGER ? "<number>" : "<string>", myopts[i].desc);
136 }
137
138 exit(EXIT_FAILURE);
139 }