summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcontrib/net-speed.sh2
-rw-r--r--src/print_file_contents.c44
-rw-r--r--src/print_ipv6_addr.c16
-rw-r--r--src/print_load.c35
-rw-r--r--src/print_mem.c85
-rw-r--r--src/print_path_exists.c23
-rw-r--r--src/print_run_watch.c23
-rw-r--r--src/print_time.c27
8 files changed, 116 insertions, 139 deletions
diff --git a/contrib/net-speed.sh b/contrib/net-speed.sh
index 71a563f..ed41974 100755
--- a/contrib/net-speed.sh
+++ b/contrib/net-speed.sh
@@ -24,7 +24,7 @@
#
# Auto detect interfaces
-ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan|enp|wlp)')
+ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan|enp|enx|wlp)')
last_time=0
last_rx=0
diff --git a/src/print_file_contents.c b/src/print_file_contents.c
index 91ad80e..65813f5 100644
--- a/src/print_file_contents.c
+++ b/src/print_file_contents.c
@@ -12,6 +12,8 @@
#include <errno.h>
#include "i3status.h"
+#define STRING_SIZE 10
+
static void *scalloc(size_t size) {
void *result = calloc(size, 1);
if (result == NULL) {
@@ -42,32 +44,32 @@ void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, con
START_COLOR("color_bad");
}
- for (; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
- } else if (BEGINS_WITH(walk + 1, "title")) {
- outwalk += sprintf(outwalk, "%s", title);
- walk += strlen("title");
- } else if (BEGINS_WITH(walk + 1, "content")) {
- for (char *s = buf; *s != '\0' && n > 0; s++, n--) {
- if (*s != '\n') {
- *(outwalk++) = *s;
- }
- }
- walk += strlen("content");
- } else if (BEGINS_WITH(walk + 1, "errno")) {
- outwalk += sprintf(outwalk, "%d", errno);
- walk += strlen("errno");
- } else if (BEGINS_WITH(walk + 1, "error")) {
- outwalk += sprintf(outwalk, "%s", strerror(errno));
- walk += strlen("error");
- } else {
- *(outwalk++) = '%';
+ // remove newline chars
+ char *src, *dst;
+ for (src = dst = buf; *src != '\0'; src++) {
+ *dst = *src;
+ if (*dst != '\n') {
+ dst++;
}
}
+ *dst = '\0';
+
+ char string_errno[STRING_SIZE];
+
+ sprintf(string_errno, "%d", errno);
+
+ placeholder_t placeholders[] = {
+ {.name = "%title", .value = title},
+ {.name = "%content", .value = buf},
+ {.name = "%errno", .value = string_errno},
+ {.name = "%error", .value = strerror(errno)}};
+
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(walk, &placeholders[0], num);
free(buf);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
+ free(buffer);
}
diff --git a/src/print_ipv6_addr.c b/src/print_ipv6_addr.c
index 5a0c38d..8fd8669 100644
--- a/src/print_ipv6_addr.c
+++ b/src/print_ipv6_addr.c
@@ -118,7 +118,6 @@ static char *get_ipv6_addr(void) {
}
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) {
- const char *walk;
char *addr_string = get_ipv6_addr();
char *outwalk = buffer;
@@ -131,18 +130,13 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con
}
START_COLOR("color_good");
- for (walk = format_up; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
- } else if (BEGINS_WITH(walk + 1, "ip")) {
- outwalk += sprintf(outwalk, "%s", addr_string);
- walk += strlen("ip");
+ placeholder_t placeholders[] = {
+ {.name = "%ip", .value = addr_string}};
- } else {
- *(outwalk++) = '%';
- }
- }
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(format_up, &placeholders[0], num);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
+ free(buffer);
}
diff --git a/src/print_load.c b/src/print_load.c
index 5d97a2c..3ffc576 100644
--- a/src/print_load.c
+++ b/src/print_load.c
@@ -7,6 +7,10 @@
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>
+#include "i3status.h"
+
+#define STRING_SIZE 10
+
void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold) {
char *outwalk = buffer;
/* Get load */
@@ -14,7 +18,6 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__)
double loadavg[3];
const char *selected_format = format;
- const char *walk;
bool colorful_output = false;
if (getloadavg(loadavg, 3) == -1)
@@ -27,32 +30,28 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char
selected_format = format_above_threshold;
}
- for (walk = selected_format; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
+ char string_loadavg_1[STRING_SIZE];
+ char string_loadavg_5[STRING_SIZE];
+ char string_loadavg_15[STRING_SIZE];
- } else if (BEGINS_WITH(walk + 1, "1min")) {
- outwalk += sprintf(outwalk, "%1.2f", loadavg[0]);
- walk += strlen("1min");
+ snprintf(string_loadavg_1, STRING_SIZE, "%1.2f", loadavg[0]);
+ snprintf(string_loadavg_5, STRING_SIZE, "%1.2f", loadavg[1]);
+ snprintf(string_loadavg_15, STRING_SIZE, "%1.2f", loadavg[2]);
- } else if (BEGINS_WITH(walk + 1, "5min")) {
- outwalk += sprintf(outwalk, "%1.2f", loadavg[1]);
- walk += strlen("5min");
+ placeholder_t placeholders[] = {
+ {.name = "%1min", .value = string_loadavg_1},
+ {.name = "%5min", .value = string_loadavg_5},
+ {.name = "%15min", .value = string_loadavg_15}};
- } else if (BEGINS_WITH(walk + 1, "15min")) {
- outwalk += sprintf(outwalk, "%1.2f", loadavg[2]);
- walk += strlen("15min");
-
- } else {
- *(outwalk++) = '%';
- }
- }
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(selected_format, &placeholders[0], num);
if (colorful_output)
END_COLOR;
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
+ free(buffer);
return;
error:
diff --git a/src/print_mem.c b/src/print_mem.c
index 51dff1c..cbe42a9 100644
--- a/src/print_mem.c
+++ b/src/print_mem.c
@@ -7,6 +7,8 @@
#include <yajl/yajl_version.h>
#include "i3status.h"
+#define STRING_SIZE 10
+
#define BINARY_BASE 1024UL
#if defined(linux)
@@ -32,6 +34,10 @@ static int print_bytes_human(char *outwalk, unsigned long bytes, const char *uni
}
return sprintf(outwalk, "%.*f %s", decimals, base, iec_symbols[exponent]);
}
+
+static int print_percentage(char *outwalk, float percent) {
+ return snprintf(outwalk, STRING_SIZE, "%.1f%s", percent, pct_mark);
+}
#endif
#if defined(linux)
@@ -157,56 +163,45 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha
selected_format = format_degraded;
}
- for (walk = selected_format; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
-
- } else if (BEGINS_WITH(walk + 1, "total")) {
- outwalk += print_bytes_human(outwalk, ram_total, unit, decimals);
- walk += strlen("total");
-
- } else if (BEGINS_WITH(walk + 1, "used")) {
- outwalk += print_bytes_human(outwalk, ram_used, unit, decimals);
- walk += strlen("used");
-
- } else if (BEGINS_WITH(walk + 1, "free")) {
- outwalk += print_bytes_human(outwalk, ram_free, unit, decimals);
- walk += strlen("free");
-
- } else if (BEGINS_WITH(walk + 1, "available")) {
- outwalk += print_bytes_human(outwalk, ram_available, unit, decimals);
- walk += strlen("available");
-
- } else if (BEGINS_WITH(walk + 1, "shared")) {
- outwalk += print_bytes_human(outwalk, ram_shared, unit, decimals);
- walk += strlen("shared");
-
- } else if (BEGINS_WITH(walk + 1, "percentage_free")) {
- outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_free / ram_total, pct_mark);
- walk += strlen("percentage_free");
-
- } else if (BEGINS_WITH(walk + 1, "percentage_available")) {
- outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_available / ram_total, pct_mark);
- walk += strlen("percentage_available");
-
- } else if (BEGINS_WITH(walk + 1, "percentage_used")) {
- outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_used / ram_total, pct_mark);
- walk += strlen("percentage_used");
-
- } else if (BEGINS_WITH(walk + 1, "percentage_shared")) {
- outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_shared / ram_total, pct_mark);
- walk += strlen("percentage_shared");
-
- } else {
- *(outwalk++) = '%';
- }
- }
+ char string_ram_total[STRING_SIZE];
+ char string_ram_used[STRING_SIZE];
+ char string_ram_free[STRING_SIZE];
+ char string_ram_available[STRING_SIZE];
+ char string_ram_shared[STRING_SIZE];
+ char string_percentage_free[STRING_SIZE];
+ char string_percentage_available[STRING_SIZE];
+ char string_percentage_used[STRING_SIZE];
+ char string_percentage_shared[STRING_SIZE];
+
+ print_bytes_human(string_ram_total, ram_total, unit, decimals);
+ print_bytes_human(string_ram_used, ram_used, unit, decimals);
+ print_bytes_human(string_ram_free, ram_free, unit, decimals);
+ print_bytes_human(string_ram_available, ram_available, unit, decimals);
+ print_bytes_human(string_ram_shared, ram_shared, unit, decimals);
+ print_percentage(string_percentage_free, 100.0 * ram_free / ram_total);
+ print_percentage(string_percentage_available, 100.0 * ram_available / ram_total);
+ print_percentage(string_percentage_used, 100.0 * ram_used / ram_total);
+ print_percentage(string_percentage_shared, 100.0 * ram_shared / ram_total);
+
+ placeholder_t placeholders[] = {
+ {.name = "%total", .value = string_ram_total},
+ {.name = "%used", .value = string_ram_used},
+ {.name = "%free", .value = string_ram_free},
+ {.name = "%available", .value = string_ram_available},
+ {.name = "%shared", .value = string_ram_shared},
+ {.name = "%percentage_free", .value = string_percentage_free},
+ {.name = "%percentage_available", .value = string_percentage_available},
+ {.name = "%percentage_used", .value = string_percentage_used},
+ {.name = "%percentage_shared", .value = string_percentage_shared}};
+
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(selected_format, &placeholders[0], num);
if (output_color)
END_COLOR;
- *outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
+ free(buffer);
return;
error:
diff --git a/src/print_path_exists.c b/src/print_path_exists.c
index 87601b4..504eb6c 100644
--- a/src/print_path_exists.c
+++ b/src/print_path_exists.c
@@ -7,6 +7,8 @@
#include <sys/stat.h>
#include "i3status.h"
+#define STRING_SIZE 5
+
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down) {
const char *walk;
char *outwalk = buffer;
@@ -23,23 +25,18 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const
START_COLOR((exists ? "color_good" : "color_bad"));
- for (; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
+ char string_status[STRING_SIZE];
- } else if (BEGINS_WITH(walk + 1, "title")) {
- outwalk += sprintf(outwalk, "%s", title);
- walk += strlen("title");
+ snprintf(string_status, STRING_SIZE, "%s", (exists ? "yes" : "no"));
- } else if (BEGINS_WITH(walk + 1, "status")) {
- outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
- walk += strlen("status");
+ placeholder_t placeholders[] = {
+ {.name = "%title", .value = title},
+ {.name = "%status", .value = string_status}};
- } else {
- *(outwalk++) = '%';
- }
- }
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(walk, &placeholders[0], num);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
+ free(buffer);
}
diff --git a/src/print_run_watch.c b/src/print_run_watch.c
index f0a0746..d153da4 100644
--- a/src/print_run_watch.c
+++ b/src/print_run_watch.c
@@ -6,6 +6,8 @@
#include <yajl/yajl_version.h>
#include "i3status.h"
+#define STRING_SIZE 5
+
void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down) {
bool running = process_runs(pidfile);
const char *walk;
@@ -21,22 +23,15 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c
START_COLOR((running ? "color_good" : "color_bad"));
- for (; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
-
- } else if (BEGINS_WITH(walk + 1, "title")) {
- outwalk += sprintf(outwalk, "%s", title);
- walk += strlen("title");
+ char string_status[STRING_SIZE];
+ snprintf(string_status, STRING_SIZE, "%s", (running ? "yes" : "no"));
- } else if (BEGINS_WITH(walk + 1, "status")) {
- outwalk += sprintf(outwalk, "%s", (running ? "yes" : "no"));
- walk += strlen("status");
+ placeholder_t placeholders[] = {
+ {.name = "%title", .value = title},
+ {.name = "%status", .value = string_status}};
- } else {
- *(outwalk++) = '%';
- }
- }
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(walk, &placeholders[0], num);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
diff --git a/src/print_time.c b/src/print_time.c
index 5133c51..67930b7 100644
--- a/src/print_time.c
+++ b/src/print_time.c
@@ -10,6 +10,8 @@
#include "i3status.h"
+#define STRING_SIZE 50
+
static bool local_timezone_init = false;
static const char *local_timezone = NULL;
static const char *current_timezone = NULL;
@@ -36,10 +38,8 @@ void set_timezone(const char *tz) {
}
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) {
- const char *walk;
char *outwalk = buffer;
struct tm local_tm, tm;
- char timebuf[1024];
if (title != NULL)
INSTANCE(title);
@@ -61,23 +61,18 @@ void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *
setlocale(LC_ALL, locale);
}
+ char string_time[STRING_SIZE];
+
if (format_time == NULL) {
- strftime(timebuf, sizeof(timebuf), format, &tm);
- maybe_escape_markup(timebuf, &outwalk);
+ strftime(string_time, sizeof(string_time), format, &tm);
+ maybe_escape_markup(string_time, &outwalk);
} else {
- for (walk = format; *walk != '\0'; walk++) {
- if (*walk != '%') {
- *(outwalk++) = *walk;
+ strftime(string_time, sizeof(string_time), format_time, &tm);
+ placeholder_t placeholders[] = {
+ {.name = "%time", .value = string_time}};
- } else if (BEGINS_WITH(walk + 1, "time")) {
- strftime(timebuf, sizeof(timebuf), format_time, &tm);
- maybe_escape_markup(timebuf, &outwalk);
- walk += strlen("time");
-
- } else {
- *(outwalk++) = '%';
- }
- }
+ const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
+ buffer = format_placeholders(format_time, &placeholders[0], num);
}
if (locale != NULL) {