ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/getopt.c
Revision: 1592
Committed: Sat Oct 27 21:02:32 2012 UTC (12 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 3377 byte(s)
Log Message:
- Second time's the charm? Moving svnroot/ircd-hybrid-8 to
  svnroot/ircd-hybrid/trunk

File Contents

# User Rev Content
1 adx 30 /*
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 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26     #include "ircd_getopt.h"
27    
28     #define OPTCHAR '-'
29    
30    
31 michael 1357 static void
32     usage(const char *name, const struct lgetopt *opts)
33     {
34     unsigned int i;
35    
36     fprintf(stderr, "Usage: %s [options]\n", name);
37     fprintf(stderr, "Where valid options are:\n");
38    
39     for (i = 0; opts[i].opt; ++i)
40     fprintf(stderr, "\t%c%-10s %-20s%s\n", OPTCHAR, opts[i].opt,
41     (opts[i].argtype == YESNO || opts[i].argtype == USAGE) ? "" :
42     opts[i].argtype == INTEGER ? "<number>" : "<string>",
43     opts[i].desc);
44    
45     exit(EXIT_FAILURE);
46     }
47    
48 adx 30 void
49     parseargs(int *argc, char ***argv, struct lgetopt *opts)
50     {
51 michael 1357 unsigned int i;
52     const char *progname = (*argv)[0];
53 adx 30
54     /* loop through each argument */
55 michael 1357 while (1)
56     {
57     int found = 0;
58    
59     (*argc)--;
60     (*argv)++;
61    
62     if (*argc < 1 || (*argv)[0][0] != OPTCHAR)
63     return;
64    
65     (*argv)[0]++;
66    
67     /* search through our argument list, and see if it matches */
68     for (i = 0; opts[i].opt; i++)
69 adx 30 {
70 michael 1357 if (!strcmp(opts[i].opt, (*argv)[0]))
71     {
72     /* found our argument */
73     found = 1;
74 adx 30
75 michael 1357 switch (opts[i].argtype)
76     {
77     case YESNO:
78     *((int *)opts[i].argloc) = 1;
79     break;
80 adx 30
81 michael 1357 case INTEGER:
82     if (*argc < 2)
83     {
84     fprintf(stderr, "Error: option '%c%s' requires an argument\n",
85     OPTCHAR, opts[i].opt);
86     usage((*argv)[0], opts);
87     }
88 adx 30
89 michael 1357 *((int *)opts[i].argloc) = atoi((*argv)[1]);
90 michael 755 (*argc)--;
91     (*argv)++;
92 michael 1357 break;
93    
94     case STRING:
95     if (*argc < 2)
96     {
97     fprintf(stderr, "error: option '%c%s' requires an argument\n",
98     OPTCHAR, opts[i].opt);
99     usage(progname, opts);
100     }
101    
102     *((char**)opts[i].argloc) = malloc(strlen((*argv)[1]) + 1);
103     strcpy(*((char**)opts[i].argloc), (*argv)[1]);
104 michael 755 (*argc)--;
105     (*argv)++;
106 michael 1357 break;
107 adx 30
108 michael 1357 case USAGE:
109     usage(progname, opts);
110     /* NOTREACHED */
111 adx 30
112 michael 1357 default:
113     fprintf(stderr, "Error: internal error in parseargs() at %s:%d\n",
114     __FILE__, __LINE__);
115     exit(EXIT_FAILURE);
116     }
117     }
118 adx 30 }
119    
120 michael 1357 if (!found)
121     {
122     fprintf(stderr, "error: unknown argument '%c%s'\n",
123     OPTCHAR, (*argv)[0]);
124     usage(progname, opts);
125     }
126 adx 30 }
127     }

Properties

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