use param structs everywhere for consistency

This commit is contained in:
Michael Stapelberg 2021-11-02 21:45:31 +01:00
parent 6f348e612b
commit 73c6eb2d4c
17 changed files with 127 additions and 86 deletions

View File

@ -834,19 +834,50 @@ int main(int argc, char *argv[]) {
CASE_SEC("time") { CASE_SEC("time") {
SEC_OPEN_MAP("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; SEC_CLOSE_MAP;
} }
CASE_SEC_TITLE("tztime") { CASE_SEC_TITLE("tztime") {
SEC_OPEN_MAP("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; SEC_CLOSE_MAP;
} }
CASE_SEC("ddate") { CASE_SEC("ddate") {
SEC_OPEN_MAP("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; SEC_CLOSE_MAP;
} }

View File

@ -82,20 +82,20 @@ extern char *pct_mark;
/* Macro which any plugin can use to output the full_text part (when the output /* 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). */ * format is JSON) or just output to stdout (any other output format). */
#define OUTPUT_FULL_TEXT(text) \ #define OUTPUT_FULL_TEXT(text) \
do { \ do { \
/* Terminate the output buffer here in any case, so that its \ /* Terminate the output buffer here in any case, so that its \
* not forgotten in the module */ \ * not forgotten in the module */ \
*outwalk = '\0'; \ *outwalk = '\0'; \
if (output_format == O_I3BAR) { \ if (output_format == O_I3BAR) { \
char *_markup = cfg_getstr(cfg_general, "markup"); \ char *_markup = cfg_getstr(cfg_general, "markup"); \
yajl_gen_string(json_gen, (const unsigned char *)"markup", strlen("markup")); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)"markup", strlen("markup")); \
yajl_gen_string(json_gen, (const unsigned char *)_markup, strlen(_markup)); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)_markup, strlen(_markup)); \
yajl_gen_string(json_gen, (const unsigned char *)"full_text", strlen("full_text")); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)"full_text", strlen("full_text")); \
yajl_gen_string(json_gen, (const unsigned char *)text, strlen(text)); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)text, strlen(text)); \
} else { \ } else { \
printf("%s", text); \ printf("%s", text); \
} \ } \
} while (0) } while (0)
#define SEC_OPEN_MAP(name) \ #define SEC_OPEN_MAP(name) \
@ -143,21 +143,21 @@ extern char *pct_mark;
} \ } \
} while (0) } while (0)
#define START_COLOR(colorstr) \ #define START_COLOR(colorstr) \
do { \ do { \
if (cfg_getbool(cfg_general, "colors")) { \ if (cfg_getbool(cfg_general, "colors")) { \
const char *_val = NULL; \ const char *_val = NULL; \
if (cfg_section) \ if (cfg_section) \
_val = cfg_getstr(cfg_section, colorstr); \ _val = cfg_getstr(cfg_section, colorstr); \
if (!_val) \ if (!_val) \
_val = cfg_getstr(cfg_general, colorstr); \ _val = cfg_getstr(cfg_general, colorstr); \
if (output_format == O_I3BAR) { \ if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *)"color", strlen("color")); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)"color", strlen("color")); \
yajl_gen_string(json_gen, (const unsigned char *)_val, strlen(_val)); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)_val, strlen(_val)); \
} else { \ } else { \
outwalk += sprintf(outwalk, "%s", color(colorstr)); \ outwalk += sprintf(outwalk, "%s", color(colorstr)); \
} \ } \
} \ } \
} while (0) } while (0)
#define END_COLOR \ #define END_COLOR \
@ -167,12 +167,12 @@ extern char *pct_mark;
} \ } \
} while (0) } while (0)
#define INSTANCE(instance) \ #define INSTANCE(instance) \
do { \ do { \
if (output_format == O_I3BAR) { \ if (output_format == O_I3BAR) { \
yajl_gen_string(json_gen, (const unsigned char *)"instance", strlen("instance")); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)"instance", strlen("instance")); \
yajl_gen_string(json_gen, (const unsigned char *)instance, strlen(instance)); \ yajl_gen_string(ctx->json_gen, (const unsigned char *)instance, strlen(instance)); \
} \ } \
} while (0) } while (0)
#define OUTPUT_FORMATTED \ #define OUTPUT_FORMATTED \
@ -280,8 +280,31 @@ typedef struct {
void print_battery_info(battery_info_ctx_t *ctx); 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); typedef struct {
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t); 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); const char *get_ip_addr(const char *interface, int family);
typedef struct { typedef struct {

View File

@ -135,7 +135,7 @@ static void add_battery_info(struct battery_info *acc, const struct battery_info
} }
#endif #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; char *outwalk = buffer;
#if defined(__linux__) #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. * Populate batt_info with aggregate information about all batteries.
* Returns false on error, and an error message will have been written. * 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__) #if defined(__linux__)
char *outwalk = buffer; char *outwalk = buffer;
bool is_found = false; 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, .present_rate = 0,
.status = CS_UNKNOWN, .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); globfree(&globbuf);
free(globpath); free(globpath);
return false; 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 /* FreeBSD and OpenBSD only report aggregates. NetBSD always
* iterates through all batteries, so it's more efficient to * iterates through all batteries, so it's more efficient to
* aggregate in slurp_battery_info. */ * 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 #endif
return true; return true;
@ -586,7 +586,6 @@ void print_battery_info(battery_info_ctx_t *ctx) {
.status = CS_UNKNOWN, .status = CS_UNKNOWN,
}; };
bool colorful_output = false; bool colorful_output = false;
#define json_gen ctx->json_gen
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
/* These OSes report battery stats in whole percent. */ /* These OSes report battery stats in whole percent. */
@ -600,10 +599,10 @@ void print_battery_info(battery_info_ctx_t *ctx) {
#endif #endif
if (ctx->number < 0) { 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; return;
} else { } 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; return;
} }

View File

@ -220,7 +220,6 @@ void print_cpu_temperature_info(cpu_temperature_ctx_t *ctx) {
temperature_t temperature; temperature_t temperature;
temperature.raw_value = 0; temperature.raw_value = 0;
sprintf(temperature.formatted_value, "%.2f", 0.0); sprintf(temperature.formatted_value, "%.2f", 0.0);
#define json_gen ctx->json_gen
if (ctx->path == NULL) if (ctx->path == NULL)
asprintf(&thermal_zone, THERMAL_ZONE, ctx->zone); asprintf(&thermal_zone, THERMAL_ZONE, ctx->zone);

View File

@ -65,7 +65,6 @@ void print_cpu_usage(cpu_usage_ctx_t *ctx) {
struct cpu_usage curr_all = {0, 0, 0, 0, 0}; struct cpu_usage curr_all = {0, 0, 0, 0, 0};
int diff_idle, diff_total, diff_usage; int diff_idle, diff_total, diff_usage;
bool colorful_output = false; bool colorful_output = false;
#define json_gen ctx->json_gen
#if defined(__linux__) #if defined(__linux__)

View File

@ -92,12 +92,12 @@ struct disc_time *get_ddate(struct tm *current_tm) {
return &dt; return &dt;
} }
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t) { void print_ddate(ddate_ctx_t *ctx) {
char *outwalk = buffer; char *outwalk = ctx->buf;
struct tm current_tm; struct tm current_tm;
struct disc_time *dt; struct disc_time *dt;
set_timezone(NULL); /* Use local time. */ set_timezone(NULL); /* Use local time. */
localtime_r(&t, &current_tm); localtime_r(&ctx->t, &current_tm);
if ((dt = get_ddate(&current_tm)) == NULL) if ((dt = get_ddate(&current_tm)) == NULL)
return; return;
@ -171,7 +171,8 @@ void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t)
{.name = "%}", .value = ""}}; {.name = "%}", .value = ""}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t); const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
buffer = format_placeholders(format, &placeholders[0], num); char *formatted = format_placeholders(ctx->format, &placeholders[0], num);
OUTPUT_FULL_TEXT(buffer); OUTPUT_FORMATTED;
free(buffer); free(formatted);
OUTPUT_FULL_TEXT(ctx->buf);
} }

View File

@ -121,7 +121,6 @@ void print_disk_info(disk_info_ctx_t *ctx) {
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
bool colorful_output = false; bool colorful_output = false;
bool mounted = false; bool mounted = false;
#define json_gen ctx->json_gen
INSTANCE(ctx->path); INSTANCE(ctx->path);

View File

@ -138,7 +138,6 @@ static int print_eth_speed(char *outwalk, const char *interface) {
*/ */
void print_eth_info(eth_info_ctx_t *ctx) { void print_eth_info(eth_info_ctx_t *ctx) {
const char *format = ctx->format_down; // default format const char *format = ctx->format_down; // default format
#define json_gen ctx->json_gen
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
size_t num = 0; size_t num = 0;

View File

@ -23,7 +23,6 @@ void print_file_contents(file_contents_ctx_t *ctx) {
int fd = open(abs_path, O_RDONLY); int fd = open(abs_path, O_RDONLY);
free(abs_path); free(abs_path);
#define json_gen ctx->json_gen
INSTANCE(ctx->path); INSTANCE(ctx->path);
int n = -1; int n = -1;

View File

@ -121,7 +121,6 @@ void print_ipv6_info(ipv6_info_ctx_t *ctx) {
char *addr_string = get_ipv6_addr(); char *addr_string = get_ipv6_addr();
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
#define json_gen ctx->json_gen
if (addr_string == NULL) { if (addr_string == NULL) {
START_COLOR("color_bad"); START_COLOR("color_bad");
outwalk += sprintf(outwalk, "%s", ctx->format_down); outwalk += sprintf(outwalk, "%s", ctx->format_down);

View File

@ -13,8 +13,7 @@
void print_load(load_ctx_t *ctx) { void print_load(load_ctx_t *ctx) {
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
/* Get load */ /* Get load */
#define json_gen ctx->json_gen
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
double loadavg[3]; double loadavg[3];

View File

@ -88,7 +88,6 @@ static unsigned long memory_absolute(const char *mem_amount, const unsigned long
void print_memory(memory_ctx_t *ctx) { void print_memory(memory_ctx_t *ctx) {
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
#define json_gen ctx->json_gen
#if defined(__linux__) #if defined(__linux__)
const char *selected_format = ctx->format; const char *selected_format = ctx->format;

View File

@ -15,7 +15,6 @@ void print_path_exists(path_exists_ctx_t *ctx) {
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
struct stat st; struct stat st;
const bool exists = (stat(ctx->path, &st) == 0); const bool exists = (stat(ctx->path, &st) == 0);
#define json_gen ctx->json_gen
if (exists || ctx->format_down == NULL) { if (exists || ctx->format_down == NULL) {
walk = ctx->format; walk = ctx->format;

View File

@ -13,7 +13,6 @@ void print_run_watch(run_watch_ctx_t *ctx) {
bool running = process_runs(ctx->pidfile); bool running = process_runs(ctx->pidfile);
const char *walk; const char *walk;
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
#define json_gen ctx->json_gen
if (running || ctx->format_down == NULL) { if (running || ctx->format_down == NULL) {
walk = ctx->format; walk = ctx->format;

View File

@ -37,51 +37,50 @@ void set_timezone(const char *tz) {
tzset(); 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) { void print_time(time_ctx_t *ctx) {
char *outwalk = buffer; char *outwalk = ctx->buf;
struct tm local_tm, tm; struct tm local_tm, tm;
if (title != NULL) if (ctx->title != NULL)
INSTANCE(title); INSTANCE(ctx->title);
set_timezone(NULL); set_timezone(NULL);
localtime_r(&t, &local_tm); localtime_r(&ctx->t, &local_tm);
set_timezone(tz); set_timezone(ctx->tz);
localtime_r(&t, &tm); localtime_r(&ctx->t, &tm);
// When hide_if_equals_localtime is true, compare local and target time to display only if different // When hide_if_equals_localtime is true, compare local and target time to display only if different
time_t local_t = mktime(&local_tm); time_t local_t = mktime(&local_tm);
double diff = difftime(local_t, t); double diff = difftime(local_t, ctx->t);
if (hide_if_equals_localtime && diff == 0.0) { if (ctx->hide_if_equals_localtime && diff == 0.0) {
goto out; goto out;
} }
if (locale != NULL) { if (ctx->locale != NULL) {
setlocale(LC_ALL, locale); setlocale(LC_ALL, ctx->locale);
} }
char string_time[STRING_SIZE]; char string_time[STRING_SIZE];
if (format_time == NULL) { if (ctx->format_time == NULL) {
outwalk += strftime(buffer, 4096, format, &tm); outwalk += strftime(ctx->buf, 4096, ctx->format, &tm);
} else { } else {
strftime(string_time, sizeof(string_time), format_time, &tm); strftime(string_time, sizeof(string_time), ctx->format_time, &tm);
placeholder_t placeholders[] = { placeholder_t placeholders[] = {
{.name = "%time", .value = string_time}}; {.name = "%time", .value = string_time}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t); 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, ""); setlocale(LC_ALL, "");
} }
out: out:
*outwalk = '\0'; *outwalk = '\0';
OUTPUT_FULL_TEXT(buffer); OUTPUT_FULL_TEXT(ctx->buf);
if (format_time != NULL) {
free(buffer);
}
} }

View File

@ -67,7 +67,6 @@ static char *apply_volume_format(const char *fmt, int ivolume, const char *devic
void print_volume(volume_ctx_t *ctx) { void print_volume(volume_ctx_t *ctx) {
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
int pbval = 1; int pbval = 1;
#define json_gen ctx->json_gen
/* Printing volume works with ALSA and PulseAudio at the moment */ /* Printing volume works with ALSA and PulseAudio at the moment */
if (output_format == O_I3BAR) { if (output_format == O_I3BAR) {

View File

@ -502,7 +502,6 @@ void print_wireless_info(wireless_info_ctx_t *ctx) {
const char *walk; const char *walk;
char *outwalk = ctx->buf; char *outwalk = ctx->buf;
wireless_info_t info; wireless_info_t info;
#define json_gen ctx->json_gen
INSTANCE(ctx->interface); INSTANCE(ctx->interface);