blob: 6df8fc148b7abf054e1dd25af0892942bd08313e [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 Weltefbb9c7f2017-09-24 11:50:20 +080078 " --no-tx-gpdu-seq Don't transmit G-PDU sequence nums\n (default=off)",
Harald Weltebed35df2011-11-02 13:06:18 +010079 0
Harald Welte1b3e5772010-05-04 11:13:56 +020080};
81
Harald Weltef6214982017-09-24 10:23:24 +080082typedef enum { ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT
83} cmdline_parser_arg_type;
84
Harald Welte1b3e5772010-05-04 11:13:56 +020085static
Harald Weltebed35df2011-11-02 13:06:18 +010086void clear_given(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020087static
Harald Weltebed35df2011-11-02 13:06:18 +010088void clear_args(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020089
90static int
Harald Weltef6214982017-09-24 10:23:24 +080091cmdline_parser_internal(int argc, char **argv,
92 struct gengetopt_args_info *args_info,
93 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +010094 const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020095
Harald Weltebed35df2011-11-02 13:06:18 +010096struct line_list {
97 char *string_arg;
98 struct line_list *next;
Harald Welte1b3e5772010-05-04 11:13:56 +020099};
100
101static struct line_list *cmd_line_list = 0;
102static struct line_list *cmd_line_list_tmp = 0;
103
Harald Weltebed35df2011-11-02 13:06:18 +0100104static void free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200105{
Harald Weltebed35df2011-11-02 13:06:18 +0100106 /* free the list of a previous call */
107 if (cmd_line_list) {
108 while (cmd_line_list) {
109 cmd_line_list_tmp = cmd_line_list;
110 cmd_line_list = cmd_line_list->next;
111 free(cmd_line_list_tmp->string_arg);
112 free(cmd_line_list_tmp);
113 }
114 }
jjako52c24142002-12-16 13:33:51 +0000115}
116
Harald Weltebed35df2011-11-02 13:06:18 +0100117static char *gengetopt_strdup(const char *s);
jjako52c24142002-12-16 13:33:51 +0000118
Harald Welte1b3e5772010-05-04 11:13:56 +0200119static
Harald Weltebed35df2011-11-02 13:06:18 +0100120void clear_given(struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000121{
Harald Weltebed35df2011-11-02 13:06:18 +0100122 args_info->help_given = 0;
123 args_info->version_given = 0;
124 args_info->debug_given = 0;
125 args_info->conf_given = 0;
126 args_info->pidfile_given = 0;
127 args_info->statedir_given = 0;
128 args_info->dns_given = 0;
129 args_info->listen_given = 0;
130 args_info->remote_given = 0;
131 args_info->contexts_given = 0;
132 args_info->timelimit_given = 0;
133 args_info->gtpversion_given = 0;
134 args_info->apn_given = 0;
135 args_info->selmode_given = 0;
136 args_info->rattype_given = 0;
137 args_info->userloc_given = 0;
138 args_info->rai_given = 0;
139 args_info->mstz_given = 0;
140 args_info->imeisv_given = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800141 args_info->norecovery_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100142 args_info->imsi_given = 0;
143 args_info->nsapi_given = 0;
144 args_info->msisdn_given = 0;
145 args_info->qos_given = 0;
146 args_info->qose1_given = 0;
147 args_info->qose2_given = 0;
148 args_info->qose3_given = 0;
149 args_info->qose4_given = 0;
150 args_info->charging_given = 0;
151 args_info->uid_given = 0;
152 args_info->pwd_given = 0;
153 args_info->createif_given = 0;
154 args_info->net_given = 0;
155 args_info->defaultroute_given = 0;
156 args_info->ipup_given = 0;
157 args_info->ipdown_given = 0;
158 args_info->pinghost_given = 0;
159 args_info->pingrate_given = 0;
160 args_info->pingsize_given = 0;
161 args_info->pingcount_given = 0;
162 args_info->pingquiet_given = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800163 args_info->no_tx_gpdu_seq_given = 0;
jjako52c24142002-12-16 13:33:51 +0000164}
165
Harald Welte1b3e5772010-05-04 11:13:56 +0200166static
Harald Weltebed35df2011-11-02 13:06:18 +0100167void clear_args(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200168{
Harald Weltef6214982017-09-24 10:23:24 +0800169 FIX_UNUSED(args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100170 args_info->debug_flag = 0;
171 args_info->conf_arg = NULL;
172 args_info->conf_orig = NULL;
173 args_info->pidfile_arg = gengetopt_strdup("./sgsnemu.pid");
174 args_info->pidfile_orig = NULL;
175 args_info->statedir_arg = gengetopt_strdup("./");
176 args_info->statedir_orig = NULL;
177 args_info->dns_arg = NULL;
178 args_info->dns_orig = NULL;
179 args_info->listen_arg = NULL;
180 args_info->listen_orig = NULL;
181 args_info->remote_arg = NULL;
182 args_info->remote_orig = NULL;
183 args_info->contexts_arg = 1;
184 args_info->contexts_orig = NULL;
185 args_info->timelimit_arg = 0;
186 args_info->timelimit_orig = NULL;
187 args_info->gtpversion_arg = 1;
188 args_info->gtpversion_orig = NULL;
189 args_info->apn_arg = gengetopt_strdup("internet");
190 args_info->apn_orig = NULL;
191 args_info->selmode_arg = 0x01;
192 args_info->selmode_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800193 args_info->rattype_arg = 1;
Harald Weltebed35df2011-11-02 13:06:18 +0100194 args_info->rattype_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800195 args_info->userloc_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100196 args_info->userloc_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800197 args_info->rai_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100198 args_info->rai_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800199 args_info->mstz_arg = gengetopt_strdup("0");
Harald Weltebed35df2011-11-02 13:06:18 +0100200 args_info->mstz_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800201 args_info->imeisv_arg = gengetopt_strdup("2143658709214365");
Harald Weltebed35df2011-11-02 13:06:18 +0100202 args_info->imeisv_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800203 args_info->norecovery_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100204 args_info->imsi_arg = gengetopt_strdup("240010123456789");
205 args_info->imsi_orig = NULL;
206 args_info->nsapi_arg = 0;
207 args_info->nsapi_orig = NULL;
208 args_info->msisdn_arg = gengetopt_strdup("46702123456");
209 args_info->msisdn_orig = NULL;
210 args_info->qos_arg = 0x000b921f;
211 args_info->qos_orig = NULL;
212 args_info->qose1_arg = 0x9396404074f9ffff;
213 args_info->qose1_orig = NULL;
214 args_info->qose2_arg = 0x11;
215 args_info->qose2_orig = NULL;
216 args_info->qose3_arg = 0x0101;
217 args_info->qose3_orig = NULL;
218 args_info->qose4_arg = 0x4040;
219 args_info->qose4_orig = NULL;
220 args_info->charging_arg = 0x0800;
221 args_info->charging_orig = NULL;
222 args_info->uid_arg = gengetopt_strdup("mig");
223 args_info->uid_orig = NULL;
224 args_info->pwd_arg = gengetopt_strdup("hemmelig");
225 args_info->pwd_orig = NULL;
226 args_info->createif_flag = 0;
227 args_info->net_arg = NULL;
228 args_info->net_orig = NULL;
229 args_info->defaultroute_flag = 0;
230 args_info->ipup_arg = NULL;
231 args_info->ipup_orig = NULL;
232 args_info->ipdown_arg = NULL;
233 args_info->ipdown_orig = NULL;
234 args_info->pinghost_arg = NULL;
235 args_info->pinghost_orig = NULL;
236 args_info->pingrate_arg = 1;
237 args_info->pingrate_orig = NULL;
238 args_info->pingsize_arg = 56;
239 args_info->pingsize_orig = NULL;
240 args_info->pingcount_arg = 0;
241 args_info->pingcount_orig = NULL;
242 args_info->pingquiet_flag = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800243 args_info->no_tx_gpdu_seq_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100244
Harald Welte1b3e5772010-05-04 11:13:56 +0200245}
246
247static
248void init_args_info(struct gengetopt_args_info *args_info)
249{
Harald Weltef6214982017-09-24 10:23:24 +0800250
Harald Weltebed35df2011-11-02 13:06:18 +0100251 args_info->help_help = gengetopt_args_info_help[0];
252 args_info->version_help = gengetopt_args_info_help[1];
253 args_info->debug_help = gengetopt_args_info_help[2];
254 args_info->conf_help = gengetopt_args_info_help[3];
255 args_info->pidfile_help = gengetopt_args_info_help[4];
256 args_info->statedir_help = gengetopt_args_info_help[5];
257 args_info->dns_help = gengetopt_args_info_help[6];
258 args_info->listen_help = gengetopt_args_info_help[7];
259 args_info->remote_help = gengetopt_args_info_help[8];
260 args_info->contexts_help = gengetopt_args_info_help[9];
261 args_info->timelimit_help = gengetopt_args_info_help[10];
262 args_info->gtpversion_help = gengetopt_args_info_help[11];
263 args_info->apn_help = gengetopt_args_info_help[12];
264 args_info->selmode_help = gengetopt_args_info_help[13];
Harald Weltef6214982017-09-24 10:23:24 +0800265 args_info->rattype_help = gengetopt_args_info_help[14];
266 args_info->userloc_help = gengetopt_args_info_help[15];
267 args_info->rai_help = gengetopt_args_info_help[16];
268 args_info->mstz_help = gengetopt_args_info_help[17];
269 args_info->imeisv_help = gengetopt_args_info_help[18];
270 args_info->norecovery_help = gengetopt_args_info_help[19];
271 args_info->imsi_help = gengetopt_args_info_help[20];
272 args_info->nsapi_help = gengetopt_args_info_help[21];
273 args_info->msisdn_help = gengetopt_args_info_help[22];
274 args_info->qos_help = gengetopt_args_info_help[23];
275 args_info->qose1_help = gengetopt_args_info_help[24];
276 args_info->qose2_help = gengetopt_args_info_help[25];
277 args_info->qose3_help = gengetopt_args_info_help[26];
278 args_info->qose4_help = gengetopt_args_info_help[27];
279 args_info->charging_help = gengetopt_args_info_help[28];
280 args_info->uid_help = gengetopt_args_info_help[29];
281 args_info->pwd_help = gengetopt_args_info_help[30];
282 args_info->createif_help = gengetopt_args_info_help[31];
283 args_info->net_help = gengetopt_args_info_help[32];
284 args_info->defaultroute_help = gengetopt_args_info_help[33];
285 args_info->ipup_help = gengetopt_args_info_help[34];
286 args_info->ipdown_help = gengetopt_args_info_help[35];
287 args_info->pinghost_help = gengetopt_args_info_help[36];
288 args_info->pingrate_help = gengetopt_args_info_help[37];
289 args_info->pingsize_help = gengetopt_args_info_help[38];
290 args_info->pingcount_help = gengetopt_args_info_help[39];
291 args_info->pingquiet_help = gengetopt_args_info_help[40];
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800292 args_info->no_tx_gpdu_seq_help = gengetopt_args_info_help[41];
Harald Weltebed35df2011-11-02 13:06:18 +0100293
Harald Welte1b3e5772010-05-04 11:13:56 +0200294}
295
Harald Weltebed35df2011-11-02 13:06:18 +0100296void cmdline_parser_print_version(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200297{
Harald Weltef6214982017-09-24 10:23:24 +0800298 printf("%s %s\n",
299 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ?
300 CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
301 CMDLINE_PARSER_VERSION);
302
303 if (strlen(gengetopt_args_info_versiontext) > 0)
304 printf("\n%s\n", gengetopt_args_info_versiontext);
Harald Welte1b3e5772010-05-04 11:13:56 +0200305}
306
Harald Weltef6214982017-09-24 10:23:24 +0800307static void print_help_common(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200308{
Harald Weltebed35df2011-11-02 13:06:18 +0100309 cmdline_parser_print_version();
Harald Welte1b3e5772010-05-04 11:13:56 +0200310
Harald Weltebed35df2011-11-02 13:06:18 +0100311 if (strlen(gengetopt_args_info_purpose) > 0)
312 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200313
Harald Weltef6214982017-09-24 10:23:24 +0800314 if (strlen(gengetopt_args_info_usage) > 0)
315 printf("\n%s\n", gengetopt_args_info_usage);
316
317 printf("\n");
318
319 if (strlen(gengetopt_args_info_description) > 0)
320 printf("%s\n\n", gengetopt_args_info_description);
321}
322
323void cmdline_parser_print_help(void)
324{
325 int i = 0;
326 print_help_common();
Harald Weltebed35df2011-11-02 13:06:18 +0100327 while (gengetopt_args_info_help[i])
328 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200329}
330
Harald Weltebed35df2011-11-02 13:06:18 +0100331void cmdline_parser_init(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200332{
Harald Weltebed35df2011-11-02 13:06:18 +0100333 clear_given(args_info);
334 clear_args(args_info);
335 init_args_info(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200336}
337
Harald Weltef6214982017-09-24 10:23:24 +0800338void cmdline_parser_params_init(struct cmdline_parser_params *params)
339{
340 if (params) {
341 params->override = 0;
342 params->initialize = 1;
343 params->check_required = 1;
344 params->check_ambiguity = 0;
345 params->print_errors = 1;
346 }
347}
348
349struct cmdline_parser_params *cmdline_parser_params_create(void)
350{
351 struct cmdline_parser_params *params =
352 (struct cmdline_parser_params *)
353 malloc(sizeof(struct cmdline_parser_params));
354 cmdline_parser_params_init(params);
355 return params;
356}
357
358static void free_string_field(char **s)
359{
360 if (*s) {
361 free(*s);
362 *s = 0;
363 }
364}
365
Harald Weltebed35df2011-11-02 13:06:18 +0100366static void cmdline_parser_release(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200367{
Harald Weltebed35df2011-11-02 13:06:18 +0100368
Harald Weltef6214982017-09-24 10:23:24 +0800369 free_string_field(&(args_info->conf_arg));
370 free_string_field(&(args_info->conf_orig));
371 free_string_field(&(args_info->pidfile_arg));
372 free_string_field(&(args_info->pidfile_orig));
373 free_string_field(&(args_info->statedir_arg));
374 free_string_field(&(args_info->statedir_orig));
375 free_string_field(&(args_info->dns_arg));
376 free_string_field(&(args_info->dns_orig));
377 free_string_field(&(args_info->listen_arg));
378 free_string_field(&(args_info->listen_orig));
379 free_string_field(&(args_info->remote_arg));
380 free_string_field(&(args_info->remote_orig));
381 free_string_field(&(args_info->contexts_orig));
382 free_string_field(&(args_info->timelimit_orig));
383 free_string_field(&(args_info->gtpversion_orig));
384 free_string_field(&(args_info->apn_arg));
385 free_string_field(&(args_info->apn_orig));
386 free_string_field(&(args_info->selmode_orig));
387 free_string_field(&(args_info->rattype_orig));
388 free_string_field(&(args_info->userloc_arg));
389 free_string_field(&(args_info->userloc_orig));
390 free_string_field(&(args_info->rai_arg));
391 free_string_field(&(args_info->rai_orig));
392 free_string_field(&(args_info->mstz_arg));
393 free_string_field(&(args_info->mstz_orig));
394 free_string_field(&(args_info->imeisv_arg));
395 free_string_field(&(args_info->imeisv_orig));
396 free_string_field(&(args_info->imsi_arg));
397 free_string_field(&(args_info->imsi_orig));
398 free_string_field(&(args_info->nsapi_orig));
399 free_string_field(&(args_info->msisdn_arg));
400 free_string_field(&(args_info->msisdn_orig));
401 free_string_field(&(args_info->qos_orig));
402 free_string_field(&(args_info->qose1_orig));
403 free_string_field(&(args_info->qose2_orig));
404 free_string_field(&(args_info->qose3_orig));
405 free_string_field(&(args_info->qose4_orig));
406 free_string_field(&(args_info->charging_orig));
407 free_string_field(&(args_info->uid_arg));
408 free_string_field(&(args_info->uid_orig));
409 free_string_field(&(args_info->pwd_arg));
410 free_string_field(&(args_info->pwd_orig));
411 free_string_field(&(args_info->net_arg));
412 free_string_field(&(args_info->net_orig));
413 free_string_field(&(args_info->ipup_arg));
414 free_string_field(&(args_info->ipup_orig));
415 free_string_field(&(args_info->ipdown_arg));
416 free_string_field(&(args_info->ipdown_orig));
417 free_string_field(&(args_info->pinghost_arg));
418 free_string_field(&(args_info->pinghost_orig));
419 free_string_field(&(args_info->pingrate_orig));
420 free_string_field(&(args_info->pingsize_orig));
421 free_string_field(&(args_info->pingcount_orig));
Harald Weltebed35df2011-11-02 13:06:18 +0100422
423 clear_given(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200424}
425
Harald Weltef6214982017-09-24 10:23:24 +0800426static void
427write_into_file(FILE * outfile, const char *opt, const char *arg,
428 const char *values[])
429{
430 FIX_UNUSED(values);
431 if (arg) {
432 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
433 } else {
434 fprintf(outfile, "%s\n", opt);
435 }
436}
437
438int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info)
439{
440 int i = 0;
441
442 if (!outfile) {
443 fprintf(stderr, "%s: cannot dump options to stream\n",
444 CMDLINE_PARSER_PACKAGE);
445 return EXIT_FAILURE;
446 }
447
448 if (args_info->help_given)
449 write_into_file(outfile, "help", 0, 0);
450 if (args_info->version_given)
451 write_into_file(outfile, "version", 0, 0);
452 if (args_info->debug_given)
453 write_into_file(outfile, "debug", 0, 0);
454 if (args_info->conf_given)
455 write_into_file(outfile, "conf", args_info->conf_orig, 0);
456 if (args_info->pidfile_given)
457 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
458 if (args_info->statedir_given)
459 write_into_file(outfile, "statedir", args_info->statedir_orig,
460 0);
461 if (args_info->dns_given)
462 write_into_file(outfile, "dns", args_info->dns_orig, 0);
463 if (args_info->listen_given)
464 write_into_file(outfile, "listen", args_info->listen_orig, 0);
465 if (args_info->remote_given)
466 write_into_file(outfile, "remote", args_info->remote_orig, 0);
467 if (args_info->contexts_given)
468 write_into_file(outfile, "contexts", args_info->contexts_orig,
469 0);
470 if (args_info->timelimit_given)
471 write_into_file(outfile, "timelimit", args_info->timelimit_orig,
472 0);
473 if (args_info->gtpversion_given)
474 write_into_file(outfile, "gtpversion",
475 args_info->gtpversion_orig, 0);
476 if (args_info->apn_given)
477 write_into_file(outfile, "apn", args_info->apn_orig, 0);
478 if (args_info->selmode_given)
479 write_into_file(outfile, "selmode", args_info->selmode_orig, 0);
480 if (args_info->rattype_given)
481 write_into_file(outfile, "rattype", args_info->rattype_orig, 0);
482 if (args_info->userloc_given)
483 write_into_file(outfile, "userloc", args_info->userloc_orig, 0);
484 if (args_info->rai_given)
485 write_into_file(outfile, "rai", args_info->rai_orig, 0);
486 if (args_info->mstz_given)
487 write_into_file(outfile, "mstz", args_info->mstz_orig, 0);
488 if (args_info->imeisv_given)
489 write_into_file(outfile, "imeisv", args_info->imeisv_orig, 0);
490 if (args_info->norecovery_given)
491 write_into_file(outfile, "norecovery", 0, 0);
492 if (args_info->imsi_given)
493 write_into_file(outfile, "imsi", args_info->imsi_orig, 0);
494 if (args_info->nsapi_given)
495 write_into_file(outfile, "nsapi", args_info->nsapi_orig, 0);
496 if (args_info->msisdn_given)
497 write_into_file(outfile, "msisdn", args_info->msisdn_orig, 0);
498 if (args_info->qos_given)
499 write_into_file(outfile, "qos", args_info->qos_orig, 0);
500 if (args_info->qose1_given)
501 write_into_file(outfile, "qose1", args_info->qose1_orig, 0);
502 if (args_info->qose2_given)
503 write_into_file(outfile, "qose2", args_info->qose2_orig, 0);
504 if (args_info->qose3_given)
505 write_into_file(outfile, "qose3", args_info->qose3_orig, 0);
506 if (args_info->qose4_given)
507 write_into_file(outfile, "qose4", args_info->qose4_orig, 0);
508 if (args_info->charging_given)
509 write_into_file(outfile, "charging", args_info->charging_orig,
510 0);
511 if (args_info->uid_given)
512 write_into_file(outfile, "uid", args_info->uid_orig, 0);
513 if (args_info->pwd_given)
514 write_into_file(outfile, "pwd", args_info->pwd_orig, 0);
515 if (args_info->createif_given)
516 write_into_file(outfile, "createif", 0, 0);
517 if (args_info->net_given)
518 write_into_file(outfile, "net", args_info->net_orig, 0);
519 if (args_info->defaultroute_given)
520 write_into_file(outfile, "defaultroute", 0, 0);
521 if (args_info->ipup_given)
522 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
523 if (args_info->ipdown_given)
524 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
525 if (args_info->pinghost_given)
526 write_into_file(outfile, "pinghost", args_info->pinghost_orig,
527 0);
528 if (args_info->pingrate_given)
529 write_into_file(outfile, "pingrate", args_info->pingrate_orig,
530 0);
531 if (args_info->pingsize_given)
532 write_into_file(outfile, "pingsize", args_info->pingsize_orig,
533 0);
534 if (args_info->pingcount_given)
535 write_into_file(outfile, "pingcount", args_info->pingcount_orig,
536 0);
537 if (args_info->pingquiet_given)
538 write_into_file(outfile, "pingquiet", 0, 0);
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800539 if (args_info->no_tx_gpdu_seq_given)
540 write_into_file(outfile, "no-tx-gpdu-seq", 0, 0);
Harald Weltef6214982017-09-24 10:23:24 +0800541
542 i = EXIT_SUCCESS;
543 return i;
544}
545
Harald Welte1b3e5772010-05-04 11:13:56 +0200546int
Harald Weltebed35df2011-11-02 13:06:18 +0100547cmdline_parser_file_save(const char *filename,
548 struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200549{
Harald Weltebed35df2011-11-02 13:06:18 +0100550 FILE *outfile;
551 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200552
Harald Weltebed35df2011-11-02 13:06:18 +0100553 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200554
Harald Weltebed35df2011-11-02 13:06:18 +0100555 if (!outfile) {
556 fprintf(stderr, "%s: cannot open file for writing: %s\n",
557 CMDLINE_PARSER_PACKAGE, filename);
558 return EXIT_FAILURE;
559 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200560
Harald Weltef6214982017-09-24 10:23:24 +0800561 i = cmdline_parser_dump(outfile, args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100562 fclose(outfile);
563
Harald Weltebed35df2011-11-02 13:06:18 +0100564 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200565}
566
Harald Weltebed35df2011-11-02 13:06:18 +0100567void cmdline_parser_free(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200568{
Harald Weltebed35df2011-11-02 13:06:18 +0100569 cmdline_parser_release(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200570}
571
Harald Weltef6214982017-09-24 10:23:24 +0800572/** @brief replacement of strdup, which is not standard */
Harald Weltebed35df2011-11-02 13:06:18 +0100573char *gengetopt_strdup(const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200574{
Harald Weltef6214982017-09-24 10:23:24 +0800575 char *result = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100576 if (!s)
577 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200578
Harald Weltebed35df2011-11-02 13:06:18 +0100579 result = (char *)malloc(strlen(s) + 1);
580 if (result == (char *)0)
581 return (char *)0;
582 strcpy(result, s);
583 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200584}
585
Harald Weltef6214982017-09-24 10:23:24 +0800586int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200587{
Harald Weltebed35df2011-11-02 13:06:18 +0100588 return cmdline_parser2(argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200589}
590
591int
Harald Weltef6214982017-09-24 10:23:24 +0800592cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info,
593 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200594{
Harald Weltebed35df2011-11-02 13:06:18 +0100595 int result;
Harald Weltef6214982017-09-24 10:23:24 +0800596 result = cmdline_parser_internal(argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200597
Harald Weltef6214982017-09-24 10:23:24 +0800598 if (result == EXIT_FAILURE) {
599 cmdline_parser_free(args_info);
600 exit(EXIT_FAILURE);
601 }
602
603 return result;
604}
605
606int
607cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info,
608 int override, int initialize, int check_required)
609{
610 int result;
611 struct cmdline_parser_params params;
612
613 params.override = override;
614 params.initialize = initialize;
615 params.check_required = check_required;
616 params.check_ambiguity = 0;
617 params.print_errors = 1;
618
619 result = cmdline_parser_internal(argc, argv, args_info, &params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200620
Harald Weltebed35df2011-11-02 13:06:18 +0100621 if (result == EXIT_FAILURE) {
622 cmdline_parser_free(args_info);
623 exit(EXIT_FAILURE);
624 }
625
626 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200627}
628
629int
Harald Weltebed35df2011-11-02 13:06:18 +0100630cmdline_parser_required(struct gengetopt_args_info *args_info,
631 const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200632{
Harald Weltef6214982017-09-24 10:23:24 +0800633 FIX_UNUSED(args_info);
634 FIX_UNUSED(prog_name);
Harald Weltebed35df2011-11-02 13:06:18 +0100635 return EXIT_SUCCESS;
Harald Welte1b3e5772010-05-04 11:13:56 +0200636}
637
Harald Weltef6214982017-09-24 10:23:24 +0800638static char *package_name = 0;
639
640/**
641 * @brief updates an option
642 * @param field the generic pointer to the field to update
643 * @param orig_field the pointer to the orig field
644 * @param field_given the pointer to the number of occurrence of this option
645 * @param prev_given the pointer to the number of occurrence already seen
646 * @param value the argument for this option (if null no arg was specified)
647 * @param possible_values the possible values for this option (if specified)
648 * @param default_value the default value (in case the option only accepts fixed values)
649 * @param arg_type the type of this option
650 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
651 * @param override @see cmdline_parser_params.override
652 * @param no_free whether to free a possible previous value
653 * @param multiple_option whether this is a multiple option
654 * @param long_opt the corresponding long option
655 * @param short_opt the corresponding short option (or '-' if none)
656 * @param additional_error possible further error specification
657 */
658static
659int update_arg(void *field, char **orig_field,
660 unsigned int *field_given, unsigned int *prev_given,
661 char *value, const char *possible_values[],
662 const char *default_value,
663 cmdline_parser_arg_type arg_type,
664 int check_ambiguity, int override,
665 int no_free, int multiple_option,
666 const char *long_opt, char short_opt,
667 const char *additional_error)
668{
669 char *stop_char = 0;
670 const char *val = value;
671 int found;
672 char **string_field;
673 FIX_UNUSED(field);
674
675 stop_char = 0;
676 found = 0;
677
678 if (!multiple_option && prev_given
679 && (*prev_given || (check_ambiguity && *field_given))) {
680 if (short_opt != '-')
681 fprintf(stderr,
682 "%s: `--%s' (`-%c') option given more than once%s\n",
683 package_name, long_opt, short_opt,
684 (additional_error ? additional_error : ""));
685 else
686 fprintf(stderr,
687 "%s: `--%s' option given more than once%s\n",
688 package_name, long_opt,
689 (additional_error ? additional_error : ""));
690 return 1; /* failure */
691 }
692
693 FIX_UNUSED(default_value);
694
695 if (field_given && *field_given && !override)
696 return 0;
697 if (prev_given)
698 (*prev_given)++;
699 if (field_given)
700 (*field_given)++;
701 if (possible_values)
702 val = possible_values[found];
703
704 switch (arg_type) {
705 case ARG_FLAG:
706 *((int *)field) = !*((int *)field);
707 break;
708 case ARG_INT:
709 if (val)
710 *((int *)field) = strtol(val, &stop_char, 0);
711 break;
712 case ARG_STRING:
713 if (val) {
714 string_field = (char **)field;
715 if (!no_free && *string_field)
716 free(*string_field); /* free previous string */
717 *string_field = gengetopt_strdup(val);
718 }
719 break;
720 default:
721 break;
722 };
723
724 /* check numeric conversion */
725 switch (arg_type) {
726 case ARG_INT:
727 if (val && !(stop_char && *stop_char == '\0')) {
728 fprintf(stderr, "%s: invalid numeric value: %s\n",
729 package_name, val);
730 return 1; /* failure */
731 }
732 break;
733 default:
734 ;
735 };
736
737 /* store the original value */
738 switch (arg_type) {
739 case ARG_NO:
740 case ARG_FLAG:
741 break;
742 default:
743 if (value && orig_field) {
744 if (no_free) {
745 *orig_field = value;
746 } else {
747 if (*orig_field)
748 free(*orig_field); /* free previous string */
749 *orig_field = gengetopt_strdup(value);
750 }
751 }
752 };
753
754 return 0; /* OK */
755}
756
Harald Welte1b3e5772010-05-04 11:13:56 +0200757int
Harald Weltef6214982017-09-24 10:23:24 +0800758cmdline_parser_internal(int argc, char **argv,
759 struct gengetopt_args_info *args_info,
760 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +0100761 const char *additional_error)
Harald Welte1b3e5772010-05-04 11:13:56 +0200762{
Harald Weltebed35df2011-11-02 13:06:18 +0100763 int c; /* Character of the parsed option. */
Harald Welte1b3e5772010-05-04 11:13:56 +0200764
Harald Weltef6214982017-09-24 10:23:24 +0800765 int error_occurred = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100766 struct gengetopt_args_info local_args_info;
Harald Welte1b3e5772010-05-04 11:13:56 +0200767
Harald Weltef6214982017-09-24 10:23:24 +0800768 int override;
769 int initialize;
770 int check_required;
771 int check_ambiguity;
772
773 package_name = argv[0];
774
775 override = params->override;
776 initialize = params->initialize;
777 check_required = params->check_required;
778 check_ambiguity = params->check_ambiguity;
779
Harald Weltebed35df2011-11-02 13:06:18 +0100780 if (initialize)
781 cmdline_parser_init(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200782
Harald Weltebed35df2011-11-02 13:06:18 +0100783 cmdline_parser_init(&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000784
Harald Weltebed35df2011-11-02 13:06:18 +0100785 optarg = 0;
786 optind = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800787 opterr = params->print_errors;
Harald Weltebed35df2011-11-02 13:06:18 +0100788 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000789
Harald Weltebed35df2011-11-02 13:06:18 +0100790 while (1) {
791 int option_index = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200792
Harald Weltebed35df2011-11-02 13:06:18 +0100793 static struct option long_options[] = {
794 {"help", 0, NULL, 'h'},
795 {"version", 0, NULL, 'V'},
796 {"debug", 0, NULL, 'd'},
797 {"conf", 1, NULL, 'c'},
798 {"pidfile", 1, NULL, 0},
799 {"statedir", 1, NULL, 0},
800 {"dns", 1, NULL, 0},
801 {"listen", 1, NULL, 'l'},
802 {"remote", 1, NULL, 'r'},
803 {"contexts", 1, NULL, 0},
804 {"timelimit", 1, NULL, 0},
805 {"gtpversion", 1, NULL, 0},
806 {"apn", 1, NULL, 'a'},
807 {"selmode", 1, NULL, 0},
808 {"rattype", 1, NULL, 0},
809 {"userloc", 1, NULL, 0},
810 {"rai", 1, NULL, 0},
811 {"mstz", 1, NULL, 0},
812 {"imeisv", 1, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800813 {"norecovery", 0, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100814 {"imsi", 1, NULL, 'i'},
815 {"nsapi", 1, NULL, 0},
816 {"msisdn", 1, NULL, 'm'},
817 {"qos", 1, NULL, 'q'},
818 {"qose1", 1, NULL, 0},
819 {"qose2", 1, NULL, 0},
820 {"qose3", 1, NULL, 0},
821 {"qose4", 1, NULL, 0},
822 {"charging", 1, NULL, 0},
823 {"uid", 1, NULL, 'u'},
824 {"pwd", 1, NULL, 'p'},
825 {"createif", 0, NULL, 0},
826 {"net", 1, NULL, 'n'},
827 {"defaultroute", 0, NULL, 0},
828 {"ipup", 1, NULL, 0},
829 {"ipdown", 1, NULL, 0},
830 {"pinghost", 1, NULL, 0},
831 {"pingrate", 1, NULL, 0},
832 {"pingsize", 1, NULL, 0},
833 {"pingcount", 1, NULL, 0},
834 {"pingquiet", 0, NULL, 0},
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800835 {"no-tx-gpdu-seq", 0, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800836 {0, 0, 0, 0}
Harald Weltebed35df2011-11-02 13:06:18 +0100837 };
jjako52c24142002-12-16 13:33:51 +0000838
Harald Weltebed35df2011-11-02 13:06:18 +0100839 c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:",
840 long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000841
Harald Weltebed35df2011-11-02 13:06:18 +0100842 if (c == -1)
843 break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000844
Harald Weltebed35df2011-11-02 13:06:18 +0100845 switch (c) {
846 case 'h': /* Print help and exit. */
847 cmdline_parser_print_help();
848 cmdline_parser_free(&local_args_info);
849 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000850
Harald Weltebed35df2011-11-02 13:06:18 +0100851 case 'V': /* Print version and exit. */
852 cmdline_parser_print_version();
853 cmdline_parser_free(&local_args_info);
854 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000855
Harald Weltebed35df2011-11-02 13:06:18 +0100856 case 'd': /* Run in debug mode. */
jjako52c24142002-12-16 13:33:51 +0000857
Harald Weltef6214982017-09-24 10:23:24 +0800858 if (update_arg
859 ((void *)&(args_info->debug_flag), 0,
860 &(args_info->debug_given),
861 &(local_args_info.debug_given), optarg, 0, 0,
862 ARG_FLAG, check_ambiguity, override, 1, 0, "debug",
863 'd', additional_error))
864 goto failure;
865
866 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100867 case 'c': /* Read configuration file. */
jjako52c24142002-12-16 13:33:51 +0000868
Harald Weltef6214982017-09-24 10:23:24 +0800869 if (update_arg((void *)&(args_info->conf_arg),
870 &(args_info->conf_orig),
871 &(args_info->conf_given),
872 &(local_args_info.conf_given), optarg, 0,
873 0, ARG_STRING, check_ambiguity, override,
874 0, 0, "conf", 'c', additional_error))
875 goto failure;
876
877 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100878 case 'l': /* Local interface. */
jjako52c24142002-12-16 13:33:51 +0000879
Harald Weltef6214982017-09-24 10:23:24 +0800880 if (update_arg((void *)&(args_info->listen_arg),
881 &(args_info->listen_orig),
882 &(args_info->listen_given),
883 &(local_args_info.listen_given), optarg,
884 0, 0, ARG_STRING, check_ambiguity,
885 override, 0, 0, "listen", 'l',
886 additional_error))
887 goto failure;
888
889 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100890 case 'r': /* Remote host. */
jjako52c24142002-12-16 13:33:51 +0000891
Harald Weltef6214982017-09-24 10:23:24 +0800892 if (update_arg((void *)&(args_info->remote_arg),
893 &(args_info->remote_orig),
894 &(args_info->remote_given),
895 &(local_args_info.remote_given), optarg,
896 0, 0, ARG_STRING, check_ambiguity,
897 override, 0, 0, "remote", 'r',
898 additional_error))
899 goto failure;
900
901 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100902 case 'a': /* Access point name. */
jjako52c24142002-12-16 13:33:51 +0000903
Harald Weltef6214982017-09-24 10:23:24 +0800904 if (update_arg((void *)&(args_info->apn_arg),
905 &(args_info->apn_orig),
906 &(args_info->apn_given),
907 &(local_args_info.apn_given), optarg, 0,
908 "internet", ARG_STRING, check_ambiguity,
909 override, 0, 0, "apn", 'a',
910 additional_error))
911 goto failure;
912
913 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100914 case 'i': /* IMSI. */
jjako52c24142002-12-16 13:33:51 +0000915
Harald Weltef6214982017-09-24 10:23:24 +0800916 if (update_arg((void *)&(args_info->imsi_arg),
917 &(args_info->imsi_orig),
918 &(args_info->imsi_given),
919 &(local_args_info.imsi_given), optarg, 0,
920 "240010123456789", ARG_STRING,
921 check_ambiguity, override, 0, 0, "imsi",
922 'i', additional_error))
923 goto failure;
924
925 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100926 case 'm': /* Mobile Station ISDN number. */
jjako52c24142002-12-16 13:33:51 +0000927
Harald Weltef6214982017-09-24 10:23:24 +0800928 if (update_arg((void *)&(args_info->msisdn_arg),
929 &(args_info->msisdn_orig),
930 &(args_info->msisdn_given),
931 &(local_args_info.msisdn_given), optarg,
932 0, "46702123456", ARG_STRING,
933 check_ambiguity, override, 0, 0,
934 "msisdn", 'm', additional_error))
935 goto failure;
936
937 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100938 case 'q': /* Requested quality of service. */
jjako52c24142002-12-16 13:33:51 +0000939
Harald Weltef6214982017-09-24 10:23:24 +0800940 if (update_arg((void *)&(args_info->qos_arg),
941 &(args_info->qos_orig),
942 &(args_info->qos_given),
943 &(local_args_info.qos_given), optarg, 0,
944 "0x000b921f", ARG_INT, check_ambiguity,
945 override, 0, 0, "qos", 'q',
946 additional_error))
947 goto failure;
948
949 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100950 case 'u': /* Login user ID. */
jjako52c24142002-12-16 13:33:51 +0000951
Harald Weltef6214982017-09-24 10:23:24 +0800952 if (update_arg((void *)&(args_info->uid_arg),
953 &(args_info->uid_orig),
954 &(args_info->uid_given),
955 &(local_args_info.uid_given), optarg, 0,
956 "mig", ARG_STRING, check_ambiguity,
957 override, 0, 0, "uid", 'u',
958 additional_error))
959 goto failure;
960
961 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100962 case 'p': /* Login password. */
jjako52c24142002-12-16 13:33:51 +0000963
Harald Weltef6214982017-09-24 10:23:24 +0800964 if (update_arg((void *)&(args_info->pwd_arg),
965 &(args_info->pwd_orig),
966 &(args_info->pwd_given),
967 &(local_args_info.pwd_given), optarg, 0,
968 "hemmelig", ARG_STRING, check_ambiguity,
969 override, 0, 0, "pwd", 'p',
970 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +0100971 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +0800972
973 break;
974 case 'n': /* Network address for local interface. */
975
976 if (update_arg((void *)&(args_info->net_arg),
977 &(args_info->net_orig),
978 &(args_info->net_given),
979 &(local_args_info.net_given), optarg, 0,
980 0, ARG_STRING, check_ambiguity, override,
981 0, 0, "net", 'n', additional_error))
982 goto failure;
983
Harald Weltebed35df2011-11-02 13:06:18 +0100984 break;
jjako193e8b12003-11-10 12:31:41 +0000985
Harald Weltebed35df2011-11-02 13:06:18 +0100986 case 0: /* Long option with no short option */
987 /* Filename of process id file. */
988 if (strcmp(long_options[option_index].name, "pidfile")
989 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +0800990
991 if (update_arg
992 ((void *)&(args_info->pidfile_arg),
993 &(args_info->pidfile_orig),
994 &(args_info->pidfile_given),
995 &(local_args_info.pidfile_given), optarg,
996 0, "./sgsnemu.pid", ARG_STRING,
997 check_ambiguity, override, 0, 0, "pidfile",
998 '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +0100999 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001000
Harald Weltebed35df2011-11-02 13:06:18 +01001001 }
1002 /* Directory of nonvolatile data. */
1003 else if (strcmp
1004 (long_options[option_index].name,
1005 "statedir") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001006
1007 if (update_arg
1008 ((void *)&(args_info->statedir_arg),
1009 &(args_info->statedir_orig),
1010 &(args_info->statedir_given),
1011 &(local_args_info.statedir_given), optarg,
1012 0, "./", ARG_STRING, check_ambiguity,
1013 override, 0, 0, "statedir", '-',
1014 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001015 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001016
Harald Weltebed35df2011-11-02 13:06:18 +01001017 }
1018 /* DNS Server to use. */
1019 else if (strcmp(long_options[option_index].name, "dns")
1020 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001021
1022 if (update_arg((void *)&(args_info->dns_arg),
1023 &(args_info->dns_orig),
1024 &(args_info->dns_given),
1025 &(local_args_info.dns_given),
1026 optarg, 0, 0, ARG_STRING,
1027 check_ambiguity, override, 0, 0,
1028 "dns", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001029 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001030
Harald Weltebed35df2011-11-02 13:06:18 +01001031 }
1032 /* Number of contexts. */
1033 else if (strcmp
1034 (long_options[option_index].name,
1035 "contexts") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001036
1037 if (update_arg
1038 ((void *)&(args_info->contexts_arg),
1039 &(args_info->contexts_orig),
1040 &(args_info->contexts_given),
1041 &(local_args_info.contexts_given), optarg,
1042 0, "1", ARG_INT, check_ambiguity, override,
1043 0, 0, "contexts", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001044 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001045
Harald Weltebed35df2011-11-02 13:06:18 +01001046 }
1047 /* Exit after timelimit seconds. */
1048 else if (strcmp
1049 (long_options[option_index].name,
1050 "timelimit") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001051
1052 if (update_arg
1053 ((void *)&(args_info->timelimit_arg),
1054 &(args_info->timelimit_orig),
1055 &(args_info->timelimit_given),
1056 &(local_args_info.timelimit_given), optarg,
1057 0, "0", ARG_INT, check_ambiguity, override,
1058 0, 0, "timelimit", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001059 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001060
Harald Weltebed35df2011-11-02 13:06:18 +01001061 }
1062 /* GTP version to use. */
1063 else if (strcmp
1064 (long_options[option_index].name,
1065 "gtpversion") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001066
1067 if (update_arg
1068 ((void *)&(args_info->gtpversion_arg),
1069 &(args_info->gtpversion_orig),
1070 &(args_info->gtpversion_given),
1071 &(local_args_info.gtpversion_given),
1072 optarg, 0, "1", ARG_INT, check_ambiguity,
1073 override, 0, 0, "gtpversion", '-',
1074 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001075 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001076
Harald Weltebed35df2011-11-02 13:06:18 +01001077 }
1078 /* Selection mode. */
1079 else if (strcmp
1080 (long_options[option_index].name,
1081 "selmode") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001082
1083 if (update_arg
1084 ((void *)&(args_info->selmode_arg),
1085 &(args_info->selmode_orig),
1086 &(args_info->selmode_given),
1087 &(local_args_info.selmode_given), optarg,
1088 0, "0x01", ARG_INT, check_ambiguity,
1089 override, 0, 0, "selmode", '-',
1090 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001091 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001092
Harald Weltebed35df2011-11-02 13:06:18 +01001093 }
1094 /* Radio Access Technology Type. */
1095 else if (strcmp
1096 (long_options[option_index].name,
1097 "rattype") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001098
1099 if (update_arg
1100 ((void *)&(args_info->rattype_arg),
1101 &(args_info->rattype_orig),
1102 &(args_info->rattype_given),
1103 &(local_args_info.rattype_given), optarg,
1104 0, "1", ARG_INT, check_ambiguity, override,
1105 0, 0, "rattype", '-', additional_error))
1106 goto failure;
1107
Harald Weltebed35df2011-11-02 13:06:18 +01001108 }
1109 /* User Location Information. */
1110 else if (strcmp
1111 (long_options[option_index].name,
1112 "userloc") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001113
1114 if (update_arg
1115 ((void *)&(args_info->userloc_arg),
1116 &(args_info->userloc_orig),
1117 &(args_info->userloc_given),
1118 &(local_args_info.userloc_given), optarg,
1119 0, "02509946241207", ARG_STRING,
1120 check_ambiguity, override, 0, 0, "userloc",
1121 '-', additional_error))
1122 goto failure;
1123
Harald Weltebed35df2011-11-02 13:06:18 +01001124 }
1125 /* Routing Area Information. */
1126 else if (strcmp(long_options[option_index].name, "rai")
1127 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001128
1129 if (update_arg((void *)&(args_info->rai_arg),
1130 &(args_info->rai_orig),
1131 &(args_info->rai_given),
1132 &(local_args_info.rai_given),
1133 optarg, 0, "02509946241207",
1134 ARG_STRING, check_ambiguity,
1135 override, 0, 0, "rai", '-',
1136 additional_error))
1137 goto failure;
1138
Harald Weltebed35df2011-11-02 13:06:18 +01001139 }
Harald Weltef6214982017-09-24 10:23:24 +08001140 /* MS Time Zone. */
Harald Weltebed35df2011-11-02 13:06:18 +01001141 else if (strcmp(long_options[option_index].name, "mstz")
1142 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001143
1144 if (update_arg((void *)&(args_info->mstz_arg),
1145 &(args_info->mstz_orig),
1146 &(args_info->mstz_given),
1147 &(local_args_info.mstz_given),
1148 optarg, 0, "0", ARG_STRING,
1149 check_ambiguity, override, 0, 0,
1150 "mstz", '-', additional_error))
1151 goto failure;
1152
Harald Weltebed35df2011-11-02 13:06:18 +01001153 }
Harald Weltef6214982017-09-24 10:23:24 +08001154 /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */
Harald Weltebed35df2011-11-02 13:06:18 +01001155 else if (strcmp
1156 (long_options[option_index].name,
1157 "imeisv") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001158
1159 if (update_arg((void *)&(args_info->imeisv_arg),
1160 &(args_info->imeisv_orig),
1161 &(args_info->imeisv_given),
1162 &(local_args_info.imeisv_given),
1163 optarg, 0, "2143658709214365",
1164 ARG_STRING, check_ambiguity,
1165 override, 0, 0, "imeisv", '-',
1166 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001167 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001168
Harald Weltebed35df2011-11-02 13:06:18 +01001169 }
1170 /* Do not send recovery. */
1171 else if (strcmp
1172 (long_options[option_index].name,
1173 "norecovery") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001174
1175 if (update_arg
1176 ((void *)&(args_info->norecovery_flag), 0,
1177 &(args_info->norecovery_given),
1178 &(local_args_info.norecovery_given),
1179 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1180 override, 1, 0, "norecovery", '-',
1181 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001182 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001183
1184 }
1185 /* NSAPI. */
1186 else if (strcmp
1187 (long_options[option_index].name,
1188 "nsapi") == 0) {
1189
1190 if (update_arg((void *)&(args_info->nsapi_arg),
1191 &(args_info->nsapi_orig),
1192 &(args_info->nsapi_given),
1193 &(local_args_info.nsapi_given),
1194 optarg, 0, "0", ARG_INT,
1195 check_ambiguity, override, 0, 0,
1196 "nsapi", '-', additional_error))
1197 goto failure;
1198
1199 }
1200 /* Requested quality of service Extension 1. */
1201 else if (strcmp
1202 (long_options[option_index].name,
1203 "qose1") == 0) {
1204
1205 if (update_arg((void *)&(args_info->qose1_arg),
1206 &(args_info->qose1_orig),
1207 &(args_info->qose1_given),
1208 &(local_args_info.qose1_given),
1209 optarg, 0, "0x9396404074f9ffff",
1210 ARG_INT, check_ambiguity,
1211 override, 0, 0, "qose1", '-',
1212 additional_error))
1213 goto failure;
1214
1215 }
1216 /* Requested quality of service Extension 2. */
1217 else if (strcmp
1218 (long_options[option_index].name,
1219 "qose2") == 0) {
1220
1221 if (update_arg((void *)&(args_info->qose2_arg),
1222 &(args_info->qose2_orig),
1223 &(args_info->qose2_given),
1224 &(local_args_info.qose2_given),
1225 optarg, 0, "0x11", ARG_INT,
1226 check_ambiguity, override, 0, 0,
1227 "qose2", '-', additional_error))
1228 goto failure;
1229
1230 }
1231 /* Requested quality of service Extension 3. */
1232 else if (strcmp
1233 (long_options[option_index].name,
1234 "qose3") == 0) {
1235
1236 if (update_arg((void *)&(args_info->qose3_arg),
1237 &(args_info->qose3_orig),
1238 &(args_info->qose3_given),
1239 &(local_args_info.qose3_given),
1240 optarg, 0, "0x0101", ARG_INT,
1241 check_ambiguity, override, 0, 0,
1242 "qose3", '-', additional_error))
1243 goto failure;
1244
1245 }
1246 /* Requested quality of service Extension 4. */
1247 else if (strcmp
1248 (long_options[option_index].name,
1249 "qose4") == 0) {
1250
1251 if (update_arg((void *)&(args_info->qose4_arg),
1252 &(args_info->qose4_orig),
1253 &(args_info->qose4_given),
1254 &(local_args_info.qose4_given),
1255 optarg, 0, "0x4040", ARG_INT,
1256 check_ambiguity, override, 0, 0,
1257 "qose4", '-', additional_error))
1258 goto failure;
1259
1260 }
1261 /* Charging characteristics. */
1262 else if (strcmp
1263 (long_options[option_index].name,
1264 "charging") == 0) {
1265
1266 if (update_arg
1267 ((void *)&(args_info->charging_arg),
1268 &(args_info->charging_orig),
1269 &(args_info->charging_given),
1270 &(local_args_info.charging_given), optarg,
1271 0, "0x0800", ARG_INT, check_ambiguity,
1272 override, 0, 0, "charging", '-',
1273 additional_error))
1274 goto failure;
1275
1276 }
1277 /* Create local network interface. */
1278 else if (strcmp
1279 (long_options[option_index].name,
1280 "createif") == 0) {
1281
1282 if (update_arg
1283 ((void *)&(args_info->createif_flag), 0,
1284 &(args_info->createif_given),
1285 &(local_args_info.createif_given), optarg,
1286 0, 0, ARG_FLAG, check_ambiguity, override,
1287 1, 0, "createif", '-', additional_error))
1288 goto failure;
1289
1290 }
1291 /* Create default route. */
1292 else if (strcmp
1293 (long_options[option_index].name,
1294 "defaultroute") == 0) {
1295
1296 if (update_arg
1297 ((void *)&(args_info->defaultroute_flag), 0,
1298 &(args_info->defaultroute_given),
1299 &(local_args_info.defaultroute_given),
1300 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1301 override, 1, 0, "defaultroute", '-',
1302 additional_error))
1303 goto failure;
1304
1305 }
1306 /* Script to run after link-up. */
1307 else if (strcmp(long_options[option_index].name, "ipup")
1308 == 0) {
1309
1310 if (update_arg((void *)&(args_info->ipup_arg),
1311 &(args_info->ipup_orig),
1312 &(args_info->ipup_given),
1313 &(local_args_info.ipup_given),
1314 optarg, 0, 0, ARG_STRING,
1315 check_ambiguity, override, 0, 0,
1316 "ipup", '-', additional_error))
1317 goto failure;
1318
1319 }
1320 /* Script to run after link-down. */
1321 else if (strcmp
1322 (long_options[option_index].name,
1323 "ipdown") == 0) {
1324
1325 if (update_arg((void *)&(args_info->ipdown_arg),
1326 &(args_info->ipdown_orig),
1327 &(args_info->ipdown_given),
1328 &(local_args_info.ipdown_given),
1329 optarg, 0, 0, ARG_STRING,
1330 check_ambiguity, override, 0, 0,
1331 "ipdown", '-', additional_error))
1332 goto failure;
1333
1334 }
1335 /* Ping remote host. */
1336 else if (strcmp
1337 (long_options[option_index].name,
1338 "pinghost") == 0) {
1339
1340 if (update_arg
1341 ((void *)&(args_info->pinghost_arg),
1342 &(args_info->pinghost_orig),
1343 &(args_info->pinghost_given),
1344 &(local_args_info.pinghost_given), optarg,
1345 0, 0, ARG_STRING, check_ambiguity,
1346 override, 0, 0, "pinghost", '-',
1347 additional_error))
1348 goto failure;
1349
1350 }
1351 /* Number of ping req per second. */
1352 else if (strcmp
1353 (long_options[option_index].name,
1354 "pingrate") == 0) {
1355
1356 if (update_arg
1357 ((void *)&(args_info->pingrate_arg),
1358 &(args_info->pingrate_orig),
1359 &(args_info->pingrate_given),
1360 &(local_args_info.pingrate_given), optarg,
1361 0, "1", ARG_INT, check_ambiguity, override,
1362 0, 0, "pingrate", '-', additional_error))
1363 goto failure;
1364
1365 }
1366 /* Number of ping data bytes. */
1367 else if (strcmp
1368 (long_options[option_index].name,
1369 "pingsize") == 0) {
1370
1371 if (update_arg
1372 ((void *)&(args_info->pingsize_arg),
1373 &(args_info->pingsize_orig),
1374 &(args_info->pingsize_given),
1375 &(local_args_info.pingsize_given), optarg,
1376 0, "56", ARG_INT, check_ambiguity,
1377 override, 0, 0, "pingsize", '-',
1378 additional_error))
1379 goto failure;
1380
1381 }
1382 /* Number of ping req to send. */
1383 else if (strcmp
1384 (long_options[option_index].name,
1385 "pingcount") == 0) {
1386
1387 if (update_arg
1388 ((void *)&(args_info->pingcount_arg),
1389 &(args_info->pingcount_orig),
1390 &(args_info->pingcount_given),
1391 &(local_args_info.pingcount_given), optarg,
1392 0, "0", ARG_INT, check_ambiguity, override,
1393 0, 0, "pingcount", '-', additional_error))
1394 goto failure;
1395
1396 }
1397 /* Do not print ping packet info. */
1398 else if (strcmp
1399 (long_options[option_index].name,
1400 "pingquiet") == 0) {
1401
1402 if (update_arg
1403 ((void *)&(args_info->pingquiet_flag), 0,
1404 &(args_info->pingquiet_given),
1405 &(local_args_info.pingquiet_given), optarg,
1406 0, 0, ARG_FLAG, check_ambiguity, override,
1407 1, 0, "pingquiet", '-', additional_error))
1408 goto failure;
1409
Harald Weltebed35df2011-11-02 13:06:18 +01001410 }
Harald Weltefbb9c7f2017-09-24 11:50:20 +08001411 /* Don't transmit G-PDU sequence nums. */
1412 else if (strcmp
1413 (long_options[option_index].name,
1414 "no-tx-gpdu-seq") == 0) {
1415
1416 if (update_arg
1417 ((void *)&(args_info->no_tx_gpdu_seq_flag),
1418 0, &(args_info->no_tx_gpdu_seq_given),
1419 &(local_args_info.no_tx_gpdu_seq_given),
1420 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1421 override, 1, 0, "no-tx-gpdu-seq", '-',
1422 additional_error))
1423 goto failure;
1424
1425 }
jjako52c24142002-12-16 13:33:51 +00001426
Harald Weltebed35df2011-11-02 13:06:18 +01001427 break;
1428 case '?': /* Invalid option. */
1429 /* `getopt_long' already printed an error message. */
1430 goto failure;
jjako52c24142002-12-16 13:33:51 +00001431
Harald Weltebed35df2011-11-02 13:06:18 +01001432 default: /* bug: option not considered. */
1433 fprintf(stderr, "%s: option unknown: %c%s\n",
1434 CMDLINE_PARSER_PACKAGE, c,
1435 (additional_error ? additional_error : ""));
1436 abort();
1437 } /* switch */
1438 } /* while */
jjako52c24142002-12-16 13:33:51 +00001439
Harald Weltebed35df2011-11-02 13:06:18 +01001440 cmdline_parser_release(&local_args_info);
jjako52c24142002-12-16 13:33:51 +00001441
Harald Weltef6214982017-09-24 10:23:24 +08001442 if (error_occurred)
Harald Weltebed35df2011-11-02 13:06:18 +01001443 return (EXIT_FAILURE);
Harald Welte1b3e5772010-05-04 11:13:56 +02001444
Harald Weltebed35df2011-11-02 13:06:18 +01001445 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001446
1447failure:
Harald Weltebed35df2011-11-02 13:06:18 +01001448
1449 cmdline_parser_release(&local_args_info);
1450 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +00001451}
1452
Harald Welte1b3e5772010-05-04 11:13:56 +02001453#ifndef CONFIG_FILE_LINE_SIZE
1454#define CONFIG_FILE_LINE_SIZE 2048
1455#endif
1456#define ADDITIONAL_ERROR " in configuration file "
1457
1458#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1459/* 3 is for "--" and "=" */
1460
Harald Weltef6214982017-09-24 10:23:24 +08001461static int _cmdline_parser_configfile(const char *filename, int *my_argc)
jjako52c24142002-12-16 13:33:51 +00001462{
Harald Weltebed35df2011-11-02 13:06:18 +01001463 FILE *file;
Harald Weltef6214982017-09-24 10:23:24 +08001464 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
Harald Weltebed35df2011-11-02 13:06:18 +01001465 char linebuf[CONFIG_FILE_LINE_SIZE];
1466 int line_num = 0;
Harald Weltef6214982017-09-24 10:23:24 +08001467 int result = 0, equal;
Harald Weltebed35df2011-11-02 13:06:18 +01001468 char *fopt, *farg;
1469 char *str_index;
1470 size_t len, next_token;
1471 char delimiter;
Harald Welte1b3e5772010-05-04 11:13:56 +02001472
Harald Weltef6214982017-09-24 10:23:24 +08001473 if ((file = fopen(filename, "r")) == 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001474 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1475 CMDLINE_PARSER_PACKAGE, filename);
Harald Weltef6214982017-09-24 10:23:24 +08001476 return EXIT_FAILURE;
Harald Weltebed35df2011-11-02 13:06:18 +01001477 }
jjako52c24142002-12-16 13:33:51 +00001478
Harald Weltef6214982017-09-24 10:23:24 +08001479 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001480 ++line_num;
1481 my_argv[0] = '\0';
1482 len = strlen(linebuf);
1483 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) {
1484 fprintf(stderr,
1485 "%s:%s:%d: Line too long in configuration file\n",
1486 CMDLINE_PARSER_PACKAGE, filename, line_num);
1487 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001488 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001489 }
jjako52c24142002-12-16 13:33:51 +00001490
Harald Weltebed35df2011-11-02 13:06:18 +01001491 /* find first non-whitespace character in the line */
1492 next_token = strspn(linebuf, " \t\r\n");
1493 str_index = linebuf + next_token;
jjako52c24142002-12-16 13:33:51 +00001494
Harald Weltebed35df2011-11-02 13:06:18 +01001495 if (str_index[0] == '\0' || str_index[0] == '#')
1496 continue; /* empty line or comment line is skipped */
Harald Welte1b3e5772010-05-04 11:13:56 +02001497
Harald Weltebed35df2011-11-02 13:06:18 +01001498 fopt = str_index;
Harald Welte1b3e5772010-05-04 11:13:56 +02001499
Harald Weltebed35df2011-11-02 13:06:18 +01001500 /* truncate fopt at the end of the first non-valid character */
1501 next_token = strcspn(fopt, " \t\r\n=");
Harald Welte1b3e5772010-05-04 11:13:56 +02001502
Harald Weltebed35df2011-11-02 13:06:18 +01001503 if (fopt[next_token] == '\0') { /* the line is over */
Harald Weltef6214982017-09-24 10:23:24 +08001504 farg = 0;
Harald Weltebed35df2011-11-02 13:06:18 +01001505 equal = 0;
1506 goto noarg;
1507 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001508
Harald Weltebed35df2011-11-02 13:06:18 +01001509 /* remember if equal sign is present */
1510 equal = (fopt[next_token] == '=');
1511 fopt[next_token++] = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001512
Harald Weltebed35df2011-11-02 13:06:18 +01001513 /* advance pointers to the next token after the end of fopt */
1514 next_token += strspn(fopt + next_token, " \t\r\n");
Harald Weltef6214982017-09-24 10:23:24 +08001515
Harald Weltebed35df2011-11-02 13:06:18 +01001516 /* check for the presence of equal sign, and if so, skip it */
1517 if (!equal)
1518 if ((equal = (fopt[next_token] == '='))) {
1519 next_token++;
1520 next_token +=
1521 strspn(fopt + next_token, " \t\r\n");
1522 }
1523 str_index += next_token;
Harald Welte1b3e5772010-05-04 11:13:56 +02001524
Harald Weltebed35df2011-11-02 13:06:18 +01001525 /* find argument */
1526 farg = str_index;
1527 if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */
1528 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1529 if (!str_index) {
1530 fprintf
1531 (stderr,
1532 "%s:%s:%d: unterminated string in configuration file\n",
1533 CMDLINE_PARSER_PACKAGE, filename,
1534 line_num);
1535 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001536 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001537 }
1538 } else { /* read up the remaining part up to a delimiter */
1539 next_token = strcspn(farg, " \t\r\n#\'\"");
1540 str_index += next_token;
1541 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001542
Harald Weltebed35df2011-11-02 13:06:18 +01001543 /* truncate farg at the delimiter and store it for further check */
1544 delimiter = *str_index, *str_index++ = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001545
Harald Weltebed35df2011-11-02 13:06:18 +01001546 /* everything but comment is illegal at the end of line */
1547 if (delimiter != '\0' && delimiter != '#') {
1548 str_index += strspn(str_index, " \t\r\n");
1549 if (*str_index != '\0' && *str_index != '#') {
1550 fprintf
1551 (stderr,
1552 "%s:%s:%d: malformed string in configuration file\n",
1553 CMDLINE_PARSER_PACKAGE, filename,
1554 line_num);
1555 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001556 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001557 }
1558 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001559
Harald Weltebed35df2011-11-02 13:06:18 +01001560noarg:
Harald Weltef6214982017-09-24 10:23:24 +08001561 if (!strcmp(fopt, "include")) {
1562 if (farg && *farg) {
1563 result =
1564 _cmdline_parser_configfile(farg, my_argc);
1565 } else {
1566 fprintf(stderr,
1567 "%s:%s:%d: include requires a filename argument.\n",
1568 CMDLINE_PARSER_PACKAGE, filename,
1569 line_num);
1570 }
1571 continue;
1572 }
Harald Weltebed35df2011-11-02 13:06:18 +01001573 len = strlen(fopt);
Harald Weltebed35df2011-11-02 13:06:18 +01001574 strcat(my_argv, len > 1 ? "--" : "-");
1575 strcat(my_argv, fopt);
1576 if (len > 1 && ((farg && *farg) || equal))
1577 strcat(my_argv, "=");
1578 if (farg && *farg)
1579 strcat(my_argv, farg);
Harald Weltef6214982017-09-24 10:23:24 +08001580 ++(*my_argc);
Harald Welte1b3e5772010-05-04 11:13:56 +02001581
Harald Weltebed35df2011-11-02 13:06:18 +01001582 cmd_line_list_tmp =
1583 (struct line_list *)malloc(sizeof(struct line_list));
1584 cmd_line_list_tmp->next = cmd_line_list;
1585 cmd_line_list = cmd_line_list_tmp;
1586 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1587 } /* while */
jjako52c24142002-12-16 13:33:51 +00001588
Harald Weltebed35df2011-11-02 13:06:18 +01001589 if (file)
1590 fclose(file);
Harald Weltef6214982017-09-24 10:23:24 +08001591 return result;
1592}
1593
1594int
1595cmdline_parser_configfile(const char *filename,
1596 struct gengetopt_args_info *args_info,
1597 int override, int initialize, int check_required)
1598{
1599 struct cmdline_parser_params params;
1600
1601 params.override = override;
1602 params.initialize = initialize;
1603 params.check_required = check_required;
1604 params.check_ambiguity = 0;
1605 params.print_errors = 1;
1606
1607 return cmdline_parser_config_file(filename, args_info, &params);
1608}
1609
1610int
1611cmdline_parser_config_file(const char *filename,
1612 struct gengetopt_args_info *args_info,
1613 struct cmdline_parser_params *params)
1614{
1615 int i, result;
1616 int my_argc = 1;
1617 char **my_argv_arg;
1618 char *additional_error;
1619
1620 /* store the program name */
1621 cmd_line_list_tmp =
1622 (struct line_list *)malloc(sizeof(struct line_list));
1623 cmd_line_list_tmp->next = cmd_line_list;
1624 cmd_line_list = cmd_line_list_tmp;
1625 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1626
1627 result = _cmdline_parser_configfile(filename, &my_argc);
1628
1629 if (result != EXIT_FAILURE) {
1630 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1631 cmd_line_list_tmp = cmd_line_list;
1632
1633 for (i = my_argc - 1; i >= 0; --i) {
1634 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1635 cmd_line_list_tmp = cmd_line_list_tmp->next;
1636 }
1637
1638 my_argv_arg[my_argc] = 0;
1639
1640 additional_error =
1641 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) +
1642 1);
1643 strcpy(additional_error, ADDITIONAL_ERROR);
1644 strcat(additional_error, filename);
1645 result =
1646 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1647 params, additional_error);
1648
1649 free(additional_error);
1650 free(my_argv_arg);
1651 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001652
Harald Weltebed35df2011-11-02 13:06:18 +01001653 free_cmd_list();
1654 if (result == EXIT_FAILURE) {
1655 cmdline_parser_free(args_info);
1656 exit(EXIT_FAILURE);
1657 }
1658
1659 return result;
jjako52c24142002-12-16 13:33:51 +00001660}