2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
5 * © 2010-2011 Axel Wagner and contributors (see also: LICENSE)
7 * child.c: Getting Input for the statusline
12 #include <sys/types.h>
21 #include <yajl/yajl_common.h>
22 #include <yajl/yajl_parse.h>
23 #include <yajl/yajl_version.h>
27 /* Global variables for child_*() */
30 /* stdin- and sigchild-watchers */
34 /* JSON parser for stdin */
35 bool first_line = true;
36 bool plaintext = false;
37 yajl_callbacks callbacks;
40 typedef struct parser_ctx {
41 /* A copy of the last JSON map key. */
44 /* The current block. Will be filled, then copied and put into the list of
46 struct status_block block;
49 parser_ctx parser_context;
51 /* The buffer statusline points to */
52 struct statusline_head statusline_head = TAILQ_HEAD_INITIALIZER(statusline_head);
53 char *statusline_buffer = NULL;
56 * Stop and free() the stdin- and sigchild-watchers
60 if (stdin_io != NULL) {
61 ev_io_stop(main_loop, stdin_io);
63 FREE(statusline_buffer);
64 /* statusline pointed to memory within statusline_buffer */
68 if (child_sig != NULL) {
69 ev_child_stop(main_loop, child_sig);
75 * The start of a new array is the start of a new status line, so we clear all
79 static int stdin_start_array(void *context) {
80 struct status_block *first;
81 while (!TAILQ_EMPTY(&statusline_head)) {
82 first = TAILQ_FIRST(&statusline_head);
83 FREE(first->full_text);
85 TAILQ_REMOVE(&statusline_head, first, blocks);
92 * The start of a map is the start of a single block of the status line.
95 static int stdin_start_map(void *context) {
96 parser_ctx *ctx = context;
97 memset(&(ctx->block), '\0', sizeof(struct status_block));
102 static int stdin_map_key(void *context, const unsigned char *key, size_t len) {
104 static int stdin_map_key(void *context, const unsigned char *key, unsigned int len) {
106 parser_ctx *ctx = context;
107 FREE(ctx->last_map_key);
108 sasprintf(&(ctx->last_map_key), "%.*s", len, key);
113 static int stdin_string(void *context, const unsigned char *val, size_t len) {
115 static int stdin_string(void *context, const unsigned char *val, unsigned int len) {
117 parser_ctx *ctx = context;
118 if (strcasecmp(ctx->last_map_key, "full_text") == 0) {
119 sasprintf(&(ctx->block.full_text), "%.*s", len, val);
121 if (strcasecmp(ctx->last_map_key, "color") == 0) {
122 sasprintf(&(ctx->block.color), "%.*s", len, val);
127 static int stdin_end_map(void *context) {
128 parser_ctx *ctx = context;
129 struct status_block *new_block = smalloc(sizeof(struct status_block));
130 memcpy(new_block, &(ctx->block), sizeof(struct status_block));
131 TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks);
135 static int stdin_end_array(void *context) {
136 DLOG("dumping statusline:\n");
137 struct status_block *current;
138 TAILQ_FOREACH(current, &statusline_head, blocks) {
139 DLOG("full_text = %s\n", current->full_text);
140 DLOG("color = %s\n", current->color);
142 DLOG("end of dump\n");
147 * Callbalk for stdin. We read a line from stdin and store the result
151 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
152 int fd = watcher->fd;
155 int buffer_len = STDIN_CHUNK_SIZE;
156 unsigned char *buffer = smalloc(buffer_len);
159 n = read(fd, buffer + rec, buffer_len - rec);
161 if (errno == EAGAIN) {
165 ELOG("read() failed!: %s\n", strerror(errno));
169 /* end of file, kill the watcher */
170 ELOG("stdin: received EOF\n");
177 if (rec == buffer_len) {
178 buffer_len += STDIN_CHUNK_SIZE;
179 buffer = srealloc(buffer, buffer_len);
182 if (*buffer == '\0') {
187 unsigned char *json_input = buffer;
189 DLOG("Detecting input type based on buffer *%.*s*\n", rec, buffer);
190 /* Detect whether this is JSON or plain text. */
191 plaintext = (strncasecmp((char*)buffer, "{\"version\":", strlen("{\"version\":")) != 0);
193 /* In case of plaintext, we just add a single block and change its
194 * full_text pointer later. */
195 struct status_block *new_block = scalloc(sizeof(struct status_block));
196 TAILQ_INSERT_TAIL(&statusline_head, new_block, blocks);
198 /* At the moment, we don’t care for the version. This might change
199 * in the future, but for now, we just discard it. */
200 while (*json_input != '\n' && *json_input != '\0') {
208 yajl_status status = yajl_parse(parser, json_input, rec);
209 if (status != yajl_status_ok) {
210 fprintf(stderr, "[i3bar] Could not parse JSON input: %.*s\n",
215 struct status_block *first = TAILQ_FIRST(&statusline_head);
216 /* Clear the old buffer if any. */
217 FREE(first->full_text);
218 /* Remove the trailing newline and terminate the string at the same
220 buffer[rec-1] = '\0';
221 first->full_text = (char*)buffer;
227 * We received a sigchild, meaning, that the child-process terminated.
228 * We simply free the respective data-structures and don't care for input
232 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
233 ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
240 * Start a child-process with the specified command and reroute stdin.
241 * We actually start a $SHELL to execute the command so we don't have to care
242 * about arguments and such
245 void start_child(char *command) {
246 /* Allocate a yajl parser which will be used to parse stdin. */
247 memset(&callbacks, '\0', sizeof(yajl_callbacks));
248 callbacks.yajl_map_key = stdin_map_key;
249 callbacks.yajl_string = stdin_string;
250 callbacks.yajl_start_array = stdin_start_array;
251 callbacks.yajl_end_array = stdin_end_array;
252 callbacks.yajl_start_map = stdin_start_map;
253 callbacks.yajl_end_map = stdin_end_map;
255 yajl_parser_config parse_conf = { 0, 0 };
257 parser = yajl_alloc(&callbacks, &parse_conf, NULL, (void*)&parser_context);
259 parser = yajl_alloc(&callbacks, NULL, &parser_context);
263 if (command != NULL) {
266 err(EXIT_FAILURE, "pipe(fd)");
271 ELOG("Couldn't fork(): %s\n", strerror(errno));
274 /* Child-process. Reroute stdout and start shell */
277 dup2(fd[1], STDOUT_FILENO);
279 static const char *shell = NULL;
281 if ((shell = getenv("SHELL")) == NULL)
284 execl(shell, shell, "-c", command, (char*) NULL);
287 /* Parent-process. Rerout stdin */
290 dup2(fd[0], STDIN_FILENO);
292 /* If hide-on-modifier is set, we start of by sending the
293 * child a SIGSTOP, because the bars aren't mapped at start */
294 if (config.hide_on_modifier) {
302 /* We set O_NONBLOCK because blocking is evil in event-driven software */
303 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
305 stdin_io = smalloc(sizeof(ev_io));
306 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
307 ev_io_start(main_loop, stdin_io);
309 /* We must cleanup, if the child unexpectedly terminates */
310 child_sig = smalloc(sizeof(ev_child));
311 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
312 ev_child_start(main_loop, child_sig);
317 * kill()s the child-process (if existent) and closes and
318 * free()s the stdin- and sigchild-watchers
322 if (child_pid != 0) {
323 kill(child_pid, SIGCONT);
324 kill(child_pid, SIGTERM);
326 waitpid(child_pid, &status, 0);
333 * Sends a SIGSTOP to the child-process (if existent)
337 if (child_pid != 0) {
338 kill(child_pid, SIGSTOP);
343 * Sends a SIGCONT to the child-process (if existent)
347 if (child_pid != 0) {
348 kill(child_pid, SIGCONT);