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