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