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