diff options
| author | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-07-25 23:25:00 -0500 |
|---|---|---|
| committer | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-07-25 23:25:00 -0500 |
| commit | 136476ddeb93d259dce19ec9de1e4c2a24ab3f8c (patch) | |
| tree | b0b0f6d928cd6888aeb31566f09b2678cd11bc19 | |
| parent | bd9fde3a2aea12b34f5c2d5351ef1d47fe780f71 (diff) | |
| download | life-136476ddeb93d259dce19ec9de1e4c2a24ab3f8c.tar.gz life-136476ddeb93d259dce19ec9de1e4c2a24ab3f8c.tar.bz2 life-136476ddeb93d259dce19ec9de1e4c2a24ab3f8c.zip | |
Use gengetopt for commandline argslife-0.4.0
Added life.ggo
Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
| -rw-r--r-- | cmdline-life.c | 604 | ||||
| -rw-r--r-- | cmdline-life.h | 201 | ||||
| -rw-r--r-- | life.c | 39 | ||||
| -rw-r--r-- | life.ggo | 5 | ||||
| -rw-r--r-- | life.h | 3 |
5 files changed, 834 insertions, 18 deletions
diff --git a/cmdline-life.c b/cmdline-life.c new file mode 100644 index 0000000..5a11296 --- /dev/null +++ b/cmdline-life.c | |||
| @@ -0,0 +1,604 @@ | |||
| 1 | /* | ||
| 2 | File autogenerated by gengetopt version 2.22.6 | ||
| 3 | generated with the following command: | ||
| 4 | gengetopt --file-name=cmdline-life | ||
| 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 | |||
| 11 | /* If we use autoconf. */ | ||
| 12 | #ifdef HAVE_CONFIG_H | ||
| 13 | #include "config.h" | ||
| 14 | #endif | ||
| 15 | |||
| 16 | #include <stdio.h> | ||
| 17 | #include <stdlib.h> | ||
| 18 | #include <string.h> | ||
| 19 | |||
| 20 | #ifndef FIX_UNUSED | ||
| 21 | #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */ | ||
| 22 | #endif | ||
| 23 | |||
| 24 | #include <getopt.h> | ||
| 25 | |||
| 26 | #include "cmdline-life.h" | ||
| 27 | |||
| 28 | const char *gengetopt_args_info_purpose = ""; | ||
| 29 | |||
| 30 | const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]..."; | ||
| 31 | |||
| 32 | const char *gengetopt_args_info_versiontext = ""; | ||
| 33 | |||
| 34 | const char *gengetopt_args_info_description = ""; | ||
| 35 | |||
| 36 | const char *gengetopt_args_info_detailed_help[] = { | ||
| 37 | " --help Print help and exit", | ||
| 38 | " --detailed-help Print help, including all details and hidden\n options, and exit", | ||
| 39 | " -V, --version Print version and exit", | ||
| 40 | " -r, --ruleint=Rule integer Specify rule-int on command line", | ||
| 41 | " -w, --width=width Specify width", | ||
| 42 | " -h, --height=height Specify height", | ||
| 43 | " -d, --delay=delay Specify delay time", | ||
| 44 | " Specify delay time, multiplied by 10ms", | ||
| 45 | 0 | ||
| 46 | }; | ||
| 47 | |||
| 48 | static void | ||
| 49 | init_help_array(void) | ||
| 50 | { | ||
| 51 | gengetopt_args_info_help[0] = gengetopt_args_info_detailed_help[0]; | ||
| 52 | gengetopt_args_info_help[1] = gengetopt_args_info_detailed_help[1]; | ||
| 53 | gengetopt_args_info_help[2] = gengetopt_args_info_detailed_help[2]; | ||
| 54 | gengetopt_args_info_help[3] = gengetopt_args_info_detailed_help[3]; | ||
| 55 | gengetopt_args_info_help[4] = gengetopt_args_info_detailed_help[4]; | ||
| 56 | gengetopt_args_info_help[5] = gengetopt_args_info_detailed_help[5]; | ||
| 57 | gengetopt_args_info_help[6] = gengetopt_args_info_detailed_help[6]; | ||
| 58 | gengetopt_args_info_help[7] = 0; | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | const char *gengetopt_args_info_help[8]; | ||
| 63 | |||
| 64 | typedef enum {ARG_NO | ||
| 65 | , ARG_INT | ||
| 66 | } cmdline_parser_arg_type; | ||
| 67 | |||
| 68 | static | ||
| 69 | void clear_given (struct gengetopt_args_info *args_info); | ||
| 70 | static | ||
| 71 | void clear_args (struct gengetopt_args_info *args_info); | ||
| 72 | |||
| 73 | static int | ||
| 74 | cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info, | ||
| 75 | struct cmdline_parser_params *params, const char *additional_error); | ||
| 76 | |||
| 77 | |||
| 78 | static char * | ||
| 79 | gengetopt_strdup (const char *s); | ||
| 80 | |||
| 81 | static | ||
| 82 | void clear_given (struct gengetopt_args_info *args_info) | ||
| 83 | { | ||
| 84 | args_info->help_given = 0 ; | ||
| 85 | args_info->detailed_help_given = 0 ; | ||
| 86 | args_info->version_given = 0 ; | ||
| 87 | args_info->ruleint_given = 0 ; | ||
| 88 | args_info->width_given = 0 ; | ||
| 89 | args_info->height_given = 0 ; | ||
| 90 | args_info->delay_given = 0 ; | ||
| 91 | } | ||
| 92 | |||
| 93 | static | ||
| 94 | void clear_args (struct gengetopt_args_info *args_info) | ||
| 95 | { | ||
| 96 | FIX_UNUSED (args_info); | ||
| 97 | args_info->ruleint_orig = NULL; | ||
| 98 | args_info->width_orig = NULL; | ||
| 99 | args_info->height_orig = NULL; | ||
| 100 | args_info->delay_orig = NULL; | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | static | ||
| 105 | void init_args_info(struct gengetopt_args_info *args_info) | ||
| 106 | { | ||
| 107 | |||
| 108 | init_help_array(); | ||
| 109 | args_info->help_help = gengetopt_args_info_detailed_help[0] ; | ||
| 110 | args_info->detailed_help_help = gengetopt_args_info_detailed_help[1] ; | ||
| 111 | args_info->version_help = gengetopt_args_info_detailed_help[2] ; | ||
| 112 | args_info->ruleint_help = gengetopt_args_info_detailed_help[3] ; | ||
| 113 | args_info->width_help = gengetopt_args_info_detailed_help[4] ; | ||
| 114 | args_info->height_help = gengetopt_args_info_detailed_help[5] ; | ||
| 115 | args_info->delay_help = gengetopt_args_info_detailed_help[6] ; | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | void | ||
| 120 | cmdline_parser_print_version (void) | ||
| 121 | { | ||
| 122 | printf ("%s %s\n", | ||
| 123 | (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE), | ||
| 124 | CMDLINE_PARSER_VERSION); | ||
| 125 | |||
| 126 | if (strlen(gengetopt_args_info_versiontext) > 0) | ||
| 127 | printf("\n%s\n", gengetopt_args_info_versiontext); | ||
| 128 | } | ||
| 129 | |||
| 130 | static void print_help_common(void) { | ||
| 131 | cmdline_parser_print_version (); | ||
| 132 | |||
| 133 | if (strlen(gengetopt_args_info_purpose) > 0) | ||
| 134 | printf("\n%s\n", gengetopt_args_info_purpose); | ||
| 135 | |||
| 136 | if (strlen(gengetopt_args_info_usage) > 0) | ||
| 137 | printf("\n%s\n", gengetopt_args_info_usage); | ||
| 138 | |||
| 139 | printf("\n"); | ||
| 140 | |||
| 141 | if (strlen(gengetopt_args_info_description) > 0) | ||
| 142 | printf("%s\n\n", gengetopt_args_info_description); | ||
| 143 | } | ||
| 144 | |||
| 145 | void | ||
| 146 | cmdline_parser_print_help (void) | ||
| 147 | { | ||
| 148 | int i = 0; | ||
| 149 | print_help_common(); | ||
| 150 | while (gengetopt_args_info_help[i]) | ||
| 151 | printf("%s\n", gengetopt_args_info_help[i++]); | ||
| 152 | } | ||
| 153 | |||
| 154 | void | ||
| 155 | cmdline_parser_print_detailed_help (void) | ||
| 156 | { | ||
| 157 | int i = 0; | ||
| 158 | print_help_common(); | ||
| 159 | while (gengetopt_args_info_detailed_help[i]) | ||
| 160 | printf("%s\n", gengetopt_args_info_detailed_help[i++]); | ||
| 161 | } | ||
| 162 | |||
| 163 | void | ||
| 164 | cmdline_parser_init (struct gengetopt_args_info *args_info) | ||
| 165 | { | ||
| 166 | clear_given (args_info); | ||
| 167 | clear_args (args_info); | ||
| 168 | init_args_info (args_info); | ||
| 169 | } | ||
| 170 | |||
| 171 | void | ||
| 172 | cmdline_parser_params_init(struct cmdline_parser_params *params) | ||
| 173 | { | ||
| 174 | if (params) | ||
| 175 | { | ||
| 176 | params->override = 0; | ||
| 177 | params->initialize = 1; | ||
| 178 | params->check_required = 1; | ||
| 179 | params->check_ambiguity = 0; | ||
| 180 | params->print_errors = 1; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | struct cmdline_parser_params * | ||
| 185 | cmdline_parser_params_create(void) | ||
| 186 | { | ||
| 187 | struct cmdline_parser_params *params = | ||
| 188 | (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params)); | ||
| 189 | cmdline_parser_params_init(params); | ||
| 190 | return params; | ||
| 191 | } | ||
| 192 | |||
| 193 | static void | ||
| 194 | free_string_field (char **s) | ||
| 195 | { | ||
| 196 | if (*s) | ||
| 197 | { | ||
| 198 | free (*s); | ||
| 199 | *s = 0; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | static void | ||
| 205 | cmdline_parser_release (struct gengetopt_args_info *args_info) | ||
| 206 | { | ||
| 207 | |||
| 208 | free_string_field (&(args_info->ruleint_orig)); | ||
| 209 | free_string_field (&(args_info->width_orig)); | ||
| 210 | free_string_field (&(args_info->height_orig)); | ||
| 211 | free_string_field (&(args_info->delay_orig)); | ||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | clear_given (args_info); | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | static void | ||
| 220 | write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[]) | ||
| 221 | { | ||
| 222 | FIX_UNUSED (values); | ||
| 223 | if (arg) { | ||
| 224 | fprintf(outfile, "%s=\"%s\"\n", opt, arg); | ||
| 225 | } else { | ||
| 226 | fprintf(outfile, "%s\n", opt); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | int | ||
| 232 | cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info) | ||
| 233 | { | ||
| 234 | int i = 0; | ||
| 235 | |||
| 236 | if (!outfile) | ||
| 237 | { | ||
| 238 | fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE); | ||
| 239 | return EXIT_FAILURE; | ||
| 240 | } | ||
| 241 | |||
| 242 | if (args_info->help_given) | ||
| 243 | write_into_file(outfile, "help", 0, 0 ); | ||
| 244 | if (args_info->detailed_help_given) | ||
| 245 | write_into_file(outfile, "detailed-help", 0, 0 ); | ||
| 246 | if (args_info->version_given) | ||
| 247 | write_into_file(outfile, "version", 0, 0 ); | ||
| 248 | if (args_info->ruleint_given) | ||
| 249 | write_into_file(outfile, "ruleint", args_info->ruleint_orig, 0); | ||
| 250 | if (args_info->width_given) | ||
| 251 | write_into_file(outfile, "width", args_info->width_orig, 0); | ||
| 252 | if (args_info->height_given) | ||
| 253 | write_into_file(outfile, "height", args_info->height_orig, 0); | ||
| 254 | if (args_info->delay_given) | ||
| 255 | write_into_file(outfile, "delay", args_info->delay_orig, 0); | ||
| 256 | |||
| 257 | |||
| 258 | i = EXIT_SUCCESS; | ||
| 259 | return i; | ||
| 260 | } | ||
| 261 | |||
| 262 | int | ||
| 263 | cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info) | ||
| 264 | { | ||
| 265 | FILE *outfile; | ||
| 266 | int i = 0; | ||
| 267 | |||
| 268 | outfile = fopen(filename, "w"); | ||
| 269 | |||
| 270 | if (!outfile) | ||
| 271 | { | ||
| 272 | fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename); | ||
| 273 | return EXIT_FAILURE; | ||
| 274 | } | ||
| 275 | |||
| 276 | i = cmdline_parser_dump(outfile, args_info); | ||
| 277 | fclose (outfile); | ||
| 278 | |||
| 279 | return i; | ||
| 280 | } | ||
| 281 | |||
| 282 | void | ||
| 283 | cmdline_parser_free (struct gengetopt_args_info *args_info) | ||
| 284 | { | ||
| 285 | cmdline_parser_release (args_info); | ||
| 286 | } | ||
| 287 | |||
| 288 | /** @brief replacement of strdup, which is not standard */ | ||
| 289 | char * | ||
| 290 | gengetopt_strdup (const char *s) | ||
| 291 | { | ||
| 292 | char *result = 0; | ||
| 293 | if (!s) | ||
| 294 | return result; | ||
| 295 | |||
| 296 | result = (char*)malloc(strlen(s) + 1); | ||
| 297 | if (result == (char*)0) | ||
| 298 | return (char*)0; | ||
| 299 | strcpy(result, s); | ||
| 300 | return result; | ||
| 301 | } | ||
| 302 | |||
| 303 | int | ||
| 304 | cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info) | ||
| 305 | { | ||
| 306 | return cmdline_parser2 (argc, argv, args_info, 0, 1, 1); | ||
| 307 | } | ||
| 308 | |||
| 309 | int | ||
| 310 | cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info, | ||
| 311 | struct cmdline_parser_params *params) | ||
| 312 | { | ||
| 313 | int result; | ||
| 314 | result = cmdline_parser_internal (argc, argv, args_info, params, 0); | ||
| 315 | |||
| 316 | if (result == EXIT_FAILURE) | ||
| 317 | { | ||
| 318 | cmdline_parser_free (args_info); | ||
| 319 | exit (EXIT_FAILURE); | ||
| 320 | } | ||
| 321 | |||
| 322 | return result; | ||
| 323 | } | ||
| 324 | |||
| 325 | int | ||
| 326 | cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required) | ||
| 327 | { | ||
| 328 | int result; | ||
| 329 | struct cmdline_parser_params params; | ||
| 330 | |||
| 331 | params.override = override; | ||
| 332 | params.initialize = initialize; | ||
| 333 | params.check_required = check_required; | ||
| 334 | params.check_ambiguity = 0; | ||
| 335 | params.print_errors = 1; | ||
| 336 | |||
| 337 | result = cmdline_parser_internal (argc, argv, args_info, ¶ms, 0); | ||
| 338 | |||
| 339 | if (result == EXIT_FAILURE) | ||
| 340 | { | ||
| 341 | cmdline_parser_free (args_info); | ||
| 342 | exit (EXIT_FAILURE); | ||
| 343 | } | ||
| 344 | |||
| 345 | return result; | ||
| 346 | } | ||
| 347 | |||
| 348 | int | ||
| 349 | cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name) | ||
| 350 | { | ||
| 351 | FIX_UNUSED (args_info); | ||
| 352 | FIX_UNUSED (prog_name); | ||
| 353 | return EXIT_SUCCESS; | ||
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | static char *package_name = 0; | ||
| 358 | |||
| 359 | /** | ||
| 360 | * @brief updates an option | ||
| 361 | * @param field the generic pointer to the field to update | ||
| 362 | * @param orig_field the pointer to the orig field | ||
| 363 | * @param field_given the pointer to the number of occurrence of this option | ||
| 364 | * @param prev_given the pointer to the number of occurrence already seen | ||
| 365 | * @param value the argument for this option (if null no arg was specified) | ||
| 366 | * @param possible_values the possible values for this option (if specified) | ||
| 367 | * @param default_value the default value (in case the option only accepts fixed values) | ||
| 368 | * @param arg_type the type of this option | ||
| 369 | * @param check_ambiguity @see cmdline_parser_params.check_ambiguity | ||
| 370 | * @param override @see cmdline_parser_params.override | ||
| 371 | * @param no_free whether to free a possible previous value | ||
| 372 | * @param multiple_option whether this is a multiple option | ||
| 373 | * @param long_opt the corresponding long option | ||
| 374 | * @param short_opt the corresponding short option (or '-' if none) | ||
| 375 | * @param additional_error possible further error specification | ||
| 376 | */ | ||
| 377 | static | ||
| 378 | int update_arg(void *field, char **orig_field, | ||
| 379 | unsigned int *field_given, unsigned int *prev_given, | ||
| 380 | char *value, const char *possible_values[], | ||
| 381 | const char *default_value, | ||
| 382 | cmdline_parser_arg_type arg_type, | ||
| 383 | int check_ambiguity, int override, | ||
| 384 | int no_free, int multiple_option, | ||
| 385 | const char *long_opt, char short_opt, | ||
| 386 | const char *additional_error) | ||
| 387 | { | ||
| 388 | char *stop_char = 0; | ||
| 389 | const char *val = value; | ||
| 390 | int found; | ||
| 391 | FIX_UNUSED (field); | ||
| 392 | |||
| 393 | stop_char = 0; | ||
| 394 | found = 0; | ||
| 395 | |||
| 396 | if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given))) | ||
| 397 | { | ||
| 398 | if (short_opt != '-') | ||
| 399 | fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", | ||
| 400 | package_name, long_opt, short_opt, | ||
| 401 | (additional_error ? additional_error : "")); | ||
| 402 | else | ||
| 403 | fprintf (stderr, "%s: `--%s' option given more than once%s\n", | ||
| 404 | package_name, long_opt, | ||
| 405 | (additional_error ? additional_error : "")); | ||
| 406 | return 1; /* failure */ | ||
| 407 | } | ||
| 408 | |||
| 409 | FIX_UNUSED (default_value); | ||
| 410 | |||
| 411 | if (field_given && *field_given && ! override) | ||
| 412 | return 0; | ||
| 413 | if (prev_given) | ||
| 414 | (*prev_given)++; | ||
| 415 | if (field_given) | ||
| 416 | (*field_given)++; | ||
| 417 | if (possible_values) | ||
| 418 | val = possible_values[found]; | ||
| 419 | |||
| 420 | switch(arg_type) { | ||
| 421 | case ARG_INT: | ||
| 422 | if (val) *((int *)field) = strtol (val, &stop_char, 0); | ||
| 423 | break; | ||
| 424 | default: | ||
| 425 | break; | ||
| 426 | }; | ||
| 427 | |||
| 428 | /* check numeric conversion */ | ||
| 429 | switch(arg_type) { | ||
| 430 | case ARG_INT: | ||
| 431 | if (val && !(stop_char && *stop_char == '\0')) { | ||
| 432 | fprintf(stderr, "%s: invalid numeric value: %s\n", package_name, val); | ||
| 433 | return 1; /* failure */ | ||
| 434 | } | ||
| 435 | break; | ||
| 436 | default: | ||
| 437 | ; | ||
| 438 | }; | ||
| 439 | |||
| 440 | /* store the original value */ | ||
| 441 | switch(arg_type) { | ||
| 442 | case ARG_NO: | ||
| 443 | break; | ||
| 444 | default: | ||
| 445 | if (value && orig_field) { | ||
| 446 | if (no_free) { | ||
| 447 | *orig_field = value; | ||
| 448 | } else { | ||
| 449 | if (*orig_field) | ||
| 450 | free (*orig_field); /* free previous string */ | ||
| 451 | *orig_field = gengetopt_strdup (value); | ||
| 452 | } | ||
| 453 | } | ||
| 454 | }; | ||
| 455 | |||
| 456 | return 0; /* OK */ | ||
| 457 | } | ||
| 458 | |||
| 459 | |||
| 460 | int | ||
| 461 | cmdline_parser_internal ( | ||
| 462 | int argc, char **argv, struct gengetopt_args_info *args_info, | ||
| 463 | struct cmdline_parser_params *params, const char *additional_error) | ||
| 464 | { | ||
| 465 | int c; /* Character of the parsed option. */ | ||
| 466 | |||
| 467 | int error_occurred = 0; | ||
| 468 | struct gengetopt_args_info local_args_info; | ||
| 469 | |||
| 470 | int override; | ||
| 471 | int initialize; | ||
| 472 | int check_required; | ||
| 473 | int check_ambiguity; | ||
| 474 | |||
| 475 | package_name = argv[0]; | ||
| 476 | |||
| 477 | override = params->override; | ||
| 478 | initialize = params->initialize; | ||
| 479 | check_required = params->check_required; | ||
| 480 | check_ambiguity = params->check_ambiguity; | ||
| 481 | |||
| 482 | if (initialize) | ||
| 483 | cmdline_parser_init (args_info); | ||
| 484 | |||
| 485 | cmdline_parser_init (&local_args_info); | ||
| 486 | |||
| 487 | optarg = 0; | ||
| 488 | optind = 0; | ||
| 489 | opterr = params->print_errors; | ||
| 490 | optopt = '?'; | ||
| 491 | |||
| 492 | while (1) | ||
| 493 | { | ||
| 494 | int option_index = 0; | ||
| 495 | |||
| 496 | static struct option long_options[] = { | ||
| 497 | { "help", 0, NULL, 0 }, | ||
| 498 | { "detailed-help", 0, NULL, 0 }, | ||
| 499 | { "version", 0, NULL, 'V' }, | ||
| 500 | { "ruleint", 1, NULL, 'r' }, | ||
| 501 | { "width", 1, NULL, 'w' }, | ||
| 502 | { "height", 1, NULL, 'h' }, | ||
| 503 | { "delay", 1, NULL, 'd' }, | ||
| 504 | { 0, 0, 0, 0 } | ||
| 505 | }; | ||
| 506 | |||
| 507 | c = getopt_long (argc, argv, "Vr:w:h:d:", long_options, &option_index); | ||
| 508 | |||
| 509 | if (c == -1) break; /* Exit from `while (1)' loop. */ | ||
| 510 | |||
| 511 | switch (c) | ||
| 512 | { | ||
| 513 | case 'V': /* Print version and exit. */ | ||
| 514 | cmdline_parser_print_version (); | ||
| 515 | cmdline_parser_free (&local_args_info); | ||
| 516 | exit (EXIT_SUCCESS); | ||
| 517 | |||
| 518 | case 'r': /* Specify rule-int on command line. */ | ||
| 519 | |||
| 520 | |||
| 521 | if (update_arg( (void *)&(args_info->ruleint_arg), | ||
| 522 | &(args_info->ruleint_orig), &(args_info->ruleint_given), | ||
| 523 | &(local_args_info.ruleint_given), optarg, 0, 0, ARG_INT, | ||
| 524 | check_ambiguity, override, 0, 0, | ||
| 525 | "ruleint", 'r', | ||
| 526 | additional_error)) | ||
| 527 | goto failure; | ||
| 528 | |||
| 529 | break; | ||
| 530 | case 'w': /* Specify width. */ | ||
| 531 | |||
| 532 | |||
| 533 | if (update_arg( (void *)&(args_info->width_arg), | ||
| 534 | &(args_info->width_orig), &(args_info->width_given), | ||
| 535 | &(local_args_info.width_given), optarg, 0, 0, ARG_INT, | ||
| 536 | check_ambiguity, override, 0, 0, | ||
| 537 | "width", 'w', | ||
| 538 | additional_error)) | ||
| 539 | goto failure; | ||
| 540 | |||
| 541 | break; | ||
| 542 | case 'h': /* Specify height. */ | ||
| 543 | |||
| 544 | |||
| 545 | if (update_arg( (void *)&(args_info->height_arg), | ||
| 546 | &(args_info->height_orig), &(args_info->height_given), | ||
| 547 | &(local_args_info.height_given), optarg, 0, 0, ARG_INT, | ||
| 548 | check_ambiguity, override, 0, 0, | ||
| 549 | "height", 'h', | ||
| 550 | additional_error)) | ||
| 551 | goto failure; | ||
| 552 | |||
| 553 | break; | ||
| 554 | case 'd': /* Specify delay time. */ | ||
| 555 | |||
| 556 | |||
| 557 | if (update_arg( (void *)&(args_info->delay_arg), | ||
| 558 | &(args_info->delay_orig), &(args_info->delay_given), | ||
| 559 | &(local_args_info.delay_given), optarg, 0, 0, ARG_INT, | ||
| 560 | check_ambiguity, override, 0, 0, | ||
| 561 | "delay", 'd', | ||
| 562 | additional_error)) | ||
| 563 | goto failure; | ||
| 564 | |||
| 565 | break; | ||
| 566 | |||
| 567 | case 0: /* Long option with no short option */ | ||
| 568 | if (strcmp (long_options[option_index].name, "help") == 0) { | ||
| 569 | cmdline_parser_print_help (); | ||
| 570 | cmdline_parser_free (&local_args_info); | ||
| 571 | exit (EXIT_SUCCESS); | ||
| 572 | } | ||
| 573 | |||
| 574 | if (strcmp (long_options[option_index].name, "detailed-help") == 0) { | ||
| 575 | cmdline_parser_print_detailed_help (); | ||
| 576 | cmdline_parser_free (&local_args_info); | ||
| 577 | exit (EXIT_SUCCESS); | ||
| 578 | } | ||
| 579 | |||
| 580 | case '?': /* Invalid option. */ | ||
| 581 | /* `getopt_long' already printed an error message. */ | ||
| 582 | goto failure; | ||
| 583 | |||
| 584 | default: /* bug: option not considered. */ | ||
| 585 | fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : "")); | ||
| 586 | abort (); | ||
| 587 | } /* switch */ | ||
| 588 | } /* while */ | ||
| 589 | |||
| 590 | |||
| 591 | |||
| 592 | |||
| 593 | cmdline_parser_release (&local_args_info); | ||
| 594 | |||
| 595 | if ( error_occurred ) | ||
| 596 | return (EXIT_FAILURE); | ||
| 597 | |||
| 598 | return 0; | ||
| 599 | |||
| 600 | failure: | ||
| 601 | |||
| 602 | cmdline_parser_release (&local_args_info); | ||
| 603 | return (EXIT_FAILURE); | ||
| 604 | } | ||
diff --git a/cmdline-life.h b/cmdline-life.h new file mode 100644 index 0000000..b9d0629 --- /dev/null +++ b/cmdline-life.h | |||
| @@ -0,0 +1,201 @@ | |||
| 1 | /** @file cmdline-life.h | ||
| 2 | * @brief The header file for the command line option parser | ||
| 3 | * generated by GNU Gengetopt version 2.22.6 | ||
| 4 | * http://www.gnu.org/software/gengetopt. | ||
| 5 | * DO NOT modify this file, since it can be overwritten | ||
| 6 | * @author GNU Gengetopt by Lorenzo Bettini */ | ||
| 7 | |||
| 8 | #ifndef CMDLINE_LIFE_H | ||
| 9 | #define CMDLINE_LIFE_H | ||
| 10 | |||
| 11 | /* If we use autoconf. */ | ||
| 12 | #ifdef HAVE_CONFIG_H | ||
| 13 | #include "config.h" | ||
| 14 | #endif | ||
| 15 | |||
| 16 | #include <stdio.h> /* for FILE */ | ||
| 17 | |||
| 18 | #ifdef __cplusplus | ||
| 19 | extern "C" { | ||
| 20 | #endif /* __cplusplus */ | ||
| 21 | |||
| 22 | #ifndef CMDLINE_PARSER_PACKAGE | ||
| 23 | /** @brief the program name (used for printing errors) */ | ||
| 24 | #define CMDLINE_PARSER_PACKAGE PACKAGE | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #ifndef CMDLINE_PARSER_PACKAGE_NAME | ||
| 28 | /** @brief the complete program name (used for help and version) */ | ||
| 29 | #ifdef PACKAGE_NAME | ||
| 30 | #define CMDLINE_PARSER_PACKAGE_NAME PACKAGE_NAME | ||
| 31 | #else | ||
| 32 | #define CMDLINE_PARSER_PACKAGE_NAME PACKAGE | ||
| 33 | #endif | ||
| 34 | #endif | ||
| 35 | |||
| 36 | #ifndef CMDLINE_PARSER_VERSION | ||
| 37 | /** @brief the program version */ | ||
| 38 | #define CMDLINE_PARSER_VERSION VERSION | ||
| 39 | #endif | ||
| 40 | |||
| 41 | /** @brief Where the command line options are stored */ | ||
| 42 | struct gengetopt_args_info | ||
| 43 | { | ||
| 44 | const char *help_help; /**< @brief Print help and exit help description. */ | ||
| 45 | const char *detailed_help_help; /**< @brief Print help, including all details and hidden options, and exit help description. */ | ||
| 46 | const char *version_help; /**< @brief Print version and exit help description. */ | ||
| 47 | int ruleint_arg; /**< @brief Specify rule-int on command line. */ | ||
| 48 | char * ruleint_orig; /**< @brief Specify rule-int on command line original value given at command line. */ | ||
| 49 | const char *ruleint_help; /**< @brief Specify rule-int on command line help description. */ | ||
| 50 | int width_arg; /**< @brief Specify width. */ | ||
| 51 | char * width_orig; /**< @brief Specify width original value given at command line. */ | ||
| 52 | const char *width_help; /**< @brief Specify width help description. */ | ||
| 53 | int height_arg; /**< @brief Specify height. */ | ||
| 54 | char * height_orig; /**< @brief Specify height original value given at command line. */ | ||
| 55 | const char *height_help; /**< @brief Specify height help description. */ | ||
| 56 | int delay_arg; /**< @brief Specify delay time. */ | ||
| 57 | char * delay_orig; /**< @brief Specify delay time original value given at command line. */ | ||
| 58 | const char *delay_help; /**< @brief Specify delay time help description. */ | ||
| 59 | |||
| 60 | unsigned int help_given ; /**< @brief Whether help was given. */ | ||
| 61 | unsigned int detailed_help_given ; /**< @brief Whether detailed-help was given. */ | ||
| 62 | unsigned int version_given ; /**< @brief Whether version was given. */ | ||
| 63 | unsigned int ruleint_given ; /**< @brief Whether ruleint was given. */ | ||
| 64 | unsigned int width_given ; /**< @brief Whether width was given. */ | ||
| 65 | unsigned int height_given ; /**< @brief Whether height was given. */ | ||
| 66 | unsigned int delay_given ; /**< @brief Whether delay was given. */ | ||
| 67 | |||
| 68 | } ; | ||
| 69 | |||
| 70 | /** @brief The additional parameters to pass to parser functions */ | ||
| 71 | struct cmdline_parser_params | ||
| 72 | { | ||
| 73 | int override; /**< @brief whether to override possibly already present options (default 0) */ | ||
| 74 | int initialize; /**< @brief whether to initialize the option structure gengetopt_args_info (default 1) */ | ||
| 75 | int check_required; /**< @brief whether to check that all required options were provided (default 1) */ | ||
| 76 | int check_ambiguity; /**< @brief whether to check for options already specified in the option structure gengetopt_args_info (default 0) */ | ||
| 77 | int print_errors; /**< @brief whether getopt_long should print an error message for a bad option (default 1) */ | ||
| 78 | } ; | ||
| 79 | |||
| 80 | /** @brief the purpose string of the program */ | ||
| 81 | extern const char *gengetopt_args_info_purpose; | ||
| 82 | /** @brief the usage string of the program */ | ||
| 83 | extern const char *gengetopt_args_info_usage; | ||
| 84 | /** @brief the description string of the program */ | ||
| 85 | extern const char *gengetopt_args_info_description; | ||
| 86 | /** @brief all the lines making the help output */ | ||
| 87 | extern const char *gengetopt_args_info_help[]; | ||
| 88 | /** @brief all the lines making the detailed help output (including hidden options and details) */ | ||
| 89 | extern const char *gengetopt_args_info_detailed_help[]; | ||
| 90 | |||
| 91 | /** | ||
| 92 | * The command line parser | ||
| 93 | * @param argc the number of command line options | ||
| 94 | * @param argv the command line options | ||
| 95 | * @param args_info the structure where option information will be stored | ||
| 96 | * @return 0 if everything went fine, NON 0 if an error took place | ||
| 97 | */ | ||
| 98 | int cmdline_parser (int argc, char **argv, | ||
| 99 | struct gengetopt_args_info *args_info); | ||
| 100 | |||
| 101 | /** | ||
| 102 | * The command line parser (version with additional parameters - deprecated) | ||
| 103 | * @param argc the number of command line options | ||
| 104 | * @param argv the command line options | ||
| 105 | * @param args_info the structure where option information will be stored | ||
| 106 | * @param override whether to override possibly already present options | ||
| 107 | * @param initialize whether to initialize the option structure my_args_info | ||
| 108 | * @param check_required whether to check that all required options were provided | ||
| 109 | * @return 0 if everything went fine, NON 0 if an error took place | ||
| 110 | * @deprecated use cmdline_parser_ext() instead | ||
| 111 | */ | ||
| 112 | int cmdline_parser2 (int argc, char **argv, | ||
| 113 | struct gengetopt_args_info *args_info, | ||
| 114 | int override, int initialize, int check_required); | ||
| 115 | |||
| 116 | /** | ||
| 117 | * The command line parser (version with additional parameters) | ||
| 118 | * @param argc the number of command line options | ||
| 119 | * @param argv the command line options | ||
| 120 | * @param args_info the structure where option information will be stored | ||
| 121 | * @param params additional parameters for the parser | ||
| 122 | * @return 0 if everything went fine, NON 0 if an error took place | ||
| 123 | */ | ||
| 124 | int cmdline_parser_ext (int argc, char **argv, | ||
| 125 | struct gengetopt_args_info *args_info, | ||
| 126 | struct cmdline_parser_params *params); | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Save the contents of the option struct into an already open FILE stream. | ||
| 130 | * @param outfile the stream where to dump options | ||
| 131 | * @param args_info the option struct to dump | ||
| 132 | * @return 0 if everything went fine, NON 0 if an error took place | ||
| 133 | */ | ||
| 134 | int cmdline_parser_dump(FILE *outfile, | ||
| 135 | struct gengetopt_args_info *args_info); | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Save the contents of the option struct into a (text) file. | ||
| 139 | * This file can be read by the config file parser (if generated by gengetopt) | ||
| 140 | * @param filename the file where to save | ||
| 141 | * @param args_info the option struct to save | ||
| 142 | * @return 0 if everything went fine, NON 0 if an error took place | ||
| 143 | */ | ||
| 144 | int cmdline_parser_file_save(const char *filename, | ||
| 145 | struct gengetopt_args_info *args_info); | ||
| 146 | |||
| 147 | /** | ||
| 148 | * Print the help | ||
| 149 | */ | ||
| 150 | void cmdline_parser_print_help(void); | ||
| 151 | /** | ||
| 152 | * Print the detailed help (including hidden options and details) | ||
| 153 | */ | ||
| 154 | void cmdline_parser_print_detailed_help(void); | ||
| 155 | /** | ||
| 156 | * Print the version | ||
| 157 | */ | ||
| 158 | void cmdline_parser_print_version(void); | ||
| 159 | |||
| 160 | /** | ||
| 161 | * Initializes all the fields a cmdline_parser_params structure | ||
| 162 | * to their default values | ||
| 163 | * @param params the structure to initialize | ||
| 164 | */ | ||
| 165 | void cmdline_parser_params_init(struct cmdline_parser_params *params); | ||
| 166 | |||
| 167 | /** | ||
| 168 | * Allocates dynamically a cmdline_parser_params structure and initializes | ||
| 169 | * all its fields to their default values | ||
| 170 | * @return the created and initialized cmdline_parser_params structure | ||
| 171 | */ | ||
| 172 | struct cmdline_parser_params *cmdline_parser_params_create(void); | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Initializes the passed gengetopt_args_info structure's fields | ||
| 176 | * (also set default values for options that have a default) | ||
| 177 | * @param args_info the structure to initialize | ||
| 178 | */ | ||
| 179 | void cmdline_parser_init (struct gengetopt_args_info *args_info); | ||
| 180 | /** | ||
| 181 | * Deallocates the string fields of the gengetopt_args_info structure | ||
| 182 | * (but does not deallocate the structure itself) | ||
| 183 | * @param args_info the structure to deallocate | ||
| 184 | */ | ||
| 185 | void cmdline_parser_free (struct gengetopt_args_info *args_info); | ||
| 186 | |||
| 187 | /** | ||
| 188 | * Checks that all the required options were specified | ||
| 189 | * @param args_info the structure to check | ||
| 190 | * @param prog_name the name of the program that will be used to print | ||
| 191 | * possible errors | ||
| 192 | * @return | ||
| 193 | */ | ||
| 194 | int cmdline_parser_required (struct gengetopt_args_info *args_info, | ||
| 195 | const char *prog_name); | ||
| 196 | |||
| 197 | |||
| 198 | #ifdef __cplusplus | ||
| 199 | } | ||
| 200 | #endif /* __cplusplus */ | ||
| 201 | #endif /* CMDLINE_LIFE_H */ | ||
| @@ -15,11 +15,11 @@ char * update_map (char * base_map, const int width, const int height, char live | |||
| 15 | y = (i - x)/width; | 15 | y = (i - x)/width; |
| 16 | /* | 16 | /* |
| 17 | Where i is the current cell: | 17 | Where i is the current cell: |
| 18 | A1 | A2 | A3 | 18 | A1 | A2 | A3 |
| 19 | ----|----|---- | 19 | ----|----|---- |
| 20 | B1 | i | B3 | 20 | B1 | i | B3 |
| 21 | ----|----|---- | 21 | ----|----|---- |
| 22 | C1 | C2 | C3 | 22 | C1 | C2 | C3 |
| 23 | */ | 23 | */ |
| 24 | if (x > 0) { | 24 | if (x > 0) { |
| 25 | if (base_map[i-1] == livecell) surround++; // B1 | 25 | if (base_map[i-1] == livecell) surround++; // B1 |
| @@ -63,16 +63,20 @@ char * update_map (char * base_map, const int width, const int height, char live | |||
| 63 | 63 | ||
| 64 | int main (int argc, char * argv[]) { | 64 | int main (int argc, char * argv[]) { |
| 65 | unsigned int RULE = 0; | 65 | unsigned int RULE = 0; |
| 66 | int rule_size = 0; | 66 | struct gengetopt_args_info args_info; |
| 67 | char * rulestr = NULL; | 67 | if (cmdline_parser(argc, argv, &args_info) != 0) exit(EXIT_FAILURE); |
| 68 | if (argc == 1) { | 68 | if (args_info.ruleint_given) { |
| 69 | printf("Rule Integer: "); | 69 | RULE = args_info.ruleint_arg; |
| 70 | getline(&rulestr, (size_t *) &rule_size, stdin); | 70 | } else { |
| 71 | } else if (argc > 1) rulestr = argv[1]; | 71 | int rule_size = 0; |
| 72 | RULE = atoi(rulestr); | 72 | char * rulestr = NULL; |
| 73 | if (RULE > 262143) { | 73 | printf("Rule Integer: "); |
| 74 | fprintf(stderr, "Invalid integer\n"); | 74 | getline(&rulestr, (size_t *) &rule_size, stdin); |
| 75 | exit(EXIT_FAILURE); | 75 | RULE = atoi(rulestr); |
| 76 | if (RULE > 262143) { | ||
| 77 | fprintf(stderr, "Invalid integer\n"); | ||
| 78 | exit(EXIT_FAILURE); | ||
| 79 | } | ||
| 76 | } | 80 | } |
| 77 | srand(time(NULL)); | 81 | srand(time(NULL)); |
| 78 | int ch = 0; | 82 | int ch = 0; |
| @@ -82,11 +86,10 @@ int main (int argc, char * argv[]) { | |||
| 82 | unsigned int delay = 0; // Counts up to 10 so display is slow | 86 | unsigned int delay = 0; // Counts up to 10 so display is slow |
| 83 | int livecell = '#'; | 87 | int livecell = '#'; |
| 84 | int deadcell = '.'; | 88 | int deadcell = '.'; |
| 85 | if (argc > 3) { | 89 | if (args_info.width_given) width = args_info.width_arg; |
| 86 | width = atoi(argv[2]); | 90 | if (args_info.height_given) height = args_info.height_arg; |
| 87 | height = atoi(argv[3]); | 91 | if (args_info.delay_given) delay = args_info.delay_arg; |
| 88 | } | 92 | cmdline_parser_free(&args_info); |
| 89 | if (argc > 4) delay = atoi(argv[4]); | ||
| 90 | char * map = NULL; | 93 | char * map = NULL; |
| 91 | map = malloc((height * width)+1); | 94 | map = malloc((height * width)+1); |
| 92 | memset(map, 0, height * width + 1); | 95 | memset(map, 0, height * width + 1); |
diff --git a/life.ggo b/life.ggo new file mode 100644 index 0000000..c51fa69 --- /dev/null +++ b/life.ggo | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | args "--file-name=cmdline-life" | ||
| 2 | option "ruleint" r "Specify rule-int on command line" typestr="Rule integer" int optional | ||
| 3 | option "width" w "Specify width" typestr="width" int optional | ||
| 4 | option "height" h "Specify height" typestr="height" int optional | ||
| 5 | option "delay" d "Specify delay time" details="Specify delay time, multiplied by 10ms" typestr="delay" int optional | ||
| @@ -7,3 +7,6 @@ | |||
| 7 | 7 | ||
| 8 | // Module Libraries | 8 | // Module Libraries |
| 9 | #include <ncurses.h> | 9 | #include <ncurses.h> |
| 10 | |||
| 11 | // Project Libraries | ||
| 12 | #include <cmdline-life.h> | ||
