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