]> git.sur5r.net Git - i3/i3/blobdiff - i3bar/src/child.c
Merge branch 'master' into next
[i3/i3] / i3bar / src / child.c
index 52e99b4091cf959932c8587adec07c823f16bb8c..cfdf911c11442866176ce158293dd2952befcefc 100644 (file)
 #include "common.h"
 
 /* Global variables for child_*() */
-i3bar_child child = { 0 };
+i3bar_child child;
 
 /* stdin- and sigchild-watchers */
 ev_io    *stdin_io;
 ev_child *child_sig;
 
 /* JSON parser for stdin */
-yajl_callbacks callbacks;
 yajl_handle parser;
 
 /* JSON generator for stdout */
@@ -81,12 +80,7 @@ static void clear_status_blocks() {
  * `draw_bars' is called, the error message text will be drawn on the bar in
  * the space allocated for the statusline.
  */
-
-/* forward function declaration is needed to add __attribute__ mechanism which
- * helps the compiler understand we are defining a printf wrapper */
-static void set_statusline_error(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
-
-static void set_statusline_error(const char *format, ...) {
+__attribute__ ((format (printf, 1, 2))) static void set_statusline_error(const char *format, ...) {
     clear_status_blocks();
 
     char *message;
@@ -272,6 +266,8 @@ static int stdin_end_array(void *context) {
 /*
  * Helper function to read stdin
  *
+ * Returns NULL on EOF.
+ *
  */
 static unsigned char *get_buffer(ev_io *watcher, int *ret_buffer_len) {
     int fd = watcher->fd;
@@ -291,9 +287,7 @@ static unsigned char *get_buffer(ev_io *watcher, int *ret_buffer_len) {
             exit(EXIT_FAILURE);
         }
         if (n == 0) {
-            /* end of file, kill the watcher */
             ELOG("stdin: received EOF\n");
-            cleanup();
             *ret_buffer_len = -1;
             return NULL;
         }
@@ -424,7 +418,7 @@ void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
     if (exit_status == 126)
         set_statusline_error("status_command is not executable (exit %d)", exit_status);
     else if (exit_status == 127)
-        set_statusline_error("status_command not found (exit %d)", exit_status);
+        set_statusline_error("status_command not found or is missing a library dependency (exit %d)", exit_status);
     else
         set_statusline_error("status_command process exited unexpectedly (exit %d)", exit_status);
 
@@ -450,20 +444,27 @@ void child_write_output(void) {
 /*
  * Start a child-process with the specified command and reroute stdin.
  * We actually start a $SHELL to execute the command so we don't have to care
- * about arguments and such
+ * about arguments and such.
+ *
+ * If `command' is NULL, such as in the case when no `status_command' is given
+ * in the bar config, no child will be started.
  *
  */
 void start_child(char *command) {
+    if (command == NULL)
+        return;
+
     /* Allocate a yajl parser which will be used to parse stdin. */
-    memset(&callbacks, '\0', sizeof(yajl_callbacks));
-    callbacks.yajl_map_key = stdin_map_key;
-    callbacks.yajl_boolean = stdin_boolean;
-    callbacks.yajl_string = stdin_string;
-    callbacks.yajl_integer = stdin_integer;
-    callbacks.yajl_start_array = stdin_start_array;
-    callbacks.yajl_end_array = stdin_end_array;
-    callbacks.yajl_start_map = stdin_start_map;
-    callbacks.yajl_end_map = stdin_end_map;
+    static yajl_callbacks callbacks = {
+        .yajl_boolean = stdin_boolean,
+        .yajl_integer = stdin_integer,
+        .yajl_string = stdin_string,
+        .yajl_start_map = stdin_start_map,
+        .yajl_map_key = stdin_map_key,
+        .yajl_end_map = stdin_end_map,
+        .yajl_start_array = stdin_start_array,
+        .yajl_end_array = stdin_end_array,
+    };
 #if YAJL_MAJOR < 2
     yajl_parser_config parse_conf = { 0, 0 };
 
@@ -476,43 +477,41 @@ void start_child(char *command) {
     gen = yajl_gen_alloc(NULL);
 #endif
 
-    if (command != NULL) {
-        int pipe_in[2]; /* pipe we read from */
-        int pipe_out[2]; /* pipe we write to */
+    int pipe_in[2]; /* pipe we read from */
+    int pipe_out[2]; /* pipe we write to */
 
-        if (pipe(pipe_in) == -1)
-            err(EXIT_FAILURE, "pipe(pipe_in)");
-        if (pipe(pipe_out) == -1)
-            err(EXIT_FAILURE, "pipe(pipe_out)");
+    if (pipe(pipe_in) == -1)
+        err(EXIT_FAILURE, "pipe(pipe_in)");
+    if (pipe(pipe_out) == -1)
+        err(EXIT_FAILURE, "pipe(pipe_out)");
 
-        child.pid = fork();
-        switch (child.pid) {
-            case -1:
-                ELOG("Couldn't fork(): %s\n", strerror(errno));
-                exit(EXIT_FAILURE);
-            case 0:
-                /* Child-process. Reroute streams and start shell */
+    child.pid = fork();
+    switch (child.pid) {
+        case -1:
+            ELOG("Couldn't fork(): %s\n", strerror(errno));
+            exit(EXIT_FAILURE);
+        case 0:
+            /* Child-process. Reroute streams and start shell */
 
-                close(pipe_in[0]);
-                close(pipe_out[1]);
+            close(pipe_in[0]);
+            close(pipe_out[1]);
 
-                dup2(pipe_in[1], STDOUT_FILENO);
-                dup2(pipe_out[0], STDIN_FILENO);
+            dup2(pipe_in[1], STDOUT_FILENO);
+            dup2(pipe_out[0], STDIN_FILENO);
 
-                setpgid(child.pid, 0);
-                execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (char*) NULL);
-                return;
-            default:
-                /* Parent-process. Reroute streams */
+            setpgid(child.pid, 0);
+            execl(_PATH_BSHELL, _PATH_BSHELL, "-c", command, (char*) NULL);
+            return;
+        default:
+            /* Parent-process. Reroute streams */
 
-                close(pipe_in[1]);
-                close(pipe_out[0]);
+            close(pipe_in[1]);
+            close(pipe_out[0]);
 
-                dup2(pipe_in[0], STDIN_FILENO);
-                child_stdin = pipe_out[1];
+            dup2(pipe_in[0], STDIN_FILENO);
+            child_stdin = pipe_out[1];
 
-                break;
-        }
+            break;
     }
 
     /* We set O_NONBLOCK because blocking is evil in event-driven software */