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