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