blob: 37ed992e030aabe418c19dcfc58e87758875dec3 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01002 File autogenerated by gengetopt version 2.22.6
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
32const char *gengetopt_args_info_versiontext = "";
33
34const char *gengetopt_args_info_description = "";
jjako52c24142002-12-16 13:33:51 +000035
Harald Welte1b3e5772010-05-04 11:13:56 +020036const char *gengetopt_args_info_help[] = {
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010037 " -h, --help Print help and exit",
38 " -V, --version Print version and exit",
39 " -f, --fg Run in foreground (default=off)",
40 " -d, --debug Run in debug mode (default=off)",
41 " -c, --conf=STRING Read configuration file (default=`/etc/ggsn.conf')",
42 " --pidfile=STRING Filename of process id file\n (default=`/var/run/ggsn.pid')",
43 " --statedir=STRING Directory of nonvolatile data\n (default=`/var/lib/ggsn/')",
44 " -l, --listen=STRING Local interface",
45 " -n, --net=STRING Network (default=`192.168.0.0/24')",
46 " --ipup=STRING Script to run after link-up",
47 " --ipdown=STRING Script to run after link-down",
48 " --dynip=STRING Dynamic IP address pool",
49 " --statip=STRING Static IP address pool",
50 " --pcodns1=STRING PCO DNS Server 1 (default=`0.0.0.0')",
51 " --pcodns2=STRING PCO DNS Server 2 (default=`0.0.0.0')",
52 " --timelimit=INT Exit after timelimit seconds (default=`0')",
53 " -a, --apn=STRING Access point name (default=`internet')",
54 " -q, --qos=INT Requested quality of service (default=`0x0b921f')",
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010055 " --logfile=STRING Logfile for errors",
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +010056 " --loglevel=STRING Global log ldevel (default=`error')",
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010057 0
Harald Welte1b3e5772010-05-04 11:13:56 +020058};
59
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010060typedef enum {ARG_NO
61 , ARG_FLAG
62 , ARG_STRING
63 , ARG_INT
64} cmdline_parser_arg_type;
65
Harald Welte1b3e5772010-05-04 11:13:56 +020066static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010067void clear_given (struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020068static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010069void clear_args (struct gengetopt_args_info *args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +020070
71static int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010072cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
73 struct cmdline_parser_params *params, const char *additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +020074
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010075struct line_list
76{
77 char * string_arg;
78 struct line_list * next;
Harald Welte1b3e5772010-05-04 11:13:56 +020079};
80
81static struct line_list *cmd_line_list = 0;
82static struct line_list *cmd_line_list_tmp = 0;
83
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010084static void
85free_cmd_list(void)
Harald Welte1b3e5772010-05-04 11:13:56 +020086{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010087 /* free the list of a previous call */
88 if (cmd_line_list)
89 {
90 while (cmd_line_list) {
91 cmd_line_list_tmp = cmd_line_list;
92 cmd_line_list = cmd_line_list->next;
93 free (cmd_line_list_tmp->string_arg);
94 free (cmd_line_list_tmp);
95 }
96 }
jjako52c24142002-12-16 13:33:51 +000097}
98
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +010099
100static char *
101gengetopt_strdup (const char *s);
jjako52c24142002-12-16 13:33:51 +0000102
Harald Welte1b3e5772010-05-04 11:13:56 +0200103static
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100104void clear_given (struct gengetopt_args_info *args_info)
jjako52c24142002-12-16 13:33:51 +0000105{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100106 args_info->help_given = 0 ;
107 args_info->version_given = 0 ;
108 args_info->fg_given = 0 ;
109 args_info->debug_given = 0 ;
110 args_info->conf_given = 0 ;
111 args_info->pidfile_given = 0 ;
112 args_info->statedir_given = 0 ;
113 args_info->listen_given = 0 ;
114 args_info->net_given = 0 ;
115 args_info->ipup_given = 0 ;
116 args_info->ipdown_given = 0 ;
117 args_info->dynip_given = 0 ;
118 args_info->statip_given = 0 ;
119 args_info->pcodns1_given = 0 ;
120 args_info->pcodns2_given = 0 ;
121 args_info->timelimit_given = 0 ;
122 args_info->apn_given = 0 ;
123 args_info->qos_given = 0 ;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100124 args_info->logfile_given = 0 ;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100125 args_info->loglevel_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;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100166
Harald Welte1b3e5772010-05-04 11:13:56 +0200167}
168
169static
170void init_args_info(struct gengetopt_args_info *args_info)
171{
Harald Weltebed35df2011-11-02 13:06:18 +0100172
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100173
174 args_info->help_help = gengetopt_args_info_help[0] ;
175 args_info->version_help = gengetopt_args_info_help[1] ;
176 args_info->fg_help = gengetopt_args_info_help[2] ;
177 args_info->debug_help = gengetopt_args_info_help[3] ;
178 args_info->conf_help = gengetopt_args_info_help[4] ;
179 args_info->pidfile_help = gengetopt_args_info_help[5] ;
180 args_info->statedir_help = gengetopt_args_info_help[6] ;
181 args_info->listen_help = gengetopt_args_info_help[7] ;
182 args_info->net_help = gengetopt_args_info_help[8] ;
183 args_info->ipup_help = gengetopt_args_info_help[9] ;
184 args_info->ipdown_help = gengetopt_args_info_help[10] ;
185 args_info->dynip_help = gengetopt_args_info_help[11] ;
186 args_info->statip_help = gengetopt_args_info_help[12] ;
187 args_info->pcodns1_help = gengetopt_args_info_help[13] ;
188 args_info->pcodns2_help = gengetopt_args_info_help[14] ;
189 args_info->timelimit_help = gengetopt_args_info_help[15] ;
190 args_info->apn_help = gengetopt_args_info_help[16] ;
191 args_info->qos_help = gengetopt_args_info_help[17] ;
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100192 args_info->logfile_help = gengetopt_args_info_help[18] ;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100193 args_info->loglevel_help = gengetopt_args_info_help[19] ;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100194
Harald Welte1b3e5772010-05-04 11:13:56 +0200195}
196
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100197void
198cmdline_parser_print_version (void)
Harald Welte1b3e5772010-05-04 11:13:56 +0200199{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100200 printf ("%s %s\n",
201 (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
202 CMDLINE_PARSER_VERSION);
203
204 if (strlen(gengetopt_args_info_versiontext) > 0)
205 printf("\n%s\n", gengetopt_args_info_versiontext);
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));
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100307
308
Harald Weltebed35df2011-11-02 13:06:18 +0100309
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100310 clear_given (args_info);
311}
312
313
314static void
315write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
316{
317 FIX_UNUSED (values);
318 if (arg) {
319 fprintf(outfile, "%s=\"%s\"\n", opt, arg);
320 } else {
321 fprintf(outfile, "%s\n", opt);
322 }
323}
324
325
326int
327cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
328{
329 int i = 0;
330
331 if (!outfile)
332 {
333 fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
334 return EXIT_FAILURE;
335 }
336
337 if (args_info->help_given)
338 write_into_file(outfile, "help", 0, 0 );
339 if (args_info->version_given)
340 write_into_file(outfile, "version", 0, 0 );
341 if (args_info->fg_given)
342 write_into_file(outfile, "fg", 0, 0 );
343 if (args_info->debug_given)
344 write_into_file(outfile, "debug", 0, 0 );
345 if (args_info->conf_given)
346 write_into_file(outfile, "conf", args_info->conf_orig, 0);
347 if (args_info->pidfile_given)
348 write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0);
349 if (args_info->statedir_given)
350 write_into_file(outfile, "statedir", args_info->statedir_orig, 0);
351 if (args_info->listen_given)
352 write_into_file(outfile, "listen", args_info->listen_orig, 0);
353 if (args_info->net_given)
354 write_into_file(outfile, "net", args_info->net_orig, 0);
355 if (args_info->ipup_given)
356 write_into_file(outfile, "ipup", args_info->ipup_orig, 0);
357 if (args_info->ipdown_given)
358 write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0);
359 if (args_info->dynip_given)
360 write_into_file(outfile, "dynip", args_info->dynip_orig, 0);
361 if (args_info->statip_given)
362 write_into_file(outfile, "statip", args_info->statip_orig, 0);
363 if (args_info->pcodns1_given)
364 write_into_file(outfile, "pcodns1", args_info->pcodns1_orig, 0);
365 if (args_info->pcodns2_given)
366 write_into_file(outfile, "pcodns2", args_info->pcodns2_orig, 0);
367 if (args_info->timelimit_given)
368 write_into_file(outfile, "timelimit", args_info->timelimit_orig, 0);
369 if (args_info->apn_given)
370 write_into_file(outfile, "apn", args_info->apn_orig, 0);
371 if (args_info->qos_given)
372 write_into_file(outfile, "qos", args_info->qos_orig, 0);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100373 if (args_info->logfile_given)
374 write_into_file(outfile, "logfile", args_info->logfile_orig, 0);
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100375 if (args_info->loglevel_given)
376 write_into_file(outfile, "loglevel", args_info->loglevel_orig, 0);
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100377
378
379 i = EXIT_SUCCESS;
380 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200381}
382
383int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100384cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200385{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100386 FILE *outfile;
387 int i = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200388
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100389 outfile = fopen(filename, "w");
Harald Welte1b3e5772010-05-04 11:13:56 +0200390
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100391 if (!outfile)
392 {
393 fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
394 return EXIT_FAILURE;
395 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200396
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100397 i = cmdline_parser_dump(outfile, args_info);
398 fclose (outfile);
Harald Welte1b3e5772010-05-04 11:13:56 +0200399
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100400 return i;
Harald Welte1b3e5772010-05-04 11:13:56 +0200401}
402
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100403void
404cmdline_parser_free (struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200405{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100406 cmdline_parser_release (args_info);
Harald Welte1b3e5772010-05-04 11:13:56 +0200407}
408
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100409/** @brief replacement of strdup, which is not standard */
410char *
411gengetopt_strdup (const char *s)
Harald Welte1b3e5772010-05-04 11:13:56 +0200412{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100413 char *result = 0;
414 if (!s)
415 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200416
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100417 result = (char*)malloc(strlen(s) + 1);
418 if (result == (char*)0)
419 return (char*)0;
420 strcpy(result, s);
421 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200422}
423
424int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100425cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
Harald Welte1b3e5772010-05-04 11:13:56 +0200426{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100427 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
Harald Welte1b3e5772010-05-04 11:13:56 +0200428}
429
430int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100431cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
432 struct cmdline_parser_params *params)
Harald Welte1b3e5772010-05-04 11:13:56 +0200433{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100434 int result;
435 result = cmdline_parser_internal (argc, argv, args_info, params, 0);
Harald Welte1b3e5772010-05-04 11:13:56 +0200436
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100437 if (result == EXIT_FAILURE)
438 {
439 cmdline_parser_free (args_info);
440 exit (EXIT_FAILURE);
441 }
442
443 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200444}
445
446int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100447cmdline_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 +0200448{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100449 int result;
450 struct cmdline_parser_params params;
451
452 params.override = override;
453 params.initialize = initialize;
454 params.check_required = check_required;
455 params.check_ambiguity = 0;
456 params.print_errors = 1;
457
458 result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
459
460 if (result == EXIT_FAILURE)
461 {
462 cmdline_parser_free (args_info);
463 exit (EXIT_FAILURE);
464 }
465
466 return result;
Harald Welte1b3e5772010-05-04 11:13:56 +0200467}
468
469int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100470cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
Harald Welte1b3e5772010-05-04 11:13:56 +0200471{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100472 FIX_UNUSED (args_info);
473 FIX_UNUSED (prog_name);
474 return EXIT_SUCCESS;
475}
Harald Welte1b3e5772010-05-04 11:13:56 +0200476
Harald Welte1b3e5772010-05-04 11:13:56 +0200477
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100478static char *package_name = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200479
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100480/**
481 * @brief updates an option
482 * @param field the generic pointer to the field to update
483 * @param orig_field the pointer to the orig field
484 * @param field_given the pointer to the number of occurrence of this option
485 * @param prev_given the pointer to the number of occurrence already seen
486 * @param value the argument for this option (if null no arg was specified)
487 * @param possible_values the possible values for this option (if specified)
488 * @param default_value the default value (in case the option only accepts fixed values)
489 * @param arg_type the type of this option
490 * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
491 * @param override @see cmdline_parser_params.override
492 * @param no_free whether to free a possible previous value
493 * @param multiple_option whether this is a multiple option
494 * @param long_opt the corresponding long option
495 * @param short_opt the corresponding short option (or '-' if none)
496 * @param additional_error possible further error specification
497 */
498static
499int update_arg(void *field, char **orig_field,
500 unsigned int *field_given, unsigned int *prev_given,
501 char *value, const char *possible_values[],
502 const char *default_value,
503 cmdline_parser_arg_type arg_type,
504 int check_ambiguity, int override,
505 int no_free, int multiple_option,
506 const char *long_opt, char short_opt,
507 const char *additional_error)
508{
509 char *stop_char = 0;
510 const char *val = value;
511 int found;
512 char **string_field;
513 FIX_UNUSED (field);
jjako52c24142002-12-16 13:33:51 +0000514
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100515 stop_char = 0;
516 found = 0;
jjako52c24142002-12-16 13:33:51 +0000517
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100518 if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
519 {
520 if (short_opt != '-')
521 fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n",
522 package_name, long_opt, short_opt,
523 (additional_error ? additional_error : ""));
524 else
525 fprintf (stderr, "%s: `--%s' option given more than once%s\n",
526 package_name, long_opt,
527 (additional_error ? additional_error : ""));
528 return 1; /* failure */
529 }
Harald Welte1b3e5772010-05-04 11:13:56 +0200530
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100531 FIX_UNUSED (default_value);
532
533 if (field_given && *field_given && ! override)
534 return 0;
535 if (prev_given)
536 (*prev_given)++;
537 if (field_given)
538 (*field_given)++;
539 if (possible_values)
540 val = possible_values[found];
jjako52c24142002-12-16 13:33:51 +0000541
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100542 switch(arg_type) {
543 case ARG_FLAG:
544 *((int *)field) = !*((int *)field);
545 break;
546 case ARG_INT:
547 if (val) *((int *)field) = strtol (val, &stop_char, 0);
548 break;
549 case ARG_STRING:
550 if (val) {
551 string_field = (char **)field;
552 if (!no_free && *string_field)
553 free (*string_field); /* free previous string */
554 *string_field = gengetopt_strdup (val);
555 }
556 break;
557 default:
558 break;
559 };
jjako52c24142002-12-16 13:33:51 +0000560
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100561 /* check numeric conversion */
562 switch(arg_type) {
563 case ARG_INT:
564 if (val && !(stop_char && *stop_char == '\0')) {
565 fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val);
566 return 1; /* failure */
567 }
568 break;
569 default:
570 ;
571 };
jjako52c24142002-12-16 13:33:51 +0000572
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100573 /* store the original value */
574 switch(arg_type) {
575 case ARG_NO:
576 case ARG_FLAG:
577 break;
578 default:
579 if (value && orig_field) {
580 if (no_free) {
581 *orig_field = value;
582 } else {
583 if (*orig_field)
584 free (*orig_field); /* free previous string */
585 *orig_field = gengetopt_strdup (value);
586 }
587 }
588 };
jjako52c24142002-12-16 13:33:51 +0000589
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100590 return 0; /* OK */
591}
jjako52c24142002-12-16 13:33:51 +0000592
jjako52c24142002-12-16 13:33:51 +0000593
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100594int
595cmdline_parser_internal (
596 int argc, char **argv, struct gengetopt_args_info *args_info,
597 struct cmdline_parser_params *params, const char *additional_error)
598{
599 int c; /* Character of the parsed option. */
jjako52c24142002-12-16 13:33:51 +0000600
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100601 int error_occurred = 0;
602 struct gengetopt_args_info local_args_info;
603
604 int override;
605 int initialize;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100606 int check_required;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100607 int check_ambiguity;
608
609 package_name = argv[0];
610
611 override = params->override;
612 initialize = params->initialize;
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100613 check_required = params->check_required;
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100614 check_ambiguity = params->check_ambiguity;
jjako52c24142002-12-16 13:33:51 +0000615
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100616 if (initialize)
617 cmdline_parser_init (args_info);
jjako52c24142002-12-16 13:33:51 +0000618
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100619 cmdline_parser_init (&local_args_info);
jjako52c24142002-12-16 13:33:51 +0000620
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100621 optarg = 0;
622 optind = 0;
623 opterr = params->print_errors;
624 optopt = '?';
jjako52c24142002-12-16 13:33:51 +0000625
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100626 while (1)
627 {
628 int option_index = 0;
jjako52c24142002-12-16 13:33:51 +0000629
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100630 static struct option long_options[] = {
631 { "help", 0, NULL, 'h' },
632 { "version", 0, NULL, 'V' },
633 { "fg", 0, NULL, 'f' },
634 { "debug", 0, NULL, 'd' },
635 { "conf", 1, NULL, 'c' },
636 { "pidfile", 1, NULL, 0 },
637 { "statedir", 1, NULL, 0 },
638 { "listen", 1, NULL, 'l' },
639 { "net", 1, NULL, 'n' },
640 { "ipup", 1, NULL, 0 },
641 { "ipdown", 1, NULL, 0 },
642 { "dynip", 1, NULL, 0 },
643 { "statip", 1, NULL, 0 },
644 { "pcodns1", 1, NULL, 0 },
645 { "pcodns2", 1, NULL, 0 },
646 { "timelimit", 1, NULL, 0 },
647 { "apn", 1, NULL, 'a' },
648 { "qos", 1, NULL, 'q' },
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100649 { "logfile", 1, NULL, 0 },
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100650 { "loglevel", 1, NULL, 0 },
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100651 { 0, 0, 0, 0 }
652 };
jjako52c24142002-12-16 13:33:51 +0000653
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100654 c = getopt_long (argc, argv, "hVfdc:l:n:a:q:", long_options, &option_index);
jjako52c24142002-12-16 13:33:51 +0000655
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100656 if (c == -1) break; /* Exit from `while (1)' loop. */
jjako52c24142002-12-16 13:33:51 +0000657
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100658 switch (c)
659 {
660 case 'h': /* Print help and exit. */
661 cmdline_parser_print_help ();
662 cmdline_parser_free (&local_args_info);
663 exit (EXIT_SUCCESS);
jjako52c24142002-12-16 13:33:51 +0000664
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100665 case 'V': /* Print version and exit. */
666 cmdline_parser_print_version ();
667 cmdline_parser_free (&local_args_info);
668 exit (EXIT_SUCCESS);
Harald Welte1b3e5772010-05-04 11:13:56 +0200669
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100670 case 'f': /* Run in foreground. */
671
672
673 if (update_arg((void *)&(args_info->fg_flag), 0, &(args_info->fg_given),
674 &(local_args_info.fg_given), optarg, 0, 0, ARG_FLAG,
675 check_ambiguity, override, 1, 0, "fg", 'f',
676 additional_error))
677 goto failure;
678
679 break;
680 case 'd': /* Run in debug mode. */
681
682
683 if (update_arg((void *)&(args_info->debug_flag), 0, &(args_info->debug_given),
684 &(local_args_info.debug_given), optarg, 0, 0, ARG_FLAG,
685 check_ambiguity, override, 1, 0, "debug", 'd',
686 additional_error))
687 goto failure;
688
689 break;
690 case 'c': /* Read configuration file. */
691
692
693 if (update_arg( (void *)&(args_info->conf_arg),
694 &(args_info->conf_orig), &(args_info->conf_given),
695 &(local_args_info.conf_given), optarg, 0, "/etc/ggsn.conf", ARG_STRING,
696 check_ambiguity, override, 0, 0,
697 "conf", 'c',
698 additional_error))
699 goto failure;
700
701 break;
702 case 'l': /* Local interface. */
703
704
705 if (update_arg( (void *)&(args_info->listen_arg),
706 &(args_info->listen_orig), &(args_info->listen_given),
707 &(local_args_info.listen_given), optarg, 0, 0, ARG_STRING,
708 check_ambiguity, override, 0, 0,
709 "listen", 'l',
710 additional_error))
711 goto failure;
712
713 break;
714 case 'n': /* Network. */
715
716
717 if (update_arg( (void *)&(args_info->net_arg),
718 &(args_info->net_orig), &(args_info->net_given),
719 &(local_args_info.net_given), optarg, 0, "192.168.0.0/24", ARG_STRING,
720 check_ambiguity, override, 0, 0,
721 "net", 'n',
722 additional_error))
723 goto failure;
724
725 break;
726 case 'a': /* Access point name. */
727
728
729 if (update_arg( (void *)&(args_info->apn_arg),
730 &(args_info->apn_orig), &(args_info->apn_given),
731 &(local_args_info.apn_given), optarg, 0, "internet", ARG_STRING,
732 check_ambiguity, override, 0, 0,
733 "apn", 'a',
734 additional_error))
735 goto failure;
736
737 break;
738 case 'q': /* Requested quality of service. */
739
740
741 if (update_arg( (void *)&(args_info->qos_arg),
742 &(args_info->qos_orig), &(args_info->qos_given),
743 &(local_args_info.qos_given), optarg, 0, "0x0b921f", ARG_INT,
744 check_ambiguity, override, 0, 0,
745 "qos", 'q',
746 additional_error))
747 goto failure;
748
749 break;
750
751 case 0: /* Long option with no short option */
752 /* Filename of process id file. */
753 if (strcmp (long_options[option_index].name, "pidfile") == 0)
754 {
755
756
757 if (update_arg( (void *)&(args_info->pidfile_arg),
758 &(args_info->pidfile_orig), &(args_info->pidfile_given),
759 &(local_args_info.pidfile_given), optarg, 0, "/var/run/ggsn.pid", ARG_STRING,
760 check_ambiguity, override, 0, 0,
761 "pidfile", '-',
762 additional_error))
763 goto failure;
764
765 }
766 /* Directory of nonvolatile data. */
767 else if (strcmp (long_options[option_index].name, "statedir") == 0)
768 {
769
770
771 if (update_arg( (void *)&(args_info->statedir_arg),
772 &(args_info->statedir_orig), &(args_info->statedir_given),
773 &(local_args_info.statedir_given), optarg, 0, "/var/lib/ggsn/", ARG_STRING,
774 check_ambiguity, override, 0, 0,
775 "statedir", '-',
776 additional_error))
777 goto failure;
778
779 }
780 /* Script to run after link-up. */
781 else if (strcmp (long_options[option_index].name, "ipup") == 0)
782 {
783
784
785 if (update_arg( (void *)&(args_info->ipup_arg),
786 &(args_info->ipup_orig), &(args_info->ipup_given),
787 &(local_args_info.ipup_given), optarg, 0, 0, ARG_STRING,
788 check_ambiguity, override, 0, 0,
789 "ipup", '-',
790 additional_error))
791 goto failure;
792
793 }
794 /* Script to run after link-down. */
795 else if (strcmp (long_options[option_index].name, "ipdown") == 0)
796 {
797
798
799 if (update_arg( (void *)&(args_info->ipdown_arg),
800 &(args_info->ipdown_orig), &(args_info->ipdown_given),
801 &(local_args_info.ipdown_given), optarg, 0, 0, ARG_STRING,
802 check_ambiguity, override, 0, 0,
803 "ipdown", '-',
804 additional_error))
805 goto failure;
806
807 }
808 /* Dynamic IP address pool. */
809 else if (strcmp (long_options[option_index].name, "dynip") == 0)
810 {
811
812
813 if (update_arg( (void *)&(args_info->dynip_arg),
814 &(args_info->dynip_orig), &(args_info->dynip_given),
815 &(local_args_info.dynip_given), optarg, 0, 0, ARG_STRING,
816 check_ambiguity, override, 0, 0,
817 "dynip", '-',
818 additional_error))
819 goto failure;
820
821 }
822 /* Static IP address pool. */
823 else if (strcmp (long_options[option_index].name, "statip") == 0)
824 {
825
826
827 if (update_arg( (void *)&(args_info->statip_arg),
828 &(args_info->statip_orig), &(args_info->statip_given),
829 &(local_args_info.statip_given), optarg, 0, 0, ARG_STRING,
830 check_ambiguity, override, 0, 0,
831 "statip", '-',
832 additional_error))
833 goto failure;
834
835 }
836 /* PCO DNS Server 1. */
837 else if (strcmp (long_options[option_index].name, "pcodns1") == 0)
838 {
839
840
841 if (update_arg( (void *)&(args_info->pcodns1_arg),
842 &(args_info->pcodns1_orig), &(args_info->pcodns1_given),
843 &(local_args_info.pcodns1_given), optarg, 0, "0.0.0.0", ARG_STRING,
844 check_ambiguity, override, 0, 0,
845 "pcodns1", '-',
846 additional_error))
847 goto failure;
848
849 }
850 /* PCO DNS Server 2. */
851 else if (strcmp (long_options[option_index].name, "pcodns2") == 0)
852 {
853
854
855 if (update_arg( (void *)&(args_info->pcodns2_arg),
856 &(args_info->pcodns2_orig), &(args_info->pcodns2_given),
857 &(local_args_info.pcodns2_given), optarg, 0, "0.0.0.0", ARG_STRING,
858 check_ambiguity, override, 0, 0,
859 "pcodns2", '-',
860 additional_error))
861 goto failure;
862
863 }
864 /* Exit after timelimit seconds. */
865 else if (strcmp (long_options[option_index].name, "timelimit") == 0)
866 {
867
868
869 if (update_arg( (void *)&(args_info->timelimit_arg),
870 &(args_info->timelimit_orig), &(args_info->timelimit_given),
871 &(local_args_info.timelimit_given), optarg, 0, "0", ARG_INT,
872 check_ambiguity, override, 0, 0,
873 "timelimit", '-',
874 additional_error))
875 goto failure;
876
877 }
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100878 /* Logfile for errors. */
879 else if (strcmp (long_options[option_index].name, "logfile") == 0)
880 {
881
882
883 if (update_arg( (void *)&(args_info->logfile_arg),
884 &(args_info->logfile_orig), &(args_info->logfile_given),
885 &(local_args_info.logfile_given), optarg, 0, 0, ARG_STRING,
886 check_ambiguity, override, 0, 0,
887 "logfile", '-',
888 additional_error))
889 goto failure;
890
891 }
Holger Hans Peter Freytherc38bf642014-12-04 18:54:58 +0100892 /* Global log ldevel. */
893 else if (strcmp (long_options[option_index].name, "loglevel") == 0)
894 {
895
896
897 if (update_arg( (void *)&(args_info->loglevel_arg),
898 &(args_info->loglevel_orig), &(args_info->loglevel_given),
899 &(local_args_info.loglevel_given), optarg, 0, "error", ARG_STRING,
900 check_ambiguity, override, 0, 0,
901 "loglevel", '-',
902 additional_error))
903 goto failure;
904
905 }
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100906
907 break;
908 case '?': /* Invalid option. */
909 /* `getopt_long' already printed an error message. */
910 goto failure;
911
912 default: /* bug: option not considered. */
913 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
914 abort ();
915 } /* switch */
916 } /* while */
917
918
919
920
921 cmdline_parser_release (&local_args_info);
922
923 if ( error_occurred )
924 return (EXIT_FAILURE);
925
926 return 0;
Harald Welte1b3e5772010-05-04 11:13:56 +0200927
928failure:
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100929
930 cmdline_parser_release (&local_args_info);
931 return (EXIT_FAILURE);
jjako52c24142002-12-16 13:33:51 +0000932}
933
Harald Welte1b3e5772010-05-04 11:13:56 +0200934#ifndef CONFIG_FILE_LINE_SIZE
935#define CONFIG_FILE_LINE_SIZE 2048
936#endif
937#define ADDITIONAL_ERROR " in configuration file "
938
939#define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3)
940/* 3 is for "--" and "=" */
941
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +0100942static int
943_cmdline_parser_configfile (const char *filename, int *my_argc)
944{
945 FILE* file;
946 char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1];
947 char linebuf[CONFIG_FILE_LINE_SIZE];
948 int line_num = 0;
949 int result = 0, equal;
950 char *fopt, *farg;
951 char *str_index;
952 size_t len, next_token;
953 char delimiter;
954
955 if ((file = fopen(filename, "r")) == 0)
956 {
957 fprintf (stderr, "%s: Error opening configuration file '%s'\n",
958 CMDLINE_PARSER_PACKAGE, filename);
959 return EXIT_FAILURE;
960 }
961
962 while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0)
963 {
964 ++line_num;
965 my_argv[0] = '\0';
966 len = strlen(linebuf);
967 if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1))
968 {
969 fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n",
970 CMDLINE_PARSER_PACKAGE, filename, line_num);
971 result = EXIT_FAILURE;
972 break;
973 }
974
975 /* find first non-whitespace character in the line */
976 next_token = strspn (linebuf, " \t\r\n");
977 str_index = linebuf + next_token;
978
979 if ( str_index[0] == '\0' || str_index[0] == '#')
980 continue; /* empty line or comment line is skipped */
981
982 fopt = str_index;
983
984 /* truncate fopt at the end of the first non-valid character */
985 next_token = strcspn (fopt, " \t\r\n=");
986
987 if (fopt[next_token] == '\0') /* the line is over */
988 {
989 farg = 0;
990 equal = 0;
991 goto noarg;
992 }
993
994 /* remember if equal sign is present */
995 equal = (fopt[next_token] == '=');
996 fopt[next_token++] = '\0';
997
998 /* advance pointers to the next token after the end of fopt */
999 next_token += strspn (fopt + next_token, " \t\r\n");
1000
1001 /* check for the presence of equal sign, and if so, skip it */
1002 if ( !equal )
1003 if ((equal = (fopt[next_token] == '=')))
1004 {
1005 next_token++;
1006 next_token += strspn (fopt + next_token, " \t\r\n");
1007 }
1008 str_index += next_token;
1009
1010 /* find argument */
1011 farg = str_index;
1012 if ( farg[0] == '\"' || farg[0] == '\'' )
1013 { /* quoted argument */
1014 str_index = strchr (++farg, str_index[0] ); /* skip opening quote */
1015 if (! str_index)
1016 {
1017 fprintf
1018 (stderr,
1019 "%s:%s:%d: unterminated string in configuration file\n",
1020 CMDLINE_PARSER_PACKAGE, filename, line_num);
1021 result = EXIT_FAILURE;
1022 break;
1023 }
1024 }
1025 else
1026 { /* read up the remaining part up to a delimiter */
1027 next_token = strcspn (farg, " \t\r\n#\'\"");
1028 str_index += next_token;
1029 }
1030
1031 /* truncate farg at the delimiter and store it for further check */
1032 delimiter = *str_index, *str_index++ = '\0';
1033
1034 /* everything but comment is illegal at the end of line */
1035 if (delimiter != '\0' && delimiter != '#')
1036 {
1037 str_index += strspn(str_index, " \t\r\n");
1038 if (*str_index != '\0' && *str_index != '#')
1039 {
1040 fprintf
1041 (stderr,
1042 "%s:%s:%d: malformed string in configuration file\n",
1043 CMDLINE_PARSER_PACKAGE, filename, line_num);
1044 result = EXIT_FAILURE;
1045 break;
1046 }
1047 }
1048
1049 noarg:
1050 if (!strcmp(fopt,"include")) {
1051 if (farg && *farg) {
1052 result = _cmdline_parser_configfile(farg, my_argc);
1053 } else {
1054 fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n",
1055 CMDLINE_PARSER_PACKAGE, filename, line_num);
1056 }
1057 continue;
1058 }
1059 len = strlen(fopt);
1060 strcat (my_argv, len > 1 ? "--" : "-");
1061 strcat (my_argv, fopt);
1062 if (len > 1 && ((farg && *farg) || equal))
1063 strcat (my_argv, "=");
1064 if (farg && *farg)
1065 strcat (my_argv, farg);
1066 ++(*my_argc);
1067
1068 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1069 cmd_line_list_tmp->next = cmd_line_list;
1070 cmd_line_list = cmd_line_list_tmp;
1071 cmd_line_list->string_arg = gengetopt_strdup(my_argv);
1072 } /* while */
1073
1074 if (file)
1075 fclose(file);
1076 return result;
1077}
jjako52c24142002-12-16 13:33:51 +00001078
1079int
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001080cmdline_parser_configfile (
1081 const char *filename,
1082 struct gengetopt_args_info *args_info,
1083 int override, int initialize, int check_required)
jjako52c24142002-12-16 13:33:51 +00001084{
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001085 struct cmdline_parser_params params;
Harald Welte1b3e5772010-05-04 11:13:56 +02001086
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001087 params.override = override;
1088 params.initialize = initialize;
1089 params.check_required = check_required;
1090 params.check_ambiguity = 0;
1091 params.print_errors = 1;
1092
1093 return cmdline_parser_config_file (filename, args_info, &params);
1094}
jjako52c24142002-12-16 13:33:51 +00001095
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001096int
1097cmdline_parser_config_file (const char *filename,
1098 struct gengetopt_args_info *args_info,
1099 struct cmdline_parser_params *params)
1100{
1101 int i, result;
1102 int my_argc = 1;
1103 char **my_argv_arg;
1104 char *additional_error;
jjako52c24142002-12-16 13:33:51 +00001105
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001106 /* store the program name */
1107 cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list));
1108 cmd_line_list_tmp->next = cmd_line_list;
1109 cmd_line_list = cmd_line_list_tmp;
1110 cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE);
jjako52c24142002-12-16 13:33:51 +00001111
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001112 result = _cmdline_parser_configfile(filename, &my_argc);
jjako52c24142002-12-16 13:33:51 +00001113
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001114 if (result != EXIT_FAILURE) {
1115 my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *));
1116 cmd_line_list_tmp = cmd_line_list;
Harald Welte1b3e5772010-05-04 11:13:56 +02001117
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001118 for (i = my_argc - 1; i >= 0; --i) {
1119 my_argv_arg[i] = cmd_line_list_tmp->string_arg;
1120 cmd_line_list_tmp = cmd_line_list_tmp->next;
1121 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001122
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001123 my_argv_arg[my_argc] = 0;
Harald Welte1b3e5772010-05-04 11:13:56 +02001124
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001125 additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1);
1126 strcpy (additional_error, ADDITIONAL_ERROR);
1127 strcat (additional_error, filename);
1128 result =
1129 cmdline_parser_internal (my_argc, my_argv_arg, args_info,
1130 params,
1131 additional_error);
Harald Welte1b3e5772010-05-04 11:13:56 +02001132
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001133 free (additional_error);
1134 free (my_argv_arg);
1135 }
Harald Welte1b3e5772010-05-04 11:13:56 +02001136
Holger Hans Peter Freyther6c0b9c22014-03-23 10:02:14 +01001137 free_cmd_list();
1138 if (result == EXIT_FAILURE)
1139 {
1140 cmdline_parser_free (args_info);
1141 exit (EXIT_FAILURE);
1142 }
1143
1144 return result;
jjako52c24142002-12-16 13:33:51 +00001145}