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