blob: a4c25d89d8a0f83c3ee354058c6e8b616ec45695 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +01002 File autogenerated by gengetopt version 2.22.5
jjako52c24142002-12-16 13:33:51 +00003 generated with the following command:
jjakoa7cd2492003-04-11 09:40:12 +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
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010020#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
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010030const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]...";
31
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010032const char *gengetopt_args_info_description = "";
jjako52c24142002-12-16 13:33:51 +000033
Harald Welte1b3e5772010-05-04 11:13:56 +020034const char *gengetopt_args_info_help[] = {
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010035 " -h, --help Print help and exit",
36 " -V, --version Print version and exit",
37 " -f, --fg Run in foreground (default=off)",
38 " -d, --debug Run in debug mode (default=off)",
39 " -c, --conf=STRING Read configuration file (default=`/etc/ggsn.conf')",
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010040 " --pidfile=STRING Filename of process id file \n (default=`/var/run/ggsn.pid')",
41 " --statedir=STRING Directory of nonvolatile data \n (default=`/var/lib/ggsn/')",
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010042 " -l, --listen=STRING Local interface",
43 " -n, --net=STRING Network (default=`192.168.0.0/24')",
44 " --ipup=STRING Script to run after link-up",
45 " --ipdown=STRING Script to run after link-down",
46 " --dynip=STRING Dynamic IP address pool",
47 " --statip=STRING Static IP address pool",
48 " --pcodns1=STRING PCO DNS Server 1 (default=`0.0.0.0')",
49 " --pcodns2=STRING PCO DNS Server 2 (default=`0.0.0.0')",
50 " --timelimit=INT Exit after timelimit seconds (default=`0')",
51 " -a, --apn=STRING Access point name (default=`internet')",
52 " -q, --qos=INT Requested quality of service (default=`0x0b921f')",
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010053 " --logfile=STRING Logfile for errors",
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +010054 " --loglevel=STRING Global log ldevel (default=`error')",
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010055 " -g, --gtpnl=STRING GTP kernel support (default=`eth0')",
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010056 0
Harald Welte1b3e5772010-05-04 11:13:56 +020057};
58
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010059typedef enum {ARG_NO
60 , ARG_FLAG
61 , ARG_STRING
62 , ARG_INT
63} cmdline_parser_arg_type;
64
Harald Welte1b3e5772010-05-04 11:13:56 +020065static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010066void clear_given (struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020067static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010068void clear_args (struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020069
70static int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010071cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
72 struct cmdline_parser_params *params, const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020073
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010074struct line_list
75{
76 char * string_arg;
77 struct line_list * next;
Harald Welte1b3e5772010-05-04 11:13:56 +020078};
79
80static struct line_list *cmd_line_list = 0;
81static struct line_list *cmd_line_list_tmp = 0;
82
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010083static void
84free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +020085{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010086 /* free the list of a previous call */
87 if (cmd_line_list)
88 {
89 while (cmd_line_list) {
90 cmd_line_list_tmp = cmd_line_list;
91 cmd_line_list = cmd_line_list->next;
92 free (cmd_line_list_tmp->string_arg);
93 free (cmd_line_list_tmp);
94 }
95 }
jjako52c24142002-12-16 13:33:51 +000096}
97
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010098
99static char *
100gengetopt_strdup (const char *s);
jjako52c24142002-12-16 13:33:51 +0000101
Harald Welte1b3e5772010-05-04 11:13:56 +0200102static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100103void clear_given (struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000104{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100105 args_info->help_given = 0 ;
106 args_info->version_given = 0 ;
107 args_info->fg_given = 0 ;
108 args_info->debug_given = 0 ;
109 args_info->conf_given = 0 ;
110 args_info->pidfile_given = 0 ;
111 args_info->statedir_given = 0 ;
112 args_info->listen_given = 0 ;
113 args_info->net_given = 0 ;
114 args_info->ipup_given = 0 ;
115 args_info->ipdown_given = 0 ;
116 args_info->dynip_given = 0 ;
117 args_info->statip_given = 0 ;
118 args_info->pcodns1_given = 0 ;
119 args_info->pcodns2_given = 0 ;
120 args_info->timelimit_given = 0 ;
121 args_info->apn_given = 0 ;
122 args_info->qos_given = 0 ;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100123 args_info->logfile_given = 0 ;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100124 args_info->loglevel_given = 0 ;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100125 args_info->gtpnl_given = 0 ;
jjako52c24142002-12-16 13:33:51 +0000126}
127
Harald Welte1b3e5772010-05-04 11:13:56 +0200128static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100129void clear_args (struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200130{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100131 FIX_UNUSED (args_info);
132 args_info->fg_flag = 0;
133 args_info->debug_flag = 0;
134 args_info->conf_arg = gengetopt_strdup ("/etc/ggsn.conf");
135 args_info->conf_orig = NULL;
136 args_info->pidfile_arg = gengetopt_strdup ("/var/run/ggsn.pid");
137 args_info->pidfile_orig = NULL;
138 args_info->statedir_arg = gengetopt_strdup ("/var/lib/ggsn/");
139 args_info->statedir_orig = NULL;
140 args_info->listen_arg = NULL;
141 args_info->listen_orig = NULL;
142 args_info->net_arg = gengetopt_strdup ("192.168.0.0/24");
143 args_info->net_orig = NULL;
144 args_info->ipup_arg = NULL;
145 args_info->ipup_orig = NULL;
146 args_info->ipdown_arg = NULL;
147 args_info->ipdown_orig = NULL;
148 args_info->dynip_arg = NULL;
149 args_info->dynip_orig = NULL;
150 args_info->statip_arg = NULL;
151 args_info->statip_orig = NULL;
152 args_info->pcodns1_arg = gengetopt_strdup ("0.0.0.0");
153 args_info->pcodns1_orig = NULL;
154 args_info->pcodns2_arg = gengetopt_strdup ("0.0.0.0");
155 args_info->pcodns2_orig = NULL;
156 args_info->timelimit_arg = 0;
157 args_info->timelimit_orig = NULL;
158 args_info->apn_arg = gengetopt_strdup ("internet");
159 args_info->apn_orig = NULL;
160 args_info->qos_arg = 0x0b921f;
161 args_info->qos_orig = NULL;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100162 args_info->logfile_arg = NULL;
163 args_info->logfile_orig = NULL;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100164 args_info->loglevel_arg = gengetopt_strdup ("error");
165 args_info->loglevel_orig = NULL;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100166 args_info->gtpnl_arg = gengetopt_strdup ("eth0");
167 args_info->gtpnl_orig = NULL;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100168
Harald Welte1b3e5772010-05-04 11:13:56 +0200169}
170
171static
172void init_args_info(struct gengetopt_args_info *args_info)
173{
Harald Weltebed35df2011-11-02 13:06:18 +0100174
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100175
176 args_info->help_help = gengetopt_args_info_help[0] ;
177 args_info->version_help = gengetopt_args_info_help[1] ;
178 args_info->fg_help = gengetopt_args_info_help[2] ;
179 args_info->debug_help = gengetopt_args_info_help[3] ;
180 args_info->conf_help = gengetopt_args_info_help[4] ;
181 args_info->pidfile_help = gengetopt_args_info_help[5] ;
182 args_info->statedir_help = gengetopt_args_info_help[6] ;
183 args_info->listen_help = gengetopt_args_info_help[7] ;
184 args_info->net_help = gengetopt_args_info_help[8] ;
185 args_info->ipup_help = gengetopt_args_info_help[9] ;
186 args_info->ipdown_help = gengetopt_args_info_help[10] ;
187 args_info->dynip_help = gengetopt_args_info_help[11] ;
188 args_info->statip_help = gengetopt_args_info_help[12] ;
189 args_info->pcodns1_help = gengetopt_args_info_help[13] ;
190 args_info->pcodns2_help = gengetopt_args_info_help[14] ;
191 args_info->timelimit_help = gengetopt_args_info_help[15] ;
192 args_info->apn_help = gengetopt_args_info_help[16] ;
193 args_info->qos_help = gengetopt_args_info_help[17] ;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100194 args_info->logfile_help = gengetopt_args_info_help[18] ;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100195 args_info->loglevel_help = gengetopt_args_info_help[19] ;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100196 args_info->gtpnl_help = gengetopt_args_info_help[19] ;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100197
Harald Welte1b3e5772010-05-04 11:13:56 +0200198}
199
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100200void
201cmdline_parser_print_version (void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200202{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100203 printf ("%s %s\n",
204 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
205 CMDLINE_PARSER_VERSION);
Harald Welte1b3e5772010-05-04 11:13:56 +0200206}
207
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100208static void print_help_common(void) {
209 cmdline_parser_print_version ();
Harald Welte1b3e5772010-05-04 11:13:56 +0200210
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100211 if (strlen(gengetopt_args_info_purpose) > 0)
212 printf("\n%s\n", gengetopt_args_info_purpose);
Harald Welte1b3e5772010-05-04 11:13:56 +0200213
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100214 if (strlen(gengetopt_args_info_usage) > 0)
215 printf("\n%s\n", gengetopt_args_info_usage);
216
217 printf("\n");
218
219 if (strlen(gengetopt_args_info_description) > 0)
220 printf("%s\n\n", gengetopt_args_info_description);
Harald Welte1b3e5772010-05-04 11:13:56 +0200221}
222
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100223void
224cmdline_parser_print_help (void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200225{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100226 int i = 0;
227 print_help_common();
228 while (gengetopt_args_info_help[i])
229 printf("%s\n", gengetopt_args_info_help[i++]);
Harald Welte1b3e5772010-05-04 11:13:56 +0200230}
231
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100232void
233cmdline_parser_init (struct gengetopt_args_info *args_info)
234{
235 clear_given (args_info);
236 clear_args (args_info);
237 init_args_info (args_info);
238}
239
240void
241cmdline_parser_params_init(struct cmdline_parser_params *params)
242{
243 if (params)
244 {
245 params->override = 0;
246 params->initialize = 1;
247 params->check_required = 1;
248 params->check_ambiguity = 0;
249 params->print_errors = 1;
250 }
251}
252
253struct cmdline_parser_params *
254cmdline_parser_params_create(void)
255{
256 struct cmdline_parser_params *params =
257 (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
258 cmdline_parser_params_init(params);
259 return params;
260}
261
262static void
263free_string_field (char **s)
264{
265 if (*s)
266 {
267 free (*s);
268 *s = 0;
269 }
270}
271
272
273static void
274cmdline_parser_release (struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200275{
Harald Weltebed35df2011-11-02 13:06:18 +0100276
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100277 free_string_field (&(args_info->conf_arg));
278 free_string_field (&(args_info->conf_orig));
279 free_string_field (&(args_info->pidfile_arg));
280 free_string_field (&(args_info->pidfile_orig));
281 free_string_field (&(args_info->statedir_arg));
282 free_string_field (&(args_info->statedir_orig));
283 free_string_field (&(args_info->listen_arg));
284 free_string_field (&(args_info->listen_orig));
285 free_string_field (&(args_info->net_arg));
286 free_string_field (&(args_info->net_orig));
287 free_string_field (&(args_info->ipup_arg));
288 free_string_field (&(args_info->ipup_orig));
289 free_string_field (&(args_info->ipdown_arg));
290 free_string_field (&(args_info->ipdown_orig));
291 free_string_field (&(args_info->dynip_arg));
292 free_string_field (&(args_info->dynip_orig));
293 free_string_field (&(args_info->statip_arg));
294 free_string_field (&(args_info->statip_orig));
295 free_string_field (&(args_info->pcodns1_arg));
296 free_string_field (&(args_info->pcodns1_orig));
297 free_string_field (&(args_info->pcodns2_arg));
298 free_string_field (&(args_info->pcodns2_orig));
299 free_string_field (&(args_info->timelimit_orig));
300 free_string_field (&(args_info->apn_arg));
301 free_string_field (&(args_info->apn_orig));
302 free_string_field (&(args_info->qos_orig));
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100303 free_string_field (&(args_info->logfile_arg));
304 free_string_field (&(args_info->logfile_orig));
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100305 free_string_field (&(args_info->loglevel_arg));
306 free_string_field (&(args_info->loglevel_orig));
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100307 free_string_field (&(args_info->gtpnl_arg));
308 free_string_field (&(args_info->gtpnl_orig));
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100309
310
Harald Weltebed35df2011-11-02 13:06:18 +0100311
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100312 clear_given (args_info);
313}
314
315
316static void
317write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
318{
319 FIX_UNUSED (values);
320 if (arg) {
321 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
322 } else {
323 fprintf(outfile, "%s\n", opt);
324 }
325}
326
327
328int
329cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
330{
331 int i = 0;
332
333 if (!outfile)
334 {
335 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
336 return EXIT_FAILURE;
337 }
338
339 if (args_info->help_given)
340 write_into_file(outfile, "help", 0, 0 );
341 if (args_info->version_given)
342 write_into_file(outfile, "version", 0, 0 );
343 if (args_info->fg_given)
344 write_into_file(outfile, "fg", 0, 0 );
345 if (args_info->debug_given)
346 write_into_file(outfile, "debug", 0, 0 );
347 if (args_info->conf_given)
348 write_into_file(outfile, "conf", args_info->conf_orig, 0);
349 if (args_info->pidfile_given)
350 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
351 if (args_info->statedir_given)
352 write_into_file(outfile, "statedir", args_info->statedir_orig, 0);
353 if (args_info->listen_given)
354 write_into_file(outfile, "listen", args_info->listen_orig, 0);
355 if (args_info->net_given)
356 write_into_file(outfile, "net", args_info->net_orig, 0);
357 if (args_info->ipup_given)
358 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
359 if (args_info->ipdown_given)
360 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
361 if (args_info->dynip_given)
362 write_into_file(outfile, "dynip", args_info->dynip_orig, 0);
363 if (args_info->statip_given)
364 write_into_file(outfile, "statip", args_info->statip_orig, 0);
365 if (args_info->pcodns1_given)
366 write_into_file(outfile, "pcodns1", args_info->pcodns1_orig, 0);
367 if (args_info->pcodns2_given)
368 write_into_file(outfile, "pcodns2", args_info->pcodns2_orig, 0);
369 if (args_info->timelimit_given)
370 write_into_file(outfile, "timelimit", args_info->timelimit_orig, 0);
371 if (args_info->apn_given)
372 write_into_file(outfile, "apn", args_info->apn_orig, 0);
373 if (args_info->qos_given)
374 write_into_file(outfile, "qos", args_info->qos_orig, 0);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100375 if (args_info->logfile_given)
376 write_into_file(outfile, "logfile", args_info->logfile_orig, 0);
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100377 if (args_info->loglevel_given)
378 write_into_file(outfile, "loglevel", args_info->loglevel_orig, 0);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100379 if (args_info->gtpnl_given)
380 write_into_file(outfile, "gtpnl", args_info->gtpnl_orig, 0);
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100381
382
383 i = EXIT_SUCCESS;
384 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200385}
386
387int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100388cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200389{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100390 FILE *outfile;
391 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200392
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100393 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200394
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100395 if (!outfile)
396 {
397 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
398 return EXIT_FAILURE;
399 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200400
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100401 i = cmdline_parser_dump(outfile, args_info);
402 fclose (outfile);
Harald Welte1b3e5772010-05-04 11:13:56 +0200403
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100404 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200405}
406
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100407void
408cmdline_parser_free (struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200409{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100410 cmdline_parser_release (args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200411}
412
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100413/** @brief replacement of strdup, which is not standard */
414char *
415gengetopt_strdup (const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200416{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100417 char *result = 0;
418 if (!s)
419 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200420
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100421 result = (char*)malloc(strlen(s) + 1);
422 if (result == (char*)0)
423 return (char*)0;
424 strcpy(result, s);
425 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200426}
427
428int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100429cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200430{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100431 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200432}
433
434int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100435cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
436 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200437{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100438 int result;
439 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200440
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100441 if (result == EXIT_FAILURE)
442 {
443 cmdline_parser_free (args_info);
444 exit (EXIT_FAILURE);
445 }
446
447 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200448}
449
450int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100451cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
Harald Welte1b3e5772010-05-04 11:13:56 +0200452{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100453 int result;
454 struct cmdline_parser_params params;
455
456 params.override = override;
457 params.initialize = initialize;
458 params.check_required = check_required;
459 params.check_ambiguity = 0;
460 params.print_errors = 1;
461
462 result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
463
464 if (result == EXIT_FAILURE)
465 {
466 cmdline_parser_free (args_info);
467 exit (EXIT_FAILURE);
468 }
469
470 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200471}
472
473int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100474cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200475{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100476 FIX_UNUSED (args_info);
477 FIX_UNUSED (prog_name);
478 return EXIT_SUCCESS;
479}
Harald Welte1b3e5772010-05-04 11:13:56 +0200480
Harald Welte1b3e5772010-05-04 11:13:56 +0200481
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100482static char *package_name = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200483
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100484/**
485 * @brief updates an option
486 * @param field the generic pointer to the field to update
487 * @param orig_field the pointer to the orig field
488 * @param field_given the pointer to the number of occurrence of this option
489 * @param prev_given the pointer to the number of occurrence already seen
490 * @param value the argument for this option (if null no arg was specified)
491 * @param possible_values the possible values for this option (if specified)
492 * @param default_value the default value (in case the option only accepts fixed values)
493 * @param arg_type the type of this option
494 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
495 * @param override @see cmdline_parser_params.override
496 * @param no_free whether to free a possible previous value
497 * @param multiple_option whether this is a multiple option
498 * @param long_opt the corresponding long option
499 * @param short_opt the corresponding short option (or '-' if none)
500 * @param additional_error possible further error specification
501 */
502static
503int update_arg(void *field, char **orig_field,
504 unsigned int *field_given, unsigned int *prev_given,
505 char *value, const char *possible_values[],
506 const char *default_value,
507 cmdline_parser_arg_type arg_type,
508 int check_ambiguity, int override,
509 int no_free, int multiple_option,
510 const char *long_opt, char short_opt,
511 const char *additional_error)
512{
513 char *stop_char = 0;
514 const char *val = value;
515 int found;
516 char **string_field;
517 FIX_UNUSED (field);
jjako52c24142002-12-16 13:33:51 +0000518
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100519 stop_char = 0;
520 found = 0;
jjako52c24142002-12-16 13:33:51 +0000521
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100522 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
523 {
524 if (short_opt != '-')
525 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
526 package_name, long_opt, short_opt,
527 (additional_error ? additional_error : ""));
528 else
529 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
530 package_name, long_opt,
531 (additional_error ? additional_error : ""));
532 return 1; /* failure */
533 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200534
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100535 FIX_UNUSED (default_value);
536
537 if (field_given && *field_given && ! override)
538 return 0;
539 if (prev_given)
540 (*prev_given)++;
541 if (field_given)
542 (*field_given)++;
543 if (possible_values)
544 val = possible_values[found];
jjako52c24142002-12-16 13:33:51 +0000545
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100546 switch(arg_type) {
547 case ARG_FLAG:
548 *((int *)field) = !*((int *)field);
549 break;
550 case ARG_INT:
551 if (val) *((int *)field) = strtol (val, &stop_char, 0);
552 break;
553 case ARG_STRING:
554 if (val) {
555 string_field = (char **)field;
556 if (!no_free && *string_field)
557 free (*string_field); /* free previous string */
558 *string_field = gengetopt_strdup (val);
559 }
560 break;
561 default:
562 break;
563 };
jjako52c24142002-12-16 13:33:51 +0000564
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100565 /* check numeric conversion */
566 switch(arg_type) {
567 case ARG_INT:
568 if (val && !(stop_char && *stop_char == '\0')) {
569 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
570 return 1; /* failure */
571 }
572 break;
573 default:
574 ;
575 };
jjako52c24142002-12-16 13:33:51 +0000576
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100577 /* store the original value */
578 switch(arg_type) {
579 case ARG_NO:
580 case ARG_FLAG:
581 break;
582 default:
583 if (value && orig_field) {
584 if (no_free) {
585 *orig_field = value;
586 } else {
587 if (*orig_field)
588 free (*orig_field); /* free previous string */
589 *orig_field = gengetopt_strdup (value);
590 }
591 }
592 };
jjako52c24142002-12-16 13:33:51 +0000593
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100594 return 0; /* OK */
595}
jjako52c24142002-12-16 13:33:51 +0000596
jjako52c24142002-12-16 13:33:51 +0000597
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100598int
599cmdline_parser_internal (
600 int argc, char **argv, struct gengetopt_args_info *args_info,
601 struct cmdline_parser_params *params, const char *additional_error)
602{
603 int c; /* Character of the parsed option. */
jjako52c24142002-12-16 13:33:51 +0000604
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100605 int error = 0;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100606 struct gengetopt_args_info local_args_info;
607
608 int override;
609 int initialize;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100610 int check_required;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100611 int check_ambiguity;
612
613 package_name = argv[0];
614
615 override = params->override;
616 initialize = params->initialize;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100617 check_required = params->check_required;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100618 check_ambiguity = params->check_ambiguity;
jjako52c24142002-12-16 13:33:51 +0000619
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100620 if (initialize)
621 cmdline_parser_init (args_info);
jjako52c24142002-12-16 13:33:51 +0000622
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100623 cmdline_parser_init (&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000624
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100625 optarg = 0;
626 optind = 0;
627 opterr = params->print_errors;
628 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000629
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100630 while (1)
631 {
632 int option_index = 0;
jjako52c24142002-12-16 13:33:51 +0000633
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100634 static struct option long_options[] = {
635 { "help", 0, NULL, 'h' },
636 { "version", 0, NULL, 'V' },
637 { "fg", 0, NULL, 'f' },
638 { "debug", 0, NULL, 'd' },
639 { "conf", 1, NULL, 'c' },
640 { "pidfile", 1, NULL, 0 },
641 { "statedir", 1, NULL, 0 },
642 { "listen", 1, NULL, 'l' },
643 { "net", 1, NULL, 'n' },
644 { "ipup", 1, NULL, 0 },
645 { "ipdown", 1, NULL, 0 },
646 { "dynip", 1, NULL, 0 },
647 { "statip", 1, NULL, 0 },
648 { "pcodns1", 1, NULL, 0 },
649 { "pcodns2", 1, NULL, 0 },
650 { "timelimit", 1, NULL, 0 },
651 { "apn", 1, NULL, 'a' },
652 { "qos", 1, NULL, 'q' },
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100653 { "logfile", 1, NULL, 0 },
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100654 { "loglevel", 1, NULL, 0 },
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100655 { "gtpnl", 1, NULL, 'g' },
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100656 { 0, 0, 0, 0 }
657 };
jjako52c24142002-12-16 13:33:51 +0000658
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100659 c = getopt_long (argc, argv, "hVfdc:l:n:a:q:g:", long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000660
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100661 if (c == -1) break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000662
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100663 switch (c)
664 {
665 case 'h': /* Print help and exit. */
666 cmdline_parser_print_help ();
667 cmdline_parser_free (&local_args_info);
668 exit (EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000669
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100670 case 'V': /* Print version and exit. */
671 cmdline_parser_print_version ();
672 cmdline_parser_free (&local_args_info);
673 exit (EXIT_SUCCESS);
Harald Welte1b3e5772010-05-04 11:13:56 +0200674
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100675 case 'f': /* Run in foreground. */
676
677
678 if (update_arg((void *)&(args_info->fg_flag), 0, &(args_info->fg_given),
679 &(local_args_info.fg_given), optarg, 0, 0, ARG_FLAG,
680 check_ambiguity, override, 1, 0, "fg", 'f',
681 additional_error))
682 goto failure;
683
684 break;
685 case 'd': /* Run in debug mode. */
686
687
688 if (update_arg((void *)&(args_info->debug_flag), 0, &(args_info->debug_given),
689 &(local_args_info.debug_given), optarg, 0, 0, ARG_FLAG,
690 check_ambiguity, override, 1, 0, "debug", 'd',
691 additional_error))
692 goto failure;
693
694 break;
695 case 'c': /* Read configuration file. */
696
697
698 if (update_arg( (void *)&(args_info->conf_arg),
699 &(args_info->conf_orig), &(args_info->conf_given),
700 &(local_args_info.conf_given), optarg, 0, "/etc/ggsn.conf", ARG_STRING,
701 check_ambiguity, override, 0, 0,
702 "conf", 'c',
703 additional_error))
704 goto failure;
705
706 break;
707 case 'l': /* Local interface. */
708
709
710 if (update_arg( (void *)&(args_info->listen_arg),
711 &(args_info->listen_orig), &(args_info->listen_given),
712 &(local_args_info.listen_given), optarg, 0, 0, ARG_STRING,
713 check_ambiguity, override, 0, 0,
714 "listen", 'l',
715 additional_error))
716 goto failure;
717
718 break;
719 case 'n': /* Network. */
720
721
722 if (update_arg( (void *)&(args_info->net_arg),
723 &(args_info->net_orig), &(args_info->net_given),
724 &(local_args_info.net_given), optarg, 0, "192.168.0.0/24", ARG_STRING,
725 check_ambiguity, override, 0, 0,
726 "net", 'n',
727 additional_error))
728 goto failure;
729
730 break;
731 case 'a': /* Access point name. */
732
733
734 if (update_arg( (void *)&(args_info->apn_arg),
735 &(args_info->apn_orig), &(args_info->apn_given),
736 &(local_args_info.apn_given), optarg, 0, "internet", ARG_STRING,
737 check_ambiguity, override, 0, 0,
738 "apn", 'a',
739 additional_error))
740 goto failure;
741
742 break;
743 case 'q': /* Requested quality of service. */
744
745
746 if (update_arg( (void *)&(args_info->qos_arg),
747 &(args_info->qos_orig), &(args_info->qos_given),
748 &(local_args_info.qos_given), optarg, 0, "0x0b921f", ARG_INT,
749 check_ambiguity, override, 0, 0,
750 "qos", 'q',
751 additional_error))
752 goto failure;
753
754 break;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100755 case 'g': /* GTP kernel support. */
756
757
758 if (update_arg( (void *)&(args_info->gtpnl_arg),
759 &(args_info->gtpnl_orig), &(args_info->gtpnl_given),
760 &(local_args_info.gtpnl_given), optarg, 0, "eth0", ARG_STRING,
761 check_ambiguity, override, 0, 0,
762 "gtpnl", 'g',
763 additional_error))
764 goto failure;
765
766 break;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100767
768 case 0: /* Long option with no short option */
769 /* Filename of process id file. */
770 if (strcmp (long_options[option_index].name, "pidfile") == 0)
771 {
772
773
774 if (update_arg( (void *)&(args_info->pidfile_arg),
775 &(args_info->pidfile_orig), &(args_info->pidfile_given),
776 &(local_args_info.pidfile_given), optarg, 0, "/var/run/ggsn.pid", ARG_STRING,
777 check_ambiguity, override, 0, 0,
778 "pidfile", '-',
779 additional_error))
780 goto failure;
781
782 }
783 /* Directory of nonvolatile data. */
784 else if (strcmp (long_options[option_index].name, "statedir") == 0)
785 {
786
787
788 if (update_arg( (void *)&(args_info->statedir_arg),
789 &(args_info->statedir_orig), &(args_info->statedir_given),
790 &(local_args_info.statedir_given), optarg, 0, "/var/lib/ggsn/", ARG_STRING,
791 check_ambiguity, override, 0, 0,
792 "statedir", '-',
793 additional_error))
794 goto failure;
795
796 }
797 /* Script to run after link-up. */
798 else if (strcmp (long_options[option_index].name, "ipup") == 0)
799 {
800
801
802 if (update_arg( (void *)&(args_info->ipup_arg),
803 &(args_info->ipup_orig), &(args_info->ipup_given),
804 &(local_args_info.ipup_given), optarg, 0, 0, ARG_STRING,
805 check_ambiguity, override, 0, 0,
806 "ipup", '-',
807 additional_error))
808 goto failure;
809
810 }
811 /* Script to run after link-down. */
812 else if (strcmp (long_options[option_index].name, "ipdown") == 0)
813 {
814
815
816 if (update_arg( (void *)&(args_info->ipdown_arg),
817 &(args_info->ipdown_orig), &(args_info->ipdown_given),
818 &(local_args_info.ipdown_given), optarg, 0, 0, ARG_STRING,
819 check_ambiguity, override, 0, 0,
820 "ipdown", '-',
821 additional_error))
822 goto failure;
823
824 }
825 /* Dynamic IP address pool. */
826 else if (strcmp (long_options[option_index].name, "dynip") == 0)
827 {
828
829
830 if (update_arg( (void *)&(args_info->dynip_arg),
831 &(args_info->dynip_orig), &(args_info->dynip_given),
832 &(local_args_info.dynip_given), optarg, 0, 0, ARG_STRING,
833 check_ambiguity, override, 0, 0,
834 "dynip", '-',
835 additional_error))
836 goto failure;
837
838 }
839 /* Static IP address pool. */
840 else if (strcmp (long_options[option_index].name, "statip") == 0)
841 {
842
843
844 if (update_arg( (void *)&(args_info->statip_arg),
845 &(args_info->statip_orig), &(args_info->statip_given),
846 &(local_args_info.statip_given), optarg, 0, 0, ARG_STRING,
847 check_ambiguity, override, 0, 0,
848 "statip", '-',
849 additional_error))
850 goto failure;
851
852 }
853 /* PCO DNS Server 1. */
854 else if (strcmp (long_options[option_index].name, "pcodns1") == 0)
855 {
856
857
858 if (update_arg( (void *)&(args_info->pcodns1_arg),
859 &(args_info->pcodns1_orig), &(args_info->pcodns1_given),
860 &(local_args_info.pcodns1_given), optarg, 0, "0.0.0.0", ARG_STRING,
861 check_ambiguity, override, 0, 0,
862 "pcodns1", '-',
863 additional_error))
864 goto failure;
865
866 }
867 /* PCO DNS Server 2. */
868 else if (strcmp (long_options[option_index].name, "pcodns2") == 0)
869 {
870
871
872 if (update_arg( (void *)&(args_info->pcodns2_arg),
873 &(args_info->pcodns2_orig), &(args_info->pcodns2_given),
874 &(local_args_info.pcodns2_given), optarg, 0, "0.0.0.0", ARG_STRING,
875 check_ambiguity, override, 0, 0,
876 "pcodns2", '-',
877 additional_error))
878 goto failure;
879
880 }
881 /* Exit after timelimit seconds. */
882 else if (strcmp (long_options[option_index].name, "timelimit") == 0)
883 {
884
885
886 if (update_arg( (void *)&(args_info->timelimit_arg),
887 &(args_info->timelimit_orig), &(args_info->timelimit_given),
888 &(local_args_info.timelimit_given), optarg, 0, "0", ARG_INT,
889 check_ambiguity, override, 0, 0,
890 "timelimit", '-',
891 additional_error))
892 goto failure;
893
894 }
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100895 /* Logfile for errors. */
896 else if (strcmp (long_options[option_index].name, "logfile") == 0)
897 {
898
899
900 if (update_arg( (void *)&(args_info->logfile_arg),
901 &(args_info->logfile_orig), &(args_info->logfile_given),
902 &(local_args_info.logfile_given), optarg, 0, 0, ARG_STRING,
903 check_ambiguity, override, 0, 0,
904 "logfile", '-',
905 additional_error))
906 goto failure;
907
908 }
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100909 /* Global log ldevel. */
910 else if (strcmp (long_options[option_index].name, "loglevel") == 0)
911 {
912
913
914 if (update_arg( (void *)&(args_info->loglevel_arg),
915 &(args_info->loglevel_orig), &(args_info->loglevel_given),
916 &(local_args_info.loglevel_given), optarg, 0, "error", ARG_STRING,
917 check_ambiguity, override, 0, 0,
918 "loglevel", '-',
919 additional_error))
920 goto failure;
921
922 }
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100923
924 break;
925 case '?': /* Invalid option. */
926 /* `getopt_long' already printed an error message. */
927 goto failure;
928
929 default: /* bug: option not considered. */
930 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
931 abort ();
932 } /* switch */
933 } /* while */
934
935
936
937
938 cmdline_parser_release (&local_args_info);
939
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100940 if ( error )
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100941 return (EXIT_FAILURE);
942
943 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200944
945failure:
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100946
947 cmdline_parser_release (&local_args_info);
948 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +0000949}
950
Harald Welte1b3e5772010-05-04 11:13:56 +0200951#ifndef CONFIG_FILE_LINE_SIZE
952#define CONFIG_FILE_LINE_SIZE 2048
953#endif
954#define ADDITIONAL_ERROR " in configuration file "
955
956#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
957/* 3 is for "--" and "=" */
958
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100959static int
960_cmdline_parser_configfile (const char *filename, int *my_argc)
961{
962 FILE* file;
963 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
964 char linebuf[CONFIG_FILE_LINE_SIZE];
965 int line_num = 0;
966 int result = 0, equal;
967 char *fopt, *farg;
968 char *str_index;
969 size_t len, next_token;
970 char delimiter;
971
972 if ((file = fopen(filename, "r")) == 0)
973 {
974 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
975 CMDLINE_PARSER_PACKAGE, filename);
976 return EXIT_FAILURE;
977 }
978
979 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
980 {
981 ++line_num;
982 my_argv[0] = '\0';
983 len = strlen(linebuf);
984 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
985 {
986 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
987 CMDLINE_PARSER_PACKAGE, filename, line_num);
988 result = EXIT_FAILURE;
989 break;
990 }
991
992 /* find first non-whitespace character in the line */
993 next_token = strspn (linebuf, " \t\r\n");
994 str_index = linebuf + next_token;
995
996 if ( str_index[0] == '\0' || str_index[0] == '#')
997 continue; /* empty line or comment line is skipped */
998
999 fopt = str_index;
1000
1001 /* truncate fopt at the end of the first non-valid character */
1002 next_token = strcspn (fopt, " \t\r\n=");
1003
1004 if (fopt[next_token] == '\0') /* the line is over */
1005 {
1006 farg = 0;
1007 equal = 0;
1008 goto noarg;
1009 }
1010
1011 /* remember if equal sign is present */
1012 equal = (fopt[next_token] == '=');
1013 fopt[next_token++] = '\0';
1014
1015 /* advance pointers to the next token after the end of fopt */
1016 next_token += strspn (fopt + next_token, " \t\r\n");
1017
1018 /* check for the presence of equal sign, and if so, skip it */
1019 if ( !equal )
1020 if ((equal = (fopt[next_token] == '=')))
1021 {
1022 next_token++;
1023 next_token += strspn (fopt + next_token, " \t\r\n");
1024 }
1025 str_index += next_token;
1026
1027 /* find argument */
1028 farg = str_index;
1029 if ( farg[0] == '\"' || farg[0] == '\'' )
1030 { /* quoted argument */
1031 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1032 if (! str_index)
1033 {
1034 fprintf
1035 (stderr,
1036 "%s:%s:%d: unterminated string in configuration file\n",
1037 CMDLINE_PARSER_PACKAGE, filename, line_num);
1038 result = EXIT_FAILURE;
1039 break;
1040 }
1041 }
1042 else
1043 { /* read up the remaining part up to a delimiter */
1044 next_token = strcspn (farg, " \t\r\n#\'\"");
1045 str_index += next_token;
1046 }
1047
1048 /* truncate farg at the delimiter and store it for further check */
1049 delimiter = *str_index, *str_index++ = '\0';
1050
1051 /* everything but comment is illegal at the end of line */
1052 if (delimiter != '\0' && delimiter != '#')
1053 {
1054 str_index += strspn(str_index, " \t\r\n");
1055 if (*str_index != '\0' && *str_index != '#')
1056 {
1057 fprintf
1058 (stderr,
1059 "%s:%s:%d: malformed string in configuration file\n",
1060 CMDLINE_PARSER_PACKAGE, filename, line_num);
1061 result = EXIT_FAILURE;
1062 break;
1063 }
1064 }
1065
1066 noarg:
1067 if (!strcmp(fopt,"include")) {
1068 if (farg && *farg) {
1069 result = _cmdline_parser_configfile(farg, my_argc);
1070 } else {
1071 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1072 CMDLINE_PARSER_PACKAGE, filename, line_num);
1073 }
1074 continue;
1075 }
1076 len = strlen(fopt);
1077 strcat (my_argv, len > 1 ? "--" : "-");
1078 strcat (my_argv, fopt);
1079 if (len > 1 && ((farg && *farg) || equal))
1080 strcat (my_argv, "=");
1081 if (farg && *farg)
1082 strcat (my_argv, farg);
1083 ++(*my_argc);
1084
1085 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1086 cmd_line_list_tmp->next = cmd_line_list;
1087 cmd_line_list = cmd_line_list_tmp;
1088 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1089 } /* while */
1090
1091 if (file)
1092 fclose(file);
1093 return result;
1094}
jjako52c24142002-12-16 13:33:51 +00001095
1096int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001097cmdline_parser_configfile (
1098 const char *filename,
1099 struct gengetopt_args_info *args_info,
1100 int override, int initialize, int check_required)
jjako52c24142002-12-16 13:33:51 +00001101{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001102 struct cmdline_parser_params params;
Harald Welte1b3e5772010-05-04 11:13:56 +02001103
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001104 params.override = override;
1105 params.initialize = initialize;
1106 params.check_required = check_required;
1107 params.check_ambiguity = 0;
1108 params.print_errors = 1;
1109
1110 return cmdline_parser_config_file (filename, args_info, &params);
1111}
jjako52c24142002-12-16 13:33:51 +00001112
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001113int
1114cmdline_parser_config_file (const char *filename,
1115 struct gengetopt_args_info *args_info,
1116 struct cmdline_parser_params *params)
1117{
1118 int i, result;
1119 int my_argc = 1;
1120 char **my_argv_arg;
1121 char *additional_error;
jjako52c24142002-12-16 13:33:51 +00001122
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001123 /* store the program name */
1124 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1125 cmd_line_list_tmp->next = cmd_line_list;
1126 cmd_line_list = cmd_line_list_tmp;
1127 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
jjako52c24142002-12-16 13:33:51 +00001128
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001129 result = _cmdline_parser_configfile(filename, &my_argc);
jjako52c24142002-12-16 13:33:51 +00001130
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001131 if (result != EXIT_FAILURE) {
1132 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1133 cmd_line_list_tmp = cmd_line_list;
Harald Welte1b3e5772010-05-04 11:13:56 +02001134
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001135 for (i = my_argc - 1; i >= 0; --i) {
1136 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1137 cmd_line_list_tmp = cmd_line_list_tmp->next;
1138 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001139
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001140 my_argv_arg[my_argc] = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001141
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001142 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1143 strcpy (additional_error, ADDITIONAL_ERROR);
1144 strcat (additional_error, filename);
1145 result =
1146 cmdline_parser_internal (my_argc, my_argv_arg, args_info,
1147 params,
1148 additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +02001149
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001150 free (additional_error);
1151 free (my_argv_arg);
1152 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001153
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001154 free_cmd_list();
1155 if (result == EXIT_FAILURE)
1156 {
1157 cmdline_parser_free (args_info);
1158 exit (EXIT_FAILURE);
1159 }
1160
1161 return result;
jjako52c24142002-12-16 13:33:51 +00001162}