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