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