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