ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/getopt.c
Revision: 2916
Committed: Sat Jan 25 21:09:18 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 3395 byte(s)
Log Message:
- Clean up all files in include/ (fixed indentation, removed whitespaces/tabs)
- Fixed copyright years

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 2001-2014 ircd-hybrid development team
5 *
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 /*! \file getopt.c
23 * \brief Uses getopt to fetch the command line options.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "ircd_getopt.h"
29
30 #define OPTCHAR '-'
31
32
33 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 void
51 parseargs(int *argc, char ***argv, struct lgetopt *opts)
52 {
53 unsigned int i;
54 const char *progname = (*argv)[0];
55
56 /* loop through each argument */
57 while (1)
58 {
59 int found = 0;
60
61 (*argc)--;
62 (*argv)++;
63
64 if (*argc < 1 || (*argv)[0][0] != OPTCHAR)
65 return;
66
67 (*argv)[0]++;
68
69 /* search through our argument list, and see if it matches */
70 for (i = 0; opts[i].opt; i++)
71 {
72 if (!strcmp(opts[i].opt, (*argv)[0]))
73 {
74 /* found our argument */
75 found = 1;
76
77 switch (opts[i].argtype)
78 {
79 case YESNO:
80 *((int *)opts[i].argloc) = 1;
81 break;
82
83 case INTEGER:
84 if (*argc < 2)
85 {
86 fprintf(stderr, "Error: option '%c%s' requires an argument\n",
87 OPTCHAR, opts[i].opt);
88 usage((*argv)[0], opts);
89 }
90
91 *((int *)opts[i].argloc) = atoi((*argv)[1]);
92 (*argc)--;
93 (*argv)++;
94 break;
95
96 case STRING:
97 if (*argc < 2)
98 {
99 fprintf(stderr, "error: option '%c%s' requires an argument\n",
100 OPTCHAR, opts[i].opt);
101 usage(progname, opts);
102 }
103
104 *((char**)opts[i].argloc) = malloc(strlen((*argv)[1]) + 1);
105 strcpy(*((char**)opts[i].argloc), (*argv)[1]);
106 (*argc)--;
107 (*argv)++;
108 break;
109
110 case USAGE:
111 usage(progname, opts);
112 /* NOTREACHED */
113
114 default:
115 fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n",
116 __FILE__, __LINE__);
117 exit(EXIT_FAILURE);
118 }
119 }
120 }
121
122 if (!found)
123 {
124 fprintf(stderr, "error: unknown argument '%c%s'\n",
125 OPTCHAR, (*argv)[0]);
126 usage(progname, opts);
127 }
128 }
129 }

Properties

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