add emacs indentation variables to source files in line with current vim settings
[monky] / src / conky.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "config.h"
33 #include "text_object.h"
34 #include "conky.h"
35 #include "common.h"
36 #include <stdarg.h>
37 #include <math.h>
38 #include <ctype.h>
39 #include <time.h>
40 #include <locale.h>
41 #include <signal.h>
42 #include <errno.h>
43 #include <limits.h>
44 #if HAVE_DIRENT_H
45 #include <dirent.h>
46 #endif
47 #include <sys/time.h>
48 #include <sys/param.h>
49 #ifdef HAVE_SYS_INOTIFY_H
50 #include <sys/inotify.h>
51 #endif /* HAVE_SYS_INOTIFY_H */
52 #ifdef X11
53 #include "x11.h"
54 #include <X11/Xutil.h>
55 #ifdef HAVE_XDAMAGE
56 #include <X11/extensions/Xdamage.h>
57 #endif
58 #ifdef IMLIB2
59 #include "imlib2.h"
60 #endif /* IMLIB2 */
61 #endif /* X11 */
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <netinet/in.h>
65 #include <netdb.h>
66 #include <fcntl.h>
67 #include <getopt.h>
68
69 /* local headers */
70 #include "algebra.h"
71 #include "build.h"
72 #include "colours.h"
73 #include "diskio.h"
74 #ifdef X11
75 #include "fonts.h"
76 #endif
77 #include "fs.h"
78 #include "logging.h"
79 #include "mixer.h"
80 #include "mail.h"
81 #include "mboxscan.h"
82 #include "specials.h"
83 #include "temphelper.h"
84 #include "tailhead.h"
85 #include "top.h"
86
87 /* check for OS and include appropriate headers */
88 #if defined(__linux__)
89 #include "linux.h"
90 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
91 #include "freebsd.h"
92 #elif defined(__OpenBSD__)
93 #include "openbsd.h"
94 #endif
95
96 #if defined(__FreeBSD_kernel__)
97 #include <bsd/bsd.h>
98 #endif
99
100 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
101 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
102                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
103 int apm_getinfo(int fd, apm_info_t aip);
104 char *get_apm_adapter(void);
105 char *get_apm_battery_life(void);
106 char *get_apm_battery_time(void);
107 #endif
108
109 #ifdef HAVE_ICONV
110 #include <iconv.h>
111 #endif
112
113 #ifdef CONFIG_OUTPUT
114 #include "defconfig.h"
115 #include "conf_cookie.h"
116 #endif
117
118 #ifndef S_ISSOCK
119 #define S_ISSOCK(x)   ((x & S_IFMT) == S_IFSOCK)
120 #endif
121
122 #define MAIL_FILE "$MAIL"
123 #define MAX_IF_BLOCK_DEPTH 5
124
125 //#define SIGNAL_BLOCKING
126 #undef SIGNAL_BLOCKING
127
128 /* debugging level, used by logging.h */
129 int global_debug_level = 0;
130
131 /* two strings for internal use */
132 static char *tmpstring1, *tmpstring2;
133
134 /* variables holding various config settings */
135 int short_units;
136 int format_human_readable;
137 int cpu_separate;
138 enum {
139         NO_SPACER = 0,
140         LEFT_SPACER,
141         RIGHT_SPACER
142 } use_spacer;
143 int top_cpu, top_mem, top_time;
144 #ifdef IOSTATS
145 int top_io;
146 #endif
147 static unsigned int top_name_width = 15;
148 int output_methods;
149 static int extra_newline;
150 enum x_initialiser_state x_initialised = NO;
151 static volatile int g_signal_pending;
152 /* Update interval */
153 double update_interval;
154 double update_interval_old;
155 double update_interval_bat;
156 void *global_cpu = NULL;
157 pid_t childpid = 0;
158
159 int argc_copy;
160 char** argv_copy;
161
162 /* prototypes for internally used functions */
163 static void signal_handler(int);
164 static void print_version(void) __attribute__((noreturn));
165 static void reload_config(void);
166 static void generate_text_internal(char *, int, struct text_object,
167                                    struct information *);
168 static int extract_variable_text_internal(struct text_object *,
169                                           const char *);
170
171 static void print_version(void)
172 {
173         printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
174
175         printf("\nCompiled in features:\n\n"
176                    "System config file: "SYSTEM_CONFIG_FILE"\n"
177                    "Package library path: "PACKAGE_LIBDIR"\n\n"
178 #ifdef X11
179                    " X11:\n"
180 # ifdef HAVE_XDAMAGE
181                    "  * Xdamage extension\n"
182 # endif /* HAVE_XDAMAGE */
183 # ifdef HAVE_XDBE
184                    "  * XDBE (double buffer extension)\n"
185 # endif /* HAVE_XDBE */
186 # ifdef XFT
187                    "  * Xft\n"
188 # endif /* XFT */
189 #endif /* X11 */
190                    "\n Music detection:\n"
191 #ifdef AUDACIOUS
192                    "  * Audacious\n"
193 #endif /* AUDACIOUS */
194 #ifdef BMPX
195                    "  * BMPx\n"
196 #endif /* BMPX */
197 #ifdef MPD
198                    "  * MPD\n"
199 #endif /* MPD */
200 #ifdef MOC
201                    "  * MOC\n"
202 #endif /* MOC */
203 #ifdef XMMS2
204                    "  * XMMS2\n"
205 #endif /* XMMS2 */
206                    "\n General:\n"
207 #ifdef HAVE_OPENMP
208                    "  * OpenMP\n"
209 #endif /* HAVE_OPENMP */
210 #ifdef MATH
211                    "  * math\n"
212 #endif /* Math */
213 #ifdef HDDTEMP
214                    "  * hddtemp\n"
215 #endif /* HDDTEMP */
216 #ifdef TCP_PORT_MONITOR
217                    "  * portmon\n"
218 #endif /* TCP_PORT_MONITOR */
219 #ifdef HAVE_CURL
220                    "  * Curl\n"
221 #endif /* HAVE_CURL */
222 #ifdef RSS
223                    "  * RSS\n"
224 #endif /* RSS */
225 #ifdef WEATHER
226                    "  * Weather (METAR)\n"
227 #ifdef XOAP
228                    "  * Weather (XOAP)\n"
229 #endif /* XOAP */
230 #endif /* WEATHER */
231 #ifdef HAVE_IWLIB
232                    "  * wireless\n"
233 #endif /* HAVE_IWLIB */
234 #ifdef IBM
235                    "  * support for IBM/Lenovo notebooks\n"
236 #endif /* IBM */
237 #ifdef NVIDIA
238                    "  * nvidia\n"
239 #endif /* NVIDIA */
240 #ifdef EVE
241                    "  * eve-online\n"
242 #endif /* EVE */
243 #ifdef CONFIG_OUTPUT
244                    "  * config-output\n"
245 #endif /* CONFIG_OUTPUT */
246 #ifdef IMLIB2
247                    "  * Imlib2\n"
248 #endif /* IMLIB2 */
249 #ifdef MIXER_IS_ALSA
250                    "  * ALSA mixer support\n"
251 #endif /* MIXER_IS_ALSA */
252 #ifdef APCUPSD
253                    "  * apcupsd\n"
254 #endif /* APCUPSD */
255 #ifdef IOSTATS
256                    "  * iostats\n"
257 #endif /* IOSTATS */
258 #ifdef HAVE_LUA
259                    "  * Lua\n"
260                    "\n  Lua bindings:\n"
261 #ifdef HAVE_LUA_CAIRO
262                    "   * Cairo\n"
263 #endif /* HAVE_LUA_CAIRO */
264 #ifdef HAVE_LUA_IMLIB2
265                    "   * Imlib2\n"
266 #endif /* IMLIB2 */
267 #endif /* HAVE_LUA */
268         );
269
270         exit(EXIT_SUCCESS);
271 }
272
273 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
274
275
276 #ifdef X11
277
278 static void X11_create_window(void);
279 static void X11_initialisation(void);
280
281 struct _x11_stuff_s {
282         Region region;
283 #ifdef HAVE_XDAMAGE
284         Damage damage;
285         XserverRegion region2, part;
286         int event_base, error_base;
287 #endif
288 } x11_stuff;
289
290 /* text size */
291
292 static int text_start_x, text_start_y;  /* text start position in window */
293 static int text_width, text_height;
294
295 /* alignments */
296 enum alignment {
297         TOP_LEFT = 1,
298         TOP_RIGHT,
299         TOP_MIDDLE,
300         BOTTOM_LEFT,
301         BOTTOM_RIGHT,
302         BOTTOM_MIDDLE,
303         MIDDLE_LEFT,
304         MIDDLE_RIGHT,
305         NONE
306 };
307
308 /* display to connect to */
309 static char *disp = NULL;
310
311 #endif /* X11 */
312
313 /* struct that has all info to be shared between
314  * instances of the same text object */
315 struct information info;
316
317 /* path to config file */
318 char *current_config;
319
320 /* set to 1 if you want all text to be in uppercase */
321 static unsigned int stuff_in_uppercase;
322
323 /* Run how many times? */
324 static unsigned long total_run_times;
325
326 /* fork? */
327 static int fork_to_background;
328
329 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
330
331 /* filenames for output */
332 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
333 char *append_file = NULL; FILE *append_fpointer = NULL;
334
335 #ifdef X11
336
337 static int show_graph_scale;
338 static int show_graph_range;
339
340 /* Position on the screen */
341 static int text_alignment;
342 static int gap_x, gap_y;
343
344 /* border */
345 static int draw_borders;
346 static int draw_graph_borders;
347 static int stippled_borders;
348
349 static int draw_shades, draw_outline;
350
351 static long default_fg_color, default_bg_color, default_out_color;
352
353 /* create own window or draw stuff to root? */
354 static int set_transparent = 0;
355
356 #ifdef OWN_WINDOW
357 static int own_window = 0;
358 static int background_colour = 0;
359
360 /* fixed size/pos is set if wm/user changes them */
361 static int fixed_size = 0, fixed_pos = 0;
362 #endif
363
364 static int minimum_width, minimum_height;
365 static int maximum_width;
366
367 #endif /* X11 */
368
369 #ifdef __OpenBSD__
370 static int sensor_device;
371 #endif
372
373 static long color0, color1, color2, color3, color4, color5, color6, color7,
374         color8, color9;
375
376 #define MAX_TEMPLATES 10
377 static char *template[MAX_TEMPLATES];
378
379 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
380 unsigned int max_user_text;
381
382 /* maximum size of individual text buffers, ie $exec buffer size */
383 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
384
385 #ifdef HAVE_ICONV
386 #define CODEPAGE_LENGTH 20
387 long iconv_selected;
388 long iconv_count = 0;
389 char iconv_converting;
390 static iconv_t **iconv_cd = 0;
391
392 int register_iconv(iconv_t *new_iconv)
393 {
394         iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
395         if (!iconv_cd) {
396                 CRIT_ERR(NULL, NULL, "Out of memory");
397         }
398         iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
399         if (!iconv_cd[iconv_count]) {
400                 CRIT_ERR(NULL, NULL, "Out of memory");
401         }
402         memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
403         iconv_count++;
404         return iconv_count;
405 }
406
407 void free_iconv(void)
408 {
409         if (iconv_cd) {
410                 long i;
411
412                 for (i = 0; i < iconv_count; i++) {
413                         if (iconv_cd[i]) {
414                                 iconv_close(*iconv_cd[i]);
415                                 free(iconv_cd[i]);
416                         }
417                 }
418                 free(iconv_cd);
419         }
420         iconv_cd = 0;
421 }
422
423 #endif
424
425 /* UTF-8 */
426 int utf8_mode = 0;
427
428 /* no buffers in used memory? */
429 int no_buffers;
430
431 /* pad percentages to decimals? */
432 static int pad_percents = 0;
433
434 static char *global_text = 0;
435 long global_text_lines;
436
437 static int total_updates;
438 static int updatereset;
439
440 int check_contains(char *f, char *s)
441 {
442         int ret = 0;
443         FILE *where = open_file(f, 0);
444
445         if (where) {
446                 char buf1[256];
447
448                 while (fgets(buf1, 256, where)) {
449                         if (strstr(buf1, s)) {
450                                 ret = 1;
451                                 break;
452                         }
453                 }
454                 fclose(where);
455         } else {
456                 ERR("Could not open the file");
457         }
458         return ret;
459 }
460
461 #define SECRIT_MULTILINE_CHAR '\x02'
462
463 #ifdef X11
464
465 static inline int calc_text_width(const char *s, int l)
466 {
467         if ((output_methods & TO_X) == 0) {
468                 return 0;
469         }
470 #ifdef XFT
471         if (use_xft) {
472                 XGlyphInfo gi;
473
474                 if (utf8_mode) {
475                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
476                                 (const FcChar8 *) s, l, &gi);
477                 } else {
478                         XftTextExtents8(display, fonts[selected_font].xftfont,
479                                 (const FcChar8 *) s, l, &gi);
480                 }
481                 return gi.xOff;
482         } else
483 #endif
484         {
485                 return XTextWidth(fonts[selected_font].font, s, l);
486         }
487 }
488 #endif /* X11 */
489
490 /* formatted text to render on screen, generated in generate_text(),
491  * drawn in draw_stuff() */
492
493 static char *text_buffer;
494
495 /* quite boring functions */
496
497 static inline void for_each_line(char *b, int f(char *, int))
498 {
499         char *ps, *pe;
500         int special_index = 0; /* specials index */
501
502         for (ps = b, pe = b; *pe; pe++) {
503                 if (*pe == '\n') {
504                         *pe = '\0';
505                         special_index = f(ps, special_index);
506                         *pe = '\n';
507                         ps = pe + 1;
508                 }
509         }
510
511         if (ps < pe) {
512                 f(ps, special_index);
513         }
514 }
515
516 static void convert_escapes(char *buf)
517 {
518         char *p = buf, *s = buf;
519
520         while (*s) {
521                 if (*s == '\\') {
522                         s++;
523                         if (*s == 'n') {
524                                 *p++ = '\n';
525                         } else if (*s == '\\') {
526                                 *p++ = '\\';
527                         }
528                         s++;
529                 } else {
530                         *p++ = *s++;
531                 }
532         }
533         *p = '\0';
534 }
535
536 /* Prints anything normally printed with snprintf according to the current value
537  * of use_spacer.  Actually slightly more flexible than snprintf, as you can
538  * safely specify the destination buffer as one of your inputs.  */
539 int spaced_print(char *buf, int size, const char *format, int width, ...)
540 {
541         int len = 0;
542         va_list argp;
543         char *tempbuf;
544
545         if (size < 1) {
546                 return 0;
547         }
548         tempbuf = malloc(size * sizeof(char));
549
550         // Passes the varargs along to vsnprintf
551         va_start(argp, width);
552         vsnprintf(tempbuf, size, format, argp);
553         va_end(argp);
554
555         switch (use_spacer) {
556                 case NO_SPACER:
557                         len = snprintf(buf, size, "%s", tempbuf);
558                         break;
559                 case LEFT_SPACER:
560                         len = snprintf(buf, size, "%*s", width, tempbuf);
561                         break;
562                 case RIGHT_SPACER:
563                         len = snprintf(buf, size, "%-*s", width, tempbuf);
564                         break;
565         }
566         free(tempbuf);
567         return len;
568 }
569
570 /* print percentage values
571  *
572  * - i.e., unsigned values between 0 and 100
573  * - respect the value of pad_percents */
574 static int percent_print(char *buf, int size, unsigned value)
575 {
576         return spaced_print(buf, size, "%u", pad_percents, value);
577 }
578
579 /* converts from bytes to human readable format (K, M, G, T)
580  *
581  * The algorithm always divides by 1024, as unit-conversion of byte
582  * counts suggests. But for output length determination we need to
583  * compare with 1000 here, as we print in decimal form. */
584 static void human_readable(long long num, char *buf, int size)
585 {
586         const char **suffix = suffixes;
587         float fnum;
588         int precision;
589         int width;
590         const char *format;
591
592         /* Possibly just output as usual, for example for stdout usage */
593         if (!format_human_readable) {
594                 spaced_print(buf, size, "%d", 6, round_to_int(num));
595                 return;
596         }
597         if (short_units) {
598                 width = 5;
599                 format = "%.*f%.1s";
600         } else {
601                 width = 7;
602                 format = "%.*f%-3s";
603         }
604
605         if (llabs(num) < 1000LL) {
606                 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
607                 return;
608         }
609
610         while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
611                 num /= 1024;
612                 suffix++;
613         }
614
615         suffix++;
616         fnum = num / 1024.0;
617
618         /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
619          *
620          * The goal is to always have a significance of 3, by
621          * adjusting the decimal part of the number. Sample output:
622          *  123MiB
623          * 23.4GiB
624          * 5.12B   
625          * so the point of alignment resides between number and unit. The
626          * upside of this is that there is minimal padding necessary, though
627          * there should be a way to make alignment take place at the decimal
628          * dot (then with fixed width decimal part). 
629          *
630          * Note the repdigits below: when given a precision value, printf()
631          * rounds the float to it, not just cuts off the remaining digits. So
632          * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
633          * printed with a precision of 0. Yay. */
634
635         precision = 0;          /* print 100-999 without decimal part */
636         if (fnum < 99.95)
637                 precision = 1;  /* print 10-99 with one decimal place */
638         if (fnum < 9.995)
639                 precision = 2;  /* print 0-9 with two decimal places */
640
641         spaced_print(buf, size, format, width, precision, fnum, *suffix);
642 }
643
644 /* global object list root element */
645 static struct text_object global_root_object;
646
647 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
648 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
649 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
650 FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
651         int ends[2];
652         int parentend, childend;
653
654         //by running pipe after the strcmp's we make sure that we don't have to create a pipe
655         //and close the ends if mode is something illegal
656         if(strcmp(mode, "r") == 0) {
657                 if(pipe(ends) != 0) {
658                         return NULL;
659                 }
660                 parentend = ends[0];
661                 childend = ends[1];
662         } else if(strcmp(mode, "w") == 0) {
663                 if(pipe(ends) != 0) {
664                         return NULL;
665                 }
666                 parentend = ends[1];
667                 childend = ends[0];
668         } else {
669                 return NULL;
670         }
671         *child = fork();
672         if(*child == -1) {
673                 close(parentend);
674                 close(childend);
675                 return NULL;
676         } else if(*child > 0) {
677                 close(childend);
678         } else {
679                 //don't read from both stdin and pipe or write to both stdout and pipe
680                 if(childend == ends[0]) {
681                         close(0);
682                 } else {
683                         close(1);
684                 }
685                 dup(childend);  //by dupping childend, the returned fd will have close-on-exec turned off
686                 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
687                 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail) 
688         }
689         return fdopen(parentend, mode);
690 }
691
692 static inline void read_exec(const char *data, char *buf, const int size)
693 {
694         FILE *fp = pid_popen(data, "r", &childpid);
695         if(fp) {
696                 int length;
697
698                 alarm(update_interval);
699                 length = fread(buf, 1, size, fp);
700                 pclose(fp);
701                 alarm(0);
702                 buf[length] = '\0';
703                 if (length > 0 && buf[length - 1] == '\n') {
704                         buf[length - 1] = '\0';
705                 }
706         } else {
707                 buf[0] = '\0';
708         }
709 }
710
711 void *threaded_exec(void *) __attribute__((noreturn));
712
713 void *threaded_exec(void *arg)
714 {
715         char *buff, *p2;
716         struct text_object *obj = (struct text_object *)arg;
717
718         while (1) {
719                 buff = malloc(text_buffer_size);
720                 read_exec(obj->data.texeci.cmd, buff,
721                         text_buffer_size);
722                 p2 = buff;
723                 while (*p2) {
724                         if (*p2 == '\001') {
725                                 *p2 = ' ';
726                         }
727                         p2++;
728                 }
729                 timed_thread_lock(obj->data.texeci.p_timed_thread);
730                 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
731                 timed_thread_unlock(obj->data.texeci.p_timed_thread);
732                 free(buff);
733                 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
734                         timed_thread_exit(obj->data.texeci.p_timed_thread);
735                 }
736         }
737         /* never reached */
738 }
739
740 static struct text_object *new_text_object_internal(void)
741 {
742         struct text_object *obj = malloc(sizeof(struct text_object));
743         memset(obj, 0, sizeof(struct text_object));
744         return obj;
745 }
746
747 /*
748  * Frees the list of text objects root points to.  When internal = 1, it won't
749  * free global objects.
750  */
751 static void free_text_objects(struct text_object *root, int internal)
752 {
753         struct text_object *obj;
754
755         if (!root->prev) {
756                 return;
757         }
758
759 #define data obj->data
760         for (obj = root->prev; obj; obj = root->prev) {
761                 root->prev = obj->prev;
762                 switch (obj->type) {
763 #ifndef __OpenBSD__
764                         case OBJ_acpitemp:
765                                 close(data.i);
766                                 break;
767 #endif /* !__OpenBSD__ */
768 #ifdef __linux__
769                         case OBJ_i2c:
770                         case OBJ_platform:
771                         case OBJ_hwmon:
772                                 close(data.sysfs.fd);
773                                 break;
774 #endif /* __linux__ */
775                         case OBJ_read_tcp:
776                                 free(data.read_tcp.host);
777                                 break;
778                         case OBJ_time:
779                         case OBJ_utime:
780                                 free(data.s);
781                                 break;
782                         case OBJ_tztime:
783                                 free(data.tztime.tz);
784                                 free(data.tztime.fmt);
785                                 break;
786                         case OBJ_mboxscan:
787                                 free(data.mboxscan.args);
788                                 free(data.mboxscan.output);
789                                 break;
790                         case OBJ_mails:
791                         case OBJ_new_mails:
792                         case OBJ_seen_mails:
793                         case OBJ_unseen_mails:
794                         case OBJ_flagged_mails:
795                         case OBJ_unflagged_mails:
796                         case OBJ_forwarded_mails:
797                         case OBJ_unforwarded_mails:
798                         case OBJ_replied_mails:
799                         case OBJ_unreplied_mails:
800                         case OBJ_draft_mails:
801                         case OBJ_trashed_mails:
802                                 free(data.local_mail.box);
803                                 break;
804                         case OBJ_imap_unseen:
805                                 if (!obj->char_b) {
806                                         free(data.mail);
807                                 }
808                                 break;
809                         case OBJ_imap_messages:
810                                 if (!obj->char_b) {
811                                         free(data.mail);
812                                 }
813                                 break;
814                         case OBJ_pop3_unseen:
815                                 if (!obj->char_b) {
816                                         free(data.mail);
817                                 }
818                                 break;
819                         case OBJ_pop3_used:
820                                 if (!obj->char_b) {
821                                         free(data.mail);
822                                 }
823                                 break;
824                         case OBJ_if_empty:
825                         case OBJ_if_match:
826                                 free_text_objects(obj->sub, 1);
827                                 free(obj->sub);
828                                 /* fall through */
829                         case OBJ_if_existing:
830                         case OBJ_if_mounted:
831                         case OBJ_if_running:
832                                 free(data.ifblock.s);
833                                 free(data.ifblock.str);
834                                 break;
835                         case OBJ_head:
836                         case OBJ_tail:
837                                 free(data.headtail.logfile);
838                                 if(data.headtail.buffer) {
839                                         free(data.headtail.buffer);
840                                 }
841                                 break;
842                         case OBJ_text:
843                         case OBJ_font:
844                         case OBJ_image:
845                         case OBJ_eval:
846                         case OBJ_exec:
847                         case OBJ_execbar:
848 #ifdef X11
849                         case OBJ_execgauge:
850                         case OBJ_execgraph:
851 #endif
852                         case OBJ_execp:
853                                 free(data.s);
854                                 break;
855 #ifdef HAVE_ICONV
856                         case OBJ_iconv_start:
857                                 free_iconv();
858                                 break;
859 #endif
860 #ifdef __linux__
861                         case OBJ_disk_protect:
862                                 free(data.s);
863                                 break;
864                         case OBJ_if_gw:
865                                 free(data.ifblock.s);
866                                 free(data.ifblock.str);
867                         case OBJ_gw_iface:
868                         case OBJ_gw_ip:
869                                 if (info.gw_info.iface) {
870                                         free(info.gw_info.iface);
871                                         info.gw_info.iface = 0;
872                                 }
873                                 if (info.gw_info.ip) {
874                                         free(info.gw_info.ip);
875                                         info.gw_info.ip = 0;
876                                 }
877                                 break;
878                         case OBJ_ioscheduler:
879                                 if(data.s)
880                                         free(data.s);
881                                 break;
882 #endif
883 #if (defined(__FreeBSD__) || defined(__linux__))
884                         case OBJ_if_up:
885                                 free(data.ifblock.s);
886                                 free(data.ifblock.str);
887                                 break;
888 #endif
889 #ifdef XMMS2
890                         case OBJ_xmms2_artist:
891                                 if (info.xmms2.artist) {
892                                         free(info.xmms2.artist);
893                                         info.xmms2.artist = 0;
894                                 }
895                                 break;
896                         case OBJ_xmms2_album:
897                                 if (info.xmms2.album) {
898                                         free(info.xmms2.album);
899                                         info.xmms2.album = 0;
900                                 }
901                                 break;
902                         case OBJ_xmms2_title:
903                                 if (info.xmms2.title) {
904                                         free(info.xmms2.title);
905                                         info.xmms2.title = 0;
906                                 }
907                                 break;
908                         case OBJ_xmms2_genre:
909                                 if (info.xmms2.genre) {
910                                         free(info.xmms2.genre);
911                                         info.xmms2.genre = 0;
912                                 }
913                                 break;
914                         case OBJ_xmms2_comment:
915                                 if (info.xmms2.comment) {
916                                         free(info.xmms2.comment);
917                                         info.xmms2.comment = 0;
918                                 }
919                                 break;
920                         case OBJ_xmms2_url:
921                                 if (info.xmms2.url) {
922                                         free(info.xmms2.url);
923                                         info.xmms2.url = 0;
924                                 }
925                                 break;
926                         case OBJ_xmms2_date:
927                                 if (info.xmms2.date) {
928                                         free(info.xmms2.date);
929                                         info.xmms2.date = 0;
930                                 }
931                                 break;
932                         case OBJ_xmms2_status:
933                                 if (info.xmms2.status) {
934                                         free(info.xmms2.status);
935                                         info.xmms2.status = 0;
936                                 }
937                                 break;
938                         case OBJ_xmms2_playlist:
939                                 if (info.xmms2.playlist) {
940                                         free(info.xmms2.playlist);
941                                         info.xmms2.playlist = 0;
942                                 }
943                                 break;
944                         case OBJ_xmms2_smart:
945                                 if (info.xmms2.artist) {
946                                         free(info.xmms2.artist);
947                                         info.xmms2.artist = 0;
948                                 }
949                                 if (info.xmms2.title) {
950                                         free(info.xmms2.title);
951                                         info.xmms2.title = 0;
952                                 }
953                                 if (info.xmms2.url) {
954                                         free(info.xmms2.url);
955                                         info.xmms2.url = 0;
956                                 }
957                                 break;
958 #endif
959 #ifdef BMPX
960                         case OBJ_bmpx_title:
961                         case OBJ_bmpx_artist:
962                         case OBJ_bmpx_album:
963                         case OBJ_bmpx_track:
964                         case OBJ_bmpx_uri:
965                         case OBJ_bmpx_bitrate:
966                                 break;
967 #endif
968 #ifdef EVE
969                         case OBJ_eve:
970                                 break;
971 #endif
972 #ifdef HAVE_CURL
973                         case OBJ_curl:
974                                 free(data.curl.uri);
975                                 break;
976 #endif
977 #ifdef RSS
978                         case OBJ_rss:
979                                 free(data.rss.uri);
980                                 free(data.rss.action);
981                                 break;
982 #endif
983 #ifdef WEATHER
984                         case OBJ_weather:
985                                 free(data.weather.uri);
986                                 free(data.weather.data_type);
987                                 break;
988 #endif
989 #ifdef HAVE_LUA
990                         case OBJ_lua:
991                         case OBJ_lua_parse:
992                         case OBJ_lua_bar:
993 #ifdef X11
994                         case OBJ_lua_graph:
995                         case OBJ_lua_gauge:
996 #endif /* X11 */
997                                 free(data.s);
998                                 break;
999 #endif /* HAVE_LUA */
1000                         case OBJ_pre_exec:
1001                                 break;
1002 #ifndef __OpenBSD__
1003                         case OBJ_battery:
1004                                 free(data.s);
1005                                 break;
1006                         case OBJ_battery_short:
1007                                 free(data.s);
1008                                 break;
1009                         case OBJ_battery_time:
1010                                 free(data.s);
1011                                 break;
1012 #endif /* !__OpenBSD__ */
1013                         case OBJ_execpi:
1014                         case OBJ_execi:
1015                         case OBJ_execibar:
1016 #ifdef X11
1017                         case OBJ_execigraph:
1018                         case OBJ_execigauge:
1019 #endif /* X11 */
1020                                 free(data.execi.cmd);
1021                                 free(data.execi.buffer);
1022                                 break;
1023                         case OBJ_texeci:
1024                                 if (data.texeci.p_timed_thread) timed_thread_destroy(data.texeci.p_timed_thread, &data.texeci.p_timed_thread);
1025                                 free(data.texeci.cmd);
1026                                 free(data.texeci.buffer);
1027                                 break;
1028                         case OBJ_nameserver:
1029                                 free_dns_data();
1030                                 break;
1031                         case OBJ_top:
1032                         case OBJ_top_mem:
1033                         case OBJ_top_time:
1034 #ifdef IOSTATS
1035                         case OBJ_top_io:
1036 #endif
1037                                 if (info.first_process && !internal) {
1038                                         free_all_processes();
1039                                         info.first_process = NULL;
1040                                 }
1041                                 if (data.top.s) free(data.top.s);
1042                                 break;
1043 #ifdef HDDTEMP
1044                         case OBJ_hddtemp:
1045                                 free(data.hddtemp.dev);
1046                                 free(data.hddtemp.addr);
1047                                 if (data.hddtemp.temp)
1048                                         free(data.hddtemp.temp);
1049                                 break;
1050 #endif /* HDDTEMP */
1051                         case OBJ_entropy_avail:
1052                         case OBJ_entropy_perc:
1053                         case OBJ_entropy_poolsize:
1054                         case OBJ_entropy_bar:
1055                                 break;
1056                         case OBJ_user_names:
1057                                 if (info.users.names) {
1058                                         free(info.users.names);
1059                                         info.users.names = 0;
1060                                 }
1061                                 break;
1062                         case OBJ_user_terms:
1063                                 if (info.users.terms) {
1064                                         free(info.users.terms);
1065                                         info.users.terms = 0;
1066                                 }
1067                                 break;
1068                         case OBJ_user_times:
1069                                 if (info.users.times) {
1070                                         free(info.users.times);
1071                                         info.users.times = 0;
1072                                 }
1073                                 break;
1074 #ifdef IBM
1075                         case OBJ_smapi:
1076                         case OBJ_smapi_bat_perc:
1077                         case OBJ_smapi_bat_temp:
1078                         case OBJ_smapi_bat_power:
1079                                 free(data.s);
1080                                 break;
1081                         case OBJ_if_smapi_bat_installed:
1082                                 free(data.ifblock.s);
1083                                 free(data.ifblock.str);
1084                                 break;
1085 #endif /* IBM */
1086 #ifdef NVIDIA
1087                         case OBJ_nvidia:
1088                                 break;
1089 #endif /* NVIDIA */
1090 #ifdef MPD
1091                         case OBJ_mpd_title:
1092                         case OBJ_mpd_artist:
1093                         case OBJ_mpd_album:
1094                         case OBJ_mpd_random:
1095                         case OBJ_mpd_repeat:
1096                         case OBJ_mpd_vol:
1097                         case OBJ_mpd_bitrate:
1098                         case OBJ_mpd_status:
1099                         case OBJ_mpd_bar:
1100                         case OBJ_mpd_elapsed:
1101                         case OBJ_mpd_length:
1102                         case OBJ_mpd_track:
1103                         case OBJ_mpd_name:
1104                         case OBJ_mpd_file:
1105                         case OBJ_mpd_percent:
1106                         case OBJ_mpd_smart:
1107                         case OBJ_if_mpd_playing:
1108                                 free_mpd();
1109                                 break;
1110 #endif /* MPD */
1111 #ifdef MOC
1112                         case OBJ_moc_state:
1113                         case OBJ_moc_file:
1114                         case OBJ_moc_title:
1115                         case OBJ_moc_artist:
1116                         case OBJ_moc_song:
1117                         case OBJ_moc_album:
1118                         case OBJ_moc_totaltime:
1119                         case OBJ_moc_timeleft:
1120                         case OBJ_moc_curtime:
1121                         case OBJ_moc_bitrate:
1122                         case OBJ_moc_rate:
1123                                 free_moc();
1124                                 break;
1125 #endif /* MOC */
1126                         case OBJ_blink:
1127                         case OBJ_to_bytes:
1128                                 free_text_objects(obj->sub, 1);
1129                                 free(obj->sub);
1130                                 break;
1131                         case OBJ_scroll:
1132                                 free(data.scroll.text);
1133                                 free_text_objects(obj->sub, 1);
1134                                 free(obj->sub);
1135                                 break;
1136                         case OBJ_combine:
1137                                 free(data.combine.left);
1138                                 free(data.combine.seperation);
1139                                 free(data.combine.right);
1140                                 free_text_objects(obj->sub, 1);
1141                                 free(obj->sub);
1142                                 break;
1143 #ifdef APCUPSD
1144                         case OBJ_apcupsd:
1145                         case OBJ_apcupsd_name:
1146                         case OBJ_apcupsd_model:
1147                         case OBJ_apcupsd_upsmode:
1148                         case OBJ_apcupsd_cable:
1149                         case OBJ_apcupsd_status:
1150                         case OBJ_apcupsd_linev:
1151                         case OBJ_apcupsd_load:
1152                         case OBJ_apcupsd_loadbar:
1153 #ifdef X11
1154                         case OBJ_apcupsd_loadgraph:
1155                         case OBJ_apcupsd_loadgauge:
1156 #endif /* X11 */
1157                         case OBJ_apcupsd_charge:
1158                         case OBJ_apcupsd_timeleft:
1159                         case OBJ_apcupsd_temp:
1160                         case OBJ_apcupsd_lastxfer:
1161                                 break;
1162 #endif /* APCUPSD */
1163 #ifdef X11
1164                         case OBJ_desktop:
1165                         case OBJ_desktop_number:
1166                         case OBJ_desktop_name:
1167                                 if(info.x11.desktop.name) {
1168                                   free(info.x11.desktop.name);
1169                                   info.x11.desktop.name = NULL;
1170                                 }
1171                                 if(info.x11.desktop.all_names) {
1172                                   free(info.x11.desktop.all_names);
1173                                   info.x11.desktop.all_names = NULL;
1174                                 }
1175                                 break;
1176 #endif /* X11 */
1177                 }
1178                 free(obj);
1179         }
1180 #undef data
1181 }
1182
1183 #ifdef X11
1184 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1185 {
1186         char buf1[64];
1187         int n;
1188
1189         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1190                 *a = mixer_init(buf1);
1191                 scan_bar(arg + n, w, h);
1192         } else {
1193                 *a = mixer_init(NULL);
1194                 scan_bar(arg, w, h);
1195         }
1196 }
1197 #endif /* X11 */
1198
1199 /* strip a leading /dev/ if any, following symlinks first
1200  *
1201  * BEWARE: this function returns a pointer to static content
1202  *         which gets overwritten in consecutive calls. I.e.:
1203  *         this function is NOT reentrant.
1204  */
1205 static const char *dev_name(const char *path)
1206 {
1207         static char buf[255];   /* should be enough for pathnames */
1208         ssize_t buflen;
1209
1210         if (!path)
1211                 return NULL;
1212
1213 #define DEV_NAME(x) \
1214   x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1215         if ((buflen = readlink(path, buf, 254)) == -1)
1216                 return DEV_NAME(path);
1217         buf[buflen] = '\0';
1218         return DEV_NAME(buf);
1219 #undef DEV_NAME
1220 }
1221
1222 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1223 {
1224         char buf[64];
1225         int n;
1226
1227         if (obj->data.top.was_parsed) {
1228                 return 1;
1229         }
1230         obj->data.top.was_parsed = 1;
1231
1232         if (arg && !obj->data.top.s) {
1233                 obj->data.top.s = strndup(arg, text_buffer_size);
1234         }
1235
1236         need_mask |= (1 << INFO_TOP);
1237
1238         if (s[3] == 0) {
1239                 obj->type = OBJ_top;
1240                 top_cpu = 1;
1241         } else if (strcmp(&s[3], "_mem") == EQUAL) {
1242                 obj->type = OBJ_top_mem;
1243                 top_mem = 1;
1244         } else if (strcmp(&s[3], "_time") == EQUAL) {
1245                 obj->type = OBJ_top_time;
1246                 top_time = 1;
1247 #ifdef IOSTATS
1248         } else if (strcmp(&s[3], "_io") == EQUAL) {
1249                 obj->type = OBJ_top_io;
1250                 top_io = 1;
1251 #endif
1252         } else {
1253 #ifdef IOSTATS
1254                 ERR("Must be top, top_mem, top_time or top_io");
1255 #else
1256                 ERR("Must be top, top_mem or top_time");
1257 #endif
1258                 return 0;
1259         }
1260
1261         if (!arg) {
1262                 ERR("top needs arguments");
1263                 return 0;
1264         }
1265
1266         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1267                 if (strcmp(buf, "name") == EQUAL) {
1268                         obj->data.top.type = TOP_NAME;
1269                 } else if (strcmp(buf, "cpu") == EQUAL) {
1270                         obj->data.top.type = TOP_CPU;
1271                 } else if (strcmp(buf, "pid") == EQUAL) {
1272                         obj->data.top.type = TOP_PID;
1273                 } else if (strcmp(buf, "mem") == EQUAL) {
1274                         obj->data.top.type = TOP_MEM;
1275                 } else if (strcmp(buf, "time") == EQUAL) {
1276                         obj->data.top.type = TOP_TIME;
1277                 } else if (strcmp(buf, "mem_res") == EQUAL) {
1278                         obj->data.top.type = TOP_MEM_RES;
1279                 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1280                         obj->data.top.type = TOP_MEM_VSIZE;
1281 #ifdef IOSTATS
1282                 } else if (strcmp(buf, "io_read") == EQUAL) {
1283                         obj->data.top.type = TOP_READ_BYTES;
1284                 } else if (strcmp(buf, "io_write") == EQUAL) {
1285                         obj->data.top.type = TOP_WRITE_BYTES;
1286                 } else if (strcmp(buf, "io_perc") == EQUAL) {
1287                         obj->data.top.type = TOP_IO_PERC;
1288 #endif
1289                 } else {
1290                         ERR("invalid type arg for top");
1291 #ifdef IOSTATS
1292                         ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1293                                         "io_read, io_write, io_perc");
1294 #else
1295                         ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1296 #endif
1297                         return 0;
1298                 }
1299                 if (n < 1 || n > 10) {
1300                         ERR("invalid num arg for top. Must be between 1 and 10.");
1301                         return 0;
1302                 } else {
1303                         obj->data.top.num = n - 1;
1304                 }
1305         } else {
1306                 ERR("invalid argument count for top");
1307                 return 0;
1308         }
1309         return 1;
1310 }
1311
1312 long current_text_color;
1313
1314 /* construct_text_object() creates a new text_object */
1315 static struct text_object *construct_text_object(const char *s,
1316                 const char *arg, long line, void **ifblock_opaque, void *free_at_crash)
1317 {
1318         // struct text_object *obj = new_text_object();
1319         struct text_object *obj = new_text_object_internal();
1320
1321         obj->line = line;
1322
1323 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1324         obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1325 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1326         obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1327         obj_be_ifblock_if(ifblock_opaque, obj); {
1328 #define END } } else
1329
1330 #define SIZE_DEFAULTS(arg) { \
1331         obj->a = default_##arg##_width; \
1332         obj->b = default_##arg##_height; \
1333 }
1334
1335 #ifdef X11
1336         if (s[0] == '#') {
1337                 obj->type = OBJ_color;
1338                 obj->data.l = get_x11_color(s);
1339         } else
1340 #endif /* X11 */
1341 #ifdef __OpenBSD__
1342         OBJ(freq, INFO_FREQ)
1343 #else
1344         OBJ(acpitemp, 0)
1345                 obj->data.i = open_acpi_temperature(arg);
1346         END OBJ(acpiacadapter, 0)
1347         END OBJ(freq, INFO_FREQ)
1348 #endif /* !__OpenBSD__ */
1349                 get_cpu_count();
1350                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1351                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1352                         obj->data.cpu_index = 1;
1353                         /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1354                                 "Displaying the clock for CPU 1."); */
1355                 } else {
1356                         obj->data.cpu_index = atoi(&arg[0]);
1357                 }
1358                 obj->a = 1;
1359         END OBJ(freq_g, INFO_FREQ)
1360                 get_cpu_count();
1361                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1362                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1363                         obj->data.cpu_index = 1;
1364                         /* ERR("freq_g: Invalid CPU number or you don't have that many "
1365                                 "CPUs! Displaying the clock for CPU 1."); */
1366                 } else {
1367                         obj->data.cpu_index = atoi(&arg[0]);
1368                 }
1369                 obj->a = 1;
1370         END OBJ(read_tcp, 0)
1371                 if (arg) {
1372                         obj->data.read_tcp.host = malloc(text_buffer_size);
1373                         sscanf(arg, "%s", obj->data.read_tcp.host);
1374                         sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1375                         if(obj->data.read_tcp.port == 0) {
1376                                 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1377                                 strcpy(obj->data.read_tcp.host,"localhost");
1378                         }
1379                         obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1380                         if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1381                                 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1382                         }
1383                 }else{
1384                         CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1385                 }
1386 #if defined(__linux__)
1387         END OBJ(voltage_mv, 0)
1388                 get_cpu_count();
1389                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1390                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1391                         obj->data.cpu_index = 1;
1392                         /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1393                                 "CPUs! Displaying voltage for CPU 1."); */
1394                 } else {
1395                         obj->data.cpu_index = atoi(&arg[0]);
1396                 }
1397                 obj->a = 1;
1398         END OBJ(voltage_v, 0)
1399                 get_cpu_count();
1400                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1401                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1402                         obj->data.cpu_index = 1;
1403                         /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1404                                 "CPUs! Displaying voltage for CPU 1."); */
1405                 } else {
1406                         obj->data.cpu_index = atoi(&arg[0]);
1407                 }
1408                 obj->a = 1;
1409
1410 #ifdef HAVE_IWLIB
1411         END OBJ(wireless_essid, INFO_NET)
1412                 if (arg) {
1413                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1414                 } else {
1415                         // default to DEFAULTNETDEV
1416                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1417                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1418                         free(buf);
1419                 }
1420         END OBJ(wireless_mode, INFO_NET)
1421                 if (arg) {
1422                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1423                 } else {
1424                         // default to DEFAULTNETDEV
1425                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1426                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1427                         free(buf);
1428                 }
1429         END OBJ(wireless_bitrate, INFO_NET)
1430                 if (arg) {
1431                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1432                 } else {
1433                         // default to DEFAULTNETDEV
1434                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1435                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1436                         free(buf);
1437                 }
1438         END OBJ(wireless_ap, INFO_NET)
1439                 if (arg) {
1440                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1441                 } else {
1442                         // default to DEFAULTNETDEV
1443                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1444                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1445                         free(buf);
1446                 }
1447         END OBJ(wireless_link_qual, INFO_NET)
1448                 if (arg) {
1449                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1450                 } else {
1451                         // default to DEFAULTNETDEV
1452                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1453                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1454                         free(buf);
1455                 }
1456         END OBJ(wireless_link_qual_max, INFO_NET)
1457                 if (arg) {
1458                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1459                 } else {
1460                         // default to DEFAULTNETDEV
1461                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1462                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1463                         free(buf);
1464                 }
1465         END OBJ(wireless_link_qual_perc, INFO_NET)
1466                 if (arg) {
1467                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1468                 } else {
1469                         // default to DEFAULTNETDEV
1470                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1471                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1472                         free(buf);
1473                 }
1474         END OBJ(wireless_link_bar, INFO_NET)
1475                 SIZE_DEFAULTS(bar);
1476                 if (arg) {
1477                         arg = scan_bar(arg, &obj->a, &obj->b);
1478                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1479                 } else {
1480                         // default to DEFAULTNETDEV
1481                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1482                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1483                         free(buf);
1484                 }
1485 #endif /* HAVE_IWLIB */
1486
1487 #endif /* __linux__ */
1488
1489 #ifndef __OpenBSD__
1490         END OBJ(acpifan, 0)
1491         END OBJ(battery, 0)
1492                 char bat[64];
1493
1494                 if (arg) {
1495                         sscanf(arg, "%63s", bat);
1496                 } else {
1497                         strcpy(bat, "BAT0");
1498                 }
1499                 obj->data.s = strndup(bat, text_buffer_size);
1500         END OBJ(battery_short, 0)
1501                 char bat[64];
1502
1503                 if (arg) {
1504                         sscanf(arg, "%63s", bat);
1505                 } else {
1506                         strcpy(bat, "BAT0");
1507                 }
1508                 obj->data.s = strndup(bat, text_buffer_size);
1509         END OBJ(battery_time, 0)
1510                 char bat[64];
1511
1512                 if (arg) {
1513                         sscanf(arg, "%63s", bat);
1514                 } else {
1515                         strcpy(bat, "BAT0");
1516                 }
1517                 obj->data.s = strndup(bat, text_buffer_size);
1518         END OBJ(battery_percent, 0)
1519                 char bat[64];
1520
1521                 if (arg) {
1522                         sscanf(arg, "%63s", bat);
1523                 } else {
1524                         strcpy(bat, "BAT0");
1525                 }
1526                 obj->data.s = strndup(bat, text_buffer_size);
1527         END OBJ(battery_bar, 0)
1528                 char bat[64];
1529                 SIZE_DEFAULTS(bar);
1530                 obj->b = 6;
1531                 if (arg) {
1532                         arg = scan_bar(arg, &obj->a, &obj->b);
1533                         sscanf(arg, "%63s", bat);
1534                 } else {
1535                         strcpy(bat, "BAT0");
1536                 }
1537                 obj->data.s = strndup(bat, text_buffer_size);
1538 #endif /* !__OpenBSD__ */
1539
1540 #if defined(__linux__)
1541         END OBJ(disk_protect, 0)
1542                 if (arg)
1543                         obj->data.s = strndup(dev_name(arg), text_buffer_size);
1544                 else
1545                         CRIT_ERR(obj, free_at_crash, "disk_protect needs an argument");
1546         END OBJ(i8k_version, INFO_I8K)
1547         END OBJ(i8k_bios, INFO_I8K)
1548         END OBJ(i8k_serial, INFO_I8K)
1549         END OBJ(i8k_cpu_temp, INFO_I8K)
1550         END OBJ(i8k_left_fan_status, INFO_I8K)
1551         END OBJ(i8k_right_fan_status, INFO_I8K)
1552         END OBJ(i8k_left_fan_rpm, INFO_I8K)
1553         END OBJ(i8k_right_fan_rpm, INFO_I8K)
1554         END OBJ(i8k_ac_status, INFO_I8K)
1555         END OBJ(i8k_buttons_status, INFO_I8K)
1556 #if defined(IBM)
1557         END OBJ(ibm_fan, 0)
1558         END OBJ(ibm_temps, 0)
1559                 if (!arg) {
1560                         CRIT_ERR(obj, free_at_crash, "ibm_temps: needs an argument");
1561                 }
1562                 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1563                         obj->data.sensor = 0;
1564                         ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1565                                 "Using 0 (CPU temp sensor).");
1566                 }
1567                 obj->data.sensor = atoi(&arg[0]);
1568         END OBJ(ibm_volume, 0)
1569         END OBJ(ibm_brightness, 0)
1570 #endif
1571         /* information from sony_laptop kernel module
1572          * /sys/devices/platform/sony-laptop */
1573         END OBJ(sony_fanspeed, 0)
1574         END OBJ_IF(if_gw, INFO_GW)
1575         END OBJ(ioscheduler, 0)
1576                 if (!arg) {
1577                         CRIT_ERR(obj, free_at_crash, "get_ioscheduler needs an argument (e.g. hda)");
1578                         obj->data.s = 0;
1579                 } else
1580                         obj->data.s = strndup(dev_name(arg), text_buffer_size);
1581         END OBJ(laptop_mode, 0)
1582         END OBJ(pb_battery, 0)
1583                 if (arg && strcmp(arg, "status") == EQUAL) {
1584                         obj->data.i = PB_BATT_STATUS;
1585                 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1586                         obj->data.i = PB_BATT_PERCENT;
1587                 } else if (arg && strcmp(arg, "time") == EQUAL) {
1588                         obj->data.i = PB_BATT_TIME;
1589                 } else {
1590                         ERR("pb_battery: needs one argument: status, percent or time");
1591                         free(obj);
1592                         return NULL;
1593                 }
1594
1595 #endif /* __linux__ */
1596 #if (defined(__FreeBSD__) || defined(__linux__))
1597         END OBJ_IF(if_up, 0)
1598                 if (!arg) {
1599                         ERR("if_up needs an argument");
1600                         obj->data.ifblock.s = 0;
1601                 } else {
1602                         obj->data.ifblock.s = strndup(arg, text_buffer_size);
1603                 }
1604 #endif
1605 #if defined(__OpenBSD__)
1606         END OBJ(obsd_sensors_temp, 0)
1607                 if (!arg) {
1608                         CRIT_ERR(obj, free_at_crash, "obsd_sensors_temp: needs an argument");
1609                 }
1610                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1611                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1612                         obj->data.sensor = 0;
1613                         ERR("Invalid temperature sensor number!");
1614                 }
1615                 obj->data.sensor = atoi(&arg[0]);
1616         END OBJ(obsd_sensors_fan, 0)
1617                 if (!arg) {
1618                         CRIT_ERR(obj, free_at_crash, "obsd_sensors_fan: needs 2 arguments (device and sensor "
1619                                 "number)");
1620                 }
1621                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1622                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1623                         obj->data.sensor = 0;
1624                         ERR("Invalid fan sensor number!");
1625                 }
1626                 obj->data.sensor = atoi(&arg[0]);
1627         END OBJ(obsd_sensors_volt, 0)
1628                 if (!arg) {
1629                         CRIT_ERR(obj, free_at_crash, "obsd_sensors_volt: needs 2 arguments (device and sensor "
1630                                 "number)");
1631                 }
1632                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1633                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1634                         obj->data.sensor = 0;
1635                         ERR("Invalid voltage sensor number!");
1636                 }
1637                 obj->data.sensor = atoi(&arg[0]);
1638         END OBJ(obsd_vendor, 0)
1639         END OBJ(obsd_product, 0)
1640 #endif /* __OpenBSD__ */
1641         END OBJ(buffers, INFO_BUFFERS)
1642         END OBJ(cached, INFO_BUFFERS)
1643 #define SCAN_CPU(__arg, __var) { \
1644         int __offset = 0; \
1645         if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1646                 __arg += __offset; \
1647         else \
1648                 __var = 0; \
1649 }
1650         END OBJ(cpu, INFO_CPU)
1651                 SCAN_CPU(arg, obj->data.cpu_index);
1652                 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1653 #ifdef X11
1654         END OBJ(cpugauge, INFO_CPU)
1655                 SIZE_DEFAULTS(gauge);
1656                 SCAN_CPU(arg, obj->data.cpu_index);
1657                 scan_gauge(arg, &obj->a, &obj->b);
1658                 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1659 #endif /* X11 */
1660         END OBJ(cpubar, INFO_CPU)
1661                 SIZE_DEFAULTS(bar);
1662                 SCAN_CPU(arg, obj->data.cpu_index);
1663                 scan_bar(arg, &obj->a, &obj->b);
1664                 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1665 #ifdef X11
1666         END OBJ(cpugraph, INFO_CPU)
1667                 char *buf = 0;
1668                 SIZE_DEFAULTS(graph);
1669                 SCAN_CPU(arg, obj->data.cpu_index);
1670                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1671                         &obj->e, &obj->char_a, &obj->char_b);
1672                 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1673                 if (buf) free(buf);
1674         END OBJ(loadgraph, INFO_LOADAVG)
1675                 char *buf = 0;
1676                 SIZE_DEFAULTS(graph);
1677                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1678                                 &obj->e, &obj->char_a, &obj->char_b);
1679                 if (buf) {
1680                         int a = 1, r = 3;
1681                         if (arg) {
1682                                 r = sscanf(arg, "%d", &a);
1683                         }
1684                         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1685                         free(buf);
1686                 }
1687 #endif /* X11 */
1688         END OBJ(diskio, INFO_DISKIO)
1689                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1690         END OBJ(diskio_read, INFO_DISKIO)
1691                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1692         END OBJ(diskio_write, INFO_DISKIO)
1693                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1694 #ifdef X11
1695         END OBJ(diskiograph, INFO_DISKIO)
1696                 char *buf = 0;
1697                 SIZE_DEFAULTS(graph);
1698                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1699                                 &obj->e, &obj->char_a, &obj->char_b);
1700
1701                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1702                 if (buf) free(buf);
1703         END OBJ(diskiograph_read, INFO_DISKIO)
1704                 char *buf = 0;
1705                 SIZE_DEFAULTS(graph);
1706                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1707                                 &obj->e, &obj->char_a, &obj->char_b);
1708
1709                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1710                 if (buf) free(buf);
1711         END OBJ(diskiograph_write, INFO_DISKIO)
1712                 char *buf = 0;
1713                 SIZE_DEFAULTS(graph);
1714                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1715                                 &obj->e, &obj->char_a, &obj->char_b);
1716
1717                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1718                 if (buf) free(buf);
1719 #endif /* X11 */
1720         END OBJ(color, 0)
1721 #ifdef X11
1722                 if (output_methods & TO_X) {
1723                         obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1724                         current_text_color = obj->data.l;
1725                 }
1726 #endif /* X11 */
1727         END OBJ(color0, 0)
1728                 obj->data.l = color0;
1729                 current_text_color = obj->data.l;
1730         END OBJ(color1, 0)
1731                 obj->data.l = color1;
1732                 current_text_color = obj->data.l;
1733         END OBJ(color2, 0)
1734                 obj->data.l = color2;
1735                 current_text_color = obj->data.l;
1736         END OBJ(color3, 0)
1737                 obj->data.l = color3;
1738                 current_text_color = obj->data.l;
1739         END OBJ(color4, 0)
1740                 obj->data.l = color4;
1741                 current_text_color = obj->data.l;
1742         END OBJ(color5, 0)
1743                 obj->data.l = color5;
1744                 current_text_color = obj->data.l;
1745         END OBJ(color6, 0)
1746                 obj->data.l = color6;
1747                 current_text_color = obj->data.l;
1748         END OBJ(color7, 0)
1749                 obj->data.l = color7;
1750                 current_text_color = obj->data.l;
1751         END OBJ(color8, 0)
1752                 obj->data.l = color8;
1753                 current_text_color = obj->data.l;
1754         END OBJ(color9, 0)
1755                 obj->data.l = color9;
1756                 current_text_color = obj->data.l;
1757 #ifdef X11
1758         END OBJ(font, 0)
1759                 obj->data.s = scan_font(arg);
1760 #endif /* X11 */
1761         END OBJ(conky_version, 0)
1762         END OBJ(conky_build_date, 0)
1763         END OBJ(conky_build_arch, 0)
1764         END OBJ(downspeed, INFO_NET)
1765                 if (arg) {
1766                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1767                 } else {
1768                         // default to DEFAULTNETDEV
1769                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1770                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1771                         free(buf);
1772                 }
1773         END OBJ(downspeedf, INFO_NET)
1774                 if (arg) {
1775                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1776                 } else {
1777                         // default to DEFAULTNETDEV
1778                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1779                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1780                         free(buf);
1781                 }
1782 #ifdef X11
1783         END OBJ(downspeedgraph, INFO_NET)
1784                 char *buf = 0;
1785                 SIZE_DEFAULTS(graph);
1786                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1787                                 &obj->e, &obj->char_a, &obj->char_b);
1788
1789                 // default to DEFAULTNETDEV
1790                 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1791                 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1792                 free(buf);
1793 #endif /* X11 */
1794         END OBJ(else, 0)
1795                 obj_be_ifblock_else(ifblock_opaque, obj);
1796         END OBJ(endif, 0)
1797                 obj_be_ifblock_endif(ifblock_opaque, obj);
1798         END OBJ(eval, 0)
1799                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1800         END OBJ(image, 0)
1801                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1802         END OBJ(exec, 0)
1803                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1804         END OBJ(execp, 0)
1805                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1806         END OBJ(execbar, 0)
1807                 SIZE_DEFAULTS(bar);
1808                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1809 #ifdef X11
1810         END OBJ(execgauge, 0)
1811                 SIZE_DEFAULTS(gauge);
1812                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1813         END OBJ(execgraph, 0)
1814                 SIZE_DEFAULTS(graph);
1815                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1816 #endif /* X11 */
1817         END OBJ(execibar, 0)
1818                 int n;
1819                 SIZE_DEFAULTS(bar);
1820
1821                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1822                         char buf[256];
1823
1824                         ERR("${execibar <interval> command}");
1825                         obj->type = OBJ_text;
1826                         snprintf(buf, 256, "${%s}", s);
1827                         obj->data.s = strndup(buf, text_buffer_size);
1828                 } else {
1829                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1830                 }
1831 #ifdef X11
1832         END OBJ(execigraph, 0)
1833                 int n;
1834                 SIZE_DEFAULTS(graph);
1835
1836                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1837                         char buf[256];
1838
1839                         ERR("${execigraph <interval> command}");
1840                         obj->type = OBJ_text;
1841                         snprintf(buf, 256, "${%s}", s);
1842                         obj->data.s = strndup(buf, text_buffer_size);
1843                 } else {
1844                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1845                 }
1846         END OBJ(execigauge, 0)
1847                 int n;
1848                 SIZE_DEFAULTS(gauge);
1849
1850                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1851                         char buf[256];
1852
1853                         ERR("${execigauge <interval> command}");
1854                         obj->type = OBJ_text;
1855                         snprintf(buf, 256, "${%s}", s);
1856                         obj->data.s = strndup(buf, text_buffer_size);
1857                 } else {
1858                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1859                 }
1860 #endif /* X11 */
1861         END OBJ(execi, 0)
1862                 int n;
1863
1864                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1865                         char buf[256];
1866
1867                         ERR("${execi <interval> command}");
1868                         obj->type = OBJ_text;
1869                         snprintf(buf, 256, "${%s}", s);
1870                         obj->data.s = strndup(buf, text_buffer_size);
1871                 } else {
1872                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1873                         obj->data.execi.buffer = malloc(text_buffer_size);
1874                 }
1875         END OBJ(execpi, 0)
1876                 int n;
1877
1878                 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1879                         char buf[256];
1880
1881                         ERR("${execi <interval> command}");
1882                         obj->type = OBJ_text;
1883                         snprintf(buf, 256, "${%s}", s);
1884                         obj->data.s = strndup(buf, text_buffer_size);
1885                 } else {
1886                         obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1887                         obj->data.execi.buffer = malloc(text_buffer_size);
1888                 }
1889         END OBJ(texeci, 0)
1890                         int n;
1891
1892                         if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1893                                 char buf[256];
1894
1895                                 ERR("${texeci <interval> command}");
1896                                 obj->type = OBJ_text;
1897                                 snprintf(buf, 256, "${%s}", s);
1898                                 obj->data.s = strndup(buf, text_buffer_size);
1899                         } else {
1900                                 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1901                                 obj->data.texeci.buffer = malloc(text_buffer_size);
1902                         }
1903                         obj->data.texeci.p_timed_thread = NULL;
1904         END OBJ(pre_exec, 0)
1905                 obj->type = OBJ_text;
1906                 if (arg) {
1907                         char buf[2048];
1908
1909                         read_exec(arg, buf, sizeof(buf));
1910                         obj->data.s = strndup(buf, text_buffer_size);
1911                 } else {
1912                         obj->data.s = strndup("", text_buffer_size);
1913                 }
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                         OBJ(addr) {
4275                                 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4276                                                 && (obj->data.net->addr.sa_data[3] & 255) == 0
4277                                                 && (obj->data.net->addr.sa_data[4] & 255) == 0
4278                                                 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4279                                         snprintf(p, p_max_size, "No Address");
4280                                 } else {
4281                                         snprintf(p, p_max_size, "%u.%u.%u.%u",
4282                                                 obj->data.net->addr.sa_data[2] & 255,
4283                                                 obj->data.net->addr.sa_data[3] & 255,
4284                                                 obj->data.net->addr.sa_data[4] & 255,
4285                                                 obj->data.net->addr.sa_data[5] & 255);
4286                                 }
4287                         }
4288 #if defined(__linux__)
4289                         OBJ(addrs) {
4290                                 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
4291                                         obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4292                                         strcpy(p, obj->data.net->addrs);
4293                                 } else {
4294                                         strcpy(p, "0.0.0.0");
4295                                 }
4296                         }
4297 #endif /* __linux__ */
4298 #if defined(IMLIB2) && defined(X11)
4299                         OBJ(image) {
4300                                 /* doesn't actually draw anything, just queues it omp.  the
4301                                  * image will get drawn after the X event loop */
4302                                 cimlib_add_image(obj->data.s);
4303                         }
4304 #endif /* IMLIB2 */
4305                         OBJ(eval) {
4306                                 evaluate(obj->data.s, p);
4307                         }
4308                         OBJ(exec) {
4309                                 read_exec(obj->data.s, p, text_buffer_size);
4310                                 remove_deleted_chars(p);
4311                         }
4312                         OBJ(execp) {
4313                                 struct information *tmp_info;
4314                                 struct text_object subroot;
4315
4316                                 read_exec(obj->data.s, p, text_buffer_size);
4317
4318                                 tmp_info = malloc(sizeof(struct information));
4319                                 memcpy(tmp_info, cur, sizeof(struct information));
4320                                 parse_conky_vars(&subroot, p, p, tmp_info);
4321
4322                                 free_text_objects(&subroot, 1);
4323                                 free(tmp_info);
4324                         }
4325 #ifdef X11
4326                         OBJ(execgauge) {
4327                                 double barnum;
4328
4329                                 read_exec(obj->data.s, p, text_buffer_size);
4330                                 barnum = get_barnum(p); /*using the same function*/
4331
4332                                 if (barnum >= 0.0) {
4333                                         barnum /= 100;
4334                                         new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4335                                 }
4336                         }
4337 #endif /* X11 */
4338                         OBJ(execbar) {
4339                                 double barnum;
4340
4341                                 read_exec(obj->data.s, p, text_buffer_size);
4342                                 barnum = get_barnum(p);
4343
4344                                 if (barnum >= 0.0) {
4345 #ifdef X11
4346                                         if(output_methods & TO_X) {
4347                                                 barnum /= 100;
4348                                                 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4349                                         }else{
4350 #endif /* X11 */
4351                                                 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4352                                                 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4353 #ifdef X11
4354                                         }
4355 #endif /* X11 */
4356                                 }
4357                         }
4358 #ifdef X11
4359                         OBJ(execgraph) {
4360                                 char showaslog = FALSE;
4361                                 char tempgrad = FALSE;
4362                                 double barnum;
4363                                 char *cmd = obj->data.s;
4364
4365                                 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4366                                         tempgrad = TRUE;
4367                                         cmd += strlen(" "TEMPGRAD);
4368                                 }
4369                                 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4370                                         showaslog = TRUE;
4371                                         cmd += strlen(" "LOGGRAPH);
4372                                 }
4373                                 read_exec(cmd, p, text_buffer_size);
4374                                 barnum = get_barnum(p);
4375
4376                                 if (barnum > 0) {
4377                                         new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4378                                                         100, 1, showaslog, tempgrad);
4379                                 }
4380                         }
4381 #endif /* X11 */
4382                         OBJ(execibar) {
4383                                 if (current_update_time - obj->data.execi.last_update
4384                                                 >= obj->data.execi.interval) {
4385                                         double barnum;
4386
4387                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
4388                                         barnum = get_barnum(p);
4389
4390                                         if (barnum >= 0.0) {
4391                                                 obj->f = barnum;
4392                                         }
4393                                         obj->data.execi.last_update = current_update_time;
4394                                 }
4395 #ifdef X11
4396                                 if(output_methods & TO_X) {
4397                                         new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4398                                 } else {
4399 #endif /* X11 */
4400                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4401                                         new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4402 #ifdef X11
4403                                 }
4404 #endif /* X11 */
4405                         }
4406 #ifdef X11
4407                         OBJ(execigraph) {
4408                                 if (current_update_time - obj->data.execi.last_update
4409                                                 >= obj->data.execi.interval) {
4410                                         double barnum;
4411                                         char showaslog = FALSE;
4412                                         char tempgrad = FALSE;
4413                                         char *cmd = obj->data.execi.cmd;
4414
4415                                         if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4416                                                 tempgrad = TRUE;
4417                                                 cmd += strlen(" "TEMPGRAD);
4418                                         }
4419                                         if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4420                                                 showaslog = TRUE;
4421                                                 cmd += strlen(" "LOGGRAPH);
4422                                         }
4423                                         obj->char_a = showaslog;
4424                                         obj->char_b = tempgrad;
4425                                         read_exec(cmd, p, text_buffer_size);
4426                                         barnum = get_barnum(p);
4427
4428                                         if (barnum >= 0.0) {
4429                                                 obj->f = barnum;
4430                                         }
4431                                         obj->data.execi.last_update = current_update_time;
4432                                 }
4433                                 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4434                         }
4435                         OBJ(execigauge) {
4436                                 if (current_update_time - obj->data.execi.last_update
4437                                                 >= obj->data.execi.interval) {
4438                                         double barnum;
4439
4440                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
4441                                         barnum = get_barnum(p);
4442
4443                                         if (barnum >= 0.0) {
4444                                                 obj->f = 255 * barnum / 100.0;
4445                                         }
4446                                         obj->data.execi.last_update = current_update_time;
4447                                 }
4448                                 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4449                         }
4450 #endif /* X11 */
4451                         OBJ(execi) {
4452                                 if (current_update_time - obj->data.execi.last_update
4453                                                 >= obj->data.execi.interval
4454                                                 && obj->data.execi.interval != 0) {
4455                                         read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4456                                                 text_buffer_size);
4457                                         obj->data.execi.last_update = current_update_time;
4458                                 }
4459                                 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4460                         }
4461                         OBJ(execpi) {
4462                                 struct text_object subroot;
4463                                 struct information *tmp_info =
4464                                         malloc(sizeof(struct information));
4465                                 memcpy(tmp_info, cur, sizeof(struct information));
4466
4467                                 if (current_update_time - obj->data.execi.last_update
4468                                                 < obj->data.execi.interval
4469                                                 || obj->data.execi.interval == 0) {
4470                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4471                                 } else {
4472                                         char *output = obj->data.execi.buffer;
4473                                         FILE *fp = pid_popen(obj->data.execi.cmd, "r", &childpid);
4474                                         int length = fread(output, 1, text_buffer_size, fp);
4475
4476                                         pclose(fp);
4477
4478                                         output[length] = '\0';
4479                                         if (length > 0 && output[length - 1] == '\n') {
4480                                                 output[length - 1] = '\0';
4481                                         }
4482
4483                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4484                                         obj->data.execi.last_update = current_update_time;
4485                                 }
4486                                 free_text_objects(&subroot, 1);
4487                                 free(tmp_info);
4488                         }
4489                         OBJ(texeci) {
4490                                 if (!obj->data.texeci.p_timed_thread) {
4491                                         obj->data.texeci.p_timed_thread =
4492                                                 timed_thread_create(&threaded_exec,
4493                                                 (void *) obj, obj->data.texeci.interval * 1000000);
4494                                         if (!obj->data.texeci.p_timed_thread) {
4495                                                 ERR("Error creating texeci timed thread");
4496                                         }
4497                                         /*
4498                                          * note that we don't register this thread with the
4499                                          * timed_thread list, because we destroy it manually
4500                                          */
4501                                         if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4502                                                 ERR("Error running texeci timed thread");
4503                                         }
4504                                 } else {
4505                                         timed_thread_lock(obj->data.texeci.p_timed_thread);
4506                                         snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4507                                         timed_thread_unlock(obj->data.texeci.p_timed_thread);
4508                                 }
4509                         }
4510                         OBJ(imap_unseen) {
4511                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4512
4513                                 if (mail && mail->p_timed_thread) {
4514                                         timed_thread_lock(mail->p_timed_thread);
4515                                         snprintf(p, p_max_size, "%lu", mail->unseen);
4516                                         timed_thread_unlock(mail->p_timed_thread);
4517                                 }
4518                         }
4519                         OBJ(imap_messages) {
4520                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4521
4522                                 if (mail && mail->p_timed_thread) {
4523                                         timed_thread_lock(mail->p_timed_thread);
4524                                         snprintf(p, p_max_size, "%lu", mail->messages);
4525                                         timed_thread_unlock(mail->p_timed_thread);
4526                                 }
4527                         }
4528                         OBJ(pop3_unseen) {
4529                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4530
4531                                 if (mail && mail->p_timed_thread) {
4532                                         timed_thread_lock(mail->p_timed_thread);
4533                                         snprintf(p, p_max_size, "%lu", mail->unseen);
4534                                         timed_thread_unlock(mail->p_timed_thread);
4535                                 }
4536                         }
4537                         OBJ(pop3_used) {
4538                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4539
4540                                 if (mail && mail->p_timed_thread) {
4541                                         timed_thread_lock(mail->p_timed_thread);
4542                                         snprintf(p, p_max_size, "%.1f",
4543                                                 mail->used / 1024.0 / 1024.0);
4544                                         timed_thread_unlock(mail->p_timed_thread);
4545                                 }
4546                         }
4547                         OBJ(fs_bar) {
4548                                 if (obj->data.fs != NULL) {
4549                                         if (obj->data.fs->size == 0) {
4550 #ifdef X11
4551                                                 if(output_methods & TO_X) {
4552                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4553                                                 }else{
4554 #endif /* X11 */
4555                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4556                                                         new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4557 #ifdef X11
4558                                                 }
4559 #endif /* X11 */
4560                                         } else {
4561 #ifdef X11
4562                                                 if(output_methods & TO_X) {
4563                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4564                                                                 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4565                                                                 obj->data.fs->size));
4566                                                 }else{
4567 #endif /* X11 */
4568                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4569                                                         new_bar_in_shell(p, p_max_size,
4570                                                                 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4571 #ifdef X11
4572                                                 }
4573 #endif /* X11 */
4574                                         }
4575                                 }
4576                         }
4577                         OBJ(fs_free) {
4578                                 if (obj->data.fs != NULL) {
4579                                         human_readable(obj->data.fs->avail, p, 255);
4580                                 }
4581                         }
4582                         OBJ(fs_free_perc) {
4583                                 if (obj->data.fs != NULL) {
4584                                         int val = 0;
4585
4586                                         if (obj->data.fs->size) {
4587                                                 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4588                                         }
4589
4590                                         percent_print(p, p_max_size, val);
4591                                 }
4592                         }
4593                         OBJ(fs_size) {
4594                                 if (obj->data.fs != NULL) {
4595                                         human_readable(obj->data.fs->size, p, 255);
4596                                 }
4597                         }
4598                         OBJ(fs_type) {
4599                                 if (obj->data.fs != NULL)
4600                                         snprintf(p, p_max_size, "%s", obj->data.fs->type);
4601                         }
4602                         OBJ(fs_used) {
4603                                 if (obj->data.fs != NULL) {
4604                                         human_readable(obj->data.fs->size - obj->data.fs->free, p,
4605                                                         255);
4606                                 }
4607                         }
4608                         OBJ(fs_bar_free) {
4609                                 if (obj->data.fs != NULL) {
4610                                         if (obj->data.fs->size == 0) {
4611 #ifdef X11
4612                                                 if(output_methods & TO_X) {
4613                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4614                                                 }else{
4615 #endif /* X11 */
4616                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4617                                                         new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4618 #ifdef X11
4619                                                 }
4620 #endif /* X11 */
4621                                         } else {
4622 #ifdef X11
4623                                                 if(output_methods & TO_X) {
4624                                                         new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4625                                                                 (int) (obj->data.fsbar.fs->avail * 255 /
4626                                                                 obj->data.fs->size));
4627                                                 }else{
4628 #endif /* X11 */
4629                                                         if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4630                                                         new_bar_in_shell(p, p_max_size,
4631                                                                 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4632 #ifdef X11
4633                                                 }
4634 #endif /* X11 */
4635                                         }
4636                                 }
4637                         }
4638                         OBJ(fs_used_perc) {
4639                                 if (obj->data.fs != NULL) {
4640                                         int val = 0;
4641
4642                                         if (obj->data.fs->size) {
4643                                                 val = obj->data.fs->free
4644                                                                 * 100 /
4645                                                         obj->data.fs->size;
4646                                         }
4647
4648                                         percent_print(p, p_max_size, 100 - val);
4649                                 }
4650                         }
4651                         OBJ(loadavg) {
4652                                 float *v = info.loadavg;
4653
4654                                 if (obj->data.loadavg[2]) {
4655                                         snprintf(p, p_max_size, "%.2f %.2f %.2f",
4656                                                 v[obj->data.loadavg[0] - 1],
4657                                                 v[obj->data.loadavg[1] - 1],
4658                                                 v[obj->data.loadavg[2] - 1]);
4659                                 } else if (obj->data.loadavg[1]) {
4660                                         snprintf(p, p_max_size, "%.2f %.2f",
4661                                                 v[obj->data.loadavg[0] - 1],
4662                                                 v[obj->data.loadavg[1] - 1]);
4663                                 } else if (obj->data.loadavg[0]) {
4664                                         snprintf(p, p_max_size, "%.2f",
4665                                                 v[obj->data.loadavg[0] - 1]);
4666                                 }
4667                         }
4668                         OBJ(goto) {
4669                                 new_goto(p, obj->data.i);
4670                         }
4671                         OBJ(tab) {
4672                                 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4673                         }
4674 #ifdef X11
4675                         OBJ(hr) {
4676                                 new_hr(p, obj->data.i);
4677                         }
4678 #endif
4679                         OBJ(nameserver) {
4680                                 if (cur->nameserver_info.nscount > obj->data.i)
4681                                         snprintf(p, p_max_size, "%s",
4682                                                         cur->nameserver_info.ns_list[obj->data.i]);
4683                         }
4684 #ifdef EVE
4685                         OBJ(eve) {
4686                                 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4687                                 snprintf(p, p_max_size, "%s", skill);
4688                         }
4689 #endif
4690 #ifdef HAVE_CURL
4691                         OBJ(curl) {
4692                                 if (obj->data.curl.uri != NULL) {
4693                                         ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
4694                                 } else {
4695                                         ERR("error processing Curl data");
4696                                 }
4697                         }
4698 #endif
4699 #ifdef RSS
4700                         OBJ(rss) {
4701                                 if (obj->data.rss.uri != NULL) {
4702                                         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);
4703                                 } else {
4704                                         ERR("error processing RSS data");
4705                                 }
4706                         }
4707 #endif
4708 #ifdef WEATHER
4709                         OBJ(weather) {
4710                                 if (obj->data.weather.uri != NULL) {
4711                                         weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4712                                 } else {
4713                                         ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
4714                                 }
4715                         }
4716 #endif
4717 #ifdef HAVE_LUA
4718                         OBJ(lua) {
4719                                 char *str = llua_getstring(obj->data.s);
4720                                 if (str) {
4721                                         snprintf(p, p_max_size, "%s", str);
4722                                         free(str);
4723                                 }
4724                         }
4725                         OBJ(lua_parse) {
4726                                 char *str = llua_getstring(obj->data.s);
4727                                 if (str) {
4728                                         evaluate(str, p);
4729                                         free(str);
4730                                 }
4731                         }
4732                         OBJ(lua_bar) {
4733                                 double per;
4734                                 if (llua_getnumber(obj->data.s, &per)) {
4735 #ifdef X11
4736                                         if(output_methods & TO_X) {
4737                                                 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4738                                         } else {
4739 #endif /* X11 */
4740                                                 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4741                                                 new_bar_in_shell(p, p_max_size, per, obj->a);
4742 #ifdef X11
4743                                         }
4744 #endif /* X11 */
4745                                 }
4746                         }
4747 #ifdef X11
4748                         OBJ(lua_graph) {
4749                                 double per;
4750                                 if (llua_getnumber(obj->data.s, &per)) {
4751                                         new_graph(p, obj->a, obj->b, obj->c, obj->d,
4752                                                         per, obj->e, 1, obj->char_a, obj->char_b);
4753                                 }
4754                         }
4755                         OBJ(lua_gauge) {
4756                                 double per;
4757                                 if (llua_getnumber(obj->data.s, &per)) {
4758                                         new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4759                                 }
4760                         }
4761 #endif /* X11 */
4762 #endif /* HAVE_LUA */
4763 #ifdef HDDTEMP
4764                         OBJ(hddtemp) {
4765                                 char *endptr, unit;
4766                                 long val;
4767                                 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4768                                         if (obj->data.hddtemp.temp)
4769                                                 free(obj->data.hddtemp.temp);
4770                                         obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4771                                                         obj->data.hddtemp.addr, obj->data.hddtemp.port);
4772                                         obj->data.hddtemp.update_time = current_update_time;
4773                                 }
4774                                 if (!obj->data.hddtemp.temp) {
4775                                         snprintf(p, p_max_size, "N/A");
4776                                 } else {
4777                                         val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4778                                         unit = obj->data.hddtemp.temp[0];
4779
4780                                         if (*endptr != '\0')
4781                                                 snprintf(p, p_max_size, "N/A");
4782                                         else if (unit == 'C')
4783                                                 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4784                                         else if (unit == 'F')
4785                                                 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4786                                         else
4787                                                 snprintf(p, p_max_size, "N/A");
4788                                 }
4789                         }
4790 #endif
4791                         OBJ(offset) {
4792                                 new_offset(p, obj->data.i);
4793                         }
4794                         OBJ(voffset) {
4795                                 new_voffset(p, obj->data.i);
4796                         }
4797 #ifdef __linux__
4798                         OBJ(i2c) {
4799                                 double r;
4800
4801                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4802                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4803
4804                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4805
4806                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4807                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4808                                 } else if (r >= 100.0 || r == 0) {
4809                                         snprintf(p, p_max_size, "%d", (int) r);
4810                                 } else {
4811                                         snprintf(p, p_max_size, "%.1f", r);
4812                                 }
4813                         }
4814                         OBJ(platform) {
4815                                 double r;
4816
4817                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4818                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4819
4820                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4821
4822                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4823                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4824                                 } else if (r >= 100.0 || r == 0) {
4825                                         snprintf(p, p_max_size, "%d", (int) r);
4826                                 } else {
4827                                         snprintf(p, p_max_size, "%.1f", r);
4828                                 }
4829                         }
4830                         OBJ(hwmon) {
4831                                 double r;
4832
4833                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4834                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4835
4836                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4837
4838                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4839                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4840                                 } else if (r >= 100.0 || r == 0) {
4841                                         snprintf(p, p_max_size, "%d", (int) r);
4842                                 } else {
4843                                         snprintf(p, p_max_size, "%.1f", r);
4844                                 }
4845                         }
4846 #endif /* __linux__ */
4847                         OBJ(alignr) {
4848                                 new_alignr(p, obj->data.i);
4849                         }
4850                         OBJ(alignc) {
4851                                 new_alignc(p, obj->data.i);
4852                         }
4853                         OBJ(if_empty) {
4854                                 char buf[max_user_text];
4855                                 struct information *tmp_info =
4856                                         malloc(sizeof(struct information));
4857                                 memcpy(tmp_info, cur, sizeof(struct information));
4858                                 generate_text_internal(buf, max_user_text,
4859                                                        *obj->sub, tmp_info);
4860
4861                                 if (strlen(buf) != 0) {
4862                                         DO_JUMP;
4863                                 }
4864                                 free(tmp_info);
4865                         }
4866                         OBJ(if_match) {
4867                                 char expression[max_user_text];
4868                                 int val;
4869                                 struct information *tmp_info;
4870
4871                                 tmp_info = malloc(sizeof(struct information));
4872                                 memcpy(tmp_info, cur, sizeof(struct information));
4873                                 generate_text_internal(expression, max_user_text,
4874                                                        *obj->sub, tmp_info);
4875                                 DBGP("parsed arg into '%s'", expression);
4876
4877                                 val = compare(expression);
4878                                 if (val == -2) {
4879                                         ERR("compare failed for expression '%s'",
4880                                                         expression);
4881                                 } else if (!val) {
4882                                         DO_JUMP;
4883                                 }
4884                                 free(tmp_info);
4885                         }
4886                         OBJ(if_existing) {
4887                                 if (obj->data.ifblock.str
4888                                     && !check_contains(obj->data.ifblock.s,
4889                                                        obj->data.ifblock.str)) {
4890                                         DO_JUMP;
4891                                 } else if (obj->data.ifblock.s
4892                                            && access(obj->data.ifblock.s, F_OK)) {
4893                                         DO_JUMP;
4894                                 }
4895                         }
4896                         OBJ(if_mounted) {
4897                                 if ((obj->data.ifblock.s)
4898                                                 && (!check_mount(obj->data.ifblock.s))) {
4899                                         DO_JUMP;
4900                                 }
4901                         }
4902                         OBJ(if_running) {
4903 #ifdef __linux__
4904                                 if (!get_process_by_name(obj->data.ifblock.s)) {
4905 #else
4906                                 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4907 #endif
4908                                         DO_JUMP;
4909                                 }
4910                         }
4911 #if defined(__linux__)
4912                         OBJ(ioscheduler) {
4913                                 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4914                         }
4915 #endif
4916                         OBJ(kernel) {
4917                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4918                         }
4919                         OBJ(machine) {
4920                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4921                         }
4922
4923                         /* memory stuff */
4924                         OBJ(mem) {
4925                                 human_readable(cur->mem * 1024, p, 255);
4926                         }
4927                         OBJ(memeasyfree) {
4928                                 human_readable(cur->memeasyfree * 1024, p, 255);
4929                         }
4930                         OBJ(memfree) {
4931                                 human_readable(cur->memfree * 1024, p, 255);
4932                         }
4933                         OBJ(memmax) {
4934                                 human_readable(cur->memmax * 1024, p, 255);
4935                         }
4936                         OBJ(memperc) {
4937                                 if (cur->memmax)
4938                                         percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4939                         }
4940 #ifdef X11
4941                         OBJ(memgauge){
4942                                 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4943                                         cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4944                         }
4945 #endif /* X11 */
4946                         OBJ(membar) {
4947 #ifdef X11
4948                                 if(output_methods & TO_X) {
4949                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
4950                                                 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4951                                 }else{
4952 #endif /* X11 */
4953                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4954                                         new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
4955 #ifdef X11
4956                                 }
4957 #endif /* X11 */
4958                         }
4959 #ifdef X11
4960                         OBJ(memgraph) {
4961                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4962                                         cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4963                                         100, 1, obj->char_a, obj->char_b);
4964                         }
4965 #endif /* X11 */
4966                         /* mixer stuff */
4967                         OBJ(mixer) {
4968                                 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4969                         }
4970                         OBJ(mixerl) {
4971                                 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4972                         }
4973                         OBJ(mixerr) {
4974                                 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4975                         }
4976 #ifdef X11
4977                         OBJ(mixerbar) {
4978                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4979                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4980                         }
4981                         OBJ(mixerlbar) {
4982                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4983                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4984                         }
4985                         OBJ(mixerrbar) {
4986                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4987                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4988                         }
4989 #endif /* X11 */
4990                         OBJ(if_mixer_mute) {
4991                                 if (!mixer_is_mute(obj->data.ifblock.i)) {
4992                                         DO_JUMP;
4993                                 }
4994                         }
4995 #ifdef X11
4996 #define NOT_IN_X "Not running in X"
4997                         OBJ(monitor) {
4998                                 if(x_initialised != YES) {
4999                                         strncpy(p, NOT_IN_X, p_max_size);
5000                                 }else{
5001                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
5002                                 }
5003                         }
5004                         OBJ(monitor_number) {
5005                                 if(x_initialised != YES) {
5006                                         strncpy(p, NOT_IN_X, p_max_size);
5007                                 }else{
5008                                         snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
5009                                 }
5010                         }
5011                         OBJ(desktop) {
5012                                 if(x_initialised != YES) {
5013                                         strncpy(p, NOT_IN_X, p_max_size);
5014                                 }else{
5015                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
5016                                 }
5017                         }
5018                         OBJ(desktop_number) {
5019                                 if(x_initialised != YES) {
5020                                         strncpy(p, NOT_IN_X, p_max_size);
5021                                 }else{
5022                                         snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
5023                                 }
5024                         }
5025                         OBJ(desktop_name) {
5026                                 if(x_initialised != YES) {
5027                                         strncpy(p, NOT_IN_X, p_max_size);
5028                                 }else if(cur->x11.desktop.name != NULL) {
5029                                         strncpy(p, cur->x11.desktop.name, p_max_size);
5030                                 }
5031                         }
5032 #endif /* X11 */
5033
5034                         /* mail stuff */
5035                         OBJ(mails) {
5036                                 update_mail_count(&obj->data.local_mail);
5037                                 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5038                         }
5039                         OBJ(new_mails) {
5040                                 update_mail_count(&obj->data.local_mail);
5041                                 snprintf(p, p_max_size, "%d",
5042                                         obj->data.local_mail.new_mail_count);
5043                         }
5044                         OBJ(seen_mails) {
5045                                 update_mail_count(&obj->data.local_mail);
5046                                 snprintf(p, p_max_size, "%d",
5047                                         obj->data.local_mail.seen_mail_count);
5048                         }
5049                         OBJ(unseen_mails) {
5050                                 update_mail_count(&obj->data.local_mail);
5051                                 snprintf(p, p_max_size, "%d",
5052                                         obj->data.local_mail.unseen_mail_count);
5053                         }
5054                         OBJ(flagged_mails) {
5055                                 update_mail_count(&obj->data.local_mail);
5056                                 snprintf(p, p_max_size, "%d",
5057                                         obj->data.local_mail.flagged_mail_count);
5058                         }
5059                         OBJ(unflagged_mails) {
5060                                 update_mail_count(&obj->data.local_mail);
5061                                 snprintf(p, p_max_size, "%d",
5062                                         obj->data.local_mail.unflagged_mail_count);
5063                         }
5064                         OBJ(forwarded_mails) {
5065                                 update_mail_count(&obj->data.local_mail);
5066                                 snprintf(p, p_max_size, "%d",
5067                                         obj->data.local_mail.forwarded_mail_count);
5068                         }
5069                         OBJ(unforwarded_mails) {
5070                                 update_mail_count(&obj->data.local_mail);
5071                                 snprintf(p, p_max_size, "%d",
5072                                         obj->data.local_mail.unforwarded_mail_count);
5073                         }
5074                         OBJ(replied_mails) {
5075                                 update_mail_count(&obj->data.local_mail);
5076                                 snprintf(p, p_max_size, "%d",
5077                                         obj->data.local_mail.replied_mail_count);
5078                         }
5079                         OBJ(unreplied_mails) {
5080                                 update_mail_count(&obj->data.local_mail);
5081                                 snprintf(p, p_max_size, "%d",
5082                                         obj->data.local_mail.unreplied_mail_count);
5083                         }
5084                         OBJ(draft_mails) {
5085                                 update_mail_count(&obj->data.local_mail);
5086                                 snprintf(p, p_max_size, "%d",
5087                                         obj->data.local_mail.draft_mail_count);
5088                         }
5089                         OBJ(trashed_mails) {
5090                                 update_mail_count(&obj->data.local_mail);
5091                                 snprintf(p, p_max_size, "%d",
5092                                         obj->data.local_mail.trashed_mail_count);
5093                         }
5094                         OBJ(mboxscan) {
5095                                 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5096                                         text_buffer_size);
5097                                 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5098                         }
5099                         OBJ(nodename) {
5100                                 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5101                         }
5102                         OBJ(outlinecolor) {
5103                                 new_outline(p, obj->data.l);
5104                         }
5105                         OBJ(processes) {
5106                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5107                         }
5108                         OBJ(running_processes) {
5109                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5110                         }
5111                         OBJ(text) {
5112                                 snprintf(p, p_max_size, "%s", obj->data.s);
5113                         }
5114 #ifdef X11
5115                         OBJ(shadecolor) {
5116                                 new_bg(p, obj->data.l);
5117                         }
5118                         OBJ(stippled_hr) {
5119                                 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5120                         }
5121 #endif /* X11 */
5122                         OBJ(swap) {
5123                                 human_readable(cur->swap * 1024, p, 255);
5124                         }
5125                         OBJ(swapfree) {
5126                                 human_readable(cur->swapfree * 1024, p, 255);
5127                         }
5128                         OBJ(swapmax) {
5129                                 human_readable(cur->swapmax * 1024, p, 255);
5130                         }
5131                         OBJ(swapperc) {
5132                                 if (cur->swapmax == 0) {
5133                                         strncpy(p, "No swap", p_max_size);
5134                                 } else {
5135                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5136                                 }
5137                         }
5138                         OBJ(swapbar) {
5139 #ifdef X11
5140                                 if(output_methods & TO_X) {
5141                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
5142                                                 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5143                                 }else{
5144 #endif /* X11 */
5145                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5146                                         new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5147 #ifdef X11
5148                                 }
5149 #endif /* X11 */
5150                         }
5151                         OBJ(sysname) {
5152                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5153                         }
5154                         OBJ(time) {
5155                                 time_t t = time(NULL);
5156                                 struct tm *tm = localtime(&t);
5157
5158                                 setlocale(LC_TIME, "");
5159                                 strftime(p, p_max_size, obj->data.s, tm);
5160                         }
5161                         OBJ(utime) {
5162                                 time_t t = time(NULL);
5163                                 struct tm *tm = gmtime(&t);
5164
5165                                 strftime(p, p_max_size, obj->data.s, tm);
5166                         }
5167                         OBJ(tztime) {
5168                                 char *oldTZ = NULL;
5169                                 time_t t;
5170                                 struct tm *tm;
5171
5172                                 if (obj->data.tztime.tz) {
5173                                         oldTZ = getenv("TZ");
5174                                         setenv("TZ", obj->data.tztime.tz, 1);
5175                                         tzset();
5176                                 }
5177                                 t = time(NULL);
5178                                 tm = localtime(&t);
5179
5180                                 setlocale(LC_TIME, "");
5181                                 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5182                                 if (oldTZ) {
5183                                         setenv("TZ", oldTZ, 1);
5184                                         tzset();
5185                                 } else {
5186                                         unsetenv("TZ");
5187                                 }
5188                                 // Needless to free oldTZ since getenv gives ptr to static data
5189                         }
5190                         OBJ(totaldown) {
5191                                 human_readable(obj->data.net->recv, p, 255);
5192                         }
5193                         OBJ(totalup) {
5194                                 human_readable(obj->data.net->trans, p, 255);
5195                         }
5196                         OBJ(updates) {
5197                                 snprintf(p, p_max_size, "%d", total_updates);
5198                         }
5199                         OBJ(if_updatenr) {
5200                                 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5201                                         DO_JUMP;
5202                                 }
5203                         }
5204                         OBJ(upspeed) {
5205                                 human_readable(obj->data.net->trans_speed, p, 255);
5206                         }
5207                         OBJ(upspeedf) {
5208                                 spaced_print(p, p_max_size, "%.1f", 8,
5209                                         obj->data.net->trans_speed / 1024.0);
5210                         }
5211 #ifdef X11
5212                         OBJ(upspeedgraph) {
5213                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5214                                         obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5215                         }
5216 #endif /* X11 */
5217                         OBJ(uptime_short) {
5218                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
5219                         }
5220                         OBJ(uptime) {
5221                                 format_seconds(p, p_max_size, (int) cur->uptime);
5222                         }
5223                         OBJ(user_names) {
5224                                 snprintf(p, p_max_size, "%s", cur->users.names);
5225                         }
5226                         OBJ(user_terms) {
5227                                 snprintf(p, p_max_size, "%s", cur->users.terms);
5228                         }
5229                         OBJ(user_times) {
5230                                 snprintf(p, p_max_size, "%s", cur->users.times);
5231                         }
5232                         OBJ(user_number) {
5233                                 snprintf(p, p_max_size, "%d", cur->users.number);
5234                         }
5235 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5236                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5237                         OBJ(apm_adapter) {
5238                                 char *msg;
5239
5240                                 msg = get_apm_adapter();
5241                                 snprintf(p, p_max_size, "%s", msg);
5242                                 free(msg);
5243                         }
5244                         OBJ(apm_battery_life) {
5245                                 char *msg;
5246
5247                                 msg = get_apm_battery_life();
5248                                 snprintf(p, p_max_size, "%s", msg);
5249                                 free(msg);
5250                         }
5251                         OBJ(apm_battery_time) {
5252                                 char *msg;
5253
5254                                 msg = get_apm_battery_time();
5255                                 snprintf(p, p_max_size, "%s", msg);
5256                                 free(msg);
5257                         }
5258 #endif /* __FreeBSD__ __OpenBSD__ */
5259
5260 #ifdef MPD
5261 #define mpd_printf(fmt, val) \
5262         snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5263 #define mpd_sprintf(val) { \
5264         if (!obj->data.i || obj->data.i > p_max_size) \
5265                 mpd_printf("%s", val); \
5266         else \
5267                 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5268 }
5269                         OBJ(mpd_title)
5270                                 mpd_sprintf(title);
5271                         OBJ(mpd_artist)
5272                                 mpd_sprintf(artist);
5273                         OBJ(mpd_album)
5274                                 mpd_sprintf(album);
5275                         OBJ(mpd_random)
5276                                 mpd_printf("%s", random);
5277                         OBJ(mpd_repeat)
5278                                 mpd_printf("%s", repeat);
5279                         OBJ(mpd_track)
5280                                 mpd_sprintf(track);
5281                         OBJ(mpd_name)
5282                                 mpd_sprintf(name);
5283                         OBJ(mpd_file)
5284                                 mpd_sprintf(file);
5285                         OBJ(mpd_vol)
5286                                 mpd_printf("%d", volume);
5287                         OBJ(mpd_bitrate)
5288                                 mpd_printf("%d", bitrate);
5289                         OBJ(mpd_status)
5290                                 mpd_printf("%s", status);
5291                         OBJ(mpd_elapsed) {
5292                                 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5293                         }
5294                         OBJ(mpd_length) {
5295                                 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5296                         }
5297                         OBJ(mpd_percent) {
5298                                 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5299                         }
5300                         OBJ(mpd_bar) {
5301 #ifdef X11
5302                                 if(output_methods & TO_X) {
5303                                         new_bar(p, obj->data.pair.a, obj->data.pair.b,
5304                                                 (int) (mpd_get_info()->progress * 255.0f));
5305                                 } else {
5306 #endif /* X11 */
5307                                         if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5308                                         new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5309 #ifdef X11
5310                                 }
5311 #endif /* X11 */
5312                         }
5313                         OBJ(mpd_smart) {
5314                                 struct mpd_s *mpd = mpd_get_info();
5315                                 int len = obj->data.i;
5316                                 if (len == 0 || len > p_max_size)
5317                                         len = p_max_size;
5318
5319                                 memset(p, 0, p_max_size);
5320                                 if (mpd->artist && *mpd->artist &&
5321                                     mpd->title && *mpd->title) {
5322                                         snprintf(p, len, "%s - %s", mpd->artist,
5323                                                 mpd->title);
5324                                 } else if (mpd->title && *mpd->title) {
5325                                         snprintf(p, len, "%s", mpd->title);
5326                                 } else if (mpd->artist && *mpd->artist) {
5327                                         snprintf(p, len, "%s", mpd->artist);
5328                                 } else if (mpd->file && *mpd->file) {
5329                                         snprintf(p, len, "%s", mpd->file);
5330                                 } else {
5331                                         *p = 0;
5332                                 }
5333                         }
5334                         OBJ(if_mpd_playing) {
5335                                 if (!mpd_get_info()->is_playing) {
5336                                         DO_JUMP;
5337                                 }
5338                         }
5339 #undef mpd_sprintf
5340 #undef mpd_printf
5341 #endif
5342
5343 #ifdef MOC
5344 #define MOC_PRINT(t, a) \
5345         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5346                         OBJ(moc_state) {
5347                                 MOC_PRINT(state, "??");
5348                         }
5349                         OBJ(moc_file) {
5350                                 MOC_PRINT(file, "no file");
5351                         }
5352                         OBJ(moc_title) {
5353                                 MOC_PRINT(title, "no title");
5354                         }
5355                         OBJ(moc_artist) {
5356                                 MOC_PRINT(artist, "no artist");
5357                         }
5358                         OBJ(moc_song) {
5359                                 MOC_PRINT(song, "no song");
5360                         }
5361                         OBJ(moc_album) {
5362                                 MOC_PRINT(album, "no album");
5363                         }
5364                         OBJ(moc_totaltime) {
5365                                 MOC_PRINT(totaltime, "0:00");
5366                         }
5367                         OBJ(moc_timeleft) {
5368                                 MOC_PRINT(timeleft, "0:00");
5369                         }
5370                         OBJ(moc_curtime) {
5371                                 MOC_PRINT(curtime, "0:00");
5372                         }
5373                         OBJ(moc_bitrate) {
5374                                 MOC_PRINT(bitrate, "0Kbps");
5375                         }
5376                         OBJ(moc_rate) {
5377                                 MOC_PRINT(rate, "0KHz");
5378                         }
5379 #undef MOC_PRINT
5380 #endif /* MOC */
5381 #ifdef XMMS2
5382                         OBJ(xmms2_artist) {
5383                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5384                         }
5385                         OBJ(xmms2_album) {
5386                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5387                         }
5388                         OBJ(xmms2_title) {
5389                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5390                         }
5391                         OBJ(xmms2_genre) {
5392                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5393                         }
5394                         OBJ(xmms2_comment) {
5395                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5396                         }
5397                         OBJ(xmms2_url) {
5398                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5399                         }
5400                         OBJ(xmms2_status) {
5401                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5402                         }
5403                         OBJ(xmms2_date) {
5404                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5405                         }
5406                         OBJ(xmms2_tracknr) {
5407                                 if (cur->xmms2.tracknr != -1) {
5408                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5409                                 }
5410                         }
5411                         OBJ(xmms2_bitrate) {
5412                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5413                         }
5414                         OBJ(xmms2_id) {
5415                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5416                         }
5417                         OBJ(xmms2_size) {
5418                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5419                         }
5420                         OBJ(xmms2_elapsed) {
5421                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5422                                         (cur->xmms2.elapsed / 1000) % 60);
5423                         }
5424                         OBJ(xmms2_duration) {
5425                                 snprintf(p, p_max_size, "%02d:%02d",
5426                                         cur->xmms2.duration / 60000,
5427                                         (cur->xmms2.duration / 1000) % 60);
5428                         }
5429                         OBJ(xmms2_percent) {
5430                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5431                         }
5432 #ifdef X11
5433                         OBJ(xmms2_bar) {
5434                                 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5435                                         (int) (cur->xmms2.progress * 255.0f));
5436                         }
5437 #endif /* X11 */
5438                         OBJ(xmms2_playlist) {
5439                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5440                         }
5441                         OBJ(xmms2_timesplayed) {
5442                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5443                         }
5444                         OBJ(xmms2_smart) {
5445                                 if (strlen(cur->xmms2.title) < 2
5446                                                 && strlen(cur->xmms2.title) < 2) {
5447                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
5448                                 } else {
5449                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5450                                                 cur->xmms2.title);
5451                                 }
5452                         }
5453                         OBJ(if_xmms2_connected) {
5454                                 if (cur->xmms2.conn_state != 1) {
5455                                         DO_JUMP;
5456                                 }
5457                         }
5458 #endif /* XMMS */
5459 #ifdef AUDACIOUS
5460                         OBJ(audacious_status) {
5461                                 snprintf(p, p_max_size, "%s",
5462                                         cur->audacious.items[AUDACIOUS_STATUS]);
5463                         }
5464                         OBJ(audacious_title) {
5465                                 snprintf(p, cur->audacious.max_title_len > 0
5466                                         ? cur->audacious.max_title_len : p_max_size, "%s",
5467                                         cur->audacious.items[AUDACIOUS_TITLE]);
5468                         }
5469                         OBJ(audacious_length) {
5470                                 snprintf(p, p_max_size, "%s",
5471                                         cur->audacious.items[AUDACIOUS_LENGTH]);
5472                         }
5473                         OBJ(audacious_length_seconds) {
5474                                 snprintf(p, p_max_size, "%s",
5475                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5476                         }
5477                         OBJ(audacious_position) {
5478                                 snprintf(p, p_max_size, "%s",
5479                                         cur->audacious.items[AUDACIOUS_POSITION]);
5480                         }
5481                         OBJ(audacious_position_seconds) {
5482                                 snprintf(p, p_max_size, "%s",
5483                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5484                         }
5485                         OBJ(audacious_bitrate) {
5486                                 snprintf(p, p_max_size, "%s",
5487                                         cur->audacious.items[AUDACIOUS_BITRATE]);
5488                         }
5489                         OBJ(audacious_frequency) {
5490                                 snprintf(p, p_max_size, "%s",
5491                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
5492                         }
5493                         OBJ(audacious_channels) {
5494                                 snprintf(p, p_max_size, "%s",
5495                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
5496                         }
5497                         OBJ(audacious_filename) {
5498                                 snprintf(p, p_max_size, "%s",
5499                                         cur->audacious.items[AUDACIOUS_FILENAME]);
5500                         }
5501                         OBJ(audacious_playlist_length) {
5502                                 snprintf(p, p_max_size, "%s",
5503                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5504                         }
5505                         OBJ(audacious_playlist_position) {
5506                                 snprintf(p, p_max_size, "%s",
5507                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5508                         }
5509                         OBJ(audacious_main_volume) {
5510                                 snprintf(p, p_max_size, "%s",
5511                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5512                         }
5513 #ifdef X11
5514                         OBJ(audacious_bar) {
5515                                 double progress;
5516
5517                                 progress =
5518                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5519                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5520                                 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5521                         }
5522 #endif /* X11 */
5523 #endif /* AUDACIOUS */
5524
5525 #ifdef BMPX
5526                         OBJ(bmpx_title) {
5527                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5528                         }
5529                         OBJ(bmpx_artist) {
5530                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5531                         }
5532                         OBJ(bmpx_album) {
5533                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5534                         }
5535                         OBJ(bmpx_uri) {
5536                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5537                         }
5538                         OBJ(bmpx_track) {
5539                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5540                         }
5541                         OBJ(bmpx_bitrate) {
5542                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5543                         }
5544 #endif /* BMPX */
5545                         /* we have four different types of top (top, top_mem,
5546                          * top_time and top_io). To avoid having almost-same code four
5547                          * times, we have this special handler. */
5548                         break;
5549                         case OBJ_top:
5550                                 parse_top_args("top", obj->data.top.s, obj);
5551                                 if (!needed) needed = cur->cpu;
5552                         case OBJ_top_mem:
5553                                 parse_top_args("top_mem", obj->data.top.s, obj);
5554                                 if (!needed) needed = cur->memu;
5555                         case OBJ_top_time:
5556                                 parse_top_args("top_time", obj->data.top.s, obj);
5557                                 if (!needed) needed = cur->time;
5558 #ifdef IOSTATS
5559                         case OBJ_top_io:
5560                                 parse_top_args("top_io", obj->data.top.s, obj);
5561                                 if (!needed) needed = cur->io;
5562 #endif
5563
5564                                 if (needed[obj->data.top.num]) {
5565                                         char *timeval;
5566
5567                                         switch (obj->data.top.type) {
5568                                                 case TOP_NAME:
5569                                                         snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5570                                                                         needed[obj->data.top.num]->name);
5571                                                         break;
5572                                                 case TOP_CPU:
5573                                                         snprintf(p, 7, "%6.2f",
5574                                                                         needed[obj->data.top.num]->amount);
5575                                                         break;
5576                                                 case TOP_PID:
5577                                                         snprintf(p, 6, "%5i",
5578                                                                         needed[obj->data.top.num]->pid);
5579                                                         break;
5580                                                 case TOP_MEM:
5581                                                         snprintf(p, 7, "%6.2f",
5582                                                                         needed[obj->data.top.num]->totalmem);
5583                                                         break;
5584                                                 case TOP_TIME:
5585                                                         timeval = format_time(
5586                                                                         needed[obj->data.top.num]->total_cpu_time, 9);
5587                                                         snprintf(p, 10, "%9s", timeval);
5588                                                         free(timeval);
5589                                                         break;
5590                                                 case TOP_MEM_RES:
5591                                                         human_readable(needed[obj->data.top.num]->rss,
5592                                                                         p, 255);
5593                                                         break;
5594                                                 case TOP_MEM_VSIZE:
5595                                                         human_readable(needed[obj->data.top.num]->vsize,
5596                                                                         p, 255);
5597                                                         break;
5598 #ifdef IOSTATS
5599                                                 case TOP_READ_BYTES:
5600                                                         human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5601                                                                         p, 255);
5602                                                         break;
5603                                                 case TOP_WRITE_BYTES:
5604                                                         human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5605                                                                         p, 255);
5606                                                         break;
5607                                                 case TOP_IO_PERC:
5608                                                         snprintf(p, 7, "%6.2f",
5609                                                                         needed[obj->data.top.num]->io_perc);
5610                                                         break;
5611 #endif
5612                                         }
5613                                 }
5614                         OBJ(tail) {
5615                                 print_tailhead("tail", obj, p, p_max_size);
5616                         }
5617                         OBJ(head) {
5618                                 print_tailhead("head", obj, p, p_max_size);
5619                         }
5620                         OBJ(lines) {
5621                                 FILE *fp = open_file(obj->data.s, &obj->a);
5622
5623                                 if(fp != NULL) {
5624 /* FIXME: use something more general (see also tail.c, head.c */
5625 #define BUFSZ 0x1000
5626                                         char buf[BUFSZ];
5627                                         int j, lines;
5628
5629                                         lines = 0;
5630                                         while(fgets(buf, BUFSZ, fp) != NULL){
5631                                                 for(j = 0; buf[j] != 0; j++) {
5632                                                         if(buf[j] == '\n') {
5633                                                                 lines++;
5634                                                         }
5635                                                 }
5636                                         }
5637                                         sprintf(p, "%d", lines);
5638                                         fclose(fp);
5639                                 } else {
5640                                         sprintf(p, "File Unreadable");
5641                                 }
5642                         }
5643
5644                         OBJ(words) {
5645                                 FILE *fp = open_file(obj->data.s, &obj->a);
5646
5647                                 if(fp != NULL) {
5648                                         char buf[BUFSZ];
5649                                         int j, words;
5650                                         char inword = FALSE;
5651
5652                                         words = 0;
5653                                         while(fgets(buf, BUFSZ, fp) != NULL){
5654                                                 for(j = 0; buf[j] != 0; j++) {
5655                                                         if(!isspace(buf[j])) {
5656                                                                 if(inword == FALSE) {
5657                                                                         words++;
5658                                                                         inword = TRUE;
5659                                                                 }
5660                                                         } else {
5661                                                                 inword = FALSE;
5662                                                         }
5663                                                 }
5664                                         }
5665                                         sprintf(p, "%d", words);
5666                                         fclose(fp);
5667                                 } else {
5668                                         sprintf(p, "File Unreadable");
5669                                 }
5670                         }
5671 #ifdef TCP_PORT_MONITOR
5672                         OBJ(tcp_portmon) {
5673                                 tcp_portmon_action(p, p_max_size,
5674                                                    &obj->data.tcp_port_monitor);
5675                         }
5676 #endif /* TCP_PORT_MONITOR */
5677
5678 #ifdef HAVE_ICONV
5679                         OBJ(iconv_start) {
5680                                 iconv_converting = 1;
5681                                 iconv_selected = obj->a;
5682                         }
5683                         OBJ(iconv_stop) {
5684                                 iconv_converting = 0;
5685                                 iconv_selected = 0;
5686                         }
5687 #endif /* HAVE_ICONV */
5688
5689                         OBJ(entropy_avail) {
5690                                 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5691                         }
5692                         OBJ(entropy_perc) {
5693                                 percent_print(p, p_max_size,
5694                                               cur->entropy.entropy_avail *
5695                                               100 / cur->entropy.poolsize);
5696                         }
5697                         OBJ(entropy_poolsize) {
5698                                 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5699                         }
5700                         OBJ(entropy_bar) {
5701                                 double entropy_perc;
5702
5703                                 entropy_perc = (double) cur->entropy.entropy_avail /
5704                                         (double) cur->entropy.poolsize;
5705 #ifdef X11
5706                                 if(output_methods & TO_X) {
5707                                         new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5708                                 } else {
5709 #endif /* X11 */
5710                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5711                                         new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5712 #ifdef X11
5713                                 }
5714 #endif /* X11 */
5715                         }
5716 #ifdef IBM
5717                         OBJ(smapi) {
5718                                 char *s;
5719                                 if(obj->data.s) {
5720                                         s = smapi_get_val(obj->data.s);
5721                                         snprintf(p, p_max_size, "%s", s);
5722                                         free(s);
5723                                 }
5724                         }
5725                         OBJ(if_smapi_bat_installed) {
5726                                 int idx;
5727                                 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5728                                         if(!smapi_bat_installed(idx)) {
5729                                                 DO_JUMP;
5730                                         }
5731                                 } else
5732                                         ERR("argument to if_smapi_bat_installed must be an integer");
5733                         }
5734                         OBJ(smapi_bat_perc) {
5735                                 int idx, val;
5736                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5737                                         val = smapi_bat_installed(idx) ?
5738                                                 smapi_get_bat_int(idx, "remaining_percent") : 0;
5739                                         percent_print(p, p_max_size, val);
5740                                 } else
5741                                         ERR("argument to smapi_bat_perc must be an integer");
5742                         }
5743                         OBJ(smapi_bat_temp) {
5744                                 int idx, val;
5745                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5746                                         val = smapi_bat_installed(idx) ?
5747                                                 smapi_get_bat_int(idx, "temperature") : 0;
5748                                         /* temperature is in milli degree celsius */
5749                                         temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5750                                 } else
5751                                         ERR("argument to smapi_bat_temp must be an integer");
5752                         }
5753                         OBJ(smapi_bat_power) {
5754                                 int idx, val;
5755                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5756                                         val = smapi_bat_installed(idx) ?
5757                                                 smapi_get_bat_int(idx, "power_now") : 0;
5758                                         /* power_now is in mW, set to W with one digit precision */
5759                                         snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5760                                 } else
5761                                         ERR("argument to smapi_bat_power must be an integer");
5762                         }
5763 #ifdef X11
5764                         OBJ(smapi_bat_bar) {
5765                                 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5766                                         new_bar(p, obj->a, obj->b, (int)
5767                                                         (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5768                                 else
5769                                         new_bar(p, obj->a, obj->b, 0);
5770                         }
5771 #endif /* X11 */
5772 #endif /* IBM */
5773                         OBJ(blink) {
5774                                 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5775                                 char buf[max_user_text];
5776                                 unsigned int j;
5777
5778                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5779                                 snprintf(p, p_max_size, "%s", buf);
5780                                 if(total_updates % 2) {
5781                                         for(j=0; p[j] != 0; j++) {
5782                                                 p[j] = ' ';
5783                                         }
5784                                 }
5785                         }
5786                         OBJ(to_bytes) {
5787                                 char buf[max_user_text];
5788                                 long long bytes;
5789                                 char unit[16];  // 16 because we can also have long names (like mega-bytes)
5790
5791                                 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5792                                 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5793                                         if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5794                                         else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5795                                         else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5796                                         else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5797                                         else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5798                                 }
5799                                 snprintf(p, p_max_size, "%s", buf);
5800                         }
5801                         OBJ(scroll) {
5802                                 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
5803                                 char *pwithcolors;
5804                                 char buf[max_user_text];
5805                                 generate_text_internal(buf, max_user_text,
5806                                                        *obj->sub, cur);
5807                                 for(j = 0; buf[j] != 0; j++) {
5808                                         switch(buf[j]) {
5809                                         case '\n':      //place all the lines behind each other with LINESEPARATOR between them
5810 #define LINESEPARATOR '|'
5811                                                 buf[j]=LINESEPARATOR;
5812                                                 break;
5813                                         case SPECIAL_CHAR:
5814                                                 colorchanges++;
5815                                                 break;
5816                                         }
5817                                 }
5818                                 //no scrolling necessary if the length of the text to scroll is too short
5819                                 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
5820                                         snprintf(p, p_max_size, "%s", buf);
5821                                         break;
5822                                 }
5823                                 //make sure a colorchange at the front is not part of the string we are going to show
5824                                 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
5825                                         obj->data.scroll.start++;
5826                                 }
5827                                 //place all chars that should be visible in p, including colorchanges
5828                                 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
5829                                         p[j] = *(buf + obj->data.scroll.start + j);
5830                                         if(p[j] == SPECIAL_CHAR) {
5831                                                 visibcolorchanges++;
5832                                         }
5833                                         //if there is still room fill it with spaces
5834                                         if( ! p[j]) break;
5835                                 }
5836                                 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
5837                                         p[j] = ' ';
5838                                 }
5839                                 p[j] = 0;
5840                                 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
5841                                 for(j = 0; j < obj->data.scroll.start; j++) {
5842                                         if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
5843                                 }
5844                                 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
5845                                 for(j = 0; j < frontcolorchanges; j++) {
5846                                         pwithcolors[j] = SPECIAL_CHAR;
5847                                 }
5848                                 pwithcolors[j] = 0;
5849                                 strcat(pwithcolors,p);
5850                                 strend = strlen(pwithcolors);
5851                                 //and place the colorchanges not in front or in the visible part behind the visible part
5852                                 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
5853                                         pwithcolors[strend + j] = SPECIAL_CHAR;
5854                                 }
5855                                 pwithcolors[strend + j] = 0;
5856                                 strcpy(p, pwithcolors);
5857                                 free(pwithcolors);
5858                                 //scroll
5859                                 obj->data.scroll.start += obj->data.scroll.step;
5860                                 if(buf[obj->data.scroll.start] == 0){
5861                                          obj->data.scroll.start = 0;
5862                                 }
5863 #ifdef X11
5864                                 //reset color when scroll is finished
5865                                 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
5866 #endif
5867                         }
5868                         OBJ(combine) {
5869                                 char buf[2][max_user_text];
5870                                 int i, j;
5871                                 long longest=0;
5872                                 int nextstart;
5873                                 int nr_rows[2];
5874                                 struct llrows {
5875                                         char* row;
5876                                         struct llrows* next;
5877                                 };
5878                                 struct llrows *ll_rows[2], *current[2];
5879                                 struct text_object * objsub = obj->sub;
5880
5881                                 p[0]=0;
5882                                 for(i=0; i<2; i++) {
5883                                         nr_rows[i] = 1;
5884                                         nextstart = 0;
5885                                         ll_rows[i] = malloc(sizeof(struct llrows));
5886                                         current[i] = ll_rows[i];
5887                                         for(j=0; j<i; j++) objsub = objsub->sub;
5888                                         generate_text_internal(buf[i], max_user_text, *objsub, cur);
5889                                         for(j=0; buf[i][j] != 0; j++) {
5890                                                 if(buf[i][j] == '\t') buf[i][j] = ' ';
5891                                                 if(buf[i][j] == '\n') {
5892                                                         buf[i][j] = 0;
5893                                                         current[i]->row = strdup(buf[i]+nextstart);
5894                                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5895                                                         current[i]->next = malloc(sizeof(struct llrows));
5896                                                         current[i] = current[i]->next;
5897                                                         nextstart = j + 1;
5898                                                         nr_rows[i]++;
5899                                                 }
5900                                         }
5901                                         current[i]->row = strdup(buf[i]+nextstart);
5902                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5903                                         current[i]->next = NULL;
5904                                         current[i] = ll_rows[i];
5905                                 }
5906                                 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5907                                         if(current[0]) {
5908                                                 strcat(p, current[0]->row);
5909                                                 i=strlen(current[0]->row);
5910                                         }else i = 0;
5911                                         while(i < longest) {
5912                                                 strcat(p, " ");
5913                                                 i++;
5914                                         }
5915                                         if(current[1]) {
5916                                                 strcat(p, obj->data.combine.seperation);
5917                                                 strcat(p, current[1]->row);
5918                                         }
5919                                         strcat(p, "\n");
5920                                         #ifdef HAVE_OPENMP
5921                                         #pragma omp parallel for schedule(dynamic,10)
5922                                         #endif /* HAVE_OPENMP */
5923                                         for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5924                                 }
5925                                 #ifdef HAVE_OPENMP
5926                                 #pragma omp parallel for schedule(dynamic,10)
5927                                 #endif /* HAVE_OPENMP */
5928                                 for(i=0; i<2; i++) {
5929                                         while(ll_rows[i] != NULL) {
5930                                                 current[i]=ll_rows[i];
5931                                                 free(current[i]->row);
5932                                                 ll_rows[i]=current[i]->next;
5933                                                 free(current[i]);
5934                                         }
5935                                 }
5936                         }
5937 #ifdef NVIDIA
5938                         OBJ(nvidia) {
5939                                 int value = get_nvidia_value(obj->data.nvidia.type, display);
5940                                 if(value == -1)
5941                                         snprintf(p, p_max_size, "N/A");
5942                                 else if (obj->data.nvidia.type == NV_TEMP)
5943                                         temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5944                                 else if (obj->data.nvidia.print_as_float &&
5945                                                 value > 0 && value < 100)
5946                                         snprintf(p, p_max_size, "%.1f", (float)value);
5947                                 else
5948                                         snprintf(p, p_max_size, "%d", value);
5949                         }
5950 #endif /* NVIDIA */
5951 #ifdef APCUPSD
5952                         OBJ(apcupsd) {
5953                                 /* This is just a meta-object to set host:port */
5954                         }
5955                         OBJ(apcupsd_name) {
5956                                 snprintf(p, p_max_size, "%s",
5957                                                  cur->apcupsd.items[APCUPSD_NAME]);
5958                         }
5959                         OBJ(apcupsd_model) {
5960                                 snprintf(p, p_max_size, "%s",
5961                                                  cur->apcupsd.items[APCUPSD_MODEL]);
5962                         }
5963                         OBJ(apcupsd_upsmode) {
5964                                 snprintf(p, p_max_size, "%s",
5965                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
5966                         }
5967                         OBJ(apcupsd_cable) {
5968                                 snprintf(p, p_max_size, "%s",
5969                                                  cur->apcupsd.items[APCUPSD_CABLE]);
5970                         }
5971                         OBJ(apcupsd_status) {
5972                                 snprintf(p, p_max_size, "%s",
5973                                                  cur->apcupsd.items[APCUPSD_STATUS]);
5974                         }
5975                         OBJ(apcupsd_linev) {
5976                                 snprintf(p, p_max_size, "%s",
5977                                                  cur->apcupsd.items[APCUPSD_LINEV]);
5978                         }
5979                         OBJ(apcupsd_load) {
5980                                 snprintf(p, p_max_size, "%s",
5981                                                  cur->apcupsd.items[APCUPSD_LOAD]);
5982                         }
5983                         OBJ(apcupsd_loadbar) {
5984                                 double progress;
5985 #ifdef X11
5986                                 if(output_methods & TO_X) {
5987                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5988                                         new_bar(p, obj->a, obj->b, (int) progress);
5989                                 } else {
5990 #endif /* X11 */
5991                                         progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5992                                         if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5993                                         new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5994 #ifdef X11
5995                                 }
5996 #endif /* X11 */
5997                         }
5998 #ifdef X11
5999                         OBJ(apcupsd_loadgraph) {
6000                                 double progress;
6001                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
6002                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
6003                                                   (int)progress, 100, 1, obj->char_a, obj->char_b);
6004                         }
6005                         OBJ(apcupsd_loadgauge) {
6006                                 double progress;
6007                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
6008                                 new_gauge(p, obj->a, obj->b,
6009                                                   (int)progress);
6010                         }
6011 #endif /* X11 */
6012                         OBJ(apcupsd_charge) {
6013                                 snprintf(p, p_max_size, "%s",
6014                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
6015                         }
6016                         OBJ(apcupsd_timeleft) {
6017                                 snprintf(p, p_max_size, "%s",
6018                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
6019                         }
6020                         OBJ(apcupsd_temp) {
6021                                 snprintf(p, p_max_size, "%s",
6022                                                  cur->apcupsd.items[APCUPSD_TEMP]);
6023                         }
6024                         OBJ(apcupsd_lastxfer) {
6025                                 snprintf(p, p_max_size, "%s",
6026                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
6027                         }
6028 #endif /* APCUPSD */
6029                         break;
6030                 }
6031 #undef DO_JUMP
6032
6033
6034                 {
6035                         unsigned int a = strlen(p);
6036
6037 #ifdef HAVE_ICONV
6038                         if (a > 0 && iconv_converting && iconv_selected > 0
6039                                         && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6040                                 int bytes;
6041                                 size_t dummy1, dummy2;
6042 #ifdef __FreeBSD__
6043                                 const char *ptr = buff_in;
6044 #else
6045                                 char *ptr = buff_in;
6046 #endif
6047                                 char *outptr = p;
6048
6049                                 dummy1 = dummy2 = a;
6050
6051                                 strncpy(buff_in, p, p_max_size);
6052
6053                                 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6054                                 while (dummy1 > 0) {
6055                                         bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6056                                                         &outptr, &dummy2);
6057                                         if (bytes == -1) {
6058                                                 ERR("Iconv codeset conversion failed");
6059                                                 break;
6060                                         }
6061                                 }
6062
6063                                 /* It is nessecary when we are converting from multibyte to
6064                                  * singlebyte codepage */
6065                                 a = outptr - p;
6066                         }
6067 #endif /* HAVE_ICONV */
6068                         if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi) {
6069                                 substitute_newlines(p, a - 2);
6070                         }
6071                         p += a;
6072                         p_max_size -= a;
6073                 }
6074                 obj = obj->next;
6075         }
6076 #ifdef X11
6077         /* load any new fonts we may have had */
6078         if (need_to_load_fonts) {
6079                 load_fonts();
6080         }
6081 #endif /* X11 */
6082 }
6083
6084 void evaluate(char *text, char *buffer)
6085 {
6086         struct information *tmp_info;
6087         struct text_object subroot;
6088
6089         tmp_info = malloc(sizeof(struct information));
6090         memcpy(tmp_info, &info, sizeof(struct information));
6091         parse_conky_vars(&subroot, text, buffer, tmp_info);
6092         DBGP("evaluated '%s' to '%s'", text, buffer);
6093
6094         free_text_objects(&subroot, 1);
6095         free(tmp_info);
6096 }
6097
6098 double current_update_time, next_update_time, last_update_time;
6099
6100 static void generate_text(void)
6101 {
6102         struct information *cur = &info;
6103         char *p;
6104
6105         special_count = 0;
6106
6107         /* update info */
6108
6109         current_update_time = get_time();
6110
6111         update_stuff();
6112
6113         /* add things to the buffer */
6114
6115         /* generate text */
6116
6117         p = text_buffer;
6118
6119         generate_text_internal(p, max_user_text, global_root_object, cur);
6120
6121         if (stuff_in_uppercase) {
6122                 char *tmp_p;
6123
6124                 tmp_p = text_buffer;
6125                 while (*tmp_p) {
6126                         *tmp_p = toupper(*tmp_p);
6127                         tmp_p++;
6128                 }
6129         }
6130
6131         next_update_time += update_interval;
6132         if (next_update_time < get_time()) {
6133                 next_update_time = get_time() + update_interval;
6134         } else if (next_update_time > get_time() + update_interval) {
6135                 next_update_time = get_time() + update_interval;
6136         }
6137         last_update_time = current_update_time;
6138         total_updates++;
6139 }
6140
6141 void set_update_interval(double interval)
6142 {
6143         update_interval = interval;
6144         update_interval_old = interval;
6145 }
6146
6147 static inline int get_string_width(const char *s)
6148 {
6149 #ifdef X11
6150         if (output_methods & TO_X) {
6151                 return *s ? calc_text_width(s, strlen(s)) : 0;
6152         }
6153 #endif /* X11 */
6154         return strlen(s);
6155 }
6156
6157 #ifdef X11
6158 static int get_string_width_special(char *s, int special_index)
6159 {
6160         char *p, *final;
6161         int idx = 1;
6162         int width = 0;
6163         long i;
6164
6165         if ((output_methods & TO_X) == 0) {
6166                 return (s) ? strlen(s) : 0;
6167         }
6168
6169         if (!s) {
6170                 return 0;
6171         }
6172
6173         p = strndup(s, text_buffer_size);
6174         final = p;
6175
6176         while (*p) {
6177                 if (*p == SPECIAL_CHAR) {
6178                         /* shift everything over by 1 so that the special char
6179                          * doesn't mess up the size calculation */
6180                         for (i = 0; i < (long)strlen(p); i++) {
6181                                 *(p + i) = *(p + i + 1);
6182                         }
6183                         if (specials[special_index + idx].type == GRAPH
6184                                         || specials[special_index + idx].type == GAUGE
6185                                         || specials[special_index + idx].type == BAR) {
6186                                 width += specials[special_index + idx].width;
6187                         }
6188                         idx++;
6189                 } else if (*p == SECRIT_MULTILINE_CHAR) {
6190                         *p = 0;
6191                         break;
6192                 } else {
6193                         p++;
6194                 }
6195         }
6196         if (strlen(final) > 1) {
6197                 width += calc_text_width(final, strlen(final));
6198         }
6199         free(final);
6200         return width;
6201 }
6202
6203 static int text_size_updater(char *s, int special_index);
6204
6205 int last_font_height;
6206 static void update_text_area(void)
6207 {
6208         int x = 0, y = 0;
6209
6210         if ((output_methods & TO_X) == 0)
6211                 return;
6212         /* update text size if it isn't fixed */
6213 #ifdef OWN_WINDOW
6214         if (!fixed_size)
6215 #endif
6216         {
6217                 text_width = minimum_width;
6218                 text_height = 0;
6219                 last_font_height = font_height();
6220                 for_each_line(text_buffer, text_size_updater);
6221                 text_width += 1;
6222                 if (text_height < minimum_height) {
6223                         text_height = minimum_height;
6224                 }
6225                 if (text_width > maximum_width && maximum_width > 0) {
6226                         text_width = maximum_width;
6227                 }
6228         }
6229
6230         /* get text position on workarea */
6231         switch (text_alignment) {
6232                 case TOP_LEFT:
6233                         x = gap_x;
6234                         y = gap_y;
6235                         break;
6236
6237                 case TOP_RIGHT:
6238                         x = workarea[2] - text_width - gap_x;
6239                         y = gap_y;
6240                         break;
6241
6242                 case TOP_MIDDLE:
6243                         x = workarea[2] / 2 - text_width / 2 - gap_x;
6244                         y = gap_y;
6245                         break;
6246
6247                 default:
6248                 case BOTTOM_LEFT:
6249                         x = gap_x;
6250                         y = workarea[3] - text_height - gap_y;
6251                         break;
6252
6253                 case BOTTOM_RIGHT:
6254                         x = workarea[2] - text_width - gap_x;
6255                         y = workarea[3] - text_height - gap_y;
6256                         break;
6257
6258                 case BOTTOM_MIDDLE:
6259                         x = workarea[2] / 2 - text_width / 2 - gap_x;
6260                         y = workarea[3] - text_height - gap_y;
6261                         break;
6262
6263                 case MIDDLE_LEFT:
6264                         x = gap_x;
6265                         y = workarea[3] / 2 - text_height / 2 - gap_y;
6266                         break;
6267
6268                 case MIDDLE_RIGHT:
6269                         x = workarea[2] - text_width - gap_x;
6270                         y = workarea[3] / 2 - text_height / 2 - gap_y;
6271                         break;
6272
6273 #ifdef OWN_WINDOW
6274                 case NONE:      // Let the WM manage the window
6275                         x = window.x;
6276                         y = window.y;
6277
6278                         fixed_pos = 1;
6279                         fixed_size = 1;
6280                         break;
6281 #endif
6282         }
6283 #ifdef OWN_WINDOW
6284
6285         if (own_window && !fixed_pos) {
6286                 x += workarea[0];
6287                 y += workarea[1];
6288                 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6289                 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6290                 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6291                 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6292         } else
6293 #endif
6294         {
6295                 /* If window size doesn't match to workarea's size,
6296                  * then window probably includes panels (gnome).
6297                  * Blah, doesn't work on KDE. */
6298                 if (workarea[2] != window.width || workarea[3] != window.height) {
6299                         y += workarea[1];
6300                         x += workarea[0];
6301                 }
6302
6303                 text_start_x = x;
6304                 text_start_y = y;
6305         }
6306 #ifdef HAVE_LUA
6307         /* update lua window globals */
6308         llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6309 #endif /* HAVE_LUA */
6310 }
6311
6312 /* drawing stuff */
6313
6314 static int cur_x, cur_y;        /* current x and y for drawing */
6315 #endif
6316 //draw_mode also without X11 because we only need to print to stdout with FG
6317 static int draw_mode;           /* FG, BG or OUTLINE */
6318 #ifdef X11
6319 static long current_color;
6320
6321 static int text_size_updater(char *s, int special_index)
6322 {
6323         int w = 0;
6324         char *p;
6325
6326         if ((output_methods & TO_X) == 0)
6327                 return 0;
6328         /* get string widths and skip specials */
6329         p = s;
6330         while (*p) {
6331                 if (*p == SPECIAL_CHAR) {
6332                         *p = '\0';
6333                         w += get_string_width(s);
6334                         *p = SPECIAL_CHAR;
6335
6336                         if (specials[special_index].type == BAR
6337                                         || specials[special_index].type == GAUGE
6338                                         || specials[special_index].type == GRAPH) {
6339                                 w += specials[special_index].width;
6340                                 if (specials[special_index].height > last_font_height) {
6341                                         last_font_height = specials[special_index].height;
6342                                         last_font_height += font_height();
6343                                 }
6344                         } else if (specials[special_index].type == OFFSET) {
6345                                 if (specials[special_index].arg > 0) {
6346                                         w += specials[special_index].arg;
6347                                 }
6348                         } else if (specials[special_index].type == VOFFSET) {
6349                                 last_font_height += specials[special_index].arg;
6350                         } else if (specials[special_index].type == GOTO) {
6351                                 if (specials[special_index].arg > cur_x) {
6352                                         w = (int) specials[special_index].arg;
6353                                 }
6354                         } else if (specials[special_index].type == TAB) {
6355                                 int start = specials[special_index].arg;
6356                                 int step = specials[special_index].width;
6357
6358                                 if (!step || step < 0) {
6359                                         step = 10;
6360                                 }
6361                                 w += step - (cur_x - text_start_x - start) % step;
6362                         } else if (specials[special_index].type == FONT) {
6363                                 selected_font = specials[special_index].font_added;
6364                                 if (font_height() > last_font_height) {
6365                                         last_font_height = font_height();
6366                                 }
6367                         }
6368
6369                         special_index++;
6370                         s = p + 1;
6371                 } else if (*p == SECRIT_MULTILINE_CHAR) {
6372                         int lw;
6373                         *p = '\0';
6374                         lw = get_string_width(s);
6375                         *p = SECRIT_MULTILINE_CHAR;
6376                         s = p + 1;
6377                         w = lw > w ? lw : w;
6378                         text_height += last_font_height;
6379                 }
6380                 p++;
6381         }
6382         w += get_string_width(s);
6383         if (w > text_width) {
6384                 text_width = w;
6385         }
6386         if (text_width > maximum_width && maximum_width) {
6387                 text_width = maximum_width;
6388         }
6389
6390         text_height += last_font_height;
6391         last_font_height = font_height();
6392         return special_index;
6393 }
6394
6395 static inline void set_foreground_color(long c)
6396 {
6397         if ((output_methods & TO_X) == 0)
6398                 return;
6399         current_color = c;
6400         XSetForeground(display, window.gc, c);
6401 }
6402 #endif /* X11 */
6403
6404 static void draw_string(const char *s)
6405 {
6406         int i, i2, pos, width_of_s;
6407         int max = 0;
6408         int added;
6409         char *s_with_newlines;
6410
6411         if (s[0] == '\0') {
6412                 return;
6413         }
6414
6415         width_of_s = get_string_width(s);
6416         s_with_newlines = strdup(s);
6417         for(i = 0; i < (int) strlen(s_with_newlines); i++) {
6418                 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
6419                         s_with_newlines[i] = '\n';
6420                 }
6421         }
6422         if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6423                 printf("%s\n", s_with_newlines);
6424                 if (extra_newline) fputc('\n', stdout);
6425                 fflush(stdout); /* output immediately, don't buffer */
6426         }
6427         if ((output_methods & TO_STDERR) && draw_mode == FG) {
6428                 fprintf(stderr, "%s\n", s_with_newlines);
6429                 fflush(stderr); /* output immediately, don't buffer */
6430         }
6431         if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6432                 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
6433         }
6434         if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6435                 fprintf(append_fpointer, "%s\n", s_with_newlines);
6436         }
6437         free(s_with_newlines);
6438         memset(tmpstring1, 0, text_buffer_size);
6439         memset(tmpstring2, 0, text_buffer_size);
6440         strncpy(tmpstring1, s, text_buffer_size - 1);
6441         pos = 0;
6442         added = 0;
6443
6444 #ifdef X11
6445         if (output_methods & TO_X) {
6446                 max = ((text_width - width_of_s) / get_string_width(" "));
6447         }
6448 #endif /* X11 */
6449         /* This code looks for tabs in the text and coverts them to spaces.
6450          * The trick is getting the correct number of spaces, and not going
6451          * over the window's size without forcing the window larger. */
6452         for (i = 0; i < (int) text_buffer_size; i++) {
6453                 if (tmpstring1[i] == '\t') {
6454                         i2 = 0;
6455                         for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6456                                 /* guard against overrun */
6457                                 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6458                                 added++;
6459                         }
6460                         pos += i2;
6461                 } else {
6462                         /* guard against overrun */
6463                         tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6464                         pos++;
6465                 }
6466         }
6467 #ifdef X11
6468         if (output_methods & TO_X) {
6469                 if (text_width == maximum_width) {
6470                         /* this means the text is probably pushing the limit,
6471                          * so we'll chop it */
6472                         while (cur_x + get_string_width(tmpstring2) - text_start_x
6473                                         > maximum_width && strlen(tmpstring2) > 0) {
6474                                 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6475                         }
6476                 }
6477         }
6478 #endif /* X11 */
6479         s = tmpstring2;
6480 #ifdef X11
6481         if (output_methods & TO_X) {
6482 #ifdef XFT
6483                 if (use_xft) {
6484                         XColor c;
6485                         XftColor c2;
6486
6487                         c.pixel = current_color;
6488                         XQueryColor(display, DefaultColormap(display, screen), &c);
6489
6490                         c2.pixel = c.pixel;
6491                         c2.color.red = c.red;
6492                         c2.color.green = c.green;
6493                         c2.color.blue = c.blue;
6494                         c2.color.alpha = fonts[selected_font].font_alpha;
6495                         if (utf8_mode) {
6496                                 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6497                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6498                         } else {
6499                                 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6500                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6501                         }
6502                 } else
6503 #endif
6504                 {
6505                         XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6506                                 strlen(s));
6507                 }
6508                 cur_x += width_of_s;
6509         }
6510 #endif /* X11 */
6511         memcpy(tmpstring1, s, text_buffer_size);
6512 }
6513
6514 #ifdef X11
6515 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
6516 {
6517         int font_h = font_height();
6518         int cur_y_add = 0;
6519         char *recurse = 0;
6520         char *p = s;
6521         int last_special_needed = -1;
6522         int orig_special_index = special_index;
6523
6524         cur_x = text_start_x;
6525         cur_y += font_ascent();
6526
6527         while (*p) {
6528                 if (*p == SECRIT_MULTILINE_CHAR) {
6529                         /* special newline marker for multiline objects */
6530                         recurse = p + 1;
6531                         *p = '\0';
6532                         break;
6533                 }
6534                 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
6535                         int w = 0;
6536
6537                         /* draw string before special, unless we're dealing multiline
6538                          * specials */
6539                         if (last_special_applied > -1) {
6540                                 special_index = last_special_applied;
6541                         } else {
6542                                 *p = '\0';
6543                                 draw_string(s);
6544                                 *p = SPECIAL_CHAR;
6545                                 s = p + 1;
6546                         }
6547                         /* draw special */
6548                         switch (specials[special_index].type) {
6549                                 case HORIZONTAL_LINE:
6550                                 {
6551                                         int h = specials[special_index].height;
6552                                         int mid = font_ascent() / 2;
6553
6554                                         w = text_start_x + text_width - cur_x;
6555
6556                                         XSetLineAttributes(display, window.gc, h, LineSolid,
6557                                                 CapButt, JoinMiter);
6558                                         XDrawLine(display, window.drawable, window.gc, cur_x,
6559                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6560                                         break;
6561                                 }
6562
6563                                 case STIPPLED_HR:
6564                                 {
6565                                         int h = specials[special_index].height;
6566                                         int tmp_s = specials[special_index].arg;
6567                                         int mid = font_ascent() / 2;
6568                                         char ss[2] = { tmp_s, tmp_s };
6569
6570                                         w = text_start_x + text_width - cur_x - 1;
6571                                         XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6572                                                 CapButt, JoinMiter);
6573                                         XSetDashes(display, window.gc, 0, ss, 2);
6574                                         XDrawLine(display, window.drawable, window.gc, cur_x,
6575                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6576                                         break;
6577                                 }
6578
6579                                 case BAR:
6580                                 {
6581                                         int h, bar_usage, by;
6582                                         if (cur_x - text_start_x > maximum_width
6583                                                         && maximum_width > 0) {
6584                                                 break;
6585                                         }
6586                                         h = specials[special_index].height;
6587                                         bar_usage = specials[special_index].arg;
6588                                         by = cur_y - (font_ascent() / 2) - 1;
6589
6590                                         if (h < font_h) {
6591                                                 by -= h / 2 - 1;
6592                                         }
6593                                         w = specials[special_index].width;
6594                                         if (w == 0) {
6595                                                 w = text_start_x + text_width - cur_x - 1;
6596                                         }
6597                                         if (w < 0) {
6598                                                 w = 0;
6599                                         }
6600
6601                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6602                                                 CapButt, JoinMiter);
6603
6604                                         XDrawRectangle(display, window.drawable, window.gc, cur_x,
6605                                                 by, w, h);
6606                                         XFillRectangle(display, window.drawable, window.gc, cur_x,
6607                                                 by, w * bar_usage / 255, h);
6608                                         if (h > cur_y_add
6609                                                         && h > font_h) {
6610                                                 cur_y_add = h;
6611                                         }
6612                                         break;
6613                                 }
6614
6615                                 case GAUGE: /* new GAUGE  */
6616                                 {
6617                                         int h, by = 0;
6618                                         unsigned long last_colour = current_color;
6619 #ifdef MATH
6620                                         float angle, px, py;
6621                                         int usage;
6622 #endif /* MATH */
6623
6624                                         if (cur_x - text_start_x > maximum_width
6625                                                         && maximum_width > 0) {
6626                                                 break;
6627                                         }
6628
6629                                         h = specials[special_index].height;
6630                                         by = cur_y - (font_ascent() / 2) - 1;
6631
6632                                         if (h < font_h) {
6633                                                 by -= h / 2 - 1;
6634                                         }
6635                                         w = specials[special_index].width;
6636                                         if (w == 0) {
6637                                                 w = text_start_x + text_width - cur_x - 1;
6638                                         }
6639                                         if (w < 0) {
6640                                                 w = 0;
6641                                         }
6642
6643                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6644                                                         CapButt, JoinMiter);
6645
6646                                         XDrawArc(display, window.drawable, window.gc,
6647                                                         cur_x, by, w, h * 2, 0, 180*64);
6648
6649 #ifdef MATH
6650                                         usage = specials[special_index].arg;
6651                                         angle = (M_PI)*(float)(usage)/255.;
6652                                         px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6653                                         py = (float)(by+(h))-(float)(h)*sin(angle);
6654
6655                                         XDrawLine(display, window.drawable, window.gc,
6656                                                         cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6657 #endif /* MATH */
6658
6659                                         if (h > cur_y_add
6660                                                         && h > font_h) {
6661                                                 cur_y_add = h;
6662                                         }
6663
6664                                         set_foreground_color(last_colour);
6665
6666                                         break;
6667
6668                                 }
6669
6670                                 case GRAPH:
6671                                 {
6672                                         int h, by, i = 0, j = 0;
6673                                         int colour_idx = 0;
6674                                         unsigned long last_colour = current_color;
6675                                         unsigned long *tmpcolour = 0;
6676                                         if (cur_x - text_start_x > maximum_width
6677                                                         && maximum_width > 0) {
6678                                                 break;
6679                                         }
6680                                         h = specials[special_index].height;
6681                                         by = cur_y - (font_ascent() / 2) - 1;
6682
6683                                         if (h < font_h) {
6684                                                 by -= h / 2 - 1;
6685                                         }
6686                                         w = specials[special_index].width;
6687                                         if (w == 0) {
6688                                                 w = text_start_x + text_width - cur_x - 1;
6689                                         }
6690                                         if (w < 0) {
6691                                                 w = 0;
6692                                         }
6693                                         if (draw_graph_borders) {
6694                                                 XSetLineAttributes(display, window.gc, 1, LineSolid,
6695                                                         CapButt, JoinMiter);
6696                                                 XDrawRectangle(display, window.drawable, window.gc,
6697                                                         cur_x, by, w, h);
6698                                         }
6699                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6700                                                 CapButt, JoinMiter);
6701
6702                                         if (specials[special_index].last_colour != 0
6703                                                         || specials[special_index].first_colour != 0) {
6704                                                 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6705                                         }
6706                                         colour_idx = 0;
6707                                         for (i = w - 2; i > -1; i--) {
6708                                                 if (specials[special_index].last_colour != 0
6709                                                                 || specials[special_index].first_colour != 0) {
6710                                                         if (specials[special_index].tempgrad) {
6711 #ifdef DEBUG_lol
6712                                                                 assert(
6713                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
6714                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
6715                                                                                 < w - 1
6716                                                                           );
6717                                                                 assert(
6718                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
6719                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
6720                                                                                 > -1
6721                                                                           );
6722                                                                 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6723                                                                         assert(
6724                                                                                         (int)((float)(w - 2) - specials[special_index].graph[j] *
6725                                                                                                 (w - 2) / (float)specials[special_index].graph_scale)
6726                                                                                         == 0
6727                                                                                   );
6728                                                                 }
6729 #endif /* DEBUG_lol */
6730                                                                 XSetForeground(display, window.gc, tmpcolour[
6731                                                                                 (int)((float)(w - 2) - specials[special_index].graph[j] *
6732                                                                                         (w - 2) / (float)specials[special_index].graph_scale)
6733                                                                                 ]);
6734                                                         } else {
6735                                                                 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6736                                                         }
6737                                                 }
6738                                                 /* this is mugfugly, but it works */
6739                                                 XDrawLine(display, window.drawable, window.gc,
6740                                                                 cur_x + i + 1, by + h, cur_x + i + 1,
6741                                                                 round_to_int((double)by + h - specials[special_index].graph[j] *
6742                                                                         (h - 1) / specials[special_index].graph_scale));
6743                                                 if ((w - i) / ((float) (w - 2) /
6744                                                                         (specials[special_index].graph_width)) > j
6745                                                                 && j < MAX_GRAPH_DEPTH - 3) {
6746                                                         j++;
6747                                                 }
6748                                         }
6749                                         if (tmpcolour) free(tmpcolour);
6750                                         if (h > cur_y_add
6751                                                         && h > font_h) {
6752                                                 cur_y_add = h;
6753                                         }
6754                                         /* if (draw_mode == BG) {
6755                                                 set_foreground_color(default_bg_color);
6756                                         } else if (draw_mode == OUTLINE) {
6757                                                 set_foreground_color(default_out_color);
6758                                         } else {
6759                                                 set_foreground_color(default_fg_color);
6760                                         } */
6761                                         if (show_graph_range) {
6762                                                 int tmp_x = cur_x;
6763                                                 int tmp_y = cur_y;
6764                                                 unsigned short int seconds = update_interval * w;
6765                                                 char *tmp_day_str;
6766                                                 char *tmp_hour_str;
6767                                                 char *tmp_min_str;
6768                                                 char *tmp_sec_str;
6769                                                 char *tmp_str;
6770                                                 unsigned short int timeunits;
6771                                                 if (seconds != 0) {
6772                                                         timeunits = seconds / 86400; seconds %= 86400;
6773                                                         if (timeunits > 0) {
6774                                                                 asprintf(&tmp_day_str, "%dd", timeunits);
6775                                                         } else {
6776                                                                 tmp_day_str = strdup("");
6777                                                         }
6778                                                         timeunits = seconds / 3600; seconds %= 3600;
6779                                                         if (timeunits > 0) {
6780                                                                 asprintf(&tmp_hour_str, "%dh", timeunits);
6781                                                         } else {
6782                                                                 tmp_hour_str = strdup("");
6783                                                         }
6784                                                         timeunits = seconds / 60; seconds %= 60;
6785                                                         if (timeunits > 0) {
6786                                                                 asprintf(&tmp_min_str, "%dm", timeunits);
6787                                                         } else {
6788                                                                 tmp_min_str = strdup("");
6789                                                         }
6790                                                         if (seconds > 0) {
6791                                                                 asprintf(&tmp_sec_str, "%ds", seconds);
6792                                                         } else {
6793                                                                 tmp_sec_str = strdup("");
6794                                                         }
6795                                                         asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6796                                                         free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6797                                                 } else {
6798                                                         asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6799                                                 }
6800                                                 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6801                                                 cur_y += font_h / 2;
6802                                                 draw_string(tmp_str);
6803                                                 free(tmp_str);
6804                                                 cur_x = tmp_x;
6805                                                 cur_y = tmp_y;
6806                                         }
6807 #ifdef MATH
6808                                         if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6809                                                 int tmp_x = cur_x;
6810                                                 int tmp_y = cur_y;
6811                                                 char *tmp_str;
6812                                                 cur_x += font_ascent() / 2;
6813                                                 cur_y += font_h / 2;
6814                                                 tmp_str = (char *)
6815                                                         calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6816                                                                         sizeof(char));
6817                                                 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6818                                                 draw_string(tmp_str);
6819                                                 free(tmp_str);
6820                                                 cur_x = tmp_x;
6821                                                 cur_y = tmp_y;
6822                                         }
6823 #endif
6824                                         set_foreground_color(last_colour);
6825                                         break;
6826                                 }
6827
6828                                 case FONT:
6829                                 {
6830                                         int old = font_ascent();
6831
6832                                         cur_y -= font_ascent();
6833                                         selected_font = specials[special_index].font_added;
6834                                         set_font();
6835                                         if (cur_y + font_ascent() < cur_y + old) {
6836                                                 cur_y += old;
6837                                         } else {
6838                                                 cur_y += font_ascent();
6839                                         }
6840                                         font_h = font_height();
6841                                         break;
6842                                 }
6843                                 case FG:
6844                                         if (draw_mode == FG) {
6845                                                 set_foreground_color(specials[special_index].arg);
6846                                         }
6847                                         break;
6848
6849                                 case BG:
6850                                         if (draw_mode == BG) {
6851                                                 set_foreground_color(specials[special_index].arg);
6852                                         }
6853                                         break;
6854
6855                                 case OUTLINE:
6856                                         if (draw_mode == OUTLINE) {
6857                                                 set_foreground_color(specials[special_index].arg);
6858                                         }
6859                                         break;
6860
6861                                 case OFFSET:
6862                                         w += specials[special_index].arg;
6863                                         last_special_needed = special_index;
6864                                         break;
6865
6866                                 case VOFFSET:
6867                                         cur_y += specials[special_index].arg;
6868                                         break;
6869
6870                                 case GOTO:
6871                                         if (specials[special_index].arg >= 0) {
6872                                                 cur_x = (int) specials[special_index].arg;
6873                                         }
6874                                         last_special_needed = special_index;
6875                                         break;
6876
6877                                 case TAB:
6878                                 {
6879                                         int start = specials[special_index].arg;
6880                                         int step = specials[special_index].width;
6881
6882                                         if (!step || step < 0) {
6883                                                 step = 10;
6884                                         }
6885                                         w = step - (cur_x - text_start_x - start) % step;
6886                                         last_special_needed = special_index;
6887                                         break;
6888                                 }
6889
6890                                 case ALIGNR:
6891                                 {
6892                                         /* TODO: add back in "+ window.border_inner_margin" to the end of
6893                                          * this line? */
6894                                         int pos_x = text_start_x + text_width -
6895                                                 get_string_width_special(s, special_index);
6896
6897                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6898                                                 "get_string_width(p) %i gap_x %i "
6899                                                 "specials[special_index].arg %i window.border_inner_margin %i "
6900                                                 "window.border_width %i\n", pos_x, text_start_x, text_width,
6901                                                 cur_x, get_string_width_special(s), gap_x,
6902                                                 specials[special_index].arg, window.border_inner_margin,
6903                                                 window.border_width); */
6904                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6905                                                 cur_x = pos_x - specials[special_index].arg;
6906                                         }
6907                                         last_special_needed = special_index;
6908                                         break;
6909                                 }
6910
6911                                 case ALIGNC:
6912                                 {
6913                                         int pos_x = (text_width) / 2 - get_string_width_special(s,
6914                                                         special_index) / 2 - (cur_x -
6915                                                                 text_start_x);
6916                                         /* int pos_x = text_start_x + text_width / 2 -
6917                                                 get_string_width_special(s) / 2; */
6918
6919                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6920                                                 "get_string_width(p) %i gap_x %i "
6921                                                 "specials[special_index].arg %i\n", pos_x, text_start_x,
6922                                                 text_width, cur_x, get_string_width(s), gap_x,
6923                                                 specials[special_index].arg); */
6924                                         if (pos_x > specials[special_index].arg) {
6925                                                 w = pos_x - specials[special_index].arg;
6926                                         }
6927                                         last_special_needed = special_index;
6928                                         break;
6929                                 }
6930                         }
6931
6932                         cur_x += w;
6933
6934                         if (special_index != last_special_applied) {
6935                                 special_index++;
6936                         } else {
6937                                 special_index = orig_special_index;
6938                                 last_special_applied = -1;
6939                         }
6940                 }
6941                 p++;
6942         }
6943
6944         cur_y += cur_y_add;
6945         draw_string(s);
6946         cur_y += font_descent();
6947         if (recurse && *recurse) {
6948                 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
6949                 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
6950         }
6951         return special_index;
6952 }
6953 #endif /* X11 */
6954
6955 static int draw_line(char *s, int special_index)
6956 {
6957 #ifdef X11
6958         if ((output_methods & TO_X) == 0) {
6959 #endif /* X11 */
6960                 draw_string(s);
6961                 //'special_index - special_index' instead of 0 otherwise gcc complains about not using special_index when build without X11
6962                 return special_index - special_index;
6963 #ifdef X11
6964         }
6965
6966         /* find specials and draw stuff */
6967         return draw_each_line_inner(s, special_index, -1);
6968 #endif /* X11 */
6969 }
6970
6971 static void draw_text(void)
6972 {
6973 #ifdef X11
6974 #ifdef HAVE_LUA
6975         llua_draw_pre_hook();
6976 #endif /* HAVE_LUA */
6977         if (output_methods & TO_X) {
6978                 cur_y = text_start_y;
6979
6980                 /* draw borders */
6981                 if (draw_borders && window.border_width > 0) {
6982                         if (stippled_borders) {
6983                                 char ss[2] = { stippled_borders, stippled_borders };
6984                                 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
6985                                         CapButt, JoinMiter);
6986                                 XSetDashes(display, window.gc, 0, ss, 2);
6987                         } else {
6988                                 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
6989                                         CapButt, JoinMiter);
6990                         }
6991
6992                         XDrawRectangle(display, window.drawable, window.gc,
6993                                 text_start_x - window.border_inner_margin - window.border_width,
6994                                 text_start_y - window.border_inner_margin - window.border_width,
6995                                 text_width + window.border_inner_margin * 2 + window.border_width * 2,
6996                                 text_height + window.border_inner_margin * 2 + window.border_width * 2);
6997                 }
6998
6999                 /* draw text */
7000         }
7001         setup_fonts();
7002 #endif /* X11 */
7003         for_each_line(text_buffer, draw_line);
7004 #if defined(HAVE_LUA) && defined(X11)
7005         llua_draw_post_hook();
7006 #endif /* HAVE_LUA */
7007 }
7008
7009 static void draw_stuff(void)
7010 {
7011         if (overwrite_file) {
7012                 overwrite_fpointer = fopen(overwrite_file, "w");
7013                 if(!overwrite_fpointer)
7014                         ERR("Can't overwrite '%s' anymore", overwrite_file);
7015         }
7016         if (append_file) {
7017                 append_fpointer = fopen(append_file, "a");
7018                 if(!append_fpointer)
7019                         ERR("Can't append '%s' anymore", append_file);
7020         }
7021 #ifdef X11
7022         if (output_methods & TO_X) {
7023                 selected_font = 0;
7024                 if (draw_shades && !draw_outline) {
7025                         text_start_x++;
7026                         text_start_y++;
7027                         set_foreground_color(default_bg_color);
7028                         draw_mode = BG;
7029                         draw_text();
7030                         text_start_x--;
7031                         text_start_y--;
7032                 }
7033
7034                 if (draw_outline) {
7035                         int i, j;
7036                         selected_font = 0;
7037
7038                         for (i = -1; i < 2; i++) {
7039                                 for (j = -1; j < 2; j++) {
7040                                         if (i == 0 && j == 0) {
7041                                                 continue;
7042                                         }
7043                                         text_start_x += i;
7044                                         text_start_y += j;
7045                                         set_foreground_color(default_out_color);
7046                                         draw_mode = OUTLINE;
7047                                         draw_text();
7048                                         text_start_x -= i;
7049                                         text_start_y -= j;
7050                                 }
7051                         }
7052                 }
7053
7054                 set_foreground_color(default_fg_color);
7055         }
7056 #endif /* X11 */
7057         draw_mode = FG;
7058         draw_text();
7059 #ifdef X11
7060         if (output_methods & TO_X) {
7061 #ifdef HAVE_XDBE
7062                 if (use_xdbe) {
7063                         XdbeSwapInfo swap;
7064
7065                         swap.swap_window = window.window;
7066                         swap.swap_action = XdbeBackground;
7067                         XdbeSwapBuffers(display, &swap, 1);
7068                 }
7069 #endif
7070         }
7071 #endif /* X11 */
7072         if(overwrite_fpointer) {
7073                 fclose(overwrite_fpointer);
7074                 overwrite_fpointer = 0;
7075         }
7076         if(append_fpointer) {
7077                 fclose(append_fpointer);
7078                 append_fpointer = 0;
7079         }
7080 }
7081
7082 #ifdef X11
7083 static void clear_text(int exposures)
7084 {
7085 #ifdef HAVE_XDBE
7086         if (use_xdbe) {
7087                 /* The swap action is XdbeBackground, which clears */
7088                 return;
7089         } else
7090 #endif
7091         if (display && window.window) { // make sure these are !null
7092                 /* there is some extra space for borders and outlines */
7093                 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
7094                         text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
7095                         text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7096                         text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
7097         }
7098 }
7099 #endif /* X11 */
7100
7101 static int need_to_update;
7102
7103 /* update_text() generates new text and clears old text area */
7104 static void update_text(void)
7105 {
7106 #ifdef IMLIB2
7107         cimlib_cleanup();
7108 #endif /* IMLIB2 */
7109         generate_text();
7110 #ifdef X11
7111         if (output_methods & TO_X)
7112                 clear_text(1);
7113 #endif /* X11 */
7114         need_to_update = 1;
7115 #ifdef HAVE_LUA
7116         llua_update_info(&info, update_interval);
7117 #endif /* HAVE_LUA */
7118 }
7119
7120 #ifdef HAVE_SYS_INOTIFY_H
7121 int inotify_fd;
7122 #endif
7123
7124 static void main_loop(void)
7125 {
7126         int terminate = 0;
7127 #ifdef SIGNAL_BLOCKING
7128         sigset_t newmask, oldmask;
7129 #endif
7130         double t;
7131 #ifdef HAVE_SYS_INOTIFY_H
7132         int inotify_config_wd = -1;
7133 #define INOTIFY_EVENT_SIZE  (sizeof(struct inotify_event))
7134 #define INOTIFY_BUF_LEN     (20 * (INOTIFY_EVENT_SIZE + 16))
7135         char inotify_buff[INOTIFY_BUF_LEN];
7136 #endif /* HAVE_SYS_INOTIFY_H */
7137
7138
7139 #ifdef SIGNAL_BLOCKING
7140         sigemptyset(&newmask);
7141         sigaddset(&newmask, SIGINT);
7142         sigaddset(&newmask, SIGTERM);
7143         sigaddset(&newmask, SIGUSR1);
7144 #endif
7145
7146         last_update_time = 0.0;
7147         next_update_time = get_time();
7148         info.looped = 0;
7149         while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7150                 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
7151                         char buf[max_user_text];
7152
7153                         get_battery_short_status(buf, max_user_text, "BAT0");
7154                         if(buf[0] == 'D') {
7155                                 update_interval = update_interval_bat;
7156                         } else {
7157                                 update_interval = update_interval_old;
7158                         }
7159                 }
7160                 info.looped++;
7161
7162 #ifdef SIGNAL_BLOCKING
7163                 /* block signals.  we will inspect for pending signals later */
7164                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7165                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7166                 }
7167 #endif
7168
7169 #ifdef X11
7170                 if (output_methods & TO_X) {
7171                         XFlush(display);
7172
7173                         /* wait for X event or timeout */
7174
7175                         if (!XPending(display)) {
7176                                 fd_set fdsr;
7177                                 struct timeval tv;
7178                                 int s;
7179                                 t = next_update_time - get_time();
7180
7181                                 if (t < 0) {
7182                                         t = 0;
7183                                 } else if (t > update_interval) {
7184                                         t = update_interval;
7185                                 }
7186
7187                                 tv.tv_sec = (long) t;
7188                                 tv.tv_usec = (long) (t * 1000000) % 1000000;
7189                                 FD_ZERO(&fdsr);
7190                                 FD_SET(ConnectionNumber(display), &fdsr);
7191
7192                                 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7193                                 if (s == -1) {
7194                                         if (errno != EINTR) {
7195                                                 ERR("can't select(): %s", strerror(errno));
7196                                         }
7197                                 } else {
7198                                         /* timeout */
7199                                         if (s == 0) {
7200                                                 update_text();
7201                                         }
7202                                 }
7203                         }
7204
7205                         if (need_to_update) {
7206 #ifdef OWN_WINDOW
7207                                 int wx = window.x, wy = window.y;
7208 #endif
7209
7210                                 need_to_update = 0;
7211                                 selected_font = 0;
7212                                 update_text_area();
7213 #ifdef OWN_WINDOW
7214                                 if (own_window) {
7215                                         int changed = 0;
7216
7217                                         /* resize window if it isn't right size */
7218                                         if (!fixed_size
7219                                                 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7220                                                 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7221                                                         window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7222                                                         window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7223                                                         XResizeWindow(display, window.window, window.width,
7224                                                                 window.height);
7225                                                         set_transparent_background(window.window);
7226
7227                                                         changed++;
7228                                         }
7229
7230                                         /* move window if it isn't in right position */
7231                                         if (!fixed_pos && (window.x != wx || window.y != wy)) {
7232                                                 XMoveWindow(display, window.window, window.x, window.y);
7233                                                 changed++;
7234                                         }
7235
7236                                         /* update struts */
7237                                         if (changed && window.type == TYPE_PANEL) {
7238                                                 int sidenum = -1;
7239
7240                                                 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7241                                                 fflush(stderr);
7242
7243                                                 switch (text_alignment) {
7244                                                         case TOP_LEFT:
7245                                                         case TOP_RIGHT:
7246                                                         case TOP_MIDDLE:
7247                                                         {
7248                                                                 sidenum = 2;
7249                                                                 break;
7250                                                         }
7251                                                         case BOTTOM_LEFT:
7252                                                         case BOTTOM_RIGHT:
7253                                                         case BOTTOM_MIDDLE:
7254                                                         {
7255                                                                 sidenum = 3;
7256                                                                 break;
7257                                                         }
7258                                                         case MIDDLE_LEFT:
7259                                                         {
7260                                                                 sidenum = 0;
7261                                                                 break;
7262                                                         }
7263                                                         case MIDDLE_RIGHT:
7264                                                         {
7265                                                                 sidenum = 1;
7266                                                                 break;
7267                                                         }
7268                                                 }
7269
7270                                                 set_struts(sidenum);
7271                                         }
7272                                 }
7273 #endif
7274
7275                                 clear_text(1);
7276
7277 #ifdef HAVE_XDBE
7278                                 if (use_xdbe) {
7279                                         XRectangle r;
7280
7281                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7282                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7283                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7284                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7285                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7286                                 }
7287 #endif
7288                         }
7289
7290                         /* handle X events */
7291                         while (XPending(display)) {
7292                                 XEvent ev;
7293
7294                                 XNextEvent(display, &ev);
7295                                 switch (ev.type) {
7296                                         case Expose:
7297                                         {
7298                                                 XRectangle r;
7299                                                 r.x = ev.xexpose.x;
7300                                                 r.y = ev.xexpose.y;
7301                                                 r.width = ev.xexpose.width;
7302                                                 r.height = ev.xexpose.height;
7303                                                 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7304                                                 break;
7305                                         }
7306
7307                                         case PropertyNotify:
7308                                         {
7309                                                 if ( ev.xproperty.state == PropertyNewValue ) {
7310                                                         get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
7311                                                 }
7312                                                 break;
7313                                         }
7314
7315 #ifdef OWN_WINDOW
7316                                         case ReparentNotify:
7317                                                 /* set background to ParentRelative for all parents */
7318                                                 if (own_window) {
7319                                                         set_transparent_background(window.window);
7320                                                 }
7321                                                 break;
7322
7323                                         case ConfigureNotify:
7324                                                 if (own_window) {
7325                                                         /* if window size isn't what expected, set fixed size */
7326                                                         if (ev.xconfigure.width != window.width
7327                                                                         || ev.xconfigure.height != window.height) {
7328                                                                 if (window.width != 0 && window.height != 0) {
7329                                                                         fixed_size = 1;
7330                                                                 }
7331
7332                                                                 /* clear old stuff before screwing up
7333                                                                  * size and pos */
7334                                                                 clear_text(1);
7335
7336                                                                 {
7337                                                                         XWindowAttributes attrs;
7338                                                                         if (XGetWindowAttributes(display,
7339                                                                                         window.window, &attrs)) {
7340                                                                                 window.width = attrs.width;
7341                                                                                 window.height = attrs.height;
7342                                                                         }
7343                                                                 }
7344
7345                                                                 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7346                                                                 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7347                                                                 if (text_width > maximum_width
7348                                                                                 && maximum_width > 0) {
7349                                                                         text_width = maximum_width;
7350                                                                 }
7351                                                         }
7352
7353                                                         /* if position isn't what expected, set fixed pos
7354                                                          * total_updates avoids setting fixed_pos when window
7355                                                          * is set to weird locations when started */
7356                                                         /* // this is broken
7357                                                         if (total_updates >= 2 && !fixed_pos
7358                                                                         && (window.x != ev.xconfigure.x
7359                                                                         || window.y != ev.xconfigure.y)
7360                                                                         && (ev.xconfigure.x != 0
7361                                                                         || ev.xconfigure.y != 0)) {
7362                                                                 fixed_pos = 1;
7363                                                         } */
7364                                                 }
7365                                                 break;
7366
7367                                         case ButtonPress:
7368                                                 if (own_window) {
7369                                                         /* if an ordinary window with decorations */
7370                                                         if ((window.type == TYPE_NORMAL)
7371                                                                 && (!TEST_HINT(window.hints,
7372                                                                 HINT_UNDECORATED))) {
7373                                                                 /* allow conky to hold input focus. */
7374                                                                 break;
7375                                                         } else {
7376                                                                 /* forward the click to the desktop window */
7377                                                                 XUngrabPointer(display, ev.xbutton.time);
7378                                                                 ev.xbutton.window = window.desktop;
7379                                                                 ev.xbutton.x = ev.xbutton.x_root;
7380                                                                 ev.xbutton.y = ev.xbutton.y_root;
7381                                                                 XSendEvent(display, ev.xbutton.window, False,
7382                                                                         ButtonPressMask, &ev);
7383                                                                 XSetInputFocus(display, ev.xbutton.window,
7384                                                                         RevertToParent, ev.xbutton.time);
7385                                                         }
7386                                                 }
7387                                                 break;
7388
7389                                         case ButtonRelease:
7390                                                 if (own_window) {
7391                                                         /* if an ordinary window with decorations */
7392                                                         if ((window.type == TYPE_NORMAL)
7393                                                                         && (!TEST_HINT(window.hints,
7394                                                                         HINT_UNDECORATED))) {
7395                                                                 /* allow conky to hold input focus. */
7396                                                                 break;
7397                                                         } else {
7398                                                                 /* forward the release to the desktop window */
7399                                                                 ev.xbutton.window = window.desktop;
7400                                                                 ev.xbutton.x = ev.xbutton.x_root;
7401                                                                 ev.xbutton.y = ev.xbutton.y_root;
7402                                                                 XSendEvent(display, ev.xbutton.window, False,
7403                                                                         ButtonReleaseMask, &ev);
7404                                                         }
7405                                                 }
7406                                                 break;
7407
7408 #endif
7409
7410                                         default:
7411 #ifdef HAVE_XDAMAGE
7412                                                 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7413                                                         XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7414
7415                                                         XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7416                                                         XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7417                                                 }
7418 #endif /* HAVE_XDAMAGE */
7419                                                 break;
7420                                 }
7421                         }
7422
7423 #ifdef HAVE_XDAMAGE
7424                         XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7425                         XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7426 #endif /* HAVE_XDAMAGE */
7427
7428                         /* XDBE doesn't seem to provide a way to clear the back buffer without
7429                          * interfering with the front buffer, other than passing XdbeBackground
7430                          * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7431                          * redraw the text even if it wasn't part of the exposed area. OTOH,
7432                          * if we're not going to call draw_stuff at all, then no swap happens
7433                          * and we can safely do nothing. */
7434
7435                         if (!XEmptyRegion(x11_stuff.region)) {
7436 #ifdef HAVE_XDBE
7437                                 if (use_xdbe) {
7438                                         XRectangle r;
7439
7440                                         r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7441                                         r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7442                                         r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7443                                         r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7444                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7445                                 }
7446 #endif
7447                                 XSetRegion(display, window.gc, x11_stuff.region);
7448 #ifdef XFT
7449                                 if (use_xft) {
7450                                         XftDrawSetClip(window.xftdraw, x11_stuff.region);
7451                                 }
7452 #endif
7453 #ifdef IMLIB2
7454                                 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7455 #endif /* IMLIB2 */
7456                                 draw_stuff();
7457                                 XDestroyRegion(x11_stuff.region);
7458                                 x11_stuff.region = XCreateRegion();
7459                         }
7460                 } else {
7461 #endif /* X11 */
7462                         t = (next_update_time - get_time()) * 1000000;
7463                         if(t > 0) usleep((useconds_t)t);
7464                         update_text();
7465                         draw_stuff();
7466 #ifdef X11
7467                 }
7468 #endif /* X11 */
7469
7470 #ifdef SIGNAL_BLOCKING
7471                 /* unblock signals of interest and let handler fly */
7472                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7473                         CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7474                 }
7475 #endif
7476
7477                 switch (g_signal_pending) {
7478                         case SIGHUP:
7479                         case SIGUSR1:
7480                                 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7481                                 reload_config();
7482                                 break;
7483                         case SIGINT:
7484                         case SIGTERM:
7485                                 ERR("received SIGINT or SIGTERM to terminate. bye!");
7486                                 terminate = 1;
7487 #ifdef X11
7488                                 if (output_methods & TO_X) {
7489                                         XDestroyRegion(x11_stuff.region);
7490                                         x11_stuff.region = NULL;
7491 #ifdef HAVE_XDAMAGE
7492                                         XDamageDestroy(display, x11_stuff.damage);
7493                                         XFixesDestroyRegion(display, x11_stuff.region2);
7494                                         XFixesDestroyRegion(display, x11_stuff.part);
7495 #endif /* HAVE_XDAMAGE */
7496                                         if (disp) {
7497                                                 free(disp);
7498                                         }
7499                                 }
7500 #endif /* X11 */
7501                                 if(overwrite_file) {
7502                                         free(overwrite_file);
7503                                         overwrite_file = 0;
7504                                 }
7505                                 if(append_file) {
7506                                         free(append_file);
7507                                         append_file = 0;
7508                                 }
7509                                 break;
7510                         default:
7511                                 /* Reaching here means someone set a signal
7512                                  * (SIGXXXX, signal_handler), but didn't write any code
7513                                  * to deal with it.
7514                                  * If you don't want to handle a signal, don't set a handler on
7515                                  * it in the first place. */
7516                                 if (g_signal_pending) {
7517                                         ERR("ignoring signal (%d)", g_signal_pending);
7518                                 }
7519                                 break;
7520                 }
7521 #ifdef HAVE_SYS_INOTIFY_H
7522                 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7523                         inotify_config_wd = inotify_add_watch(inotify_fd,
7524                                         current_config,
7525                                         IN_MODIFY);
7526                 }
7527                 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7528                         int len = 0, idx = 0;
7529                         fd_set descriptors;
7530                         struct timeval time_to_wait;
7531
7532                         FD_ZERO(&descriptors);
7533                         FD_SET(inotify_fd, &descriptors);
7534
7535                         time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7536
7537                         select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7538                         if (FD_ISSET(inotify_fd, &descriptors)) {
7539                                 /* process inotify events */
7540                                 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7541                                 while (len > 0 && idx < len) {
7542                                         struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7543                                         if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7544                                                 /* current_config should be reloaded */
7545                                                 ERR("'%s' modified, reloading...", current_config);
7546                                                 reload_config();
7547                                                 if (ev->mask & IN_IGNORED) {
7548                                                         /* for some reason we get IN_IGNORED here
7549                                                          * sometimes, so we need to re-add the watch */
7550                                                         inotify_config_wd = inotify_add_watch(inotify_fd,
7551                                                                         current_config,
7552                                                                         IN_MODIFY);
7553                                                 }
7554                                         }
7555 #ifdef HAVE_LUA
7556                                         else {
7557                                                 llua_inotify_query(ev->wd, ev->mask);
7558                                         }
7559 #endif /* HAVE_LUA */
7560                                         idx += INOTIFY_EVENT_SIZE + ev->len;
7561                                 }
7562                         }
7563                 }
7564 #endif /* HAVE_SYS_INOTIFY_H */
7565
7566 #ifdef HAVE_LUA
7567         llua_update_info(&info, update_interval);
7568 #endif /* HAVE_LUA */
7569                 g_signal_pending = 0;
7570         }
7571         clean_up(NULL, NULL);
7572
7573 #ifdef HAVE_SYS_INOTIFY_H
7574         if (inotify_fd != -1) {
7575                 inotify_rm_watch(inotify_fd, inotify_config_wd);
7576                 close(inotify_fd);
7577                 inotify_fd = inotify_config_wd = 0;
7578         }
7579 #endif /* HAVE_SYS_INOTIFY_H */
7580 }
7581
7582 static void load_config_file(const char *);
7583 #ifdef X11
7584 static void load_config_file_x11(const char *);
7585 #endif /* X11 */
7586 void initialisation(int argc, char** argv);
7587
7588         /* reload the config file */
7589 static void reload_config(void)
7590 {
7591         char *current_config_copy = strdup(current_config);
7592         clean_up(NULL, NULL);
7593         current_config = current_config_copy;
7594         initialisation(argc_copy, argv_copy);
7595 }
7596
7597 void clean_up(void *memtofree1, void* memtofree2)
7598 {
7599         int i;
7600         if(memtofree1) {
7601                 free(memtofree1);
7602         }
7603         if(memtofree2) {
7604                 free(memtofree2);
7605         }
7606         timed_thread_destroy_registered_threads();
7607
7608         if (info.cpu_usage) {
7609                 free(info.cpu_usage);
7610                 info.cpu_usage = NULL;
7611         }
7612 #ifdef X11
7613         if (x_initialised == YES) {
7614                 destroy_window();
7615                 free_fonts();
7616                 if(x11_stuff.region) {
7617                         XDestroyRegion(x11_stuff.region);
7618                         x11_stuff.region = NULL;
7619                 }
7620                 XClearWindow(display, RootWindow(display, screen));
7621                 XCloseDisplay(display);
7622                 display = NULL;
7623                 if(info.x11.desktop.all_names) {
7624                         free(info.x11.desktop.all_names);
7625                         info.x11.desktop.all_names = NULL;
7626                 }
7627                 if (info.x11.desktop.name) {
7628                         free(info.x11.desktop.name);
7629                         info.x11.desktop.name = NULL;
7630                 }
7631                 x_initialised = NO;
7632         }else{
7633                 free(fonts);    //in set_default_configurations a font is set but not loaded
7634                 font_count = -1;
7635         }
7636
7637 #endif /* X11 */
7638
7639         for (i = 0; i < MAX_TEMPLATES; i++) {
7640                 if (template[i]) {
7641                         free(template[i]);
7642                         template[i] = NULL;
7643                 }
7644         }
7645
7646         free_text_objects(&global_root_object, 0);
7647         if (tmpstring1) {
7648                 free(tmpstring1);
7649                 tmpstring1 = 0;
7650         }
7651         if (tmpstring2) {
7652                 free(tmpstring2);
7653                 tmpstring2 = 0;
7654         }
7655         if (text_buffer) {
7656                 free(text_buffer);
7657                 text_buffer = 0;
7658         }
7659
7660         if (global_text) {
7661                 free(global_text);
7662                 global_text = 0;
7663         }
7664
7665         free(current_config);
7666         current_config = 0;
7667
7668 #ifdef TCP_PORT_MONITOR
7669         tcp_portmon_clear();
7670 #endif
7671 #ifdef HAVE_CURL
7672         ccurl_free_info();
7673 #endif
7674 #ifdef RSS
7675         rss_free_info();
7676 #endif
7677 #ifdef WEATHER
7678         weather_free_info();
7679 #endif
7680 #ifdef HAVE_LUA
7681         llua_close();
7682 #endif /* HAVE_LUA */
7683
7684         if (specials) {
7685                 for (i = 0; i < special_count; i++) {
7686                         if (specials[i].type == GRAPH) {
7687                                 free(specials[i].graph);
7688                         }
7689                 }
7690                 free(specials);
7691                 specials = NULL;
7692         }
7693
7694         clear_net_stats();
7695         clear_diskio_stats();
7696         if(global_cpu != NULL) {
7697                 free(global_cpu);
7698                 global_cpu = NULL;
7699         }
7700 }
7701
7702 static int string_to_bool(const char *s)
7703 {
7704         if (!s) {
7705                 // Assumes an option without a true/false means true
7706                 return 1;
7707         } else if (strcasecmp(s, "yes") == EQUAL) {
7708                 return 1;
7709         } else if (strcasecmp(s, "true") == EQUAL) {
7710                 return 1;
7711         } else if (strcasecmp(s, "1") == EQUAL) {
7712                 return 1;
7713         }
7714         return 0;
7715 }
7716
7717 #ifdef X11
7718 static enum alignment string_to_alignment(const char *s)
7719 {
7720         if (strcasecmp(s, "top_left") == EQUAL) {
7721                 return TOP_LEFT;
7722         } else if (strcasecmp(s, "top_right") == EQUAL) {
7723                 return TOP_RIGHT;
7724         } else if (strcasecmp(s, "top_middle") == EQUAL) {
7725                 return TOP_MIDDLE;
7726         } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7727                 return BOTTOM_LEFT;
7728         } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7729                 return BOTTOM_RIGHT;
7730         } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7731                 return BOTTOM_MIDDLE;
7732         } else if (strcasecmp(s, "middle_left") == EQUAL) {
7733                 return MIDDLE_LEFT;
7734         } else if (strcasecmp(s, "middle_right") == EQUAL) {
7735                 return MIDDLE_RIGHT;
7736         } else if (strcasecmp(s, "tl") == EQUAL) {
7737                 return TOP_LEFT;
7738         } else if (strcasecmp(s, "tr") == EQUAL) {
7739                 return TOP_RIGHT;
7740         } else if (strcasecmp(s, "tm") == EQUAL) {
7741                 return TOP_MIDDLE;
7742         } else if (strcasecmp(s, "bl") == EQUAL) {
7743                 return BOTTOM_LEFT;
7744         } else if (strcasecmp(s, "br") == EQUAL) {
7745                 return BOTTOM_RIGHT;
7746         } else if (strcasecmp(s, "bm") == EQUAL) {
7747                 return BOTTOM_MIDDLE;
7748         } else if (strcasecmp(s, "ml") == EQUAL) {
7749                 return MIDDLE_LEFT;
7750         } else if (strcasecmp(s, "mr") == EQUAL) {
7751                 return MIDDLE_RIGHT;
7752         } else if (strcasecmp(s, "none") == EQUAL) {
7753                 return NONE;
7754         }
7755         return TOP_LEFT;
7756 }
7757 #endif /* X11 */
7758
7759 #ifdef X11
7760 static void set_default_configurations_for_x(void)
7761 {
7762         default_fg_color = WhitePixel(display, screen);
7763         default_bg_color = BlackPixel(display, screen);
7764         default_out_color = BlackPixel(display, screen);
7765         color0 = default_fg_color;
7766         color1 = default_fg_color;
7767         color2 = default_fg_color;
7768         color3 = default_fg_color;
7769         color4 = default_fg_color;
7770         color5 = default_fg_color;
7771         color6 = default_fg_color;
7772         color7 = default_fg_color;
7773         color8 = default_fg_color;
7774         color9 = default_fg_color;
7775         current_text_color = default_fg_color;
7776 }
7777 #endif /* X11 */
7778
7779 static void set_default_configurations(void)
7780 {
7781         int i;
7782 #ifdef MPD
7783         char *mpd_env_host;
7784         char *mpd_env_port;
7785 #endif
7786         update_uname();
7787         fork_to_background = 0;
7788         total_run_times = 0;
7789         info.cpu_avg_samples = 2;
7790         info.net_avg_samples = 2;
7791         info.diskio_avg_samples = 2;
7792         info.memmax = 0;
7793         top_cpu = 0;
7794         cpu_separate = 0;
7795         short_units = 0;
7796         format_human_readable = 1;
7797         top_mem = 0;
7798         top_time = 0;
7799 #ifdef IOSTATS
7800         top_io = 0;
7801 #endif
7802 #ifdef MPD
7803         mpd_env_host = getenv("MPD_HOST");
7804         mpd_env_port = getenv("MPD_PORT");
7805
7806         if (!mpd_env_host || !strlen(mpd_env_host)) {
7807                 mpd_set_host("localhost");
7808         } else {
7809                 /* MPD_HOST environment variable is set */
7810                 char *mpd_hostpart = strchr(mpd_env_host, '@');
7811                 if (!mpd_hostpart) {
7812                         mpd_set_host(mpd_env_host);
7813                 } else {
7814                         /* MPD_HOST contains a password */
7815                         char mpd_password[mpd_hostpart - mpd_env_host + 1];
7816                         snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7817
7818                         if (!strlen(mpd_hostpart + 1)) {
7819                                 mpd_set_host("localhost");
7820                         } else {
7821                                 mpd_set_host(mpd_hostpart + 1);
7822                         }
7823
7824                         mpd_set_password(mpd_password, 1);
7825                 }
7826         }
7827
7828
7829         if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7830                 /* failed to set port from environment variable */
7831                 mpd_set_port("6600");
7832         }
7833 #endif
7834 #ifdef XMMS2
7835         info.xmms2.artist = NULL;
7836         info.xmms2.album = NULL;
7837         info.xmms2.title = NULL;
7838         info.xmms2.genre = NULL;
7839         info.xmms2.comment = NULL;
7840         info.xmms2.url = NULL;
7841         info.xmms2.status = NULL;
7842         info.xmms2.playlist = NULL;
7843 #endif
7844         use_spacer = NO_SPACER;
7845 #ifdef X11
7846         output_methods = TO_X;
7847 #else
7848         output_methods = TO_STDOUT;
7849 #endif
7850 #ifdef X11
7851         show_graph_scale = 0;
7852         show_graph_range = 0;
7853         draw_shades = 1;
7854         draw_borders = 0;
7855         draw_graph_borders = 1;
7856         draw_outline = 0;
7857         set_first_font("6x10");
7858         gap_x = 5;
7859         gap_y = 60;
7860         minimum_width = 5;
7861         minimum_height = 5;
7862         maximum_width = 0;
7863 #ifdef OWN_WINDOW
7864         own_window = 0;
7865         window.type = TYPE_NORMAL;
7866         window.hints = 0;
7867         strcpy(window.class_name, PACKAGE_NAME);
7868         sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7869 #endif
7870         stippled_borders = 0;
7871         window.border_inner_margin = 3;
7872         window.border_outer_margin = 1;
7873         window.border_width = 1;
7874         text_alignment = BOTTOM_LEFT;
7875         info.x11.monitor.number = 1;
7876         info.x11.monitor.current = 0;
7877         info.x11.desktop.current = 1; 
7878         info.x11.desktop.number = 1;
7879         info.x11.desktop.nitems = 0;
7880         info.x11.desktop.all_names = NULL; 
7881         info.x11.desktop.name = NULL; 
7882 #endif /* X11 */
7883
7884         for (i = 0; i < MAX_TEMPLATES; i++) {
7885                 if (template[i])
7886                         free(template[i]);
7887                 template[i] = strdup("");
7888         }
7889
7890         free(current_mail_spool);
7891         {
7892                 char buf[256];
7893
7894                 variable_substitute(MAIL_FILE, buf, 256);
7895                 if (buf[0] != '\0') {
7896                         current_mail_spool = strndup(buf, text_buffer_size);
7897                 }
7898         }
7899
7900         no_buffers = 1;
7901         set_update_interval(3);
7902         update_interval_bat = NOBATTERY;
7903         info.music_player_interval = 1.0;
7904         stuff_in_uppercase = 0;
7905         info.users.number = 1;
7906
7907 #ifdef TCP_PORT_MONITOR
7908         /* set default connection limit */
7909         tcp_portmon_set_max_connections(0);
7910 #endif
7911 }
7912
7913 /* returns 1 if you can overwrite or create the file at 'path' */
7914 static _Bool overwrite_works(const char *path)
7915 {
7916         FILE *filepointer;
7917
7918         if (!(filepointer = fopen(path, "w")))
7919                 return 0;
7920         fclose(filepointer);
7921         return 1;
7922 }
7923
7924 /* returns 1 if you can append or create the file at 'path' */
7925 static _Bool append_works(const char *path)
7926 {
7927         FILE *filepointer;
7928
7929         if (!(filepointer = fopen(path, "a")))
7930                 return 0;
7931         fclose(filepointer);
7932         return 1;
7933 }
7934
7935 #ifdef X11
7936 #ifdef DEBUG
7937 /* WARNING, this type not in Xlib spec */
7938 int x11_error_handler(Display *d, XErrorEvent *err)
7939         __attribute__((noreturn));
7940 int x11_error_handler(Display *d, XErrorEvent *err)
7941 {
7942         ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7943                         err->type,
7944                         (long unsigned)err->display,
7945                         (long)err->resourceid,
7946                         err->serial,
7947                         err->error_code,
7948                         err->request_code,
7949                         err->minor_code,
7950                         (long unsigned)d
7951                         );
7952         abort();
7953 }
7954
7955 int x11_ioerror_handler(Display *d)
7956         __attribute__((noreturn));
7957 int x11_ioerror_handler(Display *d)
7958 {
7959         ERR("X Error: Display %lx\n",
7960                         (long unsigned)d
7961                         );
7962         abort();
7963 }
7964 #endif /* DEBUG */
7965
7966 static void X11_initialisation(void)
7967 {
7968         if (x_initialised == YES) return;
7969         output_methods |= TO_X;
7970         init_X11(disp);
7971         set_default_configurations_for_x();
7972         x_initialised = YES;
7973 #ifdef DEBUG
7974         _Xdebug = 1;
7975         /* WARNING, this type not in Xlib spec */
7976         XSetErrorHandler(&x11_error_handler);
7977         XSetIOErrorHandler(&x11_ioerror_handler);
7978 #endif /* DEBUG */
7979 }
7980
7981 static char **xargv = 0;
7982 static int xargc = 0;
7983
7984 static void X11_create_window(void)
7985 {
7986         if (output_methods & TO_X) {
7987 #ifdef OWN_WINDOW
7988                 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7989                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
7990                                 xargv, xargc);
7991 #else /* OWN_WINDOW */
7992                 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7993                                 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
7994                                 xargv, xargc);
7995 #endif /* OWN_WINDOW */
7996
7997                 setup_fonts();
7998                 load_fonts();
7999                 update_text_area();     /* to position text/window on screen */
8000
8001 #ifdef OWN_WINDOW
8002                 if (own_window && !fixed_pos) {
8003                         XMoveWindow(display, window.window, window.x, window.y);
8004                 }
8005                 if (own_window) {
8006                         set_transparent_background(window.window);
8007                 }
8008 #endif
8009
8010                 create_gc();
8011
8012                 draw_stuff();
8013
8014                 x11_stuff.region = XCreateRegion();
8015 #ifdef HAVE_XDAMAGE
8016                 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
8017                         ERR("Xdamage extension unavailable");
8018                 }
8019                 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
8020                 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
8021                 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
8022 #endif /* HAVE_XDAMAGE */
8023
8024                 selected_font = 0;
8025                 update_text_area();     /* to get initial size of the window */
8026         }
8027 #ifdef HAVE_LUA
8028         /* setup lua window globals */
8029         llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
8030 #endif /* HAVE_LUA */
8031 }
8032 #endif /* X11 */
8033
8034 #define CONF_ERR ERR("%s: %d: config file error", f, line)
8035 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
8036 #define CONF2(a) if (strcasecmp(name, a) == 0)
8037 #define CONF(a) else CONF2(a)
8038 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
8039                 || strcasecmp(name, b) == 0)
8040 #define CONF_CONTINUE 1
8041 #define CONF_BREAK 2
8042 #define CONF_BUFF_SIZE 512
8043
8044 static FILE *open_config_file(const char *f)
8045 {
8046 #ifdef CONFIG_OUTPUT
8047         if (!strcmp(f, "==builtin==")) {
8048                 return conf_cookie_open();
8049         } else
8050 #endif /* CONFIG_OUTPUT */
8051                 return fopen(f, "r");
8052 }
8053
8054 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
8055 {
8056         char *p, *p2;
8057         (*line)++;
8058         if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8059                 return CONF_BREAK;
8060         }
8061         remove_comments(buf);
8062
8063         p = buf;
8064
8065         /* skip spaces */
8066         while (*p && isspace((int) *p)) {
8067                 p++;
8068         }
8069         if (*p == '\0') {
8070                 return CONF_CONTINUE;   /* empty line */
8071         }
8072
8073         *name = p;
8074
8075         /* skip name */
8076         p2 = p;
8077         while (*p2 && !isspace((int) *p2)) {
8078                 p2++;
8079         }
8080         if (*p2 != '\0') {
8081                 *p2 = '\0';     /* break at name's end */
8082                 p2++;
8083         }
8084
8085         /* get value */
8086         if (*p2) {
8087                 p = p2;
8088                 while (*p && isspace((int) *p)) {
8089                         p++;
8090                 }
8091
8092                 *value = p;
8093
8094                 p2 = *value + strlen(*value);
8095                 while (isspace((int) *(p2 - 1))) {
8096                         *--p2 = '\0';
8097                 }
8098         } else {
8099                 *value = 0;
8100         }
8101         return 0;
8102 }
8103
8104 static void load_config_file(const char *f)
8105 {
8106         int line = 0;
8107         FILE *fp;
8108
8109         set_default_configurations();
8110         fp = open_config_file(f);
8111         if (!fp) {
8112                 return;
8113         }
8114         DBGP("reading contents from config file '%s'", f);
8115
8116         while (!feof(fp)) {
8117                 char buff[CONF_BUFF_SIZE], *name, *value;
8118                 int ret = do_config_step(&line, fp, buff, &name, &value);
8119                 if (ret == CONF_BREAK) {
8120                         break;
8121                 } else if (ret == CONF_CONTINUE) {
8122                         continue;
8123                 }
8124
8125 #ifdef X11
8126                 CONF2("out_to_x") {
8127                         /* don't listen if X is already initialised or
8128                          * if we already know we don't want it */
8129                         if(x_initialised != YES) {
8130                                 if (string_to_bool(value)) {
8131                                         output_methods &= TO_X;
8132                                 } else {
8133                                         output_methods &= ~TO_X;
8134                                         x_initialised = NEVER;
8135                                 }
8136                         }
8137                 }
8138                 CONF("display") {
8139                         if (!value || x_initialised == YES) {
8140                                 CONF_ERR;
8141                         } else {
8142                                 if (disp)
8143                                         free(disp);
8144                                 disp = strdup(value);
8145                         }
8146                 }
8147                 CONF("alignment") {
8148 #ifdef OWN_WINDOW
8149                         if (window.type == TYPE_DOCK)
8150                                 ;
8151                         else
8152 #endif /*OWN_WINDOW */
8153                         if (value) {
8154                                 int a = string_to_alignment(value);
8155
8156                                 if (a <= 0) {
8157                                         CONF_ERR;
8158                                 } else {
8159                                         text_alignment = a;
8160                                 }
8161                         } else {
8162                                 CONF_ERR;
8163                         }
8164                 }
8165                 CONF("background") {
8166                         fork_to_background = string_to_bool(value);
8167                 }
8168 #else
8169                 CONF2("background") {
8170                         fork_to_background = string_to_bool(value);
8171                 }
8172 #endif /* X11 */
8173 #ifdef X11
8174                 CONF("show_graph_scale") {
8175                         show_graph_scale = string_to_bool(value);
8176                 }
8177                 CONF("show_graph_range") {
8178                         show_graph_range = string_to_bool(value);
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                                 set_update_interval(strtod(value, 0));
8661                         } else {
8662                                 CONF_ERR;
8663                         }
8664                         if (info.music_player_interval == 0) {
8665                                 // default to update_interval
8666                                 info.music_player_interval = update_interval;
8667                         }
8668                 }
8669                 CONF("total_run_times") {
8670                         if (value) {
8671                                 total_run_times = strtod(value, 0);
8672                         } else {
8673                                 CONF_ERR;
8674                         }
8675                 }
8676                 CONF("uppercase") {
8677                         stuff_in_uppercase = string_to_bool(value);
8678                 }
8679                 CONF("max_specials") {
8680                         if (value) {
8681                                 max_specials = atoi(value);
8682                         } else {
8683                                 CONF_ERR;
8684                         }
8685                 }
8686                 CONF("max_user_text") {
8687                         if (value) {
8688                                 max_user_text = atoi(value);
8689                         } else {
8690                                 CONF_ERR;
8691                         }
8692                 }
8693                 CONF("text_buffer_size") {
8694                         if (value) {
8695                                 text_buffer_size = atoi(value);
8696                                 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8697                                         ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8698                                         text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8699                                 }
8700                         } else {
8701                                 CONF_ERR;
8702                         }
8703                 }
8704                 CONF("text") {
8705 #ifdef X11
8706                         if (output_methods & TO_X) {
8707                                 X11_initialisation();
8708                         }
8709 #endif
8710
8711                         if (global_text) {
8712                                 free(global_text);
8713                                 global_text = 0;
8714                         }
8715
8716                         global_text = (char *) malloc(1);
8717                         global_text[0] = '\0';
8718
8719                         while (!feof(fp)) {
8720                                 unsigned int l = strlen(global_text);
8721                                 unsigned int bl;
8722                                 char buf[CONF_BUFF_SIZE];
8723
8724                                 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8725                                         break;
8726                                 }
8727
8728                                 /* Remove \\-\n. */
8729                                 bl = strlen(buf);
8730                                 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8731                                         buf[bl-2] = '\0';
8732                                         bl -= 2;
8733                                         if (bl == 0) {
8734                                                 continue;
8735                                         }
8736                                 }
8737
8738                                 /* Check for continuation of \\-\n. */
8739                                 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8740                                         global_text[l-1] = '\0';
8741                                         continue;
8742                                 }
8743
8744                                 global_text = (char *) realloc(global_text, l + bl + 1);
8745                                 strcat(global_text, buf);
8746
8747                                 if (strlen(global_text) > max_user_text) {
8748                                         break;
8749                                 }
8750                         }
8751                         fclose(fp);
8752                         if (strlen(global_text) < 1) {
8753                                 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
8754                         }
8755                         global_text_lines = line + 1;
8756                         return;
8757                 }
8758 #ifdef TCP_PORT_MONITOR
8759                 CONF("max_port_monitor_connections") {
8760                         int max;
8761                         if (!value || (sscanf(value, "%d", &max) != 1)) {
8762                                 /* an error. use default, warn and continue. */
8763                                 tcp_portmon_set_max_connections(0);
8764                                 CONF_ERR;
8765                         } else if (tcp_portmon_set_max_connections(max)) {
8766                                 /* max is < 0, default has been set*/
8767                                 CONF_ERR;
8768                         }
8769                 }
8770 #endif
8771                 CONF("if_up_strictness") {
8772                         if (!value) {
8773                                 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8774                                 ifup_strictness = IFUP_UP;
8775                         } else if (strcasecmp(value, "up") == EQUAL) {
8776                                 ifup_strictness = IFUP_UP;
8777                         } else if (strcasecmp(value, "link") == EQUAL) {
8778                                 ifup_strictness = IFUP_LINK;
8779                         } else if (strcasecmp(value, "address") == EQUAL) {
8780                                 ifup_strictness = IFUP_ADDR;
8781                         } else {
8782                                 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8783                                 ifup_strictness = IFUP_UP;
8784                         }
8785                 }
8786
8787                 CONF("temperature_unit") {
8788                         if (!value) {
8789                                 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8790                         } else if (set_temp_output_unit(value)) {
8791                                 ERR("temperature_unit: incorrect argument");
8792                         }
8793                 }
8794
8795                 CONF("alias") {
8796                         if (value) {
8797                                 size_t maxlength = strlen(value);       //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8798                                 char *skey = malloc(maxlength);
8799                                 char *svalue = malloc(maxlength);
8800                                 char *oldvalue;
8801                                 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8802                                         oldvalue = getenv(skey);
8803                                         if (oldvalue == NULL) {
8804                                                 setenv(skey, svalue, 0);
8805                                         }
8806                                         //PS: Don't free oldvalue, it's the real envvar, not a copy
8807                                 } else {
8808                                         CONF_ERR;
8809                                 }
8810                                 free(skey);
8811                                 free(svalue);
8812                         } else {
8813                                 CONF_ERR;
8814                         }
8815                 }
8816 #ifdef HAVE_LUA
8817                 CONF("lua_load") {
8818                         if (value) {
8819                                 char *ptr = strtok(value, " ");
8820                                 while (ptr) {
8821                                         llua_load(ptr);
8822                                         ptr = strtok(NULL, " ");
8823                                 }
8824                         } else {
8825                                 CONF_ERR;
8826                         }
8827                 }
8828 #ifdef X11
8829                 CONF("lua_draw_hook_pre") {
8830                         if (value) {
8831                                 llua_set_draw_pre_hook(value);
8832                         } else {
8833                                 CONF_ERR;
8834                         }
8835                 }
8836                 CONF("lua_draw_hook_post") {
8837                         if (value) {
8838                                 llua_set_draw_post_hook(value);
8839                         } else {
8840                                 CONF_ERR;
8841                         }
8842                 }
8843 #endif /* X11 */
8844 #endif /* HAVE_LUA */
8845
8846                 CONF("color0"){}
8847                 CONF("color1"){}
8848                 CONF("color2"){}
8849                 CONF("color3"){}
8850                 CONF("color4"){}
8851                 CONF("color5"){}
8852                 CONF("color6"){}
8853                 CONF("color7"){}
8854                 CONF("color8"){}
8855                 CONF("color9"){}
8856                 CONF("default_color"){}
8857                 CONF3("default_shade_color", "default_shadecolor"){}
8858                 CONF3("default_outline_color", "default_outlinecolor") {}
8859                 CONF("own_window_colour") {}
8860
8861                 else {
8862                         ERR("%s: %d: no such configuration: '%s'", f, line, name);
8863                 }
8864         }
8865
8866         fclose(fp);
8867
8868         if (info.music_player_interval == 0) {
8869                 // default to update_interval
8870                 info.music_player_interval = update_interval;
8871         }
8872         if (!global_text) { // didn't supply any text
8873                 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
8874         }
8875 }
8876
8877 #ifdef X11
8878 static void load_config_file_x11(const char *f)
8879 {
8880         int line = 0;
8881         FILE *fp;
8882
8883         fp = open_config_file(f);
8884         if (!fp) {
8885                 return;
8886         }
8887         DBGP("reading contents from config file '%s'", f);
8888
8889         while (!feof(fp)) {
8890                 char buff[CONF_BUFF_SIZE], *name, *value;
8891                 int ret = do_config_step(&line, fp, buff, &name, &value);
8892                 if (ret == CONF_BREAK) {
8893                         break;
8894                 } else if (ret == CONF_CONTINUE) {
8895                         continue;
8896                 }
8897
8898                 CONF2("color0") {
8899                         X11_initialisation();
8900                         if (x_initialised == YES) {
8901                                 if (value) {
8902                                         color0 = get_x11_color(value);
8903                                 } else {
8904                                         CONF_ERR;
8905                                 }
8906                         }
8907                 }
8908                 CONF("color1") {
8909                         X11_initialisation();
8910                         if (x_initialised == YES) {
8911                                 if (value) {
8912                                         color1 = get_x11_color(value);
8913                                 } else {
8914                                         CONF_ERR;
8915                                 }
8916                         }
8917                 }
8918                 CONF("color2") {
8919                         X11_initialisation();
8920                         if (x_initialised == YES) {
8921                                 if (value) {
8922                                         color2 = get_x11_color(value);
8923                                 } else {
8924                                         CONF_ERR;
8925                                 }
8926                         }
8927                 }
8928                 CONF("color3") {
8929                         X11_initialisation();
8930                         if (x_initialised == YES) {
8931                                 if (value) {
8932                                         color3 = get_x11_color(value);
8933                                 } else {
8934                                         CONF_ERR;
8935                                 }
8936                         }
8937                 }
8938                 CONF("color4") {
8939                         X11_initialisation();
8940                         if (x_initialised == YES) {
8941                                 if (value) {
8942                                         color4 = get_x11_color(value);
8943                                 } else {
8944                                         CONF_ERR;
8945                                 }
8946                         }
8947                 }
8948                 CONF("color5") {
8949                         X11_initialisation();
8950                         if (x_initialised == YES) {
8951                                 if (value) {
8952                                         color5 = get_x11_color(value);
8953                                 } else {
8954                                         CONF_ERR;
8955                                 }
8956                         }
8957                 }
8958                 CONF("color6") {
8959                         X11_initialisation();
8960                         if (x_initialised == YES) {
8961                                 if (value) {
8962                                         color6 = get_x11_color(value);
8963                                 } else {
8964                                         CONF_ERR;
8965                                 }
8966                         }
8967                 }
8968                 CONF("color7") {
8969                         X11_initialisation();
8970                         if (x_initialised == YES) {
8971                                 if (value) {
8972                                         color7 = get_x11_color(value);
8973                                 } else {
8974                                         CONF_ERR;
8975                                 }
8976                         }
8977                 }
8978                 CONF("color8") {
8979                         X11_initialisation();
8980                         if (x_initialised == YES) {
8981                                 if (value) {
8982                                         color8 = get_x11_color(value);
8983                                 } else {
8984                                         CONF_ERR;
8985                                 }
8986                         }
8987                 }
8988                 CONF("color9") {
8989                         X11_initialisation();
8990                         if (x_initialised == YES) {
8991                                 if (value) {
8992                                         color9 = get_x11_color(value);
8993                                 } else {
8994                                         CONF_ERR;
8995                                 }
8996                         }
8997                 }
8998                 CONF("default_color") {
8999                         X11_initialisation();
9000                         if (x_initialised == YES) {
9001                                 if (value) {
9002                                         default_fg_color = get_x11_color(value);
9003                                 } else {
9004                                         CONF_ERR;
9005                                 }
9006                         }
9007                 }
9008                 CONF3("default_shade_color", "default_shadecolor") {
9009                         X11_initialisation();
9010                         if (x_initialised == YES) {
9011                                 if (value) {
9012                                         default_bg_color = get_x11_color(value);
9013                                 } else {
9014                                         CONF_ERR;
9015                                 }
9016                         }
9017                 }
9018                 CONF3("default_outline_color", "default_outlinecolor") {
9019                         X11_initialisation();
9020                         if (x_initialised == YES) {
9021                                 if (value) {
9022                                         default_out_color = get_x11_color(value);
9023                                 } else {
9024                                         CONF_ERR;
9025                                 }
9026                         }
9027                 }
9028 #ifdef OWN_WINDOW
9029                 CONF("own_window_colour") {
9030                         X11_initialisation();
9031                         if (x_initialised == YES) {
9032                                 if (value) {
9033                                         background_colour = get_x11_color(value);
9034                                 } else {
9035                                         ERR("Invalid colour for own_window_colour (try omitting the "
9036                                                 "'#' for hex colours");
9037                                 }
9038                         }
9039                 }
9040 #endif
9041                 CONF("text") {
9042                         /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
9043                         if(output_methods & TO_X) {
9044                                 X11_initialisation();
9045                         }
9046                 }
9047 #undef CONF
9048 #undef CONF2
9049 #undef CONF3
9050 #undef CONF_ERR
9051 #undef CONF_ERR2
9052 #undef CONF_BREAK
9053 #undef CONF_CONTINUE
9054 #undef CONF_BUFF_SIZE
9055         }
9056
9057         fclose(fp);
9058
9059 }
9060 #endif /* X11 */
9061
9062 static void print_help(const char *prog_name) {
9063         printf("Usage: %s [OPTION]...\n"
9064                         PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
9065                         "window. Command line options will override configurations defined in config\n"
9066                         "file.\n"
9067                         "   -v, --version             version\n"
9068                         "   -q, --quiet               quiet mode\n"
9069                         "   -D, --debug               increase debugging output, ie. -DD for more debugging\n"
9070                         "   -c, --config=FILE         config file to load\n"
9071 #ifdef CONFIG_OUTPUT
9072                         "   -C, --print-config        print the builtin default config to stdout\n"
9073                         "                             e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
9074 #endif
9075                         "   -d, --daemonize           daemonize, fork to background\n"
9076                         "   -h, --help                help\n"
9077 #ifdef X11
9078                         "   -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
9079                         "   -f, --font=FONT           font to use\n"
9080                         "   -X, --display=DISPLAY     X11 display to use\n"
9081 #ifdef OWN_WINDOW
9082                         "   -o, --own-window          create own window to draw\n"
9083 #endif
9084 #ifdef HAVE_XDBE
9085                         "   -b, --double-buffer       double buffer (prevents flickering)\n"
9086 #endif
9087                         "   -w, --window-id=WIN_ID    window id to draw\n"
9088                         "   -x X                      x position\n"
9089                         "   -y Y                      y position\n"
9090 #endif /* X11 */
9091                         "   -t, --text=TEXT           text to render, remember single quotes, like -t '$uptime'\n"
9092                         "   -u, --interval=SECS       update interval\n"
9093                         "   -i COUNT                  number of times to update "PACKAGE_NAME" (and quit)\n",
9094                         prog_name
9095         );
9096 }
9097
9098 /* : means that character before that takes an argument */
9099 static const char *getopt_string = "vVqdDt:u:i:hc:"
9100 #ifdef X11
9101         "x:y:w:a:f:X:"
9102 #ifdef OWN_WINDOW
9103         "o"
9104 #endif
9105 #ifdef HAVE_XDBE
9106         "b"
9107 #endif
9108 #endif /* X11 */
9109 #ifdef CONFIG_OUTPUT
9110         "C"
9111 #endif
9112         ;
9113
9114 static const struct option longopts[] = {
9115         { "help", 0, NULL, 'h' },
9116         { "version", 0, NULL, 'V' },
9117         { "debug", 0, NULL, 'D' },
9118         { "config", 1, NULL, 'c' },
9119 #ifdef CONFIG_OUTPUT
9120         { "print-config", 0, NULL, 'C' },
9121 #endif
9122         { "daemonize", 0, NULL, 'd' },
9123 #ifdef X11
9124         { "alignment", 1, NULL, 'a' },
9125         { "font", 1, NULL, 'f' },
9126         { "display", 1, NULL, 'X' },
9127 #ifdef OWN_WINDOW
9128         { "own-window", 0, NULL, 'o' },
9129 #endif
9130 #ifdef HAVE_XDBE
9131         { "double-buffer", 0, NULL, 'b' },
9132 #endif
9133         { "window-id", 1, NULL, 'w' },
9134 #endif /* X11 */
9135         { "text", 1, NULL, 't' },
9136         { "interval", 0, NULL, 'u' },
9137         { 0, 0, 0, 0 }
9138 };
9139
9140 void initialisation(int argc, char **argv) {
9141         struct sigaction act, oact;
9142
9143         load_config_file(current_config);
9144
9145         /* init specials array */
9146         if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9147                 ERR("failed to create specials array");
9148         }
9149
9150 #ifdef MAIL_FILE
9151         if (current_mail_spool == NULL) {
9152                 char buf[256];
9153
9154                 variable_substitute(MAIL_FILE, buf, 256);
9155
9156                 if (buf[0] != '\0') {
9157                         current_mail_spool = strndup(buf, text_buffer_size);
9158                 }
9159         }
9160 #endif
9161
9162         /* handle other command line arguments */
9163
9164 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9165                 || defined(__NetBSD__)
9166         optind = optreset = 1;
9167 #else
9168         optind = 0;
9169 #endif
9170
9171 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9172         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9173                         "kvm_open")) == NULL) {
9174                 CRIT_ERR(NULL, NULL, "cannot read kvm");
9175         }
9176 #endif
9177
9178         while (1) {
9179                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9180
9181                 if (c == -1) {
9182                         break;
9183                 }
9184
9185                 switch (c) {
9186                         case 'd':
9187                                 fork_to_background = 1;
9188                                 break;
9189                         case 'D':
9190                                 global_debug_level++;
9191                                 break;
9192 #ifdef X11
9193                         case 'f':
9194                                 set_first_font(optarg);
9195                                 break;
9196                         case 'a':
9197                                 text_alignment = string_to_alignment(optarg);
9198                                 break;
9199                         case 'X':
9200                                 if (disp)
9201                                         free(disp);
9202                                 disp = strdup(optarg);
9203                                 break;
9204
9205 #ifdef OWN_WINDOW
9206                         case 'o':
9207                                 own_window = 1;
9208                                 break;
9209 #endif
9210 #ifdef HAVE_XDBE
9211                         case 'b':
9212                                 use_xdbe = 1;
9213                                 break;
9214 #endif
9215 #endif /* X11 */
9216                         case 't':
9217                                 if (global_text) {
9218                                         free(global_text);
9219                                         global_text = 0;
9220                                 }
9221                                 global_text = strndup(optarg, max_user_text);
9222                                 convert_escapes(global_text);
9223                                 break;
9224
9225                         case 'u':
9226                                 update_interval = strtod(optarg, 0);
9227                                 update_interval_old = update_interval;
9228                                 if (info.music_player_interval == 0) {
9229                                         // default to update_interval
9230                                         info.music_player_interval = update_interval;
9231                                 }
9232                                 break;
9233
9234                         case 'i':
9235                                 total_run_times = strtod(optarg, 0);
9236                                 break;
9237 #ifdef X11
9238                         case 'x':
9239                                 gap_x = atoi(optarg);
9240                                 break;
9241
9242                         case 'y':
9243                                 gap_y = atoi(optarg);
9244                                 break;
9245 #endif /* X11 */
9246
9247                         case '?':
9248                                 exit(EXIT_FAILURE);
9249                 }
9250         }
9251
9252 #ifdef X11
9253         /* load font */
9254         if (output_methods & TO_X) {
9255                 load_config_file_x11(current_config);
9256         }
9257 #endif /* X11 */
9258
9259         /* generate text and get initial size */
9260         extract_variable_text(global_text);
9261         if (global_text) {
9262                 free(global_text);
9263                 global_text = 0;
9264         }
9265         global_text = NULL;
9266         /* fork */
9267         if (fork_to_background) {
9268                 int pid = fork();
9269
9270                 switch (pid) {
9271                         case -1:
9272                                 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9273                                         strerror(errno));
9274                                 break;
9275
9276                         case 0:
9277                                 /* child process */
9278                                 usleep(25000);
9279                                 fprintf(stderr, "\n");
9280                                 fflush(stderr);
9281                                 break;
9282
9283                         default:
9284                                 /* parent process */
9285                                 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9286                                         pid);
9287                                 fflush(stderr);
9288                                 exit(EXIT_SUCCESS);
9289                 }
9290         }
9291
9292         text_buffer = malloc(max_user_text);
9293         memset(text_buffer, 0, max_user_text);
9294         tmpstring1 = malloc(text_buffer_size);
9295         memset(tmpstring1, 0, text_buffer_size);
9296         tmpstring2 = malloc(text_buffer_size);
9297         memset(tmpstring2, 0, text_buffer_size);
9298
9299 #ifdef X11
9300         xargc = argc;
9301         xargv = argv;
9302         X11_create_window();
9303 #endif /* X11 */
9304 #ifdef HAVE_LUA
9305         llua_setup_info(&info, update_interval);
9306 #endif /* HAVE_LUA */
9307
9308         /* Set signal handlers */
9309         act.sa_handler = signal_handler;
9310         sigemptyset(&act.sa_mask);
9311         act.sa_flags = 0;
9312 #ifdef SA_RESTART
9313         act.sa_flags |= SA_RESTART;
9314 #endif
9315
9316         if (            sigaction(SIGINT,  &act, &oact) < 0
9317                         ||      sigaction(SIGALRM, &act, &oact) < 0
9318                         ||      sigaction(SIGUSR1, &act, &oact) < 0
9319                         ||      sigaction(SIGHUP,  &act, &oact) < 0
9320                         ||      sigaction(SIGTERM, &act, &oact) < 0) {
9321                 ERR("error setting signal handler: %s", strerror(errno));
9322         }
9323
9324 }
9325
9326 int main(int argc, char **argv)
9327 {
9328 #ifdef X11
9329         char *s, *temp;
9330         unsigned int x;
9331 #endif
9332
9333         argc_copy = argc;
9334         argv_copy = argv;
9335         g_signal_pending = 0;
9336         max_user_text = MAX_USER_TEXT_DEFAULT;
9337         current_config = 0;
9338         memset(&info, 0, sizeof(info));
9339         memset(template, 0, sizeof(template));
9340         clear_net_stats();
9341
9342 #ifdef TCP_PORT_MONITOR
9343         /* set default connection limit */
9344         tcp_portmon_set_max_connections(0);
9345 #endif
9346
9347         /* handle command line parameters that don't change configs */
9348 #ifdef X11
9349         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9350                         || ((s = getenv("LANG")) && *s)) {
9351                 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9352                 if (temp == NULL) {
9353                         ERR("malloc failed");
9354                 }
9355                 for (x = 0; x < strlen(s); x++) {
9356                         temp[x] = tolower(s[x]);
9357                 }
9358                 temp[x] = 0;
9359                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9360                         utf8_mode = 1;
9361                 }
9362
9363                 free(temp);
9364         }
9365         if (!setlocale(LC_CTYPE, "")) {
9366                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9367         }
9368 #endif /* X11 */
9369         while (1) {
9370                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9371
9372                 if (c == -1) {
9373                         break;
9374                 }
9375
9376                 switch (c) {
9377                         case 'v':
9378                         case 'V':
9379                                 print_version();
9380                         case 'c':
9381                                 if (current_config) {
9382                                         free(current_config);
9383                                 }
9384                                 current_config = strndup(optarg, max_user_text);
9385                                 break;
9386                         case 'q':
9387                                 freopen("/dev/null", "w", stderr);
9388                                 break;
9389                         case 'h':
9390                                 print_help(argv[0]);
9391                                 return 0;
9392 #ifdef CONFIG_OUTPUT
9393                         case 'C':
9394                                 print_defconfig();
9395                                 return 0;
9396 #endif
9397 #ifdef X11
9398                         case 'w':
9399                                 window.window = strtol(optarg, 0, 0);
9400                                 break;
9401 #endif /* X11 */
9402
9403                         case '?':
9404                                 exit(EXIT_FAILURE);
9405                 }
9406         }
9407
9408         /* check if specified config file is valid */
9409         if (current_config) {
9410                 struct stat sb;
9411                 if (stat(current_config, &sb) ||
9412                                 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9413                         ERR("invalid configuration file '%s'\n", current_config);
9414                         free(current_config);
9415                         current_config = 0;
9416                 }
9417         }
9418
9419         /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9420
9421         if (!current_config) {
9422                 /* load default config file */
9423                 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9424                 FILE *fp;
9425
9426                 /* Try to use personal config file first */
9427                 to_real_path(buf, CONFIG_FILE);
9428                 if (buf[0] && (fp = fopen(buf, "r"))) {
9429                         current_config = strndup(buf, max_user_text);
9430                         fclose(fp);
9431                 }
9432
9433                 /* Try to use system config file if personal config not readable */
9434                 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9435                         current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9436                         fclose(fp);
9437                 }
9438
9439                 /* No readable config found */
9440                 if (!current_config) {
9441 #ifdef CONFIG_OUTPUT
9442                         current_config = strdup("==builtin==");
9443                         ERR("no readable personal or system-wide config file found,"
9444                                         " using builtin default");
9445 #else
9446                         CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
9447 #endif /* ! CONF_OUTPUT */
9448                 }
9449         }
9450
9451 #ifdef XOAP
9452         /* Load xoap keys, if existing */
9453         load_xoap_keys();
9454 #endif /* XOAP */
9455
9456 #ifdef HAVE_SYS_INOTIFY_H
9457         inotify_fd = inotify_init();
9458 #endif /* HAVE_SYS_INOTIFY_H */
9459
9460         initialisation(argc, argv);
9461
9462         main_loop();
9463
9464 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9465         kvm_close(kd);
9466 #endif
9467
9468         return 0;
9469
9470 }
9471
9472 void alarm_handler(void) {
9473         if(childpid > 0) {
9474                 kill(childpid, SIGTERM);
9475         }
9476 }
9477
9478 static void signal_handler(int sig)
9479 {
9480         /* signal handler is light as a feather, as it should be.
9481          * we will poll g_signal_pending with each loop of conky
9482          * and do any signal processing there, NOT here (except 
9483          * SIGALRM because this is caused when conky is hanging) */
9484         if(sig == SIGALRM) {
9485                 alarm_handler();
9486         } else {
9487                 g_signal_pending = sig;
9488         }
9489 }