ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/getopt.c
Revision: 6381
Committed: Fri Aug 21 17:20:11 2015 UTC (10 years ago) by michael
Content type: text/x-csrc
File size: 3367 byte(s)
Log Message:
- getopt.c: fixed inconsistent use of 'error' vs. 'Error'

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 5347 * Copyright (c) 2001-2015 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     unsigned int i;
38    
39     fprintf(stderr, "Usage: %s [options]\n", name);
40     fprintf(stderr, "Where valid options are:\n");
41    
42     for (i = 0; opts[i].opt; ++i)
43     fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, opts[i].opt,
44     (opts[i].argtype == YESNO || opts[i].argtype == USAGE) ? "" :
45     opts[i].argtype == INTEGER ? "<number>" : "<string>",
46     opts[i].desc);
47    
48     exit(EXIT_FAILURE);
49     }
50    
51 adx 30 void
52     parseargs(int *argc, char ***argv, struct lgetopt *opts)
53     {
54 michael 1357 const char *progname = (*argv)[0];
55 adx 30
56 michael 3250 /* Loop through each argument */
57 michael 1357 while (1)
58     {
59     int found = 0;
60 michael 4470 const char *opt = NULL;
61 michael 1357
62     (*argc)--;
63     (*argv)++;
64    
65     if (*argc < 1 || (*argv)[0][0] != OPTCHAR)
66     return;
67    
68 michael 4470 opt = &(*argv)[0][1];
69 michael 1357
70 michael 3250 /* Search through our argument list, and see if it matches */
71     for (unsigned int i = 0; opts[i].opt; ++i)
72 adx 30 {
73 michael 4470 if (!strcmp(opts[i].opt, opt))
74 michael 1357 {
75 michael 3250 /* Found our argument */
76 michael 1357 found = 1;
77 adx 30
78 michael 1357 switch (opts[i].argtype)
79     {
80     case YESNO:
81     *((int *)opts[i].argloc) = 1;
82     break;
83 adx 30
84 michael 1357 case INTEGER:
85     if (*argc < 2)
86     {
87     fprintf(stderr, "Error: option '%c%s' requires an argument\n",
88     OPTCHAR, opts[i].opt);
89     usage((*argv)[0], opts);
90     }
91 michael 2916
92 michael 1357 *((int *)opts[i].argloc) = atoi((*argv)[1]);
93 michael 755 (*argc)--;
94     (*argv)++;
95 michael 1357 break;
96    
97     case STRING:
98     if (*argc < 2)
99     {
100 michael 6381 fprintf(stderr, "Error: option '%c%s' requires an argument\n",
101 michael 1357 OPTCHAR, opts[i].opt);
102     usage(progname, opts);
103     }
104    
105 michael 4470 *((char **)opts[i].argloc) = xstrdup((*argv)[1]);
106 michael 755 (*argc)--;
107     (*argv)++;
108 michael 1357 break;
109 adx 30
110 michael 1357 case USAGE:
111     usage(progname, opts);
112     /* NOTREACHED */
113 adx 30
114 michael 1357 default:
115     fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n",
116     __FILE__, __LINE__);
117     exit(EXIT_FAILURE);
118     }
119     }
120 adx 30 }
121    
122 michael 1357 if (!found)
123     {
124 michael 6381 fprintf(stderr, "Error: unknown argument '%c%s'\n",
125 michael 4470 OPTCHAR, opt);
126 michael 1357 usage(progname, opts);
127     }
128 adx 30 }
129     }

Properties

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