blob: d7be13a632d146a6853dda079080202130cc69b0 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
Harald Weltef6214982017-09-24 10:23:24 +08002 File autogenerated by gengetopt version 2.22.6
jjako52c24142002-12-16 13:33:51 +00003 generated with the following command:
jjako1a51df72004-07-20 08:30:21 +00004 gengetopt --conf-parser
jjako52c24142002-12-16 13:33:51 +00005
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9*/
10
jjako52c24142002-12-16 13:33:51 +000011/* If we use autoconf. */
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
jjako52c24142002-12-16 13:33:51 +000015
Harald Welte1b3e5772010-05-04 11:13:56 +020016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
Harald Weltef6214982017-09-24 10:23:24 +080020#ifndef FIX_UNUSED
21#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22#endif
23
24#include <getopt.h>
jjako52c24142002-12-16 13:33:51 +000025
26#include "cmdline.h"
27
Harald Welte1b3e5772010-05-04 11:13:56 +020028const char *gengetopt_args_info_purpose = "";
jjako52c24142002-12-16 13:33:51 +000029
Harald Welte7e1175f2017-09-24 10:53:53 +080030const char *gengetopt_args_info_usage = "Usage: sgsnemu [OPTIONS]...";
jjako52c24142002-12-16 13:33:51 +000031
Harald Weltef6214982017-09-24 10:23:24 +080032const char *gengetopt_args_info_versiontext = "";
33
34const char *gengetopt_args_info_description = "";
35
Harald Welte1b3e5772010-05-04 11:13:56 +020036const char *gengetopt_args_info_help[] = {
Harald Weltef6214982017-09-24 10:23:24 +080037 " -h, --help Print help and exit",
38 " -V, --version Print version and exit",
39 " -d, --debug Run in debug mode (default=off)",
40 " -c, --conf=STRING Read configuration file",
41 " --pidfile=STRING Filename of process id file\n (default=`./sgsnemu.pid')",
42 " --statedir=STRING Directory of nonvolatile data (default=`./')",
43 " --dns=STRING DNS Server to use",
44 " -l, --listen=STRING Local interface",
45 " -r, --remote=STRING Remote host",
46 " --contexts=INT Number of contexts (default=`1')",
47 " --timelimit=INT Exit after timelimit seconds (default=`0')",
48 " --gtpversion=INT GTP version to use (default=`1')",
49 " -a, --apn=STRING Access point name (default=`internet')",
50 " --selmode=INT Selection mode (default=`0x01')",
51 " --rattype=1..5 Radio Access Technology Type (default=`1')",
52 " --userloc=type.MCC.MNC.LAC.CIorSACorRAC\n User Location Information\n (default=`02509946241207')",
53 " --rai=MCC.MNC.LAC.RAC Routing Area Information\n (default=`02509946241207')",
54 " --mstz=sign.NbQuartersOfAnHour.DSTAdjustment\n MS Time Zone (default=`0')",
55 " --imeisv=STRING IMEI(SV) International Mobile Equipment\n Identity (and Software Version)\n (default=`2143658709214365')",
56 " --norecovery Do not send recovery (default=off)",
57 " -i, --imsi=STRING IMSI (default=`240010123456789')",
58 " --nsapi=INT NSAPI (default=`0')",
59 " -m, --msisdn=STRING Mobile Station ISDN number\n (default=`46702123456')",
60 " -q, --qos=INT Requested quality of service\n (default=`0x000b921f')",
61 " --qose1=INT Requested quality of service Extension 1\n (default=`0x9396404074f9ffff')",
62 " --qose2=INT Requested quality of service Extension 2\n (default=`0x11')",
63 " --qose3=INT Requested quality of service Extension 3\n (default=`0x0101')",
64 " --qose4=INT Requested quality of service Extension 4\n (default=`0x4040')",
65 " --charging=INT Charging characteristics (default=`0x0800')",
66 " -u, --uid=STRING Login user ID (default=`mig')",
67 " -p, --pwd=STRING Login password (default=`hemmelig')",
68 " --createif Create local network interface (default=off)",
69 " -n, --net=STRING Network address for local interface",
70 " --defaultroute Create default route (default=off)",
71 " --ipup=STRING Script to run after link-up",
72 " --ipdown=STRING Script to run after link-down",
73 " --pinghost=STRING Ping remote host",
74 " --pingrate=INT Number of ping req per second (default=`1')",
75 " --pingsize=INT Number of ping data bytes (default=`56')",
76 " --pingcount=INT Number of ping req to send (default=`0')",
77 " --pingquiet Do not print ping packet info (default=off)",
Harald Weltebed35df2011-11-02 13:06:18 +010078 0
Harald Welte1b3e5772010-05-04 11:13:56 +020079};
80
Harald Weltef6214982017-09-24 10:23:24 +080081typedef enum { ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT
82} cmdline_parser_arg_type;
83
Harald Welte1b3e5772010-05-04 11:13:56 +020084static
Harald Weltebed35df2011-11-02 13:06:18 +010085void clear_given(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020086static
Harald Weltebed35df2011-11-02 13:06:18 +010087void clear_args(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020088
89static int
Harald Weltef6214982017-09-24 10:23:24 +080090cmdline_parser_internal(int argc, char **argv,
91 struct gengetopt_args_info *args_info,
92 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +010093 const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020094
Harald Weltebed35df2011-11-02 13:06:18 +010095struct line_list {
96 char *string_arg;
97 struct line_list *next;
Harald Welte1b3e5772010-05-04 11:13:56 +020098};
99
100static struct line_list *cmd_line_list = 0;
101static struct line_list *cmd_line_list_tmp = 0;
102
Harald Weltebed35df2011-11-02 13:06:18 +0100103static void free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200104{
Harald Weltebed35df2011-11-02 13:06:18 +0100105 /* free the list of a previous call */
106 if (cmd_line_list) {
107 while (cmd_line_list) {
108 cmd_line_list_tmp = cmd_line_list;
109 cmd_line_list = cmd_line_list->next;
110 free(cmd_line_list_tmp->string_arg);
111 free(cmd_line_list_tmp);
112 }
113 }
jjako52c24142002-12-16 13:33:51 +0000114}
115
Harald Weltebed35df2011-11-02 13:06:18 +0100116static char *gengetopt_strdup(const char *s);
jjako52c24142002-12-16 13:33:51 +0000117
Harald Welte1b3e5772010-05-04 11:13:56 +0200118static
Harald Weltebed35df2011-11-02 13:06:18 +0100119void clear_given(struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000120{
Harald Weltebed35df2011-11-02 13:06:18 +0100121 args_info->help_given = 0;
122 args_info->version_given = 0;
123 args_info->debug_given = 0;
124 args_info->conf_given = 0;
125 args_info->pidfile_given = 0;
126 args_info->statedir_given = 0;
127 args_info->dns_given = 0;
128 args_info->listen_given = 0;
129 args_info->remote_given = 0;
130 args_info->contexts_given = 0;
131 args_info->timelimit_given = 0;
132 args_info->gtpversion_given = 0;
133 args_info->apn_given = 0;
134 args_info->selmode_given = 0;
135 args_info->rattype_given = 0;
136 args_info->userloc_given = 0;
137 args_info->rai_given = 0;
138 args_info->mstz_given = 0;
139 args_info->imeisv_given = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800140 args_info->norecovery_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100141 args_info->imsi_given = 0;
142 args_info->nsapi_given = 0;
143 args_info->msisdn_given = 0;
144 args_info->qos_given = 0;
145 args_info->qose1_given = 0;
146 args_info->qose2_given = 0;
147 args_info->qose3_given = 0;
148 args_info->qose4_given = 0;
149 args_info->charging_given = 0;
150 args_info->uid_given = 0;
151 args_info->pwd_given = 0;
152 args_info->createif_given = 0;
153 args_info->net_given = 0;
154 args_info->defaultroute_given = 0;
155 args_info->ipup_given = 0;
156 args_info->ipdown_given = 0;
157 args_info->pinghost_given = 0;
158 args_info->pingrate_given = 0;
159 args_info->pingsize_given = 0;
160 args_info->pingcount_given = 0;
161 args_info->pingquiet_given = 0;
jjako52c24142002-12-16 13:33:51 +0000162}
163
Harald Welte1b3e5772010-05-04 11:13:56 +0200164static
Harald Weltebed35df2011-11-02 13:06:18 +0100165void clear_args(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200166{
Harald Weltef6214982017-09-24 10:23:24 +0800167 FIX_UNUSED(args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100168 args_info->debug_flag = 0;
169 args_info->conf_arg = NULL;
170 args_info->conf_orig = NULL;
171 args_info->pidfile_arg = gengetopt_strdup("./sgsnemu.pid");
172 args_info->pidfile_orig = NULL;
173 args_info->statedir_arg = gengetopt_strdup("./");
174 args_info->statedir_orig = NULL;
175 args_info->dns_arg = NULL;
176 args_info->dns_orig = NULL;
177 args_info->listen_arg = NULL;
178 args_info->listen_orig = NULL;
179 args_info->remote_arg = NULL;
180 args_info->remote_orig = NULL;
181 args_info->contexts_arg = 1;
182 args_info->contexts_orig = NULL;
183 args_info->timelimit_arg = 0;
184 args_info->timelimit_orig = NULL;
185 args_info->gtpversion_arg = 1;
186 args_info->gtpversion_orig = NULL;
187 args_info->apn_arg = gengetopt_strdup("internet");
188 args_info->apn_orig = NULL;
189 args_info->selmode_arg = 0x01;
190 args_info->selmode_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800191 args_info->rattype_arg = 1;
Harald Weltebed35df2011-11-02 13:06:18 +0100192 args_info->rattype_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800193 args_info->userloc_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100194 args_info->userloc_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800195 args_info->rai_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100196 args_info->rai_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800197 args_info->mstz_arg = gengetopt_strdup("0");
Harald Weltebed35df2011-11-02 13:06:18 +0100198 args_info->mstz_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800199 args_info->imeisv_arg = gengetopt_strdup("2143658709214365");
Harald Weltebed35df2011-11-02 13:06:18 +0100200 args_info->imeisv_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800201 args_info->norecovery_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100202 args_info->imsi_arg = gengetopt_strdup("240010123456789");
203 args_info->imsi_orig = NULL;
204 args_info->nsapi_arg = 0;
205 args_info->nsapi_orig = NULL;
206 args_info->msisdn_arg = gengetopt_strdup("46702123456");
207 args_info->msisdn_orig = NULL;
208 args_info->qos_arg = 0x000b921f;
209 args_info->qos_orig = NULL;
210 args_info->qose1_arg = 0x9396404074f9ffff;
211 args_info->qose1_orig = NULL;
212 args_info->qose2_arg = 0x11;
213 args_info->qose2_orig = NULL;
214 args_info->qose3_arg = 0x0101;
215 args_info->qose3_orig = NULL;
216 args_info->qose4_arg = 0x4040;
217 args_info->qose4_orig = NULL;
218 args_info->charging_arg = 0x0800;
219 args_info->charging_orig = NULL;
220 args_info->uid_arg = gengetopt_strdup("mig");
221 args_info->uid_orig = NULL;
222 args_info->pwd_arg = gengetopt_strdup("hemmelig");
223 args_info->pwd_orig = NULL;
224 args_info->createif_flag = 0;
225 args_info->net_arg = NULL;
226 args_info->net_orig = NULL;
227 args_info->defaultroute_flag = 0;
228 args_info->ipup_arg = NULL;
229 args_info->ipup_orig = NULL;
230 args_info->ipdown_arg = NULL;
231 args_info->ipdown_orig = NULL;
232 args_info->pinghost_arg = NULL;
233 args_info->pinghost_orig = NULL;
234 args_info->pingrate_arg = 1;
235 args_info->pingrate_orig = NULL;
236 args_info->pingsize_arg = 56;
237 args_info->pingsize_orig = NULL;
238 args_info->pingcount_arg = 0;
239 args_info->pingcount_orig = NULL;
240 args_info->pingquiet_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100241
Harald Welte1b3e5772010-05-04 11:13:56 +0200242}
243
244static
245void init_args_info(struct gengetopt_args_info *args_info)
246{
Harald Weltef6214982017-09-24 10:23:24 +0800247
Harald Weltebed35df2011-11-02 13:06:18 +0100248 args_info->help_help = gengetopt_args_info_help[0];
249 args_info->version_help = gengetopt_args_info_help[1];
250 args_info->debug_help = gengetopt_args_info_help[2];
251 args_info->conf_help = gengetopt_args_info_help[3];
252 args_info->pidfile_help = gengetopt_args_info_help[4];
253 args_info->statedir_help = gengetopt_args_info_help[5];
254 args_info->dns_help = gengetopt_args_info_help[6];
255 args_info->listen_help = gengetopt_args_info_help[7];
256 args_info->remote_help = gengetopt_args_info_help[8];
257 args_info->contexts_help = gengetopt_args_info_help[9];
258 args_info->timelimit_help = gengetopt_args_info_help[10];
259 args_info->gtpversion_help = gengetopt_args_info_help[11];
260 args_info->apn_help = gengetopt_args_info_help[12];
261 args_info->selmode_help = gengetopt_args_info_help[13];
Harald Weltef6214982017-09-24 10:23:24 +0800262 args_info->rattype_help = gengetopt_args_info_help[14];
263 args_info->userloc_help = gengetopt_args_info_help[15];
264 args_info->rai_help = gengetopt_args_info_help[16];
265 args_info->mstz_help = gengetopt_args_info_help[17];
266 args_info->imeisv_help = gengetopt_args_info_help[18];
267 args_info->norecovery_help = gengetopt_args_info_help[19];
268 args_info->imsi_help = gengetopt_args_info_help[20];
269 args_info->nsapi_help = gengetopt_args_info_help[21];
270 args_info->msisdn_help = gengetopt_args_info_help[22];
271 args_info->qos_help = gengetopt_args_info_help[23];
272 args_info->qose1_help = gengetopt_args_info_help[24];
273 args_info->qose2_help = gengetopt_args_info_help[25];
274 args_info->qose3_help = gengetopt_args_info_help[26];
275 args_info->qose4_help = gengetopt_args_info_help[27];
276 args_info->charging_help = gengetopt_args_info_help[28];
277 args_info->uid_help = gengetopt_args_info_help[29];
278 args_info->pwd_help = gengetopt_args_info_help[30];
279 args_info->createif_help = gengetopt_args_info_help[31];
280 args_info->net_help = gengetopt_args_info_help[32];
281 args_info->defaultroute_help = gengetopt_args_info_help[33];
282 args_info->ipup_help = gengetopt_args_info_help[34];
283 args_info->ipdown_help = gengetopt_args_info_help[35];
284 args_info->pinghost_help = gengetopt_args_info_help[36];
285 args_info->pingrate_help = gengetopt_args_info_help[37];
286 args_info->pingsize_help = gengetopt_args_info_help[38];
287 args_info->pingcount_help = gengetopt_args_info_help[39];
288 args_info->pingquiet_help = gengetopt_args_info_help[40];
Harald Weltebed35df2011-11-02 13:06:18 +0100289
Harald Welte1b3e5772010-05-04 11:13:56 +0200290}
291
Harald Weltebed35df2011-11-02 13:06:18 +0100292void cmdline_parser_print_version(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200293{
Harald Weltef6214982017-09-24 10:23:24 +0800294 printf("%s %s\n",
295 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ?
296 CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
297 CMDLINE_PARSER_VERSION);
298
299 if (strlen(gengetopt_args_info_versiontext) > 0)
300 printf("\n%s\n", gengetopt_args_info_versiontext);
Harald Welte1b3e5772010-05-04 11:13:56 +0200301}
302
Harald Weltef6214982017-09-24 10:23:24 +0800303static void print_help_common(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200304{
Harald Weltebed35df2011-11-02 13:06:18 +0100305 cmdline_parser_print_version();
Harald Welte1b3e5772010-05-04 11:13:56 +0200306
Harald Weltebed35df2011-11-02 13:06:18 +0100307 if (strlen(gengetopt_args_info_purpose) > 0)
308 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200309
Harald Weltef6214982017-09-24 10:23:24 +0800310 if (strlen(gengetopt_args_info_usage) > 0)
311 printf("\n%s\n", gengetopt_args_info_usage);
312
313 printf("\n");
314
315 if (strlen(gengetopt_args_info_description) > 0)
316 printf("%s\n\n", gengetopt_args_info_description);
317}
318
319void cmdline_parser_print_help(void)
320{
321 int i = 0;
322 print_help_common();
Harald Weltebed35df2011-11-02 13:06:18 +0100323 while (gengetopt_args_info_help[i])
324 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200325}
326
Harald Weltebed35df2011-11-02 13:06:18 +0100327void cmdline_parser_init(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200328{
Harald Weltebed35df2011-11-02 13:06:18 +0100329 clear_given(args_info);
330 clear_args(args_info);
331 init_args_info(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200332}
333
Harald Weltef6214982017-09-24 10:23:24 +0800334void cmdline_parser_params_init(struct cmdline_parser_params *params)
335{
336 if (params) {
337 params->override = 0;
338 params->initialize = 1;
339 params->check_required = 1;
340 params->check_ambiguity = 0;
341 params->print_errors = 1;
342 }
343}
344
345struct cmdline_parser_params *cmdline_parser_params_create(void)
346{
347 struct cmdline_parser_params *params =
348 (struct cmdline_parser_params *)
349 malloc(sizeof(struct cmdline_parser_params));
350 cmdline_parser_params_init(params);
351 return params;
352}
353
354static void free_string_field(char **s)
355{
356 if (*s) {
357 free(*s);
358 *s = 0;
359 }
360}
361
Harald Weltebed35df2011-11-02 13:06:18 +0100362static void cmdline_parser_release(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200363{
Harald Weltebed35df2011-11-02 13:06:18 +0100364
Harald Weltef6214982017-09-24 10:23:24 +0800365 free_string_field(&(args_info->conf_arg));
366 free_string_field(&(args_info->conf_orig));
367 free_string_field(&(args_info->pidfile_arg));
368 free_string_field(&(args_info->pidfile_orig));
369 free_string_field(&(args_info->statedir_arg));
370 free_string_field(&(args_info->statedir_orig));
371 free_string_field(&(args_info->dns_arg));
372 free_string_field(&(args_info->dns_orig));
373 free_string_field(&(args_info->listen_arg));
374 free_string_field(&(args_info->listen_orig));
375 free_string_field(&(args_info->remote_arg));
376 free_string_field(&(args_info->remote_orig));
377 free_string_field(&(args_info->contexts_orig));
378 free_string_field(&(args_info->timelimit_orig));
379 free_string_field(&(args_info->gtpversion_orig));
380 free_string_field(&(args_info->apn_arg));
381 free_string_field(&(args_info->apn_orig));
382 free_string_field(&(args_info->selmode_orig));
383 free_string_field(&(args_info->rattype_orig));
384 free_string_field(&(args_info->userloc_arg));
385 free_string_field(&(args_info->userloc_orig));
386 free_string_field(&(args_info->rai_arg));
387 free_string_field(&(args_info->rai_orig));
388 free_string_field(&(args_info->mstz_arg));
389 free_string_field(&(args_info->mstz_orig));
390 free_string_field(&(args_info->imeisv_arg));
391 free_string_field(&(args_info->imeisv_orig));
392 free_string_field(&(args_info->imsi_arg));
393 free_string_field(&(args_info->imsi_orig));
394 free_string_field(&(args_info->nsapi_orig));
395 free_string_field(&(args_info->msisdn_arg));
396 free_string_field(&(args_info->msisdn_orig));
397 free_string_field(&(args_info->qos_orig));
398 free_string_field(&(args_info->qose1_orig));
399 free_string_field(&(args_info->qose2_orig));
400 free_string_field(&(args_info->qose3_orig));
401 free_string_field(&(args_info->qose4_orig));
402 free_string_field(&(args_info->charging_orig));
403 free_string_field(&(args_info->uid_arg));
404 free_string_field(&(args_info->uid_orig));
405 free_string_field(&(args_info->pwd_arg));
406 free_string_field(&(args_info->pwd_orig));
407 free_string_field(&(args_info->net_arg));
408 free_string_field(&(args_info->net_orig));
409 free_string_field(&(args_info->ipup_arg));
410 free_string_field(&(args_info->ipup_orig));
411 free_string_field(&(args_info->ipdown_arg));
412 free_string_field(&(args_info->ipdown_orig));
413 free_string_field(&(args_info->pinghost_arg));
414 free_string_field(&(args_info->pinghost_orig));
415 free_string_field(&(args_info->pingrate_orig));
416 free_string_field(&(args_info->pingsize_orig));
417 free_string_field(&(args_info->pingcount_orig));
Harald Weltebed35df2011-11-02 13:06:18 +0100418
419 clear_given(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200420}
421
Harald Weltef6214982017-09-24 10:23:24 +0800422static void
423write_into_file(FILE * outfile, const char *opt, const char *arg,
424 const char *values[])
425{
426 FIX_UNUSED(values);
427 if (arg) {
428 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
429 } else {
430 fprintf(outfile, "%s\n", opt);
431 }
432}
433
434int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info)
435{
436 int i = 0;
437
438 if (!outfile) {
439 fprintf(stderr, "%s: cannot dump options to stream\n",
440 CMDLINE_PARSER_PACKAGE);
441 return EXIT_FAILURE;
442 }
443
444 if (args_info->help_given)
445 write_into_file(outfile, "help", 0, 0);
446 if (args_info->version_given)
447 write_into_file(outfile, "version", 0, 0);
448 if (args_info->debug_given)
449 write_into_file(outfile, "debug", 0, 0);
450 if (args_info->conf_given)
451 write_into_file(outfile, "conf", args_info->conf_orig, 0);
452 if (args_info->pidfile_given)
453 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
454 if (args_info->statedir_given)
455 write_into_file(outfile, "statedir", args_info->statedir_orig,
456 0);
457 if (args_info->dns_given)
458 write_into_file(outfile, "dns", args_info->dns_orig, 0);
459 if (args_info->listen_given)
460 write_into_file(outfile, "listen", args_info->listen_orig, 0);
461 if (args_info->remote_given)
462 write_into_file(outfile, "remote", args_info->remote_orig, 0);
463 if (args_info->contexts_given)
464 write_into_file(outfile, "contexts", args_info->contexts_orig,
465 0);
466 if (args_info->timelimit_given)
467 write_into_file(outfile, "timelimit", args_info->timelimit_orig,
468 0);
469 if (args_info->gtpversion_given)
470 write_into_file(outfile, "gtpversion",
471 args_info->gtpversion_orig, 0);
472 if (args_info->apn_given)
473 write_into_file(outfile, "apn", args_info->apn_orig, 0);
474 if (args_info->selmode_given)
475 write_into_file(outfile, "selmode", args_info->selmode_orig, 0);
476 if (args_info->rattype_given)
477 write_into_file(outfile, "rattype", args_info->rattype_orig, 0);
478 if (args_info->userloc_given)
479 write_into_file(outfile, "userloc", args_info->userloc_orig, 0);
480 if (args_info->rai_given)
481 write_into_file(outfile, "rai", args_info->rai_orig, 0);
482 if (args_info->mstz_given)
483 write_into_file(outfile, "mstz", args_info->mstz_orig, 0);
484 if (args_info->imeisv_given)
485 write_into_file(outfile, "imeisv", args_info->imeisv_orig, 0);
486 if (args_info->norecovery_given)
487 write_into_file(outfile, "norecovery", 0, 0);
488 if (args_info->imsi_given)
489 write_into_file(outfile, "imsi", args_info->imsi_orig, 0);
490 if (args_info->nsapi_given)
491 write_into_file(outfile, "nsapi", args_info->nsapi_orig, 0);
492 if (args_info->msisdn_given)
493 write_into_file(outfile, "msisdn", args_info->msisdn_orig, 0);
494 if (args_info->qos_given)
495 write_into_file(outfile, "qos", args_info->qos_orig, 0);
496 if (args_info->qose1_given)
497 write_into_file(outfile, "qose1", args_info->qose1_orig, 0);
498 if (args_info->qose2_given)
499 write_into_file(outfile, "qose2", args_info->qose2_orig, 0);
500 if (args_info->qose3_given)
501 write_into_file(outfile, "qose3", args_info->qose3_orig, 0);
502 if (args_info->qose4_given)
503 write_into_file(outfile, "qose4", args_info->qose4_orig, 0);
504 if (args_info->charging_given)
505 write_into_file(outfile, "charging", args_info->charging_orig,
506 0);
507 if (args_info->uid_given)
508 write_into_file(outfile, "uid", args_info->uid_orig, 0);
509 if (args_info->pwd_given)
510 write_into_file(outfile, "pwd", args_info->pwd_orig, 0);
511 if (args_info->createif_given)
512 write_into_file(outfile, "createif", 0, 0);
513 if (args_info->net_given)
514 write_into_file(outfile, "net", args_info->net_orig, 0);
515 if (args_info->defaultroute_given)
516 write_into_file(outfile, "defaultroute", 0, 0);
517 if (args_info->ipup_given)
518 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
519 if (args_info->ipdown_given)
520 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
521 if (args_info->pinghost_given)
522 write_into_file(outfile, "pinghost", args_info->pinghost_orig,
523 0);
524 if (args_info->pingrate_given)
525 write_into_file(outfile, "pingrate", args_info->pingrate_orig,
526 0);
527 if (args_info->pingsize_given)
528 write_into_file(outfile, "pingsize", args_info->pingsize_orig,
529 0);
530 if (args_info->pingcount_given)
531 write_into_file(outfile, "pingcount", args_info->pingcount_orig,
532 0);
533 if (args_info->pingquiet_given)
534 write_into_file(outfile, "pingquiet", 0, 0);
535
536 i = EXIT_SUCCESS;
537 return i;
538}
539
Harald Welte1b3e5772010-05-04 11:13:56 +0200540int
Harald Weltebed35df2011-11-02 13:06:18 +0100541cmdline_parser_file_save(const char *filename,
542 struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200543{
Harald Weltebed35df2011-11-02 13:06:18 +0100544 FILE *outfile;
545 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200546
Harald Weltebed35df2011-11-02 13:06:18 +0100547 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200548
Harald Weltebed35df2011-11-02 13:06:18 +0100549 if (!outfile) {
550 fprintf(stderr, "%s: cannot open file for writing: %s\n",
551 CMDLINE_PARSER_PACKAGE, filename);
552 return EXIT_FAILURE;
553 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200554
Harald Weltef6214982017-09-24 10:23:24 +0800555 i = cmdline_parser_dump(outfile, args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100556 fclose(outfile);
557
Harald Weltebed35df2011-11-02 13:06:18 +0100558 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200559}
560
Harald Weltebed35df2011-11-02 13:06:18 +0100561void cmdline_parser_free(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200562{
Harald Weltebed35df2011-11-02 13:06:18 +0100563 cmdline_parser_release(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200564}
565
Harald Weltef6214982017-09-24 10:23:24 +0800566/** @brief replacement of strdup, which is not standard */
Harald Weltebed35df2011-11-02 13:06:18 +0100567char *gengetopt_strdup(const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200568{
Harald Weltef6214982017-09-24 10:23:24 +0800569 char *result = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100570 if (!s)
571 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200572
Harald Weltebed35df2011-11-02 13:06:18 +0100573 result = (char *)malloc(strlen(s) + 1);
574 if (result == (char *)0)
575 return (char *)0;
576 strcpy(result, s);
577 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200578}
579
Harald Weltef6214982017-09-24 10:23:24 +0800580int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200581{
Harald Weltebed35df2011-11-02 13:06:18 +0100582 return cmdline_parser2(argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200583}
584
585int
Harald Weltef6214982017-09-24 10:23:24 +0800586cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info,
587 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200588{
Harald Weltebed35df2011-11-02 13:06:18 +0100589 int result;
Harald Weltef6214982017-09-24 10:23:24 +0800590 result = cmdline_parser_internal(argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200591
Harald Weltef6214982017-09-24 10:23:24 +0800592 if (result == EXIT_FAILURE) {
593 cmdline_parser_free(args_info);
594 exit(EXIT_FAILURE);
595 }
596
597 return result;
598}
599
600int
601cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info,
602 int override, int initialize, int check_required)
603{
604 int result;
605 struct cmdline_parser_params params;
606
607 params.override = override;
608 params.initialize = initialize;
609 params.check_required = check_required;
610 params.check_ambiguity = 0;
611 params.print_errors = 1;
612
613 result = cmdline_parser_internal(argc, argv, args_info, &params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200614
Harald Weltebed35df2011-11-02 13:06:18 +0100615 if (result == EXIT_FAILURE) {
616 cmdline_parser_free(args_info);
617 exit(EXIT_FAILURE);
618 }
619
620 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200621}
622
623int
Harald Weltebed35df2011-11-02 13:06:18 +0100624cmdline_parser_required(struct gengetopt_args_info *args_info,
625 const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200626{
Harald Weltef6214982017-09-24 10:23:24 +0800627 FIX_UNUSED(args_info);
628 FIX_UNUSED(prog_name);
Harald Weltebed35df2011-11-02 13:06:18 +0100629 return EXIT_SUCCESS;
Harald Welte1b3e5772010-05-04 11:13:56 +0200630}
631
Harald Weltef6214982017-09-24 10:23:24 +0800632static char *package_name = 0;
633
634/**
635 * @brief updates an option
636 * @param field the generic pointer to the field to update
637 * @param orig_field the pointer to the orig field
638 * @param field_given the pointer to the number of occurrence of this option
639 * @param prev_given the pointer to the number of occurrence already seen
640 * @param value the argument for this option (if null no arg was specified)
641 * @param possible_values the possible values for this option (if specified)
642 * @param default_value the default value (in case the option only accepts fixed values)
643 * @param arg_type the type of this option
644 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
645 * @param override @see cmdline_parser_params.override
646 * @param no_free whether to free a possible previous value
647 * @param multiple_option whether this is a multiple option
648 * @param long_opt the corresponding long option
649 * @param short_opt the corresponding short option (or '-' if none)
650 * @param additional_error possible further error specification
651 */
652static
653int update_arg(void *field, char **orig_field,
654 unsigned int *field_given, unsigned int *prev_given,
655 char *value, const char *possible_values[],
656 const char *default_value,
657 cmdline_parser_arg_type arg_type,
658 int check_ambiguity, int override,
659 int no_free, int multiple_option,
660 const char *long_opt, char short_opt,
661 const char *additional_error)
662{
663 char *stop_char = 0;
664 const char *val = value;
665 int found;
666 char **string_field;
667 FIX_UNUSED(field);
668
669 stop_char = 0;
670 found = 0;
671
672 if (!multiple_option && prev_given
673 && (*prev_given || (check_ambiguity && *field_given))) {
674 if (short_opt != '-')
675 fprintf(stderr,
676 "%s: `--%s' (`-%c') option given more than once%s\n",
677 package_name, long_opt, short_opt,
678 (additional_error ? additional_error : ""));
679 else
680 fprintf(stderr,
681 "%s: `--%s' option given more than once%s\n",
682 package_name, long_opt,
683 (additional_error ? additional_error : ""));
684 return 1; /* failure */
685 }
686
687 FIX_UNUSED(default_value);
688
689 if (field_given && *field_given && !override)
690 return 0;
691 if (prev_given)
692 (*prev_given)++;
693 if (field_given)
694 (*field_given)++;
695 if (possible_values)
696 val = possible_values[found];
697
698 switch (arg_type) {
699 case ARG_FLAG:
700 *((int *)field) = !*((int *)field);
701 break;
702 case ARG_INT:
703 if (val)
704 *((int *)field) = strtol(val, &stop_char, 0);
705 break;
706 case ARG_STRING:
707 if (val) {
708 string_field = (char **)field;
709 if (!no_free && *string_field)
710 free(*string_field); /* free previous string */
711 *string_field = gengetopt_strdup(val);
712 }
713 break;
714 default:
715 break;
716 };
717
718 /* check numeric conversion */
719 switch (arg_type) {
720 case ARG_INT:
721 if (val && !(stop_char && *stop_char == '\0')) {
722 fprintf(stderr, "%s: invalid numeric value: %s\n",
723 package_name, val);
724 return 1; /* failure */
725 }
726 break;
727 default:
728 ;
729 };
730
731 /* store the original value */
732 switch (arg_type) {
733 case ARG_NO:
734 case ARG_FLAG:
735 break;
736 default:
737 if (value && orig_field) {
738 if (no_free) {
739 *orig_field = value;
740 } else {
741 if (*orig_field)
742 free(*orig_field); /* free previous string */
743 *orig_field = gengetopt_strdup(value);
744 }
745 }
746 };
747
748 return 0; /* OK */
749}
750
Harald Welte1b3e5772010-05-04 11:13:56 +0200751int
Harald Weltef6214982017-09-24 10:23:24 +0800752cmdline_parser_internal(int argc, char **argv,
753 struct gengetopt_args_info *args_info,
754 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +0100755 const char *additional_error)
Harald Welte1b3e5772010-05-04 11:13:56 +0200756{
Harald Weltebed35df2011-11-02 13:06:18 +0100757 int c; /* Character of the parsed option. */
Harald Welte1b3e5772010-05-04 11:13:56 +0200758
Harald Weltef6214982017-09-24 10:23:24 +0800759 int error_occurred = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100760 struct gengetopt_args_info local_args_info;
Harald Welte1b3e5772010-05-04 11:13:56 +0200761
Harald Weltef6214982017-09-24 10:23:24 +0800762 int override;
763 int initialize;
764 int check_required;
765 int check_ambiguity;
766
767 package_name = argv[0];
768
769 override = params->override;
770 initialize = params->initialize;
771 check_required = params->check_required;
772 check_ambiguity = params->check_ambiguity;
773
Harald Weltebed35df2011-11-02 13:06:18 +0100774 if (initialize)
775 cmdline_parser_init(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200776
Harald Weltebed35df2011-11-02 13:06:18 +0100777 cmdline_parser_init(&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000778
Harald Weltebed35df2011-11-02 13:06:18 +0100779 optarg = 0;
780 optind = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800781 opterr = params->print_errors;
Harald Weltebed35df2011-11-02 13:06:18 +0100782 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000783
Harald Weltebed35df2011-11-02 13:06:18 +0100784 while (1) {
785 int option_index = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200786
Harald Weltebed35df2011-11-02 13:06:18 +0100787 static struct option long_options[] = {
788 {"help", 0, NULL, 'h'},
789 {"version", 0, NULL, 'V'},
790 {"debug", 0, NULL, 'd'},
791 {"conf", 1, NULL, 'c'},
792 {"pidfile", 1, NULL, 0},
793 {"statedir", 1, NULL, 0},
794 {"dns", 1, NULL, 0},
795 {"listen", 1, NULL, 'l'},
796 {"remote", 1, NULL, 'r'},
797 {"contexts", 1, NULL, 0},
798 {"timelimit", 1, NULL, 0},
799 {"gtpversion", 1, NULL, 0},
800 {"apn", 1, NULL, 'a'},
801 {"selmode", 1, NULL, 0},
802 {"rattype", 1, NULL, 0},
803 {"userloc", 1, NULL, 0},
804 {"rai", 1, NULL, 0},
805 {"mstz", 1, NULL, 0},
806 {"imeisv", 1, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800807 {"norecovery", 0, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100808 {"imsi", 1, NULL, 'i'},
809 {"nsapi", 1, NULL, 0},
810 {"msisdn", 1, NULL, 'm'},
811 {"qos", 1, NULL, 'q'},
812 {"qose1", 1, NULL, 0},
813 {"qose2", 1, NULL, 0},
814 {"qose3", 1, NULL, 0},
815 {"qose4", 1, NULL, 0},
816 {"charging", 1, NULL, 0},
817 {"uid", 1, NULL, 'u'},
818 {"pwd", 1, NULL, 'p'},
819 {"createif", 0, NULL, 0},
820 {"net", 1, NULL, 'n'},
821 {"defaultroute", 0, NULL, 0},
822 {"ipup", 1, NULL, 0},
823 {"ipdown", 1, NULL, 0},
824 {"pinghost", 1, NULL, 0},
825 {"pingrate", 1, NULL, 0},
826 {"pingsize", 1, NULL, 0},
827 {"pingcount", 1, NULL, 0},
828 {"pingquiet", 0, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800829 {0, 0, 0, 0}
Harald Weltebed35df2011-11-02 13:06:18 +0100830 };
jjako52c24142002-12-16 13:33:51 +0000831
Harald Weltebed35df2011-11-02 13:06:18 +0100832 c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:",
833 long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000834
Harald Weltebed35df2011-11-02 13:06:18 +0100835 if (c == -1)
836 break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000837
Harald Weltebed35df2011-11-02 13:06:18 +0100838 switch (c) {
839 case 'h': /* Print help and exit. */
840 cmdline_parser_print_help();
841 cmdline_parser_free(&local_args_info);
842 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000843
Harald Weltebed35df2011-11-02 13:06:18 +0100844 case 'V': /* Print version and exit. */
845 cmdline_parser_print_version();
846 cmdline_parser_free(&local_args_info);
847 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000848
Harald Weltebed35df2011-11-02 13:06:18 +0100849 case 'd': /* Run in debug mode. */
jjako52c24142002-12-16 13:33:51 +0000850
Harald Weltef6214982017-09-24 10:23:24 +0800851 if (update_arg
852 ((void *)&(args_info->debug_flag), 0,
853 &(args_info->debug_given),
854 &(local_args_info.debug_given), optarg, 0, 0,
855 ARG_FLAG, check_ambiguity, override, 1, 0, "debug",
856 'd', additional_error))
857 goto failure;
858
859 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100860 case 'c': /* Read configuration file. */
jjako52c24142002-12-16 13:33:51 +0000861
Harald Weltef6214982017-09-24 10:23:24 +0800862 if (update_arg((void *)&(args_info->conf_arg),
863 &(args_info->conf_orig),
864 &(args_info->conf_given),
865 &(local_args_info.conf_given), optarg, 0,
866 0, ARG_STRING, check_ambiguity, override,
867 0, 0, "conf", 'c', additional_error))
868 goto failure;
869
870 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100871 case 'l': /* Local interface. */
jjako52c24142002-12-16 13:33:51 +0000872
Harald Weltef6214982017-09-24 10:23:24 +0800873 if (update_arg((void *)&(args_info->listen_arg),
874 &(args_info->listen_orig),
875 &(args_info->listen_given),
876 &(local_args_info.listen_given), optarg,
877 0, 0, ARG_STRING, check_ambiguity,
878 override, 0, 0, "listen", 'l',
879 additional_error))
880 goto failure;
881
882 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100883 case 'r': /* Remote host. */
jjako52c24142002-12-16 13:33:51 +0000884
Harald Weltef6214982017-09-24 10:23:24 +0800885 if (update_arg((void *)&(args_info->remote_arg),
886 &(args_info->remote_orig),
887 &(args_info->remote_given),
888 &(local_args_info.remote_given), optarg,
889 0, 0, ARG_STRING, check_ambiguity,
890 override, 0, 0, "remote", 'r',
891 additional_error))
892 goto failure;
893
894 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100895 case 'a': /* Access point name. */
jjako52c24142002-12-16 13:33:51 +0000896
Harald Weltef6214982017-09-24 10:23:24 +0800897 if (update_arg((void *)&(args_info->apn_arg),
898 &(args_info->apn_orig),
899 &(args_info->apn_given),
900 &(local_args_info.apn_given), optarg, 0,
901 "internet", ARG_STRING, check_ambiguity,
902 override, 0, 0, "apn", 'a',
903 additional_error))
904 goto failure;
905
906 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100907 case 'i': /* IMSI. */
jjako52c24142002-12-16 13:33:51 +0000908
Harald Weltef6214982017-09-24 10:23:24 +0800909 if (update_arg((void *)&(args_info->imsi_arg),
910 &(args_info->imsi_orig),
911 &(args_info->imsi_given),
912 &(local_args_info.imsi_given), optarg, 0,
913 "240010123456789", ARG_STRING,
914 check_ambiguity, override, 0, 0, "imsi",
915 'i', additional_error))
916 goto failure;
917
918 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100919 case 'm': /* Mobile Station ISDN number. */
jjako52c24142002-12-16 13:33:51 +0000920
Harald Weltef6214982017-09-24 10:23:24 +0800921 if (update_arg((void *)&(args_info->msisdn_arg),
922 &(args_info->msisdn_orig),
923 &(args_info->msisdn_given),
924 &(local_args_info.msisdn_given), optarg,
925 0, "46702123456", ARG_STRING,
926 check_ambiguity, override, 0, 0,
927 "msisdn", 'm', additional_error))
928 goto failure;
929
930 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100931 case 'q': /* Requested quality of service. */
jjako52c24142002-12-16 13:33:51 +0000932
Harald Weltef6214982017-09-24 10:23:24 +0800933 if (update_arg((void *)&(args_info->qos_arg),
934 &(args_info->qos_orig),
935 &(args_info->qos_given),
936 &(local_args_info.qos_given), optarg, 0,
937 "0x000b921f", ARG_INT, check_ambiguity,
938 override, 0, 0, "qos", 'q',
939 additional_error))
940 goto failure;
941
942 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100943 case 'u': /* Login user ID. */
jjako52c24142002-12-16 13:33:51 +0000944
Harald Weltef6214982017-09-24 10:23:24 +0800945 if (update_arg((void *)&(args_info->uid_arg),
946 &(args_info->uid_orig),
947 &(args_info->uid_given),
948 &(local_args_info.uid_given), optarg, 0,
949 "mig", ARG_STRING, check_ambiguity,
950 override, 0, 0, "uid", 'u',
951 additional_error))
952 goto failure;
953
954 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100955 case 'p': /* Login password. */
jjako52c24142002-12-16 13:33:51 +0000956
Harald Weltef6214982017-09-24 10:23:24 +0800957 if (update_arg((void *)&(args_info->pwd_arg),
958 &(args_info->pwd_orig),
959 &(args_info->pwd_given),
960 &(local_args_info.pwd_given), optarg, 0,
961 "hemmelig", ARG_STRING, check_ambiguity,
962 override, 0, 0, "pwd", 'p',
963 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +0100964 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +0800965
966 break;
967 case 'n': /* Network address for local interface. */
968
969 if (update_arg((void *)&(args_info->net_arg),
970 &(args_info->net_orig),
971 &(args_info->net_given),
972 &(local_args_info.net_given), optarg, 0,
973 0, ARG_STRING, check_ambiguity, override,
974 0, 0, "net", 'n', additional_error))
975 goto failure;
976
Harald Weltebed35df2011-11-02 13:06:18 +0100977 break;
jjako193e8b12003-11-10 12:31:41 +0000978
Harald Weltebed35df2011-11-02 13:06:18 +0100979 case 0: /* Long option with no short option */
980 /* Filename of process id file. */
981 if (strcmp(long_options[option_index].name, "pidfile")
982 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +0800983
984 if (update_arg
985 ((void *)&(args_info->pidfile_arg),
986 &(args_info->pidfile_orig),
987 &(args_info->pidfile_given),
988 &(local_args_info.pidfile_given), optarg,
989 0, "./sgsnemu.pid", ARG_STRING,
990 check_ambiguity, override, 0, 0, "pidfile",
991 '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +0100992 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +0800993
Harald Weltebed35df2011-11-02 13:06:18 +0100994 }
995 /* Directory of nonvolatile data. */
996 else if (strcmp
997 (long_options[option_index].name,
998 "statedir") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +0800999
1000 if (update_arg
1001 ((void *)&(args_info->statedir_arg),
1002 &(args_info->statedir_orig),
1003 &(args_info->statedir_given),
1004 &(local_args_info.statedir_given), optarg,
1005 0, "./", ARG_STRING, check_ambiguity,
1006 override, 0, 0, "statedir", '-',
1007 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001008 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001009
Harald Weltebed35df2011-11-02 13:06:18 +01001010 }
1011 /* DNS Server to use. */
1012 else if (strcmp(long_options[option_index].name, "dns")
1013 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001014
1015 if (update_arg((void *)&(args_info->dns_arg),
1016 &(args_info->dns_orig),
1017 &(args_info->dns_given),
1018 &(local_args_info.dns_given),
1019 optarg, 0, 0, ARG_STRING,
1020 check_ambiguity, override, 0, 0,
1021 "dns", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001022 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001023
Harald Weltebed35df2011-11-02 13:06:18 +01001024 }
1025 /* Number of contexts. */
1026 else if (strcmp
1027 (long_options[option_index].name,
1028 "contexts") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001029
1030 if (update_arg
1031 ((void *)&(args_info->contexts_arg),
1032 &(args_info->contexts_orig),
1033 &(args_info->contexts_given),
1034 &(local_args_info.contexts_given), optarg,
1035 0, "1", ARG_INT, check_ambiguity, override,
1036 0, 0, "contexts", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001037 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001038
Harald Weltebed35df2011-11-02 13:06:18 +01001039 }
1040 /* Exit after timelimit seconds. */
1041 else if (strcmp
1042 (long_options[option_index].name,
1043 "timelimit") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001044
1045 if (update_arg
1046 ((void *)&(args_info->timelimit_arg),
1047 &(args_info->timelimit_orig),
1048 &(args_info->timelimit_given),
1049 &(local_args_info.timelimit_given), optarg,
1050 0, "0", ARG_INT, check_ambiguity, override,
1051 0, 0, "timelimit", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001052 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001053
Harald Weltebed35df2011-11-02 13:06:18 +01001054 }
1055 /* GTP version to use. */
1056 else if (strcmp
1057 (long_options[option_index].name,
1058 "gtpversion") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001059
1060 if (update_arg
1061 ((void *)&(args_info->gtpversion_arg),
1062 &(args_info->gtpversion_orig),
1063 &(args_info->gtpversion_given),
1064 &(local_args_info.gtpversion_given),
1065 optarg, 0, "1", ARG_INT, check_ambiguity,
1066 override, 0, 0, "gtpversion", '-',
1067 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001068 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001069
Harald Weltebed35df2011-11-02 13:06:18 +01001070 }
1071 /* Selection mode. */
1072 else if (strcmp
1073 (long_options[option_index].name,
1074 "selmode") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001075
1076 if (update_arg
1077 ((void *)&(args_info->selmode_arg),
1078 &(args_info->selmode_orig),
1079 &(args_info->selmode_given),
1080 &(local_args_info.selmode_given), optarg,
1081 0, "0x01", ARG_INT, check_ambiguity,
1082 override, 0, 0, "selmode", '-',
1083 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001084 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001085
Harald Weltebed35df2011-11-02 13:06:18 +01001086 }
1087 /* Radio Access Technology Type. */
1088 else if (strcmp
1089 (long_options[option_index].name,
1090 "rattype") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001091
1092 if (update_arg
1093 ((void *)&(args_info->rattype_arg),
1094 &(args_info->rattype_orig),
1095 &(args_info->rattype_given),
1096 &(local_args_info.rattype_given), optarg,
1097 0, "1", ARG_INT, check_ambiguity, override,
1098 0, 0, "rattype", '-', additional_error))
1099 goto failure;
1100
Harald Weltebed35df2011-11-02 13:06:18 +01001101 }
1102 /* User Location Information. */
1103 else if (strcmp
1104 (long_options[option_index].name,
1105 "userloc") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001106
1107 if (update_arg
1108 ((void *)&(args_info->userloc_arg),
1109 &(args_info->userloc_orig),
1110 &(args_info->userloc_given),
1111 &(local_args_info.userloc_given), optarg,
1112 0, "02509946241207", ARG_STRING,
1113 check_ambiguity, override, 0, 0, "userloc",
1114 '-', additional_error))
1115 goto failure;
1116
Harald Weltebed35df2011-11-02 13:06:18 +01001117 }
1118 /* Routing Area Information. */
1119 else if (strcmp(long_options[option_index].name, "rai")
1120 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001121
1122 if (update_arg((void *)&(args_info->rai_arg),
1123 &(args_info->rai_orig),
1124 &(args_info->rai_given),
1125 &(local_args_info.rai_given),
1126 optarg, 0, "02509946241207",
1127 ARG_STRING, check_ambiguity,
1128 override, 0, 0, "rai", '-',
1129 additional_error))
1130 goto failure;
1131
Harald Weltebed35df2011-11-02 13:06:18 +01001132 }
Harald Weltef6214982017-09-24 10:23:24 +08001133 /* MS Time Zone. */
Harald Weltebed35df2011-11-02 13:06:18 +01001134 else if (strcmp(long_options[option_index].name, "mstz")
1135 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001136
1137 if (update_arg((void *)&(args_info->mstz_arg),
1138 &(args_info->mstz_orig),
1139 &(args_info->mstz_given),
1140 &(local_args_info.mstz_given),
1141 optarg, 0, "0", ARG_STRING,
1142 check_ambiguity, override, 0, 0,
1143 "mstz", '-', additional_error))
1144 goto failure;
1145
Harald Weltebed35df2011-11-02 13:06:18 +01001146 }
Harald Weltef6214982017-09-24 10:23:24 +08001147 /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */
Harald Weltebed35df2011-11-02 13:06:18 +01001148 else if (strcmp
1149 (long_options[option_index].name,
1150 "imeisv") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001151
1152 if (update_arg((void *)&(args_info->imeisv_arg),
1153 &(args_info->imeisv_orig),
1154 &(args_info->imeisv_given),
1155 &(local_args_info.imeisv_given),
1156 optarg, 0, "2143658709214365",
1157 ARG_STRING, check_ambiguity,
1158 override, 0, 0, "imeisv", '-',
1159 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001160 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001161
Harald Weltebed35df2011-11-02 13:06:18 +01001162 }
1163 /* Do not send recovery. */
1164 else if (strcmp
1165 (long_options[option_index].name,
1166 "norecovery") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001167
1168 if (update_arg
1169 ((void *)&(args_info->norecovery_flag), 0,
1170 &(args_info->norecovery_given),
1171 &(local_args_info.norecovery_given),
1172 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1173 override, 1, 0, "norecovery", '-',
1174 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001175 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001176
1177 }
1178 /* NSAPI. */
1179 else if (strcmp
1180 (long_options[option_index].name,
1181 "nsapi") == 0) {
1182
1183 if (update_arg((void *)&(args_info->nsapi_arg),
1184 &(args_info->nsapi_orig),
1185 &(args_info->nsapi_given),
1186 &(local_args_info.nsapi_given),
1187 optarg, 0, "0", ARG_INT,
1188 check_ambiguity, override, 0, 0,
1189 "nsapi", '-', additional_error))
1190 goto failure;
1191
1192 }
1193 /* Requested quality of service Extension 1. */
1194 else if (strcmp
1195 (long_options[option_index].name,
1196 "qose1") == 0) {
1197
1198 if (update_arg((void *)&(args_info->qose1_arg),
1199 &(args_info->qose1_orig),
1200 &(args_info->qose1_given),
1201 &(local_args_info.qose1_given),
1202 optarg, 0, "0x9396404074f9ffff",
1203 ARG_INT, check_ambiguity,
1204 override, 0, 0, "qose1", '-',
1205 additional_error))
1206 goto failure;
1207
1208 }
1209 /* Requested quality of service Extension 2. */
1210 else if (strcmp
1211 (long_options[option_index].name,
1212 "qose2") == 0) {
1213
1214 if (update_arg((void *)&(args_info->qose2_arg),
1215 &(args_info->qose2_orig),
1216 &(args_info->qose2_given),
1217 &(local_args_info.qose2_given),
1218 optarg, 0, "0x11", ARG_INT,
1219 check_ambiguity, override, 0, 0,
1220 "qose2", '-', additional_error))
1221 goto failure;
1222
1223 }
1224 /* Requested quality of service Extension 3. */
1225 else if (strcmp
1226 (long_options[option_index].name,
1227 "qose3") == 0) {
1228
1229 if (update_arg((void *)&(args_info->qose3_arg),
1230 &(args_info->qose3_orig),
1231 &(args_info->qose3_given),
1232 &(local_args_info.qose3_given),
1233 optarg, 0, "0x0101", ARG_INT,
1234 check_ambiguity, override, 0, 0,
1235 "qose3", '-', additional_error))
1236 goto failure;
1237
1238 }
1239 /* Requested quality of service Extension 4. */
1240 else if (strcmp
1241 (long_options[option_index].name,
1242 "qose4") == 0) {
1243
1244 if (update_arg((void *)&(args_info->qose4_arg),
1245 &(args_info->qose4_orig),
1246 &(args_info->qose4_given),
1247 &(local_args_info.qose4_given),
1248 optarg, 0, "0x4040", ARG_INT,
1249 check_ambiguity, override, 0, 0,
1250 "qose4", '-', additional_error))
1251 goto failure;
1252
1253 }
1254 /* Charging characteristics. */
1255 else if (strcmp
1256 (long_options[option_index].name,
1257 "charging") == 0) {
1258
1259 if (update_arg
1260 ((void *)&(args_info->charging_arg),
1261 &(args_info->charging_orig),
1262 &(args_info->charging_given),
1263 &(local_args_info.charging_given), optarg,
1264 0, "0x0800", ARG_INT, check_ambiguity,
1265 override, 0, 0, "charging", '-',
1266 additional_error))
1267 goto failure;
1268
1269 }
1270 /* Create local network interface. */
1271 else if (strcmp
1272 (long_options[option_index].name,
1273 "createif") == 0) {
1274
1275 if (update_arg
1276 ((void *)&(args_info->createif_flag), 0,
1277 &(args_info->createif_given),
1278 &(local_args_info.createif_given), optarg,
1279 0, 0, ARG_FLAG, check_ambiguity, override,
1280 1, 0, "createif", '-', additional_error))
1281 goto failure;
1282
1283 }
1284 /* Create default route. */
1285 else if (strcmp
1286 (long_options[option_index].name,
1287 "defaultroute") == 0) {
1288
1289 if (update_arg
1290 ((void *)&(args_info->defaultroute_flag), 0,
1291 &(args_info->defaultroute_given),
1292 &(local_args_info.defaultroute_given),
1293 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1294 override, 1, 0, "defaultroute", '-',
1295 additional_error))
1296 goto failure;
1297
1298 }
1299 /* Script to run after link-up. */
1300 else if (strcmp(long_options[option_index].name, "ipup")
1301 == 0) {
1302
1303 if (update_arg((void *)&(args_info->ipup_arg),
1304 &(args_info->ipup_orig),
1305 &(args_info->ipup_given),
1306 &(local_args_info.ipup_given),
1307 optarg, 0, 0, ARG_STRING,
1308 check_ambiguity, override, 0, 0,
1309 "ipup", '-', additional_error))
1310 goto failure;
1311
1312 }
1313 /* Script to run after link-down. */
1314 else if (strcmp
1315 (long_options[option_index].name,
1316 "ipdown") == 0) {
1317
1318 if (update_arg((void *)&(args_info->ipdown_arg),
1319 &(args_info->ipdown_orig),
1320 &(args_info->ipdown_given),
1321 &(local_args_info.ipdown_given),
1322 optarg, 0, 0, ARG_STRING,
1323 check_ambiguity, override, 0, 0,
1324 "ipdown", '-', additional_error))
1325 goto failure;
1326
1327 }
1328 /* Ping remote host. */
1329 else if (strcmp
1330 (long_options[option_index].name,
1331 "pinghost") == 0) {
1332
1333 if (update_arg
1334 ((void *)&(args_info->pinghost_arg),
1335 &(args_info->pinghost_orig),
1336 &(args_info->pinghost_given),
1337 &(local_args_info.pinghost_given), optarg,
1338 0, 0, ARG_STRING, check_ambiguity,
1339 override, 0, 0, "pinghost", '-',
1340 additional_error))
1341 goto failure;
1342
1343 }
1344 /* Number of ping req per second. */
1345 else if (strcmp
1346 (long_options[option_index].name,
1347 "pingrate") == 0) {
1348
1349 if (update_arg
1350 ((void *)&(args_info->pingrate_arg),
1351 &(args_info->pingrate_orig),
1352 &(args_info->pingrate_given),
1353 &(local_args_info.pingrate_given), optarg,
1354 0, "1", ARG_INT, check_ambiguity, override,
1355 0, 0, "pingrate", '-', additional_error))
1356 goto failure;
1357
1358 }
1359 /* Number of ping data bytes. */
1360 else if (strcmp
1361 (long_options[option_index].name,
1362 "pingsize") == 0) {
1363
1364 if (update_arg
1365 ((void *)&(args_info->pingsize_arg),
1366 &(args_info->pingsize_orig),
1367 &(args_info->pingsize_given),
1368 &(local_args_info.pingsize_given), optarg,
1369 0, "56", ARG_INT, check_ambiguity,
1370 override, 0, 0, "pingsize", '-',
1371 additional_error))
1372 goto failure;
1373
1374 }
1375 /* Number of ping req to send. */
1376 else if (strcmp
1377 (long_options[option_index].name,
1378 "pingcount") == 0) {
1379
1380 if (update_arg
1381 ((void *)&(args_info->pingcount_arg),
1382 &(args_info->pingcount_orig),
1383 &(args_info->pingcount_given),
1384 &(local_args_info.pingcount_given), optarg,
1385 0, "0", ARG_INT, check_ambiguity, override,
1386 0, 0, "pingcount", '-', additional_error))
1387 goto failure;
1388
1389 }
1390 /* Do not print ping packet info. */
1391 else if (strcmp
1392 (long_options[option_index].name,
1393 "pingquiet") == 0) {
1394
1395 if (update_arg
1396 ((void *)&(args_info->pingquiet_flag), 0,
1397 &(args_info->pingquiet_given),
1398 &(local_args_info.pingquiet_given), optarg,
1399 0, 0, ARG_FLAG, check_ambiguity, override,
1400 1, 0, "pingquiet", '-', additional_error))
1401 goto failure;
1402
Harald Weltebed35df2011-11-02 13:06:18 +01001403 }
jjako52c24142002-12-16 13:33:51 +00001404
Harald Weltebed35df2011-11-02 13:06:18 +01001405 break;
1406 case '?': /* Invalid option. */
1407 /* `getopt_long' already printed an error message. */
1408 goto failure;
jjako52c24142002-12-16 13:33:51 +00001409
Harald Weltebed35df2011-11-02 13:06:18 +01001410 default: /* bug: option not considered. */
1411 fprintf(stderr, "%s: option unknown: %c%s\n",
1412 CMDLINE_PARSER_PACKAGE, c,
1413 (additional_error ? additional_error : ""));
1414 abort();
1415 } /* switch */
1416 } /* while */
jjako52c24142002-12-16 13:33:51 +00001417
Harald Weltebed35df2011-11-02 13:06:18 +01001418 cmdline_parser_release(&local_args_info);
jjako52c24142002-12-16 13:33:51 +00001419
Harald Weltef6214982017-09-24 10:23:24 +08001420 if (error_occurred)
Harald Weltebed35df2011-11-02 13:06:18 +01001421 return (EXIT_FAILURE);
Harald Welte1b3e5772010-05-04 11:13:56 +02001422
Harald Weltebed35df2011-11-02 13:06:18 +01001423 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001424
1425failure:
Harald Weltebed35df2011-11-02 13:06:18 +01001426
1427 cmdline_parser_release(&local_args_info);
1428 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +00001429}
1430
Harald Welte1b3e5772010-05-04 11:13:56 +02001431#ifndef CONFIG_FILE_LINE_SIZE
1432#define CONFIG_FILE_LINE_SIZE 2048
1433#endif
1434#define ADDITIONAL_ERROR " in configuration file "
1435
1436#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1437/* 3 is for "--" and "=" */
1438
Harald Weltef6214982017-09-24 10:23:24 +08001439static int _cmdline_parser_configfile(const char *filename, int *my_argc)
jjako52c24142002-12-16 13:33:51 +00001440{
Harald Weltebed35df2011-11-02 13:06:18 +01001441 FILE *file;
Harald Weltef6214982017-09-24 10:23:24 +08001442 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
Harald Weltebed35df2011-11-02 13:06:18 +01001443 char linebuf[CONFIG_FILE_LINE_SIZE];
1444 int line_num = 0;
Harald Weltef6214982017-09-24 10:23:24 +08001445 int result = 0, equal;
Harald Weltebed35df2011-11-02 13:06:18 +01001446 char *fopt, *farg;
1447 char *str_index;
1448 size_t len, next_token;
1449 char delimiter;
Harald Welte1b3e5772010-05-04 11:13:56 +02001450
Harald Weltef6214982017-09-24 10:23:24 +08001451 if ((file = fopen(filename, "r")) == 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001452 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1453 CMDLINE_PARSER_PACKAGE, filename);
Harald Weltef6214982017-09-24 10:23:24 +08001454 return EXIT_FAILURE;
Harald Weltebed35df2011-11-02 13:06:18 +01001455 }
jjako52c24142002-12-16 13:33:51 +00001456
Harald Weltef6214982017-09-24 10:23:24 +08001457 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001458 ++line_num;
1459 my_argv[0] = '\0';
1460 len = strlen(linebuf);
1461 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) {
1462 fprintf(stderr,
1463 "%s:%s:%d: Line too long in configuration file\n",
1464 CMDLINE_PARSER_PACKAGE, filename, line_num);
1465 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001466 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001467 }
jjako52c24142002-12-16 13:33:51 +00001468
Harald Weltebed35df2011-11-02 13:06:18 +01001469 /* find first non-whitespace character in the line */
1470 next_token = strspn(linebuf, " \t\r\n");
1471 str_index = linebuf + next_token;
jjako52c24142002-12-16 13:33:51 +00001472
Harald Weltebed35df2011-11-02 13:06:18 +01001473 if (str_index[0] == '\0' || str_index[0] == '#')
1474 continue; /* empty line or comment line is skipped */
Harald Welte1b3e5772010-05-04 11:13:56 +02001475
Harald Weltebed35df2011-11-02 13:06:18 +01001476 fopt = str_index;
Harald Welte1b3e5772010-05-04 11:13:56 +02001477
Harald Weltebed35df2011-11-02 13:06:18 +01001478 /* truncate fopt at the end of the first non-valid character */
1479 next_token = strcspn(fopt, " \t\r\n=");
Harald Welte1b3e5772010-05-04 11:13:56 +02001480
Harald Weltebed35df2011-11-02 13:06:18 +01001481 if (fopt[next_token] == '\0') { /* the line is over */
Harald Weltef6214982017-09-24 10:23:24 +08001482 farg = 0;
Harald Weltebed35df2011-11-02 13:06:18 +01001483 equal = 0;
1484 goto noarg;
1485 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001486
Harald Weltebed35df2011-11-02 13:06:18 +01001487 /* remember if equal sign is present */
1488 equal = (fopt[next_token] == '=');
1489 fopt[next_token++] = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001490
Harald Weltebed35df2011-11-02 13:06:18 +01001491 /* advance pointers to the next token after the end of fopt */
1492 next_token += strspn(fopt + next_token, " \t\r\n");
Harald Weltef6214982017-09-24 10:23:24 +08001493
Harald Weltebed35df2011-11-02 13:06:18 +01001494 /* check for the presence of equal sign, and if so, skip it */
1495 if (!equal)
1496 if ((equal = (fopt[next_token] == '='))) {
1497 next_token++;
1498 next_token +=
1499 strspn(fopt + next_token, " \t\r\n");
1500 }
1501 str_index += next_token;
Harald Welte1b3e5772010-05-04 11:13:56 +02001502
Harald Weltebed35df2011-11-02 13:06:18 +01001503 /* find argument */
1504 farg = str_index;
1505 if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */
1506 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1507 if (!str_index) {
1508 fprintf
1509 (stderr,
1510 "%s:%s:%d: unterminated string in configuration file\n",
1511 CMDLINE_PARSER_PACKAGE, filename,
1512 line_num);
1513 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001514 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001515 }
1516 } else { /* read up the remaining part up to a delimiter */
1517 next_token = strcspn(farg, " \t\r\n#\'\"");
1518 str_index += next_token;
1519 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001520
Harald Weltebed35df2011-11-02 13:06:18 +01001521 /* truncate farg at the delimiter and store it for further check */
1522 delimiter = *str_index, *str_index++ = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001523
Harald Weltebed35df2011-11-02 13:06:18 +01001524 /* everything but comment is illegal at the end of line */
1525 if (delimiter != '\0' && delimiter != '#') {
1526 str_index += strspn(str_index, " \t\r\n");
1527 if (*str_index != '\0' && *str_index != '#') {
1528 fprintf
1529 (stderr,
1530 "%s:%s:%d: malformed string in configuration file\n",
1531 CMDLINE_PARSER_PACKAGE, filename,
1532 line_num);
1533 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001534 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001535 }
1536 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001537
Harald Weltebed35df2011-11-02 13:06:18 +01001538noarg:
Harald Weltef6214982017-09-24 10:23:24 +08001539 if (!strcmp(fopt, "include")) {
1540 if (farg && *farg) {
1541 result =
1542 _cmdline_parser_configfile(farg, my_argc);
1543 } else {
1544 fprintf(stderr,
1545 "%s:%s:%d: include requires a filename argument.\n",
1546 CMDLINE_PARSER_PACKAGE, filename,
1547 line_num);
1548 }
1549 continue;
1550 }
Harald Weltebed35df2011-11-02 13:06:18 +01001551 len = strlen(fopt);
Harald Weltebed35df2011-11-02 13:06:18 +01001552 strcat(my_argv, len > 1 ? "--" : "-");
1553 strcat(my_argv, fopt);
1554 if (len > 1 && ((farg && *farg) || equal))
1555 strcat(my_argv, "=");
1556 if (farg && *farg)
1557 strcat(my_argv, farg);
Harald Weltef6214982017-09-24 10:23:24 +08001558 ++(*my_argc);
Harald Welte1b3e5772010-05-04 11:13:56 +02001559
Harald Weltebed35df2011-11-02 13:06:18 +01001560 cmd_line_list_tmp =
1561 (struct line_list *)malloc(sizeof(struct line_list));
1562 cmd_line_list_tmp->next = cmd_line_list;
1563 cmd_line_list = cmd_line_list_tmp;
1564 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1565 } /* while */
jjako52c24142002-12-16 13:33:51 +00001566
Harald Weltebed35df2011-11-02 13:06:18 +01001567 if (file)
1568 fclose(file);
Harald Weltef6214982017-09-24 10:23:24 +08001569 return result;
1570}
1571
1572int
1573cmdline_parser_configfile(const char *filename,
1574 struct gengetopt_args_info *args_info,
1575 int override, int initialize, int check_required)
1576{
1577 struct cmdline_parser_params params;
1578
1579 params.override = override;
1580 params.initialize = initialize;
1581 params.check_required = check_required;
1582 params.check_ambiguity = 0;
1583 params.print_errors = 1;
1584
1585 return cmdline_parser_config_file(filename, args_info, &params);
1586}
1587
1588int
1589cmdline_parser_config_file(const char *filename,
1590 struct gengetopt_args_info *args_info,
1591 struct cmdline_parser_params *params)
1592{
1593 int i, result;
1594 int my_argc = 1;
1595 char **my_argv_arg;
1596 char *additional_error;
1597
1598 /* store the program name */
1599 cmd_line_list_tmp =
1600 (struct line_list *)malloc(sizeof(struct line_list));
1601 cmd_line_list_tmp->next = cmd_line_list;
1602 cmd_line_list = cmd_line_list_tmp;
1603 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1604
1605 result = _cmdline_parser_configfile(filename, &my_argc);
1606
1607 if (result != EXIT_FAILURE) {
1608 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1609 cmd_line_list_tmp = cmd_line_list;
1610
1611 for (i = my_argc - 1; i >= 0; --i) {
1612 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1613 cmd_line_list_tmp = cmd_line_list_tmp->next;
1614 }
1615
1616 my_argv_arg[my_argc] = 0;
1617
1618 additional_error =
1619 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) +
1620 1);
1621 strcpy(additional_error, ADDITIONAL_ERROR);
1622 strcat(additional_error, filename);
1623 result =
1624 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1625 params, additional_error);
1626
1627 free(additional_error);
1628 free(my_argv_arg);
1629 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001630
Harald Weltebed35df2011-11-02 13:06:18 +01001631 free_cmd_list();
1632 if (result == EXIT_FAILURE) {
1633 cmdline_parser_free(args_info);
1634 exit(EXIT_FAILURE);
1635 }
1636
1637 return result;
jjako52c24142002-12-16 13:33:51 +00001638}