| 1 | /* |
| 2 | ** petopt.c - Command line argument parser |
| 3 | ** |
| 4 | ** Copyright (c) 1999 Peter Eriksson <[email protected]> |
| 5 | ** |
| 6 | ** This program is free software; you can redistribute it and/or |
| 7 | ** modify it as you wish - as long as you don't claim that you wrote |
| 8 | ** it. |
| 9 | ** |
| 10 | ** This program is distributed in the hope that it will be useful, |
| 11 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | */ |
| 14 | |
| 15 | // #include "config.h" removed because OES does not use this file |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
| 22 | #include <syslog.h> |
| 23 | |
| 24 | #include "petopt.h"cxx . . $ `<file>/src/petopt.h`/ |
| 25 | |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | ** Compare the user supplied long style command line option against |
| 30 | ** the program defined. Handle abbreviations, truncation and ambiguous |
| 31 | ** abbreviations. |
| 32 | */ |
| 33 | static int |
| 34 | compare_longcxx . . $ compare_long(f0be30763a490cce).(const char *user_option, |
| 35 | const char *aov_long, |
| 36 | int len) |
| 37 | { |
| 38 | unsigned char *uc, *ac; |
| 39 | int diff = 0; |
| 40 | int saved_len = len; |
| 41 | |
| 42 | |
| 43 | uc = (unsigned char *) user_option; |
| 44 | ac = (unsigned char *) aov_long; |
| 45 | |
| 46 | while (len > 0 && diff == 0) |
| 47 | { |
| 48 | /* If we reach a word delimiter, skip to next word */ |
| 49 | if (*uc == '-') |
| 50 | while (*ac && *ac != '-') |
| 51 | ++ac; |
| 52 | |
| 53 | diff = (int) tolower(*uc) - (int) tolower(*ac); |
| 54 | |
| 55 | ++uc; |
| 56 | ++ac; |
| 57 | --len; |
| 58 | } |
| 59 | |
| 60 | if (diff) |
| 61 | { |
| 62 | /* Check abbrev */ |
| 63 | int adiff = 0; |
| 64 | |
| 65 | |
| 66 | uc = (unsigned char *) user_option; |
| 67 | ac = (unsigned char *) aov_long; |
| 68 | len = saved_len; |
| 69 | |
| 70 | while (len > 0 && adiff == 0) |
| 71 | { |
| 72 | /* Locate abbrev characters (uppercase) */ |
| 73 | while (*ac && (islower(*ac) || *ac == '-')) |
| 74 | ++ac; |
| 75 | |
| 76 | adiff = (int) tolower(*uc) - (int) tolower(*ac); |
| 77 | |
| 78 | ++uc; |
| 79 | ++ac; |
| 80 | --len; |
| 81 | } |
| 82 | |
| 83 | if (adiff == 0) |
| 84 | diff = 0; |
| 85 | } |
| 86 | return diff; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | static int |
| 92 | petopt_parse_optioncxx . . $ petopt_parse_option(593af1eac325019).(PETOPTcxx . . $ PETOPT# *pop, |
| 93 | PETOPTScxx . . $ PETOPTS# **pov, |
| 94 | const char **optarg) |
| 95 | { |
| 96 | int i; |
| 97 | |
| 98 | |
| 99 | *pov = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 100 | *optarg = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 101 | |
| 102 | |
| 103 | Again: |
| 104 | /* Index out of bounds? */ |
| 105 | if (pop->aicxx . . $ petopt#ai. >= pop->argccxx . . $ petopt#argc.) |
| 106 | { |
| 107 | return POE_EOFcxx . . $ `src/petopt.h:35:9`!; |
| 108 | } |
| 109 | |
| 110 | if (pop->cicxx . . $ petopt#ci. == 0) |
| 111 | { |
| 112 | /* Non option or "-" */ |
| 113 | if (pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][0] != '-' || |
| 114 | (pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][0] == '-' && |
| 115 | pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][1] == '\0')) |
| 116 | { |
| 117 | pop->oavcxx . . $ petopt#oav.[pop->oaccxx . . $ petopt#oac.++] = strdup(pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.++]); |
| 118 | goto Again; |
| 119 | } |
| 120 | |
| 121 | /* "--", stop parsing and skip */ |
| 122 | if (strcmp(pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.], "--") == 0) |
| 123 | { |
| 124 | pop->aicxx . . $ petopt#ai.++; |
| 125 | |
| 126 | while (pop->aicxx . . $ petopt#ai. < pop->argccxx . . $ petopt#argc.) |
| 127 | { |
| 128 | pop->oavcxx . . $ petopt#oav.[pop->oaccxx . . $ petopt#oac.++] = strdup(pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.++]); |
| 129 | } |
| 130 | |
| 131 | return POE_EOFcxx . . $ `src/petopt.h:35:9`!; |
| 132 | } |
| 133 | |
| 134 | if (pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][1] == '-') |
| 135 | { |
| 136 | /* Long option */ |
| 137 | const char *opt, *arg; |
| 138 | int len, ix; |
| 139 | char *boolval = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 140 | int withval = 0; |
| 141 | |
| 142 | |
| 143 | pop->saved_aicxx . . $ petopt#saved_ai. = pop->aicxx . . $ petopt#ai.; |
| 144 | pop->saved_cicxx . . $ petopt#saved_ci. = pop->cicxx . . $ petopt#ci.; |
| 145 | |
| 146 | opt = pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.]+2; |
| 147 | if (strncasecmp(opt, "enable-", 7) == 0) |
| 148 | { |
| 149 | boolval = "true"; |
| 150 | opt += 7; |
| 151 | } |
| 152 | else if (strncasecmp(opt, "disable-", 8) == 0) |
| 153 | { |
| 154 | boolval = "false"; |
| 155 | opt += 8; |
| 156 | } |
| 157 | else if (strncasecmp(opt, "with-", 5) == 0) |
| 158 | { |
| 159 | withval = 1; |
| 160 | opt += 5; |
| 161 | } |
| 162 | else if (strncasecmp(opt, "without-", 8) == 0) |
| 163 | { |
| 164 | withval = -1; |
| 165 | opt += 8; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | arg = strchr(opt, '='); |
| 170 | if (arg) |
| 171 | len = arg - opt; |
| 172 | else |
| 173 | len = strlen(opt); |
| 174 | |
| 175 | |
| 176 | ix = -1; |
| 177 | for (i = 0; pop->povcxx . . $ petopt#pov.[i].scxx . . $ petopt_option#s. != -1; i++) |
| 178 | { |
| 179 | if (pop->povcxx . . $ petopt#pov.[i].lcxx . . $ petopt_option#l. != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`! && |
| 180 | compare_longcxx . . $ compare_long(f0be30763a490cce).(opt, pop->povcxx . . $ petopt#pov.[i].lcxx . . $ petopt_option#l., len) == 0) |
| 181 | { |
| 182 | /* Match found */ |
| 183 | if (ix < 0) |
| 184 | ix = i; |
| 185 | else |
| 186 | return POE_MULTIcxx . . $ `src/petopt.h:38:9`!; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | if (ix >= 0) |
| 192 | { |
| 193 | /* Found a unique match */ |
| 194 | |
| 195 | pop->aicxx . . $ petopt#ai.++; |
| 196 | |
| 197 | *pov = &pop->povcxx . . $ petopt#pov.[ix]; |
| 198 | |
| 199 | if (pop->povcxx . . $ petopt#pov.[ix].fcxx . . $ petopt_option#f.) |
| 200 | { |
| 201 | /* Check for (optional) argument */ |
| 202 | |
| 203 | if (boolval) |
| 204 | { |
| 205 | /* --enable-XXX or --disable-XXX */ |
| 206 | |
| 207 | if (arg || |
| 208 | (pop->povcxx . . $ petopt#pov.[ix].fcxx . . $ petopt_option#f. & POF_TYPEMASKcxx . . $ `src/petopt.h:19:9`!) != POF_BOOLcxx . . $ `src/petopt.h:24:9`!) |
| 209 | { |
| 210 | return POE_INVALIDcxx . . $ `src/petopt.h:40:9`!; |
| 211 | } |
| 212 | |
| 213 | *optarg = boolval; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | if (withval) |
| 218 | { |
| 219 | if (withval == 1) |
| 220 | { |
| 221 | if (arg) |
| 222 | *optarg = arg+1; |
| 223 | else |
| 224 | *optarg = "yes"; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | if (arg) |
| 229 | return POE_INVALIDcxx . . $ `src/petopt.h:40:9`!; |
| 230 | else |
| 231 | *optarg = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | if (arg) |
| 237 | { |
| 238 | /* Argument after "=" */ |
| 239 | *optarg = arg+1; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | if (pop->aicxx . . $ petopt#ai. < pop->argccxx . . $ petopt#argc. && |
| 244 | (pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][0] != '-' || |
| 245 | pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][0] == '\0')) |
| 246 | { |
| 247 | /* Have an argument, empty string or "-" */ |
| 248 | if (((pop->povcxx . . $ petopt#pov.[ix].fcxx . . $ petopt_option#f. & POF_TYPEMASKcxx . . $ `src/petopt.h:19:9`!) == POF_STRcxx . . $ `src/petopt.h:23:9`!) || |
| 249 | isdigit((int) pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][0])) |
| 250 | { |
| 251 | *optarg = pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.]; |
| 252 | pop->aicxx . . $ petopt#ai.++; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* Argument missing, and not optional? */ |
| 257 | if (*optarg == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`! && |
| 258 | !((pop->povcxx . . $ petopt#pov.[ix].fcxx . . $ petopt_option#f. & POF_OPTcxx . . $ `src/petopt.h:26:9`!) || |
| 259 | ((pop->povcxx . . $ petopt#pov.[ix].fcxx . . $ petopt_option#f. & POF_TYPEMASKcxx . . $ `src/petopt.h:19:9`!) == POF_BOOLcxx . . $ `src/petopt.h:24:9`!))) |
| 260 | { |
| 261 | pop->aicxx . . $ petopt#ai.--; |
| 262 | return POE_MISSINGcxx . . $ `src/petopt.h:39:9`!; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* Unknown long command line switch */ |
| 270 | return POE_OPTIONcxx . . $ `src/petopt.h:37:9`!; |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | /* Short option */ |
| 275 | |
| 276 | pop->cicxx . . $ petopt#ci. = 1; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* Short option */ |
| 281 | |
| 282 | pop->saved_aicxx . . $ petopt#saved_ai. = pop->aicxx . . $ petopt#ai.; |
| 283 | pop->saved_cicxx . . $ petopt#saved_ci. = pop->cicxx . . $ petopt#ci.; |
| 284 | |
| 285 | i = 0; |
| 286 | while (pop->povcxx . . $ petopt#pov.[i].scxx . . $ petopt_option#s. != -1 && pop->povcxx . . $ petopt#pov.[i].scxx . . $ petopt_option#s. != pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][pop->cicxx . . $ petopt#ci.]) |
| 287 | i++; |
| 288 | |
| 289 | if (pop->povcxx . . $ petopt#pov.[i].scxx . . $ petopt_option#s. == -1) |
| 290 | { |
| 291 | /* Unknown short command line switch */ |
| 292 | return POE_OPTIONcxx . . $ `src/petopt.h:37:9`!; |
| 293 | } |
| 294 | |
| 295 | /* Found a matching short option */ |
| 296 | |
| 297 | *pov = &pop->povcxx . . $ petopt#pov.[i]; |
| 298 | pop->cicxx . . $ petopt#ci.++; |
| 299 | |
| 300 | if (pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][pop->cicxx . . $ petopt#ci.] == '\0') |
| 301 | { |
| 302 | pop->aicxx . . $ petopt#ai.++; |
| 303 | pop->cicxx . . $ petopt#ci. = 0; |
| 304 | } |
| 305 | |
| 306 | /* Have an (optional) argument? */ |
| 307 | if (pop->povcxx . . $ petopt#pov.[i].fcxx . . $ petopt_option#f.) |
| 308 | { |
| 309 | if (pop->aicxx . . $ petopt#ai. < pop->argccxx . . $ petopt#argc. && |
| 310 | (pop->cicxx . . $ petopt#ci. != 0 || |
| 311 | ((pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][0] != '-') || |
| 312 | pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][1] == '\0'))) |
| 313 | { |
| 314 | if (((pop->povcxx . . $ petopt#pov.[i].fcxx . . $ petopt_option#f. & POF_TYPEMASKcxx . . $ `src/petopt.h:19:9`!) == POF_STRcxx . . $ `src/petopt.h:23:9`!) || |
| 315 | (isdigit((int) pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][pop->cicxx . . $ petopt#ci.]) || |
| 316 | pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.][pop->cicxx . . $ petopt#ci.] == '-')) |
| 317 | { |
| 318 | *optarg = pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.]+pop->cicxx . . $ petopt#ci.; |
| 319 | |
| 320 | pop->aicxx . . $ petopt#ai.++; |
| 321 | pop->cicxx . . $ petopt#ci. = 0; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* Argument missing, and not optional? */ |
| 327 | if (pop->povcxx . . $ petopt#pov.[i].fcxx . . $ petopt_option#f. && |
| 328 | !((pop->povcxx . . $ petopt#pov.[i].fcxx . . $ petopt_option#f. & POF_OPTcxx . . $ `src/petopt.h:26:9`!) || |
| 329 | (pop->povcxx . . $ petopt#pov.[i].fcxx . . $ petopt_option#f. & POF_TYPEMASKcxx . . $ `src/petopt.h:19:9`!) == POF_BOOLcxx . . $ `src/petopt.h:24:9`!) && |
| 330 | *optarg == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!) |
| 331 | { |
| 332 | if (pop->cicxx . . $ petopt#ci. == 0) |
| 333 | { |
| 334 | pop->aicxx . . $ petopt#ai.--; |
| 335 | pop->cicxx . . $ petopt#ci. = strlen(pop->argvcxx . . $ petopt#argv.[pop->aicxx . . $ petopt#ai.])-1; |
| 336 | } |
| 337 | |
| 338 | return POE_MISSINGcxx . . $ `src/petopt.h:39:9`!; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | int |
| 346 | petopt_print_errorcxx . . $ petopt_print_error(3e3c917375c6672a).(PETOPTcxx . . $ PETOPT# *pop, |
| 347 | PETOPTScxx . . $ PETOPTS# *pov, |
| 348 | int err, |
| 349 | FILE *fp) |
| 350 | { |
| 351 | char buf[3]; |
| 352 | const char *arg; |
| 353 | |
| 354 | |
| 355 | if (pop->saved_cicxx . . $ petopt#saved_ci. > 0) |
| 356 | { |
| 357 | buf[0] = '-'; |
| 358 | buf[1] = pop->argvcxx . . $ petopt#argv.[pop->saved_aicxx . . $ petopt#saved_ai.][pop->saved_cicxx . . $ petopt#saved_ci.]; |
| 359 | buf[2] = '\0'; |
| 360 | arg = buf; |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | arg = pop->argvcxx . . $ petopt#argv.[pop->saved_aicxx . . $ petopt#saved_ai.]; |
| 365 | if (arg == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!) |
| 366 | arg = ""; |
| 367 | } |
| 368 | |
| 369 | switch (err) |
| 370 | { |
| 371 | case POE_EOFcxx . . $ `src/petopt.h:35:9`!: |
| 372 | return 0; |
| 373 | |
| 374 | case POE_OPTIONcxx . . $ `src/petopt.h:37:9`!: |
| 375 | if (pop->fcxx . . $ petopt#f. & POF_SYSLOGcxx . . $ `src/petopt.h:30:9`!) |
| 376 | syslog(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "Unrecognized option: %s", arg); |
| 377 | |
| 378 | fprintf(fp, "%s: Unrecognized option: %s\n", |
| 379 | pop->argvcxx . . $ petopt#argv.[0], arg); |
| 380 | break; |
| 381 | |
| 382 | case POE_MULTIcxx . . $ `src/petopt.h:38:9`!: |
| 383 | if (pop->fcxx . . $ petopt#f. & POF_SYSLOGcxx . . $ `src/petopt.h:30:9`!) |
| 384 | syslog(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "Ambiguous option: %s", arg); |
| 385 | |
| 386 | fprintf(fp, "%s: Ambiguous option: %s\n", |
| 387 | pop->argvcxx . . $ petopt#argv.[0], arg); |
| 388 | break; |
| 389 | |
| 390 | case POE_MISSINGcxx . . $ `src/petopt.h:39:9`!: |
| 391 | if (pop->fcxx . . $ petopt#f. & POF_SYSLOGcxx . . $ `src/petopt.h:30:9`!) |
| 392 | syslog(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "Missing argument for option: %s", arg); |
| 393 | |
| 394 | fprintf(fp, "%s: Missing argument for option: %s\n", |
| 395 | pop->argvcxx . . $ petopt#argv.[0], arg); |
| 396 | break; |
| 397 | |
| 398 | case POE_INVALIDcxx . . $ `src/petopt.h:40:9`!: |
| 399 | if (pop->fcxx . . $ petopt#f. & POF_SYSLOGcxx . . $ `src/petopt.h:30:9`!) |
| 400 | syslog(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "Invalid argument for option: %s", arg); |
| 401 | |
| 402 | fprintf(fp, "%s: Invalid argument for option: %s\n", |
| 403 | pop->argvcxx . . $ petopt#argv.[0], arg); |
| 404 | break; |
| 405 | |
| 406 | case POE_INTERNALcxx . . $ `src/petopt.h:41:9`!: |
| 407 | if (pop->fcxx . . $ petopt#f. & POF_SYSLOGcxx . . $ `src/petopt.h:30:9`!) |
| 408 | syslog(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "Internal error parsing option: %s", arg); |
| 409 | |
| 410 | fprintf(fp, "%s: Internal error parsing option: %s\n", |
| 411 | pop->argvcxx . . $ petopt#argv.[0], arg); |
| 412 | break; |
| 413 | |
| 414 | default: |
| 415 | if (pop->fcxx . . $ petopt#f. & POF_SYSLOGcxx . . $ `src/petopt.h:30:9`!) |
| 416 | syslog(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "Internal options parsing error: #%d", err); |
| 417 | |
| 418 | fprintf(fp, "%s: Internal options parsing error: #%d\n", |
| 419 | pop->argvcxx . . $ petopt#argv.[0], err); |
| 420 | } |
| 421 | |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | int |
| 427 | petopt_parsecxx . . $ petopt_parse(7260f6448cc115d3).(PETOPTcxx . . $ PETOPT# *pop, |
| 428 | int *o_argc, |
| 429 | char ***o_argv) |
| 430 | { |
| 431 | int err; |
| 432 | const char *arg; |
| 433 | PETOPTScxx . . $ PETOPTS# *pov; |
| 434 | |
| 435 | |
| 436 | while ((err = petopt_parse_optioncxx . . $ petopt_parse_option(593af1eac325019).(pop, &pov, &arg)) == 0) |
| 437 | { |
| 438 | if (pop->parsecxx . . $ petopt#parse. && |
| 439 | (err = pop->parsecxx . . $ petopt#parse.(pop, pov, arg)) < 0) |
| 440 | { |
| 441 | goto Fail; |
| 442 | } |
| 443 | |
| 444 | /* Parser assigned the values */ |
| 445 | if (err == 0) |
| 446 | continue; |
| 447 | |
| 448 | /* Option not handled, and no storage assigned? */ |
| 449 | if (pov->vcxx . . $ petopt_option#v. == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!) |
| 450 | { |
| 451 | err = POE_INTERNALcxx . . $ `src/petopt.h:41:9`!; |
| 452 | goto Fail; |
| 453 | } |
| 454 | |
| 455 | switch (pov->fcxx . . $ petopt_option#f. & POF_TYPEMASKcxx . . $ `src/petopt.h:19:9`!) |
| 456 | { |
| 457 | case POF_NONEcxx . . $ `src/petopt.h:21:9`!: |
| 458 | break; |
| 459 | |
| 460 | case POF_BOOLcxx . . $ `src/petopt.h:24:9`!: |
| 461 | if (arg && *arg != '\0') |
| 462 | { |
| 463 | if (strcasecmp(arg, "true") == 0 || |
| 464 | strcasecmp(arg, "yes") == 0 || |
| 465 | strcasecmp(arg, "on") == 0) |
| 466 | { |
| 467 | *(int *)(pov->vcxx . . $ petopt_option#v.) = 1; |
| 468 | } |
| 469 | else if (strcasecmp(arg, "false") == 0 || |
| 470 | strcasecmp(arg, "no") == 0 || |
| 471 | strcasecmp(arg, "off") == 0) |
| 472 | { |
| 473 | *(int *)(pov->vcxx . . $ petopt_option#v.) = 0; |
| 474 | } |
| 475 | else |
| 476 | { |
| 477 | err = POE_INVALIDcxx . . $ `src/petopt.h:40:9`!; |
| 478 | goto Fail; |
| 479 | } |
| 480 | } |
| 481 | else |
| 482 | { |
| 483 | *(int *)(pov->vcxx . . $ petopt_option#v.) = 1; |
| 484 | } |
| 485 | break; |
| 486 | |
| 487 | case POF_INTcxx . . $ `src/petopt.h:22:9`!: |
| 488 | if (arg && *arg != '\0') |
| 489 | { |
| 490 | /* XXX: Check for integer number */ |
| 491 | *(int *)(pov->vcxx . . $ petopt_option#v.) = atoicxx . . $ atoi(f5ecf3191ed0cd93).(arg); |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | /* XXX: Check for POF_INC flag?? */ |
| 496 | ++*(int *)(pov->vcxx . . $ petopt_option#v.); |
| 497 | } |
| 498 | break; |
| 499 | |
| 500 | case POF_STRcxx . . $ `src/petopt.h:23:9`!: |
| 501 | if (pov->fcxx . . $ petopt_option#f. & POF_DUPcxx . . $ `src/petopt.h:27:9`!) |
| 502 | *(char **)(pov->vcxx . . $ petopt_option#v.) = strdup(arg); |
| 503 | else |
| 504 | *(char **)(pov->vcxx . . $ petopt_option#v.) = (char *) arg; |
| 505 | break; |
| 506 | |
| 507 | default: |
| 508 | err = POE_OPTIONcxx . . $ `src/petopt.h:37:9`!; |
| 509 | goto Fail; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if (err == POE_EOFcxx . . $ `src/petopt.h:35:9`!) |
| 514 | err = 0; |
| 515 | |
| 516 | Fail: |
| 517 | if (err) |
| 518 | err = pop->errprcxx . . $ petopt#errpr.(pop, pov, err, |
| 519 | (pop->fcxx . . $ petopt#f. & POF_NOERRPRINTcxx . . $ `src/petopt.h:31:9`!) ? NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`! : stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!); |
| 520 | |
| 521 | *o_argc = pop->oaccxx . . $ petopt#oac.; |
| 522 | *o_argv = pop->oavcxx . . $ petopt#oav.; |
| 523 | |
| 524 | return err; |
| 525 | } |
| 526 | |
| 527 | |
| 528 | |
| 529 | int |
| 530 | petopt_print_usagecxx . . $ petopt_print_usage(d7c4fb50eb813c46).(PETOPTcxx . . $ PETOPT# *pop, |
| 531 | FILE *fp) |
| 532 | { |
| 533 | int i, len; |
| 534 | struct petopt_optioncxx . . $ petopt_option# *pov; |
| 535 | |
| 536 | |
| 537 | pov = pop->povcxx . . $ petopt#pov.; |
| 538 | for (i = 0; pov[i].scxx . . $ petopt_option#s. != -1; i++) |
| 539 | { |
| 540 | if (pov[i].scxx . . $ petopt_option#s. > ' ' && pov[i].scxx . . $ petopt_option#s. <='~') |
| 541 | fprintf(fp, " -%c, ", pov[i].scxx . . $ petopt_option#s.); |
| 542 | else |
| 543 | fprintf(fp, " "); |
| 544 | |
| 545 | len = 0; |
| 546 | if (pov[i].lcxx . . $ petopt_option#l.) |
| 547 | { |
| 548 | len = fprintf(fp, "--%s", pov[i].lcxx . . $ petopt_option#l.); |
| 549 | if (pov[i].fcxx . . $ petopt_option#f.) |
| 550 | { |
| 551 | if (pov[i].fcxx . . $ petopt_option#f. & POF_OPTcxx . . $ `src/petopt.h:26:9`!) |
| 552 | len += fprintf(fp, " [ARG]"); |
| 553 | else |
| 554 | len += fprintf(fp, " ARG"); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | while (len++ < 30) |
| 559 | putchar(' '); |
| 560 | |
| 561 | if (pov[i].hcxx . . $ petopt_option#h.) |
| 562 | fputs(pov[i].hcxx . . $ petopt_option#h., fp); |
| 563 | if (pop->fcxx . . $ petopt#f. & POF_PRDEFAULTcxx . . $ `src/petopt.h:32:9`! && pov[i].vcxx . . $ petopt_option#v. != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!) |
| 564 | switch (pov[i].fcxx . . $ petopt_option#f. & POF_TYPEMASKcxx . . $ `src/petopt.h:19:9`!) |
| 565 | { |
| 566 | case POF_INTcxx . . $ `src/petopt.h:22:9`!: |
| 567 | { |
| 568 | int *iv = (int*)pov[i].vcxx . . $ petopt_option#v.; |
| 569 | if (*iv != 0) |
| 570 | fprintf(fp, " (Default: %d)", *iv); |
| 571 | } |
| 572 | break; |
| 573 | |
| 574 | case POF_STRcxx . . $ `src/petopt.h:23:9`!: |
| 575 | { |
| 576 | char *cv = (char*)pov[i].vcxx . . $ petopt_option#v.; |
| 577 | if (cv) |
| 578 | fprintf(fp, " (Default: \"%s\")", cv); |
| 579 | } |
| 580 | break; |
| 581 | } |
| 582 | putc('\n', fp); |
| 583 | } |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | int |
| 590 | petopt_setupcxx . . $ petopt_setup(9cab59fb7568ad4c).(PETOPTcxx . . $ PETOPT# **popp, |
| 591 | int f, |
| 592 | int argc, |
| 593 | char **argv, |
| 594 | PETOPTScxx . . $ PETOPTS# *pov, |
| 595 | int (*parse)(PETOPTcxx . . $ PETOPT# *pop, PETOPTScxx . . $ PETOPTS# *pov, const char *arg), |
| 596 | int (*errpr)(PETOPTcxx . . $ PETOPT# *pop, PETOPTScxx . . $ PETOPTS# *pov, int err, FILE *fp)) |
| 597 | { |
| 598 | PETOPTcxx . . $ PETOPT# *pop = (PETOPTcxx . . $ PETOPT#*)malloccxx . . $ malloc(9f008b2a9698428b).(sizeof(PETOPTcxx . . $ PETOPT#)); |
| 599 | if (pop == NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!) |
| 600 | return errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!; |
| 601 | |
| 602 | memset(pop, 0, sizeof(*pop)); |
| 603 | |
| 604 | pop->fcxx . . $ petopt#f. = f; |
| 605 | |
| 606 | pop->argccxx . . $ petopt#argc. = argc; |
| 607 | pop->argvcxx . . $ petopt#argv. = (const char **) argv; |
| 608 | pop->povcxx . . $ petopt#pov. = pov; |
| 609 | pop->parsecxx . . $ petopt#parse. = parse; |
| 610 | |
| 611 | if (errpr) |
| 612 | pop->errprcxx . . $ petopt#errpr. = errpr; |
| 613 | else |
| 614 | pop->errprcxx . . $ petopt#errpr. = petopt_print_errorcxx . . $ petopt_print_error(3e3c917375c6672a).; |
| 615 | |
| 616 | pop->aicxx . . $ petopt#ai. = 1; |
| 617 | pop->cicxx . . $ petopt#ci. = 0; |
| 618 | |
| 619 | pop->saved_aicxx . . $ petopt#saved_ai. = 0; |
| 620 | pop->saved_cicxx . . $ petopt#saved_ci. = 0; |
| 621 | |
| 622 | pop->oaccxx . . $ petopt#oac. = 1; |
| 623 | |
| 624 | pop->oavcxx . . $ petopt#oav. = (char**)calloccxx . . $ calloc(95acc9b79bb49189).(argc+1, sizeof(char *)); |
| 625 | pop->oavcxx . . $ petopt#oav.[0] = strdup(pop->argvcxx . . $ petopt#argv.[0]); |
| 626 | pop->oavcxx . . $ petopt#oav.[1] = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 627 | |
| 628 | *popp = pop; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | |
| 633 | int |
| 634 | petopt_rewindcxx . . $ petopt_rewind(c5237268d77c6cf4).(PETOPTcxx . . $ PETOPT# *pop) |
| 635 | { |
| 636 | pop->aicxx . . $ petopt#ai. = 1; |
| 637 | pop->cicxx . . $ petopt#ci. = 0; |
| 638 | |
| 639 | pop->oaccxx . . $ petopt#oac. = 1; |
| 640 | pop->oavcxx . . $ petopt#oav.[1] = NULLcxx . . $ `<external>/15298297234472979974/stddef.h:416:9`!; |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | int |
| 647 | petopt_cleanupcxx . . $ petopt_cleanup(c5237268d77c6cf4).(PETOPTcxx . . $ PETOPT# *pop) |
| 648 | { |
| 649 | int i; |
| 650 | |
| 651 | if (pop->oavcxx . . $ petopt#oav.) |
| 652 | { |
| 653 | for (i = 0; i < pop->oaccxx . . $ petopt#oac.; i++) |
| 654 | if (pop->oavcxx . . $ petopt#oav.[i]) |
| 655 | freecxx . . $ free(bd21765a0afc8e3c).(pop->oavcxx . . $ petopt#oav.[i]); |
| 656 | freecxx . . $ free(bd21765a0afc8e3c).(pop->oavcxx . . $ petopt#oav.); |
| 657 | } |
| 658 | |
| 659 | freecxx . . $ free(bd21765a0afc8e3c).(pop); |
| 660 | return 0; |
| 661 | } |