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