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