Remove freebsd wifi stuff since the API seems to be
[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 #if 0
2607         END OBJ(linkstatus, INFO_WIFI) 
2608                 if(arg) {
2609                         obj->data.net = get_net_stat(arg);
2610                 }
2611                 else {
2612                         CRIT_ERR("linkstatus needs argument");
2613                 }
2614 #endif
2615         END OBJ(tail, 0)
2616                 char buf[64];
2617         int n1, n2;
2618         if (!arg) {
2619                 ERR("tail needs arguments");
2620                 obj->type = OBJ_text;
2621                 obj->data.s = strdup("${tail}");
2622                 return NULL;
2623         }
2624         if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2625                 if (n1 < 1 || n1 > 30) {
2626                         CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
2627                         return NULL;
2628                 } else {
2629                         FILE *fp = NULL;
2630                         fp = fopen(buf, "r");
2631                         if (fp) {
2632                                 obj->data.tail.logfile =
2633                                         malloc(text_buffer_size);
2634                                 strcpy(obj->data.tail.logfile, buf);
2635                                 obj->data.tail.wantedlines = n1;
2636                                 obj->data.tail.interval =
2637                                         update_interval * 2;
2638                                 fclose(fp);
2639                         } else {
2640                                 //fclose (fp);
2641                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2642                         }
2643                 }
2644         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2645                 if (n1 < 1 || n1 > 30) {
2646                         CRIT_ERR
2647                                 ("invalid arg for tail, number of lines must be between 1 and 30");
2648                         return NULL;
2649                 } else if (n2 < 1 || n2 < update_interval) {
2650                         CRIT_ERR
2651                                 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
2652                         return NULL;
2653                 } else {
2654                         FILE *fp;
2655                         fp = fopen(buf, "r");
2656                         if (fp != NULL) {
2657                                 obj->data.tail.logfile =
2658                                         malloc(text_buffer_size);
2659                                 strcpy(obj->data.tail.logfile, buf);
2660                                 obj->data.tail.wantedlines = n1;
2661                                 obj->data.tail.interval = n2;
2662                                 fclose(fp);
2663                         } else {
2664                                 //fclose (fp);
2665                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2666                         }
2667                 }
2668         }
2669
2670         else {
2671                 ERR("invalid args given for tail");
2672                 return NULL;
2673         }
2674         obj->data.tail.buffer = malloc(text_buffer_size * 20); /* asumming all else worked */
2675         END OBJ(head, 0)
2676                 char buf[64];
2677         int n1, n2;
2678         if (!arg) {
2679                 ERR("head needs arguments");
2680                 obj->type = OBJ_text;
2681                 obj->data.s = strdup("${head}");
2682                 return NULL;
2683         }
2684         if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2685                 if (n1 < 1 || n1 > 30) {
2686                         CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
2687                         return NULL;
2688                 } else {
2689                         FILE *fp;
2690                         fp = fopen(buf, "r");
2691                         if (fp != NULL) {
2692                                 obj->data.tail.logfile =
2693                                         malloc(text_buffer_size);
2694                                 strcpy(obj->data.tail.logfile, buf);
2695                                 obj->data.tail.wantedlines = n1;
2696                                 obj->data.tail.interval =
2697                                         update_interval * 2;
2698                                 fclose(fp);
2699                         } else {
2700                                 //fclose (fp);
2701                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2702                         }
2703                 }
2704         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2705                 if (n1 < 1 || n1 > 30) {
2706                         CRIT_ERR
2707                                 ("invalid arg for head, number of lines must be between 1 and 30");
2708                         return NULL;
2709                 } else if (n2 < 1 || n2 < update_interval) {
2710                         CRIT_ERR
2711                                 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
2712                         return NULL;
2713                 } else {
2714                         FILE *fp;
2715                         fp = fopen(buf, "r");
2716                         if (fp != NULL) {
2717                                 obj->data.tail.logfile =
2718                                         malloc(text_buffer_size);
2719                                 strcpy(obj->data.tail.logfile, buf);
2720                                 obj->data.tail.wantedlines = n1;
2721                                 obj->data.tail.interval = n2;
2722                                 fclose(fp);
2723                         } else {
2724                                 //fclose (fp);
2725                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2726                         }
2727                 }
2728         }
2729
2730         else {
2731                 ERR("invalid args given for head");
2732                 return NULL;
2733         }
2734         obj->data.tail.buffer = malloc(text_buffer_size * 20); /* asumming all else worked */
2735         END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
2736         if (arg) {
2737                 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2738                 if (r >= 3 && (c < 1 || c > 3))
2739                         r--;
2740                 if (r >= 2 && (b < 1 || b > 3))
2741                         r--, b = c;
2742                 if (r >= 1 && (a < 1 || a > 3))
2743                         r--, a = b, b = c;
2744         }
2745         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2746         obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2747         obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2748         END OBJ(if_empty, 0)
2749                 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2750                         CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2751                 }
2752         if (!arg) {
2753                 ERR("if_empty needs an argument");
2754                 obj->data.ifblock.s = 0;
2755         } else
2756                 obj->data.ifblock.s = strdup(arg);
2757         blockstart[blockdepth] = object_count;
2758         obj->data.ifblock.pos = object_count + 2;
2759         blockdepth++;
2760         END OBJ(if_existing, 0)
2761                 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2762                         CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2763                 }
2764         if (!arg) {
2765                 ERR("if_existing needs an argument or two");
2766                 obj->data.ifblock.s = NULL;
2767                 obj->data.ifblock.str = NULL;       
2768         } else {
2769                 char buf1[256], buf2[256];
2770                 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2771                 if (r == 1) {
2772                         obj->data.ifblock.s = strdup(buf1);             
2773                         obj->data.ifblock.str = NULL;       
2774                 } else {
2775                         obj->data.ifblock.s = strdup(buf1);
2776                         obj->data.ifblock.str = strdup(buf2);
2777                 }
2778         }               
2779         blockstart[blockdepth] = object_count;
2780         obj->data.ifblock.pos = object_count + 2;
2781         blockdepth++;
2782         END OBJ(if_mounted, 0)
2783                 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2784                         CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2785                 }
2786         if (!arg) {
2787                 ERR("if_mounted needs an argument");
2788                 obj->data.ifblock.s = 0;
2789         } else
2790                 obj->data.ifblock.s = strdup(arg);
2791         blockstart[blockdepth] = object_count;
2792         obj->data.ifblock.pos = object_count + 2;
2793         blockdepth++;
2794         END OBJ(if_running, 0)
2795                 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2796                         CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2797                 }
2798         if (arg) {
2799                 char buf[256];
2800                 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2801                 obj->data.ifblock.s = strdup(buf);
2802         } else {
2803                 ERR("if_running needs an argument");
2804                 obj->data.ifblock.s = 0;
2805         }
2806         blockstart[blockdepth] = object_count;
2807         obj->data.ifblock.pos = object_count + 2;
2808         blockdepth++;
2809         END OBJ(kernel, 0)
2810                 END OBJ(machine, 0)
2811                 END OBJ(mails, INFO_MAIL)
2812                 END OBJ(mboxscan, 0)
2813                 obj->data.mboxscan.args = (char*)malloc(TEXT_BUFFER_SIZE);
2814                 obj->data.mboxscan.output = (char*)malloc(text_buffer_size);
2815                 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2816                 obj->data.mboxscan.output[0] = 1;
2817                 strncpy(obj->data.mboxscan.args, arg, TEXT_BUFFER_SIZE);
2818                 END OBJ(mem, INFO_MEM)
2819                 END OBJ(memmax, INFO_MEM)
2820                 END OBJ(memperc, INFO_MEM)
2821                 END OBJ(membar, INFO_MEM)
2822                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2823         END OBJ(memgraph, INFO_MEM)
2824                 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2825         END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
2826         END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
2827         END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
2828         END OBJ(mixerbar, INFO_MIXER)
2829                 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2830                                 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2831         END OBJ(mixerlbar, INFO_MIXER)
2832                 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2833                                 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2834         END OBJ(mixerrbar, INFO_MIXER)
2835                 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2836                                 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2837         END
2838                 OBJ(new_mails, INFO_MAIL)
2839                 END OBJ(nodename, 0)
2840                 END OBJ(processes, INFO_PROCS)
2841                 END OBJ(running_processes, INFO_RUN_PROCS)
2842                 END OBJ(shadecolor, 0)
2843 #ifdef X11
2844                 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2845 #endif /* X11 */
2846         END OBJ(outlinecolor, 0)
2847 #ifdef X11
2848                 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2849 #endif /* X11 */
2850         END OBJ(stippled_hr, 0)
2851 #ifdef X11
2852                 int a = stippled_borders, b = 1;
2853         if (arg) {
2854                 if (sscanf(arg, "%d %d", &a, &b) != 2)
2855                         sscanf(arg, "%d", &b);
2856         }
2857         if (a <= 0)
2858                 a = 1;
2859         obj->data.pair.a = a;
2860         obj->data.pair.b = b;
2861 #endif /* X11 */
2862         END OBJ(swap, INFO_MEM)
2863                 END OBJ(swapmax, INFO_MEM)
2864                 END OBJ(swapperc, INFO_MEM)
2865                 END OBJ(swapbar, INFO_MEM)
2866                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2867         END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
2868         obj->data.i2c.fd =
2869                 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
2870                                 obj->data.i2c.devtype);
2871         END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
2872         obj->data.i2c.fd =
2873                 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
2874                                 obj->data.i2c.devtype);
2875         END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2876         END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2877         END OBJ(tztime, 0)
2878                 char buf1[256], buf2[256], *fmt, *tz;
2879                 fmt = tz = NULL;
2880                 if (arg) {
2881                         int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2882                         switch (nArgs) {
2883                                 case 2:
2884                                         tz = buf1;
2885                                 case 1:
2886                                         fmt = buf2;
2887                         }
2888                 }
2889
2890                 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
2891                 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
2892 #ifdef HAVE_ICONV
2893         END OBJ(iconv_start, 0)
2894                 if (iconv_converting) {
2895                         CRIT_ERR("You must stop your last iconv conversion before starting another");
2896                 }
2897                 if (arg) {
2898                         char iconv_from[CODEPAGE_LENGTH];
2899                         char iconv_to[CODEPAGE_LENGTH];
2900                         if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2901                                 CRIT_ERR("Invalid arguments for iconv_start");
2902                         } else {
2903                                 iconv_t new_iconv;
2904                                 new_iconv = iconv_open(iconv_to, iconv_from);
2905                                 if (new_iconv == (iconv_t)(-1)) {
2906                                         ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2907                                 } else {
2908                                         obj->a = register_iconv(&new_iconv);
2909                                         iconv_converting = 1;
2910                                 }
2911                         }
2912                 } else {
2913                         CRIT_ERR("Iconv requires arguments");
2914                 }
2915         END OBJ(iconv_stop, 0)
2916                 iconv_converting = 0;
2917         
2918 #endif
2919         END OBJ(totaldown, INFO_NET)
2920                 if(arg) {
2921                         obj->data.net = get_net_stat(arg);
2922                 }
2923                 else {
2924                         CRIT_ERR("totaldown needs argument");
2925                 }
2926         END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
2927         if(arg) {
2928                 obj->data.net = get_net_stat(arg);
2929         }
2930         else {
2931                 CRIT_ERR("totalup needs argument");
2932         }
2933         END OBJ(updates, 0)
2934                 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
2935         END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
2936         END OBJ(upspeed, INFO_NET)
2937                 if(arg) {
2938                         obj->data.net = get_net_stat(arg);
2939                 }
2940                 else {
2941                         CRIT_ERR("upspeed needs argument");
2942                 }
2943         END OBJ(upspeedf, INFO_NET) 
2944                 if(arg) {
2945                         obj->data.net = get_net_stat(arg);
2946                 }
2947                 else {
2948                         CRIT_ERR("upspeedf needs argument");
2949                 }
2950
2951         END OBJ(upspeedgraph, INFO_NET)
2952                 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2953         char buf[64];
2954         sscanf(arg, "%63s %*i,%*i %*i", buf);
2955         obj->data.net = get_net_stat(buf);
2956         if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2957                 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
2958                         obj->a = 0;
2959                         obj->b = 25;
2960                 }
2961         }
2962         END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
2963                 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
2964 #if (defined(__FreeBSD__) || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2965                 OBJ(apm_adapter, 0) END
2966                 OBJ(apm_battery_life, 0) END
2967                 OBJ(apm_battery_time, 0) END
2968 #endif /* __FreeBSD__ */
2969                 OBJ(imap_unseen, 0)
2970                 if (arg) {
2971                         // proccss
2972                         obj->data.mail = parse_mail_args(IMAP, arg);
2973                         obj->global_mode = 0;
2974                 } else {
2975                         obj->global_mode = 1;
2976                 }
2977                 END
2978                 OBJ(imap_messages, 0)
2979                 if (arg) {
2980                         // proccss
2981                         obj->data.mail = parse_mail_args(IMAP, arg);
2982                         obj->global_mode = 0;
2983                 } else {
2984                         obj->global_mode = 1;
2985                 }
2986                 END
2987                 OBJ(pop3_unseen, 0)
2988                 if (arg) {
2989                         // proccss
2990                         obj->data.mail = parse_mail_args(POP3, arg);
2991                         obj->global_mode = 0;
2992                 } else {
2993                         obj->global_mode = 1;
2994                 }
2995                 END
2996                 OBJ(pop3_used, 0)
2997                 if (arg) {
2998                         // proccss
2999                         obj->data.mail = parse_mail_args(POP3, arg);
3000                         obj->global_mode = 0;
3001                 } else {
3002                         obj->global_mode = 1;
3003                 }
3004                 END
3005 #ifdef MPD
3006                 OBJ(mpd_artist, INFO_MPD) END
3007                 OBJ(mpd_title, INFO_MPD)
3008                 {
3009                         if (arg)
3010                         {
3011                             sscanf (arg, "%d", &info.mpd.max_title_len);
3012                             if (info.mpd.max_title_len > 0)
3013                                 info.mpd.max_title_len++;
3014                             else
3015                                 CRIT_ERR ("mpd_title: invalid length argument");
3016                         }
3017                 }
3018                 END OBJ(mpd_random, INFO_MPD)
3019                 END OBJ(mpd_repeat, INFO_MPD)
3020                 END OBJ(mpd_elapsed, INFO_MPD)
3021                 END OBJ(mpd_length, INFO_MPD)
3022                 END OBJ(mpd_track, INFO_MPD)
3023                 END OBJ(mpd_name, INFO_MPD)
3024                 END OBJ(mpd_file, INFO_MPD)
3025                 END OBJ(mpd_percent, INFO_MPD)
3026                 END OBJ(mpd_album, INFO_MPD)
3027                 END OBJ(mpd_vol, INFO_MPD)
3028                 END OBJ(mpd_bitrate, INFO_MPD)
3029                 END OBJ(mpd_status, INFO_MPD)
3030                 END OBJ(mpd_bar, INFO_MPD)
3031                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3032                 END OBJ(mpd_smart, INFO_MPD) END
3033 #endif
3034 #ifdef XMMS2
3035                 OBJ(xmms2_artist, INFO_XMMS2)
3036                 END OBJ(xmms2_album, INFO_XMMS2)
3037                 END OBJ(xmms2_title, INFO_XMMS2)
3038                 END OBJ(xmms2_genre, INFO_XMMS2)
3039                 END OBJ(xmms2_comment, INFO_XMMS2)
3040                 END OBJ(xmms2_decoder, INFO_XMMS2)
3041                 END OBJ(xmms2_transport, INFO_XMMS2)
3042                 END OBJ(xmms2_url, INFO_XMMS2)
3043                 END OBJ(xmms2_tracknr, INFO_XMMS2)
3044                 END OBJ(xmms2_bitrate, INFO_XMMS2)
3045                 END OBJ(xmms2_date, INFO_XMMS2)
3046                 END OBJ(xmms2_id, INFO_XMMS2)
3047                 END OBJ(xmms2_duration, INFO_XMMS2)
3048                 END OBJ(xmms2_elapsed, INFO_XMMS2)
3049                 END OBJ(xmms2_size, INFO_XMMS2)
3050                 END OBJ(xmms2_status, INFO_XMMS2)
3051                 END OBJ(xmms2_percent, INFO_XMMS2)
3052                 END OBJ(xmms2_bar, INFO_XMMS2)
3053                 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
3054                 END OBJ(xmms2_smart, INFO_XMMS2)
3055                 END
3056 #endif
3057 #ifdef AUDACIOUS
3058                 OBJ(audacious_status, INFO_AUDACIOUS) END
3059                 OBJ(audacious_title, INFO_AUDACIOUS) 
3060                 {
3061                         if (arg)
3062                         {
3063                             sscanf (arg, "%d", &info.audacious.max_title_len);
3064                             if (info.audacious.max_title_len > 0)
3065                                 info.audacious.max_title_len++;
3066                             else
3067                                 CRIT_ERR ("audacious_title: invalid length argument");
3068                         }
3069                 }
3070                 END
3071                 OBJ(audacious_length, INFO_AUDACIOUS) END
3072                 OBJ(audacious_length_seconds, INFO_AUDACIOUS) END
3073                 OBJ(audacious_position, INFO_AUDACIOUS) END
3074                 OBJ(audacious_position_seconds, INFO_AUDACIOUS) END
3075                 OBJ(audacious_bitrate, INFO_AUDACIOUS) END
3076                 OBJ(audacious_frequency, INFO_AUDACIOUS) END
3077                 OBJ(audacious_channels, INFO_AUDACIOUS) END
3078                 OBJ(audacious_filename, INFO_AUDACIOUS) END
3079                 OBJ(audacious_playlist_length, INFO_AUDACIOUS) END
3080                 OBJ(audacious_playlist_position, INFO_AUDACIOUS) END
3081                 OBJ(audacious_bar, INFO_AUDACIOUS)
3082                 (void) scan_bar(arg, &obj->a, &obj->b);
3083         END
3084 #endif
3085 #ifdef BMPX
3086                 OBJ(bmpx_title, INFO_BMPX)
3087                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3088         END
3089                 OBJ(bmpx_artist, INFO_BMPX)
3090                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3091         END
3092                 OBJ(bmpx_album, INFO_BMPX)
3093                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3094         END
3095                 OBJ(bmpx_track, INFO_BMPX)
3096                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3097         END
3098                 OBJ(bmpx_uri, INFO_BMPX)
3099                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3100         END
3101                 OBJ(bmpx_bitrate, INFO_BMPX)
3102                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
3103         END
3104 #endif
3105 #ifdef RSS
3106         OBJ(rss, 0) 
3107                 if (arg) {
3108                         int argc, delay, act_par;
3109                         char *uri = (char *)malloc(128 * sizeof(char));
3110                         char *action = (char *)malloc(64 * sizeof(char));
3111
3112                         argc = sscanf(arg, "%127s %d %63s %d", uri, &delay, action, &act_par);
3113                         obj->data.rss.uri = uri;
3114                         obj->data.rss.delay = delay;
3115                         obj->data.rss.action = action;
3116                         obj->data.rss.act_par = act_par;
3117
3118                         init_rss_info();
3119                 } else
3120                         CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> [act_par]");
3121
3122         END
3123 #endif
3124 #ifdef HDDTEMP
3125         OBJ(hddtemp, 0)
3126                 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev, 
3127                         &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3128                         ERR("hddtemp needs arguments");
3129                         obj->type = OBJ_text;
3130                         obj->data.s = strdup("${hddtemp}");
3131                         return NULL;
3132                 }
3133         END
3134 #endif
3135 #ifdef TCP_PORT_MONITOR
3136                 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR) 
3137                 int argc, port_begin, port_end, item, connection_index;
3138         char itembuf[32];
3139         memset(itembuf,0,sizeof(itembuf));
3140         connection_index=0;
3141         /* massive argument checking */
3142         if (!arg) {
3143                 CRIT_ERR("tcp_portmon: needs arguments");
3144         }
3145         argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
3146         if ( (argc != 3) && (argc != 4) ) 
3147         {
3148                 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
3149         }
3150         if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
3151         {
3152                 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
3153         }
3154         if ( port_begin > port_end )
3155         {
3156                 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
3157         }
3158         if ( strncmp(itembuf,"count",31) == 0 )
3159                 item=COUNT;
3160         else if ( strncmp(itembuf,"rip",31) == 0 )
3161                 item=REMOTEIP;
3162         else if ( strncmp(itembuf,"rhost",31) == 0 )
3163                 item=REMOTEHOST;
3164         else if ( strncmp(itembuf,"rport",31) == 0 )
3165                 item=REMOTEPORT;
3166         else if ( strncmp(itembuf,"rservice",31) == 0 )
3167                 item=REMOTESERVICE;
3168         else if ( strncmp(itembuf,"lip",31) == 0 )
3169                 item=LOCALIP;
3170         else if ( strncmp(itembuf,"lhost",31) == 0 )
3171                 item=LOCALHOST;
3172         else if ( strncmp(itembuf,"lport",31) == 0 )
3173                 item=LOCALPORT;
3174         else if ( strncmp(itembuf,"lservice",31) == 0 )
3175                 item=LOCALSERVICE;
3176         else
3177         {
3178                 CRIT_ERR("tcp_portmon: invalid item specified"); 
3179         }
3180         if ( (argc==3) && (item!=COUNT) )
3181         {
3182                 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
3183         }
3184         if ( (argc==4) && (connection_index<0) )
3185         {
3186                 CRIT_ERR("tcp_portmon: connection index must be non-negative");
3187         }
3188         /* ok, args looks good. save the text object data */
3189         obj->data.tcp_port_monitor.port_range_begin = (in_port_t)port_begin;
3190         obj->data.tcp_port_monitor.port_range_end = (in_port_t)port_end;
3191         obj->data.tcp_port_monitor.item = item;
3192         obj->data.tcp_port_monitor.connection_index = connection_index;
3193
3194         /* if the port monitor collection hasn't been created, we must create it */
3195         if ( !info.p_tcp_port_monitor_collection )
3196         {
3197                 info.p_tcp_port_monitor_collection = 
3198                         create_tcp_port_monitor_collection ();
3199                 if ( !info.p_tcp_port_monitor_collection )
3200                 {
3201                         CRIT_ERR("tcp_portmon: unable to create port monitor collection");
3202                 }
3203         }
3204
3205         /* if a port monitor for this port does not exist, create one and add it to the collection */
3206         if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
3207         {
3208                 tcp_port_monitor_t * p_monitor = 
3209                         create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
3210                 if ( !p_monitor )
3211                 {
3212                         CRIT_ERR("tcp_portmon: unable to create port monitor");
3213                 }
3214                 /* add the newly created monitor to the collection */
3215                 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
3216                                         p_monitor ) != 0 )
3217                 {
3218                         CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
3219                 }
3220         }
3221         END
3222 #endif
3223         OBJ(entropy_avail, INFO_ENTROPY) END
3224         OBJ(entropy_poolsize, INFO_ENTROPY) END
3225         OBJ(entropy_bar, INFO_ENTROPY)
3226                 (void) scan_bar(arg, &obj->a, &obj->b);
3227         END
3228
3229         {
3230                 char buf[256];
3231                 ERR("unknown variable %s", s);
3232                 obj->type = OBJ_text;
3233                 snprintf(buf, 256, "${%s}", s);
3234                 obj->data.s = strdup(buf);
3235         }
3236 #undef OBJ
3237
3238         return obj;
3239 }
3240
3241 static struct text_object *create_plain_text(const char *s)
3242 {
3243         struct text_object *obj;
3244
3245         if (s == NULL || *s == '\0') {
3246                 return NULL;
3247         }
3248
3249         obj = new_text_object_internal();
3250
3251         obj->type = OBJ_text;
3252         obj->data.s = strdup(s);
3253         return obj;
3254 }
3255
3256 static struct text_object_list *extract_variable_text_internal(const char *p)
3257 {
3258         struct text_object_list *retval;
3259         struct text_object *obj;
3260         const char *s = p;
3261
3262         retval = malloc(sizeof(struct text_object_list));
3263         memset(retval, 0, sizeof(struct text_object_list));
3264         retval->text_object_count = 0;
3265
3266         long line = text_lines;
3267
3268         while (*p) {
3269                 if (*p == '\n') {
3270                         line++;
3271                 }
3272                 if (*p == '$') {
3273                         *(char *) p = '\0';
3274                         obj = create_plain_text(s);
3275                         if(obj != NULL) {
3276                                 // allocate memory for the object
3277                                 retval->text_objects = realloc(retval->text_objects, 
3278                                                 sizeof(struct text_object) * (retval->text_object_count+1));
3279                                 // assign the new object to the end of the list.
3280                                 memcpy(&retval->text_objects[retval->text_object_count++],
3281                                                 obj, sizeof(struct text_object));
3282                                 free(obj);
3283                         }
3284                         *(char *) p = '$';
3285                         p++;
3286                         s = p;
3287
3288                         if (*p != '$') {
3289                                 char buf[256];
3290                                 const char *var;
3291                                 unsigned int len;
3292
3293                                 /* variable is either $foo or ${foo} */
3294                                 if (*p == '{') {
3295                                         unsigned int brl = 1, brr = 0;
3296                                         p++;
3297                                         s = p;
3298                                         while (*p && brl != brr) {
3299                                                 if (*p == '{') brl++;
3300                                                 if (*p == '}') brr++;
3301                                                 p++;
3302                                         }
3303                                         p--;
3304                                 } else {
3305                                         s = p;
3306                                         if (*p == '#')
3307                                                 p++;
3308                                         while (*p && (isalnum((int) *p)
3309                                                                 || *p == '_'))
3310                                                 p++;
3311                                 }
3312
3313                                 /* copy variable to buffer */
3314                                 len = (p - s > 255) ? 255 : (p - s);
3315                                 strncpy(buf, s, len);
3316                                 buf[len] = '\0';
3317
3318                                 if (*p == '}')
3319                                         p++;
3320                                 s = p;
3321
3322                                 var = getenv(buf);
3323
3324                                 /* if variable wasn't found from environment, use some special */
3325                                 if (!var) {
3326                                         char *p;
3327                                         char *arg = 0;
3328
3329                                         /* split arg */
3330                                         if (strchr(buf, ' ')) {
3331                                                 arg = strchr(buf, ' ');
3332                                                 *arg = '\0';
3333                                                 arg++;
3334                                                 while (isspace((int) *arg))
3335                                                         arg++;
3336                                                 if (!*arg)
3337                                                         arg = 0;
3338                                         }
3339
3340                                         /* lowercase variable name */
3341                                         p = buf;
3342                                         while (*p) {
3343                                                 *p = tolower(*p);
3344                                                 p++;
3345                                         }
3346
3347                                         // create new object
3348                                         obj = construct_text_object(buf, arg, retval->text_object_count, retval->text_objects, line);
3349                                         if(obj != NULL) {
3350                                                 // allocate memory for the object
3351                                                 retval->text_objects = realloc(retval->text_objects, 
3352                                                                 sizeof(struct text_object) * (retval->text_object_count+1));
3353                                                 // assign the new object to the end of the list.
3354                                                 memcpy(&retval->text_objects[retval->text_object_count++],
3355                                                                 obj, sizeof(struct text_object));
3356                                                 free(obj);
3357                                         }
3358                                 }
3359                                 continue;
3360                         } else {
3361                                 obj = create_plain_text("$");
3362                                 if(obj != NULL) {
3363                                         // allocate memory for the object
3364                                         retval->text_objects = realloc(retval->text_objects, 
3365                                                         sizeof(struct text_object) * (retval->text_object_count+1));
3366                                         // assign the new object to the end of the list.
3367                                         memcpy(&retval->text_objects[retval->text_object_count++],
3368                                                         obj, sizeof(struct text_object));
3369                                         free(obj);
3370                                 }
3371                         }
3372                 }
3373                 p++;
3374         }
3375         obj = create_plain_text(s);
3376         if(obj != NULL) {
3377                 // allocate memory for the object
3378                 retval->text_objects = realloc(retval->text_objects,
3379                                 sizeof(struct text_object) * (retval->text_object_count+1));
3380                 // assign the new object to the end of the list.
3381                 memcpy(&retval->text_objects[retval->text_object_count++],
3382                                 obj, sizeof(struct text_object));
3383                 free(obj);
3384         }
3385
3386         if (blockdepth) {
3387                 ERR("one or more $endif's are missing");
3388         }
3389
3390         return retval;
3391 }
3392
3393 static void extract_variable_text(const char *p)
3394 {
3395         struct text_object_list *list;
3396
3397         free_text_objects(text_object_count, text_objects);
3398         text_object_count = 0;
3399         text_objects = NULL;
3400
3401
3402         list = extract_variable_text_internal(p);
3403         text_objects = list->text_objects;
3404         text_object_count = list->text_object_count;
3405
3406         free(list);
3407
3408         return;
3409 }
3410
3411 void parse_conky_vars(char * text, char * p, struct information *cur) { 
3412         struct text_object_list *object_list = extract_variable_text_internal(text);
3413         generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
3414         free(object_list);
3415 }
3416
3417 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
3418 {
3419         unsigned int i;
3420
3421 #ifdef HAVE_ICONV
3422         char buff_in[P_MAX_SIZE] = {0};
3423         iconv_converting = 0;
3424 #endif
3425
3426         for (i = 0; i < object_count; i++) {
3427                 struct text_object *obj = &objs[i];
3428
3429 #define OBJ(a) break; case OBJ_##a:
3430
3431                 switch (obj->type) {
3432                         default:
3433                                 {
3434                                         ERR("not implemented obj type %d",
3435                                                         obj->type);
3436                                 }
3437                                 OBJ(acpitemp) {
3438                                         /* does anyone have decimals in acpi temperature? */
3439                                         if (!use_spacer)
3440                                                 snprintf(p, p_max_size, "%d", (int)
3441                                                                 get_acpi_temperature(obj->
3442                                                                         data.
3443                                                                         i));
3444                                         else
3445                                                 snprintf(p, 5, "%d    ", (int)
3446                                                                 get_acpi_temperature(obj->
3447                                                                         data.
3448                                                                         i));
3449                                 }
3450                                 OBJ(acpitempf) {
3451                                         /* does anyone have decimals in acpi temperature? */
3452                                         if (!use_spacer)
3453                                                 snprintf(p, p_max_size, "%d", (int)
3454                                                                 ((get_acpi_temperature(obj->
3455                                                                                        data.
3456                                                                                        i)+ 40) * 9.0 / 5 - 40));
3457                                         else
3458                                                 snprintf(p, 5, "%d    ", (int)
3459                                                                 ((get_acpi_temperature(obj->
3460                                                                                        data.
3461                                                                                        i)+ 40) * 9.0 / 5 - 40));
3462                                 }
3463                                 OBJ(freq) {
3464                                         if (obj->a) {
3465                                                 obj->a = get_freq(p, p_max_size, "%.0f", 1, obj->data.cpu_index); 
3466                                         }
3467                                 }
3468                                 OBJ(freq_g) {
3469                                         if (obj->a) {
3470                                                 obj->a = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.cpu_index); 
3471                                         }
3472                                 }
3473 #if defined(__linux__)
3474                                 OBJ(voltage_mv) {
3475                                         if (obj->a) {
3476                                                 obj->a = get_voltage(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3477                                         }
3478                                 }
3479                                 OBJ(voltage_v) {
3480                                         if (obj->a) {
3481                                                 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.cpu_index);
3482                                         }
3483                                 }
3484 #endif /* __linux__ */
3485
3486                                 OBJ(freq_dyn) {
3487                                         if (use_spacer) {
3488                                                 get_freq_dynamic(p, 6, "%.0f     ", 1 ); 
3489                                         } else {
3490                                                 get_freq_dynamic(p, p_max_size, "%.0f", 1 ); 
3491                                         }
3492                                 }
3493                                 OBJ(freq_dyn_g) {
3494                                         if (use_spacer) {
3495                                                 get_freq_dynamic(p, 6, "%'.2f     ", 1000); 
3496                                         } else {
3497                                                 get_freq_dynamic(p, p_max_size, "%'.2f", 1000); 
3498                                         }
3499                                 }
3500                                 OBJ(adt746xcpu) {
3501                                         get_adt746x_cpu(p, p_max_size); 
3502                                 }
3503                                 OBJ(adt746xfan) {
3504                                         get_adt746x_fan(p, p_max_size); 
3505                                 }
3506                                 OBJ(acpifan) {
3507                                         get_acpi_fan(p, p_max_size);  
3508                                 }
3509                                 OBJ(acpiacadapter) {
3510                                         get_acpi_ac_adapter(p, p_max_size); 
3511                                 }
3512                                 OBJ(battery) {
3513                                         get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3514                                 }
3515                                 OBJ(battery_time) {
3516                                         get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3517                                 }
3518                                 OBJ(battery_percent) {
3519                                         snprintf(p, p_max_size, "%d", 
3520                                                 get_battery_perct(obj->data.s));
3521                                 }
3522                                 OBJ(battery_bar) {
3523                                         new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3524                                 }
3525                                 OBJ(buffers) {
3526                                         human_readable(cur->buffers * 1024, p, 255);
3527                                 }
3528                                 OBJ(cached) {
3529                                         human_readable(cur->cached * 1024, p, 255);
3530                                 }
3531                                 OBJ(cpu) {
3532                                         if (obj->data.cpu_index > info.cpu_count) {
3533                                                 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
3534                                                 CRIT_ERR("attempting to use more CPUs then you have!");
3535                                         }
3536                                         if (!use_spacer)
3537                                                 snprintf(p, p_max_size, "%*d", pad_percents,
3538                                                                 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3539                                                                                    100.0));
3540                                         else
3541                                                 snprintf(p, 4, "%*d    ",
3542                                                                 pad_percents,
3543                                                                 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3544                                                                                    100.0));
3545                                 }
3546                                 OBJ(cpubar) {
3547                                         new_bar(p, obj->a,
3548                                                         obj->b,
3549                                                         (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3550                                 }
3551                                 OBJ(cpugraph) {
3552                                         new_graph(p, obj->a,
3553                                                         obj->b, obj->c, obj->d,
3554                                                         (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3555                                                                                     100), 100, 1);
3556                                 }
3557                                 OBJ(color) {
3558                                         new_fg(p, obj->data.l);
3559                                 }
3560                                 OBJ(color0) {
3561                                         new_fg(p, color0);
3562                                 }
3563                                 OBJ(color1) {
3564                                         new_fg(p, color1);
3565                                 }
3566                                 OBJ(color2) {
3567                                         new_fg(p, color2);
3568                                 }
3569                                 OBJ(color3) {
3570                                         new_fg(p, color3);
3571                                 }
3572                                 OBJ(color4) {
3573                                         new_fg(p, color4);
3574                                 }
3575                                 OBJ(color5) {
3576                                         new_fg(p, color5);
3577                                 }
3578                                 OBJ(color6) {
3579                                         new_fg(p, color6);
3580                                 }
3581                                 OBJ(color7) {
3582                                         new_fg(p, color7);
3583                                 }
3584                                 OBJ(color8) {
3585                                         new_fg(p, color8);
3586                                 }
3587                                 OBJ(color9) {
3588                                         new_fg(p, color9);
3589                                 }
3590 #if defined(__linux__)
3591                                 OBJ(i8k_version) {
3592                                         snprintf(p, p_max_size, "%s", i8k.version);
3593                                 }
3594                                 OBJ(i8k_bios) {
3595                                         snprintf(p, p_max_size, "%s", i8k.bios);
3596                                 }
3597                                 OBJ(i8k_serial) { 
3598                                         snprintf(p, p_max_size, "%s", i8k.serial);
3599                                 }
3600                                 OBJ(i8k_cpu_temp) { 
3601                                         snprintf(p, p_max_size, "%s", i8k.cpu_temp);
3602                                 }
3603                                 OBJ(i8k_cpu_tempf) { 
3604                                         int cpu_temp;
3605                                         sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3606                                         snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
3607                                 }
3608                                 OBJ(i8k_left_fan_status) { 
3609                                         int left_fan_status;
3610                                         sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3611                                         if(left_fan_status == 0) {
3612                                                 snprintf(p, p_max_size,"off");
3613                                         } if(left_fan_status == 1) {
3614                                                 snprintf(p, p_max_size, "low");
3615                                         }       if(left_fan_status == 2) {
3616                                                 snprintf(p, p_max_size, "high");
3617                                         }
3618
3619                                 }
3620                                 OBJ(i8k_right_fan_status) { 
3621                                         int right_fan_status;
3622                                         sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3623                                         if(right_fan_status == 0) {
3624                                                 snprintf(p, p_max_size,"off");
3625                                         } if(right_fan_status == 1) {
3626                                                 snprintf(p, p_max_size, "low");
3627                                         }       if(right_fan_status == 2) {
3628                                                 snprintf(p, p_max_size, "high");
3629                                         }
3630                                 }
3631                                 OBJ(i8k_left_fan_rpm) { 
3632                                         snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3633                                 }
3634                                 OBJ(i8k_right_fan_rpm) { 
3635                                         snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3636                                 }
3637                                 OBJ(i8k_ac_status) { 
3638                                         int ac_status;
3639                                         sscanf(i8k.ac_status, "%d", &ac_status);
3640                                         if(ac_status == -1) {
3641                                                 snprintf(p, p_max_size,"disabled (read i8k docs)");
3642                                         } if(ac_status == 0) {
3643                                                 snprintf(p, p_max_size, "off");
3644                                         }       if(ac_status == 1) {
3645                                                 snprintf(p, p_max_size, "on");
3646                                         }
3647                                 }
3648                                 OBJ(i8k_buttons_status) {
3649                                         snprintf(p, p_max_size, "%s", i8k.buttons_status); 
3650
3651                                 }
3652                                 OBJ(ibm_fan) {
3653                                     get_ibm_acpi_fan(p, p_max_size);
3654                                 }
3655                                 OBJ(ibm_temps) {
3656                                     get_ibm_acpi_temps();
3657                                     snprintf(p, p_max_size, "%d",
3658                                              ibm_acpi.temps[obj->data.sensor]);
3659                                 }
3660                                 OBJ(ibm_volume) {
3661                                     get_ibm_acpi_volume(p, p_max_size);
3662                                 }
3663                                 OBJ(ibm_brightness) {
3664                                     get_ibm_acpi_brightness(p, p_max_size);
3665                                 }
3666                                 OBJ(pb_battery) {
3667                                     get_powerbook_batt_info(p, p_max_size, obj->data.i);
3668                                 }
3669 #endif /* __linux__ */
3670
3671 #ifdef __OpenBSD__
3672                                 OBJ(obsd_sensors_temp) {
3673                                         obsd_sensors.device = sensor_device;
3674                                         update_obsd_sensors();
3675                                         snprintf(p, p_max_size, "%.1f",
3676                                                 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor]);
3677                                 }
3678
3679                                 OBJ(obsd_sensors_fan) {
3680                                         obsd_sensors.device = sensor_device;
3681                                         update_obsd_sensors();
3682                                         snprintf(p, p_max_size, "%d",
3683                                                 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
3684                                 }
3685
3686                                 OBJ(obsd_sensors_volt) {
3687                                         obsd_sensors.device = sensor_device;
3688                                         update_obsd_sensors();
3689                                         snprintf(p, p_max_size, "%.2f",
3690                                                 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
3691                                 }
3692
3693                                 OBJ(obsd_vendor) {
3694                                         get_obsd_vendor(p, p_max_size);
3695                                 }
3696
3697                                 OBJ(obsd_product) {
3698                                         get_obsd_product(p, p_max_size);
3699                                 }
3700 #endif /* __OpenBSD__ */
3701
3702                                 OBJ(font) {
3703 #ifdef X11
3704                                         new_font(p, obj->data.s);
3705 #endif /* X11 */
3706                                 }
3707                                 void format_diskio(unsigned int diskio_value)
3708                                 {
3709                                         if (!use_spacer) {
3710                                                 if (diskio_value > 1024*1024) {
3711                                                         snprintf(p, p_max_size, "%.1fGiB",
3712                                                                         (double)diskio_value/1024/1024);
3713                                                 } else if (diskio_value > 1024) {
3714                                                         snprintf(p, p_max_size, "%.1fMiB",
3715                                                                         (double)diskio_value/1024);
3716                                                 } else if (diskio_value > 0) {
3717                                                         snprintf(p, p_max_size, "%dKiB", diskio_value);
3718                                                 } else {
3719                                                         snprintf(p, p_max_size, "%dB", diskio_value);
3720                                                 }
3721                                         } else {
3722                                                 if (diskio_value > 1024*1024) {
3723                                                         snprintf(p, 12, "%.1fGiB   ",
3724                                                                         (double)diskio_value/1024/1024);
3725                                                 } else if (diskio_value > 1024) {
3726                                                         snprintf(p, 12, "%.1fMiB   ",
3727                                                                         (double)diskio_value/1024);
3728                                                 } else if (diskio_value > 0) {
3729                                                         snprintf(p, 12, "%dKiB ", diskio_value);
3730                                                 } else {
3731                                                         snprintf(p, 12, "%dB     ", diskio_value);
3732                                                 }
3733                                         }
3734                                 }
3735                                 OBJ(diskio) {
3736                                         format_diskio(diskio_value);
3737                                 }
3738                                 OBJ(diskio_write) {
3739                                         format_diskio(diskio_write_value);
3740                                 }
3741                                 OBJ(diskio_read) {
3742                                         format_diskio(diskio_read_value);
3743                                 }
3744                                 OBJ(diskiograph) {
3745                                         new_graph(p, obj->a,
3746                                                         obj->b, obj->c, obj->d,
3747                                                         diskio_value, obj->e, 1);
3748                                 }
3749                                 OBJ(diskiograph_read) {
3750                                         new_graph(p, obj->a,
3751                                                         obj->b, obj->c, obj->d,
3752                                                         diskio_read_value, obj->e, 1);
3753                                 }
3754                                 OBJ(diskiograph_write) {
3755                                         new_graph(p, obj->a,
3756                                                         obj->b, obj->c, obj->d,
3757                                                         diskio_write_value, obj->e, 1);
3758                                 }
3759                                 OBJ(downspeed) {
3760                                         if (!use_spacer) {
3761                                                 snprintf(p, p_max_size, "%d",
3762                                                                 (int) (obj->data.net->
3763                                                                        recv_speed /
3764                                                                        1024));
3765                                         } else
3766                                                 snprintf(p, 6, "%d     ",
3767                                                                 (int) (obj->data.net->
3768                                                                        recv_speed /
3769                                                                        1024));
3770                                 }
3771                                 OBJ(downspeedf) {
3772                                         if (!use_spacer)
3773                                                 snprintf(p, p_max_size, "%.1f",
3774                                                                 obj->data.net->
3775                                                                 recv_speed / 1024.0);
3776                                         else
3777                                                 snprintf(p, 8, "%.1f       ",
3778                                                                 obj->data.net->
3779                                                                 recv_speed / 1024.0);
3780                                 }
3781                                 OBJ(downspeedgraph) {
3782           /*
3783                                         if (obj->data.net->recv_speed == 0)     
3784                                                 obj->data.net->recv_speed = 0.01;
3785             */
3786                                         new_graph(p, obj->a, obj->b, obj->c, obj->d,
3787                                                         (obj->data.net->recv_speed /
3788                                                          1024.0), obj->e, 1);
3789                                 }
3790                                 OBJ(else) {
3791                                         if (!if_jumped) {
3792                                                 i = obj->data.ifblock.pos - 1;
3793                                         } else {
3794                                                 if_jumped = 0;
3795                                         }
3796                                 }
3797                                 OBJ(endif) {
3798                                         if_jumped = 0;
3799                                 }
3800 #ifdef HAVE_POPEN
3801                                 OBJ(addr) {
3802                                         snprintf(p, p_max_size, "%u.%u.%u.%u",
3803                                                         obj->data.net->addr.
3804                                                         sa_data[2] & 255,
3805                                                         obj->data.net->addr.
3806                                                         sa_data[3] & 255,
3807                                                         obj->data.net->addr.
3808                                                         sa_data[4] & 255,
3809                                                         obj->data.net->addr.
3810                                                         sa_data[5] & 255);
3811
3812                                 }
3813                                 OBJ(linkstatus) {
3814                                         if (!use_spacer) {
3815                                                 snprintf(p, p_max_size, "%d",
3816                                                         obj->data.net->linkstatus);
3817                                         } else
3818                                         {
3819                                                 snprintf(p, 5, "%4d ",
3820                                                         obj->data.net->linkstatus);
3821                                         }
3822                                 }
3823 #if defined(IMLIB2) && defined(X11)
3824                                 OBJ(image) {
3825                                         if (obj->a < 1) {
3826                                                 obj->a++;
3827                                         } else {
3828                                                 Imlib_Image image, buffer;
3829                                                 image = imlib_load_image(obj->data.s);
3830                                                 imlib_context_set_image(image);
3831                                                 if (image) {
3832                                                         int w, h;
3833                                                         w = imlib_image_get_width();
3834                                                         h = imlib_image_get_height();
3835                                                         buffer = imlib_create_image(w, h);
3836                                                         imlib_context_set_display(display);
3837                                                         imlib_context_set_drawable(window.drawable);
3838                                                         imlib_context_set_colormap(DefaultColormap(display, screen));
3839                                                         imlib_context_set_visual(DefaultVisual(display, screen));
3840                                                         imlib_context_set_image(buffer);
3841                                                         imlib_blend_image_onto_image(image, 0, 0, 0, w, h, text_start_x, text_start_y, w, h);
3842                                                         imlib_render_image_on_drawable(text_start_x, text_start_y);
3843                                                         imlib_free_image();
3844                                                         imlib_context_set_image(image);
3845                                                         imlib_free_image();
3846                                                 }
3847                                         }
3848                                 }
3849 #endif /* IMLIB2 */
3850                                 OBJ(exec) {
3851                                         FILE *fp = popen(obj->data.s, "r");
3852                                         int length = fread(p, 1, p_max_size, fp);
3853                                         (void) pclose(fp);
3854
3855                                         /*output[length] = '\0';
3856                                           if (length > 0 && output[length - 1] == '\n') {
3857                                           output[length - 1] = '\0';
3858                                           }*/
3859                                         p[length] = '\0';
3860                                         if (length > 0 && p[length - 1] == '\n') {
3861                                                 p[length - 1] = '\0';
3862                                         }
3863
3864                                         //parse_conky_vars(output, p, cur);
3865                                 }
3866                                 OBJ(execbar) {
3867                                         char *p2 = p;
3868                                         FILE *fp = popen(obj->data.s, "r");
3869                                         int n2 = fread(p, 1, p_max_size, fp);
3870                                         (void) pclose(fp);
3871
3872                                         p[n2] = '\0';
3873                                         if (n2 && p[n2 - 1] == '\n')
3874                                                 p[n2 - 1] = '\0';
3875
3876                                         while (*p2) {
3877                                                 if (*p2 == '\001')
3878                                                         *p2 = ' ';
3879                                                 p2++;
3880                                         }
3881                                         double barnum;
3882                                         if (sscanf(p, "%lf", &barnum) == 0) {
3883                                                 ERR("reading execbar value failed (perhaps it's not the correct format?)");
3884                                         }
3885                                         if (barnum > 100 || barnum < 0) {
3886                                                 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
3887                                         } else {
3888                                                 barnum = barnum / 100.0;
3889                                                 new_bar(p, 0, 4, (int) (barnum * 255.0));
3890                                         }
3891
3892                                 }
3893                                 OBJ(execgraph) {
3894                                         char *p2 = p;
3895                                         FILE *fp = popen(obj->data.s, "r");
3896                                         int n2 = fread(p, 1, p_max_size, fp);
3897                                         (void) pclose(fp);
3898
3899                                         p[n2] = '\0';
3900                                         if (n2 && p[n2 - 1] == '\n')
3901                                                 p[n2 - 1] = '\0';
3902
3903                                         while (*p2) {
3904                                                 if (*p2 == '\001')
3905                                                         *p2 = ' ';
3906                                                 p2++;
3907                                         }
3908                                         double barnum;
3909                                         if (sscanf(p, "%lf", &barnum) == 0) {
3910                                                 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
3911                                         }
3912                                         if (barnum > 100 || barnum < 0) {
3913                                                 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
3914                                         } else {
3915                                                 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
3916                                         }
3917
3918                                 }
3919                                 OBJ(execibar) {
3920                                         if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3921                                                 new_bar(p, 0, 4, (int) obj->f);
3922                                         } else {
3923                                                 char *p2 = p;
3924                                                 FILE *fp = popen(obj->data.execi.cmd, "r");
3925                                                 int n2 = fread(p, 1, p_max_size, fp);
3926                                                 (void) pclose(fp);
3927                                                 p[n2] = '\0';
3928                                                 if (n2 && p[n2 - 1] == '\n')
3929                                                         p[n2 - 1] = '\0';
3930
3931                                                 while (*p2) {
3932                                                         if (*p2 == '\001')
3933                                                                 *p2 = ' ';
3934                                                         p2++;
3935                                                 }
3936                                                 float barnum;
3937                                                 if (sscanf(p, "%f", &barnum) == 0) {
3938                                                         ERR("reading execibar value failed (perhaps it's not the correct format?)");
3939                                                 }
3940                                                 if (barnum > 100 || barnum < 0) {
3941                                                         ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
3942                                                 } else {
3943                                                         obj->f = 255 * barnum / 100.0;
3944                                                         new_bar(p, 0, 4, (int) obj->f);
3945                                                 }
3946                                                 obj->data.execi.last_update =
3947                                                         current_update_time;
3948                                         }
3949                                 }
3950                                 OBJ(execigraph) {
3951                                         if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3952                                                 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
3953                                         } else {
3954                                                 char *p2 = p;
3955                                                 FILE *fp = popen(obj->data.execi.cmd, "r");
3956                                                 int n2 = fread(p, 1, p_max_size, fp);
3957                                                 (void) pclose(fp);
3958                                                 p[n2] = '\0';
3959                                                 if (n2 && p[n2 - 1] == '\n')
3960                                                         p[n2 - 1] = '\0';
3961
3962                                                 while (*p2) {
3963                                                         if (*p2 == '\001')
3964                                                                 *p2 = ' ';
3965                                                         p2++;
3966                                                 }
3967                                                 float barnum;
3968                                                 if (sscanf(p, "%f", &barnum) == 0) {
3969                                                         ERR("reading execigraph value failed (perhaps it's not the correct format?)");
3970                                                 }
3971                                                 if (barnum > 100 || barnum < 0) {
3972                                                         ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
3973                                                 } else {
3974                                                         obj->f = barnum;
3975                                                         new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
3976                                                 }
3977                                                 obj->data.execi.last_update = current_update_time;
3978
3979                                         }
3980
3981                                 }
3982                                 OBJ(execi) {
3983                                         if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
3984                                                 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3985                                         } else {
3986                                                 char *output = obj->data.execi.buffer;
3987                                                 FILE *fp = popen(obj->data.execi.cmd, "r");
3988                                                 //int length = fread(output, 1, text_buffer_size, fp);
3989                                                 int length = fread(output, 1, text_buffer_size, fp);
3990                                                 (void) pclose(fp);
3991
3992                                                 output[length] = '\0';
3993                                                 if (length > 0 && output[length - 1] == '\n') {
3994                                                         output[length - 1] = '\0';
3995                                                 }
3996                                                 obj->data.execi.last_update = current_update_time;
3997                                                 snprintf(p, p_max_size, "%s", output);
3998                                         }
3999                                         //parse_conky_vars(output, p, cur);
4000                                 }
4001                                 OBJ(texeci) {
4002                                         if (!obj->data.texeci.p_timed_thread)
4003                                         {
4004                                             obj->data.texeci.p_timed_thread=
4005                                             timed_thread_create ((void*)threaded_exec, (void*) obj, 
4006                                                                  obj->data.texeci.interval * 1000000);
4007                                             if (!obj->data.texeci.p_timed_thread)
4008                                                 ERR("Error starting texeci thread");
4009                                             timed_thread_register (obj->data.texeci.p_timed_thread,
4010                                                                    &obj->data.texeci.p_timed_thread);
4011                                         }
4012                                         timed_thread_lock (obj->data.texeci.p_timed_thread);
4013                                         snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
4014                                         timed_thread_unlock (obj->data.texeci.p_timed_thread);
4015                                 }
4016 #endif /* HAVE_POPEN */
4017                                 OBJ(imap_unseen) {
4018                                         if (obj->global_mode && info.mail) { // this means we use info
4019                                                 if (!info.mail->p_timed_thread)
4020                                                 {
4021                                                     info.mail->p_timed_thread = 
4022                                                     timed_thread_create ((void*)imap_thread, 
4023                                                                          (void*)info.mail,
4024                                                                          info.mail->interval * 1000000);
4025                                                     if (!info.mail->p_timed_thread)
4026                                                          ERR("Error starting imap thread");
4027                                                     timed_thread_register (info.mail->p_timed_thread,
4028                                                                            &info.mail->p_timed_thread);
4029                                                 }
4030                                                 timed_thread_lock (info.mail->p_timed_thread);
4031                                                 snprintf(p, p_max_size, "%lu", info.mail->unseen);
4032                                                 timed_thread_unlock (info.mail->p_timed_thread);
4033                                         } else if (obj->data.mail) { // this means we use obj
4034                                                 if (!obj->data.mail->p_timed_thread)
4035                                                 {
4036                                                     obj->data.mail->p_timed_thread = 
4037                                                     timed_thread_create ((void*)imap_thread, 
4038                                                                          (void*)obj->data.mail,
4039                                                                          obj->data.mail->interval * 1000000);
4040                                                     if (!obj->data.mail->p_timed_thread)
4041                                                         ERR("Error starting imap thread");
4042                                                     timed_thread_register (obj->data.mail->p_timed_thread,
4043                                                                            &obj->data.mail->p_timed_thread);
4044                                                 }
4045                                                 timed_thread_lock (obj->data.mail->p_timed_thread);
4046                                                 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
4047                                                 timed_thread_unlock (obj->data.mail->p_timed_thread);
4048                                         } else if (!obj->a) { // something is wrong, warn once then stop
4049                                                 ERR("Theres a problem with your imap_unseen settings.  Check that the global IMAP settings are defined properly (line %li).", obj->line);
4050                                                         obj->a++;
4051                                         }
4052                                 }
4053                                 OBJ(imap_messages) {
4054                                         if (obj->global_mode && info.mail) { // this means we use info
4055                                                 if (!info.mail->p_timed_thread)
4056                                                 {
4057                                                     info.mail->p_timed_thread =
4058                                                     timed_thread_create ((void*)imap_thread, 
4059                                                                          (void*)info.mail,
4060                                                                          info.mail->interval * 1000000);
4061                                                     if (!info.mail->p_timed_thread)
4062                                                          ERR("Error starting imap thread");
4063                                                     timed_thread_register (info.mail->p_timed_thread,
4064                                                                            &info.mail->p_timed_thread);
4065                                                 }
4066                                                 timed_thread_lock (info.mail->p_timed_thread);
4067                                                 snprintf(p, p_max_size, "%lu", info.mail->messages);
4068                                                 timed_thread_unlock (info.mail->p_timed_thread);
4069                                         } else if (obj->data.mail) { // this means we use obj
4070                                                 if (!obj->data.mail->p_timed_thread)
4071                                                 {
4072                                                     obj->data.mail->p_timed_thread =
4073                                                     timed_thread_create ((void*)imap_thread,
4074                                                                          (void*)obj->data.mail,
4075                                                                          obj->data.mail->interval * 1000000);
4076                                                     if (!obj->data.mail->p_timed_thread)
4077                                                         ERR("Error starting imap thread");
4078                                                     timed_thread_register (obj->data.mail->p_timed_thread,
4079                                                                            &obj->data.mail->p_timed_thread);
4080                                                 }
4081                                                 timed_thread_lock (obj->data.mail->p_timed_thread);
4082                                                 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
4083                                                 timed_thread_lock (obj->data.mail->p_timed_thread);
4084                                         } else if (!obj->a) { // something is wrong, warn once then stop
4085                                                 ERR("Theres a problem with your imap_messages settings.  Check that the global IMAP settings are defined properly (line %li).", obj->line);
4086                                                         obj->a++;
4087                                         }
4088                                 }
4089                                 OBJ(pop3_unseen) {
4090                                         if (obj->global_mode && info.mail) { // this means we use info
4091                                                 if (!info.mail->p_timed_thread)
4092                                                 {
4093                                                     info.mail->p_timed_thread = 
4094                                                     timed_thread_create ((void*)pop3_thread,
4095                                                                          (void*)info.mail,
4096                                                                          info.mail->interval * 1000000);
4097                                                     if (!info.mail->p_timed_thread)
4098                                                         ERR("Error starting pop3 thread");
4099                                                     timed_thread_register (info.mail->p_timed_thread,
4100                                                                            &info.mail->p_timed_thread);
4101                                                 }
4102                                                 timed_thread_lock (info.mail->p_timed_thread);
4103                                                 snprintf(p, p_max_size, "%lu", info.mail->unseen);
4104                                                 timed_thread_unlock (info.mail->p_timed_thread);
4105                                         } else if (obj->data.mail) { // this means we use obj
4106                                                 if (!obj->data.mail->p_timed_thread)
4107                                                 {
4108                                                     obj->data.mail->p_timed_thread = 
4109                                                     timed_thread_create ((void*)pop3_thread,
4110                                                                          (void*)obj->data.mail,
4111                                                                          obj->data.mail->interval * 1000000);
4112                                                     if (!obj->data.mail->p_timed_thread)
4113                                                         ERR("Error starting pop3 thread");
4114                                                     timed_thread_register (obj->data.mail->p_timed_thread,
4115                                                                            &obj->data.mail->p_timed_thread);
4116                                                 }
4117                                                 timed_thread_lock (obj->data.mail->p_timed_thread);
4118                                                 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
4119                                                 timed_thread_unlock (obj->data.mail->p_timed_thread);
4120                                         } else if (!obj->a) { // something is wrong, warn once then stop
4121                                                 ERR("Theres a problem with your pop3_unseen settings.  Check that the global POP3 settings are defined properly (line %li).", obj->line);
4122                                                         obj->a++;
4123                                         }
4124                                 }
4125                                 OBJ(pop3_used) {
4126                                         if (obj->global_mode && info.mail) { // this means we use info
4127                                                 if (!info.mail->p_timed_thread)
4128                                                 {
4129                                                     info.mail->p_timed_thread = 
4130                                                     timed_thread_create ((void*)pop3_thread,
4131                                                                          (void*)info.mail,
4132                                                                          info.mail->interval * 1000000);
4133                                                     if (!info.mail->p_timed_thread)
4134                                                         ERR("Error starting pop3 thread");
4135                                                     timed_thread_register (info.mail->p_timed_thread,
4136                                                                            &info.mail->p_timed_thread);
4137                                                 }
4138                                                 timed_thread_lock (info.mail->p_timed_thread);
4139                                                 snprintf(p, p_max_size, "%.1f", info.mail->used/1024.0/1024.0);
4140                                                 timed_thread_unlock (info.mail->p_timed_thread);
4141                                         } else if (obj->data.mail) { // this means we use obj
4142                                                 if (!obj->data.mail->p_timed_thread)
4143                                                 {
4144                                                     obj->data.mail->p_timed_thread =
4145                                                     timed_thread_create ((void*)pop3_thread,
4146                                                                          (void*)obj->data.mail,
4147                                                                          obj->data.mail->interval * 1000000);
4148                                                     if (!obj->data.mail->p_timed_thread)
4149                                                         ERR("Error starting pop3 thread");
4150                                                     timed_thread_register (obj->data.mail->p_timed_thread,
4151                                                                            &obj->data.mail->p_timed_thread);
4152                                                 }
4153                                                 timed_thread_lock (obj->data.mail->p_timed_thread);
4154                                                 snprintf(p, p_max_size, "%.1f", obj->data.mail->used/1024.0/1024.0);
4155                                                 timed_thread_unlock (obj->data.mail->p_timed_thread);
4156                                         } else if (!obj->a) { // something is wrong, warn once then stop
4157                                                 ERR("Theres a problem with your pop3_used settings.  Check that the global POP3 settings are defined properly (line %li).", obj->line);
4158                                                         obj->a++;
4159                                         }
4160                                 }
4161                         OBJ(fs_bar) {
4162                                 if (obj->data.fs != NULL) {
4163                                         if (obj->data.fs->size == 0)
4164                                                 new_bar(p,
4165                                                         obj->data.fsbar.w,
4166                                                         obj->data.fsbar.h,
4167                                                         255);
4168                                         else
4169                                                 new_bar(p,
4170                                                         obj->data.fsbar.w,
4171                                                         obj->data.fsbar.h,
4172                                                         (int) (255 -
4173                                                                obj->data.
4174                                                                fsbar.fs->
4175                                                                avail *
4176                                                                255 /
4177                                                                obj->data.
4178                                                                fs->size));
4179                                 }
4180                         }
4181                         OBJ(fs_free) {
4182                                 if (obj->data.fs != NULL)
4183                                         human_readable(obj->data.fs->avail,
4184                                                        p, 255);
4185                         }
4186                         OBJ(fs_free_perc) {
4187                                 if (obj->data.fs != NULL) {
4188                                         if (obj->data.fs->size)
4189                                                 snprintf(p, p_max_size, "%*d",
4190                                                          pad_percents,
4191                                                          (int) ((obj->data.
4192                                                                  fs->
4193                                                                  avail *
4194                                                                  100) /
4195                                                                 obj->data.
4196                                                                 fs->size));
4197                                         else
4198                                                 snprintf(p, p_max_size, "0");
4199                                 }
4200                         }
4201                         OBJ(fs_size) {
4202                                 if (obj->data.fs != NULL)
4203                                         human_readable(obj->data.fs->size,
4204                                                        p, 255);
4205                         }
4206                         OBJ(fs_used) {
4207                                 if (obj->data.fs != NULL)
4208                                         human_readable(obj->data.fs->size -
4209                                                        (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
4210                                                        p, 255);
4211                         }
4212                         OBJ(fs_bar_free) {
4213                                 if (obj->data.fs != NULL) {
4214                                         if (obj->data.fs->size == 0)
4215                                                 new_bar(p,
4216                                                         obj->data.fsbar.w,
4217                                                         obj->data.fsbar.h,
4218                                                         255);
4219                                         else
4220                                                 new_bar(p,
4221                                                         obj->data.fsbar.w,
4222                                                         obj->data.fsbar.h,
4223                                                         (int) (obj->data.
4224                                                                fsbar.fs->
4225                                                                avail *
4226                                                                255 /
4227                                                                obj->data.
4228                                                                fs->size));
4229                                 }
4230                         }
4231                         OBJ(fs_used_perc) {
4232                                 if (obj->data.fs != NULL) {
4233                                         if (obj->data.fs->size)
4234                                                 snprintf(p, 4, "%d",
4235                                                          100 - ((int)
4236                                                                 ((obj->
4237                                                                   data.fs->
4238                                                                   avail *
4239                                                                   100) /
4240                                                                  obj->data.
4241                                                                  fs->
4242                                                                  size)));
4243                                         else
4244                                                 snprintf(p, p_max_size, "0");
4245                                 }
4246                         }
4247                         OBJ(loadavg) {
4248                                 float *v = info.loadavg;
4249
4250                                 if (obj->data.loadavg[2])
4251                                         snprintf(p, p_max_size, "%.2f %.2f %.2f",
4252                                                  v[obj->data.loadavg[0] -
4253                                                    1],
4254                                                  v[obj->data.loadavg[1] -
4255                                                    1],
4256                                                  v[obj->data.loadavg[2] -
4257                                                    1]);
4258                                 else if (obj->data.loadavg[1])
4259                                         snprintf(p, p_max_size, "%.2f %.2f",
4260                                                  v[obj->data.loadavg[0] -
4261                                                    1],
4262                                                  v[obj->data.loadavg[1] -
4263                                                    1]);
4264                                 else if (obj->data.loadavg[0])
4265                                         snprintf(p, p_max_size, "%.2f",
4266                                                  v[obj->data.loadavg[0] -
4267                                                    1]);
4268                         }
4269                         OBJ(goto) {
4270                                 new_goto(p, obj->data.i);
4271                         }
4272                         OBJ(tab) {
4273                                 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4274                         }
4275                         OBJ(hr) {
4276                                 new_hr(p, obj->data.i);
4277                         }
4278 #ifdef RSS
4279                         OBJ(rss) {
4280                                 PRSS* data = get_rss_info(obj->data.rss.uri, obj->data.rss.delay);
4281                                 char *str;
4282                                 if(data == NULL)
4283                                         snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4284                                 else {
4285                                         if(!strcmp(obj->data.rss.action, "feed_title")) {
4286                                                         str = data->title;
4287                                                         if(str[strlen(str)-1] == '\n')
4288                                                                 str[strlen(str)-1] = 0; // remove trailing new line if one exists
4289                                                         snprintf(p, p_max_size, "%s", str);
4290                                         } else if(!strcmp(obj->data.rss.action, "item_title")) {
4291                                                 if(obj->data.rss.act_par < data->item_count) {
4292                                                         str = data->items[obj->data.rss.act_par].title;
4293                                                         if(str[strlen(str)-1] == '\n')
4294                                                                 str[strlen(str)-1] = 0; // remove trailing new line if one exists
4295                                                         snprintf(p, p_max_size, "%s", str);
4296                                                 }
4297                                         } else if(!strcmp(obj->data.rss.action, "item_desc")) {
4298                                                 if(obj->data.rss.act_par < data->item_count) {
4299                                                         str = data->items[obj->data.rss.act_par].description;
4300                                                         if(str[strlen(str)-1] == '\n')
4301                                                                 str[strlen(str)-1] = 0; // remove trailing new line if one exists
4302                                                         snprintf(p, p_max_size, "%s", str);
4303                                                 }
4304                                         } else if(!strcmp(obj->data.rss.action, "item_titles")) {
4305                                                 if(data->item_count > 0) {
4306                                                         int itmp;
4307                                                         p[0] = 0;
4308                                                         int show;
4309                                                         if(obj->data.rss.act_par > data->item_count)
4310                                                                 show = data->item_count;
4311                                                         else    show = obj->data.rss.act_par;
4312                                                         for(itmp = 0; itmp < show; itmp++) {
4313                                                                 PRSS_Item *item = &data->items[itmp];
4314                                                                 str = item->title;
4315                                                                 if(str) {
4316                                                                         if(itmp>0) // don't add new line before first item
4317                                                                                 strncat(p, "\n", p_max_size);
4318                                                                         if(str[strlen(str)-1] == '\n')
4319                                                                                 str[strlen(str)-1] = 0; // remove trailing new line if one exists, we have our own
4320                                                                         strncat(p, str, p_max_size);
4321                                                                 }
4322                                                         }
4323                                                 }
4324                                         }
4325                                 }
4326                         }
4327 #endif
4328 #ifdef HDDTEMP
4329                         OBJ(hddtemp) {
4330                                 char *temp;
4331                                 char unit;
4332                                 
4333                                 temp = get_hddtemp_info(obj->data.hddtemp.dev, 
4334                                                 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
4335                                 if (!temp) {
4336                                         snprintf(p, p_max_size, "N/A");
4337                                 } else if (unit == '*') {
4338                                         snprintf(p, p_max_size, "%s", temp);
4339                                 } else {
4340                                          snprintf(p, p_max_size, "%s°%c", temp, unit);
4341                                 }
4342                         }
4343 #endif
4344                         OBJ(offset) {
4345                                 new_offset(p, obj->data.i);
4346                         }
4347                         OBJ(voffset) {
4348                                 new_voffset(p, obj->data.i);
4349                         }
4350                         OBJ(i2c) {
4351                                 double r;
4352
4353                                 r = get_i2c_info(&obj->data.i2c.fd,
4354                                                  obj->data.i2c.arg,
4355                                                  obj->data.i2c.devtype,
4356                                                  obj->data.i2c.type);
4357
4358                                 if (r >= 100.0 || r == 0)
4359                                         snprintf(p, p_max_size, "%d", (int) r);
4360                                 else
4361                                         snprintf(p, p_max_size, "%.1f", r);
4362                         }
4363                         OBJ(alignr) {
4364                                 new_alignr(p, obj->data.i);
4365                         }
4366                         OBJ(alignc) {
4367                                 new_alignc(p, obj->data.i);
4368                         }
4369                         OBJ(if_empty) {
4370                                 struct information *my_info =
4371                                     malloc(sizeof(struct information));
4372                                 memcpy(my_info, cur, sizeof(struct information));
4373                                 parse_conky_vars(obj->data.ifblock.s, p, my_info);
4374                                 if (strlen(p) != 0) {
4375                                         i = obj->data.ifblock.pos;
4376                                         if_jumped = 1;
4377                                 } else {
4378                                         if_jumped = 0;
4379                                 }
4380                                 p[0] = '\0';
4381                                 free(my_info);
4382                         }
4383                         OBJ(if_existing) {
4384                                 struct stat tmp;
4385                                 if ((obj->data.ifblock.s)
4386                                     && (stat(obj->data.ifblock.s, &tmp) ==
4387                                         -1)) {
4388                                         i = obj->data.ifblock.pos;
4389                                         if_jumped = 1;
4390                                 } else {
4391                                         if (obj->data.ifblock.str) {
4392                                                 if (!check_contains(obj->data.ifblock.s, 
4393                                                         obj->data.ifblock.str)) {
4394                                                         i = obj->data.ifblock.pos;
4395                                                         if_jumped = 1;
4396                                                 } else 
4397                                                         if_jumped = 0;
4398                                         } else 
4399                                                 if_jumped = 0;
4400                                 }
4401                         }
4402                         OBJ(if_mounted) {
4403                                 if ((obj->data.ifblock.s)
4404                                     && (!check_mount(obj->data.ifblock.s))) {
4405                                         i = obj->data.ifblock.pos;
4406                                         if_jumped = 1;
4407                                 } else {
4408                                         if_jumped = 0;
4409                                 }
4410                         }
4411                         OBJ(if_running) {
4412                                 if ((obj->data.ifblock.s)
4413                                     && system(obj->data.ifblock.s)) {
4414                                         i = obj->data.ifblock.pos;
4415                                         if_jumped = 1;
4416                                 } else {
4417                                         if_jumped = 0;
4418                                 }
4419                         }
4420                         OBJ(kernel) {
4421                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4422                         }
4423                         OBJ(machine) {
4424                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4425                         }
4426
4427                         /* memory stuff */
4428                         OBJ(mem) {
4429                                 human_readable(cur->mem * 1024, p, 255);
4430                         }
4431                         OBJ(memmax) {
4432                                 human_readable(cur->memmax * 1024, p, 255);
4433                         }
4434                         OBJ(memperc) {
4435                                 if (cur->memmax) {
4436                                         if (!use_spacer)
4437                                                 snprintf(p, p_max_size, "%*Lu",
4438                                                          pad_percents,
4439                                                          (cur->mem * 100) /
4440                                                          (cur->memmax));
4441                                         else
4442                                                 snprintf(p, 4, "%*Lu   ",
4443                                                          pad_percents,
4444                                                          (cur->mem * 100) /
4445                                                          (cur->memmax));
4446                                 }
4447                         }
4448                         OBJ(membar) {
4449                                 new_bar(p, obj->data.pair.a,
4450                                         obj->data.pair.b,
4451                                         cur->memmax ? (cur->mem * 255) /
4452                                         (cur->memmax) : 0);
4453                         }
4454
4455                         OBJ(memgraph) {
4456                                 new_graph(p, obj->a,
4457                                 obj->b, obj->c, obj->d,
4458                                 cur->memmax ? (cur->mem * 100.0) /
4459                                                 (cur->memmax) : 0.0, 100, 1);
4460                         }
4461                         /* mixer stuff */
4462                         OBJ(mixer) {
4463                                 snprintf(p, p_max_size, "%d",
4464                                          mixer_get_avg(obj->data.l));
4465                         }
4466                         OBJ(mixerl) {
4467                                 snprintf(p, p_max_size, "%d",
4468                                          mixer_get_left(obj->data.l));
4469                         }
4470                         OBJ(mixerr) {
4471                                 snprintf(p, p_max_size, "%d",
4472                                          mixer_get_right(obj->data.l));
4473                         }
4474                         OBJ(mixerbar) {
4475                                 new_bar(p, obj->data.mixerbar.w,
4476                                         obj->data.mixerbar.h,
4477                                         mixer_get_avg(obj->data.mixerbar.
4478                                                       l) * 255 / 100);
4479                         }
4480                         OBJ(mixerlbar) {
4481                                 new_bar(p, obj->data.mixerbar.w,
4482                                         obj->data.mixerbar.h,
4483                                         mixer_get_left(obj->data.mixerbar.
4484                                                        l) * 255 / 100);
4485                         }
4486                         OBJ(mixerrbar) {
4487                                 new_bar(p, obj->data.mixerbar.w,
4488                                         obj->data.mixerbar.h,
4489                                         mixer_get_right(obj->data.mixerbar.
4490                                                         l) * 255 / 100);
4491                         }
4492
4493                         /* mail stuff */
4494                         OBJ(mails) {
4495                                 snprintf(p, p_max_size, "%d", cur->mail_count);
4496                         }
4497                         OBJ(mboxscan) {
4498                 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output, TEXT_BUFFER_SIZE);
4499                                 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
4500                         }
4501                         OBJ(new_mails) {
4502                                 snprintf(p, p_max_size, "%d", cur->new_mail_count);
4503                         }
4504
4505                         OBJ(nodename) {
4506                                 snprintf(p, p_max_size, "%s",
4507                                          cur->uname_s.nodename);
4508                         }
4509                         OBJ(outlinecolor) {
4510                                 new_outline(p, obj->data.l);
4511                         }
4512                         OBJ(processes) {
4513                                 if (!use_spacer)
4514                                         snprintf(p, p_max_size, "%hu", cur->procs);
4515                                 else
4516                                         snprintf(p, 5, "%hu    ",
4517                                                  cur->procs);
4518                         }
4519                         OBJ(running_processes) {
4520                                 if (!use_spacer)
4521                                         snprintf(p, p_max_size, "%hu",
4522                                                  cur->run_procs);
4523                                 else
4524                                         snprintf(p, 3, "%hu     ",
4525                                                  cur->run_procs);
4526                         }
4527                         OBJ(text) {
4528                                 snprintf(p, p_max_size, "%s", obj->data.s);
4529                         }
4530                         OBJ(shadecolor) {
4531                                 new_bg(p, obj->data.l);
4532                         }
4533                         OBJ(stippled_hr) {
4534                                 new_stippled_hr(p, obj->data.pair.a,
4535                                                 obj->data.pair.b);
4536                         }
4537                         OBJ(swap) {
4538                                 human_readable(cur->swap * 1024, p, 255);
4539                         }
4540                         OBJ(swapmax) {
4541                                 human_readable(cur->swapmax * 1024, p,
4542                                                255);
4543                         }
4544                         OBJ(swapperc) {
4545                                 if (cur->swapmax == 0) {
4546                                         strncpy(p, "No swap", 255);
4547                                 } else {
4548                                         if (!use_spacer)
4549                                                 snprintf(p, 255, "%*Lu",
4550                                                          pad_percents,
4551                                                          (cur->swap *
4552                                                           100) /
4553                                                          cur->swapmax);
4554                                         else
4555                                                 snprintf(p, 4, "%*Lu   ",
4556                                                          pad_percents,
4557                                                          (cur->swap *
4558                                                           100) /
4559                                                          cur->swapmax);
4560                                 }
4561                         }
4562                         OBJ(swapbar) {
4563                                 new_bar(p, obj->data.pair.a,
4564                                         obj->data.pair.b,
4565                                         cur->swapmax ? (cur->swap * 255) /
4566                                         (cur->swapmax) : 0);
4567                         }
4568                         OBJ(sysname) {
4569                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4570                         }
4571                         OBJ(time) {
4572                                 time_t t = time(NULL);
4573                                 struct tm *tm = localtime(&t);
4574                                 setlocale(LC_TIME, "");
4575                                 strftime(p, p_max_size, obj->data.s, tm);
4576                         }
4577                         OBJ(utime) {
4578                                 time_t t = time(NULL);
4579                                 struct tm *tm = gmtime(&t);
4580                                 strftime(p, p_max_size, obj->data.s, tm);
4581                         }
4582                         OBJ(tztime) {
4583                                 char* oldTZ = NULL;
4584                                 if (obj->data.tztime.tz) {
4585                                         oldTZ = getenv("TZ");
4586                                         setenv("TZ", obj->data.tztime.tz, 1);
4587                                         tzset();
4588                                 }
4589                                 time_t t = time(NULL);
4590                                 struct tm *tm = localtime(&t);
4591                                 setlocale(LC_TIME, "");
4592                                 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4593                                 if (oldTZ) {
4594                                         setenv("TZ", oldTZ, 1);
4595                                         tzset();
4596                                 } else {
4597                                         unsetenv("TZ");
4598                                 }
4599                                 // Needless to free oldTZ since getenv gives ptr to static data 
4600                         }
4601                         OBJ(totaldown) {
4602                                 human_readable(obj->data.net->recv, p,
4603                                                255);
4604                         }
4605                         OBJ(totalup) {
4606                                 human_readable(obj->data.net->trans, p,
4607                                                255);
4608                         }
4609                         OBJ(updates) {
4610                                 snprintf(p, p_max_size, "%d", total_updates);
4611                         }
4612                         OBJ(upspeed) {
4613                                 if (!use_spacer)
4614                                         snprintf(p, p_max_size, "%d",
4615                                                  (int) (obj->data.net->
4616                                                         trans_speed /
4617                                                         1024));
4618                                 else
4619                                         snprintf(p, 6, "%d     ",
4620                                                  (int) (obj->data.net->
4621                                                         trans_speed /
4622                                                         1024));
4623                         }
4624                         OBJ(upspeedf) {
4625                                 if (!use_spacer)
4626                                         snprintf(p, p_max_size, "%.1f",
4627                                                  obj->data.net->
4628                                                  trans_speed / 1024.0);
4629                                 else
4630                                         snprintf(p, 8, "%.1f       ",
4631                                                  obj->data.net->
4632                                                  trans_speed / 1024.0);
4633                         }
4634                         OBJ(upspeedgraph) {
4635         /*
4636                                 if (obj->data.net->trans_speed == 0)
4637                                         obj->data.net->trans_speed = 0.01;
4638           */
4639                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4640                                           (obj->data.net->trans_speed /
4641                                 1024.0), obj->e, 1);
4642                         }
4643                         OBJ(uptime_short) {
4644                                 format_seconds_short(p, p_max_size,
4645                                                      (int) cur->uptime);
4646                         }
4647                         OBJ(uptime) {
4648                                 format_seconds(p, p_max_size, (int) cur->uptime);
4649                         }
4650
4651 #if (defined(__FreeBSD__) || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
4652                         OBJ(apm_adapter) {
4653                                 char    *msg;
4654                                 msg = get_apm_adapter();
4655                                 snprintf(p, p_max_size, "%s", msg);
4656                                 free(msg);
4657                         }
4658                         OBJ(apm_battery_life) {
4659                                 char    *msg;
4660                                 msg = get_apm_battery_life();
4661                                 snprintf(p, p_max_size, "%s", msg);
4662                                 free(msg);
4663                         }
4664                         OBJ(apm_battery_time) {
4665                                 char    *msg;
4666                                 msg = get_apm_battery_time();
4667                                 snprintf(p, p_max_size, "%s", msg);
4668                                 free(msg);
4669                         }
4670 #endif /* __FreeBSD__ __OpenBSD__ */
4671
4672 #ifdef MPD
4673                         OBJ(mpd_title) {
4674                             snprintf(p, cur->mpd.max_title_len > 0 ?
4675                                         cur->mpd.max_title_len : p_max_size, "%s", 
4676                                      cur->mpd.title);
4677                         }
4678                         OBJ(mpd_artist) {
4679                                 snprintf(p, p_max_size, "%s", cur->mpd.artist);
4680                         }
4681                         OBJ(mpd_album) {
4682                                 snprintf(p, p_max_size, "%s", cur->mpd.album);
4683                         }
4684                         OBJ(mpd_random) {
4685                                 snprintf(p, p_max_size, "%s", cur->mpd.random);
4686                         }
4687                         OBJ(mpd_repeat) {
4688                                 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4689                         }
4690                         OBJ(mpd_track) {
4691                                 snprintf(p, p_max_size, "%s", cur->mpd.track);
4692                         }
4693                         OBJ(mpd_name) {
4694                                 snprintf(p, p_max_size, "%s", cur->mpd.name);
4695                         }
4696                         OBJ(mpd_file) {
4697                                 snprintf(p, p_max_size, "%s", cur->mpd.file);
4698                         }
4699                         OBJ(mpd_vol) {
4700                                 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4701                         }
4702                         OBJ(mpd_bitrate) {
4703                                 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4704                         }
4705                         OBJ(mpd_status) {
4706                                 snprintf(p, p_max_size, "%s", cur->mpd.status);
4707                         }
4708                         OBJ(mpd_elapsed) {
4709                                 int days = 0, hours = 0, minutes =
4710                                     0, seconds = 0;
4711                                 int tmp = cur->mpd.elapsed;
4712                                 while (tmp >= 86400) {
4713                                         tmp -= 86400;
4714                                         days++;
4715                                 }
4716                                 while (tmp >= 3600) {
4717                                         tmp -= 3600;
4718                                         hours++;
4719                                 }
4720                                 while (tmp >= 60) {
4721                                         tmp -= 60;
4722                                         minutes++;
4723                                 }
4724                                 seconds = tmp;
4725                                 if (days > 0)
4726                                         snprintf(p, p_max_size, "%i days %i:%02i:%02i",
4727                                                  days, hours, minutes,
4728                                                  seconds);
4729                                 else if (hours > 0)
4730                                         snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4731                                                  minutes, seconds);
4732                                 else
4733                                         snprintf(p, p_max_size, "%i:%02i", minutes,
4734                                                  seconds);
4735                         }
4736                         OBJ(mpd_length) {
4737                                 int days = 0, hours = 0, minutes =
4738                                     0, seconds = 0;
4739                                 int tmp = cur->mpd.length;
4740                                 while (tmp >= 86400) {
4741                                         tmp -= 86400;
4742                                         days++;
4743                                 }
4744                                 while (tmp >= 3600) {
4745                                         tmp -= 3600;
4746                                         hours++;
4747                                 }
4748                                 while (tmp >= 60) {
4749                                         tmp -= 60;
4750                                         minutes++;
4751                                 }
4752                                 seconds = tmp;
4753                                 if (days > 0)
4754                                         snprintf(p, p_max_size,
4755                                                  "%i days %i:%02i:%02i",
4756                                                  days, hours, minutes,
4757                                                  seconds);
4758                                 else if (hours > 0)
4759                                         snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4760                                                  minutes, seconds);
4761                                 else
4762                                         snprintf(p, p_max_size, "%i:%02i", minutes,
4763                                                  seconds);
4764                         }
4765                         OBJ(mpd_percent) {
4766                                 snprintf(p, p_max_size, "%2.0f",
4767                                          cur->mpd.progress * 100);
4768                         }
4769                         OBJ(mpd_bar) {
4770                                 new_bar(p, obj->data.pair.a,
4771                                         obj->data.pair.b,
4772                                         (int) (cur->mpd.progress *
4773                                                255.0f));
4774                         }
4775                         OBJ(mpd_smart) {
4776                                 if (strlen(cur->mpd.title) < 2 && strlen(cur->mpd.title) < 2) {
4777                                         snprintf(p, p_max_size, "%s", cur->mpd.file);
4778                                 } else {
4779                                         snprintf(p, p_max_size, "%s - %s", cur->mpd.artist, cur->mpd.title);
4780                                 }
4781                         }
4782 #endif
4783 #ifdef XMMS2
4784                         OBJ(xmms2_artist) {
4785                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4786                         }
4787                         OBJ(xmms2_album) {
4788                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4789                         }
4790                         OBJ(xmms2_title) {
4791                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4792                         }
4793                         OBJ(xmms2_genre) {
4794                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4795                         }
4796                         OBJ(xmms2_comment) {
4797                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4798                         }
4799                         OBJ(xmms2_decoder) {
4800                                 snprintf(p, p_max_size, "%s", cur->xmms2.decoder);
4801                         }
4802                         OBJ(xmms2_transport) {
4803                                 snprintf(p, p_max_size, "%s", cur->xmms2.transport);
4804                         }
4805                         OBJ(xmms2_url) {
4806                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4807                         }
4808                         OBJ(xmms2_status) {
4809                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4810                         }
4811             OBJ(xmms2_date) {
4812                     snprintf(p, p_max_size, "%s", cur->xmms2.date);
4813                         }
4814                         OBJ(xmms2_tracknr) {
4815                             if (cur->xmms2.tracknr != -1)
4816                     snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4817                         }
4818                         OBJ(xmms2_bitrate) {
4819                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4820                         }
4821             OBJ(xmms2_id) {
4822                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4823                         }
4824             OBJ(xmms2_size) {
4825                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4826                         }
4827                         OBJ(xmms2_elapsed) {
4828                                 int tmp = cur->xmms2.elapsed;
4829                                 snprintf(p, p_max_size, "%02d:%02d",
4830                                     tmp / 60000, (tmp / 1000) % 60);
4831                         }
4832                         OBJ(xmms2_duration) {
4833                                 int tmp = cur->xmms2.duration;
4834                                 snprintf(p, p_max_size, "%02d:%02d",
4835                                     tmp / 60000, (tmp / 1000) % 60);
4836                         }
4837                         OBJ(xmms2_percent) {
4838                                 snprintf(p, p_max_size, "%2.0f",
4839                                          cur->xmms2.progress * 100);
4840                         }
4841                         OBJ(xmms2_bar) {
4842                                 new_bar(p, obj->data.pair.a,
4843                                         obj->data.pair.b,
4844                                         (int) (cur->xmms2.progress *
4845                                                255.0f));
4846                         }
4847                         OBJ(xmms2_smart) {
4848                                 if (strlen(cur->xmms2.title) < 2 && strlen(cur->xmms2.title) < 2) {
4849                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
4850                                 } else {
4851                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist, cur->xmms2.title);
4852                                 }
4853                         }
4854 #endif
4855 #ifdef AUDACIOUS
4856                         OBJ(audacious_status) {
4857                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_STATUS]);
4858                         }
4859                         OBJ(audacious_title) {
4860                             snprintf(p, cur->audacious.max_title_len > 0 ?
4861                                         cur->audacious.max_title_len : p_max_size, "%s", 
4862                                      cur->audacious.items[AUDACIOUS_TITLE]);
4863                         }
4864                         OBJ(audacious_length) {
4865                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH]);
4866                         }
4867                         OBJ(audacious_length_seconds) {
4868                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4869                         }
4870                         OBJ(audacious_position) {
4871                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION]);
4872                         }
4873                         OBJ(audacious_position_seconds) {
4874                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
4875                         }
4876                         OBJ(audacious_bitrate) {
4877                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_BITRATE]);
4878                         }
4879                         OBJ(audacious_frequency) {
4880                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FREQUENCY]);
4881                         }
4882                         OBJ(audacious_channels) {
4883                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_CHANNELS]);
4884                         }
4885                         OBJ(audacious_filename) {
4886                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FILENAME]);
4887                         }
4888                         OBJ(audacious_playlist_length) {
4889                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
4890                         }
4891                         OBJ(audacious_playlist_position) {
4892                             snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
4893                         }
4894                         OBJ(audacious_bar) {
4895                             double progress;
4896                             progress= atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
4897                                       atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4898                             new_bar(p,obj->a,obj->b,(int)(progress*255.0f));
4899                         }
4900 #endif
4901 #ifdef BMPX
4902                         OBJ(bmpx_title) {
4903                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4904                         }
4905                         OBJ(bmpx_artist) {
4906                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4907                         }
4908                         OBJ(bmpx_album) {
4909                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4910                         }
4911                         OBJ(bmpx_uri) {
4912                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4913                         }
4914                         OBJ(bmpx_track) {
4915                                  snprintf(p, p_max_size, "%i", cur->bmpx.track);
4916                         }
4917                         OBJ(bmpx_bitrate) {
4918                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4919                         }
4920 #endif
4921                         OBJ(top) {
4922                                 if (obj->data.top.type == TOP_NAME
4923                                     && obj->data.top.num >= 0
4924                                     && obj->data.top.num < 10) {
4925                                         snprintf(p, 17, "%-17s", cur->cpu[obj->data.top.num]->name);
4926                                 } else if (obj->data.top.type == TOP_CPU
4927                                            && obj->data.top.num >= 0
4928                                            && obj->data.top.num < 10) {
4929                                         snprintf(p, 7, "%7.3f",
4930                                                  cur->cpu[obj->data.top.
4931                                                           num]->amount);
4932                                 } else if (obj->data.top.type == TOP_PID
4933                                            && obj->data.top.num >= 0
4934                                            && obj->data.top.num < 10) {
4935                                         snprintf(p, 8, "%7i",
4936                                                  cur->cpu[obj->data.top.
4937                                                           num]->pid);
4938                                 } else if (obj->data.top.type == TOP_MEM
4939                                            && obj->data.top.num >= 0
4940                                            && obj->data.top.num < 10) {
4941                                         snprintf(p, 7, "%7.3f",
4942                                                  cur->cpu[obj->data.top.
4943                                                           num]->totalmem);
4944                                 }
4945                         }
4946                         OBJ(top_mem) {
4947                                 if (obj->data.top.type == TOP_NAME
4948                                     && obj->data.top.num >= 0
4949                                     && obj->data.top.num < 10) {
4950                                         snprintf(p, 17, "%-17s",
4951                                                  cur->memu[obj->data.top.
4952                                                            num]->name);
4953                                 } else if (obj->data.top.type == TOP_CPU
4954                                            && obj->data.top.num >= 0
4955                                            && obj->data.top.num < 10) {
4956                                         snprintf(p, 7, "%7.3f",
4957                                                  cur->memu[obj->data.top.
4958                                                            num]->amount);
4959                                 } else if (obj->data.top.type == TOP_PID
4960                                            && obj->data.top.num >= 0
4961                                            && obj->data.top.num < 10) {
4962                                         snprintf(p, 8, "%7i",
4963                                                  cur->memu[obj->data.top.
4964                                                            num]->pid);
4965                                 } else if (obj->data.top.type == TOP_MEM
4966                                            && obj->data.top.num >= 0
4967                                            && obj->data.top.num < 10) {
4968                                         snprintf(p, 7, "%7.3f",
4969                                                  cur->memu[obj->data.top.
4970                                                            num]->totalmem);
4971                                 }
4972                         }
4973
4974
4975                         OBJ(tail) {
4976                                 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4977                                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4978                                 } else {
4979                                         obj->data.tail.last_update = current_update_time;
4980                                         FILE *fp;
4981                                         long nl=0, bsize;
4982                                         int iter;
4983                                         fp = fopen(obj->data.tail.logfile, "rt");
4984                                         if (fp == NULL) {
4985                                                 /* Send one message, but do not consistently spam on
4986                                                  * missing logfiles. */
4987                                                 if(obj->data.tail.readlines != 0) {
4988                                                         ERR("tail logfile failed to open");
4989                                                         strcpy(obj->data.tail.buffer, "Logfile Missing");
4990                                                 }
4991                                                 obj->data.tail.readlines = 0;
4992                                                 snprintf(p, p_max_size, "Logfile Missing");
4993                                         }
4994                                         else {
4995                                                 obj->data.tail.readlines = 0;
4996                                                 /* -1 instead of 0 to avoid counting a trailing newline */
4997                                                 fseek(fp, -1, SEEK_END); 
4998                                                 bsize = ftell(fp) + 1;
4999                                                 for(iter = obj->data.tail.wantedlines; iter > 0; iter--) {
5000                                                         nl = rev_fcharfind(fp, '\n', iter);
5001                                                         if(nl >= 0)
5002                                                                 break;
5003                                                 }
5004                                                 obj->data.tail.readlines = iter;
5005                                                 if(obj->data.tail.readlines < obj->data.tail.wantedlines) {
5006                                                         fseek(fp, 0, SEEK_SET);
5007                                                 }
5008                                                 else {
5009                                                         fseek(fp, nl+1, SEEK_SET);
5010                                                         bsize -= ftell(fp);
5011                                                 }
5012                                                 /* Make sure bsize is at least 1 byte smaller than
5013                                                  * the buffer max size. */
5014                                                 if(bsize > (long)((text_buffer_size*20) - 1)) {
5015                                                         fseek(fp, bsize - text_buffer_size*20 - 1, SEEK_CUR);
5016                                                         bsize = text_buffer_size*20 - 1;
5017                                                 }
5018                                                 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
5019                                                 fclose(fp);
5020                                                 if(bsize > 0) {
5021                                                         /* Clean up trailing newline, make sure the buffer
5022                                                          * is null terminated. */
5023                                                         if(obj->data.tail.buffer[bsize-1] == '\n')
5024                                                                 obj->data.tail.buffer[bsize-1] = '\0';
5025                                                         else
5026                                                                 obj->data.tail.buffer[bsize] = '\0';
5027                                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5028                                                 }
5029                                                 else {
5030                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
5031                                                         snprintf(p, p_max_size, "Logfile Empty");
5032                                                 } /* bsize > 0 */
5033                                         } /*  fp == NULL  */
5034                                 } /* if cur_upd_time >= */
5035
5036                                 //parse_conky_vars(obj->data.tail.buffer, p, cur);
5037
5038                         }
5039                         OBJ(head) {
5040                                 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
5041                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5042                                 } else {
5043                                         obj->data.tail.last_update = current_update_time;
5044                                         FILE *fp;
5045                                         long nl=0;
5046                                         int iter;
5047                                         fp = fopen(obj->data.tail.logfile, "rt");
5048                                         if (fp == NULL) {
5049                                                 /* Send one message, but do not consistently spam on
5050                                                  * missing logfiles. */
5051                                                 if(obj->data.tail.readlines != 0) {
5052                                                         ERR("head logfile failed to open");
5053                                                         strcpy(obj->data.tail.buffer, "Logfile Missing");
5054                                                 }
5055                                                 obj->data.tail.readlines = 0;
5056                                                 snprintf(p, p_max_size, "Logfile Missing");
5057                                         } else {
5058                                                 obj->data.tail.readlines = 0;
5059                                                 for(iter = obj->data.tail.wantedlines; iter > 0; iter--) {
5060                                                         nl = fwd_fcharfind(fp, '\n', iter);
5061                                                         if(nl >= 0)
5062                                                                 break;
5063                                                 }
5064                                                 obj->data.tail.readlines = iter;
5065                                                 /* Make sure nl is at least 1 byte smaller than
5066                                                  * the buffer max size. */
5067                                                 if(nl > (long)((text_buffer_size*20) - 1)) {
5068                                                         nl = text_buffer_size*20 - 1;
5069                                                 }
5070                                                 nl = fread(obj->data.tail.buffer, 1, nl, fp);
5071                                                 fclose(fp);
5072                                                 if(nl > 0) {
5073                                                         /* Clean up trailing newline, make sure the buffer
5074                                                          * is null terminated. */
5075                                                         if (obj->data.tail.buffer[nl-1] == '\n') {
5076                                                                 obj->data.tail.buffer[nl-1] = '\0';
5077                                                         }
5078                                                         else {
5079                                                                 obj->data.tail.buffer[nl] = '\0';
5080                                                         }
5081                                                         snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
5082                                                 } 
5083                                                 else {
5084                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
5085                                                         snprintf(p, p_max_size, "Logfile Empty");
5086                                                 } /* nl > 0 */
5087                                         } /* if fp == null */
5088                                 } /* cur_upd_time >= */
5089
5090                                 //parse_conky_vars(obj->data.tail.buffer, p, cur);
5091
5092                         }
5093 #ifdef TCP_PORT_MONITOR
5094                         OBJ(tcp_portmon)
5095                         {
5096                                 /* grab a pointer to this port monitor */
5097                                 tcp_port_monitor_t * p_monitor = 
5098                                         find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
5099                                                                 obj->data.tcp_port_monitor.port_range_begin,
5100                                                                 obj->data.tcp_port_monitor.port_range_end );
5101                                 if ( !p_monitor ) {
5102                                         snprintf(p, p_max_size, "monitor not found");
5103                                         break;
5104                                 }
5105
5106                                 /* now grab the text of interest */
5107                                 if ( peek_tcp_port_monitor( p_monitor, 
5108                                                             obj->data.tcp_port_monitor.item, 
5109                                                             obj->data.tcp_port_monitor.connection_index,
5110                                                             p, p_max_size ) != 0 )
5111                                 {
5112                                         snprintf(p, p_max_size, "monitor peek error");
5113                                         break;
5114                                 }
5115                         }
5116 #endif
5117
5118 #ifdef HAVE_ICONV
5119                         OBJ(iconv_start)
5120                         {
5121                                 iconv_converting = 1;
5122                                 iconv_selected = obj->a;
5123                                 
5124                         }
5125                         OBJ(iconv_stop)
5126                         {
5127                                 iconv_converting = 0;
5128                                 iconv_selected = 0;
5129                         }
5130 #endif
5131                         OBJ(entropy_avail) 
5132                         {
5133                                 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5134                         }
5135                         OBJ(entropy_poolsize)
5136                         {
5137                                 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5138                         }
5139                         OBJ(entropy_bar)
5140                         {
5141                                 double entropy_perc;
5142                                 entropy_perc = (double)cur->entropy.entropy_avail / 
5143                                                (double)cur->entropy.poolsize;
5144                                 new_bar(p,obj->a,obj->b,(int)(entropy_perc * 255.0f));
5145                         }
5146
5147
5148                         break;
5149                 }
5150
5151                 {
5152                         unsigned int a = strlen(p);
5153
5154 #ifdef HAVE_ICONV
5155                         if (a > 0 && iconv_converting && iconv_selected > 0 && (iconv_cd[iconv_selected - 1] != (iconv_t)(-1))) {
5156                                 int bytes;
5157                                 size_t dummy1, dummy2;
5158                                 char *ptr = buff_in;
5159                                 char *outptr = p;
5160
5161                                 dummy1 = dummy2 = a;
5162                                 
5163                                 strncpy(buff_in, p, P_MAX_SIZE);
5164
5165                                 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5166                                 while (dummy1 > 0) {
5167                                         bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1, &outptr, &dummy2);
5168                                         if (bytes == -1) {
5169                                                 ERR("Iconv codeset conversion failed");
5170                                                 break;
5171                                         }
5172                                 }
5173
5174                                 /* It is nessecary when we are converting from multibyte to singlebyte codepage */
5175                                 a = outptr - p;
5176                         }
5177 #endif
5178                         p += a;
5179                         p_max_size -= a;
5180                 }
5181         }
5182 }
5183
5184
5185 double current_update_time, last_update_time;
5186
5187 static void generate_text()
5188 {
5189         struct information *cur = &info;
5190         char *p;
5191
5192
5193         special_count = 0;
5194
5195         /* update info */
5196
5197         current_update_time = get_time();
5198
5199         update_stuff(cur);
5200         /* fix diskio rates to b/s (use update_interval */
5201         diskio_read_value = diskio_read_value / update_interval;
5202         diskio_write_value = diskio_write_value / update_interval;
5203         diskio_value = diskio_value / update_interval;
5204         
5205
5206         /* add things to the buffer */
5207
5208         /* generate text */
5209
5210         p = text_buffer;
5211
5212         generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
5213
5214         if (stuff_in_upper_case) {
5215                 char *p;
5216
5217                 p = text_buffer;
5218                 while (*p) {
5219                         *p = toupper(*p);
5220                         p++;
5221                 }
5222         }
5223
5224
5225         last_update_time = current_update_time;
5226         total_updates++;
5227         //free(p);
5228 }
5229
5230
5231 #ifdef X11
5232 static void set_font()
5233 {
5234 #ifdef XFT
5235         if (use_xft) {
5236                         if (window.xftdraw != NULL) {
5237                                 XftDrawDestroy(window.xftdraw);
5238                         }
5239                         window.xftdraw = XftDrawCreate(display, window.drawable,
5240                                         DefaultVisual(display,
5241                                                         screen),
5242                                         DefaultColormap(display,
5243                                                         screen));
5244                 } else
5245 #endif
5246 {
5247         XSetFont(display, window.gc, fonts[selected_font].font->fid);
5248 }
5249 }
5250
5251 #endif /* X11 */
5252
5253 static inline int get_string_width(const char *s)
5254 {
5255 #ifdef X11
5256         return *s ? calc_text_width(s, strlen(s)) : 0;
5257 #else
5258         return strlen(s);
5259 #endif /* X11 */
5260 }
5261
5262 static inline int get_string_width_special(char *s)
5263 {
5264         if (!s) {
5265                 return 0;
5266         }
5267 #ifdef X11
5268         char *p, *final;
5269         p = strdup(s);
5270         final = p;
5271         int index = 1;
5272         int width = 0;
5273         unsigned int i;
5274         while (*p) {
5275                 if (*p == SPECIAL_CHAR) {
5276                         /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
5277                         for (i = 0; i < strlen(p); i++) {
5278                                 *(p + i) = *(p + i + 1);
5279                         }
5280                         if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
5281                                 width += specials[special_index+index].width;
5282                         }
5283                         index++;
5284                 } else {
5285                         p++;
5286                 }
5287         }
5288         if (strlen(final) > 1) {
5289                 width += calc_text_width(final, strlen(final));
5290         }
5291         free(final);
5292         return width;
5293 #else
5294         return strlen(s);
5295 #endif /* X11 */
5296 }
5297
5298 #ifdef X11
5299 static void text_size_updater(char *s);
5300
5301 int last_font_height;
5302 static void update_text_area()
5303 {
5304         int x, y;
5305
5306         /* update text size if it isn't fixed */
5307 #ifdef OWN_WINDOW
5308         if (!fixed_size)
5309 #endif
5310         {
5311                 text_width = minimum_width;
5312                 text_height = 0;
5313                 special_index = 0;
5314                 last_font_height = font_height();
5315                 for_each_line(text_buffer, text_size_updater);
5316                 text_width += 1;
5317                 if (text_height < minimum_height)
5318                         text_height = minimum_height;
5319                 if (text_width > maximum_width && maximum_width > 0)
5320                         text_width = maximum_width;
5321         }
5322
5323         /* get text position on workarea */
5324         switch (text_alignment) {
5325         case TOP_LEFT:
5326                 x = gap_x;
5327                 y = gap_y;
5328                 break;
5329
5330         case TOP_RIGHT:
5331                 x = workarea[2] - text_width - gap_x;
5332                 y = gap_y;
5333                 break;
5334
5335         default:
5336         case BOTTOM_LEFT:
5337                 x = gap_x;
5338                 y = workarea[3] - text_height - gap_y;
5339                 break;
5340
5341         case BOTTOM_RIGHT:
5342                 x = workarea[2] - text_width - gap_x;
5343                 y = workarea[3] - text_height - gap_y;
5344                 break;
5345         
5346 #ifdef OWN_WINDOW
5347         case NONE: // Let the WM manage the window
5348                 x = window.x;
5349                 y = window.y;
5350
5351                 fixed_pos  = 1;
5352                 fixed_size = 1;
5353                 break;
5354 #endif
5355         }
5356 #ifdef OWN_WINDOW
5357
5358         if (own_window && !fixed_pos) {
5359                 x += workarea[0];
5360                 y += workarea[1];
5361                 text_start_x = border_margin + 1;
5362                 text_start_y = border_margin + 1;
5363                 window.x = x - border_margin - 1;
5364                 window.y = y - border_margin - 1;
5365         } else
5366 #endif
5367         {
5368                 /* If window size doesn't match to workarea's size, then window
5369                  * probably includes panels (gnome).
5370                  * Blah, doesn't work on KDE. */
5371                 if (workarea[2] != window.width
5372                     || workarea[3] != window.height) {
5373                         y += workarea[1];
5374                         x += workarea[0];
5375                 }
5376
5377                 text_start_x = x;
5378                 text_start_y = y;
5379         }
5380 }
5381
5382 /*
5383  * drawing stuff
5384  */
5385
5386 static int cur_x, cur_y;        /* current x and y for drawing */
5387 static int draw_mode;           /* FG, BG or OUTLINE */
5388 static long current_color;
5389
5390 #ifdef X11
5391 static void text_size_updater(char *s)
5392 {
5393         int w = 0;
5394         char *p;
5395         /* get string widths and skip specials */
5396         p = s;
5397         while (*p) {
5398                 if (*p == SPECIAL_CHAR) {
5399                         *p = '\0';
5400                         w += get_string_width(s);
5401                         *p = SPECIAL_CHAR;
5402
5403                         if (specials[special_index].type == BAR
5404                             || specials[special_index].type == GRAPH) {
5405                                 w += specials[special_index].width;
5406                                 if (specials[special_index].height > last_font_height) {
5407                                         last_font_height = specials[special_index].height;
5408                                         last_font_height += font_ascent();
5409                                 }
5410                         } else if (specials[special_index].type == OFFSET) {
5411                                 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
5412                         } else if (specials[special_index].type == VOFFSET) {
5413                                 last_font_height += specials[special_index].arg;
5414                         } else if (specials[special_index].type == GOTO) {
5415                                 if (specials[special_index].arg >= 0)
5416                                         w += (int)specials[special_index].arg - cur_x;
5417                         } else if (specials[special_index].type == TAB) { 
5418                                 int start = specials[special_index].arg;
5419                                 int step = specials[special_index].width;
5420                                 if (!step || step < 0)
5421                                         step = 10;
5422                                 w += step - (cur_x - text_start_x - start) % step;
5423                         } else if (specials[special_index].type == FONT) {
5424                                 selected_font = specials[special_index].font_added;
5425                                 if (font_height() > last_font_height) {
5426                                         last_font_height = font_height();
5427                                 }
5428                         }
5429                         
5430                         special_index++;
5431                         s = p + 1;
5432                 }
5433                 p++;
5434         }
5435         w += get_string_width(s);
5436         if (w > text_width) {
5437                 text_width = w;
5438         }
5439         if (text_width > maximum_width && maximum_width) {
5440                 text_width = maximum_width;
5441         }
5442
5443         text_height += last_font_height;
5444         last_font_height = font_height();
5445 }
5446 #endif /* X11 */
5447
5448 static inline void set_foreground_color(long c)
5449 {
5450         current_color = c;
5451         XSetForeground(display, window.gc, c);
5452 }
5453 #endif /* X11 */
5454
5455 static void draw_string(const char *s)
5456 {
5457         if (s[0] == '\0')
5458                 return;
5459         int i, i2, pos, width_of_s;
5460         int max=0;
5461         int added;
5462         width_of_s = get_string_width(s);
5463         if (out_to_console) {
5464                 printf("%s\n", s);
5465                 fflush(stdout);   /* output immediately, don't buffer */
5466         }
5467         memset(tmpstring1,0,TEXT_BUFFER_SIZE);
5468         memset(tmpstring2,0,TEXT_BUFFER_SIZE);
5469         strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
5470         pos = 0;
5471         added = 0;
5472         char space[2];
5473         snprintf(space, 2, " ");
5474 #ifdef X11
5475         max = ((text_width - width_of_s) / get_string_width(space));
5476 #endif /* X11 */
5477         /*
5478          * This code looks for tabs in the text and coverts them to spaces.
5479          * The trick is getting the correct number of spaces,
5480          * and not going over the window's size without forcing
5481          * the window larger.
5482          */
5483         for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
5484                 if (tmpstring1[i] == '\t')      // 9 is ascii tab
5485                 {
5486                         i2 = 0;
5487                         for (i2 = 0;
5488                              i2 < (8 - (1 + pos) % 8) && added <= max;
5489                              i2++) {
5490                                 /*
5491                                 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
5492                                         fprintf(stderr,"buffer overrun detected\n");
5493                                 */
5494                                 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
5495                                 added++;
5496                         }
5497                         pos += i2;
5498                 } else {
5499                         if (tmpstring1[i] != 9) {
5500                                 /*
5501                                 if ( pos > TEXT_BUFFER_SIZE-1 )
5502                                          fprintf(stderr,"buffer overrun detected\n");
5503                                 */
5504                                 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
5505                                 pos++;
5506                         }
5507                 }
5508         }
5509 #ifdef X11
5510         if (text_width == maximum_width) {
5511                 /* this means the text is probably pushing the limit, so we'll chop it */
5512                 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
5513                         tmpstring2[strlen(tmpstring2)-1] = '\0';
5514                 }
5515         }
5516 #endif /* X11 */
5517         s = tmpstring2;
5518 #ifdef X11
5519 #ifdef XFT
5520         if (use_xft) {
5521                 XColor c;
5522                 XftColor c2;
5523                 c.pixel = current_color;
5524                 XQueryColor(display, DefaultColormap(display, screen), &c);
5525
5526                 c2.pixel = c.pixel;
5527                 c2.color.red = c.red;
5528                 c2.color.green = c.green;
5529                 c2.color.blue = c.blue;
5530                 c2.color.alpha = fonts[selected_font].font_alpha;
5531                 if (utf8_mode) {
5532                         XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5533                                           cur_x, cur_y, (XftChar8 *) s,
5534                                           strlen(s));
5535                 } else {
5536                         XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5537                                        cur_x, cur_y, (XftChar8 *) s,
5538                                        strlen(s));
5539                 }
5540         } else
5541 #endif
5542         {
5543                 XDrawString(display, window.drawable, window.gc,
5544                             cur_x, cur_y, s, strlen(s));
5545         }
5546         cur_x += width_of_s;
5547 #endif /* X11 */
5548         memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
5549 }
5550
5551 long redmask, greenmask, bluemask;
5552
5553 void set_up_gradient()
5554 {
5555 #ifdef X11
5556         colour_depth = DisplayPlanes(display, screen);
5557 #else
5558         colour_depth = 16;
5559 #endif /* X11 */
5560         if (colour_depth != 24 && colour_depth != 16) {
5561                 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
5562         }
5563         int i;
5564         redmask = 0;
5565         greenmask = 0;
5566         bluemask = 0;
5567         for(i = (colour_depth / 3)-1; i>=0; i--) {
5568                 redmask |= 1 << i;
5569                 greenmask |= 1 << i;
5570                 bluemask |= 1 << i;
5571         }
5572         if (colour_depth%3 == 1) {
5573                 greenmask |= 1 << (colour_depth / 3);
5574         }
5575         redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
5576         greenmask = greenmask << (colour_depth / 3);
5577 }
5578
5579 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 */
5580         int tmp_color = 0;
5581         int red1, green1, blue1; // first colour
5582         int red2, green2, blue2; // second colour
5583         int red3 = 0, green3 = 0, blue3 = 0; // difference
5584         short redshift = (2*colour_depth / 3 + colour_depth%3);
5585         short greenshift = (colour_depth / 3);
5586         red1 = (first_colour & redmask) >> redshift;
5587         green1 = (first_colour & greenmask) >> greenshift;
5588         blue1 = first_colour & bluemask;
5589         red2 = (last_colour & redmask) >> redshift;
5590         green2 = (last_colour & greenmask) >> greenshift;
5591         blue2 = last_colour & bluemask;
5592         if (red1 > red2) {
5593                 red3 = -1;
5594         }
5595         if (red1 < red2) {
5596                 red3 = 1;
5597         }
5598         if (green1 > green2) {
5599                 green3 = -1;
5600         }
5601         if (green1 < green2) {
5602                 green3 = 1;
5603         }
5604         if (blue1 > blue2) {
5605                 blue3 = -1;
5606         }
5607         if (blue1 < blue2) {
5608                 blue3 = 1;
5609         }
5610         red1 += red3;
5611         green1 += green3;
5612         blue1 += blue3;
5613         if (red1 < 0) {
5614                 red1 = 0;
5615         }
5616         if (green1 < 0) {
5617                 green1 = 0;
5618         }
5619         if (blue1 < 0) {
5620                 blue1 = 0;
5621         }
5622         if (red1 > bluemask) {
5623                 red1 = bluemask;
5624         }
5625         if (green1 > bluemask) {
5626                 green1 = bluemask;
5627         }
5628         if (blue1 > bluemask) {
5629                 blue1 = bluemask;
5630         }
5631         tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
5632         return tmp_color;
5633 }
5634
5635 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
5636         if (colour_depth == 0) {
5637                 set_up_gradient();
5638         }
5639         int red1, green1, blue1; // first colour
5640         int red2, green2, blue2; // second colour
5641         long redshift = (2*colour_depth / 3 + colour_depth%3);
5642         long greenshift = (colour_depth / 3);
5643         int red3 = 0, green3 = 0, blue3 = 0; // difference
5644         red1 = (first_colour & redmask) >> redshift;
5645         green1 = (first_colour & greenmask) >> greenshift;
5646         blue1 = first_colour & bluemask;
5647         red2 = (last_colour & redmask) >> redshift;
5648         green2 = (last_colour & greenmask) >> greenshift;
5649         blue2 = last_colour & bluemask;
5650         red3 = abs(red1 - red2);
5651         green3 = abs(green1 - green2);
5652         blue3 = abs(blue1 - blue2);
5653         int max = red3;
5654         if (green3 > max)
5655                 max = green3;
5656         if (blue3 > max)
5657                 max = blue3;
5658         return max;
5659 }
5660
5661 static void draw_line(char *s)
5662 {
5663 #ifdef X11
5664         char *p;
5665         cur_x = text_start_x;
5666         cur_y += font_ascent();
5667         int cur_y_add = 0;
5668         short font_h = font_height();
5669
5670         /* find specials and draw stuff */
5671         p = s;
5672         while (*p) {
5673                 if (*p == SPECIAL_CHAR) {
5674                         int w = 0;
5675
5676                         /* draw string before special */
5677                         *p = '\0';
5678                         draw_string(s);
5679                         *p = SPECIAL_CHAR;
5680                         s = p + 1;
5681
5682                         /* draw special */
5683                         switch (specials[special_index].type) {
5684                         case HORIZONTAL_LINE:
5685                                 {
5686                                         int h =
5687                                             specials[special_index].height;
5688                                         int mid = font_ascent() / 2;
5689                                         w = text_start_x + text_width -
5690                                             cur_x;
5691
5692                                         XSetLineAttributes(display,
5693                                                            window.gc, h,
5694                                                            LineSolid,
5695                                                            CapButt,
5696                                                            JoinMiter);
5697                                         XDrawLine(display, window.drawable,
5698                                                   window.gc, cur_x,
5699                                                   cur_y - mid / 2,
5700                                                   cur_x + w,
5701                                                   cur_y - mid / 2);
5702                                 }
5703                                 break;
5704
5705                         case STIPPLED_HR:
5706                                 {
5707                                         int h =
5708                                             specials[special_index].height;
5709                                         int s =
5710                                             specials[special_index].arg;
5711                                         int mid = font_ascent() / 2;
5712                                         char ss[2] = { s, s };
5713                                         w = text_start_x + text_width -
5714                                             cur_x - 1;
5715
5716                                         XSetLineAttributes(display,
5717                                                            window.gc, h,
5718                                                            LineOnOffDash,
5719                                                            CapButt,
5720                                                            JoinMiter);
5721                                         XSetDashes(display, window.gc, 0,
5722                                                    ss, 2);
5723                                         XDrawLine(display, window.drawable,
5724                                                   window.gc, cur_x,
5725                                                   cur_y - mid / 2,
5726                                                   cur_x + w,
5727                                                   cur_y - mid / 2);
5728                                 }
5729                                 break;
5730
5731                         case BAR:
5732                                 {
5733                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5734                                                 break;
5735                                         }
5736                                         int h =
5737                                             specials[special_index].height;
5738                                         int bar_usage =
5739                                             specials[special_index].arg;
5740                                         int by = cur_y - (font_ascent() / 2) - 1;
5741                                         if (h < font_height()) {
5742                                                 by -= h / 2 - 1;
5743                                         }
5744                                         w = specials[special_index].width;
5745                                         if (w == 0)
5746                                                 w = text_start_x +
5747                                                     text_width - cur_x - 1;
5748                                         if (w < 0)
5749                                                 w = 0;
5750
5751                                         XSetLineAttributes(display,
5752                                                            window.gc, 1,
5753                                                            LineSolid,
5754                                                            CapButt,
5755                                                            JoinMiter);
5756
5757                                         XDrawRectangle(display,
5758                                                        window.drawable,
5759                                                        window.gc, cur_x,
5760                                                        by, w, h);
5761                                         XFillRectangle(display,
5762                                                        window.drawable,
5763                                                        window.gc, cur_x,
5764                                                        by,
5765                                                        w * bar_usage / 255,
5766                                                        h);
5767                                         if (specials[special_index].
5768                                             height > cur_y_add
5769                                             && specials[special_index].
5770                                             height > font_h) {
5771                                                 cur_y_add =
5772                                                     specials
5773                                                     [special_index].height;
5774                                         }
5775                                 }
5776                                 break;
5777
5778                         case GRAPH:
5779                         {
5780                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5781                                                 break;
5782                                         }
5783                                         int h =
5784                                             specials[special_index].height;
5785                                         unsigned long last_colour = current_color;
5786                                         int by = cur_y - (font_ascent()/2) - 1;
5787                                         if (h < font_height()) {
5788                                                 by -= h / 2 - 1;
5789                                         }
5790                                         w = specials[special_index].width;
5791                                         if (w == 0)
5792                                                 w = text_start_x + text_width - cur_x - 1;
5793                                         if (w < 0)
5794                                                 w = 0;
5795                                         if (draw_graph_borders) {
5796                                                 XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter);
5797                                                 XDrawRectangle(display,window.drawable, window.gc, cur_x, by, w, h);
5798                                         }
5799                                         XSetLineAttributes(display,
5800                                                            window.gc, 1,
5801                                                            LineSolid,
5802                                                            CapButt,
5803                                                            JoinMiter);
5804         int i;
5805         int j = 0;
5806         int gradient_size = 0;
5807         float gradient_factor = 0;
5808         float gradient_update = 0;
5809         unsigned long tmpcolour = current_color;
5810         if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5811                 tmpcolour = specials[special_index].last_colour;
5812                 gradient_size = gradient_max(specials[special_index].last_colour, specials[special_index].first_colour);
5813                 gradient_factor = (float)gradient_size / (w - 2);
5814         }
5815         for (i = w - 2; i > -1; i--) {
5816                 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5817                         XSetForeground(display, window.gc, tmpcolour);
5818                         gradient_update += gradient_factor;
5819                         while (gradient_update > 0) {
5820                                 tmpcolour = do_gradient(tmpcolour, specials[special_index].first_colour);
5821                                 gradient_update--;
5822                         }
5823                 }
5824                 if ((w - i) / ((float) (w - 2) / (specials[special_index].graph_width)) > j && j < MAX_GRAPH_DEPTH - 3) {
5825                         j++;
5826                 }
5827                                                 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 */
5828                                         }
5829                                         if (specials[special_index].
5830                                             height > cur_y_add
5831                                             && specials[special_index].
5832                                             height > font_h) {
5833                                                 cur_y_add =
5834                                                     specials
5835                                                     [special_index].height;
5836                                         }
5837 /*                              if (draw_mode == BG) {
5838                                         set_foreground_color(default_bg_color);
5839                                 }
5840                                 else if (draw_mode == OUTLINE) {
5841                                         set_foreground_color(default_out_color);
5842                                 } else {
5843                                         set_foreground_color(default_fg_color);
5844                                 }*/
5845                                 set_foreground_color(last_colour);
5846                         }
5847                                 break;
5848                         
5849                                 case FONT:
5850                                 {
5851                                         int old = font_ascent();
5852                                         cur_y -= font_ascent();
5853                                         selected_font = specials[special_index].font_added;
5854                                         if (cur_y + font_ascent() < cur_y + old) {
5855                                                 cur_y += old;
5856                                         } else {
5857                                                 cur_y += font_ascent();
5858                                         }
5859                                         set_font();
5860                                 }
5861                                 break;
5862                         case FG:
5863                                 if (draw_mode == FG)
5864                                         set_foreground_color(specials
5865                                                              [special_index].
5866                                                              arg);
5867                                 break;
5868
5869                         case BG:
5870                                 if (draw_mode == BG)
5871                                         set_foreground_color(specials
5872                                                              [special_index].
5873                                                              arg);
5874                                 break;
5875
5876                         case OUTLINE:
5877                                 if (draw_mode == OUTLINE)
5878                                         set_foreground_color(specials
5879                                                              [special_index].
5880                                                              arg);
5881                                 break;
5882
5883                         case OFFSET:
5884                                 w += specials[special_index].arg;
5885                                 break;
5886                 
5887                         case VOFFSET:
5888                                 cur_y += specials[special_index].arg;
5889                                 break;
5890
5891                         case GOTO:
5892                                 if (specials[special_index].arg >= 0)
5893                                         cur_x = (int)specials[special_index].arg;
5894                                 break;
5895
5896                         case TAB:
5897                                 {
5898                                         int start = specials[special_index].arg;
5899                                         int step = specials[special_index].width;
5900                                         if (!step || step < 0)
5901                                                 step = 10;
5902                                         w = step - (cur_x - text_start_x - start) % step;
5903                                 }
5904                                 break;
5905
5906                         case ALIGNR:
5907                                 {
5908                                         int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
5909                                         /*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);*/
5910                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5911                                                 cur_x = pos_x - specials[special_index].arg;
5912                                 }
5913                                 }
5914                                 break;
5915
5916                         case ALIGNC:
5917                                 {
5918                                         int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
5919                                         /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
5920                                         /*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);*/
5921                                         if (pos_x >
5922                                             specials[special_index].arg)
5923                                                 w = pos_x -
5924                                                     specials
5925                                                     [special_index].arg;
5926                                 }
5927                                 break;
5928
5929                         }
5930
5931                         cur_x += w;
5932
5933                         special_index++;
5934                 }
5935
5936                 p++;
5937         }
5938 #else
5939         draw_string(s);
5940 #endif
5941 #ifdef X11
5942         if (cur_y_add > 0) {
5943                 cur_y += cur_y_add;
5944                 cur_y -= font_descent();
5945         }
5946
5947         draw_string(s);
5948
5949         cur_y += font_descent();
5950 #endif /* X11 */
5951 }
5952
5953 static void draw_text()
5954 {
5955 #ifdef X11
5956         cur_y = text_start_y;
5957
5958         /* draw borders */
5959         if (draw_borders && border_width > 0) {
5960                 unsigned int b = (border_width + 1) / 2;
5961
5962                 if (stippled_borders) {
5963                         char ss[2] =
5964                             { stippled_borders, stippled_borders };
5965                         XSetLineAttributes(display, window.gc,
5966                                            border_width, LineOnOffDash,
5967                                            CapButt, JoinMiter);
5968                         XSetDashes(display, window.gc, 0, ss, 2);
5969                 } else {
5970                         XSetLineAttributes(display, window.gc,
5971                                            border_width, LineSolid,
5972                                            CapButt, JoinMiter);
5973                 }
5974
5975                 XDrawRectangle(display, window.drawable, window.gc,
5976                                text_start_x - border_margin + b,
5977                                text_start_y - border_margin + b,
5978                                text_width + border_margin * 2 - 1 - b * 2,
5979                                text_height + border_margin * 2 - 1 -
5980                                b * 2);
5981         }
5982
5983         /* draw text */
5984         special_index = 0;
5985 #endif /* X11 */
5986         for_each_line(text_buffer, draw_line);
5987 }
5988
5989 static void draw_stuff()
5990 {
5991 #ifdef X11
5992         selected_font = 0;
5993         if (draw_shades && !draw_outline) {
5994                 text_start_x++;
5995                 text_start_y++;
5996                 set_foreground_color(default_bg_color);
5997                 draw_mode = BG;
5998                 draw_text();
5999                 text_start_x--;
6000                 text_start_y--;
6001         }
6002
6003         if (draw_outline) {
6004                 selected_font = 0;
6005                 int i, j;
6006                 for (i = -1; i < 2; i++)
6007                         for (j = -1; j < 2; j++) {
6008                                 if (i == 0 && j == 0)
6009                                         continue;
6010                                 text_start_x += i;
6011                                 text_start_y += j;
6012                                 set_foreground_color(default_out_color);
6013                                 draw_mode = OUTLINE;
6014                                 draw_text();
6015                                 text_start_x -= i;
6016                                 text_start_y -= j;
6017                         }
6018         }
6019
6020         set_foreground_color(default_fg_color);
6021         draw_mode = FG;
6022 #endif /* X11 */
6023         draw_text();
6024 #ifdef X11
6025 #ifdef HAVE_XDBE
6026         if (use_xdbe) {
6027                 XdbeSwapInfo swap;
6028                 swap.swap_window = window.window;
6029                 swap.swap_action = XdbeBackground;
6030                 XdbeSwapBuffers(display, &swap, 1);
6031         }
6032 #endif
6033 #endif /* X11 */
6034 }
6035 #ifdef X11
6036 static void clear_text(int exposures)
6037 {
6038 #ifdef HAVE_XDBE
6039         if (use_xdbe) {
6040                 return;         /* The swap action is XdbeBackground, which clears */
6041         } else
6042 #endif
6043         {
6044         /* there is some extra space for borders and outlines */
6045         XClearArea(display, window.window,
6046                    text_start_x - border_margin - 1,
6047                    text_start_y - border_margin - 1,
6048                    text_width + border_margin * 2 + 2,
6049                    text_height + border_margin * 2 + 2,
6050                    exposures ? True : 0);
6051         }
6052 }
6053 #endif /* X11 */
6054
6055 static int need_to_update;
6056
6057 /* update_text() generates new text and clears old text area */
6058 static void update_text()
6059 {
6060         generate_text();
6061 #ifdef X11
6062         clear_text(1);
6063 #endif /* X11 */
6064         need_to_update = 1;
6065 }
6066
6067 static void main_loop()
6068 {
6069 #ifdef SIGNAL_BLOCKING
6070         sigset_t  newmask, oldmask;
6071
6072         sigemptyset(&newmask);
6073         sigaddset(&newmask,SIGINT);
6074         sigaddset(&newmask,SIGTERM);
6075         sigaddset(&newmask,SIGUSR1);
6076 #endif
6077
6078 #ifdef X11
6079         Region region = XCreateRegion();
6080 #ifdef HAVE_XDAMAGE
6081         int event_base, error_base;
6082         if (!XDamageQueryExtension (display, &event_base, &error_base)) {
6083                 ERR("Xdamage extension unavailable");
6084         }
6085         Damage damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
6086         XserverRegion region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
6087         XserverRegion part = XFixesCreateRegionFromWindow(display, window.window, 0);
6088 #endif /* HAVE_XDAMAGE */
6089 #endif /* X11 */
6090
6091         info.looped = 0;
6092         while (total_run_times == 0 || info.looped < total_run_times) {
6093                 info.looped++;
6094
6095 #ifdef SIGNAL_BLOCKING
6096                 /* block signals.  we will inspect for pending signals later */
6097                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
6098                         CRIT_ERR("unable to sigprocmask()");
6099 #endif
6100
6101 #ifdef X11
6102                 XFlush(display);
6103
6104                 /* wait for X event or timeout */
6105
6106                 if (!XPending(display)) {
6107                         fd_set fdsr;
6108                         struct timeval tv;
6109                         int s;
6110                         double t =
6111                             update_interval - (get_time() -
6112                                                last_update_time);
6113
6114                         if (t < 0)
6115                                 t = 0;
6116
6117                         tv.tv_sec = (long) t;
6118                         tv.tv_usec = (long) (t * 1000000) % 1000000;
6119                         FD_ZERO(&fdsr);
6120                         FD_SET(ConnectionNumber(display), &fdsr);
6121
6122
6123                         s = select(ConnectionNumber(display) + 1, &fdsr, 0,
6124                                    0, &tv);
6125 #else
6126                         usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
6127 #endif /* X11 */
6128 #ifdef X11
6129                         if (s == -1) {
6130                                 if (errno != EINTR)
6131                                         ERR("can't select(): %s",
6132                                             strerror(errno));
6133                         } else {
6134                                 /* timeout */
6135                                 if (s == 0)
6136 #endif /* X11 */
6137                                         update_text();
6138 #ifdef X11
6139                         }
6140                 }
6141                 
6142                 if (need_to_update) {
6143 #ifdef OWN_WINDOW
6144                         int wx = window.x, wy = window.y;
6145 #endif
6146
6147                         need_to_update = 0;
6148                         selected_font = 0;
6149                         update_text_area();
6150 #ifdef OWN_WINDOW
6151                         if (own_window) {
6152                                 /* resize window if it isn't right size */
6153                                 if (!fixed_size &&
6154                                     (text_width + border_margin * 2 + 1 !=
6155                                      window.width
6156                                      || text_height + border_margin * 2 + 1 !=
6157                                      window.height)) {
6158                                         window.width =
6159                                             text_width +
6160                                             border_margin * 2 + 1;
6161                                         window.height =
6162                                             text_height +
6163                                             border_margin * 2 + 1;
6164                                         XResizeWindow(display,
6165                                                       window.window,
6166                                                       window.width,
6167                                                       window.height);
6168                         if (own_window) {
6169                                 set_transparent_background(window.window);
6170                         }
6171                                      }
6172
6173                                 /* move window if it isn't in right position */
6174                                 if (!fixed_pos
6175                                     && (window.x != wx
6176                                         || window.y != wy)) {
6177                                         XMoveWindow(display, window.window,
6178                                                     window.x, window.y);
6179                                 }
6180                         }
6181 #endif
6182
6183                         clear_text(1);
6184
6185 #ifdef HAVE_XDBE
6186                         if (use_xdbe) {
6187                                 XRectangle r;
6188                                 r.x = text_start_x - border_margin;
6189                                 r.y = text_start_y - border_margin;
6190                                 r.width = text_width + border_margin * 2;
6191                                 r.height = text_height + border_margin * 2;
6192                                 XUnionRectWithRegion(&r, region, region);
6193                         }
6194 #endif
6195                 }
6196
6197                 /* handle X events */
6198
6199                 while (XPending(display)) {
6200                         XEvent ev;
6201                         XNextEvent(display, &ev);
6202                         switch (ev.type) {
6203                         case Expose:
6204                                 {
6205                                         XRectangle r;
6206                                         r.x = ev.xexpose.x;
6207                                         r.y = ev.xexpose.y;
6208                                         r.width = ev.xexpose.width;
6209                                         r.height = ev.xexpose.height;
6210                                         XUnionRectWithRegion(&r, region,
6211                                                              region);
6212                                 }
6213                                 break;
6214
6215 #ifdef OWN_WINDOW
6216                         case ReparentNotify:
6217                                 /* set background to ParentRelative for all parents */
6218                                 if (own_window) {
6219                                         set_transparent_background(window.
6220                                         window);
6221                                 }
6222                                 break;
6223
6224                         case ConfigureNotify:
6225                                 if (own_window) {
6226                                         /* if window size isn't what expected, set fixed size */
6227                                         if (ev.xconfigure.width !=
6228                                             window.width
6229                                             || ev.xconfigure.height !=
6230                                             window.height) {
6231                                                 if (window.width != 0
6232                                                     && window.height != 0)
6233                                                         fixed_size = 1;
6234
6235                                                 /* clear old stuff before screwing up size and pos */
6236                                                 clear_text(1);
6237
6238                                                 {
6239                                                         XWindowAttributes
6240                                                             attrs;
6241                                                         if (XGetWindowAttributes(display, window.window, &attrs)) {
6242                                                                 window.
6243                                                                     width =
6244                                                                     attrs.
6245                                                                     width;
6246                                                                 window.
6247                                                                     height
6248                                                                     =
6249                                                                     attrs.
6250                                                                     height;
6251                                                         }
6252                                                 }
6253
6254                                                 text_width =
6255                                                     window.width -
6256                                                     border_margin * 2 - 1;
6257                                                 text_height =
6258                                                     window.height -
6259                                                     border_margin * 2 - 1;
6260                                                 if (text_width > maximum_width && maximum_width > 0)
6261                                                         text_width = maximum_width;
6262                                         }
6263
6264                                         /* if position isn't what expected, set fixed pos, total_updates
6265                                          * avoids setting fixed_pos when window is set to weird locations
6266                                          * when started */
6267                                         /*if (total_updates >= 2 this is broken
6268                                             && !fixed_pos
6269                                             && (window.x != ev.xconfigure.x
6270                                                 || window.y !=
6271                                                 ev.xconfigure.y)
6272                                             && (ev.xconfigure.x != 0
6273                                                 || ev.xconfigure.y != 0)) {
6274                                                 fixed_pos = 1;
6275                                 }*/
6276                                         set_font();
6277                                 }
6278                                 break;
6279
6280                         case ButtonPress:
6281                                 if (own_window)
6282         {
6283           /* if an ordinary window with decorations */
6284           if ((window.type==TYPE_NORMAL) && (!TEST_HINT(window.hints,HINT_UNDECORATED)))
6285           {
6286             /* allow conky to hold input focus.*/
6287             break;
6288           }
6289           else
6290                                   {
6291                                     /* forward the click to the desktop window */
6292                                     XUngrabPointer(display, ev.xbutton.time);
6293                                     ev.xbutton.window = window.desktop;
6294                                     XSendEvent(display, ev.xbutton.window, False, ButtonPressMask, &ev);
6295             XSetInputFocus(display, ev.xbutton.window,RevertToParent,ev.xbutton.time);
6296                                   }
6297         }
6298                                 break;
6299
6300                         case ButtonRelease:
6301         if (own_window)
6302         {
6303           /* if an ordinary window with decorations */
6304           if ((window.type==TYPE_NORMAL) && (!TEST_HINT(window.hints,HINT_UNDECORATED)))
6305           {
6306             /* allow conky to hold input focus.*/
6307             break;
6308           }
6309           else
6310           {
6311             /* forward the release to the desktop window */
6312             ev.xbutton.window = window.desktop;
6313             XSendEvent(display, ev.xbutton.window, False, ButtonReleaseMask, &ev);
6314           }
6315         }
6316         break;
6317
6318 #endif
6319
6320                         default:
6321 #ifdef HAVE_XDAMAGE
6322                                 if (ev.type == event_base + XDamageNotify) {
6323                                         XDamageNotifyEvent  *dev = (XDamageNotifyEvent *) &ev;
6324                                         XFixesSetRegion(display, part, &dev->area, 1);
6325                                         XFixesUnionRegion(display, region2, region2, part);
6326                                 }
6327 #endif /* HAVE_XDAMAGE */
6328                                 break;
6329                         }
6330         }
6331 #ifdef HAVE_XDAMAGE
6332                 XDamageSubtract(display, damage, region2, None);
6333                 XFixesSetRegion(display, region2, 0, 0);
6334 #endif /* HAVE_XDAMAGE */
6335
6336                 /* XDBE doesn't seem to provide a way to clear the back buffer without
6337                  * interfering with the front buffer, other than passing XdbeBackground
6338                  * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6339                  * redraw the text even if it wasn't part of the exposed area. OTOH,
6340                  * if we're not going to call draw_stuff at all, then no swap happens
6341                  * and we can safely do nothing.
6342                  */
6343
6344                 if (!XEmptyRegion(region)) {
6345 #ifdef HAVE_XDBE
6346                         if (use_xdbe) {
6347                                 XRectangle r;
6348                                 r.x = text_start_x - border_margin;
6349                                 r.y = text_start_y - border_margin;
6350                                 r.width = text_width + border_margin * 2;
6351                                 r.height = text_height + border_margin * 2;
6352                                 XUnionRectWithRegion(&r, region, region);
6353                 }
6354 #endif
6355                         XSetRegion(display, window.gc, region);
6356 #ifdef XFT
6357                         if (use_xft) {
6358                                 XftDrawSetClip(window.xftdraw, region);
6359                         }
6360 #endif
6361 #endif /* X11 */
6362                         draw_stuff();
6363 #ifdef X11
6364                         XDestroyRegion(region);
6365                         region = XCreateRegion();
6366                 }
6367 #endif /* X11 */
6368
6369 #ifdef SIGNAL_BLOCKING
6370                 /* unblock signals of interest and let handler fly */
6371                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
6372                         CRIT_ERR("unable to sigprocmask()");
6373 #endif
6374
6375                 switch(g_signal_pending) {
6376                 case SIGUSR1:
6377                         {
6378                         ERR("received SIGUSR1. reloading the config file.");
6379                         reload_config();
6380                         break;
6381                         }
6382                 case SIGINT:
6383                 case SIGTERM:
6384                         {
6385                         ERR("received SIGINT or SIGTERM to terminate. bye!");
6386                         clean_up();
6387 #ifdef X11
6388                         XDestroyRegion(region);
6389                         region = NULL;
6390 #endif /* X11 */
6391                         return;  /* return from main_loop */
6392                         /*break;*/
6393                         }
6394                 default:
6395                         {
6396                         /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
6397                          * but didn't write any code to deal with it.   if you don't want to
6398                          * handle a signal, don't set a handler on it in the first place. */
6399                         if (g_signal_pending)
6400                                 ERR("ignoring signal (%d)", g_signal_pending);
6401                         break;
6402                         }
6403                 }
6404                 g_signal_pending=0;
6405         
6406         }
6407 #if defined(X11) && defined(HAVE_XDAMAGE)
6408         XDamageDestroy(display, damage);
6409         XFixesDestroyRegion(display, region2);
6410         XFixesDestroyRegion(display, part);
6411         XDestroyRegion(region);
6412         region = NULL;
6413 #endif /* X11 && HAVE_XDAMAGE */
6414 }
6415
6416 static void load_config_file(const char *);
6417
6418 /* reload the config file */
6419 void reload_config(void)
6420 {
6421         timed_thread_destroy_registered_threads ();
6422
6423         if (info.cpu_usage) {
6424                 free(info.cpu_usage);
6425                 info.cpu_usage = NULL;
6426         }
6427
6428 #ifdef TCP_PORT_MONITOR
6429         destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6430 #endif
6431
6432         if (current_config) {
6433                 clear_fs_stats();
6434                 load_config_file(current_config);
6435
6436                 /* re-init specials array */
6437                 if ((specials = realloc ((void *)specials, sizeof(struct special_t)*max_specials)) == 0)
6438                     ERR("failed to realloc specials array");
6439
6440 #ifdef X11
6441                 load_fonts();
6442                 set_font();
6443                 XClearWindow(display, RootWindow(display, screen)); // clear the window first
6444
6445 #endif /* X11 */
6446 #ifdef TCP_PORT_MONITOR
6447                 info.p_tcp_port_monitor_collection = NULL; 
6448 #endif
6449 #ifdef AUDACIOUS
6450                 if (create_audacious_thread() !=0)
6451                     CRIT_ERR("unable to create audacious thread!");
6452                 timed_thread_register (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
6453 #endif
6454                 extract_variable_text(text);
6455                 free(text);
6456                 text = NULL;
6457                 update_text();
6458         }
6459 }
6460
6461 void clean_up(void)
6462 {
6463         timed_thread_destroy_registered_threads ();
6464
6465         if (info.cpu_usage) {
6466                 free(info.cpu_usage);
6467                 info.cpu_usage = NULL;
6468         }
6469 #ifdef X11
6470 #ifdef HAVE_XDBE
6471         if (use_xdbe) {
6472                 XdbeDeallocateBackBufferName(display, window.back_buffer);
6473         }
6474 #endif
6475 #ifdef OWN_WINDOW
6476         if (own_window) 
6477         {
6478                 XDestroyWindow(display, window.window);
6479                 XClearWindow(display, RootWindow(display, screen));
6480                 XFlush(display);
6481         }
6482         else
6483 #endif
6484         {
6485                 XClearWindow(display, RootWindow(display, screen));
6486                 clear_text(1);
6487                 XFlush(display);
6488         }
6489
6490         XFreeGC(display, window.gc);
6491 #endif /* X11 */
6492
6493         free_text_objects(text_object_count, text_objects);
6494         text_object_count = 0;
6495         text_objects = NULL;
6496
6497         if (text != original_text)
6498                 free(text);
6499
6500         free(current_config);
6501         free(current_mail_spool);
6502
6503 #ifdef TCP_PORT_MONITOR
6504         destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6505         info.p_tcp_port_monitor_collection = NULL;
6506 #endif
6507 #ifdef RSS
6508         free_rss_info();
6509 #endif
6510
6511         if (specials) {
6512                 unsigned int i;
6513                 for(i=0;i<special_count;i++)
6514                         if(specials[i].type == GRAPH)
6515                                 free(specials[i].graph);
6516                 free (specials);
6517                 specials=NULL;
6518         }
6519 }
6520
6521 static int string_to_bool(const char *s)
6522 {
6523         if (!s)
6524                 return 1;
6525         if (strcasecmp(s, "yes") == 0)
6526                 return 1;
6527         if (strcasecmp(s, "true") == 0)
6528                 return 1;
6529         if (strcasecmp(s, "1") == 0)
6530                 return 1;
6531         return 0;
6532 }
6533 #ifdef X11
6534 static enum alignment string_to_alignment(const char *s)
6535 {
6536         if (strcasecmp(s, "top_left") == 0)
6537                 return TOP_LEFT;
6538         else if (strcasecmp(s, "top_right") == 0)
6539                 return TOP_RIGHT;
6540         else if (strcasecmp(s, "bottom_left") == 0)
6541                 return BOTTOM_LEFT;
6542         else if (strcasecmp(s, "bottom_right") == 0)
6543                 return BOTTOM_RIGHT;
6544         else if (strcasecmp(s, "tl") == 0)
6545                 return TOP_LEFT;
6546         else if (strcasecmp(s, "tr") == 0)
6547                 return TOP_RIGHT;
6548         else if (strcasecmp(s, "bl") == 0)
6549                 return BOTTOM_LEFT;
6550         else if (strcasecmp(s, "br") == 0)
6551                 return BOTTOM_RIGHT;
6552         else if (strcasecmp(s, "none") == 0)
6553                 return NONE;
6554         return TOP_LEFT;
6555 }
6556 #endif /* X11 */
6557
6558
6559 static void set_default_configurations(void)
6560 {
6561         fork_to_background = 0;
6562         total_run_times = 0;
6563         info.cpu_avg_samples = 2;
6564         info.net_avg_samples = 2;
6565         info.memmax = 0;
6566         top_cpu = 0;
6567         top_mem = 0;
6568 #ifdef MPD
6569         strcpy(info.mpd.host, "localhost");
6570         info.mpd.port = 6600;
6571         info.mpd.status = NULL;
6572         info.mpd.artist = NULL;
6573         info.mpd.album = NULL;
6574         info.mpd.title = NULL;
6575         info.mpd.random = NULL;
6576         info.mpd.track = NULL;
6577         info.mpd.name = NULL;
6578         info.mpd.file = NULL;
6579 #endif
6580 #ifdef XMMS2
6581     info.xmms2.artist = NULL;
6582     info.xmms2.album = NULL;
6583     info.xmms2.title = NULL;
6584     info.xmms2.genre = NULL;
6585     info.xmms2.comment = NULL;
6586     info.xmms2.decoder = NULL;
6587     info.xmms2.transport = NULL;
6588     info.xmms2.url = NULL;
6589     info.xmms2.status = NULL;
6590 #endif
6591         use_spacer = 0;
6592 #if defined(__linux__)
6593         post_21_kernel = 0;
6594 #endif /* __linux__ */
6595 #ifdef X11
6596         out_to_console = 0;
6597 #else
6598         out_to_console = 1;
6599 #endif
6600 #ifdef X11
6601         default_fg_color = WhitePixel(display, screen);
6602         default_bg_color = BlackPixel(display, screen);
6603         default_out_color = BlackPixel(display, screen);
6604         color0 = default_fg_color;
6605         color1 = default_fg_color;
6606         color2 = default_fg_color;
6607         color3 = default_fg_color;
6608         color4 = default_fg_color;
6609         color5 = default_fg_color;
6610         color6 = default_fg_color;
6611         color7 = default_fg_color;
6612         color8 = default_fg_color;
6613         color9 = default_fg_color;
6614         draw_shades = 1;
6615         draw_borders = 0;
6616         draw_graph_borders = 1;
6617         draw_outline = 0;
6618         set_first_font("6x10");
6619         gap_x = 5;
6620         gap_y = 5;
6621         minimum_width = 5;
6622         minimum_height = 5;
6623         maximum_width = 0;
6624 #ifdef OWN_WINDOW
6625         own_window = 0;
6626         window.type=TYPE_NORMAL;
6627         window.hints=0;
6628   strcpy(window.class_name, "Conky");   
6629   update_uname();
6630   sprintf(window.title,"%s - conky",info.uname_s.nodename);
6631 #endif
6632         stippled_borders = 0;
6633         border_margin = 3;
6634         border_width = 1;
6635         text_alignment = BOTTOM_LEFT;
6636 #endif /* X11 */
6637
6638         free(current_mail_spool);
6639         {
6640                 char buf[256];
6641                 variable_substitute(MAIL_FILE, buf, 256);
6642                 if (buf[0] != '\0')
6643                         current_mail_spool = strdup(buf);
6644         }
6645
6646         no_buffers = 1;
6647         update_interval = 10.0;
6648         stuff_in_upper_case = 0;
6649
6650 #ifdef TCP_PORT_MONITOR
6651         tcp_port_monitor_args.max_port_monitor_connections = MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
6652 #endif
6653 }
6654
6655 static void load_config_file(const char *f)
6656 {
6657 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6658         int line = 0;
6659         FILE *fp;
6660
6661         set_default_configurations();
6662         fp = fopen(f, "r");
6663         if (!fp)
6664                 return;
6665
6666         while (!feof(fp)) {
6667                 char buf[256], *p, *p2, *name, *value;
6668                 line++;
6669                 if (fgets(buf, 256, fp) == NULL)
6670                         break;
6671
6672                 p = buf;
6673
6674                 /* break at comment */
6675                 p2 = strchr(p, '#');
6676                 if (p2)
6677                         *p2 = '\0';
6678
6679                 /* skip spaces */
6680                 while (*p && isspace((int) *p))
6681                         p++;
6682                 if (*p == '\0')
6683                         continue;       /* empty line */
6684
6685                 name = p;
6686
6687                 /* skip name */
6688                 p2 = p;
6689                 while (*p2 && !isspace((int) *p2))
6690                         p2++;
6691                 if (*p2 != '\0') {
6692                         *p2 = '\0';     /* break at name's end */
6693                         p2++;
6694                 }
6695
6696                 /* get value */
6697                 if (*p2) {
6698                         p = p2;
6699                         while (*p && isspace((int) *p))
6700                                 p++;
6701
6702                         value = p;
6703
6704                         p2 = value + strlen(value);
6705                         while (isspace((int) *(p2 - 1)))
6706                                 *--p2 = '\0';
6707                 } else {
6708                         value = 0;
6709                 }
6710
6711 #define CONF2(a) if (strcasecmp(name, a) == 0)
6712 #define CONF(a) else CONF2(a)
6713 #define CONF3(a,b) \
6714 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
6715
6716
6717 #ifdef X11
6718                 CONF2("alignment") {
6719                 if (value) {
6720                         int a = string_to_alignment(value);
6721                         if (a <= 0)
6722                                 CONF_ERR;
6723                         else
6724                                 text_alignment = a;
6725                 } else
6726                         CONF_ERR;
6727                 }
6728                 CONF("background") {
6729                         fork_to_background = string_to_bool(value);
6730                 }
6731
6732 #else
6733                 CONF2("background") {
6734                         fork_to_background = string_to_bool(value);
6735                 }
6736 #endif /* X11 */
6737 #ifdef X11
6738                 CONF("border_margin") {
6739                         if (value)
6740                                 border_margin = strtol(value, 0, 0);
6741                         else
6742                                 CONF_ERR;
6743                 }
6744                 CONF("border_width") {
6745                         if (value)
6746                                 border_width = strtol(value, 0, 0);
6747                         else
6748                                 CONF_ERR;
6749                 }
6750                 CONF("color0") {
6751                         if (value)
6752                                 color0 = get_x11_color(value);
6753                         else
6754                                 CONF_ERR;
6755                 }
6756                 CONF("color1") {
6757                         if (value)
6758                                 color1 = get_x11_color(value);
6759                         else
6760                                 CONF_ERR;
6761                 }
6762                 CONF("color2") {
6763                         if (value)
6764                                 color2 = get_x11_color(value);
6765                         else
6766                                 CONF_ERR;
6767                 }
6768                 CONF("color3") {
6769                         if (value)
6770                                 color3 = get_x11_color(value);
6771                         else
6772                                 CONF_ERR;
6773                 }
6774                 CONF("color4") {
6775                         if (value)
6776                                 color4 = get_x11_color(value);
6777                         else
6778                                 CONF_ERR;
6779                 }
6780                 CONF("color5") {
6781                         if (value)
6782                                 color5 = get_x11_color(value);
6783                         else
6784                                 CONF_ERR;
6785                 }
6786                 CONF("color6") {
6787                         if (value)
6788                                 color6 = get_x11_color(value);
6789                         else
6790                                 CONF_ERR;
6791                 }
6792                 CONF("color7") {
6793                         if (value)
6794                                 color7 = get_x11_color(value);
6795                         else
6796                                 CONF_ERR;
6797                 }
6798                 CONF("color8") {
6799                         if (value)
6800                                 color8 = get_x11_color(value);
6801                         else
6802                                 CONF_ERR;
6803                 }
6804                 CONF("color9") {
6805                         if (value)
6806                                 color9 = get_x11_color(value);
6807                         else
6808                                 CONF_ERR;
6809                 }
6810                 CONF("default_color") {
6811                         if (value)
6812                                 default_fg_color = get_x11_color(value);
6813                         else
6814                                 CONF_ERR;
6815                 }
6816                 CONF3("default_shade_color", "default_shadecolor") {
6817                         if (value)
6818                                 default_bg_color = get_x11_color(value);
6819                         else
6820                                 CONF_ERR;
6821                 }
6822                 CONF3("default_outline_color", "default_outlinecolor") {
6823                         if (value)
6824                                 default_out_color = get_x11_color(value);
6825                         else
6826                                 CONF_ERR;
6827                 }
6828 #endif /* X11 */
6829                 CONF("imap") {
6830                         if (value) {
6831                                 info.mail = parse_mail_args(IMAP, value);
6832                         } else {
6833                                 CONF_ERR;
6834                         }
6835                 }
6836                 CONF("pop3") {
6837                         if (value) {
6838                                 info.mail = parse_mail_args(POP3, value);
6839                         } else {
6840                                 CONF_ERR;
6841                         }
6842                 }
6843 #ifdef MPD
6844                 CONF("mpd_host") {
6845                         if (value)
6846                                 strncpy(info.mpd.host, value, 127);
6847                         else
6848                                 CONF_ERR;
6849                 }
6850                 CONF("mpd_port") {
6851                         if (value) {
6852                                 info.mpd.port = strtol(value, 0, 0);
6853                                 if (info.mpd.port < 1
6854                                     || info.mpd.port > 0xffff)
6855                                         CONF_ERR;
6856                         }
6857                 }
6858                 CONF("mpd_password") {
6859                         if (value)
6860                                 strncpy(info.mpd.password, value, 127);
6861                         else
6862                                 CONF_ERR;
6863                 }
6864 #endif
6865 #ifdef __OpenBSD__
6866                 CONF("sensor_device") {
6867                         if(value)
6868                                 sensor_device = strtol(value, 0, 0);
6869                         else
6870                                 CONF_ERR;
6871                 }
6872 #endif
6873                 CONF("cpu_avg_samples") {
6874                         if (value) {
6875                                 cpu_avg_samples = strtol(value, 0, 0);
6876                                 if (cpu_avg_samples < 1
6877                                     || cpu_avg_samples > 14)
6878                                         CONF_ERR;
6879                                 else
6880                                         info.
6881                                             cpu_avg_samples
6882                                             = cpu_avg_samples;
6883                         } else
6884                                 CONF_ERR;
6885                 }
6886                 CONF("net_avg_samples") {
6887                         if (value) {
6888                                 net_avg_samples = strtol(value, 0, 0);
6889                                 if (net_avg_samples < 1
6890                                     || net_avg_samples > 14)
6891                                         CONF_ERR;
6892                                 else
6893                                         info.
6894                                             net_avg_samples
6895                                             = net_avg_samples;
6896                         } else
6897                                 CONF_ERR;
6898                 }
6899
6900
6901
6902
6903
6904
6905 #ifdef HAVE_XDBE
6906                 CONF("double_buffer") {
6907                 use_xdbe = string_to_bool(value);
6908                 }
6909 #endif
6910 #ifdef X11
6911                 CONF("override_utf8_locale") {
6912         utf8_mode = string_to_bool(value);
6913                 }
6914
6915                 CONF("draw_borders") {
6916                         draw_borders = string_to_bool(value);
6917                 }
6918                 CONF("draw_graph_borders") {
6919                         draw_graph_borders = string_to_bool(value);
6920                 }
6921                 CONF("draw_shades") {
6922                         draw_shades = string_to_bool(value);
6923                 }
6924                 CONF("draw_outline") {
6925                         draw_outline = string_to_bool(value);
6926                 }
6927 #if defined(__linux__)
6928                 CONF("post_21_kernel") {
6929                         post_21_kernel = string_to_bool(value);
6930                 }
6931 #endif /* __linux__ */
6932 #endif /* X11 */
6933                 CONF("out_to_console") {
6934                         out_to_console = string_to_bool(value);
6935                 }
6936                 CONF("use_spacer") {
6937                         use_spacer = string_to_bool(value);
6938                 }
6939 #ifdef X11
6940 #ifdef XFT
6941                 CONF("use_xft") {
6942                         use_xft = string_to_bool(value);
6943                 }
6944                 CONF("font") {
6945                         if (value) {
6946                                 set_first_font(value);
6947                         } else
6948                                 CONF_ERR;
6949                 }
6950                 CONF("xftalpha") {
6951                         if (value && font_count >= 0)
6952                                 fonts[0].font_alpha = atof(value)
6953                                     * 65535.0;
6954                         else
6955                                 CONF_ERR;
6956                 }
6957                 CONF("xftfont") {
6958                         if (use_xft) {
6959 #else
6960                 CONF("use_xft") {
6961                         if (string_to_bool(value))
6962                                 ERR("Xft not enabled");
6963                 }
6964                 CONF("xftfont") {
6965                         /* xftfont silently ignored when no Xft */
6966                 }
6967                 CONF("xftalpha") {
6968                         /* xftalpha is silently ignored when no Xft */
6969                 }
6970                 CONF("font") {
6971 #endif
6972                                 if (value) {
6973                                         set_first_font(value);
6974                                 } else
6975                                         CONF_ERR;
6976 #ifdef XFT
6977                         }
6978 #endif
6979                 }
6980                 CONF("gap_x") {
6981                         if (value)
6982                                 gap_x = atoi(value);
6983                         else
6984                                 CONF_ERR;
6985                 }
6986                 CONF("gap_y") {
6987                         if (value)
6988                                 gap_y = atoi(value);
6989                         else
6990                                 CONF_ERR;
6991                 }
6992 #endif /* X11 */
6993                 CONF("mail_spool") {
6994                         if (value) {
6995                                 char buf[256];
6996                                 variable_substitute(value, buf, 256);
6997
6998                                 if (buf[0]
6999                                     != '\0') {
7000                                         if (current_mail_spool)
7001                                                 free(current_mail_spool);
7002                                         current_mail_spool = strdup(buf);
7003                                 }
7004                         } else
7005                                 CONF_ERR;
7006                 }
7007 #ifdef X11
7008                 CONF("minimum_size") {
7009         if (value) {
7010                 if (sscanf
7011                                   (value, "%d %d", &minimum_width,
7012                                    &minimum_height) != 2)
7013                         if (sscanf
7014                                                  (value, "%d",
7015                                                    &minimum_width) != 1)
7016                                 CONF_ERR;
7017         } else
7018                 CONF_ERR;
7019                 }
7020                 CONF("maximum_width") {
7021                         if (value) {
7022                                 if (sscanf(value, "%d", &maximum_width) != 1)
7023                                         CONF_ERR;
7024                         } else
7025                                 CONF_ERR;
7026                 }
7027 #endif /* X11 */
7028                 CONF("no_buffers") {
7029                         no_buffers = string_to_bool(value);
7030                 }
7031                 CONF("pad_percents") {
7032         pad_percents = atoi(value);
7033                 }
7034 #ifdef X11
7035 #ifdef OWN_WINDOW
7036                 CONF("own_window") {
7037       if (value)
7038                           own_window = string_to_bool(value);
7039       else
7040         CONF_ERR;
7041                 }
7042                 CONF("own_window_class") {
7043       if (value)
7044       {
7045                           memset(window.class_name,0,sizeof(window.class_name));
7046                           strncpy(window.class_name, value, sizeof(window.class_name)-1);
7047       }
7048       else 
7049         CONF_ERR;
7050                 }
7051     CONF("own_window_title") {
7052       if (value)
7053       {
7054         memset(window.title,0,sizeof(window.title));
7055         strncpy(window.title, value, sizeof(window.title)-1);
7056       }
7057       else
7058         CONF_ERR;
7059     }
7060                 CONF("own_window_transparent") {
7061       if (value)
7062                           set_transparent = string_to_bool(value);
7063       else
7064         CONF_ERR;
7065                 }
7066                 CONF("own_window_colour") {
7067                         if (value) {
7068                                 background_colour = get_x11_color(value);
7069                         } else {
7070                                 ERR("Invalid colour for own_winder_colour (try omitting the '#' for hex colours");
7071                         }
7072                 }
7073                 CONF("own_window_hints") {
7074                         if (value) {
7075                                 char *p_hint, *p_save;
7076                                 char delim[] = ", ";
7077
7078                                 /* tokenize the value into individual hints */
7079                                 if ((p_hint=strtok_r(value, delim, &p_save)) != NULL)
7080                                 do {
7081                                          /*fprintf(stderr, "hint [%s] parsed\n", p_hint);*/
7082                                          if (strncmp(p_hint,"undecorate",10) == 0)
7083                                              SET_HINT(window.hints,HINT_UNDECORATED);
7084                                          else if (strncmp(p_hint,"below",5) == 0)
7085                                              SET_HINT(window.hints,HINT_BELOW);
7086                                          else if (strncmp(p_hint,"above",5) == 0)
7087                                              SET_HINT(window.hints,HINT_ABOVE);
7088                                          else if (strncmp(p_hint,"sticky",6) == 0)
7089                                              SET_HINT(window.hints,HINT_STICKY);
7090                                          else if (strncmp(p_hint,"skip_taskbar",12) == 0)
7091                                              SET_HINT(window.hints,HINT_SKIP_TASKBAR);
7092                                          else if (strncmp(p_hint,"skip_pager",10) == 0)
7093                                              SET_HINT(window.hints,HINT_SKIP_PAGER);
7094                                          else
7095                                              CONF_ERR;
7096
7097                                          p_hint=strtok_r(NULL, delim, &p_save);
7098                                 }
7099                                 while (p_hint!=NULL);
7100                         }
7101       else
7102         CONF_ERR;
7103                 }
7104                 CONF("own_window_type") {
7105                         if (value) {
7106                                 if (strncmp(value,"normal",6)==0)
7107                                         window.type = TYPE_NORMAL;
7108                                 else if  (strncmp(value,"desktop",7)==0)
7109                                         window.type = TYPE_DESKTOP;
7110                                 else if (strncmp(value,"override",8)==0)
7111                                         window.type = TYPE_OVERRIDE;
7112                                 else
7113                                   CONF_ERR;
7114                         }
7115       else
7116         CONF_ERR;
7117                 }
7118 #endif
7119                 CONF("stippled_borders") {
7120                         if (value)
7121                                 stippled_borders = strtol(value, 0, 0);
7122                         else
7123                                 stippled_borders = 4;
7124                 }
7125 #endif /* X11 */
7126                 CONF("temp1") {
7127                         ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
7128                 }
7129                 CONF("temp1") {
7130                         ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
7131                 }
7132                 CONF("update_interval") {
7133                         if (value)
7134                                 update_interval = strtod(value, 0);
7135                         else
7136                                 CONF_ERR;
7137                 }
7138                 CONF("total_run_times") {
7139                         if (value)
7140                                 total_run_times = strtod(value, 0);
7141                         else
7142                                 CONF_ERR;
7143                 }
7144                 CONF("uppercase") {
7145                         stuff_in_upper_case = string_to_bool(value);
7146                 }
7147                 CONF("max_specials") {
7148                         if (value)
7149                                 max_specials = atoi(value);
7150                         else
7151                                 CONF_ERR;
7152                 }
7153                 CONF("max_user_text") {
7154                         if (value)
7155                                 max_user_text = atoi(value);
7156                         else
7157                                 CONF_ERR;
7158                 }
7159                 CONF("text_buffer_size") {
7160                         if (value)
7161                                 text_buffer_size = atoi(value);
7162                         else
7163                                 CONF_ERR;
7164                 }
7165                 CONF("text") {
7166                         if (text != original_text)
7167                                 free(text);
7168
7169                         text = (char *)
7170                             malloc(1);
7171                         text[0]
7172                             = '\0';
7173
7174                         while (!feof(fp)) {
7175                                 unsigned
7176                                 int l = strlen(text);
7177                                 if (fgets(buf, 256, fp) == NULL)
7178                                         break;
7179                                 text = (char *)
7180                                     realloc(text, l + strlen(buf)
7181                                             + 1);
7182                                 strcat(text, buf);
7183
7184                                 if (strlen(text) > max_user_text)
7185                                         break;
7186                         }
7187                         fclose(fp);
7188                         text_lines = line + 1;
7189                         return;
7190                 }
7191 #ifdef TCP_PORT_MONITOR
7192                 CONF("max_port_monitor_connections") 
7193                 {
7194                         if ( !value || 
7195                              (sscanf(value, "%d", &tcp_port_monitor_args.max_port_monitor_connections) != 1) 
7196                              || tcp_port_monitor_args.max_port_monitor_connections < 0 )
7197                         {
7198                                 /* an error. use default, warn and continue. */
7199                                 tcp_port_monitor_args.max_port_monitor_connections = 
7200                                         MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
7201                                 CONF_ERR;
7202                         }
7203                         else if ( tcp_port_monitor_args.max_port_monitor_connections == 0 )
7204                         {
7205                                 /* no error, just use default */
7206                                 tcp_port_monitor_args.max_port_monitor_connections = 
7207                                         MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
7208                         }
7209                         /* else tcp_port_monitor_args.max_port_monitor_connections > 0 as per config */
7210                 }
7211 #endif
7212
7213                 else
7214                 ERR("%s: %d: no such configuration: '%s'", f, line, name);
7215
7216 #undef CONF
7217 #undef CONF2
7218         }
7219
7220         fclose(fp);
7221 #undef CONF_ERR
7222
7223
7224 }
7225
7226                                                                                                                                                                                         /* : means that character before that takes an argument */
7227 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
7228 #ifdef X11
7229                 "x:y:w:a:f:"
7230 #ifdef OWN_WINDOW
7231     "o"
7232 #endif
7233 #ifdef HAVE_XDBE
7234     "b"
7235 #endif
7236 #endif /* X11 */
7237     ;
7238
7239
7240 int main(int argc, char **argv)
7241 {
7242         struct sigaction act, oact;
7243
7244         g_signal_pending=0;
7245         memset(&info, 0, sizeof(info) );
7246
7247 #ifdef TCP_PORT_MONITOR
7248         tcp_port_monitor_args.max_port_monitor_connections = MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
7249 #endif
7250
7251         /* handle command line parameters that don't change configs */
7252 #ifdef X11
7253         char *s, *temp;
7254         unsigned int x;
7255
7256         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && 
7257                      *s) || ((s = getenv("LANG")) && *s)) {
7258                 temp = (char *)malloc((strlen(s) + 1) * sizeof(char));
7259                 if (temp == NULL) {
7260                         ERR("malloc failed");
7261                 }
7262                 for (x = 0; x < strlen(s); x++) {
7263                         temp[x] = tolower(s[x]);
7264                 }
7265                 temp[x] = 0;
7266                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
7267                         utf8_mode = 1;
7268                 }
7269                 
7270                 free(temp);
7271         }
7272         if (!setlocale(LC_CTYPE, "")) {
7273                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
7274         }
7275 #endif /* X11 */
7276         while (1) {
7277                 int c = getopt(argc,
7278                                argv,
7279                                getopt_string);
7280                 if (c == -1)
7281                         break;
7282
7283                 switch (c) {
7284                 case 'v':
7285                 case 'V':
7286                         print_version();
7287                 case 'c':
7288                         /* if current_config is set to a strdup of CONFIG_FILE, free it (even
7289                          * though free() does the NULL check itself;), then load optarg value */
7290                         if (current_config)
7291                                 free(current_config);
7292                         current_config = strdup(optarg);
7293                         break;
7294
7295                 case 'h':
7296                         printf
7297                                         ("Usage: %s [OPTION]...\n"
7298                                         "Conky is a system monitor that renders text on desktop or to own transparent\n"
7299                                         "window. Command line options will override configurations defined in config\n"
7300                                         "file.\n"
7301                                         "   -V            version\n"
7302                                         "   -c FILE       config file to load instead of "
7303                                         CONFIG_FILE
7304                                         "\n"
7305                                         "   -d            daemonize, fork to background\n"
7306                                         "   -h            help\n"
7307 #ifdef X11
7308                                         "   -a ALIGNMENT  text alignment on screen, {top,bottom}_{left,right}\n"
7309                                         "   -f FONT       font to use\n"
7310 #ifdef OWN_WINDOW
7311                                         "   -o            create own window to draw\n"
7312 #endif
7313 #ifdef HAVE_XDBE
7314                                         "   -b            double buffer (prevents flickering)\n"
7315 #endif
7316                                         "   -w WIN_ID     window id to draw\n"
7317                                         "   -x X          x position\n"
7318                                         "   -y Y          y position\n"
7319 #endif /* X11 */
7320                                         "   -t TEXT       text to render, remember single quotes, like -t '$uptime'\n"
7321                                         "   -u SECS       update interval\n"
7322                                         "   -i NUM        number of times to update Conky\n", argv[0]);
7323                         return 0;
7324 #ifdef X11
7325                 case 'w':
7326                         window.window = strtol(optarg, 0, 0);
7327                         break;
7328 #endif /* X11 */
7329
7330                 case '?':
7331                         exit(EXIT_FAILURE);
7332                 }
7333         }
7334 #ifdef X11
7335         /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
7336         init_X11();
7337 #endif /* X11 */
7338
7339         /* load current_config or CONFIG_FILE */
7340
7341 #ifdef CONFIG_FILE
7342         if (current_config == NULL) {
7343                 /* load default config file */
7344                 char buf[256];
7345
7346                 variable_substitute(CONFIG_FILE, buf, 256);
7347
7348                 if (buf[0] != '\0')
7349                         current_config = strdup(buf);
7350         }
7351 #endif
7352
7353         if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
7354                 load_config_file(current_config);
7355         else { 
7356                 set_default_configurations();
7357         }
7358
7359         /* init specials array */
7360         if ((specials = calloc (sizeof(struct special_t), max_specials)) == 0)
7361             ERR("failed to create specials array");
7362
7363 #ifdef MAIL_FILE
7364         if (current_mail_spool == NULL) {
7365                 char buf[256];
7366                 variable_substitute(MAIL_FILE, buf, 256);
7367
7368                 if (buf[0] != '\0')
7369                         current_mail_spool = strdup(buf);
7370         }
7371 #endif
7372
7373         /* handle other command line arguments */
7374
7375 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
7376         optind = optreset = 1;
7377 #else
7378         optind = 0;
7379 #endif
7380
7381 #if defined(__FreeBSD__)
7382         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
7383                         O_RDONLY, "kvm_open")) == NULL)
7384                 CRIT_ERR( "cannot read kvm");
7385 #endif
7386         
7387         while (1) {
7388                 int c = getopt(argc,
7389                                argv,
7390                                getopt_string);
7391                 if (c == -1)
7392                         break;
7393
7394                 switch (c) {
7395                 case 'd':
7396                         fork_to_background = 1;
7397                         break;
7398
7399 #ifdef X11
7400                         case 'f':
7401                         set_first_font(optarg);
7402                         break;
7403                         case 'a':
7404                                 text_alignment = string_to_alignment(optarg);
7405                                 break;
7406
7407 #ifdef OWN_WINDOW
7408                 case 'o':
7409                         own_window = 1;
7410                         break;
7411 #endif
7412 #ifdef HAVE_XDBE
7413                 case 'b':
7414                                 use_xdbe = 1;
7415                         break;
7416 #endif
7417 #endif /* X11 */
7418                 case 't':
7419                         if (text != original_text)
7420                                 free(text);
7421                         text = strdup(optarg);
7422                         convert_escapes(text);
7423                         break;
7424
7425                 case 'u':
7426                         update_interval = strtod(optarg, 0);
7427                         break;
7428
7429                 case 'i':
7430                         total_run_times = strtod(optarg, 0);
7431                         break;
7432 #ifdef X11
7433                 case 'x':
7434                         gap_x = atoi(optarg);
7435                         break;
7436
7437                 case 'y':
7438                         gap_y = atoi(optarg);
7439                         break;
7440 #endif /* X11 */
7441
7442                 case '?':
7443                         exit(EXIT_FAILURE);
7444                 }
7445         }
7446
7447 #ifdef X11
7448         /* load font */
7449         load_fonts();
7450 #endif /* X11 */
7451
7452         /* generate text and get initial size */
7453         extract_variable_text(text);
7454         if (text != original_text) {
7455                 free(text);
7456         }
7457         text = NULL;
7458         /* fork */
7459         if (fork_to_background) {
7460                 int pid = fork();
7461                 switch (pid) {
7462                 case -1:
7463                         ERR("Conky: couldn't fork() to background: %s", strerror(errno));
7464                         break;
7465
7466                 case 0:
7467                         /* child process */
7468                         usleep(25000);
7469                         fprintf(stderr,"\n"); fflush(stderr);
7470                         break;
7471
7472                 default:
7473                         /* parent process */
7474                         fprintf(stderr,"Conky: forked to background, pid is %d\n", pid); fflush(stderr);
7475                         return 0;
7476                 }
7477         }
7478
7479 #ifdef X11
7480         selected_font = 0;
7481         update_text_area();     /* to get initial size of the window */
7482
7483         init_window
7484                 (own_window,
7485                  text_width + border_margin * 2 + 1,
7486                  text_height + border_margin * 2 + 1,
7487                  set_transparent, background_colour, argv, argc);
7488         
7489         selected_font = 0;
7490         update_text_area();     /* to position text/window on screen */
7491 #endif /* X11 */
7492
7493 #ifdef X11
7494 #ifdef OWN_WINDOW
7495         if (own_window && !fixed_pos) {
7496                 XMoveWindow(display, window.window, window.x, window.y);
7497         }
7498         if (own_window) {
7499                 set_transparent_background(window.window);
7500         }
7501 #endif
7502
7503         create_gc();
7504
7505         set_font();
7506         draw_stuff();
7507 #endif /* X11 */
7508
7509
7510         /* Set signal handlers */
7511         act.sa_handler = signal_handler;
7512         sigemptyset(&act.sa_mask);
7513         act.sa_flags = 0;
7514 #ifdef SA_RESTART
7515         act.sa_flags |= SA_RESTART;
7516 #endif
7517
7518         if ( sigaction(SIGINT,&act,&oact) < 0 ||
7519              sigaction(SIGUSR1,&act,&oact) < 0 ||
7520              sigaction(SIGTERM,&act,&oact) < 0 )
7521         {
7522                 ERR("error setting signal handler: %s", strerror(errno) );
7523         }
7524
7525 #ifdef AUDACIOUS
7526         if (create_audacious_thread() !=0) 
7527             CRIT_ERR("unable to create audacious thread!");
7528         timed_thread_register (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
7529 #endif
7530
7531   /*
7532    * ***************
7533    * MAIN CONKY LOOP
7534    * ***************
7535    *
7536    */
7537   main_loop();
7538
7539 #if defined(__FreeBSD__)
7540         kvm_close(kd);
7541 #endif
7542         
7543         return 0;
7544 }
7545
7546 void signal_handler(int sig)
7547 {
7548         /* signal handler is light as a feather, as it should be. 
7549          * we will poll g_signal_pending with each loop of conky
7550          * and do any signal processing there, NOT here. */
7551         g_signal_pending=sig;
7552 }