fix potential buffer overflow in parse_conky_vars()
[monky] / src / conky.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "text_object.h"
33 #include "conky.h"
34 #include "common.h"
35 #include "timed_thread.h"
36 #include <stdarg.h>
37 #include <math.h>
38 #include <time.h>
39 #include <locale.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <limits.h>
43 #if HAVE_DIRENT_H
44 #include <dirent.h>
45 #endif
46 #include <sys/time.h>
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
51 #ifdef X11
52 #include "x11.h"
53 #include <X11/Xutil.h>
54 #ifdef HAVE_XDAMAGE
55 #include <X11/extensions/Xdamage.h>
56 #endif
57 #ifdef IMLIB2
58 #include "imlib2.h"
59 #endif /* IMLIB2 */
60 #endif /* X11 */
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <netinet/in.h>
64 #include <netdb.h>
65 #include <fcntl.h>
66 #include <getopt.h>
67 #ifdef NCURSES
68 #include <ncurses.h>
69 #endif
70 #ifdef XOAP
71 #include <libxml/parser.h>
72 #endif /* XOAP */
73
74 /* local headers */
75 #include "core.h"
76 #include "algebra.h"
77 #include "build.h"
78 #include "colours.h"
79 #include "combine.h"
80 #include "diskio.h"
81 #include "entropy.h"
82 #include "exec.h"
83 #include "proc.h"
84 #include "user.h"
85 #ifdef X11
86 #include "fonts.h"
87 #endif
88 #include "fs.h"
89 #ifdef HAVE_ICONV
90 #include "iconv_tools.h"
91 #endif
92 #include "logging.h"
93 #include "mixer.h"
94 #include "mail.h"
95 #include "mboxscan.h"
96 #include "net_stat.h"
97 #ifdef NVIDIA
98 #include "nvidia.h"
99 #endif
100 #include "read_tcp.h"
101 #include "scroll.h"
102 #include "specials.h"
103 #include "temphelper.h"
104 #include "template.h"
105 #include "tailhead.h"
106 #include "timeinfo.h"
107 #include "top.h"
108
109 /* check for OS and include appropriate headers */
110 #if defined(__linux__)
111 #include "linux.h"
112 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
113 #include "freebsd.h"
114 #elif defined(__OpenBSD__)
115 #include "openbsd.h"
116 #endif
117
118 #if defined(__FreeBSD_kernel__)
119 #include <bsd/bsd.h>
120 #endif
121
122 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
123 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
124                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
125 int apm_getinfo(int fd, apm_info_t aip);
126 char *get_apm_adapter(void);
127 char *get_apm_battery_life(void);
128 char *get_apm_battery_time(void);
129 #endif
130
131 #ifdef CONFIG_OUTPUT
132 #include "defconfig.h"
133 #include "conf_cookie.h"
134 #endif
135
136 #ifndef S_ISSOCK
137 #define S_ISSOCK(x)   ((x & S_IFMT) == S_IFSOCK)
138 #endif
139
140 #define MAIL_FILE "$MAIL"
141 #define MAX_IF_BLOCK_DEPTH 5
142
143 //#define SIGNAL_BLOCKING
144 #undef SIGNAL_BLOCKING
145
146 /* debugging level, used by logging.h */
147 int global_debug_level = 0;
148
149 /* two strings for internal use */
150 static char *tmpstring1, *tmpstring2;
151
152 /* variables holding various config settings */
153 int short_units;
154 int format_human_readable;
155 int cpu_separate;
156 enum {
157         NO_SPACER = 0,
158         LEFT_SPACER,
159         RIGHT_SPACER
160 } use_spacer;
161 int top_cpu, top_mem, top_time;
162 #ifdef IOSTATS
163 int top_io;
164 #endif
165 #ifdef __linux__
166 int top_running;
167 #endif
168 static unsigned int top_name_width = 15;
169 int output_methods;
170 static int extra_newline;
171 enum x_initialiser_state x_initialised = NO;
172 static volatile int g_signal_pending;
173 /* Update interval */
174 double update_interval;
175 double update_interval_old;
176 double update_interval_bat;
177 void *global_cpu = NULL;
178
179 int argc_copy;
180 char** argv_copy;
181
182 /* prototypes for internally used functions */
183 static void signal_handler(int);
184 static void print_version(void) __attribute__((noreturn));
185 static void reload_config(void);
186
187 static void print_version(void)
188 {
189         printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
190
191         printf("\nCompiled in features:\n\n"
192                    "System config file: "SYSTEM_CONFIG_FILE"\n"
193                    "Package library path: "PACKAGE_LIBDIR"\n\n"
194 #ifdef X11
195                    " X11:\n"
196 # ifdef HAVE_XDAMAGE
197                    "  * Xdamage extension\n"
198 # endif /* HAVE_XDAMAGE */
199 # ifdef HAVE_XDBE
200                    "  * XDBE (double buffer extension)\n"
201 # endif /* HAVE_XDBE */
202 # ifdef XFT
203                    "  * Xft\n"
204 # endif /* XFT */
205 #endif /* X11 */
206                    "\n Music detection:\n"
207 #ifdef AUDACIOUS
208                    "  * Audacious\n"
209 #endif /* AUDACIOUS */
210 #ifdef BMPX
211                    "  * BMPx\n"
212 #endif /* BMPX */
213 #ifdef MPD
214                    "  * MPD\n"
215 #endif /* MPD */
216 #ifdef MOC
217                    "  * MOC\n"
218 #endif /* MOC */
219 #ifdef XMMS2
220                    "  * XMMS2\n"
221 #endif /* XMMS2 */
222                    "\n General:\n"
223 #ifdef HAVE_OPENMP
224                    "  * OpenMP\n"
225 #endif /* HAVE_OPENMP */
226 #ifdef MATH
227                    "  * math\n"
228 #endif /* Math */
229 #ifdef HDDTEMP
230                    "  * hddtemp\n"
231 #endif /* HDDTEMP */
232 #ifdef TCP_PORT_MONITOR
233                    "  * portmon\n"
234 #endif /* TCP_PORT_MONITOR */
235 #ifdef HAVE_CURL
236                    "  * Curl\n"
237 #endif /* HAVE_CURL */
238 #ifdef RSS
239                    "  * RSS\n"
240 #endif /* RSS */
241 #ifdef WEATHER
242                    "  * Weather (METAR)\n"
243 #ifdef XOAP
244                    "  * Weather (XOAP)\n"
245 #endif /* XOAP */
246 #endif /* WEATHER */
247 #ifdef HAVE_IWLIB
248                    "  * wireless\n"
249 #endif /* HAVE_IWLIB */
250 #ifdef IBM
251                    "  * support for IBM/Lenovo notebooks\n"
252 #endif /* IBM */
253 #ifdef NVIDIA
254                    "  * nvidia\n"
255 #endif /* NVIDIA */
256 #ifdef EVE
257                    "  * eve-online\n"
258 #endif /* EVE */
259 #ifdef CONFIG_OUTPUT
260                    "  * config-output\n"
261 #endif /* CONFIG_OUTPUT */
262 #ifdef IMLIB2
263                    "  * Imlib2\n"
264 #endif /* IMLIB2 */
265 #ifdef MIXER_IS_ALSA
266                    "  * ALSA mixer support\n"
267 #endif /* MIXER_IS_ALSA */
268 #ifdef APCUPSD
269                    "  * apcupsd\n"
270 #endif /* APCUPSD */
271 #ifdef IOSTATS
272                    "  * iostats\n"
273 #endif /* IOSTATS */
274 #ifdef NCURSES
275                    "  * ncurses\n"
276 #endif /* NCURSES */
277 #ifdef HAVE_LUA
278                    "  * Lua\n"
279                    "\n  Lua bindings:\n"
280 #ifdef HAVE_LUA_CAIRO
281                    "   * Cairo\n"
282 #endif /* HAVE_LUA_CAIRO */
283 #ifdef HAVE_LUA_IMLIB2
284                    "   * Imlib2\n"
285 #endif /* IMLIB2 */
286 #endif /* HAVE_LUA */
287         );
288
289         exit(EXIT_SUCCESS);
290 }
291
292 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
293
294
295 #ifdef X11
296
297 static void X11_create_window(void);
298 static void X11_initialisation(void);
299
300 struct _x11_stuff_s {
301         Region region;
302 #ifdef HAVE_XDAMAGE
303         Damage damage;
304         XserverRegion region2, part;
305         int event_base, error_base;
306 #endif
307 } x11_stuff;
308
309 /* text size */
310
311 static int text_start_x, text_start_y;  /* text start position in window */
312 static int text_width, text_height;
313
314 /* alignments */
315 enum alignment {
316         TOP_LEFT = 1,
317         TOP_RIGHT,
318         TOP_MIDDLE,
319         BOTTOM_LEFT,
320         BOTTOM_RIGHT,
321         BOTTOM_MIDDLE,
322         MIDDLE_LEFT,
323         MIDDLE_MIDDLE,
324         MIDDLE_RIGHT,
325         NONE
326 };
327
328 /* display to connect to */
329 static char *disp = NULL;
330
331 #endif /* X11 */
332
333 /* struct that has all info to be shared between
334  * instances of the same text object */
335 struct information info;
336
337 /* path to config file */
338 char *current_config;
339
340 /* set to 1 if you want all text to be in uppercase */
341 static unsigned int stuff_in_uppercase;
342
343 /* Run how many times? */
344 static unsigned long total_run_times;
345
346 /* fork? */
347 static int fork_to_background;
348
349 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
350
351 /* filenames for output */
352 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
353 char *append_file = NULL; FILE *append_fpointer = NULL;
354
355 #ifdef X11
356
357 static int show_graph_scale;
358 static int show_graph_range;
359
360 /* Position on the screen */
361 static int text_alignment;
362 static int gap_x, gap_y;
363
364 /* border */
365 static int draw_borders;
366 static int draw_graph_borders;
367 static int stippled_borders;
368
369 int get_stippled_borders(void)
370 {
371         return stippled_borders;
372 }
373
374 static int draw_shades, draw_outline;
375
376 long default_fg_color, default_bg_color, default_out_color;
377
378 /* create own window or draw stuff to root? */
379 static int set_transparent = 0;
380
381 #ifdef OWN_WINDOW
382 static int own_window = 0;
383 static int background_colour = 0;
384
385 /* fixed size/pos is set if wm/user changes them */
386 static int fixed_size = 0, fixed_pos = 0;
387 #endif
388
389 static int minimum_width, minimum_height;
390 static int maximum_width;
391
392 #endif /* X11 */
393
394 #ifdef __OpenBSD__
395 static int sensor_device;
396 #endif
397
398 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
399          color9;
400
401 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
402 unsigned int max_user_text;
403
404 /* maximum size of individual text buffers, ie $exec buffer size */
405 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
406
407 /* UTF-8 */
408 int utf8_mode = 0;
409
410 /* no buffers in used memory? */
411 int no_buffers;
412
413 /* pad percentages to decimals? */
414 static int pad_percents = 0;
415
416 static char *global_text = 0;
417
418 char *get_global_text(void)
419 {
420         return global_text;
421 }
422
423 long global_text_lines;
424
425 static int total_updates;
426 static int updatereset;
427
428 void set_updatereset(int i)
429 {
430         updatereset = i;
431 }
432
433 int get_updatereset(void)
434 {
435         return updatereset;
436 }
437
438 int check_contains(char *f, char *s)
439 {
440         int ret = 0;
441         FILE *where = open_file(f, 0);
442
443         if (where) {
444                 char buf1[256];
445
446                 while (fgets(buf1, 256, where)) {
447                         if (strstr(buf1, s)) {
448                                 ret = 1;
449                                 break;
450                         }
451                 }
452                 fclose(where);
453         } else {
454                 NORM_ERR("Could not open the file");
455         }
456         return ret;
457 }
458
459 #define SECRIT_MULTILINE_CHAR '\x02'
460
461 static inline int calc_text_width(const char *s)
462 {
463         size_t slen = strlen(s);
464
465 #ifdef X11
466         if ((output_methods & TO_X) == 0) {
467 #endif /* X11 */
468                 return slen;
469 #ifdef X11
470         }
471 #ifdef XFT
472         if (use_xft) {
473                 XGlyphInfo gi;
474
475                 if (utf8_mode) {
476                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
477                                 (const FcChar8 *) s, slen, &gi);
478                 } else {
479                         XftTextExtents8(display, fonts[selected_font].xftfont,
480                                 (const FcChar8 *) s, slen, &gi);
481                 }
482                 return gi.xOff;
483         } else
484 #endif
485         {
486                 return XTextWidth(fonts[selected_font].font, s, slen);
487         }
488 #endif /* X11 */
489 }
490
491 /* formatted text to render on screen, generated in generate_text(),
492  * drawn in draw_stuff() */
493
494 static char *text_buffer;
495
496 /* quite boring functions */
497
498 static inline void for_each_line(char *b, int f(char *, int))
499 {
500         char *ps, *pe;
501         int special_index = 0; /* specials index */
502
503         for (ps = b, pe = b; *pe; pe++) {
504                 if (*pe == '\n') {
505                         *pe = '\0';
506                         special_index = f(ps, special_index);
507                         *pe = '\n';
508                         ps = pe + 1;
509                 }
510         }
511
512         if (ps < pe) {
513                 f(ps, special_index);
514         }
515 }
516
517 static void convert_escapes(char *buf)
518 {
519         char *p = buf, *s = buf;
520
521         while (*s) {
522                 if (*s == '\\') {
523                         s++;
524                         if (*s == 'n') {
525                                 *p++ = '\n';
526                         } else if (*s == '\\') {
527                                 *p++ = '\\';
528                         }
529                         s++;
530                 } else {
531                         *p++ = *s++;
532                 }
533         }
534         *p = '\0';
535 }
536
537 /* Prints anything normally printed with snprintf according to the current value
538  * of use_spacer.  Actually slightly more flexible than snprintf, as you can
539  * safely specify the destination buffer as one of your inputs.  */
540 int spaced_print(char *buf, int size, const char *format, int width, ...)
541 {
542         int len = 0;
543         va_list argp;
544         char *tempbuf;
545
546         if (size < 1) {
547                 return 0;
548         }
549         tempbuf = malloc(size * sizeof(char));
550
551         // Passes the varargs along to vsnprintf
552         va_start(argp, width);
553         vsnprintf(tempbuf, size, format, argp);
554         va_end(argp);
555
556         switch (use_spacer) {
557                 case NO_SPACER:
558                         len = snprintf(buf, size, "%s", tempbuf);
559                         break;
560                 case LEFT_SPACER:
561                         len = snprintf(buf, size, "%*s", width, tempbuf);
562                         break;
563                 case RIGHT_SPACER:
564                         len = snprintf(buf, size, "%-*s", width, tempbuf);
565                         break;
566         }
567         free(tempbuf);
568         return len;
569 }
570
571 /* print percentage values
572  *
573  * - i.e., unsigned values between 0 and 100
574  * - respect the value of pad_percents */
575 int percent_print(char *buf, int size, unsigned value)
576 {
577         return spaced_print(buf, size, "%u", pad_percents, value);
578 }
579
580 /* converts from bytes to human readable format (K, M, G, T)
581  *
582  * The algorithm always divides by 1024, as unit-conversion of byte
583  * counts suggests. But for output length determination we need to
584  * compare with 1000 here, as we print in decimal form. */
585 void human_readable(long long num, char *buf, int size)
586 {
587         const char **suffix = suffixes;
588         float fnum;
589         int precision;
590         int width;
591         const char *format;
592
593         /* Possibly just output as usual, for example for stdout usage */
594         if (!format_human_readable) {
595                 spaced_print(buf, size, "%d", 6, round_to_int(num));
596                 return;
597         }
598         if (short_units) {
599                 width = 5;
600                 format = "%.*f%.1s";
601         } else {
602                 width = 7;
603                 format = "%.*f%-3s";
604         }
605
606         if (llabs(num) < 1000LL) {
607                 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
608                 return;
609         }
610
611         while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
612                 num /= 1024;
613                 suffix++;
614         }
615
616         suffix++;
617         fnum = num / 1024.0;
618
619         /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
620          *
621          * The goal is to always have a significance of 3, by
622          * adjusting the decimal part of the number. Sample output:
623          *  123MiB
624          * 23.4GiB
625          * 5.12B   
626          * so the point of alignment resides between number and unit. The
627          * upside of this is that there is minimal padding necessary, though
628          * there should be a way to make alignment take place at the decimal
629          * dot (then with fixed width decimal part). 
630          *
631          * Note the repdigits below: when given a precision value, printf()
632          * rounds the float to it, not just cuts off the remaining digits. So
633          * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
634          * printed with a precision of 0. Yay. */
635
636         precision = 0;          /* print 100-999 without decimal part */
637         if (fnum < 99.95)
638                 precision = 1;  /* print 10-99 with one decimal place */
639         if (fnum < 9.995)
640                 precision = 2;  /* print 0-9 with two decimal places */
641
642         spaced_print(buf, size, format, width, precision, fnum, *suffix);
643 }
644
645 /* global object list root element */
646 static struct text_object global_root_object;
647
648 static long current_text_color;
649
650 void set_current_text_color(long colour)
651 {
652         current_text_color = colour;
653 }
654
655 long get_current_text_color(void)
656 {
657         return current_text_color;
658 }
659
660 //adds newstring to to the tree unless you can already see it when travelling back.
661 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
662 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
663         struct conftree* node;
664         struct conftree* node2;
665
666         for(node = previous; node != NULL; node = node->back) {
667                 if(strcmp(node->string, newstring) == 0) {
668                         return NULL;
669                 }
670         }
671         node = malloc(sizeof(struct conftree));
672         if (previous != NULL) {
673                 if(previous->vert_next == NULL) {
674                         previous->vert_next = node;
675                 } else {
676                         for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
677                         node2->horz_next = node;
678                 }
679         }
680         node->string = strdup(newstring);
681         node->horz_next = NULL;
682         node->vert_next = NULL;
683         node->back = previous;
684         return node;
685 }
686
687 void conftree_empty(struct conftree* tree) {
688         if(tree) {
689                 conftree_empty(tree->horz_next);
690                 conftree_empty(tree->vert_next);
691                 free(tree->string);
692                 free(tree);
693         }
694 }
695
696 struct conftree *currentconffile;
697
698 static void extract_variable_text(const char *p)
699 {
700         free_text_objects(&global_root_object, 0);
701         if (tmpstring1) {
702                 free(tmpstring1);
703                 tmpstring1 = 0;
704         }
705         if (tmpstring2) {
706                 free(tmpstring2);
707                 tmpstring2 = 0;
708         }
709         if (text_buffer) {
710                 free(text_buffer);
711                 text_buffer = 0;
712         }
713
714         extract_variable_text_internal(&global_root_object, p);
715 }
716
717 void parse_conky_vars(struct text_object *root, const char *txt,
718                 char *p, int p_max_size, struct information *cur)
719 {
720         extract_variable_text_internal(root, txt);
721         generate_text_internal(p, p_max_size, *root, cur);
722 }
723
724 static inline void format_media_player_time(char *buf, const int size,
725                 int seconds)
726 {
727         int days, hours, minutes;
728
729         days = seconds / (24 * 60 * 60);
730         seconds %= (24 * 60 * 60);
731         hours = seconds / (60 * 60);
732         seconds %= (60 * 60);
733         minutes = seconds / 60;
734         seconds %= 60;
735
736         if (days > 0) {
737                 snprintf(buf, size, "%i days %i:%02i:%02i", days,
738                                 hours, minutes, seconds);
739         } else if (hours > 0) {
740                 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
741                                 seconds);
742         } else {
743                 snprintf(buf, size, "%i:%02i", minutes, seconds);
744         }
745 }
746
747 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
748  * multiline objects like $exec work with $align[rc] and friends
749  */
750 void substitute_newlines(char *p, long l)
751 {
752         char *s = p;
753         if (l < 0) return;
754         while (p && *p && p < s + l) {
755                 if (*p == '\n') {
756                         /* only substitute if it's not the last newline */
757                         *p = SECRIT_MULTILINE_CHAR;
758                 }
759                 p++;
760         }
761 }
762
763 void generate_text_internal(char *p, int p_max_size,
764                 struct text_object root, struct information *cur)
765 {
766         struct text_object *obj;
767 #ifdef X11
768         int need_to_load_fonts = 0;
769 #endif /* X11 */
770
771         /* for the OBJ_top* handler */
772         struct process **needed = 0;
773
774 #ifdef HAVE_ICONV
775         char buff_in[p_max_size];
776         buff_in[0] = 0;
777 #endif /* HAVE_ICONV */
778
779         p[0] = 0;
780         obj = root.next;
781         while (obj && p_max_size > 0) {
782                 needed = 0; /* reset for top stuff */
783
784 /* IFBLOCK jumping algorithm
785  *
786  * This is easier as it looks like:
787  * - each IF checks it's condition
788  *   - on FALSE: call DO_JUMP
789  *   - on TRUE: don't care
790  * - each ELSE calls DO_JUMP unconditionally
791  * - each ENDIF is silently being ignored
792  *
793  * Why this works:
794  * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
795  * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
796  * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
797  * regularly, "obj" is being updated to point to obj->next, so object parsing
798  * continues right after the corresponding ELSE or ENDIF. This means that if we
799  * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
800  * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
801  * jumped, and there is nothing to do.
802  */
803 #define DO_JUMP { \
804         DBGP2("jumping"); \
805         if (obj->ifblock_next) \
806                 obj = obj->ifblock_next; \
807 }
808
809 #define OBJ(a) break; case OBJ_##a:
810
811                 switch (obj->type) {
812                         default:
813                                 NORM_ERR("not implemented obj type %d", obj->type);
814                         OBJ(read_tcp) {
815                                 print_read_tcp(obj, p, p_max_size);
816                         }
817 #ifndef __OpenBSD__
818                         OBJ(acpitemp) {
819                                 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
820                         }
821 #endif /* !__OpenBSD__ */
822                         OBJ(freq) {
823                                 static int ok = 1;
824                                 if (ok) {
825                                         ok = get_freq(p, p_max_size, "%.0f", 1,
826                                                         obj->data.i);
827                                 }
828                         }
829                         OBJ(freq_g) {
830                                 static int ok = 1;
831                                 if (ok) {
832 #ifndef __OpenBSD__
833                                         ok = get_freq(p, p_max_size, "%'.2f", 1000,
834                                                         obj->data.i);
835 #else
836                                         /* OpenBSD has no such flag (SUSv2) */
837                                         ok = get_freq(p, p_max_size, "%.2f", 1000,
838                                                         obj->data.i);
839 #endif /* __OpenBSD */
840                                 }
841                         }
842 #if defined(__linux__)
843                         OBJ(voltage_mv) {
844                                 static int ok = 1;
845                                 if (ok) {
846                                         ok = get_voltage(p, p_max_size, "%.0f", 1,
847                                                         obj->data.i);
848                                 }
849                         }
850                         OBJ(voltage_v) {
851                                 static int ok = 1;
852                                 if (ok) {
853                                         ok = get_voltage(p, p_max_size, "%'.3f", 1000,
854                                                         obj->data.i);
855                                 }
856                         }
857
858 #ifdef HAVE_IWLIB
859                         OBJ(wireless_essid) {
860                                 print_wireless_essid(obj, p, p_max_size);
861                         }
862                         OBJ(wireless_mode) {
863                                 print_wireless_mode(obj, p, p_max_size);
864                         }
865                         OBJ(wireless_bitrate) {
866                                 print_wireless_bitrate(obj, p, p_max_size);
867                         }
868                         OBJ(wireless_ap) {
869                                 print_wireless_ap(obj, p, p_max_size);
870                         }
871                         OBJ(wireless_link_qual) {
872                                 print_wireless_link_qual(obj, p, p_max_size);
873                         }
874                         OBJ(wireless_link_qual_max) {
875                                 print_wireless_link_qual_max(obj, p, p_max_size);
876                         }
877                         OBJ(wireless_link_qual_perc) {
878                                 print_wireless_link_qual_perc(obj, p, p_max_size);
879                         }
880                         OBJ(wireless_link_bar) {
881                                 print_wireless_link_bar(obj, p, p_max_size);
882                         }
883 #endif /* HAVE_IWLIB */
884
885 #endif /* __linux__ */
886
887 #ifndef __OpenBSD__
888                         OBJ(acpifan) {
889                                 get_acpi_fan(p, p_max_size);
890                         }
891                         OBJ(acpiacadapter) {
892                                 get_acpi_ac_adapter(p, p_max_size);
893                         }
894                         OBJ(battery) {
895                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
896                         }
897                         OBJ(battery_time) {
898                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
899                         }
900                         OBJ(battery_percent) {
901                                 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
902                         }
903                         OBJ(battery_bar) {
904 #ifdef X11
905                                 if(output_methods & TO_X) {
906                                         new_bar(obj, p, get_battery_perct_bar(obj->data.s));
907                                 }else
908 #endif /* X11 */
909                                         new_bar_in_shell(obj, p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55);
910                         }
911                         OBJ(battery_short) {
912                                 get_battery_short_status(p, p_max_size, obj->data.s);
913                         }
914 #endif /* __OpenBSD__ */
915
916                         OBJ(buffers) {
917                                 human_readable(cur->buffers * 1024, p, 255);
918                         }
919                         OBJ(cached) {
920                                 human_readable(cur->cached * 1024, p, 255);
921                         }
922                         OBJ(cmdline_to_pid) {
923                                 print_cmdline_to_pid(obj, p, p_max_size);
924                         }
925                         OBJ(cpu) {
926                                 if (obj->data.i > info.cpu_count) {
927                                         NORM_ERR("obj->data.i %i info.cpu_count %i",
928                                                         obj->data.i, info.cpu_count);
929                                         CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
930                                 }
931                                 percent_print(p, p_max_size,
932                                               round_to_int(cur->cpu_usage[obj->data.i] * 100.0));
933                         }
934 #ifdef X11
935                         OBJ(cpugauge)
936                                 new_gauge(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
937 #endif /* X11 */
938                         OBJ(cpubar) {
939 #ifdef X11
940                                 if(output_methods & TO_X) {
941                                         new_bar(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 255.0));
942                                 }else
943 #endif /* X11 */
944                                         new_bar_in_shell(obj, p, p_max_size, round_to_int(cur->cpu_usage[obj->data.i] * 100));
945                         }
946 #ifdef X11
947                         OBJ(cpugraph) {
948                                 new_graph(obj, p, round_to_int(cur->cpu_usage[obj->data.i] * 100));
949                         }
950                         OBJ(loadgraph) {
951                                 print_loadgraph(obj, p);
952                         }
953 #endif /* X11 */
954                         OBJ(color) {
955                                 new_fg(p, obj->data.l);
956                         }
957 #ifdef X11
958                         OBJ(color0) {
959                                 new_fg(p, color0);
960                         }
961                         OBJ(color1) {
962                                 new_fg(p, color1);
963                         }
964                         OBJ(color2) {
965                                 new_fg(p, color2);
966                         }
967                         OBJ(color3) {
968                                 new_fg(p, color3);
969                         }
970                         OBJ(color4) {
971                                 new_fg(p, color4);
972                         }
973                         OBJ(color5) {
974                                 new_fg(p, color5);
975                         }
976                         OBJ(color6) {
977                                 new_fg(p, color6);
978                         }
979                         OBJ(color7) {
980                                 new_fg(p, color7);
981                         }
982                         OBJ(color8) {
983                                 new_fg(p, color8);
984                         }
985                         OBJ(color9) {
986                                 new_fg(p, color9);
987                         }
988 #endif /* X11 */
989                         OBJ(conky_version) {
990                                 snprintf(p, p_max_size, "%s", VERSION);
991                         }
992                         OBJ(conky_build_date) {
993                                 snprintf(p, p_max_size, "%s", BUILD_DATE);
994                         }
995                         OBJ(conky_build_arch) {
996                                 snprintf(p, p_max_size, "%s", BUILD_ARCH);
997                         }
998 #if defined(__linux__)
999                         OBJ(disk_protect) {
1000                                 snprintf(p, p_max_size, "%s",
1001                                                 get_disk_protect_queue(obj->data.s));
1002                         }
1003                         OBJ(i8k_version) {
1004                                 snprintf(p, p_max_size, "%s", i8k.version);
1005                         }
1006                         OBJ(i8k_bios) {
1007                                 snprintf(p, p_max_size, "%s", i8k.bios);
1008                         }
1009                         OBJ(i8k_serial) {
1010                                 snprintf(p, p_max_size, "%s", i8k.serial);
1011                         }
1012                         OBJ(i8k_cpu_temp) {
1013                                 int cpu_temp;
1014
1015                                 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1016                                 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1017                         }
1018                         OBJ(i8k_left_fan_status) {
1019                                 int left_fan_status;
1020
1021                                 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1022                                 if (left_fan_status == 0) {
1023                                         snprintf(p, p_max_size, "off");
1024                                 }
1025                                 if (left_fan_status == 1) {
1026                                         snprintf(p, p_max_size, "low");
1027                                 }
1028                                 if (left_fan_status == 2) {
1029                                         snprintf(p, p_max_size, "high");
1030                                 }
1031                         }
1032                         OBJ(i8k_right_fan_status) {
1033                                 int right_fan_status;
1034
1035                                 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1036                                 if (right_fan_status == 0) {
1037                                         snprintf(p, p_max_size, "off");
1038                                 }
1039                                 if (right_fan_status == 1) {
1040                                         snprintf(p, p_max_size, "low");
1041                                 }
1042                                 if (right_fan_status == 2) {
1043                                         snprintf(p, p_max_size, "high");
1044                                 }
1045                         }
1046                         OBJ(i8k_left_fan_rpm) {
1047                                 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1048                         }
1049                         OBJ(i8k_right_fan_rpm) {
1050                                 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1051                         }
1052                         OBJ(i8k_ac_status) {
1053                                 int ac_status;
1054
1055                                 sscanf(i8k.ac_status, "%d", &ac_status);
1056                                 if (ac_status == -1) {
1057                                         snprintf(p, p_max_size, "disabled (read i8k docs)");
1058                                 }
1059                                 if (ac_status == 0) {
1060                                         snprintf(p, p_max_size, "off");
1061                                 }
1062                                 if (ac_status == 1) {
1063                                         snprintf(p, p_max_size, "on");
1064                                 }
1065                         }
1066                         OBJ(i8k_buttons_status) {
1067                                 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1068                         }
1069 #if defined(IBM)
1070                         OBJ(ibm_fan) {
1071                                 get_ibm_acpi_fan(p, p_max_size);
1072                         }
1073                         OBJ(ibm_temps) {
1074                                 print_ibm_temps(obj, p, p_max_size);
1075                         }
1076                         OBJ(ibm_volume) {
1077                                 get_ibm_acpi_volume(p, p_max_size);
1078                         }
1079                         OBJ(ibm_brightness) {
1080                                 get_ibm_acpi_brightness(p, p_max_size);
1081                         }
1082 #endif /* IBM */
1083                         /* information from sony_laptop kernel module
1084                          * /sys/devices/platform/sony-laptop */
1085                         OBJ(sony_fanspeed) {
1086                                 get_sony_fanspeed(p, p_max_size);
1087                         }
1088                         OBJ(if_gw) {
1089                                 if (!gateway_exists()) {
1090                                         DO_JUMP;
1091                                 }
1092                         }
1093                         OBJ(gw_iface) {
1094                                 print_gateway_iface(p, p_max_size);
1095                         }
1096                         OBJ(gw_ip) {
1097                                 print_gateway_ip(p, p_max_size);
1098                         }
1099                         OBJ(laptop_mode) {
1100                                 snprintf(p, p_max_size, "%d", get_laptop_mode());
1101                         }
1102                         OBJ(pb_battery) {
1103                                 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1104                         }
1105 #endif /* __linux__ */
1106 #if (defined(__FreeBSD__) || defined(__linux__))
1107                         OBJ(if_up) {
1108                                 if (!interface_up(obj)) {
1109                                         DO_JUMP;
1110                                 }
1111                         }
1112 #endif
1113 #ifdef __OpenBSD__
1114                         OBJ(obsd_sensors_temp) {
1115                                 print_obsd_sensors_temp(obj, p, p_max_size);
1116                         }
1117                         OBJ(obsd_sensors_fan) {
1118                                 print_obsd_sensors_fan(obj, p, p_max_size);
1119                         }
1120                         OBJ(obsd_sensors_volt) {
1121                                 print_obsd_sensors_volt(obj, p, p_max_size);
1122                         }
1123                         OBJ(obsd_vendor) {
1124                                 get_obsd_vendor(p, p_max_size);
1125                         }
1126                         OBJ(obsd_product) {
1127                                 get_obsd_product(p, p_max_size);
1128                         }
1129 #endif /* __OpenBSD__ */
1130 #ifdef X11
1131                         OBJ(font) {
1132                                 new_font(p, obj->data.s);
1133                                 need_to_load_fonts = 1;
1134                         }
1135 #endif /* X11 */
1136                         OBJ(diskio) {
1137                                 print_diskio(obj, 0, p, p_max_size);
1138                         }
1139                         OBJ(diskio_write) {
1140                                 print_diskio(obj, 1, p, p_max_size);
1141                         }
1142                         OBJ(diskio_read) {
1143                                 print_diskio(obj, -1, p, p_max_size);
1144                         }
1145 #ifdef X11
1146                         OBJ(diskiograph) {
1147                                 print_diskiograph(obj, 0, p);
1148                         }
1149                         OBJ(diskiograph_read) {
1150                                 print_diskiograph(obj, -1, p);
1151                         }
1152                         OBJ(diskiograph_write) {
1153                                 print_diskiograph(obj, 1, p);
1154                         }
1155 #endif /* X11 */
1156                         OBJ(downspeed) {
1157                                 print_downspeed(obj, p, p_max_size);
1158                         }
1159                         OBJ(downspeedf) {
1160                                 print_downspeedf(obj, p, p_max_size);
1161                         }
1162 #ifdef X11
1163                         OBJ(downspeedgraph) {
1164                                 print_downspeedgraph(obj, p);
1165                         }
1166 #endif /* X11 */
1167                         OBJ(else) {
1168                                 /* Since we see you, you're if has not jumped.
1169                                  * Do Ninja jump here: without leaving traces.
1170                                  * This is to prevent us from stale jumped flags.
1171                                  */
1172                                 obj = obj->ifblock_next;
1173                                 continue;
1174                         }
1175                         OBJ(endif) {
1176                                 /* harmless object, just ignore */
1177                         }
1178                         OBJ(addr) {
1179                                 print_addr(obj, p, p_max_size);
1180                         }
1181 #if defined(__linux__)
1182                         OBJ(addrs) {
1183                                 print_addrs(obj, p, p_max_size);
1184                         }
1185 #endif /* __linux__ */
1186 #if defined(IMLIB2) && defined(X11)
1187                         OBJ(image) {
1188                                 /* doesn't actually draw anything, just queues it omp.  the
1189                                  * image will get drawn after the X event loop */
1190                                 cimlib_add_image(obj->data.s);
1191                         }
1192 #endif /* IMLIB2 */
1193                         OBJ(eval) {
1194                                 evaluate(obj->data.s, p, p_max_size);
1195                         }
1196                         OBJ(exec) {
1197                                 print_exec(obj, p, p_max_size);
1198                         }
1199                         OBJ(execp) {
1200                                 print_execp(obj, p, p_max_size);
1201                         }
1202 #ifdef X11
1203                         OBJ(execgauge) {
1204                                 print_execgauge(obj, p, p_max_size);
1205                         }
1206 #endif /* X11 */
1207                         OBJ(execbar) {
1208                                 print_execbar(obj, p, p_max_size);
1209                         }
1210 #ifdef X11
1211                         OBJ(execgraph) {
1212                                 print_execgraph(obj, p, p_max_size);
1213                         }
1214 #endif /* X11 */
1215                         OBJ(execibar) {
1216                                 print_execibar(obj, p, p_max_size);
1217                         }
1218 #ifdef X11
1219                         OBJ(execigraph) {
1220                                 print_execigraph(obj, p, p_max_size);
1221                         }
1222                         OBJ(execigauge) {
1223                                 print_execigauge(obj, p, p_max_size);
1224                         }
1225 #endif /* X11 */
1226                         OBJ(execi) {
1227                                 print_execi(obj, p, p_max_size);
1228                         }
1229                         OBJ(execpi) {
1230                                 print_execpi(obj, p, p_max_size);
1231                         }
1232                         OBJ(texeci) {
1233                                 print_texeci(obj, p, p_max_size);
1234                         }
1235                         OBJ(imap_unseen) {
1236                                 print_imap_unseen(obj, p, p_max_size);
1237                         }
1238                         OBJ(imap_messages) {
1239                                 print_imap_messages(obj, p, p_max_size);
1240                         }
1241                         OBJ(pop3_unseen) {
1242                                 print_pop3_unseen(obj, p, p_max_size);
1243                         }
1244                         OBJ(pop3_used) {
1245                                 print_pop3_used(obj, p, p_max_size);
1246                         }
1247                         OBJ(fs_bar) {
1248                                 print_fs_bar(obj, 0, p, p_max_size);
1249                         }
1250                         OBJ(fs_free) {
1251                                 print_fs_free(obj, p, p_max_size);
1252                         }
1253                         OBJ(fs_free_perc) {
1254                                 print_fs_perc(obj, 1, p, p_max_size);
1255                         }
1256                         OBJ(fs_size) {
1257                                 print_fs_size(obj, p, p_max_size);
1258                         }
1259                         OBJ(fs_type) {
1260                                 print_fs_type(obj, p, p_max_size);
1261                         }
1262                         OBJ(fs_used) {
1263                                 print_fs_used(obj, p, p_max_size);
1264                         }
1265                         OBJ(fs_bar_free) {
1266                                 print_fs_bar(obj, 1, p, p_max_size);
1267                         }
1268                         OBJ(fs_used_perc) {
1269                                 print_fs_perc(obj, 0, p, p_max_size);
1270                         }
1271                         OBJ(loadavg) {
1272                                 print_loadavg(obj, p, p_max_size);
1273                         }
1274                         OBJ(goto) {
1275                                 new_goto(p, obj->data.i);
1276                         }
1277                         OBJ(tab) {
1278                                 new_tab(obj, p);
1279                         }
1280 #ifdef X11
1281                         OBJ(hr) {
1282                                 new_hr(p, obj->data.i);
1283                         }
1284 #endif
1285                         OBJ(nameserver) {
1286                                 print_nameserver(obj, p, p_max_size);
1287                         }
1288 #ifdef EVE
1289                         OBJ(eve) {
1290                                 print_eve(obj, p, p_max_size);
1291                         }
1292 #endif
1293 #ifdef HAVE_CURL
1294                         OBJ(curl) {
1295                                 curl_print(obj, p, p_max_size);
1296                         }
1297 #endif
1298 #ifdef RSS
1299                         OBJ(rss) {
1300                                 rss_print_info(obj, p, p_max_size);
1301                         }
1302 #endif
1303 #ifdef WEATHER
1304                         OBJ(weather) {
1305                                 print_weather(obj, p, p_max_size);
1306                         }
1307 #endif
1308 #ifdef XOAP
1309                         OBJ(weather_forecast) {
1310                                 print_weather_forecast(obj, p, p_max_size);
1311                         }
1312 #endif
1313 #ifdef HAVE_LUA
1314                         OBJ(lua) {
1315                                 char *str = llua_getstring(obj->data.s);
1316                                 if (str) {
1317                                         snprintf(p, p_max_size, "%s", str);
1318                                         free(str);
1319                                 }
1320                         }
1321                         OBJ(lua_parse) {
1322                                 char *str = llua_getstring(obj->data.s);
1323                                 if (str) {
1324                                         evaluate(str, p, p_max_size);
1325                                         free(str);
1326                                 }
1327                         }
1328                         OBJ(lua_bar) {
1329                                 double per;
1330                                 if (llua_getnumber(obj->data.s, &per)) {
1331 #ifdef X11
1332                                         if(output_methods & TO_X) {
1333                                                 new_bar(obj, p, (per/100.0 * 255));
1334                                         } else
1335 #endif /* X11 */
1336                                                 new_bar_in_shell(obj, p, p_max_size, per);
1337                                 }
1338                         }
1339 #ifdef X11
1340                         OBJ(lua_graph) {
1341                                 double per;
1342                                 if (llua_getnumber(obj->data.s, &per)) {
1343                                         new_graph(obj, p, per);
1344                                 }
1345                         }
1346                         OBJ(lua_gauge) {
1347                                 double per;
1348                                 if (llua_getnumber(obj->data.s, &per)) {
1349                                         new_gauge(obj, p, (per/100.0 * 255));
1350                                 }
1351                         }
1352 #endif /* X11 */
1353 #endif /* HAVE_LUA */
1354 #ifdef HDDTEMP
1355                         OBJ(hddtemp) {
1356                                 short val;
1357                                 char unit;
1358
1359                                 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1360                                         snprintf(p, p_max_size, "N/A");
1361                                 } else {
1362                                         temp_print(p, p_max_size, (double)val,
1363                                                         (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1364                                 }
1365                         }
1366 #endif
1367                         OBJ(offset) {
1368                                 new_offset(p, obj->data.i);
1369                         }
1370                         OBJ(voffset) {
1371                                 new_voffset(p, obj->data.i);
1372                         }
1373 #ifdef __linux__
1374                         OBJ(i2c) {
1375                                 print_sysfs_sensor(obj, p, p_max_size);
1376                         }
1377                         OBJ(platform) {
1378                                 print_sysfs_sensor(obj, p, p_max_size);
1379                         }
1380                         OBJ(hwmon) {
1381                                 print_sysfs_sensor(obj, p, p_max_size);
1382                         }
1383 #endif /* __linux__ */
1384                         OBJ(alignr) {
1385                                 new_alignr(p, obj->data.i);
1386                         }
1387                         OBJ(alignc) {
1388                                 new_alignc(p, obj->data.i);
1389                         }
1390                         OBJ(if_empty) {
1391                                 char buf[max_user_text];
1392                                 struct information *tmp_info =
1393                                         malloc(sizeof(struct information));
1394                                 memcpy(tmp_info, cur, sizeof(struct information));
1395                                 generate_text_internal(buf, max_user_text,
1396                                                        *obj->sub, tmp_info);
1397
1398                                 if (strlen(buf) != 0) {
1399                                         DO_JUMP;
1400                                 }
1401                                 free(tmp_info);
1402                         }
1403                         OBJ(if_match) {
1404                                 char expression[max_user_text];
1405                                 int val;
1406                                 struct information *tmp_info;
1407
1408                                 tmp_info = malloc(sizeof(struct information));
1409                                 memcpy(tmp_info, cur, sizeof(struct information));
1410                                 generate_text_internal(expression, max_user_text,
1411                                                        *obj->sub, tmp_info);
1412                                 DBGP("parsed arg into '%s'", expression);
1413
1414                                 val = compare(expression);
1415                                 if (val == -2) {
1416                                         NORM_ERR("compare failed for expression '%s'",
1417                                                         expression);
1418                                 } else if (!val) {
1419                                         DO_JUMP;
1420                                 }
1421                                 free(tmp_info);
1422                         }
1423                         OBJ(if_existing) {
1424                                 char *spc;
1425
1426                                 spc = strchr(obj->data.s, ' ');
1427                                 if (!spc && access(obj->data.s, F_OK)) {
1428                                         DO_JUMP;
1429                                 } else if (spc) {
1430                                         *spc = '\0';
1431                                         if (check_contains(obj->data.s, spc + 1))
1432                                                 DO_JUMP;
1433                                         *spc = ' ';
1434                                 }
1435                         }
1436                         OBJ(if_mounted) {
1437                                 if ((obj->data.s)
1438                                                 && (!check_mount(obj->data.s))) {
1439                                         DO_JUMP;
1440                                 }
1441                         }
1442                         OBJ(if_running) {
1443 #ifdef __linux__
1444                                 if (!get_process_by_name(obj->data.s)) {
1445 #else
1446                                 if ((obj->data.s) && system(obj->data.s)) {
1447 #endif
1448                                         DO_JUMP;
1449                                 }
1450                         }
1451 #if defined(__linux__)
1452                         OBJ(ioscheduler) {
1453                                 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
1454                         }
1455 #endif
1456                         OBJ(kernel) {
1457                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
1458                         }
1459                         OBJ(machine) {
1460                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
1461                         }
1462
1463                         /* memory stuff */
1464                         OBJ(mem) {
1465                                 human_readable(cur->mem * 1024, p, 255);
1466                         }
1467                         OBJ(memeasyfree) {
1468                                 human_readable(cur->memeasyfree * 1024, p, 255);
1469                         }
1470                         OBJ(memfree) {
1471                                 human_readable(cur->memfree * 1024, p, 255);
1472                         }
1473                         OBJ(memmax) {
1474                                 human_readable(cur->memmax * 1024, p, 255);
1475                         }
1476                         OBJ(memperc) {
1477                                 if (cur->memmax)
1478                                         percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
1479                         }
1480 #ifdef X11
1481                         OBJ(memgauge){
1482                                 new_gauge(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1483                         }
1484 #endif /* X11 */
1485                         OBJ(membar) {
1486 #ifdef X11
1487                                 if(output_methods & TO_X) {
1488                                         new_bar(obj, p, cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
1489                                 }else
1490 #endif /* X11 */
1491                                         new_bar_in_shell(obj, p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0);
1492                         }
1493 #ifdef X11
1494                         OBJ(memgraph) {
1495                                 new_graph(obj, p, cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0);
1496                         }
1497 #endif /* X11 */
1498                         /* mixer stuff */
1499                         OBJ(mixer) {
1500                                 print_mixer(obj, 0, p, p_max_size);
1501                         }
1502                         OBJ(mixerl) {
1503                                 print_mixer(obj, -1, p, p_max_size);
1504                         }
1505                         OBJ(mixerr) {
1506                                 print_mixer(obj, 1, p, p_max_size);
1507                         }
1508 #ifdef X11
1509                         OBJ(mixerbar) {
1510                                 print_mixer_bar(obj, 0, p);
1511                         }
1512                         OBJ(mixerlbar) {
1513                                 print_mixer_bar(obj, -1, p);
1514                         }
1515                         OBJ(mixerrbar) {
1516                                 print_mixer_bar(obj, 1, p);
1517                         }
1518 #endif /* X11 */
1519                         OBJ(if_mixer_mute) {
1520                                 if (!check_mixer_muted(obj)) {
1521                                         DO_JUMP;
1522                                 }
1523                         }
1524 #ifdef X11
1525 #define NOT_IN_X "Not running in X"
1526                         OBJ(monitor) {
1527                                 if(x_initialised != YES) {
1528                                         strncpy(p, NOT_IN_X, p_max_size);
1529                                 }else{
1530                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
1531                                 }
1532                         }
1533                         OBJ(monitor_number) {
1534                                 if(x_initialised != YES) {
1535                                         strncpy(p, NOT_IN_X, p_max_size);
1536                                 }else{
1537                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
1538                                 }
1539                         }
1540                         OBJ(desktop) {
1541                                 if(x_initialised != YES) {
1542                                         strncpy(p, NOT_IN_X, p_max_size);
1543                                 }else{
1544                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
1545                                 }
1546                         }
1547                         OBJ(desktop_number) {
1548                                 if(x_initialised != YES) {
1549                                         strncpy(p, NOT_IN_X, p_max_size);
1550                                 }else{
1551                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
1552                                 }
1553                         }
1554                         OBJ(desktop_name) {
1555                                 if(x_initialised != YES) {
1556                                         strncpy(p, NOT_IN_X, p_max_size);
1557                                 }else if(cur->x11.desktop.name != NULL) {
1558                                         strncpy(p, cur->x11.desktop.name, p_max_size);
1559                                 }
1560                         }
1561 #endif /* X11 */
1562
1563                         /* mail stuff */
1564                         OBJ(mails) {
1565                                 print_mails(obj, p, p_max_size);
1566                         }
1567                         OBJ(new_mails) {
1568                                 print_new_mails(obj, p, p_max_size);
1569                         }
1570                         OBJ(seen_mails) {
1571                                 print_seen_mails(obj, p, p_max_size);
1572                         }
1573                         OBJ(unseen_mails) {
1574                                 print_unseen_mails(obj, p, p_max_size);
1575                         }
1576                         OBJ(flagged_mails) {
1577                                 print_flagged_mails(obj, p, p_max_size);
1578                         }
1579                         OBJ(unflagged_mails) {
1580                                 print_unflagged_mails(obj, p, p_max_size);
1581                         }
1582                         OBJ(forwarded_mails) {
1583                                 print_forwarded_mails(obj, p, p_max_size);
1584                         }
1585                         OBJ(unforwarded_mails) {
1586                                 print_unforwarded_mails(obj, p, p_max_size);
1587                         }
1588                         OBJ(replied_mails) {
1589                                 print_replied_mails(obj, p, p_max_size);
1590                         }
1591                         OBJ(unreplied_mails) {
1592                                 print_unreplied_mails(obj, p, p_max_size);
1593                         }
1594                         OBJ(draft_mails) {
1595                                 print_draft_mails(obj, p, p_max_size);
1596                         }
1597                         OBJ(trashed_mails) {
1598                                 print_trashed_mails(obj, p, p_max_size);
1599                         }
1600                         OBJ(mboxscan) {
1601                                 print_mboxscan(obj, p, p_max_size);
1602                         }
1603                         OBJ(nodename) {
1604                                 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
1605                         }
1606                         OBJ(outlinecolor) {
1607                                 new_outline(p, obj->data.l);
1608                         }
1609                         OBJ(pid_chroot) {
1610                                 char buf[max_user_text];
1611
1612                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1613                                 obj->data.s = buf;
1614                                 print_pid_chroot(obj, p, p_max_size);
1615                         }
1616                         OBJ(pid_cmdline) {
1617                                 char buf[max_user_text];
1618
1619                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1620                                 obj->data.s = buf;
1621                                 print_pid_cmdline(obj, p, p_max_size);
1622                         }
1623                         OBJ(pid_cwd) {
1624                                 char buf[max_user_text];
1625
1626                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1627                                 obj->data.s = buf;
1628                                 print_pid_cwd(obj, p, p_max_size);
1629                         }
1630                         OBJ(pid_environ) {
1631                                 char buf[max_user_text];
1632
1633                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1634                                 obj->data.s = buf;
1635                                 print_pid_environ(obj, p, p_max_size);
1636                         }
1637                         OBJ(pid_environ_list) {
1638                                 char buf[max_user_text];
1639
1640                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1641                                 obj->data.s = buf;
1642                                 print_pid_environ_list(obj, p, p_max_size);
1643                         }
1644                         OBJ(pid_exe) {
1645                                 char buf[max_user_text];
1646
1647                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1648                                 obj->data.s = buf;
1649                                 print_pid_exe(obj, p, p_max_size);
1650                         }
1651                         OBJ(pid_nice) {
1652                                 char buf[max_user_text];
1653
1654                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1655                                 obj->data.s = buf;
1656                                 print_pid_nice(obj, p, p_max_size);
1657                         }
1658                         OBJ(pid_openfiles) {
1659                                 char buf[max_user_text];
1660
1661                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1662                                 obj->data.s = buf;
1663                                 print_pid_openfiles(obj, p, p_max_size);
1664                         }
1665                         OBJ(pid_parent) {
1666                                 char buf[max_user_text];
1667
1668                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1669                                 obj->data.s = buf;
1670                                 print_pid_parent(obj, p, p_max_size);
1671                         }
1672                         OBJ(pid_priority) {
1673                                 char buf[max_user_text];
1674
1675                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1676                                 obj->data.s = buf;
1677                                 print_pid_priority(obj, p, p_max_size);
1678                         }
1679                         OBJ(pid_state) {
1680                                 char buf[max_user_text];
1681
1682                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1683                                 obj->data.s = buf;
1684                                 print_pid_state(obj, p, p_max_size);
1685                         }
1686                         OBJ(pid_state_short) {
1687                                 char buf[max_user_text];
1688
1689                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1690                                 obj->data.s = buf;
1691                                 print_pid_state_short(obj, p, p_max_size);
1692                         }
1693                         OBJ(pid_stderr) {
1694                                 char buf[max_user_text];
1695
1696                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1697                                 obj->data.s = buf;
1698                                 print_pid_stderr(obj, p, p_max_size);
1699                         }
1700                         OBJ(pid_stdin) {
1701                                 char buf[max_user_text];
1702
1703                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1704                                 obj->data.s = buf;
1705                                 print_pid_stdin(obj, p, p_max_size);
1706                         }
1707                         OBJ(pid_stdout) {
1708                                 char buf[max_user_text];
1709
1710                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1711                                 obj->data.s = buf;
1712                                 print_pid_stdout(obj, p, p_max_size);
1713                         }
1714                         OBJ(pid_threads) {
1715                                 char buf[max_user_text];
1716
1717                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1718                                 obj->data.s = buf;
1719                                 print_pid_threads(obj, p, p_max_size);
1720                         }
1721                         OBJ(pid_thread_list) {
1722                                 char buf[max_user_text];
1723
1724                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1725                                 obj->data.s = buf;
1726                                 print_pid_thread_list(obj, p, p_max_size);
1727                         }
1728                         OBJ(pid_time_kernelmode) {
1729                                 char buf[max_user_text];
1730
1731                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1732                                 obj->data.s = buf;
1733                                 print_pid_time_kernelmode(obj, p, p_max_size);
1734                         }
1735                         OBJ(pid_time_usermode) {
1736                                 char buf[max_user_text];
1737
1738                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1739                                 obj->data.s = buf;
1740                                 print_pid_time_usermode(obj, p, p_max_size);
1741                         }
1742                         OBJ(pid_time) {
1743                                 char buf[max_user_text];
1744
1745                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1746                                 obj->data.s = buf;
1747                                 print_pid_time(obj, p, p_max_size);
1748                         }
1749                         OBJ(pid_uid) {
1750                                 char buf[max_user_text];
1751
1752                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1753                                 obj->data.s = buf;
1754                                 print_pid_uid(obj, p, p_max_size);
1755                         }
1756                         OBJ(pid_euid) {
1757                                 char buf[max_user_text];
1758
1759                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1760                                 obj->data.s = buf;
1761                                 print_pid_euid(obj, p, p_max_size);
1762                         }
1763                         OBJ(pid_suid) {
1764                                 char buf[max_user_text];
1765
1766                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1767                                 obj->data.s = buf;
1768                                 print_pid_suid(obj, p, p_max_size);
1769                         }
1770                         OBJ(pid_fsuid) {
1771                                 char buf[max_user_text];
1772
1773                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1774                                 obj->data.s = buf;
1775                                 print_pid_fsuid(obj, p, p_max_size);
1776                         }
1777                         OBJ(pid_gid) {
1778                                 char buf[max_user_text];
1779
1780                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1781                                 obj->data.s = buf;
1782                                 print_pid_gid(obj, p, p_max_size);
1783                         }
1784                         OBJ(pid_egid) {
1785                                 char buf[max_user_text];
1786
1787                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1788                                 obj->data.s = buf;
1789                                 print_pid_egid(obj, p, p_max_size);
1790                         }
1791                         OBJ(pid_sgid) {
1792                                 char buf[max_user_text];
1793
1794                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1795                                 obj->data.s = buf;
1796                                 print_pid_sgid(obj, p, p_max_size);
1797                         }
1798                         OBJ(pid_fsgid) {
1799                                 char buf[max_user_text];
1800
1801                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1802                                 obj->data.s = buf;
1803                                 print_pid_fsgid(obj, p, p_max_size);
1804                         }
1805                         OBJ(pid_vmpeak) {
1806                                 char buf[max_user_text];
1807
1808                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1809                                 obj->data.s = buf;
1810                                 print_pid_vmpeak(obj, p, p_max_size);
1811                         }
1812                         OBJ(pid_vmsize) {
1813                                 char buf[max_user_text];
1814
1815                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1816                                 obj->data.s = buf;
1817                                 print_pid_vmsize(obj, p, p_max_size);
1818                         }
1819                         OBJ(pid_vmlck) {
1820                                 char buf[max_user_text];
1821
1822                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1823                                 obj->data.s = buf;
1824                                 print_pid_vmlck(obj, p, p_max_size);
1825                         }
1826                         OBJ(pid_vmhwm) {
1827                                 char buf[max_user_text];
1828
1829                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1830                                 obj->data.s = buf;
1831                                 print_pid_vmhwm(obj, p, p_max_size);
1832                         }
1833                         OBJ(pid_vmrss) {
1834                                 char buf[max_user_text];
1835
1836                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1837                                 obj->data.s = buf;
1838                                 print_pid_vmrss(obj, p, p_max_size);
1839                         }
1840                         OBJ(pid_vmdata) {
1841                                 char buf[max_user_text];
1842
1843                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1844                                 obj->data.s = buf;
1845                                 print_pid_vmdata(obj, p, p_max_size);
1846                         }
1847                         OBJ(pid_vmstk) {
1848                                 char buf[max_user_text];
1849
1850                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1851                                 obj->data.s = buf;
1852                                 print_pid_vmstk(obj, p, p_max_size);
1853                         }
1854                         OBJ(pid_vmexe) {
1855                                 char buf[max_user_text];
1856
1857                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1858                                 obj->data.s = buf;
1859                                 print_pid_vmexe(obj, p, p_max_size);
1860                         }
1861                         OBJ(pid_vmlib) {
1862                                 char buf[max_user_text];
1863
1864                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1865                                 obj->data.s = buf;
1866                                 print_pid_vmlib(obj, p, p_max_size);
1867                         }
1868                         OBJ(pid_vmpte) {
1869                                 char buf[max_user_text];
1870
1871                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1872                                 obj->data.s = buf;
1873                                 print_pid_vmpte(obj, p, p_max_size);
1874                         }
1875                         OBJ(processes) {
1876                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
1877                         }
1878                         OBJ(running_processes) {
1879                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
1880                         }
1881                         OBJ(running_threads) {
1882                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_threads);
1883                         }
1884                         OBJ(threads) {
1885                                 spaced_print(p, p_max_size, "%hu", 4, cur->threads);
1886                         }
1887                         OBJ(text) {
1888                                 snprintf(p, p_max_size, "%s", obj->data.s);
1889                         }
1890 #ifdef X11
1891                         OBJ(shadecolor) {
1892                                 new_bg(p, obj->data.l);
1893                         }
1894                         OBJ(stippled_hr) {
1895                                 new_stippled_hr(obj, p);
1896                         }
1897 #endif /* X11 */
1898                         OBJ(swap) {
1899                                 human_readable(cur->swap * 1024, p, 255);
1900                         }
1901                         OBJ(swapfree) {
1902                                 human_readable(cur->swapfree * 1024, p, 255);
1903                         }
1904                         OBJ(swapmax) {
1905                                 human_readable(cur->swapmax * 1024, p, 255);
1906                         }
1907                         OBJ(swapperc) {
1908                                 if (cur->swapmax == 0) {
1909                                         strncpy(p, "No swap", p_max_size);
1910                                 } else {
1911                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
1912                                 }
1913                         }
1914                         OBJ(swapbar) {
1915 #ifdef X11
1916                                 if(output_methods & TO_X) {
1917                                         new_bar(obj, p, cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
1918                                 }else
1919 #endif /* X11 */
1920                                         new_bar_in_shell(obj, p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0);
1921                         }
1922                         OBJ(sysname) {
1923                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
1924                         }
1925                         OBJ(time) {
1926                                 print_time(obj, p, p_max_size);
1927                         }
1928                         OBJ(utime) {
1929                                 print_utime(obj, p, p_max_size);
1930                         }
1931                         OBJ(tztime) {
1932                                 print_tztime(obj, p, p_max_size);
1933                         }
1934                         OBJ(totaldown) {
1935                                 print_totaldown(obj, p, p_max_size);
1936                         }
1937                         OBJ(totalup) {
1938                                 print_totalup(obj, p, p_max_size);
1939                         }
1940                         OBJ(gid_name) {
1941                                 char buf[max_user_text];
1942
1943                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1944                                 obj->data.s = buf;
1945                                 print_gid_name(obj, p, p_max_size);
1946                         }
1947                         OBJ(uid_name) {
1948                                 char buf[max_user_text];
1949
1950                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
1951                                 obj->data.s = buf;
1952                                 print_uid_name(obj, p, p_max_size);
1953                         }
1954                         OBJ(updates) {
1955                                 snprintf(p, p_max_size, "%d", total_updates);
1956                         }
1957                         OBJ(if_updatenr) {
1958                                 if(total_updates % updatereset != obj->data.i - 1) {
1959                                         DO_JUMP;
1960                                 }
1961                         }
1962                         OBJ(upspeed) {
1963                                 print_upspeed(obj, p, p_max_size);
1964                         }
1965                         OBJ(upspeedf) {
1966                                 print_upspeedf(obj, p, p_max_size);
1967                         }
1968 #ifdef X11
1969                         OBJ(upspeedgraph) {
1970                                 print_upspeedgraph(obj, p);
1971                         }
1972 #endif /* X11 */
1973                         OBJ(uptime_short) {
1974                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
1975                         }
1976                         OBJ(uptime) {
1977                                 format_seconds(p, p_max_size, (int) cur->uptime);
1978                         }
1979 #ifdef __linux__
1980                         OBJ(user_names) {
1981                                 snprintf(p, p_max_size, "%s", cur->users.names);
1982                         }
1983                         OBJ(user_terms) {
1984                                 snprintf(p, p_max_size, "%s", cur->users.terms);
1985                         }
1986                         OBJ(user_times) {
1987                                 snprintf(p, p_max_size, "%s", cur->users.times);
1988                         }
1989                         OBJ(user_time) {
1990                                 update_user_time(obj->data.s);
1991                                 snprintf(p, p_max_size, "%s", cur->users.ctime);
1992                         }
1993                         OBJ(user_number) {
1994                                 snprintf(p, p_max_size, "%d", cur->users.number);
1995                         }
1996 #endif /* __linux__ */
1997 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1998                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1999                         OBJ(apm_adapter) {
2000                                 char *msg;
2001
2002                                 msg = get_apm_adapter();
2003                                 snprintf(p, p_max_size, "%s", msg);
2004                                 free(msg);
2005                         }
2006                         OBJ(apm_battery_life) {
2007                                 char *msg;
2008
2009                                 msg = get_apm_battery_life();
2010                                 snprintf(p, p_max_size, "%s", msg);
2011                                 free(msg);
2012                         }
2013                         OBJ(apm_battery_time) {
2014                                 char *msg;
2015
2016                                 msg = get_apm_battery_time();
2017                                 snprintf(p, p_max_size, "%s", msg);
2018                                 free(msg);
2019                         }
2020 #endif /* __FreeBSD__ __OpenBSD__ */
2021
2022 #ifdef MPD
2023 #define mpd_printf(fmt, val) \
2024         snprintf(p, p_max_size, fmt, mpd_get_info()->val)
2025 #define mpd_sprintf(val) { \
2026         if (!obj->data.i || obj->data.i > p_max_size) \
2027                 mpd_printf("%s", val); \
2028         else \
2029                 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
2030 }
2031                         OBJ(mpd_title)
2032                                 mpd_sprintf(title);
2033                         OBJ(mpd_artist)
2034                                 mpd_sprintf(artist);
2035                         OBJ(mpd_album)
2036                                 mpd_sprintf(album);
2037                         OBJ(mpd_random)
2038                                 mpd_printf("%s", random);
2039                         OBJ(mpd_repeat)
2040                                 mpd_printf("%s", repeat);
2041                         OBJ(mpd_track)
2042                                 mpd_sprintf(track);
2043                         OBJ(mpd_name)
2044                                 mpd_sprintf(name);
2045                         OBJ(mpd_file)
2046                                 mpd_sprintf(file);
2047                         OBJ(mpd_vol)
2048                                 mpd_printf("%d", volume);
2049                         OBJ(mpd_bitrate)
2050                                 mpd_printf("%d", bitrate);
2051                         OBJ(mpd_status)
2052                                 mpd_printf("%s", status);
2053                         OBJ(mpd_elapsed) {
2054                                 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
2055                         }
2056                         OBJ(mpd_length) {
2057                                 format_media_player_time(p, p_max_size, mpd_get_info()->length);
2058                         }
2059                         OBJ(mpd_percent) {
2060                                 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
2061                         }
2062                         OBJ(mpd_bar) {
2063 #ifdef X11
2064                                 if(output_methods & TO_X) {
2065                                         new_bar(obj, p, (int) (mpd_get_info()->progress * 255.0f));
2066                                 } else
2067 #endif /* X11 */
2068                                         new_bar_in_shell(obj, p, p_max_size, (int) (mpd_get_info()->progress * 100.0f));
2069                         }
2070                         OBJ(mpd_smart) {
2071                                 struct mpd_s *mpd = mpd_get_info();
2072                                 int len = obj->data.i;
2073                                 if (len == 0 || len > p_max_size)
2074                                         len = p_max_size;
2075
2076                                 memset(p, 0, p_max_size);
2077                                 if (mpd->artist && *mpd->artist &&
2078                                     mpd->title && *mpd->title) {
2079                                         snprintf(p, len, "%s - %s", mpd->artist,
2080                                                 mpd->title);
2081                                 } else if (mpd->title && *mpd->title) {
2082                                         snprintf(p, len, "%s", mpd->title);
2083                                 } else if (mpd->artist && *mpd->artist) {
2084                                         snprintf(p, len, "%s", mpd->artist);
2085                                 } else if (mpd->file && *mpd->file) {
2086                                         snprintf(p, len, "%s", mpd->file);
2087                                 } else {
2088                                         *p = 0;
2089                                 }
2090                         }
2091                         OBJ(if_mpd_playing) {
2092                                 if (!mpd_get_info()->is_playing) {
2093                                         DO_JUMP;
2094                                 }
2095                         }
2096 #undef mpd_sprintf
2097 #undef mpd_printf
2098 #endif
2099
2100 #ifdef MOC
2101 #define MOC_PRINT(t, a) \
2102         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2103                         OBJ(moc_state) {
2104                                 MOC_PRINT(state, "??");
2105                         }
2106                         OBJ(moc_file) {
2107                                 MOC_PRINT(file, "no file");
2108                         }
2109                         OBJ(moc_title) {
2110                                 MOC_PRINT(title, "no title");
2111                         }
2112                         OBJ(moc_artist) {
2113                                 MOC_PRINT(artist, "no artist");
2114                         }
2115                         OBJ(moc_song) {
2116                                 MOC_PRINT(song, "no song");
2117                         }
2118                         OBJ(moc_album) {
2119                                 MOC_PRINT(album, "no album");
2120                         }
2121                         OBJ(moc_totaltime) {
2122                                 MOC_PRINT(totaltime, "0:00");
2123                         }
2124                         OBJ(moc_timeleft) {
2125                                 MOC_PRINT(timeleft, "0:00");
2126                         }
2127                         OBJ(moc_curtime) {
2128                                 MOC_PRINT(curtime, "0:00");
2129                         }
2130                         OBJ(moc_bitrate) {
2131                                 MOC_PRINT(bitrate, "0Kbps");
2132                         }
2133                         OBJ(moc_rate) {
2134                                 MOC_PRINT(rate, "0KHz");
2135                         }
2136 #undef MOC_PRINT
2137 #endif /* MOC */
2138 #ifdef XMMS2
2139                         OBJ(xmms2_artist) {
2140                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2141                         }
2142                         OBJ(xmms2_album) {
2143                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2144                         }
2145                         OBJ(xmms2_title) {
2146                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2147                         }
2148                         OBJ(xmms2_genre) {
2149                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2150                         }
2151                         OBJ(xmms2_comment) {
2152                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2153                         }
2154                         OBJ(xmms2_url) {
2155                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2156                         }
2157                         OBJ(xmms2_status) {
2158                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2159                         }
2160                         OBJ(xmms2_date) {
2161                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2162                         }
2163                         OBJ(xmms2_tracknr) {
2164                                 if (cur->xmms2.tracknr != -1) {
2165                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2166                                 }
2167                         }
2168                         OBJ(xmms2_bitrate) {
2169                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2170                         }
2171                         OBJ(xmms2_id) {
2172                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2173                         }
2174                         OBJ(xmms2_size) {
2175                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2176                         }
2177                         OBJ(xmms2_elapsed) {
2178                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2179                                         (cur->xmms2.elapsed / 1000) % 60);
2180                         }
2181                         OBJ(xmms2_duration) {
2182                                 snprintf(p, p_max_size, "%02d:%02d",
2183                                         cur->xmms2.duration / 60000,
2184                                         (cur->xmms2.duration / 1000) % 60);
2185                         }
2186                         OBJ(xmms2_percent) {
2187                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2188                         }
2189 #ifdef X11
2190                         OBJ(xmms2_bar) {
2191                                 new_bar(obj, p, (int) (cur->xmms2.progress * 255.0f));
2192                         }
2193 #endif /* X11 */
2194                         OBJ(xmms2_playlist) {
2195                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2196                         }
2197                         OBJ(xmms2_timesplayed) {
2198                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2199                         }
2200                         OBJ(xmms2_smart) {
2201                                 if (strlen(cur->xmms2.title) < 2
2202                                                 && strlen(cur->xmms2.title) < 2) {
2203                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
2204                                 } else {
2205                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2206                                                 cur->xmms2.title);
2207                                 }
2208                         }
2209                         OBJ(if_xmms2_connected) {
2210                                 if (cur->xmms2.conn_state != 1) {
2211                                         DO_JUMP;
2212                                 }
2213                         }
2214 #endif /* XMMS */
2215 #ifdef AUDACIOUS
2216                         OBJ(audacious_status) {
2217                                 snprintf(p, p_max_size, "%s",
2218                                         cur->audacious.items[AUDACIOUS_STATUS]);
2219                         }
2220                         OBJ(audacious_title) {
2221                                 snprintf(p, cur->audacious.max_title_len > 0
2222                                         ? cur->audacious.max_title_len : p_max_size, "%s",
2223                                         cur->audacious.items[AUDACIOUS_TITLE]);
2224                         }
2225                         OBJ(audacious_length) {
2226                                 snprintf(p, p_max_size, "%s",
2227                                         cur->audacious.items[AUDACIOUS_LENGTH]);
2228                         }
2229                         OBJ(audacious_length_seconds) {
2230                                 snprintf(p, p_max_size, "%s",
2231                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2232                         }
2233                         OBJ(audacious_position) {
2234                                 snprintf(p, p_max_size, "%s",
2235                                         cur->audacious.items[AUDACIOUS_POSITION]);
2236                         }
2237                         OBJ(audacious_position_seconds) {
2238                                 snprintf(p, p_max_size, "%s",
2239                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2240                         }
2241                         OBJ(audacious_bitrate) {
2242                                 snprintf(p, p_max_size, "%s",
2243                                         cur->audacious.items[AUDACIOUS_BITRATE]);
2244                         }
2245                         OBJ(audacious_frequency) {
2246                                 snprintf(p, p_max_size, "%s",
2247                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
2248                         }
2249                         OBJ(audacious_channels) {
2250                                 snprintf(p, p_max_size, "%s",
2251                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
2252                         }
2253                         OBJ(audacious_filename) {
2254                                 snprintf(p, p_max_size, "%s",
2255                                         cur->audacious.items[AUDACIOUS_FILENAME]);
2256                         }
2257                         OBJ(audacious_playlist_length) {
2258                                 snprintf(p, p_max_size, "%s",
2259                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2260                         }
2261                         OBJ(audacious_playlist_position) {
2262                                 snprintf(p, p_max_size, "%s",
2263                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2264                         }
2265                         OBJ(audacious_main_volume) {
2266                                 snprintf(p, p_max_size, "%s",
2267                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2268                         }
2269 #ifdef X11
2270                         OBJ(audacious_bar) {
2271                                 double progress;
2272
2273                                 progress =
2274                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2275                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2276                                 new_bar(obj, p, (int) (progress * 255.0f));
2277                         }
2278 #endif /* X11 */
2279 #endif /* AUDACIOUS */
2280
2281 #ifdef BMPX
2282                         OBJ(bmpx_title) {
2283                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2284                         }
2285                         OBJ(bmpx_artist) {
2286                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2287                         }
2288                         OBJ(bmpx_album) {
2289                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2290                         }
2291                         OBJ(bmpx_uri) {
2292                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2293                         }
2294                         OBJ(bmpx_track) {
2295                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2296                         }
2297                         OBJ(bmpx_bitrate) {
2298                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2299                         }
2300 #endif /* BMPX */
2301                         /* we have four different types of top (top, top_mem,
2302                          * top_time and top_io). To avoid having almost-same code four
2303                          * times, we have this special handler. */
2304 #ifdef __linux__
2305                         break;
2306                         case OBJ_top:
2307                         case OBJ_top_mem:
2308                         case OBJ_top_time:
2309 #ifdef IOSTATS
2310                         case OBJ_top_io:
2311 #endif
2312                                 /* yes, passing top_name_width instead
2313                                  * of p_max_size is intended here */
2314                                 print_top(obj, p, top_name_width);
2315 #endif /* __linux__ */
2316                         OBJ(tail) {
2317                                 print_tailhead("tail", obj, p, p_max_size);
2318                         }
2319                         OBJ(head) {
2320                                 print_tailhead("head", obj, p, p_max_size);
2321                         }
2322                         OBJ(lines) {
2323                                 static int rep = 0;
2324                                 FILE *fp = open_file(obj->data.s, &rep);
2325
2326                                 if(fp != NULL) {
2327 /* FIXME: use something more general (see also tail.c, head.c */
2328 #define BUFSZ 0x1000
2329                                         char buf[BUFSZ];
2330                                         int j, lines;
2331
2332                                         lines = 0;
2333                                         while(fgets(buf, BUFSZ, fp) != NULL){
2334                                                 for(j = 0; buf[j] != 0; j++) {
2335                                                         if(buf[j] == '\n') {
2336                                                                 lines++;
2337                                                         }
2338                                                 }
2339                                         }
2340                                         sprintf(p, "%d", lines);
2341                                         fclose(fp);
2342                                 } else {
2343                                         sprintf(p, "File Unreadable");
2344                                 }
2345                         }
2346
2347                         OBJ(words) {
2348                                 static int rep = 0;
2349                                 FILE *fp = open_file(obj->data.s, &rep);
2350
2351                                 if(fp != NULL) {
2352                                         char buf[BUFSZ];
2353                                         int j, words;
2354                                         char inword = FALSE;
2355
2356                                         words = 0;
2357                                         while(fgets(buf, BUFSZ, fp) != NULL){
2358                                                 for(j = 0; buf[j] != 0; j++) {
2359                                                         if(!isspace(buf[j])) {
2360                                                                 if(inword == FALSE) {
2361                                                                         words++;
2362                                                                         inword = TRUE;
2363                                                                 }
2364                                                         } else {
2365                                                                 inword = FALSE;
2366                                                         }
2367                                                 }
2368                                         }
2369                                         sprintf(p, "%d", words);
2370                                         fclose(fp);
2371                                 } else {
2372                                         sprintf(p, "File Unreadable");
2373                                 }
2374                         }
2375 #ifdef TCP_PORT_MONITOR
2376                         OBJ(tcp_portmon) {
2377                                 tcp_portmon_action(obj, p, p_max_size);
2378                         }
2379 #endif /* TCP_PORT_MONITOR */
2380
2381 #ifdef HAVE_ICONV
2382                         OBJ(iconv_start) {
2383                                 do_iconv_start(obj);
2384                         }
2385                         OBJ(iconv_stop) {
2386                                 do_iconv_stop();
2387                         }
2388 #endif /* HAVE_ICONV */
2389
2390                         OBJ(entropy_avail) {
2391                                 print_entropy_avail(obj, p, p_max_size);
2392                         }
2393                         OBJ(entropy_perc) {
2394                                 print_entropy_perc(obj, p, p_max_size);
2395                         }
2396                         OBJ(entropy_poolsize) {
2397                                 print_entropy_poolsize(obj, p, p_max_size);
2398                         }
2399                         OBJ(entropy_bar) {
2400                                 print_entropy_bar(obj, p, p_max_size);
2401                         }
2402 #ifdef IBM
2403                         OBJ(smapi) {
2404                                 char *s;
2405                                 if(obj->data.s) {
2406                                         s = smapi_get_val(obj->data.s);
2407                                         snprintf(p, p_max_size, "%s", s);
2408                                         free(s);
2409                                 }
2410                         }
2411                         OBJ(if_smapi_bat_installed) {
2412                                 int idx;
2413                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2414                                         if(!smapi_bat_installed(idx)) {
2415                                                 DO_JUMP;
2416                                         }
2417                                 } else
2418                                         NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2419                         }
2420                         OBJ(smapi_bat_perc) {
2421                                 int idx, val;
2422                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2423                                         val = smapi_bat_installed(idx) ?
2424                                                 smapi_get_bat_int(idx, "remaining_percent") : 0;
2425                                         percent_print(p, p_max_size, val);
2426                                 } else
2427                                         NORM_ERR("argument to smapi_bat_perc must be an integer");
2428                         }
2429                         OBJ(smapi_bat_temp) {
2430                                 int idx, val;
2431                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2432                                         val = smapi_bat_installed(idx) ?
2433                                                 smapi_get_bat_int(idx, "temperature") : 0;
2434                                         /* temperature is in milli degree celsius */
2435                                         temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2436                                 } else
2437                                         NORM_ERR("argument to smapi_bat_temp must be an integer");
2438                         }
2439                         OBJ(smapi_bat_power) {
2440                                 int idx, val;
2441                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2442                                         val = smapi_bat_installed(idx) ?
2443                                                 smapi_get_bat_int(idx, "power_now") : 0;
2444                                         /* power_now is in mW, set to W with one digit precision */
2445                                         snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2446                                 } else
2447                                         NORM_ERR("argument to smapi_bat_power must be an integer");
2448                         }
2449 #ifdef X11
2450                         OBJ(smapi_bat_bar) {
2451                                 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2452                                         new_bar(obj, p, (int)
2453                                                         (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2454                                 else
2455                                         new_bar(obj, p, 0);
2456                         }
2457 #endif /* X11 */
2458 #endif /* IBM */
2459                         OBJ(include) {
2460                                 if(obj->sub) {
2461                                         char buf[max_user_text];
2462
2463                                         generate_text_internal(buf, max_user_text, *obj->sub, cur);
2464                                         snprintf(p, p_max_size, "%s", buf);
2465                                 } else {
2466                                         p[0] = 0;
2467                                 }
2468                         }
2469                         OBJ(blink) {
2470                                 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2471                                 char buf[max_user_text];
2472                                 unsigned int j;
2473
2474                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2475                                 snprintf(p, p_max_size, "%s", buf);
2476                                 if(total_updates % 2) {
2477                                         for(j=0; p[j] != 0; j++) {
2478                                                 p[j] = ' ';
2479                                         }
2480                                 }
2481                         }
2482                         OBJ(to_bytes) {
2483                                 char buf[max_user_text];
2484                                 long long bytes;
2485                                 char unit[16];  // 16 because we can also have long names (like mega-bytes)
2486
2487                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2488                                 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2489                                         if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2490                                         else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2491                                         else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2492                                         else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2493                                         else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2494                                 }
2495                                 snprintf(p, p_max_size, "%s", buf);
2496                         }
2497                         OBJ(scroll) {
2498                                 print_scroll(obj, p, p_max_size, cur);
2499                         }
2500                         OBJ(combine) {
2501                                 print_combine(obj, p, cur);
2502                         }
2503 #ifdef NVIDIA
2504                         OBJ(nvidia) {
2505                                 print_nvidia_value(obj, display, p, p_max_size);
2506                         }
2507 #endif /* NVIDIA */
2508 #ifdef APCUPSD
2509                         OBJ(apcupsd) {
2510                                 /* This is just a meta-object to set host:port */
2511                         }
2512                         OBJ(apcupsd_name) {
2513                                 snprintf(p, p_max_size, "%s",
2514                                                  cur->apcupsd.items[APCUPSD_NAME]);
2515                         }
2516                         OBJ(apcupsd_model) {
2517                                 snprintf(p, p_max_size, "%s",
2518                                                  cur->apcupsd.items[APCUPSD_MODEL]);
2519                         }
2520                         OBJ(apcupsd_upsmode) {
2521                                 snprintf(p, p_max_size, "%s",
2522                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
2523                         }
2524                         OBJ(apcupsd_cable) {
2525                                 snprintf(p, p_max_size, "%s",
2526                                                  cur->apcupsd.items[APCUPSD_CABLE]);
2527                         }
2528                         OBJ(apcupsd_status) {
2529                                 snprintf(p, p_max_size, "%s",
2530                                                  cur->apcupsd.items[APCUPSD_STATUS]);
2531                         }
2532                         OBJ(apcupsd_linev) {
2533                                 snprintf(p, p_max_size, "%s",
2534                                                  cur->apcupsd.items[APCUPSD_LINEV]);
2535                         }
2536                         OBJ(apcupsd_load) {
2537                                 snprintf(p, p_max_size, "%s",
2538                                                  cur->apcupsd.items[APCUPSD_LOAD]);
2539                         }
2540                         OBJ(apcupsd_loadbar) {
2541                                 double progress;
2542 #ifdef X11
2543                                 if(output_methods & TO_X) {
2544                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2545                                         new_bar(obj, p, (int) progress);
2546                                 } else
2547 #endif /* X11 */
2548                                 {
2549                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
2550                                         new_bar_in_shell(obj, p, p_max_size, (int) progress);
2551                                 }
2552                         }
2553 #ifdef X11
2554                         OBJ(apcupsd_loadgraph) {
2555                                 double progress;
2556                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
2557                                 new_graph(obj, p, (int)progress);
2558                         }
2559                         OBJ(apcupsd_loadgauge) {
2560                                 double progress;
2561                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
2562                                 new_gauge(obj, p, (int)progress);
2563                         }
2564 #endif /* X11 */
2565                         OBJ(apcupsd_charge) {
2566                                 snprintf(p, p_max_size, "%s",
2567                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
2568                         }
2569                         OBJ(apcupsd_timeleft) {
2570                                 snprintf(p, p_max_size, "%s",
2571                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
2572                         }
2573                         OBJ(apcupsd_temp) {
2574                                 snprintf(p, p_max_size, "%s",
2575                                                  cur->apcupsd.items[APCUPSD_TEMP]);
2576                         }
2577                         OBJ(apcupsd_lastxfer) {
2578                                 snprintf(p, p_max_size, "%s",
2579                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
2580                         }
2581 #endif /* APCUPSD */
2582                         break;
2583                 }
2584 #undef DO_JUMP
2585
2586
2587                 {
2588                         size_t a = strlen(p);
2589
2590 #ifdef HAVE_ICONV
2591                         iconv_convert(&a, buff_in, p, p_max_size);
2592 #endif /* HAVE_ICONV */
2593                         if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
2594 #ifdef HAVE_LUA
2595                                         && obj->type != OBJ_lua && obj->type != OBJ_lua_parse
2596 #endif /* HAVE_LUA */
2597                                         ) {
2598                                 substitute_newlines(p, a - 2);
2599                         }
2600                         p += a;
2601                         p_max_size -= a;
2602                         (*p) = 0;
2603                 }
2604                 obj = obj->next;
2605         }
2606 #ifdef X11
2607         /* load any new fonts we may have had */
2608         if (need_to_load_fonts) {
2609                 load_fonts();
2610         }
2611 #endif /* X11 */
2612 }
2613
2614 void evaluate(const char *text, char *p, int p_max_size)
2615 {
2616         struct information *tmp_info;
2617         struct text_object subroot;
2618
2619         tmp_info = malloc(sizeof(struct information));
2620         memcpy(tmp_info, &info, sizeof(struct information));
2621         parse_conky_vars(&subroot, text, p, p_max_size, tmp_info);
2622         DBGP("evaluated '%s' to '%s'", text, p);
2623
2624         free_text_objects(&subroot, 1);
2625         free(tmp_info);
2626 }
2627
2628 double current_update_time, next_update_time, last_update_time;
2629
2630 static void generate_text(void)
2631 {
2632         struct information *cur = &info;
2633         char *p;
2634
2635         special_count = 0;
2636
2637         /* update info */
2638
2639         current_update_time = get_time();
2640
2641         update_stuff();
2642
2643         /* add things to the buffer */
2644
2645         /* generate text */
2646
2647         p = text_buffer;
2648
2649         generate_text_internal(p, max_user_text, global_root_object, cur);
2650
2651         if (stuff_in_uppercase) {
2652                 char *tmp_p;
2653
2654                 tmp_p = text_buffer;
2655                 while (*tmp_p) {
2656                         *tmp_p = toupper(*tmp_p);
2657                         tmp_p++;
2658                 }
2659         }
2660
2661         next_update_time += update_interval;
2662         if (next_update_time < get_time()) {
2663                 next_update_time = get_time() + update_interval;
2664         } else if (next_update_time > get_time() + update_interval) {
2665                 next_update_time = get_time() + update_interval;
2666         }
2667         last_update_time = current_update_time;
2668         total_updates++;
2669 }
2670
2671 void set_update_interval(double interval)
2672 {
2673         update_interval = interval;
2674         update_interval_old = interval;
2675 }
2676
2677 static inline int get_string_width(const char *s)
2678 {
2679         return *s ? calc_text_width(s) : 0;
2680 }
2681
2682 #ifdef X11
2683 static int get_string_width_special(char *s, int special_index)
2684 {
2685         char *p, *final;
2686         int idx = 1;
2687         int width = 0;
2688         long i;
2689
2690         if (!s)
2691                 return 0;
2692
2693         if ((output_methods & TO_X) == 0)
2694                 return strlen(s);
2695
2696         p = strndup(s, text_buffer_size);
2697         final = p;
2698
2699         while (*p) {
2700                 if (*p == SPECIAL_CHAR) {
2701                         /* shift everything over by 1 so that the special char
2702                          * doesn't mess up the size calculation */
2703                         for (i = 0; i < (long)strlen(p); i++) {
2704                                 *(p + i) = *(p + i + 1);
2705                         }
2706                         if (specials[special_index + idx].type == GRAPH
2707                                         || specials[special_index + idx].type == GAUGE
2708                                         || specials[special_index + idx].type == BAR) {
2709                                 width += specials[special_index + idx].width;
2710                         }
2711                         idx++;
2712                 } else if (*p == SECRIT_MULTILINE_CHAR) {
2713                         *p = 0;
2714                         break;
2715                 } else {
2716                         p++;
2717                 }
2718         }
2719         if (strlen(final) > 1) {
2720                 width += calc_text_width(final);
2721         }
2722         free(final);
2723         return width;
2724 }
2725
2726 static int text_size_updater(char *s, int special_index);
2727
2728 int last_font_height;
2729 static void update_text_area(void)
2730 {
2731         int x = 0, y = 0;
2732
2733         if ((output_methods & TO_X) == 0)
2734                 return;
2735         /* update text size if it isn't fixed */
2736 #ifdef OWN_WINDOW
2737         if (!fixed_size)
2738 #endif
2739         {
2740                 text_width = minimum_width;
2741                 text_height = 0;
2742                 last_font_height = font_height();
2743                 for_each_line(text_buffer, text_size_updater);
2744                 text_width += 1;
2745                 if (text_height < minimum_height) {
2746                         text_height = minimum_height;
2747                 }
2748                 if (text_width > maximum_width && maximum_width > 0) {
2749                         text_width = maximum_width;
2750                 }
2751         }
2752
2753         /* get text position on workarea */
2754         switch (text_alignment) {
2755                 case TOP_LEFT: case TOP_RIGHT: case TOP_MIDDLE:
2756                         y = gap_y;
2757                         break;
2758
2759                 case BOTTOM_LEFT: case BOTTOM_RIGHT: case BOTTOM_MIDDLE: default:
2760                         y = workarea[3] - text_height - gap_y;
2761                         break;
2762
2763                 case MIDDLE_LEFT: case MIDDLE_RIGHT: case MIDDLE_MIDDLE:
2764                         y = workarea[3] / 2 - text_height / 2 - gap_y;
2765                         break;
2766         }
2767         switch (text_alignment) {
2768                 case TOP_LEFT: case BOTTOM_LEFT: case MIDDLE_LEFT: default:
2769                         x = gap_x;
2770                         break;
2771
2772                 case TOP_RIGHT: case BOTTOM_RIGHT: case MIDDLE_RIGHT:
2773                         x = workarea[2] - text_width - gap_x;
2774                         break;
2775
2776                 case TOP_MIDDLE: case BOTTOM_MIDDLE: case MIDDLE_MIDDLE:
2777                         x = workarea[2] / 2 - text_width / 2 - gap_x;
2778                         break;
2779         }
2780 #ifdef OWN_WINDOW
2781         if (text_alignment == NONE) {   // Let the WM manage the window
2782                         x = window.x;
2783                         y = window.y;
2784
2785                         fixed_pos = 1;
2786                         fixed_size = 1;
2787         }
2788 #endif /* OWN_WINDOW */
2789 #ifdef OWN_WINDOW
2790
2791         if (own_window && !fixed_pos) {
2792                 x += workarea[0];
2793                 y += workarea[1];
2794                 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
2795                 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
2796                 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
2797                 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
2798         } else
2799 #endif
2800         {
2801                 /* If window size doesn't match to workarea's size,
2802                  * then window probably includes panels (gnome).
2803                  * Blah, doesn't work on KDE. */
2804                 if (workarea[2] != window.width || workarea[3] != window.height) {
2805                         y += workarea[1];
2806                         x += workarea[0];
2807                 }
2808
2809                 text_start_x = x;
2810                 text_start_y = y;
2811         }
2812 #ifdef HAVE_LUA
2813         /* update lua window globals */
2814         llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
2815 #endif /* HAVE_LUA */
2816 }
2817
2818 /* drawing stuff */
2819
2820 static int cur_x, cur_y;        /* current x and y for drawing */
2821 #endif
2822 //draw_mode also without X11 because we only need to print to stdout with FG
2823 static int draw_mode;           /* FG, BG or OUTLINE */
2824 #ifdef X11
2825 static long current_color;
2826
2827 static int text_size_updater(char *s, int special_index)
2828 {
2829         int w = 0;
2830         char *p;
2831
2832         if ((output_methods & TO_X) == 0)
2833                 return 0;
2834         /* get string widths and skip specials */
2835         p = s;
2836         while (*p) {
2837                 if (*p == SPECIAL_CHAR) {
2838                         *p = '\0';
2839                         w += get_string_width(s);
2840                         *p = SPECIAL_CHAR;
2841
2842                         if (specials[special_index].type == BAR
2843                                         || specials[special_index].type == GAUGE
2844                                         || specials[special_index].type == GRAPH) {
2845                                 w += specials[special_index].width;
2846                                 if (specials[special_index].height > last_font_height) {
2847                                         last_font_height = specials[special_index].height;
2848                                         last_font_height += font_height();
2849                                 }
2850                         } else if (specials[special_index].type == OFFSET) {
2851                                 if (specials[special_index].arg > 0) {
2852                                         w += specials[special_index].arg;
2853                                 }
2854                         } else if (specials[special_index].type == VOFFSET) {
2855                                 last_font_height += specials[special_index].arg;
2856                         } else if (specials[special_index].type == GOTO) {
2857                                 if (specials[special_index].arg > cur_x) {
2858                                         w = (int) specials[special_index].arg;
2859                                 }
2860                         } else if (specials[special_index].type == TAB) {
2861                                 int start = specials[special_index].arg;
2862                                 int step = specials[special_index].width;
2863
2864                                 if (!step || step < 0) {
2865                                         step = 10;
2866                                 }
2867                                 w += step - (cur_x - text_start_x - start) % step;
2868                         } else if (specials[special_index].type == FONT) {
2869                                 selected_font = specials[special_index].font_added;
2870                                 if (font_height() > last_font_height) {
2871                                         last_font_height = font_height();
2872                                 }
2873                         }
2874
2875                         special_index++;
2876                         s = p + 1;
2877                 } else if (*p == SECRIT_MULTILINE_CHAR) {
2878                         int lw;
2879                         *p = '\0';
2880                         lw = get_string_width(s);
2881                         *p = SECRIT_MULTILINE_CHAR;
2882                         s = p + 1;
2883                         w = lw > w ? lw : w;
2884                         text_height += last_font_height;
2885                 }
2886                 p++;
2887         }
2888         w += get_string_width(s);
2889         if (w > text_width) {
2890                 text_width = w;
2891         }
2892         if (text_width > maximum_width && maximum_width) {
2893                 text_width = maximum_width;
2894         }
2895
2896         text_height += last_font_height;
2897         last_font_height = font_height();
2898         return special_index;
2899 }
2900 #endif /* X11 */
2901
2902 static inline void set_foreground_color(long c)
2903 {
2904 #ifdef X11
2905         if (output_methods & TO_X) {
2906                 current_color = c;
2907                 XSetForeground(display, window.gc, c);
2908         }
2909 #endif /* X11 */
2910 #ifdef NCURSES
2911         if (output_methods & TO_NCURSES) {
2912                 attron(COLOR_PAIR(c));
2913         }
2914 #endif /* NCURSES */
2915         UNUSED(c);
2916         return;
2917 }
2918
2919 static void draw_string(const char *s)
2920 {
2921         int i, i2, pos, width_of_s;
2922         int max = 0;
2923         int added;
2924         char *s_with_newlines;
2925
2926         if (s[0] == '\0') {
2927                 return;
2928         }
2929
2930         width_of_s = get_string_width(s);
2931         s_with_newlines = strdup(s);
2932         for(i = 0; i < (int) strlen(s_with_newlines); i++) {
2933                 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
2934                         s_with_newlines[i] = '\n';
2935                 }
2936         }
2937         if ((output_methods & TO_STDOUT) && draw_mode == FG) {
2938                 printf("%s\n", s_with_newlines);
2939                 if (extra_newline) fputc('\n', stdout);
2940                 fflush(stdout); /* output immediately, don't buffer */
2941         }
2942         if ((output_methods & TO_STDERR) && draw_mode == FG) {
2943                 fprintf(stderr, "%s\n", s_with_newlines);
2944                 fflush(stderr); /* output immediately, don't buffer */
2945         }
2946         if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
2947                 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
2948         }
2949         if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
2950                 fprintf(append_fpointer, "%s\n", s_with_newlines);
2951         }
2952 #ifdef NCURSES
2953         if ((output_methods & TO_NCURSES) && draw_mode == FG) {
2954                 printw("%s", s_with_newlines);
2955         }
2956 #endif
2957         free(s_with_newlines);
2958         memset(tmpstring1, 0, text_buffer_size);
2959         memset(tmpstring2, 0, text_buffer_size);
2960         strncpy(tmpstring1, s, text_buffer_size - 1);
2961         pos = 0;
2962         added = 0;
2963
2964 #ifdef X11
2965         if (output_methods & TO_X) {
2966                 max = ((text_width - width_of_s) / get_string_width(" "));
2967         }
2968 #endif /* X11 */
2969         /* This code looks for tabs in the text and coverts them to spaces.
2970          * The trick is getting the correct number of spaces, and not going
2971          * over the window's size without forcing the window larger. */
2972         for (i = 0; i < (int) text_buffer_size; i++) {
2973                 if (tmpstring1[i] == '\t') {
2974                         i2 = 0;
2975                         for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
2976                                 /* guard against overrun */
2977                                 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
2978                                 added++;
2979                         }
2980                         pos += i2;
2981                 } else {
2982                         /* guard against overrun */
2983                         tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
2984                         pos++;
2985                 }
2986         }
2987 #ifdef X11
2988         if (output_methods & TO_X) {
2989                 if (text_width == maximum_width) {
2990                         /* this means the text is probably pushing the limit,
2991                          * so we'll chop it */
2992                         while (cur_x + get_string_width(tmpstring2) - text_start_x
2993                                         > maximum_width && strlen(tmpstring2) > 0) {
2994                                 tmpstring2[strlen(tmpstring2) - 1] = '\0';
2995                         }
2996                 }
2997         }
2998 #endif /* X11 */
2999         s = tmpstring2;
3000 #ifdef X11
3001         if (output_methods & TO_X) {
3002 #ifdef XFT
3003                 if (use_xft) {
3004                         XColor c;
3005                         XftColor c2;
3006
3007                         c.pixel = current_color;
3008                         XQueryColor(display, DefaultColormap(display, screen), &c);
3009
3010                         c2.pixel = c.pixel;
3011                         c2.color.red = c.red;
3012                         c2.color.green = c.green;
3013                         c2.color.blue = c.blue;
3014                         c2.color.alpha = fonts[selected_font].font_alpha;
3015                         if (utf8_mode) {
3016                                 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3017                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3018                         } else {
3019                                 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3020                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3021                         }
3022                 } else
3023 #endif
3024                 {
3025                         XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
3026                                 strlen(s));
3027                 }
3028                 cur_x += width_of_s;
3029         }
3030 #endif /* X11 */
3031         memcpy(tmpstring1, s, text_buffer_size);
3032 }
3033
3034 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
3035 {
3036 #ifdef X11
3037         int font_h;
3038         int cur_y_add = 0;
3039 #endif /* X11 */
3040         char *recurse = 0;
3041         char *p = s;
3042         int last_special_needed = -1;
3043         int orig_special_index = special_index;
3044
3045 #ifdef X11
3046         if (output_methods & TO_X) {
3047                 font_h = font_height();
3048                 cur_y += font_ascent();
3049         }
3050         cur_x = text_start_x;
3051 #endif /* X11 */
3052
3053         while (*p) {
3054                 if (*p == SECRIT_MULTILINE_CHAR) {
3055                         /* special newline marker for multiline objects */
3056                         recurse = p + 1;
3057                         *p = '\0';
3058                         break;
3059                 }
3060                 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
3061 #ifdef X11
3062                         int w = 0;
3063 #endif /* X11 */
3064
3065                         /* draw string before special, unless we're dealing multiline
3066                          * specials */
3067                         if (last_special_applied > -1) {
3068                                 special_index = last_special_applied;
3069                         } else {
3070                                 *p = '\0';
3071                                 draw_string(s);
3072                                 *p = SPECIAL_CHAR;
3073                                 s = p + 1;
3074                         }
3075                         /* draw special */
3076                         switch (specials[special_index].type) {
3077 #ifdef X11
3078                                 case HORIZONTAL_LINE:
3079                                 {
3080                                         int h = specials[special_index].height;
3081                                         int mid = font_ascent() / 2;
3082
3083                                         w = text_start_x + text_width - cur_x;
3084
3085                                         XSetLineAttributes(display, window.gc, h, LineSolid,
3086                                                 CapButt, JoinMiter);
3087                                         XDrawLine(display, window.drawable, window.gc, cur_x,
3088                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3089                                         break;
3090                                 }
3091
3092                                 case STIPPLED_HR:
3093                                 {
3094                                         int h = specials[special_index].height;
3095                                         int tmp_s = specials[special_index].arg;
3096                                         int mid = font_ascent() / 2;
3097                                         char ss[2] = { tmp_s, tmp_s };
3098
3099                                         w = text_start_x + text_width - cur_x - 1;
3100                                         XSetLineAttributes(display, window.gc, h, LineOnOffDash,
3101                                                 CapButt, JoinMiter);
3102                                         XSetDashes(display, window.gc, 0, ss, 2);
3103                                         XDrawLine(display, window.drawable, window.gc, cur_x,
3104                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3105                                         break;
3106                                 }
3107
3108                                 case BAR:
3109                                 {
3110                                         int h, bar_usage, by;
3111                                         if (cur_x - text_start_x > maximum_width
3112                                                         && maximum_width > 0) {
3113                                                 break;
3114                                         }
3115                                         h = specials[special_index].height;
3116                                         bar_usage = specials[special_index].arg;
3117                                         by = cur_y - (font_ascent() / 2) - 1;
3118
3119                                         if (h < font_h) {
3120                                                 by -= h / 2 - 1;
3121                                         }
3122                                         w = specials[special_index].width;
3123                                         if (w == 0) {
3124                                                 w = text_start_x + text_width - cur_x - 1;
3125                                         }
3126                                         if (w < 0) {
3127                                                 w = 0;
3128                                         }
3129
3130                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
3131                                                 CapButt, JoinMiter);
3132
3133                                         XDrawRectangle(display, window.drawable, window.gc, cur_x,
3134                                                 by, w, h);
3135                                         XFillRectangle(display, window.drawable, window.gc, cur_x,
3136                                                 by, w * bar_usage / 255, h);
3137                                         if (h > cur_y_add
3138                                                         && h > font_h) {
3139                                                 cur_y_add = h;
3140                                         }
3141                                         break;
3142                                 }
3143
3144                                 case GAUGE: /* new GAUGE  */
3145                                 {
3146                                         int h, by = 0;
3147                                         unsigned long last_colour = current_color;
3148 #ifdef MATH
3149                                         float angle, px, py;
3150                                         int usage;
3151 #endif /* MATH */
3152
3153                                         if (cur_x - text_start_x > maximum_width
3154                                                         && maximum_width > 0) {
3155                                                 break;
3156                                         }
3157
3158                                         h = specials[special_index].height;
3159                                         by = cur_y - (font_ascent() / 2) - 1;
3160
3161                                         if (h < font_h) {
3162                                                 by -= h / 2 - 1;
3163                                         }
3164                                         w = specials[special_index].width;
3165                                         if (w == 0) {
3166                                                 w = text_start_x + text_width - cur_x - 1;
3167                                         }
3168                                         if (w < 0) {
3169                                                 w = 0;
3170                                         }
3171
3172                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
3173                                                         CapButt, JoinMiter);
3174
3175                                         XDrawArc(display, window.drawable, window.gc,
3176                                                         cur_x, by, w, h * 2, 0, 180*64);
3177
3178 #ifdef MATH
3179                                         usage = specials[special_index].arg;
3180                                         angle = (M_PI)*(float)(usage)/255.;
3181                                         px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3182                                         py = (float)(by+(h))-(float)(h)*sin(angle);
3183
3184                                         XDrawLine(display, window.drawable, window.gc,
3185                                                         cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3186 #endif /* MATH */
3187
3188                                         if (h > cur_y_add
3189                                                         && h > font_h) {
3190                                                 cur_y_add = h;
3191                                         }
3192
3193                                         set_foreground_color(last_colour);
3194
3195                                         break;
3196
3197                                 }
3198
3199                                 case GRAPH:
3200                                 {
3201                                         int h, by, i = 0, j = 0;
3202                                         int colour_idx = 0;
3203                                         unsigned long last_colour = current_color;
3204                                         unsigned long *tmpcolour = 0;
3205                                         if (cur_x - text_start_x > maximum_width
3206                                                         && maximum_width > 0) {
3207                                                 break;
3208                                         }
3209                                         h = specials[special_index].height;
3210                                         by = cur_y - (font_ascent() / 2) - 1;
3211
3212                                         if (h < font_h) {
3213                                                 by -= h / 2 - 1;
3214                                         }
3215                                         w = specials[special_index].width;
3216                                         if (w == 0) {
3217                                                 w = text_start_x + text_width - cur_x - 1;
3218                                         }
3219                                         if (w < 0) {
3220                                                 w = 0;
3221                                         }
3222                                         if (draw_graph_borders) {
3223                                                 XSetLineAttributes(display, window.gc, 1, LineSolid,
3224                                                         CapButt, JoinMiter);
3225                                                 XDrawRectangle(display, window.drawable, window.gc,
3226                                                         cur_x, by, w, h);
3227                                         }
3228                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
3229                                                 CapButt, JoinMiter);
3230
3231                                         if (specials[special_index].last_colour != 0
3232                                                         || specials[special_index].first_colour != 0) {
3233                                                 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3234                                         }
3235                                         colour_idx = 0;
3236                                         for (i = w - 2; i > -1; i--) {
3237                                                 if (specials[special_index].last_colour != 0
3238                                                                 || specials[special_index].first_colour != 0) {
3239                                                         if (specials[special_index].tempgrad) {
3240 #ifdef DEBUG_lol
3241                                                                 assert(
3242                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3243                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3244                                                                                 < w - 1
3245                                                                           );
3246                                                                 assert(
3247                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3248                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3249                                                                                 > -1
3250                                                                           );
3251                                                                 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3252                                                                         assert(
3253                                                                                         (int)((float)(w - 2) - specials[special_index].graph[j] *
3254                                                                                                 (w - 2) / (float)specials[special_index].graph_scale)
3255                                                                                         == 0
3256                                                                                   );
3257                                                                 }
3258 #endif /* DEBUG_lol */
3259                                                                 XSetForeground(display, window.gc, tmpcolour[
3260                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
3261                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
3262                                                                                 ]);
3263                                                         } else {
3264                                                                 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3265                                                         }
3266                                                 }
3267                                                 /* this is mugfugly, but it works */
3268                                                 XDrawLine(display, window.drawable, window.gc,
3269                                                                 cur_x + i + 1, by + h, cur_x + i + 1,
3270                                                                 round_to_int((double)by + h - specials[special_index].graph[j] *
3271                                                                         (h - 1) / specials[special_index].graph_scale));
3272                                                 if ((w - i) / ((float) (w - 2) /
3273                                                                         (specials[special_index].graph_width)) > j
3274                                                                 && j < MAX_GRAPH_DEPTH - 3) {
3275                                                         j++;
3276                                                 }
3277                                         }
3278                                         if (tmpcolour) free(tmpcolour);
3279                                         if (h > cur_y_add
3280                                                         && h > font_h) {
3281                                                 cur_y_add = h;
3282                                         }
3283                                         /* if (draw_mode == BG) {
3284                                                 set_foreground_color(default_bg_color);
3285                                         } else if (draw_mode == OUTLINE) {
3286                                                 set_foreground_color(default_out_color);
3287                                         } else {
3288                                                 set_foreground_color(default_fg_color);
3289                                         } */
3290                                         if (show_graph_range) {
3291                                                 int tmp_x = cur_x;
3292                                                 int tmp_y = cur_y;
3293                                                 unsigned short int seconds = update_interval * w;
3294                                                 char *tmp_day_str;
3295                                                 char *tmp_hour_str;
3296                                                 char *tmp_min_str;
3297                                                 char *tmp_sec_str;
3298                                                 char *tmp_str;
3299                                                 unsigned short int timeunits;
3300                                                 if (seconds != 0) {
3301                                                         timeunits = seconds / 86400; seconds %= 86400;
3302                                                         if (timeunits > 0) {
3303                                                                 asprintf(&tmp_day_str, "%dd", timeunits);
3304                                                         } else {
3305                                                                 tmp_day_str = strdup("");
3306                                                         }
3307                                                         timeunits = seconds / 3600; seconds %= 3600;
3308                                                         if (timeunits > 0) {
3309                                                                 asprintf(&tmp_hour_str, "%dh", timeunits);
3310                                                         } else {
3311                                                                 tmp_hour_str = strdup("");
3312                                                         }
3313                                                         timeunits = seconds / 60; seconds %= 60;
3314                                                         if (timeunits > 0) {
3315                                                                 asprintf(&tmp_min_str, "%dm", timeunits);
3316                                                         } else {
3317                                                                 tmp_min_str = strdup("");
3318                                                         }
3319                                                         if (seconds > 0) {
3320                                                                 asprintf(&tmp_sec_str, "%ds", seconds);
3321                                                         } else {
3322                                                                 tmp_sec_str = strdup("");
3323                                                         }
3324                                                         asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3325                                                         free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3326                                                 } else {
3327                                                         asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3328                                                 }
3329                                                 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3330                                                 cur_y += font_h / 2;
3331                                                 draw_string(tmp_str);
3332                                                 free(tmp_str);
3333                                                 cur_x = tmp_x;
3334                                                 cur_y = tmp_y;
3335                                         }
3336 #ifdef MATH
3337                                         if (show_graph_scale && (specials[special_index].show_scale == 1)) {
3338                                                 int tmp_x = cur_x;
3339                                                 int tmp_y = cur_y;
3340                                                 char *tmp_str;
3341                                                 cur_x += font_ascent() / 2;
3342                                                 cur_y += font_h / 2;
3343                                                 tmp_str = (char *)
3344                                                         calloc(log10(floor(specials[special_index].graph_scale)) + 4,
3345                                                                         sizeof(char));
3346                                                 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
3347                                                 draw_string(tmp_str);
3348                                                 free(tmp_str);
3349                                                 cur_x = tmp_x;
3350                                                 cur_y = tmp_y;
3351                                         }
3352 #endif
3353                                         set_foreground_color(last_colour);
3354                                         break;
3355                                 }
3356
3357                                 case FONT:
3358                                 {
3359                                         int old = font_ascent();
3360
3361                                         cur_y -= font_ascent();
3362                                         selected_font = specials[special_index].font_added;
3363                                         set_font();
3364                                         if (cur_y + font_ascent() < cur_y + old) {
3365                                                 cur_y += old;
3366                                         } else {
3367                                                 cur_y += font_ascent();
3368                                         }
3369                                         font_h = font_height();
3370                                         break;
3371                                 }
3372 #endif /* X11 */
3373                                 case FG:
3374                                         if (draw_mode == FG) {
3375                                                 set_foreground_color(specials[special_index].arg);
3376                                         }
3377                                         break;
3378
3379 #ifdef X11
3380                                 case BG:
3381                                         if (draw_mode == BG) {
3382                                                 set_foreground_color(specials[special_index].arg);
3383                                         }
3384                                         break;
3385
3386                                 case OUTLINE:
3387                                         if (draw_mode == OUTLINE) {
3388                                                 set_foreground_color(specials[special_index].arg);
3389                                         }
3390                                         break;
3391
3392                                 case OFFSET:
3393                                         w += specials[special_index].arg;
3394                                         last_special_needed = special_index;
3395                                         break;
3396
3397                                 case VOFFSET:
3398                                         cur_y += specials[special_index].arg;
3399                                         break;
3400
3401                                 case GOTO:
3402                                         if (specials[special_index].arg >= 0) {
3403                                                 cur_x = (int) specials[special_index].arg;
3404                                         }
3405                                         last_special_needed = special_index;
3406                                         break;
3407
3408                                 case TAB:
3409                                 {
3410                                         int start = specials[special_index].arg;
3411                                         int step = specials[special_index].width;
3412
3413                                         if (!step || step < 0) {
3414                                                 step = 10;
3415                                         }
3416                                         w = step - (cur_x - text_start_x - start) % step;
3417                                         last_special_needed = special_index;
3418                                         break;
3419                                 }
3420
3421                                 case ALIGNR:
3422                                 {
3423                                         /* TODO: add back in "+ window.border_inner_margin" to the end of
3424                                          * this line? */
3425                                         int pos_x = text_start_x + text_width -
3426                                                 get_string_width_special(s, special_index);
3427
3428                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3429                                                 "get_string_width(p) %i gap_x %i "
3430                                                 "specials[special_index].arg %i window.border_inner_margin %i "
3431                                                 "window.border_width %i\n", pos_x, text_start_x, text_width,
3432                                                 cur_x, get_string_width_special(s), gap_x,
3433                                                 specials[special_index].arg, window.border_inner_margin,
3434                                                 window.border_width); */
3435                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3436                                                 cur_x = pos_x - specials[special_index].arg;
3437                                         }
3438                                         last_special_needed = special_index;
3439                                         break;
3440                                 }
3441
3442                                 case ALIGNC:
3443                                 {
3444                                         int pos_x = (text_width) / 2 - get_string_width_special(s,
3445                                                         special_index) / 2 - (cur_x -
3446                                                                 text_start_x);
3447                                         /* int pos_x = text_start_x + text_width / 2 -
3448                                                 get_string_width_special(s) / 2; */
3449
3450                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
3451                                                 "get_string_width(p) %i gap_x %i "
3452                                                 "specials[special_index].arg %i\n", pos_x, text_start_x,
3453                                                 text_width, cur_x, get_string_width(s), gap_x,
3454                                                 specials[special_index].arg); */
3455                                         if (pos_x > specials[special_index].arg) {
3456                                                 w = pos_x - specials[special_index].arg;
3457                                         }
3458                                         last_special_needed = special_index;
3459                                         break;
3460                                 }
3461 #endif /* X11 */
3462                         }
3463
3464 #ifdef X11
3465                         cur_x += w;
3466 #endif /* X11 */
3467
3468                         if (special_index != last_special_applied) {
3469                                 special_index++;
3470                         } else {
3471                                 special_index = orig_special_index;
3472                                 last_special_applied = -1;
3473                         }
3474                 }
3475                 p++;
3476         }
3477
3478 #ifdef X11
3479         cur_y += cur_y_add;
3480 #endif /* X11 */
3481         draw_string(s);
3482 #ifdef NCURSES
3483         if (output_methods & TO_NCURSES) {
3484                 printw("\n");
3485         }
3486 #endif /* NCURSES */
3487 #ifdef X11
3488         if (output_methods & TO_X)
3489                 cur_y += font_descent();
3490 #endif /* X11 */
3491         if (recurse && *recurse) {
3492                 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
3493                 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
3494         }
3495         return special_index;
3496 }
3497
3498 static int draw_line(char *s, int special_index)
3499 {
3500 #ifdef X11
3501         if (output_methods & TO_X) {
3502                 return draw_each_line_inner(s, special_index, -1);
3503         }
3504 #endif /* X11 */
3505 #ifdef NCURSES
3506         if (output_methods & TO_NCURSES) {
3507                 return draw_each_line_inner(s, special_index, -1);
3508         }
3509 #endif /* NCURSES */
3510         draw_string(s);
3511         UNUSED(special_index);
3512         return 0;
3513 }
3514
3515 static void draw_text(void)
3516 {
3517 #ifdef X11
3518 #ifdef HAVE_LUA
3519         llua_draw_pre_hook();
3520 #endif /* HAVE_LUA */
3521         if (output_methods & TO_X) {
3522                 cur_y = text_start_y;
3523
3524                 /* draw borders */
3525                 if (draw_borders && window.border_width > 0) {
3526                         if (stippled_borders) {
3527                                 char ss[2] = { stippled_borders, stippled_borders };
3528                                 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
3529                                         CapButt, JoinMiter);
3530                                 XSetDashes(display, window.gc, 0, ss, 2);
3531                         } else {
3532                                 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
3533                                         CapButt, JoinMiter);
3534                         }
3535
3536                         XDrawRectangle(display, window.drawable, window.gc,
3537                                 text_start_x - window.border_inner_margin - window.border_width,
3538                                 text_start_y - window.border_inner_margin - window.border_width,
3539                                 text_width + window.border_inner_margin * 2 + window.border_width * 2,
3540                                 text_height + window.border_inner_margin * 2 + window.border_width * 2);
3541                 }
3542
3543                 /* draw text */
3544         }
3545         setup_fonts();
3546 #endif /* X11 */
3547 #ifdef NCURSES
3548         init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
3549         attron(COLOR_PAIR(COLOR_WHITE));
3550 #endif /* NCURSES */
3551         for_each_line(text_buffer, draw_line);
3552 #if defined(HAVE_LUA) && defined(X11)
3553         llua_draw_post_hook();
3554 #endif /* HAVE_LUA */
3555 }
3556
3557 static void draw_stuff(void)
3558 {
3559 #ifdef IMLIB2
3560         cimlib_render(text_start_x, text_start_y, window.width, window.height);
3561 #endif /* IMLIB2 */
3562         if (overwrite_file) {
3563                 overwrite_fpointer = fopen(overwrite_file, "w");
3564                 if(!overwrite_fpointer)
3565                         NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
3566         }
3567         if (append_file) {
3568                 append_fpointer = fopen(append_file, "a");
3569                 if(!append_fpointer)
3570                         NORM_ERR("Can't append '%s' anymore", append_file);
3571         }
3572 #ifdef X11
3573         if (output_methods & TO_X) {
3574                 selected_font = 0;
3575                 if (draw_shades && !draw_outline) {
3576                         text_start_x++;
3577                         text_start_y++;
3578                         set_foreground_color(default_bg_color);
3579                         draw_mode = BG;
3580                         draw_text();
3581                         text_start_x--;
3582                         text_start_y--;
3583                 }
3584
3585                 if (draw_outline) {
3586                         int i, j;
3587                         selected_font = 0;
3588
3589                         for (i = -1; i < 2; i++) {
3590                                 for (j = -1; j < 2; j++) {
3591                                         if (i == 0 && j == 0) {
3592                                                 continue;
3593                                         }
3594                                         text_start_x += i;
3595                                         text_start_y += j;
3596                                         set_foreground_color(default_out_color);
3597                                         draw_mode = OUTLINE;
3598                                         draw_text();
3599                                         text_start_x -= i;
3600                                         text_start_y -= j;
3601                                 }
3602                         }
3603                 }
3604
3605                 set_foreground_color(default_fg_color);
3606         }
3607 #endif /* X11 */
3608         draw_mode = FG;
3609         draw_text();
3610 #if defined(X11) && defined(HAVE_XDBE)
3611         if (output_methods & TO_X) {
3612                 xdbe_swap_buffers();
3613         }
3614 #endif /* X11 && HAVE_XDBE */
3615         if(overwrite_fpointer) {
3616                 fclose(overwrite_fpointer);
3617                 overwrite_fpointer = 0;
3618         }
3619         if(append_fpointer) {
3620                 fclose(append_fpointer);
3621                 append_fpointer = 0;
3622         }
3623 }
3624
3625 #ifdef X11
3626 static void clear_text(int exposures)
3627 {
3628 #ifdef HAVE_XDBE
3629         if (use_xdbe) {
3630                 /* The swap action is XdbeBackground, which clears */
3631                 return;
3632         } else
3633 #endif
3634         if (display && window.window) { // make sure these are !null
3635                 /* there is some extra space for borders and outlines */
3636                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
3637                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
3638                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
3639                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
3640         }
3641 }
3642 #endif /* X11 */
3643
3644 static int need_to_update;
3645
3646 /* update_text() generates new text and clears old text area */
3647 static void update_text(void)
3648 {
3649 #ifdef IMLIB2
3650         cimlib_cleanup();
3651 #endif /* IMLIB2 */
3652         generate_text();
3653 #ifdef X11
3654         if (output_methods & TO_X)
3655                 clear_text(1);
3656 #endif /* X11 */
3657         need_to_update = 1;
3658 #ifdef HAVE_LUA
3659         llua_update_info(&info, update_interval);
3660 #endif /* HAVE_LUA */
3661 }
3662
3663 #ifdef HAVE_SYS_INOTIFY_H
3664 int inotify_fd;
3665 #endif
3666
3667 static void main_loop(void)
3668 {
3669         int terminate = 0;
3670 #ifdef SIGNAL_BLOCKING
3671         sigset_t newmask, oldmask;
3672 #endif
3673         double t;
3674 #ifdef HAVE_SYS_INOTIFY_H
3675         int inotify_config_wd = -1;
3676 #define INOTIFY_EVENT_SIZE  (sizeof(struct inotify_event))
3677 #define INOTIFY_BUF_LEN     (20 * (INOTIFY_EVENT_SIZE + 16))
3678         char inotify_buff[INOTIFY_BUF_LEN];
3679 #endif /* HAVE_SYS_INOTIFY_H */
3680
3681
3682 #ifdef SIGNAL_BLOCKING
3683         sigemptyset(&newmask);
3684         sigaddset(&newmask, SIGINT);
3685         sigaddset(&newmask, SIGTERM);
3686         sigaddset(&newmask, SIGUSR1);
3687 #endif
3688
3689         last_update_time = 0.0;
3690         next_update_time = get_time();
3691         info.looped = 0;
3692         while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
3693                 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
3694                         char buf[max_user_text];
3695
3696                         get_battery_short_status(buf, max_user_text, "BAT0");
3697                         if(buf[0] == 'D') {
3698                                 update_interval = update_interval_bat;
3699                         } else {
3700                                 update_interval = update_interval_old;
3701                         }
3702                 }
3703                 info.looped++;
3704
3705 #ifdef SIGNAL_BLOCKING
3706                 /* block signals.  we will inspect for pending signals later */
3707                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
3708                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
3709                 }
3710 #endif
3711
3712 #ifdef X11
3713                 if (output_methods & TO_X) {
3714                         XFlush(display);
3715
3716                         /* wait for X event or timeout */
3717
3718                         if (!XPending(display)) {
3719                                 fd_set fdsr;
3720                                 struct timeval tv;
3721                                 int s;
3722                                 t = next_update_time - get_time();
3723
3724                                 if (t < 0) {
3725                                         t = 0;
3726                                 } else if (t > update_interval) {
3727                                         t = update_interval;
3728                                 }
3729
3730                                 tv.tv_sec = (long) t;
3731                                 tv.tv_usec = (long) (t * 1000000) % 1000000;
3732                                 FD_ZERO(&fdsr);
3733                                 FD_SET(ConnectionNumber(display), &fdsr);
3734
3735                                 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
3736                                 if (s == -1) {
3737                                         if (errno != EINTR) {
3738                                                 NORM_ERR("can't select(): %s", strerror(errno));
3739                                         }
3740                                 } else {
3741                                         /* timeout */
3742                                         if (s == 0) {
3743                                                 update_text();
3744                                         }
3745                                 }
3746                         }
3747
3748                         if (need_to_update) {
3749 #ifdef OWN_WINDOW
3750                                 int wx = window.x, wy = window.y;
3751 #endif
3752
3753                                 need_to_update = 0;
3754                                 selected_font = 0;
3755                                 update_text_area();
3756 #ifdef OWN_WINDOW
3757                                 if (own_window) {
3758                                         int changed = 0;
3759
3760                                         /* resize window if it isn't right size */
3761                                         if (!fixed_size
3762                                                         && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
3763                                                                 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
3764                                                 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3765                                                 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3766                                                 draw_stuff(); /* redraw everything in our newly sized window */
3767                                                 XResizeWindow(display, window.window, window.width,
3768                                                                 window.height); /* resize window */
3769                                                 set_transparent_background(window.window);
3770 #ifdef HAVE_XDBE
3771                                                 /* swap buffers */
3772                                                 xdbe_swap_buffers();
3773 #endif
3774
3775                                                 changed++;
3776 #ifdef HAVE_LUA
3777                                                 /* update lua window globals */
3778                                                 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3779 #endif /* HAVE_LUA */
3780                                         }
3781
3782                                         /* move window if it isn't in right position */
3783                                         if (!fixed_pos && (window.x != wx || window.y != wy)) {
3784                                                 XMoveWindow(display, window.window, window.x, window.y);
3785                                                 changed++;
3786                                         }
3787
3788                                         /* update struts */
3789                                         if (changed && window.type == TYPE_PANEL) {
3790                                                 int sidenum = -1;
3791
3792                                                 fprintf(stderr, PACKAGE_NAME": defining struts\n");
3793                                                 fflush(stderr);
3794
3795                                                 switch (text_alignment) {
3796                                                         case TOP_LEFT:
3797                                                         case TOP_RIGHT:
3798                                                         case TOP_MIDDLE:
3799                                                                 {
3800                                                                         sidenum = 2;
3801                                                                         break;
3802                                                                 }
3803                                                         case BOTTOM_LEFT:
3804                                                         case BOTTOM_RIGHT:
3805                                                         case BOTTOM_MIDDLE:
3806                                                                 {
3807                                                                         sidenum = 3;
3808                                                                         break;
3809                                                                 }
3810                                                         case MIDDLE_LEFT:
3811                                                                 {
3812                                                                         sidenum = 0;
3813                                                                         break;
3814                                                                 }
3815                                                         case MIDDLE_RIGHT:
3816                                                                 {
3817                                                                         sidenum = 1;
3818                                                                         break;
3819                                                                 }
3820                                                 }
3821
3822                                                 set_struts(sidenum);
3823                                         }
3824                                 }
3825 #endif
3826
3827                                 clear_text(1);
3828
3829 #ifdef HAVE_XDBE
3830                                 if (use_xdbe) {
3831                                         XRectangle r;
3832
3833                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3834                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3835                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3836                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3837                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3838                                 }
3839 #endif
3840                         }
3841
3842                         /* handle X events */
3843                         while (XPending(display)) {
3844                                 XEvent ev;
3845
3846                                 XNextEvent(display, &ev);
3847                                 switch (ev.type) {
3848                                         case Expose:
3849                                         {
3850                                                 XRectangle r;
3851                                                 r.x = ev.xexpose.x;
3852                                                 r.y = ev.xexpose.y;
3853                                                 r.width = ev.xexpose.width;
3854                                                 r.height = ev.xexpose.height;
3855                                                 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3856                                                 break;
3857                                         }
3858
3859                                         case PropertyNotify:
3860                                         {
3861                                                 if ( ev.xproperty.state == PropertyNewValue ) {
3862                                                         get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
3863                                                 }
3864                                                 break;
3865                                         }
3866
3867 #ifdef OWN_WINDOW
3868                                         case ReparentNotify:
3869                                                 /* set background to ParentRelative for all parents */
3870                                                 if (own_window) {
3871                                                         set_transparent_background(window.window);
3872                                                 }
3873                                                 break;
3874
3875                                         case ConfigureNotify:
3876                                                 if (own_window) {
3877                                                         /* if window size isn't what expected, set fixed size */
3878                                                         if (ev.xconfigure.width != window.width
3879                                                                         || ev.xconfigure.height != window.height) {
3880                                                                 if (window.width != 0 && window.height != 0) {
3881                                                                         fixed_size = 1;
3882                                                                 }
3883
3884                                                                 /* clear old stuff before screwing up
3885                                                                  * size and pos */
3886                                                                 clear_text(1);
3887
3888                                                                 {
3889                                                                         XWindowAttributes attrs;
3890                                                                         if (XGetWindowAttributes(display,
3891                                                                                         window.window, &attrs)) {
3892                                                                                 window.width = attrs.width;
3893                                                                                 window.height = attrs.height;
3894                                                                         }
3895                                                                 }
3896
3897                                                                 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3898                                                                 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
3899                                                                 if (text_width > maximum_width
3900                                                                                 && maximum_width > 0) {
3901                                                                         text_width = maximum_width;
3902                                                                 }
3903                                                         }
3904
3905                                                         /* if position isn't what expected, set fixed pos
3906                                                          * total_updates avoids setting fixed_pos when window
3907                                                          * is set to weird locations when started */
3908                                                         /* // this is broken
3909                                                         if (total_updates >= 2 && !fixed_pos
3910                                                                         && (window.x != ev.xconfigure.x
3911                                                                         || window.y != ev.xconfigure.y)
3912                                                                         && (ev.xconfigure.x != 0
3913                                                                         || ev.xconfigure.y != 0)) {
3914                                                                 fixed_pos = 1;
3915                                                         } */
3916                                                 }
3917                                                 break;
3918
3919                                         case ButtonPress:
3920                                                 if (own_window) {
3921                                                         /* if an ordinary window with decorations */
3922                                                         if ((window.type == TYPE_NORMAL &&
3923                                                                                 (!TEST_HINT(window.hints,
3924                                                                                                         HINT_UNDECORATED))) ||
3925                                                                         window.type == TYPE_DESKTOP) {
3926                                                                 /* allow conky to hold input focus. */
3927                                                                 break;
3928                                                         } else {
3929                                                                 /* forward the click to the desktop window */
3930                                                                 XUngrabPointer(display, ev.xbutton.time);
3931                                                                 ev.xbutton.window = window.desktop;
3932                                                                 ev.xbutton.x = ev.xbutton.x_root;
3933                                                                 ev.xbutton.y = ev.xbutton.y_root;
3934                                                                 XSendEvent(display, ev.xbutton.window, False,
3935                                                                         ButtonPressMask, &ev);
3936                                                                 XSetInputFocus(display, ev.xbutton.window,
3937                                                                         RevertToParent, ev.xbutton.time);
3938                                                         }
3939                                                 }
3940                                                 break;
3941
3942                                         case ButtonRelease:
3943                                                 if (own_window) {
3944                                                         /* if an ordinary window with decorations */
3945                                                         if ((window.type == TYPE_NORMAL)
3946                                                                         && (!TEST_HINT(window.hints,
3947                                                                         HINT_UNDECORATED))) {
3948                                                                 /* allow conky to hold input focus. */
3949                                                                 break;
3950                                                         } else {
3951                                                                 /* forward the release to the desktop window */
3952                                                                 ev.xbutton.window = window.desktop;
3953                                                                 ev.xbutton.x = ev.xbutton.x_root;
3954                                                                 ev.xbutton.y = ev.xbutton.y_root;
3955                                                                 XSendEvent(display, ev.xbutton.window, False,
3956                                                                         ButtonReleaseMask, &ev);
3957                                                         }
3958                                                 }
3959                                                 break;
3960
3961 #endif
3962
3963                                         default:
3964 #ifdef HAVE_XDAMAGE
3965                                                 if (ev.type == x11_stuff.event_base + XDamageNotify) {
3966                                                         XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
3967
3968                                                         XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
3969                                                         XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
3970                                                 }
3971 #endif /* HAVE_XDAMAGE */
3972                                                 break;
3973                                 }
3974                         }
3975
3976 #ifdef HAVE_XDAMAGE
3977                         XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
3978                         XFixesSetRegion(display, x11_stuff.region2, 0, 0);
3979 #endif /* HAVE_XDAMAGE */
3980
3981                         /* XDBE doesn't seem to provide a way to clear the back buffer
3982                          * without interfering with the front buffer, other than passing
3983                          * XdbeBackground to XdbeSwapBuffers. That means that if we're
3984                          * using XDBE, we need to redraw the text even if it wasn't part of
3985                          * the exposed area. OTOH, if we're not going to call draw_stuff at
3986                          * all, then no swap happens and we can safely do nothing. */
3987
3988                         if (!XEmptyRegion(x11_stuff.region)) {
3989 #ifdef HAVE_XDBE
3990                                 if (use_xdbe) {
3991                                         XRectangle r;
3992
3993                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3994                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3995                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3996                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
3997                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
3998                                 }
3999 #endif
4000                                 XSetRegion(display, window.gc, x11_stuff.region);
4001 #ifdef XFT
4002                                 if (use_xft) {
4003                                         XftDrawSetClip(window.xftdraw, x11_stuff.region);
4004                                 }
4005 #endif
4006                                 draw_stuff();
4007                                 XDestroyRegion(x11_stuff.region);
4008                                 x11_stuff.region = XCreateRegion();
4009                         }
4010                 } else {
4011 #endif /* X11 */
4012                         t = (next_update_time - get_time()) * 1000000;
4013                         if(t > 0) usleep((useconds_t)t);
4014                         update_text();
4015                         draw_stuff();
4016 #ifdef NCURSES
4017                         if(output_methods & TO_NCURSES) {
4018                                 refresh();
4019                                 clear();
4020                         }
4021 #endif
4022 #ifdef X11
4023                 }
4024 #endif /* X11 */
4025
4026 #ifdef SIGNAL_BLOCKING
4027                 /* unblock signals of interest and let handler fly */
4028                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4029                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4030                 }
4031 #endif
4032
4033                 switch (g_signal_pending) {
4034                         case SIGHUP:
4035                         case SIGUSR1:
4036                                 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4037                                 reload_config();
4038                                 break;
4039                         case SIGINT:
4040                         case SIGTERM:
4041                                 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4042                                 terminate = 1;
4043 #ifdef X11
4044                                 if (output_methods & TO_X) {
4045                                         XDestroyRegion(x11_stuff.region);
4046                                         x11_stuff.region = NULL;
4047 #ifdef HAVE_XDAMAGE
4048                                         XDamageDestroy(display, x11_stuff.damage);
4049                                         XFixesDestroyRegion(display, x11_stuff.region2);
4050                                         XFixesDestroyRegion(display, x11_stuff.part);
4051 #endif /* HAVE_XDAMAGE */
4052                                         if (disp) {
4053                                                 free(disp);
4054                                         }
4055                                 }
4056 #endif /* X11 */
4057                                 if(overwrite_file) {
4058                                         free(overwrite_file);
4059                                         overwrite_file = 0;
4060                                 }
4061                                 if(append_file) {
4062                                         free(append_file);
4063                                         append_file = 0;
4064                                 }
4065                                 break;
4066                         default:
4067                                 /* Reaching here means someone set a signal
4068                                  * (SIGXXXX, signal_handler), but didn't write any code
4069                                  * to deal with it.
4070                                  * If you don't want to handle a signal, don't set a handler on
4071                                  * it in the first place. */
4072                                 if (g_signal_pending) {
4073                                         NORM_ERR("ignoring signal (%d)", g_signal_pending);
4074                                 }
4075                                 break;
4076                 }
4077 #ifdef HAVE_SYS_INOTIFY_H
4078                 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4079                         inotify_config_wd = inotify_add_watch(inotify_fd,
4080                                         current_config,
4081                                         IN_MODIFY);
4082                 }
4083                 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4084                         int len = 0, idx = 0;
4085                         fd_set descriptors;
4086                         struct timeval time_to_wait;
4087
4088                         FD_ZERO(&descriptors);
4089                         FD_SET(inotify_fd, &descriptors);
4090
4091                         time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4092
4093                         select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4094                         if (FD_ISSET(inotify_fd, &descriptors)) {
4095                                 /* process inotify events */
4096                                 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4097                                 while (len > 0 && idx < len) {
4098                                         struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4099                                         if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4100                                                 /* current_config should be reloaded */
4101                                                 NORM_ERR("'%s' modified, reloading...", current_config);
4102                                                 reload_config();
4103                                                 if (ev->mask & IN_IGNORED) {
4104                                                         /* for some reason we get IN_IGNORED here
4105                                                          * sometimes, so we need to re-add the watch */
4106                                                         inotify_config_wd = inotify_add_watch(inotify_fd,
4107                                                                         current_config,
4108                                                                         IN_MODIFY);
4109                                                 }
4110                                         }
4111 #ifdef HAVE_LUA
4112                                         else {
4113                                                 llua_inotify_query(ev->wd, ev->mask);
4114                                         }
4115 #endif /* HAVE_LUA */
4116                                         idx += INOTIFY_EVENT_SIZE + ev->len;
4117                                 }
4118                         }
4119                 }
4120 #endif /* HAVE_SYS_INOTIFY_H */
4121
4122 #ifdef HAVE_LUA
4123                 llua_update_info(&info, update_interval);
4124 #endif /* HAVE_LUA */
4125                 g_signal_pending = 0;
4126         }
4127         clean_up(NULL, NULL);
4128
4129 #ifdef HAVE_SYS_INOTIFY_H
4130         if (inotify_fd != -1) {
4131                 inotify_rm_watch(inotify_fd, inotify_config_wd);
4132                 close(inotify_fd);
4133                 inotify_fd = inotify_config_wd = 0;
4134         }
4135 #endif /* HAVE_SYS_INOTIFY_H */
4136 }
4137
4138 #ifdef X11
4139 static void load_config_file_x11(const char *);
4140 #endif /* X11 */
4141 void initialisation(int argc, char** argv);
4142
4143         /* reload the config file */
4144 static void reload_config(void)
4145 {
4146         char *current_config_copy = strdup(current_config);
4147         clean_up(NULL, NULL);
4148         current_config = current_config_copy;
4149         initialisation(argc_copy, argv_copy);
4150 }
4151
4152 void clean_up(void *memtofree1, void* memtofree2)
4153 {
4154         int i;
4155
4156 #ifdef NCURSES
4157         if(output_methods & TO_NCURSES) {
4158                 endwin();
4159         }
4160 #endif
4161         conftree_empty(currentconffile);
4162         currentconffile = NULL;
4163         if(memtofree1) {
4164                 free(memtofree1);
4165         }
4166         if(memtofree2) {
4167                 free(memtofree2);
4168         }
4169         timed_thread_destroy_registered_threads();
4170
4171         if (info.cpu_usage) {
4172                 free(info.cpu_usage);
4173                 info.cpu_usage = NULL;
4174         }
4175 #ifdef X11
4176         if (x_initialised == YES) {
4177                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4178                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4179                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4180                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4181                 destroy_window();
4182                 free_fonts();
4183                 if(x11_stuff.region) {
4184                         XDestroyRegion(x11_stuff.region);
4185                         x11_stuff.region = NULL;
4186                 }
4187                 XCloseDisplay(display);
4188                 display = NULL;
4189                 if(info.x11.desktop.all_names) {
4190                         free(info.x11.desktop.all_names);
4191                         info.x11.desktop.all_names = NULL;
4192                 }
4193                 if (info.x11.desktop.name) {
4194                         free(info.x11.desktop.name);
4195                         info.x11.desktop.name = NULL;
4196                 }
4197                 x_initialised = NO;
4198         }else{
4199                 free(fonts);    //in set_default_configurations a font is set but not loaded
4200                 font_count = -1;
4201         }
4202
4203 #endif /* X11 */
4204
4205         free_update_callbacks();
4206
4207         free_templates();
4208
4209         free_text_objects(&global_root_object, 0);
4210         if (tmpstring1) {
4211                 free(tmpstring1);
4212                 tmpstring1 = 0;
4213         }
4214         if (tmpstring2) {
4215                 free(tmpstring2);
4216                 tmpstring2 = 0;
4217         }
4218         if (text_buffer) {
4219                 free(text_buffer);
4220                 text_buffer = 0;
4221         }
4222
4223         if (global_text) {
4224                 free(global_text);
4225                 global_text = 0;
4226         }
4227
4228         free(current_config);
4229         current_config = 0;
4230
4231 #ifdef TCP_PORT_MONITOR
4232         tcp_portmon_clear();
4233 #endif
4234 #ifdef HAVE_CURL
4235         ccurl_free_info();
4236 #endif
4237 #ifdef RSS
4238         rss_free_info();
4239 #endif
4240 #ifdef WEATHER
4241         weather_free_info();
4242 #endif
4243 #ifdef HAVE_LUA
4244         llua_shutdown_hook();
4245         llua_close();
4246 #endif /* HAVE_LUA */
4247 #ifdef IMLIB2
4248         if (output_methods & TO_X)
4249                 cimlib_deinit();
4250 #endif /* IMLIB2 */
4251 #ifdef XOAP
4252         xmlCleanupParser();
4253 #endif /* XOAP */
4254
4255         if (specials) {
4256                 for (i = 0; i < special_count; i++) {
4257                         if (specials[i].type == GRAPH) {
4258                                 free(specials[i].graph);
4259                         }
4260                 }
4261                 free(specials);
4262                 specials = NULL;
4263         }
4264
4265         clear_net_stats();
4266         clear_diskio_stats();
4267         if(global_cpu != NULL) {
4268                 free(global_cpu);
4269                 global_cpu = NULL;
4270         }
4271 }
4272
4273 static int string_to_bool(const char *s)
4274 {
4275         if (!s) {
4276                 // Assumes an option without a true/false means true
4277                 return 1;
4278         } else if (strcasecmp(s, "yes") == EQUAL) {
4279                 return 1;
4280         } else if (strcasecmp(s, "true") == EQUAL) {
4281                 return 1;
4282         } else if (strcasecmp(s, "1") == EQUAL) {
4283                 return 1;
4284         }
4285         return 0;
4286 }
4287
4288 #ifdef X11
4289 static enum alignment string_to_alignment(const char *s)
4290 {
4291         if (strcasecmp(s, "top_left") == EQUAL) {
4292                 return TOP_LEFT;
4293         } else if (strcasecmp(s, "top_right") == EQUAL) {
4294                 return TOP_RIGHT;
4295         } else if (strcasecmp(s, "top_middle") == EQUAL) {
4296                 return TOP_MIDDLE;
4297         } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4298                 return BOTTOM_LEFT;
4299         } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4300                 return BOTTOM_RIGHT;
4301         } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4302                 return BOTTOM_MIDDLE;
4303         } else if (strcasecmp(s, "middle_left") == EQUAL) {
4304                 return MIDDLE_LEFT;
4305         } else if (strcasecmp(s, "middle_right") == EQUAL) {
4306                 return MIDDLE_RIGHT;
4307         } else if (strcasecmp(s, "middle_middle") == EQUAL) {
4308                 return MIDDLE_MIDDLE;
4309         } else if (strcasecmp(s, "tl") == EQUAL) {
4310                 return TOP_LEFT;
4311         } else if (strcasecmp(s, "tr") == EQUAL) {
4312                 return TOP_RIGHT;
4313         } else if (strcasecmp(s, "tm") == EQUAL) {
4314                 return TOP_MIDDLE;
4315         } else if (strcasecmp(s, "bl") == EQUAL) {
4316                 return BOTTOM_LEFT;
4317         } else if (strcasecmp(s, "br") == EQUAL) {
4318                 return BOTTOM_RIGHT;
4319         } else if (strcasecmp(s, "bm") == EQUAL) {
4320                 return BOTTOM_MIDDLE;
4321         } else if (strcasecmp(s, "ml") == EQUAL) {
4322                 return MIDDLE_LEFT;
4323         } else if (strcasecmp(s, "mr") == EQUAL) {
4324                 return MIDDLE_RIGHT;
4325         } else if (strcasecmp(s, "mm") == EQUAL) {
4326                 return MIDDLE_MIDDLE;
4327         } else if (strcasecmp(s, "none") == EQUAL) {
4328                 return NONE;
4329         }
4330         return TOP_LEFT;
4331 }
4332 #endif /* X11 */
4333
4334 #ifdef X11
4335 static void set_default_configurations_for_x(void)
4336 {
4337         default_fg_color = WhitePixel(display, screen);
4338         default_bg_color = BlackPixel(display, screen);
4339         default_out_color = BlackPixel(display, screen);
4340         color0 = default_fg_color;
4341         color1 = default_fg_color;
4342         color2 = default_fg_color;
4343         color3 = default_fg_color;
4344         color4 = default_fg_color;
4345         color5 = default_fg_color;
4346         color6 = default_fg_color;
4347         color7 = default_fg_color;
4348         color8 = default_fg_color;
4349         color9 = default_fg_color;
4350         current_text_color = default_fg_color;
4351 }
4352 #endif /* X11 */
4353
4354 static void set_default_configurations(void)
4355 {
4356 #ifdef MPD
4357         char *mpd_env_host;
4358         char *mpd_env_port;
4359 #endif
4360         update_uname();
4361         fork_to_background = 0;
4362         total_run_times = 0;
4363         info.cpu_avg_samples = 2;
4364         info.net_avg_samples = 2;
4365         info.diskio_avg_samples = 2;
4366         info.memmax = 0;
4367         top_cpu = 0;
4368         cpu_separate = 0;
4369         short_units = 0;
4370         format_human_readable = 1;
4371         top_mem = 0;
4372         top_time = 0;
4373 #ifdef IOSTATS
4374         top_io = 0;
4375 #endif
4376 #ifdef __linux__
4377         top_running = 0;
4378 #endif
4379 #ifdef MPD
4380         mpd_env_host = getenv("MPD_HOST");
4381         mpd_env_port = getenv("MPD_PORT");
4382
4383         if (!mpd_env_host || !strlen(mpd_env_host)) {
4384                 mpd_set_host("localhost");
4385         } else {
4386                 /* MPD_HOST environment variable is set */
4387                 char *mpd_hostpart = strchr(mpd_env_host, '@');
4388                 if (!mpd_hostpart) {
4389                         mpd_set_host(mpd_env_host);
4390                 } else {
4391                         /* MPD_HOST contains a password */
4392                         char mpd_password[mpd_hostpart - mpd_env_host + 1];
4393                         snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
4394
4395                         if (!strlen(mpd_hostpart + 1)) {
4396                                 mpd_set_host("localhost");
4397                         } else {
4398                                 mpd_set_host(mpd_hostpart + 1);
4399                         }
4400
4401                         mpd_set_password(mpd_password, 1);
4402                 }
4403         }
4404
4405
4406         if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
4407                 /* failed to set port from environment variable */
4408                 mpd_set_port("6600");
4409         }
4410 #endif
4411 #ifdef XMMS2
4412         info.xmms2.artist = NULL;
4413         info.xmms2.album = NULL;
4414         info.xmms2.title = NULL;
4415         info.xmms2.genre = NULL;
4416         info.xmms2.comment = NULL;
4417         info.xmms2.url = NULL;
4418         info.xmms2.status = NULL;
4419         info.xmms2.playlist = NULL;
4420 #endif
4421         use_spacer = NO_SPACER;
4422 #ifdef X11
4423         output_methods = TO_X;
4424 #else
4425         output_methods = TO_STDOUT;
4426 #endif
4427 #ifdef X11
4428         show_graph_scale = 0;
4429         show_graph_range = 0;
4430         draw_shades = 1;
4431         draw_borders = 0;
4432         draw_graph_borders = 1;
4433         draw_outline = 0;
4434         set_first_font("6x10");
4435         gap_x = 5;
4436         gap_y = 60;
4437         minimum_width = 5;
4438         minimum_height = 5;
4439         maximum_width = 0;
4440 #ifdef OWN_WINDOW
4441         own_window = 0;
4442         window.type = TYPE_NORMAL;
4443         window.hints = 0;
4444         strcpy(window.class_name, PACKAGE_NAME);
4445         sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
4446 #endif
4447         stippled_borders = 0;
4448         window.border_inner_margin = 3;
4449         window.border_outer_margin = 1;
4450         window.border_width = 1;
4451         text_alignment = BOTTOM_LEFT;
4452         info.x11.monitor.number = 1;
4453         info.x11.monitor.current = 0;
4454         info.x11.desktop.current = 1; 
4455         info.x11.desktop.number = 1;
4456         info.x11.desktop.nitems = 0;
4457         info.x11.desktop.all_names = NULL; 
4458         info.x11.desktop.name = NULL; 
4459 #endif /* X11 */
4460
4461         free_templates();
4462
4463         free(current_mail_spool);
4464         {
4465                 char buf[256];
4466
4467                 variable_substitute(MAIL_FILE, buf, 256);
4468                 if (buf[0] != '\0') {
4469                         current_mail_spool = strndup(buf, text_buffer_size);
4470                 }
4471         }
4472
4473         no_buffers = 1;
4474         set_update_interval(3);
4475         update_interval_bat = NOBATTERY;
4476         info.music_player_interval = 1.0;
4477         stuff_in_uppercase = 0;
4478         info.users.number = 1;
4479
4480 #ifdef TCP_PORT_MONITOR
4481         /* set default connection limit */
4482         tcp_portmon_set_max_connections(0);
4483 #endif
4484 }
4485
4486 /* returns 1 if you can overwrite or create the file at 'path' */
4487 static _Bool overwrite_works(const char *path)
4488 {
4489         FILE *filepointer;
4490
4491         if (!(filepointer = fopen(path, "w")))
4492                 return 0;
4493         fclose(filepointer);
4494         return 1;
4495 }
4496
4497 /* returns 1 if you can append or create the file at 'path' */
4498 static _Bool append_works(const char *path)
4499 {
4500         FILE *filepointer;
4501
4502         if (!(filepointer = fopen(path, "a")))
4503                 return 0;
4504         fclose(filepointer);
4505         return 1;
4506 }
4507
4508 #ifdef X11
4509 #ifdef DEBUG
4510 /* WARNING, this type not in Xlib spec */
4511 int x11_error_handler(Display *d, XErrorEvent *err)
4512         __attribute__((noreturn));
4513 int x11_error_handler(Display *d, XErrorEvent *err)
4514 {
4515         NORM_ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
4516                         err->type,
4517                         (long unsigned)err->display,
4518                         (long)err->resourceid,
4519                         err->serial,
4520                         err->error_code,
4521                         err->request_code,
4522                         err->minor_code,
4523                         (long unsigned)d
4524                         );
4525         abort();
4526 }
4527
4528 int x11_ioerror_handler(Display *d)
4529         __attribute__((noreturn));
4530 int x11_ioerror_handler(Display *d)
4531 {
4532         NORM_ERR("X Error: Display %lx\n",
4533                         (long unsigned)d
4534                         );
4535         abort();
4536 }
4537 #endif /* DEBUG */
4538
4539 static void X11_initialisation(void)
4540 {
4541         if (x_initialised == YES) return;
4542         output_methods |= TO_X;
4543         init_X11(disp);
4544         set_default_configurations_for_x();
4545         x_initialised = YES;
4546 #ifdef DEBUG
4547         _Xdebug = 1;
4548         /* WARNING, this type not in Xlib spec */
4549         XSetErrorHandler(&x11_error_handler);
4550         XSetIOErrorHandler(&x11_ioerror_handler);
4551 #endif /* DEBUG */
4552 }
4553
4554 static char **xargv = 0;
4555 static int xargc = 0;
4556
4557 static void X11_create_window(void)
4558 {
4559         if (output_methods & TO_X) {
4560 #ifdef OWN_WINDOW
4561                 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4562                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
4563                                 xargv, xargc);
4564 #else /* OWN_WINDOW */
4565                 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4566                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
4567                                 xargv, xargc);
4568 #endif /* OWN_WINDOW */
4569
4570                 setup_fonts();
4571                 load_fonts();
4572                 update_text_area();     /* to position text/window on screen */
4573
4574 #ifdef OWN_WINDOW
4575                 if (own_window && !fixed_pos) {
4576                         XMoveWindow(display, window.window, window.x, window.y);
4577                 }
4578                 if (own_window) {
4579                         set_transparent_background(window.window);
4580                 }
4581 #endif
4582
4583                 create_gc();
4584
4585                 draw_stuff();
4586
4587                 x11_stuff.region = XCreateRegion();
4588 #ifdef HAVE_XDAMAGE
4589                 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
4590                         NORM_ERR("Xdamage extension unavailable");
4591                 }
4592                 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
4593                 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
4594                 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
4595 #endif /* HAVE_XDAMAGE */
4596
4597                 selected_font = 0;
4598                 update_text_area();     /* to get initial size of the window */
4599         }
4600 #ifdef HAVE_LUA
4601         /* setup lua window globals */
4602         llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
4603 #endif /* HAVE_LUA */
4604 }
4605 #endif /* X11 */
4606
4607 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
4608 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
4609 #define CONF2(a) if (strcasecmp(name, a) == 0)
4610 #define CONF(a) else CONF2(a)
4611 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
4612                 || strcasecmp(name, b) == 0)
4613 #define CONF_CONTINUE 1
4614 #define CONF_BREAK 2
4615 #define CONF_BUFF_SIZE 512
4616
4617 static FILE *open_config_file(const char *f)
4618 {
4619 #ifdef CONFIG_OUTPUT
4620         if (!strcmp(f, "==builtin==")) {
4621                 return conf_cookie_open();
4622         } else
4623 #endif /* CONFIG_OUTPUT */
4624                 return fopen(f, "r");
4625 }
4626
4627 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
4628 {
4629         char *p, *p2;
4630         (*line)++;
4631         if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
4632                 return CONF_BREAK;
4633         }
4634         remove_comments(buf);
4635
4636         p = buf;
4637
4638         /* skip spaces */
4639         while (*p && isspace((int) *p)) {
4640                 p++;
4641         }
4642         if (*p == '\0') {
4643                 return CONF_CONTINUE;   /* empty line */
4644         }
4645
4646         *name = p;
4647
4648         /* skip name */
4649         p2 = p;
4650         while (*p2 && !isspace((int) *p2)) {
4651                 p2++;
4652         }
4653         if (*p2 != '\0') {
4654                 *p2 = '\0';     /* break at name's end */
4655                 p2++;
4656         }
4657
4658         /* get value */
4659         if (*p2) {
4660                 p = p2;
4661                 while (*p && isspace((int) *p)) {
4662                         p++;
4663                 }
4664
4665                 *value = p;
4666
4667                 p2 = *value + strlen(*value);
4668                 while (isspace((int) *(p2 - 1))) {
4669                         *--p2 = '\0';
4670                 }
4671         } else {
4672                 *value = 0;
4673         }
4674         return 0;
4675 }
4676
4677 char load_config_file(const char *f)
4678 {
4679         int line = 0;
4680         FILE *fp;
4681
4682         fp = open_config_file(f);
4683         if (!fp) {
4684                 return FALSE;
4685         }
4686         DBGP("reading contents from config file '%s'", f);
4687
4688         while (!feof(fp)) {
4689                 char buff[CONF_BUFF_SIZE], *name, *value;
4690                 int ret = do_config_step(&line, fp, buff, &name, &value);
4691                 if (ret == CONF_BREAK) {
4692                         break;
4693                 } else if (ret == CONF_CONTINUE) {
4694                         continue;
4695                 }
4696
4697 #ifdef X11
4698                 CONF2("out_to_x") {
4699                         /* don't listen if X is already initialised or
4700                          * if we already know we don't want it */
4701                         if(x_initialised != YES) {
4702                                 if (string_to_bool(value)) {
4703                                         output_methods &= TO_X;
4704                                 } else {
4705                                         output_methods &= ~TO_X;
4706                                         x_initialised = NEVER;
4707                                 }
4708                         }
4709                 }
4710                 CONF("display") {
4711                         if (!value || x_initialised == YES) {
4712                                 CONF_ERR;
4713                         } else {
4714                                 if (disp)
4715                                         free(disp);
4716                                 disp = strdup(value);
4717                         }
4718                 }
4719                 CONF("alignment") {
4720 #ifdef OWN_WINDOW
4721                         if (window.type == TYPE_DOCK)
4722                                 ;
4723                         else
4724 #endif /*OWN_WINDOW */
4725                         if (value) {
4726                                 int a = string_to_alignment(value);
4727
4728                                 if (a <= 0) {
4729                                         CONF_ERR;
4730                                 } else {
4731                                         text_alignment = a;
4732                                 }
4733                         } else {
4734                                 CONF_ERR;
4735                         }
4736                 }
4737                 CONF("background") {
4738                         fork_to_background = string_to_bool(value);
4739                 }
4740 #else
4741                 CONF2("background") {
4742                         fork_to_background = string_to_bool(value);
4743                 }
4744 #endif /* X11 */
4745 #ifdef X11
4746                 CONF("show_graph_scale") {
4747                         show_graph_scale = string_to_bool(value);
4748                 }
4749                 CONF("show_graph_range") {
4750                         show_graph_range = string_to_bool(value);
4751                 }
4752                 CONF("border_inner_margin") {
4753                         if (value) {
4754                                 window.border_inner_margin = strtol(value, 0, 0);
4755                                 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
4756                         } else {
4757                                 CONF_ERR;
4758                         }
4759                 }
4760                 CONF("border_outer_margin") {
4761                         if (value) {
4762                                 window.border_outer_margin = strtol(value, 0, 0);
4763                                 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
4764                         } else {
4765                                 CONF_ERR;
4766                         }
4767                 }
4768                 CONF("border_width") {
4769                         if (value) {
4770                                 window.border_width = strtol(value, 0, 0);
4771                                 if (window.border_width < 0) window.border_width = 0;
4772                         } else {
4773                                 CONF_ERR;
4774                         }
4775                 }
4776 #endif /* X11 */
4777 #define TEMPLATE_CONF(n) \
4778                 CONF("template"#n) { \
4779                         if (set_template(n, value)) \
4780                                 CONF_ERR; \
4781                 }
4782                 TEMPLATE_CONF(0)
4783                 TEMPLATE_CONF(1)
4784                 TEMPLATE_CONF(2)
4785                 TEMPLATE_CONF(3)
4786                 TEMPLATE_CONF(4)
4787                 TEMPLATE_CONF(5)
4788                 TEMPLATE_CONF(6)
4789                 TEMPLATE_CONF(7)
4790                 TEMPLATE_CONF(8)
4791                 TEMPLATE_CONF(9)
4792                 CONF("imap") {
4793                         if (value) {
4794                                 parse_global_imap_mail_args(value);
4795                         } else {
4796                                 CONF_ERR;
4797                         }
4798                 }
4799                 CONF("pop3") {
4800                         if (value) {
4801                                 parse_global_pop3_mail_args(value);
4802                         } else {
4803                                 CONF_ERR;
4804                         }
4805                 }
4806                 CONF("default_bar_size") {
4807                         char err = 0;
4808                         if (value) {
4809                                 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
4810                                         err = 1;
4811                                 }
4812                         } else {
4813                                 err = 1;
4814                         }
4815                         if (err) {
4816                                 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
4817                         }
4818                 }
4819 #ifdef X11
4820                 CONF("default_graph_size") {
4821                         char err = 0;
4822                         if (value) {
4823                                 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
4824                                         err = 1;
4825                                 }
4826                         } else {
4827                                 err = 1;
4828                         }
4829                         if (err) {
4830                                 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
4831                         }
4832                 }
4833                 CONF("default_gauge_size") {
4834                         char err = 0;
4835                         if (value) {
4836                                 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
4837                                         err = 1;
4838                                 }
4839                         } else {
4840                                 err = 1;
4841                         }
4842                         if (err) {
4843                                 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
4844                         }
4845                 }
4846 #endif
4847 #ifdef MPD
4848                 CONF("mpd_host") {
4849                         if (value) {
4850                                 mpd_set_host(value);
4851                         } else {
4852                                 CONF_ERR;
4853                         }
4854                 }
4855                 CONF("mpd_port") {
4856                         if (value && mpd_set_port(value)) {
4857                                 CONF_ERR;
4858                         }
4859                 }
4860                 CONF("mpd_password") {
4861                         if (value) {
4862                                 mpd_set_password(value, 0);
4863                         } else {
4864                                 CONF_ERR;
4865                         }
4866                 }
4867 #endif
4868                 CONF("music_player_interval") {
4869                         if (value) {
4870                                 info.music_player_interval = strtod(value, 0);
4871                         } else {
4872                                 CONF_ERR;
4873                         }
4874                 }
4875 #ifdef __OpenBSD__
4876                 CONF("sensor_device") {
4877                         if (value) {
4878                                 sensor_device = strtol(value, 0, 0);
4879                         } else {
4880                                 CONF_ERR;
4881                         }
4882                 }
4883 #endif
4884                 CONF("cpu_avg_samples") {
4885                         if (value) {
4886                                 cpu_avg_samples = strtol(value, 0, 0);
4887                                 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
4888                                         CONF_ERR;
4889                                 } else {
4890                                         info.cpu_avg_samples = cpu_avg_samples;
4891                                 }
4892                         } else {
4893                                 CONF_ERR;
4894                         }
4895                 }
4896                 CONF("net_avg_samples") {
4897                         if (value) {
4898                                 net_avg_samples = strtol(value, 0, 0);
4899                                 if (net_avg_samples < 1 || net_avg_samples > 14) {
4900                                         CONF_ERR;
4901                                 } else {
4902                                         info.net_avg_samples = net_avg_samples;
4903                                 }
4904                         } else {
4905                                 CONF_ERR;
4906                         }
4907                 }
4908                 CONF("diskio_avg_samples") {
4909                         if (value) {
4910                                 diskio_avg_samples = strtol(value, 0, 0);
4911                                 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
4912                                         CONF_ERR;
4913                                 } else {
4914                                         info.diskio_avg_samples = diskio_avg_samples;
4915                                 }
4916                         } else {
4917                                 CONF_ERR;
4918                         }
4919                 }
4920
4921 #ifdef HAVE_XDBE
4922                 CONF("double_buffer") {
4923                         use_xdbe = string_to_bool(value);
4924                 }
4925 #endif
4926 #ifdef X11
4927                 CONF("override_utf8_locale") {
4928                         utf8_mode = string_to_bool(value);
4929                 }
4930                 CONF("draw_borders") {
4931                         draw_borders = string_to_bool(value);
4932                 }
4933                 CONF("draw_graph_borders") {
4934                         draw_graph_borders = string_to_bool(value);
4935                 }
4936                 CONF("draw_shades") {
4937                         draw_shades = string_to_bool(value);
4938                 }
4939                 CONF("draw_outline") {
4940                         draw_outline = string_to_bool(value);
4941                 }
4942 #endif /* X11 */
4943                 CONF("out_to_console") {
4944                         if(string_to_bool(value)) {
4945                                 output_methods |= TO_STDOUT;
4946                         } else {
4947                                 output_methods &= ~TO_STDOUT;
4948                         }
4949                 }
4950                 CONF("extra_newline") {
4951                         extra_newline = string_to_bool(value);
4952                 }
4953                 CONF("out_to_stderr") {
4954                         if(string_to_bool(value))
4955                                 output_methods |= TO_STDERR;
4956                 }
4957 #ifdef NCURSES
4958                 CONF("out_to_ncurses") {
4959                         if(string_to_bool(value)) {
4960                                 initscr();
4961                                 start_color();
4962                                 output_methods |= TO_NCURSES;
4963                         }
4964                 }
4965 #endif
4966                 CONF("overwrite_file") {
4967                         if(overwrite_file) {
4968                                 free(overwrite_file);
4969                                 overwrite_file = 0;
4970                         }
4971                         if(overwrite_works(value)) {
4972                                 overwrite_file = strdup(value);
4973                                 output_methods |= OVERWRITE_FILE;
4974                         } else
4975                                 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
4976                 }
4977                 CONF("append_file") {
4978                         if(append_file) {
4979                                 free(append_file);
4980                                 append_file = 0;
4981                         }
4982                         if(append_works(value)) {
4983                                 append_file = strdup(value);
4984                                 output_methods |= APPEND_FILE;
4985                         } else
4986                                 NORM_ERR("append_file won't be able to create/append '%s'", value);
4987                 }
4988                 CONF("use_spacer") {
4989                         if (value) {
4990                                 if (strcasecmp(value, "left") == EQUAL) {
4991                                         use_spacer = LEFT_SPACER;
4992                                 } else if (strcasecmp(value, "right") == EQUAL) {
4993                                         use_spacer = RIGHT_SPACER;
4994                                 } else if (strcasecmp(value, "none") == EQUAL) {
4995                                         use_spacer = NO_SPACER;
4996                                 } else {
4997                                         use_spacer = string_to_bool(value);
4998                                         NORM_ERR("use_spacer should have an argument of left, right, or"
4999                                                 " none.  '%s' seems to be some form of '%s', so"
5000                                                 " defaulting to %s.", value,
5001                                                 use_spacer ? "true" : "false",
5002                                                 use_spacer ? "right" : "none");
5003                                         if (use_spacer) {
5004                                                 use_spacer = RIGHT_SPACER;
5005                                         } else {
5006                                                 use_spacer = NO_SPACER;
5007                                         }
5008                                 }
5009                         } else {
5010                                 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
5011                                 use_spacer = RIGHT_SPACER;
5012                         }
5013                 }
5014 #ifdef X11
5015 #ifdef XFT
5016                 CONF("use_xft") {
5017                         use_xft = string_to_bool(value);
5018                 }
5019                 CONF("font") {
5020                         if (value) {
5021                                 set_first_font(value);
5022                         }
5023                 }
5024                 CONF("xftalpha") {
5025                         if (value && font_count >= 0) {
5026                                 fonts[0].font_alpha = atof(value) * 65535.0;
5027                         }
5028                 }
5029                 CONF("xftfont") {
5030                         if (use_xft) {
5031 #else
5032                 CONF("use_xft") {
5033                         if (string_to_bool(value)) {
5034                                 NORM_ERR("Xft not enabled at compile time");
5035                         }
5036                 }
5037                 CONF("xftfont") {
5038                         /* xftfont silently ignored when no Xft */
5039                 }
5040                 CONF("xftalpha") {
5041                         /* xftalpha is silently ignored when no Xft */
5042                 }
5043                 CONF("font") {
5044 #endif
5045                         if (value) {
5046                                 set_first_font(value);
5047                         }
5048 #ifdef XFT
5049                         }
5050 #endif
5051                 }
5052                 CONF("gap_x") {
5053                         if (value) {
5054                                 gap_x = atoi(value);
5055                         } else {
5056                                 CONF_ERR;
5057                         }
5058                 }
5059                 CONF("gap_y") {
5060                         if (value) {
5061                                 gap_y = atoi(value);
5062                         } else {
5063                                 CONF_ERR;
5064                         }
5065                 }
5066 #endif /* X11 */
5067                 CONF("mail_spool") {
5068                         if (value) {
5069                                 char buffer[256];
5070
5071                                 variable_substitute(value, buffer, 256);
5072
5073                                 if (buffer[0] != '\0') {
5074                                         if (current_mail_spool) {
5075                                                 free(current_mail_spool);
5076                                         }
5077                                         current_mail_spool = strndup(buffer, text_buffer_size);
5078                                 }
5079                         } else {
5080                                 CONF_ERR;
5081                         }
5082                 }
5083 #ifdef X11
5084                 CONF("minimum_size") {
5085                         if (value) {
5086                                 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5087                                                 != 2) {
5088                                         if (sscanf(value, "%d", &minimum_width) != 1) {
5089                                                 CONF_ERR;
5090                                         }
5091                                 }
5092                         } else {
5093                                 CONF_ERR;
5094                         }
5095                 }
5096                 CONF("maximum_width") {
5097                         if (value) {
5098                                 if (sscanf(value, "%d", &maximum_width) != 1) {
5099                                         CONF_ERR;
5100                                 }
5101                         } else {
5102                                 CONF_ERR;
5103                         }
5104                 }
5105 #endif /* X11 */
5106                 CONF("no_buffers") {
5107                         no_buffers = string_to_bool(value);
5108                 }
5109                 CONF("top_name_width") {
5110                         if (value) {
5111                                 if (sscanf(value, "%u", &top_name_width) != 1) {
5112                                         CONF_ERR;
5113                                 }
5114                         } else {
5115                                 CONF_ERR;
5116                         }
5117                         if (top_name_width >= max_user_text) {
5118                                 top_name_width = max_user_text - 1;
5119                         }
5120                 }
5121                 CONF("top_cpu_separate") {
5122                         cpu_separate = string_to_bool(value);
5123                 }
5124                 CONF("short_units") {
5125                         short_units = string_to_bool(value);
5126                 }
5127                 CONF("format_human_readable") {
5128                         format_human_readable = string_to_bool(value);
5129                 }
5130 #ifdef HDDTEMP
5131                 CONF("hddtemp_host") {
5132                         set_hddtemp_host(value);
5133                 }
5134                 CONF("hddtemp_port") {
5135                         set_hddtemp_port(value);
5136                 }
5137 #endif /* HDDTEMP */
5138                 CONF("pad_percents") {
5139                         pad_percents = atoi(value);
5140                 }
5141 #ifdef X11
5142 #ifdef OWN_WINDOW
5143                 CONF("own_window") {
5144                         if (value) {
5145                                 own_window = string_to_bool(value);
5146                         }
5147                 }
5148                 CONF("own_window_class") {
5149                         if (value) {
5150                                 memset(window.class_name, 0, sizeof(window.class_name));
5151                                 strncpy(window.class_name, value,
5152                                                 sizeof(window.class_name) - 1);
5153                         }
5154                 }
5155                 CONF("own_window_title") {
5156                         if (value) {
5157                                 memset(window.title, 0, sizeof(window.title));
5158                                 strncpy(window.title, value, sizeof(window.title) - 1);
5159                         }
5160                 }
5161                 CONF("own_window_transparent") {
5162                         if (value) {
5163                                 set_transparent = string_to_bool(value);
5164                         }
5165                 }
5166                 CONF("own_window_hints") {
5167                         if (value) {
5168                                 char *p_hint, *p_save;
5169                                 char delim[] = ", ";
5170
5171                                 /* tokenize the value into individual hints */
5172                                 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5173                                         do {
5174                                                 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5175                                                 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5176                                                         SET_HINT(window.hints, HINT_UNDECORATED);
5177                                                 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5178                                                         SET_HINT(window.hints, HINT_BELOW);
5179                                                 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5180                                                         SET_HINT(window.hints, HINT_ABOVE);
5181                                                 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5182                                                         SET_HINT(window.hints, HINT_STICKY);
5183                                                 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5184                                                         SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5185                                                 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5186                                                         SET_HINT(window.hints, HINT_SKIP_PAGER);
5187                                                 } else {
5188                                                         CONF_ERR;
5189                                                 }
5190
5191                                                 p_hint = strtok_r(NULL, delim, &p_save);
5192                                         } while (p_hint != NULL);
5193                                 }
5194                         } else {
5195                                 CONF_ERR;
5196                         }
5197                 }
5198                 CONF("own_window_type") {
5199                         if (value) {
5200                                 if (strncmp(value, "normal", 6) == EQUAL) {
5201                                         window.type = TYPE_NORMAL;
5202                                 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5203                                         window.type = TYPE_DESKTOP;
5204                                 } else if (strncmp(value, "dock", 4) == EQUAL) {
5205                                         window.type = TYPE_DOCK;
5206                                         text_alignment = TOP_LEFT;
5207                                 } else if (strncmp(value, "panel", 5) == EQUAL) {
5208                                         window.type = TYPE_PANEL;
5209                                 } else if (strncmp(value, "override", 8) == EQUAL) {
5210                                         window.type = TYPE_OVERRIDE;
5211                                 } else {
5212                                         CONF_ERR;
5213                                 }
5214                         } else {
5215                                 CONF_ERR;
5216                         }
5217                 }
5218 #endif
5219                 CONF("stippled_borders") {
5220                         if (value) {
5221                                 stippled_borders = strtol(value, 0, 0);
5222                         } else {
5223                                 stippled_borders = 4;
5224                         }
5225                 }
5226 #ifdef IMLIB2
5227                 CONF("imlib_cache_size") {
5228                         if (value) {
5229                                 cimlib_set_cache_size(atoi(value));
5230                         }
5231                 }
5232                 CONF("imlib_cache_flush_interval") {
5233                         if (value) {
5234                                 cimlib_set_cache_flush_interval(atoi(value));
5235                         }
5236                 }
5237 #endif /* IMLIB2 */
5238 #endif /* X11 */
5239                 CONF("update_interval_on_battery") {
5240                         if (value) {
5241                                 update_interval_bat = strtod(value, 0);
5242                         } else {
5243                                 CONF_ERR;
5244                         }
5245                 }
5246                 CONF("update_interval") {
5247                         if (value) {
5248                                 set_update_interval(strtod(value, 0));
5249                         } else {
5250                                 CONF_ERR;
5251                         }
5252                         if (info.music_player_interval == 0) {
5253                                 // default to update_interval
5254                                 info.music_player_interval = update_interval;
5255                         }
5256                 }
5257                 CONF("total_run_times") {
5258                         if (value) {
5259                                 total_run_times = strtod(value, 0);
5260                         } else {
5261                                 CONF_ERR;
5262                         }
5263                 }
5264                 CONF("uppercase") {
5265                         stuff_in_uppercase = string_to_bool(value);
5266                 }
5267                 CONF("max_specials") {
5268                         if (value) {
5269                                 max_specials = atoi(value);
5270                         } else {
5271                                 CONF_ERR;
5272                         }
5273                 }
5274                 CONF("max_user_text") {
5275                         if (value) {
5276                                 max_user_text = atoi(value);
5277                         } else {
5278                                 CONF_ERR;
5279                         }
5280                 }
5281                 CONF("text_buffer_size") {
5282                         if (value) {
5283                                 text_buffer_size = atoi(value);
5284                                 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5285                                         NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5286                                         text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5287                                 }
5288                         } else {
5289                                 CONF_ERR;
5290                         }
5291                 }
5292                 CONF("text") {
5293 #ifdef X11
5294                         if (output_methods & TO_X) {
5295                                 X11_initialisation();
5296                         }
5297 #endif
5298
5299                         if (global_text) {
5300                                 free(global_text);
5301                                 global_text = 0;
5302                         }
5303
5304                         global_text = (char *) malloc(1);
5305                         global_text[0] = '\0';
5306
5307                         while (!feof(fp)) {
5308                                 unsigned int l = strlen(global_text);
5309                                 unsigned int bl;
5310                                 char buf[CONF_BUFF_SIZE];
5311
5312                                 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5313                                         break;
5314                                 }
5315
5316                                 /* Remove \\-\n. */
5317                                 bl = strlen(buf);
5318                                 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5319                                         buf[bl-2] = '\0';
5320                                         bl -= 2;
5321                                         if (bl == 0) {
5322                                                 continue;
5323                                         }
5324                                 }
5325
5326                                 /* Check for continuation of \\-\n. */
5327                                 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5328                                         global_text[l-1] = '\0';
5329                                         continue;
5330                                 }
5331
5332                                 global_text = (char *) realloc(global_text, l + bl + 1);
5333                                 strcat(global_text, buf);
5334
5335                                 if (strlen(global_text) > max_user_text) {
5336                                         break;
5337                                 }
5338                         }
5339                         global_text_lines = line + 1;
5340                         break;
5341                 }
5342 #ifdef TCP_PORT_MONITOR
5343                 CONF("max_port_monitor_connections") {
5344                         int max;
5345                         if (!value || (sscanf(value, "%d", &max) != 1)) {
5346                                 /* an error. use default, warn and continue. */
5347                                 tcp_portmon_set_max_connections(0);
5348                                 CONF_ERR;
5349                         } else if (tcp_portmon_set_max_connections(max)) {
5350                                 /* max is < 0, default has been set*/
5351                                 CONF_ERR;
5352                         }
5353                 }
5354 #endif
5355                 CONF("if_up_strictness") {
5356                         if (!value) {
5357                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5358                                 ifup_strictness = IFUP_UP;
5359                         } else if (strcasecmp(value, "up") == EQUAL) {
5360                                 ifup_strictness = IFUP_UP;
5361                         } else if (strcasecmp(value, "link") == EQUAL) {
5362                                 ifup_strictness = IFUP_LINK;
5363                         } else if (strcasecmp(value, "address") == EQUAL) {
5364                                 ifup_strictness = IFUP_ADDR;
5365                         } else {
5366                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
5367                                 ifup_strictness = IFUP_UP;
5368                         }
5369                 }
5370
5371                 CONF("temperature_unit") {
5372                         if (!value) {
5373                                 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
5374                         } else if (set_temp_output_unit(value)) {
5375                                 NORM_ERR("temperature_unit: incorrect argument");
5376                         }
5377                 }
5378
5379 #ifdef HAVE_LUA
5380                 CONF("lua_load") {
5381                         if (value) {
5382                                 char *ptr = strtok(value, " ");
5383                                 while (ptr) {
5384                                         llua_load(ptr);
5385                                         ptr = strtok(NULL, " ");
5386                                 }
5387                         } else {
5388                                 CONF_ERR;
5389                         }
5390                 }
5391 #ifdef X11
5392                 CONF("lua_draw_hook_pre") {
5393                         if (value) {
5394                                 llua_set_draw_pre_hook(value);
5395                         } else {
5396                                 CONF_ERR;
5397                         }
5398                 }
5399                 CONF("lua_draw_hook_post") {
5400                         if (value) {
5401                                 llua_set_draw_post_hook(value);
5402                         } else {
5403                                 CONF_ERR;
5404                         }
5405                 }
5406                 CONF("lua_startup_hook") {
5407                         if (value) {
5408                                 llua_set_startup_hook(value);
5409                         } else {
5410                                 CONF_ERR;
5411                         }
5412                 }
5413                 CONF("lua_shutdown_hook") {
5414                         if (value) {
5415                                 llua_set_shutdown_hook(value);
5416                         } else {
5417                                 CONF_ERR;
5418                         }
5419                 }
5420 #endif /* X11 */
5421 #endif /* HAVE_LUA */
5422
5423                 CONF("color0"){}
5424                 CONF("color1"){}
5425                 CONF("color2"){}
5426                 CONF("color3"){}
5427                 CONF("color4"){}
5428                 CONF("color5"){}
5429                 CONF("color6"){}
5430                 CONF("color7"){}
5431                 CONF("color8"){}
5432                 CONF("color9"){}
5433                 CONF("default_color"){}
5434                 CONF3("default_shade_color", "default_shadecolor"){}
5435                 CONF3("default_outline_color", "default_outlinecolor") {}
5436                 CONF("own_window_colour") {}
5437
5438                 else {
5439                         NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
5440                 }
5441         }
5442
5443         fclose(fp);
5444
5445         if (info.music_player_interval == 0) {
5446                 // default to update_interval
5447                 info.music_player_interval = update_interval;
5448         }
5449         if (!global_text) { // didn't supply any text
5450                 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
5451         }
5452         if (!output_methods) {
5453                 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
5454         }
5455 #if defined(NCURSES)
5456 #if defined(X11)
5457         if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
5458                 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
5459                 output_methods &= ~TO_NCURSES;
5460                 endwin();
5461         }
5462 #endif /* X11 */
5463         if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
5464                 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
5465                 output_methods &= ~(TO_STDOUT | TO_STDERR);
5466         }
5467 #endif /* NCURSES */
5468         return TRUE;
5469 }
5470
5471 #ifdef X11
5472 static void load_config_file_x11(const char *f)
5473 {
5474         int line = 0;
5475         FILE *fp;
5476
5477         fp = open_config_file(f);
5478         if (!fp) {
5479                 return;
5480         }
5481         DBGP("reading contents from config file '%s'", f);
5482
5483         while (!feof(fp)) {
5484                 char buff[CONF_BUFF_SIZE], *name, *value;
5485                 int ret = do_config_step(&line, fp, buff, &name, &value);
5486                 if (ret == CONF_BREAK) {
5487                         break;
5488                 } else if (ret == CONF_CONTINUE) {
5489                         continue;
5490                 }
5491
5492                 CONF2("color0") {
5493                         X11_initialisation();
5494                         if (x_initialised == YES) {
5495                                 if (value) {
5496                                         color0 = get_x11_color(value);
5497                                 } else {
5498                                         CONF_ERR;
5499                                 }
5500                         }
5501                 }
5502                 CONF("color1") {
5503                         X11_initialisation();
5504                         if (x_initialised == YES) {
5505                                 if (value) {
5506                                         color1 = get_x11_color(value);
5507                                 } else {
5508                                         CONF_ERR;
5509                                 }
5510                         }
5511                 }
5512                 CONF("color2") {
5513                         X11_initialisation();
5514                         if (x_initialised == YES) {
5515                                 if (value) {
5516                                         color2 = get_x11_color(value);
5517                                 } else {
5518                                         CONF_ERR;
5519                                 }
5520                         }
5521                 }
5522                 CONF("color3") {
5523                         X11_initialisation();
5524                         if (x_initialised == YES) {
5525                                 if (value) {
5526                                         color3 = get_x11_color(value);
5527                                 } else {
5528                                         CONF_ERR;
5529                                 }
5530                         }
5531                 }
5532                 CONF("color4") {
5533                         X11_initialisation();
5534                         if (x_initialised == YES) {
5535                                 if (value) {
5536                                         color4 = get_x11_color(value);
5537                                 } else {
5538                                         CONF_ERR;
5539                                 }
5540                         }
5541                 }
5542                 CONF("color5") {
5543                         X11_initialisation();
5544                         if (x_initialised == YES) {
5545                                 if (value) {
5546                                         color5 = get_x11_color(value);
5547                                 } else {
5548                                         CONF_ERR;
5549                                 }
5550                         }
5551                 }
5552                 CONF("color6") {
5553                         X11_initialisation();
5554                         if (x_initialised == YES) {
5555                                 if (value) {
5556                                         color6 = get_x11_color(value);
5557                                 } else {
5558                                         CONF_ERR;
5559                                 }
5560                         }
5561                 }
5562                 CONF("color7") {
5563                         X11_initialisation();
5564                         if (x_initialised == YES) {
5565                                 if (value) {
5566                                         color7 = get_x11_color(value);
5567                                 } else {
5568                                         CONF_ERR;
5569                                 }
5570                         }
5571                 }
5572                 CONF("color8") {
5573                         X11_initialisation();
5574                         if (x_initialised == YES) {
5575                                 if (value) {
5576                                         color8 = get_x11_color(value);
5577                                 } else {
5578                                         CONF_ERR;
5579                                 }
5580                         }
5581                 }
5582                 CONF("color9") {
5583                         X11_initialisation();
5584                         if (x_initialised == YES) {
5585                                 if (value) {
5586                                         color9 = get_x11_color(value);
5587                                 } else {
5588                                         CONF_ERR;
5589                                 }
5590                         }
5591                 }
5592                 CONF("default_color") {
5593                         X11_initialisation();
5594                         if (x_initialised == YES) {
5595                                 if (value) {
5596                                         default_fg_color = get_x11_color(value);
5597                                 } else {
5598                                         CONF_ERR;
5599                                 }
5600                         }
5601                 }
5602                 CONF3("default_shade_color", "default_shadecolor") {
5603                         X11_initialisation();
5604                         if (x_initialised == YES) {
5605                                 if (value) {
5606                                         default_bg_color = get_x11_color(value);
5607                                 } else {
5608                                         CONF_ERR;
5609                                 }
5610                         }
5611                 }
5612                 CONF3("default_outline_color", "default_outlinecolor") {
5613                         X11_initialisation();
5614                         if (x_initialised == YES) {
5615                                 if (value) {
5616                                         default_out_color = get_x11_color(value);
5617                                 } else {
5618                                         CONF_ERR;
5619                                 }
5620                         }
5621                 }
5622 #ifdef OWN_WINDOW
5623                 CONF("own_window_colour") {
5624                         X11_initialisation();
5625                         if (x_initialised == YES) {
5626                                 if (value) {
5627                                         background_colour = get_x11_color(value);
5628                                 } else {
5629                                         NORM_ERR("Invalid colour for own_window_colour (try omitting the "
5630                                                 "'#' for hex colours");
5631                                 }
5632                         }
5633                 }
5634 #endif
5635                 CONF("text") {
5636                         /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
5637                         if(output_methods & TO_X) {
5638                                 X11_initialisation();
5639                         }
5640                 }
5641 #undef CONF
5642 #undef CONF2
5643 #undef CONF3
5644 #undef CONF_ERR
5645 #undef CONF_ERR2
5646 #undef CONF_BREAK
5647 #undef CONF_CONTINUE
5648 #undef CONF_BUFF_SIZE
5649         }
5650
5651         fclose(fp);
5652
5653 }
5654 #endif /* X11 */
5655
5656 static void print_help(const char *prog_name) {
5657         printf("Usage: %s [OPTION]...\n"
5658                         PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
5659                         "window. Command line options will override configurations defined in config\n"
5660                         "file.\n"
5661                         "   -v, --version             version\n"
5662                         "   -q, --quiet               quiet mode\n"
5663                         "   -D, --debug               increase debugging output, ie. -DD for more debugging\n"
5664                         "   -c, --config=FILE         config file to load\n"
5665 #ifdef CONFIG_OUTPUT
5666                         "   -C, --print-config        print the builtin default config to stdout\n"
5667                         "                             e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
5668 #endif
5669                         "   -d, --daemonize           daemonize, fork to background\n"
5670                         "   -h, --help                help\n"
5671 #ifdef X11
5672                         "   -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
5673                         "   -f, --font=FONT           font to use\n"
5674                         "   -X, --display=DISPLAY     X11 display to use\n"
5675 #ifdef OWN_WINDOW
5676                         "   -o, --own-window          create own window to draw\n"
5677 #endif
5678 #ifdef HAVE_XDBE
5679                         "   -b, --double-buffer       double buffer (prevents flickering)\n"
5680 #endif
5681                         "   -w, --window-id=WIN_ID    window id to draw\n"
5682                         "   -x X                      x position\n"
5683                         "   -y Y                      y position\n"
5684 #endif /* X11 */
5685                         "   -t, --text=TEXT           text to render, remember single quotes, like -t '$uptime'\n"
5686                         "   -u, --interval=SECS       update interval\n"
5687                         "   -i COUNT                  number of times to update "PACKAGE_NAME" (and quit)\n"
5688                         "   -p, --pause=SECS          pause for SECS seconds at startup before doing anything\n",
5689                         prog_name
5690         );
5691 }
5692
5693 /* : means that character before that takes an argument */
5694 static const char *getopt_string = "vVqdDt:u:i:hc:p:"
5695 #ifdef X11
5696         "x:y:w:a:f:X:"
5697 #ifdef OWN_WINDOW
5698         "o"
5699 #endif
5700 #ifdef HAVE_XDBE
5701         "b"
5702 #endif
5703 #endif /* X11 */
5704 #ifdef CONFIG_OUTPUT
5705         "C"
5706 #endif
5707         ;
5708
5709 static const struct option longopts[] = {
5710         { "help", 0, NULL, 'h' },
5711         { "version", 0, NULL, 'V' },
5712         { "debug", 0, NULL, 'D' },
5713         { "config", 1, NULL, 'c' },
5714 #ifdef CONFIG_OUTPUT
5715         { "print-config", 0, NULL, 'C' },
5716 #endif
5717         { "daemonize", 0, NULL, 'd' },
5718 #ifdef X11
5719         { "alignment", 1, NULL, 'a' },
5720         { "font", 1, NULL, 'f' },
5721         { "display", 1, NULL, 'X' },
5722 #ifdef OWN_WINDOW
5723         { "own-window", 0, NULL, 'o' },
5724 #endif
5725 #ifdef HAVE_XDBE
5726         { "double-buffer", 0, NULL, 'b' },
5727 #endif
5728         { "window-id", 1, NULL, 'w' },
5729 #endif /* X11 */
5730         { "text", 1, NULL, 't' },
5731         { "interval", 0, NULL, 'u' },
5732         { "pause", 0, NULL, 'p' },
5733         { 0, 0, 0, 0 }
5734 };
5735
5736 void initialisation(int argc, char **argv) {
5737         struct sigaction act, oact;
5738
5739         set_default_configurations();
5740         load_config_file(current_config);
5741         currentconffile = conftree_add(currentconffile, current_config);
5742
5743         /* init specials array */
5744         if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
5745                 NORM_ERR("failed to create specials array");
5746         }
5747
5748 #ifdef MAIL_FILE
5749         if (current_mail_spool == NULL) {
5750                 char buf[256];
5751
5752                 variable_substitute(MAIL_FILE, buf, 256);
5753
5754                 if (buf[0] != '\0') {
5755                         current_mail_spool = strndup(buf, text_buffer_size);
5756                 }
5757         }
5758 #endif
5759
5760         /* handle other command line arguments */
5761
5762 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
5763                 || defined(__NetBSD__)
5764         optind = optreset = 1;
5765 #else
5766         optind = 0;
5767 #endif
5768
5769 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5770         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
5771                         "kvm_open")) == NULL) {
5772                 CRIT_ERR(NULL, NULL, "cannot read kvm");
5773         }
5774 #endif
5775
5776         while (1) {
5777                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5778                 int startup_pause;
5779
5780                 if (c == -1) {
5781                         break;
5782                 }
5783
5784                 switch (c) {
5785                         case 'd':
5786                                 fork_to_background = 1;
5787                                 break;
5788                         case 'D':
5789                                 global_debug_level++;
5790                                 break;
5791 #ifdef X11
5792                         case 'f':
5793                                 set_first_font(optarg);
5794                                 break;
5795                         case 'a':
5796                                 text_alignment = string_to_alignment(optarg);
5797                                 break;
5798
5799 #ifdef OWN_WINDOW
5800                         case 'o':
5801                                 own_window = 1;
5802                                 break;
5803 #endif
5804 #ifdef HAVE_XDBE
5805                         case 'b':
5806                                 use_xdbe = 1;
5807                                 break;
5808 #endif
5809 #endif /* X11 */
5810                         case 't':
5811                                 if (global_text) {
5812                                         free(global_text);
5813                                         global_text = 0;
5814                                 }
5815                                 global_text = strndup(optarg, max_user_text);
5816                                 convert_escapes(global_text);
5817                                 break;
5818
5819                         case 'u':
5820                                 update_interval = strtod(optarg, 0);
5821                                 update_interval_old = update_interval;
5822                                 if (info.music_player_interval == 0) {
5823                                         // default to update_interval
5824                                         info.music_player_interval = update_interval;
5825                                 }
5826                                 break;
5827
5828                         case 'i':
5829                                 total_run_times = strtod(optarg, 0);
5830                                 break;
5831 #ifdef X11
5832                         case 'x':
5833                                 gap_x = atoi(optarg);
5834                                 break;
5835
5836                         case 'y':
5837                                 gap_y = atoi(optarg);
5838                                 break;
5839 #endif /* X11 */
5840                         case 'p':
5841                                 startup_pause = atoi(optarg);
5842                                 sleep(startup_pause);
5843                                 break;
5844
5845                         case '?':
5846                                 exit(EXIT_FAILURE);
5847                 }
5848         }
5849
5850 #ifdef X11
5851         /* load font */
5852         if (output_methods & TO_X) {
5853                 load_config_file_x11(current_config);
5854         }
5855 #endif /* X11 */
5856
5857         /* generate text and get initial size */
5858         extract_variable_text(global_text);
5859         if (global_text) {
5860                 free(global_text);
5861                 global_text = 0;
5862         }
5863         global_text = NULL;
5864         /* fork */
5865         if (fork_to_background) {
5866                 int pid = fork();
5867
5868                 switch (pid) {
5869                         case -1:
5870                                 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
5871                                         strerror(errno));
5872                                 break;
5873
5874                         case 0:
5875                                 /* child process */
5876                                 usleep(25000);
5877                                 fprintf(stderr, "\n");
5878                                 fflush(stderr);
5879                                 break;
5880
5881                         default:
5882                                 /* parent process */
5883                                 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
5884                                         pid);
5885                                 fflush(stderr);
5886                                 exit(EXIT_SUCCESS);
5887                 }
5888         }
5889
5890         start_update_threading();
5891
5892         text_buffer = malloc(max_user_text);
5893         memset(text_buffer, 0, max_user_text);
5894         tmpstring1 = malloc(text_buffer_size);
5895         memset(tmpstring1, 0, text_buffer_size);
5896         tmpstring2 = malloc(text_buffer_size);
5897         memset(tmpstring2, 0, text_buffer_size);
5898
5899 #ifdef X11
5900         xargc = argc;
5901         xargv = argv;
5902         X11_create_window();
5903 #endif /* X11 */
5904 #ifdef HAVE_LUA
5905         llua_setup_info(&info, update_interval);
5906 #endif /* HAVE_LUA */
5907 #ifdef XOAP
5908         xmlInitParser();
5909 #endif /* XOAP */
5910
5911         /* Set signal handlers */
5912         act.sa_handler = signal_handler;
5913         sigemptyset(&act.sa_mask);
5914         act.sa_flags = 0;
5915 #ifdef SA_RESTART
5916         act.sa_flags |= SA_RESTART;
5917 #endif
5918
5919         if (            sigaction(SIGINT,  &act, &oact) < 0
5920                         ||      sigaction(SIGALRM, &act, &oact) < 0
5921                         ||      sigaction(SIGUSR1, &act, &oact) < 0
5922                         ||      sigaction(SIGHUP,  &act, &oact) < 0
5923                         ||      sigaction(SIGTERM, &act, &oact) < 0) {
5924                 NORM_ERR("error setting signal handler: %s", strerror(errno));
5925         }
5926
5927 #ifdef HAVE_LUA
5928         llua_startup_hook();
5929 #endif /* HAVE_LUA */
5930 }
5931
5932 int main(int argc, char **argv)
5933 {
5934 #ifdef X11
5935         char *s, *temp;
5936         unsigned int x;
5937 #endif
5938
5939         argc_copy = argc;
5940         argv_copy = argv;
5941         g_signal_pending = 0;
5942         max_user_text = MAX_USER_TEXT_DEFAULT;
5943         current_config = 0;
5944         memset(&info, 0, sizeof(info));
5945         free_templates();
5946         clear_net_stats();
5947
5948 #ifdef TCP_PORT_MONITOR
5949         /* set default connection limit */
5950         tcp_portmon_set_max_connections(0);
5951 #endif
5952
5953         /* handle command line parameters that don't change configs */
5954 #ifdef X11
5955         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
5956                         || ((s = getenv("LANG")) && *s)) {
5957                 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
5958                 if (temp == NULL) {
5959                         NORM_ERR("malloc failed");
5960                 }
5961                 for (x = 0; x < strlen(s); x++) {
5962                         temp[x] = tolower(s[x]);
5963                 }
5964                 temp[x] = 0;
5965                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5966                         utf8_mode = 1;
5967                 }
5968
5969                 free(temp);
5970         }
5971         if (!setlocale(LC_CTYPE, "")) {
5972                 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5973         }
5974 #endif /* X11 */
5975         while (1) {
5976                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
5977
5978                 if (c == -1) {
5979                         break;
5980                 }
5981
5982                 switch (c) {
5983                         case 'v':
5984                         case 'V':
5985                                 print_version();
5986                         case 'c':
5987                                 if (current_config) {
5988                                         free(current_config);
5989                                 }
5990                                 current_config = strndup(optarg, max_user_text);
5991                                 break;
5992                         case 'q':
5993                                 freopen("/dev/null", "w", stderr);
5994                                 break;
5995                         case 'h':
5996                                 print_help(argv[0]);
5997                                 return 0;
5998 #ifdef CONFIG_OUTPUT
5999                         case 'C':
6000                                 print_defconfig();
6001                                 return 0;
6002 #endif
6003 #ifdef X11
6004                         case 'w':
6005                                 window.window = strtol(optarg, 0, 0);
6006                                 break;
6007                         case 'X':
6008                                 if (disp)
6009                                         free(disp);
6010                                 disp = strdup(optarg);
6011                                 break;
6012 #endif /* X11 */
6013
6014                         case '?':
6015                                 exit(EXIT_FAILURE);
6016                 }
6017         }
6018
6019         /* check if specified config file is valid */
6020         if (current_config) {
6021                 struct stat sb;
6022                 if (stat(current_config, &sb) ||
6023                                 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
6024                         NORM_ERR("invalid configuration file '%s'\n", current_config);
6025                         free(current_config);
6026                         current_config = 0;
6027                 }
6028         }
6029
6030         /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
6031
6032         if (!current_config) {
6033                 /* load default config file */
6034                 char buf[DEFAULT_TEXT_BUFFER_SIZE];
6035                 FILE *fp;
6036
6037                 /* Try to use personal config file first */
6038                 to_real_path(buf, CONFIG_FILE);
6039                 if (buf[0] && (fp = fopen(buf, "r"))) {
6040                         current_config = strndup(buf, max_user_text);
6041                         fclose(fp);
6042                 }
6043
6044                 /* Try to use system config file if personal config not readable */
6045                 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
6046                         current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
6047                         fclose(fp);
6048                 }
6049
6050                 /* No readable config found */
6051                 if (!current_config) {
6052 #ifdef CONFIG_OUTPUT
6053                         current_config = strdup("==builtin==");
6054                         NORM_ERR("no readable personal or system-wide config file found,"
6055                                         " using builtin default");
6056 #else
6057                         CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
6058 #endif /* ! CONF_OUTPUT */
6059                 }
6060         }
6061
6062 #ifdef XOAP
6063         /* Load xoap keys, if existing */
6064         load_xoap_keys();
6065 #endif /* XOAP */
6066
6067 #ifdef HAVE_SYS_INOTIFY_H
6068         inotify_fd = inotify_init1(IN_NONBLOCK);
6069 #endif /* HAVE_SYS_INOTIFY_H */
6070
6071         initialisation(argc, argv);
6072
6073         main_loop();
6074
6075 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6076         kvm_close(kd);
6077 #endif
6078
6079         return 0;
6080
6081 }
6082
6083 void alarm_handler(void) {
6084         if(childpid > 0) {
6085                 kill(childpid, SIGTERM);
6086         }
6087 }
6088
6089 static void signal_handler(int sig)
6090 {
6091         /* signal handler is light as a feather, as it should be.
6092          * we will poll g_signal_pending with each loop of conky
6093          * and do any signal processing there, NOT here (except 
6094          * SIGALRM because this is caused when conky is hanging) */
6095         if(sig == SIGALRM) {
6096                 alarm_handler();
6097         } else {
6098                 g_signal_pending = sig;
6099         }
6100 }