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

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 2001-2014 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     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * 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    
30     #define OPTCHAR '-'
31    
32    
33 michael 1357 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 adx 30 void
51     parseargs(int *argc, char ***argv, struct lgetopt *opts)
52     {
53 michael 1357 unsigned int i;
54     const char *progname = (*argv)[0];
55 adx 30
56     /* loop through each argument */
57 michael 1357 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 michael 2916 for (i = 0; opts[i].opt; i++)
71 adx 30 {
72 michael 1357 if (!strcmp(opts[i].opt, (*argv)[0]))
73     {
74     /* found our argument */
75     found = 1;
76 adx 30
77 michael 1357 switch (opts[i].argtype)
78     {
79     case YESNO:
80     *((int *)opts[i].argloc) = 1;
81     break;
82 adx 30
83 michael 1357 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 michael 2916
91 michael 1357 *((int *)opts[i].argloc) = atoi((*argv)[1]);
92 michael 755 (*argc)--;
93     (*argv)++;
94 michael 1357 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 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     fprintf(stderr, "error: unknown argument '%c%s'\n",
125     OPTCHAR, (*argv)[0]);
126     usage(progname, opts);
127     }
128 adx 30 }
129     }

Properties

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