blob: 441322b2ae1723970ebf5844bbd652af7af5775e [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 */
679 if (args_info->tun_device_given && !args_info->createif_given) {
680 fprintf(stderr,
681 "%s: '--tun-device' option depends on option 'createif'%s\n",
682 prog_name, (additional_error ? additional_error : ""));
683 error_occurred = 1;
684 }
685
686 return error_occurred;
Harald Welte1b3e5772010-05-04 11:13:56 +0200687}
688
Harald Weltef6214982017-09-24 10:23:24 +0800689static char *package_name = 0;
690
691/**
692 * @brief updates an option
693 * @param field the generic pointer to the field to update
694 * @param orig_field the pointer to the orig field
695 * @param field_given the pointer to the number of occurrence of this option
696 * @param prev_given the pointer to the number of occurrence already seen
697 * @param value the argument for this option (if null no arg was specified)
698 * @param possible_values the possible values for this option (if specified)
699 * @param default_value the default value (in case the option only accepts fixed values)
700 * @param arg_type the type of this option
701 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
702 * @param override @see cmdline_parser_params.override
703 * @param no_free whether to free a possible previous value
704 * @param multiple_option whether this is a multiple option
705 * @param long_opt the corresponding long option
706 * @param short_opt the corresponding short option (or '-' if none)
707 * @param additional_error possible further error specification
708 */
709static
710int update_arg(void *field, char **orig_field,
711 unsigned int *field_given, unsigned int *prev_given,
712 char *value, const char *possible_values[],
713 const char *default_value,
714 cmdline_parser_arg_type arg_type,
715 int check_ambiguity, int override,
716 int no_free, int multiple_option,
717 const char *long_opt, char short_opt,
718 const char *additional_error)
719{
720 char *stop_char = 0;
721 const char *val = value;
722 int found;
723 char **string_field;
724 FIX_UNUSED(field);
725
726 stop_char = 0;
727 found = 0;
728
729 if (!multiple_option && prev_given
730 && (*prev_given || (check_ambiguity && *field_given))) {
731 if (short_opt != '-')
732 fprintf(stderr,
733 "%s: `--%s' (`-%c') option given more than once%s\n",
734 package_name, long_opt, short_opt,
735 (additional_error ? additional_error : ""));
736 else
737 fprintf(stderr,
738 "%s: `--%s' option given more than once%s\n",
739 package_name, long_opt,
740 (additional_error ? additional_error : ""));
741 return 1; /* failure */
742 }
743
744 FIX_UNUSED(default_value);
745
746 if (field_given && *field_given && !override)
747 return 0;
748 if (prev_given)
749 (*prev_given)++;
750 if (field_given)
751 (*field_given)++;
752 if (possible_values)
753 val = possible_values[found];
754
755 switch (arg_type) {
756 case ARG_FLAG:
757 *((int *)field) = !*((int *)field);
758 break;
759 case ARG_INT:
760 if (val)
761 *((int *)field) = strtol(val, &stop_char, 0);
762 break;
763 case ARG_STRING:
764 if (val) {
765 string_field = (char **)field;
766 if (!no_free && *string_field)
767 free(*string_field); /* free previous string */
768 *string_field = gengetopt_strdup(val);
769 }
770 break;
771 default:
772 break;
773 };
774
775 /* check numeric conversion */
776 switch (arg_type) {
777 case ARG_INT:
778 if (val && !(stop_char && *stop_char == '\0')) {
779 fprintf(stderr, "%s: invalid numeric value: %s\n",
780 package_name, val);
781 return 1; /* failure */
782 }
783 break;
784 default:
785 ;
786 };
787
788 /* store the original value */
789 switch (arg_type) {
790 case ARG_NO:
791 case ARG_FLAG:
792 break;
793 default:
794 if (value && orig_field) {
795 if (no_free) {
796 *orig_field = value;
797 } else {
798 if (*orig_field)
799 free(*orig_field); /* free previous string */
800 *orig_field = gengetopt_strdup(value);
801 }
802 }
803 };
804
805 return 0; /* OK */
806}
807
Harald Welte1b3e5772010-05-04 11:13:56 +0200808int
Harald Weltef6214982017-09-24 10:23:24 +0800809cmdline_parser_internal(int argc, char **argv,
810 struct gengetopt_args_info *args_info,
811 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +0100812 const char *additional_error)
Harald Welte1b3e5772010-05-04 11:13:56 +0200813{
Harald Weltebed35df2011-11-02 13:06:18 +0100814 int c; /* Character of the parsed option. */
Harald Welte1b3e5772010-05-04 11:13:56 +0200815
Harald Weltef6214982017-09-24 10:23:24 +0800816 int error_occurred = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100817 struct gengetopt_args_info local_args_info;
Harald Welte1b3e5772010-05-04 11:13:56 +0200818
Harald Weltef6214982017-09-24 10:23:24 +0800819 int override;
820 int initialize;
821 int check_required;
822 int check_ambiguity;
823
824 package_name = argv[0];
825
826 override = params->override;
827 initialize = params->initialize;
828 check_required = params->check_required;
829 check_ambiguity = params->check_ambiguity;
830
Harald Weltebed35df2011-11-02 13:06:18 +0100831 if (initialize)
832 cmdline_parser_init(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200833
Harald Weltebed35df2011-11-02 13:06:18 +0100834 cmdline_parser_init(&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000835
Harald Weltebed35df2011-11-02 13:06:18 +0100836 optarg = 0;
837 optind = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800838 opterr = params->print_errors;
Harald Weltebed35df2011-11-02 13:06:18 +0100839 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000840
Harald Weltebed35df2011-11-02 13:06:18 +0100841 while (1) {
842 int option_index = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200843
Harald Weltebed35df2011-11-02 13:06:18 +0100844 static struct option long_options[] = {
845 {"help", 0, NULL, 'h'},
846 {"version", 0, NULL, 'V'},
847 {"debug", 0, NULL, 'd'},
848 {"conf", 1, NULL, 'c'},
849 {"pidfile", 1, NULL, 0},
850 {"statedir", 1, NULL, 0},
851 {"dns", 1, NULL, 0},
852 {"listen", 1, NULL, 'l'},
853 {"remote", 1, NULL, 'r'},
854 {"contexts", 1, NULL, 0},
855 {"timelimit", 1, NULL, 0},
856 {"gtpversion", 1, NULL, 0},
857 {"apn", 1, NULL, 'a'},
858 {"selmode", 1, NULL, 0},
859 {"rattype", 1, NULL, 0},
860 {"userloc", 1, NULL, 0},
861 {"rai", 1, NULL, 0},
862 {"mstz", 1, NULL, 0},
863 {"imeisv", 1, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800864 {"norecovery", 0, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100865 {"imsi", 1, NULL, 'i'},
866 {"nsapi", 1, NULL, 0},
867 {"msisdn", 1, NULL, 'm'},
868 {"qos", 1, NULL, 'q'},
869 {"qose1", 1, NULL, 0},
870 {"qose2", 1, NULL, 0},
871 {"qose3", 1, NULL, 0},
872 {"qose4", 1, NULL, 0},
873 {"charging", 1, NULL, 0},
874 {"uid", 1, NULL, 'u'},
875 {"pwd", 1, NULL, 'p'},
876 {"createif", 0, NULL, 0},
877 {"net", 1, NULL, 'n'},
878 {"defaultroute", 0, NULL, 0},
879 {"ipup", 1, NULL, 0},
880 {"ipdown", 1, NULL, 0},
Harald Welte73abc382017-10-10 08:50:11 +0800881 {"tun-device", 1, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100882 {"pinghost", 1, NULL, 0},
883 {"pingrate", 1, NULL, 0},
884 {"pingsize", 1, NULL, 0},
885 {"pingcount", 1, NULL, 0},
886 {"pingquiet", 0, NULL, 0},
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800887 {"no-tx-gpdu-seq", 0, NULL, 0},
Harald Welte840a8e92017-09-24 18:12:40 +0800888 {"pdp-type", 1, NULL, 't'},
Harald Weltef6214982017-09-24 10:23:24 +0800889 {0, 0, 0, 0}
Harald Weltebed35df2011-11-02 13:06:18 +0100890 };
jjako52c24142002-12-16 13:33:51 +0000891
Harald Welte840a8e92017-09-24 18:12:40 +0800892 c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:t:",
Harald Weltebed35df2011-11-02 13:06:18 +0100893 long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000894
Harald Weltebed35df2011-11-02 13:06:18 +0100895 if (c == -1)
896 break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000897
Harald Weltebed35df2011-11-02 13:06:18 +0100898 switch (c) {
899 case 'h': /* Print help and exit. */
900 cmdline_parser_print_help();
901 cmdline_parser_free(&local_args_info);
902 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000903
Harald Weltebed35df2011-11-02 13:06:18 +0100904 case 'V': /* Print version and exit. */
905 cmdline_parser_print_version();
906 cmdline_parser_free(&local_args_info);
907 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000908
Harald Weltebed35df2011-11-02 13:06:18 +0100909 case 'd': /* Run in debug mode. */
jjako52c24142002-12-16 13:33:51 +0000910
Harald Weltef6214982017-09-24 10:23:24 +0800911 if (update_arg
912 ((void *)&(args_info->debug_flag), 0,
913 &(args_info->debug_given),
914 &(local_args_info.debug_given), optarg, 0, 0,
915 ARG_FLAG, check_ambiguity, override, 1, 0, "debug",
916 'd', additional_error))
917 goto failure;
918
919 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100920 case 'c': /* Read configuration file. */
jjako52c24142002-12-16 13:33:51 +0000921
Harald Weltef6214982017-09-24 10:23:24 +0800922 if (update_arg((void *)&(args_info->conf_arg),
923 &(args_info->conf_orig),
924 &(args_info->conf_given),
925 &(local_args_info.conf_given), optarg, 0,
926 0, ARG_STRING, check_ambiguity, override,
927 0, 0, "conf", 'c', additional_error))
928 goto failure;
929
930 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100931 case 'l': /* Local interface. */
jjako52c24142002-12-16 13:33:51 +0000932
Harald Weltef6214982017-09-24 10:23:24 +0800933 if (update_arg((void *)&(args_info->listen_arg),
934 &(args_info->listen_orig),
935 &(args_info->listen_given),
936 &(local_args_info.listen_given), optarg,
937 0, 0, ARG_STRING, check_ambiguity,
938 override, 0, 0, "listen", 'l',
939 additional_error))
940 goto failure;
941
942 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100943 case 'r': /* Remote host. */
jjako52c24142002-12-16 13:33:51 +0000944
Harald Weltef6214982017-09-24 10:23:24 +0800945 if (update_arg((void *)&(args_info->remote_arg),
946 &(args_info->remote_orig),
947 &(args_info->remote_given),
948 &(local_args_info.remote_given), optarg,
949 0, 0, ARG_STRING, check_ambiguity,
950 override, 0, 0, "remote", 'r',
951 additional_error))
952 goto failure;
953
954 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100955 case 'a': /* Access point name. */
jjako52c24142002-12-16 13:33:51 +0000956
Harald Weltef6214982017-09-24 10:23:24 +0800957 if (update_arg((void *)&(args_info->apn_arg),
958 &(args_info->apn_orig),
959 &(args_info->apn_given),
960 &(local_args_info.apn_given), optarg, 0,
961 "internet", ARG_STRING, check_ambiguity,
962 override, 0, 0, "apn", 'a',
963 additional_error))
964 goto failure;
965
966 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100967 case 'i': /* IMSI. */
jjako52c24142002-12-16 13:33:51 +0000968
Harald Weltef6214982017-09-24 10:23:24 +0800969 if (update_arg((void *)&(args_info->imsi_arg),
970 &(args_info->imsi_orig),
971 &(args_info->imsi_given),
972 &(local_args_info.imsi_given), optarg, 0,
973 "240010123456789", ARG_STRING,
974 check_ambiguity, override, 0, 0, "imsi",
975 'i', additional_error))
976 goto failure;
977
978 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100979 case 'm': /* Mobile Station ISDN number. */
jjako52c24142002-12-16 13:33:51 +0000980
Harald Weltef6214982017-09-24 10:23:24 +0800981 if (update_arg((void *)&(args_info->msisdn_arg),
982 &(args_info->msisdn_orig),
983 &(args_info->msisdn_given),
984 &(local_args_info.msisdn_given), optarg,
985 0, "46702123456", ARG_STRING,
986 check_ambiguity, override, 0, 0,
987 "msisdn", 'm', additional_error))
988 goto failure;
989
990 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100991 case 'q': /* Requested quality of service. */
jjako52c24142002-12-16 13:33:51 +0000992
Harald Weltef6214982017-09-24 10:23:24 +0800993 if (update_arg((void *)&(args_info->qos_arg),
994 &(args_info->qos_orig),
995 &(args_info->qos_given),
996 &(local_args_info.qos_given), optarg, 0,
997 "0x000b921f", ARG_INT, check_ambiguity,
998 override, 0, 0, "qos", 'q',
999 additional_error))
1000 goto failure;
1001
1002 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001003 case 'u': /* Login user ID. */
jjako52c24142002-12-16 13:33:51 +00001004
Harald Weltef6214982017-09-24 10:23:24 +08001005 if (update_arg((void *)&(args_info->uid_arg),
1006 &(args_info->uid_orig),
1007 &(args_info->uid_given),
1008 &(local_args_info.uid_given), optarg, 0,
1009 "mig", ARG_STRING, check_ambiguity,
1010 override, 0, 0, "uid", 'u',
1011 additional_error))
1012 goto failure;
1013
1014 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001015 case 'p': /* Login password. */
jjako52c24142002-12-16 13:33:51 +00001016
Harald Weltef6214982017-09-24 10:23:24 +08001017 if (update_arg((void *)&(args_info->pwd_arg),
1018 &(args_info->pwd_orig),
1019 &(args_info->pwd_given),
1020 &(local_args_info.pwd_given), optarg, 0,
1021 "hemmelig", ARG_STRING, check_ambiguity,
1022 override, 0, 0, "pwd", 'p',
1023 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001024 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001025
1026 break;
1027 case 'n': /* Network address for local interface. */
1028
1029 if (update_arg((void *)&(args_info->net_arg),
1030 &(args_info->net_orig),
1031 &(args_info->net_given),
1032 &(local_args_info.net_given), optarg, 0,
1033 0, ARG_STRING, check_ambiguity, override,
1034 0, 0, "net", 'n', additional_error))
1035 goto failure;
1036
Harald Weltebed35df2011-11-02 13:06:18 +01001037 break;
Harald Welte840a8e92017-09-24 18:12:40 +08001038 case 't': /* PDP Type. */
1039
1040 if (update_arg((void *)&(args_info->pdp_type_arg),
1041 &(args_info->pdp_type_orig),
1042 &(args_info->pdp_type_given),
1043 &(local_args_info.pdp_type_given),
1044 optarg, 0, "v4", ARG_STRING,
1045 check_ambiguity, override, 0, 0,
1046 "pdp-type", 't', additional_error))
1047 goto failure;
1048
1049 break;
jjako193e8b12003-11-10 12:31:41 +00001050
Harald Weltebed35df2011-11-02 13:06:18 +01001051 case 0: /* Long option with no short option */
1052 /* Filename of process id file. */
1053 if (strcmp(long_options[option_index].name, "pidfile")
1054 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001055
1056 if (update_arg
1057 ((void *)&(args_info->pidfile_arg),
1058 &(args_info->pidfile_orig),
1059 &(args_info->pidfile_given),
1060 &(local_args_info.pidfile_given), optarg,
1061 0, "./sgsnemu.pid", ARG_STRING,
1062 check_ambiguity, override, 0, 0, "pidfile",
1063 '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001064 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001065
Harald Weltebed35df2011-11-02 13:06:18 +01001066 }
1067 /* Directory of nonvolatile data. */
1068 else if (strcmp
1069 (long_options[option_index].name,
1070 "statedir") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001071
1072 if (update_arg
1073 ((void *)&(args_info->statedir_arg),
1074 &(args_info->statedir_orig),
1075 &(args_info->statedir_given),
1076 &(local_args_info.statedir_given), optarg,
1077 0, "./", ARG_STRING, check_ambiguity,
1078 override, 0, 0, "statedir", '-',
1079 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001080 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001081
Harald Weltebed35df2011-11-02 13:06:18 +01001082 }
1083 /* DNS Server to use. */
1084 else if (strcmp(long_options[option_index].name, "dns")
1085 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001086
1087 if (update_arg((void *)&(args_info->dns_arg),
1088 &(args_info->dns_orig),
1089 &(args_info->dns_given),
1090 &(local_args_info.dns_given),
1091 optarg, 0, 0, ARG_STRING,
1092 check_ambiguity, override, 0, 0,
1093 "dns", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001094 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001095
Harald Weltebed35df2011-11-02 13:06:18 +01001096 }
1097 /* Number of contexts. */
1098 else if (strcmp
1099 (long_options[option_index].name,
1100 "contexts") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001101
1102 if (update_arg
1103 ((void *)&(args_info->contexts_arg),
1104 &(args_info->contexts_orig),
1105 &(args_info->contexts_given),
1106 &(local_args_info.contexts_given), optarg,
1107 0, "1", ARG_INT, check_ambiguity, override,
1108 0, 0, "contexts", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001109 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001110
Harald Weltebed35df2011-11-02 13:06:18 +01001111 }
1112 /* Exit after timelimit seconds. */
1113 else if (strcmp
1114 (long_options[option_index].name,
1115 "timelimit") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001116
1117 if (update_arg
1118 ((void *)&(args_info->timelimit_arg),
1119 &(args_info->timelimit_orig),
1120 &(args_info->timelimit_given),
1121 &(local_args_info.timelimit_given), optarg,
1122 0, "0", ARG_INT, check_ambiguity, override,
1123 0, 0, "timelimit", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001124 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001125
Harald Weltebed35df2011-11-02 13:06:18 +01001126 }
1127 /* GTP version to use. */
1128 else if (strcmp
1129 (long_options[option_index].name,
1130 "gtpversion") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001131
1132 if (update_arg
1133 ((void *)&(args_info->gtpversion_arg),
1134 &(args_info->gtpversion_orig),
1135 &(args_info->gtpversion_given),
1136 &(local_args_info.gtpversion_given),
1137 optarg, 0, "1", ARG_INT, check_ambiguity,
1138 override, 0, 0, "gtpversion", '-',
1139 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001140 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001141
Harald Weltebed35df2011-11-02 13:06:18 +01001142 }
1143 /* Selection mode. */
1144 else if (strcmp
1145 (long_options[option_index].name,
1146 "selmode") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001147
1148 if (update_arg
1149 ((void *)&(args_info->selmode_arg),
1150 &(args_info->selmode_orig),
1151 &(args_info->selmode_given),
1152 &(local_args_info.selmode_given), optarg,
1153 0, "0x01", ARG_INT, check_ambiguity,
1154 override, 0, 0, "selmode", '-',
1155 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001156 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001157
Harald Weltebed35df2011-11-02 13:06:18 +01001158 }
1159 /* Radio Access Technology Type. */
1160 else if (strcmp
1161 (long_options[option_index].name,
1162 "rattype") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001163
1164 if (update_arg
1165 ((void *)&(args_info->rattype_arg),
1166 &(args_info->rattype_orig),
1167 &(args_info->rattype_given),
1168 &(local_args_info.rattype_given), optarg,
1169 0, "1", ARG_INT, check_ambiguity, override,
1170 0, 0, "rattype", '-', additional_error))
1171 goto failure;
1172
Harald Weltebed35df2011-11-02 13:06:18 +01001173 }
1174 /* User Location Information. */
1175 else if (strcmp
1176 (long_options[option_index].name,
1177 "userloc") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001178
1179 if (update_arg
1180 ((void *)&(args_info->userloc_arg),
1181 &(args_info->userloc_orig),
1182 &(args_info->userloc_given),
1183 &(local_args_info.userloc_given), optarg,
1184 0, "02509946241207", ARG_STRING,
1185 check_ambiguity, override, 0, 0, "userloc",
1186 '-', additional_error))
1187 goto failure;
1188
Harald Weltebed35df2011-11-02 13:06:18 +01001189 }
1190 /* Routing Area Information. */
1191 else if (strcmp(long_options[option_index].name, "rai")
1192 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001193
1194 if (update_arg((void *)&(args_info->rai_arg),
1195 &(args_info->rai_orig),
1196 &(args_info->rai_given),
1197 &(local_args_info.rai_given),
1198 optarg, 0, "02509946241207",
1199 ARG_STRING, check_ambiguity,
1200 override, 0, 0, "rai", '-',
1201 additional_error))
1202 goto failure;
1203
Harald Weltebed35df2011-11-02 13:06:18 +01001204 }
Harald Weltef6214982017-09-24 10:23:24 +08001205 /* MS Time Zone. */
Harald Weltebed35df2011-11-02 13:06:18 +01001206 else if (strcmp(long_options[option_index].name, "mstz")
1207 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001208
1209 if (update_arg((void *)&(args_info->mstz_arg),
1210 &(args_info->mstz_orig),
1211 &(args_info->mstz_given),
1212 &(local_args_info.mstz_given),
1213 optarg, 0, "0", ARG_STRING,
1214 check_ambiguity, override, 0, 0,
1215 "mstz", '-', additional_error))
1216 goto failure;
1217
Harald Weltebed35df2011-11-02 13:06:18 +01001218 }
Harald Weltef6214982017-09-24 10:23:24 +08001219 /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */
Harald Weltebed35df2011-11-02 13:06:18 +01001220 else if (strcmp
1221 (long_options[option_index].name,
1222 "imeisv") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001223
1224 if (update_arg((void *)&(args_info->imeisv_arg),
1225 &(args_info->imeisv_orig),
1226 &(args_info->imeisv_given),
1227 &(local_args_info.imeisv_given),
1228 optarg, 0, "2143658709214365",
1229 ARG_STRING, check_ambiguity,
1230 override, 0, 0, "imeisv", '-',
1231 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001232 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001233
Harald Weltebed35df2011-11-02 13:06:18 +01001234 }
1235 /* Do not send recovery. */
1236 else if (strcmp
1237 (long_options[option_index].name,
1238 "norecovery") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001239
1240 if (update_arg
1241 ((void *)&(args_info->norecovery_flag), 0,
1242 &(args_info->norecovery_given),
1243 &(local_args_info.norecovery_given),
1244 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1245 override, 1, 0, "norecovery", '-',
1246 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001247 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001248
1249 }
1250 /* NSAPI. */
1251 else if (strcmp
1252 (long_options[option_index].name,
1253 "nsapi") == 0) {
1254
1255 if (update_arg((void *)&(args_info->nsapi_arg),
1256 &(args_info->nsapi_orig),
1257 &(args_info->nsapi_given),
1258 &(local_args_info.nsapi_given),
1259 optarg, 0, "0", ARG_INT,
1260 check_ambiguity, override, 0, 0,
1261 "nsapi", '-', additional_error))
1262 goto failure;
1263
1264 }
1265 /* Requested quality of service Extension 1. */
1266 else if (strcmp
1267 (long_options[option_index].name,
1268 "qose1") == 0) {
1269
1270 if (update_arg((void *)&(args_info->qose1_arg),
1271 &(args_info->qose1_orig),
1272 &(args_info->qose1_given),
1273 &(local_args_info.qose1_given),
1274 optarg, 0, "0x9396404074f9ffff",
1275 ARG_INT, check_ambiguity,
1276 override, 0, 0, "qose1", '-',
1277 additional_error))
1278 goto failure;
1279
1280 }
1281 /* Requested quality of service Extension 2. */
1282 else if (strcmp
1283 (long_options[option_index].name,
1284 "qose2") == 0) {
1285
1286 if (update_arg((void *)&(args_info->qose2_arg),
1287 &(args_info->qose2_orig),
1288 &(args_info->qose2_given),
1289 &(local_args_info.qose2_given),
1290 optarg, 0, "0x11", ARG_INT,
1291 check_ambiguity, override, 0, 0,
1292 "qose2", '-', additional_error))
1293 goto failure;
1294
1295 }
1296 /* Requested quality of service Extension 3. */
1297 else if (strcmp
1298 (long_options[option_index].name,
1299 "qose3") == 0) {
1300
1301 if (update_arg((void *)&(args_info->qose3_arg),
1302 &(args_info->qose3_orig),
1303 &(args_info->qose3_given),
1304 &(local_args_info.qose3_given),
1305 optarg, 0, "0x0101", ARG_INT,
1306 check_ambiguity, override, 0, 0,
1307 "qose3", '-', additional_error))
1308 goto failure;
1309
1310 }
1311 /* Requested quality of service Extension 4. */
1312 else if (strcmp
1313 (long_options[option_index].name,
1314 "qose4") == 0) {
1315
1316 if (update_arg((void *)&(args_info->qose4_arg),
1317 &(args_info->qose4_orig),
1318 &(args_info->qose4_given),
1319 &(local_args_info.qose4_given),
1320 optarg, 0, "0x4040", ARG_INT,
1321 check_ambiguity, override, 0, 0,
1322 "qose4", '-', additional_error))
1323 goto failure;
1324
1325 }
1326 /* Charging characteristics. */
1327 else if (strcmp
1328 (long_options[option_index].name,
1329 "charging") == 0) {
1330
1331 if (update_arg
1332 ((void *)&(args_info->charging_arg),
1333 &(args_info->charging_orig),
1334 &(args_info->charging_given),
1335 &(local_args_info.charging_given), optarg,
1336 0, "0x0800", ARG_INT, check_ambiguity,
1337 override, 0, 0, "charging", '-',
1338 additional_error))
1339 goto failure;
1340
1341 }
1342 /* Create local network interface. */
1343 else if (strcmp
1344 (long_options[option_index].name,
1345 "createif") == 0) {
1346
1347 if (update_arg
1348 ((void *)&(args_info->createif_flag), 0,
1349 &(args_info->createif_given),
1350 &(local_args_info.createif_given), optarg,
1351 0, 0, ARG_FLAG, check_ambiguity, override,
1352 1, 0, "createif", '-', additional_error))
1353 goto failure;
1354
1355 }
1356 /* Create default route. */
1357 else if (strcmp
1358 (long_options[option_index].name,
1359 "defaultroute") == 0) {
1360
1361 if (update_arg
1362 ((void *)&(args_info->defaultroute_flag), 0,
1363 &(args_info->defaultroute_given),
1364 &(local_args_info.defaultroute_given),
1365 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1366 override, 1, 0, "defaultroute", '-',
1367 additional_error))
1368 goto failure;
1369
1370 }
1371 /* Script to run after link-up. */
1372 else if (strcmp(long_options[option_index].name, "ipup")
1373 == 0) {
1374
1375 if (update_arg((void *)&(args_info->ipup_arg),
1376 &(args_info->ipup_orig),
1377 &(args_info->ipup_given),
1378 &(local_args_info.ipup_given),
1379 optarg, 0, 0, ARG_STRING,
1380 check_ambiguity, override, 0, 0,
1381 "ipup", '-', additional_error))
1382 goto failure;
1383
1384 }
1385 /* Script to run after link-down. */
1386 else if (strcmp
1387 (long_options[option_index].name,
1388 "ipdown") == 0) {
1389
1390 if (update_arg((void *)&(args_info->ipdown_arg),
1391 &(args_info->ipdown_orig),
1392 &(args_info->ipdown_given),
1393 &(local_args_info.ipdown_given),
1394 optarg, 0, 0, ARG_STRING,
1395 check_ambiguity, override, 0, 0,
1396 "ipdown", '-', additional_error))
1397 goto failure;
1398
1399 }
Harald Welte73abc382017-10-10 08:50:11 +08001400 /* Name of the local network interface. */
1401 else if (strcmp
1402 (long_options[option_index].name,
1403 "tun-device") == 0) {
1404
1405 if (update_arg
1406 ((void *)&(args_info->tun_device_arg),
1407 &(args_info->tun_device_orig),
1408 &(args_info->tun_device_given),
1409 &(local_args_info.tun_device_given),
1410 optarg, 0, 0, ARG_STRING, check_ambiguity,
1411 override, 0, 0, "tun-device", '-',
1412 additional_error))
1413 goto failure;
1414
1415 }
Harald Weltef6214982017-09-24 10:23:24 +08001416 /* Ping remote host. */
1417 else if (strcmp
1418 (long_options[option_index].name,
1419 "pinghost") == 0) {
1420
1421 if (update_arg
1422 ((void *)&(args_info->pinghost_arg),
1423 &(args_info->pinghost_orig),
1424 &(args_info->pinghost_given),
1425 &(local_args_info.pinghost_given), optarg,
1426 0, 0, ARG_STRING, check_ambiguity,
1427 override, 0, 0, "pinghost", '-',
1428 additional_error))
1429 goto failure;
1430
1431 }
1432 /* Number of ping req per second. */
1433 else if (strcmp
1434 (long_options[option_index].name,
1435 "pingrate") == 0) {
1436
1437 if (update_arg
1438 ((void *)&(args_info->pingrate_arg),
1439 &(args_info->pingrate_orig),
1440 &(args_info->pingrate_given),
1441 &(local_args_info.pingrate_given), optarg,
1442 0, "1", ARG_INT, check_ambiguity, override,
1443 0, 0, "pingrate", '-', additional_error))
1444 goto failure;
1445
1446 }
1447 /* Number of ping data bytes. */
1448 else if (strcmp
1449 (long_options[option_index].name,
1450 "pingsize") == 0) {
1451
1452 if (update_arg
1453 ((void *)&(args_info->pingsize_arg),
1454 &(args_info->pingsize_orig),
1455 &(args_info->pingsize_given),
1456 &(local_args_info.pingsize_given), optarg,
1457 0, "56", ARG_INT, check_ambiguity,
1458 override, 0, 0, "pingsize", '-',
1459 additional_error))
1460 goto failure;
1461
1462 }
1463 /* Number of ping req to send. */
1464 else if (strcmp
1465 (long_options[option_index].name,
1466 "pingcount") == 0) {
1467
1468 if (update_arg
1469 ((void *)&(args_info->pingcount_arg),
1470 &(args_info->pingcount_orig),
1471 &(args_info->pingcount_given),
1472 &(local_args_info.pingcount_given), optarg,
1473 0, "0", ARG_INT, check_ambiguity, override,
1474 0, 0, "pingcount", '-', additional_error))
1475 goto failure;
1476
1477 }
1478 /* Do not print ping packet info. */
1479 else if (strcmp
1480 (long_options[option_index].name,
1481 "pingquiet") == 0) {
1482
1483 if (update_arg
1484 ((void *)&(args_info->pingquiet_flag), 0,
1485 &(args_info->pingquiet_given),
1486 &(local_args_info.pingquiet_given), optarg,
1487 0, 0, ARG_FLAG, check_ambiguity, override,
1488 1, 0, "pingquiet", '-', additional_error))
1489 goto failure;
1490
Harald Weltebed35df2011-11-02 13:06:18 +01001491 }
Harald Weltefbb9c7f2017-09-24 11:50:20 +08001492 /* Don't transmit G-PDU sequence nums. */
1493 else if (strcmp
1494 (long_options[option_index].name,
1495 "no-tx-gpdu-seq") == 0) {
1496
1497 if (update_arg
1498 ((void *)&(args_info->no_tx_gpdu_seq_flag),
1499 0, &(args_info->no_tx_gpdu_seq_given),
1500 &(local_args_info.no_tx_gpdu_seq_given),
1501 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1502 override, 1, 0, "no-tx-gpdu-seq", '-',
1503 additional_error))
1504 goto failure;
1505
1506 }
jjako52c24142002-12-16 13:33:51 +00001507
Harald Weltebed35df2011-11-02 13:06:18 +01001508 break;
1509 case '?': /* Invalid option. */
1510 /* `getopt_long' already printed an error message. */
1511 goto failure;
jjako52c24142002-12-16 13:33:51 +00001512
Harald Weltebed35df2011-11-02 13:06:18 +01001513 default: /* bug: option not considered. */
1514 fprintf(stderr, "%s: option unknown: %c%s\n",
1515 CMDLINE_PARSER_PACKAGE, c,
1516 (additional_error ? additional_error : ""));
1517 abort();
1518 } /* switch */
1519 } /* while */
jjako52c24142002-12-16 13:33:51 +00001520
Harald Welte73abc382017-10-10 08:50:11 +08001521 if (check_required) {
1522 error_occurred +=
1523 cmdline_parser_required2(args_info, argv[0],
1524 additional_error);
1525 }
1526
Harald Weltebed35df2011-11-02 13:06:18 +01001527 cmdline_parser_release(&local_args_info);
jjako52c24142002-12-16 13:33:51 +00001528
Harald Weltef6214982017-09-24 10:23:24 +08001529 if (error_occurred)
Harald Weltebed35df2011-11-02 13:06:18 +01001530 return (EXIT_FAILURE);
Harald Welte1b3e5772010-05-04 11:13:56 +02001531
Harald Weltebed35df2011-11-02 13:06:18 +01001532 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001533
1534failure:
Harald Weltebed35df2011-11-02 13:06:18 +01001535
1536 cmdline_parser_release(&local_args_info);
1537 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +00001538}
1539
Harald Welte1b3e5772010-05-04 11:13:56 +02001540#ifndef CONFIG_FILE_LINE_SIZE
1541#define CONFIG_FILE_LINE_SIZE 2048
1542#endif
1543#define ADDITIONAL_ERROR " in configuration file "
1544
1545#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1546/* 3 is for "--" and "=" */
1547
Harald Weltef6214982017-09-24 10:23:24 +08001548static int _cmdline_parser_configfile(const char *filename, int *my_argc)
jjako52c24142002-12-16 13:33:51 +00001549{
Harald Weltebed35df2011-11-02 13:06:18 +01001550 FILE *file;
Harald Weltef6214982017-09-24 10:23:24 +08001551 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
Harald Weltebed35df2011-11-02 13:06:18 +01001552 char linebuf[CONFIG_FILE_LINE_SIZE];
1553 int line_num = 0;
Harald Weltef6214982017-09-24 10:23:24 +08001554 int result = 0, equal;
Harald Weltebed35df2011-11-02 13:06:18 +01001555 char *fopt, *farg;
1556 char *str_index;
1557 size_t len, next_token;
1558 char delimiter;
Harald Welte1b3e5772010-05-04 11:13:56 +02001559
Harald Weltef6214982017-09-24 10:23:24 +08001560 if ((file = fopen(filename, "r")) == 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001561 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1562 CMDLINE_PARSER_PACKAGE, filename);
Harald Weltef6214982017-09-24 10:23:24 +08001563 return EXIT_FAILURE;
Harald Weltebed35df2011-11-02 13:06:18 +01001564 }
jjako52c24142002-12-16 13:33:51 +00001565
Harald Weltef6214982017-09-24 10:23:24 +08001566 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001567 ++line_num;
1568 my_argv[0] = '\0';
1569 len = strlen(linebuf);
1570 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) {
1571 fprintf(stderr,
1572 "%s:%s:%d: Line too long in configuration file\n",
1573 CMDLINE_PARSER_PACKAGE, filename, line_num);
1574 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001575 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001576 }
jjako52c24142002-12-16 13:33:51 +00001577
Harald Weltebed35df2011-11-02 13:06:18 +01001578 /* find first non-whitespace character in the line */
1579 next_token = strspn(linebuf, " \t\r\n");
1580 str_index = linebuf + next_token;
jjako52c24142002-12-16 13:33:51 +00001581
Harald Weltebed35df2011-11-02 13:06:18 +01001582 if (str_index[0] == '\0' || str_index[0] == '#')
1583 continue; /* empty line or comment line is skipped */
Harald Welte1b3e5772010-05-04 11:13:56 +02001584
Harald Weltebed35df2011-11-02 13:06:18 +01001585 fopt = str_index;
Harald Welte1b3e5772010-05-04 11:13:56 +02001586
Harald Weltebed35df2011-11-02 13:06:18 +01001587 /* truncate fopt at the end of the first non-valid character */
1588 next_token = strcspn(fopt, " \t\r\n=");
Harald Welte1b3e5772010-05-04 11:13:56 +02001589
Harald Weltebed35df2011-11-02 13:06:18 +01001590 if (fopt[next_token] == '\0') { /* the line is over */
Harald Weltef6214982017-09-24 10:23:24 +08001591 farg = 0;
Harald Weltebed35df2011-11-02 13:06:18 +01001592 equal = 0;
1593 goto noarg;
1594 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001595
Harald Weltebed35df2011-11-02 13:06:18 +01001596 /* remember if equal sign is present */
1597 equal = (fopt[next_token] == '=');
1598 fopt[next_token++] = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001599
Harald Weltebed35df2011-11-02 13:06:18 +01001600 /* advance pointers to the next token after the end of fopt */
1601 next_token += strspn(fopt + next_token, " \t\r\n");
Harald Weltef6214982017-09-24 10:23:24 +08001602
Harald Weltebed35df2011-11-02 13:06:18 +01001603 /* check for the presence of equal sign, and if so, skip it */
1604 if (!equal)
1605 if ((equal = (fopt[next_token] == '='))) {
1606 next_token++;
1607 next_token +=
1608 strspn(fopt + next_token, " \t\r\n");
1609 }
1610 str_index += next_token;
Harald Welte1b3e5772010-05-04 11:13:56 +02001611
Harald Weltebed35df2011-11-02 13:06:18 +01001612 /* find argument */
1613 farg = str_index;
1614 if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */
1615 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1616 if (!str_index) {
1617 fprintf
1618 (stderr,
1619 "%s:%s:%d: unterminated string in configuration file\n",
1620 CMDLINE_PARSER_PACKAGE, filename,
1621 line_num);
1622 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001623 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001624 }
1625 } else { /* read up the remaining part up to a delimiter */
1626 next_token = strcspn(farg, " \t\r\n#\'\"");
1627 str_index += next_token;
1628 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001629
Harald Weltebed35df2011-11-02 13:06:18 +01001630 /* truncate farg at the delimiter and store it for further check */
1631 delimiter = *str_index, *str_index++ = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001632
Harald Weltebed35df2011-11-02 13:06:18 +01001633 /* everything but comment is illegal at the end of line */
1634 if (delimiter != '\0' && delimiter != '#') {
1635 str_index += strspn(str_index, " \t\r\n");
1636 if (*str_index != '\0' && *str_index != '#') {
1637 fprintf
1638 (stderr,
1639 "%s:%s:%d: malformed string in configuration file\n",
1640 CMDLINE_PARSER_PACKAGE, filename,
1641 line_num);
1642 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001643 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001644 }
1645 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001646
Harald Weltebed35df2011-11-02 13:06:18 +01001647noarg:
Harald Weltef6214982017-09-24 10:23:24 +08001648 if (!strcmp(fopt, "include")) {
1649 if (farg && *farg) {
1650 result =
1651 _cmdline_parser_configfile(farg, my_argc);
1652 } else {
1653 fprintf(stderr,
1654 "%s:%s:%d: include requires a filename argument.\n",
1655 CMDLINE_PARSER_PACKAGE, filename,
1656 line_num);
1657 }
1658 continue;
1659 }
Harald Weltebed35df2011-11-02 13:06:18 +01001660 len = strlen(fopt);
Harald Weltebed35df2011-11-02 13:06:18 +01001661 strcat(my_argv, len > 1 ? "--" : "-");
1662 strcat(my_argv, fopt);
1663 if (len > 1 && ((farg && *farg) || equal))
1664 strcat(my_argv, "=");
1665 if (farg && *farg)
1666 strcat(my_argv, farg);
Harald Weltef6214982017-09-24 10:23:24 +08001667 ++(*my_argc);
Harald Welte1b3e5772010-05-04 11:13:56 +02001668
Harald Weltebed35df2011-11-02 13:06:18 +01001669 cmd_line_list_tmp =
1670 (struct line_list *)malloc(sizeof(struct line_list));
1671 cmd_line_list_tmp->next = cmd_line_list;
1672 cmd_line_list = cmd_line_list_tmp;
1673 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1674 } /* while */
jjako52c24142002-12-16 13:33:51 +00001675
Harald Weltebed35df2011-11-02 13:06:18 +01001676 if (file)
1677 fclose(file);
Harald Weltef6214982017-09-24 10:23:24 +08001678 return result;
1679}
1680
1681int
1682cmdline_parser_configfile(const char *filename,
1683 struct gengetopt_args_info *args_info,
1684 int override, int initialize, int check_required)
1685{
1686 struct cmdline_parser_params params;
1687
1688 params.override = override;
1689 params.initialize = initialize;
1690 params.check_required = check_required;
1691 params.check_ambiguity = 0;
1692 params.print_errors = 1;
1693
1694 return cmdline_parser_config_file(filename, args_info, &params);
1695}
1696
1697int
1698cmdline_parser_config_file(const char *filename,
1699 struct gengetopt_args_info *args_info,
1700 struct cmdline_parser_params *params)
1701{
1702 int i, result;
1703 int my_argc = 1;
1704 char **my_argv_arg;
1705 char *additional_error;
1706
1707 /* store the program name */
1708 cmd_line_list_tmp =
1709 (struct line_list *)malloc(sizeof(struct line_list));
1710 cmd_line_list_tmp->next = cmd_line_list;
1711 cmd_line_list = cmd_line_list_tmp;
1712 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1713
1714 result = _cmdline_parser_configfile(filename, &my_argc);
1715
1716 if (result != EXIT_FAILURE) {
1717 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1718 cmd_line_list_tmp = cmd_line_list;
1719
1720 for (i = my_argc - 1; i >= 0; --i) {
1721 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1722 cmd_line_list_tmp = cmd_line_list_tmp->next;
1723 }
1724
1725 my_argv_arg[my_argc] = 0;
1726
1727 additional_error =
1728 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) +
1729 1);
1730 strcpy(additional_error, ADDITIONAL_ERROR);
1731 strcat(additional_error, filename);
1732 result =
1733 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1734 params, additional_error);
1735
1736 free(additional_error);
1737 free(my_argv_arg);
1738 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001739
Harald Weltebed35df2011-11-02 13:06:18 +01001740 free_cmd_list();
1741 if (result == EXIT_FAILURE) {
1742 cmdline_parser_free(args_info);
1743 exit(EXIT_FAILURE);
1744 }
1745
1746 return result;
jjako52c24142002-12-16 13:33:51 +00001747}