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