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