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