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