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