/* * Get configuration from configuration file. */ void pgut_readopt(const char *path, pgut_option options[], int elevel) { FILE *fp; char buf[1024]; char key[1024]; char value[1024]; if (!options) return; if ((fp = pgut_fopen(path, "rt", true)) == NULL) return; while (fgets(buf, lengthof(buf), fp)) { size_t i; for (i = strlen(buf); i > 0 && IsSpace(buf[i - 1]); i--) buf[i - 1] = '\0'; if (parse_pair(buf, key, value)) { for (i = 0; options[i].type; i++) { pgut_option *opt = &options[i]; if (key_equals(key, opt->lname)) { if (opt->allowed == SOURCE_DEFAULT || opt->allowed > SOURCE_FILE) if (elevel >= ERROR) { ereport(ERROR, (errcode(elevel), errmsg("option %s cannot specified in file", opt->lname))); } else { elog(elevel, "option %s cannot specified in file", opt->lname); } else if (opt->source <= SOURCE_FILE) assign_option(opt, value, SOURCE_FILE); break; } } if (!options[i].type) { if (elevel >= ERROR) { ereport(ERROR, (errcode(elevel), errmsg("invalid option \"%s\"", key))); } else { elog(elevel, "invalid option \"%s\"", key); } } } } fclose(fp); }
static int32_t assign_com_configure_option( struct programmer_arguments *args, const int32_t parameter, char *value ) { /* name & value */ if( 0 == parameter ) { /* name */ if( 0 != assign_option((int32_t *) &(args->com_configure_data.name), value, configure_map) ) { return -1; } } else { int32_t temp = 0; /* value */ if( 1 != sscanf(value, "%i", &(temp)) ) return -2; /* ensure the range is greater than 0 */ if( temp < 0 ) return -3; args->com_configure_data.value = temp; } return 0; }
static void option_from_env(pgut_option options[]) { size_t i; for (i = 0; options && options[i].type; i++) { pgut_option *opt = &options[i]; char name[256]; size_t j; const char *s; const char *value; if (opt->source > SOURCE_ENV || opt->allowed == SOURCE_DEFAULT || opt->allowed > SOURCE_ENV) continue; for (s = opt->lname, j = 0; *s && j < lengthof(name) - 1; s++, j++) { if (strchr("-_ ", *s)) name[j] = '_'; /* - to _ */ else name[j] = toupper(*s); } name[j] = '\0'; if ((value = getenv(name)) != NULL) assign_option(opt, value, SOURCE_ENV); } }
int pgut_getopt(int argc, char **argv, pgut_option options[]) { int c; int optindex = 0; char *optstring; struct option *longopts; pgut_option *opt; if (PROGRAM_NAME == NULL) { PROGRAM_NAME = get_progname(argv[0]); set_pglocale_pgservice(argv[0], "pgscripts"); } /* Help message and version are handled at first. */ if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { help(true); exit_or_abort(HELP); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit_or_abort(HELP); } } /* Merge default and user options. */ longopts = option_merge(default_options, options); optstring = longopts_to_optstring(longopts); /* Assign named options */ while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1) { opt = option_find(c, default_options, options); assign_option(opt, optarg, SOURCE_CMDLINE); } /* Read environment variables */ option_from_env(options); (void) (dbname || (dbname = getenv("PGDATABASE")) || (dbname = getenv("PGUSER")) || (dbname = get_username())); init_cancel_handler(); atexit(on_cleanup); return optind; }
static int32_t assign_com_get_option( struct programmer_arguments *args, const int32_t parameter, char *value ) { /* name */ if( 0 != assign_option((int32_t *) &(args->com_get_data.name), value, get_map) ) { return -1; } return 0; }
int32_t parse_arguments( struct programmer_arguments *args, const size_t argc, char **argv ) { int32_t i; int32_t status = 0; if( NULL == args ) return -1; /* initialize the argument block to empty, known values */ args->target = tar_none; args->command = com_none; args->quiet = 0; args->suppressbootloader = 0; /* Special case - check for the help commands which do not require a device type */ if( argc == 2 ) { if( 0 == strcasecmp(argv[1], "--version") ) { fprintf( stderr, PACKAGE_STRING "\n"); return 1; } if( 0 == strcasecmp(argv[1], "--targets") ) { list_targets( LIST_STD ); return 1; } if( 0 == strcasecmp(argv[1], "--targets-tex") ) { list_targets( LIST_TEX ); return 1; } if( 0 == strcasecmp(argv[1], "--targets-html") ) { list_targets( LIST_HTML ); return 1; } if( 0 == strcasecmp(argv[1], "--help") || 0 == strcasecmp(argv[1], "-h") || 0 == strcasecmp(argv[1], "--h") ) { usage(); return 1; } } /* Make sure there are the minimum arguments */ if( argc < 3 ) { basic_help(); return -1; } if( 0 != assign_target(args, argv[1], target_map) ) { fprintf( stderr, "Unsupported target '%s'.\n", argv[1]); status = -3; goto done; } if( 0 != assign_option((int32_t *) &(args->command), argv[2], command_map) ) { status = -4; goto done; } /* These were taken care of above. */ *argv[0] = '\0'; *argv[1] = '\0'; *argv[2] = '\0'; /* assign command specific default values */ switch( args->command ) { case com_flash : args->com_flash_data.force = 0; args->com_flash_data.segment = mem_flash; break; case com_launch : args->com_launch_config.noreset = 0; break; case com_dump : args->com_read_data.segment = mem_flash; args->com_flash_data.force = 0; break; case com_bin2hex : args->com_convert_data.segment = mem_flash; break; default : break; } if( 0 != assign_global_options(args, argc, argv) ) { status = -5; goto done; } if( 0 != assign_command_options(args, argc, argv) ) { status = -6; goto done; } /* Make sure there weren't any *extra* options. */ for( i = 0; i < argc; i++ ) { if( '\0' != *argv[i] ) { fprintf( stderr, "unrecognized parameter\n" ); status = -7; goto done; } } /* if this is a flash command, restore the filename */ if( (com_flash == args->command) || (com_eflash == args->command) || (com_user == args->command) ) { if( 0 == args->com_flash_data.file ) { // TODO : it should be ok to not have a filename if --serial=hexdigits:offset is // provided, this should be implemented.. in fact, given that most of this // program is written to use a single command by it self, this probably should // be separated out as a new command. The caveat is if data is written to '\0' // in the hex file, serialize will do nothing bc can't unwrite w/o erase fprintf( stderr, "flash filename is missing\n" ); status = -8; goto done; } args->com_flash_data.file[0] = args->com_flash_data.original_first_char; } if( (com_bin2hex == args->command) || (com_hex2bin == args->command) ) { if( 0 == args->com_convert_data.file ) { fprintf( stderr, "conversion filename is missing\n" ); status = -8; goto done; } args->com_convert_data.file[0] = args->com_convert_data.original_first_char; } done: if( 1 < debug ) { print_args( args ); } if(-3 == status ) { list_targets( LIST_STD ); } else if( 0 != status ) { usage(); } return status; }
int32_t parse_arguments( struct programmer_arguments *args, const size_t argc, char **argv ) { int32_t i; int32_t status = 0; if( NULL == args ) return -1; /* initialize the argument block to empty, known values */ args->target = tar_none; args->command = com_none; args->quiet = 0; args->suppressbootloader = 0; /* Make sure there are the minimum arguments */ if( argc < 3 ) { status = -2; goto done; } if( 0 != assign_target(args, argv[1], target_map) ) { status = -3; goto done; } if( 0 != assign_option((int32_t *) &(args->command), argv[2], command_map) ) { status = -4; goto done; } /* These were taken care of above. */ *argv[0] = '\0'; *argv[1] = '\0'; *argv[2] = '\0'; if( 0 != assign_global_options(args, argc, argv) ) { status = -5; goto done; } if( 0 != assign_command_options(args, argc, argv) ) { status = -6; goto done; } /* Make sure there weren't any *extra* options. */ for( i = 0; i < argc; i++ ) { if( '\0' != *argv[i] ) { fprintf( stderr, "unrecognized parameter\n" ); status = -7; goto done; } } /* if this is a flash command, restore the filename */ if( (com_flash == args->command) || (com_eflash == args->command) || (com_user == args->command) ) { if( 0 == args->com_flash_data.file ) { fprintf( stderr, "flash filename is missing\n" ); status = -8; goto done; } args->com_flash_data.file[0] = args->com_flash_data.original_first_char; } done: if( 1 < debug ) { print_args( args ); } if( 0 != status ) { usage(); } return status; }
int32_t parse_arguments( struct programmer_arguments *args, const size_t argc, char **argv ) { int32_t i; int32_t status = 0; if( NULL == args ) return -1; /* initialize the argument block to empty, known values */ args->target = tar_none; args->command = com_none; args->quiet = 0; args->suppressbootloader = 0; /* Special case - check for the help commands which do not require a device type */ if( argc == 2 ) { if( 0 == strcasecmp(argv[1], "--version") ) { fprintf( stderr, PACKAGE_STRING "\n"); return -1; } if( 0 == strcasecmp(argv[1], "--targets") ) { list_targets(); return -1; } if( 0 == strcasecmp(argv[1], "--help") ) { usage(); return -1; } } /* Make sure there are the minimum arguments */ if( argc < 3 ) { basic_help(); return -1; } if( 0 != assign_target(args, argv[1], target_map) ) { fprintf( stderr, "Unsupported target '%s'.\n", argv[1]); status = -3; goto done; } if( 0 != assign_option((int32_t *) &(args->command), argv[2], command_map) ) { status = -4; goto done; } /* These were taken care of above. */ *argv[0] = '\0'; *argv[1] = '\0'; *argv[2] = '\0'; if( 0 != assign_global_options(args, argc, argv) ) { status = -5; goto done; } if( 0 != assign_command_options(args, argc, argv) ) { status = -6; goto done; } /* Make sure there weren't any *extra* options. */ for( i = 0; i < argc; i++ ) { if( '\0' != *argv[i] ) { fprintf( stderr, "unrecognized parameter\n" ); status = -7; goto done; } } /* if this is a flash command, restore the filename */ if( (com_flash == args->command) || (com_eflash == args->command) || (com_user == args->command) ) { if( 0 == args->com_flash_data.file ) { fprintf( stderr, "flash filename is missing\n" ); status = -8; goto done; } args->com_flash_data.file[0] = args->com_flash_data.original_first_char; } done: if( 1 < debug ) { print_args( args ); } if(-3 == status ) { list_targets(); } else if( 0 != status ) { usage(); } return status; }