hirc

IRC client
Log | Files | Refs

commit 63afde8e5ead8feada596e9817faeebb56bf6cb5
parent 055763efe503afc213705564124dbac6465a95cc
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 20 Apr 2022 17:49:10 +0100

Simplify (config.num|config.pair) -> str process

Diffstat:
Msrc/commands.c | 7+------
Msrc/config.c | 79+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/hirc.h | 2+-
Msrc/main.c | 9+--------
4 files changed, 46 insertions(+), 51 deletions(-)

diff --git a/src/commands.c b/src/commands.c @@ -1800,12 +1800,7 @@ command_dump) { if (selected & opt_formats && strncmp(config[i].name, "format.", CONSTLEN("format.")) == 0) { fprintf(file, "/format %s %s\n", config[i].name + CONSTLEN("format."), config[i].str); } else if (selected & opt_config && strncmp(config[i].name, "format.", CONSTLEN("format.")) != 0) { - if (config[i].valtype == Val_string) - fprintf(file, "/set %s %s\n", config[i].name, config[i].str); - else if (config[i].valtype == Val_pair || config[i].valtype == Val_colourpair) - fprintf(file, "/set %s %ld %ld\n", config[i].name, config[i].pair[0], config[i].pair[1]); - else - fprintf(file, "/set %s %ld\n", config[i].name, config[i].num); + fprintf(file, "/set %s %s\n", config[i].name, config_get_pretty(&config[i], 0)); } } } diff --git a/src/config.c b/src/config.c @@ -164,7 +164,8 @@ struct Config config[] = { .num = Location_right, .numhandle = config_nicklist_location, .description = { - "Location of nicklist", NULL}}, + "Location of nicklist.", + "Accepted values: hidden, left, right.", NULL}}, {"nicklist.width", 1, Val_nzunsigned, .num = 15, .numhandle = config_nicklist_width, @@ -174,7 +175,8 @@ struct Config config[] = { .num = Location_left, .numhandle = config_buflist_location, .description = { - "Location of nicklist", NULL}}, + "Location of buflist.", + "Accepted values: hidden, left, right.", NULL}}, {"buflist.width", 1, Val_nzunsigned, .num = 25, .numhandle = config_buflist_width, @@ -1285,39 +1287,32 @@ struct Config * config_getp(char *name) { int i; + if (!name) + return NULL; + for (i = 0; config[i].name; i++) if (strcmp(config[i].name, name) == 0) return &config[i]; return NULL; } -void -config_get_print(char *name) { - int i, found; - - for (i = found = 0; config[i].name; i++) { - if (strncmp(config[i].name, name, strlen(name)) == 0) { - if (config[i].valtype == Val_string) - hist_format(selected.history, Activity_status, HIST_UI, "SELF_UI :%s: %s", - config[i].name, config[i].str); - else if (config[i].valtype == Val_pair || config[i].valtype == Val_colourpair) - hist_format(selected.history, Activity_status, HIST_UI, "SELF_UI :%s: {%ld, %ld}", - config[i].name, config[i].pair[0], config[i].pair[1]); - else if (config[i].valtype == Val_location) - hist_format(selected.history, Activity_status, HIST_UI, "SELF_UO :%s: %s", - config[i].name, strlocation[config[i].num]); - else if (config[i].valtype == Val_bool) - hist_format(selected.history, Activity_status, HIST_UI, "SELF_UI :%s: %s", - config[i].name, strbool[config[i].num]); - else - hist_format(selected.history, Activity_status, HIST_UI, "SELF_UI :%s: %ld", - config[i].name, config[i].num); - found = 1; - } - } +char * +config_get_pretty(struct Config *conf, int pairbrace) { + static char ret[8192]; - if (!found) - ui_error("no such configuration variable: '%s'", name); + if (conf) { + if (conf->valtype == Val_string) + return conf->str; + else if (conf->valtype == Val_location) + return strlocation[conf->num]; + else if (conf->valtype == Val_bool) + return strbool[conf->num]; + else if (conf->valtype == Val_pair || conf->valtype == Val_colourpair) + snprintf(ret, sizeof(ret), pairbrace ? "{%ld, %ld}" : "%ld %ld", conf->pair[0], conf->pair[1]); + else + snprintf(ret, sizeof(ret), "%ld", conf->num); + return ret; + } else return NULL; } long @@ -1415,26 +1410,29 @@ config_set(char *name, char *val) { char *str = val ? estrdup(val) : NULL; char *tok[3], *save, *p; struct Config *conf; - - if (!(conf = config_getp(name))) { - ui_error("no such configuration variable", NULL); - goto end; - } + int i, found; tok[0] = strtok_r(val, " ", &save); tok[1] = strtok_r(NULL, " ", &save); tok[2] = strtok_r(NULL, " ", &save); + if (!(conf = config_getp(name))) { + if (tok[0]) { + ui_error("no such configuration variable", NULL); + goto end; + } + } + if (strisnum(tok[0], 1) && strisnum(tok[1], 1) && !tok[2]) { config_setr(conf, strtol(tok[0], NULL, 10), strtol(tok[1], NULL, 10)); } else if (strisnum(tok[0], 1) && !tok[1]) { config_setl(conf, strtol(tok[0], NULL, 10)); - } else if (conf->valtype == Val_bool && tok[0] && !tok[1]) { + } else if (tok[0] && !tok[1] && conf->valtype == Val_bool) { if (strcmp(tok[0], "true") == 0) config_setl(conf, 1); else if (strcmp(tok[0], "false") == 0) config_setl(conf, 0); - } else if (conf->valtype == Val_location && tok[0] && !tok[1]) { + } else if (tok[0] && !tok[1] && conf->valtype == Val_location) { if (strcmp(tok[0], "hidden") == 0) config_setl(conf, Location_hidden); else if (strcmp(tok[0], "left") == 0) @@ -1444,7 +1442,16 @@ config_set(char *name, char *val) { } else if (tok[0]) { config_sets(conf, str); } else { - config_get_print(name); + for (i = found = 0; config[i].name; i++) { + if (strncmp(config[i].name, name, strlen(name)) == 0) { + hist_format(selected.history, Activity_status, HIST_UI, "SELF_UI :%s: %s", + config[i].name, config_get_pretty(&config[i], 1)); + found = 1; + } + } + + if (!found) + ui_error("no such configuration variable", NULL); } end: diff --git a/src/hirc.h b/src/hirc.h @@ -189,7 +189,7 @@ int alias_remove(char *binding); char * alias_eval(char *cmd); /* config.c */ -void config_get_print(char *name); +char * config_get_pretty(struct Config *conf, int pairbrace); long config_getl(char *name); char * config_gets(char *name); void config_getr(char *name, long *a, long *b); diff --git a/src/main.c b/src/main.c @@ -246,14 +246,7 @@ main(int argc, char *argv[]) { for (i=0; config[i].name; i++) { printf(".It %s\n", config[i].name); printf(".Bd -literal -compact\n"); - if (config[i].valtype == Val_string) - printf("Default value: %s\n", config[i].str); - else if (config[i].valtype == Val_pair || config[i].valtype == Val_colourpair) - printf("Default value: {%02ld, %02ld}\n", config[i].pair[0], config[i].pair[1]); - else if (config[i].valtype == Val_bool) - printf("Default value: %s\n", config[i].num ? "true" : "false"); - else - printf("Default value: %ld\n", config[i].num); + printf("Default value: %s\n", config_get_pretty(&config[i], 1)); for (j=0; config[i].description[j]; j++) printf("%s\n", config[i].description[j]); printf(".Ed\n");