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