From 717484184f6e8a3bb38f50cc6fbe4fbfb3544cd9 Mon Sep 17 00:00:00 2001 From: Kinware AB Date: Tue, 12 Nov 2013 20:51:23 +0100 Subject: [PATCH] Add support for path_exists directive. --- i3status.c | 14 ++++++++++++++ include/i3status.h | 1 + man/i3status.man | 20 ++++++++++++++++++-- src/general.c | 1 + src/print_path_exists.c | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/print_path_exists.c diff --git a/i3status.c b/i3status.c index 1b9c595..e2befbc 100644 --- a/i3status.c +++ b/i3status.c @@ -210,6 +210,13 @@ int main(int argc, char *argv[]) { CFG_END() }; + cfg_opt_t path_exists_opts[] = { + CFG_STR("path", NULL, CFGF_NONE), + CFG_STR("format", "%title: %status", CFGF_NONE), + CFG_CUSTOM_COLOR_OPTS, + CFG_END() + }; + cfg_opt_t wireless_opts[] = { CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE), CFG_STR("format_down", "W: down", CFGF_NONE), @@ -298,6 +305,7 @@ int main(int argc, char *argv[]) { CFG_STR_LIST("order", "{}", CFGF_NONE), CFG_SEC("general", general_opts, CFGF_NONE), CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI), + CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI), CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI), CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI), CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI), @@ -481,6 +489,12 @@ int main(int argc, char *argv[]) { SEC_CLOSE_MAP; } + CASE_SEC_TITLE("path_exists") { + SEC_OPEN_MAP("path_exists"); + print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format")); + SEC_CLOSE_MAP; + } + CASE_SEC_TITLE("disk") { SEC_OPEN_MAP("disk_info"); print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format")); diff --git a/include/i3status.h b/include/i3status.h index 64cd660..e64cde5 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -152,6 +152,7 @@ void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t); const char *get_ip_addr(); void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down); void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format); +void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format); void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int); void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format); void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down); diff --git a/man/i3status.man b/man/i3status.man index 8c6f616..e18dbc7 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -50,7 +50,8 @@ general { order += "ipv6" order += "disk /" order += "run_watch DHCP" -order += "run_watch VPN" +order += "run_watch VPNC" +order += "path_exists VPN" order += "wireless wlan0" order += "ethernet eth0" order += "battery 0" @@ -81,10 +82,16 @@ run_watch DHCP { pidfile = "/var/run/dhclient*.pid" } -run_watch VPN { +run_watch VPNC { + # file containing the PID of a vpnc process pidfile = "/var/run/vpnc/pid" } +path_exists VPN { + # path exists when a VPN tunnel launched by nmcli/nm-applet is active + path = "/proc/sys/net/ipv4/conf/tun0" +} + tztime local { format = "%Y-%m-%d %H:%M:%S" } @@ -193,6 +200,15 @@ a specific application, such as a VPN client or your DHCP client is running. *Example format*: +%title: %status+ +=== Path-exists + +Checks if the given path exists in the filesystem. You can use this to check if +something is active, like for example a VPN tunnel managed by NetworkManager. + +*Example order*: +path_exists VPN+ + +*Example format*: +%title: %status+ + === Wireless Gets the link quality and ESSID of the given wireless network interface. You diff --git a/src/general.c b/src/general.c index 7ec80a8..c5b33b4 100644 --- a/src/general.c +++ b/src/general.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "i3status.h" diff --git a/src/print_path_exists.c b/src/print_path_exists.c new file mode 100644 index 0000000..c5225a1 --- /dev/null +++ b/src/print_path_exists.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include "i3status.h" + +void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format) { + const char *walk; + char *outwalk = buffer; + struct stat st; + const bool exists = (stat(path, &st) == 0); + + INSTANCE(path); + + START_COLOR((exists ? "color_good" : "color_bad")); + + for (walk = format; *walk != '\0'; walk++) { + if (*walk != '%') { + *(outwalk++) = *walk; + continue; + } + + if (strncmp(walk+1, "title", strlen("title")) == 0) { + outwalk += sprintf(outwalk, "%s", title); + walk += strlen("title"); + } else if (strncmp(walk+1, "status", strlen("status")) == 0) { + outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no")); + walk += strlen("status"); + } + } + + END_COLOR; + OUTPUT_FULL_TEXT(buffer); +} -- 2.39.5