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