diff options
Diffstat (limited to 'src/options.c')
-rw-r--r-- | src/options.c | 304 |
1 files changed, 125 insertions, 179 deletions
diff --git a/src/options.c b/src/options.c index 725599e..4c8fb70 100644 --- a/src/options.c +++ b/src/options.c @@ -86,16 +86,14 @@ parse_transition_time(const char *str, const char **end) errno = 0; hours = strtol(str, (void *)&min, 10); - if (errno != 0 || min == str || min[0] != ':' || hours < 0 || hours >= 24) { + if (errno || min == str || min[0] != ':' || hours < 0 || hours >= 24) return -1; - } min += 1; errno = 0; minutes = strtol(min, (void *)end, 10); - if (errno != 0 || *end == min || minutes < 0 || minutes >= 60) { + if (errno || *end == min || minutes < 0 || minutes >= 60) return -1; - } return minutes * 60 + hours * 3600; } @@ -119,7 +117,7 @@ parse_transition_range(const char *str, struct time_range *range) const char *end = NULL; next += 1; end_time = parse_transition_time(next, &end); - if (end_time < 0 || end[0] != '\0') + if (end_time < 0 || *end) return -1; } else { return -1; @@ -203,21 +201,21 @@ print_help(const char *program_name) static void print_method_list(const struct gamma_method *gamma_methods) { + size_t i; + fputs(_("Available adjustment methods:\n"), stdout); - for (int i = 0; gamma_methods[i].name != NULL; i++) { + for (i = 0; gamma_methods[i].name != NULL; i++) printf(" %s\n", gamma_methods[i].name); - } fputs("\n", stdout); - fputs(_("Specify colon-separated options with" - " `-m METHOD:OPTIONS'.\n"), stdout); + fputs(_("Specify colon-separated options with `-m METHOD:OPTIONS'.\n"), stdout); /* TRANSLATORS: `help' must not be translated. */ fputs(_("Try `-m METHOD:help' for help.\n"), stdout); } /* Print list of location providers. */ static void -print_provider_list(const struct location_provider location_providers[]) +print_provider_list(const struct location_provider *location_providers) { fputs(_("Available location providers:\n"), stdout); for (int i = 0; location_providers[i].name != NULL; i++) { @@ -225,43 +223,32 @@ print_provider_list(const struct location_provider location_providers[]) } fputs("\n", stdout); - fputs(_("Specify colon-separated options with" - "`-l PROVIDER:OPTIONS'.\n"), stdout); + fputs(_("Specify colon-separated options with`-l PROVIDER:OPTIONS'.\n"), stdout); /* TRANSLATORS: `help' must not be translated. */ fputs(_("Try `-l PROVIDER:help' for help.\n"), stdout); } /* Return the gamma method with the given name. */ static const struct gamma_method * -find_gamma_method(const struct gamma_method gamma_methods[], const char *name) +find_gamma_method(const struct gamma_method *gamma_methods, const char *name) { - const struct gamma_method *method = NULL; - for (int i = 0; gamma_methods[i].name != NULL; i++) { - const struct gamma_method *m = &gamma_methods[i]; - if (strcasecmp(name, m->name) == 0) { - method = m; - break; - } - } - - return method; + size_t i; + for (i = 0; gamma_methods[i].name; i++) + if (!strcasecmp(name, gamma_methods[i].name)) + return &gamma_methods[i]; + return NULL; } /* Return location provider with the given name. */ static const struct location_provider * -find_location_provider( - const struct location_provider location_providers[], const char *name) +find_location_provider(const struct location_provider *location_providers, const char *name) { - const struct location_provider *provider = NULL; - for (int i = 0; location_providers[i].name != NULL; i++) { - const struct location_provider *p = &location_providers[i]; - if (strcasecmp(name, p->name) == 0) { - provider = p; - break; - } + size_t i; + for (i = 0; location_providers[i].name != NULL; i++) { + if (!strcasecmp(name, location_providers[i].name)) + return &location_providers[i]; } - - return provider; + return NULL; } @@ -308,50 +295,44 @@ options_init(struct options *options) /* Parse a single option from the command-line. */ static int -parse_command_line_option( - const char option, char *value, struct options *options, - const char *program_name, const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) +parse_command_line_option(const char option, char *value, struct options *options, const char *program_name, + const struct gamma_method *gamma_methods, const struct location_provider *location_providers) { - int r; - char *s; - char *end; const char *provider_name; + char *s, *end; + int r; switch (option) { case 'b': - parse_brightness_string( - value, &options->scheme.day.brightness, - &options->scheme.night.brightness); + parse_brightness_string(value, &options->scheme.day.brightness, &options->scheme.night.brightness); break; + case 'c': free(options->config_filepath); - options->config_filepath = strdup(value); + options->config_filepath = estrdup(value); break; + case 'g': r = parse_gamma_string(value, options->scheme.day.gamma); if (r < 0) { - fputs(_("Malformed gamma argument.\n"), stderr); - fputs(_("Try `-h' for more information.\n"), stderr); + weprintf(_("Malformed gamma argument.")); + weprintf(_("Try `-h' for more information.")); return -1; } - /* Set night gamma to the same value as day gamma. - To set these to distinct values use the config - file. */ - memcpy(options->scheme.night.gamma, - options->scheme.day.gamma, - sizeof(options->scheme.night.gamma)); + To set these to distinct values use the config file. */ + memcpy(options->scheme.night.gamma, options->scheme.day.gamma, sizeof(options->scheme.night.gamma)); break; + case 'h': print_help(program_name); - exit(EXIT_SUCCESS); - break; + exit(0); + case 'l': /* Print list of providers if argument is `list' */ - if (strcasecmp(value, "list") == 0) { + if (!strcasecmp(value, "list")) { print_provider_list(location_providers); - exit(EXIT_SUCCESS); + exit(0); } provider_name = NULL; @@ -360,15 +341,15 @@ parse_command_line_option( to know if value can be parsed as a float. */ errno = 0; strtof(value, &end); - if (errno == 0 && *end == ':') { + if (!errno && *end == ':') { /* Use instead as arguments to `manual'. */ provider_name = "manual"; options->provider_args = value; } else { /* Split off provider arguments. */ s = strchr(value, ':'); - if (s != NULL) { - *(s++) = '\0'; + if (s) { + *s++ = '\0'; options->provider_args = s; } @@ -377,91 +358,96 @@ parse_command_line_option( /* Lookup provider from name. */ options->provider = find_location_provider(location_providers, provider_name); - if (options->provider == NULL) { - fprintf(stderr, _("Unknown location provider `%s'.\n"), - provider_name); + if (!options->provider) { + weprintf(_("Unknown location provider `%s'."), provider_name); return -1; } /* Print provider help if arg is `help'. */ - if (options->provider_args != NULL && - strcasecmp(options->provider_args, "help") == 0) { + if (options->provider_args && !strcasecmp(options->provider_args, "help")) { options->provider->print_help(stdout); - exit(EXIT_SUCCESS); + exit(0); } break; + case 'm': /* Print list of methods if argument is `list' */ - if (strcasecmp(value, "list") == 0) { + if (!strcasecmp(value, "list")) { print_method_list(gamma_methods); - exit(EXIT_SUCCESS); + exit(0); } /* Split off method arguments. */ s = strchr(value, ':'); - if (s != NULL) { - *(s++) = '\0'; + if (s) { + *s++ = '\0'; options->method_args = s; } /* Find adjustment method by name. */ options->method = find_gamma_method(gamma_methods, value); - if (options->method == NULL) { - /* TRANSLATORS: This refers to the method - used to adjust colors e.g VidMode */ - fprintf(stderr, _("Unknown adjustment method `%s'.\n"), - value); + if (!options->method) { + /* TRANSLATORS: This refers to the method used to adjust colors e.g. VidMode */ + weprintf(_("Unknown adjustment method `%s'."), value); return -1; } /* Print method help if arg is `help'. */ - if (options->method_args != NULL && - strcasecmp(options->method_args, "help") == 0) { + if (options->method_args && !strcasecmp(options->method_args, "help")) { options->method->print_help(stdout); - exit(EXIT_SUCCESS); + exit(0); } break; + case 'o': options->mode = PROGRAM_MODE_ONE_SHOT; break; + case 'O': options->mode = PROGRAM_MODE_MANUAL; options->temp_set = atoi(value); break; + case 'p': options->mode = PROGRAM_MODE_PRINT; break; + case 'P': options->preserve_gamma = 0; break; + case 'r': options->use_fade = 0; break; + case 't': s = strchr(value, ':'); - if (s == NULL) { - fputs(_("Malformed temperature argument.\n"), stderr); - fputs(_("Try `-h' for more information.\n"), stderr); + if (!s) { + weprintf(_("Malformed temperature argument.")); + weprintf(_("Try `-h' for more information.")); return -1; } - *(s++) = '\0'; + *s++ = '\0'; options->scheme.day.temperature = atoi(value); options->scheme.night.temperature = atoi(s); break; + case 'v': options->verbose = 1; break; + case 'V': printf("%s\n", PACKAGE_STRING); exit(EXIT_SUCCESS); break; + case 'x': options->mode = PROGRAM_MODE_RESET; break; + case '?': - fputs(_("Try `-h' for more information.\n"), stderr); + weprintf(_("Try `-h' for more information.")); return -1; - break; } return 0; @@ -469,138 +455,102 @@ parse_command_line_option( /* Parse command line arguments. */ void -options_parse_args( - struct options *options, int argc, char *argv[], - const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) +options_parse_args(struct options *options, int argc, char *argv[], const struct gamma_method *gamma_methods, + const struct location_provider *location_providers) { - int r, opt; - while ((opt = getopt(argc, argv, "b:c:g:hl:m:oO:pPrt:vVx")) != -1) { - r = parse_command_line_option(opt, optarg, options, argv0, - gamma_methods, location_providers); - if (r < 0) + int opt; + while ((opt = getopt(argc, argv, "b:c:g:hl:m:oO:pPrt:vVx")) != -1) + if (parse_command_line_option(opt, optarg, options, argv0, gamma_methods, location_providers) < 0) exit(EXIT_FAILURE); - } } /* Parse a single key-value pair from the configuration file. */ static int -parse_config_file_option( - const char *key, const char *value, struct options *options, - const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) +parse_config_file_option(const char *key, const char *value, struct options *options, + const struct gamma_method *gamma_methods, const struct location_provider *location_providers) { - if (strcasecmp(key, "temp-day") == 0) { - if (options->scheme.day.temperature < 0) { + if (!strcasecmp(key, "temp-day")) { + if (options->scheme.day.temperature < 0) options->scheme.day.temperature = atoi(value); - } - } else if (strcasecmp(key, "temp-night") == 0) { - if (options->scheme.night.temperature < 0) { + } else if (!strcasecmp(key, "temp-night")) { + if (options->scheme.night.temperature < 0) options->scheme.night.temperature = atoi(value); - } - } else if (strcasecmp(key, "transition") == 0 || - strcasecmp(key, "fade") == 0) { - /* "fade" is preferred, "transition" is - deprecated as the setting key. */ - if (options->use_fade < 0) { + } else if (!strcasecmp(key, "transition") || !strcasecmp(key, "fade")) { + /* "fade" is preferred, "transition" is deprecated as the setting key. */ + if (options->use_fade < 0) options->use_fade = !!atoi(value); - } - } else if (strcasecmp(key, "brightness") == 0) { - if (isnan(options->scheme.day.brightness)) { + } else if (!strcasecmp(key, "brightness")) { + if (isnan(options->scheme.day.brightness)) options->scheme.day.brightness = atof(value); - } - if (isnan(options->scheme.night.brightness)) { + if (isnan(options->scheme.night.brightness)) options->scheme.night.brightness = atof(value); - } - } else if (strcasecmp(key, "brightness-day") == 0) { - if (isnan(options->scheme.day.brightness)) { + } else if (!strcasecmp(key, "brightness-day")) { + if (isnan(options->scheme.day.brightness)) options->scheme.day.brightness = atof(value); - } - } else if (strcasecmp(key, "brightness-night") == 0) { - if (isnan(options->scheme.night.brightness)) { + } else if (!strcasecmp(key, "brightness-night")) { + if (isnan(options->scheme.night.brightness)) options->scheme.night.brightness = atof(value); - } - } else if (strcasecmp(key, "elevation-high") == 0) { + } else if (!strcasecmp(key, "elevation-high")) { options->scheme.high = atof(value); - } else if (strcasecmp(key, "elevation-low") == 0) { + } else if (!strcasecmp(key, "elevation-low")) { options->scheme.low = atof(value); - } else if (strcasecmp(key, "gamma") == 0) { + } else if (!strcasecmp(key, "gamma")) { if (isnan(options->scheme.day.gamma[0])) { - int r = parse_gamma_string( - value, options->scheme.day.gamma); - if (r < 0) { - fputs(_("Malformed gamma setting.\n"), stderr); + if (parse_gamma_string(value, options->scheme.day.gamma) < 0) { + weprintf(_("Malformed gamma setting.")); return -1; } - memcpy(options->scheme.night.gamma, - options->scheme.day.gamma, - sizeof(options->scheme.night.gamma)); + memcpy(options->scheme.night.gamma, options->scheme.day.gamma, sizeof(options->scheme.night.gamma)); } - } else if (strcasecmp(key, "gamma-day") == 0) { + } else if (!strcasecmp(key, "gamma-day")) { if (isnan(options->scheme.day.gamma[0])) { - int r = parse_gamma_string( - value, options->scheme.day.gamma); - if (r < 0) { - fputs(_("Malformed gamma setting.\n"), stderr); + if (parse_gamma_string(value, options->scheme.day.gamma) < 0) { + weprintf(_("Malformed gamma setting.")); return -1; } } - } else if (strcasecmp(key, "gamma-night") == 0) { + } else if (!strcasecmp(key, "gamma-night")) { if (isnan(options->scheme.night.gamma[0])) { - int r = parse_gamma_string( - value, options->scheme.night.gamma); - if (r < 0) { - fputs(_("Malformed gamma setting.\n"), stderr); + if (parse_gamma_string(value, options->scheme.night.gamma) < 0) { + weprintf(_("Malformed gamma setting.")); return -1; } } - } else if (strcasecmp(key, "preserve-gamma") == 0) { - if (options->preserve_gamma == 1) { + } else if (!strcasecmp(key, "preserve-gamma")) { + if (options->preserve_gamma == 1) options->preserve_gamma = !!atoi(value); - } - } else if (strcasecmp(key, "adjustment-method") == 0) { - if (options->method == NULL) { - options->method = find_gamma_method( - gamma_methods, value); - if (options->method == NULL) { - fprintf(stderr, _("Unknown adjustment" - " method `%s'.\n"), value); + } else if (!strcasecmp(key, "adjustment-method")) { + if (!options->method) { + options->method = find_gamma_method(gamma_methods, value); + if (!options->method) { + weprintf(_("Unknown adjustment method `%s'."), value); return -1; } } - } else if (strcasecmp(key, "location-provider") == 0) { - if (options->provider == NULL) { - options->provider = find_location_provider( - location_providers, value); - if (options->provider == NULL) { - fprintf(stderr, _("Unknown location" - " provider `%s'.\n"), value); + } else if (!strcasecmp(key, "location-provider")) { + if (!options->provider) { + options->provider = find_location_provider(location_providers, value); + if (!options->provider) { + weprintf(_("Unknown location provider `%s'."), value); return -1; } } - } else if (strcasecmp(key, "dawn-time") == 0) { + } else if (!strcasecmp(key, "dawn-time")) { if (options->scheme.dawn.start < 0) { - int r = parse_transition_range( - value, &options->scheme.dawn); - if (r < 0) { - fprintf(stderr, _("Malformed dawn-time" - " setting `%s'.\n"), value); + if (parse_transition_range(value, &options->scheme.dawn) < 0) { + weprintf(_("Malformed dawn-time setting `%s'."), value); return -1; } } - } else if (strcasecmp(key, "dusk-time") == 0) { + } else if (!strcasecmp(key, "dusk-time")) { if (options->scheme.dusk.start < 0) { - int r = parse_transition_range( - value, &options->scheme.dusk); - if (r < 0) { - fprintf(stderr, _("Malformed dusk-time" - " setting `%s'.\n"), value); + if (parse_transition_range(value, &options->scheme.dusk) < 0) { + weprintf(_("Malformed dusk-time setting `%s'."), value); return -1; } } } else { - fprintf(stderr, _("Unknown configuration setting `%s'.\n"), - key); + weprintf(_("Unknown configuration setting `%s'."), key); } return 0; @@ -609,8 +559,7 @@ parse_config_file_option( /* Parse options defined in the config file. */ void options_parse_config_file(struct options *options, struct config_ini_state *config_state, - const struct gamma_method *gamma_methods, - const struct location_provider *location_providers) + const struct gamma_method *gamma_methods, const struct location_provider *location_providers) { struct config_ini_section *section; struct config_ini_setting *setting; @@ -620,12 +569,9 @@ options_parse_config_file(struct options *options, struct config_ini_state *conf if (!section) return; - for (setting = section->settings; setting; setting = setting->next) { - int r = parse_config_file_option(setting->name, setting->value, options, - gamma_methods, location_providers); - if (r < 0) + for (setting = section->settings; setting; setting = setting->next) + if (parse_config_file_option(setting->name, setting->value, options, gamma_methods, location_providers) < 0) exit(EXIT_FAILURE); - } } /* Replace unspecified options with default values. */ |