From 73c6eb2d4c00f9897a8f61c90a3a67fbb79cda8a Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 2 Nov 2021 21:45:31 +0100 Subject: [PATCH] use param structs everywhere for consistency --- i3status.c | 37 ++++++++++++-- include/i3status.h | 97 +++++++++++++++++++++++-------------- src/print_battery_info.c | 13 +++-- src/print_cpu_temperature.c | 1 - src/print_cpu_usage.c | 1 - src/print_ddate.c | 13 ++--- src/print_disk_info.c | 1 - src/print_eth_info.c | 1 - src/print_file_contents.c | 1 - src/print_ipv6_addr.c | 1 - src/print_load.c | 3 +- src/print_mem.c | 1 - src/print_path_exists.c | 1 - src/print_run_watch.c | 1 - src/print_time.c | 39 ++++++++------- src/print_volume.c | 1 - src/print_wireless_info.c | 1 - 17 files changed, 127 insertions(+), 86 deletions(-) diff --git a/i3status.c b/i3status.c index 8c55b3c..437144f 100644 --- a/i3status.c +++ b/i3status.c @@ -834,19 +834,50 @@ int main(int argc, char *argv[]) { CASE_SEC("time") { SEC_OPEN_MAP("time"); - print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, NULL, NULL, false, tv.tv_sec); + time_ctx_t ctx = { + .json_gen = json_gen, + .buf = buffer, + .buflen = sizeof(buffer), + .title = NULL, + .format = cfg_getstr(sec, "format"), + .tz = NULL, + .locale = NULL, + .format_time = NULL, + .hide_if_equals_localtime = false, + .t = tv.tv_sec, + }; + print_time(&ctx); SEC_CLOSE_MAP; } CASE_SEC_TITLE("tztime") { SEC_OPEN_MAP("tztime"); - print_time(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), cfg_getstr(sec, "locale"), cfg_getstr(sec, "format_time"), cfg_getbool(sec, "hide_if_equals_localtime"), tv.tv_sec); + time_ctx_t ctx = { + .json_gen = json_gen, + .buf = buffer, + .buflen = sizeof(buffer), + .title = title, + .format = cfg_getstr(sec, "format"), + .tz = cfg_getstr(sec, "timezone"), + .locale = cfg_getstr(sec, "locale"), + .format_time = cfg_getstr(sec, "format_time"), + .hide_if_equals_localtime = cfg_getbool(sec, "hide_if_equals_localtime"), + .t = tv.tv_sec, + }; + print_time(&ctx); SEC_CLOSE_MAP; } CASE_SEC("ddate") { SEC_OPEN_MAP("ddate"); - print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), tv.tv_sec); + ddate_ctx_t ctx = { + .json_gen = json_gen, + .buf = buffer, + .buflen = sizeof(buffer), + .format = cfg_getstr(sec, "format"), + .t = tv.tv_sec, + }; + print_ddate(&ctx); SEC_CLOSE_MAP; } diff --git a/include/i3status.h b/include/i3status.h index d6c919c..fe44780 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -82,20 +82,20 @@ extern char *pct_mark; /* Macro which any plugin can use to output the full_text part (when the output * format is JSON) or just output to stdout (any other output format). */ -#define OUTPUT_FULL_TEXT(text) \ - do { \ - /* Terminate the output buffer here in any case, so that it’s \ - * not forgotten in the module */ \ - *outwalk = '\0'; \ - if (output_format == O_I3BAR) { \ - char *_markup = cfg_getstr(cfg_general, "markup"); \ - yajl_gen_string(json_gen, (const unsigned char *)"markup", strlen("markup")); \ - yajl_gen_string(json_gen, (const unsigned char *)_markup, strlen(_markup)); \ - yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \ - yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \ - } else { \ - printf("%s", text); \ - } \ +#define OUTPUT_FULL_TEXT(text) \ + do { \ + /* Terminate the output buffer here in any case, so that it’s \ + * not forgotten in the module */ \ + *outwalk = '\0'; \ + if (output_format == O_I3BAR) { \ + char *_markup = cfg_getstr(cfg_general, "markup"); \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)"markup", strlen("markup")); \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)_markup, strlen(_markup)); \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)"full_text", strlen("full_text")); \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)text, strlen(text)); \ + } else { \ + printf("%s", text); \ + } \ } while (0) #define SEC_OPEN_MAP(name) \ @@ -143,21 +143,21 @@ extern char *pct_mark; } \ } while (0) -#define START_COLOR(colorstr) \ - do { \ - if (cfg_getbool(cfg_general, "colors")) { \ - const char *_val = NULL; \ - if (cfg_section) \ - _val = cfg_getstr(cfg_section, colorstr); \ - if (!_val) \ - _val = cfg_getstr(cfg_general, colorstr); \ - if (output_format == O_I3BAR) { \ - yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \ - yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \ - } else { \ - outwalk += sprintf(outwalk, "%s", color(colorstr)); \ - } \ - } \ +#define START_COLOR(colorstr) \ + do { \ + if (cfg_getbool(cfg_general, "colors")) { \ + const char *_val = NULL; \ + if (cfg_section) \ + _val = cfg_getstr(cfg_section, colorstr); \ + if (!_val) \ + _val = cfg_getstr(cfg_general, colorstr); \ + if (output_format == O_I3BAR) { \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)"color", strlen("color")); \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)_val, strlen(_val)); \ + } else { \ + outwalk += sprintf(outwalk, "%s", color(colorstr)); \ + } \ + } \ } while (0) #define END_COLOR \ @@ -167,12 +167,12 @@ extern char *pct_mark; } \ } while (0) -#define INSTANCE(instance) \ - do { \ - if (output_format == O_I3BAR) { \ - yajl_gen_string(json_gen, (const unsigned char *)"instance", strlen("instance")); \ - yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \ - } \ +#define INSTANCE(instance) \ + do { \ + if (output_format == O_I3BAR) { \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)"instance", strlen("instance")); \ + yajl_gen_string(ctx->json_gen, (const unsigned char *)instance, strlen(instance)); \ + } \ } while (0) #define OUTPUT_FORMATTED \ @@ -280,8 +280,31 @@ typedef struct { void print_battery_info(battery_info_ctx_t *ctx); -void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t); -void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t); +typedef struct { + yajl_gen json_gen; + char *buf; + const size_t buflen; + const char *title; + const char *format; + const char *tz; + const char *locale; + const char *format_time; + bool hide_if_equals_localtime; + time_t t; +} time_ctx_t; + +void print_time(time_ctx_t *ctx); + +typedef struct { + yajl_gen json_gen; + char *buf; + const size_t buflen; + const char *format; + time_t t; +} ddate_ctx_t; + +void print_ddate(ddate_ctx_t *ctx); + const char *get_ip_addr(const char *interface, int family); typedef struct { diff --git a/src/print_battery_info.c b/src/print_battery_info.c index cade48a..fad8bab 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -135,7 +135,7 @@ static void add_battery_info(struct battery_info *acc, const struct battery_info } #endif -static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, int number, const char *path, const char *format_down) { +static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *batt_info, yajl_gen json_gen, char *buffer, int number, const char *path, const char *format_down) { char *outwalk = buffer; #if defined(__linux__) @@ -516,7 +516,7 @@ static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen * Populate batt_info with aggregate information about all batteries. * Returns false on error, and an error message will have been written. */ -static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) { +static bool slurp_all_batteries(battery_info_ctx_t *ctx, struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) { #if defined(__linux__) char *outwalk = buffer; bool is_found = false; @@ -545,7 +545,7 @@ static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_ge .present_rate = 0, .status = CS_UNKNOWN, }; - if (!slurp_battery_info(&batt_buf, json_gen, buffer, i, globbuf.gl_pathv[i], format_down)) { + if (!slurp_battery_info(ctx, &batt_buf, json_gen, buffer, i, globbuf.gl_pathv[i], format_down)) { globfree(&globbuf); free(globpath); return false; @@ -568,7 +568,7 @@ static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_ge /* FreeBSD and OpenBSD only report aggregates. NetBSD always * iterates through all batteries, so it's more efficient to * aggregate in slurp_battery_info. */ - return slurp_battery_info(batt_info, json_gen, buffer, -1, path, format_down); + return slurp_battery_info(ctx, batt_info, json_gen, buffer, -1, path, format_down); #endif return true; @@ -586,7 +586,6 @@ void print_battery_info(battery_info_ctx_t *ctx) { .status = CS_UNKNOWN, }; bool colorful_output = false; -#define json_gen ctx->json_gen #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__) /* These OSes report battery stats in whole percent. */ @@ -600,10 +599,10 @@ void print_battery_info(battery_info_ctx_t *ctx) { #endif if (ctx->number < 0) { - if (!slurp_all_batteries(&batt_info, json_gen, ctx->buf, ctx->path, ctx->format_down)) + if (!slurp_all_batteries(ctx, &batt_info, ctx->json_gen, ctx->buf, ctx->path, ctx->format_down)) return; } else { - if (!slurp_battery_info(&batt_info, json_gen, ctx->buf, ctx->number, ctx->path, ctx->format_down)) + if (!slurp_battery_info(ctx, &batt_info, ctx->json_gen, ctx->buf, ctx->number, ctx->path, ctx->format_down)) return; } diff --git a/src/print_cpu_temperature.c b/src/print_cpu_temperature.c index 6759235..9c9bbf0 100644 --- a/src/print_cpu_temperature.c +++ b/src/print_cpu_temperature.c @@ -220,7 +220,6 @@ void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx) { temperature_t temperature; temperature.raw_value = 0; sprintf(temperature.formatted_value, "%.2f", 0.0); -#define json_gen ctx->json_gen if (ctx->path == NULL) asprintf(&thermal_zone, THERMAL_ZONE, ctx->zone); diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index 7e505ea..712bbff 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -65,7 +65,6 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) { struct cpu_usage curr_all = {0, 0, 0, 0, 0}; int diff_idle, diff_total, diff_usage; bool colorful_output = false; -#define json_gen ctx->json_gen #if defined(__linux__) diff --git a/src/print_ddate.c b/src/print_ddate.c index 149a267..d9cafd3 100644 --- a/src/print_ddate.c +++ b/src/print_ddate.c @@ -92,12 +92,12 @@ struct disc_time *get_ddate(struct tm *current_tm) { return &dt; } -void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t) { - char *outwalk = buffer; +void print_ddate(ddate_ctx_t *ctx) { + char *outwalk = ctx->buf; struct tm current_tm; struct disc_time *dt; set_timezone(NULL); /* Use local time. */ - localtime_r(&t, ¤t_tm); + localtime_r(&ctx->t, ¤t_tm); if ((dt = get_ddate(¤t_tm)) == NULL) return; @@ -171,7 +171,8 @@ void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t) {.name = "%}", .value = ""}}; const size_t num = sizeof(placeholders) / sizeof(placeholder_t); - buffer = format_placeholders(format, &placeholders[0], num); - OUTPUT_FULL_TEXT(buffer); - free(buffer); + char *formatted = format_placeholders(ctx->format, &placeholders[0], num); + OUTPUT_FORMATTED; + free(formatted); + OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_disk_info.c b/src/print_disk_info.c index 9afe2e1..f1c2f09 100644 --- a/src/print_disk_info.c +++ b/src/print_disk_info.c @@ -121,7 +121,6 @@ void print_disk_info(disk_info_ctx_t *ctx) { char *outwalk = ctx->buf; bool colorful_output = false; bool mounted = false; -#define json_gen ctx->json_gen INSTANCE(ctx->path); diff --git a/src/print_eth_info.c b/src/print_eth_info.c index 0d05dda..82a96db 100644 --- a/src/print_eth_info.c +++ b/src/print_eth_info.c @@ -138,7 +138,6 @@ static int print_eth_speed(char *outwalk, const char *interface) { */ void print_eth_info(eth_info_ctx_t *ctx) { const char *format = ctx->format_down; // default format -#define json_gen ctx->json_gen char *outwalk = ctx->buf; size_t num = 0; diff --git a/src/print_file_contents.c b/src/print_file_contents.c index 58fb4e6..3e165ff 100644 --- a/src/print_file_contents.c +++ b/src/print_file_contents.c @@ -23,7 +23,6 @@ void print_file_contents(file_contents_ctx_t *ctx) { int fd = open(abs_path, O_RDONLY); free(abs_path); -#define json_gen ctx->json_gen INSTANCE(ctx->path); int n = -1; diff --git a/src/print_ipv6_addr.c b/src/print_ipv6_addr.c index 535b0b1..10bf488 100644 --- a/src/print_ipv6_addr.c +++ b/src/print_ipv6_addr.c @@ -121,7 +121,6 @@ void print_ipv6_info(ipv6_info_ctx_t *ctx) { char *addr_string = get_ipv6_addr(); char *outwalk = ctx->buf; -#define json_gen ctx->json_gen if (addr_string == NULL) { START_COLOR("color_bad"); outwalk += sprintf(outwalk, "%s", ctx->format_down); diff --git a/src/print_load.c b/src/print_load.c index 911779c..080e5f1 100644 --- a/src/print_load.c +++ b/src/print_load.c @@ -13,8 +13,7 @@ void print_load(load_ctx_t *ctx) { char *outwalk = ctx->buf; -/* Get load */ -#define json_gen ctx->json_gen + /* Get load */ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__) double loadavg[3]; diff --git a/src/print_mem.c b/src/print_mem.c index 9141843..76a70b9 100644 --- a/src/print_mem.c +++ b/src/print_mem.c @@ -88,7 +88,6 @@ static unsigned long memory_absolute(const char *mem_amount, const unsigned long void print_memory(memory_ctx_t *ctx) { char *outwalk = ctx->buf; -#define json_gen ctx->json_gen #if defined(__linux__) const char *selected_format = ctx->format; diff --git a/src/print_path_exists.c b/src/print_path_exists.c index b724084..054f12e 100644 --- a/src/print_path_exists.c +++ b/src/print_path_exists.c @@ -15,7 +15,6 @@ void print_path_exists(path_exists_ctx_t *ctx) { char *outwalk = ctx->buf; struct stat st; const bool exists = (stat(ctx->path, &st) == 0); -#define json_gen ctx->json_gen if (exists || ctx->format_down == NULL) { walk = ctx->format; diff --git a/src/print_run_watch.c b/src/print_run_watch.c index de58019..15cec6f 100644 --- a/src/print_run_watch.c +++ b/src/print_run_watch.c @@ -13,7 +13,6 @@ void print_run_watch(run_watch_ctx_t *ctx) { bool running = process_runs(ctx->pidfile); const char *walk; char *outwalk = ctx->buf; -#define json_gen ctx->json_gen if (running || ctx->format_down == NULL) { walk = ctx->format; diff --git a/src/print_time.c b/src/print_time.c index f7f77f0..5b57e2d 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -37,51 +37,50 @@ void set_timezone(const char *tz) { tzset(); } -void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t) { - char *outwalk = buffer; +void print_time(time_ctx_t *ctx) { + char *outwalk = ctx->buf; struct tm local_tm, tm; - if (title != NULL) - INSTANCE(title); + if (ctx->title != NULL) + INSTANCE(ctx->title); set_timezone(NULL); - localtime_r(&t, &local_tm); + localtime_r(&ctx->t, &local_tm); - set_timezone(tz); - localtime_r(&t, &tm); + set_timezone(ctx->tz); + localtime_r(&ctx->t, &tm); // When hide_if_equals_localtime is true, compare local and target time to display only if different time_t local_t = mktime(&local_tm); - double diff = difftime(local_t, t); - if (hide_if_equals_localtime && diff == 0.0) { + double diff = difftime(local_t, ctx->t); + if (ctx->hide_if_equals_localtime && diff == 0.0) { goto out; } - if (locale != NULL) { - setlocale(LC_ALL, locale); + if (ctx->locale != NULL) { + setlocale(LC_ALL, ctx->locale); } char string_time[STRING_SIZE]; - if (format_time == NULL) { - outwalk += strftime(buffer, 4096, format, &tm); + if (ctx->format_time == NULL) { + outwalk += strftime(ctx->buf, 4096, ctx->format, &tm); } else { - strftime(string_time, sizeof(string_time), format_time, &tm); + strftime(string_time, sizeof(string_time), ctx->format_time, &tm); placeholder_t placeholders[] = { {.name = "%time", .value = string_time}}; const size_t num = sizeof(placeholders) / sizeof(placeholder_t); - buffer = format_placeholders(format_time, &placeholders[0], num); + char *formatted = format_placeholders(ctx->format_time, &placeholders[0], num); + OUTPUT_FORMATTED; + free(formatted); } - if (locale != NULL) { + if (ctx->locale != NULL) { setlocale(LC_ALL, ""); } out: *outwalk = '\0'; - OUTPUT_FULL_TEXT(buffer); - if (format_time != NULL) { - free(buffer); - } + OUTPUT_FULL_TEXT(ctx->buf); } diff --git a/src/print_volume.c b/src/print_volume.c index 3d2cc92..e936d1a 100644 --- a/src/print_volume.c +++ b/src/print_volume.c @@ -67,7 +67,6 @@ static char *apply_volume_format(const char *fmt, int ivolume, const char *devic void print_volume(volume_ctx_t *ctx) { char *outwalk = ctx->buf; int pbval = 1; -#define json_gen ctx->json_gen /* Printing volume works with ALSA and PulseAudio at the moment */ if (output_format == O_I3BAR) { diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index a4a90cf..26162c4 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -502,7 +502,6 @@ void print_wireless_info(wireless_info_ctx_t *ctx) { const char *walk; char *outwalk = ctx->buf; wireless_info_t info; -#define json_gen ctx->json_gen INSTANCE(ctx->interface);