jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 2 | File autogenerated by gengetopt version 2.22.6 |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3 | generated with the following command: |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 4 | gengetopt --conf-parser |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 5 | |
| 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 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 11 | /* If we use autoconf. */ |
| 12 | #ifdef HAVE_CONFIG_H |
| 13 | #include "config.h" |
| 14 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 15 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 20 | #ifndef FIX_UNUSED |
| 21 | #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */ |
| 22 | #endif |
| 23 | |
| 24 | #include <getopt.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 25 | |
| 26 | #include "cmdline.h" |
| 27 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 28 | const char *gengetopt_args_info_purpose = ""; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 29 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 30 | const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]..."; |
| 31 | |
| 32 | const char *gengetopt_args_info_versiontext = ""; |
| 33 | |
| 34 | const char *gengetopt_args_info_description = ""; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 35 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 36 | const char *gengetopt_args_info_help[] = { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 37 | " -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 Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 55 | " --logfile=STRING Logfile for errors", |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 56 | 0 |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 59 | typedef enum {ARG_NO |
| 60 | , ARG_FLAG |
| 61 | , ARG_STRING |
| 62 | , ARG_INT |
| 63 | } cmdline_parser_arg_type; |
| 64 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 65 | static |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 66 | void clear_given (struct gengetopt_args_info *args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 67 | static |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 68 | void clear_args (struct gengetopt_args_info *args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 69 | |
| 70 | static int |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 71 | cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info, |
| 72 | struct cmdline_parser_params *params, const char *additional_error); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 73 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 74 | struct line_list |
| 75 | { |
| 76 | char * string_arg; |
| 77 | struct line_list * next; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static struct line_list *cmd_line_list = 0; |
| 81 | static struct line_list *cmd_line_list_tmp = 0; |
| 82 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 83 | static void |
| 84 | free_cmd_list(void) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 85 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 86 | /* 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 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 98 | |
| 99 | static char * |
| 100 | gengetopt_strdup (const char *s); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 101 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 102 | static |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 103 | void clear_given (struct gengetopt_args_info *args_info) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 104 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 105 | 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 Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 123 | args_info->logfile_given = 0 ; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 126 | static |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 127 | void clear_args (struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 128 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 129 | FIX_UNUSED (args_info); |
| 130 | args_info->fg_flag = 0; |
| 131 | args_info->debug_flag = 0; |
| 132 | args_info->conf_arg = gengetopt_strdup ("/etc/ggsn.conf"); |
| 133 | args_info->conf_orig = NULL; |
| 134 | args_info->pidfile_arg = gengetopt_strdup ("/var/run/ggsn.pid"); |
| 135 | args_info->pidfile_orig = NULL; |
| 136 | args_info->statedir_arg = gengetopt_strdup ("/var/lib/ggsn/"); |
| 137 | args_info->statedir_orig = NULL; |
| 138 | args_info->listen_arg = NULL; |
| 139 | args_info->listen_orig = NULL; |
| 140 | args_info->net_arg = gengetopt_strdup ("192.168.0.0/24"); |
| 141 | args_info->net_orig = NULL; |
| 142 | args_info->ipup_arg = NULL; |
| 143 | args_info->ipup_orig = NULL; |
| 144 | args_info->ipdown_arg = NULL; |
| 145 | args_info->ipdown_orig = NULL; |
| 146 | args_info->dynip_arg = NULL; |
| 147 | args_info->dynip_orig = NULL; |
| 148 | args_info->statip_arg = NULL; |
| 149 | args_info->statip_orig = NULL; |
| 150 | args_info->pcodns1_arg = gengetopt_strdup ("0.0.0.0"); |
| 151 | args_info->pcodns1_orig = NULL; |
| 152 | args_info->pcodns2_arg = gengetopt_strdup ("0.0.0.0"); |
| 153 | args_info->pcodns2_orig = NULL; |
| 154 | args_info->timelimit_arg = 0; |
| 155 | args_info->timelimit_orig = NULL; |
| 156 | args_info->apn_arg = gengetopt_strdup ("internet"); |
| 157 | args_info->apn_orig = NULL; |
| 158 | args_info->qos_arg = 0x0b921f; |
| 159 | args_info->qos_orig = NULL; |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 160 | args_info->logfile_arg = NULL; |
| 161 | args_info->logfile_orig = NULL; |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 162 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static |
| 166 | void init_args_info(struct gengetopt_args_info *args_info) |
| 167 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 168 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 169 | |
| 170 | args_info->help_help = gengetopt_args_info_help[0] ; |
| 171 | args_info->version_help = gengetopt_args_info_help[1] ; |
| 172 | args_info->fg_help = gengetopt_args_info_help[2] ; |
| 173 | args_info->debug_help = gengetopt_args_info_help[3] ; |
| 174 | args_info->conf_help = gengetopt_args_info_help[4] ; |
| 175 | args_info->pidfile_help = gengetopt_args_info_help[5] ; |
| 176 | args_info->statedir_help = gengetopt_args_info_help[6] ; |
| 177 | args_info->listen_help = gengetopt_args_info_help[7] ; |
| 178 | args_info->net_help = gengetopt_args_info_help[8] ; |
| 179 | args_info->ipup_help = gengetopt_args_info_help[9] ; |
| 180 | args_info->ipdown_help = gengetopt_args_info_help[10] ; |
| 181 | args_info->dynip_help = gengetopt_args_info_help[11] ; |
| 182 | args_info->statip_help = gengetopt_args_info_help[12] ; |
| 183 | args_info->pcodns1_help = gengetopt_args_info_help[13] ; |
| 184 | args_info->pcodns2_help = gengetopt_args_info_help[14] ; |
| 185 | args_info->timelimit_help = gengetopt_args_info_help[15] ; |
| 186 | args_info->apn_help = gengetopt_args_info_help[16] ; |
| 187 | args_info->qos_help = gengetopt_args_info_help[17] ; |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 188 | args_info->logfile_help = gengetopt_args_info_help[18] ; |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 189 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 192 | void |
| 193 | cmdline_parser_print_version (void) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 194 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 195 | printf ("%s %s\n", |
| 196 | (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), |
| 197 | CMDLINE_PARSER_VERSION); |
| 198 | |
| 199 | if (strlen(gengetopt_args_info_versiontext) > 0) |
| 200 | printf("\n%s\n", gengetopt_args_info_versiontext); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 203 | static void print_help_common(void) { |
| 204 | cmdline_parser_print_version (); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 205 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 206 | if (strlen(gengetopt_args_info_purpose) > 0) |
| 207 | printf("\n%s\n", gengetopt_args_info_purpose); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 208 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 209 | if (strlen(gengetopt_args_info_usage) > 0) |
| 210 | printf("\n%s\n", gengetopt_args_info_usage); |
| 211 | |
| 212 | printf("\n"); |
| 213 | |
| 214 | if (strlen(gengetopt_args_info_description) > 0) |
| 215 | printf("%s\n\n", gengetopt_args_info_description); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 218 | void |
| 219 | cmdline_parser_print_help (void) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 220 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 221 | int i = 0; |
| 222 | print_help_common(); |
| 223 | while (gengetopt_args_info_help[i]) |
| 224 | printf("%s\n", gengetopt_args_info_help[i++]); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 227 | void |
| 228 | cmdline_parser_init (struct gengetopt_args_info *args_info) |
| 229 | { |
| 230 | clear_given (args_info); |
| 231 | clear_args (args_info); |
| 232 | init_args_info (args_info); |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | cmdline_parser_params_init(struct cmdline_parser_params *params) |
| 237 | { |
| 238 | if (params) |
| 239 | { |
| 240 | params->override = 0; |
| 241 | params->initialize = 1; |
| 242 | params->check_required = 1; |
| 243 | params->check_ambiguity = 0; |
| 244 | params->print_errors = 1; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | struct cmdline_parser_params * |
| 249 | cmdline_parser_params_create(void) |
| 250 | { |
| 251 | struct cmdline_parser_params *params = |
| 252 | (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params)); |
| 253 | cmdline_parser_params_init(params); |
| 254 | return params; |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | free_string_field (char **s) |
| 259 | { |
| 260 | if (*s) |
| 261 | { |
| 262 | free (*s); |
| 263 | *s = 0; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
| 268 | static void |
| 269 | cmdline_parser_release (struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 270 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 271 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 272 | free_string_field (&(args_info->conf_arg)); |
| 273 | free_string_field (&(args_info->conf_orig)); |
| 274 | free_string_field (&(args_info->pidfile_arg)); |
| 275 | free_string_field (&(args_info->pidfile_orig)); |
| 276 | free_string_field (&(args_info->statedir_arg)); |
| 277 | free_string_field (&(args_info->statedir_orig)); |
| 278 | free_string_field (&(args_info->listen_arg)); |
| 279 | free_string_field (&(args_info->listen_orig)); |
| 280 | free_string_field (&(args_info->net_arg)); |
| 281 | free_string_field (&(args_info->net_orig)); |
| 282 | free_string_field (&(args_info->ipup_arg)); |
| 283 | free_string_field (&(args_info->ipup_orig)); |
| 284 | free_string_field (&(args_info->ipdown_arg)); |
| 285 | free_string_field (&(args_info->ipdown_orig)); |
| 286 | free_string_field (&(args_info->dynip_arg)); |
| 287 | free_string_field (&(args_info->dynip_orig)); |
| 288 | free_string_field (&(args_info->statip_arg)); |
| 289 | free_string_field (&(args_info->statip_orig)); |
| 290 | free_string_field (&(args_info->pcodns1_arg)); |
| 291 | free_string_field (&(args_info->pcodns1_orig)); |
| 292 | free_string_field (&(args_info->pcodns2_arg)); |
| 293 | free_string_field (&(args_info->pcodns2_orig)); |
| 294 | free_string_field (&(args_info->timelimit_orig)); |
| 295 | free_string_field (&(args_info->apn_arg)); |
| 296 | free_string_field (&(args_info->apn_orig)); |
| 297 | free_string_field (&(args_info->qos_orig)); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 298 | free_string_field (&(args_info->logfile_arg)); |
| 299 | free_string_field (&(args_info->logfile_orig)); |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 300 | |
| 301 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 302 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 303 | clear_given (args_info); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | static void |
| 308 | write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) |
| 309 | { |
| 310 | FIX_UNUSED (values); |
| 311 | if (arg) { |
| 312 | fprintf(outfile, "%s=\"%s\"\n", opt, arg); |
| 313 | } else { |
| 314 | fprintf(outfile, "%s\n", opt); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | |
| 319 | int |
| 320 | cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) |
| 321 | { |
| 322 | int i = 0; |
| 323 | |
| 324 | if (!outfile) |
| 325 | { |
| 326 | fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE); |
| 327 | return EXIT_FAILURE; |
| 328 | } |
| 329 | |
| 330 | if (args_info->help_given) |
| 331 | write_into_file(outfile, "help", 0, 0 ); |
| 332 | if (args_info->version_given) |
| 333 | write_into_file(outfile, "version", 0, 0 ); |
| 334 | if (args_info->fg_given) |
| 335 | write_into_file(outfile, "fg", 0, 0 ); |
| 336 | if (args_info->debug_given) |
| 337 | write_into_file(outfile, "debug", 0, 0 ); |
| 338 | if (args_info->conf_given) |
| 339 | write_into_file(outfile, "conf", args_info->conf_orig, 0); |
| 340 | if (args_info->pidfile_given) |
| 341 | write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0); |
| 342 | if (args_info->statedir_given) |
| 343 | write_into_file(outfile, "statedir", args_info->statedir_orig, 0); |
| 344 | if (args_info->listen_given) |
| 345 | write_into_file(outfile, "listen", args_info->listen_orig, 0); |
| 346 | if (args_info->net_given) |
| 347 | write_into_file(outfile, "net", args_info->net_orig, 0); |
| 348 | if (args_info->ipup_given) |
| 349 | write_into_file(outfile, "ipup", args_info->ipup_orig, 0); |
| 350 | if (args_info->ipdown_given) |
| 351 | write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0); |
| 352 | if (args_info->dynip_given) |
| 353 | write_into_file(outfile, "dynip", args_info->dynip_orig, 0); |
| 354 | if (args_info->statip_given) |
| 355 | write_into_file(outfile, "statip", args_info->statip_orig, 0); |
| 356 | if (args_info->pcodns1_given) |
| 357 | write_into_file(outfile, "pcodns1", args_info->pcodns1_orig, 0); |
| 358 | if (args_info->pcodns2_given) |
| 359 | write_into_file(outfile, "pcodns2", args_info->pcodns2_orig, 0); |
| 360 | if (args_info->timelimit_given) |
| 361 | write_into_file(outfile, "timelimit", args_info->timelimit_orig, 0); |
| 362 | if (args_info->apn_given) |
| 363 | write_into_file(outfile, "apn", args_info->apn_orig, 0); |
| 364 | if (args_info->qos_given) |
| 365 | write_into_file(outfile, "qos", args_info->qos_orig, 0); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 366 | if (args_info->logfile_given) |
| 367 | write_into_file(outfile, "logfile", args_info->logfile_orig, 0); |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 368 | |
| 369 | |
| 370 | i = EXIT_SUCCESS; |
| 371 | return i; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | int |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 375 | cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 376 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 377 | FILE *outfile; |
| 378 | int i = 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 379 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 380 | outfile = fopen(filename, "w"); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 381 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 382 | if (!outfile) |
| 383 | { |
| 384 | fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); |
| 385 | return EXIT_FAILURE; |
| 386 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 387 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 388 | i = cmdline_parser_dump(outfile, args_info); |
| 389 | fclose (outfile); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 390 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 391 | return i; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 394 | void |
| 395 | cmdline_parser_free (struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 396 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 397 | cmdline_parser_release (args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 400 | /** @brief replacement of strdup, which is not standard */ |
| 401 | char * |
| 402 | gengetopt_strdup (const char *s) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 403 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 404 | char *result = 0; |
| 405 | if (!s) |
| 406 | return result; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 407 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 408 | result = (char*)malloc(strlen(s) + 1); |
| 409 | if (result == (char*)0) |
| 410 | return (char*)0; |
| 411 | strcpy(result, s); |
| 412 | return result; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | int |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 416 | cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 417 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 418 | return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | int |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 422 | cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info, |
| 423 | struct cmdline_parser_params *params) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 424 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 425 | int result; |
| 426 | result = cmdline_parser_internal (argc, argv, args_info, params, 0); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 427 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 428 | if (result == EXIT_FAILURE) |
| 429 | { |
| 430 | cmdline_parser_free (args_info); |
| 431 | exit (EXIT_FAILURE); |
| 432 | } |
| 433 | |
| 434 | return result; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | int |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 438 | cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 439 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 440 | int result; |
| 441 | struct cmdline_parser_params params; |
| 442 | |
| 443 | params.override = override; |
| 444 | params.initialize = initialize; |
| 445 | params.check_required = check_required; |
| 446 | params.check_ambiguity = 0; |
| 447 | params.print_errors = 1; |
| 448 | |
| 449 | result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0); |
| 450 | |
| 451 | if (result == EXIT_FAILURE) |
| 452 | { |
| 453 | cmdline_parser_free (args_info); |
| 454 | exit (EXIT_FAILURE); |
| 455 | } |
| 456 | |
| 457 | return result; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | int |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 461 | cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 462 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 463 | FIX_UNUSED (args_info); |
| 464 | FIX_UNUSED (prog_name); |
| 465 | return EXIT_SUCCESS; |
| 466 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 467 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 468 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 469 | static char *package_name = 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 470 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 471 | /** |
| 472 | * @brief updates an option |
| 473 | * @param field the generic pointer to the field to update |
| 474 | * @param orig_field the pointer to the orig field |
| 475 | * @param field_given the pointer to the number of occurrence of this option |
| 476 | * @param prev_given the pointer to the number of occurrence already seen |
| 477 | * @param value the argument for this option (if null no arg was specified) |
| 478 | * @param possible_values the possible values for this option (if specified) |
| 479 | * @param default_value the default value (in case the option only accepts fixed values) |
| 480 | * @param arg_type the type of this option |
| 481 | * @param check_ambiguity @see cmdline_parser_params.check_ambiguity |
| 482 | * @param override @see cmdline_parser_params.override |
| 483 | * @param no_free whether to free a possible previous value |
| 484 | * @param multiple_option whether this is a multiple option |
| 485 | * @param long_opt the corresponding long option |
| 486 | * @param short_opt the corresponding short option (or '-' if none) |
| 487 | * @param additional_error possible further error specification |
| 488 | */ |
| 489 | static |
| 490 | int update_arg(void *field, char **orig_field, |
| 491 | unsigned int *field_given, unsigned int *prev_given, |
| 492 | char *value, const char *possible_values[], |
| 493 | const char *default_value, |
| 494 | cmdline_parser_arg_type arg_type, |
| 495 | int check_ambiguity, int override, |
| 496 | int no_free, int multiple_option, |
| 497 | const char *long_opt, char short_opt, |
| 498 | const char *additional_error) |
| 499 | { |
| 500 | char *stop_char = 0; |
| 501 | const char *val = value; |
| 502 | int found; |
| 503 | char **string_field; |
| 504 | FIX_UNUSED (field); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 505 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 506 | stop_char = 0; |
| 507 | found = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 508 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 509 | if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) |
| 510 | { |
| 511 | if (short_opt != '-') |
| 512 | fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", |
| 513 | package_name, long_opt, short_opt, |
| 514 | (additional_error ? additional_error : "")); |
| 515 | else |
| 516 | fprintf (stderr, "%s: `--%s' option given more than once%s\n", |
| 517 | package_name, long_opt, |
| 518 | (additional_error ? additional_error : "")); |
| 519 | return 1; /* failure */ |
| 520 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 521 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 522 | FIX_UNUSED (default_value); |
| 523 | |
| 524 | if (field_given && *field_given && ! override) |
| 525 | return 0; |
| 526 | if (prev_given) |
| 527 | (*prev_given)++; |
| 528 | if (field_given) |
| 529 | (*field_given)++; |
| 530 | if (possible_values) |
| 531 | val = possible_values[found]; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 532 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 533 | switch(arg_type) { |
| 534 | case ARG_FLAG: |
| 535 | *((int *)field) = !*((int *)field); |
| 536 | break; |
| 537 | case ARG_INT: |
| 538 | if (val) *((int *)field) = strtol (val, &stop_char, 0); |
| 539 | break; |
| 540 | case ARG_STRING: |
| 541 | if (val) { |
| 542 | string_field = (char **)field; |
| 543 | if (!no_free && *string_field) |
| 544 | free (*string_field); /* free previous string */ |
| 545 | *string_field = gengetopt_strdup (val); |
| 546 | } |
| 547 | break; |
| 548 | default: |
| 549 | break; |
| 550 | }; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 551 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 552 | /* check numeric conversion */ |
| 553 | switch(arg_type) { |
| 554 | case ARG_INT: |
| 555 | if (val && !(stop_char && *stop_char == '\0')) { |
| 556 | fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); |
| 557 | return 1; /* failure */ |
| 558 | } |
| 559 | break; |
| 560 | default: |
| 561 | ; |
| 562 | }; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 563 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 564 | /* store the original value */ |
| 565 | switch(arg_type) { |
| 566 | case ARG_NO: |
| 567 | case ARG_FLAG: |
| 568 | break; |
| 569 | default: |
| 570 | if (value && orig_field) { |
| 571 | if (no_free) { |
| 572 | *orig_field = value; |
| 573 | } else { |
| 574 | if (*orig_field) |
| 575 | free (*orig_field); /* free previous string */ |
| 576 | *orig_field = gengetopt_strdup (value); |
| 577 | } |
| 578 | } |
| 579 | }; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 580 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 581 | return 0; /* OK */ |
| 582 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 583 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 584 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 585 | int |
| 586 | cmdline_parser_internal ( |
| 587 | int argc, char **argv, struct gengetopt_args_info *args_info, |
| 588 | struct cmdline_parser_params *params, const char *additional_error) |
| 589 | { |
| 590 | int c; /* Character of the parsed option. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 591 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 592 | int error_occurred = 0; |
| 593 | struct gengetopt_args_info local_args_info; |
| 594 | |
| 595 | int override; |
| 596 | int initialize; |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 597 | int check_ambiguity; |
| 598 | |
| 599 | package_name = argv[0]; |
| 600 | |
| 601 | override = params->override; |
| 602 | initialize = params->initialize; |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 603 | check_ambiguity = params->check_ambiguity; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 604 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 605 | if (initialize) |
| 606 | cmdline_parser_init (args_info); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 607 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 608 | cmdline_parser_init (&local_args_info); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 609 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 610 | optarg = 0; |
| 611 | optind = 0; |
| 612 | opterr = params->print_errors; |
| 613 | optopt = '?'; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 614 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 615 | while (1) |
| 616 | { |
| 617 | int option_index = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 618 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 619 | static struct option long_options[] = { |
| 620 | { "help", 0, NULL, 'h' }, |
| 621 | { "version", 0, NULL, 'V' }, |
| 622 | { "fg", 0, NULL, 'f' }, |
| 623 | { "debug", 0, NULL, 'd' }, |
| 624 | { "conf", 1, NULL, 'c' }, |
| 625 | { "pidfile", 1, NULL, 0 }, |
| 626 | { "statedir", 1, NULL, 0 }, |
| 627 | { "listen", 1, NULL, 'l' }, |
| 628 | { "net", 1, NULL, 'n' }, |
| 629 | { "ipup", 1, NULL, 0 }, |
| 630 | { "ipdown", 1, NULL, 0 }, |
| 631 | { "dynip", 1, NULL, 0 }, |
| 632 | { "statip", 1, NULL, 0 }, |
| 633 | { "pcodns1", 1, NULL, 0 }, |
| 634 | { "pcodns2", 1, NULL, 0 }, |
| 635 | { "timelimit", 1, NULL, 0 }, |
| 636 | { "apn", 1, NULL, 'a' }, |
| 637 | { "qos", 1, NULL, 'q' }, |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 638 | { "logfile", 1, NULL, 0 }, |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 639 | { 0, 0, 0, 0 } |
| 640 | }; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 641 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 642 | c = getopt_long (argc, argv, "hVfdc:l:n:a:q:", long_options, &option_index); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 643 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 644 | if (c == -1) break; /* Exit from `while (1)' loop. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 645 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 646 | switch (c) |
| 647 | { |
| 648 | case 'h': /* Print help and exit. */ |
| 649 | cmdline_parser_print_help (); |
| 650 | cmdline_parser_free (&local_args_info); |
| 651 | exit (EXIT_SUCCESS); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 652 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 653 | case 'V': /* Print version and exit. */ |
| 654 | cmdline_parser_print_version (); |
| 655 | cmdline_parser_free (&local_args_info); |
| 656 | exit (EXIT_SUCCESS); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 657 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 658 | case 'f': /* Run in foreground. */ |
| 659 | |
| 660 | |
| 661 | if (update_arg((void *)&(args_info->fg_flag), 0, &(args_info->fg_given), |
| 662 | &(local_args_info.fg_given), optarg, 0, 0, ARG_FLAG, |
| 663 | check_ambiguity, override, 1, 0, "fg", 'f', |
| 664 | additional_error)) |
| 665 | goto failure; |
| 666 | |
| 667 | break; |
| 668 | case 'd': /* Run in debug mode. */ |
| 669 | |
| 670 | |
| 671 | if (update_arg((void *)&(args_info->debug_flag), 0, &(args_info->debug_given), |
| 672 | &(local_args_info.debug_given), optarg, 0, 0, ARG_FLAG, |
| 673 | check_ambiguity, override, 1, 0, "debug", 'd', |
| 674 | additional_error)) |
| 675 | goto failure; |
| 676 | |
| 677 | break; |
| 678 | case 'c': /* Read configuration file. */ |
| 679 | |
| 680 | |
| 681 | if (update_arg( (void *)&(args_info->conf_arg), |
| 682 | &(args_info->conf_orig), &(args_info->conf_given), |
| 683 | &(local_args_info.conf_given), optarg, 0, "/etc/ggsn.conf", ARG_STRING, |
| 684 | check_ambiguity, override, 0, 0, |
| 685 | "conf", 'c', |
| 686 | additional_error)) |
| 687 | goto failure; |
| 688 | |
| 689 | break; |
| 690 | case 'l': /* Local interface. */ |
| 691 | |
| 692 | |
| 693 | if (update_arg( (void *)&(args_info->listen_arg), |
| 694 | &(args_info->listen_orig), &(args_info->listen_given), |
| 695 | &(local_args_info.listen_given), optarg, 0, 0, ARG_STRING, |
| 696 | check_ambiguity, override, 0, 0, |
| 697 | "listen", 'l', |
| 698 | additional_error)) |
| 699 | goto failure; |
| 700 | |
| 701 | break; |
| 702 | case 'n': /* Network. */ |
| 703 | |
| 704 | |
| 705 | if (update_arg( (void *)&(args_info->net_arg), |
| 706 | &(args_info->net_orig), &(args_info->net_given), |
| 707 | &(local_args_info.net_given), optarg, 0, "192.168.0.0/24", ARG_STRING, |
| 708 | check_ambiguity, override, 0, 0, |
| 709 | "net", 'n', |
| 710 | additional_error)) |
| 711 | goto failure; |
| 712 | |
| 713 | break; |
| 714 | case 'a': /* Access point name. */ |
| 715 | |
| 716 | |
| 717 | if (update_arg( (void *)&(args_info->apn_arg), |
| 718 | &(args_info->apn_orig), &(args_info->apn_given), |
| 719 | &(local_args_info.apn_given), optarg, 0, "internet", ARG_STRING, |
| 720 | check_ambiguity, override, 0, 0, |
| 721 | "apn", 'a', |
| 722 | additional_error)) |
| 723 | goto failure; |
| 724 | |
| 725 | break; |
| 726 | case 'q': /* Requested quality of service. */ |
| 727 | |
| 728 | |
| 729 | if (update_arg( (void *)&(args_info->qos_arg), |
| 730 | &(args_info->qos_orig), &(args_info->qos_given), |
| 731 | &(local_args_info.qos_given), optarg, 0, "0x0b921f", ARG_INT, |
| 732 | check_ambiguity, override, 0, 0, |
| 733 | "qos", 'q', |
| 734 | additional_error)) |
| 735 | goto failure; |
| 736 | |
| 737 | break; |
| 738 | |
| 739 | case 0: /* Long option with no short option */ |
| 740 | /* Filename of process id file. */ |
| 741 | if (strcmp (long_options[option_index].name, "pidfile") == 0) |
| 742 | { |
| 743 | |
| 744 | |
| 745 | if (update_arg( (void *)&(args_info->pidfile_arg), |
| 746 | &(args_info->pidfile_orig), &(args_info->pidfile_given), |
| 747 | &(local_args_info.pidfile_given), optarg, 0, "/var/run/ggsn.pid", ARG_STRING, |
| 748 | check_ambiguity, override, 0, 0, |
| 749 | "pidfile", '-', |
| 750 | additional_error)) |
| 751 | goto failure; |
| 752 | |
| 753 | } |
| 754 | /* Directory of nonvolatile data. */ |
| 755 | else if (strcmp (long_options[option_index].name, "statedir") == 0) |
| 756 | { |
| 757 | |
| 758 | |
| 759 | if (update_arg( (void *)&(args_info->statedir_arg), |
| 760 | &(args_info->statedir_orig), &(args_info->statedir_given), |
| 761 | &(local_args_info.statedir_given), optarg, 0, "/var/lib/ggsn/", ARG_STRING, |
| 762 | check_ambiguity, override, 0, 0, |
| 763 | "statedir", '-', |
| 764 | additional_error)) |
| 765 | goto failure; |
| 766 | |
| 767 | } |
| 768 | /* Script to run after link-up. */ |
| 769 | else if (strcmp (long_options[option_index].name, "ipup") == 0) |
| 770 | { |
| 771 | |
| 772 | |
| 773 | if (update_arg( (void *)&(args_info->ipup_arg), |
| 774 | &(args_info->ipup_orig), &(args_info->ipup_given), |
| 775 | &(local_args_info.ipup_given), optarg, 0, 0, ARG_STRING, |
| 776 | check_ambiguity, override, 0, 0, |
| 777 | "ipup", '-', |
| 778 | additional_error)) |
| 779 | goto failure; |
| 780 | |
| 781 | } |
| 782 | /* Script to run after link-down. */ |
| 783 | else if (strcmp (long_options[option_index].name, "ipdown") == 0) |
| 784 | { |
| 785 | |
| 786 | |
| 787 | if (update_arg( (void *)&(args_info->ipdown_arg), |
| 788 | &(args_info->ipdown_orig), &(args_info->ipdown_given), |
| 789 | &(local_args_info.ipdown_given), optarg, 0, 0, ARG_STRING, |
| 790 | check_ambiguity, override, 0, 0, |
| 791 | "ipdown", '-', |
| 792 | additional_error)) |
| 793 | goto failure; |
| 794 | |
| 795 | } |
| 796 | /* Dynamic IP address pool. */ |
| 797 | else if (strcmp (long_options[option_index].name, "dynip") == 0) |
| 798 | { |
| 799 | |
| 800 | |
| 801 | if (update_arg( (void *)&(args_info->dynip_arg), |
| 802 | &(args_info->dynip_orig), &(args_info->dynip_given), |
| 803 | &(local_args_info.dynip_given), optarg, 0, 0, ARG_STRING, |
| 804 | check_ambiguity, override, 0, 0, |
| 805 | "dynip", '-', |
| 806 | additional_error)) |
| 807 | goto failure; |
| 808 | |
| 809 | } |
| 810 | /* Static IP address pool. */ |
| 811 | else if (strcmp (long_options[option_index].name, "statip") == 0) |
| 812 | { |
| 813 | |
| 814 | |
| 815 | if (update_arg( (void *)&(args_info->statip_arg), |
| 816 | &(args_info->statip_orig), &(args_info->statip_given), |
| 817 | &(local_args_info.statip_given), optarg, 0, 0, ARG_STRING, |
| 818 | check_ambiguity, override, 0, 0, |
| 819 | "statip", '-', |
| 820 | additional_error)) |
| 821 | goto failure; |
| 822 | |
| 823 | } |
| 824 | /* PCO DNS Server 1. */ |
| 825 | else if (strcmp (long_options[option_index].name, "pcodns1") == 0) |
| 826 | { |
| 827 | |
| 828 | |
| 829 | if (update_arg( (void *)&(args_info->pcodns1_arg), |
| 830 | &(args_info->pcodns1_orig), &(args_info->pcodns1_given), |
| 831 | &(local_args_info.pcodns1_given), optarg, 0, "0.0.0.0", ARG_STRING, |
| 832 | check_ambiguity, override, 0, 0, |
| 833 | "pcodns1", '-', |
| 834 | additional_error)) |
| 835 | goto failure; |
| 836 | |
| 837 | } |
| 838 | /* PCO DNS Server 2. */ |
| 839 | else if (strcmp (long_options[option_index].name, "pcodns2") == 0) |
| 840 | { |
| 841 | |
| 842 | |
| 843 | if (update_arg( (void *)&(args_info->pcodns2_arg), |
| 844 | &(args_info->pcodns2_orig), &(args_info->pcodns2_given), |
| 845 | &(local_args_info.pcodns2_given), optarg, 0, "0.0.0.0", ARG_STRING, |
| 846 | check_ambiguity, override, 0, 0, |
| 847 | "pcodns2", '-', |
| 848 | additional_error)) |
| 849 | goto failure; |
| 850 | |
| 851 | } |
| 852 | /* Exit after timelimit seconds. */ |
| 853 | else if (strcmp (long_options[option_index].name, "timelimit") == 0) |
| 854 | { |
| 855 | |
| 856 | |
| 857 | if (update_arg( (void *)&(args_info->timelimit_arg), |
| 858 | &(args_info->timelimit_orig), &(args_info->timelimit_given), |
| 859 | &(local_args_info.timelimit_given), optarg, 0, "0", ARG_INT, |
| 860 | check_ambiguity, override, 0, 0, |
| 861 | "timelimit", '-', |
| 862 | additional_error)) |
| 863 | goto failure; |
| 864 | |
| 865 | } |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 866 | /* Logfile for errors. */ |
| 867 | else if (strcmp (long_options[option_index].name, "logfile") == 0) |
| 868 | { |
| 869 | |
| 870 | |
| 871 | if (update_arg( (void *)&(args_info->logfile_arg), |
| 872 | &(args_info->logfile_orig), &(args_info->logfile_given), |
| 873 | &(local_args_info.logfile_given), optarg, 0, 0, ARG_STRING, |
| 874 | check_ambiguity, override, 0, 0, |
| 875 | "logfile", '-', |
| 876 | additional_error)) |
| 877 | goto failure; |
| 878 | |
| 879 | } |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 880 | |
| 881 | break; |
| 882 | case '?': /* Invalid option. */ |
| 883 | /* `getopt_long' already printed an error message. */ |
| 884 | goto failure; |
| 885 | |
| 886 | default: /* bug: option not considered. */ |
| 887 | fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); |
| 888 | abort (); |
| 889 | } /* switch */ |
| 890 | } /* while */ |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | cmdline_parser_release (&local_args_info); |
| 896 | |
| 897 | if ( error_occurred ) |
| 898 | return (EXIT_FAILURE); |
| 899 | |
| 900 | return 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 901 | |
| 902 | failure: |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 903 | |
| 904 | cmdline_parser_release (&local_args_info); |
| 905 | return (EXIT_FAILURE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 908 | #ifndef CONFIG_FILE_LINE_SIZE |
| 909 | #define CONFIG_FILE_LINE_SIZE 2048 |
| 910 | #endif |
| 911 | #define ADDITIONAL_ERROR " in configuration file " |
| 912 | |
| 913 | #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3) |
| 914 | /* 3 is for "--" and "=" */ |
| 915 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 916 | static int |
| 917 | _cmdline_parser_configfile (const char *filename, int *my_argc) |
| 918 | { |
| 919 | FILE* file; |
| 920 | char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE+1]; |
| 921 | char linebuf[CONFIG_FILE_LINE_SIZE]; |
| 922 | int line_num = 0; |
| 923 | int result = 0, equal; |
| 924 | char *fopt, *farg; |
| 925 | char *str_index; |
| 926 | size_t len, next_token; |
| 927 | char delimiter; |
| 928 | |
| 929 | if ((file = fopen(filename, "r")) == 0) |
| 930 | { |
| 931 | fprintf (stderr, "%s: Error opening configuration file '%s'\n", |
| 932 | CMDLINE_PARSER_PACKAGE, filename); |
| 933 | return EXIT_FAILURE; |
| 934 | } |
| 935 | |
| 936 | while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) |
| 937 | { |
| 938 | ++line_num; |
| 939 | my_argv[0] = '\0'; |
| 940 | len = strlen(linebuf); |
| 941 | if (len > (CONFIG_FILE_LINE_BUFFER_SIZE-1)) |
| 942 | { |
| 943 | fprintf (stderr, "%s:%s:%d: Line too long in configuration file\n", |
| 944 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 945 | result = EXIT_FAILURE; |
| 946 | break; |
| 947 | } |
| 948 | |
| 949 | /* find first non-whitespace character in the line */ |
| 950 | next_token = strspn (linebuf, " \t\r\n"); |
| 951 | str_index = linebuf + next_token; |
| 952 | |
| 953 | if ( str_index[0] == '\0' || str_index[0] == '#') |
| 954 | continue; /* empty line or comment line is skipped */ |
| 955 | |
| 956 | fopt = str_index; |
| 957 | |
| 958 | /* truncate fopt at the end of the first non-valid character */ |
| 959 | next_token = strcspn (fopt, " \t\r\n="); |
| 960 | |
| 961 | if (fopt[next_token] == '\0') /* the line is over */ |
| 962 | { |
| 963 | farg = 0; |
| 964 | equal = 0; |
| 965 | goto noarg; |
| 966 | } |
| 967 | |
| 968 | /* remember if equal sign is present */ |
| 969 | equal = (fopt[next_token] == '='); |
| 970 | fopt[next_token++] = '\0'; |
| 971 | |
| 972 | /* advance pointers to the next token after the end of fopt */ |
| 973 | next_token += strspn (fopt + next_token, " \t\r\n"); |
| 974 | |
| 975 | /* check for the presence of equal sign, and if so, skip it */ |
| 976 | if ( !equal ) |
| 977 | if ((equal = (fopt[next_token] == '='))) |
| 978 | { |
| 979 | next_token++; |
| 980 | next_token += strspn (fopt + next_token, " \t\r\n"); |
| 981 | } |
| 982 | str_index += next_token; |
| 983 | |
| 984 | /* find argument */ |
| 985 | farg = str_index; |
| 986 | if ( farg[0] == '\"' || farg[0] == '\'' ) |
| 987 | { /* quoted argument */ |
| 988 | str_index = strchr (++farg, str_index[0] ); /* skip opening quote */ |
| 989 | if (! str_index) |
| 990 | { |
| 991 | fprintf |
| 992 | (stderr, |
| 993 | "%s:%s:%d: unterminated string in configuration file\n", |
| 994 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 995 | result = EXIT_FAILURE; |
| 996 | break; |
| 997 | } |
| 998 | } |
| 999 | else |
| 1000 | { /* read up the remaining part up to a delimiter */ |
| 1001 | next_token = strcspn (farg, " \t\r\n#\'\""); |
| 1002 | str_index += next_token; |
| 1003 | } |
| 1004 | |
| 1005 | /* truncate farg at the delimiter and store it for further check */ |
| 1006 | delimiter = *str_index, *str_index++ = '\0'; |
| 1007 | |
| 1008 | /* everything but comment is illegal at the end of line */ |
| 1009 | if (delimiter != '\0' && delimiter != '#') |
| 1010 | { |
| 1011 | str_index += strspn(str_index, " \t\r\n"); |
| 1012 | if (*str_index != '\0' && *str_index != '#') |
| 1013 | { |
| 1014 | fprintf |
| 1015 | (stderr, |
| 1016 | "%s:%s:%d: malformed string in configuration file\n", |
| 1017 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 1018 | result = EXIT_FAILURE; |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | noarg: |
| 1024 | if (!strcmp(fopt,"include")) { |
| 1025 | if (farg && *farg) { |
| 1026 | result = _cmdline_parser_configfile(farg, my_argc); |
| 1027 | } else { |
| 1028 | fprintf(stderr, "%s:%s:%d: include requires a filename argument.\n", |
| 1029 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 1030 | } |
| 1031 | continue; |
| 1032 | } |
| 1033 | len = strlen(fopt); |
| 1034 | strcat (my_argv, len > 1 ? "--" : "-"); |
| 1035 | strcat (my_argv, fopt); |
| 1036 | if (len > 1 && ((farg && *farg) || equal)) |
| 1037 | strcat (my_argv, "="); |
| 1038 | if (farg && *farg) |
| 1039 | strcat (my_argv, farg); |
| 1040 | ++(*my_argc); |
| 1041 | |
| 1042 | cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); |
| 1043 | cmd_line_list_tmp->next = cmd_line_list; |
| 1044 | cmd_line_list = cmd_line_list_tmp; |
| 1045 | cmd_line_list->string_arg = gengetopt_strdup(my_argv); |
| 1046 | } /* while */ |
| 1047 | |
| 1048 | if (file) |
| 1049 | fclose(file); |
| 1050 | return result; |
| 1051 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1052 | |
| 1053 | int |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1054 | cmdline_parser_configfile ( |
| 1055 | const char *filename, |
| 1056 | struct gengetopt_args_info *args_info, |
| 1057 | int override, int initialize, int check_required) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1058 | { |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1059 | struct cmdline_parser_params params; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1060 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1061 | params.override = override; |
| 1062 | params.initialize = initialize; |
| 1063 | params.check_required = check_required; |
| 1064 | params.check_ambiguity = 0; |
| 1065 | params.print_errors = 1; |
| 1066 | |
| 1067 | return cmdline_parser_config_file (filename, args_info, ¶ms); |
| 1068 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1069 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1070 | int |
| 1071 | cmdline_parser_config_file (const char *filename, |
| 1072 | struct gengetopt_args_info *args_info, |
| 1073 | struct cmdline_parser_params *params) |
| 1074 | { |
| 1075 | int i, result; |
| 1076 | int my_argc = 1; |
| 1077 | char **my_argv_arg; |
| 1078 | char *additional_error; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1079 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1080 | /* store the program name */ |
| 1081 | cmd_line_list_tmp = (struct line_list *) malloc (sizeof (struct line_list)); |
| 1082 | cmd_line_list_tmp->next = cmd_line_list; |
| 1083 | cmd_line_list = cmd_line_list_tmp; |
| 1084 | cmd_line_list->string_arg = gengetopt_strdup (CMDLINE_PARSER_PACKAGE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1085 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1086 | result = _cmdline_parser_configfile(filename, &my_argc); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1087 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1088 | if (result != EXIT_FAILURE) { |
| 1089 | my_argv_arg = (char **) malloc((my_argc+1) * sizeof(char *)); |
| 1090 | cmd_line_list_tmp = cmd_line_list; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1091 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1092 | for (i = my_argc - 1; i >= 0; --i) { |
| 1093 | my_argv_arg[i] = cmd_line_list_tmp->string_arg; |
| 1094 | cmd_line_list_tmp = cmd_line_list_tmp->next; |
| 1095 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1096 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1097 | my_argv_arg[my_argc] = 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1098 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1099 | additional_error = (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + 1); |
| 1100 | strcpy (additional_error, ADDITIONAL_ERROR); |
| 1101 | strcat (additional_error, filename); |
| 1102 | result = |
| 1103 | cmdline_parser_internal (my_argc, my_argv_arg, args_info, |
| 1104 | params, |
| 1105 | additional_error); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1106 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1107 | free (additional_error); |
| 1108 | free (my_argv_arg); |
| 1109 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1110 | |
Holger Hans Peter Freyther | 6c0b9c2 | 2014-03-23 10:02:14 +0100 | [diff] [blame] | 1111 | free_cmd_list(); |
| 1112 | if (result == EXIT_FAILURE) |
| 1113 | { |
| 1114 | cmdline_parser_free (args_info); |
| 1115 | exit (EXIT_FAILURE); |
| 1116 | } |
| 1117 | |
| 1118 | return result; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1119 | } |