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) pgut_setopt(opt, value, SOURCE_ENV); } }
static List * ParseControlFile(const char *path) { #define LINEBUF 1024 char buf[LINEBUF]; int lineno; FILE *file; List *items = NIL; file = pgut_fopen(path, "rt"); for (lineno = 1; fgets(buf, LINEBUF, file); lineno++) { char *keyword; char *value; int i; if (!ParseControlFileLine(buf, &keyword, &value)) continue; /* PATH_OPTIONS */ for (i = 0; i < NUM_PATH_OPTIONS; i++) { pgut_option *opt = &options[i]; if (pgut_keyeq(keyword, opt->lname)) { pgut_setopt(opt, value, SOURCE_FILE); break; } } /* Other options */ if (i >= NUM_PATH_OPTIONS) { size_t len; char *item; len = strlen(keyword) + strlen(value) + 2; item = pgut_malloc(len); snprintf(item, len, "%s=%s", keyword, value); items = lappend(items, item); if (pg_strcasecmp(item, "TYPE=FUNCTION") == 0) type_function = true; if (pg_strcasecmp(item, "TYPE=BINARY") == 0 || pg_strcasecmp(item, "TYPE=FIXED") == 0) type_binary = true; if (pg_strcasecmp(item, "WRITER=BINARY") == 0) writer_binary = true; } } fclose(file); return items; }
int pgut_getopt(int argc, char **argv, pgut_option options[]) { int c; int optindex = 0; char *optstring; struct option *longopts; pgut_option *opt; pgut_init(argc, argv); /* Help message and version are handled at first. */ if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { help(true); exit(1); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { printf("%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit(1); } if (strcmp(argv[1], "--configuration") == 0) { printf("%s\n", PG_VERSION_STR); exit(0); } } /* 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); pgut_setopt(opt, optarg, SOURCE_CMDLINE); } /* Read environment variables */ option_from_env(options); (void) (dbname || (dbname = getenv("PGDATABASE")) || (dbname = getenv("PGUSER")) || (dbname = get_username())); return optind; }
/* * 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")) == 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 (pgut_keyeq(key, opt->lname)) { if (opt->allowed == SOURCE_DEFAULT || opt->allowed > SOURCE_FILE) elog(elevel, "option %s cannot specified in file", opt->lname); else if (opt->source <= SOURCE_FILE) pgut_setopt(opt, value, SOURCE_FILE); break; } } if (!options[i].type) elog(elevel, "invalid option \"%s\"", key); } } fclose(fp); }
int pgut_getopt(int argc, char **argv, pgut_option options[]) { int c; int optindex = 0; char *optstring; struct option *longopts; pgut_option *opt; pgut_init(argc, argv); /* Help message and version are handled at first. */ if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { help(true); exit(1); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit(1); } } /* 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) { /* Show help message if are specified '-?, --help' */ if (c == '?') { /* Actual help option given */ if (strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0) { help(true); exit(1); } /* unknown option reported by getopt */ else { fprintf(stderr, "Try \"%s --help\" for more information.\n", PROGRAM_NAME); exit(EINVAL); } } /* Show version information if are specified '-V, --version' */ if (c == 'V') { fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit(1); } opt = option_find(c, default_options, options); pgut_setopt(opt, optarg, SOURCE_CMDLINE); } /* Read environment variables */ option_from_env(options); return optind; }