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