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