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