From c3c41441948b856d39ba794bc2616f1bab0f6eed Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Thu, 12 Nov 2020 09:54:40 -0700 Subject: Implement shell node type This node block supports calling out to a shell program for a custom status section. It reads the first line of stdout from whatever is called. --- Makefile | 5 ++++- i3cstat.conf.sample | 5 +++++ src/config.c | 3 +++ src/config.h | 1 + src/config_shell.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/config_shell.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.c | 2 +- 7 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/config_shell.c create mode 100644 src/config_shell.h diff --git a/Makefile b/Makefile index 2e61691..341e1a0 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,10 @@ config_mem: common config_node config_net: common config_node cc $(CCOPTS) src/config_net.c -c -o obj/config_net.o -config: config_node config_bat config_fs config_net config_date config_time config_mem config_cpu +config_shell: common config_node + cc $(CCOPTS) src/config_shell.c -c -o obj/config_shell.o + +config: config_node config_bat config_fs config_net config_date config_time config_mem config_cpu config_shell cc $(CCOPTS) src/config.c -c -o obj/config.o install: diff --git a/i3cstat.conf.sample b/i3cstat.conf.sample index d267e83..ad06dac 100644 --- a/i3cstat.conf.sample +++ b/i3cstat.conf.sample @@ -41,3 +41,8 @@ label = GMT: [date] tz = Greenwich + +[shell] +label = Stuff done: +script = /usr/bin/dostuff.sh +interval = 5 diff --git a/src/config.c b/src/config.c index eeaa5fe..07bbc2f 100644 --- a/src/config.c +++ b/src/config.c @@ -91,6 +91,9 @@ struct node* config_load(char* path) { } else if(strcmp(section, "cpu") == 0) { cur = node_new(&config_cpu_init); + + } else if(strcmp(section, "shell") == 0) { + cur = node_new(&config_shell_init); } if(!list) diff --git a/src/config.h b/src/config.h index 8364ab5..de0e294 100644 --- a/src/config.h +++ b/src/config.h @@ -27,5 +27,6 @@ #include "config_time.h" #include "config_fs.h" #include "config_bat.h" +#include "config_shell.h" struct node* config_load(char*); diff --git a/src/config_shell.c b/src/config_shell.c new file mode 100644 index 0000000..bbb8042 --- /dev/null +++ b/src/config_shell.c @@ -0,0 +1,51 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "config_shell.h" + +void config_shell_init(struct node* n) { + n->data = malloc(sizeof(struct config_shell)); + n->type = CTYPE_SHELL; + n->loadfunc = &config_shell_load; + n->loadkey = &load_shell_key; +} + +void load_shell_key(struct node* n, char* key, char* val) { + struct config_shell* data = (struct config_shell*) n->data; + + if(strcmp(key, "script") == 0) { + strcpy(data->script, val); + if(n->label[0] == '\0') + sprintf(n->label, "%s", val); + } else { + printf("ERROR: Unknown shell key %s\n", key); + } +} + +int config_shell_load(struct node* n) { + struct config_shell* data = (struct config_shell*) n->data; + FILE* pipe; + char buf[4096]; + + // Call the configured script + pipe = popen(data->script, "r"); + fgets(buf, 4096, pipe); + pclose(pipe); + + sprintf(n->text, "%s", trim(buf)); + return 0; +} diff --git a/src/config_shell.h b/src/config_shell.h new file mode 100644 index 0000000..7cb8608 --- /dev/null +++ b/src/config_shell.h @@ -0,0 +1,40 @@ +/** + * I3cstatus prints a configurable status bar for the i3 window manager + * Copyright (C) 2020 Aaron Ball + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef CONFIG_SHELL +#define CONFIG_SHELL + +#define CTYPE_SHELL 80 + +#include +#include +#include +#include "common.h" +#include "config_node.h" + +struct config_shell { + char script[128]; + char label[128]; +}; + +void config_shell_init(struct node*); + +int config_shell_load(struct node*); + +void load_shell_key(struct node*, char*, char*); + +#endif diff --git a/src/main.c b/src/main.c index c37cc4a..9971521 100644 --- a/src/main.c +++ b/src/main.c @@ -55,7 +55,7 @@ int main(int argc, char* argv[]) { while(1) { printf("[\n"); while(cur) { - if(time(NULL) - cur->lastrun > cur->interval) { + if(time(NULL) - cur->lastrun >= cur->interval) { cur->loadfunc(cur); cur->lastrun = time(NULL); } -- cgit v1.2.3