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