5b966963105cc28e68c0c8258797d2365010906b
[monky] / src / conky.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "config.h"
33 #include "text_object.h"
34 #include "conky.h"
35 #include "common.h"
36 #include <stdarg.h>
37 #include <math.h>
38 #include <ctype.h>
39 #include <time.h>
40 #include <locale.h>
41 #include <signal.h>
42 #include <errno.h>
43 #include <limits.h>
44 #if HAVE_DIRENT_H
45 #include <dirent.h>
46 #endif
47 #include <sys/time.h>
48 #include <sys/param.h>
49 #ifdef HAVE_SYS_INOTIFY_H
50 #include <sys/inotify.h>
51 #endif /* HAVE_SYS_INOTIFY_H */
52 #ifdef X11
53 #include "x11.h"
54 #include <X11/Xutil.h>
55 #ifdef HAVE_XDAMAGE
56 #include <X11/extensions/Xdamage.h>
57 #endif
58 #ifdef IMLIB2
59 #include "imlib2.h"
60 #endif /* IMLIB2 */
61 #endif /* X11 */
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <netinet/in.h>
65 #include <netdb.h>
66 #include <fcntl.h>
67 #include <getopt.h>
68 #ifdef NCURSES
69 #include <ncurses.h>
70 #endif
71
72 /* local headers */
73 #include "algebra.h"
74 #include "build.h"
75 #include "colours.h"
76 #include "diskio.h"
77 #ifdef X11
78 #include "fonts.h"
79 #endif
80 #include "fs.h"
81 #include "logging.h"
82 #include "mixer.h"
83 #include "mail.h"
84 #include "mboxscan.h"
85 #include "specials.h"
86 #include "temphelper.h"
87 #include "tailhead.h"
88 #include "top.h"
89
90 /* check for OS and include appropriate headers */
91 #if defined(__linux__)
92 #include "linux.h"
93 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
94 #include "freebsd.h"
95 #elif defined(__OpenBSD__)
96 #include "openbsd.h"
97 #endif
98
99 #if defined(__FreeBSD_kernel__)
100 #include <bsd/bsd.h>
101 #endif
102
103 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
104 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
105                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
106 int apm_getinfo(int fd, apm_info_t aip);
107 char *get_apm_adapter(void);
108 char *get_apm_battery_life(void);
109 char *get_apm_battery_time(void);
110 #endif
111
112 #ifdef HAVE_ICONV
113 #include <iconv.h>
114 #endif
115
116 #ifdef CONFIG_OUTPUT
117 #include "defconfig.h"
118 #include "conf_cookie.h"
119 #endif
120
121 #ifndef S_ISSOCK
122 #define S_ISSOCK(x)   ((x & S_IFMT) == S_IFSOCK)
123 #endif
124
125 #define MAIL_FILE "$MAIL"
126 #define MAX_IF_BLOCK_DEPTH 5
127
128 //#define SIGNAL_BLOCKING
129 #undef SIGNAL_BLOCKING
130
131 /* debugging level, used by logging.h */
132 int global_debug_level = 0;
133
134 /* two strings for internal use */
135 static char *tmpstring1, *tmpstring2;
136
137 /* variables holding various config settings */
138 int short_units;
139 int format_human_readable;
140 int cpu_separate;
141 enum {
142         NO_SPACER = 0,
143         LEFT_SPACER,
144         RIGHT_SPACER
145 } use_spacer;
146 int top_cpu, top_mem, top_time;
147 #ifdef IOSTATS
148 int top_io;
149 #endif
150 static unsigned int top_name_width = 15;
151 int output_methods;
152 static int extra_newline;
153 enum x_initialiser_state x_initialised = NO;
154 static volatile int g_signal_pending;
155 /* Update interval */
156 double update_interval;
157 double update_interval_old;
158 double update_interval_bat;
159 void *global_cpu = NULL;
160 pid_t childpid = 0;
161
162 int argc_copy;
163 char** argv_copy;
164
165 /* prototypes for internally used functions */
166 static void signal_handler(int);
167 static void print_version(void) __attribute__((noreturn));
168 static void reload_config(void);
169 static void generate_text_internal(char *, int, struct text_object,
170                                    struct information *);
171 static int extract_variable_text_internal(struct text_object *,
172                                           const char *);
173
174 static void print_version(void)
175 {
176         printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
177
178         printf("\nCompiled in features:\n\n"
179                    "System config file: "SYSTEM_CONFIG_FILE"\n"
180                    "Package library path: "PACKAGE_LIBDIR"\n\n"
181 #ifdef X11
182                    " X11:\n"
183 # ifdef HAVE_XDAMAGE
184                    "  * Xdamage extension\n"
185 # endif /* HAVE_XDAMAGE */
186 # ifdef HAVE_XDBE
187                    "  * XDBE (double buffer extension)\n"
188 # endif /* HAVE_XDBE */
189 # ifdef XFT
190                    "  * Xft\n"
191 # endif /* XFT */
192 #endif /* X11 */
193                    "\n Music detection:\n"
194 #ifdef AUDACIOUS
195                    "  * Audacious\n"
196 #endif /* AUDACIOUS */
197 #ifdef BMPX
198                    "  * BMPx\n"
199 #endif /* BMPX */
200 #ifdef MPD
201                    "  * MPD\n"
202 #endif /* MPD */
203 #ifdef MOC
204                    "  * MOC\n"
205 #endif /* MOC */
206 #ifdef XMMS2
207                    "  * XMMS2\n"
208 #endif /* XMMS2 */
209                    "\n General:\n"
210 #ifdef HAVE_OPENMP
211                    "  * OpenMP\n"
212 #endif /* HAVE_OPENMP */
213 #ifdef MATH
214                    "  * math\n"
215 #endif /* Math */
216 #ifdef HDDTEMP
217                    "  * hddtemp\n"
218 #endif /* HDDTEMP */
219 #ifdef TCP_PORT_MONITOR
220                    "  * portmon\n"
221 #endif /* TCP_PORT_MONITOR */
222 #ifdef HAVE_CURL
223                    "  * Curl\n"
224 #endif /* HAVE_CURL */
225 #ifdef RSS
226                    "  * RSS\n"
227 #endif /* RSS */
228 #ifdef WEATHER
229                    "  * Weather (METAR)\n"
230 #ifdef XOAP
231                    "  * Weather (XOAP)\n"
232 #endif /* XOAP */
233 #endif /* WEATHER */
234 #ifdef HAVE_IWLIB
235                    "  * wireless\n"
236 #endif /* HAVE_IWLIB */
237 #ifdef IBM
238                    "  * support for IBM/Lenovo notebooks\n"
239 #endif /* IBM */
240 #ifdef NVIDIA
241                    "  * nvidia\n"
242 #endif /* NVIDIA */
243 #ifdef EVE
244                    "  * eve-online\n"
245 #endif /* EVE */
246 #ifdef CONFIG_OUTPUT
247                    "  * config-output\n"
248 #endif /* CONFIG_OUTPUT */
249 #ifdef IMLIB2
250                    "  * Imlib2\n"
251 #endif /* IMLIB2 */
252 #ifdef MIXER_IS_ALSA
253                    "  * ALSA mixer support\n"
254 #endif /* MIXER_IS_ALSA */
255 #ifdef APCUPSD
256                    "  * apcupsd\n"
257 #endif /* APCUPSD */
258 #ifdef IOSTATS
259                    "  * iostats\n"
260 #endif /* IOSTATS */
261 #ifdef HAVE_LUA
262                    "  * Lua\n"
263                    "\n  Lua bindings:\n"
264 #ifdef HAVE_LUA_CAIRO
265                    "   * Cairo\n"
266 #endif /* HAVE_LUA_CAIRO */
267 #ifdef HAVE_LUA_IMLIB2
268                    "   * Imlib2\n"
269 #endif /* IMLIB2 */
270 #endif /* HAVE_LUA */
271         );
272
273         exit(EXIT_SUCCESS);
274 }
275
276 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
277
278
279 #ifdef X11
280
281 static void X11_create_window(void);
282 static void X11_initialisation(void);
283
284 struct _x11_stuff_s {
285         Region region;
286 #ifdef HAVE_XDAMAGE
287         Damage damage;
288         XserverRegion region2, part;
289         int event_base, error_base;
290 #endif
291 } x11_stuff;
292
293 /* text size */
294
295 static int text_start_x, text_start_y;  /* text start position in window */
296 static int text_width, text_height;
297
298 /* alignments */
299 enum alignment {
300         TOP_LEFT = 1,
301         TOP_RIGHT,
302         TOP_MIDDLE,
303         BOTTOM_LEFT,
304         BOTTOM_RIGHT,
305         BOTTOM_MIDDLE,
306         MIDDLE_LEFT,
307         MIDDLE_RIGHT,
308         NONE
309 };
310
311 /* display to connect to */
312 static char *disp = NULL;
313
314 #endif /* X11 */
315
316 /* struct that has all info to be shared between
317  * instances of the same text object */
318 struct information info;
319
320 /* path to config file */
321 char *current_config;
322
323 /* set to 1 if you want all text to be in uppercase */
324 static unsigned int stuff_in_uppercase;
325
326 /* Run how many times? */
327 static unsigned long total_run_times;
328
329 /* fork? */
330 static int fork_to_background;
331
332 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
333
334 /* filenames for output */
335 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
336 char *append_file = NULL; FILE *append_fpointer = NULL;
337
338 #ifdef X11
339
340 static int show_graph_scale;
341 static int show_graph_range;
342
343 /* Position on the screen */
344 static int text_alignment;
345 static int gap_x, gap_y;
346
347 /* border */
348 static int draw_borders;
349 static int draw_graph_borders;
350 static int stippled_borders;
351
352 static int draw_shades, draw_outline;
353
354 static long default_fg_color, default_bg_color, default_out_color;
355
356 /* create own window or draw stuff to root? */
357 static int set_transparent = 0;
358
359 #ifdef OWN_WINDOW
360 static int own_window = 0;
361 static int background_colour = 0;
362
363 /* fixed size/pos is set if wm/user changes them */
364 static int fixed_size = 0, fixed_pos = 0;
365 #endif
366
367 static int minimum_width, minimum_height;
368 static int maximum_width;
369
370 #endif /* X11 */
371
372 #ifdef __OpenBSD__
373 static int sensor_device;
374 #endif
375
376 static long color0, color1, color2, color3, color4, color5, color6, color7,
377         color8, color9;
378
379 #define MAX_TEMPLATES 10
380 static char *template[MAX_TEMPLATES];
381
382 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
383 unsigned int max_user_text;
384
385 /* maximum size of individual text buffers, ie $exec buffer size */
386 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
387
388 #ifdef HAVE_ICONV
389 #define CODEPAGE_LENGTH 20
390 long iconv_selected;
391 long iconv_count = 0;
392 char iconv_converting;
393 static iconv_t **iconv_cd = 0;
394
395 int register_iconv(iconv_t *new_iconv)
396 {
397         iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
398         if (!iconv_cd) {
399                 CRIT_ERR(NULL, NULL, "Out of memory");
400         }
401         iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
402         if (!iconv_cd[iconv_count]) {
403                 CRIT_ERR(NULL, NULL, "Out of memory");
404         }
405         memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
406         iconv_count++;
407         return iconv_count;
408 }
409
410 void free_iconv(void)
411 {
412         if (iconv_cd) {
413                 long i;
414
415                 for (i = 0; i < iconv_count; i++) {
416                         if (iconv_cd[i]) {
417                                 iconv_close(*iconv_cd[i]);
418                                 free(iconv_cd[i]);
419                         }
420                 }
421                 free(iconv_cd);
422         }
423         iconv_cd = 0;
424 }
425
426 #endif
427
428 /* UTF-8 */
429 int utf8_mode = 0;
430
431 /* no buffers in used memory? */
432 int no_buffers;
433
434 /* pad percentages to decimals? */
435 static int pad_percents = 0;
436
437 static char *global_text = 0;
438 long global_text_lines;
439
440 static int total_updates;
441 static int updatereset;
442
443 int check_contains(char *f, char *s)
444 {
445         int ret = 0;
446         FILE *where = open_file(f, 0);
447
448         if (where) {
449                 char buf1[256];
450
451                 while (fgets(buf1, 256, where)) {
452                         if (strstr(buf1, s)) {
453                                 ret = 1;
454                                 break;
455                         }
456                 }
457                 fclose(where);
458         } else {
459                 NORM_ERR("Could not open the file");
460         }
461         return ret;
462 }
463
464 #define SECRIT_MULTILINE_CHAR '\x02'
465
466 #ifdef X11
467
468 static inline int calc_text_width(const char *s, int l)
469 {
470         if ((output_methods & TO_X) == 0) {
471                 return 0;
472         }
473 #ifdef XFT
474         if (use_xft) {
475                 XGlyphInfo gi;
476
477                 if (utf8_mode) {
478                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
479                                 (const FcChar8 *) s, l, &gi);
480                 } else {
481                         XftTextExtents8(display, fonts[selected_font].xftfont,
482                                 (const FcChar8 *) s, l, &gi);
483                 }
484                 return gi.xOff;
485         } else
486 #endif
487         {
488                 return XTextWidth(fonts[selected_font].font, s, l);
489         }
490 }
491 #endif /* X11 */
492
493 /* formatted text to render on screen, generated in generate_text(),
494  * drawn in draw_stuff() */
495
496 static char *text_buffer;
497
498 /* quite boring functions */
499
500 static inline void for_each_line(char *b, int f(char *, int))
501 {
502         char *ps, *pe;
503         int special_index = 0; /* specials index */
504
505         for (ps = b, pe = b; *pe; pe++) {
506                 if (*pe == '\n') {
507                         *pe = '\0';
508                         special_index = f(ps, special_index);
509                         *pe = '\n';
510                         ps = pe + 1;
511                 }
512         }
513
514         if (ps < pe) {
515                 f(ps, special_index);
516         }
517 }
518
519 static void convert_escapes(char *buf)
520 {
521         char *p = buf, *s = buf;
522
523         while (*s) {
524                 if (*s == '\\') {
525                         s++;
526                         if (*s == 'n') {
527                                 *p++ = '\n';
528                         } else if (*s == '\\') {
529                                 *p++ = '\\';
530                         }
531                         s++;
532                 } else {
533                         *p++ = *s++;
534                 }
535         }
536         *p = '\0';
537 }
538
539 /* Prints anything normally printed with snprintf according to the current value
540  * of use_spacer.  Actually slightly more flexible than snprintf, as you can
541  * safely specify the destination buffer as one of your inputs.  */
542 int spaced_print(char *buf, int size, const char *format, int width, ...)
543 {
544         int len = 0;
545         va_list argp;
546         char *tempbuf;
547
548         if (size < 1) {
549                 return 0;
550         }
551         tempbuf = malloc(size * sizeof(char));
552
553         // Passes the varargs along to vsnprintf
554         va_start(argp, width);
555         vsnprintf(tempbuf, size, format, argp);
556         va_end(argp);
557
558         switch (use_spacer) {
559                 case NO_SPACER:
560                         len = snprintf(buf, size, "%s", tempbuf);
561                         break;
562                 case LEFT_SPACER:
563                         len = snprintf(buf, size, "%*s", width, tempbuf);
564                         break;
565                 case RIGHT_SPACER:
566                         len = snprintf(buf, size, "%-*s", width, tempbuf);
567                         break;
568         }
569         free(tempbuf);
570         return len;
571 }
572
573 /* print percentage values
574  *
575  * - i.e., unsigned values between 0 and 100
576  * - respect the value of pad_percents */
577 static int percent_print(char *buf, int size, unsigned value)
578 {
579         return spaced_print(buf, size, "%u", pad_percents, value);
580 }
581
582 /* converts from bytes to human readable format (K, M, G, T)
583  *
584  * The algorithm always divides by 1024, as unit-conversion of byte
585  * counts suggests. But for output length determination we need to
586  * compare with 1000 here, as we print in decimal form. */
587 static void human_readable(long long num, char *buf, int size)
588 {
589         const char **suffix = suffixes;
590         float fnum;
591         int precision;
592         int width;
593         const char *format;
594
595         /* Possibly just output as usual, for example for stdout usage */
596         if (!format_human_readable) {
597                 spaced_print(buf, size, "%d", 6, round_to_int(num));
598                 return;
599         }
600         if (short_units) {
601                 width = 5;
602                 format = "%.*f%.1s";
603         } else {
604                 width = 7;
605                 format = "%.*f%-3s";
606         }
607
608         if (llabs(num) < 1000LL) {
609                 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
610                 return;
611         }
612
613         while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
614                 num /= 1024;
615                 suffix++;
616         }
617
618         suffix++;
619         fnum = num / 1024.0;
620
621         /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
622          *
623          * The goal is to always have a significance of 3, by
624          * adjusting the decimal part of the number. Sample output:
625          *  123MiB
626          * 23.4GiB
627          * 5.12B   
628          * so the point of alignment resides between number and unit. The
629          * upside of this is that there is minimal padding necessary, though
630          * there should be a way to make alignment take place at the decimal
631          * dot (then with fixed width decimal part). 
632          *
633          * Note the repdigits below: when given a precision value, printf()
634          * rounds the float to it, not just cuts off the remaining digits. So
635          * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
636          * printed with a precision of 0. Yay. */
637
638         precision = 0;          /* print 100-999 without decimal part */
639         if (fnum < 99.95)
640                 precision = 1;  /* print 10-99 with one decimal place */
641         if (fnum < 9.995)
642                 precision = 2;  /* print 0-9 with two decimal places */
643
644         spaced_print(buf, size, format, width, precision, fnum, *suffix);
645 }
646
647 /* global object list root element */
648 static struct text_object global_root_object;
649
650 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
651 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
652 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
653 FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
654         int ends[2];
655         int parentend, childend;
656
657         //by running pipe after the strcmp's we make sure that we don't have to create a pipe
658         //and close the ends if mode is something illegal
659         if(strcmp(mode, "r") == 0) {
660                 if(pipe(ends) != 0) {
661                         return NULL;
662                 }
663                 parentend = ends[0];
664                 childend = ends[1];
665         } else if(strcmp(mode, "w") == 0) {
666                 if(pipe(ends) != 0) {
667                         return NULL;
668                 }
669                 parentend = ends[1];
670                 childend = ends[0];
671         } else {
672                 return NULL;
673         }
674         *child = fork();
675         if(*child == -1) {
676                 close(parentend);
677                 close(childend);
678                 return NULL;
679         } else if(*child > 0) {
680                 close(childend);
681                 waitpid(*child, NULL, 0);
682         } else {
683                 //don't read from both stdin and pipe or write to both stdout and pipe
684                 if(childend == ends[0]) {
685                         close(0);
686                 } else {
687                         close(1);
688                 }
689                 dup(childend);  //by dupping childend, the returned fd will have close-on-exec turned off
690                 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
691                 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail) 
692         }
693         return fdopen(parentend, mode);
694 }
695
696 static inline void read_exec(const char *data, char *buf, const int size)
697 {
698         FILE *fp;
699
700         alarm(update_interval);
701         fp = pid_popen(data, "r", &childpid);
702         if(fp) {
703                 int length;
704
705                 length = fread(buf, 1, size, fp);
706                 pclose(fp);
707                 buf[length] = '\0';
708                 if (length > 0 && buf[length - 1] == '\n') {
709                         buf[length - 1] = '\0';
710                 }
711         } else {
712                 buf[0] = '\0';
713         }
714         alarm(0);
715 }
716
717 void *threaded_exec(void *) __attribute__((noreturn));
718
719 void *threaded_exec(void *arg)
720 {
721         char *buff, *p2;
722         struct text_object *obj = (struct text_object *)arg;
723
724         while (1) {
725                 buff = malloc(text_buffer_size);
726                 read_exec(obj->data.texeci.cmd, buff,
727                         text_buffer_size);
728                 p2 = buff;
729                 while (*p2) {
730                         if (*p2 == '\001') {
731                                 *p2 = ' ';
732                         }
733                         p2++;
734                 }
735                 timed_thread_lock(obj->data.texeci.p_timed_thread);
736                 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
737                 timed_thread_unlock(obj->data.texeci.p_timed_thread);
738                 free(buff);
739                 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
740                         timed_thread_exit(obj->data.texeci.p_timed_thread);
741                 }
742         }
743         /* never reached */
744 }
745
746 static struct text_object *new_text_object_internal(void)
747 {
748         struct text_object *obj = malloc(sizeof(struct text_object));
749         memset(obj, 0, sizeof(struct text_object));
750         return obj;
751 }
752
753 /*
754  * Frees the list of text objects root points to.  When internal = 1, it won't
755  * free global objects.
756  */
757 static void free_text_objects(struct text_object *root, int internal)
758 {
759         struct text_object *obj;
760
761         if (!root->prev) {
762                 return;
763         }
764
765 #define data obj->data
766         for (obj = root->prev; obj; obj = root->prev) {
767                 root->prev = obj->prev;
768                 switch (obj->type) {
769 #ifndef __OpenBSD__
770                         case OBJ_acpitemp:
771                                 close(data.i);
772                                 break;
773 #endif /* !__OpenBSD__ */
774 #ifdef __linux__
775                         case OBJ_i2c:
776                         case OBJ_platform:
777                         case OBJ_hwmon:
778                                 close(data.sysfs.fd);
779                                 break;
780 #endif /* __linux__ */
781                         case OBJ_read_tcp:
782                                 free(data.read_tcp.host);
783                                 break;
784                         case OBJ_time:
785                         case OBJ_utime:
786                                 free(data.s);
787                                 break;
788                         case OBJ_tztime:
789                                 free(data.tztime.tz);
790                                 free(data.tztime.fmt);
791                                 break;
792                         case OBJ_mboxscan:
793                                 free(data.mboxscan.args);
794                                 free(data.mboxscan.output);
795                                 break;
796                         case OBJ_mails:
797                         case OBJ_new_mails:
798                         case OBJ_seen_mails:
799                         case OBJ_unseen_mails:
800                         case OBJ_flagged_mails:
801                         case OBJ_unflagged_mails:
802                         case OBJ_forwarded_mails:
803                         case OBJ_unforwarded_mails:
804                         case OBJ_replied_mails:
805                         case OBJ_unreplied_mails:
806                         case OBJ_draft_mails:
807                         case OBJ_trashed_mails:
808                                 free(data.local_mail.mbox);
809                                 break;
810                         case OBJ_imap_unseen:
811                                 if (!obj->char_b) {
812                                         free(data.mail);
813                                 }
814                                 break;
815                         case OBJ_imap_messages:
816                                 if (!obj->char_b) {
817                                         free(data.mail);
818                                 }
819                                 break;
820                         case OBJ_pop3_unseen:
821                                 if (!obj->char_b) {
822                                         free(data.mail);
823                                 }
824                                 break;
825                         case OBJ_pop3_used:
826                                 if (!obj->char_b) {
827                                         free(data.mail);
828                                 }
829                                 break;
830                         case OBJ_if_empty:
831                         case OBJ_if_match:
832                                 free_text_objects(obj->sub, 1);
833                                 free(obj->sub);
834                                 /* fall through */
835                         case OBJ_if_existing:
836                         case OBJ_if_mounted:
837                         case OBJ_if_running:
838                                 free(data.ifblock.s);
839                                 free(data.ifblock.str);
840                                 break;
841                         case OBJ_head:
842                         case OBJ_tail:
843                                 free(data.headtail.logfile);
844                                 if(data.headtail.buffer) {
845                                         free(data.headtail.buffer);
846                                 }
847                                 break;
848                         case OBJ_text:
849                         case OBJ_font:
850                         case OBJ_image:
851                         case OBJ_eval:
852                         case OBJ_exec:
853                         case OBJ_execbar:
854 #ifdef X11
855                         case OBJ_execgauge:
856                         case OBJ_execgraph:
857 #endif
858                         case OBJ_execp:
859                                 free(data.s);
860                                 break;
861 #ifdef HAVE_ICONV
862                         case OBJ_iconv_start:
863                                 free_iconv();
864                                 break;
865 #endif
866 #ifdef __linux__
867                         case OBJ_disk_protect:
868                                 free(data.s);
869                                 break;
870                         case OBJ_if_gw:
871                                 free(data.ifblock.s);
872                                 free(data.ifblock.str);
873                         case OBJ_gw_iface:
874                         case OBJ_gw_ip:
875                                 if (info.gw_info.iface) {
876                                         free(info.gw_info.iface);
877                                         info.gw_info.iface = 0;
878                                 }
879                                 if (info.gw_info.ip) {
880                                         free(info.gw_info.ip);
881                                         info.gw_info.ip = 0;
882                                 }
883                                 break;
884                         case OBJ_ioscheduler:
885                                 if(data.s)
886                                         free(data.s);
887                                 break;
888 #endif
889 #if (defined(__FreeBSD__) || defined(__linux__))
890                         case OBJ_if_up:
891                                 free(data.ifblock.s);
892                                 free(data.ifblock.str);
893                                 break;
894 #endif
895 #ifdef XMMS2
896                         case OBJ_xmms2_artist:
897                                 if (info.xmms2.artist) {
898                                         free(info.xmms2.artist);
899                                         info.xmms2.artist = 0;
900                                 }
901                                 break;
902                         case OBJ_xmms2_album:
903                                 if (info.xmms2.album) {
904                                         free(info.xmms2.album);
905                                         info.xmms2.album = 0;
906                                 }
907                                 break;
908                         case OBJ_xmms2_title:
909                                 if (info.xmms2.title) {
910                                         free(info.xmms2.title);
911                                         info.xmms2.title = 0;
912                                 }
913                                 break;
914                         case OBJ_xmms2_genre:
915                                 if (info.xmms2.genre) {
916                                         free(info.xmms2.genre);
917                                         info.xmms2.genre = 0;
918                                 }
919                                 break;
920                         case OBJ_xmms2_comment:
921                                 if (info.xmms2.comment) {
922                                         free(info.xmms2.comment);
923                                         info.xmms2.comment = 0;
924                                 }
925                                 break;
926                         case OBJ_xmms2_url:
927                                 if (info.xmms2.url) {
928                                         free(info.xmms2.url);
929                                         info.xmms2.url = 0;
930                                 }
931                                 break;
932                         case OBJ_xmms2_date:
933                                 if (info.xmms2.date) {
934                                         free(info.xmms2.date);
935                                         info.xmms2.date = 0;
936                                 }
937                                 break;
938                         case OBJ_xmms2_status:
939                                 if (info.xmms2.status) {
940                                         free(info.xmms2.status);
941                                         info.xmms2.status = 0;
942                                 }
943                                 break;
944                         case OBJ_xmms2_playlist:
945                                 if (info.xmms2.playlist) {
946                                         free(info.xmms2.playlist);
947                                         info.xmms2.playlist = 0;
948                                 }
949                                 break;
950                         case OBJ_xmms2_smart:
951                                 if (info.xmms2.artist) {
952                                         free(info.xmms2.artist);
953                                         info.xmms2.artist = 0;
954                                 }
955                                 if (info.xmms2.title) {
956                                         free(info.xmms2.title);
957                                         info.xmms2.title = 0;
958                                 }
959                                 if (info.xmms2.url) {
960                                         free(info.xmms2.url);
961                                         info.xmms2.url = 0;
962                                 }
963                                 break;
964 #endif
965 #ifdef BMPX
966                         case OBJ_bmpx_title:
967                         case OBJ_bmpx_artist:
968                         case OBJ_bmpx_album:
969                         case OBJ_bmpx_track:
970                         case OBJ_bmpx_uri:
971                         case OBJ_bmpx_bitrate:
972                                 break;
973 #endif
974 #ifdef EVE
975                         case OBJ_eve:
976                                 break;
977 #endif
978 #ifdef HAVE_CURL
979                         case OBJ_curl:
980                                 free(data.curl.uri);
981                                 break;
982 #endif
983 #ifdef RSS
984                         case OBJ_rss:
985                                 free(data.rss.uri);
986                                 free(data.rss.action);
987                                 break;
988 #endif
989 #ifdef WEATHER
990                         case OBJ_weather:
991                                 free(data.weather.uri);
992                                 free(data.weather.data_type);
993                                 break;
994 #endif
995 #ifdef HAVE_LUA
996                         case OBJ_lua:
997                         case OBJ_lua_parse:
998                         case OBJ_lua_bar:
999 #ifdef X11
1000                         case OBJ_lua_graph:
1001                         case OBJ_lua_gauge:
1002 #endif /* X11 */
1003                                 free(data.s);
1004                                 break;
1005 #endif /* HAVE_LUA */
1006                         case OBJ_pre_exec:
1007                                 break;
1008 #ifndef __OpenBSD__
1009                         case OBJ_battery:
1010                                 free(data.s);
1011                                 break;
1012                         case OBJ_battery_short:
1013                                 free(data.s);
1014                                 break;
1015                         case OBJ_battery_time:
1016                                 free(data.s);
1017                                 break;
1018 #endif /* !__OpenBSD__ */
1019                         case OBJ_execpi:
1020                         case OBJ_execi:
1021                         case OBJ_execibar:
1022 #ifdef X11
1023                         case OBJ_execigraph:
1024                         case OBJ_execigauge:
1025 #endif /* X11 */
1026                                 free(data.execi.cmd);
1027                                 free(data.execi.buffer);
1028                                 break;
1029                         case OBJ_texeci:
1030                                 if (data.texeci.p_timed_thread) timed_thread_destroy(data.texeci.p_timed_thread, &data.texeci.p_timed_thread);
1031                                 free(data.texeci.cmd);
1032                                 free(data.texeci.buffer);
1033                                 break;
1034                         case OBJ_nameserver:
1035                                 free_dns_data();
1036                                 break;
1037                         case OBJ_top:
1038                         case OBJ_top_mem:
1039                         case OBJ_top_time:
1040 #ifdef IOSTATS
1041                         case OBJ_top_io:
1042 #endif
1043                                 if (info.first_process && !internal) {
1044                                         free_all_processes();
1045                                         info.first_process = NULL;
1046                                 }
1047                                 if (data.top.s) free(data.top.s);
1048                                 break;
1049 #ifdef HDDTEMP
1050                         case OBJ_hddtemp:
1051                                 free(data.hddtemp.dev);
1052                                 free(data.hddtemp.addr);
1053                                 if (data.hddtemp.temp)
1054                                         free(data.hddtemp.temp);
1055                                 break;
1056 #endif /* HDDTEMP */
1057                         case OBJ_entropy_avail:
1058                         case OBJ_entropy_perc:
1059                         case OBJ_entropy_poolsize:
1060                         case OBJ_entropy_bar:
1061                                 break;
1062                         case OBJ_user_names:
1063                                 if (info.users.names) {
1064                                         free(info.users.names);
1065                                         info.users.names = 0;
1066                                 }
1067                                 break;
1068                         case OBJ_user_terms:
1069                                 if (info.users.terms) {
1070                                         free(info.users.terms);
1071                                         info.users.terms = 0;
1072                                 }
1073                                 break;
1074                         case OBJ_user_times:
1075                                 if (info.users.times) {
1076                                         free(info.users.times);
1077                                         info.users.times = 0;
1078                                 }
1079                                 break;
1080 #ifdef IBM
1081                         case OBJ_smapi:
1082                         case OBJ_smapi_bat_perc:
1083                         case OBJ_smapi_bat_temp:
1084                         case OBJ_smapi_bat_power:
1085                                 free(data.s);
1086                                 break;
1087                         case OBJ_if_smapi_bat_installed:
1088                                 free(data.ifblock.s);
1089                                 free(data.ifblock.str);
1090                                 break;
1091 #endif /* IBM */
1092 #ifdef NVIDIA
1093                         case OBJ_nvidia:
1094                                 break;
1095 #endif /* NVIDIA */
1096 #ifdef MPD
1097                         case OBJ_mpd_title:
1098                         case OBJ_mpd_artist:
1099                         case OBJ_mpd_album:
1100                         case OBJ_mpd_random:
1101                         case OBJ_mpd_repeat:
1102                         case OBJ_mpd_vol:
1103                         case OBJ_mpd_bitrate:
1104                         case OBJ_mpd_status:
1105                         case OBJ_mpd_bar:
1106                         case OBJ_mpd_elapsed:
1107                         case OBJ_mpd_length:
1108                         case OBJ_mpd_track:
1109                         case OBJ_mpd_name:
1110                         case OBJ_mpd_file:
1111                         case OBJ_mpd_percent:
1112                         case OBJ_mpd_smart:
1113                         case OBJ_if_mpd_playing:
1114                                 free_mpd();
1115                                 break;
1116 #endif /* MPD */
1117 #ifdef MOC
1118                         case OBJ_moc_state:
1119                         case OBJ_moc_file:
1120                         case OBJ_moc_title:
1121                         case OBJ_moc_artist:
1122                         case OBJ_moc_song:
1123                         case OBJ_moc_album:
1124                         case OBJ_moc_totaltime:
1125                         case OBJ_moc_timeleft:
1126                         case OBJ_moc_curtime:
1127                         case OBJ_moc_bitrate:
1128                         case OBJ_moc_rate:
1129                                 free_moc();
1130                                 break;
1131 #endif /* MOC */
1132                         case OBJ_include:
1133                         case OBJ_blink:
1134                         case OBJ_to_bytes:
1135                                 if(obj->sub) {
1136                                         free_text_objects(obj->sub, 1);
1137                                         free(obj->sub);
1138                                 }
1139                                 break;
1140                         case OBJ_scroll:
1141                                 free(data.scroll.text);
1142                                 free_text_objects(obj->sub, 1);
1143                                 free(obj->sub);
1144                                 break;
1145                         case OBJ_combine:
1146                                 free(data.combine.left);
1147                                 free(data.combine.seperation);
1148                                 free(data.combine.right);
1149                                 free_text_objects(obj->sub, 1);
1150                                 free(obj->sub);
1151                                 break;
1152 #ifdef APCUPSD
1153                         case OBJ_apcupsd:
1154                         case OBJ_apcupsd_name:
1155                         case OBJ_apcupsd_model:
1156                         case OBJ_apcupsd_upsmode:
1157                         case OBJ_apcupsd_cable:
1158                         case OBJ_apcupsd_status:
1159                         case OBJ_apcupsd_linev:
1160                         case OBJ_apcupsd_load:
1161                         case OBJ_apcupsd_loadbar:
1162 #ifdef X11
1163                         case OBJ_apcupsd_loadgraph:
1164                         case OBJ_apcupsd_loadgauge:
1165 #endif /* X11 */
1166                         case OBJ_apcupsd_charge:
1167                         case OBJ_apcupsd_timeleft:
1168                         case OBJ_apcupsd_temp:
1169                         case OBJ_apcupsd_lastxfer:
1170                                 break;
1171 #endif /* APCUPSD */
1172 #ifdef X11
1173                         case OBJ_desktop:
1174                         case OBJ_desktop_number:
1175                         case OBJ_desktop_name:
1176                                 if(info.x11.desktop.name) {
1177                                   free(info.x11.desktop.name);
1178                                   info.x11.desktop.name = NULL;
1179                                 }
1180                                 if(info.x11.desktop.all_names) {
1181                                   free(info.x11.desktop.all_names);
1182                                   info.x11.desktop.all_names = NULL;
1183                                 }
1184                                 break;
1185 #endif /* X11 */
1186                 }
1187                 free(obj);
1188         }
1189 #undef data
1190 }
1191
1192 #ifdef X11
1193 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1194 {
1195         char buf1[64];
1196         int n;
1197
1198         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1199                 *a = mixer_init(buf1);
1200                 scan_bar(arg + n, w, h);
1201         } else {
1202                 *a = mixer_init(NULL);
1203                 scan_bar(arg, w, h);
1204         }
1205 }
1206 #endif /* X11 */
1207
1208 /* strip a leading /dev/ if any, following symlinks first
1209  *
1210  * BEWARE: this function returns a pointer to static content
1211  *         which gets overwritten in consecutive calls. I.e.:
1212  *         this function is NOT reentrant.
1213  */
1214 static const char *dev_name(const char *path)
1215 {
1216         static char buf[255];   /* should be enough for pathnames */
1217         ssize_t buflen;
1218
1219         if (!path)
1220                 return NULL;
1221
1222 #define DEV_NAME(x) \
1223   x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1224         if ((buflen = readlink(path, buf, 254)) == -1)
1225                 return DEV_NAME(path);
1226         buf[buflen] = '\0';
1227         return DEV_NAME(buf);
1228 #undef DEV_NAME
1229 }
1230
1231 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1232 {
1233         char buf[64];
1234         int n;
1235
1236         if (obj->data.top.was_parsed) {
1237                 return 1;
1238         }
1239         obj->data.top.was_parsed = 1;
1240
1241         if (arg && !obj->data.top.s) {
1242                 obj->data.top.s = strndup(arg, text_buffer_size);
1243         }
1244
1245         need_mask |= (1 << INFO_TOP);
1246
1247         if (s[3] == 0) {
1248                 obj->type = OBJ_top;
1249                 top_cpu = 1;
1250         } else if (strcmp(&s[3], "_mem") == EQUAL) {
1251                 obj->type = OBJ_top_mem;
1252                 top_mem = 1;
1253         } else if (strcmp(&s[3], "_time") == EQUAL) {
1254                 obj->type = OBJ_top_time;
1255                 top_time = 1;
1256 #ifdef IOSTATS
1257         } else if (strcmp(&s[3], "_io") == EQUAL) {
1258                 obj->type = OBJ_top_io;
1259                 top_io = 1;
1260 #endif
1261         } else {
1262 #ifdef IOSTATS
1263                 NORM_ERR("Must be top, top_mem, top_time or top_io");
1264 #else
1265                 NORM_ERR("Must be top, top_mem or top_time");
1266 #endif
1267                 return 0;
1268         }
1269
1270         if (!arg) {
1271                 NORM_ERR("top needs arguments");
1272                 return 0;
1273         }
1274
1275         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1276                 if (strcmp(buf, "name") == EQUAL) {
1277                         obj->data.top.type = TOP_NAME;
1278                 } else if (strcmp(buf, "cpu") == EQUAL) {
1279                         obj->data.top.type = TOP_CPU;
1280                 } else if (strcmp(buf, "pid") == EQUAL) {
1281                         obj->data.top.type = TOP_PID;
1282                 } else if (strcmp(buf, "mem") == EQUAL) {
1283                         obj->data.top.type = TOP_MEM;
1284                 } else if (strcmp(buf, "time") == EQUAL) {
1285                         obj->data.top.type = TOP_TIME;
1286                 } else if (strcmp(buf, "mem_res") == EQUAL) {
1287                         obj->data.top.type = TOP_MEM_RES;
1288                 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1289                         obj->data.top.type = TOP_MEM_VSIZE;
1290 #ifdef IOSTATS
1291                 } else if (strcmp(buf, "io_read") == EQUAL) {
1292                         obj->data.top.type = TOP_READ_BYTES;
1293                 } else if (strcmp(buf, "io_write") == EQUAL) {
1294                         obj->data.top.type = TOP_WRITE_BYTES;
1295                 } else if (strcmp(buf, "io_perc") == EQUAL) {
1296                         obj->data.top.type = TOP_IO_PERC;
1297 #endif
1298                 } else {
1299                         NORM_ERR("invalid type arg for top");
1300 #ifdef IOSTATS
1301                         NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1302                                         "io_read, io_write, io_perc");
1303 #else
1304                         NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1305 #endif
1306                         return 0;
1307                 }
1308                 if (n < 1 || n > 10) {
1309                         NORM_ERR("invalid num arg for top. Must be between 1 and 10.");
1310                         return 0;
1311                 } else {
1312                         obj->data.top.num = n - 1;
1313                 }
1314         } else {
1315                 NORM_ERR("invalid argument count for top");
1316                 return 0;
1317         }
1318         return 1;
1319 }
1320
1321 long current_text_color;
1322
1323 struct conftree {
1324         char* string;
1325         struct conftree* horz_next;
1326         struct conftree* vert_next;
1327         struct conftree* back;
1328 };
1329
1330 //adds newstring to to the tree unless you can already see it when travelling back.
1331 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
1332 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
1333         struct conftree* node;
1334         struct conftree* node2;
1335
1336         for(node = previous; node != NULL; node = node->back) {
1337                 if(strcmp(node->string, newstring) == 0) {
1338                         return NULL;
1339                 }
1340         }
1341         node = malloc(sizeof(struct conftree));
1342         if (previous != NULL) {
1343                 if(previous->vert_next == NULL) {
1344                         previous->vert_next = node;
1345                 } else {
1346                         for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
1347                         node2->horz_next = node;
1348                 }
1349         }
1350         node->string = strdup(newstring);
1351         node->horz_next = NULL;
1352         node->vert_next = NULL;
1353         node->back = previous;
1354         return node;
1355 }
1356
1357 void conftree_empty(struct conftree* tree) {
1358         if(tree) {
1359                 conftree_empty(tree->horz_next);
1360                 conftree_empty(tree->vert_next);
1361                 free(tree->string);
1362                 free(tree);
1363         }
1364 }
1365
1366 struct conftree *currentconffile;
1367
1368 char load_config_file(const char *);
1369
1370 /* construct_text_object() creates a new text_object */
1371 static struct text_object *construct_text_object(const char *s,
1372                 const char *arg, long line, void **ifblock_opaque, void *free_at_crash)
1373 {
1374         // struct text_object *obj = new_text_object();
1375         struct text_object *obj = new_text_object_internal();
1376
1377         obj->line = line;
1378
1379 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1380         obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1381 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1382         obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1383         obj_be_ifblock_if(ifblock_opaque, obj); {
1384 #define END } } else
1385
1386 #define SIZE_DEFAULTS(arg) { \
1387         obj->a = default_##arg##_width; \
1388         obj->b = default_##arg##_height; \
1389 }
1390
1391 #ifdef X11
1392         if (s[0] == '#') {
1393                 obj->type = OBJ_color;
1394                 obj->data.l = get_x11_color(s);
1395         } else
1396 #endif /* X11 */
1397 #ifdef __OpenBSD__
1398         OBJ(freq, INFO_FREQ)
1399 #else
1400         OBJ(acpitemp, 0)
1401                 obj->data.i = open_acpi_temperature(arg);
1402         END OBJ(acpiacadapter, 0)
1403         END OBJ(freq, INFO_FREQ)
1404 #endif /* !__OpenBSD__ */
1405                 get_cpu_count();
1406                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1407                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1408                         obj->data.cpu_index = 1;
1409                         /* NORM_ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1410                                 "Displaying the clock for CPU 1."); */
1411                 } else {
1412                         obj->data.cpu_index = atoi(&arg[0]);
1413                 }
1414                 obj->a = 1;
1415         END OBJ(freq_g, INFO_FREQ)
1416                 get_cpu_count();
1417                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1418                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1419                         obj->data.cpu_index = 1;
1420                         /* NORM_ERR("freq_g: Invalid CPU number or you don't have that many "
1421                                 "CPUs! Displaying the clock for CPU 1."); */
1422                 } else {
1423                         obj->data.cpu_index = atoi(&arg[0]);
1424                 }
1425                 obj->a = 1;
1426         END OBJ(read_tcp, 0)
1427                 if (arg) {
1428                         obj->data.read_tcp.host = malloc(text_buffer_size);
1429                         sscanf(arg, "%s", obj->data.read_tcp.host);
1430                         sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1431                         if(obj->data.read_tcp.port == 0) {
1432                                 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1433                                 strcpy(obj->data.read_tcp.host,"localhost");
1434                         }
1435                         obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1436                         if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1437                                 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1438                         }
1439                 }else{
1440                         CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1441                 }
1442 #if defined(__linux__)
1443         END OBJ(voltage_mv, 0)
1444                 get_cpu_count();
1445                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1446                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1447                         obj->data.cpu_index = 1;
1448                         /* NORM_ERR("voltage_mv: Invalid CPU number or you don't have that many "
1449                                 "CPUs! Displaying voltage for CPU 1."); */
1450                 } else {
1451                         obj->data.cpu_index = atoi(&arg[0]);
1452                 }
1453                 obj->a = 1;
1454         END OBJ(voltage_v, 0)
1455                 get_cpu_count();
1456                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1457                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1458                         obj->data.cpu_index = 1;
1459                         /* NORM_ERR("voltage_v: Invalid CPU number or you don't have that many "
1460                                 "CPUs! Displaying voltage for CPU 1."); */
1461                 } else {
1462                         obj->data.cpu_index = atoi(&arg[0]);
1463                 }
1464                 obj->a = 1;
1465
1466 #ifdef HAVE_IWLIB
1467         END OBJ(wireless_essid, INFO_NET)
1468                 if (arg) {
1469                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1470                 } else {
1471                         // default to DEFAULTNETDEV
1472                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1473                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1474                         free(buf);
1475                 }
1476         END OBJ(wireless_mode, INFO_NET)
1477                 if (arg) {
1478                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1479                 } else {
1480                         // default to DEFAULTNETDEV
1481                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1482                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1483                         free(buf);
1484                 }
1485         END OBJ(wireless_bitrate, INFO_NET)
1486                 if (arg) {
1487                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1488                 } else {
1489                         // default to DEFAULTNETDEV
1490                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1491                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1492                         free(buf);
1493                 }
1494         END OBJ(wireless_ap, INFO_NET)
1495                 if (arg) {
1496                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1497                 } else {
1498                         // default to DEFAULTNETDEV
1499                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1500                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1501                         free(buf);
1502                 }
1503         END OBJ(wireless_link_qual, INFO_NET)
1504                 if (arg) {
1505                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1506                 } else {
1507                         // default to DEFAULTNETDEV
1508                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1509                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1510                         free(buf);
1511                 }
1512         END OBJ(wireless_link_qual_max, INFO_NET)
1513                 if (arg) {
1514                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1515                 } else {
1516                         // default to DEFAULTNETDEV
1517                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1518                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1519                         free(buf);
1520                 }
1521         END OBJ(wireless_link_qual_perc, INFO_NET)
1522                 if (arg) {
1523                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1524                 } else {
1525                         // default to DEFAULTNETDEV
1526                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1527                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1528                         free(buf);
1529                 }
1530         END OBJ(wireless_link_bar, INFO_NET)
1531                 SIZE_DEFAULTS(bar);
1532                 if (arg) {
1533                         arg = scan_bar(arg, &obj->a, &obj->b);
1534                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1535                 } else {
1536                         // default to DEFAULTNETDEV
1537                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1538                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1539                         free(buf);
1540                 }
1541 #endif /* HAVE_IWLIB */
1542
1543 #endif /* __linux__ */
1544
1545 #ifndef __OpenBSD__
1546         END OBJ(acpifan, 0)
1547         END OBJ(battery, 0)
1548                 char bat[64];
1549
1550                 if (arg) {
1551                         sscanf(arg, "%63s", bat);
1552                 } else {
1553                         strcpy(bat, "BAT0");
1554                 }
1555                 obj->data.s = strndup(bat, text_buffer_size);
1556         END OBJ(battery_short, 0)
1557                 char bat[64];
1558
1559                 if (arg) {
1560                         sscanf(arg, "%63s", bat);
1561                 } else {
1562                         strcpy(bat, "BAT0");
1563                 }
1564                 obj->data.s = strndup(bat, text_buffer_size);
1565         END OBJ(battery_time, 0)
1566                 char bat[64];
1567
1568                 if (arg) {
1569                         sscanf(arg, "%63s", bat);
1570                 } else {
1571                         strcpy(bat, "BAT0");
1572                 }
1573                 obj->data.s = strndup(bat, text_buffer_size);
1574         END OBJ(battery_percent, 0)
1575                 char bat[64];
1576
1577                 if (arg) {
1578                         sscanf(arg, "%63s", bat);
1579                 } else {
1580                         strcpy(bat, "BAT0");
1581                 }
1582                 obj->data.s = strndup(bat, text_buffer_size);
1583         END OBJ(battery_bar, 0)
1584                 char bat[64];
1585                 SIZE_DEFAULTS(bar);
1586                 obj->b = 6;
1587                 if (arg) {
1588                         arg = scan_bar(arg, &obj->a, &obj->b);
1589                         sscanf(arg, "%63s", bat);
1590                 } else {
1591                         strcpy(bat, "BAT0");
1592                 }
1593                 obj->data.s = strndup(bat, text_buffer_size);
1594 #endif /* !__OpenBSD__ */
1595
1596 #if defined(__linux__)
1597         END OBJ(disk_protect, 0)
1598                 if (arg)
1599                         obj->data.s = strndup(dev_name(arg), text_buffer_size);
1600                 else
1601                         CRIT_ERR(obj, free_at_crash, "disk_protect needs an argument");
1602         END OBJ(i8k_version, INFO_I8K)
1603         END OBJ(i8k_bios, INFO_I8K)
1604         END OBJ(i8k_serial, INFO_I8K)
1605         END OBJ(i8k_cpu_temp, INFO_I8K)
1606         END OBJ(i8k_left_fan_status, INFO_I8K)
1607         END OBJ(i8k_right_fan_status, INFO_I8K)
1608         END OBJ(i8k_left_fan_rpm, INFO_I8K)
1609         END OBJ(i8k_right_fan_rpm, INFO_I8K)
1610         END OBJ(i8k_ac_status, INFO_I8K)
1611         END OBJ(i8k_buttons_status, INFO_I8K)
1612 #if defined(IBM)
1613         END OBJ(ibm_fan, 0)
1614         END OBJ(ibm_temps, 0)
1615                 if (!arg) {
1616                         CRIT_ERR(obj, free_at_crash, "ibm_temps: needs an argument");
1617                 }
1618                 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1619                         obj->data.sensor = 0;
1620                         NORM_ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1621                                 "Using 0 (CPU temp sensor).");
1622                 }
1623                 obj->data.sensor = atoi(&arg[0]);
1624         END OBJ(ibm_volume, 0)
1625         END OBJ(ibm_brightness, 0)
1626 #endif
1627         /* information from sony_laptop kernel module
1628          * /sys/devices/platform/sony-laptop */
1629         END OBJ(sony_fanspeed, 0)
1630         END OBJ_IF(if_gw, INFO_GW)
1631         END OBJ(ioscheduler, 0)
1632                 if (!arg) {
1633                         CRIT_ERR(obj, free_at_crash, "get_ioscheduler needs an argument (e.g. hda)");
1634                         obj->data.s = 0;
1635                 } else
1636                         obj->data.s = strndup(dev_name(arg), text_buffer_size);
1637         END OBJ(laptop_mode, 0)
1638         END OBJ(pb_battery, 0)
1639                 if (arg && strcmp(arg, "status") == EQUAL) {
1640                         obj->data.i = PB_BATT_STATUS;
1641                 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1642                         obj->data.i = PB_BATT_PERCENT;
1643                 } else if (arg && strcmp(arg, "time") == EQUAL) {
1644                         obj->data.i = PB_BATT_TIME;
1645                 } else {
1646                         NORM_ERR("pb_battery: needs one argument: status, percent or time");
1647                         free(obj);
1648                         return NULL;
1649                 }
1650
1651 #endif /* __linux__ */
1652 #if (defined(__FreeBSD__) || defined(__linux__))
1653         END OBJ_IF(if_up, 0)
1654                 if (!arg) {
1655                         NORM_ERR("if_up needs an argument");
1656                         obj->data.ifblock.s = 0;
1657                 } else {
1658                         obj->data.ifblock.s = strndup(arg, text_buffer_size);
1659                 }
1660 #endif
1661 #if defined(__OpenBSD__)
1662         END OBJ(obsd_sensors_temp, 0)
1663                 if (!arg) {
1664                         CRIT_ERR(obj, free_at_crash, "obsd_sensors_temp: needs an argument");
1665                 }
1666                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1667                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1668                         obj->data.sensor = 0;
1669                         NORM_ERR("Invalid temperature sensor number!");
1670                 }
1671                 obj->data.sensor = atoi(&arg[0]);
1672         END OBJ(obsd_sensors_fan, 0)
1673                 if (!arg) {
1674                         CRIT_ERR(obj, free_at_crash, "obsd_sensors_fan: needs 2 arguments (device and sensor "
1675                                 "number)");
1676                 }
1677                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1678                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1679                         obj->data.sensor = 0;
1680                         NORM_ERR("Invalid fan sensor number!");
1681                 }
1682                 obj->data.sensor = atoi(&arg[0]);
1683         END OBJ(obsd_sensors_volt, 0)
1684                 if (!arg) {
1685                         CRIT_ERR(obj, free_at_crash, "obsd_sensors_volt: needs 2 arguments (device and sensor "
1686                                 "number)");
1687                 }
1688                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1689                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1690                         obj->data.sensor = 0;
1691                         NORM_ERR("Invalid voltage sensor number!");
1692                 }
1693                 obj->data.sensor = atoi(&arg[0]);
1694         END OBJ(obsd_vendor, 0)
1695         END OBJ(obsd_product, 0)
1696 #endif /* __OpenBSD__ */
1697         END OBJ(buffers, INFO_BUFFERS)
1698         END OBJ(cached, INFO_BUFFERS)
1699 #define SCAN_CPU(__arg, __var) { \
1700         int __offset = 0; \
1701         if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1702                 __arg += __offset; \
1703         else \
1704                 __var = 0; \
1705 }
1706         END OBJ(cpu, INFO_CPU)
1707                 SCAN_CPU(arg, obj->data.cpu_index);
1708                 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1709 #ifdef X11
1710         END OBJ(cpugauge, INFO_CPU)
1711                 SIZE_DEFAULTS(gauge);
1712                 SCAN_CPU(arg, obj->data.cpu_index);
1713                 scan_gauge(arg, &obj->a, &obj->b);
1714                 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1715 #endif /* X11 */
1716         END OBJ(cpubar, INFO_CPU)
1717                 SIZE_DEFAULTS(bar);
1718                 SCAN_CPU(arg, obj->data.cpu_index);
1719                 scan_bar(arg, &obj->a, &obj->b);
1720                 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1721 #ifdef X11
1722         END OBJ(cpugraph, INFO_CPU)
1723                 char *buf = 0;
1724                 SIZE_DEFAULTS(graph);
1725                 SCAN_CPU(arg, obj->data.cpu_index);
1726                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1727                         &obj->e, &obj->char_a, &obj->char_b);
1728                 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1729                 if (buf) free(buf);
1730         END OBJ(loadgraph, INFO_LOADAVG)
1731                 char *buf = 0;
1732                 SIZE_DEFAULTS(graph);
1733                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1734                                 &obj->e, &obj->char_a, &obj->char_b);
1735                 if (buf) {
1736                         int a = 1, r = 3;
1737                         if (arg) {
1738                                 r = sscanf(arg, "%d", &a);
1739                         }
1740                         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1741                         free(buf);
1742                 }
1743 #endif /* X11 */
1744         END OBJ(diskio, INFO_DISKIO)
1745                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1746         END OBJ(diskio_read, INFO_DISKIO)
1747                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1748         END OBJ(diskio_write, INFO_DISKIO)
1749                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1750 #ifdef X11
1751         END OBJ(diskiograph, INFO_DISKIO)
1752                 char *buf = 0;
1753                 SIZE_DEFAULTS(graph);
1754                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1755                                 &obj->e, &obj->char_a, &obj->char_b);
1756
1757                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1758                 if (buf) free(buf);
1759         END OBJ(diskiograph_read, INFO_DISKIO)
1760                 char *buf = 0;
1761                 SIZE_DEFAULTS(graph);
1762                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1763                                 &obj->e, &obj->char_a, &obj->char_b);
1764
1765                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1766                 if (buf) free(buf);
1767         END OBJ(diskiograph_write, INFO_DISKIO)
1768                 char *buf = 0;
1769                 SIZE_DEFAULTS(graph);
1770                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1771                                 &obj->e, &obj->char_a, &obj->char_b);
1772
1773                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1774                 if (buf) free(buf);
1775 #endif /* X11 */
1776         END OBJ(color, 0)
1777 #ifdef X11
1778                 if (output_methods & TO_X) {
1779                         obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1780                         current_text_color = obj->data.l;
1781                 }
1782 #endif /* X11 */
1783         END OBJ(color0, 0)
1784                 obj->data.l = color0;
1785                 current_text_color = obj->data.l;
1786         END OBJ(color1, 0)
1787                 obj->data.l = color1;
1788                 current_text_color = obj->data.l;
1789         END OBJ(color2, 0)
1790                 obj->data.l = color2;
1791                 current_text_color = obj->data.l;
1792         END OBJ(color3, 0)
1793                 obj->data.l = color3;
1794                 current_text_color = obj->data.l;
1795         END OBJ(color4, 0)
1796                 obj->data.l = color4;
1797                 current_text_color = obj->data.l;
1798         END OBJ(color5, 0)
1799                 obj->data.l = color5;
1800                 current_text_color = obj->data.l;
1801         END OBJ(color6, 0)
1802                 obj->data.l = color6;
1803                 current_text_color = obj->data.l;
1804         END OBJ(color7, 0)
1805                 obj->data.l = color7;
1806                 current_text_color = obj->data.l;
1807         END OBJ(color8, 0)
1808                 obj->data.l = color8;
1809                 current_text_color = obj->data.l;
1810         END OBJ(color9, 0)
1811                 obj->data.l = color9;
1812                 current_text_color = obj->data.l;
1813 #ifdef X11
1814         END OBJ(font, 0)
1815                 obj->data.s = scan_font(arg);
1816 #endif /* X11 */
1817         END OBJ(conky_version, 0)
1818         END OBJ(conky_build_date, 0)
1819         END OBJ(conky_build_arch, 0)
1820         END OBJ(downspeed, INFO_NET)
1821                 if (arg) {
1822                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1823                 } else {
1824                         // default to DEFAULTNETDEV
1825                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1826                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1827                         free(buf);
1828                 }
1829         END OBJ(downspeedf, INFO_NET)
1830                 if (arg) {
1831                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1832                 } else {
1833                         // default to DEFAULTNETDEV
1834                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1835                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1836                         free(buf);
1837                 }
1838 #ifdef X11
1839         END OBJ(downspeedgraph, INFO_NET)
1840                 char *buf = 0;
1841                 SIZE_DEFAULTS(graph);
1842                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1843                                 &obj->e, &obj->char_a, &obj->char_b);
1844
1845                 // default to DEFAULTNETDEV
1846                 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1847                 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1848                 free(buf);
1849 #endif /* X11 */
1850         END OBJ(else, 0)
1851                 obj_be_ifblock_else(ifblock_opaque, obj);
1852         END OBJ(endif, 0)
1853                 obj_be_ifblock_endif(ifblock_opaque, obj);
1854         END OBJ(eval, 0)
1855                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1856         END OBJ(image, 0)
1857                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1858         END OBJ(exec, 0)
1859                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1860         END OBJ(execp, 0)
1861                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1862         END OBJ(execbar, 0)
1863                 SIZE_DEFAULTS(bar);
1864                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1865 #ifdef X11
1866         END OBJ(execgauge, 0)
1867                 SIZE_DEFAULTS(gauge);
1868                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1869         END OBJ(execgraph, 0)
1870                 SIZE_DEFAULTS(graph);
1871                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1872 #endif /* X11 */
1873         END OBJ(execibar, 0)
1874                 int n;
1875                 SIZE_DEFAULTS(bar);
1876
1877                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1878                         char buf[256];
1879
1880                         NORM_ERR("${execibar <interval> command}");
1881                         obj->type = OBJ_text;
1882                         snprintf(buf, 256, "${%s}", s);
1883                         obj->data.s = strndup(buf, text_buffer_size);
1884                 } else {
1885                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1886                 }
1887 #ifdef X11
1888         END OBJ(execigraph, 0)
1889                 int n;
1890                 SIZE_DEFAULTS(graph);
1891
1892                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1893                         char buf[256];
1894
1895                         NORM_ERR("${execigraph <interval> command}");
1896                         obj->type = OBJ_text;
1897                         snprintf(buf, 256, "${%s}", s);
1898                         obj->data.s = strndup(buf, text_buffer_size);
1899                 } else {
1900                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1901                 }
1902         END OBJ(execigauge, 0)
1903                 int n;
1904                 SIZE_DEFAULTS(gauge);
1905
1906                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1907                         char buf[256];
1908
1909                         NORM_ERR("${execigauge <interval> command}");
1910                         obj->type = OBJ_text;
1911                         snprintf(buf, 256, "${%s}", s);
1912                         obj->data.s = strndup(buf, text_buffer_size);
1913                 } else {
1914                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1915                 }
1916 #endif /* X11 */
1917         END OBJ(execi, 0)
1918                 int n;
1919
1920                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1921                         char buf[256];
1922
1923                         NORM_ERR("${execi <interval> command}");
1924                         obj->type = OBJ_text;
1925                         snprintf(buf, 256, "${%s}", s);
1926                         obj->data.s = strndup(buf, text_buffer_size);
1927                 } else {
1928                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1929                         obj->data.execi.buffer = malloc(text_buffer_size);
1930                 }
1931         END OBJ(execpi, 0)
1932                 int n;
1933
1934                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1935                         char buf[256];
1936
1937                         NORM_ERR("${execi <interval> command}");
1938                         obj->type = OBJ_text;
1939                         snprintf(buf, 256, "${%s}", s);
1940                         obj->data.s = strndup(buf, text_buffer_size);
1941                 } else {
1942                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1943                         obj->data.execi.buffer = malloc(text_buffer_size);
1944                 }
1945         END OBJ(texeci, 0)
1946                         int n;
1947
1948                         if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1949                                 char buf[256];
1950
1951                                 NORM_ERR("${texeci <interval> command}");
1952                                 obj->type = OBJ_text;
1953                                 snprintf(buf, 256, "${%s}", s);
1954                                 obj->data.s = strndup(buf, text_buffer_size);
1955                         } else {
1956                                 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1957                                 obj->data.texeci.buffer = malloc(text_buffer_size);
1958                         }
1959                         obj->data.texeci.p_timed_thread = NULL;
1960         END OBJ(pre_exec, 0)
1961                 obj->type = OBJ_text;
1962                 if (arg) {
1963                         char buf[2048];
1964
1965                         read_exec(arg, buf, sizeof(buf));
1966                         obj->data.s = strndup(buf, text_buffer_size);
1967                 } else {
1968                         obj->data.s = strndup("", text_buffer_size);
1969                 }
1970         END OBJ(fs_bar, INFO_FS)
1971                 SIZE_DEFAULTS(bar);
1972                 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1973                 if (arg) {
1974                         while (isspace(*arg)) {
1975                                 arg++;
1976                         }
1977                         if (*arg == '\0') {
1978                                 arg = "/";
1979                         }
1980                 } else {
1981                         arg = "/";
1982                 }
1983                 obj->data.fsbar.fs = prepare_fs_stat(arg);
1984         END OBJ(fs_bar_free, INFO_FS)
1985                 SIZE_DEFAULTS(bar);
1986                 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1987                 if (arg) {
1988                         while (isspace(*arg)) {
1989                                 arg++;
1990                         }
1991                         if (*arg == '\0') {
1992                                 arg = "/";
1993                         }
1994                 } else {
1995                         arg = "/";
1996                 }
1997
1998                 obj->data.fsbar.fs = prepare_fs_stat(arg);
1999         END OBJ(fs_free, INFO_FS)
2000                 if (!arg) {
2001                         arg = "/";
2002                 }
2003                 obj->data.fs = prepare_fs_stat(arg);
2004         END OBJ(fs_used_perc, INFO_FS)
2005                 if (!arg) {
2006                         arg = "/";
2007                 }
2008                 obj->data.fs = prepare_fs_stat(arg);
2009         END OBJ(fs_free_perc, INFO_FS)
2010                 if (!arg) {
2011                         arg = "/";
2012                 }
2013                 obj->data.fs = prepare_fs_stat(arg);
2014         END OBJ(fs_size, INFO_FS)
2015                 if (!arg) {
2016                         arg = "/";
2017                 }
2018                 obj->data.fs = prepare_fs_stat(arg);
2019         END OBJ(fs_type, INFO_FS)
2020                 if (!arg) {
2021                         arg = "/";
2022                 }
2023                 obj->data.fs = prepare_fs_stat(arg);
2024         END OBJ(fs_used, INFO_FS)
2025                 if (!arg) {
2026                         arg = "/";
2027                 }
2028                 obj->data.fs = prepare_fs_stat(arg);
2029         END OBJ(hr, 0)
2030                 obj->data.i = arg ? atoi(arg) : 1;
2031         END OBJ(nameserver, INFO_DNS)
2032                 obj->data.i = arg ? atoi(arg) : 0;
2033         END OBJ(offset, 0)
2034                 obj->data.i = arg ? atoi(arg) : 1;
2035         END OBJ(voffset, 0)
2036                 obj->data.i = arg ? atoi(arg) : 1;
2037         END OBJ(goto, 0)
2038
2039                 if (!arg) {
2040                         NORM_ERR("goto needs arguments");
2041                         obj->type = OBJ_text;
2042                         obj->data.s = strndup("${goto}", text_buffer_size);
2043                         return NULL;
2044                 }
2045
2046                 obj->data.i = atoi(arg);
2047
2048         END OBJ(tab, 0)
2049                 int a = 10, b = 0;
2050
2051                 if (arg) {
2052                         if (sscanf(arg, "%d %d", &a, &b) != 2) {
2053                                 sscanf(arg, "%d", &b);
2054                         }
2055                 }
2056                 if (a <= 0) {
2057                         a = 1;
2058                 }
2059                 obj->data.pair.a = a;
2060                 obj->data.pair.b = b;
2061
2062 #ifdef __linux__
2063         END OBJ(i2c, INFO_SYSFS)
2064                 char buf1[64], buf2[64];
2065                 float factor, offset;
2066                 int n, found = 0;
2067
2068                 if (!arg) {
2069                         NORM_ERR("i2c needs arguments");
2070                         obj->type = OBJ_text;
2071                         // obj->data.s = strndup("${i2c}", text_buffer_size);
2072                         return NULL;
2073                 }
2074
2075 #define HWMON_RESET() {\
2076                 buf1[0] = 0; \
2077                 factor = 1.0; \
2078                 offset = 0.0; }
2079
2080                 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2081                 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2082                 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2083                 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2084
2085                 if (!found) {
2086                         NORM_ERR("i2c failed to parse arguments");
2087                         obj->type = OBJ_text;
2088                         return NULL;
2089                 }
2090                 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2091                 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
2092                                 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2093                 strncpy(obj->data.sysfs.type, buf2, 63);
2094                 obj->data.sysfs.factor = factor;
2095                 obj->data.sysfs.offset = offset;
2096
2097         END OBJ(platform, INFO_SYSFS)
2098                 char buf1[64], buf2[64];
2099                 float factor, offset;
2100                 int n, found = 0;
2101
2102                 if (!arg) {
2103                         NORM_ERR("platform needs arguments");
2104                         obj->type = OBJ_text;
2105                         return NULL;
2106                 }
2107
2108                 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2109                 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2110                 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2111                 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2112
2113                 if (!found) {
2114                         NORM_ERR("platform failed to parse arguments");
2115                         obj->type = OBJ_text;
2116                         return NULL;
2117                 }
2118                 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2119                 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
2120                                 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2121                 strncpy(obj->data.sysfs.type, buf2, 63);
2122                 obj->data.sysfs.factor = factor;
2123                 obj->data.sysfs.offset = offset;
2124
2125         END OBJ(hwmon, INFO_SYSFS)
2126                 char buf1[64], buf2[64];
2127                 float factor, offset;
2128                 int n, found = 0;
2129
2130                 if (!arg) {
2131                         NORM_ERR("hwmon needs argumanets");
2132                         obj->type = OBJ_text;
2133                         return NULL;
2134                 }
2135
2136                 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2137                 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2138                 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2139                 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2140
2141 #undef HWMON_RESET
2142
2143                 if (!found) {
2144                         NORM_ERR("hwmon failed to parse arguments");
2145                         obj->type = OBJ_text;
2146                         return NULL;
2147                 }
2148                 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2149                 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
2150                                 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2151                 strncpy(obj->data.sysfs.type, buf2, 63);
2152                 obj->data.sysfs.factor = factor;
2153                 obj->data.sysfs.offset = offset;
2154
2155 #endif /* !__OpenBSD__ */
2156
2157         END
2158         /* we have four different types of top (top, top_mem, top_time and top_io). To
2159          * avoid having almost-same code four times, we have this special
2160          * handler. */
2161         if (strncmp(s, "top", 3) == EQUAL) {
2162                 if (!parse_top_args(s, arg, obj)) {
2163                         return NULL;
2164                 }
2165         } else OBJ(addr, INFO_NET)
2166                 if (arg) {
2167                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
2168                 } else {
2169                         // default to DEFAULTNETDEV
2170                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2171                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
2172                         free(buf);
2173                 }
2174 #if defined(__linux__)
2175         END OBJ(addrs, INFO_NET)
2176                 if (arg) {
2177                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
2178                 } else {
2179                         // default to DEFAULTNETDEV
2180                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2181                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
2182                         free(buf);
2183                 }
2184 #endif /* __linux__ */
2185         END OBJ(tail, 0)
2186                 init_tailhead("tail", arg, obj, free_at_crash);
2187         END OBJ(head, 0)
2188                 init_tailhead("head", arg, obj, free_at_crash);
2189         END OBJ(lines, 0)
2190                 if (arg) {
2191                         obj->data.s = strndup(arg, text_buffer_size);
2192                 }else{
2193                         CRIT_ERR(obj, free_at_crash, "lines needs a argument");
2194                 }
2195         END OBJ(words, 0)
2196                 if (arg) {
2197                         obj->data.s = strndup(arg, text_buffer_size);
2198                 }else{
2199                         CRIT_ERR(obj, free_at_crash, "words needs a argument");
2200                 }
2201         END OBJ(loadavg, INFO_LOADAVG)
2202                 int a = 1, b = 2, c = 3, r = 3;
2203
2204                 if (arg) {
2205                         r = sscanf(arg, "%d %d %d", &a, &b, &c);
2206                         if (r >= 3 && (c < 1 || c > 3)) {
2207                                 r--;
2208                         }
2209                         if (r >= 2 && (b < 1 || b > 3)) {
2210                                 r--, b = c;
2211                         }
2212                         if (r >= 1 && (a < 1 || a > 3)) {
2213                                 r--, a = b, b = c;
2214                         }
2215                 }
2216                 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2217                 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2218                 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2219         END OBJ_IF(if_empty, 0)
2220                 if (!arg) {
2221                         NORM_ERR("if_empty needs an argument");
2222                         obj->data.ifblock.s = 0;
2223                 } else {
2224                         obj->data.ifblock.s = strndup(arg, text_buffer_size);
2225                         obj->sub = malloc(sizeof(struct text_object));
2226                         extract_variable_text_internal(obj->sub,
2227                                                        obj->data.ifblock.s);
2228                 }
2229         END OBJ_IF(if_match, 0)
2230                 if (!arg) {
2231                         NORM_ERR("if_match needs arguments");
2232                         obj->data.ifblock.s = 0;
2233                 } else {
2234                         obj->data.ifblock.s = strndup(arg, text_buffer_size);
2235                         obj->sub = malloc(sizeof(struct text_object));
2236                         extract_variable_text_internal(obj->sub,
2237                                                        obj->data.ifblock.s);
2238                 }
2239         END OBJ_IF(if_existing, 0)
2240                 if (!arg) {
2241                         NORM_ERR("if_existing needs an argument or two");
2242                         obj->data.ifblock.s = NULL;
2243                         obj->data.ifblock.str = NULL;
2244                 } else {
2245                         char buf1[256], buf2[256];
2246                         int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2247
2248                         if (r == 1) {
2249                                 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2250                                 obj->data.ifblock.str = NULL;
2251                         } else {
2252                                 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2253                                 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2254                         }
2255                 }
2256                 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2257         END OBJ_IF(if_mounted, 0)
2258                 if (!arg) {
2259                         NORM_ERR("if_mounted needs an argument");
2260                         obj->data.ifblock.s = 0;
2261                 } else {
2262                         obj->data.ifblock.s = strndup(arg, text_buffer_size);
2263                 }
2264 #ifdef __linux__
2265         END OBJ_IF(if_running, INFO_TOP)
2266                 if (arg) {
2267                         obj->data.ifblock.s = strndup(arg, text_buffer_size);
2268 #else
2269         END OBJ_IF(if_running, 0)
2270                 if (arg) {
2271                         char buf[256];
2272
2273                         snprintf(buf, 256, "pidof %s >/dev/null", arg);
2274                         obj->data.ifblock.s = strndup(buf, text_buffer_size);
2275 #endif
2276                 } else {
2277                         NORM_ERR("if_running needs an argument");
2278                         obj->data.ifblock.s = 0;
2279                 }
2280         END OBJ(kernel, 0)
2281         END OBJ(machine, 0)
2282         END OBJ(mails, 0)
2283                 float n1;
2284                 char mbox[256], dst[256];
2285
2286                 if (!arg) {
2287                         n1 = 9.5;
2288                         /* Kapil: Changed from MAIL_FILE to
2289                            current_mail_spool since the latter
2290                            is a copy of the former if undefined
2291                            but the latter should take precedence
2292                            if defined */
2293                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2294                 } else {
2295                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2296                                 n1 = 9.5;
2297                                 strncpy(mbox, arg, sizeof(mbox));
2298                         }
2299                 }
2300
2301                 variable_substitute(mbox, dst, sizeof(dst));
2302                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2303                 obj->data.local_mail.interval = n1;
2304         END OBJ(new_mails, 0)
2305                 float n1;
2306                 char mbox[256], dst[256];
2307
2308                 if (!arg) {
2309                         n1 = 9.5;
2310                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2311                 } else {
2312                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2313                                 n1 = 9.5;
2314                                 strncpy(mbox, arg, sizeof(mbox));
2315                         }
2316                 }
2317
2318                 variable_substitute(mbox, dst, sizeof(dst));
2319                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2320                 obj->data.local_mail.interval = n1;
2321         END OBJ(seen_mails, 0)
2322                 float n1;
2323                 char mbox[256], dst[256];
2324
2325                 if (!arg) {
2326                         n1 = 9.5;
2327                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2328                 } else {
2329                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2330                                 n1 = 9.5;
2331                                 strncpy(mbox, arg, sizeof(mbox));
2332                         }
2333                 }
2334
2335                 variable_substitute(mbox, dst, sizeof(dst));
2336                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2337                 obj->data.local_mail.interval = n1;
2338         END OBJ(unseen_mails, 0)
2339                 float n1;
2340                 char mbox[256], dst[256];
2341
2342                 if (!arg) {
2343                         n1 = 9.5;
2344                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2345                 } else {
2346                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2347                                 n1 = 9.5;
2348                                 strncpy(mbox, arg, sizeof(mbox));
2349                         }
2350                 }
2351
2352                 variable_substitute(mbox, dst, sizeof(dst));
2353                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2354                 obj->data.local_mail.interval = n1;
2355         END OBJ(flagged_mails, 0)
2356                 float n1;
2357                 char mbox[256], dst[256];
2358
2359                 if (!arg) {
2360                         n1 = 9.5;
2361                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2362                 } else {
2363                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2364                                 n1 = 9.5;
2365                                 strncpy(mbox, arg, sizeof(mbox));
2366                         }
2367                 }
2368
2369                 variable_substitute(mbox, dst, sizeof(dst));
2370                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2371                 obj->data.local_mail.interval = n1;
2372         END OBJ(unflagged_mails, 0)
2373                 float n1;
2374                 char mbox[256], dst[256];
2375
2376                 if (!arg) {
2377                         n1 = 9.5;
2378                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2379                 } else {
2380                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2381                                 n1 = 9.5;
2382                                 strncpy(mbox, arg, sizeof(mbox));
2383                         }
2384                 }
2385
2386                 variable_substitute(mbox, dst, sizeof(dst));
2387                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2388                 obj->data.local_mail.interval = n1;
2389         END OBJ(forwarded_mails, 0)
2390                 float n1;
2391                 char mbox[256], dst[256];
2392
2393                 if (!arg) {
2394                         n1 = 9.5;
2395                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2396                 } else {
2397                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2398                                 n1 = 9.5;
2399                                 strncpy(mbox, arg, sizeof(mbox));
2400                         }
2401                 }
2402
2403                 variable_substitute(mbox, dst, sizeof(dst));
2404                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2405                 obj->data.local_mail.interval = n1;
2406         END OBJ(unforwarded_mails, 0)
2407                 float n1;
2408                 char mbox[256], dst[256];
2409
2410                 if (!arg) {
2411                         n1 = 9.5;
2412                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2413                 } else {
2414                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2415                                 n1 = 9.5;
2416                                 strncpy(mbox, arg, sizeof(mbox));
2417                         }
2418                 }
2419
2420                 variable_substitute(mbox, dst, sizeof(dst));
2421                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2422                 obj->data.local_mail.interval = n1;
2423         END OBJ(replied_mails, 0)
2424                 float n1;
2425                 char mbox[256], dst[256];
2426
2427                 if (!arg) {
2428                         n1 = 9.5;
2429                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2430                 } else {
2431                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2432                                 n1 = 9.5;
2433                                 strncpy(mbox, arg, sizeof(mbox));
2434                         }
2435                 }
2436
2437                 variable_substitute(mbox, dst, sizeof(dst));
2438                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2439                 obj->data.local_mail.interval = n1;
2440         END OBJ(unreplied_mails, 0)
2441                 float n1;
2442                 char mbox[256], dst[256];
2443
2444                 if (!arg) {
2445                         n1 = 9.5;
2446                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2447                 } else {
2448                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2449                                 n1 = 9.5;
2450                                 strncpy(mbox, arg, sizeof(mbox));
2451                         }
2452                 }
2453
2454                 variable_substitute(mbox, dst, sizeof(dst));
2455                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2456                 obj->data.local_mail.interval = n1;
2457         END OBJ(draft_mails, 0)
2458                 float n1;
2459                 char mbox[256], dst[256];
2460
2461                 if (!arg) {
2462                         n1 = 9.5;
2463                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2464                 } else {
2465                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2466                                 n1 = 9.5;
2467                                 strncpy(mbox, arg, sizeof(mbox));
2468                         }
2469                 }
2470
2471                 variable_substitute(mbox, dst, sizeof(dst));
2472                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2473                 obj->data.local_mail.interval = n1;
2474         END OBJ(trashed_mails, 0)
2475                 float n1;
2476                 char mbox[256], dst[256];
2477
2478                 if (!arg) {
2479                         n1 = 9.5;
2480                         strncpy(mbox, current_mail_spool, sizeof(mbox));
2481                 } else {
2482                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2483                                 n1 = 9.5;
2484                                 strncpy(mbox, arg, sizeof(mbox));
2485                         }
2486                 }
2487
2488                 variable_substitute(mbox, dst, sizeof(dst));
2489                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2490                 obj->data.local_mail.interval = n1;
2491         END OBJ(mboxscan, 0)
2492                 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2493                 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2494                 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2495                 obj->data.mboxscan.output[0] = 1;
2496                 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2497         END OBJ(mem, INFO_MEM)
2498         END OBJ(memeasyfree, INFO_MEM)
2499         END OBJ(memfree, INFO_MEM)
2500         END OBJ(memmax, INFO_MEM)
2501         END OBJ(memperc, INFO_MEM)
2502 #ifdef X11
2503         END OBJ(memgauge, INFO_MEM)
2504                 SIZE_DEFAULTS(gauge);
2505                 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2506 #endif /* X11*/
2507         END OBJ(membar, INFO_MEM)
2508                 SIZE_DEFAULTS(bar);
2509                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2510 #ifdef X11
2511         END OBJ(memgraph, INFO_MEM)
2512                 char *buf = 0;
2513                 SIZE_DEFAULTS(graph);
2514                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2515                                 &obj->e, &obj->char_a, &obj->char_b);
2516
2517                 if (buf) free(buf);
2518 #endif /* X11*/
2519         END OBJ(mixer, INFO_MIXER)
2520                 obj->data.l = mixer_init(arg);
2521         END OBJ(mixerl, INFO_MIXER)
2522                 obj->data.l = mixer_init(arg);
2523         END OBJ(mixerr, INFO_MIXER)
2524                 obj->data.l = mixer_init(arg);
2525 #ifdef X11
2526         END OBJ(mixerbar, INFO_MIXER)
2527                 SIZE_DEFAULTS(bar);
2528                 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2529                         &obj->data.mixerbar.h);
2530         END OBJ(mixerlbar, INFO_MIXER)
2531                 SIZE_DEFAULTS(bar);
2532                 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2533                         &obj->data.mixerbar.h);
2534         END OBJ(mixerrbar, INFO_MIXER)
2535                 SIZE_DEFAULTS(bar);
2536                 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2537                         &obj->data.mixerbar.h);
2538 #endif
2539         END OBJ_IF(if_mixer_mute, INFO_MIXER)
2540                 obj->data.ifblock.i = mixer_init(arg);
2541 #ifdef X11
2542         END OBJ(monitor, INFO_X11)
2543         END OBJ(monitor_number, INFO_X11)
2544         END OBJ(desktop, INFO_X11)
2545         END OBJ(desktop_number, INFO_X11)
2546         END OBJ(desktop_name, INFO_X11)
2547 #endif
2548         END OBJ(nodename, 0)
2549         END OBJ(processes, INFO_PROCS)
2550         END OBJ(running_processes, INFO_RUN_PROCS)
2551         END OBJ(shadecolor, 0)
2552 #ifdef X11
2553                 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2554 #endif /* X11 */
2555         END OBJ(outlinecolor, 0)
2556 #ifdef X11
2557                 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2558 #endif /* X11 */
2559         END OBJ(stippled_hr, 0)
2560 #ifdef X11
2561                 int a = stippled_borders, b = 1;
2562
2563                 if (arg) {
2564                         if (sscanf(arg, "%d %d", &a, &b) != 2) {
2565                                 sscanf(arg, "%d", &b);
2566                         }
2567                 }
2568                 if (a <= 0) {
2569                         a = 1;
2570                 }
2571                 obj->data.pair.a = a;
2572                 obj->data.pair.b = b;
2573 #endif /* X11 */
2574         END OBJ(swap, INFO_MEM)
2575         END OBJ(swapfree, INFO_MEM)
2576         END OBJ(swapmax, INFO_MEM)
2577         END OBJ(swapperc, INFO_MEM)
2578         END OBJ(swapbar, INFO_MEM)
2579                 SIZE_DEFAULTS(bar);
2580                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2581         END OBJ(sysname, 0)
2582         END OBJ(time, 0)
2583                 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2584         END OBJ(utime, 0)
2585                 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2586         END OBJ(tztime, 0)
2587                 char buf1[256], buf2[256], *fmt, *tz;
2588
2589                 fmt = tz = NULL;
2590                 if (arg) {
2591                         int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2592
2593                         switch (nArgs) {
2594                                 case 2:
2595                                         tz = buf1;
2596                                 case 1:
2597                                         fmt = buf2;
2598                         }
2599                 }
2600
2601                 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2602                 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2603 #ifdef HAVE_ICONV
2604         END OBJ(iconv_start, 0)
2605                 if (iconv_converting) {
2606                         CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
2607                                 "starting another");
2608                 }
2609                 if (arg) {
2610                         char iconv_from[CODEPAGE_LENGTH];
2611                         char iconv_to[CODEPAGE_LENGTH];
2612
2613                         if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2614                                 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
2615                         } else {
2616                                 iconv_t new_iconv;
2617
2618                                 new_iconv = iconv_open(iconv_to, iconv_from);
2619                                 if (new_iconv == (iconv_t) (-1)) {
2620                                         NORM_ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2621                                 } else {
2622                                         obj->a = register_iconv(&new_iconv);
2623                                         iconv_converting = 1;
2624                                 }
2625                         }
2626                 } else {
2627                         CRIT_ERR(obj, free_at_crash, "Iconv requires arguments");
2628                 }
2629         END OBJ(iconv_stop, 0)
2630                 iconv_converting = 0;
2631
2632 #endif
2633         END OBJ(totaldown, INFO_NET)
2634                 if (arg) {
2635                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
2636                 } else {
2637                         // default to DEFAULTNETDEV
2638                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2639                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
2640                         free(buf);
2641                 }
2642         END OBJ(totalup, INFO_NET)
2643                 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2644                 if (arg) {
2645                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
2646                 } else {
2647                         // default to DEFAULTNETDEV
2648                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2649                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
2650                         free(buf);
2651                 }
2652         END OBJ(updates, 0)
2653         END OBJ_IF(if_updatenr, 0)
2654                 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2655                 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
2656                 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2657         END OBJ(alignr, 0)
2658                 obj->data.i = arg ? atoi(arg) : 0;
2659         END OBJ(alignc, 0)
2660                 obj->data.i = arg ? atoi(arg) : 0;
2661         END OBJ(upspeed, INFO_NET)
2662                 if (arg) {
2663                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
2664                 } else {
2665                         // default to DEFAULTNETDEV
2666                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2667                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
2668                         free(buf);
2669                 }
2670         END OBJ(upspeedf, INFO_NET)
2671                 if (arg) {
2672                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
2673                 } else {
2674                         // default to DEFAULTNETDEV
2675                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2676                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
2677                         free(buf);
2678                 }
2679
2680 #ifdef X11
2681         END OBJ(upspeedgraph, INFO_NET)
2682                 char *buf = 0;
2683                 SIZE_DEFAULTS(graph);
2684                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2685                                 &obj->e, &obj->char_a, &obj->char_b);
2686
2687                 // default to DEFAULTNETDEV
2688                 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2689                 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2690                 free(buf);
2691 #endif
2692         END OBJ(uptime_short, INFO_UPTIME)
2693         END OBJ(uptime, INFO_UPTIME)
2694         END OBJ(user_names, INFO_USERS)
2695         END OBJ(user_times, INFO_USERS)
2696         END OBJ(user_terms, INFO_USERS)
2697         END OBJ(user_number, INFO_USERS)
2698 #if defined(__linux__)
2699         END OBJ(gw_iface, INFO_GW)
2700         END OBJ(gw_ip, INFO_GW)
2701 #endif /* !__linux__ */
2702 #ifndef __OpenBSD__
2703         END OBJ(adt746xcpu, 0)
2704         END OBJ(adt746xfan, 0)
2705 #endif /* !__OpenBSD__ */
2706 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2707                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2708         END OBJ(apm_adapter, 0)
2709         END OBJ(apm_battery_life, 0)
2710         END OBJ(apm_battery_time, 0)
2711 #endif /* __FreeBSD__ */
2712         END OBJ(imap_unseen, 0)
2713                 if (arg) {
2714                         // proccss
2715                         obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2716                         obj->char_b = 0;
2717                 } else {
2718                         obj->char_b = 1;
2719                 }
2720         END OBJ(imap_messages, 0)
2721                 if (arg) {
2722                         // proccss
2723                         obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2724                         obj->char_b = 0;
2725                 } else {
2726                         obj->char_b = 1;
2727                 }
2728         END OBJ(pop3_unseen, 0)
2729                 if (arg) {
2730                         // proccss
2731                         obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2732                         obj->char_b = 0;
2733                 } else {
2734                         obj->char_b = 1;
2735                 }
2736         END OBJ(pop3_used, 0)
2737                 if (arg) {
2738                         // proccss
2739                         obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2740                         obj->char_b = 0;
2741                 } else {
2742                         obj->char_b = 1;
2743                 }
2744 #ifdef IBM
2745         END OBJ(smapi, 0)
2746                 if (arg)
2747                         obj->data.s = strndup(arg, text_buffer_size);
2748                 else
2749                         NORM_ERR("smapi needs an argument");
2750         END OBJ_IF(if_smapi_bat_installed, 0)
2751                 if (!arg) {
2752                         NORM_ERR("if_smapi_bat_installed needs an argument");
2753                         obj->data.ifblock.s = 0;
2754                 } else
2755                         obj->data.ifblock.s = strndup(arg, text_buffer_size);
2756         END OBJ(smapi_bat_perc, 0)
2757                 if (arg)
2758                         obj->data.s = strndup(arg, text_buffer_size);
2759                 else
2760                         NORM_ERR("smapi_bat_perc needs an argument");
2761         END OBJ(smapi_bat_temp, 0)
2762                 if (arg)
2763                         obj->data.s = strndup(arg, text_buffer_size);
2764                 else
2765                         NORM_ERR("smapi_bat_temp needs an argument");
2766         END OBJ(smapi_bat_power, 0)
2767                 if (arg)
2768                         obj->data.s = strndup(arg, text_buffer_size);
2769                 else
2770                         NORM_ERR("smapi_bat_power needs an argument");
2771 #ifdef X11
2772         END OBJ(smapi_bat_bar, 0)
2773                 SIZE_DEFAULTS(bar);
2774                 if(arg) {
2775                         int cnt;
2776                         if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2777                                 NORM_ERR("first argument to smapi_bat_bar must be an integer value");
2778                                 obj->data.i = -1;
2779                         } else {
2780                                 obj->b = 4;
2781                                 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2782                         }
2783                 } else
2784                         NORM_ERR("smapi_bat_bar needs an argument");
2785 #endif /* X11 */
2786 #endif /* IBM */
2787 #ifdef MPD
2788 #define mpd_set_maxlen(name) \
2789                 if (arg) { \
2790                         int i; \
2791                         sscanf(arg, "%d", &i); \
2792                         if (i > 0) \
2793                                 obj->data.i = i + 1; \
2794                         else \
2795                                 NORM_ERR(#name ": invalid length argument"); \
2796                 }
2797         END OBJ(mpd_artist, INFO_MPD)
2798                 mpd_set_maxlen(mpd_artist);
2799                 init_mpd();
2800         END OBJ(mpd_title, INFO_MPD)
2801                 mpd_set_maxlen(mpd_title);
2802                 init_mpd();
2803         END OBJ(mpd_random, INFO_MPD) init_mpd();
2804         END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2805         END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2806         END OBJ(mpd_length, INFO_MPD) init_mpd();
2807         END OBJ(mpd_track, INFO_MPD)
2808                 mpd_set_maxlen(mpd_track);
2809                 init_mpd();
2810         END OBJ(mpd_name, INFO_MPD)
2811                 mpd_set_maxlen(mpd_name);
2812                 init_mpd();
2813         END OBJ(mpd_file, INFO_MPD)
2814                 mpd_set_maxlen(mpd_file);
2815                 init_mpd();
2816         END OBJ(mpd_percent, INFO_MPD) init_mpd();
2817         END OBJ(mpd_album, INFO_MPD)
2818                 mpd_set_maxlen(mpd_album);
2819                 init_mpd();
2820         END OBJ(mpd_vol, INFO_MPD) init_mpd();
2821         END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2822         END OBJ(mpd_status, INFO_MPD) init_mpd();
2823         END OBJ(mpd_bar, INFO_MPD)
2824                 SIZE_DEFAULTS(bar);
2825                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2826                 init_mpd();
2827         END OBJ(mpd_smart, INFO_MPD)
2828                 mpd_set_maxlen(mpd_smart);
2829                 init_mpd();
2830         END OBJ_IF(if_mpd_playing, INFO_MPD)
2831                 init_mpd();
2832 #undef mpd_set_maxlen
2833 #endif /* MPD */
2834 #ifdef MOC
2835         END OBJ(moc_state, INFO_MOC)
2836         END OBJ(moc_file, INFO_MOC)
2837         END OBJ(moc_title, INFO_MOC)
2838         END OBJ(moc_artist, INFO_MOC)
2839         END OBJ(moc_song, INFO_MOC)
2840         END OBJ(moc_album, INFO_MOC)
2841         END OBJ(moc_totaltime, INFO_MOC)
2842         END OBJ(moc_timeleft, INFO_MOC)
2843         END OBJ(moc_curtime, INFO_MOC)
2844         END OBJ(moc_bitrate, INFO_MOC)
2845         END OBJ(moc_rate, INFO_MOC)
2846 #endif /* MOC */
2847 #ifdef XMMS2
2848         END OBJ(xmms2_artist, INFO_XMMS2)
2849         END OBJ(xmms2_album, INFO_XMMS2)
2850         END OBJ(xmms2_title, INFO_XMMS2)
2851         END OBJ(xmms2_genre, INFO_XMMS2)
2852         END OBJ(xmms2_comment, INFO_XMMS2)
2853         END OBJ(xmms2_url, INFO_XMMS2)
2854         END OBJ(xmms2_tracknr, INFO_XMMS2)
2855         END OBJ(xmms2_bitrate, INFO_XMMS2)
2856         END OBJ(xmms2_date, INFO_XMMS2)
2857         END OBJ(xmms2_id, INFO_XMMS2)
2858         END OBJ(xmms2_duration, INFO_XMMS2)
2859         END OBJ(xmms2_elapsed, INFO_XMMS2)
2860         END OBJ(xmms2_size, INFO_XMMS2)
2861         END OBJ(xmms2_status, INFO_XMMS2)
2862         END OBJ(xmms2_percent, INFO_XMMS2)
2863 #ifdef X11
2864         END OBJ(xmms2_bar, INFO_XMMS2)
2865                 SIZE_DEFAULTS(bar);
2866                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2867 #endif /* X11 */
2868         END OBJ(xmms2_smart, INFO_XMMS2)
2869         END OBJ(xmms2_playlist, INFO_XMMS2)
2870         END OBJ(xmms2_timesplayed, INFO_XMMS2)
2871         END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2872 #endif
2873 #ifdef AUDACIOUS
2874         END OBJ(audacious_status, INFO_AUDACIOUS)
2875         END OBJ(audacious_title, INFO_AUDACIOUS)
2876                 if (arg) {
2877                         sscanf(arg, "%d", &info.audacious.max_title_len);
2878                         if (info.audacious.max_title_len > 0) {
2879                                 info.audacious.max_title_len++;
2880                         } else {
2881                                 CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
2882                         }
2883                 }
2884         END OBJ(audacious_length, INFO_AUDACIOUS)
2885         END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2886         END OBJ(audacious_position, INFO_AUDACIOUS)
2887         END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2888         END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2889         END OBJ(audacious_frequency, INFO_AUDACIOUS)
2890         END OBJ(audacious_channels, INFO_AUDACIOUS)
2891         END OBJ(audacious_filename, INFO_AUDACIOUS)
2892         END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2893         END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2894         END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2895 #ifdef X11
2896         END OBJ(audacious_bar, INFO_AUDACIOUS)
2897                 SIZE_DEFAULTS(bar);
2898                 scan_bar(arg, &obj->a, &obj->b);
2899 #endif /* X11 */
2900 #endif
2901 #ifdef BMPX
2902         END OBJ(bmpx_title, INFO_BMPX)
2903                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2904         END OBJ(bmpx_artist, INFO_BMPX)
2905                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2906         END OBJ(bmpx_album, INFO_BMPX)
2907                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2908         END OBJ(bmpx_track, INFO_BMPX)
2909                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2910         END OBJ(bmpx_uri, INFO_BMPX)
2911                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2912         END OBJ(bmpx_bitrate, INFO_BMPX)
2913                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2914 #endif
2915 #ifdef EVE
2916         END OBJ(eve, 0)
2917                 if(arg) {
2918                         int argc;
2919                         char *userid = (char *) malloc(20 * sizeof(char));
2920                         char *apikey = (char *) malloc(64 * sizeof(char));
2921                         char *charid = (char *) malloc(20 * sizeof(char));
2922
2923                         argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2924                         obj->data.eve.charid = charid;
2925                         obj->data.eve.userid = userid;
2926                         obj->data.eve.apikey = apikey;
2927
2928                         init_eve();
2929                 } else {
2930                         CRIT_ERR(obj, free_at_crash, "eve needs arguments: <userid> <apikey> <characterid>");
2931                 }
2932 #endif
2933 #ifdef HAVE_CURL
2934         END OBJ(curl, 0)
2935                 if (arg) {
2936                         int argc;
2937                         float interval = 0;
2938                         char *uri = (char *) malloc(128 * sizeof(char));
2939
2940                         argc = sscanf(arg, "%127s %f", uri, &interval);
2941                         if (argc == 2) {
2942                                 obj->data.curl.uri = uri;
2943                                 obj->data.curl.interval = interval > 0 ? interval * 60 : 15*60;
2944                         } else {
2945                                 NORM_ERR("wrong number of arguments for $curl");
2946                         }
2947                 } else {
2948                         CRIT_ERR(obj, free_at_crash, "curl needs arguments: <uri> <interval in minutes>");
2949                 }
2950 #endif
2951 #ifdef RSS
2952         END OBJ(rss, 0)
2953                 if (arg) {
2954                         float interval = 0;
2955                         int argc, act_par;
2956                         unsigned int nrspaces = 0;
2957                         char *uri = (char *) malloc(128 * sizeof(char));
2958                         char *action = (char *) malloc(64 * sizeof(char));
2959
2960                         argc = sscanf(arg, "%127s %f %63s %d %u", uri, &interval, action,
2961                                         &act_par, &nrspaces);
2962                         if (argc >= 3) {
2963                                 obj->data.rss.uri = uri;
2964                                 obj->data.rss.interval = interval > 0 ? interval * 60 : 15*60;
2965                                 obj->data.rss.action = action;
2966                                 obj->data.rss.act_par = act_par;
2967                                 obj->data.rss.nrspaces = nrspaces;
2968                         } else {
2969                                 NORM_ERR("wrong number of arguments for $rss");
2970                         }
2971                 } else {
2972                         CRIT_ERR(obj, free_at_crash, "rss needs arguments: <uri> <interval in minutes> <action> "
2973                                         "[act_par] [spaces in front]");
2974                 }
2975 #endif
2976 #ifdef WEATHER
2977         END OBJ(weather, 0)
2978                 if (arg) {
2979                         int argc;
2980                         float interval = 0;
2981                         char *locID = (char *) malloc(9 * sizeof(char));
2982                         char *uri = (char *) malloc(128 * sizeof(char));
2983                         char *data_type = (char *) malloc(32 * sizeof(char));
2984
2985                         argc = sscanf(arg, "%119s %8s %31s %f", uri, locID, data_type, &interval);
2986
2987                         if (argc >= 3) {
2988                                 if (process_weather_uri(uri, locID)) {
2989                                         free(data_type);
2990                                         free(uri);
2991                                         free(locID);
2992                                         CRIT_ERR(obj, free_at_crash, \
2993                                                         "could not recognize the weather uri");
2994                                 }
2995
2996                                 obj->data.weather.uri = uri;
2997                                 obj->data.weather.data_type = data_type;
2998
2999                                 /* Limit the data retrieval interval to half hour min */
3000                                 if (interval < 30) {
3001                                         interval = 30;
3002                                 }
3003
3004                                 /* Convert to seconds */
3005                                 obj->data.weather.interval = interval * 60;
3006                                 free(locID);
3007
3008                                 DBGP("weather: fetching %s from %s every %d seconds", \
3009                                                 data_type, uri, obj->data.weather.interval);
3010                         } else {
3011                                 free(data_type);
3012                                 free(uri);
3013                                 free(locID);
3014                                 CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather");
3015                         }
3016                 } else {
3017                         CRIT_ERR(obj, free_at_crash, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]");
3018                 }
3019 #endif
3020 #ifdef HAVE_LUA
3021         END OBJ(lua, 0)
3022                 if (arg) {
3023                         obj->data.s = strndup(arg, text_buffer_size);
3024                 } else {
3025                         CRIT_ERR(obj, free_at_crash, "lua needs arguments: <function name> [function parameters]");
3026                 }
3027         END OBJ(lua_parse, 0)
3028                 if (arg) {
3029                         obj->data.s = strndup(arg, text_buffer_size);
3030                 } else {
3031                         CRIT_ERR(obj, free_at_crash, "lua_parse needs arguments: <function name> [function parameters]");
3032                 }
3033         END OBJ(lua_bar, 0)
3034                 SIZE_DEFAULTS(bar);
3035                 if (arg) {
3036                         arg = scan_bar(arg, &obj->a, &obj->b);
3037                         if(arg) {
3038                                 obj->data.s = strndup(arg, text_buffer_size);
3039                         } else {
3040                                 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
3041                         }
3042                 } else {
3043                         CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
3044                 }
3045 #ifdef X11
3046         END OBJ(lua_graph, 0)
3047                 SIZE_DEFAULTS(graph);
3048                 if (arg) {
3049                         char *buf = 0;
3050                         buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3051                                         &obj->e, &obj->char_a, &obj->char_b);
3052                         if (buf) {
3053                                 obj->data.s = buf;
3054                         } else {
3055                                 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
3056                         }
3057                 } else {
3058                         CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
3059         }
3060         END OBJ(lua_gauge, 0)
3061                 SIZE_DEFAULTS(gauge);
3062                 if (arg) {
3063                         arg = scan_gauge(arg, &obj->a, &obj->b);
3064                         if (arg) {
3065                                 obj->data.s = strndup(arg, text_buffer_size);
3066                         } else {
3067                                 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
3068                         }
3069                 } else {
3070                         CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
3071                 }
3072 #endif /* X11 */
3073 #endif /* HAVE_LUA */
3074 #ifdef HDDTEMP
3075         END OBJ(hddtemp, 0)
3076                 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
3077                                  &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3078                         NORM_ERR("hddtemp needs arguments");
3079                         obj->type = OBJ_text;
3080                         obj->data.s = strndup("${hddtemp}", text_buffer_size);
3081                         obj->data.hddtemp.update_time = 0;
3082                 } else
3083                         obj->data.hddtemp.temp = NULL;
3084 #endif /* HDDTEMP */
3085 #ifdef TCP_PORT_MONITOR
3086         END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3087                 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
3088 #endif /* TCP_PORT_MONITOR */
3089         END OBJ(entropy_avail, INFO_ENTROPY)
3090         END OBJ(entropy_perc, INFO_ENTROPY)
3091         END OBJ(entropy_poolsize, INFO_ENTROPY)
3092         END OBJ(entropy_bar, INFO_ENTROPY)
3093                 SIZE_DEFAULTS(bar);
3094                 scan_bar(arg, &obj->a, &obj->b);
3095         END OBJ(include, 0)
3096                 if(arg) {
3097                         struct conftree *leaf = conftree_add(currentconffile, arg);
3098                         if(leaf) {
3099                                 if( load_config_file(arg) == TRUE) {
3100                                         obj->sub = malloc(sizeof(struct text_object));
3101                                         currentconffile = leaf;
3102                                         extract_variable_text_internal(obj->sub, global_text);
3103                                         currentconffile = leaf->back;
3104                                 } else {
3105                                         NORM_ERR("Can't load configfile '%s'.", arg);
3106                                 }
3107                         } else {
3108                                 NORM_ERR("You are trying to load '%s' recursively, I'm only going to load it once to prevent an infinite loop.", arg);
3109                         }
3110                 } else {
3111                         CRIT_ERR(obj, free_at_crash, "include needs a argument");
3112                 }
3113         END OBJ(blink, 0)
3114                 if(arg) {
3115                         obj->sub = malloc(sizeof(struct text_object));
3116                         extract_variable_text_internal(obj->sub, arg);
3117                 }else{
3118                         CRIT_ERR(obj, free_at_crash, "blink needs a argument");
3119                 }
3120         END OBJ(to_bytes, 0)
3121                 if(arg) {
3122                         obj->sub = malloc(sizeof(struct text_object));
3123                         extract_variable_text_internal(obj->sub, arg);
3124                 }else{
3125                         CRIT_ERR(obj, free_at_crash, "to_bytes needs a argument");
3126                 }
3127         END OBJ(scroll, 0)
3128                 int n1 = 0, n2 = 0;
3129
3130                 obj->data.scroll.resetcolor = current_text_color;
3131                 obj->data.scroll.step = 1;
3132                 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
3133                         sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
3134                         if (*(arg + n1 + n2)) {
3135                                 n1 += n2;
3136                         } else {
3137                                 obj->data.scroll.step = 1;
3138                         }
3139                         obj->data.scroll.text = malloc(strlen(arg + n1) + obj->data.scroll.show + 1);
3140                         for(n2 = 0; (unsigned int) n2 < obj->data.scroll.show; n2++) {
3141                                 obj->data.scroll.text[n2] = ' ';
3142                         }
3143                         obj->data.scroll.text[n2] = 0;
3144                         strcat(obj->data.scroll.text, arg + n1);
3145                         obj->data.scroll.start = 0;
3146                         obj->sub = malloc(sizeof(struct text_object));
3147                         extract_variable_text_internal(obj->sub,
3148                                         obj->data.scroll.text);
3149                 } else {
3150                         CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
3151                 }
3152         END OBJ(combine, 0)
3153                 if(arg) {
3154                         unsigned int i,j;
3155                         unsigned int indenting = 0;     //vars can be used as args for other vars
3156                         int startvar[2];
3157                         int endvar[2];
3158                         startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
3159                         j=0;
3160                         for (i=0; arg[i] != 0 && j < 2; i++) {
3161                                 if(startvar[j] == -1) {
3162                                         if(arg[i] == '$') {
3163                                                 startvar[j] = i;
3164                                         }
3165                                 }else if(endvar[j] == -1) {
3166                                         if(arg[i] == '{') {
3167                                                 indenting++;
3168                                         }else if(arg[i] == '}') {
3169                                                 indenting--;
3170                                         }
3171                                         if (indenting == 0 && arg[i+1] < 48) {  //<48 has 0, $, and the most used chars not used in varnames but not { or }
3172                                                 endvar[j]=i+1;
3173                                                 j++;
3174                                         }
3175                                 }
3176                         }
3177                         if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
3178                                 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
3179                                 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
3180                                 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
3181                                 
3182                                 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
3183                                 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
3184                                 
3185                                 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
3186                                 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
3187                                 
3188                                 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
3189                                 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
3190
3191                                 obj->sub = malloc(sizeof(struct text_object));
3192                                 extract_variable_text_internal(obj->sub, obj->data.combine.left);
3193                                 obj->sub->sub = malloc(sizeof(struct text_object));
3194                                 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right);
3195                         } else {
3196                                 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3197                         }
3198                 } else {
3199                         CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3200                 }
3201 #ifdef NVIDIA
3202         END OBJ(nvidia, 0)
3203                 if (!arg) {
3204                         CRIT_ERR(obj, free_at_crash, "nvidia needs an argument\n");
3205                 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3206                         CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
3207                                  " specified: '%s'\n", arg);
3208                 }
3209 #endif /* NVIDIA */
3210 #ifdef APCUPSD
3211                 init_apcupsd();
3212                 END OBJ(apcupsd, INFO_APCUPSD)
3213                         if (arg) {
3214                                 char host[64];
3215                                 int port;
3216                                 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3217                                         CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3218                                 } else {
3219                                         info.apcupsd.port = htons(port);
3220                                         strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3221                                 }
3222                         } else {
3223                                 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3224                         }
3225                         END OBJ(apcupsd_name, INFO_APCUPSD)
3226                         END OBJ(apcupsd_model, INFO_APCUPSD)
3227                         END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3228                         END OBJ(apcupsd_cable, INFO_APCUPSD)
3229                         END OBJ(apcupsd_status, INFO_APCUPSD)
3230                         END OBJ(apcupsd_linev, INFO_APCUPSD)
3231                         END OBJ(apcupsd_load, INFO_APCUPSD)
3232                         END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3233                                 SIZE_DEFAULTS(bar);
3234                                 scan_bar(arg, &obj->a, &obj->b);
3235 #ifdef X11
3236                         END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3237                                 char* buf = 0;
3238                                 SIZE_DEFAULTS(graph);
3239                                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3240                                                 &obj->e, &obj->char_a, &obj->char_b);
3241                                 if (buf) free(buf);
3242                         END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3243                                 SIZE_DEFAULTS(gauge);
3244                                 scan_gauge(arg, &obj->a, &obj->b);
3245 #endif /* X11 */
3246                         END OBJ(apcupsd_charge, INFO_APCUPSD)
3247                         END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3248                         END OBJ(apcupsd_temp, INFO_APCUPSD)
3249                         END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3250 #endif /* APCUPSD */
3251         END {
3252                 char buf[256];
3253
3254                 NORM_ERR("unknown variable %s", s);
3255                 obj->type = OBJ_text;
3256                 snprintf(buf, 256, "${%s}", s);
3257                 obj->data.s = strndup(buf, text_buffer_size);
3258         }
3259 #undef OBJ
3260
3261         return obj;
3262 }
3263
3264 static struct text_object *create_plain_text(const char *s)
3265 {
3266         struct text_object *obj;
3267
3268         if (s == NULL || *s == '\0') {
3269                 return NULL;
3270         }
3271
3272         obj = new_text_object_internal();
3273
3274         obj->type = OBJ_text;
3275         obj->data.s = strndup(s, text_buffer_size);
3276         return obj;
3277 }
3278
3279 /* backslash_escape - do the actual substitution task for template objects
3280  *
3281  * The field templates is used for substituting the \N occurences. Set it to
3282  * NULL to leave them as they are.
3283  */
3284 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3285 {
3286         char *src_dup;
3287         const char *p;
3288         unsigned int dup_idx = 0, dup_len;
3289
3290         dup_len = strlen(src) + 1;
3291         src_dup = malloc(dup_len * sizeof(char));
3292
3293         p = src;
3294         while (*p) {
3295                 switch (*p) {
3296                 case '\\':
3297                         if (!*(p + 1))
3298                                 break;
3299                         if (*(p + 1) == '\\') {
3300                                 src_dup[dup_idx++] = '\\';
3301                                 p++;
3302                         } else if (*(p + 1) == ' ') {
3303                                 src_dup[dup_idx++] = ' ';
3304                                 p++;
3305                         } else if (*(p + 1) == 'n') {
3306                                 src_dup[dup_idx++] = '\n';
3307                                 p++;
3308                         } else if (templates) {
3309                                 unsigned int tmpl_num;
3310                                 int digits;
3311                                 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3312                                     (tmpl_num > template_count))
3313                                         break;
3314                                 dup_len += strlen(templates[tmpl_num - 1]);
3315                                 src_dup = realloc(src_dup, dup_len * sizeof(char));
3316                                 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3317                                 dup_idx += strlen(templates[tmpl_num - 1]);
3318                                 p += digits;
3319                         }
3320                         break;
3321                 default:
3322                         src_dup[dup_idx++] = *p;
3323                         break;
3324                 }
3325                 p++;
3326         }
3327         src_dup[dup_idx] = '\0';
3328         src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3329         return src_dup;
3330 }
3331
3332 /* handle_template_object - core logic of the template object
3333  *
3334  * use config variables like this:
3335  * template1 = "$\1\2"
3336  * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3337  *
3338  * and use them like this:
3339  * ${template1 node name}
3340  * ${template2 root /}
3341  * ${template2 cdrom /mnt/cdrom}
3342  */
3343 static char *handle_template(const char *tmpl, const char *args)
3344 {
3345         char *args_dup = NULL;
3346         char *p, *p_old;
3347         char **argsp = NULL;
3348         unsigned int argcnt = 0, template_idx, i;
3349         char *eval_text;
3350
3351         if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3352             (template_idx >= MAX_TEMPLATES))
3353                 return NULL;
3354
3355         if(args) {
3356                 args_dup = strdup(args);
3357                 p = args_dup;
3358                 while (*p) {
3359                         while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3360                                 p++;
3361                         if (p > args_dup && *(p - 1) == '\\')
3362                                 p--;
3363                         p_old = p;
3364                         while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3365                                 p++;
3366                         if (*p) {
3367                                 (*p) = '\0';
3368                                 p++;
3369                         }
3370                         argsp = realloc(argsp, ++argcnt * sizeof(char *));
3371                         argsp[argcnt - 1] = p_old;
3372                 }
3373                 for (i = 0; i < argcnt; i++) {
3374                         char *tmp;
3375                         tmp = backslash_escape(argsp[i], NULL, 0);
3376                         DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3377                         argsp[i] = tmp;
3378                 }
3379         }
3380
3381         eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3382         DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3383         free(args_dup);
3384         for (i = 0; i < argcnt; i++)
3385                 free(argsp[i]);
3386         free(argsp);
3387         return eval_text;
3388 }
3389
3390 static char *find_and_replace_templates(const char *inbuf)
3391 {
3392         char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3393         int stack, outlen;
3394
3395         outlen = strlen(inbuf) + 1;
3396         o = outbuf = calloc(outlen, sizeof(char));
3397         memset(outbuf, 0, outlen * sizeof(char));
3398
3399         p = indup = strdup(inbuf);
3400         while (*p) {
3401                 while (*p && *p != '$')
3402                         *(o++) = *(p++);
3403
3404                 if (!(*p))
3405                         break;
3406
3407                 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3408                         *(o++) = *(p++);
3409                         continue;
3410                 }
3411
3412                 if (*(p + 1) == '{') {
3413                         p += 2;
3414                         templ = p;
3415                         while (*p && !isspace(*p) && *p != '{' && *p != '}')
3416                                 p++;
3417                         if (*p == '}')
3418                                 args = NULL;
3419                         else
3420                                 args = p;
3421
3422                         stack = 1;
3423                         while (*p && stack > 0) {
3424                                 if (*p == '{')
3425                                         stack++;
3426                                 else if (*p == '}')
3427                                         stack--;
3428                                 p++;
3429                         }
3430                         if (stack == 0) {
3431                                 // stack is empty. that means the previous char was }, so we zero it
3432                                 *(p - 1) = '\0';
3433                         } else {
3434                                 // we ran into the end of string without finding a closing }, bark
3435                                 CRIT_ERR(NULL, NULL, "cannot find a closing '}' in template expansion");
3436                         }
3437                 } else {
3438                         templ = p + 1;
3439                         while (*p && !isspace(*p))
3440                                 p++;
3441                         args = NULL;
3442                 }
3443                 tmpl_out = handle_template(templ, args);
3444                 if (tmpl_out) {
3445                         outlen += strlen(tmpl_out);
3446                         *o = '\0';
3447                         outbuf = realloc(outbuf, outlen * sizeof(char));
3448                         strcat (outbuf, tmpl_out);
3449                         free(tmpl_out);
3450                         o = outbuf + strlen(outbuf);
3451                 } else {
3452                         NORM_ERR("failed to handle template '%s' with args '%s'", templ, args);
3453                 }
3454         }
3455         *o = '\0';
3456         outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3457         free(indup);
3458         return outbuf;
3459 }
3460
3461 static int text_contains_templates(const char *text)
3462 {
3463         if (strcasestr(text, "${template") != NULL)
3464                 return 1;
3465         if (strcasestr(text, "$template") != NULL)
3466                 return 1;
3467         return 0;
3468 }
3469
3470 /*
3471  * - assumes that *string is '#'
3472  * - removes the part from '#' to the end of line ('\n' or '\0')
3473  * - it removes the '\n'
3474  * - copies the last char into 'char *last' argument, which should be a pointer
3475  *   to a char rather than a string.
3476  */
3477 static size_t remove_comment(char *string, char *last)
3478 {
3479         char *end = string;
3480         while (*end != '\0' && *end != '\n') {
3481                 ++end;
3482         }
3483         if (last) *last = *end;
3484         if (*end == '\n') end++;
3485         strfold(string, end - string);
3486         return end - string;
3487 }
3488
3489 static size_t remove_comments(char *string)
3490 {
3491         char *curplace;
3492         size_t folded = 0;
3493         for (curplace = string; *curplace != 0; curplace++) {
3494                 if (*curplace == '\\' && *(curplace + 1) == '#') {
3495                         // strcpy can't be used for overlapping strings
3496                         strfold(curplace, 1);
3497                         folded += 1;
3498                 } else if (*curplace == '#') {
3499                         folded += remove_comment(curplace, 0);
3500                 }
3501         }
3502         return folded;
3503 }
3504
3505 static int extract_variable_text_internal(struct text_object *retval, const char *const_p)
3506 {
3507         struct text_object *obj;
3508         char *p, *s, *orig_p;
3509         long line;
3510         void *ifblock_opaque = NULL;
3511         char *tmp_p;
3512         char *arg = 0;
3513         size_t len = 0;
3514
3515         p = strndup(const_p, max_user_text - 1);
3516         while (text_contains_templates(p)) {
3517                 char *tmp;
3518                 tmp = find_and_replace_templates(p);
3519                 free(p);
3520                 p = tmp;
3521         }
3522         s = orig_p = p;
3523
3524         if (strcmp(p, const_p)) {
3525                 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3526         } else {
3527                 DBGP("no templates to replace");
3528         }
3529
3530         memset(retval, 0, sizeof(struct text_object));
3531
3532         line = global_text_lines;
3533
3534         while (*p) {
3535                 if (*p == '\n') {
3536                         line++;
3537                 }
3538                 if (*p == '$') {
3539                         *p = '\0';
3540                         obj = create_plain_text(s);
3541                         if (obj != NULL) {
3542                                 append_object(retval, obj);
3543                         }
3544                         *p = '$';
3545                         p++;
3546                         s = p;
3547
3548                         if (*p != '$') {
3549                                 char buf[256];
3550                                 const char *var;
3551
3552                                 /* variable is either $foo or ${foo} */
3553                                 if (*p == '{') {
3554                                         unsigned int brl = 1, brr = 0;
3555
3556                                         p++;
3557                                         s = p;
3558                                         while (*p && brl != brr) {
3559                                                 if (*p == '{') {
3560                                                         brl++;
3561                                                 }
3562                                                 if (*p == '}') {
3563                                                         brr++;
3564                                                 }
3565                                                 p++;
3566                                         }
3567                                         p--;
3568                                 } else {
3569                                         s = p;
3570                                         if (*p == '#') {
3571                                                 p++;
3572                                         }
3573                                         while (*p && (isalnum((int) *p) || *p == '_')) {
3574                                                 p++;
3575                                         }
3576                                 }
3577
3578                                 /* copy variable to buffer */
3579                                 len = (p - s > 255) ? 255 : (p - s);
3580                                 strncpy(buf, s, len);
3581                                 buf[len] = '\0';
3582
3583                                 if (*p == '}') {
3584                                         p++;
3585                                 }
3586                                 s = p;
3587
3588                                 /* search for variable in environment */
3589
3590                                 var = getenv(buf);
3591                                 if (var) {
3592                                         obj = create_plain_text(var);
3593                                         if (obj) {
3594                                                 append_object(retval, obj);
3595                                         }
3596                                         continue;
3597                                 }
3598
3599                                 /* if variable wasn't found in environment, use some special */
3600
3601                                 arg = 0;
3602
3603                                 /* split arg */
3604                                 if (strchr(buf, ' ')) {
3605                                         arg = strchr(buf, ' ');
3606                                         *arg = '\0';
3607                                         arg++;
3608                                         while (isspace((int) *arg)) {
3609                                                 arg++;
3610                                         }
3611                                         if (!*arg) {
3612                                                 arg = 0;
3613                                         }
3614                                 }
3615
3616                                 /* lowercase variable name */
3617                                 tmp_p = buf;
3618                                 while (*tmp_p) {
3619                                         *tmp_p = tolower(*tmp_p);
3620                                         tmp_p++;
3621                                 }
3622
3623                                 obj = construct_text_object(buf, arg,
3624                                                 line, &ifblock_opaque, orig_p);
3625                                 if (obj != NULL) {
3626                                         append_object(retval, obj);
3627                                 }
3628                                 continue;
3629                         } else {
3630                                 obj = create_plain_text("$");
3631                                 s = p + 1;
3632                                 if (obj != NULL) {
3633                                         append_object(retval, obj);
3634                                 }
3635                         }
3636                 } else if (*p == '\\' && *(p+1) == '#') {
3637                         strfold(p, 1);
3638                 } else if (*p == '#') {
3639                         char c;
3640                         if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3641                                 /* if remove_comment removed a newline, we need to 'back up' with p */
3642                                 p--;
3643                         }
3644                 }
3645                 p++;
3646         }
3647         obj = create_plain_text(s);
3648         if (obj != NULL) {
3649                 append_object(retval, obj);
3650         }
3651
3652         if (!ifblock_stack_empty(&ifblock_opaque)) {
3653                 NORM_ERR("one or more $endif's are missing");
3654         }
3655
3656         free(orig_p);
3657         return 0;
3658 }
3659
3660 static void extract_variable_text(const char *p)
3661 {
3662         free_text_objects(&global_root_object, 0);
3663         if (tmpstring1) {
3664                 free(tmpstring1);
3665                 tmpstring1 = 0;
3666         }
3667         if (tmpstring2) {
3668                 free(tmpstring2);
3669                 tmpstring2 = 0;
3670         }
3671         if (text_buffer) {
3672                 free(text_buffer);
3673                 text_buffer = 0;
3674         }
3675
3676         extract_variable_text_internal(&global_root_object, p);
3677 }
3678
3679 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
3680 {
3681         extract_variable_text_internal(root, txt);
3682         generate_text_internal(p, max_user_text, *root, cur);
3683 }
3684
3685 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3686                 void *thread(void *), const char *text)
3687 {
3688         if (obj->char_b && info.mail) {
3689                 // this means we use info
3690                 if (!info.mail->p_timed_thread) {
3691                         info.mail->p_timed_thread =
3692                                 timed_thread_create(thread,
3693                                                 (void *) info.mail, info.mail->interval * 1000000);
3694                         if (!info.mail->p_timed_thread) {
3695                                 NORM_ERR("Error creating %s timed thread", text);
3696                         }
3697                         timed_thread_register(info.mail->p_timed_thread,
3698                                         &info.mail->p_timed_thread);
3699                         if (timed_thread_run(info.mail->p_timed_thread)) {
3700                                 NORM_ERR("Error running %s timed thread", text);
3701                         }
3702                 }
3703                 return info.mail;
3704         } else if (obj->data.mail) {
3705                 // this means we use obj
3706                 if (!obj->data.mail->p_timed_thread) {
3707                         obj->data.mail->p_timed_thread =
3708                                 timed_thread_create(thread,
3709                                                 (void *) obj->data.mail,
3710                                                 obj->data.mail->interval * 1000000);
3711                         if (!obj->data.mail->p_timed_thread) {
3712                                 NORM_ERR("Error creating %s timed thread", text);
3713                         }
3714                         timed_thread_register(obj->data.mail->p_timed_thread,
3715                                         &obj->data.mail->p_timed_thread);
3716                         if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3717                                 NORM_ERR("Error running %s timed thread", text);
3718                         }
3719                 }
3720                 return obj->data.mail;
3721         } else if (!obj->a) {
3722                 // something is wrong, warn once then stop
3723                 NORM_ERR("There's a problem with your mail settings.  "
3724                                 "Check that the global mail settings are properly defined"
3725                                 " (line %li).", obj->line);
3726                 obj->a++;
3727         }
3728         return NULL;
3729 }
3730
3731 char *format_time(unsigned long timeval, const int width)
3732 {
3733         char buf[10];
3734         unsigned long nt;       // narrow time, for speed on 32-bit
3735         unsigned cc;            // centiseconds
3736         unsigned nn;            // multi-purpose whatever
3737
3738         nt = timeval;
3739         cc = nt % 100;          // centiseconds past second
3740         nt /= 100;                      // total seconds
3741         nn = nt % 60;           // seconds past the minute
3742         nt /= 60;                       // total minutes
3743         if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3744                                 nt, nn, cc)) {
3745                 return strndup(buf, text_buffer_size);
3746         }
3747         if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3748                 return strndup(buf, text_buffer_size);
3749         }
3750         nn = nt % 60;           // minutes past the hour
3751         nt /= 60;                       // total hours
3752         if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3753                 return strndup(buf, text_buffer_size);
3754         }
3755         nn = nt;                        // now also hours
3756         if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3757                 return strndup(buf, text_buffer_size);
3758         }
3759         nn /= 24;                       // now days
3760         if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3761                 return strndup(buf, text_buffer_size);
3762         }
3763         nn /= 7;                        // now weeks
3764         if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3765                 return strndup(buf, text_buffer_size);
3766         }
3767         // well shoot, this outta' fit...
3768         return strndup("<inf>", text_buffer_size);
3769 }
3770
3771 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3772 //string has to end with \0 and it's length should fit in a int
3773 #define BACKSPACE 8
3774 void remove_deleted_chars(char *string){
3775         int i = 0;
3776         while(string[i] != 0){
3777                 if(string[i] == BACKSPACE){
3778                         if(i != 0){
3779                                 strcpy( &(string[i-1]), &(string[i+1]) );
3780                                 i--;
3781                         }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3782                 }else i++;
3783         }
3784 }
3785
3786 static inline void format_media_player_time(char *buf, const int size,
3787                 int seconds)
3788 {
3789         int days, hours, minutes;
3790
3791         days = seconds / (24 * 60 * 60);
3792         seconds %= (24 * 60 * 60);
3793         hours = seconds / (60 * 60);
3794         seconds %= (60 * 60);
3795         minutes = seconds / 60;
3796         seconds %= 60;
3797
3798         if (days > 0) {
3799                 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3800                                 hours, minutes, seconds);
3801         } else if (hours > 0) {
3802                 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3803                                 seconds);
3804         } else {
3805                 snprintf(buf, size, "%i:%02i", minutes, seconds);
3806         }
3807 }
3808
3809 static inline double get_barnum(char *buf)
3810 {
3811         char *c = buf;
3812         double barnum;
3813
3814         while (*c) {
3815                 if (*c == '\001') {
3816                         *c = ' ';
3817                 }
3818                 c++;
3819         }
3820
3821         if (sscanf(buf, "%lf", &barnum) == 0) {
3822                 NORM_ERR("reading exec value failed (perhaps it's not the "
3823                                 "correct format?)");
3824                 return -1;
3825         }
3826         if (barnum > 100.0 || barnum < 0.0) {
3827                 NORM_ERR("your exec value is not between 0 and 100, "
3828                                 "therefore it will be ignored");
3829                 return -1;
3830         }
3831         return barnum;
3832 }
3833
3834 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
3835  * multiline objects like $exec work with $align[rc] and friends
3836  */
3837 void substitute_newlines(char *p, long l)
3838 {
3839         char *s = p;
3840         if (l < 0) return;
3841         while (p && *p && p < s + l) {
3842                 if (*p == '\n') {
3843                         /* only substitute if it's not the last newline */
3844                         *p = SECRIT_MULTILINE_CHAR;
3845                 }
3846                 p++;
3847         }
3848 }
3849
3850 static void generate_text_internal(char *p, int p_max_size,
3851                 struct text_object root, struct information *cur)
3852 {
3853         struct text_object *obj;
3854 #ifdef X11
3855         int need_to_load_fonts = 0;
3856 #endif /* X11 */
3857
3858         /* for the OBJ_top* handler */
3859         struct process **needed = 0;
3860
3861 #ifdef HAVE_ICONV
3862         char buff_in[p_max_size];
3863         buff_in[0] = 0;
3864         iconv_converting = 0;
3865 #endif /* HAVE_ICONV */
3866
3867         p[0] = 0;
3868         obj = root.next;
3869         while (obj && p_max_size > 0) {
3870                 needed = 0; /* reset for top stuff */
3871
3872 /* IFBLOCK jumping algorithm
3873  *
3874  * This is easier as it looks like:
3875  * - each IF checks it's condition
3876  *   - on FALSE: call DO_JUMP
3877  *   - on TRUE: don't care
3878  * - each ELSE calls DO_JUMP unconditionally
3879  * - each ENDIF is silently being ignored
3880  *
3881  * Why this works:
3882  * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3883  * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3884  * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3885  * regularly, "obj" is being updated to point to obj->next, so object parsing
3886  * continues right after the corresponding ELSE or ENDIF. This means that if we
3887  * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3888  * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3889  * jumped, and there is nothing to do.
3890  */
3891 #define DO_JUMP { \
3892         DBGP2("jumping"); \
3893         obj = obj->data.ifblock.next; \
3894 }
3895
3896 #define OBJ(a) break; case OBJ_##a:
3897
3898                 switch (obj->type) {
3899                         default:
3900                                 NORM_ERR("not implemented obj type %d", obj->type);
3901                         OBJ(read_tcp) {
3902                                 int sock, received;
3903                                 struct sockaddr_in addr;
3904                                 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3905                                 if(he != NULL) {
3906                                         sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3907                                         if(sock != -1) {
3908                                                 memset(&addr, 0, sizeof(addr));
3909                                                 addr.sin_family = AF_INET;
3910                                                 addr.sin_port = obj->data.read_tcp.port;
3911                                                 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3912                                                 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3913                                                         fd_set readfds;
3914                                                         struct timeval tv;
3915                                                         FD_ZERO(&readfds);
3916                                                         FD_SET(sock, &readfds);
3917                                                         tv.tv_sec = 1;
3918                                                         tv.tv_usec = 0;
3919                                                         if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3920                                                                 received = recv(sock, p, p_max_size, 0);
3921                                                                 p[received] = 0;
3922                                                         }
3923                                                         close(sock);
3924                                                 } else {
3925                                                         NORM_ERR("read_tcp: Couldn't create a connection");
3926                                                 }
3927                                         }else{
3928                                                 NORM_ERR("read_tcp: Couldn't create a socket");
3929                                         }
3930                                 }else{
3931                                         NORM_ERR("read_tcp: Problem with resolving the hostname");
3932                                 }
3933                         }
3934 #ifndef __OpenBSD__
3935                         OBJ(acpitemp) {
3936                                 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3937                         }
3938 #endif /* !__OpenBSD__ */
3939                         OBJ(freq) {
3940                                 if (obj->a) {
3941                                         obj->a = get_freq(p, p_max_size, "%.0f", 1,
3942                                                         obj->data.cpu_index);
3943                                 }
3944                         }
3945                         OBJ(freq_g) {
3946                                 if (obj->a) {
3947 #ifndef __OpenBSD__
3948                                         obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3949                                                         obj->data.cpu_index);
3950 #else
3951                                         /* OpenBSD has no such flag (SUSv2) */
3952                                         obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3953                                                         obj->data.cpu_index);
3954 #endif /* __OpenBSD */
3955                                 }
3956                         }
3957 #if defined(__linux__)
3958                         OBJ(voltage_mv) {
3959                                 if (obj->a) {
3960                                         obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3961                                                         obj->data.cpu_index);
3962                                 }
3963                         }
3964                         OBJ(voltage_v) {
3965                                 if (obj->a) {
3966                                         obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3967                                                         obj->data.cpu_index);
3968                                 }
3969                         }
3970
3971 #ifdef HAVE_IWLIB
3972                         OBJ(wireless_essid) {
3973                                 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3974                         }
3975                         OBJ(wireless_mode) {
3976                                 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3977                         }
3978                         OBJ(wireless_bitrate) {
3979                                 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3980                         }
3981                         OBJ(wireless_ap) {
3982                                 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3983                         }
3984                         OBJ(wireless_link_qual) {
3985                                 spaced_print(p, p_max_size, "%d", 4,
3986                                                 obj->data.net->link_qual);
3987                         }
3988                         OBJ(wireless_link_qual_max) {
3989                                 spaced_print(p, p_max_size, "%d", 4,
3990                                                 obj->data.net->link_qual_max);
3991                         }
3992                         OBJ(wireless_link_qual_perc) {
3993                                 if (obj->data.net->link_qual_max > 0) {
3994                                         spaced_print(p, p_max_size, "%.0f", 5,
3995                                                         (double) obj->data.net->link_qual /
3996                                                         obj->data.net->link_qual_max * 100);
3997                                 } else {
3998                                         spaced_print(p, p_max_size, "unk", 5);
3999                                 }
4000                         }
4001                         OBJ(wireless_link_bar) {
4002 #ifdef X11
4003                                 if(output_methods & TO_X) {
4004                                         new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
4005                                                 obj->data.net->link_qual_max) * 255.0);
4006                                 }else{
4007 #endif /* X11 */
4008                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4009                                         new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
4010                                                 obj->data.net->link_qual_max) * 100.0, obj->a);
4011 #ifdef X11
4012                                 }
4013 #endif /* X11 */
4014                         }
4015 #endif /* HAVE_IWLIB */
4016
4017 #endif /* __linux__ */
4018
4019 #ifndef __OpenBSD__
4020                         OBJ(adt746xcpu) {
4021                                 get_adt746x_cpu(p, p_max_size);
4022                         }
4023                         OBJ(adt746xfan) {
4024                                 get_adt746x_fan(p, p_max_size);
4025                         }
4026                         OBJ(acpifan) {
4027                                 get_acpi_fan(p, p_max_size);
4028                         }
4029                         OBJ(acpiacadapter) {
4030                                 get_acpi_ac_adapter(p, p_max_size);
4031                         }
4032                         OBJ(battery) {
4033                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
4034                         }
4035                         OBJ(battery_time) {
4036                                 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
4037                         }
4038                         OBJ(battery_percent) {
4039                                 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
4040                         }
4041                         OBJ(battery_bar) {
4042 #ifdef X11
4043                                 if(output_methods & TO_X) {
4044                                         new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
4045                                 }else{
4046 #endif /* X11 */
4047                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4048                                         new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
4049 #ifdef X11
4050                                 }
4051 #endif /* X11 */
4052                         }
4053                         OBJ(battery_short) {
4054                                 get_battery_short_status(p, p_max_size, obj->data.s);
4055                         }
4056 #endif /* __OpenBSD__ */
4057
4058                         OBJ(buffers) {
4059                                 human_readable(cur->buffers * 1024, p, 255);
4060                         }
4061                         OBJ(cached) {
4062                                 human_readable(cur->cached * 1024, p, 255);
4063                         }
4064                         OBJ(cpu) {
4065                                 if (obj->data.cpu_index > info.cpu_count) {
4066                                         NORM_ERR("obj->data.cpu_index %i info.cpu_count %i",
4067                                                         obj->data.cpu_index, info.cpu_count);
4068                                         CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
4069                                 }
4070                                 percent_print(p, p_max_size,
4071                                               round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
4072                         }
4073 #ifdef X11
4074                         OBJ(cpugauge)
4075                                 new_gauge(p, obj->a, obj->b,
4076                                                 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4077 #endif /* X11 */
4078                         OBJ(cpubar) {
4079 #ifdef X11
4080                                 if(output_methods & TO_X) {
4081                                         new_bar(p, obj->a, obj->b,
4082                                                 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4083                                 }else{
4084 #endif /* X11 */
4085                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4086                                         new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
4087 #ifdef X11
4088                                 }
4089 #endif /* X11 */
4090                         }
4091 #ifdef X11
4092                         OBJ(cpugraph) {
4093                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4094                                                 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
4095                                                 100, 1, obj->char_a, obj->char_b);
4096                         }
4097                         OBJ(loadgraph) {
4098                                 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
4099                                                 obj->e, 1, obj->char_a, obj->char_b);
4100                         }
4101                         OBJ(color) {
4102                                 new_fg(p, obj->data.l);
4103                         }
4104                         OBJ(color0) {
4105                                 new_fg(p, color0);
4106                         }
4107                         OBJ(color1) {
4108                                 new_fg(p, color1);
4109                         }
4110                         OBJ(color2) {
4111                                 new_fg(p, color2);
4112                         }
4113                         OBJ(color3) {
4114                                 new_fg(p, color3);
4115                         }
4116                         OBJ(color4) {
4117                                 new_fg(p, color4);
4118                         }
4119                         OBJ(color5) {
4120                                 new_fg(p, color5);
4121                         }
4122                         OBJ(color6) {
4123                                 new_fg(p, color6);
4124                         }
4125                         OBJ(color7) {
4126                                 new_fg(p, color7);
4127                         }
4128                         OBJ(color8) {
4129                                 new_fg(p, color8);
4130                         }
4131                         OBJ(color9) {
4132                                 new_fg(p, color9);
4133                         }
4134 #endif /* X11 */
4135                         OBJ(conky_version) {
4136                                 snprintf(p, p_max_size, "%s", VERSION);
4137                         }
4138                         OBJ(conky_build_date) {
4139                                 snprintf(p, p_max_size, "%s", BUILD_DATE);
4140                         }
4141                         OBJ(conky_build_arch) {
4142                                 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4143                         }
4144 #if defined(__linux__)
4145                         OBJ(disk_protect) {
4146                                 snprintf(p, p_max_size, "%s",
4147                                                 get_disk_protect_queue(obj->data.s));
4148                         }
4149                         OBJ(i8k_version) {
4150                                 snprintf(p, p_max_size, "%s", i8k.version);
4151                         }
4152                         OBJ(i8k_bios) {
4153                                 snprintf(p, p_max_size, "%s", i8k.bios);
4154                         }
4155                         OBJ(i8k_serial) {
4156                                 snprintf(p, p_max_size, "%s", i8k.serial);
4157                         }
4158                         OBJ(i8k_cpu_temp) {
4159                                 int cpu_temp;
4160
4161                                 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4162                                 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
4163                         }
4164                         OBJ(i8k_left_fan_status) {
4165                                 int left_fan_status;
4166
4167                                 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4168                                 if (left_fan_status == 0) {
4169                                         snprintf(p, p_max_size, "off");
4170                                 }
4171                                 if (left_fan_status == 1) {
4172                                         snprintf(p, p_max_size, "low");
4173                                 }
4174                                 if (left_fan_status == 2) {
4175                                         snprintf(p, p_max_size, "high");
4176                                 }
4177                         }
4178                         OBJ(i8k_right_fan_status) {
4179                                 int right_fan_status;
4180
4181                                 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4182                                 if (right_fan_status == 0) {
4183                                         snprintf(p, p_max_size, "off");
4184                                 }
4185                                 if (right_fan_status == 1) {
4186                                         snprintf(p, p_max_size, "low");
4187                                 }
4188                                 if (right_fan_status == 2) {
4189                                         snprintf(p, p_max_size, "high");
4190                                 }
4191                         }
4192                         OBJ(i8k_left_fan_rpm) {
4193                                 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4194                         }
4195                         OBJ(i8k_right_fan_rpm) {
4196                                 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4197                         }
4198                         OBJ(i8k_ac_status) {
4199                                 int ac_status;
4200
4201                                 sscanf(i8k.ac_status, "%d", &ac_status);
4202                                 if (ac_status == -1) {
4203                                         snprintf(p, p_max_size, "disabled (read i8k docs)");
4204                                 }
4205                                 if (ac_status == 0) {
4206                                         snprintf(p, p_max_size, "off");
4207                                 }
4208                                 if (ac_status == 1) {
4209                                         snprintf(p, p_max_size, "on");
4210                                 }
4211                         }
4212                         OBJ(i8k_buttons_status) {
4213                                 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4214                         }
4215 #if defined(IBM)
4216                         OBJ(ibm_fan) {
4217                                 get_ibm_acpi_fan(p, p_max_size);
4218                         }
4219                         OBJ(ibm_temps) {
4220                                 get_ibm_acpi_temps();
4221                                 temp_print(p, p_max_size,
4222                                            ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4223                         }
4224                         OBJ(ibm_volume) {
4225                                 get_ibm_acpi_volume(p, p_max_size);
4226                         }
4227                         OBJ(ibm_brightness) {
4228                                 get_ibm_acpi_brightness(p, p_max_size);
4229                         }
4230 #endif /* IBM */
4231                         /* information from sony_laptop kernel module
4232                          * /sys/devices/platform/sony-laptop */
4233                         OBJ(sony_fanspeed) {
4234                                 get_sony_fanspeed(p, p_max_size);
4235                         }
4236                         OBJ(if_gw) {
4237                                 if (!cur->gw_info.count) {
4238                                         DO_JUMP;
4239                                 }
4240                         }
4241                         OBJ(gw_iface) {
4242                                 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4243                         }
4244                         OBJ(gw_ip) {
4245                                 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4246                         }
4247                         OBJ(laptop_mode) {
4248                                 snprintf(p, p_max_size, "%d", get_laptop_mode());
4249                         }
4250                         OBJ(pb_battery) {
4251                                 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4252                         }
4253 #endif /* __linux__ */
4254 #if (defined(__FreeBSD__) || defined(__linux__))
4255                         OBJ(if_up) {
4256                                 if ((obj->data.ifblock.s)
4257                                                 && (!interface_up(obj->data.ifblock.s))) {
4258                                         DO_JUMP;
4259                                 }
4260                         }
4261 #endif
4262 #ifdef __OpenBSD__
4263                         OBJ(obsd_sensors_temp) {
4264                                 obsd_sensors.device = sensor_device;
4265                                 update_obsd_sensors();
4266                                 temp_print(p, p_max_size,
4267                                            obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4268                                            TEMP_CELSIUS);
4269                         }
4270                         OBJ(obsd_sensors_fan) {
4271                                 obsd_sensors.device = sensor_device;
4272                                 update_obsd_sensors();
4273                                 snprintf(p, p_max_size, "%d",
4274                                                 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4275                         }
4276                         OBJ(obsd_sensors_volt) {
4277                                 obsd_sensors.device = sensor_device;
4278                                 update_obsd_sensors();
4279                                 snprintf(p, p_max_size, "%.2f",
4280                                                 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4281                         }
4282                         OBJ(obsd_vendor) {
4283                                 get_obsd_vendor(p, p_max_size);
4284                         }
4285                         OBJ(obsd_product) {
4286                                 get_obsd_product(p, p_max_size);
4287                         }
4288 #endif /* __OpenBSD__ */
4289 #ifdef X11
4290                         OBJ(font) {
4291                                 new_font(p, obj->data.s);
4292                                 need_to_load_fonts = 1;
4293                         }
4294 #endif /* X11 */
4295                         /* TODO: move this correction from kB to kB/s elsewhere
4296                          * (or get rid of it??) */
4297                         OBJ(diskio) {
4298                                 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4299                                                 p, p_max_size);
4300                         }
4301                         OBJ(diskio_write) {
4302                                 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4303                                                 p, p_max_size);
4304                         }
4305                         OBJ(diskio_read) {
4306                                 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4307                                                 p, p_max_size);
4308                         }
4309 #ifdef X11
4310                         OBJ(diskiograph) {
4311                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4312                                           obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4313                         }
4314                         OBJ(diskiograph_read) {
4315                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4316                                           obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4317                         }
4318                         OBJ(diskiograph_write) {
4319                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4320                                           obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4321                         }
4322 #endif /* X11 */
4323                         OBJ(downspeed) {
4324                                 human_readable(obj->data.net->recv_speed, p, 255);
4325                         }
4326                         OBJ(downspeedf) {
4327                                 spaced_print(p, p_max_size, "%.1f", 8,
4328                                                 obj->data.net->recv_speed / 1024.0);
4329                         }
4330 #ifdef X11
4331                         OBJ(downspeedgraph) {
4332                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4333                                         obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4334                         }
4335 #endif /* X11 */
4336                         OBJ(else) {
4337                                 /* Since we see you, you're if has not jumped.
4338                                  * Do Ninja jump here: without leaving traces.
4339                                  * This is to prevent us from stale jumped flags.
4340                                  */
4341                                 obj = obj->data.ifblock.next;
4342                                 continue;
4343                         }
4344                         OBJ(endif) {
4345                                 /* harmless object, just ignore */
4346                         }
4347                         OBJ(addr) {
4348                                 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4349                                                 && (obj->data.net->addr.sa_data[3] & 255) == 0
4350                                                 && (obj->data.net->addr.sa_data[4] & 255) == 0
4351                                                 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4352                                         snprintf(p, p_max_size, "No Address");
4353                                 } else {
4354                                         snprintf(p, p_max_size, "%u.%u.%u.%u",
4355                                                 obj->data.net->addr.sa_data[2] & 255,
4356                                                 obj->data.net->addr.sa_data[3] & 255,
4357                                                 obj->data.net->addr.sa_data[4] & 255,
4358                                                 obj->data.net->addr.sa_data[5] & 255);
4359                                 }
4360                         }
4361 #if defined(__linux__)
4362                         OBJ(addrs) {
4363                                 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
4364                                         obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4365                                         strcpy(p, obj->data.net->addrs);
4366                                 } else {
4367                                         strcpy(p, "0.0.0.0");
4368                                 }
4369                         }
4370 #endif /* __linux__ */
4371 #if defined(IMLIB2) && defined(X11)
4372                         OBJ(image) {
4373                                 /* doesn't actually draw anything, just queues it omp.  the
4374                                  * image will get drawn after the X event loop */
4375                                 cimlib_add_image(obj->data.s);
4376                         }
4377 #endif /* IMLIB2 */
4378                         OBJ(eval) {
4379                                 evaluate(obj->data.s, p);
4380                         }
4381                         OBJ(exec) {
4382                                 read_exec(obj->data.s, p, text_buffer_size);
4383                                 remove_deleted_chars(p);
4384                         }
4385                         OBJ(execp) {
4386                                 struct information *tmp_info;
4387                                 struct text_object subroot;
4388
4389                                 read_exec(obj->data.s, p, text_buffer_size);
4390
4391                                 tmp_info = malloc(sizeof(struct information));
4392                                 memcpy(tmp_info, cur, sizeof(struct information));
4393                                 parse_conky_vars(&subroot, p, p, tmp_info);
4394
4395                                 free_text_objects(&subroot, 1);
4396                                 free(tmp_info);
4397                         }
4398 #ifdef X11
4399                         OBJ(execgauge) {
4400                                 double barnum;
4401
4402                                 read_exec(obj->data.s, p, text_buffer_size);
4403                                 barnum = get_barnum(p); /*using the same function*/
4404
4405                                 if (barnum >= 0.0) {
4406                                         barnum /= 100;
4407                                         new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4408                                 }
4409                         }
4410 #endif /* X11 */
4411                         OBJ(execbar) {
4412                                 double barnum;
4413
4414                                 read_exec(obj->data.s, p, text_buffer_size);
4415                                 barnum = get_barnum(p);
4416
4417                                 if (barnum >= 0.0) {
4418 #ifdef X11
4419                                         if(output_methods & TO_X) {
4420                                                 barnum /= 100;
4421                                                 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4422                                         }else{
4423 #endif /* X11 */
4424                                                 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4425                                                 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4426 #ifdef X11
4427                                         }
4428 #endif /* X11 */
4429                                 }
4430                         }
4431 #ifdef X11
4432                         OBJ(execgraph) {
4433                                 char showaslog = FALSE;
4434                                 char tempgrad = FALSE;
4435                                 double barnum;
4436                                 char *cmd = obj->data.s;
4437
4438                                 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4439                                         tempgrad = TRUE;
4440                                         cmd += strlen(" "TEMPGRAD);
4441                                 }
4442                                 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4443                                         showaslog = TRUE;
4444                                         cmd += strlen(" "LOGGRAPH);
4445                                 }
4446                                 read_exec(cmd, p, text_buffer_size);
4447                                 barnum = get_barnum(p);
4448
4449                                 if (barnum > 0) {
4450                                         new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4451                                                         100, 1, showaslog, tempgrad);
4452                                 }
4453                         }
4454 #endif /* X11 */
4455                         OBJ(execibar) {
4456                                 if (current_update_time - obj->data.execi.last_update
4457                                                 >= obj->data.execi.interval) {
4458                                         double barnum;
4459
4460                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
4461                                         barnum = get_barnum(p);
4462
4463                                         if (barnum >= 0.0) {
4464                                                 obj->f = barnum;
4465                                         }
4466                                         obj->data.execi.last_update = current_update_time;
4467                                 }
4468 #ifdef X11
4469                                 if(output_methods & TO_X) {
4470                                         new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4471                                 } else {
4472 #endif /* X11 */
4473                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4474                                         new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4475 #ifdef X11
4476                                 }
4477 #endif /* X11 */
4478                         }
4479 #ifdef X11
4480                         OBJ(execigraph) {
4481                                 if (current_update_time - obj->data.execi.last_update
4482                                                 >= obj->data.execi.interval) {
4483                                         double barnum;
4484                                         char showaslog = FALSE;
4485                                         char tempgrad = FALSE;
4486                                         char *cmd = obj->data.execi.cmd;
4487
4488                                         if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4489                                                 tempgrad = TRUE;
4490                                                 cmd += strlen(" "TEMPGRAD);
4491                                         }
4492                                         if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4493                                                 showaslog = TRUE;
4494                                                 cmd += strlen(" "LOGGRAPH);
4495                                         }
4496                                         obj->char_a = showaslog;
4497                                         obj->char_b = tempgrad;
4498                                         read_exec(cmd, p, text_buffer_size);
4499                                         barnum = get_barnum(p);
4500
4501                                         if (barnum >= 0.0) {
4502                                                 obj->f = barnum;
4503                                         }
4504                                         obj->data.execi.last_update = current_update_time;
4505                                 }
4506                                 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4507                         }
4508                         OBJ(execigauge) {
4509                                 if (current_update_time - obj->data.execi.last_update
4510                                                 >= obj->data.execi.interval) {
4511                                         double barnum;
4512
4513                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
4514                                         barnum = get_barnum(p);
4515
4516                                         if (barnum >= 0.0) {
4517                                                 obj->f = 255 * barnum / 100.0;
4518                                         }
4519                                         obj->data.execi.last_update = current_update_time;
4520                                 }
4521                                 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4522                         }
4523 #endif /* X11 */
4524                         OBJ(execi) {
4525                                 if (current_update_time - obj->data.execi.last_update
4526                                                 >= obj->data.execi.interval
4527                                                 && obj->data.execi.interval != 0) {
4528                                         read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4529                                                 text_buffer_size);
4530                                         obj->data.execi.last_update = current_update_time;
4531                                 }
4532                                 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4533                         }
4534                         OBJ(execpi) {
4535                                 struct text_object subroot;
4536                                 struct information *tmp_info =
4537                                         malloc(sizeof(struct information));
4538                                 memcpy(tmp_info, cur, sizeof(struct information));
4539
4540                                 if (current_update_time - obj->data.execi.last_update
4541                                                 < obj->data.execi.interval
4542                                                 || obj->data.execi.interval == 0) {
4543                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4544                                 } else {
4545                                         char *output = obj->data.execi.buffer;
4546                                         FILE *fp = pid_popen(obj->data.execi.cmd, "r", &childpid);
4547                                         int length = fread(output, 1, text_buffer_size, fp);
4548
4549                                         pclose(fp);
4550
4551                                         output[length] = '\0';
4552                                         if (length > 0 && output[length - 1] == '\n') {
4553                                                 output[length - 1] = '\0';
4554                                         }
4555
4556                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4557                                         obj->data.execi.last_update = current_update_time;
4558                                 }
4559                                 free_text_objects(&subroot, 1);
4560                                 free(tmp_info);
4561                         }
4562                         OBJ(texeci) {
4563                                 if (!obj->data.texeci.p_timed_thread) {
4564                                         obj->data.texeci.p_timed_thread =
4565                                                 timed_thread_create(&threaded_exec,
4566                                                 (void *) obj, obj->data.texeci.interval * 1000000);
4567                                         if (!obj->data.texeci.p_timed_thread) {
4568                                                 NORM_ERR("Error creating texeci timed thread");
4569                                         }
4570                                         /*
4571                                          * note that we don't register this thread with the
4572                                          * timed_thread list, because we destroy it manually
4573                                          */
4574                                         if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4575                                                 NORM_ERR("Error running texeci timed thread");
4576                                         }
4577                                 } else {
4578                                         timed_thread_lock(obj->data.texeci.p_timed_thread);
4579                                         snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4580                                         timed_thread_unlock(obj->data.texeci.p_timed_thread);
4581                                 }
4582                         }
4583                         OBJ(imap_unseen) {
4584                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4585
4586                                 if (mail && mail->p_timed_thread) {
4587                                         timed_thread_lock(mail->p_timed_thread);
4588                                         snprintf(p, p_max_size, "%lu", mail->unseen);
4589                                         timed_thread_unlock(mail->p_timed_thread);
4590                                 }
4591                         }
4592                         OBJ(imap_messages) {
4593                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4594
4595                                 if (mail && mail->p_timed_thread) {
4596                                         timed_thread_lock(mail->p_timed_thread);
4597                                         snprintf(p, p_max_size, "%lu", mail->messages);
4598                                         timed_thread_unlock(mail->p_timed_thread);
4599                                 }
4600                         }
4601                         OBJ(pop3_unseen) {
4602                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4603
4604                                 if (mail && mail->p_timed_thread) {
4605                                         timed_thread_lock(mail->p_timed_thread);
4606                                         snprintf(p, p_max_size, "%lu", mail->unseen);
4607                                         timed_thread_unlock(mail->p_timed_thread);
4608                                 }
4609                         }
4610                         OBJ(pop3_used) {
4611                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4612
4613                                 if (mail && mail->p_timed_thread) {
4614                                         timed_thread_lock(mail->p_timed_thread);
4615                                         snprintf(p, p_max_size, "%.1f",
4616                                                 mail->used / 1024.0 / 1024.0);
4617                                         timed_thread_unlock(mail->p_timed_thread);
4618                                 }
4619                         }
4620                         OBJ(fs_bar) {
4621                                 if (obj->data.fs != NULL) {
4622                                         if (obj->data.fs->size == 0) {
4623 #ifdef X11
4624                                                 if(output_methods & TO_X) {
4625                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4626                                                 }else{
4627 #endif /* X11 */
4628                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4629                                                         new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4630 #ifdef X11
4631                                                 }
4632 #endif /* X11 */
4633                                         } else {
4634 #ifdef X11
4635                                                 if(output_methods & TO_X) {
4636                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4637                                                                 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4638                                                                 obj->data.fs->size));
4639                                                 }else{
4640 #endif /* X11 */
4641                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4642                                                         new_bar_in_shell(p, p_max_size,
4643                                                                 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4644 #ifdef X11
4645                                                 }
4646 #endif /* X11 */
4647                                         }
4648                                 }
4649                         }
4650                         OBJ(fs_free) {
4651                                 if (obj->data.fs != NULL) {
4652                                         human_readable(obj->data.fs->avail, p, 255);
4653                                 }
4654                         }
4655                         OBJ(fs_free_perc) {
4656                                 if (obj->data.fs != NULL) {
4657                                         int val = 0;
4658
4659                                         if (obj->data.fs->size) {
4660                                                 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4661                                         }
4662
4663                                         percent_print(p, p_max_size, val);
4664                                 }
4665                         }
4666                         OBJ(fs_size) {
4667                                 if (obj->data.fs != NULL) {
4668                                         human_readable(obj->data.fs->size, p, 255);
4669                                 }
4670                         }
4671                         OBJ(fs_type) {
4672                                 if (obj->data.fs != NULL)
4673                                         snprintf(p, p_max_size, "%s", obj->data.fs->type);
4674                         }
4675                         OBJ(fs_used) {
4676                                 if (obj->data.fs != NULL) {
4677                                         human_readable(obj->data.fs->size - obj->data.fs->free, p,
4678                                                         255);
4679                                 }
4680                         }
4681                         OBJ(fs_bar_free) {
4682                                 if (obj->data.fs != NULL) {
4683                                         if (obj->data.fs->size == 0) {
4684 #ifdef X11
4685                                                 if(output_methods & TO_X) {
4686                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4687                                                 }else{
4688 #endif /* X11 */
4689                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4690                                                         new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4691 #ifdef X11
4692                                                 }
4693 #endif /* X11 */
4694                                         } else {
4695 #ifdef X11
4696                                                 if(output_methods & TO_X) {
4697                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4698                                                                 (int) (obj->data.fsbar.fs->avail * 255 /
4699                                                                 obj->data.fs->size));
4700                                                 }else{
4701 #endif /* X11 */
4702                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4703                                                         new_bar_in_shell(p, p_max_size,
4704                                                                 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4705 #ifdef X11
4706                                                 }
4707 #endif /* X11 */
4708                                         }
4709                                 }
4710                         }
4711                         OBJ(fs_used_perc) {
4712                                 if (obj->data.fs != NULL) {
4713                                         int val = 0;
4714
4715                                         if (obj->data.fs->size) {
4716                                                 val = obj->data.fs->free
4717                                                                 * 100 /
4718                                                         obj->data.fs->size;
4719                                         }
4720
4721                                         percent_print(p, p_max_size, 100 - val);
4722                                 }
4723                         }
4724                         OBJ(loadavg) {
4725                                 float *v = info.loadavg;
4726
4727                                 if (obj->data.loadavg[2]) {
4728                                         snprintf(p, p_max_size, "%.2f %.2f %.2f",
4729                                                 v[obj->data.loadavg[0] - 1],
4730                                                 v[obj->data.loadavg[1] - 1],
4731                                                 v[obj->data.loadavg[2] - 1]);
4732                                 } else if (obj->data.loadavg[1]) {
4733                                         snprintf(p, p_max_size, "%.2f %.2f",
4734                                                 v[obj->data.loadavg[0] - 1],
4735                                                 v[obj->data.loadavg[1] - 1]);
4736                                 } else if (obj->data.loadavg[0]) {
4737                                         snprintf(p, p_max_size, "%.2f",
4738                                                 v[obj->data.loadavg[0] - 1]);
4739                                 }
4740                         }
4741                         OBJ(goto) {
4742                                 new_goto(p, obj->data.i);
4743                         }
4744                         OBJ(tab) {
4745                                 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4746                         }
4747 #ifdef X11
4748                         OBJ(hr) {
4749                                 new_hr(p, obj->data.i);
4750                         }
4751 #endif
4752                         OBJ(nameserver) {
4753                                 if (cur->nameserver_info.nscount > obj->data.i)
4754                                         snprintf(p, p_max_size, "%s",
4755                                                         cur->nameserver_info.ns_list[obj->data.i]);
4756                         }
4757 #ifdef EVE
4758                         OBJ(eve) {
4759                                 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4760                                 snprintf(p, p_max_size, "%s", skill);
4761                         }
4762 #endif
4763 #ifdef HAVE_CURL
4764                         OBJ(curl) {
4765                                 if (obj->data.curl.uri != NULL) {
4766                                         ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
4767                                 } else {
4768                                         NORM_ERR("error processing Curl data");
4769                                 }
4770                         }
4771 #endif
4772 #ifdef RSS
4773                         OBJ(rss) {
4774                                 if (obj->data.rss.uri != NULL) {
4775                                         rss_process_info(p, p_max_size, obj->data.rss.uri, obj->data.rss.action, obj->data.rss.act_par, obj->data.rss.interval, obj->data.rss.nrspaces);
4776                                 } else {
4777                                         NORM_ERR("error processing RSS data");
4778                                 }
4779                         }
4780 #endif
4781 #ifdef WEATHER
4782                         OBJ(weather) {
4783                                 if (obj->data.weather.uri != NULL) {
4784                                         weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4785                                 } else {
4786                                         NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
4787                                 }
4788                         }
4789 #endif
4790 #ifdef HAVE_LUA
4791                         OBJ(lua) {
4792                                 char *str = llua_getstring(obj->data.s);
4793                                 if (str) {
4794                                         snprintf(p, p_max_size, "%s", str);
4795                                         free(str);
4796                                 }
4797                         }
4798                         OBJ(lua_parse) {
4799                                 char *str = llua_getstring(obj->data.s);
4800                                 if (str) {
4801                                         evaluate(str, p);
4802                                         free(str);
4803                                 }
4804                         }
4805                         OBJ(lua_bar) {
4806                                 double per;
4807                                 if (llua_getnumber(obj->data.s, &per)) {
4808 #ifdef X11
4809                                         if(output_methods & TO_X) {
4810                                                 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4811                                         } else {
4812 #endif /* X11 */
4813                                                 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4814                                                 new_bar_in_shell(p, p_max_size, per, obj->a);
4815 #ifdef X11
4816                                         }
4817 #endif /* X11 */
4818                                 }
4819                         }
4820 #ifdef X11
4821                         OBJ(lua_graph) {
4822                                 double per;
4823                                 if (llua_getnumber(obj->data.s, &per)) {
4824                                         new_graph(p, obj->a, obj->b, obj->c, obj->d,
4825                                                         per, obj->e, 1, obj->char_a, obj->char_b);
4826                                 }
4827                         }
4828                         OBJ(lua_gauge) {
4829                                 double per;
4830                                 if (llua_getnumber(obj->data.s, &per)) {
4831                                         new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4832                                 }
4833                         }
4834 #endif /* X11 */
4835 #endif /* HAVE_LUA */
4836 #ifdef HDDTEMP
4837                         OBJ(hddtemp) {
4838                                 char *endptr, unit;
4839                                 long val;
4840                                 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4841                                         if (obj->data.hddtemp.temp)
4842                                                 free(obj->data.hddtemp.temp);
4843                                         obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4844                                                         obj->data.hddtemp.addr, obj->data.hddtemp.port);
4845                                         obj->data.hddtemp.update_time = current_update_time;
4846                                 }
4847                                 if (!obj->data.hddtemp.temp) {
4848                                         snprintf(p, p_max_size, "N/A");
4849                                 } else {
4850                                         val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4851                                         unit = obj->data.hddtemp.temp[0];
4852
4853                                         if (*endptr != '\0')
4854                                                 snprintf(p, p_max_size, "N/A");
4855                                         else if (unit == 'C')
4856                                                 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4857                                         else if (unit == 'F')
4858                                                 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4859                                         else
4860                                                 snprintf(p, p_max_size, "N/A");
4861                                 }
4862                         }
4863 #endif
4864                         OBJ(offset) {
4865                                 new_offset(p, obj->data.i);
4866                         }
4867                         OBJ(voffset) {
4868                                 new_voffset(p, obj->data.i);
4869                         }
4870 #ifdef __linux__
4871                         OBJ(i2c) {
4872                                 double r;
4873
4874                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4875                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4876
4877                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4878
4879                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4880                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4881                                 } else if (r >= 100.0 || r == 0) {
4882                                         snprintf(p, p_max_size, "%d", (int) r);
4883                                 } else {
4884                                         snprintf(p, p_max_size, "%.1f", r);
4885                                 }
4886                         }
4887                         OBJ(platform) {
4888                                 double r;
4889
4890                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4891                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4892
4893                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4894
4895                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4896                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4897                                 } else if (r >= 100.0 || r == 0) {
4898                                         snprintf(p, p_max_size, "%d", (int) r);
4899                                 } else {
4900                                         snprintf(p, p_max_size, "%.1f", r);
4901                                 }
4902                         }
4903                         OBJ(hwmon) {
4904                                 double r;
4905
4906                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4907                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4908
4909                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4910
4911                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4912                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4913                                 } else if (r >= 100.0 || r == 0) {
4914                                         snprintf(p, p_max_size, "%d", (int) r);
4915                                 } else {
4916                                         snprintf(p, p_max_size, "%.1f", r);
4917                                 }
4918                         }
4919 #endif /* __linux__ */
4920                         OBJ(alignr) {
4921                                 new_alignr(p, obj->data.i);
4922                         }
4923                         OBJ(alignc) {
4924                                 new_alignc(p, obj->data.i);
4925                         }
4926                         OBJ(if_empty) {
4927                                 char buf[max_user_text];
4928                                 struct information *tmp_info =
4929                                         malloc(sizeof(struct information));
4930                                 memcpy(tmp_info, cur, sizeof(struct information));
4931                                 generate_text_internal(buf, max_user_text,
4932                                                        *obj->sub, tmp_info);
4933
4934                                 if (strlen(buf) != 0) {
4935                                         DO_JUMP;
4936                                 }
4937                                 free(tmp_info);
4938                         }
4939                         OBJ(if_match) {
4940                                 char expression[max_user_text];
4941                                 int val;
4942                                 struct information *tmp_info;
4943
4944                                 tmp_info = malloc(sizeof(struct information));
4945                                 memcpy(tmp_info, cur, sizeof(struct information));
4946                                 generate_text_internal(expression, max_user_text,
4947                                                        *obj->sub, tmp_info);
4948                                 DBGP("parsed arg into '%s'", expression);
4949
4950                                 val = compare(expression);
4951                                 if (val == -2) {
4952                                         NORM_ERR("compare failed for expression '%s'",
4953                                                         expression);
4954                                 } else if (!val) {
4955                                         DO_JUMP;
4956                                 }
4957                                 free(tmp_info);
4958                         }
4959                         OBJ(if_existing) {
4960                                 if (obj->data.ifblock.str
4961                                     && !check_contains(obj->data.ifblock.s,
4962                                                        obj->data.ifblock.str)) {
4963                                         DO_JUMP;
4964                                 } else if (obj->data.ifblock.s
4965                                            && access(obj->data.ifblock.s, F_OK)) {
4966                                         DO_JUMP;
4967                                 }
4968                         }
4969                         OBJ(if_mounted) {
4970                                 if ((obj->data.ifblock.s)
4971                                                 && (!check_mount(obj->data.ifblock.s))) {
4972                                         DO_JUMP;
4973                                 }
4974                         }
4975                         OBJ(if_running) {
4976 #ifdef __linux__
4977                                 if (!get_process_by_name(obj->data.ifblock.s)) {
4978 #else
4979                                 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4980 #endif
4981                                         DO_JUMP;
4982                                 }
4983                         }
4984 #if defined(__linux__)
4985                         OBJ(ioscheduler) {
4986                                 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4987                         }
4988 #endif
4989                         OBJ(kernel) {
4990                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4991                         }
4992                         OBJ(machine) {
4993                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4994                         }
4995
4996                         /* memory stuff */
4997                         OBJ(mem) {
4998                                 human_readable(cur->mem * 1024, p, 255);
4999                         }
5000                         OBJ(memeasyfree) {
5001                                 human_readable(cur->memeasyfree * 1024, p, 255);
5002                         }
5003                         OBJ(memfree) {
5004                                 human_readable(cur->memfree * 1024, p, 255);
5005                         }
5006                         OBJ(memmax) {
5007                                 human_readable(cur->memmax * 1024, p, 255);
5008                         }
5009                         OBJ(memperc) {
5010                                 if (cur->memmax)
5011                                         percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
5012                         }
5013 #ifdef X11
5014                         OBJ(memgauge){
5015                                 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
5016                                         cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5017                         }
5018 #endif /* X11 */
5019                         OBJ(membar) {
5020 #ifdef X11
5021                                 if(output_methods & TO_X) {
5022                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
5023                                                 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5024                                 }else{
5025 #endif /* X11 */
5026                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5027                                         new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
5028 #ifdef X11
5029                                 }
5030 #endif /* X11 */
5031                         }
5032 #ifdef X11
5033                         OBJ(memgraph) {
5034                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5035                                         cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5036                                         100, 1, obj->char_a, obj->char_b);
5037                         }
5038 #endif /* X11 */
5039                         /* mixer stuff */
5040                         OBJ(mixer) {
5041                                 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
5042                         }
5043                         OBJ(mixerl) {
5044                                 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
5045                         }
5046                         OBJ(mixerr) {
5047                                 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
5048                         }
5049 #ifdef X11
5050                         OBJ(mixerbar) {
5051                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5052                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
5053                         }
5054                         OBJ(mixerlbar) {
5055                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5056                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
5057                         }
5058                         OBJ(mixerrbar) {
5059                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5060                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
5061                         }
5062 #endif /* X11 */
5063                         OBJ(if_mixer_mute) {
5064                                 if (!mixer_is_mute(obj->data.ifblock.i)) {
5065                                         DO_JUMP;
5066                                 }
5067                         }
5068 #ifdef X11
5069 #define NOT_IN_X "Not running in X"
5070                         OBJ(monitor) {
5071                                 if(x_initialised != YES) {
5072                                         strncpy(p, NOT_IN_X, p_max_size);
5073                                 }else{
5074                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
5075                                 }
5076                         }
5077                         OBJ(monitor_number) {
5078                                 if(x_initialised != YES) {
5079                                         strncpy(p, NOT_IN_X, p_max_size);
5080                                 }else{
5081                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
5082                                 }
5083                         }
5084                         OBJ(desktop) {
5085                                 if(x_initialised != YES) {
5086                                         strncpy(p, NOT_IN_X, p_max_size);
5087                                 }else{
5088                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
5089                                 }
5090                         }
5091                         OBJ(desktop_number) {
5092                                 if(x_initialised != YES) {
5093                                         strncpy(p, NOT_IN_X, p_max_size);
5094                                 }else{
5095                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
5096                                 }
5097                         }
5098                         OBJ(desktop_name) {
5099                                 if(x_initialised != YES) {
5100                                         strncpy(p, NOT_IN_X, p_max_size);
5101                                 }else if(cur->x11.desktop.name != NULL) {
5102                                         strncpy(p, cur->x11.desktop.name, p_max_size);
5103                                 }
5104                         }
5105 #endif /* X11 */
5106
5107                         /* mail stuff */
5108                         OBJ(mails) {
5109                                 update_mail_count(&obj->data.local_mail);
5110                                 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5111                         }
5112                         OBJ(new_mails) {
5113                                 update_mail_count(&obj->data.local_mail);
5114                                 snprintf(p, p_max_size, "%d",
5115                                         obj->data.local_mail.new_mail_count);
5116                         }
5117                         OBJ(seen_mails) {
5118                                 update_mail_count(&obj->data.local_mail);
5119                                 snprintf(p, p_max_size, "%d",
5120                                         obj->data.local_mail.seen_mail_count);
5121                         }
5122                         OBJ(unseen_mails) {
5123                                 update_mail_count(&obj->data.local_mail);
5124                                 snprintf(p, p_max_size, "%d",
5125                                         obj->data.local_mail.unseen_mail_count);
5126                         }
5127                         OBJ(flagged_mails) {
5128                                 update_mail_count(&obj->data.local_mail);
5129                                 snprintf(p, p_max_size, "%d",
5130                                         obj->data.local_mail.flagged_mail_count);
5131                         }
5132                         OBJ(unflagged_mails) {
5133                                 update_mail_count(&obj->data.local_mail);
5134                                 snprintf(p, p_max_size, "%d",
5135                                         obj->data.local_mail.unflagged_mail_count);
5136                         }
5137                         OBJ(forwarded_mails) {
5138                                 update_mail_count(&obj->data.local_mail);
5139                                 snprintf(p, p_max_size, "%d",
5140                                         obj->data.local_mail.forwarded_mail_count);
5141                         }
5142                         OBJ(unforwarded_mails) {
5143                                 update_mail_count(&obj->data.local_mail);
5144                                 snprintf(p, p_max_size, "%d",
5145                                         obj->data.local_mail.unforwarded_mail_count);
5146                         }
5147                         OBJ(replied_mails) {
5148                                 update_mail_count(&obj->data.local_mail);
5149                                 snprintf(p, p_max_size, "%d",
5150                                         obj->data.local_mail.replied_mail_count);
5151                         }
5152                         OBJ(unreplied_mails) {
5153                                 update_mail_count(&obj->data.local_mail);
5154                                 snprintf(p, p_max_size, "%d",
5155                                         obj->data.local_mail.unreplied_mail_count);
5156                         }
5157                         OBJ(draft_mails) {
5158                                 update_mail_count(&obj->data.local_mail);
5159                                 snprintf(p, p_max_size, "%d",
5160                                         obj->data.local_mail.draft_mail_count);
5161                         }
5162                         OBJ(trashed_mails) {
5163                                 update_mail_count(&obj->data.local_mail);
5164                                 snprintf(p, p_max_size, "%d",
5165                                         obj->data.local_mail.trashed_mail_count);
5166                         }
5167                         OBJ(mboxscan) {
5168                                 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5169                                         text_buffer_size);
5170                                 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5171                         }
5172                         OBJ(nodename) {
5173                                 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5174                         }
5175                         OBJ(outlinecolor) {
5176                                 new_outline(p, obj->data.l);
5177                         }
5178                         OBJ(processes) {
5179                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5180                         }
5181                         OBJ(running_processes) {
5182                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5183                         }
5184                         OBJ(text) {
5185                                 snprintf(p, p_max_size, "%s", obj->data.s);
5186                         }
5187 #ifdef X11
5188                         OBJ(shadecolor) {
5189                                 new_bg(p, obj->data.l);
5190                         }
5191                         OBJ(stippled_hr) {
5192                                 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5193                         }
5194 #endif /* X11 */
5195                         OBJ(swap) {
5196                                 human_readable(cur->swap * 1024, p, 255);
5197                         }
5198                         OBJ(swapfree) {
5199                                 human_readable(cur->swapfree * 1024, p, 255);
5200                         }
5201                         OBJ(swapmax) {
5202                                 human_readable(cur->swapmax * 1024, p, 255);
5203                         }
5204                         OBJ(swapperc) {
5205                                 if (cur->swapmax == 0) {
5206                                         strncpy(p, "No swap", p_max_size);
5207                                 } else {
5208                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5209                                 }
5210                         }
5211                         OBJ(swapbar) {
5212 #ifdef X11
5213                                 if(output_methods & TO_X) {
5214                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
5215                                                 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5216                                 }else{
5217 #endif /* X11 */
5218                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5219                                         new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5220 #ifdef X11
5221                                 }
5222 #endif /* X11 */
5223                         }
5224                         OBJ(sysname) {
5225                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5226                         }
5227                         OBJ(time) {
5228                                 time_t t = time(NULL);
5229                                 struct tm *tm = localtime(&t);
5230
5231                                 setlocale(LC_TIME, "");
5232                                 strftime(p, p_max_size, obj->data.s, tm);
5233                         }
5234                         OBJ(utime) {
5235                                 time_t t = time(NULL);
5236                                 struct tm *tm = gmtime(&t);
5237
5238                                 strftime(p, p_max_size, obj->data.s, tm);
5239                         }
5240                         OBJ(tztime) {
5241                                 char *oldTZ = NULL;
5242                                 time_t t;
5243                                 struct tm *tm;
5244
5245                                 if (obj->data.tztime.tz) {
5246                                         oldTZ = getenv("TZ");
5247                                         setenv("TZ", obj->data.tztime.tz, 1);
5248                                         tzset();
5249                                 }
5250                                 t = time(NULL);
5251                                 tm = localtime(&t);
5252
5253                                 setlocale(LC_TIME, "");
5254                                 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5255                                 if (oldTZ) {
5256                                         setenv("TZ", oldTZ, 1);
5257                                         tzset();
5258                                 } else {
5259                                         unsetenv("TZ");
5260                                 }
5261                                 // Needless to free oldTZ since getenv gives ptr to static data
5262                         }
5263                         OBJ(totaldown) {
5264                                 human_readable(obj->data.net->recv, p, 255);
5265                         }
5266                         OBJ(totalup) {
5267                                 human_readable(obj->data.net->trans, p, 255);
5268                         }
5269                         OBJ(updates) {
5270                                 snprintf(p, p_max_size, "%d", total_updates);
5271                         }
5272                         OBJ(if_updatenr) {
5273                                 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5274                                         DO_JUMP;
5275                                 }
5276                         }
5277                         OBJ(upspeed) {
5278                                 human_readable(obj->data.net->trans_speed, p, 255);
5279                         }
5280                         OBJ(upspeedf) {
5281                                 spaced_print(p, p_max_size, "%.1f", 8,
5282                                         obj->data.net->trans_speed / 1024.0);
5283                         }
5284 #ifdef X11
5285                         OBJ(upspeedgraph) {
5286                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5287                                         obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5288                         }
5289 #endif /* X11 */
5290                         OBJ(uptime_short) {
5291                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
5292                         }
5293                         OBJ(uptime) {
5294                                 format_seconds(p, p_max_size, (int) cur->uptime);
5295                         }
5296                         OBJ(user_names) {
5297                                 snprintf(p, p_max_size, "%s", cur->users.names);
5298                         }
5299                         OBJ(user_terms) {
5300                                 snprintf(p, p_max_size, "%s", cur->users.terms);
5301                         }
5302                         OBJ(user_times) {
5303                                 snprintf(p, p_max_size, "%s", cur->users.times);
5304                         }
5305                         OBJ(user_number) {
5306                                 snprintf(p, p_max_size, "%d", cur->users.number);
5307                         }
5308 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5309                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5310                         OBJ(apm_adapter) {
5311                                 char *msg;
5312
5313                                 msg = get_apm_adapter();
5314                                 snprintf(p, p_max_size, "%s", msg);
5315                                 free(msg);
5316                         }
5317                         OBJ(apm_battery_life) {
5318                                 char *msg;
5319
5320                                 msg = get_apm_battery_life();
5321                                 snprintf(p, p_max_size, "%s", msg);
5322                                 free(msg);
5323                         }
5324                         OBJ(apm_battery_time) {
5325                                 char *msg;
5326
5327                                 msg = get_apm_battery_time();
5328                                 snprintf(p, p_max_size, "%s", msg);
5329                                 free(msg);
5330                         }
5331 #endif /* __FreeBSD__ __OpenBSD__ */
5332
5333 #ifdef MPD
5334 #define mpd_printf(fmt, val) \
5335         snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5336 #define mpd_sprintf(val) { \
5337         if (!obj->data.i || obj->data.i > p_max_size) \
5338                 mpd_printf("%s", val); \
5339         else \
5340                 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5341 }
5342                         OBJ(mpd_title)
5343                                 mpd_sprintf(title);
5344                         OBJ(mpd_artist)
5345                                 mpd_sprintf(artist);
5346                         OBJ(mpd_album)
5347                                 mpd_sprintf(album);
5348                         OBJ(mpd_random)
5349                                 mpd_printf("%s", random);
5350                         OBJ(mpd_repeat)
5351                                 mpd_printf("%s", repeat);
5352                         OBJ(mpd_track)
5353                                 mpd_sprintf(track);
5354                         OBJ(mpd_name)
5355                                 mpd_sprintf(name);
5356                         OBJ(mpd_file)
5357                                 mpd_sprintf(file);
5358                         OBJ(mpd_vol)
5359                                 mpd_printf("%d", volume);
5360                         OBJ(mpd_bitrate)
5361                                 mpd_printf("%d", bitrate);
5362                         OBJ(mpd_status)
5363                                 mpd_printf("%s", status);
5364                         OBJ(mpd_elapsed) {
5365                                 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5366                         }
5367                         OBJ(mpd_length) {
5368                                 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5369                         }
5370                         OBJ(mpd_percent) {
5371                                 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5372                         }
5373                         OBJ(mpd_bar) {
5374 #ifdef X11
5375                                 if(output_methods & TO_X) {
5376                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
5377                                                 (int) (mpd_get_info()->progress * 255.0f));
5378                                 } else {
5379 #endif /* X11 */
5380                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5381                                         new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5382 #ifdef X11
5383                                 }
5384 #endif /* X11 */
5385                         }
5386                         OBJ(mpd_smart) {
5387                                 struct mpd_s *mpd = mpd_get_info();
5388                                 int len = obj->data.i;
5389                                 if (len == 0 || len > p_max_size)
5390                                         len = p_max_size;
5391
5392                                 memset(p, 0, p_max_size);
5393                                 if (mpd->artist && *mpd->artist &&
5394                                     mpd->title && *mpd->title) {
5395                                         snprintf(p, len, "%s - %s", mpd->artist,
5396                                                 mpd->title);
5397                                 } else if (mpd->title && *mpd->title) {
5398                                         snprintf(p, len, "%s", mpd->title);
5399                                 } else if (mpd->artist && *mpd->artist) {
5400                                         snprintf(p, len, "%s", mpd->artist);
5401                                 } else if (mpd->file && *mpd->file) {
5402                                         snprintf(p, len, "%s", mpd->file);
5403                                 } else {
5404                                         *p = 0;
5405                                 }
5406                         }
5407                         OBJ(if_mpd_playing) {
5408                                 if (!mpd_get_info()->is_playing) {
5409                                         DO_JUMP;
5410                                 }
5411                         }
5412 #undef mpd_sprintf
5413 #undef mpd_printf
5414 #endif
5415
5416 #ifdef MOC
5417 #define MOC_PRINT(t, a) \
5418         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5419                         OBJ(moc_state) {
5420                                 MOC_PRINT(state, "??");
5421                         }
5422                         OBJ(moc_file) {
5423                                 MOC_PRINT(file, "no file");
5424                         }
5425                         OBJ(moc_title) {
5426                                 MOC_PRINT(title, "no title");
5427                         }
5428                         OBJ(moc_artist) {
5429                                 MOC_PRINT(artist, "no artist");
5430                         }
5431                         OBJ(moc_song) {
5432                                 MOC_PRINT(song, "no song");
5433                         }
5434                         OBJ(moc_album) {
5435                                 MOC_PRINT(album, "no album");
5436                         }
5437                         OBJ(moc_totaltime) {
5438                                 MOC_PRINT(totaltime, "0:00");
5439                         }
5440                         OBJ(moc_timeleft) {
5441                                 MOC_PRINT(timeleft, "0:00");
5442                         }
5443                         OBJ(moc_curtime) {
5444                                 MOC_PRINT(curtime, "0:00");
5445                         }
5446                         OBJ(moc_bitrate) {
5447                                 MOC_PRINT(bitrate, "0Kbps");
5448                         }
5449                         OBJ(moc_rate) {
5450                                 MOC_PRINT(rate, "0KHz");
5451                         }
5452 #undef MOC_PRINT
5453 #endif /* MOC */
5454 #ifdef XMMS2
5455                         OBJ(xmms2_artist) {
5456                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5457                         }
5458                         OBJ(xmms2_album) {
5459                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5460                         }
5461                         OBJ(xmms2_title) {
5462                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5463                         }
5464                         OBJ(xmms2_genre) {
5465                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5466                         }
5467                         OBJ(xmms2_comment) {
5468                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5469                         }
5470                         OBJ(xmms2_url) {
5471                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5472                         }
5473                         OBJ(xmms2_status) {
5474                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5475                         }
5476                         OBJ(xmms2_date) {
5477                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5478                         }
5479                         OBJ(xmms2_tracknr) {
5480                                 if (cur->xmms2.tracknr != -1) {
5481                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5482                                 }
5483                         }
5484                         OBJ(xmms2_bitrate) {
5485                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5486                         }
5487                         OBJ(xmms2_id) {
5488                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5489                         }
5490                         OBJ(xmms2_size) {
5491                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5492                         }
5493                         OBJ(xmms2_elapsed) {
5494                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5495                                         (cur->xmms2.elapsed / 1000) % 60);
5496                         }
5497                         OBJ(xmms2_duration) {
5498                                 snprintf(p, p_max_size, "%02d:%02d",
5499                                         cur->xmms2.duration / 60000,
5500                                         (cur->xmms2.duration / 1000) % 60);
5501                         }
5502                         OBJ(xmms2_percent) {
5503                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5504                         }
5505 #ifdef X11
5506                         OBJ(xmms2_bar) {
5507                                 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5508                                         (int) (cur->xmms2.progress * 255.0f));
5509                         }
5510 #endif /* X11 */
5511                         OBJ(xmms2_playlist) {
5512                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5513                         }
5514                         OBJ(xmms2_timesplayed) {
5515                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5516                         }
5517                         OBJ(xmms2_smart) {
5518                                 if (strlen(cur->xmms2.title) < 2
5519                                                 && strlen(cur->xmms2.title) < 2) {
5520                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
5521                                 } else {
5522                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5523                                                 cur->xmms2.title);
5524                                 }
5525                         }
5526                         OBJ(if_xmms2_connected) {
5527                                 if (cur->xmms2.conn_state != 1) {
5528                                         DO_JUMP;
5529                                 }
5530                         }
5531 #endif /* XMMS */
5532 #ifdef AUDACIOUS
5533                         OBJ(audacious_status) {
5534                                 snprintf(p, p_max_size, "%s",
5535                                         cur->audacious.items[AUDACIOUS_STATUS]);
5536                         }
5537                         OBJ(audacious_title) {
5538                                 snprintf(p, cur->audacious.max_title_len > 0
5539                                         ? cur->audacious.max_title_len : p_max_size, "%s",
5540                                         cur->audacious.items[AUDACIOUS_TITLE]);
5541                         }
5542                         OBJ(audacious_length) {
5543                                 snprintf(p, p_max_size, "%s",
5544                                         cur->audacious.items[AUDACIOUS_LENGTH]);
5545                         }
5546                         OBJ(audacious_length_seconds) {
5547                                 snprintf(p, p_max_size, "%s",
5548                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5549                         }
5550                         OBJ(audacious_position) {
5551                                 snprintf(p, p_max_size, "%s",
5552                                         cur->audacious.items[AUDACIOUS_POSITION]);
5553                         }
5554                         OBJ(audacious_position_seconds) {
5555                                 snprintf(p, p_max_size, "%s",
5556                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5557                         }
5558                         OBJ(audacious_bitrate) {
5559                                 snprintf(p, p_max_size, "%s",
5560                                         cur->audacious.items[AUDACIOUS_BITRATE]);
5561                         }
5562                         OBJ(audacious_frequency) {
5563                                 snprintf(p, p_max_size, "%s",
5564                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
5565                         }
5566                         OBJ(audacious_channels) {
5567                                 snprintf(p, p_max_size, "%s",
5568                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
5569                         }
5570                         OBJ(audacious_filename) {
5571                                 snprintf(p, p_max_size, "%s",
5572                                         cur->audacious.items[AUDACIOUS_FILENAME]);
5573                         }
5574                         OBJ(audacious_playlist_length) {
5575                                 snprintf(p, p_max_size, "%s",
5576                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5577                         }
5578                         OBJ(audacious_playlist_position) {
5579                                 snprintf(p, p_max_size, "%s",
5580                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5581                         }
5582                         OBJ(audacious_main_volume) {
5583                                 snprintf(p, p_max_size, "%s",
5584                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5585                         }
5586 #ifdef X11
5587                         OBJ(audacious_bar) {
5588                                 double progress;
5589
5590                                 progress =
5591                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5592                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5593                                 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5594                         }
5595 #endif /* X11 */
5596 #endif /* AUDACIOUS */
5597
5598 #ifdef BMPX
5599                         OBJ(bmpx_title) {
5600                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5601                         }
5602                         OBJ(bmpx_artist) {
5603                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5604                         }
5605                         OBJ(bmpx_album) {
5606                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5607                         }
5608                         OBJ(bmpx_uri) {
5609                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5610                         }
5611                         OBJ(bmpx_track) {
5612                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5613                         }
5614                         OBJ(bmpx_bitrate) {
5615                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5616                         }
5617 #endif /* BMPX */
5618                         /* we have four different types of top (top, top_mem,
5619                          * top_time and top_io). To avoid having almost-same code four
5620                          * times, we have this special handler. */
5621                         break;
5622                         case OBJ_top:
5623                                 parse_top_args("top", obj->data.top.s, obj);
5624                                 if (!needed) needed = cur->cpu;
5625                         case OBJ_top_mem:
5626                                 parse_top_args("top_mem", obj->data.top.s, obj);
5627                                 if (!needed) needed = cur->memu;
5628                         case OBJ_top_time:
5629                                 parse_top_args("top_time", obj->data.top.s, obj);
5630                                 if (!needed) needed = cur->time;
5631 #ifdef IOSTATS
5632                         case OBJ_top_io:
5633                                 parse_top_args("top_io", obj->data.top.s, obj);
5634                                 if (!needed) needed = cur->io;
5635 #endif
5636
5637                                 if (needed[obj->data.top.num]) {
5638                                         char *timeval;
5639
5640                                         switch (obj->data.top.type) {
5641                                                 case TOP_NAME:
5642                                                         snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5643                                                                         needed[obj->data.top.num]->name);
5644                                                         break;
5645                                                 case TOP_CPU:
5646                                                         snprintf(p, 7, "%6.2f",
5647                                                                         needed[obj->data.top.num]->amount);
5648                                                         break;
5649                                                 case TOP_PID:
5650                                                         snprintf(p, 6, "%5i",
5651                                                                         needed[obj->data.top.num]->pid);
5652                                                         break;
5653                                                 case TOP_MEM:
5654                                                         snprintf(p, 7, "%6.2f",
5655                                                                         needed[obj->data.top.num]->totalmem);
5656                                                         break;
5657                                                 case TOP_TIME:
5658                                                         timeval = format_time(
5659                                                                         needed[obj->data.top.num]->total_cpu_time, 9);
5660                                                         snprintf(p, 10, "%9s", timeval);
5661                                                         free(timeval);
5662                                                         break;
5663                                                 case TOP_MEM_RES:
5664                                                         human_readable(needed[obj->data.top.num]->rss,
5665                                                                         p, 255);
5666                                                         break;
5667                                                 case TOP_MEM_VSIZE:
5668                                                         human_readable(needed[obj->data.top.num]->vsize,
5669                                                                         p, 255);
5670                                                         break;
5671 #ifdef IOSTATS
5672                                                 case TOP_READ_BYTES:
5673                                                         human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5674                                                                         p, 255);
5675                                                         break;
5676                                                 case TOP_WRITE_BYTES:
5677                                                         human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5678                                                                         p, 255);
5679                                                         break;
5680                                                 case TOP_IO_PERC:
5681                                                         snprintf(p, 7, "%6.2f",
5682                                                                         needed[obj->data.top.num]->io_perc);
5683                                                         break;
5684 #endif
5685                                         }
5686                                 }
5687                         OBJ(tail) {
5688                                 print_tailhead("tail", obj, p, p_max_size);
5689                         }
5690                         OBJ(head) {
5691                                 print_tailhead("head", obj, p, p_max_size);
5692                         }
5693                         OBJ(lines) {
5694                                 FILE *fp = open_file(obj->data.s, &obj->a);
5695
5696                                 if(fp != NULL) {
5697 /* FIXME: use something more general (see also tail.c, head.c */
5698 #define BUFSZ 0x1000
5699                                         char buf[BUFSZ];
5700                                         int j, lines;
5701
5702                                         lines = 0;
5703                                         while(fgets(buf, BUFSZ, fp) != NULL){
5704                                                 for(j = 0; buf[j] != 0; j++) {
5705                                                         if(buf[j] == '\n') {
5706                                                                 lines++;
5707                                                         }
5708                                                 }
5709                                         }
5710                                         sprintf(p, "%d", lines);
5711                                         fclose(fp);
5712                                 } else {
5713                                         sprintf(p, "File Unreadable");
5714                                 }
5715                         }
5716
5717                         OBJ(words) {
5718                                 FILE *fp = open_file(obj->data.s, &obj->a);
5719
5720                                 if(fp != NULL) {
5721                                         char buf[BUFSZ];
5722                                         int j, words;
5723                                         char inword = FALSE;
5724
5725                                         words = 0;
5726                                         while(fgets(buf, BUFSZ, fp) != NULL){
5727                                                 for(j = 0; buf[j] != 0; j++) {
5728                                                         if(!isspace(buf[j])) {
5729                                                                 if(inword == FALSE) {
5730                                                                         words++;
5731                                                                         inword = TRUE;
5732                                                                 }
5733                                                         } else {
5734                                                                 inword = FALSE;
5735                                                         }
5736                                                 }
5737                                         }
5738                                         sprintf(p, "%d", words);
5739                                         fclose(fp);
5740                                 } else {
5741                                         sprintf(p, "File Unreadable");
5742                                 }
5743                         }
5744 #ifdef TCP_PORT_MONITOR
5745                         OBJ(tcp_portmon) {
5746                                 tcp_portmon_action(p, p_max_size,
5747                                                    &obj->data.tcp_port_monitor);
5748                         }
5749 #endif /* TCP_PORT_MONITOR */
5750
5751 #ifdef HAVE_ICONV
5752                         OBJ(iconv_start) {
5753                                 iconv_converting = 1;
5754                                 iconv_selected = obj->a;
5755                         }
5756                         OBJ(iconv_stop) {
5757                                 iconv_converting = 0;
5758                                 iconv_selected = 0;
5759                         }
5760 #endif /* HAVE_ICONV */
5761
5762                         OBJ(entropy_avail) {
5763                                 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5764                         }
5765                         OBJ(entropy_perc) {
5766                                 percent_print(p, p_max_size,
5767                                               cur->entropy.entropy_avail *
5768                                               100 / cur->entropy.poolsize);
5769                         }
5770                         OBJ(entropy_poolsize) {
5771                                 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5772                         }
5773                         OBJ(entropy_bar) {
5774                                 double entropy_perc;
5775
5776                                 entropy_perc = (double) cur->entropy.entropy_avail /
5777                                         (double) cur->entropy.poolsize;
5778 #ifdef X11
5779                                 if(output_methods & TO_X) {
5780                                         new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5781                                 } else {
5782 #endif /* X11 */
5783                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5784                                         new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5785 #ifdef X11
5786                                 }
5787 #endif /* X11 */
5788                         }
5789 #ifdef IBM
5790                         OBJ(smapi) {
5791                                 char *s;
5792                                 if(obj->data.s) {
5793                                         s = smapi_get_val(obj->data.s);
5794                                         snprintf(p, p_max_size, "%s", s);
5795                                         free(s);
5796                                 }
5797                         }
5798                         OBJ(if_smapi_bat_installed) {
5799                                 int idx;
5800                                 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5801                                         if(!smapi_bat_installed(idx)) {
5802                                                 DO_JUMP;
5803                                         }
5804                                 } else
5805                                         NORM_ERR("argument to if_smapi_bat_installed must be an integer");
5806                         }
5807                         OBJ(smapi_bat_perc) {
5808                                 int idx, val;
5809                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5810                                         val = smapi_bat_installed(idx) ?
5811                                                 smapi_get_bat_int(idx, "remaining_percent") : 0;
5812                                         percent_print(p, p_max_size, val);
5813                                 } else
5814                                         NORM_ERR("argument to smapi_bat_perc must be an integer");
5815                         }
5816                         OBJ(smapi_bat_temp) {
5817                                 int idx, val;
5818                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5819                                         val = smapi_bat_installed(idx) ?
5820                                                 smapi_get_bat_int(idx, "temperature") : 0;
5821                                         /* temperature is in milli degree celsius */
5822                                         temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5823                                 } else
5824                                         NORM_ERR("argument to smapi_bat_temp must be an integer");
5825                         }
5826                         OBJ(smapi_bat_power) {
5827                                 int idx, val;
5828                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5829                                         val = smapi_bat_installed(idx) ?
5830                                                 smapi_get_bat_int(idx, "power_now") : 0;
5831                                         /* power_now is in mW, set to W with one digit precision */
5832                                         snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5833                                 } else
5834                                         NORM_ERR("argument to smapi_bat_power must be an integer");
5835                         }
5836 #ifdef X11
5837                         OBJ(smapi_bat_bar) {
5838                                 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5839                                         new_bar(p, obj->a, obj->b, (int)
5840                                                         (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5841                                 else
5842                                         new_bar(p, obj->a, obj->b, 0);
5843                         }
5844 #endif /* X11 */
5845 #endif /* IBM */
5846                         OBJ(include) {
5847                                 if(obj->sub) {
5848                                         char buf[max_user_text];
5849
5850                                         generate_text_internal(buf, max_user_text, *obj->sub, cur);
5851                                         snprintf(p, p_max_size, "%s", buf);
5852                                 } else {
5853                                         p[0] = 0;
5854                                 }
5855                         }
5856                         OBJ(blink) {
5857                                 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5858                                 char buf[max_user_text];
5859                                 unsigned int j;
5860
5861                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5862                                 snprintf(p, p_max_size, "%s", buf);
5863                                 if(total_updates % 2) {
5864                                         for(j=0; p[j] != 0; j++) {
5865                                                 p[j] = ' ';
5866                                         }
5867                                 }
5868                         }
5869                         OBJ(to_bytes) {
5870                                 char buf[max_user_text];
5871                                 long long bytes;
5872                                 char unit[16];  // 16 because we can also have long names (like mega-bytes)
5873
5874                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5875                                 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5876                                         if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5877                                         else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5878                                         else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5879                                         else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5880                                         else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5881                                 }
5882                                 snprintf(p, p_max_size, "%s", buf);
5883                         }
5884                         OBJ(scroll) {
5885                                 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
5886                                 char *pwithcolors;
5887                                 char buf[max_user_text];
5888                                 generate_text_internal(buf, max_user_text,
5889                                                        *obj->sub, cur);
5890                                 for(j = 0; buf[j] != 0; j++) {
5891                                         switch(buf[j]) {
5892                                         case '\n':      //place all the lines behind each other with LINESEPARATOR between them
5893 #define LINESEPARATOR '|'
5894                                                 buf[j]=LINESEPARATOR;
5895                                                 break;
5896                                         case SPECIAL_CHAR:
5897                                                 colorchanges++;
5898                                                 break;
5899                                         }
5900                                 }
5901                                 //no scrolling necessary if the length of the text to scroll is too short
5902                                 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
5903                                         snprintf(p, p_max_size, "%s", buf);
5904                                         break;
5905                                 }
5906                                 //make sure a colorchange at the front is not part of the string we are going to show
5907                                 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
5908                                         obj->data.scroll.start++;
5909                                 }
5910                                 //place all chars that should be visible in p, including colorchanges
5911                                 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
5912                                         p[j] = *(buf + obj->data.scroll.start + j);
5913                                         if(p[j] == SPECIAL_CHAR) {
5914                                                 visibcolorchanges++;
5915                                         }
5916                                         //if there is still room fill it with spaces
5917                                         if( ! p[j]) break;
5918                                 }
5919                                 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
5920                                         p[j] = ' ';
5921                                 }
5922                                 p[j] = 0;
5923                                 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
5924                                 for(j = 0; j < obj->data.scroll.start; j++) {
5925                                         if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
5926                                 }
5927                                 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
5928                                 for(j = 0; j < frontcolorchanges; j++) {
5929                                         pwithcolors[j] = SPECIAL_CHAR;
5930                                 }
5931                                 pwithcolors[j] = 0;
5932                                 strcat(pwithcolors,p);
5933                                 strend = strlen(pwithcolors);
5934                                 //and place the colorchanges not in front or in the visible part behind the visible part
5935                                 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
5936                                         pwithcolors[strend + j] = SPECIAL_CHAR;
5937                                 }
5938                                 pwithcolors[strend + j] = 0;
5939                                 strcpy(p, pwithcolors);
5940                                 free(pwithcolors);
5941                                 //scroll
5942                                 obj->data.scroll.start += obj->data.scroll.step;
5943                                 if(buf[obj->data.scroll.start] == 0){
5944                                          obj->data.scroll.start = 0;
5945                                 }
5946 #ifdef X11
5947                                 //reset color when scroll is finished
5948                                 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
5949 #endif
5950                         }
5951                         OBJ(combine) {
5952                                 char buf[2][max_user_text];
5953                                 int i, j;
5954                                 long longest=0;
5955                                 int nextstart;
5956                                 int nr_rows[2];
5957                                 struct llrows {
5958                                         char* row;
5959                                         struct llrows* next;
5960                                 };
5961                                 struct llrows *ll_rows[2], *current[2];
5962                                 struct text_object * objsub = obj->sub;
5963
5964                                 p[0]=0;
5965                                 for(i=0; i<2; i++) {
5966                                         nr_rows[i] = 1;
5967                                         nextstart = 0;
5968                                         ll_rows[i] = malloc(sizeof(struct llrows));
5969                                         current[i] = ll_rows[i];
5970                                         for(j=0; j<i; j++) objsub = objsub->sub;
5971                                         generate_text_internal(buf[i], max_user_text, *objsub, cur);
5972                                         for(j=0; buf[i][j] != 0; j++) {
5973                                                 if(buf[i][j] == '\t') buf[i][j] = ' ';
5974                                                 if(buf[i][j] == '\n') {
5975                                                         buf[i][j] = 0;
5976                                                         current[i]->row = strdup(buf[i]+nextstart);
5977                                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5978                                                         current[i]->next = malloc(sizeof(struct llrows));
5979                                                         current[i] = current[i]->next;
5980                                                         nextstart = j + 1;
5981                                                         nr_rows[i]++;
5982                                                 }
5983                                         }
5984                                         current[i]->row = strdup(buf[i]+nextstart);
5985                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5986                                         current[i]->next = NULL;
5987                                         current[i] = ll_rows[i];
5988                                 }
5989                                 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5990                                         if(current[0]) {
5991                                                 strcat(p, current[0]->row);
5992                                                 i=strlen(current[0]->row);
5993                                         }else i = 0;
5994                                         while(i < longest) {
5995                                                 strcat(p, " ");
5996                                                 i++;
5997                                         }
5998                                         if(current[1]) {
5999                                                 strcat(p, obj->data.combine.seperation);
6000                                                 strcat(p, current[1]->row);
6001                                         }
6002                                         strcat(p, "\n");
6003                                         #ifdef HAVE_OPENMP
6004                                         #pragma omp parallel for schedule(dynamic,10)
6005                                         #endif /* HAVE_OPENMP */
6006                                         for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
6007                                 }
6008                                 #ifdef HAVE_OPENMP
6009                                 #pragma omp parallel for schedule(dynamic,10)
6010                                 #endif /* HAVE_OPENMP */
6011                                 for(i=0; i<2; i++) {
6012                                         while(ll_rows[i] != NULL) {
6013                                                 current[i]=ll_rows[i];
6014                                                 free(current[i]->row);
6015                                                 ll_rows[i]=current[i]->next;
6016                                                 free(current[i]);
6017                                         }
6018                                 }
6019                         }
6020 #ifdef NVIDIA
6021                         OBJ(nvidia) {
6022                                 int value = get_nvidia_value(obj->data.nvidia.type, display);
6023                                 if(value == -1)
6024                                         snprintf(p, p_max_size, "N/A");
6025                                 else if (obj->data.nvidia.type == NV_TEMP)
6026                                         temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
6027                                 else if (obj->data.nvidia.print_as_float &&
6028                                                 value > 0 && value < 100)
6029                                         snprintf(p, p_max_size, "%.1f", (float)value);
6030                                 else
6031                                         snprintf(p, p_max_size, "%d", value);
6032                         }
6033 #endif /* NVIDIA */
6034 #ifdef APCUPSD
6035                         OBJ(apcupsd) {
6036                                 /* This is just a meta-object to set host:port */
6037                         }
6038                         OBJ(apcupsd_name) {
6039                                 snprintf(p, p_max_size, "%s",
6040                                                  cur->apcupsd.items[APCUPSD_NAME]);
6041                         }
6042                         OBJ(apcupsd_model) {
6043                                 snprintf(p, p_max_size, "%s",
6044                                                  cur->apcupsd.items[APCUPSD_MODEL]);
6045                         }
6046                         OBJ(apcupsd_upsmode) {
6047                                 snprintf(p, p_max_size, "%s",
6048                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
6049                         }
6050                         OBJ(apcupsd_cable) {
6051                                 snprintf(p, p_max_size, "%s",
6052                                                  cur->apcupsd.items[APCUPSD_CABLE]);
6053                         }
6054                         OBJ(apcupsd_status) {
6055                                 snprintf(p, p_max_size, "%s",
6056                                                  cur->apcupsd.items[APCUPSD_STATUS]);
6057                         }
6058                         OBJ(apcupsd_linev) {
6059                                 snprintf(p, p_max_size, "%s",
6060                                                  cur->apcupsd.items[APCUPSD_LINEV]);
6061                         }
6062                         OBJ(apcupsd_load) {
6063                                 snprintf(p, p_max_size, "%s",
6064                                                  cur->apcupsd.items[APCUPSD_LOAD]);
6065                         }
6066                         OBJ(apcupsd_loadbar) {
6067                                 double progress;
6068 #ifdef X11
6069                                 if(output_methods & TO_X) {
6070                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
6071                                         new_bar(p, obj->a, obj->b, (int) progress);
6072                                 } else {
6073 #endif /* X11 */
6074                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
6075                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
6076                                         new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
6077 #ifdef X11
6078                                 }
6079 #endif /* X11 */
6080                         }
6081 #ifdef X11
6082                         OBJ(apcupsd_loadgraph) {
6083                                 double progress;
6084                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
6085                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
6086                                                   (int)progress, 100, 1, obj->char_a, obj->char_b);
6087                         }
6088                         OBJ(apcupsd_loadgauge) {
6089                                 double progress;
6090                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
6091                                 new_gauge(p, obj->a, obj->b,
6092                                                   (int)progress);
6093                         }
6094 #endif /* X11 */
6095                         OBJ(apcupsd_charge) {
6096                                 snprintf(p, p_max_size, "%s",
6097                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
6098                         }
6099                         OBJ(apcupsd_timeleft) {
6100                                 snprintf(p, p_max_size, "%s",
6101                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
6102                         }
6103                         OBJ(apcupsd_temp) {
6104                                 snprintf(p, p_max_size, "%s",
6105                                                  cur->apcupsd.items[APCUPSD_TEMP]);
6106                         }
6107                         OBJ(apcupsd_lastxfer) {
6108                                 snprintf(p, p_max_size, "%s",
6109                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
6110                         }
6111 #endif /* APCUPSD */
6112                         break;
6113                 }
6114 #undef DO_JUMP
6115
6116
6117                 {
6118                         unsigned int a = strlen(p);
6119
6120 #ifdef HAVE_ICONV
6121                         if (a > 0 && iconv_converting && iconv_selected > 0
6122                                         && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6123                                 int bytes;
6124                                 size_t dummy1, dummy2;
6125 #ifdef __FreeBSD__
6126                                 const char *ptr = buff_in;
6127 #else
6128                                 char *ptr = buff_in;
6129 #endif
6130                                 char *outptr = p;
6131
6132                                 dummy1 = dummy2 = a;
6133
6134                                 strncpy(buff_in, p, p_max_size);
6135
6136                                 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6137                                 while (dummy1 > 0) {
6138                                         bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6139                                                         &outptr, &dummy2);
6140                                         if (bytes == -1) {
6141                                                 NORM_ERR("Iconv codeset conversion failed");
6142                                                 break;
6143                                         }
6144                                 }
6145
6146                                 /* It is nessecary when we are converting from multibyte to
6147                                  * singlebyte codepage */
6148                                 a = outptr - p;
6149                         }
6150 #endif /* HAVE_ICONV */
6151                         if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi) {
6152                                 substitute_newlines(p, a - 2);
6153                         }
6154                         p += a;
6155                         p_max_size -= a;
6156                 }
6157                 obj = obj->next;
6158         }
6159 #ifdef X11
6160         /* load any new fonts we may have had */
6161         if (need_to_load_fonts) {
6162                 load_fonts();
6163         }
6164 #endif /* X11 */
6165 }
6166
6167 void evaluate(const char *text, char *buffer)
6168 {
6169         struct information *tmp_info;
6170         struct text_object subroot;
6171
6172         tmp_info = malloc(sizeof(struct information));
6173         memcpy(tmp_info, &info, sizeof(struct information));
6174         parse_conky_vars(&subroot, text, buffer, tmp_info);
6175         DBGP("evaluated '%s' to '%s'", text, buffer);
6176
6177         free_text_objects(&subroot, 1);
6178         free(tmp_info);
6179 }
6180
6181 double current_update_time, next_update_time, last_update_time;
6182
6183 static void generate_text(void)
6184 {
6185         struct information *cur = &info;
6186         char *p;
6187
6188         special_count = 0;
6189
6190         /* update info */
6191
6192         current_update_time = get_time();
6193
6194         update_stuff();
6195
6196         /* add things to the buffer */
6197
6198         /* generate text */
6199
6200         p = text_buffer;
6201
6202         generate_text_internal(p, max_user_text, global_root_object, cur);
6203
6204         if (stuff_in_uppercase) {
6205                 char *tmp_p;
6206
6207                 tmp_p = text_buffer;
6208                 while (*tmp_p) {
6209                         *tmp_p = toupper(*tmp_p);
6210                         tmp_p++;
6211                 }
6212         }
6213
6214         next_update_time += update_interval;
6215         if (next_update_time < get_time()) {
6216                 next_update_time = get_time() + update_interval;
6217         } else if (next_update_time > get_time() + update_interval) {
6218                 next_update_time = get_time() + update_interval;
6219         }
6220         last_update_time = current_update_time;
6221         total_updates++;
6222 }
6223
6224 void set_update_interval(double interval)
6225 {
6226         update_interval = interval;
6227         update_interval_old = interval;
6228 }
6229
6230 static inline int get_string_width(const char *s)
6231 {
6232 #ifdef X11
6233         if (output_methods & TO_X) {
6234                 return *s ? calc_text_width(s, strlen(s)) : 0;
6235         }
6236 #endif /* X11 */
6237         return strlen(s);
6238 }
6239
6240 #ifdef X11
6241 static int get_string_width_special(char *s, int special_index)
6242 {
6243         char *p, *final;
6244         int idx = 1;
6245         int width = 0;
6246         long i;
6247
6248         if ((output_methods & TO_X) == 0) {
6249                 return (s) ? strlen(s) : 0;
6250         }
6251
6252         if (!s) {
6253                 return 0;
6254         }
6255
6256         p = strndup(s, text_buffer_size);
6257         final = p;
6258
6259         while (*p) {
6260                 if (*p == SPECIAL_CHAR) {
6261                         /* shift everything over by 1 so that the special char
6262                          * doesn't mess up the size calculation */
6263                         for (i = 0; i < (long)strlen(p); i++) {
6264                                 *(p + i) = *(p + i + 1);
6265                         }
6266                         if (specials[special_index + idx].type == GRAPH
6267                                         || specials[special_index + idx].type == GAUGE
6268                                         || specials[special_index + idx].type == BAR) {
6269                                 width += specials[special_index + idx].width;
6270                         }
6271                         idx++;
6272                 } else if (*p == SECRIT_MULTILINE_CHAR) {
6273                         *p = 0;
6274                         break;
6275                 } else {
6276                         p++;
6277                 }
6278         }
6279         if (strlen(final) > 1) {
6280                 width += calc_text_width(final, strlen(final));
6281         }
6282         free(final);
6283         return width;
6284 }
6285
6286 static int text_size_updater(char *s, int special_index);
6287
6288 int last_font_height;
6289 static void update_text_area(void)
6290 {
6291         int x = 0, y = 0;
6292
6293         if ((output_methods & TO_X) == 0)
6294                 return;
6295         /* update text size if it isn't fixed */
6296 #ifdef OWN_WINDOW
6297         if (!fixed_size)
6298 #endif
6299         {
6300                 text_width = minimum_width;
6301                 text_height = 0;
6302                 last_font_height = font_height();
6303                 for_each_line(text_buffer, text_size_updater);
6304                 text_width += 1;
6305                 if (text_height < minimum_height) {
6306                         text_height = minimum_height;
6307                 }
6308                 if (text_width > maximum_width && maximum_width > 0) {
6309                         text_width = maximum_width;
6310                 }
6311         }
6312
6313         /* get text position on workarea */
6314         switch (text_alignment) {
6315                 case TOP_LEFT:
6316                         x = gap_x;
6317                         y = gap_y;
6318                         break;
6319
6320                 case TOP_RIGHT:
6321                         x = workarea[2] - text_width - gap_x;
6322                         y = gap_y;
6323                         break;
6324
6325                 case TOP_MIDDLE:
6326                         x = workarea[2] / 2 - text_width / 2 - gap_x;
6327                         y = gap_y;
6328                         break;
6329
6330                 default:
6331                 case BOTTOM_LEFT:
6332                         x = gap_x;
6333                         y = workarea[3] - text_height - gap_y;
6334                         break;
6335
6336                 case BOTTOM_RIGHT:
6337                         x = workarea[2] - text_width - gap_x;
6338                         y = workarea[3] - text_height - gap_y;
6339                         break;
6340
6341                 case BOTTOM_MIDDLE:
6342                         x = workarea[2] / 2 - text_width / 2 - gap_x;
6343                         y = workarea[3] - text_height - gap_y;
6344                         break;
6345
6346                 case MIDDLE_LEFT:
6347                         x = gap_x;
6348                         y = workarea[3] / 2 - text_height / 2 - gap_y;
6349                         break;
6350
6351                 case MIDDLE_RIGHT:
6352                         x = workarea[2] - text_width - gap_x;
6353                         y = workarea[3] / 2 - text_height / 2 - gap_y;
6354                         break;
6355
6356 #ifdef OWN_WINDOW
6357                 case NONE:      // Let the WM manage the window
6358                         x = window.x;
6359                         y = window.y;
6360
6361                         fixed_pos = 1;
6362                         fixed_size = 1;
6363                         break;
6364 #endif
6365         }
6366 #ifdef OWN_WINDOW
6367
6368         if (own_window && !fixed_pos) {
6369                 x += workarea[0];
6370                 y += workarea[1];
6371                 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6372                 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6373                 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6374                 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6375         } else
6376 #endif
6377         {
6378                 /* If window size doesn't match to workarea's size,
6379                  * then window probably includes panels (gnome).
6380                  * Blah, doesn't work on KDE. */
6381                 if (workarea[2] != window.width || workarea[3] != window.height) {
6382                         y += workarea[1];
6383                         x += workarea[0];
6384                 }
6385
6386                 text_start_x = x;
6387                 text_start_y = y;
6388         }
6389 #ifdef HAVE_LUA
6390         /* update lua window globals */
6391         llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6392 #endif /* HAVE_LUA */
6393 }
6394
6395 /* drawing stuff */
6396
6397 static int cur_x, cur_y;        /* current x and y for drawing */
6398 #endif
6399 //draw_mode also without X11 because we only need to print to stdout with FG
6400 static int draw_mode;           /* FG, BG or OUTLINE */
6401 #ifdef X11
6402 static long current_color;
6403
6404 static int text_size_updater(char *s, int special_index)
6405 {
6406         int w = 0;
6407         char *p;
6408
6409         if ((output_methods & TO_X) == 0)
6410                 return 0;
6411         /* get string widths and skip specials */
6412         p = s;
6413         while (*p) {
6414                 if (*p == SPECIAL_CHAR) {
6415                         *p = '\0';
6416                         w += get_string_width(s);
6417                         *p = SPECIAL_CHAR;
6418
6419                         if (specials[special_index].type == BAR
6420                                         || specials[special_index].type == GAUGE
6421                                         || specials[special_index].type == GRAPH) {
6422                                 w += specials[special_index].width;
6423                                 if (specials[special_index].height > last_font_height) {
6424                                         last_font_height = specials[special_index].height;
6425                                         last_font_height += font_height();
6426                                 }
6427                         } else if (specials[special_index].type == OFFSET) {
6428                                 if (specials[special_index].arg > 0) {
6429                                         w += specials[special_index].arg;
6430                                 }
6431                         } else if (specials[special_index].type == VOFFSET) {
6432                                 last_font_height += specials[special_index].arg;
6433                         } else if (specials[special_index].type == GOTO) {
6434                                 if (specials[special_index].arg > cur_x) {
6435                                         w = (int) specials[special_index].arg;
6436                                 }
6437                         } else if (specials[special_index].type == TAB) {
6438                                 int start = specials[special_index].arg;
6439                                 int step = specials[special_index].width;
6440
6441                                 if (!step || step < 0) {
6442                                         step = 10;
6443                                 }
6444                                 w += step - (cur_x - text_start_x - start) % step;
6445                         } else if (specials[special_index].type == FONT) {
6446                                 selected_font = specials[special_index].font_added;
6447                                 if (font_height() > last_font_height) {
6448                                         last_font_height = font_height();
6449                                 }
6450                         }
6451
6452                         special_index++;
6453                         s = p + 1;
6454                 } else if (*p == SECRIT_MULTILINE_CHAR) {
6455                         int lw;
6456                         *p = '\0';
6457                         lw = get_string_width(s);
6458                         *p = SECRIT_MULTILINE_CHAR;
6459                         s = p + 1;
6460                         w = lw > w ? lw : w;
6461                         text_height += last_font_height;
6462                 }
6463                 p++;
6464         }
6465         w += get_string_width(s);
6466         if (w > text_width) {
6467                 text_width = w;
6468         }
6469         if (text_width > maximum_width && maximum_width) {
6470                 text_width = maximum_width;
6471         }
6472
6473         text_height += last_font_height;
6474         last_font_height = font_height();
6475         return special_index;
6476 }
6477
6478 static inline void set_foreground_color(long c)
6479 {
6480         if ((output_methods & TO_X) == 0)
6481                 return;
6482         current_color = c;
6483         XSetForeground(display, window.gc, c);
6484 }
6485 #endif /* X11 */
6486
6487 static void draw_string(const char *s)
6488 {
6489         int i, i2, pos, width_of_s;
6490         int max = 0;
6491         int added;
6492         char *s_with_newlines;
6493
6494         if (s[0] == '\0') {
6495                 return;
6496         }
6497
6498         width_of_s = get_string_width(s);
6499         s_with_newlines = strdup(s);
6500         for(i = 0; i < (int) strlen(s_with_newlines); i++) {
6501                 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
6502                         s_with_newlines[i] = '\n';
6503                 }
6504         }
6505         if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6506                 printf("%s\n", s_with_newlines);
6507                 if (extra_newline) fputc('\n', stdout);
6508                 fflush(stdout); /* output immediately, don't buffer */
6509         }
6510         if ((output_methods & TO_STDERR) && draw_mode == FG) {
6511                 fprintf(stderr, "%s\n", s_with_newlines);
6512                 fflush(stderr); /* output immediately, don't buffer */
6513         }
6514         if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6515                 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
6516         }
6517         if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6518                 fprintf(append_fpointer, "%s\n", s_with_newlines);
6519         }
6520 #ifdef NCURSES
6521         if ((output_methods & TO_NCURSES) && draw_mode == FG) {
6522                 printw("%s\n", s_with_newlines);
6523         }
6524 #endif
6525         free(s_with_newlines);
6526         memset(tmpstring1, 0, text_buffer_size);
6527         memset(tmpstring2, 0, text_buffer_size);
6528         strncpy(tmpstring1, s, text_buffer_size - 1);
6529         pos = 0;
6530         added = 0;
6531
6532 #ifdef X11
6533         if (output_methods & TO_X) {
6534                 max = ((text_width - width_of_s) / get_string_width(" "));
6535         }
6536 #endif /* X11 */
6537         /* This code looks for tabs in the text and coverts them to spaces.
6538          * The trick is getting the correct number of spaces, and not going
6539          * over the window's size without forcing the window larger. */
6540         for (i = 0; i < (int) text_buffer_size; i++) {
6541                 if (tmpstring1[i] == '\t') {
6542                         i2 = 0;
6543                         for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6544                                 /* guard against overrun */
6545                                 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6546                                 added++;
6547                         }
6548                         pos += i2;
6549                 } else {
6550                         /* guard against overrun */
6551                         tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6552                         pos++;
6553                 }
6554         }
6555 #ifdef X11
6556         if (output_methods & TO_X) {
6557                 if (text_width == maximum_width) {
6558                         /* this means the text is probably pushing the limit,
6559                          * so we'll chop it */
6560                         while (cur_x + get_string_width(tmpstring2) - text_start_x
6561                                         > maximum_width && strlen(tmpstring2) > 0) {
6562                                 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6563                         }
6564                 }
6565         }
6566 #endif /* X11 */
6567         s = tmpstring2;
6568 #ifdef X11
6569         if (output_methods & TO_X) {
6570 #ifdef XFT
6571                 if (use_xft) {
6572                         XColor c;
6573                         XftColor c2;
6574
6575                         c.pixel = current_color;
6576                         XQueryColor(display, DefaultColormap(display, screen), &c);
6577
6578                         c2.pixel = c.pixel;
6579                         c2.color.red = c.red;
6580                         c2.color.green = c.green;
6581                         c2.color.blue = c.blue;
6582                         c2.color.alpha = fonts[selected_font].font_alpha;
6583                         if (utf8_mode) {
6584                                 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6585                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6586                         } else {
6587                                 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6588                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6589                         }
6590                 } else
6591 #endif
6592                 {
6593                         XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6594                                 strlen(s));
6595                 }
6596                 cur_x += width_of_s;
6597         }
6598 #endif /* X11 */
6599         memcpy(tmpstring1, s, text_buffer_size);
6600 }
6601
6602 #ifdef X11
6603 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
6604 {
6605         int font_h = font_height();
6606         int cur_y_add = 0;
6607         char *recurse = 0;
6608         char *p = s;
6609         int last_special_needed = -1;
6610         int orig_special_index = special_index;
6611
6612         cur_x = text_start_x;
6613         cur_y += font_ascent();
6614
6615         while (*p) {
6616                 if (*p == SECRIT_MULTILINE_CHAR) {
6617                         /* special newline marker for multiline objects */
6618                         recurse = p + 1;
6619                         *p = '\0';
6620                         break;
6621                 }
6622                 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
6623                         int w = 0;
6624
6625                         /* draw string before special, unless we're dealing multiline
6626                          * specials */
6627                         if (last_special_applied > -1) {
6628                                 special_index = last_special_applied;
6629                         } else {
6630                                 *p = '\0';
6631                                 draw_string(s);
6632                                 *p = SPECIAL_CHAR;
6633                                 s = p + 1;
6634                         }
6635                         /* draw special */
6636                         switch (specials[special_index].type) {
6637                                 case HORIZONTAL_LINE:
6638                                 {
6639                                         int h = specials[special_index].height;
6640                                         int mid = font_ascent() / 2;
6641
6642                                         w = text_start_x + text_width - cur_x;
6643
6644                                         XSetLineAttributes(display, window.gc, h, LineSolid,
6645                                                 CapButt, JoinMiter);
6646                                         XDrawLine(display, window.drawable, window.gc, cur_x,
6647                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6648                                         break;
6649                                 }
6650
6651                                 case STIPPLED_HR:
6652                                 {
6653                                         int h = specials[special_index].height;
6654                                         int tmp_s = specials[special_index].arg;
6655                                         int mid = font_ascent() / 2;
6656                                         char ss[2] = { tmp_s, tmp_s };
6657
6658                                         w = text_start_x + text_width - cur_x - 1;
6659                                         XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6660                                                 CapButt, JoinMiter);
6661                                         XSetDashes(display, window.gc, 0, ss, 2);
6662                                         XDrawLine(display, window.drawable, window.gc, cur_x,
6663                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6664                                         break;
6665                                 }
6666
6667                                 case BAR:
6668                                 {
6669                                         int h, bar_usage, by;
6670                                         if (cur_x - text_start_x > maximum_width
6671                                                         && maximum_width > 0) {
6672                                                 break;
6673                                         }
6674                                         h = specials[special_index].height;
6675                                         bar_usage = specials[special_index].arg;
6676                                         by = cur_y - (font_ascent() / 2) - 1;
6677
6678                                         if (h < font_h) {
6679                                                 by -= h / 2 - 1;
6680                                         }
6681                                         w = specials[special_index].width;
6682                                         if (w == 0) {
6683                                                 w = text_start_x + text_width - cur_x - 1;
6684                                         }
6685                                         if (w < 0) {
6686                                                 w = 0;
6687                                         }
6688
6689                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6690                                                 CapButt, JoinMiter);
6691
6692                                         XDrawRectangle(display, window.drawable, window.gc, cur_x,
6693                                                 by, w, h);
6694                                         XFillRectangle(display, window.drawable, window.gc, cur_x,
6695                                                 by, w * bar_usage / 255, h);
6696                                         if (h > cur_y_add
6697                                                         && h > font_h) {
6698                                                 cur_y_add = h;
6699                                         }
6700                                         break;
6701                                 }
6702
6703                                 case GAUGE: /* new GAUGE  */
6704                                 {
6705                                         int h, by = 0;
6706                                         unsigned long last_colour = current_color;
6707 #ifdef MATH
6708                                         float angle, px, py;
6709                                         int usage;
6710 #endif /* MATH */
6711
6712                                         if (cur_x - text_start_x > maximum_width
6713                                                         && maximum_width > 0) {
6714                                                 break;
6715                                         }
6716
6717                                         h = specials[special_index].height;
6718                                         by = cur_y - (font_ascent() / 2) - 1;
6719
6720                                         if (h < font_h) {
6721                                                 by -= h / 2 - 1;
6722                                         }
6723                                         w = specials[special_index].width;
6724                                         if (w == 0) {
6725                                                 w = text_start_x + text_width - cur_x - 1;
6726                                         }
6727                                         if (w < 0) {
6728                                                 w = 0;
6729                                         }
6730
6731                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6732                                                         CapButt, JoinMiter);
6733
6734                                         XDrawArc(display, window.drawable, window.gc,
6735                                                         cur_x, by, w, h * 2, 0, 180*64);
6736
6737 #ifdef MATH
6738                                         usage = specials[special_index].arg;
6739                                         angle = (M_PI)*(float)(usage)/255.;
6740                                         px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6741                                         py = (float)(by+(h))-(float)(h)*sin(angle);
6742
6743                                         XDrawLine(display, window.drawable, window.gc,
6744                                                         cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6745 #endif /* MATH */
6746
6747                                         if (h > cur_y_add
6748                                                         && h > font_h) {
6749                                                 cur_y_add = h;
6750                                         }
6751
6752                                         set_foreground_color(last_colour);
6753
6754                                         break;
6755
6756                                 }
6757
6758                                 case GRAPH:
6759                                 {
6760                                         int h, by, i = 0, j = 0;
6761                                         int colour_idx = 0;
6762                                         unsigned long last_colour = current_color;
6763                                         unsigned long *tmpcolour = 0;
6764                                         if (cur_x - text_start_x > maximum_width
6765                                                         && maximum_width > 0) {
6766                                                 break;
6767                                         }
6768                                         h = specials[special_index].height;
6769                                         by = cur_y - (font_ascent() / 2) - 1;
6770
6771                                         if (h < font_h) {
6772                                                 by -= h / 2 - 1;
6773                                         }
6774                                         w = specials[special_index].width;
6775                                         if (w == 0) {
6776                                                 w = text_start_x + text_width - cur_x - 1;
6777                                         }
6778                                         if (w < 0) {
6779                                                 w = 0;
6780                                         }
6781                                         if (draw_graph_borders) {
6782                                                 XSetLineAttributes(display, window.gc, 1, LineSolid,
6783                                                         CapButt, JoinMiter);
6784                                                 XDrawRectangle(display, window.drawable, window.gc,
6785                                                         cur_x, by, w, h);
6786                                         }
6787                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6788                                                 CapButt, JoinMiter);
6789
6790                                         if (specials[special_index].last_colour != 0
6791                                                         || specials[special_index].first_colour != 0) {
6792                                                 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6793                                         }
6794                                         colour_idx = 0;
6795                                         for (i = w - 2; i > -1; i--) {
6796                                                 if (specials[special_index].last_colour != 0
6797                                                                 || specials[special_index].first_colour != 0) {
6798                                                         if (specials[special_index].tempgrad) {
6799 #ifdef DEBUG_lol
6800                                                                 assert(
6801                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
6802                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
6803                                                                                 < w - 1
6804                                                                           );
6805                                                                 assert(
6806                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
6807                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
6808                                                                                 > -1
6809                                                                           );
6810                                                                 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6811                                                                         assert(
6812                                                                                         (int)((float)(w - 2) - specials[special_index].graph[j] *
6813                                                                                                 (w - 2) / (float)specials[special_index].graph_scale)
6814                                                                                         == 0
6815                                                                                   );
6816                                                                 }
6817 #endif /* DEBUG_lol */
6818                                                                 XSetForeground(display, window.gc, tmpcolour[
6819                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
6820                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
6821                                                                                 ]);
6822                                                         } else {
6823                                                                 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6824                                                         }
6825                                                 }
6826                                                 /* this is mugfugly, but it works */
6827                                                 XDrawLine(display, window.drawable, window.gc,
6828                                                                 cur_x + i + 1, by + h, cur_x + i + 1,
6829                                                                 round_to_int((double)by + h - specials[special_index].graph[j] *
6830                                                                         (h - 1) / specials[special_index].graph_scale));
6831                                                 if ((w - i) / ((float) (w - 2) /
6832                                                                         (specials[special_index].graph_width)) > j
6833                                                                 && j < MAX_GRAPH_DEPTH - 3) {
6834                                                         j++;
6835                                                 }
6836                                         }
6837                                         if (tmpcolour) free(tmpcolour);
6838                                         if (h > cur_y_add
6839                                                         && h > font_h) {
6840                                                 cur_y_add = h;
6841                                         }
6842                                         /* if (draw_mode == BG) {
6843                                                 set_foreground_color(default_bg_color);
6844                                         } else if (draw_mode == OUTLINE) {
6845                                                 set_foreground_color(default_out_color);
6846                                         } else {
6847                                                 set_foreground_color(default_fg_color);
6848                                         } */
6849                                         if (show_graph_range) {
6850                                                 int tmp_x = cur_x;
6851                                                 int tmp_y = cur_y;
6852                                                 unsigned short int seconds = update_interval * w;
6853                                                 char *tmp_day_str;
6854                                                 char *tmp_hour_str;
6855                                                 char *tmp_min_str;
6856                                                 char *tmp_sec_str;
6857                                                 char *tmp_str;
6858                                                 unsigned short int timeunits;
6859                                                 if (seconds != 0) {
6860                                                         timeunits = seconds / 86400; seconds %= 86400;
6861                                                         if (timeunits > 0) {
6862                                                                 asprintf(&tmp_day_str, "%dd", timeunits);
6863                                                         } else {
6864                                                                 tmp_day_str = strdup("");
6865                                                         }
6866                                                         timeunits = seconds / 3600; seconds %= 3600;
6867                                                         if (timeunits > 0) {
6868                                                                 asprintf(&tmp_hour_str, "%dh", timeunits);
6869                                                         } else {
6870                                                                 tmp_hour_str = strdup("");
6871                                                         }
6872                                                         timeunits = seconds / 60; seconds %= 60;
6873                                                         if (timeunits > 0) {
6874                                                                 asprintf(&tmp_min_str, "%dm", timeunits);
6875                                                         } else {
6876                                                                 tmp_min_str = strdup("");
6877                                                         }
6878                                                         if (seconds > 0) {
6879                                                                 asprintf(&tmp_sec_str, "%ds", seconds);
6880                                                         } else {
6881                                                                 tmp_sec_str = strdup("");
6882                                                         }
6883                                                         asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6884                                                         free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6885                                                 } else {
6886                                                         asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6887                                                 }
6888                                                 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6889                                                 cur_y += font_h / 2;
6890                                                 draw_string(tmp_str);
6891                                                 free(tmp_str);
6892                                                 cur_x = tmp_x;
6893                                                 cur_y = tmp_y;
6894                                         }
6895 #ifdef MATH
6896                                         if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6897                                                 int tmp_x = cur_x;
6898                                                 int tmp_y = cur_y;
6899                                                 char *tmp_str;
6900                                                 cur_x += font_ascent() / 2;
6901                                                 cur_y += font_h / 2;
6902                                                 tmp_str = (char *)
6903                                                         calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6904                                                                         sizeof(char));
6905                                                 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6906                                                 draw_string(tmp_str);
6907                                                 free(tmp_str);
6908                                                 cur_x = tmp_x;
6909                                                 cur_y = tmp_y;
6910                                         }
6911 #endif
6912                                         set_foreground_color(last_colour);
6913                                         break;
6914                                 }
6915
6916                                 case FONT:
6917                                 {
6918                                         int old = font_ascent();
6919
6920                                         cur_y -= font_ascent();
6921                                         selected_font = specials[special_index].font_added;
6922                                         set_font();
6923                                         if (cur_y + font_ascent() < cur_y + old) {
6924                                                 cur_y += old;
6925                                         } else {
6926                                                 cur_y += font_ascent();
6927                                         }
6928                                         font_h = font_height();
6929                                         break;
6930                                 }
6931                                 case FG:
6932                                         if (draw_mode == FG) {
6933                                                 set_foreground_color(specials[special_index].arg);
6934                                         }
6935                                         break;
6936
6937                                 case BG:
6938                                         if (draw_mode == BG) {
6939                                                 set_foreground_color(specials[special_index].arg);
6940                                         }
6941                                         break;
6942
6943                                 case OUTLINE:
6944                                         if (draw_mode == OUTLINE) {
6945                                                 set_foreground_color(specials[special_index].arg);
6946                                         }
6947                                         break;
6948
6949                                 case OFFSET:
6950                                         w += specials[special_index].arg;
6951                                         last_special_needed = special_index;
6952                                         break;
6953
6954                                 case VOFFSET:
6955                                         cur_y += specials[special_index].arg;
6956                                         break;
6957
6958                                 case GOTO:
6959                                         if (specials[special_index].arg >= 0) {
6960                                                 cur_x = (int) specials[special_index].arg;
6961                                         }
6962                                         last_special_needed = special_index;
6963                                         break;
6964
6965                                 case TAB:
6966                                 {
6967                                         int start = specials[special_index].arg;
6968                                         int step = specials[special_index].width;
6969
6970                                         if (!step || step < 0) {
6971                                                 step = 10;
6972                                         }
6973                                         w = step - (cur_x - text_start_x - start) % step;
6974                                         last_special_needed = special_index;
6975                                         break;
6976                                 }
6977
6978                                 case ALIGNR:
6979                                 {
6980                                         /* TODO: add back in "+ window.border_inner_margin" to the end of
6981                                          * this line? */
6982                                         int pos_x = text_start_x + text_width -
6983                                                 get_string_width_special(s, special_index);
6984
6985                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6986                                                 "get_string_width(p) %i gap_x %i "
6987                                                 "specials[special_index].arg %i window.border_inner_margin %i "
6988                                                 "window.border_width %i\n", pos_x, text_start_x, text_width,
6989                                                 cur_x, get_string_width_special(s), gap_x,
6990                                                 specials[special_index].arg, window.border_inner_margin,
6991                                                 window.border_width); */
6992                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6993                                                 cur_x = pos_x - specials[special_index].arg;
6994                                         }
6995                                         last_special_needed = special_index;
6996                                         break;
6997                                 }
6998
6999                                 case ALIGNC:
7000                                 {
7001                                         int pos_x = (text_width) / 2 - get_string_width_special(s,
7002                                                         special_index) / 2 - (cur_x -
7003                                                                 text_start_x);
7004                                         /* int pos_x = text_start_x + text_width / 2 -
7005                                                 get_string_width_special(s) / 2; */
7006
7007                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7008                                                 "get_string_width(p) %i gap_x %i "
7009                                                 "specials[special_index].arg %i\n", pos_x, text_start_x,
7010                                                 text_width, cur_x, get_string_width(s), gap_x,
7011                                                 specials[special_index].arg); */
7012                                         if (pos_x > specials[special_index].arg) {
7013                                                 w = pos_x - specials[special_index].arg;
7014                                         }
7015                                         last_special_needed = special_index;
7016                                         break;
7017                                 }
7018                         }
7019
7020                         cur_x += w;
7021
7022                         if (special_index != last_special_applied) {
7023                                 special_index++;
7024                         } else {
7025                                 special_index = orig_special_index;
7026                                 last_special_applied = -1;
7027                         }
7028                 }
7029                 p++;
7030         }
7031
7032         cur_y += cur_y_add;
7033         draw_string(s);
7034         cur_y += font_descent();
7035         if (recurse && *recurse) {
7036                 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
7037                 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
7038         }
7039         return special_index;
7040 }
7041 #endif /* X11 */
7042
7043 static int draw_line(char *s, int special_index)
7044 {
7045 #ifdef X11
7046         if ((output_methods & TO_X) == 0) {
7047 #endif /* X11 */
7048                 draw_string(s);
7049                 //'special_index - special_index' instead of 0 otherwise gcc complains about not using special_index when build without X11
7050                 return special_index - special_index;
7051 #ifdef X11
7052         }
7053
7054         /* find specials and draw stuff */
7055         return draw_each_line_inner(s, special_index, -1);
7056 #endif /* X11 */
7057 }
7058
7059 static void draw_text(void)
7060 {
7061 #ifdef X11
7062 #ifdef HAVE_LUA
7063         llua_draw_pre_hook();
7064 #endif /* HAVE_LUA */
7065         if (output_methods & TO_X) {
7066                 cur_y = text_start_y;
7067
7068                 /* draw borders */
7069                 if (draw_borders && window.border_width > 0) {
7070                         if (stippled_borders) {
7071                                 char ss[2] = { stippled_borders, stippled_borders };
7072                                 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
7073                                         CapButt, JoinMiter);
7074                                 XSetDashes(display, window.gc, 0, ss, 2);
7075                         } else {
7076                                 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
7077                                         CapButt, JoinMiter);
7078                         }
7079
7080                         XDrawRectangle(display, window.drawable, window.gc,
7081                                 text_start_x - window.border_inner_margin - window.border_width,
7082                                 text_start_y - window.border_inner_margin - window.border_width,
7083                                 text_width + window.border_inner_margin * 2 + window.border_width * 2,
7084                                 text_height + window.border_inner_margin * 2 + window.border_width * 2);
7085                 }
7086
7087                 /* draw text */
7088         }
7089         setup_fonts();
7090 #endif /* X11 */
7091         for_each_line(text_buffer, draw_line);
7092 #if defined(HAVE_LUA) && defined(X11)
7093         llua_draw_post_hook();
7094 #endif /* HAVE_LUA */
7095 }
7096
7097 static void draw_stuff(void)
7098 {
7099         if (overwrite_file) {
7100                 overwrite_fpointer = fopen(overwrite_file, "w");
7101                 if(!overwrite_fpointer)
7102                         NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
7103         }
7104         if (append_file) {
7105                 append_fpointer = fopen(append_file, "a");
7106                 if(!append_fpointer)
7107                         NORM_ERR("Can't append '%s' anymore", append_file);
7108         }
7109 #ifdef X11
7110         if (output_methods & TO_X) {
7111                 selected_font = 0;
7112                 if (draw_shades && !draw_outline) {
7113                         text_start_x++;
7114                         text_start_y++;
7115                         set_foreground_color(default_bg_color);
7116                         draw_mode = BG;
7117                         draw_text();
7118                         text_start_x--;
7119                         text_start_y--;
7120                 }
7121
7122                 if (draw_outline) {
7123                         int i, j;
7124                         selected_font = 0;
7125
7126                         for (i = -1; i < 2; i++) {
7127                                 for (j = -1; j < 2; j++) {
7128                                         if (i == 0 && j == 0) {
7129                                                 continue;
7130                                         }
7131                                         text_start_x += i;
7132                                         text_start_y += j;
7133                                         set_foreground_color(default_out_color);
7134                                         draw_mode = OUTLINE;
7135                                         draw_text();
7136                                         text_start_x -= i;
7137                                         text_start_y -= j;
7138                                 }
7139                         }
7140                 }
7141
7142                 set_foreground_color(default_fg_color);
7143         }
7144 #endif /* X11 */
7145         draw_mode = FG;
7146         draw_text();
7147 #ifdef X11
7148         if (output_methods & TO_X) {
7149 #ifdef HAVE_XDBE
7150                 if (use_xdbe) {
7151                         XdbeSwapInfo swap;
7152
7153                         swap.swap_window = window.window;
7154                         swap.swap_action = XdbeBackground;
7155                         XdbeSwapBuffers(display, &swap, 1);
7156                 }
7157 #endif
7158         }
7159 #endif /* X11 */
7160         if(overwrite_fpointer) {
7161                 fclose(overwrite_fpointer);
7162                 overwrite_fpointer = 0;
7163         }
7164         if(append_fpointer) {
7165                 fclose(append_fpointer);
7166                 append_fpointer = 0;
7167         }
7168 }
7169
7170 #ifdef X11
7171 static void clear_text(int exposures)
7172 {
7173 #ifdef HAVE_XDBE
7174         if (use_xdbe) {
7175                 /* The swap action is XdbeBackground, which clears */
7176                 return;
7177         } else
7178 #endif
7179         if (display && window.window) { // make sure these are !null
7180                 /* there is some extra space for borders and outlines */
7181                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
7182                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
7183                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7184                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
7185         }
7186 }
7187 #endif /* X11 */
7188
7189 static int need_to_update;
7190
7191 /* update_text() generates new text and clears old text area */
7192 static void update_text(void)
7193 {
7194 #ifdef IMLIB2
7195         cimlib_cleanup();
7196 #endif /* IMLIB2 */
7197         generate_text();
7198 #ifdef X11
7199         if (output_methods & TO_X)
7200                 clear_text(1);
7201 #endif /* X11 */
7202         need_to_update = 1;
7203 #ifdef HAVE_LUA
7204         llua_update_info(&info, update_interval);
7205 #endif /* HAVE_LUA */
7206 }
7207
7208 #ifdef HAVE_SYS_INOTIFY_H
7209 int inotify_fd;
7210 #endif
7211
7212 static void main_loop(void)
7213 {
7214         int terminate = 0;
7215 #ifdef SIGNAL_BLOCKING
7216         sigset_t newmask, oldmask;
7217 #endif
7218         double t;
7219 #ifdef HAVE_SYS_INOTIFY_H
7220         int inotify_config_wd = -1;
7221 #define INOTIFY_EVENT_SIZE  (sizeof(struct inotify_event))
7222 #define INOTIFY_BUF_LEN     (20 * (INOTIFY_EVENT_SIZE + 16))
7223         char inotify_buff[INOTIFY_BUF_LEN];
7224 #endif /* HAVE_SYS_INOTIFY_H */
7225
7226
7227 #ifdef SIGNAL_BLOCKING
7228         sigemptyset(&newmask);
7229         sigaddset(&newmask, SIGINT);
7230         sigaddset(&newmask, SIGTERM);
7231         sigaddset(&newmask, SIGUSR1);
7232 #endif
7233
7234         last_update_time = 0.0;
7235         next_update_time = get_time();
7236         info.looped = 0;
7237         while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7238                 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
7239                         char buf[max_user_text];
7240
7241                         get_battery_short_status(buf, max_user_text, "BAT0");
7242                         if(buf[0] == 'D') {
7243                                 update_interval = update_interval_bat;
7244                         } else {
7245                                 update_interval = update_interval_old;
7246                         }
7247                 }
7248                 info.looped++;
7249
7250 #ifdef SIGNAL_BLOCKING
7251                 /* block signals.  we will inspect for pending signals later */
7252                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7253                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7254                 }
7255 #endif
7256
7257 #ifdef X11
7258                 if (output_methods & TO_X) {
7259                         XFlush(display);
7260
7261                         /* wait for X event or timeout */
7262
7263                         if (!XPending(display)) {
7264                                 fd_set fdsr;
7265                                 struct timeval tv;
7266                                 int s;
7267                                 t = next_update_time - get_time();
7268
7269                                 if (t < 0) {
7270                                         t = 0;
7271                                 } else if (t > update_interval) {
7272                                         t = update_interval;
7273                                 }
7274
7275                                 tv.tv_sec = (long) t;
7276                                 tv.tv_usec = (long) (t * 1000000) % 1000000;
7277                                 FD_ZERO(&fdsr);
7278                                 FD_SET(ConnectionNumber(display), &fdsr);
7279
7280                                 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7281                                 if (s == -1) {
7282                                         if (errno != EINTR) {
7283                                                 NORM_ERR("can't select(): %s", strerror(errno));
7284                                         }
7285                                 } else {
7286                                         /* timeout */
7287                                         if (s == 0) {
7288                                                 update_text();
7289                                         }
7290                                 }
7291                         }
7292
7293                         if (need_to_update) {
7294 #ifdef OWN_WINDOW
7295                                 int wx = window.x, wy = window.y;
7296 #endif
7297
7298                                 need_to_update = 0;
7299                                 selected_font = 0;
7300                                 update_text_area();
7301 #ifdef OWN_WINDOW
7302                                 if (own_window) {
7303                                         int changed = 0;
7304
7305                                         /* resize window if it isn't right size */
7306                                         if (!fixed_size
7307                                                 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7308                                                 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7309                                                         window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7310                                                         window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7311                                                         XResizeWindow(display, window.window, window.width,
7312                                                                 window.height);
7313                                                         set_transparent_background(window.window);
7314
7315                                                         changed++;
7316                                         }
7317
7318                                         /* move window if it isn't in right position */
7319                                         if (!fixed_pos && (window.x != wx || window.y != wy)) {
7320                                                 XMoveWindow(display, window.window, window.x, window.y);
7321                                                 changed++;
7322                                         }
7323
7324                                         /* update struts */
7325                                         if (changed && window.type == TYPE_PANEL) {
7326                                                 int sidenum = -1;
7327
7328                                                 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7329                                                 fflush(stderr);
7330
7331                                                 switch (text_alignment) {
7332                                                         case TOP_LEFT:
7333                                                         case TOP_RIGHT:
7334                                                         case TOP_MIDDLE:
7335                                                         {
7336                                                                 sidenum = 2;
7337                                                                 break;
7338                                                         }
7339                                                         case BOTTOM_LEFT:
7340                                                         case BOTTOM_RIGHT:
7341                                                         case BOTTOM_MIDDLE:
7342                                                         {
7343                                                                 sidenum = 3;
7344                                                                 break;
7345                                                         }
7346                                                         case MIDDLE_LEFT:
7347                                                         {
7348                                                                 sidenum = 0;
7349                                                                 break;
7350                                                         }
7351                                                         case MIDDLE_RIGHT:
7352                                                         {
7353                                                                 sidenum = 1;
7354                                                                 break;
7355                                                         }
7356                                                 }
7357
7358                                                 set_struts(sidenum);
7359                                         }
7360                                 }
7361 #endif
7362
7363                                 clear_text(1);
7364
7365 #ifdef HAVE_XDBE
7366                                 if (use_xdbe) {
7367                                         XRectangle r;
7368
7369                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7370                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7371                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7372                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7373                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7374                                 }
7375 #endif
7376                         }
7377
7378                         /* handle X events */
7379                         while (XPending(display)) {
7380                                 XEvent ev;
7381
7382                                 XNextEvent(display, &ev);
7383                                 switch (ev.type) {
7384                                         case Expose:
7385                                         {
7386                                                 XRectangle r;
7387                                                 r.x = ev.xexpose.x;
7388                                                 r.y = ev.xexpose.y;
7389                                                 r.width = ev.xexpose.width;
7390                                                 r.height = ev.xexpose.height;
7391                                                 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7392                                                 break;
7393                                         }
7394
7395                                         case PropertyNotify:
7396                                         {
7397                                                 if ( ev.xproperty.state == PropertyNewValue ) {
7398                                                         get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
7399                                                 }
7400                                                 break;
7401                                         }
7402
7403 #ifdef OWN_WINDOW
7404                                         case ReparentNotify:
7405                                                 /* set background to ParentRelative for all parents */
7406                                                 if (own_window) {
7407                                                         set_transparent_background(window.window);
7408                                                 }
7409                                                 break;
7410
7411                                         case ConfigureNotify:
7412                                                 if (own_window) {
7413                                                         /* if window size isn't what expected, set fixed size */
7414                                                         if (ev.xconfigure.width != window.width
7415                                                                         || ev.xconfigure.height != window.height) {
7416                                                                 if (window.width != 0 && window.height != 0) {
7417                                                                         fixed_size = 1;
7418                                                                 }
7419
7420                                                                 /* clear old stuff before screwing up
7421                                                                  * size and pos */
7422                                                                 clear_text(1);
7423
7424                                                                 {
7425                                                                         XWindowAttributes attrs;
7426                                                                         if (XGetWindowAttributes(display,
7427                                                                                         window.window, &attrs)) {
7428                                                                                 window.width = attrs.width;
7429                                                                                 window.height = attrs.height;
7430                                                                         }
7431                                                                 }
7432
7433                                                                 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7434                                                                 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7435                                                                 if (text_width > maximum_width
7436                                                                                 && maximum_width > 0) {
7437                                                                         text_width = maximum_width;
7438                                                                 }
7439                                                         }
7440
7441                                                         /* if position isn't what expected, set fixed pos
7442                                                          * total_updates avoids setting fixed_pos when window
7443                                                          * is set to weird locations when started */
7444                                                         /* // this is broken
7445                                                         if (total_updates >= 2 && !fixed_pos
7446                                                                         && (window.x != ev.xconfigure.x
7447                                                                         || window.y != ev.xconfigure.y)
7448                                                                         && (ev.xconfigure.x != 0
7449                                                                         || ev.xconfigure.y != 0)) {
7450                                                                 fixed_pos = 1;
7451                                                         } */
7452                                                 }
7453                                                 break;
7454
7455                                         case ButtonPress:
7456                                                 if (own_window) {
7457                                                         /* if an ordinary window with decorations */
7458                                                         if ((window.type == TYPE_NORMAL)
7459                                                                 && (!TEST_HINT(window.hints,
7460                                                                 HINT_UNDECORATED))) {
7461                                                                 /* allow conky to hold input focus. */
7462                                                                 break;
7463                                                         } else {
7464                                                                 /* forward the click to the desktop window */
7465                                                                 XUngrabPointer(display, ev.xbutton.time);
7466                                                                 ev.xbutton.window = window.desktop;
7467                                                                 ev.xbutton.x = ev.xbutton.x_root;
7468                                                                 ev.xbutton.y = ev.xbutton.y_root;
7469                                                                 XSendEvent(display, ev.xbutton.window, False,
7470                                                                         ButtonPressMask, &ev);
7471                                                                 XSetInputFocus(display, ev.xbutton.window,
7472                                                                         RevertToParent, ev.xbutton.time);
7473                                                         }
7474                                                 }
7475                                                 break;
7476
7477                                         case ButtonRelease:
7478                                                 if (own_window) {
7479                                                         /* if an ordinary window with decorations */
7480                                                         if ((window.type == TYPE_NORMAL)
7481                                                                         && (!TEST_HINT(window.hints,
7482                                                                         HINT_UNDECORATED))) {
7483                                                                 /* allow conky to hold input focus. */
7484                                                                 break;
7485                                                         } else {
7486                                                                 /* forward the release to the desktop window */
7487                                                                 ev.xbutton.window = window.desktop;
7488                                                                 ev.xbutton.x = ev.xbutton.x_root;
7489                                                                 ev.xbutton.y = ev.xbutton.y_root;
7490                                                                 XSendEvent(display, ev.xbutton.window, False,
7491                                                                         ButtonReleaseMask, &ev);
7492                                                         }
7493                                                 }
7494                                                 break;
7495
7496 #endif
7497
7498                                         default:
7499 #ifdef HAVE_XDAMAGE
7500                                                 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7501                                                         XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7502
7503                                                         XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7504                                                         XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7505                                                 }
7506 #endif /* HAVE_XDAMAGE */
7507                                                 break;
7508                                 }
7509                         }
7510
7511 #ifdef HAVE_XDAMAGE
7512                         XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7513                         XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7514 #endif /* HAVE_XDAMAGE */
7515
7516                         /* XDBE doesn't seem to provide a way to clear the back buffer without
7517                          * interfering with the front buffer, other than passing XdbeBackground
7518                          * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7519                          * redraw the text even if it wasn't part of the exposed area. OTOH,
7520                          * if we're not going to call draw_stuff at all, then no swap happens
7521                          * and we can safely do nothing. */
7522
7523                         if (!XEmptyRegion(x11_stuff.region)) {
7524 #ifdef HAVE_XDBE
7525                                 if (use_xdbe) {
7526                                         XRectangle r;
7527
7528                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7529                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7530                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7531                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7532                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7533                                 }
7534 #endif
7535                                 XSetRegion(display, window.gc, x11_stuff.region);
7536 #ifdef XFT
7537                                 if (use_xft) {
7538                                         XftDrawSetClip(window.xftdraw, x11_stuff.region);
7539                                 }
7540 #endif
7541 #ifdef IMLIB2
7542                                 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7543 #endif /* IMLIB2 */
7544                                 draw_stuff();
7545                                 XDestroyRegion(x11_stuff.region);
7546                                 x11_stuff.region = XCreateRegion();
7547                         }
7548                 } else {
7549 #endif /* X11 */
7550                         t = (next_update_time - get_time()) * 1000000;
7551                         if(t > 0) usleep((useconds_t)t);
7552                         update_text();
7553                         draw_stuff();
7554 #ifdef NCURSES
7555                         if(output_methods & TO_NCURSES) {
7556                                 refresh();
7557                                 clear();
7558                         }
7559 #endif
7560 #ifdef X11
7561                 }
7562 #endif /* X11 */
7563
7564 #ifdef SIGNAL_BLOCKING
7565                 /* unblock signals of interest and let handler fly */
7566                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7567                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7568                 }
7569 #endif
7570
7571                 switch (g_signal_pending) {
7572                         case SIGHUP:
7573                         case SIGUSR1:
7574                                 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7575                                 reload_config();
7576                                 break;
7577                         case SIGINT:
7578                         case SIGTERM:
7579                                 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
7580                                 terminate = 1;
7581 #ifdef X11
7582                                 if (output_methods & TO_X) {
7583                                         XDestroyRegion(x11_stuff.region);
7584                                         x11_stuff.region = NULL;
7585 #ifdef HAVE_XDAMAGE
7586                                         XDamageDestroy(display, x11_stuff.damage);
7587                                         XFixesDestroyRegion(display, x11_stuff.region2);
7588                                         XFixesDestroyRegion(display, x11_stuff.part);
7589 #endif /* HAVE_XDAMAGE */
7590                                         if (disp) {
7591                                                 free(disp);
7592                                         }
7593                                 }
7594 #endif /* X11 */
7595                                 if(overwrite_file) {
7596                                         free(overwrite_file);
7597                                         overwrite_file = 0;
7598                                 }
7599                                 if(append_file) {
7600                                         free(append_file);
7601                                         append_file = 0;
7602                                 }
7603                                 break;
7604                         default:
7605                                 /* Reaching here means someone set a signal
7606                                  * (SIGXXXX, signal_handler), but didn't write any code
7607                                  * to deal with it.
7608                                  * If you don't want to handle a signal, don't set a handler on
7609                                  * it in the first place. */
7610                                 if (g_signal_pending) {
7611                                         NORM_ERR("ignoring signal (%d)", g_signal_pending);
7612                                 }
7613                                 break;
7614                 }
7615 #ifdef HAVE_SYS_INOTIFY_H
7616                 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7617                         inotify_config_wd = inotify_add_watch(inotify_fd,
7618                                         current_config,
7619                                         IN_MODIFY);
7620                 }
7621                 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7622                         int len = 0, idx = 0;
7623                         fd_set descriptors;
7624                         struct timeval time_to_wait;
7625
7626                         FD_ZERO(&descriptors);
7627                         FD_SET(inotify_fd, &descriptors);
7628
7629                         time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7630
7631                         select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7632                         if (FD_ISSET(inotify_fd, &descriptors)) {
7633                                 /* process inotify events */
7634                                 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7635                                 while (len > 0 && idx < len) {
7636                                         struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7637                                         if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7638                                                 /* current_config should be reloaded */
7639                                                 NORM_ERR("'%s' modified, reloading...", current_config);
7640                                                 reload_config();
7641                                                 if (ev->mask & IN_IGNORED) {
7642                                                         /* for some reason we get IN_IGNORED here
7643                                                          * sometimes, so we need to re-add the watch */
7644                                                         inotify_config_wd = inotify_add_watch(inotify_fd,
7645                                                                         current_config,
7646                                                                         IN_MODIFY);
7647                                                 }
7648                                         }
7649 #ifdef HAVE_LUA
7650                                         else {
7651                                                 llua_inotify_query(ev->wd, ev->mask);
7652                                         }
7653 #endif /* HAVE_LUA */
7654                                         idx += INOTIFY_EVENT_SIZE + ev->len;
7655                                 }
7656                         }
7657                 }
7658 #endif /* HAVE_SYS_INOTIFY_H */
7659
7660 #ifdef HAVE_LUA
7661         llua_update_info(&info, update_interval);
7662 #endif /* HAVE_LUA */
7663                 g_signal_pending = 0;
7664         }
7665         clean_up(NULL, NULL);
7666
7667 #ifdef HAVE_SYS_INOTIFY_H
7668         if (inotify_fd != -1) {
7669                 inotify_rm_watch(inotify_fd, inotify_config_wd);
7670                 close(inotify_fd);
7671                 inotify_fd = inotify_config_wd = 0;
7672         }
7673 #endif /* HAVE_SYS_INOTIFY_H */
7674 }
7675
7676 #ifdef X11
7677 static void load_config_file_x11(const char *);
7678 #endif /* X11 */
7679 void initialisation(int argc, char** argv);
7680
7681         /* reload the config file */
7682 static void reload_config(void)
7683 {
7684         char *current_config_copy = strdup(current_config);
7685         clean_up(NULL, NULL);
7686         current_config = current_config_copy;
7687         initialisation(argc_copy, argv_copy);
7688 }
7689
7690 void clean_up(void *memtofree1, void* memtofree2)
7691 {
7692         int i;
7693
7694 #ifdef NCURSES
7695         if(output_methods & TO_NCURSES) {
7696                 delwin(stdscr);
7697                 endwin();
7698         }
7699 #endif
7700         conftree_empty(currentconffile);
7701         currentconffile = NULL;
7702         if(memtofree1) {
7703                 free(memtofree1);
7704         }
7705         if(memtofree2) {
7706                 free(memtofree2);
7707         }
7708         timed_thread_destroy_registered_threads();
7709
7710         if (info.cpu_usage) {
7711                 free(info.cpu_usage);
7712                 info.cpu_usage = NULL;
7713         }
7714 #ifdef X11
7715         if (x_initialised == YES) {
7716                 destroy_window();
7717                 free_fonts();
7718                 if(x11_stuff.region) {
7719                         XDestroyRegion(x11_stuff.region);
7720                         x11_stuff.region = NULL;
7721                 }
7722                 XClearWindow(display, RootWindow(display, screen));
7723                 XCloseDisplay(display);
7724                 display = NULL;
7725                 if(info.x11.desktop.all_names) {
7726                         free(info.x11.desktop.all_names);
7727                         info.x11.desktop.all_names = NULL;
7728                 }
7729                 if (info.x11.desktop.name) {
7730                         free(info.x11.desktop.name);
7731                         info.x11.desktop.name = NULL;
7732                 }
7733                 x_initialised = NO;
7734         }else{
7735                 free(fonts);    //in set_default_configurations a font is set but not loaded
7736                 font_count = -1;
7737         }
7738
7739 #endif /* X11 */
7740
7741         for (i = 0; i < MAX_TEMPLATES; i++) {
7742                 if (template[i]) {
7743                         free(template[i]);
7744                         template[i] = NULL;
7745                 }
7746         }
7747
7748         free_text_objects(&global_root_object, 0);
7749         if (tmpstring1) {
7750                 free(tmpstring1);
7751                 tmpstring1 = 0;
7752         }
7753         if (tmpstring2) {
7754                 free(tmpstring2);
7755                 tmpstring2 = 0;
7756         }
7757         if (text_buffer) {
7758                 free(text_buffer);
7759                 text_buffer = 0;
7760         }
7761
7762         if (global_text) {
7763                 free(global_text);
7764                 global_text = 0;
7765         }
7766
7767         free(current_config);
7768         current_config = 0;
7769
7770 #ifdef TCP_PORT_MONITOR
7771         tcp_portmon_clear();
7772 #endif
7773 #ifdef HAVE_CURL
7774         ccurl_free_info();
7775 #endif
7776 #ifdef RSS
7777         rss_free_info();
7778 #endif
7779 #ifdef WEATHER
7780         weather_free_info();
7781 #endif
7782 #ifdef HAVE_LUA
7783         llua_shutdown_hook();
7784         llua_close();
7785 #endif /* HAVE_LUA */
7786
7787         if (specials) {
7788                 for (i = 0; i < special_count; i++) {
7789                         if (specials[i].type == GRAPH) {
7790                                 free(specials[i].graph);
7791                         }
7792                 }
7793                 free(specials);
7794                 specials = NULL;
7795         }
7796
7797         clear_net_stats();
7798         clear_diskio_stats();
7799         if(global_cpu != NULL) {
7800                 free(global_cpu);
7801                 global_cpu = NULL;
7802         }
7803 }
7804
7805 static int string_to_bool(const char *s)
7806 {
7807         if (!s) {
7808                 // Assumes an option without a true/false means true
7809                 return 1;
7810         } else if (strcasecmp(s, "yes") == EQUAL) {
7811                 return 1;
7812         } else if (strcasecmp(s, "true") == EQUAL) {
7813                 return 1;
7814         } else if (strcasecmp(s, "1") == EQUAL) {
7815                 return 1;
7816         }
7817         return 0;
7818 }
7819
7820 #ifdef X11
7821 static enum alignment string_to_alignment(const char *s)
7822 {
7823         if (strcasecmp(s, "top_left") == EQUAL) {
7824                 return TOP_LEFT;
7825         } else if (strcasecmp(s, "top_right") == EQUAL) {
7826                 return TOP_RIGHT;
7827         } else if (strcasecmp(s, "top_middle") == EQUAL) {
7828                 return TOP_MIDDLE;
7829         } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7830                 return BOTTOM_LEFT;
7831         } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7832                 return BOTTOM_RIGHT;
7833         } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7834                 return BOTTOM_MIDDLE;
7835         } else if (strcasecmp(s, "middle_left") == EQUAL) {
7836                 return MIDDLE_LEFT;
7837         } else if (strcasecmp(s, "middle_right") == EQUAL) {
7838                 return MIDDLE_RIGHT;
7839         } else if (strcasecmp(s, "tl") == EQUAL) {
7840                 return TOP_LEFT;
7841         } else if (strcasecmp(s, "tr") == EQUAL) {
7842                 return TOP_RIGHT;
7843         } else if (strcasecmp(s, "tm") == EQUAL) {
7844                 return TOP_MIDDLE;
7845         } else if (strcasecmp(s, "bl") == EQUAL) {
7846                 return BOTTOM_LEFT;
7847         } else if (strcasecmp(s, "br") == EQUAL) {
7848                 return BOTTOM_RIGHT;
7849         } else if (strcasecmp(s, "bm") == EQUAL) {
7850                 return BOTTOM_MIDDLE;
7851         } else if (strcasecmp(s, "ml") == EQUAL) {
7852                 return MIDDLE_LEFT;
7853         } else if (strcasecmp(s, "mr") == EQUAL) {
7854                 return MIDDLE_RIGHT;
7855         } else if (strcasecmp(s, "none") == EQUAL) {
7856                 return NONE;
7857         }
7858         return TOP_LEFT;
7859 }
7860 #endif /* X11 */
7861
7862 #ifdef X11
7863 static void set_default_configurations_for_x(void)
7864 {
7865         default_fg_color = WhitePixel(display, screen);
7866         default_bg_color = BlackPixel(display, screen);
7867         default_out_color = BlackPixel(display, screen);
7868         color0 = default_fg_color;
7869         color1 = default_fg_color;
7870         color2 = default_fg_color;
7871         color3 = default_fg_color;
7872         color4 = default_fg_color;
7873         color5 = default_fg_color;
7874         color6 = default_fg_color;
7875         color7 = default_fg_color;
7876         color8 = default_fg_color;
7877         color9 = default_fg_color;
7878         current_text_color = default_fg_color;
7879 }
7880 #endif /* X11 */
7881
7882 static void set_default_configurations(void)
7883 {
7884         int i;
7885 #ifdef MPD
7886         char *mpd_env_host;
7887         char *mpd_env_port;
7888 #endif
7889         update_uname();
7890         fork_to_background = 0;
7891         total_run_times = 0;
7892         info.cpu_avg_samples = 2;
7893         info.net_avg_samples = 2;
7894         info.diskio_avg_samples = 2;
7895         info.memmax = 0;
7896         top_cpu = 0;
7897         cpu_separate = 0;
7898         short_units = 0;
7899         format_human_readable = 1;
7900         top_mem = 0;
7901         top_time = 0;
7902 #ifdef IOSTATS
7903         top_io = 0;
7904 #endif
7905 #ifdef MPD
7906         mpd_env_host = getenv("MPD_HOST");
7907         mpd_env_port = getenv("MPD_PORT");
7908
7909         if (!mpd_env_host || !strlen(mpd_env_host)) {
7910                 mpd_set_host("localhost");
7911         } else {
7912                 /* MPD_HOST environment variable is set */
7913                 char *mpd_hostpart = strchr(mpd_env_host, '@');
7914                 if (!mpd_hostpart) {
7915                         mpd_set_host(mpd_env_host);
7916                 } else {
7917                         /* MPD_HOST contains a password */
7918                         char mpd_password[mpd_hostpart - mpd_env_host + 1];
7919                         snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7920
7921                         if (!strlen(mpd_hostpart + 1)) {
7922                                 mpd_set_host("localhost");
7923                         } else {
7924                                 mpd_set_host(mpd_hostpart + 1);
7925                         }
7926
7927                         mpd_set_password(mpd_password, 1);
7928                 }
7929         }
7930
7931
7932         if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7933                 /* failed to set port from environment variable */
7934                 mpd_set_port("6600");
7935         }
7936 #endif
7937 #ifdef XMMS2
7938         info.xmms2.artist = NULL;
7939         info.xmms2.album = NULL;
7940         info.xmms2.title = NULL;
7941         info.xmms2.genre = NULL;
7942         info.xmms2.comment = NULL;
7943         info.xmms2.url = NULL;
7944         info.xmms2.status = NULL;
7945         info.xmms2.playlist = NULL;
7946 #endif
7947         use_spacer = NO_SPACER;
7948 #ifdef X11
7949         output_methods = TO_X;
7950 #else
7951         output_methods = TO_STDOUT;
7952 #endif
7953 #ifdef X11
7954         show_graph_scale = 0;
7955         show_graph_range = 0;
7956         draw_shades = 1;
7957         draw_borders = 0;
7958         draw_graph_borders = 1;
7959         draw_outline = 0;
7960         set_first_font("6x10");
7961         gap_x = 5;
7962         gap_y = 60;
7963         minimum_width = 5;
7964         minimum_height = 5;
7965         maximum_width = 0;
7966 #ifdef OWN_WINDOW
7967         own_window = 0;
7968         window.type = TYPE_NORMAL;
7969         window.hints = 0;
7970         strcpy(window.class_name, PACKAGE_NAME);
7971         sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7972 #endif
7973         stippled_borders = 0;
7974         window.border_inner_margin = 3;
7975         window.border_outer_margin = 1;
7976         window.border_width = 1;
7977         text_alignment = BOTTOM_LEFT;
7978         info.x11.monitor.number = 1;
7979         info.x11.monitor.current = 0;
7980         info.x11.desktop.current = 1; 
7981         info.x11.desktop.number = 1;
7982         info.x11.desktop.nitems = 0;
7983         info.x11.desktop.all_names = NULL; 
7984         info.x11.desktop.name = NULL; 
7985 #endif /* X11 */
7986
7987         for (i = 0; i < MAX_TEMPLATES; i++) {
7988                 if (template[i])
7989                         free(template[i]);
7990                 template[i] = strdup("");
7991         }
7992
7993         free(current_mail_spool);
7994         {
7995                 char buf[256];
7996
7997                 variable_substitute(MAIL_FILE, buf, 256);
7998                 if (buf[0] != '\0') {
7999                         current_mail_spool = strndup(buf, text_buffer_size);
8000                 }
8001         }
8002
8003         no_buffers = 1;
8004         set_update_interval(3);
8005         update_interval_bat = NOBATTERY;
8006         info.music_player_interval = 1.0;
8007         stuff_in_uppercase = 0;
8008         info.users.number = 1;
8009
8010 #ifdef TCP_PORT_MONITOR
8011         /* set default connection limit */
8012         tcp_portmon_set_max_connections(0);
8013 #endif
8014 }
8015
8016 /* returns 1 if you can overwrite or create the file at 'path' */
8017 static _Bool overwrite_works(const char *path)
8018 {
8019         FILE *filepointer;
8020
8021         if (!(filepointer = fopen(path, "w")))
8022                 return 0;
8023         fclose(filepointer);
8024         return 1;
8025 }
8026
8027 /* returns 1 if you can append or create the file at 'path' */
8028 static _Bool append_works(const char *path)
8029 {
8030         FILE *filepointer;
8031
8032         if (!(filepointer = fopen(path, "a")))
8033                 return 0;
8034         fclose(filepointer);
8035         return 1;
8036 }
8037
8038 #ifdef X11
8039 #ifdef DEBUG
8040 /* WARNING, this type not in Xlib spec */
8041 int x11_error_handler(Display *d, XErrorEvent *err)
8042         __attribute__((noreturn));
8043 int x11_error_handler(Display *d, XErrorEvent *err)
8044 {
8045         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",
8046                         err->type,
8047                         (long unsigned)err->display,
8048                         (long)err->resourceid,
8049                         err->serial,
8050                         err->error_code,
8051                         err->request_code,
8052                         err->minor_code,
8053                         (long unsigned)d
8054                         );
8055         abort();
8056 }
8057
8058 int x11_ioerror_handler(Display *d)
8059         __attribute__((noreturn));
8060 int x11_ioerror_handler(Display *d)
8061 {
8062         NORM_ERR("X Error: Display %lx\n",
8063                         (long unsigned)d
8064                         );
8065         abort();
8066 }
8067 #endif /* DEBUG */
8068
8069 static void X11_initialisation(void)
8070 {
8071         if (x_initialised == YES) return;
8072         output_methods |= TO_X;
8073         init_X11(disp);
8074         set_default_configurations_for_x();
8075         x_initialised = YES;
8076 #ifdef DEBUG
8077         _Xdebug = 1;
8078         /* WARNING, this type not in Xlib spec */
8079         XSetErrorHandler(&x11_error_handler);
8080         XSetIOErrorHandler(&x11_ioerror_handler);
8081 #endif /* DEBUG */
8082 }
8083
8084 static char **xargv = 0;
8085 static int xargc = 0;
8086
8087 static void X11_create_window(void)
8088 {
8089         if (output_methods & TO_X) {
8090 #ifdef OWN_WINDOW
8091                 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
8092                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
8093                                 xargv, xargc);
8094 #else /* OWN_WINDOW */
8095                 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
8096                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
8097                                 xargv, xargc);
8098 #endif /* OWN_WINDOW */
8099
8100                 setup_fonts();
8101                 load_fonts();
8102                 update_text_area();     /* to position text/window on screen */
8103
8104 #ifdef OWN_WINDOW
8105                 if (own_window && !fixed_pos) {
8106                         XMoveWindow(display, window.window, window.x, window.y);
8107                 }
8108                 if (own_window) {
8109                         set_transparent_background(window.window);
8110                 }
8111 #endif
8112
8113                 create_gc();
8114
8115                 draw_stuff();
8116
8117                 x11_stuff.region = XCreateRegion();
8118 #ifdef HAVE_XDAMAGE
8119                 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
8120                         NORM_ERR("Xdamage extension unavailable");
8121                 }
8122                 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
8123                 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
8124                 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
8125 #endif /* HAVE_XDAMAGE */
8126
8127                 selected_font = 0;
8128                 update_text_area();     /* to get initial size of the window */
8129         }
8130 #ifdef HAVE_LUA
8131         /* setup lua window globals */
8132         llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
8133 #endif /* HAVE_LUA */
8134 }
8135 #endif /* X11 */
8136
8137 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
8138 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
8139 #define CONF2(a) if (strcasecmp(name, a) == 0)
8140 #define CONF(a) else CONF2(a)
8141 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
8142                 || strcasecmp(name, b) == 0)
8143 #define CONF_CONTINUE 1
8144 #define CONF_BREAK 2
8145 #define CONF_BUFF_SIZE 512
8146
8147 static FILE *open_config_file(const char *f)
8148 {
8149 #ifdef CONFIG_OUTPUT
8150         if (!strcmp(f, "==builtin==")) {
8151                 return conf_cookie_open();
8152         } else
8153 #endif /* CONFIG_OUTPUT */
8154                 return fopen(f, "r");
8155 }
8156
8157 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
8158 {
8159         char *p, *p2;
8160         (*line)++;
8161         if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8162                 return CONF_BREAK;
8163         }
8164         remove_comments(buf);
8165
8166         p = buf;
8167
8168         /* skip spaces */
8169         while (*p && isspace((int) *p)) {
8170                 p++;
8171         }
8172         if (*p == '\0') {
8173                 return CONF_CONTINUE;   /* empty line */
8174         }
8175
8176         *name = p;
8177
8178         /* skip name */
8179         p2 = p;
8180         while (*p2 && !isspace((int) *p2)) {
8181                 p2++;
8182         }
8183         if (*p2 != '\0') {
8184                 *p2 = '\0';     /* break at name's end */
8185                 p2++;
8186         }
8187
8188         /* get value */
8189         if (*p2) {
8190                 p = p2;
8191                 while (*p && isspace((int) *p)) {
8192                         p++;
8193                 }
8194
8195                 *value = p;
8196
8197                 p2 = *value + strlen(*value);
8198                 while (isspace((int) *(p2 - 1))) {
8199                         *--p2 = '\0';
8200                 }
8201         } else {
8202                 *value = 0;
8203         }
8204         return 0;
8205 }
8206
8207 char load_config_file(const char *f)
8208 {
8209         int line = 0;
8210         FILE *fp;
8211
8212         fp = open_config_file(f);
8213         if (!fp) {
8214                 return FALSE;
8215         }
8216         DBGP("reading contents from config file '%s'", f);
8217
8218         while (!feof(fp)) {
8219                 char buff[CONF_BUFF_SIZE], *name, *value;
8220                 int ret = do_config_step(&line, fp, buff, &name, &value);
8221                 if (ret == CONF_BREAK) {
8222                         break;
8223                 } else if (ret == CONF_CONTINUE) {
8224                         continue;
8225                 }
8226
8227 #ifdef X11
8228                 CONF2("out_to_x") {
8229                         /* don't listen if X is already initialised or
8230                          * if we already know we don't want it */
8231                         if(x_initialised != YES) {
8232                                 if (string_to_bool(value)) {
8233                                         output_methods &= TO_X;
8234                                 } else {
8235                                         output_methods &= ~TO_X;
8236                                         x_initialised = NEVER;
8237                                 }
8238                         }
8239                 }
8240                 CONF("display") {
8241                         if (!value || x_initialised == YES) {
8242                                 CONF_ERR;
8243                         } else {
8244                                 if (disp)
8245                                         free(disp);
8246                                 disp = strdup(value);
8247                         }
8248                 }
8249                 CONF("alignment") {
8250 #ifdef OWN_WINDOW
8251                         if (window.type == TYPE_DOCK)
8252                                 ;
8253                         else
8254 #endif /*OWN_WINDOW */
8255                         if (value) {
8256                                 int a = string_to_alignment(value);
8257
8258                                 if (a <= 0) {
8259                                         CONF_ERR;
8260                                 } else {
8261                                         text_alignment = a;
8262                                 }
8263                         } else {
8264                                 CONF_ERR;
8265                         }
8266                 }
8267                 CONF("background") {
8268                         fork_to_background = string_to_bool(value);
8269                 }
8270 #else
8271                 CONF2("background") {
8272                         fork_to_background = string_to_bool(value);
8273                 }
8274 #endif /* X11 */
8275 #ifdef X11
8276                 CONF("show_graph_scale") {
8277                         show_graph_scale = string_to_bool(value);
8278                 }
8279                 CONF("show_graph_range") {
8280                         show_graph_range = string_to_bool(value);
8281                 }
8282                 CONF("border_inner_margin") {
8283                         if (value) {
8284                                 window.border_inner_margin = strtol(value, 0, 0);
8285                                 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8286                         } else {
8287                                 CONF_ERR;
8288                         }
8289                 }
8290                 CONF("border_outer_margin") {
8291                         if (value) {
8292                                 window.border_outer_margin = strtol(value, 0, 0);
8293                                 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8294                         } else {
8295                                 CONF_ERR;
8296                         }
8297                 }
8298                 CONF("border_width") {
8299                         if (value) {
8300                                 window.border_width = strtol(value, 0, 0);
8301                                 if (window.border_width < 0) window.border_width = 0;
8302                         } else {
8303                                 CONF_ERR;
8304                         }
8305                 }
8306 #endif /* X11 */
8307 #define TEMPLATE_CONF(n) \
8308                 CONF("template"#n) { \
8309                         if (value) { \
8310                                 free(template[n]); \
8311                                 template[n] = strdup(value); \
8312                         } else { \
8313                                 CONF_ERR; \
8314                         } \
8315                 }
8316                 TEMPLATE_CONF(0)
8317                 TEMPLATE_CONF(1)
8318                 TEMPLATE_CONF(2)
8319                 TEMPLATE_CONF(3)
8320                 TEMPLATE_CONF(4)
8321                 TEMPLATE_CONF(5)
8322                 TEMPLATE_CONF(6)
8323                 TEMPLATE_CONF(7)
8324                 TEMPLATE_CONF(8)
8325                 TEMPLATE_CONF(9)
8326                 CONF("imap") {
8327                         if (value) {
8328                                 info.mail = parse_mail_args(IMAP_TYPE, value);
8329                         } else {
8330                                 CONF_ERR;
8331                         }
8332                 }
8333                 CONF("pop3") {
8334                         if (value) {
8335                                 info.mail = parse_mail_args(POP3_TYPE, value);
8336                         } else {
8337                                 CONF_ERR;
8338                         }
8339                 }
8340                 CONF("default_bar_size") {
8341                         char err = 0;
8342                         if (value) {
8343                                 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8344                                         err = 1;
8345                                 }
8346                         } else {
8347                                 err = 1;
8348                         }
8349                         if (err) {
8350                                 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8351                         }
8352                 }
8353 #ifdef X11
8354                 CONF("default_graph_size") {
8355                         char err = 0;
8356                         if (value) {
8357                                 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8358                                         err = 1;
8359                                 }
8360                         } else {
8361                                 err = 1;
8362                         }
8363                         if (err) {
8364                                 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8365                         }
8366                 }
8367                 CONF("default_gauge_size") {
8368                         char err = 0;
8369                         if (value) {
8370                                 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8371                                         err = 1;
8372                                 }
8373                         } else {
8374                                 err = 1;
8375                         }
8376                         if (err) {
8377                                 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8378                         }
8379                 }
8380 #endif
8381 #ifdef MPD
8382                 CONF("mpd_host") {
8383                         if (value) {
8384                                 mpd_set_host(value);
8385                         } else {
8386                                 CONF_ERR;
8387                         }
8388                 }
8389                 CONF("mpd_port") {
8390                         if (value && mpd_set_port(value)) {
8391                                 CONF_ERR;
8392                         }
8393                 }
8394                 CONF("mpd_password") {
8395                         if (value) {
8396                                 mpd_set_password(value, 0);
8397                         } else {
8398                                 CONF_ERR;
8399                         }
8400                 }
8401 #endif
8402                 CONF("music_player_interval") {
8403                         if (value) {
8404                                 info.music_player_interval = strtod(value, 0);
8405                         } else {
8406                                 CONF_ERR;
8407                         }
8408                 }
8409 #ifdef __OpenBSD__
8410                 CONF("sensor_device") {
8411                         if (value) {
8412                                 sensor_device = strtol(value, 0, 0);
8413                         } else {
8414                                 CONF_ERR;
8415                         }
8416                 }
8417 #endif
8418                 CONF("cpu_avg_samples") {
8419                         if (value) {
8420                                 cpu_avg_samples = strtol(value, 0, 0);
8421                                 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8422                                         CONF_ERR;
8423                                 } else {
8424                                         info.cpu_avg_samples = cpu_avg_samples;
8425                                 }
8426                         } else {
8427                                 CONF_ERR;
8428                         }
8429                 }
8430                 CONF("net_avg_samples") {
8431                         if (value) {
8432                                 net_avg_samples = strtol(value, 0, 0);
8433                                 if (net_avg_samples < 1 || net_avg_samples > 14) {
8434                                         CONF_ERR;
8435                                 } else {
8436                                         info.net_avg_samples = net_avg_samples;
8437                                 }
8438                         } else {
8439                                 CONF_ERR;
8440                         }
8441                 }
8442                 CONF("diskio_avg_samples") {
8443                         if (value) {
8444                                 diskio_avg_samples = strtol(value, 0, 0);
8445                                 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8446                                         CONF_ERR;
8447                                 } else {
8448                                         info.diskio_avg_samples = diskio_avg_samples;
8449                                 }
8450                         } else {
8451                                 CONF_ERR;
8452                         }
8453                 }
8454
8455 #ifdef HAVE_XDBE
8456                 CONF("double_buffer") {
8457                         use_xdbe = string_to_bool(value);
8458                 }
8459 #endif
8460 #ifdef X11
8461                 CONF("override_utf8_locale") {
8462                         utf8_mode = string_to_bool(value);
8463                 }
8464                 CONF("draw_borders") {
8465                         draw_borders = string_to_bool(value);
8466                 }
8467                 CONF("draw_graph_borders") {
8468                         draw_graph_borders = string_to_bool(value);
8469                 }
8470                 CONF("draw_shades") {
8471                         draw_shades = string_to_bool(value);
8472                 }
8473                 CONF("draw_outline") {
8474                         draw_outline = string_to_bool(value);
8475                 }
8476 #endif /* X11 */
8477                 CONF("out_to_console") {
8478                         if(string_to_bool(value)) {
8479                                 output_methods |= TO_STDOUT;
8480                         } else {
8481                                 output_methods &= ~TO_STDOUT;
8482                         }
8483                 }
8484                 CONF("extra_newline") {
8485                         extra_newline = string_to_bool(value);
8486                 }
8487                 CONF("out_to_stderr") {
8488                         if(string_to_bool(value))
8489                                 output_methods |= TO_STDERR;
8490                 }
8491 #ifdef NCURSES
8492                 CONF("out_to_ncurses") {
8493                         if(string_to_bool(value)) {
8494                                 initscr();
8495                                 output_methods |= TO_NCURSES;
8496                         }
8497                 }
8498 #endif
8499                 CONF("overwrite_file") {
8500                         if(overwrite_file) {
8501                                 free(overwrite_file);
8502                                 overwrite_file = 0;
8503                         }
8504                         if(overwrite_works(value)) {
8505                                 overwrite_file = strdup(value);
8506                                 output_methods |= OVERWRITE_FILE;
8507                         } else
8508                                 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8509                 }
8510                 CONF("append_file") {
8511                         if(append_file) {
8512                                 free(append_file);
8513                                 append_file = 0;
8514                         }
8515                         if(append_works(value)) {
8516                                 append_file = strdup(value);
8517                                 output_methods |= APPEND_FILE;
8518                         } else
8519                                 NORM_ERR("append_file won't be able to create/append '%s'", value);
8520                 }
8521                 CONF("use_spacer") {
8522                         if (value) {
8523                                 if (strcasecmp(value, "left") == EQUAL) {
8524                                         use_spacer = LEFT_SPACER;
8525                                 } else if (strcasecmp(value, "right") == EQUAL) {
8526                                         use_spacer = RIGHT_SPACER;
8527                                 } else if (strcasecmp(value, "none") == EQUAL) {
8528                                         use_spacer = NO_SPACER;
8529                                 } else {
8530                                         use_spacer = string_to_bool(value);
8531                                         NORM_ERR("use_spacer should have an argument of left, right, or"
8532                                                 " none.  '%s' seems to be some form of '%s', so"
8533                                                 " defaulting to %s.", value,
8534                                                 use_spacer ? "true" : "false",
8535                                                 use_spacer ? "right" : "none");
8536                                         if (use_spacer) {
8537                                                 use_spacer = RIGHT_SPACER;
8538                                         } else {
8539                                                 use_spacer = NO_SPACER;
8540                                         }
8541                                 }
8542                         } else {
8543                                 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
8544                                 use_spacer = RIGHT_SPACER;
8545                         }
8546                 }
8547 #ifdef X11
8548 #ifdef XFT
8549                 CONF("use_xft") {
8550                         use_xft = string_to_bool(value);
8551                 }
8552                 CONF("font") {
8553                         if (value) {
8554                                 set_first_font(value);
8555                         }
8556                 }
8557                 CONF("xftalpha") {
8558                         if (value && font_count >= 0) {
8559                                 fonts[0].font_alpha = atof(value) * 65535.0;
8560                         }
8561                 }
8562                 CONF("xftfont") {
8563                         if (use_xft) {
8564 #else
8565                 CONF("use_xft") {
8566                         if (string_to_bool(value)) {
8567                                 NORM_ERR("Xft not enabled at compile time");
8568                         }
8569                 }
8570                 CONF("xftfont") {
8571                         /* xftfont silently ignored when no Xft */
8572                 }
8573                 CONF("xftalpha") {
8574                         /* xftalpha is silently ignored when no Xft */
8575                 }
8576                 CONF("font") {
8577 #endif
8578                         if (value) {
8579                                 set_first_font(value);
8580                         }
8581 #ifdef XFT
8582                         }
8583 #endif
8584                 }
8585                 CONF("gap_x") {
8586                         if (value) {
8587                                 gap_x = atoi(value);
8588                         } else {
8589                                 CONF_ERR;
8590                         }
8591                 }
8592                 CONF("gap_y") {
8593                         if (value) {
8594                                 gap_y = atoi(value);
8595                         } else {
8596                                 CONF_ERR;
8597                         }
8598                 }
8599 #endif /* X11 */
8600                 CONF("mail_spool") {
8601                         if (value) {
8602                                 char buffer[256];
8603
8604                                 variable_substitute(value, buffer, 256);
8605
8606                                 if (buffer[0] != '\0') {
8607                                         if (current_mail_spool) {
8608                                                 free(current_mail_spool);
8609                                         }
8610                                         current_mail_spool = strndup(buffer, text_buffer_size);
8611                                 }
8612                         } else {
8613                                 CONF_ERR;
8614                         }
8615                 }
8616 #ifdef X11
8617                 CONF("minimum_size") {
8618                         if (value) {
8619                                 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8620                                                 != 2) {
8621                                         if (sscanf(value, "%d", &minimum_width) != 1) {
8622                                                 CONF_ERR;
8623                                         }
8624                                 }
8625                         } else {
8626                                 CONF_ERR;
8627                         }
8628                 }
8629                 CONF("maximum_width") {
8630                         if (value) {
8631                                 if (sscanf(value, "%d", &maximum_width) != 1) {
8632                                         CONF_ERR;
8633                                 }
8634                         } else {
8635                                 CONF_ERR;
8636                         }
8637                 }
8638 #endif /* X11 */
8639                 CONF("no_buffers") {
8640                         no_buffers = string_to_bool(value);
8641                 }
8642                 CONF("top_name_width") {
8643                         if (value) {
8644                                 if (sscanf(value, "%u", &top_name_width) != 1) {
8645                                         CONF_ERR;
8646                                 }
8647                         } else {
8648                                 CONF_ERR;
8649                         }
8650                         if (top_name_width >= max_user_text) {
8651                                 top_name_width = max_user_text - 1;
8652                         }
8653                 }
8654                 CONF("top_cpu_separate") {
8655                         cpu_separate = string_to_bool(value);
8656                 }
8657                 CONF("short_units") {
8658                         short_units = string_to_bool(value);
8659                 }
8660                 CONF("format_human_readable") {
8661                         format_human_readable = string_to_bool(value);
8662                 }
8663                 CONF("pad_percents") {
8664                         pad_percents = atoi(value);
8665                 }
8666 #ifdef X11
8667 #ifdef OWN_WINDOW
8668                 CONF("own_window") {
8669                         if (value) {
8670                                 own_window = string_to_bool(value);
8671                         }
8672                 }
8673                 CONF("own_window_class") {
8674                         if (value) {
8675                                 memset(window.class_name, 0, sizeof(window.class_name));
8676                                 strncpy(window.class_name, value,
8677                                                 sizeof(window.class_name) - 1);
8678                         }
8679                 }
8680                 CONF("own_window_title") {
8681                         if (value) {
8682                                 memset(window.title, 0, sizeof(window.title));
8683                                 strncpy(window.title, value, sizeof(window.title) - 1);
8684                         }
8685                 }
8686                 CONF("own_window_transparent") {
8687                         if (value) {
8688                                 set_transparent = string_to_bool(value);
8689                         }
8690                 }
8691                 CONF("own_window_hints") {
8692                         if (value) {
8693                                 char *p_hint, *p_save;
8694                                 char delim[] = ", ";
8695
8696                                 /* tokenize the value into individual hints */
8697                                 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8698                                         do {
8699                                                 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8700                                                 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8701                                                         SET_HINT(window.hints, HINT_UNDECORATED);
8702                                                 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8703                                                         SET_HINT(window.hints, HINT_BELOW);
8704                                                 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8705                                                         SET_HINT(window.hints, HINT_ABOVE);
8706                                                 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8707                                                         SET_HINT(window.hints, HINT_STICKY);
8708                                                 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8709                                                         SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8710                                                 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8711                                                         SET_HINT(window.hints, HINT_SKIP_PAGER);
8712                                                 } else {
8713                                                         CONF_ERR;
8714                                                 }
8715
8716                                                 p_hint = strtok_r(NULL, delim, &p_save);
8717                                         } while (p_hint != NULL);
8718                                 }
8719                         } else {
8720                                 CONF_ERR;
8721                         }
8722                 }
8723                 CONF("own_window_type") {
8724                         if (value) {
8725                                 if (strncmp(value, "normal", 6) == EQUAL) {
8726                                         window.type = TYPE_NORMAL;
8727                                 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8728                                         window.type = TYPE_DESKTOP;
8729                                 } else if (strncmp(value, "dock", 4) == EQUAL) {
8730                                         window.type = TYPE_DOCK;
8731                                         text_alignment = TOP_LEFT;
8732                                 } else if (strncmp(value, "panel", 5) == EQUAL) {
8733                                         window.type = TYPE_PANEL;
8734                                 } else if (strncmp(value, "override", 8) == EQUAL) {
8735                                         window.type = TYPE_OVERRIDE;
8736                                 } else {
8737                                         CONF_ERR;
8738                                 }
8739                         } else {
8740                                 CONF_ERR;
8741                         }
8742                 }
8743 #endif
8744                 CONF("stippled_borders") {
8745                         if (value) {
8746                                 stippled_borders = strtol(value, 0, 0);
8747                         } else {
8748                                 stippled_borders = 4;
8749                         }
8750                 }
8751 #ifdef IMLIB2
8752                 CONF("imlib_cache_size") {
8753                         if (value) {
8754                                 cimlib_set_cache_size(atoi(value));
8755                         }
8756                 }
8757                 CONF("imlib_cache_flush_interval") {
8758                         if (value) {
8759                                 cimlib_set_cache_flush_interval(atoi(value));
8760                         }
8761                 }
8762 #endif /* IMLIB2 */
8763 #endif /* X11 */
8764                 CONF("update_interval_on_battery") {
8765                         if (value) {
8766                                 update_interval_bat = strtod(value, 0);
8767                         } else {
8768                                 CONF_ERR;
8769                         }
8770                 }
8771                 CONF("update_interval") {
8772                         if (value) {
8773                                 set_update_interval(strtod(value, 0));
8774                         } else {
8775                                 CONF_ERR;
8776                         }
8777                         if (info.music_player_interval == 0) {
8778                                 // default to update_interval
8779                                 info.music_player_interval = update_interval;
8780                         }
8781                 }
8782                 CONF("total_run_times") {
8783                         if (value) {
8784                                 total_run_times = strtod(value, 0);
8785                         } else {
8786                                 CONF_ERR;
8787                         }
8788                 }
8789                 CONF("uppercase") {
8790                         stuff_in_uppercase = string_to_bool(value);
8791                 }
8792                 CONF("max_specials") {
8793                         if (value) {
8794                                 max_specials = atoi(value);
8795                         } else {
8796                                 CONF_ERR;
8797                         }
8798                 }
8799                 CONF("max_user_text") {
8800                         if (value) {
8801                                 max_user_text = atoi(value);
8802                         } else {
8803                                 CONF_ERR;
8804                         }
8805                 }
8806                 CONF("text_buffer_size") {
8807                         if (value) {
8808                                 text_buffer_size = atoi(value);
8809                                 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8810                                         NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8811                                         text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8812                                 }
8813                         } else {
8814                                 CONF_ERR;
8815                         }
8816                 }
8817                 CONF("text") {
8818 #ifdef X11
8819                         if (output_methods & TO_X) {
8820                                 X11_initialisation();
8821                         }
8822 #endif
8823
8824                         if (global_text) {
8825                                 free(global_text);
8826                                 global_text = 0;
8827                         }
8828
8829                         global_text = (char *) malloc(1);
8830                         global_text[0] = '\0';
8831
8832                         while (!feof(fp)) {
8833                                 unsigned int l = strlen(global_text);
8834                                 unsigned int bl;
8835                                 char buf[CONF_BUFF_SIZE];
8836
8837                                 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8838                                         break;
8839                                 }
8840
8841                                 /* Remove \\-\n. */
8842                                 bl = strlen(buf);
8843                                 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8844                                         buf[bl-2] = '\0';
8845                                         bl -= 2;
8846                                         if (bl == 0) {
8847                                                 continue;
8848                                         }
8849                                 }
8850
8851                                 /* Check for continuation of \\-\n. */
8852                                 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8853                                         global_text[l-1] = '\0';
8854                                         continue;
8855                                 }
8856
8857                                 global_text = (char *) realloc(global_text, l + bl + 1);
8858                                 strcat(global_text, buf);
8859
8860                                 if (strlen(global_text) > max_user_text) {
8861                                         break;
8862                                 }
8863                         }
8864                         fclose(fp);
8865                         if (strlen(global_text) < 1) {
8866                                 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
8867                         }
8868                         global_text_lines = line + 1;
8869                         return TRUE;
8870                 }
8871 #ifdef TCP_PORT_MONITOR
8872                 CONF("max_port_monitor_connections") {
8873                         int max;
8874                         if (!value || (sscanf(value, "%d", &max) != 1)) {
8875                                 /* an error. use default, warn and continue. */
8876                                 tcp_portmon_set_max_connections(0);
8877                                 CONF_ERR;
8878                         } else if (tcp_portmon_set_max_connections(max)) {
8879                                 /* max is < 0, default has been set*/
8880                                 CONF_ERR;
8881                         }
8882                 }
8883 #endif
8884                 CONF("if_up_strictness") {
8885                         if (!value) {
8886                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
8887                                 ifup_strictness = IFUP_UP;
8888                         } else if (strcasecmp(value, "up") == EQUAL) {
8889                                 ifup_strictness = IFUP_UP;
8890                         } else if (strcasecmp(value, "link") == EQUAL) {
8891                                 ifup_strictness = IFUP_LINK;
8892                         } else if (strcasecmp(value, "address") == EQUAL) {
8893                                 ifup_strictness = IFUP_ADDR;
8894                         } else {
8895                                 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
8896                                 ifup_strictness = IFUP_UP;
8897                         }
8898                 }
8899
8900                 CONF("temperature_unit") {
8901                         if (!value) {
8902                                 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8903                         } else if (set_temp_output_unit(value)) {
8904                                 NORM_ERR("temperature_unit: incorrect argument");
8905                         }
8906                 }
8907
8908 #ifdef HAVE_LUA
8909                 CONF("lua_load") {
8910                         if (value) {
8911                                 char *ptr = strtok(value, " ");
8912                                 while (ptr) {
8913                                         llua_load(ptr);
8914                                         ptr = strtok(NULL, " ");
8915                                 }
8916                         } else {
8917                                 CONF_ERR;
8918                         }
8919                 }
8920 #ifdef X11
8921                 CONF("lua_draw_hook_pre") {
8922                         if (value) {
8923                                 llua_set_draw_pre_hook(value);
8924                         } else {
8925                                 CONF_ERR;
8926                         }
8927                 }
8928                 CONF("lua_draw_hook_post") {
8929                         if (value) {
8930                                 llua_set_draw_post_hook(value);
8931                         } else {
8932                                 CONF_ERR;
8933                         }
8934                 }
8935                 CONF("lua_startup_hook") {
8936                         if (value) {
8937                                 llua_set_startup_hook(value);
8938                         } else {
8939                                 CONF_ERR;
8940                         }
8941                 }
8942                 CONF("lua_shutdown_hook") {
8943                         if (value) {
8944                                 llua_set_shutdown_hook(value);
8945                         } else {
8946                                 CONF_ERR;
8947                         }
8948                 }
8949 #endif /* X11 */
8950 #endif /* HAVE_LUA */
8951
8952                 CONF("color0"){}
8953                 CONF("color1"){}
8954                 CONF("color2"){}
8955                 CONF("color3"){}
8956                 CONF("color4"){}
8957                 CONF("color5"){}
8958                 CONF("color6"){}
8959                 CONF("color7"){}
8960                 CONF("color8"){}
8961                 CONF("color9"){}
8962                 CONF("default_color"){}
8963                 CONF3("default_shade_color", "default_shadecolor"){}
8964                 CONF3("default_outline_color", "default_outlinecolor") {}
8965                 CONF("own_window_colour") {}
8966
8967                 else {
8968                         NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
8969                 }
8970         }
8971
8972         fclose(fp);
8973
8974         if (info.music_player_interval == 0) {
8975                 // default to update_interval
8976                 info.music_player_interval = update_interval;
8977         }
8978         if (!global_text) { // didn't supply any text
8979                 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
8980         }
8981         return TRUE;
8982 }
8983
8984 #ifdef X11
8985 static void load_config_file_x11(const char *f)
8986 {
8987         int line = 0;
8988         FILE *fp;
8989
8990         fp = open_config_file(f);
8991         if (!fp) {
8992                 return;
8993         }
8994         DBGP("reading contents from config file '%s'", f);
8995
8996         while (!feof(fp)) {
8997                 char buff[CONF_BUFF_SIZE], *name, *value;
8998                 int ret = do_config_step(&line, fp, buff, &name, &value);
8999                 if (ret == CONF_BREAK) {
9000                         break;
9001                 } else if (ret == CONF_CONTINUE) {
9002                         continue;
9003                 }
9004
9005                 CONF2("color0") {
9006                         X11_initialisation();
9007                         if (x_initialised == YES) {
9008                                 if (value) {
9009                                         color0 = get_x11_color(value);
9010                                 } else {
9011                                         CONF_ERR;
9012                                 }
9013                         }
9014                 }
9015                 CONF("color1") {
9016                         X11_initialisation();
9017                         if (x_initialised == YES) {
9018                                 if (value) {
9019                                         color1 = get_x11_color(value);
9020                                 } else {
9021                                         CONF_ERR;
9022                                 }
9023                         }
9024                 }
9025                 CONF("color2") {
9026                         X11_initialisation();
9027                         if (x_initialised == YES) {
9028                                 if (value) {
9029                                         color2 = get_x11_color(value);
9030                                 } else {
9031                                         CONF_ERR;
9032                                 }
9033                         }
9034                 }
9035                 CONF("color3") {
9036                         X11_initialisation();
9037                         if (x_initialised == YES) {
9038                                 if (value) {
9039                                         color3 = get_x11_color(value);
9040                                 } else {
9041                                         CONF_ERR;
9042                                 }
9043                         }
9044                 }
9045                 CONF("color4") {
9046                         X11_initialisation();
9047                         if (x_initialised == YES) {
9048                                 if (value) {
9049                                         color4 = get_x11_color(value);
9050                                 } else {
9051                                         CONF_ERR;
9052                                 }
9053                         }
9054                 }
9055                 CONF("color5") {
9056                         X11_initialisation();
9057                         if (x_initialised == YES) {
9058                                 if (value) {
9059                                         color5 = get_x11_color(value);
9060                                 } else {
9061                                         CONF_ERR;
9062                                 }
9063                         }
9064                 }
9065                 CONF("color6") {
9066                         X11_initialisation();
9067                         if (x_initialised == YES) {
9068                                 if (value) {
9069                                         color6 = get_x11_color(value);
9070                                 } else {
9071                                         CONF_ERR;
9072                                 }
9073                         }
9074                 }
9075                 CONF("color7") {
9076                         X11_initialisation();
9077                         if (x_initialised == YES) {
9078                                 if (value) {
9079                                         color7 = get_x11_color(value);
9080                                 } else {
9081                                         CONF_ERR;
9082                                 }
9083                         }
9084                 }
9085                 CONF("color8") {
9086                         X11_initialisation();
9087                         if (x_initialised == YES) {
9088                                 if (value) {
9089                                         color8 = get_x11_color(value);
9090                                 } else {
9091                                         CONF_ERR;
9092                                 }
9093                         }
9094                 }
9095                 CONF("color9") {
9096                         X11_initialisation();
9097                         if (x_initialised == YES) {
9098                                 if (value) {
9099                                         color9 = get_x11_color(value);
9100                                 } else {
9101                                         CONF_ERR;
9102                                 }
9103                         }
9104                 }
9105                 CONF("default_color") {
9106                         X11_initialisation();
9107                         if (x_initialised == YES) {
9108                                 if (value) {
9109                                         default_fg_color = get_x11_color(value);
9110                                 } else {
9111                                         CONF_ERR;
9112                                 }
9113                         }
9114                 }
9115                 CONF3("default_shade_color", "default_shadecolor") {
9116                         X11_initialisation();
9117                         if (x_initialised == YES) {
9118                                 if (value) {
9119                                         default_bg_color = get_x11_color(value);
9120                                 } else {
9121                                         CONF_ERR;
9122                                 }
9123                         }
9124                 }
9125                 CONF3("default_outline_color", "default_outlinecolor") {
9126                         X11_initialisation();
9127                         if (x_initialised == YES) {
9128                                 if (value) {
9129                                         default_out_color = get_x11_color(value);
9130                                 } else {
9131                                         CONF_ERR;
9132                                 }
9133                         }
9134                 }
9135 #ifdef OWN_WINDOW
9136                 CONF("own_window_colour") {
9137                         X11_initialisation();
9138                         if (x_initialised == YES) {
9139                                 if (value) {
9140                                         background_colour = get_x11_color(value);
9141                                 } else {
9142                                         NORM_ERR("Invalid colour for own_window_colour (try omitting the "
9143                                                 "'#' for hex colours");
9144                                 }
9145                         }
9146                 }
9147 #endif
9148                 CONF("text") {
9149                         /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
9150                         if(output_methods & TO_X) {
9151                                 X11_initialisation();
9152                         }
9153                 }
9154 #undef CONF
9155 #undef CONF2
9156 #undef CONF3
9157 #undef CONF_ERR
9158 #undef CONF_ERR2
9159 #undef CONF_BREAK
9160 #undef CONF_CONTINUE
9161 #undef CONF_BUFF_SIZE
9162         }
9163
9164         fclose(fp);
9165
9166 }
9167 #endif /* X11 */
9168
9169 static void print_help(const char *prog_name) {
9170         printf("Usage: %s [OPTION]...\n"
9171                         PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
9172                         "window. Command line options will override configurations defined in config\n"
9173                         "file.\n"
9174                         "   -v, --version             version\n"
9175                         "   -q, --quiet               quiet mode\n"
9176                         "   -D, --debug               increase debugging output, ie. -DD for more debugging\n"
9177                         "   -c, --config=FILE         config file to load\n"
9178 #ifdef CONFIG_OUTPUT
9179                         "   -C, --print-config        print the builtin default config to stdout\n"
9180                         "                             e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
9181 #endif
9182                         "   -d, --daemonize           daemonize, fork to background\n"
9183                         "   -h, --help                help\n"
9184 #ifdef X11
9185                         "   -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
9186                         "   -f, --font=FONT           font to use\n"
9187                         "   -X, --display=DISPLAY     X11 display to use\n"
9188 #ifdef OWN_WINDOW
9189                         "   -o, --own-window          create own window to draw\n"
9190 #endif
9191 #ifdef HAVE_XDBE
9192                         "   -b, --double-buffer       double buffer (prevents flickering)\n"
9193 #endif
9194                         "   -w, --window-id=WIN_ID    window id to draw\n"
9195                         "   -x X                      x position\n"
9196                         "   -y Y                      y position\n"
9197 #endif /* X11 */
9198                         "   -t, --text=TEXT           text to render, remember single quotes, like -t '$uptime'\n"
9199                         "   -u, --interval=SECS       update interval\n"
9200                         "   -i COUNT                  number of times to update "PACKAGE_NAME" (and quit)\n",
9201                         prog_name
9202         );
9203 }
9204
9205 /* : means that character before that takes an argument */
9206 static const char *getopt_string = "vVqdDt:u:i:hc:"
9207 #ifdef X11
9208         "x:y:w:a:f:X:"
9209 #ifdef OWN_WINDOW
9210         "o"
9211 #endif
9212 #ifdef HAVE_XDBE
9213         "b"
9214 #endif
9215 #endif /* X11 */
9216 #ifdef CONFIG_OUTPUT
9217         "C"
9218 #endif
9219         ;
9220
9221 static const struct option longopts[] = {
9222         { "help", 0, NULL, 'h' },
9223         { "version", 0, NULL, 'V' },
9224         { "debug", 0, NULL, 'D' },
9225         { "config", 1, NULL, 'c' },
9226 #ifdef CONFIG_OUTPUT
9227         { "print-config", 0, NULL, 'C' },
9228 #endif
9229         { "daemonize", 0, NULL, 'd' },
9230 #ifdef X11
9231         { "alignment", 1, NULL, 'a' },
9232         { "font", 1, NULL, 'f' },
9233         { "display", 1, NULL, 'X' },
9234 #ifdef OWN_WINDOW
9235         { "own-window", 0, NULL, 'o' },
9236 #endif
9237 #ifdef HAVE_XDBE
9238         { "double-buffer", 0, NULL, 'b' },
9239 #endif
9240         { "window-id", 1, NULL, 'w' },
9241 #endif /* X11 */
9242         { "text", 1, NULL, 't' },
9243         { "interval", 0, NULL, 'u' },
9244         { 0, 0, 0, 0 }
9245 };
9246
9247 void initialisation(int argc, char **argv) {
9248         struct sigaction act, oact;
9249
9250         set_default_configurations();
9251         load_config_file(current_config);
9252         currentconffile = conftree_add(currentconffile, current_config);
9253
9254         /* init specials array */
9255         if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9256                 NORM_ERR("failed to create specials array");
9257         }
9258
9259 #ifdef MAIL_FILE
9260         if (current_mail_spool == NULL) {
9261                 char buf[256];
9262
9263                 variable_substitute(MAIL_FILE, buf, 256);
9264
9265                 if (buf[0] != '\0') {
9266                         current_mail_spool = strndup(buf, text_buffer_size);
9267                 }
9268         }
9269 #endif
9270
9271         /* handle other command line arguments */
9272
9273 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9274                 || defined(__NetBSD__)
9275         optind = optreset = 1;
9276 #else
9277         optind = 0;
9278 #endif
9279
9280 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9281         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9282                         "kvm_open")) == NULL) {
9283                 CRIT_ERR(NULL, NULL, "cannot read kvm");
9284         }
9285 #endif
9286
9287         while (1) {
9288                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9289
9290                 if (c == -1) {
9291                         break;
9292                 }
9293
9294                 switch (c) {
9295                         case 'd':
9296                                 fork_to_background = 1;
9297                                 break;
9298                         case 'D':
9299                                 global_debug_level++;
9300                                 break;
9301 #ifdef X11
9302                         case 'f':
9303                                 set_first_font(optarg);
9304                                 break;
9305                         case 'a':
9306                                 text_alignment = string_to_alignment(optarg);
9307                                 break;
9308
9309 #ifdef OWN_WINDOW
9310                         case 'o':
9311                                 own_window = 1;
9312                                 break;
9313 #endif
9314 #ifdef HAVE_XDBE
9315                         case 'b':
9316                                 use_xdbe = 1;
9317                                 break;
9318 #endif
9319 #endif /* X11 */
9320                         case 't':
9321                                 if (global_text) {
9322                                         free(global_text);
9323                                         global_text = 0;
9324                                 }
9325                                 global_text = strndup(optarg, max_user_text);
9326                                 convert_escapes(global_text);
9327                                 break;
9328
9329                         case 'u':
9330                                 update_interval = strtod(optarg, 0);
9331                                 update_interval_old = update_interval;
9332                                 if (info.music_player_interval == 0) {
9333                                         // default to update_interval
9334                                         info.music_player_interval = update_interval;
9335                                 }
9336                                 break;
9337
9338                         case 'i':
9339                                 total_run_times = strtod(optarg, 0);
9340                                 break;
9341 #ifdef X11
9342                         case 'x':
9343                                 gap_x = atoi(optarg);
9344                                 break;
9345
9346                         case 'y':
9347                                 gap_y = atoi(optarg);
9348                                 break;
9349 #endif /* X11 */
9350
9351                         case '?':
9352                                 exit(EXIT_FAILURE);
9353                 }
9354         }
9355
9356 #ifdef X11
9357         /* load font */
9358         if (output_methods & TO_X) {
9359                 load_config_file_x11(current_config);
9360         }
9361 #endif /* X11 */
9362
9363         /* generate text and get initial size */
9364         extract_variable_text(global_text);
9365         if (global_text) {
9366                 free(global_text);
9367                 global_text = 0;
9368         }
9369         global_text = NULL;
9370         /* fork */
9371         if (fork_to_background) {
9372                 int pid = fork();
9373
9374                 switch (pid) {
9375                         case -1:
9376                                 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9377                                         strerror(errno));
9378                                 break;
9379
9380                         case 0:
9381                                 /* child process */
9382                                 usleep(25000);
9383                                 fprintf(stderr, "\n");
9384                                 fflush(stderr);
9385                                 break;
9386
9387                         default:
9388                                 /* parent process */
9389                                 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9390                                         pid);
9391                                 fflush(stderr);
9392                                 exit(EXIT_SUCCESS);
9393                 }
9394         }
9395
9396         text_buffer = malloc(max_user_text);
9397         memset(text_buffer, 0, max_user_text);
9398         tmpstring1 = malloc(text_buffer_size);
9399         memset(tmpstring1, 0, text_buffer_size);
9400         tmpstring2 = malloc(text_buffer_size);
9401         memset(tmpstring2, 0, text_buffer_size);
9402
9403 #ifdef X11
9404         xargc = argc;
9405         xargv = argv;
9406         X11_create_window();
9407 #endif /* X11 */
9408 #ifdef HAVE_LUA
9409         llua_setup_info(&info, update_interval);
9410 #endif /* HAVE_LUA */
9411
9412         /* Set signal handlers */
9413         act.sa_handler = signal_handler;
9414         sigemptyset(&act.sa_mask);
9415         act.sa_flags = 0;
9416 #ifdef SA_RESTART
9417         act.sa_flags |= SA_RESTART;
9418 #endif
9419
9420         if (            sigaction(SIGINT,  &act, &oact) < 0
9421                         ||      sigaction(SIGALRM, &act, &oact) < 0
9422                         ||      sigaction(SIGUSR1, &act, &oact) < 0
9423                         ||      sigaction(SIGHUP,  &act, &oact) < 0
9424                         ||      sigaction(SIGTERM, &act, &oact) < 0) {
9425                 NORM_ERR("error setting signal handler: %s", strerror(errno));
9426         }
9427
9428 #ifdef HAVE_LUA
9429         llua_startup_hook();
9430 #endif /* HAVE_LUA */
9431 }
9432
9433 int main(int argc, char **argv)
9434 {
9435 #ifdef X11
9436         char *s, *temp;
9437         unsigned int x;
9438 #endif
9439
9440         argc_copy = argc;
9441         argv_copy = argv;
9442         g_signal_pending = 0;
9443         max_user_text = MAX_USER_TEXT_DEFAULT;
9444         current_config = 0;
9445         memset(&info, 0, sizeof(info));
9446         memset(template, 0, sizeof(template));
9447         clear_net_stats();
9448
9449 #ifdef TCP_PORT_MONITOR
9450         /* set default connection limit */
9451         tcp_portmon_set_max_connections(0);
9452 #endif
9453
9454         /* handle command line parameters that don't change configs */
9455 #ifdef X11
9456         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9457                         || ((s = getenv("LANG")) && *s)) {
9458                 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9459                 if (temp == NULL) {
9460                         NORM_ERR("malloc failed");
9461                 }
9462                 for (x = 0; x < strlen(s); x++) {
9463                         temp[x] = tolower(s[x]);
9464                 }
9465                 temp[x] = 0;
9466                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9467                         utf8_mode = 1;
9468                 }
9469
9470                 free(temp);
9471         }
9472         if (!setlocale(LC_CTYPE, "")) {
9473                 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9474         }
9475 #endif /* X11 */
9476         while (1) {
9477                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9478
9479                 if (c == -1) {
9480                         break;
9481                 }
9482
9483                 switch (c) {
9484                         case 'v':
9485                         case 'V':
9486                                 print_version();
9487                         case 'c':
9488                                 if (current_config) {
9489                                         free(current_config);
9490                                 }
9491                                 current_config = strndup(optarg, max_user_text);
9492                                 break;
9493                         case 'q':
9494                                 freopen("/dev/null", "w", stderr);
9495                                 break;
9496                         case 'h':
9497                                 print_help(argv[0]);
9498                                 return 0;
9499 #ifdef CONFIG_OUTPUT
9500                         case 'C':
9501                                 print_defconfig();
9502                                 return 0;
9503 #endif
9504 #ifdef X11
9505                         case 'w':
9506                                 window.window = strtol(optarg, 0, 0);
9507                                 break;
9508                         case 'X':
9509                                 if (disp)
9510                                         free(disp);
9511                                 disp = strdup(optarg);
9512                                 break;
9513 #endif /* X11 */
9514
9515                         case '?':
9516                                 exit(EXIT_FAILURE);
9517                 }
9518         }
9519
9520         /* check if specified config file is valid */
9521         if (current_config) {
9522                 struct stat sb;
9523                 if (stat(current_config, &sb) ||
9524                                 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9525                         NORM_ERR("invalid configuration file '%s'\n", current_config);
9526                         free(current_config);
9527                         current_config = 0;
9528                 }
9529         }
9530
9531         /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9532
9533         if (!current_config) {
9534                 /* load default config file */
9535                 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9536                 FILE *fp;
9537
9538                 /* Try to use personal config file first */
9539                 to_real_path(buf, CONFIG_FILE);
9540                 if (buf[0] && (fp = fopen(buf, "r"))) {
9541                         current_config = strndup(buf, max_user_text);
9542                         fclose(fp);
9543                 }
9544
9545                 /* Try to use system config file if personal config not readable */
9546                 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9547                         current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9548                         fclose(fp);
9549                 }
9550
9551                 /* No readable config found */
9552                 if (!current_config) {
9553 #ifdef CONFIG_OUTPUT
9554                         current_config = strdup("==builtin==");
9555                         NORM_ERR("no readable personal or system-wide config file found,"
9556                                         " using builtin default");
9557 #else
9558                         CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
9559 #endif /* ! CONF_OUTPUT */
9560                 }
9561         }
9562
9563 #ifdef XOAP
9564         /* Load xoap keys, if existing */
9565         load_xoap_keys();
9566 #endif /* XOAP */
9567
9568 #ifdef HAVE_SYS_INOTIFY_H
9569         inotify_fd = inotify_init();
9570 #endif /* HAVE_SYS_INOTIFY_H */
9571
9572         initialisation(argc, argv);
9573
9574         main_loop();
9575
9576 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9577         kvm_close(kd);
9578 #endif
9579
9580         return 0;
9581
9582 }
9583
9584 void alarm_handler(void) {
9585         if(childpid > 0) {
9586                 kill(childpid, SIGTERM);
9587         }
9588 }
9589
9590 static void signal_handler(int sig)
9591 {
9592         /* signal handler is light as a feather, as it should be.
9593          * we will poll g_signal_pending with each loop of conky
9594          * and do any signal processing there, NOT here (except 
9595          * SIGALRM because this is caused when conky is hanging) */
9596         if(sig == SIGALRM) {
9597                 alarm_handler();
9598         } else {
9599                 g_signal_pending = sig;
9600         }
9601 }