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