i3 - improved tiling WM


i3bar: Print error message when status_command fails

Patch status: merged

Patch by Tony Crisci

Long description:

Add a function to i3bar to print an error message in the status line
when the child process invoked by status_command fails to provide
input that can be displayed as a statusline.

When the child provides JSON that cannot be parsed, alert the user and
convey a short message provided by yajl communicating the specific
problem.

When the child (or the shell executing the status command) exits
unexpectedly, alert the user and display the exit code. The cases where
the status command is not executable or not found in the user's PATH are
treated specially.

fixes #1130

To apply this patch, use:
curl http://cr.i3wm.org/patch/348/raw.patch | git am

b/i3bar/src/child.c

27
@@ -13,6 +13,7 @@
28
 #include <sys/wait.h>
29
 #include <signal.h>
30
 #include <stdio.h>
31
+#include <stdarg.h>
32
 #include <fcntl.h>
33
 #include <string.h>
34
 #include <errno.h>
35
@@ -61,6 +62,58 @@ char *statusline_buffer = NULL;
36
 int child_stdin;
37
 
38
 /*
39
+ * Clears all blocks from the statusline structure in memory and frees their
40
+ * associated resources.
41
+ */
42
+static void clear_status_blocks() {
43
+    struct status_block *first;
44
+    while (!TAILQ_EMPTY(&statusline_head)) {
45
+        first = TAILQ_FIRST(&statusline_head);
46
+        I3STRING_FREE(first->full_text);
47
+        TAILQ_REMOVE(&statusline_head, first, blocks);
48
+        free(first);
49
+    }
50
+}
51
+
52
+/*
53
+ * Replaces the statusline in memory with an error message. Pass a format
54
+ * string and format parameters as you would in `printf'. The next time
55
+ * `draw_bars' is called, the error message text will be drawn on the bar in
56
+ * the space allocated for the statusline.
57
+ */
58
+
59
+/* forward function declaration is needed to add __attribute__ mechanism which
60
+ * helps the compiler understand we are defining a printf wrapper */
61
+static void set_statusline_error(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
62
+
63
+static void set_statusline_error(const char *format, ...) {
64
+    clear_status_blocks();
65
+
66
+    char *message;
67
+    va_list args;
68
+    va_start(args, format);
69
+    vasprintf(&message, format, args);
70
+
71
+    struct status_block *err_block = scalloc(sizeof(struct status_block));
72
+    err_block->full_text = i3string_from_utf8("Error: ");
73
+    err_block->name = "error";
74
+    err_block->color = "red";
75
+    err_block->no_separator = true;
76
+
77
+    struct status_block *message_block = scalloc(sizeof(struct status_block));
78
+    message_block->full_text = i3string_from_utf8(message);
79
+    message_block->name = "error_message";
80
+    message_block->color = "red";
81
+    message_block->no_separator = true;
82
+
83
+    TAILQ_INSERT_HEAD(&statusline_head, err_block, blocks);
84
+    TAILQ_INSERT_TAIL(&statusline_head, message_block, blocks);
85
+
86
+    FREE(message);
87
+    va_end(args);
88
+}
89
+
90
+/*
91
  * Stop and free() the stdin- and sigchild-watchers
92
  *
93
  */
94
@@ -241,6 +294,7 @@ static unsigned char *get_buffer(ev_io *watcher, int *ret_buffer_len) {
95
             /* end of file, kill the watcher */
96
             ELOG("stdin: received EOF\n");
97
             cleanup();
98
+            set_statusline_error("Received EOF from statusline process");
99
             draw_bars(false);
100
             *ret_buffer_len = -1;
101
             return NULL;
102
@@ -280,8 +334,18 @@ static bool read_json_input(unsigned char *input, int length) {
103
 #else
104
     if (status != yajl_status_ok && status != yajl_status_insufficient_data) {
105
 #endif
106
-        fprintf(stderr, "[i3bar] Could not parse JSON input (code %d): %.*s\n",
107
-                status, length, input);
108
+        char *message = (char *)yajl_get_error(parser, 0, input, length);
109
+
110
+        /* strip the newline yajl adds to the error message */
111
+        if (message[strlen(message) - 1] == '\n')
112
+            message[strlen(message) - 1] = '\0';
113
+
114
+        fprintf(stderr, "[i3bar] Could not parse JSON input (code = %d, message = %s): %.*s\n",
115
+                status, message, length, input);
116
+
117
+        set_statusline_error("Could not parse JSON (%s)", message);
118
+        yajl_free_error(parser, (unsigned char*)message);
119
+        draw_bars(false);
120
     } else if (parser_context.has_urgent) {
121
         has_urgent = true;
122
     }
123
@@ -351,10 +415,23 @@ void stdin_io_first_line_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
124
  *
125
  */
126
 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
127
+    int exit_status = WEXITSTATUS(watcher->rstatus);
128
+
129
     ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
130
            child.pid,
131
-           watcher->rstatus);
132
+           exit_status);
133
+
134
+    /* this error is most likely caused by a user giving a nonexecutable or
135
+     * nonexistent file, so we will handle those cases separately. */
136
+    if (exit_status == 126)
137
+        set_statusline_error("status_command is not executable (exit %d)", exit_status);
138
+    else if (exit_status == 127)
139
+        set_statusline_error("status_command not found (exit %d)", exit_status);
140
+    else
141
+        set_statusline_error("status_command process exited unexpectedly (exit %d)", exit_status);
142
+
143
     cleanup();
144
+    draw_bars(false);
145
 }
146
 
147
 void child_write_output(void) {