jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1 | /* |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [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 | 1a51df7 | 2004-07-20 08:30:21 +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 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [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 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 30 | const char *gengetopt_args_info_usage = |
| 31 | "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]..."; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 32 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 33 | const char *gengetopt_args_info_versiontext = ""; |
| 34 | |
| 35 | const char *gengetopt_args_info_description = ""; |
| 36 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 37 | const char *gengetopt_args_info_help[] = { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 38 | " -h, --help Print help and exit", |
| 39 | " -V, --version Print version and exit", |
| 40 | " -d, --debug Run in debug mode (default=off)", |
| 41 | " -c, --conf=STRING Read configuration file", |
| 42 | " --pidfile=STRING Filename of process id file\n (default=`./sgsnemu.pid')", |
| 43 | " --statedir=STRING Directory of nonvolatile data (default=`./')", |
| 44 | " --dns=STRING DNS Server to use", |
| 45 | " -l, --listen=STRING Local interface", |
| 46 | " -r, --remote=STRING Remote host", |
| 47 | " --contexts=INT Number of contexts (default=`1')", |
| 48 | " --timelimit=INT Exit after timelimit seconds (default=`0')", |
| 49 | " --gtpversion=INT GTP version to use (default=`1')", |
| 50 | " -a, --apn=STRING Access point name (default=`internet')", |
| 51 | " --selmode=INT Selection mode (default=`0x01')", |
| 52 | " --rattype=1..5 Radio Access Technology Type (default=`1')", |
| 53 | " --userloc=type.MCC.MNC.LAC.CIorSACorRAC\n User Location Information\n (default=`02509946241207')", |
| 54 | " --rai=MCC.MNC.LAC.RAC Routing Area Information\n (default=`02509946241207')", |
| 55 | " --mstz=sign.NbQuartersOfAnHour.DSTAdjustment\n MS Time Zone (default=`0')", |
| 56 | " --imeisv=STRING IMEI(SV) International Mobile Equipment\n Identity (and Software Version)\n (default=`2143658709214365')", |
| 57 | " --norecovery Do not send recovery (default=off)", |
| 58 | " -i, --imsi=STRING IMSI (default=`240010123456789')", |
| 59 | " --nsapi=INT NSAPI (default=`0')", |
| 60 | " -m, --msisdn=STRING Mobile Station ISDN number\n (default=`46702123456')", |
| 61 | " -q, --qos=INT Requested quality of service\n (default=`0x000b921f')", |
| 62 | " --qose1=INT Requested quality of service Extension 1\n (default=`0x9396404074f9ffff')", |
| 63 | " --qose2=INT Requested quality of service Extension 2\n (default=`0x11')", |
| 64 | " --qose3=INT Requested quality of service Extension 3\n (default=`0x0101')", |
| 65 | " --qose4=INT Requested quality of service Extension 4\n (default=`0x4040')", |
| 66 | " --charging=INT Charging characteristics (default=`0x0800')", |
| 67 | " -u, --uid=STRING Login user ID (default=`mig')", |
| 68 | " -p, --pwd=STRING Login password (default=`hemmelig')", |
| 69 | " --createif Create local network interface (default=off)", |
| 70 | " -n, --net=STRING Network address for local interface", |
| 71 | " --defaultroute Create default route (default=off)", |
| 72 | " --ipup=STRING Script to run after link-up", |
| 73 | " --ipdown=STRING Script to run after link-down", |
| 74 | " --pinghost=STRING Ping remote host", |
| 75 | " --pingrate=INT Number of ping req per second (default=`1')", |
| 76 | " --pingsize=INT Number of ping data bytes (default=`56')", |
| 77 | " --pingcount=INT Number of ping req to send (default=`0')", |
| 78 | " --pingquiet Do not print ping packet info (default=off)", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 79 | 0 |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 82 | typedef enum { ARG_NO, ARG_FLAG, ARG_STRING, ARG_INT |
| 83 | } cmdline_parser_arg_type; |
| 84 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 85 | static |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 86 | void clear_given(struct gengetopt_args_info *args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 87 | static |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 88 | void clear_args(struct gengetopt_args_info *args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 89 | |
| 90 | static int |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 91 | cmdline_parser_internal(int argc, char **argv, |
| 92 | struct gengetopt_args_info *args_info, |
| 93 | struct cmdline_parser_params *params, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 94 | const char *additional_error); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 95 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 96 | struct line_list { |
| 97 | char *string_arg; |
| 98 | struct line_list *next; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static struct line_list *cmd_line_list = 0; |
| 102 | static struct line_list *cmd_line_list_tmp = 0; |
| 103 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 104 | static void free_cmd_list(void) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 105 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 106 | /* free the list of a previous call */ |
| 107 | if (cmd_line_list) { |
| 108 | while (cmd_line_list) { |
| 109 | cmd_line_list_tmp = cmd_line_list; |
| 110 | cmd_line_list = cmd_line_list->next; |
| 111 | free(cmd_line_list_tmp->string_arg); |
| 112 | free(cmd_line_list_tmp); |
| 113 | } |
| 114 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 117 | static char *gengetopt_strdup(const char *s); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 118 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 119 | static |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 120 | void clear_given(struct gengetopt_args_info *args_info) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 121 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 122 | args_info->help_given = 0; |
| 123 | args_info->version_given = 0; |
| 124 | args_info->debug_given = 0; |
| 125 | args_info->conf_given = 0; |
| 126 | args_info->pidfile_given = 0; |
| 127 | args_info->statedir_given = 0; |
| 128 | args_info->dns_given = 0; |
| 129 | args_info->listen_given = 0; |
| 130 | args_info->remote_given = 0; |
| 131 | args_info->contexts_given = 0; |
| 132 | args_info->timelimit_given = 0; |
| 133 | args_info->gtpversion_given = 0; |
| 134 | args_info->apn_given = 0; |
| 135 | args_info->selmode_given = 0; |
| 136 | args_info->rattype_given = 0; |
| 137 | args_info->userloc_given = 0; |
| 138 | args_info->rai_given = 0; |
| 139 | args_info->mstz_given = 0; |
| 140 | args_info->imeisv_given = 0; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 141 | args_info->norecovery_given = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 142 | args_info->imsi_given = 0; |
| 143 | args_info->nsapi_given = 0; |
| 144 | args_info->msisdn_given = 0; |
| 145 | args_info->qos_given = 0; |
| 146 | args_info->qose1_given = 0; |
| 147 | args_info->qose2_given = 0; |
| 148 | args_info->qose3_given = 0; |
| 149 | args_info->qose4_given = 0; |
| 150 | args_info->charging_given = 0; |
| 151 | args_info->uid_given = 0; |
| 152 | args_info->pwd_given = 0; |
| 153 | args_info->createif_given = 0; |
| 154 | args_info->net_given = 0; |
| 155 | args_info->defaultroute_given = 0; |
| 156 | args_info->ipup_given = 0; |
| 157 | args_info->ipdown_given = 0; |
| 158 | args_info->pinghost_given = 0; |
| 159 | args_info->pingrate_given = 0; |
| 160 | args_info->pingsize_given = 0; |
| 161 | args_info->pingcount_given = 0; |
| 162 | args_info->pingquiet_given = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 165 | static |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 166 | void clear_args(struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 167 | { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 168 | FIX_UNUSED(args_info); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 169 | args_info->debug_flag = 0; |
| 170 | args_info->conf_arg = NULL; |
| 171 | args_info->conf_orig = NULL; |
| 172 | args_info->pidfile_arg = gengetopt_strdup("./sgsnemu.pid"); |
| 173 | args_info->pidfile_orig = NULL; |
| 174 | args_info->statedir_arg = gengetopt_strdup("./"); |
| 175 | args_info->statedir_orig = NULL; |
| 176 | args_info->dns_arg = NULL; |
| 177 | args_info->dns_orig = NULL; |
| 178 | args_info->listen_arg = NULL; |
| 179 | args_info->listen_orig = NULL; |
| 180 | args_info->remote_arg = NULL; |
| 181 | args_info->remote_orig = NULL; |
| 182 | args_info->contexts_arg = 1; |
| 183 | args_info->contexts_orig = NULL; |
| 184 | args_info->timelimit_arg = 0; |
| 185 | args_info->timelimit_orig = NULL; |
| 186 | args_info->gtpversion_arg = 1; |
| 187 | args_info->gtpversion_orig = NULL; |
| 188 | args_info->apn_arg = gengetopt_strdup("internet"); |
| 189 | args_info->apn_orig = NULL; |
| 190 | args_info->selmode_arg = 0x01; |
| 191 | args_info->selmode_orig = NULL; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 192 | args_info->rattype_arg = 1; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 193 | args_info->rattype_orig = NULL; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 194 | args_info->userloc_arg = gengetopt_strdup("02509946241207"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 195 | args_info->userloc_orig = NULL; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 196 | args_info->rai_arg = gengetopt_strdup("02509946241207"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 197 | args_info->rai_orig = NULL; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 198 | args_info->mstz_arg = gengetopt_strdup("0"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 199 | args_info->mstz_orig = NULL; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 200 | args_info->imeisv_arg = gengetopt_strdup("2143658709214365"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 201 | args_info->imeisv_orig = NULL; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 202 | args_info->norecovery_flag = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 203 | args_info->imsi_arg = gengetopt_strdup("240010123456789"); |
| 204 | args_info->imsi_orig = NULL; |
| 205 | args_info->nsapi_arg = 0; |
| 206 | args_info->nsapi_orig = NULL; |
| 207 | args_info->msisdn_arg = gengetopt_strdup("46702123456"); |
| 208 | args_info->msisdn_orig = NULL; |
| 209 | args_info->qos_arg = 0x000b921f; |
| 210 | args_info->qos_orig = NULL; |
| 211 | args_info->qose1_arg = 0x9396404074f9ffff; |
| 212 | args_info->qose1_orig = NULL; |
| 213 | args_info->qose2_arg = 0x11; |
| 214 | args_info->qose2_orig = NULL; |
| 215 | args_info->qose3_arg = 0x0101; |
| 216 | args_info->qose3_orig = NULL; |
| 217 | args_info->qose4_arg = 0x4040; |
| 218 | args_info->qose4_orig = NULL; |
| 219 | args_info->charging_arg = 0x0800; |
| 220 | args_info->charging_orig = NULL; |
| 221 | args_info->uid_arg = gengetopt_strdup("mig"); |
| 222 | args_info->uid_orig = NULL; |
| 223 | args_info->pwd_arg = gengetopt_strdup("hemmelig"); |
| 224 | args_info->pwd_orig = NULL; |
| 225 | args_info->createif_flag = 0; |
| 226 | args_info->net_arg = NULL; |
| 227 | args_info->net_orig = NULL; |
| 228 | args_info->defaultroute_flag = 0; |
| 229 | args_info->ipup_arg = NULL; |
| 230 | args_info->ipup_orig = NULL; |
| 231 | args_info->ipdown_arg = NULL; |
| 232 | args_info->ipdown_orig = NULL; |
| 233 | args_info->pinghost_arg = NULL; |
| 234 | args_info->pinghost_orig = NULL; |
| 235 | args_info->pingrate_arg = 1; |
| 236 | args_info->pingrate_orig = NULL; |
| 237 | args_info->pingsize_arg = 56; |
| 238 | args_info->pingsize_orig = NULL; |
| 239 | args_info->pingcount_arg = 0; |
| 240 | args_info->pingcount_orig = NULL; |
| 241 | args_info->pingquiet_flag = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 242 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static |
| 246 | void init_args_info(struct gengetopt_args_info *args_info) |
| 247 | { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 248 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 249 | args_info->help_help = gengetopt_args_info_help[0]; |
| 250 | args_info->version_help = gengetopt_args_info_help[1]; |
| 251 | args_info->debug_help = gengetopt_args_info_help[2]; |
| 252 | args_info->conf_help = gengetopt_args_info_help[3]; |
| 253 | args_info->pidfile_help = gengetopt_args_info_help[4]; |
| 254 | args_info->statedir_help = gengetopt_args_info_help[5]; |
| 255 | args_info->dns_help = gengetopt_args_info_help[6]; |
| 256 | args_info->listen_help = gengetopt_args_info_help[7]; |
| 257 | args_info->remote_help = gengetopt_args_info_help[8]; |
| 258 | args_info->contexts_help = gengetopt_args_info_help[9]; |
| 259 | args_info->timelimit_help = gengetopt_args_info_help[10]; |
| 260 | args_info->gtpversion_help = gengetopt_args_info_help[11]; |
| 261 | args_info->apn_help = gengetopt_args_info_help[12]; |
| 262 | args_info->selmode_help = gengetopt_args_info_help[13]; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 263 | args_info->rattype_help = gengetopt_args_info_help[14]; |
| 264 | args_info->userloc_help = gengetopt_args_info_help[15]; |
| 265 | args_info->rai_help = gengetopt_args_info_help[16]; |
| 266 | args_info->mstz_help = gengetopt_args_info_help[17]; |
| 267 | args_info->imeisv_help = gengetopt_args_info_help[18]; |
| 268 | args_info->norecovery_help = gengetopt_args_info_help[19]; |
| 269 | args_info->imsi_help = gengetopt_args_info_help[20]; |
| 270 | args_info->nsapi_help = gengetopt_args_info_help[21]; |
| 271 | args_info->msisdn_help = gengetopt_args_info_help[22]; |
| 272 | args_info->qos_help = gengetopt_args_info_help[23]; |
| 273 | args_info->qose1_help = gengetopt_args_info_help[24]; |
| 274 | args_info->qose2_help = gengetopt_args_info_help[25]; |
| 275 | args_info->qose3_help = gengetopt_args_info_help[26]; |
| 276 | args_info->qose4_help = gengetopt_args_info_help[27]; |
| 277 | args_info->charging_help = gengetopt_args_info_help[28]; |
| 278 | args_info->uid_help = gengetopt_args_info_help[29]; |
| 279 | args_info->pwd_help = gengetopt_args_info_help[30]; |
| 280 | args_info->createif_help = gengetopt_args_info_help[31]; |
| 281 | args_info->net_help = gengetopt_args_info_help[32]; |
| 282 | args_info->defaultroute_help = gengetopt_args_info_help[33]; |
| 283 | args_info->ipup_help = gengetopt_args_info_help[34]; |
| 284 | args_info->ipdown_help = gengetopt_args_info_help[35]; |
| 285 | args_info->pinghost_help = gengetopt_args_info_help[36]; |
| 286 | args_info->pingrate_help = gengetopt_args_info_help[37]; |
| 287 | args_info->pingsize_help = gengetopt_args_info_help[38]; |
| 288 | args_info->pingcount_help = gengetopt_args_info_help[39]; |
| 289 | args_info->pingquiet_help = gengetopt_args_info_help[40]; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 290 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 293 | void cmdline_parser_print_version(void) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 294 | { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 295 | printf("%s %s\n", |
| 296 | (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? |
| 297 | CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), |
| 298 | CMDLINE_PARSER_VERSION); |
| 299 | |
| 300 | if (strlen(gengetopt_args_info_versiontext) > 0) |
| 301 | printf("\n%s\n", gengetopt_args_info_versiontext); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 304 | static void print_help_common(void) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 305 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 306 | cmdline_parser_print_version(); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 307 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 308 | if (strlen(gengetopt_args_info_purpose) > 0) |
| 309 | printf("\n%s\n", gengetopt_args_info_purpose); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 310 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 311 | if (strlen(gengetopt_args_info_usage) > 0) |
| 312 | printf("\n%s\n", gengetopt_args_info_usage); |
| 313 | |
| 314 | printf("\n"); |
| 315 | |
| 316 | if (strlen(gengetopt_args_info_description) > 0) |
| 317 | printf("%s\n\n", gengetopt_args_info_description); |
| 318 | } |
| 319 | |
| 320 | void cmdline_parser_print_help(void) |
| 321 | { |
| 322 | int i = 0; |
| 323 | print_help_common(); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 324 | while (gengetopt_args_info_help[i]) |
| 325 | printf("%s\n", gengetopt_args_info_help[i++]); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 328 | void cmdline_parser_init(struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 329 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 330 | clear_given(args_info); |
| 331 | clear_args(args_info); |
| 332 | init_args_info(args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 335 | void cmdline_parser_params_init(struct cmdline_parser_params *params) |
| 336 | { |
| 337 | if (params) { |
| 338 | params->override = 0; |
| 339 | params->initialize = 1; |
| 340 | params->check_required = 1; |
| 341 | params->check_ambiguity = 0; |
| 342 | params->print_errors = 1; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | struct cmdline_parser_params *cmdline_parser_params_create(void) |
| 347 | { |
| 348 | struct cmdline_parser_params *params = |
| 349 | (struct cmdline_parser_params *) |
| 350 | malloc(sizeof(struct cmdline_parser_params)); |
| 351 | cmdline_parser_params_init(params); |
| 352 | return params; |
| 353 | } |
| 354 | |
| 355 | static void free_string_field(char **s) |
| 356 | { |
| 357 | if (*s) { |
| 358 | free(*s); |
| 359 | *s = 0; |
| 360 | } |
| 361 | } |
| 362 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 363 | static void cmdline_parser_release(struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 364 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 365 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 366 | free_string_field(&(args_info->conf_arg)); |
| 367 | free_string_field(&(args_info->conf_orig)); |
| 368 | free_string_field(&(args_info->pidfile_arg)); |
| 369 | free_string_field(&(args_info->pidfile_orig)); |
| 370 | free_string_field(&(args_info->statedir_arg)); |
| 371 | free_string_field(&(args_info->statedir_orig)); |
| 372 | free_string_field(&(args_info->dns_arg)); |
| 373 | free_string_field(&(args_info->dns_orig)); |
| 374 | free_string_field(&(args_info->listen_arg)); |
| 375 | free_string_field(&(args_info->listen_orig)); |
| 376 | free_string_field(&(args_info->remote_arg)); |
| 377 | free_string_field(&(args_info->remote_orig)); |
| 378 | free_string_field(&(args_info->contexts_orig)); |
| 379 | free_string_field(&(args_info->timelimit_orig)); |
| 380 | free_string_field(&(args_info->gtpversion_orig)); |
| 381 | free_string_field(&(args_info->apn_arg)); |
| 382 | free_string_field(&(args_info->apn_orig)); |
| 383 | free_string_field(&(args_info->selmode_orig)); |
| 384 | free_string_field(&(args_info->rattype_orig)); |
| 385 | free_string_field(&(args_info->userloc_arg)); |
| 386 | free_string_field(&(args_info->userloc_orig)); |
| 387 | free_string_field(&(args_info->rai_arg)); |
| 388 | free_string_field(&(args_info->rai_orig)); |
| 389 | free_string_field(&(args_info->mstz_arg)); |
| 390 | free_string_field(&(args_info->mstz_orig)); |
| 391 | free_string_field(&(args_info->imeisv_arg)); |
| 392 | free_string_field(&(args_info->imeisv_orig)); |
| 393 | free_string_field(&(args_info->imsi_arg)); |
| 394 | free_string_field(&(args_info->imsi_orig)); |
| 395 | free_string_field(&(args_info->nsapi_orig)); |
| 396 | free_string_field(&(args_info->msisdn_arg)); |
| 397 | free_string_field(&(args_info->msisdn_orig)); |
| 398 | free_string_field(&(args_info->qos_orig)); |
| 399 | free_string_field(&(args_info->qose1_orig)); |
| 400 | free_string_field(&(args_info->qose2_orig)); |
| 401 | free_string_field(&(args_info->qose3_orig)); |
| 402 | free_string_field(&(args_info->qose4_orig)); |
| 403 | free_string_field(&(args_info->charging_orig)); |
| 404 | free_string_field(&(args_info->uid_arg)); |
| 405 | free_string_field(&(args_info->uid_orig)); |
| 406 | free_string_field(&(args_info->pwd_arg)); |
| 407 | free_string_field(&(args_info->pwd_orig)); |
| 408 | free_string_field(&(args_info->net_arg)); |
| 409 | free_string_field(&(args_info->net_orig)); |
| 410 | free_string_field(&(args_info->ipup_arg)); |
| 411 | free_string_field(&(args_info->ipup_orig)); |
| 412 | free_string_field(&(args_info->ipdown_arg)); |
| 413 | free_string_field(&(args_info->ipdown_orig)); |
| 414 | free_string_field(&(args_info->pinghost_arg)); |
| 415 | free_string_field(&(args_info->pinghost_orig)); |
| 416 | free_string_field(&(args_info->pingrate_orig)); |
| 417 | free_string_field(&(args_info->pingsize_orig)); |
| 418 | free_string_field(&(args_info->pingcount_orig)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 419 | |
| 420 | clear_given(args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 423 | static void |
| 424 | write_into_file(FILE * outfile, const char *opt, const char *arg, |
| 425 | const char *values[]) |
| 426 | { |
| 427 | FIX_UNUSED(values); |
| 428 | if (arg) { |
| 429 | fprintf(outfile, "%s=\"%s\"\n", opt, arg); |
| 430 | } else { |
| 431 | fprintf(outfile, "%s\n", opt); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | int cmdline_parser_dump(FILE * outfile, struct gengetopt_args_info *args_info) |
| 436 | { |
| 437 | int i = 0; |
| 438 | |
| 439 | if (!outfile) { |
| 440 | fprintf(stderr, "%s: cannot dump options to stream\n", |
| 441 | CMDLINE_PARSER_PACKAGE); |
| 442 | return EXIT_FAILURE; |
| 443 | } |
| 444 | |
| 445 | if (args_info->help_given) |
| 446 | write_into_file(outfile, "help", 0, 0); |
| 447 | if (args_info->version_given) |
| 448 | write_into_file(outfile, "version", 0, 0); |
| 449 | if (args_info->debug_given) |
| 450 | write_into_file(outfile, "debug", 0, 0); |
| 451 | if (args_info->conf_given) |
| 452 | write_into_file(outfile, "conf", args_info->conf_orig, 0); |
| 453 | if (args_info->pidfile_given) |
| 454 | write_into_file(outfile, "pidfile", args_info->pidfile_orig, 0); |
| 455 | if (args_info->statedir_given) |
| 456 | write_into_file(outfile, "statedir", args_info->statedir_orig, |
| 457 | 0); |
| 458 | if (args_info->dns_given) |
| 459 | write_into_file(outfile, "dns", args_info->dns_orig, 0); |
| 460 | if (args_info->listen_given) |
| 461 | write_into_file(outfile, "listen", args_info->listen_orig, 0); |
| 462 | if (args_info->remote_given) |
| 463 | write_into_file(outfile, "remote", args_info->remote_orig, 0); |
| 464 | if (args_info->contexts_given) |
| 465 | write_into_file(outfile, "contexts", args_info->contexts_orig, |
| 466 | 0); |
| 467 | if (args_info->timelimit_given) |
| 468 | write_into_file(outfile, "timelimit", args_info->timelimit_orig, |
| 469 | 0); |
| 470 | if (args_info->gtpversion_given) |
| 471 | write_into_file(outfile, "gtpversion", |
| 472 | args_info->gtpversion_orig, 0); |
| 473 | if (args_info->apn_given) |
| 474 | write_into_file(outfile, "apn", args_info->apn_orig, 0); |
| 475 | if (args_info->selmode_given) |
| 476 | write_into_file(outfile, "selmode", args_info->selmode_orig, 0); |
| 477 | if (args_info->rattype_given) |
| 478 | write_into_file(outfile, "rattype", args_info->rattype_orig, 0); |
| 479 | if (args_info->userloc_given) |
| 480 | write_into_file(outfile, "userloc", args_info->userloc_orig, 0); |
| 481 | if (args_info->rai_given) |
| 482 | write_into_file(outfile, "rai", args_info->rai_orig, 0); |
| 483 | if (args_info->mstz_given) |
| 484 | write_into_file(outfile, "mstz", args_info->mstz_orig, 0); |
| 485 | if (args_info->imeisv_given) |
| 486 | write_into_file(outfile, "imeisv", args_info->imeisv_orig, 0); |
| 487 | if (args_info->norecovery_given) |
| 488 | write_into_file(outfile, "norecovery", 0, 0); |
| 489 | if (args_info->imsi_given) |
| 490 | write_into_file(outfile, "imsi", args_info->imsi_orig, 0); |
| 491 | if (args_info->nsapi_given) |
| 492 | write_into_file(outfile, "nsapi", args_info->nsapi_orig, 0); |
| 493 | if (args_info->msisdn_given) |
| 494 | write_into_file(outfile, "msisdn", args_info->msisdn_orig, 0); |
| 495 | if (args_info->qos_given) |
| 496 | write_into_file(outfile, "qos", args_info->qos_orig, 0); |
| 497 | if (args_info->qose1_given) |
| 498 | write_into_file(outfile, "qose1", args_info->qose1_orig, 0); |
| 499 | if (args_info->qose2_given) |
| 500 | write_into_file(outfile, "qose2", args_info->qose2_orig, 0); |
| 501 | if (args_info->qose3_given) |
| 502 | write_into_file(outfile, "qose3", args_info->qose3_orig, 0); |
| 503 | if (args_info->qose4_given) |
| 504 | write_into_file(outfile, "qose4", args_info->qose4_orig, 0); |
| 505 | if (args_info->charging_given) |
| 506 | write_into_file(outfile, "charging", args_info->charging_orig, |
| 507 | 0); |
| 508 | if (args_info->uid_given) |
| 509 | write_into_file(outfile, "uid", args_info->uid_orig, 0); |
| 510 | if (args_info->pwd_given) |
| 511 | write_into_file(outfile, "pwd", args_info->pwd_orig, 0); |
| 512 | if (args_info->createif_given) |
| 513 | write_into_file(outfile, "createif", 0, 0); |
| 514 | if (args_info->net_given) |
| 515 | write_into_file(outfile, "net", args_info->net_orig, 0); |
| 516 | if (args_info->defaultroute_given) |
| 517 | write_into_file(outfile, "defaultroute", 0, 0); |
| 518 | if (args_info->ipup_given) |
| 519 | write_into_file(outfile, "ipup", args_info->ipup_orig, 0); |
| 520 | if (args_info->ipdown_given) |
| 521 | write_into_file(outfile, "ipdown", args_info->ipdown_orig, 0); |
| 522 | if (args_info->pinghost_given) |
| 523 | write_into_file(outfile, "pinghost", args_info->pinghost_orig, |
| 524 | 0); |
| 525 | if (args_info->pingrate_given) |
| 526 | write_into_file(outfile, "pingrate", args_info->pingrate_orig, |
| 527 | 0); |
| 528 | if (args_info->pingsize_given) |
| 529 | write_into_file(outfile, "pingsize", args_info->pingsize_orig, |
| 530 | 0); |
| 531 | if (args_info->pingcount_given) |
| 532 | write_into_file(outfile, "pingcount", args_info->pingcount_orig, |
| 533 | 0); |
| 534 | if (args_info->pingquiet_given) |
| 535 | write_into_file(outfile, "pingquiet", 0, 0); |
| 536 | |
| 537 | i = EXIT_SUCCESS; |
| 538 | return i; |
| 539 | } |
| 540 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 541 | int |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 542 | cmdline_parser_file_save(const char *filename, |
| 543 | struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 544 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 545 | FILE *outfile; |
| 546 | int i = 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 547 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 548 | outfile = fopen(filename, "w"); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 549 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 550 | if (!outfile) { |
| 551 | fprintf(stderr, "%s: cannot open file for writing: %s\n", |
| 552 | CMDLINE_PARSER_PACKAGE, filename); |
| 553 | return EXIT_FAILURE; |
| 554 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 555 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 556 | i = cmdline_parser_dump(outfile, args_info); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 557 | fclose(outfile); |
| 558 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 559 | return i; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 562 | void cmdline_parser_free(struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 563 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 564 | cmdline_parser_release(args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 567 | /** @brief replacement of strdup, which is not standard */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 568 | char *gengetopt_strdup(const char *s) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 569 | { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 570 | char *result = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 571 | if (!s) |
| 572 | return result; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 573 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 574 | result = (char *)malloc(strlen(s) + 1); |
| 575 | if (result == (char *)0) |
| 576 | return (char *)0; |
| 577 | strcpy(result, s); |
| 578 | return result; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 581 | int cmdline_parser(int argc, char **argv, struct gengetopt_args_info *args_info) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 582 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 583 | return cmdline_parser2(argc, argv, args_info, 0, 1, 1); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | int |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 587 | cmdline_parser_ext(int argc, char **argv, struct gengetopt_args_info *args_info, |
| 588 | struct cmdline_parser_params *params) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 589 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 590 | int result; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 591 | result = cmdline_parser_internal(argc, argv, args_info, params, 0); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 592 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 593 | if (result == EXIT_FAILURE) { |
| 594 | cmdline_parser_free(args_info); |
| 595 | exit(EXIT_FAILURE); |
| 596 | } |
| 597 | |
| 598 | return result; |
| 599 | } |
| 600 | |
| 601 | int |
| 602 | cmdline_parser2(int argc, char **argv, struct gengetopt_args_info *args_info, |
| 603 | int override, int initialize, int check_required) |
| 604 | { |
| 605 | int result; |
| 606 | struct cmdline_parser_params params; |
| 607 | |
| 608 | params.override = override; |
| 609 | params.initialize = initialize; |
| 610 | params.check_required = check_required; |
| 611 | params.check_ambiguity = 0; |
| 612 | params.print_errors = 1; |
| 613 | |
| 614 | result = cmdline_parser_internal(argc, argv, args_info, ¶ms, 0); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 615 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 616 | if (result == EXIT_FAILURE) { |
| 617 | cmdline_parser_free(args_info); |
| 618 | exit(EXIT_FAILURE); |
| 619 | } |
| 620 | |
| 621 | return result; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | int |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 625 | cmdline_parser_required(struct gengetopt_args_info *args_info, |
| 626 | const char *prog_name) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 627 | { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 628 | FIX_UNUSED(args_info); |
| 629 | FIX_UNUSED(prog_name); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 630 | return EXIT_SUCCESS; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 633 | static char *package_name = 0; |
| 634 | |
| 635 | /** |
| 636 | * @brief updates an option |
| 637 | * @param field the generic pointer to the field to update |
| 638 | * @param orig_field the pointer to the orig field |
| 639 | * @param field_given the pointer to the number of occurrence of this option |
| 640 | * @param prev_given the pointer to the number of occurrence already seen |
| 641 | * @param value the argument for this option (if null no arg was specified) |
| 642 | * @param possible_values the possible values for this option (if specified) |
| 643 | * @param default_value the default value (in case the option only accepts fixed values) |
| 644 | * @param arg_type the type of this option |
| 645 | * @param check_ambiguity @see cmdline_parser_params.check_ambiguity |
| 646 | * @param override @see cmdline_parser_params.override |
| 647 | * @param no_free whether to free a possible previous value |
| 648 | * @param multiple_option whether this is a multiple option |
| 649 | * @param long_opt the corresponding long option |
| 650 | * @param short_opt the corresponding short option (or '-' if none) |
| 651 | * @param additional_error possible further error specification |
| 652 | */ |
| 653 | static |
| 654 | int update_arg(void *field, char **orig_field, |
| 655 | unsigned int *field_given, unsigned int *prev_given, |
| 656 | char *value, const char *possible_values[], |
| 657 | const char *default_value, |
| 658 | cmdline_parser_arg_type arg_type, |
| 659 | int check_ambiguity, int override, |
| 660 | int no_free, int multiple_option, |
| 661 | const char *long_opt, char short_opt, |
| 662 | const char *additional_error) |
| 663 | { |
| 664 | char *stop_char = 0; |
| 665 | const char *val = value; |
| 666 | int found; |
| 667 | char **string_field; |
| 668 | FIX_UNUSED(field); |
| 669 | |
| 670 | stop_char = 0; |
| 671 | found = 0; |
| 672 | |
| 673 | if (!multiple_option && prev_given |
| 674 | && (*prev_given || (check_ambiguity && *field_given))) { |
| 675 | if (short_opt != '-') |
| 676 | fprintf(stderr, |
| 677 | "%s: `--%s' (`-%c') option given more than once%s\n", |
| 678 | package_name, long_opt, short_opt, |
| 679 | (additional_error ? additional_error : "")); |
| 680 | else |
| 681 | fprintf(stderr, |
| 682 | "%s: `--%s' option given more than once%s\n", |
| 683 | package_name, long_opt, |
| 684 | (additional_error ? additional_error : "")); |
| 685 | return 1; /* failure */ |
| 686 | } |
| 687 | |
| 688 | FIX_UNUSED(default_value); |
| 689 | |
| 690 | if (field_given && *field_given && !override) |
| 691 | return 0; |
| 692 | if (prev_given) |
| 693 | (*prev_given)++; |
| 694 | if (field_given) |
| 695 | (*field_given)++; |
| 696 | if (possible_values) |
| 697 | val = possible_values[found]; |
| 698 | |
| 699 | switch (arg_type) { |
| 700 | case ARG_FLAG: |
| 701 | *((int *)field) = !*((int *)field); |
| 702 | break; |
| 703 | case ARG_INT: |
| 704 | if (val) |
| 705 | *((int *)field) = strtol(val, &stop_char, 0); |
| 706 | break; |
| 707 | case ARG_STRING: |
| 708 | if (val) { |
| 709 | string_field = (char **)field; |
| 710 | if (!no_free && *string_field) |
| 711 | free(*string_field); /* free previous string */ |
| 712 | *string_field = gengetopt_strdup(val); |
| 713 | } |
| 714 | break; |
| 715 | default: |
| 716 | break; |
| 717 | }; |
| 718 | |
| 719 | /* check numeric conversion */ |
| 720 | switch (arg_type) { |
| 721 | case ARG_INT: |
| 722 | if (val && !(stop_char && *stop_char == '\0')) { |
| 723 | fprintf(stderr, "%s: invalid numeric value: %s\n", |
| 724 | package_name, val); |
| 725 | return 1; /* failure */ |
| 726 | } |
| 727 | break; |
| 728 | default: |
| 729 | ; |
| 730 | }; |
| 731 | |
| 732 | /* store the original value */ |
| 733 | switch (arg_type) { |
| 734 | case ARG_NO: |
| 735 | case ARG_FLAG: |
| 736 | break; |
| 737 | default: |
| 738 | if (value && orig_field) { |
| 739 | if (no_free) { |
| 740 | *orig_field = value; |
| 741 | } else { |
| 742 | if (*orig_field) |
| 743 | free(*orig_field); /* free previous string */ |
| 744 | *orig_field = gengetopt_strdup(value); |
| 745 | } |
| 746 | } |
| 747 | }; |
| 748 | |
| 749 | return 0; /* OK */ |
| 750 | } |
| 751 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 752 | int |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 753 | cmdline_parser_internal(int argc, char **argv, |
| 754 | struct gengetopt_args_info *args_info, |
| 755 | struct cmdline_parser_params *params, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 756 | const char *additional_error) |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 757 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 758 | int c; /* Character of the parsed option. */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 759 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 760 | int error_occurred = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 761 | struct gengetopt_args_info local_args_info; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 762 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 763 | int override; |
| 764 | int initialize; |
| 765 | int check_required; |
| 766 | int check_ambiguity; |
| 767 | |
| 768 | package_name = argv[0]; |
| 769 | |
| 770 | override = params->override; |
| 771 | initialize = params->initialize; |
| 772 | check_required = params->check_required; |
| 773 | check_ambiguity = params->check_ambiguity; |
| 774 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 775 | if (initialize) |
| 776 | cmdline_parser_init(args_info); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 777 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 778 | cmdline_parser_init(&local_args_info); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 779 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 780 | optarg = 0; |
| 781 | optind = 0; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 782 | opterr = params->print_errors; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 783 | optopt = '?'; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 784 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 785 | while (1) { |
| 786 | int option_index = 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 787 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 788 | static struct option long_options[] = { |
| 789 | {"help", 0, NULL, 'h'}, |
| 790 | {"version", 0, NULL, 'V'}, |
| 791 | {"debug", 0, NULL, 'd'}, |
| 792 | {"conf", 1, NULL, 'c'}, |
| 793 | {"pidfile", 1, NULL, 0}, |
| 794 | {"statedir", 1, NULL, 0}, |
| 795 | {"dns", 1, NULL, 0}, |
| 796 | {"listen", 1, NULL, 'l'}, |
| 797 | {"remote", 1, NULL, 'r'}, |
| 798 | {"contexts", 1, NULL, 0}, |
| 799 | {"timelimit", 1, NULL, 0}, |
| 800 | {"gtpversion", 1, NULL, 0}, |
| 801 | {"apn", 1, NULL, 'a'}, |
| 802 | {"selmode", 1, NULL, 0}, |
| 803 | {"rattype", 1, NULL, 0}, |
| 804 | {"userloc", 1, NULL, 0}, |
| 805 | {"rai", 1, NULL, 0}, |
| 806 | {"mstz", 1, NULL, 0}, |
| 807 | {"imeisv", 1, NULL, 0}, |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 808 | {"norecovery", 0, NULL, 0}, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 809 | {"imsi", 1, NULL, 'i'}, |
| 810 | {"nsapi", 1, NULL, 0}, |
| 811 | {"msisdn", 1, NULL, 'm'}, |
| 812 | {"qos", 1, NULL, 'q'}, |
| 813 | {"qose1", 1, NULL, 0}, |
| 814 | {"qose2", 1, NULL, 0}, |
| 815 | {"qose3", 1, NULL, 0}, |
| 816 | {"qose4", 1, NULL, 0}, |
| 817 | {"charging", 1, NULL, 0}, |
| 818 | {"uid", 1, NULL, 'u'}, |
| 819 | {"pwd", 1, NULL, 'p'}, |
| 820 | {"createif", 0, NULL, 0}, |
| 821 | {"net", 1, NULL, 'n'}, |
| 822 | {"defaultroute", 0, NULL, 0}, |
| 823 | {"ipup", 1, NULL, 0}, |
| 824 | {"ipdown", 1, NULL, 0}, |
| 825 | {"pinghost", 1, NULL, 0}, |
| 826 | {"pingrate", 1, NULL, 0}, |
| 827 | {"pingsize", 1, NULL, 0}, |
| 828 | {"pingcount", 1, NULL, 0}, |
| 829 | {"pingquiet", 0, NULL, 0}, |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 830 | {0, 0, 0, 0} |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 831 | }; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 832 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 833 | c = getopt_long(argc, argv, "hVdc:l:r:a:i:m:q:u:p:n:", |
| 834 | long_options, &option_index); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 835 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 836 | if (c == -1) |
| 837 | break; /* Exit from `while (1)' loop. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 838 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 839 | switch (c) { |
| 840 | case 'h': /* Print help and exit. */ |
| 841 | cmdline_parser_print_help(); |
| 842 | cmdline_parser_free(&local_args_info); |
| 843 | exit(EXIT_SUCCESS); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 844 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 845 | case 'V': /* Print version and exit. */ |
| 846 | cmdline_parser_print_version(); |
| 847 | cmdline_parser_free(&local_args_info); |
| 848 | exit(EXIT_SUCCESS); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 849 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 850 | case 'd': /* Run in debug mode. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 851 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 852 | if (update_arg |
| 853 | ((void *)&(args_info->debug_flag), 0, |
| 854 | &(args_info->debug_given), |
| 855 | &(local_args_info.debug_given), optarg, 0, 0, |
| 856 | ARG_FLAG, check_ambiguity, override, 1, 0, "debug", |
| 857 | 'd', additional_error)) |
| 858 | goto failure; |
| 859 | |
| 860 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 861 | case 'c': /* Read configuration file. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 862 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 863 | if (update_arg((void *)&(args_info->conf_arg), |
| 864 | &(args_info->conf_orig), |
| 865 | &(args_info->conf_given), |
| 866 | &(local_args_info.conf_given), optarg, 0, |
| 867 | 0, ARG_STRING, check_ambiguity, override, |
| 868 | 0, 0, "conf", 'c', additional_error)) |
| 869 | goto failure; |
| 870 | |
| 871 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 872 | case 'l': /* Local interface. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 873 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 874 | if (update_arg((void *)&(args_info->listen_arg), |
| 875 | &(args_info->listen_orig), |
| 876 | &(args_info->listen_given), |
| 877 | &(local_args_info.listen_given), optarg, |
| 878 | 0, 0, ARG_STRING, check_ambiguity, |
| 879 | override, 0, 0, "listen", 'l', |
| 880 | additional_error)) |
| 881 | goto failure; |
| 882 | |
| 883 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 884 | case 'r': /* Remote host. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 885 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 886 | if (update_arg((void *)&(args_info->remote_arg), |
| 887 | &(args_info->remote_orig), |
| 888 | &(args_info->remote_given), |
| 889 | &(local_args_info.remote_given), optarg, |
| 890 | 0, 0, ARG_STRING, check_ambiguity, |
| 891 | override, 0, 0, "remote", 'r', |
| 892 | additional_error)) |
| 893 | goto failure; |
| 894 | |
| 895 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 896 | case 'a': /* Access point name. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 897 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 898 | if (update_arg((void *)&(args_info->apn_arg), |
| 899 | &(args_info->apn_orig), |
| 900 | &(args_info->apn_given), |
| 901 | &(local_args_info.apn_given), optarg, 0, |
| 902 | "internet", ARG_STRING, check_ambiguity, |
| 903 | override, 0, 0, "apn", 'a', |
| 904 | additional_error)) |
| 905 | goto failure; |
| 906 | |
| 907 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 908 | case 'i': /* IMSI. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 909 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 910 | if (update_arg((void *)&(args_info->imsi_arg), |
| 911 | &(args_info->imsi_orig), |
| 912 | &(args_info->imsi_given), |
| 913 | &(local_args_info.imsi_given), optarg, 0, |
| 914 | "240010123456789", ARG_STRING, |
| 915 | check_ambiguity, override, 0, 0, "imsi", |
| 916 | 'i', additional_error)) |
| 917 | goto failure; |
| 918 | |
| 919 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 920 | case 'm': /* Mobile Station ISDN number. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 921 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 922 | if (update_arg((void *)&(args_info->msisdn_arg), |
| 923 | &(args_info->msisdn_orig), |
| 924 | &(args_info->msisdn_given), |
| 925 | &(local_args_info.msisdn_given), optarg, |
| 926 | 0, "46702123456", ARG_STRING, |
| 927 | check_ambiguity, override, 0, 0, |
| 928 | "msisdn", 'm', additional_error)) |
| 929 | goto failure; |
| 930 | |
| 931 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 932 | case 'q': /* Requested quality of service. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 933 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 934 | if (update_arg((void *)&(args_info->qos_arg), |
| 935 | &(args_info->qos_orig), |
| 936 | &(args_info->qos_given), |
| 937 | &(local_args_info.qos_given), optarg, 0, |
| 938 | "0x000b921f", ARG_INT, check_ambiguity, |
| 939 | override, 0, 0, "qos", 'q', |
| 940 | additional_error)) |
| 941 | goto failure; |
| 942 | |
| 943 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 944 | case 'u': /* Login user ID. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 945 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 946 | if (update_arg((void *)&(args_info->uid_arg), |
| 947 | &(args_info->uid_orig), |
| 948 | &(args_info->uid_given), |
| 949 | &(local_args_info.uid_given), optarg, 0, |
| 950 | "mig", ARG_STRING, check_ambiguity, |
| 951 | override, 0, 0, "uid", 'u', |
| 952 | additional_error)) |
| 953 | goto failure; |
| 954 | |
| 955 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 956 | case 'p': /* Login password. */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 957 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 958 | if (update_arg((void *)&(args_info->pwd_arg), |
| 959 | &(args_info->pwd_orig), |
| 960 | &(args_info->pwd_given), |
| 961 | &(local_args_info.pwd_given), optarg, 0, |
| 962 | "hemmelig", ARG_STRING, check_ambiguity, |
| 963 | override, 0, 0, "pwd", 'p', |
| 964 | additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 965 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 966 | |
| 967 | break; |
| 968 | case 'n': /* Network address for local interface. */ |
| 969 | |
| 970 | if (update_arg((void *)&(args_info->net_arg), |
| 971 | &(args_info->net_orig), |
| 972 | &(args_info->net_given), |
| 973 | &(local_args_info.net_given), optarg, 0, |
| 974 | 0, ARG_STRING, check_ambiguity, override, |
| 975 | 0, 0, "net", 'n', additional_error)) |
| 976 | goto failure; |
| 977 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 978 | break; |
jjako | 193e8b1 | 2003-11-10 12:31:41 +0000 | [diff] [blame] | 979 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 980 | case 0: /* Long option with no short option */ |
| 981 | /* Filename of process id file. */ |
| 982 | if (strcmp(long_options[option_index].name, "pidfile") |
| 983 | == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 984 | |
| 985 | if (update_arg |
| 986 | ((void *)&(args_info->pidfile_arg), |
| 987 | &(args_info->pidfile_orig), |
| 988 | &(args_info->pidfile_given), |
| 989 | &(local_args_info.pidfile_given), optarg, |
| 990 | 0, "./sgsnemu.pid", ARG_STRING, |
| 991 | check_ambiguity, override, 0, 0, "pidfile", |
| 992 | '-', additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 993 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 994 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 995 | } |
| 996 | /* Directory of nonvolatile data. */ |
| 997 | else if (strcmp |
| 998 | (long_options[option_index].name, |
| 999 | "statedir") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1000 | |
| 1001 | if (update_arg |
| 1002 | ((void *)&(args_info->statedir_arg), |
| 1003 | &(args_info->statedir_orig), |
| 1004 | &(args_info->statedir_given), |
| 1005 | &(local_args_info.statedir_given), optarg, |
| 1006 | 0, "./", ARG_STRING, check_ambiguity, |
| 1007 | override, 0, 0, "statedir", '-', |
| 1008 | additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1009 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1010 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1011 | } |
| 1012 | /* DNS Server to use. */ |
| 1013 | else if (strcmp(long_options[option_index].name, "dns") |
| 1014 | == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1015 | |
| 1016 | if (update_arg((void *)&(args_info->dns_arg), |
| 1017 | &(args_info->dns_orig), |
| 1018 | &(args_info->dns_given), |
| 1019 | &(local_args_info.dns_given), |
| 1020 | optarg, 0, 0, ARG_STRING, |
| 1021 | check_ambiguity, override, 0, 0, |
| 1022 | "dns", '-', additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1023 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1024 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1025 | } |
| 1026 | /* Number of contexts. */ |
| 1027 | else if (strcmp |
| 1028 | (long_options[option_index].name, |
| 1029 | "contexts") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1030 | |
| 1031 | if (update_arg |
| 1032 | ((void *)&(args_info->contexts_arg), |
| 1033 | &(args_info->contexts_orig), |
| 1034 | &(args_info->contexts_given), |
| 1035 | &(local_args_info.contexts_given), optarg, |
| 1036 | 0, "1", ARG_INT, check_ambiguity, override, |
| 1037 | 0, 0, "contexts", '-', additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1038 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1039 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1040 | } |
| 1041 | /* Exit after timelimit seconds. */ |
| 1042 | else if (strcmp |
| 1043 | (long_options[option_index].name, |
| 1044 | "timelimit") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1045 | |
| 1046 | if (update_arg |
| 1047 | ((void *)&(args_info->timelimit_arg), |
| 1048 | &(args_info->timelimit_orig), |
| 1049 | &(args_info->timelimit_given), |
| 1050 | &(local_args_info.timelimit_given), optarg, |
| 1051 | 0, "0", ARG_INT, check_ambiguity, override, |
| 1052 | 0, 0, "timelimit", '-', additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1053 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1054 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1055 | } |
| 1056 | /* GTP version to use. */ |
| 1057 | else if (strcmp |
| 1058 | (long_options[option_index].name, |
| 1059 | "gtpversion") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1060 | |
| 1061 | if (update_arg |
| 1062 | ((void *)&(args_info->gtpversion_arg), |
| 1063 | &(args_info->gtpversion_orig), |
| 1064 | &(args_info->gtpversion_given), |
| 1065 | &(local_args_info.gtpversion_given), |
| 1066 | optarg, 0, "1", ARG_INT, check_ambiguity, |
| 1067 | override, 0, 0, "gtpversion", '-', |
| 1068 | additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1069 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1070 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1071 | } |
| 1072 | /* Selection mode. */ |
| 1073 | else if (strcmp |
| 1074 | (long_options[option_index].name, |
| 1075 | "selmode") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1076 | |
| 1077 | if (update_arg |
| 1078 | ((void *)&(args_info->selmode_arg), |
| 1079 | &(args_info->selmode_orig), |
| 1080 | &(args_info->selmode_given), |
| 1081 | &(local_args_info.selmode_given), optarg, |
| 1082 | 0, "0x01", ARG_INT, check_ambiguity, |
| 1083 | override, 0, 0, "selmode", '-', |
| 1084 | additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1085 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1086 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1087 | } |
| 1088 | /* Radio Access Technology Type. */ |
| 1089 | else if (strcmp |
| 1090 | (long_options[option_index].name, |
| 1091 | "rattype") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1092 | |
| 1093 | if (update_arg |
| 1094 | ((void *)&(args_info->rattype_arg), |
| 1095 | &(args_info->rattype_orig), |
| 1096 | &(args_info->rattype_given), |
| 1097 | &(local_args_info.rattype_given), optarg, |
| 1098 | 0, "1", ARG_INT, check_ambiguity, override, |
| 1099 | 0, 0, "rattype", '-', additional_error)) |
| 1100 | goto failure; |
| 1101 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1102 | } |
| 1103 | /* User Location Information. */ |
| 1104 | else if (strcmp |
| 1105 | (long_options[option_index].name, |
| 1106 | "userloc") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1107 | |
| 1108 | if (update_arg |
| 1109 | ((void *)&(args_info->userloc_arg), |
| 1110 | &(args_info->userloc_orig), |
| 1111 | &(args_info->userloc_given), |
| 1112 | &(local_args_info.userloc_given), optarg, |
| 1113 | 0, "02509946241207", ARG_STRING, |
| 1114 | check_ambiguity, override, 0, 0, "userloc", |
| 1115 | '-', additional_error)) |
| 1116 | goto failure; |
| 1117 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1118 | } |
| 1119 | /* Routing Area Information. */ |
| 1120 | else if (strcmp(long_options[option_index].name, "rai") |
| 1121 | == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1122 | |
| 1123 | if (update_arg((void *)&(args_info->rai_arg), |
| 1124 | &(args_info->rai_orig), |
| 1125 | &(args_info->rai_given), |
| 1126 | &(local_args_info.rai_given), |
| 1127 | optarg, 0, "02509946241207", |
| 1128 | ARG_STRING, check_ambiguity, |
| 1129 | override, 0, 0, "rai", '-', |
| 1130 | additional_error)) |
| 1131 | goto failure; |
| 1132 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1133 | } |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1134 | /* MS Time Zone. */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1135 | else if (strcmp(long_options[option_index].name, "mstz") |
| 1136 | == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1137 | |
| 1138 | if (update_arg((void *)&(args_info->mstz_arg), |
| 1139 | &(args_info->mstz_orig), |
| 1140 | &(args_info->mstz_given), |
| 1141 | &(local_args_info.mstz_given), |
| 1142 | optarg, 0, "0", ARG_STRING, |
| 1143 | check_ambiguity, override, 0, 0, |
| 1144 | "mstz", '-', additional_error)) |
| 1145 | goto failure; |
| 1146 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1147 | } |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1148 | /* IMEI(SV) International Mobile Equipment Identity (and Software Version). */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1149 | else if (strcmp |
| 1150 | (long_options[option_index].name, |
| 1151 | "imeisv") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1152 | |
| 1153 | if (update_arg((void *)&(args_info->imeisv_arg), |
| 1154 | &(args_info->imeisv_orig), |
| 1155 | &(args_info->imeisv_given), |
| 1156 | &(local_args_info.imeisv_given), |
| 1157 | optarg, 0, "2143658709214365", |
| 1158 | ARG_STRING, check_ambiguity, |
| 1159 | override, 0, 0, "imeisv", '-', |
| 1160 | additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1161 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1162 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1163 | } |
| 1164 | /* Do not send recovery. */ |
| 1165 | else if (strcmp |
| 1166 | (long_options[option_index].name, |
| 1167 | "norecovery") == 0) { |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1168 | |
| 1169 | if (update_arg |
| 1170 | ((void *)&(args_info->norecovery_flag), 0, |
| 1171 | &(args_info->norecovery_given), |
| 1172 | &(local_args_info.norecovery_given), |
| 1173 | optarg, 0, 0, ARG_FLAG, check_ambiguity, |
| 1174 | override, 1, 0, "norecovery", '-', |
| 1175 | additional_error)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1176 | goto failure; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1177 | |
| 1178 | } |
| 1179 | /* NSAPI. */ |
| 1180 | else if (strcmp |
| 1181 | (long_options[option_index].name, |
| 1182 | "nsapi") == 0) { |
| 1183 | |
| 1184 | if (update_arg((void *)&(args_info->nsapi_arg), |
| 1185 | &(args_info->nsapi_orig), |
| 1186 | &(args_info->nsapi_given), |
| 1187 | &(local_args_info.nsapi_given), |
| 1188 | optarg, 0, "0", ARG_INT, |
| 1189 | check_ambiguity, override, 0, 0, |
| 1190 | "nsapi", '-', additional_error)) |
| 1191 | goto failure; |
| 1192 | |
| 1193 | } |
| 1194 | /* Requested quality of service Extension 1. */ |
| 1195 | else if (strcmp |
| 1196 | (long_options[option_index].name, |
| 1197 | "qose1") == 0) { |
| 1198 | |
| 1199 | if (update_arg((void *)&(args_info->qose1_arg), |
| 1200 | &(args_info->qose1_orig), |
| 1201 | &(args_info->qose1_given), |
| 1202 | &(local_args_info.qose1_given), |
| 1203 | optarg, 0, "0x9396404074f9ffff", |
| 1204 | ARG_INT, check_ambiguity, |
| 1205 | override, 0, 0, "qose1", '-', |
| 1206 | additional_error)) |
| 1207 | goto failure; |
| 1208 | |
| 1209 | } |
| 1210 | /* Requested quality of service Extension 2. */ |
| 1211 | else if (strcmp |
| 1212 | (long_options[option_index].name, |
| 1213 | "qose2") == 0) { |
| 1214 | |
| 1215 | if (update_arg((void *)&(args_info->qose2_arg), |
| 1216 | &(args_info->qose2_orig), |
| 1217 | &(args_info->qose2_given), |
| 1218 | &(local_args_info.qose2_given), |
| 1219 | optarg, 0, "0x11", ARG_INT, |
| 1220 | check_ambiguity, override, 0, 0, |
| 1221 | "qose2", '-', additional_error)) |
| 1222 | goto failure; |
| 1223 | |
| 1224 | } |
| 1225 | /* Requested quality of service Extension 3. */ |
| 1226 | else if (strcmp |
| 1227 | (long_options[option_index].name, |
| 1228 | "qose3") == 0) { |
| 1229 | |
| 1230 | if (update_arg((void *)&(args_info->qose3_arg), |
| 1231 | &(args_info->qose3_orig), |
| 1232 | &(args_info->qose3_given), |
| 1233 | &(local_args_info.qose3_given), |
| 1234 | optarg, 0, "0x0101", ARG_INT, |
| 1235 | check_ambiguity, override, 0, 0, |
| 1236 | "qose3", '-', additional_error)) |
| 1237 | goto failure; |
| 1238 | |
| 1239 | } |
| 1240 | /* Requested quality of service Extension 4. */ |
| 1241 | else if (strcmp |
| 1242 | (long_options[option_index].name, |
| 1243 | "qose4") == 0) { |
| 1244 | |
| 1245 | if (update_arg((void *)&(args_info->qose4_arg), |
| 1246 | &(args_info->qose4_orig), |
| 1247 | &(args_info->qose4_given), |
| 1248 | &(local_args_info.qose4_given), |
| 1249 | optarg, 0, "0x4040", ARG_INT, |
| 1250 | check_ambiguity, override, 0, 0, |
| 1251 | "qose4", '-', additional_error)) |
| 1252 | goto failure; |
| 1253 | |
| 1254 | } |
| 1255 | /* Charging characteristics. */ |
| 1256 | else if (strcmp |
| 1257 | (long_options[option_index].name, |
| 1258 | "charging") == 0) { |
| 1259 | |
| 1260 | if (update_arg |
| 1261 | ((void *)&(args_info->charging_arg), |
| 1262 | &(args_info->charging_orig), |
| 1263 | &(args_info->charging_given), |
| 1264 | &(local_args_info.charging_given), optarg, |
| 1265 | 0, "0x0800", ARG_INT, check_ambiguity, |
| 1266 | override, 0, 0, "charging", '-', |
| 1267 | additional_error)) |
| 1268 | goto failure; |
| 1269 | |
| 1270 | } |
| 1271 | /* Create local network interface. */ |
| 1272 | else if (strcmp |
| 1273 | (long_options[option_index].name, |
| 1274 | "createif") == 0) { |
| 1275 | |
| 1276 | if (update_arg |
| 1277 | ((void *)&(args_info->createif_flag), 0, |
| 1278 | &(args_info->createif_given), |
| 1279 | &(local_args_info.createif_given), optarg, |
| 1280 | 0, 0, ARG_FLAG, check_ambiguity, override, |
| 1281 | 1, 0, "createif", '-', additional_error)) |
| 1282 | goto failure; |
| 1283 | |
| 1284 | } |
| 1285 | /* Create default route. */ |
| 1286 | else if (strcmp |
| 1287 | (long_options[option_index].name, |
| 1288 | "defaultroute") == 0) { |
| 1289 | |
| 1290 | if (update_arg |
| 1291 | ((void *)&(args_info->defaultroute_flag), 0, |
| 1292 | &(args_info->defaultroute_given), |
| 1293 | &(local_args_info.defaultroute_given), |
| 1294 | optarg, 0, 0, ARG_FLAG, check_ambiguity, |
| 1295 | override, 1, 0, "defaultroute", '-', |
| 1296 | additional_error)) |
| 1297 | goto failure; |
| 1298 | |
| 1299 | } |
| 1300 | /* Script to run after link-up. */ |
| 1301 | else if (strcmp(long_options[option_index].name, "ipup") |
| 1302 | == 0) { |
| 1303 | |
| 1304 | if (update_arg((void *)&(args_info->ipup_arg), |
| 1305 | &(args_info->ipup_orig), |
| 1306 | &(args_info->ipup_given), |
| 1307 | &(local_args_info.ipup_given), |
| 1308 | optarg, 0, 0, ARG_STRING, |
| 1309 | check_ambiguity, override, 0, 0, |
| 1310 | "ipup", '-', additional_error)) |
| 1311 | goto failure; |
| 1312 | |
| 1313 | } |
| 1314 | /* Script to run after link-down. */ |
| 1315 | else if (strcmp |
| 1316 | (long_options[option_index].name, |
| 1317 | "ipdown") == 0) { |
| 1318 | |
| 1319 | if (update_arg((void *)&(args_info->ipdown_arg), |
| 1320 | &(args_info->ipdown_orig), |
| 1321 | &(args_info->ipdown_given), |
| 1322 | &(local_args_info.ipdown_given), |
| 1323 | optarg, 0, 0, ARG_STRING, |
| 1324 | check_ambiguity, override, 0, 0, |
| 1325 | "ipdown", '-', additional_error)) |
| 1326 | goto failure; |
| 1327 | |
| 1328 | } |
| 1329 | /* Ping remote host. */ |
| 1330 | else if (strcmp |
| 1331 | (long_options[option_index].name, |
| 1332 | "pinghost") == 0) { |
| 1333 | |
| 1334 | if (update_arg |
| 1335 | ((void *)&(args_info->pinghost_arg), |
| 1336 | &(args_info->pinghost_orig), |
| 1337 | &(args_info->pinghost_given), |
| 1338 | &(local_args_info.pinghost_given), optarg, |
| 1339 | 0, 0, ARG_STRING, check_ambiguity, |
| 1340 | override, 0, 0, "pinghost", '-', |
| 1341 | additional_error)) |
| 1342 | goto failure; |
| 1343 | |
| 1344 | } |
| 1345 | /* Number of ping req per second. */ |
| 1346 | else if (strcmp |
| 1347 | (long_options[option_index].name, |
| 1348 | "pingrate") == 0) { |
| 1349 | |
| 1350 | if (update_arg |
| 1351 | ((void *)&(args_info->pingrate_arg), |
| 1352 | &(args_info->pingrate_orig), |
| 1353 | &(args_info->pingrate_given), |
| 1354 | &(local_args_info.pingrate_given), optarg, |
| 1355 | 0, "1", ARG_INT, check_ambiguity, override, |
| 1356 | 0, 0, "pingrate", '-', additional_error)) |
| 1357 | goto failure; |
| 1358 | |
| 1359 | } |
| 1360 | /* Number of ping data bytes. */ |
| 1361 | else if (strcmp |
| 1362 | (long_options[option_index].name, |
| 1363 | "pingsize") == 0) { |
| 1364 | |
| 1365 | if (update_arg |
| 1366 | ((void *)&(args_info->pingsize_arg), |
| 1367 | &(args_info->pingsize_orig), |
| 1368 | &(args_info->pingsize_given), |
| 1369 | &(local_args_info.pingsize_given), optarg, |
| 1370 | 0, "56", ARG_INT, check_ambiguity, |
| 1371 | override, 0, 0, "pingsize", '-', |
| 1372 | additional_error)) |
| 1373 | goto failure; |
| 1374 | |
| 1375 | } |
| 1376 | /* Number of ping req to send. */ |
| 1377 | else if (strcmp |
| 1378 | (long_options[option_index].name, |
| 1379 | "pingcount") == 0) { |
| 1380 | |
| 1381 | if (update_arg |
| 1382 | ((void *)&(args_info->pingcount_arg), |
| 1383 | &(args_info->pingcount_orig), |
| 1384 | &(args_info->pingcount_given), |
| 1385 | &(local_args_info.pingcount_given), optarg, |
| 1386 | 0, "0", ARG_INT, check_ambiguity, override, |
| 1387 | 0, 0, "pingcount", '-', additional_error)) |
| 1388 | goto failure; |
| 1389 | |
| 1390 | } |
| 1391 | /* Do not print ping packet info. */ |
| 1392 | else if (strcmp |
| 1393 | (long_options[option_index].name, |
| 1394 | "pingquiet") == 0) { |
| 1395 | |
| 1396 | if (update_arg |
| 1397 | ((void *)&(args_info->pingquiet_flag), 0, |
| 1398 | &(args_info->pingquiet_given), |
| 1399 | &(local_args_info.pingquiet_given), optarg, |
| 1400 | 0, 0, ARG_FLAG, check_ambiguity, override, |
| 1401 | 1, 0, "pingquiet", '-', additional_error)) |
| 1402 | goto failure; |
| 1403 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1404 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1405 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1406 | break; |
| 1407 | case '?': /* Invalid option. */ |
| 1408 | /* `getopt_long' already printed an error message. */ |
| 1409 | goto failure; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1410 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1411 | default: /* bug: option not considered. */ |
| 1412 | fprintf(stderr, "%s: option unknown: %c%s\n", |
| 1413 | CMDLINE_PARSER_PACKAGE, c, |
| 1414 | (additional_error ? additional_error : "")); |
| 1415 | abort(); |
| 1416 | } /* switch */ |
| 1417 | } /* while */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1418 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1419 | cmdline_parser_release(&local_args_info); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1420 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1421 | if (error_occurred) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1422 | return (EXIT_FAILURE); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1423 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1424 | return 0; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1425 | |
| 1426 | failure: |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1427 | |
| 1428 | cmdline_parser_release(&local_args_info); |
| 1429 | return (EXIT_FAILURE); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1432 | #ifndef CONFIG_FILE_LINE_SIZE |
| 1433 | #define CONFIG_FILE_LINE_SIZE 2048 |
| 1434 | #endif |
| 1435 | #define ADDITIONAL_ERROR " in configuration file " |
| 1436 | |
| 1437 | #define CONFIG_FILE_LINE_BUFFER_SIZE (CONFIG_FILE_LINE_SIZE+3) |
| 1438 | /* 3 is for "--" and "=" */ |
| 1439 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1440 | static int _cmdline_parser_configfile(const char *filename, int *my_argc) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1441 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1442 | FILE *file; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1443 | char my_argv[CONFIG_FILE_LINE_BUFFER_SIZE + 1]; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1444 | char linebuf[CONFIG_FILE_LINE_SIZE]; |
| 1445 | int line_num = 0; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1446 | int result = 0, equal; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1447 | char *fopt, *farg; |
| 1448 | char *str_index; |
| 1449 | size_t len, next_token; |
| 1450 | char delimiter; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1451 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1452 | if ((file = fopen(filename, "r")) == 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1453 | fprintf(stderr, "%s: Error opening configuration file '%s'\n", |
| 1454 | CMDLINE_PARSER_PACKAGE, filename); |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1455 | return EXIT_FAILURE; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1456 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1457 | |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1458 | while ((fgets(linebuf, CONFIG_FILE_LINE_SIZE, file)) != 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1459 | ++line_num; |
| 1460 | my_argv[0] = '\0'; |
| 1461 | len = strlen(linebuf); |
| 1462 | if (len > (CONFIG_FILE_LINE_BUFFER_SIZE - 1)) { |
| 1463 | fprintf(stderr, |
| 1464 | "%s:%s:%d: Line too long in configuration file\n", |
| 1465 | CMDLINE_PARSER_PACKAGE, filename, line_num); |
| 1466 | result = EXIT_FAILURE; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1467 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1468 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1469 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1470 | /* find first non-whitespace character in the line */ |
| 1471 | next_token = strspn(linebuf, " \t\r\n"); |
| 1472 | str_index = linebuf + next_token; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1473 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1474 | if (str_index[0] == '\0' || str_index[0] == '#') |
| 1475 | continue; /* empty line or comment line is skipped */ |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1476 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1477 | fopt = str_index; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1478 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1479 | /* truncate fopt at the end of the first non-valid character */ |
| 1480 | next_token = strcspn(fopt, " \t\r\n="); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1481 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1482 | if (fopt[next_token] == '\0') { /* the line is over */ |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1483 | farg = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1484 | equal = 0; |
| 1485 | goto noarg; |
| 1486 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1487 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1488 | /* remember if equal sign is present */ |
| 1489 | equal = (fopt[next_token] == '='); |
| 1490 | fopt[next_token++] = '\0'; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1491 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1492 | /* advance pointers to the next token after the end of fopt */ |
| 1493 | next_token += strspn(fopt + next_token, " \t\r\n"); |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1494 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1495 | /* check for the presence of equal sign, and if so, skip it */ |
| 1496 | if (!equal) |
| 1497 | if ((equal = (fopt[next_token] == '='))) { |
| 1498 | next_token++; |
| 1499 | next_token += |
| 1500 | strspn(fopt + next_token, " \t\r\n"); |
| 1501 | } |
| 1502 | str_index += next_token; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1503 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1504 | /* find argument */ |
| 1505 | farg = str_index; |
| 1506 | if (farg[0] == '\"' || farg[0] == '\'') { /* quoted argument */ |
| 1507 | str_index = strchr(++farg, str_index[0]); /* skip opening quote */ |
| 1508 | if (!str_index) { |
| 1509 | fprintf |
| 1510 | (stderr, |
| 1511 | "%s:%s:%d: unterminated string in configuration file\n", |
| 1512 | CMDLINE_PARSER_PACKAGE, filename, |
| 1513 | line_num); |
| 1514 | result = EXIT_FAILURE; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1515 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1516 | } |
| 1517 | } else { /* read up the remaining part up to a delimiter */ |
| 1518 | next_token = strcspn(farg, " \t\r\n#\'\""); |
| 1519 | str_index += next_token; |
| 1520 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1521 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1522 | /* truncate farg at the delimiter and store it for further check */ |
| 1523 | delimiter = *str_index, *str_index++ = '\0'; |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1524 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1525 | /* everything but comment is illegal at the end of line */ |
| 1526 | if (delimiter != '\0' && delimiter != '#') { |
| 1527 | str_index += strspn(str_index, " \t\r\n"); |
| 1528 | if (*str_index != '\0' && *str_index != '#') { |
| 1529 | fprintf |
| 1530 | (stderr, |
| 1531 | "%s:%s:%d: malformed string in configuration file\n", |
| 1532 | CMDLINE_PARSER_PACKAGE, filename, |
| 1533 | line_num); |
| 1534 | result = EXIT_FAILURE; |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1535 | break; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1536 | } |
| 1537 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1538 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1539 | noarg: |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1540 | if (!strcmp(fopt, "include")) { |
| 1541 | if (farg && *farg) { |
| 1542 | result = |
| 1543 | _cmdline_parser_configfile(farg, my_argc); |
| 1544 | } else { |
| 1545 | fprintf(stderr, |
| 1546 | "%s:%s:%d: include requires a filename argument.\n", |
| 1547 | CMDLINE_PARSER_PACKAGE, filename, |
| 1548 | line_num); |
| 1549 | } |
| 1550 | continue; |
| 1551 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1552 | len = strlen(fopt); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1553 | strcat(my_argv, len > 1 ? "--" : "-"); |
| 1554 | strcat(my_argv, fopt); |
| 1555 | if (len > 1 && ((farg && *farg) || equal)) |
| 1556 | strcat(my_argv, "="); |
| 1557 | if (farg && *farg) |
| 1558 | strcat(my_argv, farg); |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1559 | ++(*my_argc); |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1560 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1561 | cmd_line_list_tmp = |
| 1562 | (struct line_list *)malloc(sizeof(struct line_list)); |
| 1563 | cmd_line_list_tmp->next = cmd_line_list; |
| 1564 | cmd_line_list = cmd_line_list_tmp; |
| 1565 | cmd_line_list->string_arg = gengetopt_strdup(my_argv); |
| 1566 | } /* while */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1567 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1568 | if (file) |
| 1569 | fclose(file); |
Harald Welte | f621498 | 2017-09-24 10:23:24 +0800 | [diff] [blame^] | 1570 | return result; |
| 1571 | } |
| 1572 | |
| 1573 | int |
| 1574 | cmdline_parser_configfile(const char *filename, |
| 1575 | struct gengetopt_args_info *args_info, |
| 1576 | int override, int initialize, int check_required) |
| 1577 | { |
| 1578 | struct cmdline_parser_params params; |
| 1579 | |
| 1580 | params.override = override; |
| 1581 | params.initialize = initialize; |
| 1582 | params.check_required = check_required; |
| 1583 | params.check_ambiguity = 0; |
| 1584 | params.print_errors = 1; |
| 1585 | |
| 1586 | return cmdline_parser_config_file(filename, args_info, ¶ms); |
| 1587 | } |
| 1588 | |
| 1589 | int |
| 1590 | cmdline_parser_config_file(const char *filename, |
| 1591 | struct gengetopt_args_info *args_info, |
| 1592 | struct cmdline_parser_params *params) |
| 1593 | { |
| 1594 | int i, result; |
| 1595 | int my_argc = 1; |
| 1596 | char **my_argv_arg; |
| 1597 | char *additional_error; |
| 1598 | |
| 1599 | /* store the program name */ |
| 1600 | cmd_line_list_tmp = |
| 1601 | (struct line_list *)malloc(sizeof(struct line_list)); |
| 1602 | cmd_line_list_tmp->next = cmd_line_list; |
| 1603 | cmd_line_list = cmd_line_list_tmp; |
| 1604 | cmd_line_list->string_arg = gengetopt_strdup(CMDLINE_PARSER_PACKAGE); |
| 1605 | |
| 1606 | result = _cmdline_parser_configfile(filename, &my_argc); |
| 1607 | |
| 1608 | if (result != EXIT_FAILURE) { |
| 1609 | my_argv_arg = (char **)malloc((my_argc + 1) * sizeof(char *)); |
| 1610 | cmd_line_list_tmp = cmd_line_list; |
| 1611 | |
| 1612 | for (i = my_argc - 1; i >= 0; --i) { |
| 1613 | my_argv_arg[i] = cmd_line_list_tmp->string_arg; |
| 1614 | cmd_line_list_tmp = cmd_line_list_tmp->next; |
| 1615 | } |
| 1616 | |
| 1617 | my_argv_arg[my_argc] = 0; |
| 1618 | |
| 1619 | additional_error = |
| 1620 | (char *)malloc(strlen(filename) + strlen(ADDITIONAL_ERROR) + |
| 1621 | 1); |
| 1622 | strcpy(additional_error, ADDITIONAL_ERROR); |
| 1623 | strcat(additional_error, filename); |
| 1624 | result = |
| 1625 | cmdline_parser_internal(my_argc, my_argv_arg, args_info, |
| 1626 | params, additional_error); |
| 1627 | |
| 1628 | free(additional_error); |
| 1629 | free(my_argv_arg); |
| 1630 | } |
Harald Welte | 1b3e577 | 2010-05-04 11:13:56 +0200 | [diff] [blame] | 1631 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1632 | free_cmd_list(); |
| 1633 | if (result == EXIT_FAILURE) { |
| 1634 | cmdline_parser_free(args_info); |
| 1635 | exit(EXIT_FAILURE); |
| 1636 | } |
| 1637 | |
| 1638 | return result; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1639 | } |