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