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