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