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