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