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