blob: 1c02baa7371a2296139f760732503b221b10cdb8 [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",
Harald Welte73abc382017-10-10 08:50:11 +080073 " --tun-device=STRING Name of the local network interface",
Harald Weltef6214982017-09-24 10:23:24 +080074 " --pinghost=STRING Ping remote host",
75 " --pingrate=INT Number of ping req per second (default=`1')",
76 " --pingsize=INT Number of ping data bytes (default=`56')",
77 " --pingcount=INT Number of ping req to send (default=`0')",
78 " --pingquiet Do not print ping packet info (default=off)",
Harald Weltefbb9c7f2017-09-24 11:50:20 +080079 " --no-tx-gpdu-seq Don't transmit G-PDU sequence nums\n (default=off)",
Harald Welte840a8e92017-09-24 18:12:40 +080080 " -t, --pdp-type=(v4|v6) PDP Type (default=`v4')",
Harald Weltebed35df2011-11-02 13:06:18 +010081 0
Harald Welte1b3e5772010-05-04 11:13:56 +020082};
83
Harald Weltef6214982017-09-24 10:23:24 +080084typedef enum { ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT
85} cmdline_parser_arg_type;
86
Harald Welte1b3e5772010-05-04 11:13:56 +020087static
Harald Weltebed35df2011-11-02 13:06:18 +010088void clear_given(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020089static
Harald Weltebed35df2011-11-02 13:06:18 +010090void clear_args(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020091
92static int
Harald Weltef6214982017-09-24 10:23:24 +080093cmdline_parser_internal(int argc, char **argv,
94 struct gengetopt_args_info *args_info,
95 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +010096 const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020097
Harald Welte73abc382017-10-10 08:50:11 +080098static int
99cmdline_parser_required2(struct gengetopt_args_info *args_info,
100 const char *prog_name, const char *additional_error);
Harald Weltebed35df2011-11-02 13:06:18 +0100101struct line_list {
102 char *string_arg;
103 struct line_list *next;
Harald Welte1b3e5772010-05-04 11:13:56 +0200104};
105
106static struct line_list *cmd_line_list = 0;
107static struct line_list *cmd_line_list_tmp = 0;
108
Harald Weltebed35df2011-11-02 13:06:18 +0100109static void free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200110{
Harald Weltebed35df2011-11-02 13:06:18 +0100111 /* free the list of a previous call */
112 if (cmd_line_list) {
113 while (cmd_line_list) {
114 cmd_line_list_tmp = cmd_line_list;
115 cmd_line_list = cmd_line_list->next;
116 free(cmd_line_list_tmp->string_arg);
117 free(cmd_line_list_tmp);
118 }
119 }
jjako52c24142002-12-16 13:33:51 +0000120}
121
Harald Weltebed35df2011-11-02 13:06:18 +0100122static char *gengetopt_strdup(const char *s);
jjako52c24142002-12-16 13:33:51 +0000123
Harald Welte1b3e5772010-05-04 11:13:56 +0200124static
Harald Weltebed35df2011-11-02 13:06:18 +0100125void clear_given(struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000126{
Harald Weltebed35df2011-11-02 13:06:18 +0100127 args_info->help_given = 0;
128 args_info->version_given = 0;
129 args_info->debug_given = 0;
130 args_info->conf_given = 0;
131 args_info->pidfile_given = 0;
132 args_info->statedir_given = 0;
133 args_info->dns_given = 0;
134 args_info->listen_given = 0;
135 args_info->remote_given = 0;
136 args_info->contexts_given = 0;
137 args_info->timelimit_given = 0;
138 args_info->gtpversion_given = 0;
139 args_info->apn_given = 0;
140 args_info->selmode_given = 0;
141 args_info->rattype_given = 0;
142 args_info->userloc_given = 0;
143 args_info->rai_given = 0;
144 args_info->mstz_given = 0;
145 args_info->imeisv_given = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800146 args_info->norecovery_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100147 args_info->imsi_given = 0;
148 args_info->nsapi_given = 0;
149 args_info->msisdn_given = 0;
150 args_info->qos_given = 0;
151 args_info->qose1_given = 0;
152 args_info->qose2_given = 0;
153 args_info->qose3_given = 0;
154 args_info->qose4_given = 0;
155 args_info->charging_given = 0;
156 args_info->uid_given = 0;
157 args_info->pwd_given = 0;
158 args_info->createif_given = 0;
159 args_info->net_given = 0;
160 args_info->defaultroute_given = 0;
161 args_info->ipup_given = 0;
162 args_info->ipdown_given = 0;
Harald Welte73abc382017-10-10 08:50:11 +0800163 args_info->tun_device_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100164 args_info->pinghost_given = 0;
165 args_info->pingrate_given = 0;
166 args_info->pingsize_given = 0;
167 args_info->pingcount_given = 0;
168 args_info->pingquiet_given = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800169 args_info->no_tx_gpdu_seq_given = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800170 args_info->pdp_type_given = 0;
jjako52c24142002-12-16 13:33:51 +0000171}
172
Harald Welte1b3e5772010-05-04 11:13:56 +0200173static
Harald Weltebed35df2011-11-02 13:06:18 +0100174void clear_args(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200175{
Harald Weltef6214982017-09-24 10:23:24 +0800176 FIX_UNUSED(args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100177 args_info->debug_flag = 0;
178 args_info->conf_arg = NULL;
179 args_info->conf_orig = NULL;
180 args_info->pidfile_arg = gengetopt_strdup("./sgsnemu.pid");
181 args_info->pidfile_orig = NULL;
182 args_info->statedir_arg = gengetopt_strdup("./");
183 args_info->statedir_orig = NULL;
184 args_info->dns_arg = NULL;
185 args_info->dns_orig = NULL;
186 args_info->listen_arg = NULL;
187 args_info->listen_orig = NULL;
188 args_info->remote_arg = NULL;
189 args_info->remote_orig = NULL;
190 args_info->contexts_arg = 1;
191 args_info->contexts_orig = NULL;
192 args_info->timelimit_arg = 0;
193 args_info->timelimit_orig = NULL;
194 args_info->gtpversion_arg = 1;
195 args_info->gtpversion_orig = NULL;
196 args_info->apn_arg = gengetopt_strdup("internet");
197 args_info->apn_orig = NULL;
198 args_info->selmode_arg = 0x01;
199 args_info->selmode_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800200 args_info->rattype_arg = 1;
Harald Weltebed35df2011-11-02 13:06:18 +0100201 args_info->rattype_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800202 args_info->userloc_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100203 args_info->userloc_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800204 args_info->rai_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100205 args_info->rai_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800206 args_info->mstz_arg = gengetopt_strdup("0");
Harald Weltebed35df2011-11-02 13:06:18 +0100207 args_info->mstz_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800208 args_info->imeisv_arg = gengetopt_strdup("2143658709214365");
Harald Weltebed35df2011-11-02 13:06:18 +0100209 args_info->imeisv_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800210 args_info->norecovery_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100211 args_info->imsi_arg = gengetopt_strdup("240010123456789");
212 args_info->imsi_orig = NULL;
213 args_info->nsapi_arg = 0;
214 args_info->nsapi_orig = NULL;
215 args_info->msisdn_arg = gengetopt_strdup("46702123456");
216 args_info->msisdn_orig = NULL;
217 args_info->qos_arg = 0x000b921f;
218 args_info->qos_orig = NULL;
219 args_info->qose1_arg = 0x9396404074f9ffff;
220 args_info->qose1_orig = NULL;
221 args_info->qose2_arg = 0x11;
222 args_info->qose2_orig = NULL;
223 args_info->qose3_arg = 0x0101;
224 args_info->qose3_orig = NULL;
225 args_info->qose4_arg = 0x4040;
226 args_info->qose4_orig = NULL;
227 args_info->charging_arg = 0x0800;
228 args_info->charging_orig = NULL;
229 args_info->uid_arg = gengetopt_strdup("mig");
230 args_info->uid_orig = NULL;
231 args_info->pwd_arg = gengetopt_strdup("hemmelig");
232 args_info->pwd_orig = NULL;
233 args_info->createif_flag = 0;
234 args_info->net_arg = NULL;
235 args_info->net_orig = NULL;
236 args_info->defaultroute_flag = 0;
237 args_info->ipup_arg = NULL;
238 args_info->ipup_orig = NULL;
239 args_info->ipdown_arg = NULL;
240 args_info->ipdown_orig = NULL;
Harald Welte73abc382017-10-10 08:50:11 +0800241 args_info->tun_device_arg = NULL;
242 args_info->tun_device_orig = NULL;
Harald Weltebed35df2011-11-02 13:06:18 +0100243 args_info->pinghost_arg = NULL;
244 args_info->pinghost_orig = NULL;
245 args_info->pingrate_arg = 1;
246 args_info->pingrate_orig = NULL;
247 args_info->pingsize_arg = 56;
248 args_info->pingsize_orig = NULL;
249 args_info->pingcount_arg = 0;
250 args_info->pingcount_orig = NULL;
251 args_info->pingquiet_flag = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800252 args_info->no_tx_gpdu_seq_flag = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800253 args_info->pdp_type_arg = gengetopt_strdup("v4");
254 args_info->pdp_type_orig = NULL;
Harald Weltebed35df2011-11-02 13:06:18 +0100255
Harald Welte1b3e5772010-05-04 11:13:56 +0200256}
257
258static
259void init_args_info(struct gengetopt_args_info *args_info)
260{
Harald Weltef6214982017-09-24 10:23:24 +0800261
Harald Weltebed35df2011-11-02 13:06:18 +0100262 args_info->help_help = gengetopt_args_info_help[0];
263 args_info->version_help = gengetopt_args_info_help[1];
264 args_info->debug_help = gengetopt_args_info_help[2];
265 args_info->conf_help = gengetopt_args_info_help[3];
266 args_info->pidfile_help = gengetopt_args_info_help[4];
267 args_info->statedir_help = gengetopt_args_info_help[5];
268 args_info->dns_help = gengetopt_args_info_help[6];
269 args_info->listen_help = gengetopt_args_info_help[7];
270 args_info->remote_help = gengetopt_args_info_help[8];
271 args_info->contexts_help = gengetopt_args_info_help[9];
272 args_info->timelimit_help = gengetopt_args_info_help[10];
273 args_info->gtpversion_help = gengetopt_args_info_help[11];
274 args_info->apn_help = gengetopt_args_info_help[12];
275 args_info->selmode_help = gengetopt_args_info_help[13];
Harald Weltef6214982017-09-24 10:23:24 +0800276 args_info->rattype_help = gengetopt_args_info_help[14];
277 args_info->userloc_help = gengetopt_args_info_help[15];
278 args_info->rai_help = gengetopt_args_info_help[16];
279 args_info->mstz_help = gengetopt_args_info_help[17];
280 args_info->imeisv_help = gengetopt_args_info_help[18];
281 args_info->norecovery_help = gengetopt_args_info_help[19];
282 args_info->imsi_help = gengetopt_args_info_help[20];
283 args_info->nsapi_help = gengetopt_args_info_help[21];
284 args_info->msisdn_help = gengetopt_args_info_help[22];
285 args_info->qos_help = gengetopt_args_info_help[23];
286 args_info->qose1_help = gengetopt_args_info_help[24];
287 args_info->qose2_help = gengetopt_args_info_help[25];
288 args_info->qose3_help = gengetopt_args_info_help[26];
289 args_info->qose4_help = gengetopt_args_info_help[27];
290 args_info->charging_help = gengetopt_args_info_help[28];
291 args_info->uid_help = gengetopt_args_info_help[29];
292 args_info->pwd_help = gengetopt_args_info_help[30];
293 args_info->createif_help = gengetopt_args_info_help[31];
294 args_info->net_help = gengetopt_args_info_help[32];
295 args_info->defaultroute_help = gengetopt_args_info_help[33];
296 args_info->ipup_help = gengetopt_args_info_help[34];
297 args_info->ipdown_help = gengetopt_args_info_help[35];
Harald Welte73abc382017-10-10 08:50:11 +0800298 args_info->tun_device_help = gengetopt_args_info_help[36];
299 args_info->pinghost_help = gengetopt_args_info_help[37];
300 args_info->pingrate_help = gengetopt_args_info_help[38];
301 args_info->pingsize_help = gengetopt_args_info_help[39];
302 args_info->pingcount_help = gengetopt_args_info_help[40];
303 args_info->pingquiet_help = gengetopt_args_info_help[41];
304 args_info->no_tx_gpdu_seq_help = gengetopt_args_info_help[42];
305 args_info->pdp_type_help = gengetopt_args_info_help[43];
Harald Weltebed35df2011-11-02 13:06:18 +0100306
Harald Welte1b3e5772010-05-04 11:13:56 +0200307}
308
Harald Weltebed35df2011-11-02 13:06:18 +0100309void cmdline_parser_print_version(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200310{
Harald Weltef6214982017-09-24 10:23:24 +0800311 printf("%s %s\n",
312 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ?
313 CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
314 CMDLINE_PARSER_VERSION);
315
316 if (strlen(gengetopt_args_info_versiontext) > 0)
317 printf("\n%s\n", gengetopt_args_info_versiontext);
Harald Welte1b3e5772010-05-04 11:13:56 +0200318}
319
Harald Weltef6214982017-09-24 10:23:24 +0800320static void print_help_common(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200321{
Harald Weltebed35df2011-11-02 13:06:18 +0100322 cmdline_parser_print_version();
Harald Welte1b3e5772010-05-04 11:13:56 +0200323
Harald Weltebed35df2011-11-02 13:06:18 +0100324 if (strlen(gengetopt_args_info_purpose) > 0)
325 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200326
Harald Weltef6214982017-09-24 10:23:24 +0800327 if (strlen(gengetopt_args_info_usage) > 0)
328 printf("\n%s\n", gengetopt_args_info_usage);
329
330 printf("\n");
331
332 if (strlen(gengetopt_args_info_description) > 0)
333 printf("%s\n\n", gengetopt_args_info_description);
334}
335
336void cmdline_parser_print_help(void)
337{
338 int i = 0;
339 print_help_common();
Harald Weltebed35df2011-11-02 13:06:18 +0100340 while (gengetopt_args_info_help[i])
341 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200342}
343
Harald Weltebed35df2011-11-02 13:06:18 +0100344void cmdline_parser_init(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200345{
Harald Weltebed35df2011-11-02 13:06:18 +0100346 clear_given(args_info);
347 clear_args(args_info);
348 init_args_info(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200349}
350
Harald Weltef6214982017-09-24 10:23:24 +0800351void cmdline_parser_params_init(struct cmdline_parser_params *params)
352{
353 if (params) {
354 params->override = 0;
355 params->initialize = 1;
356 params->check_required = 1;
357 params->check_ambiguity = 0;
358 params->print_errors = 1;
359 }
360}
361
362struct cmdline_parser_params *cmdline_parser_params_create(void)
363{
364 struct cmdline_parser_params *params =
365 (struct cmdline_parser_params *)
366 malloc(sizeof(struct cmdline_parser_params));
367 cmdline_parser_params_init(params);
368 return params;
369}
370
371static void free_string_field(char **s)
372{
373 if (*s) {
374 free(*s);
375 *s = 0;
376 }
377}
378
Harald Weltebed35df2011-11-02 13:06:18 +0100379static void cmdline_parser_release(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200380{
Harald Weltebed35df2011-11-02 13:06:18 +0100381
Harald Weltef6214982017-09-24 10:23:24 +0800382 free_string_field(&(args_info->conf_arg));
383 free_string_field(&(args_info->conf_orig));
384 free_string_field(&(args_info->pidfile_arg));
385 free_string_field(&(args_info->pidfile_orig));
386 free_string_field(&(args_info->statedir_arg));
387 free_string_field(&(args_info->statedir_orig));
388 free_string_field(&(args_info->dns_arg));
389 free_string_field(&(args_info->dns_orig));
390 free_string_field(&(args_info->listen_arg));
391 free_string_field(&(args_info->listen_orig));
392 free_string_field(&(args_info->remote_arg));
393 free_string_field(&(args_info->remote_orig));
394 free_string_field(&(args_info->contexts_orig));
395 free_string_field(&(args_info->timelimit_orig));
396 free_string_field(&(args_info->gtpversion_orig));
397 free_string_field(&(args_info->apn_arg));
398 free_string_field(&(args_info->apn_orig));
399 free_string_field(&(args_info->selmode_orig));
400 free_string_field(&(args_info->rattype_orig));
401 free_string_field(&(args_info->userloc_arg));
402 free_string_field(&(args_info->userloc_orig));
403 free_string_field(&(args_info->rai_arg));
404 free_string_field(&(args_info->rai_orig));
405 free_string_field(&(args_info->mstz_arg));
406 free_string_field(&(args_info->mstz_orig));
407 free_string_field(&(args_info->imeisv_arg));
408 free_string_field(&(args_info->imeisv_orig));
409 free_string_field(&(args_info->imsi_arg));
410 free_string_field(&(args_info->imsi_orig));
411 free_string_field(&(args_info->nsapi_orig));
412 free_string_field(&(args_info->msisdn_arg));
413 free_string_field(&(args_info->msisdn_orig));
414 free_string_field(&(args_info->qos_orig));
415 free_string_field(&(args_info->qose1_orig));
416 free_string_field(&(args_info->qose2_orig));
417 free_string_field(&(args_info->qose3_orig));
418 free_string_field(&(args_info->qose4_orig));
419 free_string_field(&(args_info->charging_orig));
420 free_string_field(&(args_info->uid_arg));
421 free_string_field(&(args_info->uid_orig));
422 free_string_field(&(args_info->pwd_arg));
423 free_string_field(&(args_info->pwd_orig));
424 free_string_field(&(args_info->net_arg));
425 free_string_field(&(args_info->net_orig));
426 free_string_field(&(args_info->ipup_arg));
427 free_string_field(&(args_info->ipup_orig));
428 free_string_field(&(args_info->ipdown_arg));
429 free_string_field(&(args_info->ipdown_orig));
Harald Welte73abc382017-10-10 08:50:11 +0800430 free_string_field(&(args_info->tun_device_arg));
431 free_string_field(&(args_info->tun_device_orig));
Harald Weltef6214982017-09-24 10:23:24 +0800432 free_string_field(&(args_info->pinghost_arg));
433 free_string_field(&(args_info->pinghost_orig));
434 free_string_field(&(args_info->pingrate_orig));
435 free_string_field(&(args_info->pingsize_orig));
436 free_string_field(&(args_info->pingcount_orig));
Harald Welte840a8e92017-09-24 18:12:40 +0800437 free_string_field(&(args_info->pdp_type_arg));
438 free_string_field(&(args_info->pdp_type_orig));
Harald Weltebed35df2011-11-02 13:06:18 +0100439
440 clear_given(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200441}
442
Harald Weltef6214982017-09-24 10:23:24 +0800443static void
444write_into_file(FILE * outfile, const char *opt, const char *arg,
445 const char *values[])
446{
447 FIX_UNUSED(values);
448 if (arg) {
449 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
450 } else {
451 fprintf(outfile, "%s\n", opt);
452 }
453}
454
455int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info)
456{
457 int i = 0;
458
459 if (!outfile) {
460 fprintf(stderr, "%s: cannot dump options to stream\n",
461 CMDLINE_PARSER_PACKAGE);
462 return EXIT_FAILURE;
463 }
464
465 if (args_info->help_given)
466 write_into_file(outfile, "help", 0, 0);
467 if (args_info->version_given)
468 write_into_file(outfile, "version", 0, 0);
469 if (args_info->debug_given)
470 write_into_file(outfile, "debug", 0, 0);
471 if (args_info->conf_given)
472 write_into_file(outfile, "conf", args_info->conf_orig, 0);
473 if (args_info->pidfile_given)
474 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
475 if (args_info->statedir_given)
476 write_into_file(outfile, "statedir", args_info->statedir_orig,
477 0);
478 if (args_info->dns_given)
479 write_into_file(outfile, "dns", args_info->dns_orig, 0);
480 if (args_info->listen_given)
481 write_into_file(outfile, "listen", args_info->listen_orig, 0);
482 if (args_info->remote_given)
483 write_into_file(outfile, "remote", args_info->remote_orig, 0);
484 if (args_info->contexts_given)
485 write_into_file(outfile, "contexts", args_info->contexts_orig,
486 0);
487 if (args_info->timelimit_given)
488 write_into_file(outfile, "timelimit", args_info->timelimit_orig,
489 0);
490 if (args_info->gtpversion_given)
491 write_into_file(outfile, "gtpversion",
492 args_info->gtpversion_orig, 0);
493 if (args_info->apn_given)
494 write_into_file(outfile, "apn", args_info->apn_orig, 0);
495 if (args_info->selmode_given)
496 write_into_file(outfile, "selmode", args_info->selmode_orig, 0);
497 if (args_info->rattype_given)
498 write_into_file(outfile, "rattype", args_info->rattype_orig, 0);
499 if (args_info->userloc_given)
500 write_into_file(outfile, "userloc", args_info->userloc_orig, 0);
501 if (args_info->rai_given)
502 write_into_file(outfile, "rai", args_info->rai_orig, 0);
503 if (args_info->mstz_given)
504 write_into_file(outfile, "mstz", args_info->mstz_orig, 0);
505 if (args_info->imeisv_given)
506 write_into_file(outfile, "imeisv", args_info->imeisv_orig, 0);
507 if (args_info->norecovery_given)
508 write_into_file(outfile, "norecovery", 0, 0);
509 if (args_info->imsi_given)
510 write_into_file(outfile, "imsi", args_info->imsi_orig, 0);
511 if (args_info->nsapi_given)
512 write_into_file(outfile, "nsapi", args_info->nsapi_orig, 0);
513 if (args_info->msisdn_given)
514 write_into_file(outfile, "msisdn", args_info->msisdn_orig, 0);
515 if (args_info->qos_given)
516 write_into_file(outfile, "qos", args_info->qos_orig, 0);
517 if (args_info->qose1_given)
518 write_into_file(outfile, "qose1", args_info->qose1_orig, 0);
519 if (args_info->qose2_given)
520 write_into_file(outfile, "qose2", args_info->qose2_orig, 0);
521 if (args_info->qose3_given)
522 write_into_file(outfile, "qose3", args_info->qose3_orig, 0);
523 if (args_info->qose4_given)
524 write_into_file(outfile, "qose4", args_info->qose4_orig, 0);
525 if (args_info->charging_given)
526 write_into_file(outfile, "charging", args_info->charging_orig,
527 0);
528 if (args_info->uid_given)
529 write_into_file(outfile, "uid", args_info->uid_orig, 0);
530 if (args_info->pwd_given)
531 write_into_file(outfile, "pwd", args_info->pwd_orig, 0);
532 if (args_info->createif_given)
533 write_into_file(outfile, "createif", 0, 0);
534 if (args_info->net_given)
535 write_into_file(outfile, "net", args_info->net_orig, 0);
536 if (args_info->defaultroute_given)
537 write_into_file(outfile, "defaultroute", 0, 0);
538 if (args_info->ipup_given)
539 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
540 if (args_info->ipdown_given)
541 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
Harald Welte73abc382017-10-10 08:50:11 +0800542 if (args_info->tun_device_given)
543 write_into_file(outfile, "tun-device",
544 args_info->tun_device_orig, 0);
Harald Weltef6214982017-09-24 10:23:24 +0800545 if (args_info->pinghost_given)
546 write_into_file(outfile, "pinghost", args_info->pinghost_orig,
547 0);
548 if (args_info->pingrate_given)
549 write_into_file(outfile, "pingrate", args_info->pingrate_orig,
550 0);
551 if (args_info->pingsize_given)
552 write_into_file(outfile, "pingsize", args_info->pingsize_orig,
553 0);
554 if (args_info->pingcount_given)
555 write_into_file(outfile, "pingcount", args_info->pingcount_orig,
556 0);
557 if (args_info->pingquiet_given)
558 write_into_file(outfile, "pingquiet", 0, 0);
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800559 if (args_info->no_tx_gpdu_seq_given)
560 write_into_file(outfile, "no-tx-gpdu-seq", 0, 0);
Harald Welte840a8e92017-09-24 18:12:40 +0800561 if (args_info->pdp_type_given)
562 write_into_file(outfile, "pdp-type", args_info->pdp_type_orig,
563 0);
Harald Weltef6214982017-09-24 10:23:24 +0800564
565 i = EXIT_SUCCESS;
566 return i;
567}
568
Harald Welte1b3e5772010-05-04 11:13:56 +0200569int
Harald Weltebed35df2011-11-02 13:06:18 +0100570cmdline_parser_file_save(const char *filename,
571 struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200572{
Harald Weltebed35df2011-11-02 13:06:18 +0100573 FILE *outfile;
574 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200575
Harald Weltebed35df2011-11-02 13:06:18 +0100576 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200577
Harald Weltebed35df2011-11-02 13:06:18 +0100578 if (!outfile) {
579 fprintf(stderr, "%s: cannot open file for writing: %s\n",
580 CMDLINE_PARSER_PACKAGE, filename);
581 return EXIT_FAILURE;
582 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200583
Harald Weltef6214982017-09-24 10:23:24 +0800584 i = cmdline_parser_dump(outfile, args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100585 fclose(outfile);
586
Harald Weltebed35df2011-11-02 13:06:18 +0100587 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200588}
589
Harald Weltebed35df2011-11-02 13:06:18 +0100590void cmdline_parser_free(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200591{
Harald Weltebed35df2011-11-02 13:06:18 +0100592 cmdline_parser_release(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200593}
594
Harald Weltef6214982017-09-24 10:23:24 +0800595/** @brief replacement of strdup, which is not standard */
Harald Weltebed35df2011-11-02 13:06:18 +0100596char *gengetopt_strdup(const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200597{
Harald Weltef6214982017-09-24 10:23:24 +0800598 char *result = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100599 if (!s)
600 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200601
Harald Weltebed35df2011-11-02 13:06:18 +0100602 result = (char *)malloc(strlen(s) + 1);
603 if (result == (char *)0)
604 return (char *)0;
605 strcpy(result, s);
606 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200607}
608
Harald Weltef6214982017-09-24 10:23:24 +0800609int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200610{
Harald Weltebed35df2011-11-02 13:06:18 +0100611 return cmdline_parser2(argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200612}
613
614int
Harald Weltef6214982017-09-24 10:23:24 +0800615cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info,
616 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200617{
Harald Weltebed35df2011-11-02 13:06:18 +0100618 int result;
Harald Weltef6214982017-09-24 10:23:24 +0800619 result = cmdline_parser_internal(argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200620
Harald Weltef6214982017-09-24 10:23:24 +0800621 if (result == EXIT_FAILURE) {
622 cmdline_parser_free(args_info);
623 exit(EXIT_FAILURE);
624 }
625
626 return result;
627}
628
629int
630cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info,
631 int override, int initialize, int check_required)
632{
633 int result;
634 struct cmdline_parser_params params;
635
636 params.override = override;
637 params.initialize = initialize;
638 params.check_required = check_required;
639 params.check_ambiguity = 0;
640 params.print_errors = 1;
641
642 result = cmdline_parser_internal(argc, argv, args_info, &params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200643
Harald Weltebed35df2011-11-02 13:06:18 +0100644 if (result == EXIT_FAILURE) {
645 cmdline_parser_free(args_info);
646 exit(EXIT_FAILURE);
647 }
648
649 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200650}
651
652int
Harald Weltebed35df2011-11-02 13:06:18 +0100653cmdline_parser_required(struct gengetopt_args_info *args_info,
654 const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200655{
Harald Welte73abc382017-10-10 08:50:11 +0800656 int result = EXIT_SUCCESS;
657
658 if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
659 result = EXIT_FAILURE;
660
661 if (result == EXIT_FAILURE) {
662 cmdline_parser_free(args_info);
663 exit(EXIT_FAILURE);
664 }
665
666 return result;
667}
668
669int
670cmdline_parser_required2(struct gengetopt_args_info *args_info,
671 const char *prog_name, const char *additional_error)
672{
673 int error_occurred = 0;
674 FIX_UNUSED(additional_error);
675
676 /* checks for required options */
677
678 /* checks for dependences among options */
Harald Welteea0c26a2017-10-10 08:54:04 +0800679 if (args_info->net_given && !args_info->createif_given) {
680 fprintf(stderr,
681 "%s: '--net' ('-n') option depends on option 'createif'%s\n",
682 prog_name, (additional_error ? additional_error : ""));
683 error_occurred = 1;
684 }
685 if (args_info->defaultroute_given && !args_info->createif_given) {
686 fprintf(stderr,
687 "%s: '--defaultroute' option depends on option 'createif'%s\n",
688 prog_name, (additional_error ? additional_error : ""));
689 error_occurred = 1;
690 }
691 if (args_info->ipup_given && !args_info->createif_given) {
692 fprintf(stderr,
693 "%s: '--ipup' option depends on option 'createif'%s\n",
694 prog_name, (additional_error ? additional_error : ""));
695 error_occurred = 1;
696 }
697 if (args_info->ipdown_given && !args_info->createif_given) {
698 fprintf(stderr,
699 "%s: '--ipdown' option depends on option 'createif'%s\n",
700 prog_name, (additional_error ? additional_error : ""));
701 error_occurred = 1;
702 }
Harald Welte73abc382017-10-10 08:50:11 +0800703 if (args_info->tun_device_given && !args_info->createif_given) {
704 fprintf(stderr,
705 "%s: '--tun-device' option depends on option 'createif'%s\n",
706 prog_name, (additional_error ? additional_error : ""));
707 error_occurred = 1;
708 }
Harald Welteea0c26a2017-10-10 08:54:04 +0800709 if (args_info->pingrate_given && !args_info->pinghost_given) {
710 fprintf(stderr,
711 "%s: '--pingrate' option depends on option 'pinghost'%s\n",
712 prog_name, (additional_error ? additional_error : ""));
713 error_occurred = 1;
714 }
715 if (args_info->pingsize_given && !args_info->pinghost_given) {
716 fprintf(stderr,
717 "%s: '--pingsize' option depends on option 'pinghost'%s\n",
718 prog_name, (additional_error ? additional_error : ""));
719 error_occurred = 1;
720 }
721 if (args_info->pingcount_given && !args_info->pinghost_given) {
722 fprintf(stderr,
723 "%s: '--pingcount' option depends on option 'pinghost'%s\n",
724 prog_name, (additional_error ? additional_error : ""));
725 error_occurred = 1;
726 }
727 if (args_info->pingquiet_given && !args_info->pinghost_given) {
728 fprintf(stderr,
729 "%s: '--pingquiet' option depends on option 'pinghost'%s\n",
730 prog_name, (additional_error ? additional_error : ""));
731 error_occurred = 1;
732 }
Harald Welte73abc382017-10-10 08:50:11 +0800733
734 return error_occurred;
Harald Welte1b3e5772010-05-04 11:13:56 +0200735}
736
Harald Weltef6214982017-09-24 10:23:24 +0800737static char *package_name = 0;
738
739/**
740 * @brief updates an option
741 * @param field the generic pointer to the field to update
742 * @param orig_field the pointer to the orig field
743 * @param field_given the pointer to the number of occurrence of this option
744 * @param prev_given the pointer to the number of occurrence already seen
745 * @param value the argument for this option (if null no arg was specified)
746 * @param possible_values the possible values for this option (if specified)
747 * @param default_value the default value (in case the option only accepts fixed values)
748 * @param arg_type the type of this option
749 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
750 * @param override @see cmdline_parser_params.override
751 * @param no_free whether to free a possible previous value
752 * @param multiple_option whether this is a multiple option
753 * @param long_opt the corresponding long option
754 * @param short_opt the corresponding short option (or '-' if none)
755 * @param additional_error possible further error specification
756 */
757static
758int update_arg(void *field, char **orig_field,
759 unsigned int *field_given, unsigned int *prev_given,
760 char *value, const char *possible_values[],
761 const char *default_value,
762 cmdline_parser_arg_type arg_type,
763 int check_ambiguity, int override,
764 int no_free, int multiple_option,
765 const char *long_opt, char short_opt,
766 const char *additional_error)
767{
768 char *stop_char = 0;
769 const char *val = value;
770 int found;
771 char **string_field;
772 FIX_UNUSED(field);
773
774 stop_char = 0;
775 found = 0;
776
777 if (!multiple_option && prev_given
778 && (*prev_given || (check_ambiguity && *field_given))) {
779 if (short_opt != '-')
780 fprintf(stderr,
781 "%s: `--%s' (`-%c') option given more than once%s\n",
782 package_name, long_opt, short_opt,
783 (additional_error ? additional_error : ""));
784 else
785 fprintf(stderr,
786 "%s: `--%s' option given more than once%s\n",
787 package_name, long_opt,
788 (additional_error ? additional_error : ""));
789 return 1; /* failure */
790 }
791
792 FIX_UNUSED(default_value);
793
794 if (field_given && *field_given && !override)
795 return 0;
796 if (prev_given)
797 (*prev_given)++;
798 if (field_given)
799 (*field_given)++;
800 if (possible_values)
801 val = possible_values[found];
802
803 switch (arg_type) {
804 case ARG_FLAG:
805 *((int *)field) = !*((int *)field);
806 break;
807 case ARG_INT:
808 if (val)
809 *((int *)field) = strtol(val, &stop_char, 0);
810 break;
811 case ARG_STRING:
812 if (val) {
813 string_field = (char **)field;
814 if (!no_free && *string_field)
815 free(*string_field); /* free previous string */
816 *string_field = gengetopt_strdup(val);
817 }
818 break;
819 default:
820 break;
821 };
822
823 /* check numeric conversion */
824 switch (arg_type) {
825 case ARG_INT:
826 if (val && !(stop_char && *stop_char == '\0')) {
827 fprintf(stderr, "%s: invalid numeric value: %s\n",
828 package_name, val);
829 return 1; /* failure */
830 }
831 break;
832 default:
833 ;
834 };
835
836 /* store the original value */
837 switch (arg_type) {
838 case ARG_NO:
839 case ARG_FLAG:
840 break;
841 default:
842 if (value && orig_field) {
843 if (no_free) {
844 *orig_field = value;
845 } else {
846 if (*orig_field)
847 free(*orig_field); /* free previous string */
848 *orig_field = gengetopt_strdup(value);
849 }
850 }
851 };
852
853 return 0; /* OK */
854}
855
Harald Welte1b3e5772010-05-04 11:13:56 +0200856int
Harald Weltef6214982017-09-24 10:23:24 +0800857cmdline_parser_internal(int argc, char **argv,
858 struct gengetopt_args_info *args_info,
859 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +0100860 const char *additional_error)
Harald Welte1b3e5772010-05-04 11:13:56 +0200861{
Harald Weltebed35df2011-11-02 13:06:18 +0100862 int c; /* Character of the parsed option. */
Harald Welte1b3e5772010-05-04 11:13:56 +0200863
Harald Weltef6214982017-09-24 10:23:24 +0800864 int error_occurred = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100865 struct gengetopt_args_info local_args_info;
Harald Welte1b3e5772010-05-04 11:13:56 +0200866
Harald Weltef6214982017-09-24 10:23:24 +0800867 int override;
868 int initialize;
869 int check_required;
870 int check_ambiguity;
871
872 package_name = argv[0];
873
874 override = params->override;
875 initialize = params->initialize;
876 check_required = params->check_required;
877 check_ambiguity = params->check_ambiguity;
878
Harald Weltebed35df2011-11-02 13:06:18 +0100879 if (initialize)
880 cmdline_parser_init(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200881
Harald Weltebed35df2011-11-02 13:06:18 +0100882 cmdline_parser_init(&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000883
Harald Weltebed35df2011-11-02 13:06:18 +0100884 optarg = 0;
885 optind = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800886 opterr = params->print_errors;
Harald Weltebed35df2011-11-02 13:06:18 +0100887 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000888
Harald Weltebed35df2011-11-02 13:06:18 +0100889 while (1) {
890 int option_index = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200891
Harald Weltebed35df2011-11-02 13:06:18 +0100892 static struct option long_options[] = {
893 {"help", 0, NULL, 'h'},
894 {"version", 0, NULL, 'V'},
895 {"debug", 0, NULL, 'd'},
896 {"conf", 1, NULL, 'c'},
897 {"pidfile", 1, NULL, 0},
898 {"statedir", 1, NULL, 0},
899 {"dns", 1, NULL, 0},
900 {"listen", 1, NULL, 'l'},
901 {"remote", 1, NULL, 'r'},
902 {"contexts", 1, NULL, 0},
903 {"timelimit", 1, NULL, 0},
904 {"gtpversion", 1, NULL, 0},
905 {"apn", 1, NULL, 'a'},
906 {"selmode", 1, NULL, 0},
907 {"rattype", 1, NULL, 0},
908 {"userloc", 1, NULL, 0},
909 {"rai", 1, NULL, 0},
910 {"mstz", 1, NULL, 0},
911 {"imeisv", 1, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800912 {"norecovery", 0, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100913 {"imsi", 1, NULL, 'i'},
914 {"nsapi", 1, NULL, 0},
915 {"msisdn", 1, NULL, 'm'},
916 {"qos", 1, NULL, 'q'},
917 {"qose1", 1, NULL, 0},
918 {"qose2", 1, NULL, 0},
919 {"qose3", 1, NULL, 0},
920 {"qose4", 1, NULL, 0},
921 {"charging", 1, NULL, 0},
922 {"uid", 1, NULL, 'u'},
923 {"pwd", 1, NULL, 'p'},
924 {"createif", 0, NULL, 0},
925 {"net", 1, NULL, 'n'},
926 {"defaultroute", 0, NULL, 0},
927 {"ipup", 1, NULL, 0},
928 {"ipdown", 1, NULL, 0},
Harald Welte73abc382017-10-10 08:50:11 +0800929 {"tun-device", 1, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100930 {"pinghost", 1, NULL, 0},
931 {"pingrate", 1, NULL, 0},
932 {"pingsize", 1, NULL, 0},
933 {"pingcount", 1, NULL, 0},
934 {"pingquiet", 0, NULL, 0},
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800935 {"no-tx-gpdu-seq", 0, NULL, 0},
Harald Welte840a8e92017-09-24 18:12:40 +0800936 {"pdp-type", 1, NULL, 't'},
Harald Weltef6214982017-09-24 10:23:24 +0800937 {0, 0, 0, 0}
Harald Weltebed35df2011-11-02 13:06:18 +0100938 };
jjako52c24142002-12-16 13:33:51 +0000939
Harald Welte840a8e92017-09-24 18:12:40 +0800940 c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:t:",
Harald Weltebed35df2011-11-02 13:06:18 +0100941 long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000942
Harald Weltebed35df2011-11-02 13:06:18 +0100943 if (c == -1)
944 break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000945
Harald Weltebed35df2011-11-02 13:06:18 +0100946 switch (c) {
947 case 'h': /* Print help and exit. */
948 cmdline_parser_print_help();
949 cmdline_parser_free(&local_args_info);
950 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000951
Harald Weltebed35df2011-11-02 13:06:18 +0100952 case 'V': /* Print version and exit. */
953 cmdline_parser_print_version();
954 cmdline_parser_free(&local_args_info);
955 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000956
Harald Weltebed35df2011-11-02 13:06:18 +0100957 case 'd': /* Run in debug mode. */
jjako52c24142002-12-16 13:33:51 +0000958
Harald Weltef6214982017-09-24 10:23:24 +0800959 if (update_arg
960 ((void *)&(args_info->debug_flag), 0,
961 &(args_info->debug_given),
962 &(local_args_info.debug_given), optarg, 0, 0,
963 ARG_FLAG, check_ambiguity, override, 1, 0, "debug",
964 'd', additional_error))
965 goto failure;
966
967 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100968 case 'c': /* Read configuration file. */
jjako52c24142002-12-16 13:33:51 +0000969
Harald Weltef6214982017-09-24 10:23:24 +0800970 if (update_arg((void *)&(args_info->conf_arg),
971 &(args_info->conf_orig),
972 &(args_info->conf_given),
973 &(local_args_info.conf_given), optarg, 0,
974 0, ARG_STRING, check_ambiguity, override,
975 0, 0, "conf", 'c', additional_error))
976 goto failure;
977
978 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100979 case 'l': /* Local interface. */
jjako52c24142002-12-16 13:33:51 +0000980
Harald Weltef6214982017-09-24 10:23:24 +0800981 if (update_arg((void *)&(args_info->listen_arg),
982 &(args_info->listen_orig),
983 &(args_info->listen_given),
984 &(local_args_info.listen_given), optarg,
985 0, 0, ARG_STRING, check_ambiguity,
986 override, 0, 0, "listen", 'l',
987 additional_error))
988 goto failure;
989
990 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100991 case 'r': /* Remote host. */
jjako52c24142002-12-16 13:33:51 +0000992
Harald Weltef6214982017-09-24 10:23:24 +0800993 if (update_arg((void *)&(args_info->remote_arg),
994 &(args_info->remote_orig),
995 &(args_info->remote_given),
996 &(local_args_info.remote_given), optarg,
997 0, 0, ARG_STRING, check_ambiguity,
998 override, 0, 0, "remote", 'r',
999 additional_error))
1000 goto failure;
1001
1002 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001003 case 'a': /* Access point name. */
jjako52c24142002-12-16 13:33:51 +00001004
Harald Weltef6214982017-09-24 10:23:24 +08001005 if (update_arg((void *)&(args_info->apn_arg),
1006 &(args_info->apn_orig),
1007 &(args_info->apn_given),
1008 &(local_args_info.apn_given), optarg, 0,
1009 "internet", ARG_STRING, check_ambiguity,
1010 override, 0, 0, "apn", 'a',
1011 additional_error))
1012 goto failure;
1013
1014 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001015 case 'i': /* IMSI. */
jjako52c24142002-12-16 13:33:51 +00001016
Harald Weltef6214982017-09-24 10:23:24 +08001017 if (update_arg((void *)&(args_info->imsi_arg),
1018 &(args_info->imsi_orig),
1019 &(args_info->imsi_given),
1020 &(local_args_info.imsi_given), optarg, 0,
1021 "240010123456789", ARG_STRING,
1022 check_ambiguity, override, 0, 0, "imsi",
1023 'i', additional_error))
1024 goto failure;
1025
1026 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001027 case 'm': /* Mobile Station ISDN number. */
jjako52c24142002-12-16 13:33:51 +00001028
Harald Weltef6214982017-09-24 10:23:24 +08001029 if (update_arg((void *)&(args_info->msisdn_arg),
1030 &(args_info->msisdn_orig),
1031 &(args_info->msisdn_given),
1032 &(local_args_info.msisdn_given), optarg,
1033 0, "46702123456", ARG_STRING,
1034 check_ambiguity, override, 0, 0,
1035 "msisdn", 'm', additional_error))
1036 goto failure;
1037
1038 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001039 case 'q': /* Requested quality of service. */
jjako52c24142002-12-16 13:33:51 +00001040
Harald Weltef6214982017-09-24 10:23:24 +08001041 if (update_arg((void *)&(args_info->qos_arg),
1042 &(args_info->qos_orig),
1043 &(args_info->qos_given),
1044 &(local_args_info.qos_given), optarg, 0,
1045 "0x000b921f", ARG_INT, check_ambiguity,
1046 override, 0, 0, "qos", 'q',
1047 additional_error))
1048 goto failure;
1049
1050 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001051 case 'u': /* Login user ID. */
jjako52c24142002-12-16 13:33:51 +00001052
Harald Weltef6214982017-09-24 10:23:24 +08001053 if (update_arg((void *)&(args_info->uid_arg),
1054 &(args_info->uid_orig),
1055 &(args_info->uid_given),
1056 &(local_args_info.uid_given), optarg, 0,
1057 "mig", ARG_STRING, check_ambiguity,
1058 override, 0, 0, "uid", 'u',
1059 additional_error))
1060 goto failure;
1061
1062 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001063 case 'p': /* Login password. */
jjako52c24142002-12-16 13:33:51 +00001064
Harald Weltef6214982017-09-24 10:23:24 +08001065 if (update_arg((void *)&(args_info->pwd_arg),
1066 &(args_info->pwd_orig),
1067 &(args_info->pwd_given),
1068 &(local_args_info.pwd_given), optarg, 0,
1069 "hemmelig", ARG_STRING, check_ambiguity,
1070 override, 0, 0, "pwd", 'p',
1071 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001072 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001073
1074 break;
1075 case 'n': /* Network address for local interface. */
1076
1077 if (update_arg((void *)&(args_info->net_arg),
1078 &(args_info->net_orig),
1079 &(args_info->net_given),
1080 &(local_args_info.net_given), optarg, 0,
1081 0, ARG_STRING, check_ambiguity, override,
1082 0, 0, "net", 'n', additional_error))
1083 goto failure;
1084
Harald Weltebed35df2011-11-02 13:06:18 +01001085 break;
Harald Welte840a8e92017-09-24 18:12:40 +08001086 case 't': /* PDP Type. */
1087
1088 if (update_arg((void *)&(args_info->pdp_type_arg),
1089 &(args_info->pdp_type_orig),
1090 &(args_info->pdp_type_given),
1091 &(local_args_info.pdp_type_given),
1092 optarg, 0, "v4", ARG_STRING,
1093 check_ambiguity, override, 0, 0,
1094 "pdp-type", 't', additional_error))
1095 goto failure;
1096
1097 break;
jjako193e8b12003-11-10 12:31:41 +00001098
Harald Weltebed35df2011-11-02 13:06:18 +01001099 case 0: /* Long option with no short option */
1100 /* Filename of process id file. */
1101 if (strcmp(long_options[option_index].name, "pidfile")
1102 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001103
1104 if (update_arg
1105 ((void *)&(args_info->pidfile_arg),
1106 &(args_info->pidfile_orig),
1107 &(args_info->pidfile_given),
1108 &(local_args_info.pidfile_given), optarg,
1109 0, "./sgsnemu.pid", ARG_STRING,
1110 check_ambiguity, override, 0, 0, "pidfile",
1111 '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001112 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001113
Harald Weltebed35df2011-11-02 13:06:18 +01001114 }
1115 /* Directory of nonvolatile data. */
1116 else if (strcmp
1117 (long_options[option_index].name,
1118 "statedir") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001119
1120 if (update_arg
1121 ((void *)&(args_info->statedir_arg),
1122 &(args_info->statedir_orig),
1123 &(args_info->statedir_given),
1124 &(local_args_info.statedir_given), optarg,
1125 0, "./", ARG_STRING, check_ambiguity,
1126 override, 0, 0, "statedir", '-',
1127 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001128 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001129
Harald Weltebed35df2011-11-02 13:06:18 +01001130 }
1131 /* DNS Server to use. */
1132 else if (strcmp(long_options[option_index].name, "dns")
1133 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001134
1135 if (update_arg((void *)&(args_info->dns_arg),
1136 &(args_info->dns_orig),
1137 &(args_info->dns_given),
1138 &(local_args_info.dns_given),
1139 optarg, 0, 0, ARG_STRING,
1140 check_ambiguity, override, 0, 0,
1141 "dns", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001142 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001143
Harald Weltebed35df2011-11-02 13:06:18 +01001144 }
1145 /* Number of contexts. */
1146 else if (strcmp
1147 (long_options[option_index].name,
1148 "contexts") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001149
1150 if (update_arg
1151 ((void *)&(args_info->contexts_arg),
1152 &(args_info->contexts_orig),
1153 &(args_info->contexts_given),
1154 &(local_args_info.contexts_given), optarg,
1155 0, "1", ARG_INT, check_ambiguity, override,
1156 0, 0, "contexts", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001157 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001158
Harald Weltebed35df2011-11-02 13:06:18 +01001159 }
1160 /* Exit after timelimit seconds. */
1161 else if (strcmp
1162 (long_options[option_index].name,
1163 "timelimit") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001164
1165 if (update_arg
1166 ((void *)&(args_info->timelimit_arg),
1167 &(args_info->timelimit_orig),
1168 &(args_info->timelimit_given),
1169 &(local_args_info.timelimit_given), optarg,
1170 0, "0", ARG_INT, check_ambiguity, override,
1171 0, 0, "timelimit", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001172 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001173
Harald Weltebed35df2011-11-02 13:06:18 +01001174 }
1175 /* GTP version to use. */
1176 else if (strcmp
1177 (long_options[option_index].name,
1178 "gtpversion") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001179
1180 if (update_arg
1181 ((void *)&(args_info->gtpversion_arg),
1182 &(args_info->gtpversion_orig),
1183 &(args_info->gtpversion_given),
1184 &(local_args_info.gtpversion_given),
1185 optarg, 0, "1", ARG_INT, check_ambiguity,
1186 override, 0, 0, "gtpversion", '-',
1187 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001188 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001189
Harald Weltebed35df2011-11-02 13:06:18 +01001190 }
1191 /* Selection mode. */
1192 else if (strcmp
1193 (long_options[option_index].name,
1194 "selmode") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001195
1196 if (update_arg
1197 ((void *)&(args_info->selmode_arg),
1198 &(args_info->selmode_orig),
1199 &(args_info->selmode_given),
1200 &(local_args_info.selmode_given), optarg,
1201 0, "0x01", ARG_INT, check_ambiguity,
1202 override, 0, 0, "selmode", '-',
1203 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001204 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001205
Harald Weltebed35df2011-11-02 13:06:18 +01001206 }
1207 /* Radio Access Technology Type. */
1208 else if (strcmp
1209 (long_options[option_index].name,
1210 "rattype") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001211
1212 if (update_arg
1213 ((void *)&(args_info->rattype_arg),
1214 &(args_info->rattype_orig),
1215 &(args_info->rattype_given),
1216 &(local_args_info.rattype_given), optarg,
1217 0, "1", ARG_INT, check_ambiguity, override,
1218 0, 0, "rattype", '-', additional_error))
1219 goto failure;
1220
Harald Weltebed35df2011-11-02 13:06:18 +01001221 }
1222 /* User Location Information. */
1223 else if (strcmp
1224 (long_options[option_index].name,
1225 "userloc") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001226
1227 if (update_arg
1228 ((void *)&(args_info->userloc_arg),
1229 &(args_info->userloc_orig),
1230 &(args_info->userloc_given),
1231 &(local_args_info.userloc_given), optarg,
1232 0, "02509946241207", ARG_STRING,
1233 check_ambiguity, override, 0, 0, "userloc",
1234 '-', additional_error))
1235 goto failure;
1236
Harald Weltebed35df2011-11-02 13:06:18 +01001237 }
1238 /* Routing Area Information. */
1239 else if (strcmp(long_options[option_index].name, "rai")
1240 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001241
1242 if (update_arg((void *)&(args_info->rai_arg),
1243 &(args_info->rai_orig),
1244 &(args_info->rai_given),
1245 &(local_args_info.rai_given),
1246 optarg, 0, "02509946241207",
1247 ARG_STRING, check_ambiguity,
1248 override, 0, 0, "rai", '-',
1249 additional_error))
1250 goto failure;
1251
Harald Weltebed35df2011-11-02 13:06:18 +01001252 }
Harald Weltef6214982017-09-24 10:23:24 +08001253 /* MS Time Zone. */
Harald Weltebed35df2011-11-02 13:06:18 +01001254 else if (strcmp(long_options[option_index].name, "mstz")
1255 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001256
1257 if (update_arg((void *)&(args_info->mstz_arg),
1258 &(args_info->mstz_orig),
1259 &(args_info->mstz_given),
1260 &(local_args_info.mstz_given),
1261 optarg, 0, "0", ARG_STRING,
1262 check_ambiguity, override, 0, 0,
1263 "mstz", '-', additional_error))
1264 goto failure;
1265
Harald Weltebed35df2011-11-02 13:06:18 +01001266 }
Harald Weltef6214982017-09-24 10:23:24 +08001267 /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */
Harald Weltebed35df2011-11-02 13:06:18 +01001268 else if (strcmp
1269 (long_options[option_index].name,
1270 "imeisv") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001271
1272 if (update_arg((void *)&(args_info->imeisv_arg),
1273 &(args_info->imeisv_orig),
1274 &(args_info->imeisv_given),
1275 &(local_args_info.imeisv_given),
1276 optarg, 0, "2143658709214365",
1277 ARG_STRING, check_ambiguity,
1278 override, 0, 0, "imeisv", '-',
1279 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001280 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001281
Harald Weltebed35df2011-11-02 13:06:18 +01001282 }
1283 /* Do not send recovery. */
1284 else if (strcmp
1285 (long_options[option_index].name,
1286 "norecovery") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001287
1288 if (update_arg
1289 ((void *)&(args_info->norecovery_flag), 0,
1290 &(args_info->norecovery_given),
1291 &(local_args_info.norecovery_given),
1292 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1293 override, 1, 0, "norecovery", '-',
1294 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001295 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001296
1297 }
1298 /* NSAPI. */
1299 else if (strcmp
1300 (long_options[option_index].name,
1301 "nsapi") == 0) {
1302
1303 if (update_arg((void *)&(args_info->nsapi_arg),
1304 &(args_info->nsapi_orig),
1305 &(args_info->nsapi_given),
1306 &(local_args_info.nsapi_given),
1307 optarg, 0, "0", ARG_INT,
1308 check_ambiguity, override, 0, 0,
1309 "nsapi", '-', additional_error))
1310 goto failure;
1311
1312 }
1313 /* Requested quality of service Extension 1. */
1314 else if (strcmp
1315 (long_options[option_index].name,
1316 "qose1") == 0) {
1317
1318 if (update_arg((void *)&(args_info->qose1_arg),
1319 &(args_info->qose1_orig),
1320 &(args_info->qose1_given),
1321 &(local_args_info.qose1_given),
1322 optarg, 0, "0x9396404074f9ffff",
1323 ARG_INT, check_ambiguity,
1324 override, 0, 0, "qose1", '-',
1325 additional_error))
1326 goto failure;
1327
1328 }
1329 /* Requested quality of service Extension 2. */
1330 else if (strcmp
1331 (long_options[option_index].name,
1332 "qose2") == 0) {
1333
1334 if (update_arg((void *)&(args_info->qose2_arg),
1335 &(args_info->qose2_orig),
1336 &(args_info->qose2_given),
1337 &(local_args_info.qose2_given),
1338 optarg, 0, "0x11", ARG_INT,
1339 check_ambiguity, override, 0, 0,
1340 "qose2", '-', additional_error))
1341 goto failure;
1342
1343 }
1344 /* Requested quality of service Extension 3. */
1345 else if (strcmp
1346 (long_options[option_index].name,
1347 "qose3") == 0) {
1348
1349 if (update_arg((void *)&(args_info->qose3_arg),
1350 &(args_info->qose3_orig),
1351 &(args_info->qose3_given),
1352 &(local_args_info.qose3_given),
1353 optarg, 0, "0x0101", ARG_INT,
1354 check_ambiguity, override, 0, 0,
1355 "qose3", '-', additional_error))
1356 goto failure;
1357
1358 }
1359 /* Requested quality of service Extension 4. */
1360 else if (strcmp
1361 (long_options[option_index].name,
1362 "qose4") == 0) {
1363
1364 if (update_arg((void *)&(args_info->qose4_arg),
1365 &(args_info->qose4_orig),
1366 &(args_info->qose4_given),
1367 &(local_args_info.qose4_given),
1368 optarg, 0, "0x4040", ARG_INT,
1369 check_ambiguity, override, 0, 0,
1370 "qose4", '-', additional_error))
1371 goto failure;
1372
1373 }
1374 /* Charging characteristics. */
1375 else if (strcmp
1376 (long_options[option_index].name,
1377 "charging") == 0) {
1378
1379 if (update_arg
1380 ((void *)&(args_info->charging_arg),
1381 &(args_info->charging_orig),
1382 &(args_info->charging_given),
1383 &(local_args_info.charging_given), optarg,
1384 0, "0x0800", ARG_INT, check_ambiguity,
1385 override, 0, 0, "charging", '-',
1386 additional_error))
1387 goto failure;
1388
1389 }
1390 /* Create local network interface. */
1391 else if (strcmp
1392 (long_options[option_index].name,
1393 "createif") == 0) {
1394
1395 if (update_arg
1396 ((void *)&(args_info->createif_flag), 0,
1397 &(args_info->createif_given),
1398 &(local_args_info.createif_given), optarg,
1399 0, 0, ARG_FLAG, check_ambiguity, override,
1400 1, 0, "createif", '-', additional_error))
1401 goto failure;
1402
1403 }
1404 /* Create default route. */
1405 else if (strcmp
1406 (long_options[option_index].name,
1407 "defaultroute") == 0) {
1408
1409 if (update_arg
1410 ((void *)&(args_info->defaultroute_flag), 0,
1411 &(args_info->defaultroute_given),
1412 &(local_args_info.defaultroute_given),
1413 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1414 override, 1, 0, "defaultroute", '-',
1415 additional_error))
1416 goto failure;
1417
1418 }
1419 /* Script to run after link-up. */
1420 else if (strcmp(long_options[option_index].name, "ipup")
1421 == 0) {
1422
1423 if (update_arg((void *)&(args_info->ipup_arg),
1424 &(args_info->ipup_orig),
1425 &(args_info->ipup_given),
1426 &(local_args_info.ipup_given),
1427 optarg, 0, 0, ARG_STRING,
1428 check_ambiguity, override, 0, 0,
1429 "ipup", '-', additional_error))
1430 goto failure;
1431
1432 }
1433 /* Script to run after link-down. */
1434 else if (strcmp
1435 (long_options[option_index].name,
1436 "ipdown") == 0) {
1437
1438 if (update_arg((void *)&(args_info->ipdown_arg),
1439 &(args_info->ipdown_orig),
1440 &(args_info->ipdown_given),
1441 &(local_args_info.ipdown_given),
1442 optarg, 0, 0, ARG_STRING,
1443 check_ambiguity, override, 0, 0,
1444 "ipdown", '-', additional_error))
1445 goto failure;
1446
1447 }
Harald Welte73abc382017-10-10 08:50:11 +08001448 /* Name of the local network interface. */
1449 else if (strcmp
1450 (long_options[option_index].name,
1451 "tun-device") == 0) {
1452
1453 if (update_arg
1454 ((void *)&(args_info->tun_device_arg),
1455 &(args_info->tun_device_orig),
1456 &(args_info->tun_device_given),
1457 &(local_args_info.tun_device_given),
1458 optarg, 0, 0, ARG_STRING, check_ambiguity,
1459 override, 0, 0, "tun-device", '-',
1460 additional_error))
1461 goto failure;
1462
1463 }
Harald Weltef6214982017-09-24 10:23:24 +08001464 /* Ping remote host. */
1465 else if (strcmp
1466 (long_options[option_index].name,
1467 "pinghost") == 0) {
1468
1469 if (update_arg
1470 ((void *)&(args_info->pinghost_arg),
1471 &(args_info->pinghost_orig),
1472 &(args_info->pinghost_given),
1473 &(local_args_info.pinghost_given), optarg,
1474 0, 0, ARG_STRING, check_ambiguity,
1475 override, 0, 0, "pinghost", '-',
1476 additional_error))
1477 goto failure;
1478
1479 }
1480 /* Number of ping req per second. */
1481 else if (strcmp
1482 (long_options[option_index].name,
1483 "pingrate") == 0) {
1484
1485 if (update_arg
1486 ((void *)&(args_info->pingrate_arg),
1487 &(args_info->pingrate_orig),
1488 &(args_info->pingrate_given),
1489 &(local_args_info.pingrate_given), optarg,
1490 0, "1", ARG_INT, check_ambiguity, override,
1491 0, 0, "pingrate", '-', additional_error))
1492 goto failure;
1493
1494 }
1495 /* Number of ping data bytes. */
1496 else if (strcmp
1497 (long_options[option_index].name,
1498 "pingsize") == 0) {
1499
1500 if (update_arg
1501 ((void *)&(args_info->pingsize_arg),
1502 &(args_info->pingsize_orig),
1503 &(args_info->pingsize_given),
1504 &(local_args_info.pingsize_given), optarg,
1505 0, "56", ARG_INT, check_ambiguity,
1506 override, 0, 0, "pingsize", '-',
1507 additional_error))
1508 goto failure;
1509
1510 }
1511 /* Number of ping req to send. */
1512 else if (strcmp
1513 (long_options[option_index].name,
1514 "pingcount") == 0) {
1515
1516 if (update_arg
1517 ((void *)&(args_info->pingcount_arg),
1518 &(args_info->pingcount_orig),
1519 &(args_info->pingcount_given),
1520 &(local_args_info.pingcount_given), optarg,
1521 0, "0", ARG_INT, check_ambiguity, override,
1522 0, 0, "pingcount", '-', additional_error))
1523 goto failure;
1524
1525 }
1526 /* Do not print ping packet info. */
1527 else if (strcmp
1528 (long_options[option_index].name,
1529 "pingquiet") == 0) {
1530
1531 if (update_arg
1532 ((void *)&(args_info->pingquiet_flag), 0,
1533 &(args_info->pingquiet_given),
1534 &(local_args_info.pingquiet_given), optarg,
1535 0, 0, ARG_FLAG, check_ambiguity, override,
1536 1, 0, "pingquiet", '-', additional_error))
1537 goto failure;
1538
Harald Weltebed35df2011-11-02 13:06:18 +01001539 }
Harald Weltefbb9c7f2017-09-24 11:50:20 +08001540 /* Don't transmit G-PDU sequence nums. */
1541 else if (strcmp
1542 (long_options[option_index].name,
1543 "no-tx-gpdu-seq") == 0) {
1544
1545 if (update_arg
1546 ((void *)&(args_info->no_tx_gpdu_seq_flag),
1547 0, &(args_info->no_tx_gpdu_seq_given),
1548 &(local_args_info.no_tx_gpdu_seq_given),
1549 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1550 override, 1, 0, "no-tx-gpdu-seq", '-',
1551 additional_error))
1552 goto failure;
1553
1554 }
jjako52c24142002-12-16 13:33:51 +00001555
Harald Weltebed35df2011-11-02 13:06:18 +01001556 break;
1557 case '?': /* Invalid option. */
1558 /* `getopt_long' already printed an error message. */
1559 goto failure;
jjako52c24142002-12-16 13:33:51 +00001560
Harald Weltebed35df2011-11-02 13:06:18 +01001561 default: /* bug: option not considered. */
1562 fprintf(stderr, "%s: option unknown: %c%s\n",
1563 CMDLINE_PARSER_PACKAGE, c,
1564 (additional_error ? additional_error : ""));
1565 abort();
1566 } /* switch */
1567 } /* while */
jjako52c24142002-12-16 13:33:51 +00001568
Harald Welte73abc382017-10-10 08:50:11 +08001569 if (check_required) {
1570 error_occurred +=
1571 cmdline_parser_required2(args_info, argv[0],
1572 additional_error);
1573 }
1574
Harald Weltebed35df2011-11-02 13:06:18 +01001575 cmdline_parser_release(&local_args_info);
jjako52c24142002-12-16 13:33:51 +00001576
Harald Weltef6214982017-09-24 10:23:24 +08001577 if (error_occurred)
Harald Weltebed35df2011-11-02 13:06:18 +01001578 return (EXIT_FAILURE);
Harald Welte1b3e5772010-05-04 11:13:56 +02001579
Harald Weltebed35df2011-11-02 13:06:18 +01001580 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001581
1582failure:
Harald Weltebed35df2011-11-02 13:06:18 +01001583
1584 cmdline_parser_release(&local_args_info);
1585 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +00001586}
1587
Harald Welte1b3e5772010-05-04 11:13:56 +02001588#ifndef CONFIG_FILE_LINE_SIZE
1589#define CONFIG_FILE_LINE_SIZE 2048
1590#endif
1591#define ADDITIONAL_ERROR " in configuration file "
1592
1593#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1594/* 3 is for "--" and "=" */
1595
Harald Weltef6214982017-09-24 10:23:24 +08001596static int _cmdline_parser_configfile(const char *filename, int *my_argc)
jjako52c24142002-12-16 13:33:51 +00001597{
Harald Weltebed35df2011-11-02 13:06:18 +01001598 FILE *file;
Harald Weltef6214982017-09-24 10:23:24 +08001599 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
Harald Weltebed35df2011-11-02 13:06:18 +01001600 char linebuf[CONFIG_FILE_LINE_SIZE];
1601 int line_num = 0;
Harald Weltef6214982017-09-24 10:23:24 +08001602 int result = 0, equal;
Harald Weltebed35df2011-11-02 13:06:18 +01001603 char *fopt, *farg;
1604 char *str_index;
1605 size_t len, next_token;
1606 char delimiter;
Harald Welte1b3e5772010-05-04 11:13:56 +02001607
Harald Weltef6214982017-09-24 10:23:24 +08001608 if ((file = fopen(filename, "r")) == 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001609 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1610 CMDLINE_PARSER_PACKAGE, filename);
Harald Weltef6214982017-09-24 10:23:24 +08001611 return EXIT_FAILURE;
Harald Weltebed35df2011-11-02 13:06:18 +01001612 }
jjako52c24142002-12-16 13:33:51 +00001613
Harald Weltef6214982017-09-24 10:23:24 +08001614 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001615 ++line_num;
1616 my_argv[0] = '\0';
1617 len = strlen(linebuf);
1618 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) {
1619 fprintf(stderr,
1620 "%s:%s:%d: Line too long in configuration file\n",
1621 CMDLINE_PARSER_PACKAGE, filename, line_num);
1622 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001623 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001624 }
jjako52c24142002-12-16 13:33:51 +00001625
Harald Weltebed35df2011-11-02 13:06:18 +01001626 /* find first non-whitespace character in the line */
1627 next_token = strspn(linebuf, " \t\r\n");
1628 str_index = linebuf + next_token;
jjako52c24142002-12-16 13:33:51 +00001629
Harald Weltebed35df2011-11-02 13:06:18 +01001630 if (str_index[0] == '\0' || str_index[0] == '#')
1631 continue; /* empty line or comment line is skipped */
Harald Welte1b3e5772010-05-04 11:13:56 +02001632
Harald Weltebed35df2011-11-02 13:06:18 +01001633 fopt = str_index;
Harald Welte1b3e5772010-05-04 11:13:56 +02001634
Harald Weltebed35df2011-11-02 13:06:18 +01001635 /* truncate fopt at the end of the first non-valid character */
1636 next_token = strcspn(fopt, " \t\r\n=");
Harald Welte1b3e5772010-05-04 11:13:56 +02001637
Harald Weltebed35df2011-11-02 13:06:18 +01001638 if (fopt[next_token] == '\0') { /* the line is over */
Harald Weltef6214982017-09-24 10:23:24 +08001639 farg = 0;
Harald Weltebed35df2011-11-02 13:06:18 +01001640 equal = 0;
1641 goto noarg;
1642 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001643
Harald Weltebed35df2011-11-02 13:06:18 +01001644 /* remember if equal sign is present */
1645 equal = (fopt[next_token] == '=');
1646 fopt[next_token++] = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001647
Harald Weltebed35df2011-11-02 13:06:18 +01001648 /* advance pointers to the next token after the end of fopt */
1649 next_token += strspn(fopt + next_token, " \t\r\n");
Harald Weltef6214982017-09-24 10:23:24 +08001650
Harald Weltebed35df2011-11-02 13:06:18 +01001651 /* check for the presence of equal sign, and if so, skip it */
1652 if (!equal)
1653 if ((equal = (fopt[next_token] == '='))) {
1654 next_token++;
1655 next_token +=
1656 strspn(fopt + next_token, " \t\r\n");
1657 }
1658 str_index += next_token;
Harald Welte1b3e5772010-05-04 11:13:56 +02001659
Harald Weltebed35df2011-11-02 13:06:18 +01001660 /* find argument */
1661 farg = str_index;
1662 if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */
1663 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1664 if (!str_index) {
1665 fprintf
1666 (stderr,
1667 "%s:%s:%d: unterminated string in configuration file\n",
1668 CMDLINE_PARSER_PACKAGE, filename,
1669 line_num);
1670 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001671 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001672 }
1673 } else { /* read up the remaining part up to a delimiter */
1674 next_token = strcspn(farg, " \t\r\n#\'\"");
1675 str_index += next_token;
1676 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001677
Harald Weltebed35df2011-11-02 13:06:18 +01001678 /* truncate farg at the delimiter and store it for further check */
1679 delimiter = *str_index, *str_index++ = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001680
Harald Weltebed35df2011-11-02 13:06:18 +01001681 /* everything but comment is illegal at the end of line */
1682 if (delimiter != '\0' && delimiter != '#') {
1683 str_index += strspn(str_index, " \t\r\n");
1684 if (*str_index != '\0' && *str_index != '#') {
1685 fprintf
1686 (stderr,
1687 "%s:%s:%d: malformed string in configuration file\n",
1688 CMDLINE_PARSER_PACKAGE, filename,
1689 line_num);
1690 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001691 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001692 }
1693 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001694
Harald Weltebed35df2011-11-02 13:06:18 +01001695noarg:
Harald Weltef6214982017-09-24 10:23:24 +08001696 if (!strcmp(fopt, "include")) {
1697 if (farg && *farg) {
1698 result =
1699 _cmdline_parser_configfile(farg, my_argc);
1700 } else {
1701 fprintf(stderr,
1702 "%s:%s:%d: include requires a filename argument.\n",
1703 CMDLINE_PARSER_PACKAGE, filename,
1704 line_num);
1705 }
1706 continue;
1707 }
Harald Weltebed35df2011-11-02 13:06:18 +01001708 len = strlen(fopt);
Harald Weltebed35df2011-11-02 13:06:18 +01001709 strcat(my_argv, len > 1 ? "--" : "-");
1710 strcat(my_argv, fopt);
1711 if (len > 1 && ((farg && *farg) || equal))
1712 strcat(my_argv, "=");
1713 if (farg && *farg)
1714 strcat(my_argv, farg);
Harald Weltef6214982017-09-24 10:23:24 +08001715 ++(*my_argc);
Harald Welte1b3e5772010-05-04 11:13:56 +02001716
Harald Weltebed35df2011-11-02 13:06:18 +01001717 cmd_line_list_tmp =
1718 (struct line_list *)malloc(sizeof(struct line_list));
1719 cmd_line_list_tmp->next = cmd_line_list;
1720 cmd_line_list = cmd_line_list_tmp;
1721 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1722 } /* while */
jjako52c24142002-12-16 13:33:51 +00001723
Harald Weltebed35df2011-11-02 13:06:18 +01001724 if (file)
1725 fclose(file);
Harald Weltef6214982017-09-24 10:23:24 +08001726 return result;
1727}
1728
1729int
1730cmdline_parser_configfile(const char *filename,
1731 struct gengetopt_args_info *args_info,
1732 int override, int initialize, int check_required)
1733{
1734 struct cmdline_parser_params params;
1735
1736 params.override = override;
1737 params.initialize = initialize;
1738 params.check_required = check_required;
1739 params.check_ambiguity = 0;
1740 params.print_errors = 1;
1741
1742 return cmdline_parser_config_file(filename, args_info, &params);
1743}
1744
1745int
1746cmdline_parser_config_file(const char *filename,
1747 struct gengetopt_args_info *args_info,
1748 struct cmdline_parser_params *params)
1749{
1750 int i, result;
1751 int my_argc = 1;
1752 char **my_argv_arg;
1753 char *additional_error;
1754
1755 /* store the program name */
1756 cmd_line_list_tmp =
1757 (struct line_list *)malloc(sizeof(struct line_list));
1758 cmd_line_list_tmp->next = cmd_line_list;
1759 cmd_line_list = cmd_line_list_tmp;
1760 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1761
1762 result = _cmdline_parser_configfile(filename, &my_argc);
1763
1764 if (result != EXIT_FAILURE) {
1765 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1766 cmd_line_list_tmp = cmd_line_list;
1767
1768 for (i = my_argc - 1; i >= 0; --i) {
1769 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1770 cmd_line_list_tmp = cmd_line_list_tmp->next;
1771 }
1772
1773 my_argv_arg[my_argc] = 0;
1774
1775 additional_error =
1776 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) +
1777 1);
1778 strcpy(additional_error, ADDITIONAL_ERROR);
1779 strcat(additional_error, filename);
1780 result =
1781 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1782 params, additional_error);
1783
1784 free(additional_error);
1785 free(my_argv_arg);
1786 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001787
Harald Weltebed35df2011-11-02 13:06:18 +01001788 free_cmd_list();
1789 if (result == EXIT_FAILURE) {
1790 cmdline_parser_free(args_info);
1791 exit(EXIT_FAILURE);
1792 }
1793
1794 return result;
jjako52c24142002-12-16 13:33:51 +00001795}