]> git.sur5r.net Git - i3/i3status/blob - i3status.c
removing Yoda-Style
[i3/i3status] / i3status.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3status – Generates a status line for dzen2 or xmobar
5  *
6  * Copyright © 2008-2012 Michael Stapelberg and contributors
7  * Copyright © 2009 Thorsten Toepper <atsutane at freethoughts dot de>
8  * Copyright © 2010 Axel Wagner <mail at merovius dot de>
9  * Copyright © 2010 Fernando Tarlá Cardoso Lemos <fernandotcl at gmail dot com>
10  *
11  * See file LICENSE for license information.
12  *
13  */
14 #include <limits.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdbool.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <getopt.h>
23 #include <signal.h>
24 #include <confuse.h>
25 #include <glob.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <time.h>
29 #include <sys/time.h>
30 #include <locale.h>
31
32 #include <yajl/yajl_gen.h>
33 #include <yajl/yajl_version.h>
34
35 #include "i3status.h"
36
37 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); }
38
39 #define CFG_CUSTOM_ALIGN_OPT \
40     CFG_STR_CB("align", NULL, CFGF_NONE, parse_align)
41
42 #define CFG_COLOR_OPTS(good, degraded, bad) \
43     CFG_STR("color_good", good, CFGF_NONE), \
44     CFG_STR("color_degraded", degraded, CFGF_NONE), \
45     CFG_STR("color_bad", bad, CFGF_NONE)
46
47 #define CFG_CUSTOM_COLOR_OPTS CFG_COLOR_OPTS(NULL, NULL, NULL)
48
49 #define CFG_CUSTOM_MIN_WIDTH_OPT \
50     CFG_PTR_CB("min_width", NULL, CFGF_NONE, parse_min_width, free)
51
52 /* socket file descriptor for general purposes */
53 int general_socket;
54
55 static bool exit_upon_signal = false;
56
57 cfg_t *cfg, *cfg_general, *cfg_section;
58
59 void **cur_instance;
60
61 /*
62  * Set the exit_upon_signal flag, because one cannot do anything in a safe
63  * manner in a signal handler (e.g. fprintf, which we really want to do for
64  * debugging purposes), see
65  * https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers
66  *
67  */
68 void fatalsig(int signum) {
69         exit_upon_signal = true;
70 }
71
72 /*
73  * Do nothing upon SIGUSR1. Running this signal handler will nevertheless
74  * interrupt nanosleep() so that i3status immediately generates new output.
75  *
76  */
77 void sigusr1(int signum) {
78 }
79
80 /*
81  * Checks if the given path exists by calling stat().
82  *
83  */
84 static bool path_exists(const char *path) {
85         struct stat buf;
86         return (stat(path, &buf) == 0);
87 }
88
89 static void *scalloc(size_t size) {
90         void *result = calloc(size, 1);
91         exit_if_null(result, "Error: out of memory (calloc(%zd))\n", size);
92         return result;
93 }
94
95 static char *sstrdup(const char *str) {
96         char *result = strdup(str);
97         exit_if_null(result, "Error: out of memory (strdup())\n");
98         return result;
99 }
100
101 /*
102  * Parses the "align" module option (to validate input).
103  */
104 static int parse_align(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
105         if (strcasecmp(value, "left") != 0 && strcasecmp(value,"right") != 0 && strcasecmp(value, "center") != 0)
106                 die("Invalid alignment attribute found in section %s, line %d: \"%s\"\n"
107                     "Valid attributes are: left, center, right\n", context->name, context->line, value);
108
109         const char **cresult = result;
110         *cresult = value;
111
112         return 0;
113 }
114
115 /*
116  * Parses the "min_width" module option whose value can either be a string or an integer.
117  */
118 static int parse_min_width(cfg_t *context, cfg_opt_t *option, const char *value, void *result) {
119         char *end;
120         long num = strtol(value, &end, 10);
121
122         if (num < 0)
123                 die("Invalid min_width attribute found in section %s, line %d: %d\n"
124                     "Expected positive integer or string\n", context->name, context->line, num);
125         else if (num == LONG_MIN || num == LONG_MAX || (end && *end != '\0'))
126                 num = 0;
127
128         if (strlen(value) == 0)
129                 die("Empty min_width attribute found in section %s, line %d\n"
130                     "Expected positive integer or non-empty string\n", context->name, context->line);
131
132         if (strcmp(value, "0") == 0)
133                 die("Invalid min_width attribute found in section %s, line %d: \"%s\"\n"
134                     "Expected positive integer or string\n", context->name, context->line, value);
135
136         struct min_width *parsed = scalloc(sizeof(struct min_width));
137         parsed->num = num;
138
139         /* num is preferred, but if it’s 0 (i.e. not valid), store and use
140          * the raw string value */
141         if (num == 0)
142                 parsed->str = sstrdup(value);
143
144         struct min_width **cresult = result;
145         *cresult = parsed;
146
147         return 0;
148 }
149
150 /*
151  * Validates a color in "#RRGGBB" format
152  *
153  */
154 static int valid_color(const char *value)
155 {
156         if (strlen(value) != 7) return 0;
157         if (value[0] != '#') return 0;
158         for (int i = 1; i < 7; ++i) {
159                 if (value[i] >= '0' && value[i] <= '9') continue;
160                 if (value[i] >= 'a' && value[i] <= 'f') continue;
161                 if (value[i] >= 'A' && value[i] <= 'F') continue;
162                 return 0;
163         }
164         return 1;
165 }
166
167 /*
168  * This function resolves ~ in pathnames.
169  * It may resolve wildcards in the first part of the path, but if no match
170  * or multiple matches are found, it just returns a copy of path as given.
171  *
172  */
173 static char *resolve_tilde(const char *path) {
174         static glob_t globbuf;
175         char *head, *tail, *result = NULL;
176
177         tail = strchr(path, '/');
178         head = strndup(path, tail ? (size_t)(tail - path) : strlen(path));
179
180         int res = glob(head, GLOB_TILDE, NULL, &globbuf);
181         free(head);
182         /* no match, or many wildcard matches are bad */
183         if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
184                 result = sstrdup(path);
185         else if (res != 0) {
186                 die("glob() failed");
187         } else {
188                 head = globbuf.gl_pathv[0];
189                 result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
190                 strncpy(result, head, strlen(head));
191                 if (tail)
192                         strncat(result, tail, strlen(tail));
193         }
194         globfree(&globbuf);
195
196         return result;
197 }
198
199 static char *get_config_path(void) {
200         char *xdg_config_home, *xdg_config_dirs, *config_path;
201
202         /* 1: check the traditional path under the home directory */
203         config_path = resolve_tilde("~/.i3status.conf");
204         if (path_exists(config_path))
205                 return config_path;
206
207         /* 2: check for $XDG_CONFIG_HOME/i3status/config */
208         if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
209                 xdg_config_home = "~/.config";
210
211         xdg_config_home = resolve_tilde(xdg_config_home);
212         if (asprintf(&config_path, "%s/i3status/config", xdg_config_home) == -1)
213                 die("asprintf() failed");
214         free(xdg_config_home);
215
216         if (path_exists(config_path))
217                 return config_path;
218         free(config_path);
219
220         /* 3: check the traditional path under /etc */
221         config_path = SYSCONFDIR "/i3status.conf";
222         if (path_exists(config_path))
223             return sstrdup(config_path);
224
225         /* 4: check for $XDG_CONFIG_DIRS/i3status/config */
226         if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
227                 xdg_config_dirs = "/etc/xdg";
228
229         char *buf = strdup(xdg_config_dirs);
230         char *tok = strtok(buf, ":");
231         while (tok != NULL) {
232                 tok = resolve_tilde(tok);
233                 if (asprintf(&config_path, "%s/i3status/config", tok) == -1)
234                         die("asprintf() failed");
235                 free(tok);
236                 if (path_exists(config_path)) {
237                         free(buf);
238                         return config_path;
239                 }
240                 free(config_path);
241                 tok = strtok(NULL, ":");
242         }
243         free(buf);
244
245         die("Unable to find the configuration file (looked at "
246                 "~/.i3status.conf, $XDG_CONFIG_HOME/i3status/config, "
247                 "/etc/i3status.conf and $XDG_CONFIG_DIRS/i3status/config)");
248         return NULL;
249 }
250
251 /*
252  * Returns the default separator to use if no custom separator has been specified.
253  */
254 static char *get_default_separator() {
255         if (output_format == O_DZEN2)
256                 return "^p(5;-2)^ro(2)^p()^p(5)";
257         if (output_format == O_I3BAR)
258                 // anything besides the empty string indicates that the default separator should be used
259                 return "default";
260         return " | ";
261 }
262
263 int main(int argc, char *argv[]) {
264         unsigned int j;
265
266         cfg_opt_t general_opts[] = {
267                 CFG_STR("output_format", "auto", CFGF_NONE),
268                 CFG_BOOL("colors", 1, CFGF_NONE),
269                 CFG_STR("separator", "default", CFGF_NONE),
270                 CFG_STR("color_separator", "#333333", CFGF_NONE),
271                 CFG_INT("interval", 1, CFGF_NONE),
272                 CFG_COLOR_OPTS("#00FF00", "#FFFF00", "#FF0000"),
273                 CFG_END()
274         };
275
276         cfg_opt_t run_watch_opts[] = {
277                 CFG_STR("pidfile", NULL, CFGF_NONE),
278                 CFG_STR("format", "%title: %status", CFGF_NONE),
279                 CFG_CUSTOM_ALIGN_OPT,
280                 CFG_CUSTOM_COLOR_OPTS,
281                 CFG_CUSTOM_MIN_WIDTH_OPT,
282                 CFG_END()
283         };
284
285         cfg_opt_t path_exists_opts[] = {
286                 CFG_STR("path", NULL, CFGF_NONE),
287                 CFG_STR("format", "%title: %status", CFGF_NONE),
288                 CFG_CUSTOM_ALIGN_OPT,
289                 CFG_CUSTOM_COLOR_OPTS,
290                 CFG_CUSTOM_MIN_WIDTH_OPT,
291                 CFG_END()
292         };
293
294         cfg_opt_t wireless_opts[] = {
295                 CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
296                 CFG_STR("format_down", "W: down", CFGF_NONE),
297                 CFG_CUSTOM_ALIGN_OPT,
298                 CFG_CUSTOM_COLOR_OPTS,
299                 CFG_CUSTOM_MIN_WIDTH_OPT,
300                 CFG_END()
301         };
302
303         cfg_opt_t ethernet_opts[] = {
304                 CFG_STR("format_up", "E: %ip (%speed)", CFGF_NONE),
305                 CFG_STR("format_down", "E: down", CFGF_NONE),
306                 CFG_CUSTOM_ALIGN_OPT,
307                 CFG_CUSTOM_COLOR_OPTS,
308                 CFG_CUSTOM_MIN_WIDTH_OPT,
309                 CFG_END()
310         };
311
312         cfg_opt_t ipv6_opts[] = {
313                 CFG_STR("format_up", "%ip", CFGF_NONE),
314                 CFG_STR("format_down", "no IPv6", CFGF_NONE),
315                 CFG_CUSTOM_ALIGN_OPT,
316                 CFG_CUSTOM_COLOR_OPTS,
317                 CFG_CUSTOM_MIN_WIDTH_OPT,
318                 CFG_END()
319         };
320
321         cfg_opt_t battery_opts[] = {
322                 CFG_STR("format", "%status %percentage %remaining", CFGF_NONE),
323                 CFG_STR("format_down", "No battery", CFGF_NONE),
324                 CFG_STR("status_chr", "CHR", CFGF_NONE),
325                 CFG_STR("status_bat", "BAT", CFGF_NONE),
326                 CFG_STR("status_full", "FULL", CFGF_NONE),
327                 CFG_STR("path", "/sys/class/power_supply/BAT%d/uevent", CFGF_NONE),
328                 CFG_INT("low_threshold", 30, CFGF_NONE),
329                 CFG_STR("threshold_type", "time", CFGF_NONE),
330                 CFG_BOOL("last_full_capacity", false, CFGF_NONE),
331                 CFG_BOOL("integer_battery_capacity", false, CFGF_NONE),
332                 CFG_BOOL("hide_seconds", false, CFGF_NONE),
333                 CFG_CUSTOM_ALIGN_OPT,
334                 CFG_CUSTOM_COLOR_OPTS,
335                 CFG_CUSTOM_MIN_WIDTH_OPT,
336                 CFG_END()
337         };
338
339         cfg_opt_t time_opts[] = {
340                 CFG_STR("format", "%Y-%m-%d %H:%M:%S", CFGF_NONE),
341                 CFG_CUSTOM_ALIGN_OPT,
342                 CFG_CUSTOM_MIN_WIDTH_OPT,
343                 CFG_END()
344         };
345
346         cfg_opt_t tztime_opts[] = {
347                 CFG_STR("format", "%Y-%m-%d %H:%M:%S %Z", CFGF_NONE),
348                 CFG_STR("timezone", "", CFGF_NONE),
349                 CFG_CUSTOM_ALIGN_OPT,
350                 CFG_CUSTOM_MIN_WIDTH_OPT,
351                 CFG_END()
352         };
353
354         cfg_opt_t ddate_opts[] = {
355                 CFG_STR("format", "%{%a, %b %d%}, %Y%N - %H", CFGF_NONE),
356                 CFG_CUSTOM_ALIGN_OPT,
357                 CFG_CUSTOM_MIN_WIDTH_OPT,
358                 CFG_END()
359         };
360
361         cfg_opt_t load_opts[] = {
362                 CFG_STR("format", "%1min %5min %15min", CFGF_NONE),
363                 CFG_FLOAT("max_threshold", 5, CFGF_NONE),
364                 CFG_CUSTOM_ALIGN_OPT,
365                 CFG_CUSTOM_COLOR_OPTS,
366                 CFG_CUSTOM_MIN_WIDTH_OPT,
367                 CFG_END()
368         };
369
370         cfg_opt_t usage_opts[] = {
371                 CFG_STR("format", "%usage", CFGF_NONE),
372                 CFG_CUSTOM_ALIGN_OPT,
373                 CFG_CUSTOM_MIN_WIDTH_OPT,
374                 CFG_END()
375         };
376
377         cfg_opt_t temp_opts[] = {
378                 CFG_STR("format", "%degrees C", CFGF_NONE),
379                 CFG_STR("path", NULL, CFGF_NONE),
380                 CFG_INT("max_threshold", 75, CFGF_NONE),
381                 CFG_CUSTOM_ALIGN_OPT,
382                 CFG_CUSTOM_COLOR_OPTS,
383                 CFG_CUSTOM_MIN_WIDTH_OPT,
384                 CFG_END()
385         };
386
387         cfg_opt_t disk_opts[] = {
388                 CFG_STR("format", "%free", CFGF_NONE),
389                 CFG_STR("format_not_mounted", NULL, CFGF_NONE),
390                 CFG_STR("prefix_type", "binary", CFGF_NONE),
391                 CFG_STR("threshold_type", "percentage_avail", CFGF_NONE),
392                 CFG_FLOAT("low_threshold", 0, CFGF_NONE),
393                 CFG_CUSTOM_ALIGN_OPT,
394                 CFG_CUSTOM_COLOR_OPTS,
395                 CFG_CUSTOM_MIN_WIDTH_OPT,
396                 CFG_END()
397         };
398
399         cfg_opt_t volume_opts[] = {
400                 CFG_STR("format", "♪: %volume", CFGF_NONE),
401                 CFG_STR("format_muted", "♪: 0%%", CFGF_NONE),
402                 CFG_STR("device", "default", CFGF_NONE),
403                 CFG_STR("mixer", "Master", CFGF_NONE),
404                 CFG_INT("mixer_idx", 0, CFGF_NONE),
405                 CFG_CUSTOM_ALIGN_OPT,
406                 CFG_CUSTOM_COLOR_OPTS,
407                 CFG_CUSTOM_MIN_WIDTH_OPT,
408                 CFG_END()
409         };
410
411         cfg_opt_t opts[] = {
412                 CFG_STR_LIST("order", "{}", CFGF_NONE),
413                 CFG_SEC("general", general_opts, CFGF_NONE),
414                 CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
415                 CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
416                 CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
417                 CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
418                 CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
419                 CFG_SEC("cpu_temperature", temp_opts, CFGF_TITLE | CFGF_MULTI),
420                 CFG_SEC("disk", disk_opts, CFGF_TITLE | CFGF_MULTI),
421                 CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
422                 CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
423                 CFG_SEC("time", time_opts, CFGF_NONE),
424                 CFG_SEC("tztime", tztime_opts, CFGF_TITLE | CFGF_MULTI),
425                 CFG_SEC("ddate", ddate_opts, CFGF_NONE),
426                 CFG_SEC("load", load_opts, CFGF_NONE),
427                 CFG_SEC("cpu_usage", usage_opts, CFGF_NONE),
428                 CFG_END()
429         };
430
431         char *configfile = NULL;
432         int o, option_index = 0;
433         struct option long_options[] = {
434                 {"config", required_argument, 0, 'c'},
435                 {"help", no_argument, 0, 'h'},
436                 {"version", no_argument, 0, 'v'},
437                 {0, 0, 0, 0}
438         };
439
440         struct sigaction action;
441         memset(&action, 0, sizeof(struct sigaction));
442         action.sa_handler = fatalsig;
443
444         /* Exit upon SIGPIPE because when we have nowhere to write to, gathering system
445          * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
446          * only this will trigger a reset of the cursor in the terminal output-format.
447          */
448         sigaction(SIGPIPE, &action, NULL);
449         sigaction(SIGTERM, &action, NULL);
450         sigaction(SIGINT, &action, NULL);
451
452         memset(&action, 0, sizeof(struct sigaction));
453         action.sa_handler = sigusr1;
454         sigaction(SIGUSR1, &action, NULL);
455
456         if (setlocale(LC_ALL, "") == NULL)
457                 die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
458
459         while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
460                 if ((char)o == 'c')
461                         configfile = optarg;
462                 else if ((char)o == 'h') {
463                         printf("i3status " VERSION " © 2008-2012 Michael Stapelberg and contributors\n"
464                                 "Syntax: %s [-c <configfile>] [-h] [-v]\n", argv[0]);
465                         return 0;
466                 } else if ((char)o == 'v') {
467                         printf("i3status " VERSION " © 2008-2012 Michael Stapelberg and contributors\n");
468                         return 0;
469                 }
470
471
472         if (configfile == NULL)
473                 configfile = get_config_path();
474
475         cfg = cfg_init(opts, CFGF_NOCASE);
476         if (cfg_parse(cfg, configfile) == CFG_PARSE_ERROR)
477                 return EXIT_FAILURE;
478
479         if (cfg_size(cfg, "order") == 0)
480                 die("Your 'order' array is empty. Please fix your config.\n");
481
482         cfg_general = cfg_getsec(cfg, "general");
483         if (cfg_general == NULL)
484                 die("Could not get section \"general\"\n");
485
486         char *output_str = cfg_getstr(cfg_general, "output_format");
487         if (strcasecmp(output_str, "auto") == 0) {
488                 fprintf(stderr, "i3status: trying to auto-detect output_format setting\n");
489                 output_str = auto_detect_format();
490                 if (!output_str) {
491                         output_str = "none";
492                         fprintf(stderr, "i3status: falling back to \"none\"\n");
493                 } else {
494                         fprintf(stderr, "i3status: auto-detected \"%s\"\n", output_str);
495                 }
496         }
497
498         if (strcasecmp(output_str, "dzen2") == 0)
499                 output_format = O_DZEN2;
500         else if (strcasecmp(output_str, "xmobar") == 0)
501                 output_format = O_XMOBAR;
502         else if (strcasecmp(output_str, "i3bar") == 0)
503                 output_format = O_I3BAR;
504         else if (strcasecmp(output_str, "term") == 0)
505                 output_format = O_TERM;
506         else if (strcasecmp(output_str, "none") == 0)
507                 output_format = O_NONE;
508         else die("Unknown output format: \"%s\"\n", output_str);
509
510         const char *separator = cfg_getstr(cfg_general, "separator");
511
512         // if no custom separator has been provided, use the default one
513         if (strcasecmp(separator, "default") == 0)
514                 separator = get_default_separator();
515
516         if (!valid_color(cfg_getstr(cfg_general, "color_good"))
517                         || !valid_color(cfg_getstr(cfg_general, "color_degraded"))
518                         || !valid_color(cfg_getstr(cfg_general, "color_bad"))
519                         || !valid_color(cfg_getstr(cfg_general, "color_separator")))
520                die("Bad color format");
521
522 #if YAJL_MAJOR >= 2
523         yajl_gen json_gen = yajl_gen_alloc(NULL);
524 #else
525         yajl_gen json_gen = yajl_gen_alloc(NULL, NULL);
526 #endif
527
528         if (output_format == O_I3BAR) {
529                 /* Initialize the i3bar protocol. See i3/docs/i3bar-protocol
530                  * for details. */
531                 printf("{\"version\":1}\n[\n");
532                 fflush(stdout);
533                 yajl_gen_array_open(json_gen);
534                 yajl_gen_clear(json_gen);
535         }
536         if (output_format == O_TERM) {
537                 /* Save the cursor-position and hide the cursor */
538                 printf("\033[s\033[?25l");
539                 /* Undo at exit */
540                 atexit(&reset_cursor);
541         }
542
543         if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
544                 die("Could not create socket\n");
545
546         int interval = cfg_getint(cfg_general, "interval");
547
548         /* One memory page which each plugin can use to buffer output.
549          * Even though it’s unclean, we just assume that the user will not
550          * specify a format string which expands to something longer than 4096
551          * bytes — given that the output of i3status is used to display
552          * information on screen, more than 1024 characters for the full line
553          * (!), not individual plugins, seem very unlikely. */
554         char buffer[4096];
555
556         void **per_instance = calloc(cfg_size(cfg, "order"), sizeof(*per_instance));
557
558         while (1) {
559                 if (exit_upon_signal) {
560                         fprintf(stderr, "Exiting due to signal.\n");
561                         exit(1);
562                 }
563                 struct timeval tv;
564                 gettimeofday(&tv, NULL);
565                 if (output_format == O_I3BAR)
566                         yajl_gen_array_open(json_gen);
567                 else if (output_format == O_TERM)
568                         /* Restore the cursor-position, clear line */
569                         printf("\033[u\033[K");
570                 for (j = 0; j < cfg_size(cfg, "order"); j++) {
571                         cur_instance = per_instance + j;
572                         if (j > 0)
573                                 print_separator(separator);
574
575                         const char *current = cfg_getnstr(cfg, "order", j);
576
577                         CASE_SEC("ipv6") {
578                                 SEC_OPEN_MAP("ipv6");
579                                 print_ipv6_info(json_gen, buffer, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
580                                 SEC_CLOSE_MAP;
581                         }
582
583                         CASE_SEC_TITLE("wireless") {
584                                 SEC_OPEN_MAP("wireless");
585                                 const char *interface = NULL;
586                                 if (strcasecmp(title, "_first_") == 0)
587                                         interface = first_eth_interface(NET_TYPE_WIRELESS);
588                                 if (interface == NULL)
589                                         interface = title;
590                                 print_wireless_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
591                                 SEC_CLOSE_MAP;
592                         }
593
594                         CASE_SEC_TITLE("ethernet") {
595                                 SEC_OPEN_MAP("ethernet");
596                                 const char *interface = NULL;
597                                 if (strcasecmp(title, "_first_") == 0)
598                                         interface = first_eth_interface(NET_TYPE_ETHERNET);
599                                 if (interface == NULL)
600                                         interface = title;
601                                 print_eth_info(json_gen, buffer, interface, cfg_getstr(sec, "format_up"), cfg_getstr(sec, "format_down"));
602                                 SEC_CLOSE_MAP;
603                         }
604
605                         CASE_SEC_TITLE("battery") {
606                                 SEC_OPEN_MAP("battery");
607                                 print_battery_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_down"), cfg_getstr(sec, "status_chr"), cfg_getstr(sec, "status_bat"), cfg_getstr(sec, "status_full"), cfg_getint(sec, "low_threshold"), cfg_getstr(sec, "threshold_type"), cfg_getbool(sec, "last_full_capacity"), cfg_getbool(sec, "integer_battery_capacity"), cfg_getbool(sec, "hide_seconds"));
608                                 SEC_CLOSE_MAP;
609                         }
610
611                         CASE_SEC_TITLE("run_watch") {
612                                 SEC_OPEN_MAP("run_watch");
613                                 print_run_watch(json_gen, buffer, title, cfg_getstr(sec, "pidfile"), cfg_getstr(sec, "format"));
614                                 SEC_CLOSE_MAP;
615                         }
616
617                         CASE_SEC_TITLE("path_exists") {
618                                 SEC_OPEN_MAP("path_exists");
619                                 print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
620                                 SEC_CLOSE_MAP;
621                         }
622
623                         CASE_SEC_TITLE("disk") {
624                                 SEC_OPEN_MAP("disk_info");
625                                 print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "format_not_mounted"), cfg_getstr(sec, "prefix_type"), cfg_getstr(sec, "threshold_type"), cfg_getfloat(sec, "low_threshold"));
626                                 SEC_CLOSE_MAP;
627                         }
628
629                         CASE_SEC("load") {
630                                 SEC_OPEN_MAP("load");
631                                 print_load(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getfloat(sec, "max_threshold"));
632                                 SEC_CLOSE_MAP;
633                         }
634
635                         CASE_SEC("time") {
636                                 SEC_OPEN_MAP("time");
637                                 print_time(json_gen, buffer, cfg_getstr(sec, "format"), NULL, tv.tv_sec);
638                                 SEC_CLOSE_MAP;
639                         }
640
641                         CASE_SEC_TITLE("tztime") {
642                                 SEC_OPEN_MAP("tztime");
643                                 print_time(json_gen, buffer, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), tv.tv_sec);
644                                 SEC_CLOSE_MAP;
645                         }
646
647                         CASE_SEC("ddate") {
648                                 SEC_OPEN_MAP("ddate");
649                                 print_ddate(json_gen, buffer, cfg_getstr(sec, "format"), tv.tv_sec);
650                                 SEC_CLOSE_MAP;
651                         }
652
653                         CASE_SEC_TITLE("volume") {
654                                 SEC_OPEN_MAP("volume");
655                                 print_volume(json_gen, buffer, cfg_getstr(sec, "format"),
656                                              cfg_getstr(sec, "format_muted"),
657                                              cfg_getstr(sec, "device"),
658                                              cfg_getstr(sec, "mixer"),
659                                              cfg_getint(sec, "mixer_idx"));
660                                 SEC_CLOSE_MAP;
661                         }
662
663                         CASE_SEC_TITLE("cpu_temperature") {
664                                 SEC_OPEN_MAP("cpu_temperature");
665                                 print_cpu_temperature_info(json_gen, buffer, atoi(title), cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getint(sec, "max_threshold"));
666                                 SEC_CLOSE_MAP;
667                         }
668
669                         CASE_SEC("cpu_usage") {
670                                 SEC_OPEN_MAP("cpu_usage");
671                                 print_cpu_usage(json_gen, buffer, cfg_getstr(sec, "format"));
672                                 SEC_CLOSE_MAP;
673                         }
674                 }
675                 if (output_format == O_I3BAR) {
676                         yajl_gen_array_close(json_gen);
677                         const unsigned char *buf;
678 #if YAJL_MAJOR >= 2
679                         size_t len;
680 #else
681                         unsigned int len;
682 #endif
683                         yajl_gen_get_buf(json_gen, &buf, &len);
684                         write(STDOUT_FILENO, buf, len);
685                         yajl_gen_clear(json_gen);
686                 }
687
688                 printf("\n");
689                 fflush(stdout);
690
691                 /* To provide updates on every full second (as good as possible)
692                  * we don’t use sleep(interval) but we sleep until the next
693                  * second (with microsecond precision) plus (interval-1)
694                  * seconds. We also align to 60 seconds modulo interval such
695                  * that we start with :00 on every new minute. */
696                 struct timeval current_timeval;
697                 gettimeofday(&current_timeval, NULL);
698                 struct timespec ts = {interval - 1 - (current_timeval.tv_sec % interval), (10e5 - current_timeval.tv_usec) * 1000};
699                 nanosleep(&ts, NULL);
700         }
701 }