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