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