Debian Codegraph @

src/getlopt.c

raw
1 /*
2 getlopt: command line option/parameter parsing
3
4 copyright ?-2008 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written Oliver Fromme
7 old timestamp: Tue Apr 8 07:15:13 MET DST 1997
8 */
9
10 #include "config.h"
11 #include "compat/compat.h"
12 #include "getlopt.h"
13 #include "common/debug.h"
14
15 int loptind = 1; /* index in argv[] */
16 int loptchr = 0; /* index in argv[loptind] */
17 char *loptarg; /* points to argument if present, else to option */
18
19 topt *findopt (int islong, char *opt, topt *opts)
20 {
21 if (!opts)
22 return (0);
23 while (opts->lname) {
24 if (islong) {
25 if (!strcmp(opts->lname, opt))
26 return (opts);
27 }
28 else
29 if (opts->sname == *opt)
30 return (opts);
31 opts++;
32 }
33 return (0);
34 }
35
36 static void setcharoption(topt *opt, char *value)
37 {
38 if(!opt->var)
39 {
40 merror("Option %s has no argument pointer!", opt->lname);
41 return;
42 }
43 if(opt->flags & GLO_VAR_MEM)
44 free(*((char**)opt->var));
45 if(value)
46 {
47 *((char **) opt->var) = INT123_compat_strdup(value);
48 opt->flags |= GLO_VAR_MEM;
49 } else
50 {
51 *((char **) opt->var) = NULL;
52 opt->flags &= ~GLO_VAR_MEM;
53 }
54 }
55
56 void getlopt_set_char(topt *opts, char *name, char *value)
57 {
58 topt *opt = findopt(1, name, opts);
59 if(!opt)
60 return;
61 setcharoption(opt, value);
62 }
63
64 static int performoption (int argc, char *argv[], topt *opt, topt *opts)
65 {
66 int result = GLO_CONTINUE;
67 /* this really is not supposed to happen, so the exit may be justified to create asap ficing pressure */
68 #define prog_error() \
69 { \
70 fprintf(stderr, __FILE__ ":%i Option without type flag! This is a programming error! Developer: fix this ASAP to regain your honor.\n", __LINE__); \
71 exit(1); \
72 }
73
74 debug2("performoption on %c / %s"
75 , opt->sname ? opt->sname : '_', opt->lname ? opt->lname : "");
76 if (!(opt->flags & GLO_ARG)) { /* doesn't take argument */
77 if (opt->var) {
78 if (opt->flags & GLO_CHAR) /* var is *char */
79 {
80 debug1("char at %p", opt->var);
81 *((char *) opt->var) = (char) opt->value;
82 }
83 else if(opt->flags & GLO_LONG)
84 {
85 debug1("long at %p", opt->var);
86 *( (long *) opt->var ) = opt->value;
87 }
88 else if(opt->flags & GLO_INT)
89 {
90 debug1("int at %p", opt->var);
91 *( (int *) opt->var ) = (int) opt->value;
92 }
93 /* GLO_DOUBLE is not supported here */
94 else prog_error();
95
96 debug("casting assignment done");
97 }
98 #if 0 /* Oliver: What was this for?! --ThOr */
99 else
100 result = opt->value ? opt->value : opt->sname;
101 #endif
102 }
103 else { /* requires argument */
104 debug("argument required");
105 if (loptind >= argc)
106 return (GLO_NOARG);
107 loptarg = argv[loptind++]+loptchr;
108 loptchr = 0;
109 errno = 0;
110 if (opt->var) {
111 char *endptr = NULL;
112 if (opt->flags & GLO_CHAR) /* var is *char */
113 setcharoption(opt, loptarg);
114 else if(opt->flags & (GLO_LONG | GLO_INT))
115 {
116 long val = strtol(loptarg, &endptr, 10);
117 if(errno || endptr == loptarg || (endptr && *endptr))
118 return GLO_BADARG;
119 if(opt->flags & GLO_LONG)
120 *((long *) opt->var) = val;
121 else if(val <= INT_MAX && val >= INT_MIN)
122 *((int *) opt->var) = val;
123 else
124 return GLO_BADARG;
125 }
126 else if(opt->flags & GLO_DOUBLE)
127 {
128 *((double *) opt->var) = strtod(loptarg, &endptr);
129 if(errno || endptr == loptarg || (endptr && *endptr))
130 return GLO_BADARG;
131 }
132 else prog_error();
133 }
134 #if 0 /* Oliver: What was this for?! --ThOr */
135 else
136 result = opt->value ? opt->value : opt->sname;
137 #endif
138 }
139 if (opt->func)
140 opt->func(loptarg, opts);
141 debug4("result: %i (%p, %li, %i)", result, opt->var, opt->value, opt->sname);
142 return (result);
143 }
144
145 int getsingleopt (int argc, char *argv[], topt *opts)
146 {
147 char *thisopt;
148 topt *opt;
149 static char shortopt[2] = {0, 0};
150
151 if (loptind >= argc)
152 return (GLO_END);
153 thisopt = argv[loptind];
154 debug1("getsingleopt: %s", thisopt);
155 if (!loptchr) { /* start new option string */
156 if (thisopt[0] != '-' || !thisopt[1]) /* no more options */
157 return (GLO_END);
158 if (thisopt[1] == '-') { /* "--" */
159 if (thisopt[2]) { /* long option */
160 loptarg = thisopt+2;
161 loptind++;
162 if (!(opt = findopt(1, thisopt+2, opts)))
163 return (GLO_UNKNOWN);
164 else
165 return (performoption(argc, argv, opt, opts));
166 }
167 else { /* "--" == end of options */
168 loptind++;
169 return (GLO_END);
170 }
171 }
172 else /* start short option(s) */
173 loptchr = 1;
174 }
175 shortopt[0] = thisopt[loptchr];
176 loptarg = shortopt;
177 opt = findopt(0, thisopt+(loptchr++), opts);
178 if (!thisopt[loptchr]) {
179 loptind++;
180 loptchr = 0;
181 }
182 if (!opt)
183 return (GLO_UNKNOWN);
184 else
185 return (performoption(argc, argv, opt, opts));
186 }
187
188 int getlopt (int argc, char *argv[], topt *opts)
189 {
190
191 int result;
192
193 while ((result = getsingleopt(argc, argv, opts)) == GLO_CONTINUE);
194 return (result);
195 }
196
197 /* EOF */