blob: 60c0a25bfe77a944be22c3910f491af3de353714 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
Harald Weltef6214982017-09-24 10:23:24 +08002 File autogenerated by gengetopt version 2.22.6
jjako52c24142002-12-16 13:33:51 +00003 generated with the following command:
jjako1a51df72004-07-20 08:30:21 +00004 gengetopt --conf-parser
jjako52c24142002-12-16 13:33:51 +00005
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9*/
10
jjako52c24142002-12-16 13:33:51 +000011/* If we use autoconf. */
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
jjako52c24142002-12-16 13:33:51 +000015
Harald Welte1b3e5772010-05-04 11:13:56 +020016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
Harald Weltef6214982017-09-24 10:23:24 +080020#ifndef FIX_UNUSED
21#define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22#endif
23
24#include <getopt.h>
jjako52c24142002-12-16 13:33:51 +000025
26#include "cmdline.h"
27
Harald Welte1b3e5772010-05-04 11:13:56 +020028const char *gengetopt_args_info_purpose = "";
jjako52c24142002-12-16 13:33:51 +000029
Harald Welte7e1175f2017-09-24 10:53:53 +080030const char *gengetopt_args_info_usage = "Usage: sgsnemu [OPTIONS]...";
jjako52c24142002-12-16 13:33:51 +000031
Harald Weltef6214982017-09-24 10:23:24 +080032const char *gengetopt_args_info_versiontext = "";
33
34const char *gengetopt_args_info_description = "";
35
Harald Welte1b3e5772010-05-04 11:13:56 +020036const char *gengetopt_args_info_help[] = {
Harald Weltef6214982017-09-24 10:23:24 +080037 " -h, --help Print help and exit",
38 " -V, --version Print version and exit",
39 " -d, --debug Run in debug mode (default=off)",
40 " -c, --conf=STRING Read configuration file",
41 " --pidfile=STRING Filename of process id file\n (default=`./sgsnemu.pid')",
42 " --statedir=STRING Directory of nonvolatile data (default=`./')",
43 " --dns=STRING DNS Server to use",
44 " -l, --listen=STRING Local interface",
45 " -r, --remote=STRING Remote host",
46 " --contexts=INT Number of contexts (default=`1')",
47 " --timelimit=INT Exit after timelimit seconds (default=`0')",
48 " --gtpversion=INT GTP version to use (default=`1')",
49 " -a, --apn=STRING Access point name (default=`internet')",
50 " --selmode=INT Selection mode (default=`0x01')",
51 " --rattype=1..5 Radio Access Technology Type (default=`1')",
52 " --userloc=type.MCC.MNC.LAC.CIorSACorRAC\n User Location Information\n (default=`02509946241207')",
53 " --rai=MCC.MNC.LAC.RAC Routing Area Information\n (default=`02509946241207')",
54 " --mstz=sign.NbQuartersOfAnHour.DSTAdjustment\n MS Time Zone (default=`0')",
55 " --imeisv=STRING IMEI(SV) International Mobile Equipment\n Identity (and Software Version)\n (default=`2143658709214365')",
56 " --norecovery Do not send recovery (default=off)",
57 " -i, --imsi=STRING IMSI (default=`240010123456789')",
58 " --nsapi=INT NSAPI (default=`0')",
59 " -m, --msisdn=STRING Mobile Station ISDN number\n (default=`46702123456')",
60 " -q, --qos=INT Requested quality of service\n (default=`0x000b921f')",
61 " --qose1=INT Requested quality of service Extension 1\n (default=`0x9396404074f9ffff')",
62 " --qose2=INT Requested quality of service Extension 2\n (default=`0x11')",
63 " --qose3=INT Requested quality of service Extension 3\n (default=`0x0101')",
64 " --qose4=INT Requested quality of service Extension 4\n (default=`0x4040')",
65 " --charging=INT Charging characteristics (default=`0x0800')",
66 " -u, --uid=STRING Login user ID (default=`mig')",
67 " -p, --pwd=STRING Login password (default=`hemmelig')",
68 " --createif Create local network interface (default=off)",
69 " -n, --net=STRING Network address for local interface",
70 " --defaultroute Create default route (default=off)",
71 " --ipup=STRING Script to run after link-up",
72 " --ipdown=STRING Script to run after link-down",
73 " --pinghost=STRING Ping remote host",
74 " --pingrate=INT Number of ping req per second (default=`1')",
75 " --pingsize=INT Number of ping data bytes (default=`56')",
76 " --pingcount=INT Number of ping req to send (default=`0')",
77 " --pingquiet Do not print ping packet info (default=off)",
Harald Weltefbb9c7f2017-09-24 11:50:20 +080078 " --no-tx-gpdu-seq Don't transmit G-PDU sequence nums\n (default=off)",
Harald Welte840a8e92017-09-24 18:12:40 +080079 " -t, --pdp-type=(v4|v6) PDP Type (default=`v4')",
Harald Weltebed35df2011-11-02 13:06:18 +010080 0
Harald Welte1b3e5772010-05-04 11:13:56 +020081};
82
Harald Weltef6214982017-09-24 10:23:24 +080083typedef enum { ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT
84} cmdline_parser_arg_type;
85
Harald Welte1b3e5772010-05-04 11:13:56 +020086static
Harald Weltebed35df2011-11-02 13:06:18 +010087void clear_given(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020088static
Harald Weltebed35df2011-11-02 13:06:18 +010089void clear_args(struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020090
91static int
Harald Weltef6214982017-09-24 10:23:24 +080092cmdline_parser_internal(int argc, char **argv,
93 struct gengetopt_args_info *args_info,
94 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +010095 const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020096
Harald Weltebed35df2011-11-02 13:06:18 +010097struct line_list {
98 char *string_arg;
99 struct line_list *next;
Harald Welte1b3e5772010-05-04 11:13:56 +0200100};
101
102static struct line_list *cmd_line_list = 0;
103static struct line_list *cmd_line_list_tmp = 0;
104
Harald Weltebed35df2011-11-02 13:06:18 +0100105static void free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200106{
Harald Weltebed35df2011-11-02 13:06:18 +0100107 /* free the list of a previous call */
108 if (cmd_line_list) {
109 while (cmd_line_list) {
110 cmd_line_list_tmp = cmd_line_list;
111 cmd_line_list = cmd_line_list->next;
112 free(cmd_line_list_tmp->string_arg);
113 free(cmd_line_list_tmp);
114 }
115 }
jjako52c24142002-12-16 13:33:51 +0000116}
117
Harald Weltebed35df2011-11-02 13:06:18 +0100118static char *gengetopt_strdup(const char *s);
jjako52c24142002-12-16 13:33:51 +0000119
Harald Welte1b3e5772010-05-04 11:13:56 +0200120static
Harald Weltebed35df2011-11-02 13:06:18 +0100121void clear_given(struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000122{
Harald Weltebed35df2011-11-02 13:06:18 +0100123 args_info->help_given = 0;
124 args_info->version_given = 0;
125 args_info->debug_given = 0;
126 args_info->conf_given = 0;
127 args_info->pidfile_given = 0;
128 args_info->statedir_given = 0;
129 args_info->dns_given = 0;
130 args_info->listen_given = 0;
131 args_info->remote_given = 0;
132 args_info->contexts_given = 0;
133 args_info->timelimit_given = 0;
134 args_info->gtpversion_given = 0;
135 args_info->apn_given = 0;
136 args_info->selmode_given = 0;
137 args_info->rattype_given = 0;
138 args_info->userloc_given = 0;
139 args_info->rai_given = 0;
140 args_info->mstz_given = 0;
141 args_info->imeisv_given = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800142 args_info->norecovery_given = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100143 args_info->imsi_given = 0;
144 args_info->nsapi_given = 0;
145 args_info->msisdn_given = 0;
146 args_info->qos_given = 0;
147 args_info->qose1_given = 0;
148 args_info->qose2_given = 0;
149 args_info->qose3_given = 0;
150 args_info->qose4_given = 0;
151 args_info->charging_given = 0;
152 args_info->uid_given = 0;
153 args_info->pwd_given = 0;
154 args_info->createif_given = 0;
155 args_info->net_given = 0;
156 args_info->defaultroute_given = 0;
157 args_info->ipup_given = 0;
158 args_info->ipdown_given = 0;
159 args_info->pinghost_given = 0;
160 args_info->pingrate_given = 0;
161 args_info->pingsize_given = 0;
162 args_info->pingcount_given = 0;
163 args_info->pingquiet_given = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800164 args_info->no_tx_gpdu_seq_given = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800165 args_info->pdp_type_given = 0;
jjako52c24142002-12-16 13:33:51 +0000166}
167
Harald Welte1b3e5772010-05-04 11:13:56 +0200168static
Harald Weltebed35df2011-11-02 13:06:18 +0100169void clear_args(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200170{
Harald Weltef6214982017-09-24 10:23:24 +0800171 FIX_UNUSED(args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100172 args_info->debug_flag = 0;
173 args_info->conf_arg = NULL;
174 args_info->conf_orig = NULL;
175 args_info->pidfile_arg = gengetopt_strdup("./sgsnemu.pid");
176 args_info->pidfile_orig = NULL;
177 args_info->statedir_arg = gengetopt_strdup("./");
178 args_info->statedir_orig = NULL;
179 args_info->dns_arg = NULL;
180 args_info->dns_orig = NULL;
181 args_info->listen_arg = NULL;
182 args_info->listen_orig = NULL;
183 args_info->remote_arg = NULL;
184 args_info->remote_orig = NULL;
185 args_info->contexts_arg = 1;
186 args_info->contexts_orig = NULL;
187 args_info->timelimit_arg = 0;
188 args_info->timelimit_orig = NULL;
189 args_info->gtpversion_arg = 1;
190 args_info->gtpversion_orig = NULL;
191 args_info->apn_arg = gengetopt_strdup("internet");
192 args_info->apn_orig = NULL;
193 args_info->selmode_arg = 0x01;
194 args_info->selmode_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800195 args_info->rattype_arg = 1;
Harald Weltebed35df2011-11-02 13:06:18 +0100196 args_info->rattype_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800197 args_info->userloc_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100198 args_info->userloc_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800199 args_info->rai_arg = gengetopt_strdup("02509946241207");
Harald Weltebed35df2011-11-02 13:06:18 +0100200 args_info->rai_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800201 args_info->mstz_arg = gengetopt_strdup("0");
Harald Weltebed35df2011-11-02 13:06:18 +0100202 args_info->mstz_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800203 args_info->imeisv_arg = gengetopt_strdup("2143658709214365");
Harald Weltebed35df2011-11-02 13:06:18 +0100204 args_info->imeisv_orig = NULL;
Harald Weltef6214982017-09-24 10:23:24 +0800205 args_info->norecovery_flag = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100206 args_info->imsi_arg = gengetopt_strdup("240010123456789");
207 args_info->imsi_orig = NULL;
208 args_info->nsapi_arg = 0;
209 args_info->nsapi_orig = NULL;
210 args_info->msisdn_arg = gengetopt_strdup("46702123456");
211 args_info->msisdn_orig = NULL;
212 args_info->qos_arg = 0x000b921f;
213 args_info->qos_orig = NULL;
214 args_info->qose1_arg = 0x9396404074f9ffff;
215 args_info->qose1_orig = NULL;
216 args_info->qose2_arg = 0x11;
217 args_info->qose2_orig = NULL;
218 args_info->qose3_arg = 0x0101;
219 args_info->qose3_orig = NULL;
220 args_info->qose4_arg = 0x4040;
221 args_info->qose4_orig = NULL;
222 args_info->charging_arg = 0x0800;
223 args_info->charging_orig = NULL;
224 args_info->uid_arg = gengetopt_strdup("mig");
225 args_info->uid_orig = NULL;
226 args_info->pwd_arg = gengetopt_strdup("hemmelig");
227 args_info->pwd_orig = NULL;
228 args_info->createif_flag = 0;
229 args_info->net_arg = NULL;
230 args_info->net_orig = NULL;
231 args_info->defaultroute_flag = 0;
232 args_info->ipup_arg = NULL;
233 args_info->ipup_orig = NULL;
234 args_info->ipdown_arg = NULL;
235 args_info->ipdown_orig = NULL;
236 args_info->pinghost_arg = NULL;
237 args_info->pinghost_orig = NULL;
238 args_info->pingrate_arg = 1;
239 args_info->pingrate_orig = NULL;
240 args_info->pingsize_arg = 56;
241 args_info->pingsize_orig = NULL;
242 args_info->pingcount_arg = 0;
243 args_info->pingcount_orig = NULL;
244 args_info->pingquiet_flag = 0;
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800245 args_info->no_tx_gpdu_seq_flag = 0;
Harald Welte840a8e92017-09-24 18:12:40 +0800246 args_info->pdp_type_arg = gengetopt_strdup("v4");
247 args_info->pdp_type_orig = NULL;
Harald Weltebed35df2011-11-02 13:06:18 +0100248
Harald Welte1b3e5772010-05-04 11:13:56 +0200249}
250
251static
252void init_args_info(struct gengetopt_args_info *args_info)
253{
Harald Weltef6214982017-09-24 10:23:24 +0800254
Harald Weltebed35df2011-11-02 13:06:18 +0100255 args_info->help_help = gengetopt_args_info_help[0];
256 args_info->version_help = gengetopt_args_info_help[1];
257 args_info->debug_help = gengetopt_args_info_help[2];
258 args_info->conf_help = gengetopt_args_info_help[3];
259 args_info->pidfile_help = gengetopt_args_info_help[4];
260 args_info->statedir_help = gengetopt_args_info_help[5];
261 args_info->dns_help = gengetopt_args_info_help[6];
262 args_info->listen_help = gengetopt_args_info_help[7];
263 args_info->remote_help = gengetopt_args_info_help[8];
264 args_info->contexts_help = gengetopt_args_info_help[9];
265 args_info->timelimit_help = gengetopt_args_info_help[10];
266 args_info->gtpversion_help = gengetopt_args_info_help[11];
267 args_info->apn_help = gengetopt_args_info_help[12];
268 args_info->selmode_help = gengetopt_args_info_help[13];
Harald Weltef6214982017-09-24 10:23:24 +0800269 args_info->rattype_help = gengetopt_args_info_help[14];
270 args_info->userloc_help = gengetopt_args_info_help[15];
271 args_info->rai_help = gengetopt_args_info_help[16];
272 args_info->mstz_help = gengetopt_args_info_help[17];
273 args_info->imeisv_help = gengetopt_args_info_help[18];
274 args_info->norecovery_help = gengetopt_args_info_help[19];
275 args_info->imsi_help = gengetopt_args_info_help[20];
276 args_info->nsapi_help = gengetopt_args_info_help[21];
277 args_info->msisdn_help = gengetopt_args_info_help[22];
278 args_info->qos_help = gengetopt_args_info_help[23];
279 args_info->qose1_help = gengetopt_args_info_help[24];
280 args_info->qose2_help = gengetopt_args_info_help[25];
281 args_info->qose3_help = gengetopt_args_info_help[26];
282 args_info->qose4_help = gengetopt_args_info_help[27];
283 args_info->charging_help = gengetopt_args_info_help[28];
284 args_info->uid_help = gengetopt_args_info_help[29];
285 args_info->pwd_help = gengetopt_args_info_help[30];
286 args_info->createif_help = gengetopt_args_info_help[31];
287 args_info->net_help = gengetopt_args_info_help[32];
288 args_info->defaultroute_help = gengetopt_args_info_help[33];
289 args_info->ipup_help = gengetopt_args_info_help[34];
290 args_info->ipdown_help = gengetopt_args_info_help[35];
291 args_info->pinghost_help = gengetopt_args_info_help[36];
292 args_info->pingrate_help = gengetopt_args_info_help[37];
293 args_info->pingsize_help = gengetopt_args_info_help[38];
294 args_info->pingcount_help = gengetopt_args_info_help[39];
295 args_info->pingquiet_help = gengetopt_args_info_help[40];
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800296 args_info->no_tx_gpdu_seq_help = gengetopt_args_info_help[41];
Harald Welte840a8e92017-09-24 18:12:40 +0800297 args_info->pdp_type_help = gengetopt_args_info_help[42];
Harald Weltebed35df2011-11-02 13:06:18 +0100298
Harald Welte1b3e5772010-05-04 11:13:56 +0200299}
300
Harald Weltebed35df2011-11-02 13:06:18 +0100301void cmdline_parser_print_version(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200302{
Harald Weltef6214982017-09-24 10:23:24 +0800303 printf("%s %s\n",
304 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ?
305 CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
306 CMDLINE_PARSER_VERSION);
307
308 if (strlen(gengetopt_args_info_versiontext) > 0)
309 printf("\n%s\n", gengetopt_args_info_versiontext);
Harald Welte1b3e5772010-05-04 11:13:56 +0200310}
311
Harald Weltef6214982017-09-24 10:23:24 +0800312static void print_help_common(void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200313{
Harald Weltebed35df2011-11-02 13:06:18 +0100314 cmdline_parser_print_version();
Harald Welte1b3e5772010-05-04 11:13:56 +0200315
Harald Weltebed35df2011-11-02 13:06:18 +0100316 if (strlen(gengetopt_args_info_purpose) > 0)
317 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200318
Harald Weltef6214982017-09-24 10:23:24 +0800319 if (strlen(gengetopt_args_info_usage) > 0)
320 printf("\n%s\n", gengetopt_args_info_usage);
321
322 printf("\n");
323
324 if (strlen(gengetopt_args_info_description) > 0)
325 printf("%s\n\n", gengetopt_args_info_description);
326}
327
328void cmdline_parser_print_help(void)
329{
330 int i = 0;
331 print_help_common();
Harald Weltebed35df2011-11-02 13:06:18 +0100332 while (gengetopt_args_info_help[i])
333 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200334}
335
Harald Weltebed35df2011-11-02 13:06:18 +0100336void cmdline_parser_init(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200337{
Harald Weltebed35df2011-11-02 13:06:18 +0100338 clear_given(args_info);
339 clear_args(args_info);
340 init_args_info(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200341}
342
Harald Weltef6214982017-09-24 10:23:24 +0800343void cmdline_parser_params_init(struct cmdline_parser_params *params)
344{
345 if (params) {
346 params->override = 0;
347 params->initialize = 1;
348 params->check_required = 1;
349 params->check_ambiguity = 0;
350 params->print_errors = 1;
351 }
352}
353
354struct cmdline_parser_params *cmdline_parser_params_create(void)
355{
356 struct cmdline_parser_params *params =
357 (struct cmdline_parser_params *)
358 malloc(sizeof(struct cmdline_parser_params));
359 cmdline_parser_params_init(params);
360 return params;
361}
362
363static void free_string_field(char **s)
364{
365 if (*s) {
366 free(*s);
367 *s = 0;
368 }
369}
370
Harald Weltebed35df2011-11-02 13:06:18 +0100371static void cmdline_parser_release(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200372{
Harald Weltebed35df2011-11-02 13:06:18 +0100373
Harald Weltef6214982017-09-24 10:23:24 +0800374 free_string_field(&(args_info->conf_arg));
375 free_string_field(&(args_info->conf_orig));
376 free_string_field(&(args_info->pidfile_arg));
377 free_string_field(&(args_info->pidfile_orig));
378 free_string_field(&(args_info->statedir_arg));
379 free_string_field(&(args_info->statedir_orig));
380 free_string_field(&(args_info->dns_arg));
381 free_string_field(&(args_info->dns_orig));
382 free_string_field(&(args_info->listen_arg));
383 free_string_field(&(args_info->listen_orig));
384 free_string_field(&(args_info->remote_arg));
385 free_string_field(&(args_info->remote_orig));
386 free_string_field(&(args_info->contexts_orig));
387 free_string_field(&(args_info->timelimit_orig));
388 free_string_field(&(args_info->gtpversion_orig));
389 free_string_field(&(args_info->apn_arg));
390 free_string_field(&(args_info->apn_orig));
391 free_string_field(&(args_info->selmode_orig));
392 free_string_field(&(args_info->rattype_orig));
393 free_string_field(&(args_info->userloc_arg));
394 free_string_field(&(args_info->userloc_orig));
395 free_string_field(&(args_info->rai_arg));
396 free_string_field(&(args_info->rai_orig));
397 free_string_field(&(args_info->mstz_arg));
398 free_string_field(&(args_info->mstz_orig));
399 free_string_field(&(args_info->imeisv_arg));
400 free_string_field(&(args_info->imeisv_orig));
401 free_string_field(&(args_info->imsi_arg));
402 free_string_field(&(args_info->imsi_orig));
403 free_string_field(&(args_info->nsapi_orig));
404 free_string_field(&(args_info->msisdn_arg));
405 free_string_field(&(args_info->msisdn_orig));
406 free_string_field(&(args_info->qos_orig));
407 free_string_field(&(args_info->qose1_orig));
408 free_string_field(&(args_info->qose2_orig));
409 free_string_field(&(args_info->qose3_orig));
410 free_string_field(&(args_info->qose4_orig));
411 free_string_field(&(args_info->charging_orig));
412 free_string_field(&(args_info->uid_arg));
413 free_string_field(&(args_info->uid_orig));
414 free_string_field(&(args_info->pwd_arg));
415 free_string_field(&(args_info->pwd_orig));
416 free_string_field(&(args_info->net_arg));
417 free_string_field(&(args_info->net_orig));
418 free_string_field(&(args_info->ipup_arg));
419 free_string_field(&(args_info->ipup_orig));
420 free_string_field(&(args_info->ipdown_arg));
421 free_string_field(&(args_info->ipdown_orig));
422 free_string_field(&(args_info->pinghost_arg));
423 free_string_field(&(args_info->pinghost_orig));
424 free_string_field(&(args_info->pingrate_orig));
425 free_string_field(&(args_info->pingsize_orig));
426 free_string_field(&(args_info->pingcount_orig));
Harald Welte840a8e92017-09-24 18:12:40 +0800427 free_string_field(&(args_info->pdp_type_arg));
428 free_string_field(&(args_info->pdp_type_orig));
Harald Weltebed35df2011-11-02 13:06:18 +0100429
430 clear_given(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200431}
432
Harald Weltef6214982017-09-24 10:23:24 +0800433static void
434write_into_file(FILE * outfile, const char *opt, const char *arg,
435 const char *values[])
436{
437 FIX_UNUSED(values);
438 if (arg) {
439 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
440 } else {
441 fprintf(outfile, "%s\n", opt);
442 }
443}
444
445int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info)
446{
447 int i = 0;
448
449 if (!outfile) {
450 fprintf(stderr, "%s: cannot dump options to stream\n",
451 CMDLINE_PARSER_PACKAGE);
452 return EXIT_FAILURE;
453 }
454
455 if (args_info->help_given)
456 write_into_file(outfile, "help", 0, 0);
457 if (args_info->version_given)
458 write_into_file(outfile, "version", 0, 0);
459 if (args_info->debug_given)
460 write_into_file(outfile, "debug", 0, 0);
461 if (args_info->conf_given)
462 write_into_file(outfile, "conf", args_info->conf_orig, 0);
463 if (args_info->pidfile_given)
464 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
465 if (args_info->statedir_given)
466 write_into_file(outfile, "statedir", args_info->statedir_orig,
467 0);
468 if (args_info->dns_given)
469 write_into_file(outfile, "dns", args_info->dns_orig, 0);
470 if (args_info->listen_given)
471 write_into_file(outfile, "listen", args_info->listen_orig, 0);
472 if (args_info->remote_given)
473 write_into_file(outfile, "remote", args_info->remote_orig, 0);
474 if (args_info->contexts_given)
475 write_into_file(outfile, "contexts", args_info->contexts_orig,
476 0);
477 if (args_info->timelimit_given)
478 write_into_file(outfile, "timelimit", args_info->timelimit_orig,
479 0);
480 if (args_info->gtpversion_given)
481 write_into_file(outfile, "gtpversion",
482 args_info->gtpversion_orig, 0);
483 if (args_info->apn_given)
484 write_into_file(outfile, "apn", args_info->apn_orig, 0);
485 if (args_info->selmode_given)
486 write_into_file(outfile, "selmode", args_info->selmode_orig, 0);
487 if (args_info->rattype_given)
488 write_into_file(outfile, "rattype", args_info->rattype_orig, 0);
489 if (args_info->userloc_given)
490 write_into_file(outfile, "userloc", args_info->userloc_orig, 0);
491 if (args_info->rai_given)
492 write_into_file(outfile, "rai", args_info->rai_orig, 0);
493 if (args_info->mstz_given)
494 write_into_file(outfile, "mstz", args_info->mstz_orig, 0);
495 if (args_info->imeisv_given)
496 write_into_file(outfile, "imeisv", args_info->imeisv_orig, 0);
497 if (args_info->norecovery_given)
498 write_into_file(outfile, "norecovery", 0, 0);
499 if (args_info->imsi_given)
500 write_into_file(outfile, "imsi", args_info->imsi_orig, 0);
501 if (args_info->nsapi_given)
502 write_into_file(outfile, "nsapi", args_info->nsapi_orig, 0);
503 if (args_info->msisdn_given)
504 write_into_file(outfile, "msisdn", args_info->msisdn_orig, 0);
505 if (args_info->qos_given)
506 write_into_file(outfile, "qos", args_info->qos_orig, 0);
507 if (args_info->qose1_given)
508 write_into_file(outfile, "qose1", args_info->qose1_orig, 0);
509 if (args_info->qose2_given)
510 write_into_file(outfile, "qose2", args_info->qose2_orig, 0);
511 if (args_info->qose3_given)
512 write_into_file(outfile, "qose3", args_info->qose3_orig, 0);
513 if (args_info->qose4_given)
514 write_into_file(outfile, "qose4", args_info->qose4_orig, 0);
515 if (args_info->charging_given)
516 write_into_file(outfile, "charging", args_info->charging_orig,
517 0);
518 if (args_info->uid_given)
519 write_into_file(outfile, "uid", args_info->uid_orig, 0);
520 if (args_info->pwd_given)
521 write_into_file(outfile, "pwd", args_info->pwd_orig, 0);
522 if (args_info->createif_given)
523 write_into_file(outfile, "createif", 0, 0);
524 if (args_info->net_given)
525 write_into_file(outfile, "net", args_info->net_orig, 0);
526 if (args_info->defaultroute_given)
527 write_into_file(outfile, "defaultroute", 0, 0);
528 if (args_info->ipup_given)
529 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
530 if (args_info->ipdown_given)
531 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
532 if (args_info->pinghost_given)
533 write_into_file(outfile, "pinghost", args_info->pinghost_orig,
534 0);
535 if (args_info->pingrate_given)
536 write_into_file(outfile, "pingrate", args_info->pingrate_orig,
537 0);
538 if (args_info->pingsize_given)
539 write_into_file(outfile, "pingsize", args_info->pingsize_orig,
540 0);
541 if (args_info->pingcount_given)
542 write_into_file(outfile, "pingcount", args_info->pingcount_orig,
543 0);
544 if (args_info->pingquiet_given)
545 write_into_file(outfile, "pingquiet", 0, 0);
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800546 if (args_info->no_tx_gpdu_seq_given)
547 write_into_file(outfile, "no-tx-gpdu-seq", 0, 0);
Harald Welte840a8e92017-09-24 18:12:40 +0800548 if (args_info->pdp_type_given)
549 write_into_file(outfile, "pdp-type", args_info->pdp_type_orig,
550 0);
Harald Weltef6214982017-09-24 10:23:24 +0800551
552 i = EXIT_SUCCESS;
553 return i;
554}
555
Harald Welte1b3e5772010-05-04 11:13:56 +0200556int
Harald Weltebed35df2011-11-02 13:06:18 +0100557cmdline_parser_file_save(const char *filename,
558 struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200559{
Harald Weltebed35df2011-11-02 13:06:18 +0100560 FILE *outfile;
561 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200562
Harald Weltebed35df2011-11-02 13:06:18 +0100563 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200564
Harald Weltebed35df2011-11-02 13:06:18 +0100565 if (!outfile) {
566 fprintf(stderr, "%s: cannot open file for writing: %s\n",
567 CMDLINE_PARSER_PACKAGE, filename);
568 return EXIT_FAILURE;
569 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200570
Harald Weltef6214982017-09-24 10:23:24 +0800571 i = cmdline_parser_dump(outfile, args_info);
Harald Weltebed35df2011-11-02 13:06:18 +0100572 fclose(outfile);
573
Harald Weltebed35df2011-11-02 13:06:18 +0100574 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200575}
576
Harald Weltebed35df2011-11-02 13:06:18 +0100577void cmdline_parser_free(struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200578{
Harald Weltebed35df2011-11-02 13:06:18 +0100579 cmdline_parser_release(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200580}
581
Harald Weltef6214982017-09-24 10:23:24 +0800582/** @brief replacement of strdup, which is not standard */
Harald Weltebed35df2011-11-02 13:06:18 +0100583char *gengetopt_strdup(const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200584{
Harald Weltef6214982017-09-24 10:23:24 +0800585 char *result = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100586 if (!s)
587 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200588
Harald Weltebed35df2011-11-02 13:06:18 +0100589 result = (char *)malloc(strlen(s) + 1);
590 if (result == (char *)0)
591 return (char *)0;
592 strcpy(result, s);
593 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200594}
595
Harald Weltef6214982017-09-24 10:23:24 +0800596int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200597{
Harald Weltebed35df2011-11-02 13:06:18 +0100598 return cmdline_parser2(argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200599}
600
601int
Harald Weltef6214982017-09-24 10:23:24 +0800602cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info,
603 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200604{
Harald Weltebed35df2011-11-02 13:06:18 +0100605 int result;
Harald Weltef6214982017-09-24 10:23:24 +0800606 result = cmdline_parser_internal(argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200607
Harald Weltef6214982017-09-24 10:23:24 +0800608 if (result == EXIT_FAILURE) {
609 cmdline_parser_free(args_info);
610 exit(EXIT_FAILURE);
611 }
612
613 return result;
614}
615
616int
617cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info,
618 int override, int initialize, int check_required)
619{
620 int result;
621 struct cmdline_parser_params params;
622
623 params.override = override;
624 params.initialize = initialize;
625 params.check_required = check_required;
626 params.check_ambiguity = 0;
627 params.print_errors = 1;
628
629 result = cmdline_parser_internal(argc, argv, args_info, &params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200630
Harald Weltebed35df2011-11-02 13:06:18 +0100631 if (result == EXIT_FAILURE) {
632 cmdline_parser_free(args_info);
633 exit(EXIT_FAILURE);
634 }
635
636 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200637}
638
639int
Harald Weltebed35df2011-11-02 13:06:18 +0100640cmdline_parser_required(struct gengetopt_args_info *args_info,
641 const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200642{
Harald Weltef6214982017-09-24 10:23:24 +0800643 FIX_UNUSED(args_info);
644 FIX_UNUSED(prog_name);
Harald Weltebed35df2011-11-02 13:06:18 +0100645 return EXIT_SUCCESS;
Harald Welte1b3e5772010-05-04 11:13:56 +0200646}
647
Harald Weltef6214982017-09-24 10:23:24 +0800648static char *package_name = 0;
649
650/**
651 * @brief updates an option
652 * @param field the generic pointer to the field to update
653 * @param orig_field the pointer to the orig field
654 * @param field_given the pointer to the number of occurrence of this option
655 * @param prev_given the pointer to the number of occurrence already seen
656 * @param value the argument for this option (if null no arg was specified)
657 * @param possible_values the possible values for this option (if specified)
658 * @param default_value the default value (in case the option only accepts fixed values)
659 * @param arg_type the type of this option
660 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
661 * @param override @see cmdline_parser_params.override
662 * @param no_free whether to free a possible previous value
663 * @param multiple_option whether this is a multiple option
664 * @param long_opt the corresponding long option
665 * @param short_opt the corresponding short option (or '-' if none)
666 * @param additional_error possible further error specification
667 */
668static
669int update_arg(void *field, char **orig_field,
670 unsigned int *field_given, unsigned int *prev_given,
671 char *value, const char *possible_values[],
672 const char *default_value,
673 cmdline_parser_arg_type arg_type,
674 int check_ambiguity, int override,
675 int no_free, int multiple_option,
676 const char *long_opt, char short_opt,
677 const char *additional_error)
678{
679 char *stop_char = 0;
680 const char *val = value;
681 int found;
682 char **string_field;
683 FIX_UNUSED(field);
684
685 stop_char = 0;
686 found = 0;
687
688 if (!multiple_option && prev_given
689 && (*prev_given || (check_ambiguity && *field_given))) {
690 if (short_opt != '-')
691 fprintf(stderr,
692 "%s: `--%s' (`-%c') option given more than once%s\n",
693 package_name, long_opt, short_opt,
694 (additional_error ? additional_error : ""));
695 else
696 fprintf(stderr,
697 "%s: `--%s' option given more than once%s\n",
698 package_name, long_opt,
699 (additional_error ? additional_error : ""));
700 return 1; /* failure */
701 }
702
703 FIX_UNUSED(default_value);
704
705 if (field_given && *field_given && !override)
706 return 0;
707 if (prev_given)
708 (*prev_given)++;
709 if (field_given)
710 (*field_given)++;
711 if (possible_values)
712 val = possible_values[found];
713
714 switch (arg_type) {
715 case ARG_FLAG:
716 *((int *)field) = !*((int *)field);
717 break;
718 case ARG_INT:
719 if (val)
720 *((int *)field) = strtol(val, &stop_char, 0);
721 break;
722 case ARG_STRING:
723 if (val) {
724 string_field = (char **)field;
725 if (!no_free && *string_field)
726 free(*string_field); /* free previous string */
727 *string_field = gengetopt_strdup(val);
728 }
729 break;
730 default:
731 break;
732 };
733
734 /* check numeric conversion */
735 switch (arg_type) {
736 case ARG_INT:
737 if (val && !(stop_char && *stop_char == '\0')) {
738 fprintf(stderr, "%s: invalid numeric value: %s\n",
739 package_name, val);
740 return 1; /* failure */
741 }
742 break;
743 default:
744 ;
745 };
746
747 /* store the original value */
748 switch (arg_type) {
749 case ARG_NO:
750 case ARG_FLAG:
751 break;
752 default:
753 if (value && orig_field) {
754 if (no_free) {
755 *orig_field = value;
756 } else {
757 if (*orig_field)
758 free(*orig_field); /* free previous string */
759 *orig_field = gengetopt_strdup(value);
760 }
761 }
762 };
763
764 return 0; /* OK */
765}
766
Harald Welte1b3e5772010-05-04 11:13:56 +0200767int
Harald Weltef6214982017-09-24 10:23:24 +0800768cmdline_parser_internal(int argc, char **argv,
769 struct gengetopt_args_info *args_info,
770 struct cmdline_parser_params *params,
Harald Weltebed35df2011-11-02 13:06:18 +0100771 const char *additional_error)
Harald Welte1b3e5772010-05-04 11:13:56 +0200772{
Harald Weltebed35df2011-11-02 13:06:18 +0100773 int c; /* Character of the parsed option. */
Harald Welte1b3e5772010-05-04 11:13:56 +0200774
Harald Weltef6214982017-09-24 10:23:24 +0800775 int error_occurred = 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100776 struct gengetopt_args_info local_args_info;
Harald Welte1b3e5772010-05-04 11:13:56 +0200777
Harald Weltef6214982017-09-24 10:23:24 +0800778 int override;
779 int initialize;
780 int check_required;
781 int check_ambiguity;
782
783 package_name = argv[0];
784
785 override = params->override;
786 initialize = params->initialize;
787 check_required = params->check_required;
788 check_ambiguity = params->check_ambiguity;
789
Harald Weltebed35df2011-11-02 13:06:18 +0100790 if (initialize)
791 cmdline_parser_init(args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200792
Harald Weltebed35df2011-11-02 13:06:18 +0100793 cmdline_parser_init(&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000794
Harald Weltebed35df2011-11-02 13:06:18 +0100795 optarg = 0;
796 optind = 0;
Harald Weltef6214982017-09-24 10:23:24 +0800797 opterr = params->print_errors;
Harald Weltebed35df2011-11-02 13:06:18 +0100798 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000799
Harald Weltebed35df2011-11-02 13:06:18 +0100800 while (1) {
801 int option_index = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200802
Harald Weltebed35df2011-11-02 13:06:18 +0100803 static struct option long_options[] = {
804 {"help", 0, NULL, 'h'},
805 {"version", 0, NULL, 'V'},
806 {"debug", 0, NULL, 'd'},
807 {"conf", 1, NULL, 'c'},
808 {"pidfile", 1, NULL, 0},
809 {"statedir", 1, NULL, 0},
810 {"dns", 1, NULL, 0},
811 {"listen", 1, NULL, 'l'},
812 {"remote", 1, NULL, 'r'},
813 {"contexts", 1, NULL, 0},
814 {"timelimit", 1, NULL, 0},
815 {"gtpversion", 1, NULL, 0},
816 {"apn", 1, NULL, 'a'},
817 {"selmode", 1, NULL, 0},
818 {"rattype", 1, NULL, 0},
819 {"userloc", 1, NULL, 0},
820 {"rai", 1, NULL, 0},
821 {"mstz", 1, NULL, 0},
822 {"imeisv", 1, NULL, 0},
Harald Weltef6214982017-09-24 10:23:24 +0800823 {"norecovery", 0, NULL, 0},
Harald Weltebed35df2011-11-02 13:06:18 +0100824 {"imsi", 1, NULL, 'i'},
825 {"nsapi", 1, NULL, 0},
826 {"msisdn", 1, NULL, 'm'},
827 {"qos", 1, NULL, 'q'},
828 {"qose1", 1, NULL, 0},
829 {"qose2", 1, NULL, 0},
830 {"qose3", 1, NULL, 0},
831 {"qose4", 1, NULL, 0},
832 {"charging", 1, NULL, 0},
833 {"uid", 1, NULL, 'u'},
834 {"pwd", 1, NULL, 'p'},
835 {"createif", 0, NULL, 0},
836 {"net", 1, NULL, 'n'},
837 {"defaultroute", 0, NULL, 0},
838 {"ipup", 1, NULL, 0},
839 {"ipdown", 1, NULL, 0},
840 {"pinghost", 1, NULL, 0},
841 {"pingrate", 1, NULL, 0},
842 {"pingsize", 1, NULL, 0},
843 {"pingcount", 1, NULL, 0},
844 {"pingquiet", 0, NULL, 0},
Harald Weltefbb9c7f2017-09-24 11:50:20 +0800845 {"no-tx-gpdu-seq", 0, NULL, 0},
Harald Welte840a8e92017-09-24 18:12:40 +0800846 {"pdp-type", 1, NULL, 't'},
Harald Weltef6214982017-09-24 10:23:24 +0800847 {0, 0, 0, 0}
Harald Weltebed35df2011-11-02 13:06:18 +0100848 };
jjako52c24142002-12-16 13:33:51 +0000849
Harald Welte840a8e92017-09-24 18:12:40 +0800850 c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:t:",
Harald Weltebed35df2011-11-02 13:06:18 +0100851 long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000852
Harald Weltebed35df2011-11-02 13:06:18 +0100853 if (c == -1)
854 break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000855
Harald Weltebed35df2011-11-02 13:06:18 +0100856 switch (c) {
857 case 'h': /* Print help and exit. */
858 cmdline_parser_print_help();
859 cmdline_parser_free(&local_args_info);
860 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000861
Harald Weltebed35df2011-11-02 13:06:18 +0100862 case 'V': /* Print version and exit. */
863 cmdline_parser_print_version();
864 cmdline_parser_free(&local_args_info);
865 exit(EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000866
Harald Weltebed35df2011-11-02 13:06:18 +0100867 case 'd': /* Run in debug mode. */
jjako52c24142002-12-16 13:33:51 +0000868
Harald Weltef6214982017-09-24 10:23:24 +0800869 if (update_arg
870 ((void *)&(args_info->debug_flag), 0,
871 &(args_info->debug_given),
872 &(local_args_info.debug_given), optarg, 0, 0,
873 ARG_FLAG, check_ambiguity, override, 1, 0, "debug",
874 'd', additional_error))
875 goto failure;
876
877 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100878 case 'c': /* Read configuration file. */
jjako52c24142002-12-16 13:33:51 +0000879
Harald Weltef6214982017-09-24 10:23:24 +0800880 if (update_arg((void *)&(args_info->conf_arg),
881 &(args_info->conf_orig),
882 &(args_info->conf_given),
883 &(local_args_info.conf_given), optarg, 0,
884 0, ARG_STRING, check_ambiguity, override,
885 0, 0, "conf", 'c', additional_error))
886 goto failure;
887
888 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100889 case 'l': /* Local interface. */
jjako52c24142002-12-16 13:33:51 +0000890
Harald Weltef6214982017-09-24 10:23:24 +0800891 if (update_arg((void *)&(args_info->listen_arg),
892 &(args_info->listen_orig),
893 &(args_info->listen_given),
894 &(local_args_info.listen_given), optarg,
895 0, 0, ARG_STRING, check_ambiguity,
896 override, 0, 0, "listen", 'l',
897 additional_error))
898 goto failure;
899
900 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100901 case 'r': /* Remote host. */
jjako52c24142002-12-16 13:33:51 +0000902
Harald Weltef6214982017-09-24 10:23:24 +0800903 if (update_arg((void *)&(args_info->remote_arg),
904 &(args_info->remote_orig),
905 &(args_info->remote_given),
906 &(local_args_info.remote_given), optarg,
907 0, 0, ARG_STRING, check_ambiguity,
908 override, 0, 0, "remote", 'r',
909 additional_error))
910 goto failure;
911
912 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100913 case 'a': /* Access point name. */
jjako52c24142002-12-16 13:33:51 +0000914
Harald Weltef6214982017-09-24 10:23:24 +0800915 if (update_arg((void *)&(args_info->apn_arg),
916 &(args_info->apn_orig),
917 &(args_info->apn_given),
918 &(local_args_info.apn_given), optarg, 0,
919 "internet", ARG_STRING, check_ambiguity,
920 override, 0, 0, "apn", 'a',
921 additional_error))
922 goto failure;
923
924 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100925 case 'i': /* IMSI. */
jjako52c24142002-12-16 13:33:51 +0000926
Harald Weltef6214982017-09-24 10:23:24 +0800927 if (update_arg((void *)&(args_info->imsi_arg),
928 &(args_info->imsi_orig),
929 &(args_info->imsi_given),
930 &(local_args_info.imsi_given), optarg, 0,
931 "240010123456789", ARG_STRING,
932 check_ambiguity, override, 0, 0, "imsi",
933 'i', additional_error))
934 goto failure;
935
936 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100937 case 'm': /* Mobile Station ISDN number. */
jjako52c24142002-12-16 13:33:51 +0000938
Harald Weltef6214982017-09-24 10:23:24 +0800939 if (update_arg((void *)&(args_info->msisdn_arg),
940 &(args_info->msisdn_orig),
941 &(args_info->msisdn_given),
942 &(local_args_info.msisdn_given), optarg,
943 0, "46702123456", ARG_STRING,
944 check_ambiguity, override, 0, 0,
945 "msisdn", 'm', additional_error))
946 goto failure;
947
948 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100949 case 'q': /* Requested quality of service. */
jjako52c24142002-12-16 13:33:51 +0000950
Harald Weltef6214982017-09-24 10:23:24 +0800951 if (update_arg((void *)&(args_info->qos_arg),
952 &(args_info->qos_orig),
953 &(args_info->qos_given),
954 &(local_args_info.qos_given), optarg, 0,
955 "0x000b921f", ARG_INT, check_ambiguity,
956 override, 0, 0, "qos", 'q',
957 additional_error))
958 goto failure;
959
960 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100961 case 'u': /* Login user ID. */
jjako52c24142002-12-16 13:33:51 +0000962
Harald Weltef6214982017-09-24 10:23:24 +0800963 if (update_arg((void *)&(args_info->uid_arg),
964 &(args_info->uid_orig),
965 &(args_info->uid_given),
966 &(local_args_info.uid_given), optarg, 0,
967 "mig", ARG_STRING, check_ambiguity,
968 override, 0, 0, "uid", 'u',
969 additional_error))
970 goto failure;
971
972 break;
Harald Weltebed35df2011-11-02 13:06:18 +0100973 case 'p': /* Login password. */
jjako52c24142002-12-16 13:33:51 +0000974
Harald Weltef6214982017-09-24 10:23:24 +0800975 if (update_arg((void *)&(args_info->pwd_arg),
976 &(args_info->pwd_orig),
977 &(args_info->pwd_given),
978 &(local_args_info.pwd_given), optarg, 0,
979 "hemmelig", ARG_STRING, check_ambiguity,
980 override, 0, 0, "pwd", 'p',
981 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +0100982 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +0800983
984 break;
985 case 'n': /* Network address for local interface. */
986
987 if (update_arg((void *)&(args_info->net_arg),
988 &(args_info->net_orig),
989 &(args_info->net_given),
990 &(local_args_info.net_given), optarg, 0,
991 0, ARG_STRING, check_ambiguity, override,
992 0, 0, "net", 'n', additional_error))
993 goto failure;
994
Harald Weltebed35df2011-11-02 13:06:18 +0100995 break;
Harald Welte840a8e92017-09-24 18:12:40 +0800996 case 't': /* PDP Type. */
997
998 if (update_arg((void *)&(args_info->pdp_type_arg),
999 &(args_info->pdp_type_orig),
1000 &(args_info->pdp_type_given),
1001 &(local_args_info.pdp_type_given),
1002 optarg, 0, "v4", ARG_STRING,
1003 check_ambiguity, override, 0, 0,
1004 "pdp-type", 't', additional_error))
1005 goto failure;
1006
1007 break;
jjako193e8b12003-11-10 12:31:41 +00001008
Harald Weltebed35df2011-11-02 13:06:18 +01001009 case 0: /* Long option with no short option */
1010 /* Filename of process id file. */
1011 if (strcmp(long_options[option_index].name, "pidfile")
1012 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001013
1014 if (update_arg
1015 ((void *)&(args_info->pidfile_arg),
1016 &(args_info->pidfile_orig),
1017 &(args_info->pidfile_given),
1018 &(local_args_info.pidfile_given), optarg,
1019 0, "./sgsnemu.pid", ARG_STRING,
1020 check_ambiguity, override, 0, 0, "pidfile",
1021 '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001022 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001023
Harald Weltebed35df2011-11-02 13:06:18 +01001024 }
1025 /* Directory of nonvolatile data. */
1026 else if (strcmp
1027 (long_options[option_index].name,
1028 "statedir") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001029
1030 if (update_arg
1031 ((void *)&(args_info->statedir_arg),
1032 &(args_info->statedir_orig),
1033 &(args_info->statedir_given),
1034 &(local_args_info.statedir_given), optarg,
1035 0, "./", ARG_STRING, check_ambiguity,
1036 override, 0, 0, "statedir", '-',
1037 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001038 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001039
Harald Weltebed35df2011-11-02 13:06:18 +01001040 }
1041 /* DNS Server to use. */
1042 else if (strcmp(long_options[option_index].name, "dns")
1043 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001044
1045 if (update_arg((void *)&(args_info->dns_arg),
1046 &(args_info->dns_orig),
1047 &(args_info->dns_given),
1048 &(local_args_info.dns_given),
1049 optarg, 0, 0, ARG_STRING,
1050 check_ambiguity, override, 0, 0,
1051 "dns", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001052 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001053
Harald Weltebed35df2011-11-02 13:06:18 +01001054 }
1055 /* Number of contexts. */
1056 else if (strcmp
1057 (long_options[option_index].name,
1058 "contexts") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001059
1060 if (update_arg
1061 ((void *)&(args_info->contexts_arg),
1062 &(args_info->contexts_orig),
1063 &(args_info->contexts_given),
1064 &(local_args_info.contexts_given), optarg,
1065 0, "1", ARG_INT, check_ambiguity, override,
1066 0, 0, "contexts", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001067 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001068
Harald Weltebed35df2011-11-02 13:06:18 +01001069 }
1070 /* Exit after timelimit seconds. */
1071 else if (strcmp
1072 (long_options[option_index].name,
1073 "timelimit") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001074
1075 if (update_arg
1076 ((void *)&(args_info->timelimit_arg),
1077 &(args_info->timelimit_orig),
1078 &(args_info->timelimit_given),
1079 &(local_args_info.timelimit_given), optarg,
1080 0, "0", ARG_INT, check_ambiguity, override,
1081 0, 0, "timelimit", '-', additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001082 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001083
Harald Weltebed35df2011-11-02 13:06:18 +01001084 }
1085 /* GTP version to use. */
1086 else if (strcmp
1087 (long_options[option_index].name,
1088 "gtpversion") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001089
1090 if (update_arg
1091 ((void *)&(args_info->gtpversion_arg),
1092 &(args_info->gtpversion_orig),
1093 &(args_info->gtpversion_given),
1094 &(local_args_info.gtpversion_given),
1095 optarg, 0, "1", ARG_INT, check_ambiguity,
1096 override, 0, 0, "gtpversion", '-',
1097 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001098 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001099
Harald Weltebed35df2011-11-02 13:06:18 +01001100 }
1101 /* Selection mode. */
1102 else if (strcmp
1103 (long_options[option_index].name,
1104 "selmode") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001105
1106 if (update_arg
1107 ((void *)&(args_info->selmode_arg),
1108 &(args_info->selmode_orig),
1109 &(args_info->selmode_given),
1110 &(local_args_info.selmode_given), optarg,
1111 0, "0x01", ARG_INT, check_ambiguity,
1112 override, 0, 0, "selmode", '-',
1113 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001114 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001115
Harald Weltebed35df2011-11-02 13:06:18 +01001116 }
1117 /* Radio Access Technology Type. */
1118 else if (strcmp
1119 (long_options[option_index].name,
1120 "rattype") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001121
1122 if (update_arg
1123 ((void *)&(args_info->rattype_arg),
1124 &(args_info->rattype_orig),
1125 &(args_info->rattype_given),
1126 &(local_args_info.rattype_given), optarg,
1127 0, "1", ARG_INT, check_ambiguity, override,
1128 0, 0, "rattype", '-', additional_error))
1129 goto failure;
1130
Harald Weltebed35df2011-11-02 13:06:18 +01001131 }
1132 /* User Location Information. */
1133 else if (strcmp
1134 (long_options[option_index].name,
1135 "userloc") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001136
1137 if (update_arg
1138 ((void *)&(args_info->userloc_arg),
1139 &(args_info->userloc_orig),
1140 &(args_info->userloc_given),
1141 &(local_args_info.userloc_given), optarg,
1142 0, "02509946241207", ARG_STRING,
1143 check_ambiguity, override, 0, 0, "userloc",
1144 '-', additional_error))
1145 goto failure;
1146
Harald Weltebed35df2011-11-02 13:06:18 +01001147 }
1148 /* Routing Area Information. */
1149 else if (strcmp(long_options[option_index].name, "rai")
1150 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001151
1152 if (update_arg((void *)&(args_info->rai_arg),
1153 &(args_info->rai_orig),
1154 &(args_info->rai_given),
1155 &(local_args_info.rai_given),
1156 optarg, 0, "02509946241207",
1157 ARG_STRING, check_ambiguity,
1158 override, 0, 0, "rai", '-',
1159 additional_error))
1160 goto failure;
1161
Harald Weltebed35df2011-11-02 13:06:18 +01001162 }
Harald Weltef6214982017-09-24 10:23:24 +08001163 /* MS Time Zone. */
Harald Weltebed35df2011-11-02 13:06:18 +01001164 else if (strcmp(long_options[option_index].name, "mstz")
1165 == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001166
1167 if (update_arg((void *)&(args_info->mstz_arg),
1168 &(args_info->mstz_orig),
1169 &(args_info->mstz_given),
1170 &(local_args_info.mstz_given),
1171 optarg, 0, "0", ARG_STRING,
1172 check_ambiguity, override, 0, 0,
1173 "mstz", '-', additional_error))
1174 goto failure;
1175
Harald Weltebed35df2011-11-02 13:06:18 +01001176 }
Harald Weltef6214982017-09-24 10:23:24 +08001177 /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */
Harald Weltebed35df2011-11-02 13:06:18 +01001178 else if (strcmp
1179 (long_options[option_index].name,
1180 "imeisv") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001181
1182 if (update_arg((void *)&(args_info->imeisv_arg),
1183 &(args_info->imeisv_orig),
1184 &(args_info->imeisv_given),
1185 &(local_args_info.imeisv_given),
1186 optarg, 0, "2143658709214365",
1187 ARG_STRING, check_ambiguity,
1188 override, 0, 0, "imeisv", '-',
1189 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001190 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001191
Harald Weltebed35df2011-11-02 13:06:18 +01001192 }
1193 /* Do not send recovery. */
1194 else if (strcmp
1195 (long_options[option_index].name,
1196 "norecovery") == 0) {
Harald Weltef6214982017-09-24 10:23:24 +08001197
1198 if (update_arg
1199 ((void *)&(args_info->norecovery_flag), 0,
1200 &(args_info->norecovery_given),
1201 &(local_args_info.norecovery_given),
1202 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1203 override, 1, 0, "norecovery", '-',
1204 additional_error))
Harald Weltebed35df2011-11-02 13:06:18 +01001205 goto failure;
Harald Weltef6214982017-09-24 10:23:24 +08001206
1207 }
1208 /* NSAPI. */
1209 else if (strcmp
1210 (long_options[option_index].name,
1211 "nsapi") == 0) {
1212
1213 if (update_arg((void *)&(args_info->nsapi_arg),
1214 &(args_info->nsapi_orig),
1215 &(args_info->nsapi_given),
1216 &(local_args_info.nsapi_given),
1217 optarg, 0, "0", ARG_INT,
1218 check_ambiguity, override, 0, 0,
1219 "nsapi", '-', additional_error))
1220 goto failure;
1221
1222 }
1223 /* Requested quality of service Extension 1. */
1224 else if (strcmp
1225 (long_options[option_index].name,
1226 "qose1") == 0) {
1227
1228 if (update_arg((void *)&(args_info->qose1_arg),
1229 &(args_info->qose1_orig),
1230 &(args_info->qose1_given),
1231 &(local_args_info.qose1_given),
1232 optarg, 0, "0x9396404074f9ffff",
1233 ARG_INT, check_ambiguity,
1234 override, 0, 0, "qose1", '-',
1235 additional_error))
1236 goto failure;
1237
1238 }
1239 /* Requested quality of service Extension 2. */
1240 else if (strcmp
1241 (long_options[option_index].name,
1242 "qose2") == 0) {
1243
1244 if (update_arg((void *)&(args_info->qose2_arg),
1245 &(args_info->qose2_orig),
1246 &(args_info->qose2_given),
1247 &(local_args_info.qose2_given),
1248 optarg, 0, "0x11", ARG_INT,
1249 check_ambiguity, override, 0, 0,
1250 "qose2", '-', additional_error))
1251 goto failure;
1252
1253 }
1254 /* Requested quality of service Extension 3. */
1255 else if (strcmp
1256 (long_options[option_index].name,
1257 "qose3") == 0) {
1258
1259 if (update_arg((void *)&(args_info->qose3_arg),
1260 &(args_info->qose3_orig),
1261 &(args_info->qose3_given),
1262 &(local_args_info.qose3_given),
1263 optarg, 0, "0x0101", ARG_INT,
1264 check_ambiguity, override, 0, 0,
1265 "qose3", '-', additional_error))
1266 goto failure;
1267
1268 }
1269 /* Requested quality of service Extension 4. */
1270 else if (strcmp
1271 (long_options[option_index].name,
1272 "qose4") == 0) {
1273
1274 if (update_arg((void *)&(args_info->qose4_arg),
1275 &(args_info->qose4_orig),
1276 &(args_info->qose4_given),
1277 &(local_args_info.qose4_given),
1278 optarg, 0, "0x4040", ARG_INT,
1279 check_ambiguity, override, 0, 0,
1280 "qose4", '-', additional_error))
1281 goto failure;
1282
1283 }
1284 /* Charging characteristics. */
1285 else if (strcmp
1286 (long_options[option_index].name,
1287 "charging") == 0) {
1288
1289 if (update_arg
1290 ((void *)&(args_info->charging_arg),
1291 &(args_info->charging_orig),
1292 &(args_info->charging_given),
1293 &(local_args_info.charging_given), optarg,
1294 0, "0x0800", ARG_INT, check_ambiguity,
1295 override, 0, 0, "charging", '-',
1296 additional_error))
1297 goto failure;
1298
1299 }
1300 /* Create local network interface. */
1301 else if (strcmp
1302 (long_options[option_index].name,
1303 "createif") == 0) {
1304
1305 if (update_arg
1306 ((void *)&(args_info->createif_flag), 0,
1307 &(args_info->createif_given),
1308 &(local_args_info.createif_given), optarg,
1309 0, 0, ARG_FLAG, check_ambiguity, override,
1310 1, 0, "createif", '-', additional_error))
1311 goto failure;
1312
1313 }
1314 /* Create default route. */
1315 else if (strcmp
1316 (long_options[option_index].name,
1317 "defaultroute") == 0) {
1318
1319 if (update_arg
1320 ((void *)&(args_info->defaultroute_flag), 0,
1321 &(args_info->defaultroute_given),
1322 &(local_args_info.defaultroute_given),
1323 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1324 override, 1, 0, "defaultroute", '-',
1325 additional_error))
1326 goto failure;
1327
1328 }
1329 /* Script to run after link-up. */
1330 else if (strcmp(long_options[option_index].name, "ipup")
1331 == 0) {
1332
1333 if (update_arg((void *)&(args_info->ipup_arg),
1334 &(args_info->ipup_orig),
1335 &(args_info->ipup_given),
1336 &(local_args_info.ipup_given),
1337 optarg, 0, 0, ARG_STRING,
1338 check_ambiguity, override, 0, 0,
1339 "ipup", '-', additional_error))
1340 goto failure;
1341
1342 }
1343 /* Script to run after link-down. */
1344 else if (strcmp
1345 (long_options[option_index].name,
1346 "ipdown") == 0) {
1347
1348 if (update_arg((void *)&(args_info->ipdown_arg),
1349 &(args_info->ipdown_orig),
1350 &(args_info->ipdown_given),
1351 &(local_args_info.ipdown_given),
1352 optarg, 0, 0, ARG_STRING,
1353 check_ambiguity, override, 0, 0,
1354 "ipdown", '-', additional_error))
1355 goto failure;
1356
1357 }
1358 /* Ping remote host. */
1359 else if (strcmp
1360 (long_options[option_index].name,
1361 "pinghost") == 0) {
1362
1363 if (update_arg
1364 ((void *)&(args_info->pinghost_arg),
1365 &(args_info->pinghost_orig),
1366 &(args_info->pinghost_given),
1367 &(local_args_info.pinghost_given), optarg,
1368 0, 0, ARG_STRING, check_ambiguity,
1369 override, 0, 0, "pinghost", '-',
1370 additional_error))
1371 goto failure;
1372
1373 }
1374 /* Number of ping req per second. */
1375 else if (strcmp
1376 (long_options[option_index].name,
1377 "pingrate") == 0) {
1378
1379 if (update_arg
1380 ((void *)&(args_info->pingrate_arg),
1381 &(args_info->pingrate_orig),
1382 &(args_info->pingrate_given),
1383 &(local_args_info.pingrate_given), optarg,
1384 0, "1", ARG_INT, check_ambiguity, override,
1385 0, 0, "pingrate", '-', additional_error))
1386 goto failure;
1387
1388 }
1389 /* Number of ping data bytes. */
1390 else if (strcmp
1391 (long_options[option_index].name,
1392 "pingsize") == 0) {
1393
1394 if (update_arg
1395 ((void *)&(args_info->pingsize_arg),
1396 &(args_info->pingsize_orig),
1397 &(args_info->pingsize_given),
1398 &(local_args_info.pingsize_given), optarg,
1399 0, "56", ARG_INT, check_ambiguity,
1400 override, 0, 0, "pingsize", '-',
1401 additional_error))
1402 goto failure;
1403
1404 }
1405 /* Number of ping req to send. */
1406 else if (strcmp
1407 (long_options[option_index].name,
1408 "pingcount") == 0) {
1409
1410 if (update_arg
1411 ((void *)&(args_info->pingcount_arg),
1412 &(args_info->pingcount_orig),
1413 &(args_info->pingcount_given),
1414 &(local_args_info.pingcount_given), optarg,
1415 0, "0", ARG_INT, check_ambiguity, override,
1416 0, 0, "pingcount", '-', additional_error))
1417 goto failure;
1418
1419 }
1420 /* Do not print ping packet info. */
1421 else if (strcmp
1422 (long_options[option_index].name,
1423 "pingquiet") == 0) {
1424
1425 if (update_arg
1426 ((void *)&(args_info->pingquiet_flag), 0,
1427 &(args_info->pingquiet_given),
1428 &(local_args_info.pingquiet_given), optarg,
1429 0, 0, ARG_FLAG, check_ambiguity, override,
1430 1, 0, "pingquiet", '-', additional_error))
1431 goto failure;
1432
Harald Weltebed35df2011-11-02 13:06:18 +01001433 }
Harald Weltefbb9c7f2017-09-24 11:50:20 +08001434 /* Don't transmit G-PDU sequence nums. */
1435 else if (strcmp
1436 (long_options[option_index].name,
1437 "no-tx-gpdu-seq") == 0) {
1438
1439 if (update_arg
1440 ((void *)&(args_info->no_tx_gpdu_seq_flag),
1441 0, &(args_info->no_tx_gpdu_seq_given),
1442 &(local_args_info.no_tx_gpdu_seq_given),
1443 optarg, 0, 0, ARG_FLAG, check_ambiguity,
1444 override, 1, 0, "no-tx-gpdu-seq", '-',
1445 additional_error))
1446 goto failure;
1447
1448 }
jjako52c24142002-12-16 13:33:51 +00001449
Harald Weltebed35df2011-11-02 13:06:18 +01001450 break;
1451 case '?': /* Invalid option. */
1452 /* `getopt_long' already printed an error message. */
1453 goto failure;
jjako52c24142002-12-16 13:33:51 +00001454
Harald Weltebed35df2011-11-02 13:06:18 +01001455 default: /* bug: option not considered. */
1456 fprintf(stderr, "%s: option unknown: %c%s\n",
1457 CMDLINE_PARSER_PACKAGE, c,
1458 (additional_error ? additional_error : ""));
1459 abort();
1460 } /* switch */
1461 } /* while */
jjako52c24142002-12-16 13:33:51 +00001462
Harald Weltebed35df2011-11-02 13:06:18 +01001463 cmdline_parser_release(&local_args_info);
jjako52c24142002-12-16 13:33:51 +00001464
Harald Weltef6214982017-09-24 10:23:24 +08001465 if (error_occurred)
Harald Weltebed35df2011-11-02 13:06:18 +01001466 return (EXIT_FAILURE);
Harald Welte1b3e5772010-05-04 11:13:56 +02001467
Harald Weltebed35df2011-11-02 13:06:18 +01001468 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001469
1470failure:
Harald Weltebed35df2011-11-02 13:06:18 +01001471
1472 cmdline_parser_release(&local_args_info);
1473 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +00001474}
1475
Harald Welte1b3e5772010-05-04 11:13:56 +02001476#ifndef CONFIG_FILE_LINE_SIZE
1477#define CONFIG_FILE_LINE_SIZE 2048
1478#endif
1479#define ADDITIONAL_ERROR " in configuration file "
1480
1481#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
1482/* 3 is for "--" and "=" */
1483
Harald Weltef6214982017-09-24 10:23:24 +08001484static int _cmdline_parser_configfile(const char *filename, int *my_argc)
jjako52c24142002-12-16 13:33:51 +00001485{
Harald Weltebed35df2011-11-02 13:06:18 +01001486 FILE *file;
Harald Weltef6214982017-09-24 10:23:24 +08001487 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1];
Harald Weltebed35df2011-11-02 13:06:18 +01001488 char linebuf[CONFIG_FILE_LINE_SIZE];
1489 int line_num = 0;
Harald Weltef6214982017-09-24 10:23:24 +08001490 int result = 0, equal;
Harald Weltebed35df2011-11-02 13:06:18 +01001491 char *fopt, *farg;
1492 char *str_index;
1493 size_t len, next_token;
1494 char delimiter;
Harald Welte1b3e5772010-05-04 11:13:56 +02001495
Harald Weltef6214982017-09-24 10:23:24 +08001496 if ((file = fopen(filename, "r")) == 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001497 fprintf(stderr, "%s: Error opening configuration file '%s'\n",
1498 CMDLINE_PARSER_PACKAGE, filename);
Harald Weltef6214982017-09-24 10:23:24 +08001499 return EXIT_FAILURE;
Harald Weltebed35df2011-11-02 13:06:18 +01001500 }
jjako52c24142002-12-16 13:33:51 +00001501
Harald Weltef6214982017-09-24 10:23:24 +08001502 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) {
Harald Weltebed35df2011-11-02 13:06:18 +01001503 ++line_num;
1504 my_argv[0] = '\0';
1505 len = strlen(linebuf);
1506 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) {
1507 fprintf(stderr,
1508 "%s:%s:%d: Line too long in configuration file\n",
1509 CMDLINE_PARSER_PACKAGE, filename, line_num);
1510 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001511 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001512 }
jjako52c24142002-12-16 13:33:51 +00001513
Harald Weltebed35df2011-11-02 13:06:18 +01001514 /* find first non-whitespace character in the line */
1515 next_token = strspn(linebuf, " \t\r\n");
1516 str_index = linebuf + next_token;
jjako52c24142002-12-16 13:33:51 +00001517
Harald Weltebed35df2011-11-02 13:06:18 +01001518 if (str_index[0] == '\0' || str_index[0] == '#')
1519 continue; /* empty line or comment line is skipped */
Harald Welte1b3e5772010-05-04 11:13:56 +02001520
Harald Weltebed35df2011-11-02 13:06:18 +01001521 fopt = str_index;
Harald Welte1b3e5772010-05-04 11:13:56 +02001522
Harald Weltebed35df2011-11-02 13:06:18 +01001523 /* truncate fopt at the end of the first non-valid character */
1524 next_token = strcspn(fopt, " \t\r\n=");
Harald Welte1b3e5772010-05-04 11:13:56 +02001525
Harald Weltebed35df2011-11-02 13:06:18 +01001526 if (fopt[next_token] == '\0') { /* the line is over */
Harald Weltef6214982017-09-24 10:23:24 +08001527 farg = 0;
Harald Weltebed35df2011-11-02 13:06:18 +01001528 equal = 0;
1529 goto noarg;
1530 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001531
Harald Weltebed35df2011-11-02 13:06:18 +01001532 /* remember if equal sign is present */
1533 equal = (fopt[next_token] == '=');
1534 fopt[next_token++] = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001535
Harald Weltebed35df2011-11-02 13:06:18 +01001536 /* advance pointers to the next token after the end of fopt */
1537 next_token += strspn(fopt + next_token, " \t\r\n");
Harald Weltef6214982017-09-24 10:23:24 +08001538
Harald Weltebed35df2011-11-02 13:06:18 +01001539 /* check for the presence of equal sign, and if so, skip it */
1540 if (!equal)
1541 if ((equal = (fopt[next_token] == '='))) {
1542 next_token++;
1543 next_token +=
1544 strspn(fopt + next_token, " \t\r\n");
1545 }
1546 str_index += next_token;
Harald Welte1b3e5772010-05-04 11:13:56 +02001547
Harald Weltebed35df2011-11-02 13:06:18 +01001548 /* find argument */
1549 farg = str_index;
1550 if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */
1551 str_index = strchr(++farg, str_index[0]); /* skip opening quote */
1552 if (!str_index) {
1553 fprintf
1554 (stderr,
1555 "%s:%s:%d: unterminated string in configuration file\n",
1556 CMDLINE_PARSER_PACKAGE, filename,
1557 line_num);
1558 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001559 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001560 }
1561 } else { /* read up the remaining part up to a delimiter */
1562 next_token = strcspn(farg, " \t\r\n#\'\"");
1563 str_index += next_token;
1564 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001565
Harald Weltebed35df2011-11-02 13:06:18 +01001566 /* truncate farg at the delimiter and store it for further check */
1567 delimiter = *str_index, *str_index++ = '\0';
Harald Welte1b3e5772010-05-04 11:13:56 +02001568
Harald Weltebed35df2011-11-02 13:06:18 +01001569 /* everything but comment is illegal at the end of line */
1570 if (delimiter != '\0' && delimiter != '#') {
1571 str_index += strspn(str_index, " \t\r\n");
1572 if (*str_index != '\0' && *str_index != '#') {
1573 fprintf
1574 (stderr,
1575 "%s:%s:%d: malformed string in configuration file\n",
1576 CMDLINE_PARSER_PACKAGE, filename,
1577 line_num);
1578 result = EXIT_FAILURE;
Harald Weltef6214982017-09-24 10:23:24 +08001579 break;
Harald Weltebed35df2011-11-02 13:06:18 +01001580 }
1581 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001582
Harald Weltebed35df2011-11-02 13:06:18 +01001583noarg:
Harald Weltef6214982017-09-24 10:23:24 +08001584 if (!strcmp(fopt, "include")) {
1585 if (farg && *farg) {
1586 result =
1587 _cmdline_parser_configfile(farg, my_argc);
1588 } else {
1589 fprintf(stderr,
1590 "%s:%s:%d: include requires a filename argument.\n",
1591 CMDLINE_PARSER_PACKAGE, filename,
1592 line_num);
1593 }
1594 continue;
1595 }
Harald Weltebed35df2011-11-02 13:06:18 +01001596 len = strlen(fopt);
Harald Weltebed35df2011-11-02 13:06:18 +01001597 strcat(my_argv, len > 1 ? "--" : "-");
1598 strcat(my_argv, fopt);
1599 if (len > 1 && ((farg && *farg) || equal))
1600 strcat(my_argv, "=");
1601 if (farg && *farg)
1602 strcat(my_argv, farg);
Harald Weltef6214982017-09-24 10:23:24 +08001603 ++(*my_argc);
Harald Welte1b3e5772010-05-04 11:13:56 +02001604
Harald Weltebed35df2011-11-02 13:06:18 +01001605 cmd_line_list_tmp =
1606 (struct line_list *)malloc(sizeof(struct line_list));
1607 cmd_line_list_tmp->next = cmd_line_list;
1608 cmd_line_list = cmd_line_list_tmp;
1609 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1610 } /* while */
jjako52c24142002-12-16 13:33:51 +00001611
Harald Weltebed35df2011-11-02 13:06:18 +01001612 if (file)
1613 fclose(file);
Harald Weltef6214982017-09-24 10:23:24 +08001614 return result;
1615}
1616
1617int
1618cmdline_parser_configfile(const char *filename,
1619 struct gengetopt_args_info *args_info,
1620 int override, int initialize, int check_required)
1621{
1622 struct cmdline_parser_params params;
1623
1624 params.override = override;
1625 params.initialize = initialize;
1626 params.check_required = check_required;
1627 params.check_ambiguity = 0;
1628 params.print_errors = 1;
1629
1630 return cmdline_parser_config_file(filename, args_info, &params);
1631}
1632
1633int
1634cmdline_parser_config_file(const char *filename,
1635 struct gengetopt_args_info *args_info,
1636 struct cmdline_parser_params *params)
1637{
1638 int i, result;
1639 int my_argc = 1;
1640 char **my_argv_arg;
1641 char *additional_error;
1642
1643 /* store the program name */
1644 cmd_line_list_tmp =
1645 (struct line_list *)malloc(sizeof(struct line_list));
1646 cmd_line_list_tmp->next = cmd_line_list;
1647 cmd_line_list = cmd_line_list_tmp;
1648 cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE);
1649
1650 result = _cmdline_parser_configfile(filename, &my_argc);
1651
1652 if (result != EXIT_FAILURE) {
1653 my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *));
1654 cmd_line_list_tmp = cmd_line_list;
1655
1656 for (i = my_argc - 1; i >= 0; --i) {
1657 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1658 cmd_line_list_tmp = cmd_line_list_tmp->next;
1659 }
1660
1661 my_argv_arg[my_argc] = 0;
1662
1663 additional_error =
1664 (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) +
1665 1);
1666 strcpy(additional_error, ADDITIONAL_ERROR);
1667 strcat(additional_error, filename);
1668 result =
1669 cmdline_parser_internal(my_argc, my_argv_arg, args_info,
1670 params, additional_error);
1671
1672 free(additional_error);
1673 free(my_argv_arg);
1674 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001675
Harald Weltebed35df2011-11-02 13:06:18 +01001676 free_cmd_list();
1677 if (result == EXIT_FAILURE) {
1678 cmdline_parser_free(args_info);
1679 exit(EXIT_FAILURE);
1680 }
1681
1682 return result;
jjako52c24142002-12-16 13:33:51 +00001683}