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