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