i3 - improved tiling WM


i3bar: Print error message when status_command fails

Patch status: needinfo

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/347/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,55 @@ 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
+static void set_statusline_error(char *format, ...) __attribute__ ((format (printf, 1, 2)));
60
+
61
+static void set_statusline_error(char *format, ...) {
62
+    clear_status_blocks();
63
+
64
+    char buffer[256];
65
+    va_list args;
66
+    va_start(args, format);
67
+    vsnprintf(buffer, 255, format, args);
68
+
69
+    struct status_block *err_block = scalloc(sizeof(struct status_block));
70
+    err_block->full_text = i3string_from_utf8("Error: ");
71
+    err_block->name = "error";
72
+    err_block->color = "red";
73
+    err_block->no_separator = true;
74
+
75
+    struct status_block *message_block = scalloc(sizeof(struct status_block));
76
+    message_block->full_text = i3string_from_utf8(buffer);
77
+    message_block->name = "error_message";
78
+    message_block->no_separator = true;
79
+    message_block->color = "red";
80
+
81
+    TAILQ_INSERT_HEAD(&statusline_head, err_block, blocks);
82
+    TAILQ_INSERT_TAIL(&statusline_head, message_block, blocks);
83
+
84
+    va_end(args);
85
+}
86
+
87
+/*
88
  * Stop and free() the stdin- and sigchild-watchers
89
  *
90
  */
91
@@ -241,6 +291,7 @@ static unsigned char *get_buffer(ev_io *watcher, int *ret_buffer_len) {
92
             /* end of file, kill the watcher */
93
             ELOG("stdin: received EOF\n");
94
             cleanup();
95
+            set_statusline_error("Received EOF from statusline process");
96
             draw_bars(false);
97
             *ret_buffer_len = -1;
98
             return NULL;
99
@@ -280,8 +331,18 @@ static bool read_json_input(unsigned char *input, int length) {
100
 #else
101
     if (status != yajl_status_ok && status != yajl_status_insufficient_data) {
102
 #endif
103
-        fprintf(stderr, "[i3bar] Could not parse JSON input (code %d): %.*s\n",
104
-                status, length, input);
105
+        char *message = (char *)yajl_get_error(parser, 0, input, length);
106
+
107
+        /* strip the newline yajl adds to the error message */
108
+        if (message[strlen(message) - 1] == '\n')
109
+            message[strlen(message) - 1] = '\0';
110
+
111
+        fprintf(stderr, "[i3bar] Could not parse JSON input (code = %d, message = %s): %.*s\n",
112
+                status, message, length, input);
113
+
114
+        set_statusline_error("Could not parse JSON (%s)", message);
115
+        yajl_free_error(parser, (unsigned char*)message);
116
+        draw_bars(false);
117
     } else if (parser_context.has_urgent) {
118
         has_urgent = true;
119
     }
120
@@ -351,10 +412,23 @@ void stdin_io_first_line_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
121
  *
122
  */
123
 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
124
+    int exit_status = WEXITSTATUS(watcher->rstatus);
125
+
126
     ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
127
            child.pid,
128
-           watcher->rstatus);
129
+           exit_status);
130
+
131
+    /* this error is most likely caused by a user giving a nonexecutable or
132
+     * nonexistent file, so we will handle those cases separately. */
133
+    if (exit_status == 126)
134
+        set_statusline_error("status_command is not executable (exit %d)", exit_status);
135
+    else if (exit_status == 127)
136
+        set_statusline_error("status_command not found (exit %d)", exit_status);
137
+    else
138
+        set_statusline_error("status_command process exited unexpectedly (exit %d)", exit_status);
139
+
140
     cleanup();
141
+    draw_bars(false);
142
 }
143
 
144
 void child_write_output(void) {