ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-7.2/src/getopt.c
Revision: 34
Committed: Sun Oct 2 21:05:51 2005 UTC (18 years, 5 months ago) by lusky
Content type: text/x-csrc
File size: 3095 byte(s)
Log Message:
create 7.2 branch, we can move/rename it as needed.


File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * getopt.c: Uses getopt to fetch the command line options.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 #include "stdinc.h"
26 #include "ircd_getopt.h"
27
28 #define OPTCHAR '-'
29
30 static void usage(const char *name);
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, "Error: option '%c%s' requires an argument\n",
76 OPTCHAR, opts[i].opt);
77 usage((*argv)[0]);
78 }
79
80 *((int *)opts[i].argloc) = atoi((*argv)[1]);
81 break;
82 case STRING:
83 if (*argc < 2)
84 {
85 fprintf(stderr, "error: option '%c%s' requires an argument\n",
86 OPTCHAR, opts[i].opt);
87 usage(progname);
88 }
89
90 *((char**)opts[i].argloc) = malloc(strlen((*argv)[1]) + 1);
91 strcpy(*((char**)opts[i].argloc), (*argv)[1]);
92 break;
93
94 case USAGE:
95 usage(progname);
96 /*NOTREACHED*/
97
98 default:
99 fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n",
100 __FILE__, __LINE__);
101 exit(EXIT_FAILURE);
102 }
103 }
104 }
105 if (!found)
106 {
107 fprintf(stderr, "error: unknown argument '%c%s'\n", OPTCHAR, (*argv)[0]);
108 usage(progname);
109 }
110 }
111 }
112
113 static void
114 usage(const char *name)
115 {
116 int i;
117
118 fprintf(stderr, "Usage: %s [options]\n", name);
119 fprintf(stderr, "Where valid options are:\n");
120
121 for (i = 0; myopts[i].opt; i++)
122 {
123 fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, myopts[i].opt,
124 (myopts[i].argtype == YESNO || myopts[i].argtype == USAGE) ? "" :
125 myopts[i].argtype == INTEGER ? "<number>" : "<string>",
126 myopts[i].desc);
127 }
128
129 exit(EXIT_FAILURE);
130 }
131

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision