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