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