Add BMPx (http://beep-media-player.org/) support. Bugs and memleaks are here
[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 <string.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <locale.h>
16 #include <signal.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <pthread.h>
21 #include <string.h>
22 #include <limits.h>
23 #if HAVE_DIRENT_H
24 #include <dirent.h>
25 #endif
26 #include <sys/time.h>
27 #ifdef X11
28 #include <X11/Xutil.h>
29 #endif /* X11 */
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #define CONFIG_FILE "$HOME/.conkyrc"
34 #define MAIL_FILE "$MAIL"
35 #define MAX_IF_BLOCK_DEPTH 5
36
37 /* #define SIGNAL_BLOCKING */
38 #undef SIGNAL_BLOCKING
39
40 #ifdef X11
41
42 /* alignments */
43 enum alignment {
44         TOP_LEFT = 1,
45         TOP_RIGHT,
46         BOTTOM_LEFT,
47         BOTTOM_RIGHT,
48         NONE
49 };
50
51
52 /* for fonts */
53 struct font_list {
54
55         char name[TEXT_BUFFER_SIZE];
56         int num;
57         XFontStruct *font;
58
59 #ifdef XFT
60         XftFont *xftfont;
61         int font_alpha;
62 #endif  
63
64 };
65 static int selected_font = 0;
66 static int font_count = -1;
67 struct font_list *fonts = NULL;
68
69 #ifdef XFT
70
71 #define font_height() use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
72 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
73 #define font_ascent() use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent
74 #define font_descent() use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent
75
76 #else
77
78 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
79 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
80 #define font_descent() fonts[selected_font].font->max_bounds.descent
81
82 #endif
83
84 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
85
86
87 static void set_font();
88
89 int addfont(const char *data_in)
90 {
91         if (font_count > MAX_FONTS) {
92                 CRIT_ERR("you don't need that many fonts, sorry.");
93         }
94         font_count++;
95         if (font_count == 0) {
96                 if (fonts != NULL) {
97                         free(fonts);
98                 }
99                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
100                         CRIT_ERR("malloc");
101                 }
102         }
103         fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
104         if (fonts == NULL) {
105                 CRIT_ERR("realloc in addfont");
106         }
107         if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
108                 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
109 #ifdef XFT
110                 fonts[font_count].font_alpha = 0xffff;
111 #endif
112         } else {
113                 CRIT_ERR("Oops...looks like something overflowed in addfont().");
114         }
115         return font_count;
116 }
117
118 void set_first_font(const char *data_in)
119 {
120         if (font_count < 0) {
121                 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
122                         CRIT_ERR("malloc");
123                 }
124                 font_count++;
125         }
126         if (strlen(data_in) > 1) {
127                 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
128 #ifdef XFT
129                 fonts[0].font_alpha = 0xffff;
130 #endif
131         }
132 }
133
134 void free_fonts()
135 {
136         int i;
137         for (i=0;i<=font_count;i++) {
138 #ifdef XFT
139                 if (use_xft) {
140                         XftFontClose(display, fonts[i].xftfont);
141                 } else
142 #endif
143                 {
144                         XFreeFont(display, fonts[i].font);
145                 }
146 }
147         free(fonts);
148         fonts = NULL;
149         font_count = -1;
150         selected_font = 0;
151         set_first_font("6x10");
152 }
153
154
155 static void load_fonts()
156 {
157         int i;
158         for (i=0;i<=font_count;i++) {
159 #ifdef XFT
160         /* load Xft font */
161         if (use_xft) {
162         /*if (fonts[i].xftfont != NULL && selected_font == 0) {
163                         XftFontClose(display, fonts[i].xftfont);
164         }*/
165                 if ((fonts[i].xftfont =
166                         XftFontOpenName(display, screen, fonts[i].name)) != NULL)
167                         continue;
168                 
169                 ERR("can't load Xft font '%s'", fonts[i].name);
170                 if ((fonts[i].xftfont =
171                         XftFontOpenName(display, screen,
172                                         "courier-12")) != NULL)
173                         continue;
174                 
175                 ERR("can't load Xft font '%s'", "courier-12");
176                 
177                 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
178                         CRIT_ERR("can't load font '%s'", "fixed");
179                 }
180                 use_xft = 0;
181                 
182                 continue;
183         }
184 #endif
185         /* load normal font */
186 /*      if (fonts[i].font != NULL)
187                 XFreeFont(display, fonts[i].font);*/
188         
189         if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
190                 ERR("can't load font '%s'", fonts[i].name);
191                 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
192                         CRIT_ERR("can't load font '%s'", "fixed");
193                         printf("loaded fixed?\n");
194                 }
195         }
196         }
197 }
198
199 #endif /* X11 */
200
201 /* default config file */
202 static char *current_config;
203
204 /* set to 1 if you want all text to be in uppercase */
205 static unsigned int stuff_in_upper_case;
206
207 /* Update interval */
208 static double update_interval;
209
210 /* Run how many times? */
211 static unsigned long total_run_times;
212
213 /* fork? */
214 static int fork_to_background;
215
216 static int cpu_avg_samples, net_avg_samples;
217
218 #ifdef X11
219
220 /* Always on bottom */
221 static int on_bottom;
222
223 /* Position on the screen */
224 static int text_alignment;
225 static int gap_x, gap_y;
226
227 /* border */
228 static int draw_borders;
229 static int stippled_borders;
230
231 static int draw_shades, draw_outline;
232
233 static int border_margin, border_width;
234
235 static long default_fg_color, default_bg_color, default_out_color;
236
237 /* create own window or draw stuff to root? */
238 static int set_transparent = 0;
239
240
241 #ifdef OWN_WINDOW
242 static int own_window = 0;
243 static int background_colour = 0;
244 static char wm_class_name[256];
245 /* fixed size/pos is set if wm/user changes them */
246 static int fixed_size = 0, fixed_pos = 0;
247 #endif
248
249 static int minimum_width, minimum_height;
250 static int maximum_width;
251
252 /* UTF-8 */
253 int utf8_mode = 0;
254
255 #endif /* X11 */
256
257 /* no buffers in used memory? */
258 int no_buffers;
259
260 /* pad percentages to decimals? */
261 static int pad_percents = 0;
262
263 #ifdef TCP_PORT_MONITOR
264 tcp_port_monitor_collection_args_t      tcp_port_monitor_collection_args;
265 tcp_port_monitor_args_t                 tcp_port_monitor_args;
266 #endif
267
268 /* Text that is shown */
269 static char original_text[] =
270     "$nodename - $sysname $kernel on $machine\n"
271     "$hr\n"
272     "${color grey}Uptime:$color $uptime\n"
273     "${color grey}Frequency (in MHz):$color $freq\n"
274     "${color grey}Frequency (in GHz):$color $freq_g\n"
275     "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
276     "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
277     "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
278     "${color grey}Processes:$color $processes  ${color grey}Running:$color $running_processes\n"
279     "$hr\n"
280     "${color grey}File systems:\n"
281     " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
282     "${color grey}Networking:\n"
283     " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
284     "${color grey}Temperatures:\n"
285     " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
286     "$hr\n"
287 #ifdef SETI
288     "${color grey}SETI@Home Statistics:\n"
289     "${color grey}Seti Unit Number:$color $seti_credit\n"
290     "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
291 #endif
292 #ifdef MPD
293     "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
294     "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
295 #endif
296     "${color grey}Name          PID     CPU%    MEM%\n"
297     " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
298     " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
299     " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
300     " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
301     "${tail /var/log/Xorg.0.log 3}";
302
303 static char *text = original_text;
304
305 static int total_updates;
306
307 /* if-blocks */
308 static int blockdepth = 0;
309 static int if_jumped = 0;
310 static int blockstart[MAX_IF_BLOCK_DEPTH];
311
312 int check_mount(char *s)
313 {
314 #if defined(__linux__)
315         int ret = 0;
316         FILE *mtab = fopen("/etc/mtab", "r");
317         if (mtab) {
318                 char buf1[256], buf2[128];
319                 while (fgets(buf1, 256, mtab)) {
320                         sscanf(buf1, "%*s %128s", buf2);
321                         if (!strcmp(s, buf2)) {
322                                 ret = 1;
323                                 break;
324                         }
325                 }
326                 fclose(mtab);
327         } else {
328                 ERR("Could not open mtab");
329         }
330         return ret;
331 #elif defined(__FreeBSD__)
332         struct statfs *mntbuf;
333         int i, mntsize;
334
335         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
336         for (i = mntsize - 1; i >= 0; i--)
337                 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
338                         return 1;
339
340         return 0;
341 #endif
342 }
343
344
345 #ifdef X11
346 static inline int calc_text_width(const char *s, int l)
347 {
348 #ifdef XFT
349         if (use_xft) {
350                 XGlyphInfo gi;
351                 if (utf8_mode) {
352                         XftTextExtentsUtf8(display, fonts[selected_font].xftfont, s, l, &gi);
353                 } else {
354                         XftTextExtents8(display, fonts[selected_font].xftfont, s, l, &gi);
355                 }
356                 return gi.xOff;
357         } else
358 #endif
359         {
360                 return XTextWidth(fonts[selected_font].font, s, l);
361         }
362 }
363 #endif /* X11 */
364
365 /* formatted text to render on screen, generated in generate_text(),
366  * drawn in draw_stuff() */
367
368 static char text_buffer[TEXT_BUFFER_SIZE * 4];
369
370 /* special stuff in text_buffer */
371
372 #define SPECIAL_CHAR '\x01'
373
374 enum {
375         HORIZONTAL_LINE,
376         STIPPLED_HR,
377         BAR,
378         FG,
379         BG,
380         OUTLINE,
381         ALIGNR,
382         ALIGNC,
383         GRAPH,
384         OFFSET,
385         VOFFSET,
386         FONT,
387 };
388
389 static struct special_t {
390         int type;
391         short height;
392         short width;
393         long arg;
394         double *graph;
395         double graph_scale;
396         int graph_width;
397         int scaled;
398         short font_added;
399         unsigned long first_colour; // for graph gradient
400         unsigned long last_colour;
401 } specials[128];
402
403 static int special_count;
404 #ifdef X11
405 static int special_index;       /* used when drawing */
406 #endif /* X11 */
407
408 #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? */
409
410 static struct special_t *new_special(char *buf, int t)
411 {
412         if (special_count >= 128)
413                 CRIT_ERR("too many special things in text");
414
415         buf[0] = SPECIAL_CHAR;
416         buf[1] = '\0';
417         specials[special_count].type = t;
418         return &specials[special_count++];
419 }
420
421 typedef struct tailstring_list {
422         char data[TEXT_BUFFER_SIZE];
423         struct tailstring_list *next;
424         struct tailstring_list *first;
425 } tailstring;
426
427 void addtail(tailstring ** head, char *data_in)
428 {
429         tailstring *tmp;
430         if ((tmp = malloc(sizeof(*tmp))) == NULL) {
431                 CRIT_ERR("malloc");
432         }
433         if (*head == NULL) {
434                 tmp->first = tmp;
435         } else {
436                 tmp->first = (*head)->first;
437         }
438         strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
439         tmp->next = *head;
440         *head = tmp;
441 }
442
443 void freetail(tailstring * head)
444 {
445         tailstring *tmp;
446         while (head != NULL) {
447                 tmp = head->next;
448                 free(head);
449                 head = tmp;
450         }
451 }
452
453 void freelasttail(tailstring * head)
454 {
455         tailstring * tmp = head;
456         while(tmp != NULL) {
457                 if (tmp->next == head->first) {
458                         tmp->next = NULL;
459                         break;
460                 }
461                 tmp = tmp->next;
462         }
463         free(head->first);
464         while(head != NULL && tmp != NULL) {
465                 head->first = tmp;
466                 head = head->next;
467         }
468 }
469
470 static void new_bar(char *buf, int w, int h, int usage)
471 {
472         struct special_t *s = new_special(buf, BAR);
473         s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
474         s->width = w;
475         s->height = h;
476 }
477
478 static const char *scan_bar(const char *args, int *w, int *h)
479 {
480         *w = 0;                 /* zero width means all space that is available */
481         *h = 6;
482         /* bar's argument is either height or height,width */
483         if (args) {
484                 int n = 0;
485                 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
486                         sscanf(args, "%d %n", h, &n);
487                 args += n;
488         }
489
490         return args;
491 }
492
493 static char *scan_font(const char *args)
494 {
495         if (args && sizeof(args) < 127) {
496                 return strdup(args);
497         }
498         else {
499                 ERR("font scan failed, lets hope it doesn't mess stuff up");
500         }
501         return NULL;
502 }
503
504 #ifdef X11
505 static void new_font(char *buf, char * args) {
506         struct special_t *s = new_special(buf, FONT);
507         if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
508                 int tmp = selected_font;
509                 selected_font = s->font_added = addfont(args);
510                 load_fonts();
511 //              set_font();
512                 selected_font = tmp;
513         }
514 }
515 #endif
516
517 inline void graph_append(struct special_t *graph, double f)
518 {
519         if (!graph->scaled && f > graph->graph_scale) {
520                 f = graph->graph_scale;
521         }
522         int i;
523         if (graph->scaled) {
524                 graph->graph_scale = 1;
525         }
526         graph->graph[graph->graph_width - 1] = f; /* add new data */
527         for (i = 0; i < graph->graph_width - 1; i++) { /* shift all the data by 1 */
528                 graph->graph[i] = graph->graph[i + 1];
529                 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
530                         graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
531                 }
532         }
533 }
534
535 short colour_depth = 0;
536 void set_up_gradient();
537
538 /* precalculated: 31/255, and 63/255 */
539 #define CONST_8_TO_5_BITS 0.12156862745098
540 #define CONST_8_TO_6_BITS 0.247058823529412
541
542 /* adjust color values depending on color depth*/
543 static unsigned int adjust_colors(unsigned int color)
544 {
545         double r, g, b;
546         if (colour_depth == 0) {
547                 set_up_gradient();
548         }
549         if (colour_depth == 16) {
550                 r = (color & 0xff0000) >> 16;
551                 g = (color & 0xff00) >> 8;
552                 b =  color & 0xff;
553                 color  = (int)(r * CONST_8_TO_5_BITS) << 11;
554                 color |= (int)(g * CONST_8_TO_6_BITS) << 5;
555                 color |= (int)(b * CONST_8_TO_5_BITS);
556         }
557         return color;
558 }
559
560 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scale, int append)
561 {
562         struct special_t *s = new_special(buf, GRAPH);
563         s->width = w;
564         if (s->graph == NULL) {
565                 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
566                         s->graph_width = s->width - 3;  // subtract 3 for the box
567                 } else {
568                         s->graph_width = MAX_GRAPH_DEPTH - 3;
569                 }
570                 s->graph = malloc(s->graph_width * sizeof(double));
571                 memset(s->graph, 0, s->graph_width * sizeof(double));
572                 s->graph_scale = 100;
573         }
574         s->height = h;
575         s->first_colour = adjust_colors(first_colour);
576         s->last_colour = adjust_colors(second_colour);
577         if (scale != 0) {
578                 s->scaled = 0;
579         } else {
580                 s->scaled = 1;
581         }
582         /*if (s->width) {
583                 s->graph_width = s->width - 3;  // subtract 3 for rectangle around
584         }*/
585         if (s->scaled) {
586                 s->graph_scale = 1;
587         } else {
588                 s->graph_scale = scale;
589         }
590         if (append) {
591                 graph_append(s, i);
592         }
593 }
594
595 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
596 {
597         *w = 0;                 /* zero width means all space that is available */
598         *h = 25;
599         *first_colour = 0;
600         *last_colour = 0;
601         /* graph's argument is either height or height,width */
602         if (args) {
603                 if (sscanf(args, "%*s %d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
604                         if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
605                                 *scale = 0;
606                                 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
607                                         *scale = 0;
608                                         if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
609                                                 *w = 0;
610                                 *h = 25;                        
611                                 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
612                                 *w = 0;
613                                 *h = 25;
614                                 *scale = 0;
615                                 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
616                                         *w = 0;
617                                         *h = 25;
618                                         if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
619                                                 *first_colour = 0;
620                                                 *last_colour = 0;
621                                                 *scale = 0;
622                                                 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
623                                         *first_colour = 0;
624                                         *last_colour = 0;
625                                         if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
626                                                 *first_colour = 0;
627                                                 *last_colour = 0;
628                                                 *scale = 0;
629                                                 if (sscanf(args, "%d,%d", h, w) < 2) {
630                                                         *first_colour = 0;
631                                                         *last_colour = 0;
632                                                         sscanf(args, "%*s %d,%d", h, w);
633         }}}}}}}}}}} // haha
634         return args;
635 }
636
637
638 static inline void new_hr(char *buf, int a)
639 {
640         new_special(buf, HORIZONTAL_LINE)->height = a;
641 }
642
643 static inline void new_stippled_hr(char *buf, int a, int b)
644 {
645         struct special_t *s = new_special(buf, STIPPLED_HR);
646         s->height = b;
647         s->arg = a;
648 }
649
650 static inline void new_fg(char *buf, long c)
651 {
652         new_special(buf, FG)->arg = c;
653 }
654
655 static inline void new_bg(char *buf, long c)
656 {
657         new_special(buf, BG)->arg = c;
658 }
659
660 static inline void new_outline(char *buf, long c)
661 {
662         new_special(buf, OUTLINE)->arg = c;
663 }
664
665 static inline void new_offset(char *buf, long c)
666 {
667         new_special(buf, OFFSET)->arg = c;
668 }
669
670 static inline void new_voffset(char *buf, long c)
671 {
672         new_special(buf, VOFFSET)->arg = c;
673 }
674
675 static inline void new_alignr(char *buf, long c)
676 {
677         new_special(buf, ALIGNR)->arg = c;
678 }
679
680 static inline void new_alignc(char *buf, long c)
681 {
682         new_special(buf, ALIGNC)->arg = c;
683 }
684
685 /* quite boring functions */
686
687 static inline void for_each_line(char *b, void (*f) (char *))
688 {
689         char *ps, *pe;
690
691         for (ps = b, pe = b; *pe; pe++) {
692                 if (*pe == '\n') {
693                         *pe = '\0';
694                         f(ps);
695                         *pe = '\n';
696                         ps = pe + 1;
697                 }
698         }
699
700         if (ps < pe)
701                 f(ps);
702 }
703
704 static void convert_escapes(char *buf)
705 {
706         char *p = buf, *s = buf;
707
708         while (*s) {
709                 if (*s == '\\') {
710                         s++;
711                         if (*s == 'n')
712                                 *p++ = '\n';
713                         else if (*s == '\\')
714                                 *p++ = '\\';
715                         s++;
716                 } else
717                         *p++ = *s++;
718         }
719         *p = '\0';
720 }
721
722 /* converts from bytes to human readable format (k, M, G, T) */
723 static void human_readable(long long a, char *buf, int size)
724 {
725         // Strange conditional due to possible overflows
726         if(a / 1024 / 1024 / 1024.0 > 1024.0){
727                 snprintf(buf, size, "%.2fT", (a / 1024 / 1024 / 1024) / 1024.0);
728         }
729         else if (a >= 1024 * 1024 * 1024) {
730                 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
731         }
732         else if (a >= 1024 * 1024) {
733                 double m = (a / 1024) / 1024.0;
734                 if (m >= 100.0)
735                         snprintf(buf, size, "%.0fM", m);
736                 else
737                         snprintf(buf, size, "%.1fM", m);
738         } else if (a >= 1024)
739                 snprintf(buf, size, "%Ldk", a / (long long) 1024);
740         else
741                 snprintf(buf, size, "%Ld", a);
742 }
743
744 /* text handling */
745
746 enum text_object_type {
747         OBJ_acpiacadapter,
748         OBJ_adt746xcpu,
749         OBJ_adt746xfan,
750         OBJ_acpifan,
751         OBJ_addr,
752         OBJ_linkstatus,
753         OBJ_acpitemp,
754         OBJ_acpitempf,
755         OBJ_battery,
756         OBJ_buffers,
757         OBJ_cached,
758         OBJ_color,
759         OBJ_font,
760         OBJ_cpu,
761         OBJ_cpubar,
762         OBJ_cpugraph,
763         OBJ_diskio,
764         OBJ_diskiograph,
765         OBJ_downspeed,
766         OBJ_downspeedf,
767         OBJ_downspeedgraph,
768         OBJ_else,
769         OBJ_endif,
770         OBJ_exec,
771         OBJ_execi,
772         OBJ_texeci,
773         OBJ_execbar,
774         OBJ_execgraph,
775         OBJ_execibar,
776         OBJ_execigraph,
777         OBJ_freq,
778         OBJ_freq_g,
779         OBJ_freq_dyn,
780         OBJ_freq_dyn_g,
781         OBJ_fs_bar,
782         OBJ_fs_bar_free,
783         OBJ_fs_free,
784         OBJ_fs_free_perc,
785         OBJ_fs_size,
786         OBJ_fs_used,
787         OBJ_fs_used_perc,
788         OBJ_hr,
789         OBJ_offset,
790         OBJ_voffset,
791         OBJ_alignr,
792         OBJ_alignc,
793         OBJ_i2c,
794 #if defined(__linux__)
795         OBJ_i8k_version,
796         OBJ_i8k_bios,
797         OBJ_i8k_serial,
798         OBJ_i8k_cpu_temp,
799         OBJ_i8k_cpu_tempf,
800         OBJ_i8k_left_fan_status,
801         OBJ_i8k_right_fan_status,
802         OBJ_i8k_left_fan_rpm,
803         OBJ_i8k_right_fan_rpm,
804         OBJ_i8k_ac_status,      
805         OBJ_i8k_buttons_status,
806 #endif /* __linux__ */
807         OBJ_if_existing,
808         OBJ_if_mounted,
809         OBJ_if_running,
810         OBJ_top,
811         OBJ_top_mem,
812         OBJ_tail,
813         OBJ_head,
814         OBJ_kernel,
815         OBJ_loadavg,
816         OBJ_machine,
817         OBJ_mails,
818         OBJ_mem,
819         OBJ_membar,
820         OBJ_memgraph,
821         OBJ_memmax,
822         OBJ_memperc,
823         OBJ_mixer,
824         OBJ_mixerl,
825         OBJ_mixerr,
826         OBJ_mixerbar,
827         OBJ_mixerlbar,
828         OBJ_mixerrbar,
829         OBJ_new_mails,
830         OBJ_nodename,
831         OBJ_pre_exec,
832 #ifdef MLDONKEY
833         OBJ_ml_upload_counter,
834         OBJ_ml_download_counter,
835         OBJ_ml_nshared_files,
836         OBJ_ml_shared_counter,
837         OBJ_ml_tcp_upload_rate,
838         OBJ_ml_tcp_download_rate,
839         OBJ_ml_udp_upload_rate,
840         OBJ_ml_udp_download_rate,
841         OBJ_ml_ndownloaded_files,
842         OBJ_ml_ndownloading_files,
843 #endif
844         OBJ_processes,
845         OBJ_running_processes,
846         OBJ_shadecolor,
847         OBJ_outlinecolor,
848         OBJ_stippled_hr,
849         OBJ_swap,
850         OBJ_swapbar,
851         OBJ_swapmax,
852         OBJ_swapperc,
853         OBJ_sysname,
854         OBJ_temp1,              /* i2c is used instead in these */
855         OBJ_temp2,
856         OBJ_text,
857         OBJ_time,
858         OBJ_utime,
859         OBJ_totaldown,
860         OBJ_totalup,
861         OBJ_updates,
862         OBJ_upspeed,
863         OBJ_upspeedf,
864         OBJ_upspeedgraph,
865         OBJ_uptime,
866         OBJ_uptime_short,
867 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
868         OBJ_apm_adapter,
869         OBJ_apm_battery_time,
870         OBJ_apm_battery_life,
871 #endif /* __FreeBSD__ */
872 #ifdef SETI
873         OBJ_seti_prog,
874         OBJ_seti_progbar,
875         OBJ_seti_credit,
876 #endif
877 #ifdef MPD
878         OBJ_mpd_title,
879         OBJ_mpd_artist,
880         OBJ_mpd_album,
881         OBJ_mpd_random,
882         OBJ_mpd_repeat,
883         OBJ_mpd_vol,
884         OBJ_mpd_bitrate,
885         OBJ_mpd_status,
886         OBJ_mpd_host,
887         OBJ_mpd_port,
888         OBJ_mpd_bar,
889         OBJ_mpd_elapsed,
890         OBJ_mpd_length,
891         OBJ_mpd_track,
892         OBJ_mpd_percent,
893 #endif
894 #ifdef BMPX
895         OBJ_bmpx_title,
896         OBJ_bmpx_artist,
897         OBJ_bmpx_album,
898         OBJ_bmpx_track,
899         OBJ_bmpx_uri,
900         OBJ_bmpx_bitrate,
901 #endif
902 #ifdef TCP_PORT_MONITOR
903         OBJ_tcp_portmon,
904 #endif
905 };
906
907 struct text_object {
908         int type;
909         int a, b;
910         unsigned int c, d, e;
911         float f;
912         union {
913                 char *s;        /* some string */
914                 int i;          /* some integer */
915                 long l;         /* some other integer */
916                 struct net_stat *net;
917                 struct fs_stat *fs;
918                 unsigned char loadavg[3];
919                 //unsigned int diskio;
920                 unsigned int cpu_index;
921                 struct {
922                         struct fs_stat *fs;
923                         int w, h;
924                 } fsbar;        /* 3 */
925
926                 struct {
927                         int l;
928                         int w, h;
929                 } mixerbar;     /* 3 */
930
931                 struct {
932                         int fd;
933                         int arg;
934                         char devtype[256];
935                         char type[64];
936                 } i2c;          /* 2 */
937                 struct {
938                         int pos;
939                         char *s;
940                 } ifblock;
941                 struct {
942                         int num;
943                         int type;
944                 } top;
945
946                 struct {
947                         int wantedlines;
948                         int readlines;
949                         char *logfile;
950                         double last_update;
951                         float interval;
952                         char *buffer;
953                 } tail;
954
955                 struct {
956                         double last_update;
957                         float interval;
958                         char *cmd;
959                         char *buffer;
960                         double data;
961                 } execi;        /* 5 */
962
963                 struct {
964                         int a, b;
965                 } pair;         /* 2 */
966 #ifdef TCP_PORT_MONITOR
967                 struct {
968                         in_port_t  port_range_begin;  /* starting port to monitor */
969                         in_port_t  port_range_end;    /* ending port to monitor */
970                         int        item;              /* enum value from libtcp-portmon.h, e.g. COUNT, REMOTEIP, etc. */
971                         int        connection_index;  /* 0 to n-1 connections. */
972                } tcp_port_monitor;
973 #endif
974         } data;
975 };
976
977 struct text_object_list {
978   unsigned int text_object_count;
979   struct text_object *text_objects;
980 };
981
982 static unsigned int text_object_count;
983 static struct text_object *text_objects;
984 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
985
986 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
987
988 void *threaded_exec( struct text_object *obj ) {
989         char *p2 = obj->data.execi.buffer;
990         FILE *fp = popen(obj->data.execi.cmd,"r");
991         pthread_mutex_lock( &mutex1 );
992         int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
993         (void) pclose(fp);      
994         p2[n2] = '\0';
995         if (n2 && p2[n2 - 1] == '\n')
996                 p2[n2 - 1] = '\0';
997         
998         while (*p2) {
999                 if (*p2 == '\001')
1000                         *p2 = ' ';
1001                 p2++;
1002         }
1003         pthread_mutex_unlock( &mutex1 );
1004         return NULL;
1005 }
1006
1007 static struct text_object *new_text_object_internal()
1008 {
1009   struct text_object *obj = malloc(sizeof(struct text_object));
1010   memset(obj, 0, sizeof(struct text_object));    
1011   return obj;
1012 }
1013
1014 #ifdef MLDONKEY
1015 void ml_cleanup()
1016 {
1017         if (mlconfig.mldonkey_hostname) {
1018                 free(mlconfig.mldonkey_hostname);
1019                 mlconfig.mldonkey_hostname = NULL;
1020         }
1021 }
1022 #endif
1023
1024 static void free_text_objects(unsigned int count, struct text_object *objs)
1025 {
1026         unsigned int i;
1027         for (i = 0; i < count; i++) {
1028             switch (objs[i].type) {
1029                 case OBJ_acpitemp:
1030                         close(objs[i].data.i);
1031                         break;
1032                 case OBJ_acpitempf:
1033                         close(objs[i].data.i);
1034                         break;
1035                 case OBJ_i2c:
1036                         close(objs[i].data.i2c.fd);
1037                         break;
1038                 case OBJ_time:
1039                         free(objs[i].data.s);
1040                         break;
1041                 case OBJ_utime:
1042                 case OBJ_if_existing:
1043                 case OBJ_if_mounted:
1044                 case OBJ_if_running:
1045                         free(objs[i].data.ifblock.s);
1046                         break;
1047                 case OBJ_tail:
1048                         free(objs[i].data.tail.logfile);
1049                         free(objs[i].data.tail.buffer);
1050                         break;
1051                 case OBJ_text: case OBJ_font:
1052                         free(objs[i].data.s);
1053                         break;
1054                 case OBJ_exec:
1055                         free(objs[i].data.s);
1056                         break;
1057                 case OBJ_execbar:
1058                         free(objs[i].data.s);
1059                         break;
1060                 case OBJ_execgraph:
1061                         free(objs[i].data.s);
1062                         break;
1063 /*              case OBJ_execibar:
1064                         free(objs[i].data.s);
1065                         break;
1066                 case OBJ_execigraph:
1067                         free(objs[i].data.s);
1068                         break;*/
1069 #ifdef MPD
1070                 case OBJ_mpd_title:
1071                 case OBJ_mpd_artist:
1072                 case OBJ_mpd_album:
1073                 case OBJ_mpd_random:
1074                 case OBJ_mpd_repeat:
1075                 case OBJ_mpd_track:
1076                 case OBJ_mpd_status:
1077                 case OBJ_mpd_host:
1078 #endif
1079 #ifdef BMPX
1080                 case OBJ_bmpx_title:
1081                 case OBJ_bmpx_artist:
1082                 case OBJ_bmpx_album:
1083                 case OBJ_bmpx_track:
1084                 case OBJ_bmpx_uri:
1085                 case OBJ_bmpx_bitrate:
1086 #endif
1087                 case OBJ_pre_exec:
1088                 case OBJ_battery:
1089                         free(objs[i].data.s);
1090                         break;
1091
1092                 case OBJ_execi:
1093                         free(objs[i].data.execi.cmd);
1094                         free(objs[i].data.execi.buffer);
1095                         break;
1096                 case OBJ_texeci:
1097                         free(objs[i].data.execi.cmd);
1098                         free(objs[i].data.execi.buffer);
1099                         break;
1100                 case OBJ_top:
1101                         if (info.first_process) {
1102                                 free_all_processes();
1103                                 info.first_process = NULL;
1104                         }
1105                         break;
1106                 case OBJ_top_mem:
1107                         if (info.first_process) {
1108                                 free_all_processes();
1109                                 info.first_process = NULL;
1110                         }
1111                         break;
1112                 }
1113         }
1114         free(objs);
1115         //text_objects = NULL;
1116         //text_object_count = 0;
1117 }
1118
1119 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1120 {
1121         char buf1[64];
1122         int n;
1123
1124         if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1125                 *a = mixer_init(buf1);
1126                 (void) scan_bar(arg + n, w, h);
1127         } else {
1128                 *a = mixer_init(0);
1129                 (void) scan_bar(arg, w, h);
1130         }
1131 }
1132
1133
1134 /* construct_text_object() creates a new text_object */
1135 static struct text_object *construct_text_object(const char *s, const char *arg)
1136 {
1137     //struct text_object *obj = new_text_object();
1138     struct text_object *obj = new_text_object_internal();
1139
1140 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
1141 #define END ; } } else
1142
1143 #ifdef X11      
1144 if (s[0] == '#') {
1145                 obj->type = OBJ_color;
1146                 obj->data.l = get_x11_color(s);
1147         } else
1148 #endif /* X11 */
1149         OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
1150         END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
1151         END OBJ(acpiacadapter, 0)
1152         END OBJ(freq, 0);
1153         END OBJ(freq_g, 0);
1154         END OBJ(freq_dyn, 0);
1155         END OBJ(freq_dyn_g, 0);
1156         END OBJ(acpifan, 0);
1157         END OBJ(battery, 0);
1158         char bat[64];
1159         if (arg)
1160                 sscanf(arg, "%63s", bat);
1161         else
1162                 strcpy(bat, "BAT0");
1163         obj->data.s = strdup(bat);
1164 #if defined(__linux__)
1165         END OBJ(i8k_version, INFO_I8K)
1166         END OBJ(i8k_bios, INFO_I8K)
1167         END OBJ(i8k_serial, INFO_I8K)
1168         END OBJ(i8k_cpu_temp, INFO_I8K)
1169         END OBJ(i8k_cpu_tempf, INFO_I8K)
1170         END OBJ(i8k_left_fan_status, INFO_I8K)  
1171         END OBJ(i8k_right_fan_status, INFO_I8K)
1172         END OBJ(i8k_left_fan_rpm, INFO_I8K)
1173         END OBJ(i8k_right_fan_rpm, INFO_I8K)
1174         END OBJ(i8k_ac_status, INFO_I8K)
1175         END OBJ(i8k_buttons_status, INFO_I8K)
1176 #endif /* __linux__ */
1177         END OBJ(buffers, INFO_BUFFERS)
1178         END OBJ(cached, INFO_BUFFERS)
1179         END OBJ(cpu, INFO_CPU)
1180                 if (arg) {
1181                 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1182                         obj->data.cpu_index = atoi(&arg[3]);
1183                         arg += 4;
1184                 } else {obj->data.cpu_index = 0; }
1185                 } else {
1186                                 obj->data.cpu_index = 0;
1187                         }
1188         END OBJ(cpubar, INFO_CPU)
1189                 if (arg) {
1190                 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1191                         obj->data.cpu_index = atoi(&arg[3]);
1192                         arg += 4;
1193                 }
1194                 else {obj->data.cpu_index = 0;}
1195                 (void) scan_bar(arg, &obj->a, &obj->b);
1196                 } else {
1197                                 (void) scan_bar(arg, &obj->a, &obj->b);
1198                                 obj->data.cpu_index = 0;
1199                         }
1200         END OBJ(cpugraph, INFO_CPU)
1201                         if (arg) {
1202                 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1203                         obj->data.cpu_index = atoi(&arg[3]);
1204                         arg += 4;
1205                 }
1206                                 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1207                         } else {
1208         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1209         obj->data.cpu_index = 0;
1210                         }
1211         END OBJ(diskio, INFO_DISKIO)
1212         END OBJ(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1213         END OBJ(color, 0) 
1214 #ifdef X11
1215                         obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1216 #endif /* X11 */
1217         END
1218         OBJ(font, 0)
1219                         obj->data.s = scan_font(arg);
1220         END
1221                 OBJ(downspeed, INFO_NET) 
1222                 if(arg) {
1223                         obj->data.net = get_net_stat(arg);
1224                 }
1225                 else {
1226                         CRIT_ERR("downspeed needs argument");
1227                 }
1228         END OBJ(downspeedf, INFO_NET)
1229                 if(arg) {
1230                         obj->data.net = get_net_stat(arg);
1231                 }
1232                 else {
1233                         CRIT_ERR("downspeedf needs argument");
1234                 }
1235         END OBJ(downspeedgraph, INFO_NET)
1236                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1237         char buf[64];
1238         sscanf(arg, "%63s %*i,%*i %*i", buf);
1239         obj->data.net = get_net_stat(buf);
1240         if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1241                 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
1242                         obj->a = 0;
1243                         obj->b = 25;
1244                 }
1245         }
1246         END OBJ(
1247                        else
1248                        , 0)
1249         if (blockdepth) {
1250                 text_objects[blockstart[blockdepth - 1] -
1251                              1].data.ifblock.pos = text_object_count;
1252                 blockstart[blockdepth - 1] = text_object_count;
1253                 obj->data.ifblock.pos = text_object_count + 2;
1254         } else {
1255                 ERR("$else: no matching $if_*");
1256         }
1257         END OBJ(endif, 0)
1258         if (blockdepth) {
1259                 blockdepth--;
1260                 text_objects[blockstart[blockdepth] - 1].data.ifblock.pos =
1261                     text_object_count;
1262         } else {
1263                 ERR("$endif: no matching $if_*");
1264         }
1265         END
1266 #ifdef HAVE_POPEN
1267             OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
1268         END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
1269         END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
1270         END OBJ(execibar, 0) unsigned int n;
1271         if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1272                 char buf[256];
1273                 ERR("${execibar <interval> command}");
1274                 obj->type = OBJ_text;
1275                 snprintf(buf, 256, "${%s}", s);
1276                 obj->data.s = strdup(buf);
1277                 } else {
1278                         obj->data.execi.cmd = strdup(arg + n);
1279                     }
1280         END OBJ(execigraph, 0) unsigned int n;
1281         if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1282                 char buf[256];
1283                 ERR("${execigraph <interval> command}");
1284                 obj->type = OBJ_text;
1285                 snprintf(buf, 256, "${%s}", s);
1286                 obj->data.s = strdup(buf);
1287         } else {
1288                 obj->data.execi.cmd = strdup(arg + n);
1289         }
1290         END OBJ(execi, 0) unsigned int n;
1291
1292         if (!arg
1293             || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1294                 char buf[256];
1295                 ERR("${execi <interval> command}");
1296                 obj->type = OBJ_text;
1297                 snprintf(buf, 256, "${%s}", s);
1298                 obj->data.s = strdup(buf);
1299         } else {
1300                 obj->data.execi.cmd = strdup(arg + n);
1301                 obj->data.execi.buffer =
1302                     (char *) calloc(1, TEXT_BUFFER_SIZE);
1303         }
1304         END OBJ(texeci, 0) unsigned int n;
1305
1306         if (!arg
1307                     || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1308                 char buf[256];
1309                 ERR("${texeci <interval> command}");
1310                 obj->type = OBJ_text;
1311                 snprintf(buf, 256, "${%s}", s);
1312                 obj->data.s = strdup(buf);
1313                     } else {
1314                             obj->data.execi.cmd = strdup(arg + n);
1315                             obj->data.execi.buffer =
1316                                             (char *) calloc(1, TEXT_BUFFER_SIZE);
1317                     }
1318         END OBJ(pre_exec, 0) obj->type = OBJ_text;
1319         if (arg) {
1320                 FILE *fp = popen(arg, "r");
1321                 unsigned int n;
1322                 char buf[2048];
1323
1324                 n = fread(buf, 1, 2048, fp);
1325                 buf[n] = '\0';
1326
1327                 if (n && buf[n - 1] == '\n')
1328                         buf[n - 1] = '\0';
1329
1330                 (void) pclose(fp);
1331
1332                 obj->data.s = strdup(buf);
1333         } else
1334                 obj->data.s = strdup("");
1335         END
1336 #endif
1337             OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
1338         arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1339         if (arg) {
1340                 while (isspace(*arg))
1341                         arg++;
1342                 if (*arg == '\0')
1343                         arg = "/";
1344         } else
1345                 arg = "/";
1346         obj->data.fsbar.fs = prepare_fs_stat(arg);
1347         END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
1348         if (arg) {
1349                 unsigned int n;
1350                 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
1351                         arg += n;
1352         } else
1353                 arg = "/";
1354         obj->data.fsbar.fs = prepare_fs_stat(arg);
1355         END OBJ(fs_free, INFO_FS) if (!arg)
1356                  arg = "/";
1357         obj->data.fs = prepare_fs_stat(arg);
1358         END OBJ(fs_used_perc, INFO_FS) if (!arg)
1359                  arg = "/";
1360         obj->data.fs = prepare_fs_stat(arg);
1361         END OBJ(fs_free_perc, INFO_FS) if (!arg)
1362                  arg = "/";
1363         obj->data.fs = prepare_fs_stat(arg);
1364         END OBJ(fs_size, INFO_FS) if (!arg)
1365                  arg = "/";
1366         obj->data.fs = prepare_fs_stat(arg);
1367         END OBJ(fs_used, INFO_FS) if (!arg)
1368                  arg = "/";
1369         obj->data.fs = prepare_fs_stat(arg);
1370         END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
1371         END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
1372         END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
1373         END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
1374         int n;
1375
1376         if (!arg) {
1377                 ERR("i2c needs arguments");
1378                 obj->type = OBJ_text;
1379                 //obj->data.s = strdup("${i2c}");
1380                 return NULL;
1381         }
1382
1383         if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1384                 /* if scanf couldn't read three values, read type and num and use
1385                  * default device */
1386                 sscanf(arg, "%63s %d", buf2, &n);
1387                 obj->data.i2c.fd =
1388                     open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
1389                                     obj->data.i2c.devtype);
1390                 strncpy(obj->data.i2c.type, buf2, 63);
1391         } else {
1392                 obj->data.i2c.fd =
1393                     open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
1394                                     obj->data.i2c.devtype);
1395                 strncpy(obj->data.i2c.type, buf2, 63);
1396         }
1397
1398         END OBJ(top, INFO_TOP)
1399         char buf[64];
1400         int n;
1401         if (!arg) {
1402                 ERR("top needs arguments");
1403                 obj->type = OBJ_text;
1404                 //obj->data.s = strdup("${top}");
1405                 return NULL;
1406         }
1407         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1408                 if (strcmp(buf, "name") == 0) {
1409                         obj->data.top.type = TOP_NAME;
1410                 } else if (strcmp(buf, "cpu") == 0) {
1411                         obj->data.top.type = TOP_CPU;
1412                 } else if (strcmp(buf, "pid") == 0) {
1413                         obj->data.top.type = TOP_PID;
1414                 } else if (strcmp(buf, "mem") == 0) {
1415                         obj->data.top.type = TOP_MEM;
1416                 } else {
1417                         ERR("invalid arg for top");
1418                         return NULL;
1419                 }
1420                 if (n < 1 || n > 10) {
1421                         CRIT_ERR("invalid arg for top");
1422                         return NULL;
1423                 } else {
1424                         obj->data.top.num = n - 1;
1425                         top_cpu = 1;
1426                 }
1427         } else {
1428                 ERR("invalid args given for top");
1429                 return NULL;
1430         }
1431         END OBJ(top_mem, INFO_TOP)
1432         char buf[64];
1433         int n;
1434         if (!arg) {
1435                 ERR("top_mem needs arguments");
1436                 obj->type = OBJ_text;
1437                 obj->data.s = strdup("${top_mem}");
1438                 return NULL;
1439         }
1440         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1441                 if (strcmp(buf, "name") == 0) {
1442                         obj->data.top.type = TOP_NAME;
1443                 } else if (strcmp(buf, "cpu") == 0) {
1444                         obj->data.top.type = TOP_CPU;
1445                 } else if (strcmp(buf, "pid") == 0) {
1446                         obj->data.top.type = TOP_PID;
1447                 } else if (strcmp(buf, "mem") == 0) {
1448                         obj->data.top.type = TOP_MEM;
1449                 } else {
1450                         ERR("invalid arg for top");
1451                         return NULL;
1452                 }
1453                 if (n < 1 || n > 10) {
1454                         CRIT_ERR("invalid arg for top");
1455                         return NULL;
1456                 } else {
1457                         obj->data.top.num = n - 1;
1458                         top_mem = 1;
1459                 }
1460         } else {
1461                 ERR("invalid args given for top");
1462                 return NULL;
1463         }
1464         END OBJ(addr, INFO_NET)
1465                 if(arg) {
1466                         obj->data.net = get_net_stat(arg);
1467                 }
1468                 else {
1469                         CRIT_ERR("addr needs argument");
1470                 }
1471         END OBJ(linkstatus, INFO_WIFI) 
1472                 if(arg) {
1473                         obj->data.net = get_net_stat(arg);
1474                 }
1475                 else {
1476                         CRIT_ERR("linkstatus needs argument");
1477                 }
1478         END OBJ(tail, 0)
1479         char buf[64];
1480         int n1, n2;
1481         if (!arg) {
1482                 ERR("tail needs arguments");
1483                 obj->type = OBJ_text;
1484                 obj->data.s = strdup("${tail}");
1485                 return NULL;
1486         }
1487         if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1488                 if (n1 < 1 || n1 > 30) {
1489                         CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
1490                         return NULL;
1491                 } else {
1492                         FILE *fp = NULL;
1493                         fp = fopen(buf, "r");
1494                         if (fp) {
1495                                 obj->data.tail.logfile =
1496                                     malloc(TEXT_BUFFER_SIZE);
1497                                 strcpy(obj->data.tail.logfile, buf);
1498                                 obj->data.tail.wantedlines = n1 - 1;
1499                                 obj->data.tail.interval =
1500                                     update_interval * 2;
1501                                 fclose(fp);
1502                         } else {
1503                                 //fclose (fp);
1504                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1505                         }
1506                 }
1507         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1508                 if (n1 < 1 || n1 > 30) {
1509                         CRIT_ERR
1510                             ("invalid arg for tail, number of lines must be between 1 and 30");
1511                         return NULL;
1512                 } else if (n2 < 1 || n2 < update_interval) {
1513                         CRIT_ERR
1514                             ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
1515                         return NULL;
1516                 } else {
1517                         FILE *fp;
1518                         fp = fopen(buf, "r");
1519                         if (fp != NULL) {
1520                                 obj->data.tail.logfile =
1521                                     malloc(TEXT_BUFFER_SIZE);
1522                                 strcpy(obj->data.tail.logfile, buf);
1523                                 obj->data.tail.wantedlines = n1 - 1;
1524                                 obj->data.tail.interval = n2;
1525                                 fclose(fp);
1526                         } else {
1527                                 //fclose (fp);
1528                                 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1529                         }
1530                 }
1531         }
1532
1533         else {
1534                 ERR("invalid args given for tail");
1535                 return NULL;
1536         }
1537         obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
1538         END OBJ(head, 0)
1539                         char buf[64];
1540         int n1, n2;
1541         if (!arg) {
1542                 ERR("head needs arguments");
1543                 obj->type = OBJ_text;
1544                 obj->data.s = strdup("${head}");
1545                 return NULL;
1546         }
1547         if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1548                 if (n1 < 1 || n1 > 30) {
1549                         CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
1550                         return NULL;
1551                 } else {
1552                         FILE *fp;
1553                         fp = fopen(buf, "r");
1554                         if (fp != NULL) {
1555                                 obj->data.tail.logfile =
1556                                                 malloc(TEXT_BUFFER_SIZE);
1557                                 strcpy(obj->data.tail.logfile, buf);
1558                                 obj->data.tail.wantedlines = n1 - 1;
1559                                 obj->data.tail.interval =
1560                                                 update_interval * 2;
1561                                 fclose(fp);
1562                         } else {
1563                                 //fclose (fp);
1564                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1565                         }
1566                 }
1567         } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1568                 if (n1 < 1 || n1 > 30) {
1569                         CRIT_ERR
1570                                         ("invalid arg for head, number of lines must be between 1 and 30");
1571                         return NULL;
1572                 } else if (n2 < 1 || n2 < update_interval) {
1573                         CRIT_ERR
1574                                         ("invalid arg for head, interval must be greater than 0 and Conky's interval");
1575                         return NULL;
1576                 } else {
1577                         FILE *fp;
1578                         fp = fopen(buf, "r");
1579                         if (fp != NULL) {
1580                                 obj->data.tail.logfile =
1581                                                 malloc(TEXT_BUFFER_SIZE);
1582                                 strcpy(obj->data.tail.logfile, buf);
1583                                 obj->data.tail.wantedlines = n1 - 1;
1584                                 obj->data.tail.interval = n2;
1585                                 fclose(fp);
1586                         } else {
1587                                 //fclose (fp);
1588                                 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1589                         }
1590                 }
1591         }
1592
1593         else {
1594                 ERR("invalid args given for head");
1595                 return NULL;
1596         }
1597         obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
1598         END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
1599         if (arg) {
1600                 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1601                 if (r >= 3 && (c < 1 || c > 3))
1602                         r--;
1603                 if (r >= 2 && (b < 1 || b > 3))
1604                         r--, b = c;
1605                 if (r >= 1 && (a < 1 || a > 3))
1606                         r--, a = b, b = c;
1607         }
1608         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1609         obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1610         obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1611         END OBJ(if_existing, 0)
1612         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1613                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1614         }
1615         if (!arg) {
1616                 ERR("if_existing needs an argument");
1617                 obj->data.ifblock.s = 0;
1618         } else
1619                 obj->data.ifblock.s = strdup(arg);
1620         blockstart[blockdepth] = text_object_count;
1621         obj->data.ifblock.pos = text_object_count + 2;
1622         blockdepth++;
1623         END OBJ(if_mounted, 0)
1624         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1625                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1626         }
1627         if (!arg) {
1628                 ERR("if_mounted needs an argument");
1629                 obj->data.ifblock.s = 0;
1630         } else
1631                 obj->data.ifblock.s = strdup(arg);
1632         blockstart[blockdepth] = text_object_count;
1633         obj->data.ifblock.pos = text_object_count + 2;
1634         blockdepth++;
1635         END OBJ(if_running, 0)
1636         if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1637                 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1638         }
1639         if (arg) {
1640                 char buf[256];
1641                 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1642                 obj->data.ifblock.s = strdup(buf);
1643         } else {
1644                 ERR("if_running needs an argument");
1645                 obj->data.ifblock.s = 0;
1646         }
1647         blockstart[blockdepth] = text_object_count;
1648         obj->data.ifblock.pos = text_object_count + 2;
1649         blockdepth++;
1650         END OBJ(kernel, 0)
1651         END OBJ(machine, 0)
1652         END OBJ(mails, INFO_MAIL)
1653         END OBJ(mem, INFO_MEM)
1654         END OBJ(memmax, INFO_MEM)
1655         END OBJ(memperc, INFO_MEM)
1656         END OBJ(membar, INFO_MEM)
1657          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1658         END OBJ(memgraph, INFO_MEM)
1659                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1660         END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
1661         END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
1662         END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
1663         END OBJ(mixerbar, INFO_MIXER)
1664             scan_mixer_bar(arg, &obj->data.mixerbar.l,
1665                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1666         END OBJ(mixerlbar, INFO_MIXER)
1667             scan_mixer_bar(arg, &obj->data.mixerbar.l,
1668                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1669         END OBJ(mixerrbar, INFO_MIXER)
1670             scan_mixer_bar(arg, &obj->data.mixerbar.l,
1671                            &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1672         END
1673 #ifdef MLDONKEY
1674             OBJ(ml_upload_counter, INFO_MLDONKEY)
1675         END OBJ(ml_download_counter, INFO_MLDONKEY)
1676         END OBJ(ml_nshared_files, INFO_MLDONKEY)
1677         END OBJ(ml_shared_counter, INFO_MLDONKEY)
1678         END OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
1679         END OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
1680         END OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
1681         END OBJ(ml_udp_download_rate, INFO_MLDONKEY)
1682         END OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
1683         END OBJ(ml_ndownloading_files, INFO_MLDONKEY) END
1684 #endif
1685          OBJ(new_mails, INFO_MAIL)
1686         END OBJ(nodename, 0)
1687         END OBJ(processes, INFO_PROCS)
1688         END OBJ(running_processes, INFO_RUN_PROCS)
1689         END OBJ(shadecolor, 0)
1690 #ifdef X11
1691             obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1692 #endif /* X11 */
1693         END OBJ(outlinecolor, 0)
1694 #ifdef X11
1695             obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1696 #endif /* X11 */
1697         END OBJ(stippled_hr, 0)
1698 #ifdef X11
1699 int a = stippled_borders, b = 1;
1700         if (arg) {
1701                 if (sscanf(arg, "%d %d", &a, &b) != 2)
1702                         sscanf(arg, "%d", &b);
1703         }
1704         if (a <= 0)
1705                 a = 1;
1706         obj->data.pair.a = a;
1707         obj->data.pair.b = b;
1708 #endif /* X11 */
1709         END OBJ(swap, INFO_MEM)
1710         END OBJ(swapmax, INFO_MEM)
1711         END OBJ(swapperc, INFO_MEM)
1712         END OBJ(swapbar, INFO_MEM)
1713          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1714         END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
1715         obj->data.i2c.fd =
1716             open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
1717                             obj->data.i2c.devtype);
1718         END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1719         obj->data.i2c.fd =
1720             open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1721                             obj->data.i2c.devtype);
1722         END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1723         END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1724         END OBJ(totaldown, INFO_NET)
1725                 if(arg) {
1726                         obj->data.net = get_net_stat(arg);
1727                 }
1728                 else {
1729                         CRIT_ERR("totaldown needs argument");
1730                 }
1731         END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1732                 if(arg) {
1733                         obj->data.net = get_net_stat(arg);
1734                 }
1735                 else {
1736                         CRIT_ERR("totalup needs argument");
1737                 }
1738         END OBJ(updates, 0)
1739         END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
1740         END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
1741         END OBJ(upspeed, INFO_NET)
1742                 if(arg) {
1743                         obj->data.net = get_net_stat(arg);
1744                 }
1745                 else {
1746                         CRIT_ERR("upspeed needs argument");
1747                 }
1748         END OBJ(upspeedf, INFO_NET) 
1749                 if(arg) {
1750                         obj->data.net = get_net_stat(arg);
1751                 }
1752                 else {
1753                         CRIT_ERR("upspeedf needs argument");
1754                 }
1755
1756         END OBJ(upspeedgraph, INFO_NET)
1757                         (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1758         char buf[64];
1759         sscanf(arg, "%63s %*i,%*i %*i", buf);
1760         obj->data.net = get_net_stat(buf);
1761         if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1762                 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
1763                         obj->a = 0;
1764                         obj->b = 25;
1765                 }
1766         }
1767         END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1768             OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
1769 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
1770         OBJ(apm_adapter, 0) END
1771         OBJ(apm_battery_life, 0) END
1772         OBJ(apm_battery_time, 0) END
1773 #endif /* __FreeBSD__ */
1774 #ifdef SETI
1775          OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1776          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1777         END OBJ(seti_credit, INFO_SETI) END
1778 #endif
1779 #ifdef MPD
1780          OBJ(mpd_artist, INFO_MPD)
1781         END OBJ(mpd_title, INFO_MPD)
1782         END OBJ(mpd_random, INFO_MPD)
1783         END OBJ(mpd_repeat, INFO_MPD)
1784         END OBJ(mpd_elapsed, INFO_MPD)
1785         END OBJ(mpd_length, INFO_MPD)
1786         END OBJ(mpd_track, INFO_MPD)
1787         END OBJ(mpd_percent, INFO_MPD)
1788         END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol,
1789                                              INFO_MPD) END OBJ(mpd_bitrate,
1790                                                                INFO_MPD)
1791         END OBJ(mpd_status, INFO_MPD)
1792         END OBJ(mpd_bar, INFO_MPD)
1793          (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1794         END
1795 #endif
1796 #ifdef BMPX
1797         OBJ(bmpx_title, INFO_BMPX)
1798         END
1799         OBJ(bmpx_artist, INFO_BMPX)
1800         END
1801         OBJ(bmpx_album, INFO_BMPX)
1802         END
1803         OBJ(bmpx_track, INFO_BMPX)
1804         END
1805         OBJ(bmpx_uri, INFO_BMPX)
1806         END
1807         OBJ(bmpx_bitrate, INFO_BMPX)
1808         END
1809 #endif
1810 #ifdef TCP_PORT_MONITOR
1811         OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR) 
1812                 int argc, port_begin, port_end, item, connection_index;
1813                 char itembuf[32];
1814                 memset(itembuf,0,sizeof(itembuf));
1815                 connection_index=0;
1816                 /* massive argument checking */
1817                 if (!arg) {
1818                         CRIT_ERR("tcp_portmon: needs arguments");
1819                 }
1820                 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
1821                 if ( (argc != 3) && (argc != 4) ) 
1822                 {
1823                         CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
1824                 }
1825                 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
1826                 {
1827                         CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
1828                 }
1829                 if ( port_begin > port_end )
1830                 {
1831                         CRIT_ERR("tcp_portmon: starting port must be <= ending port");
1832                 }
1833                 if ( strncmp(itembuf,"count",31) == 0 )
1834                         item=COUNT;
1835                 else if ( strncmp(itembuf,"rip",31) == 0 )
1836                         item=REMOTEIP;
1837                 else if ( strncmp(itembuf,"rhost",31) == 0 )
1838                         item=REMOTEHOST;
1839                 else if ( strncmp(itembuf,"rport",31) == 0 )
1840                         item=REMOTEPORT;
1841                 else if ( strncmp(itembuf,"lip",31) == 0 )
1842                         item=LOCALIP;
1843                 else if ( strncmp(itembuf,"lhost",31) == 0 )
1844                         item=LOCALHOST;
1845                 else if ( strncmp(itembuf,"lport",31) == 0 )
1846                         item=LOCALPORT;
1847                 else if ( strncmp(itembuf,"lservice",31) == 0 )
1848                         item=LOCALSERVICE;
1849                 else
1850                 {
1851                         CRIT_ERR("tcp_portmon: invalid item specified"); 
1852                 }
1853                 if ( (argc==3) && (item!=COUNT) )
1854                 {
1855                         CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
1856                 }
1857                 if ( (argc==4) && (connection_index<0) )
1858                 {
1859                         CRIT_ERR("tcp_portmon: connection index must be non-negative");
1860                 }
1861                 /* ok, args looks good. save the text object data */
1862                 obj->data.tcp_port_monitor.port_range_begin = (in_addr_t)port_begin;
1863                 obj->data.tcp_port_monitor.port_range_end = (in_addr_t)port_end;
1864                 obj->data.tcp_port_monitor.item = item;
1865                 obj->data.tcp_port_monitor.connection_index = connection_index;
1866
1867                 /* if the port monitor collection hasn't been created, we must create it */
1868                 if ( !info.p_tcp_port_monitor_collection )
1869                 {
1870                         info.p_tcp_port_monitor_collection = 
1871                                 create_tcp_port_monitor_collection( &tcp_port_monitor_collection_args );
1872                         if ( !info.p_tcp_port_monitor_collection )
1873                         {
1874                                 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
1875                         }
1876                 }
1877
1878                 /* if a port monitor for this port does not exist, create one and add it to the collection */
1879                 if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
1880                 {
1881                         tcp_port_monitor_t * p_monitor = 
1882                                 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
1883                         if ( !p_monitor )
1884                         {
1885                                 CRIT_ERR("tcp_portmon: unable to create port monitor");
1886                         }
1887                         /* add the newly created monitor to the collection */
1888                         if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
1889                                                                       p_monitor ) != 0 )
1890                         {
1891                                 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
1892                         }
1893                 }
1894         END
1895 #endif
1896         {
1897                 char buf[256];
1898                 ERR("unknown variable %s", s);
1899                 obj->type = OBJ_text;
1900                 snprintf(buf, 256, "${%s}", s);
1901                 obj->data.s = strdup(buf);
1902         }
1903 #undef OBJ
1904
1905         return obj;
1906 }
1907
1908 static struct text_object *create_plain_text(const char *s)
1909 {
1910   struct text_object *obj;
1911
1912   if (s == NULL || *s == '\0') {
1913       return NULL;
1914   }
1915   
1916   obj = new_text_object_internal();
1917
1918   obj->type = OBJ_text;
1919   obj->data.s = strdup(s);
1920   return obj;
1921 }
1922
1923 static struct text_object_list *extract_variable_text_internal(const char *p)
1924 {
1925     struct text_object_list *retval;
1926     struct text_object *obj;
1927     const char *s = p;
1928     
1929     retval = malloc(sizeof(struct text_object_list));
1930     memset(retval, 0, sizeof(struct text_object_list));
1931     retval->text_object_count = 0;
1932         
1933         while (*p) {
1934                 if (*p == '$') {
1935                         *(char *) p = '\0';
1936                         obj = create_plain_text(s);
1937                         if(obj != NULL) {
1938                             // allocate memory for the object
1939                             retval->text_objects = realloc(retval->text_objects, 
1940                                                            sizeof(struct text_object) * (retval->text_object_count+1));
1941                             // assign the new object to the end of the list.
1942                             memcpy(&retval->text_objects[retval->text_object_count++],
1943                                    obj, sizeof(struct text_object));
1944                             free(obj);
1945                         }
1946                         *(char *) p = '$';
1947                         p++;
1948                         s = p;
1949
1950                         if (*p != '$') {
1951                                 char buf[256];
1952                                 const char *var;
1953                                 unsigned int len;
1954
1955                                 /* variable is either $foo or ${foo} */
1956                                 if (*p == '{') {
1957                                         p++;
1958                                         s = p;
1959                                         while (*p && *p != '}')
1960                                                 p++;
1961                                 } else {
1962                                         s = p;
1963                                         if (*p == '#')
1964                                                 p++;
1965                                         while (*p && (isalnum((int) *p)
1966                                                       || *p == '_'))
1967                                                 p++;
1968                                 }
1969
1970                                 /* copy variable to buffer */
1971                                 len = (p - s > 255) ? 255 : (p - s);
1972                                 strncpy(buf, s, len);
1973                                 buf[len] = '\0';
1974
1975                                 if (*p == '}')
1976                                         p++;
1977                                 s = p;
1978
1979                                 var = getenv(buf);
1980
1981                                 /* if variable wasn't found from environment, use some special */
1982                                 if (!var) {
1983                                         char *p;
1984                                         char *arg = 0;
1985
1986                                         /* split arg */
1987                                         if (strchr(buf, ' ')) {
1988                                                 arg = strchr(buf, ' ');
1989                                                 *arg = '\0';
1990                                                 arg++;
1991                                                 while (isspace((int) *arg))
1992                                                         arg++;
1993                                                 if (!*arg)
1994                                                         arg = 0;
1995                                         }
1996
1997                                         /* lowercase variable name */
1998                                         p = buf;
1999                                         while (*p) {
2000                                                 *p = tolower(*p);
2001                                                 p++;
2002                                         }
2003
2004                                         // create new object
2005                                         obj = construct_text_object(buf, arg);
2006                                         if(obj != NULL) {
2007                                             // allocate memory for the object
2008                                             retval->text_objects = realloc(retval->text_objects, 
2009                                                                            sizeof(struct text_object) * (retval->text_object_count+1));
2010                                             // assign the new object to the end of the list.
2011                                             memcpy(&retval->text_objects[retval->text_object_count++],
2012                                                    obj, sizeof(struct text_object));
2013                                             free(obj);
2014                                         }
2015                                 }
2016                                 continue;
2017                         } else {
2018                             obj = create_plain_text("$");
2019                             if(obj != NULL) {
2020                                 // allocate memory for the object
2021                                 retval->text_objects = realloc(retval->text_objects, 
2022                                                                sizeof(struct text_object) * (retval->text_object_count+1));
2023                                 // assign the new object to the end of the list.
2024                                 memcpy(&retval->text_objects[retval->text_object_count++],
2025                                        obj, sizeof(struct text_object));
2026                                 free(obj);
2027                             }
2028                         }
2029                 }
2030                 p++;
2031         }
2032         obj = create_plain_text(s);
2033         if(obj != NULL) {
2034             // allocate memory for the object
2035             retval->text_objects = realloc(retval->text_objects,
2036                                            sizeof(struct text_object) * (retval->text_object_count+1));
2037             // assign the new object to the end of the list.
2038             memcpy(&retval->text_objects[retval->text_object_count++],
2039                    obj, sizeof(struct text_object));
2040             free(obj);
2041         }
2042
2043         if (blockdepth) {
2044                 ERR("one or more $endif's are missing");
2045         }
2046
2047         return retval;
2048 }
2049
2050 static void extract_variable_text(const char *p)
2051 {
2052   struct text_object_list *list;
2053
2054   free_text_objects(text_object_count, text_objects);
2055   text_object_count = 0;
2056   text_objects = NULL;
2057
2058 #ifdef MLDONKEY 
2059   ml_cleanup();
2060 #endif /* MLDONKEY */
2061          
2062   list = extract_variable_text_internal(p);
2063   text_objects = list->text_objects;
2064   text_object_count = list->text_object_count;
2065
2066   free(list);
2067
2068   return;
2069 }
2070
2071 void parse_conky_vars(char * text, char * p, struct information *cur) { 
2072         struct text_object_list *object_list = extract_variable_text_internal(text);
2073         generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
2074         free(object_list);
2075 }
2076
2077 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
2078 {
2079     unsigned int i;
2080
2081         for (i = 0; i < object_count; i++) {
2082                 struct text_object *obj = &objs[i];
2083
2084 #define OBJ(a) break; case OBJ_##a:
2085
2086                 switch (obj->type) {
2087                 default:
2088                         {
2089                                 ERR("not implemented obj type %d",
2090                                     obj->type);
2091                         }
2092                         OBJ(acpitemp) {
2093                                 /* does anyone have decimals in acpi temperature? */
2094                                 if (!use_spacer)
2095                                         snprintf(p, p_max_size, "%d", (int)
2096                                                         get_acpi_temperature(obj->
2097                                                                         data.
2098                                                                         i));
2099                                 else
2100                                         snprintf(p, 5, "%d    ", (int)
2101                                                         get_acpi_temperature(obj->
2102                                                                         data.
2103                                                                         i));
2104                         }
2105                         OBJ(acpitempf) {
2106                                 /* does anyone have decimals in acpi temperature? */
2107                                 if (!use_spacer)
2108                                         snprintf(p, p_max_size, "%d", (int)
2109                                                         ((get_acpi_temperature(obj->
2110                                                                         data.
2111                                                                         i)+ 40) * 9.0 / 5 - 40));
2112                                 else
2113                                         snprintf(p, 5, "%d    ", (int)
2114                                                         ((get_acpi_temperature(obj->
2115                                                                         data.
2116                                                                         i)+ 40) * 9.0 / 5 - 40));
2117                         }
2118                         OBJ(freq) {
2119                                 get_freq(p, p_max_size, "%.0f", 1); /* pk */
2120                         }
2121                         OBJ(freq_g) {
2122                                 get_freq(p, p_max_size, "%'.2f", 1000); /* pk */
2123                         }
2124                         OBJ(freq_dyn) {
2125                                 if (use_spacer) {
2126                                         get_freq_dynamic(p, 6, "%.0f     ", 1 ); /* pk */
2127                                 } else {
2128                                         get_freq_dynamic(p, p_max_size, "%.0f", 1 ); /* pk */
2129                                 }
2130                         }
2131                         OBJ(freq_dyn_g) {
2132                                 if (use_spacer) {
2133                                         get_freq_dynamic(p, 6, "%'.2f     ", 1000); /* pk */
2134                                 } else {
2135                                         get_freq_dynamic(p, p_max_size, "%'.2f", 1000); /* pk */
2136                                 }
2137                         }
2138                         OBJ(adt746xcpu) {
2139                                 get_adt746x_cpu(p, p_max_size); /* pk */
2140                         }
2141                         OBJ(adt746xfan) {
2142                                 get_adt746x_fan(p, p_max_size); /* pk */
2143                         }
2144                         OBJ(acpifan) {
2145                                 get_acpi_fan(p, p_max_size);  /* pk */
2146                         }
2147                         OBJ(acpiacadapter) {
2148                                 get_acpi_ac_adapter(p, p_max_size); /* pk */
2149                         }
2150                         OBJ(battery) {
2151                                 get_battery_stuff(p, p_max_size, obj->data.s);
2152                         }
2153                         OBJ(buffers) {
2154                                 human_readable(cur->buffers * 1024, p, 255);
2155                         }
2156                         OBJ(cached) {
2157                                 human_readable(cur->cached * 1024, p, 255);
2158                         }
2159                         OBJ(cpu) {
2160                                 if (obj->data.cpu_index > info.cpu_count) {
2161                                         printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
2162                                         CRIT_ERR("attempting to use more CPUs then you have!");
2163                                 }
2164                                 if (!use_spacer)
2165                                         snprintf(p, p_max_size, "%*d", pad_percents,
2166                                                 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2167                                                         100.0));
2168                                 else
2169                                         snprintf(p, 4, "%*d    ",
2170                                                  pad_percents,
2171                                                  (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2172                                                         100.0));
2173                         }
2174                         OBJ(cpubar) {
2175                                 new_bar(p, obj->a,
2176                                         obj->b,
2177                                         (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
2178                         }
2179                         OBJ(cpugraph) {
2180                                 new_graph(p, obj->a,
2181                                           obj->b, obj->c, obj->d,
2182                                           (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2183                                                           100), 100, 1);
2184                         }
2185                         OBJ(color) {
2186                                 new_fg(p, obj->data.l);
2187                         }
2188 #if defined(__linux__)
2189                         OBJ(i8k_version) {
2190                                 snprintf(p, p_max_size, "%s", i8k.version);
2191                         }
2192                         OBJ(i8k_bios) {
2193                                 snprintf(p, p_max_size, "%s", i8k.bios);
2194                         }
2195                         OBJ(i8k_serial) { 
2196                                 snprintf(p, p_max_size, "%s", i8k.serial);
2197                         }
2198                         OBJ(i8k_cpu_temp) { 
2199                                 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
2200                         }
2201                         OBJ(i8k_cpu_tempf) { 
2202                                 int cpu_temp;
2203                                 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
2204                                 snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
2205                         }
2206                         OBJ(i8k_left_fan_status) { 
2207                                 int left_fan_status;
2208                                 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
2209                                 if(left_fan_status == 0) {
2210                                         snprintf(p, p_max_size,"off");
2211                                 } if(left_fan_status == 1) {
2212                                         snprintf(p, p_max_size, "low");
2213                                 }       if(left_fan_status == 2) {
2214                                         snprintf(p, p_max_size, "high");
2215                                 }
2216
2217                         }
2218                         OBJ(i8k_right_fan_status) { 
2219                                 int right_fan_status;
2220                                 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
2221                                 if(right_fan_status == 0) {
2222                                         snprintf(p, p_max_size,"off");
2223                                 } if(right_fan_status == 1) {
2224                                         snprintf(p, p_max_size, "low");
2225                                 }       if(right_fan_status == 2) {
2226                                         snprintf(p, p_max_size, "high");
2227                                 }
2228                         }
2229                         OBJ(i8k_left_fan_rpm) { 
2230                                 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
2231                         }
2232                         OBJ(i8k_right_fan_rpm) { 
2233                                 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
2234                         }
2235                         OBJ(i8k_ac_status) { 
2236                                 int ac_status;
2237                                 sscanf(i8k.ac_status, "%d", &ac_status);
2238                                 if(ac_status == -1) {
2239                                         snprintf(p, p_max_size,"disabled (read i8k docs)");
2240                                 } if(ac_status == 0) {
2241                                         snprintf(p, p_max_size, "off");
2242                                 }       if(ac_status == 1) {
2243                                         snprintf(p, p_max_size, "on");
2244                                 }
2245                         }
2246                         OBJ(i8k_buttons_status) {
2247                                 snprintf(p, p_max_size, "%s", i8k.buttons_status); 
2248
2249                         }
2250 #endif /* __linux__ */
2251
2252 #ifdef X11
2253                         OBJ(font) {
2254                                 new_font(p, obj->data.s);
2255                         }
2256 #endif /* X11 */
2257                         OBJ(diskio) {
2258                                 if (!use_spacer) {
2259                                         if (diskio_value > 1024*1024) {
2260                                                 snprintf(p, p_max_size, "%.1fG",
2261                                                                 (double)diskio_value/1024/1024);
2262                                         } else if (diskio_value > 1024) {
2263                                                 snprintf(p, p_max_size, "%.1fM",
2264                                                                 (double)diskio_value/1024);
2265                                         } else if (diskio_value > 0) {
2266                                                 snprintf(p, p_max_size, "%dK", diskio_value);
2267                                         } else {
2268                                                 snprintf(p, p_max_size, "%d", diskio_value);
2269                                         }
2270                                 } else {
2271                                         if (diskio_value > 1024*1024) {
2272                                                 snprintf(p, 6, "%.1fG   ",
2273                                                                 (double)diskio_value/1024/1024);
2274                                         } else if (diskio_value > 1024) {
2275                                                 snprintf(p, 6, "%.1fM   ",
2276                                                                 (double)diskio_value/1024);
2277                                         } else if (diskio_value > 0) {
2278                                                 snprintf(p, 6, "%dK ", diskio_value);
2279                                         } else {
2280                                                 snprintf(p, 6, "%d     ", diskio_value);
2281                                         }
2282                                 }
2283                         }
2284                         OBJ(diskiograph) {
2285                                 new_graph(p, obj->a,
2286                                           obj->b, obj->c, obj->d,
2287                                           diskio_value, obj->e, 1);
2288                         }
2289         
2290                         OBJ(downspeed) {
2291                                 if (!use_spacer) {
2292                                         snprintf(p, p_max_size, "%d",
2293                                                  (int) (obj->data.net->
2294                                                         recv_speed /
2295                                                         1024));
2296                                 } else
2297                                         snprintf(p, 6, "%d     ",
2298                                                  (int) (obj->data.net->
2299                                                         recv_speed /
2300                                                         1024));
2301                         }
2302                         OBJ(downspeedf) {
2303                                 if (!use_spacer)
2304                                         snprintf(p, p_max_size, "%.1f",
2305                                                  obj->data.net->
2306                                                  recv_speed / 1024.0);
2307                                 else
2308                                         snprintf(p, 8, "%.1f       ",
2309                                                  obj->data.net->
2310                                                  recv_speed / 1024.0);
2311                         }
2312                         OBJ(downspeedgraph) {
2313                                 if (obj->data.net->recv_speed == 0)     // this is just to make the ugliness at start go away
2314                                         obj->data.net->recv_speed = 0.01;
2315                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2316                                           (obj->data.net->recv_speed /
2317                                 1024.0), obj->e, 1);
2318                         }
2319                         OBJ(
2320                                    else
2321                         ) {
2322                                 if (!if_jumped) {
2323                                         i = obj->data.ifblock.pos - 2;
2324                                 } else {
2325                                         if_jumped = 0;
2326                                 }
2327                         }
2328                         OBJ(endif) {
2329                                 if_jumped = 0;
2330                         }
2331 #ifdef HAVE_POPEN
2332                         OBJ(addr) {
2333                                 snprintf(p, p_max_size, "%u.%u.%u.%u",
2334                                          obj->data.net->addr.
2335                                          sa_data[2] & 255,
2336                                          obj->data.net->addr.
2337                                          sa_data[3] & 255,
2338                                          obj->data.net->addr.
2339                                          sa_data[4] & 255,
2340                                          obj->data.net->addr.
2341                                          sa_data[5] & 255);
2342
2343                         }
2344                         OBJ(linkstatus) {
2345                                 snprintf(p, p_max_size, "%d",
2346                                          obj->data.net->linkstatus);
2347                         }
2348
2349                         OBJ(exec) {
2350                                 FILE *fp = popen(obj->data.s, "r");
2351                                 int length = fread(p, 1, p_max_size, fp);
2352                                 (void) pclose(fp);
2353
2354                                 /*output[length] = '\0';
2355                                 if (length > 0 && output[length - 1] == '\n') {
2356                                         output[length - 1] = '\0';
2357                                 }*/
2358                                         p[length] = '\0';
2359                                         if (length > 0 && p[length - 1] == '\n') {
2360                                                 p[length - 1] = '\0';
2361                                         }
2362                                     
2363                                 //parse_conky_vars(output, p, cur);
2364                         }
2365                         OBJ(execbar) {
2366                                 char *p2 = p;
2367                                 FILE *fp = popen(obj->data.s, "r");
2368                                 int n2 = fread(p, 1, p_max_size, fp);
2369                                 (void) pclose(fp);
2370
2371                                 p[n2] = '\0';
2372                                 if (n2 && p[n2 - 1] == '\n')
2373                                         p[n2 - 1] = '\0';
2374
2375                                 while (*p2) {
2376                                         if (*p2 == '\001')
2377                                                 *p2 = ' ';
2378                                         p2++;
2379                                 }
2380                                 double barnum;
2381                                 if (sscanf(p, "%lf", &barnum) == 0) {
2382                                         ERR("reading execbar value failed (perhaps it's not the correct format?)");
2383                                 }
2384                                 if (barnum > 100 || barnum < 0) {
2385                                         ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
2386                                 } else {
2387                                         barnum = barnum / 100.0;
2388                                         new_bar(p, 0, 4, (int) (barnum * 255.0));
2389                                 }
2390
2391                         }
2392                         OBJ(execgraph) {
2393                                 char *p2 = p;
2394                                 FILE *fp = popen(obj->data.s, "r");
2395                                 int n2 = fread(p, 1, p_max_size, fp);
2396                                 (void) pclose(fp);
2397
2398                                 p[n2] = '\0';
2399                                 if (n2 && p[n2 - 1] == '\n')
2400                                         p[n2 - 1] = '\0';
2401
2402                                 while (*p2) {
2403                                         if (*p2 == '\001')
2404                                                 *p2 = ' ';
2405                                         p2++;
2406                                 }
2407                                 double barnum;
2408                                 if (sscanf(p, "%lf", &barnum) == 0) {
2409                                         ERR("reading execgraph value failed (perhaps it's not the correct format?)");
2410                                 }
2411                                 if (barnum > 100 || barnum < 0) {
2412                                         ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
2413                                 } else {
2414                                         new_graph(p, 0,
2415                                         25, obj->c, obj->d, (int) (barnum), obj->e, 1);
2416                                 }
2417
2418                         }
2419                         OBJ(execibar) {
2420                                 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2421                                         new_bar(p, 0, 4, (int) obj->f);
2422                                 } else {
2423                                         char *p2 = p;
2424                                         FILE *fp = popen(obj->data.execi.cmd, "r");
2425                                         int n2 = fread(p, 1, p_max_size, fp);
2426                                         (void) pclose(fp);
2427                                         p[n2] = '\0';
2428                                         if (n2 && p[n2 - 1] == '\n')
2429                                                 p[n2 - 1] = '\0';
2430
2431                                         while (*p2) {
2432                                                 if (*p2 == '\001')
2433                                                         *p2 = ' ';
2434                                                 p2++;
2435                                         }
2436                                         float barnum;
2437                                         if (sscanf(p, "%f", &barnum) == 0) {
2438                                                 ERR("reading execibar value failed (perhaps it's not the correct format?)");
2439                                         }
2440                                         if (barnum > 100 || barnum < 0) {
2441                                                 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
2442                                         } else {
2443                                                 obj->f = 255 * barnum / 100.0;
2444                                                 new_bar(p, 0, 4, (int) obj->f);
2445                                         }
2446                                         obj->data.execi.last_update =
2447                                                         current_update_time;
2448                                 }
2449                         }
2450                         OBJ(execigraph) {
2451                                 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2452                                         new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
2453                                 } else {
2454                                         char *p2 = p;
2455                                         FILE *fp = popen(obj->data.execi.cmd, "r");
2456                                         int n2 = fread(p, 1, p_max_size, fp);
2457                                         (void) pclose(fp);
2458                                         p[n2] = '\0';
2459                                         if (n2 && p[n2 - 1] == '\n')
2460                                                 p[n2 - 1] = '\0';
2461
2462                                         while (*p2) {
2463                                                 if (*p2 == '\001')
2464                                                         *p2 = ' ';
2465                                                 p2++;
2466                                         }
2467                                         float barnum;
2468                                         if (sscanf(p, "%f", &barnum) == 0) {
2469                                                 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
2470                                         }
2471                                         if (barnum > 100 || barnum < 0) {
2472                                                 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
2473                                         } else {
2474                                                 obj->f = barnum;
2475                                                 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
2476                                         }
2477                                         obj->data.execi.last_update = current_update_time;
2478         
2479                                 }
2480
2481                         }
2482                         OBJ(execi) {
2483                                 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
2484                                         snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
2485                                 } else {
2486                                         char *output = obj->data.execi.buffer;
2487                                         FILE *fp = popen(obj->data.execi.cmd, "r");
2488                                         //int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
2489                                         int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
2490                                         (void) pclose(fp);
2491                                     
2492                                         output[length] = '\0';
2493                                         if (length > 0 && output[length - 1] == '\n') {
2494                                                 output[length - 1] = '\0';
2495                                         }
2496                                         obj->data.execi.last_update = current_update_time;
2497                                         snprintf(p, p_max_size, "%s", output);
2498                                 }
2499                                 //parse_conky_vars(output, p, cur);
2500                         }
2501                         OBJ(texeci) {
2502                                 static int running = 0;
2503                                 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2504                                         snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
2505                                 } else {
2506                                         static pthread_t execthread;
2507                                         if (running) {
2508                                                 pthread_join( execthread, NULL);
2509                                                 running = 0;
2510                                         }
2511                                         if (!running) {
2512                                                 running = 1;
2513                                                 pthread_create( &execthread, NULL, (void*)threaded_exec, (void*) obj);
2514                                                 pthread_mutex_lock( &mutex1 );
2515                                                 obj->data.execi.last_update = current_update_time;
2516                                                 pthread_mutex_unlock( &mutex1 );
2517                                         }
2518                                         snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
2519                                 }
2520                                 //parse_conky_vars(obj->data.execi.buffer, p, cur);
2521                         }
2522 #endif
2523                         OBJ(fs_bar) {
2524                                 if (obj->data.fs != NULL) {
2525                                         if (obj->data.fs->size == 0)
2526                                                 new_bar(p,
2527                                                         obj->data.fsbar.w,
2528                                                         obj->data.fsbar.h,
2529                                                         255);
2530                                         else
2531                                                 new_bar(p,
2532                                                         obj->data.fsbar.w,
2533                                                         obj->data.fsbar.h,
2534                                                         (int) (255 -
2535                                                                obj->data.
2536                                                                fsbar.fs->
2537                                                                avail *
2538                                                                255 /
2539                                                                obj->data.
2540                                                                fs->size));
2541                                 }
2542                         }
2543                         OBJ(fs_free) {
2544                                 if (obj->data.fs != NULL)
2545                                         human_readable(obj->data.fs->avail,
2546                                                        p, 255);
2547                         }
2548                         OBJ(fs_free_perc) {
2549                                 if (obj->data.fs != NULL) {
2550                                         if (obj->data.fs->size)
2551                                                 snprintf(p, p_max_size, "%*d",
2552                                                          pad_percents,
2553                                                          (int) ((obj->data.
2554                                                                  fs->
2555                                                                  avail *
2556                                                                  100) /
2557                                                                 obj->data.
2558                                                                 fs->size));
2559                                         else
2560                                                 snprintf(p, p_max_size, "0");
2561                                 }
2562                         }
2563                         OBJ(fs_size) {
2564                                 if (obj->data.fs != NULL)
2565                                         human_readable(obj->data.fs->size,
2566                                                        p, 255);
2567                         }
2568                         OBJ(fs_used) {
2569                                 if (obj->data.fs != NULL)
2570                                         human_readable(obj->data.fs->size -
2571                                                        (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
2572                                                        p, 255);
2573                         }
2574                         OBJ(fs_bar_free) {
2575                                 if (obj->data.fs != NULL) {
2576                                         if (obj->data.fs->size == 0)
2577                                                 new_bar(p,
2578                                                         obj->data.fsbar.w,
2579                                                         obj->data.fsbar.h,
2580                                                         255);
2581                                         else
2582                                                 new_bar(p,
2583                                                         obj->data.fsbar.w,
2584                                                         obj->data.fsbar.h,
2585                                                         (int) (obj->data.
2586                                                                fsbar.fs->
2587                                                                avail *
2588                                                                255 /
2589                                                                obj->data.
2590                                                                fs->size));
2591                                 }
2592                         }
2593                         OBJ(fs_used_perc) {
2594                                 if (obj->data.fs != NULL) {
2595                                         if (obj->data.fs->size)
2596                                                 snprintf(p, 4, "%d",
2597                                                          100 - ((int)
2598                                                                 ((obj->
2599                                                                   data.fs->
2600                                                                   avail *
2601                                                                   100) /
2602                                                                  obj->data.
2603                                                                  fs->
2604                                                                  size)));
2605                                         else
2606                                                 snprintf(p, p_max_size, "0");
2607                                 }
2608                         }
2609                         OBJ(loadavg) {
2610                                 float *v = info.loadavg;
2611
2612                                 if (obj->data.loadavg[2])
2613                                         snprintf(p, p_max_size, "%.2f %.2f %.2f",
2614                                                  v[obj->data.loadavg[0] -
2615                                                    1],
2616                                                  v[obj->data.loadavg[1] -
2617                                                    1],
2618                                                  v[obj->data.loadavg[2] -
2619                                                    1]);
2620                                 else if (obj->data.loadavg[1])
2621                                         snprintf(p, p_max_size, "%.2f %.2f",
2622                                                  v[obj->data.loadavg[0] -
2623                                                    1],
2624                                                  v[obj->data.loadavg[1] -
2625                                                    1]);
2626                                 else if (obj->data.loadavg[0])
2627                                         snprintf(p, p_max_size, "%.2f",
2628                                                  v[obj->data.loadavg[0] -
2629                                                    1]);
2630                         }
2631                         OBJ(hr) {
2632                                 new_hr(p, obj->data.i);
2633                         }
2634                         OBJ(offset) {
2635                                 new_offset(p, obj->data.i);
2636                         }
2637                         OBJ(voffset) {
2638                                 new_voffset(p, obj->data.i);
2639                         }
2640                         OBJ(i2c) {
2641                                 double r;
2642
2643                                 r = get_i2c_info(&obj->data.i2c.fd,
2644                                                  obj->data.i2c.arg,
2645                                                  obj->data.i2c.devtype,
2646                                                  obj->data.i2c.type);
2647
2648                                 if (r >= 100.0 || r == 0)
2649                                         snprintf(p, p_max_size, "%d", (int) r);
2650                                 else
2651                                         snprintf(p, p_max_size, "%.1f", r);
2652                         }
2653                         OBJ(alignr) {
2654                                 new_alignr(p, obj->data.i);
2655                         }
2656                         OBJ(alignc) {
2657                                 new_alignc(p, obj->data.i);
2658                         }
2659                         OBJ(if_existing) {
2660                                 struct stat tmp;
2661                                 if ((obj->data.ifblock.s)
2662                                     && (stat(obj->data.ifblock.s, &tmp) ==
2663                                         -1)) {
2664                                         i = obj->data.ifblock.pos - 2;
2665                                         if_jumped = 1;
2666                                 } else
2667                                         if_jumped = 0;
2668                         }
2669                         OBJ(if_mounted) {
2670                                 if ((obj->data.ifblock.s)
2671                                     && (!check_mount(obj->data.ifblock.s))) {
2672                                         i = obj->data.ifblock.pos - 2;
2673                                         if_jumped = 1;
2674                                 } else
2675                                         if_jumped = 0;
2676                         }
2677                         OBJ(if_running) {
2678                                 if ((obj->data.ifblock.s)
2679                                     && system(obj->data.ifblock.s)) {
2680                                         i = obj->data.ifblock.pos - 2;
2681                                         if_jumped = 1;
2682                                 } else
2683                                         if_jumped = 0;
2684                         }
2685                         OBJ(kernel) {
2686                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
2687                         }
2688                         OBJ(machine) {
2689                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
2690                         }
2691
2692                         /* memory stuff */
2693                         OBJ(mem) {
2694                                 human_readable(cur->mem * 1024, p, 6);
2695                         }
2696                         OBJ(memmax) {
2697                                 human_readable(cur->memmax * 1024, p, 255);
2698                         }
2699                         OBJ(memperc) {
2700                                 if (cur->memmax) {
2701                                         if (!use_spacer)
2702                                                 snprintf(p, p_max_size, "%*lu",
2703                                                          pad_percents,
2704                                                          (cur->mem * 100) /
2705                                                          (cur->memmax));
2706                                         else
2707                                                 snprintf(p, 4, "%*lu   ",
2708                                                          pad_percents,
2709                                                          (cur->mem * 100) /
2710                                                          (cur->memmax));
2711                                 }
2712                         }
2713                         OBJ(membar) {
2714                                 new_bar(p, obj->data.pair.a,
2715                                         obj->data.pair.b,
2716                                         cur->memmax ? (cur->mem * 255) /
2717                                         (cur->memmax) : 0);
2718                         }
2719
2720                         OBJ(memgraph) {
2721                                 new_graph(p, obj->a,
2722                                 obj->b, obj->c, obj->d,
2723                                 cur->memmax ? (cur->mem * 100.0) /
2724                                                 (cur->memmax) : 0.0, 100, 1);
2725                         }
2726                         /* mixer stuff */
2727                         OBJ(mixer) {
2728                                 snprintf(p, p_max_size, "%d",
2729                                          mixer_get_avg(obj->data.l));
2730                         }
2731                         OBJ(mixerl) {
2732                                 snprintf(p, p_max_size, "%d",
2733                                          mixer_get_left(obj->data.l));
2734                         }
2735                         OBJ(mixerr) {
2736                                 snprintf(p, p_max_size, "%d",
2737                                          mixer_get_right(obj->data.l));
2738                         }
2739                         OBJ(mixerbar) {
2740                                 new_bar(p, obj->data.mixerbar.w,
2741                                         obj->data.mixerbar.h,
2742                                         mixer_get_avg(obj->data.mixerbar.
2743                                                       l) * 255 / 100);
2744                         }
2745                         OBJ(mixerlbar) {
2746                                 new_bar(p, obj->data.mixerbar.w,
2747                                         obj->data.mixerbar.h,
2748                                         mixer_get_left(obj->data.mixerbar.
2749                                                        l) * 255 / 100);
2750                         }
2751                         OBJ(mixerrbar) {
2752                                 new_bar(p, obj->data.mixerbar.w,
2753                                         obj->data.mixerbar.h,
2754                                         mixer_get_right(obj->data.mixerbar.
2755                                                         l) * 255 / 100);
2756                         }
2757
2758                         /* mail stuff */
2759                         OBJ(mails) {
2760                                 snprintf(p, p_max_size, "%d", cur->mail_count);
2761                         }
2762                         OBJ(new_mails) {
2763                                 snprintf(p, p_max_size, "%d", cur->new_mail_count);
2764                         }
2765 #ifdef MLDONKEY
2766                         OBJ(ml_upload_counter) {
2767                                 snprintf(p, p_max_size, "%lld",
2768                                          mlinfo.upload_counter / 1048576);
2769                         }
2770                         OBJ(ml_download_counter) {
2771                                 snprintf(p, p_max_size, "%lld",
2772                                          mlinfo.download_counter /
2773                                          1048576);
2774                         }
2775                         OBJ(ml_nshared_files) {
2776                                 snprintf(p, p_max_size, "%i", mlinfo.nshared_files);
2777                         }
2778                         OBJ(ml_shared_counter) {
2779                                 snprintf(p, p_max_size, "%lld",
2780                                          mlinfo.shared_counter / 1048576);
2781                         }
2782                         OBJ(ml_tcp_upload_rate) {
2783                                 snprintf(p, p_max_size, "%.2f",
2784                                          (float) mlinfo.tcp_upload_rate /
2785                                          1024);
2786                         }
2787                         OBJ(ml_tcp_download_rate) {
2788                                 snprintf(p, p_max_size, "%.2f",
2789                                          (float) mlinfo.tcp_download_rate /
2790                                          1024);
2791                         }
2792                         OBJ(ml_udp_upload_rate) {
2793                                 snprintf(p, p_max_size, "%.2f",
2794                                          (float) mlinfo.udp_upload_rate /
2795                                          1024);
2796                         }
2797                         OBJ(ml_udp_download_rate) {
2798                                 snprintf(p, p_max_size, "%.2f",
2799                                          (float) mlinfo.udp_download_rate /
2800                                          1024);
2801                         }
2802                         OBJ(ml_ndownloaded_files) {
2803                                 snprintf(p, p_max_size, "%i",
2804                                          mlinfo.ndownloaded_files);
2805                         }
2806                         OBJ(ml_ndownloading_files) {
2807                                 snprintf(p, p_max_size, "%i",
2808                                          mlinfo.ndownloading_files);
2809                         }
2810 #endif
2811
2812                         OBJ(nodename) {
2813                                 snprintf(p, p_max_size, "%s",
2814                                          cur->uname_s.nodename);
2815                         }
2816                         OBJ(outlinecolor) {
2817                                 new_outline(p, obj->data.l);
2818                         }
2819                         OBJ(processes) {
2820                                 if (!use_spacer)
2821                                         snprintf(p, p_max_size, "%hu", cur->procs);
2822                                 else
2823                                         snprintf(p, 5, "%hu    ",
2824                                                  cur->procs);
2825                         }
2826                         OBJ(running_processes) {
2827                                 if (!use_spacer)
2828                                         snprintf(p, p_max_size, "%hu",
2829                                                  cur->run_procs);
2830                                 else
2831                                         snprintf(p, 3, "%hu     ",
2832                                                  cur->run_procs);
2833                         }
2834                         OBJ(text) {
2835                                 snprintf(p, p_max_size, "%s", obj->data.s);
2836                         }
2837                         OBJ(shadecolor) {
2838                                 new_bg(p, obj->data.l);
2839                         }
2840                         OBJ(stippled_hr) {
2841                                 new_stippled_hr(p, obj->data.pair.a,
2842                                                 obj->data.pair.b);
2843                         }
2844                         OBJ(swap) {
2845                                 human_readable(cur->swap * 1024, p, 255);
2846                         }
2847                         OBJ(swapmax) {
2848                                 human_readable(cur->swapmax * 1024, p,
2849                                                255);
2850                         }
2851                         OBJ(swapperc) {
2852                                 if (cur->swapmax == 0) {
2853                                         strncpy(p, "No swap", 255);
2854                                 } else {
2855                                         if (!use_spacer)
2856                                                 snprintf(p, 255, "%*lu",
2857                                                          pad_percents,
2858                                                          (cur->swap *
2859                                                           100) /
2860                                                          cur->swapmax);
2861                                         else
2862                                                 snprintf(p, 4, "%*lu   ",
2863                                                          pad_percents,
2864                                                          (cur->swap *
2865                                                           100) /
2866                                                          cur->swapmax);
2867                                 }
2868                         }
2869                         OBJ(swapbar) {
2870                                 new_bar(p, obj->data.pair.a,
2871                                         obj->data.pair.b,
2872                                         cur->swapmax ? (cur->swap * 255) /
2873                                         (cur->swapmax) : 0);
2874                         }
2875                         OBJ(sysname) {
2876                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
2877                         }
2878                         OBJ(time) {
2879                                 time_t t = time(NULL);
2880                                 struct tm *tm = localtime(&t);
2881                                 setlocale(LC_TIME, "");
2882                                 strftime(p, p_max_size, obj->data.s, tm);
2883                         }
2884                         OBJ(utime) {
2885                                 time_t t = time(NULL);
2886                                 struct tm *tm = gmtime(&t);
2887                                 strftime(p, p_max_size, obj->data.s, tm);
2888                         }
2889                         OBJ(totaldown) {
2890                                 human_readable(obj->data.net->recv, p,
2891                                                255);
2892                         }
2893                         OBJ(totalup) {
2894                                 human_readable(obj->data.net->trans, p,
2895                                                255);
2896                         }
2897                         OBJ(updates) {
2898                                 snprintf(p, p_max_size, "%d", total_updates);
2899                         }
2900                         OBJ(upspeed) {
2901                                 if (!use_spacer)
2902                                         snprintf(p, p_max_size, "%d",
2903                                                  (int) (obj->data.net->
2904                                                         trans_speed /
2905                                                         1024));
2906                                 else
2907                                         snprintf(p, 5, "%d     ",
2908                                                  (int) (obj->data.net->
2909                                                         trans_speed /
2910                                                         1024));
2911                         }
2912                         OBJ(upspeedf) {
2913                                 if (!use_spacer)
2914                                         snprintf(p, p_max_size, "%.1f",
2915                                                  obj->data.net->
2916                                                  trans_speed / 1024.0);
2917                                 else
2918                                         snprintf(p, 8, "%.1f       ",
2919                                                  obj->data.net->
2920                                                  trans_speed / 1024.0);
2921                         }
2922                         OBJ(upspeedgraph) {
2923                                 if (obj->data.net->trans_speed == 0)    // this is just to make the ugliness at start go away
2924                                         obj->data.net->trans_speed = 0.01;
2925                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2926                                           (obj->data.net->trans_speed /
2927                                 1024.0), obj->e, 1);
2928                         }
2929                         OBJ(uptime_short) {
2930                                 format_seconds_short(p, p_max_size,
2931                                                      (int) cur->uptime);
2932                         }
2933                         OBJ(uptime) {
2934                                 format_seconds(p, p_max_size, (int) cur->uptime);
2935                         }
2936
2937 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2938                         OBJ(apm_adapter) {
2939                                 snprintf(p, p_max_size, "%s", get_apm_adapter());
2940                         }
2941                         OBJ(apm_battery_life) {
2942                                 char    *msg;
2943                                 msg = get_apm_battery_life();
2944                                 snprintf(p, p_max_size, "%s", msg);
2945                                 free(msg);
2946                         }
2947                         OBJ(apm_battery_time) {
2948                                 char    *msg;
2949                                 msg = get_apm_battery_time();
2950                                 snprintf(p, p_max_size, "%s", msg);
2951                                 free(msg);
2952                         }
2953 #endif /* __FreeBSD__ */
2954 #ifdef SETI
2955                         OBJ(seti_prog) {
2956                                 snprintf(p, p_max_size, "%.2f",
2957                                          cur->seti_prog * 100.0f);
2958                         }
2959                         OBJ(seti_progbar) {
2960                                 new_bar(p, obj->data.pair.a,
2961                                         obj->data.pair.b,
2962                                         (int) (cur->seti_prog * 255.0f));
2963                         }
2964                         OBJ(seti_credit) {
2965                                 snprintf(p, p_max_size, "%.0f", cur->seti_credit);
2966                         }
2967 #endif
2968
2969 #ifdef MPD
2970                         OBJ(mpd_title) {
2971                                 snprintf(p, p_max_size, "%s", cur->mpd.title);
2972                         }
2973                         OBJ(mpd_artist) {
2974                                 snprintf(p, p_max_size, "%s", cur->mpd.artist);
2975                         }
2976                         OBJ(mpd_album) {
2977                                 snprintf(p, p_max_size, "%s", cur->mpd.album);
2978                         }
2979                         OBJ(mpd_random) {
2980                                 snprintf(p, p_max_size, "%s", cur->mpd.random);
2981                         }
2982                         OBJ(mpd_repeat) {
2983                                 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
2984                         }
2985                         OBJ(mpd_track) {
2986                                 snprintf(p, p_max_size, "%s", cur->mpd.track);
2987                         }
2988                         OBJ(mpd_vol) {
2989                                 snprintf(p, p_max_size, "%i", cur->mpd.volume);
2990                         }
2991                         OBJ(mpd_bitrate) {
2992                                 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
2993                         }
2994                         OBJ(mpd_status) {
2995                                 snprintf(p, p_max_size, "%s", cur->mpd.status);
2996                         }
2997                         OBJ(mpd_elapsed) {
2998                                 int days = 0, hours = 0, minutes =
2999                                     0, seconds = 0;
3000                                 int tmp = cur->mpd.elapsed;
3001                                 while (tmp >= 86400) {
3002                                         tmp -= 86400;
3003                                         days++;
3004                                 }
3005                                 while (tmp >= 3600) {
3006                                         tmp -= 3600;
3007                                         hours++;
3008                                 }
3009                                 while (tmp >= 60) {
3010                                         tmp -= 60;
3011                                         minutes++;
3012                                 }
3013                                 seconds = tmp;
3014                                 if (days > 0)
3015                                         snprintf(p, p_max_size, "%i days %i:%02i:%02i",
3016                                                  days, hours, minutes,
3017                                                  seconds);
3018                                 else if (hours > 0)
3019                                         snprintf(p, p_max_size, "%i:%02i:%02i", hours,
3020                                                  minutes, seconds);
3021                                 else
3022                                         snprintf(p, p_max_size, "%i:%02i", minutes,
3023                                                  seconds);
3024                         }
3025                         OBJ(mpd_length) {
3026                                 int days = 0, hours = 0, minutes =
3027                                     0, seconds = 0;
3028                                 int tmp = cur->mpd.length;
3029                                 while (tmp >= 86400) {
3030                                         tmp -= 86400;
3031                                         days++;
3032                                 }
3033                                 while (tmp >= 3600) {
3034                                         tmp -= 3600;
3035                                         hours++;
3036                                 }
3037                                 while (tmp >= 60) {
3038                                         tmp -= 60;
3039                                         minutes++;
3040                                 }
3041                                 seconds = tmp;
3042                                 if (days > 0)
3043                                         snprintf(p, p_max_size,
3044                                                  "%i days %i:%02i:%02i",
3045                                                  days, hours, minutes,
3046                                                  seconds);
3047                                 else if (hours > 0)
3048                                         snprintf(p, p_max_size, "%i:%02i:%02i", hours,
3049                                                  minutes, seconds);
3050                                 else
3051                                         snprintf(p, p_max_size, "%i:%02i", minutes,
3052                                                  seconds);
3053                         }
3054                         OBJ(mpd_percent) {
3055                                 snprintf(p, p_max_size, "%2.0f",
3056                                          cur->mpd.progress * 100);
3057                         }
3058                         OBJ(mpd_bar) {
3059                                 new_bar(p, obj->data.pair.a,
3060                                         obj->data.pair.b,
3061                                         (int) (cur->mpd.progress *
3062                                                255.0f));
3063                         }
3064 #endif
3065 #ifdef BMPX
3066                         OBJ(bmpx_title) {
3067                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
3068                         }
3069                         OBJ(bmpx_artist) {
3070                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
3071                         }
3072                         OBJ(bmpx_album) {
3073                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
3074                         }
3075                         OBJ(bmpx_uri) {
3076                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
3077                         }
3078                         OBJ(bmpx_track) {
3079                                  snprintf(p, p_max_size, "%i", cur->bmpx.track);
3080                         }
3081                         OBJ(bmpx_bitrate) {
3082                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
3083                         }
3084 #endif
3085                         OBJ(top) {
3086                                 if (obj->data.top.type == TOP_NAME
3087                                     && obj->data.top.num >= 0
3088                                     && obj->data.top.num < 10) {
3089                                         // if we limit the buffer and add a bunch of space after, it stops the thing from
3090                                         // moving other shit around, which is really fucking annoying
3091                                         snprintf(p, 17, "%s                              ", cur->cpu[obj->data.top.num]->name);
3092                                 } else if (obj->data.top.type == TOP_CPU
3093                                            && obj->data.top.num >= 0
3094                                            && obj->data.top.num < 10) {
3095                                         snprintf(p, 7, "%3.2f      ",
3096                                                  cur->cpu[obj->data.top.
3097                                                           num]->amount);
3098                                 } else if (obj->data.top.type == TOP_PID
3099                                            && obj->data.top.num >= 0
3100                                            && obj->data.top.num < 10) {
3101                                         snprintf(p, 8, "%i           ",
3102                                                  cur->cpu[obj->data.top.
3103                                                           num]->pid);
3104                                 } else if (obj->data.top.type == TOP_MEM
3105                                            && obj->data.top.num >= 0
3106                                            && obj->data.top.num < 10) {
3107                                         snprintf(p, 7, "%3.2f       ",
3108                                                  cur->cpu[obj->data.top.
3109                                                           num]->totalmem);
3110                                 }
3111                         }
3112                         OBJ(top_mem) {
3113                                 if (obj->data.top.type == TOP_NAME
3114                                     && obj->data.top.num >= 0
3115                                     && obj->data.top.num < 10) {
3116                                         // if we limit the buffer and add a bunch of space after, it stops the thing from
3117                                         // moving other shit around, which is really fucking annoying
3118                                         snprintf(p, 17,
3119                                                  "%s                              ",
3120                                                  cur->memu[obj->data.top.
3121                                                            num]->name);
3122                                 } else if (obj->data.top.type == TOP_CPU
3123                                            && obj->data.top.num >= 0
3124                                            && obj->data.top.num < 10) {
3125                                         snprintf(p, 7, "%3.2f      ",
3126                                                  cur->memu[obj->data.top.
3127                                                            num]->amount);
3128                                 } else if (obj->data.top.type == TOP_PID
3129                                            && obj->data.top.num >= 0
3130                                            && obj->data.top.num < 10) {
3131                                         snprintf(p, 8, "%i           ",
3132                                                  cur->memu[obj->data.top.
3133                                                            num]->pid);
3134                                 } else if (obj->data.top.type == TOP_MEM
3135                                            && obj->data.top.num >= 0
3136                                            && obj->data.top.num < 10) {
3137                                         snprintf(p, 7, "%3.2f       ",
3138                                                  cur->memu[obj->data.top.
3139                                                            num]->totalmem);
3140                                 }
3141                         }
3142
3143
3144                         OBJ(tail) {
3145                                 if (current_update_time -obj->data.tail.last_update >= obj->data.tail.interval) {
3146                                         obj->data.tail.last_update = current_update_time;
3147                                         FILE *fp;
3148                                         int i;
3149                                         int added = 0;
3150                                         tailstring *head = NULL;
3151                                         tailstring *headtmp = NULL;
3152                                         tailstring *freetmp = NULL;
3153                                         fp = fopen(obj->data.tail.logfile, "rt");
3154                                         if (fp == NULL) {
3155                                                 ERR("tail logfile failed to open");
3156                                         }
3157                                         else {
3158                                                 obj->data.tail.readlines = 0;
3159
3160                                                 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
3161                                                         if (added >= 30) {
3162                                                                 freelasttail(head);
3163                                                         }
3164                                                         else {
3165                                                                 added++;
3166                                                         }
3167                                                         addtail(&head, obj->data.tail.buffer);
3168                                                         obj->data.tail.readlines++;
3169                                                 }
3170
3171                                                 fclose(fp);
3172                                                 freetmp = head;
3173
3174                                                 if (obj->data.tail.readlines > 0) {
3175                                                         for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
3176                                                                 addtail(&headtmp, head->data);
3177                                                                 head = head->next;
3178                                                         }
3179                                                         freetail(freetmp);
3180                                                         freetmp = headtmp;
3181                                                         strcpy(obj->data.tail.buffer, headtmp->data);
3182                                                         headtmp = headtmp->next;
3183                                                         for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
3184                                                                 if (headtmp) {
3185                                                                         strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
3186                                                                         headtmp = headtmp->next;
3187                                                                 }
3188                                                         }
3189
3190                                                         /* get rid of any ugly newlines at the end */
3191                                                         if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
3192                                                                 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
3193                                                         }
3194
3195                                                         freetail(freetmp);
3196                                                 } else {
3197                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
3198                                                 }  /* if readlines */
3199                                         } /*  fp == NULL  */
3200                                 } /* if cur_upd_time >= */
3201         
3202                                 //parse_conky_vars(obj->data.tail.buffer, p, cur);
3203
3204                         }
3205                         OBJ(head) {
3206                                 if (current_update_time -obj->data.tail.last_update >= obj->data.tail.interval) {
3207                                         obj->data.tail.last_update = current_update_time;
3208                                         FILE *fp;
3209                                         tailstring *head = NULL;
3210                                         tailstring *headtmp = NULL;
3211                                         tailstring *freetmp = NULL;
3212                                         fp = fopen(obj->data.tail.logfile, "rt");
3213                                         if (fp == NULL) {
3214                                                 ERR("head logfile failed to open");
3215                                         } else {
3216                                                 obj->data.tail.readlines = 0;
3217                                                 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
3218                                                         addtail(&head, obj->data.tail.buffer);
3219                                                         obj->data.tail.readlines++;
3220                                                 }
3221                                                 fclose(fp);
3222                                                 freetmp = head;
3223                                                 if (obj->data.tail.readlines > 0) {
3224                                                         while (head) {
3225                                                                 addtail(&headtmp, head->data);
3226                                                                 head = head->next;
3227                                                         }
3228                                                         freetail(freetmp);
3229                                                         freetmp = headtmp;
3230                                                         strcpy(obj->data.tail.buffer, headtmp->data);
3231                                                         headtmp = headtmp->next;
3232                                                         while (headtmp) {
3233                                                                 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
3234                                                                 headtmp = headtmp->next;
3235                                                         }
3236                                                         freetail(freetmp);
3237                                                         /* get rid of any ugly newlines at the end */
3238                                                         if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
3239                                                                 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
3240                                                         }
3241                                                 } else {
3242                                                         strcpy(obj->data.tail.buffer, "Logfile Empty");
3243                                                 } /* if readlines > 0 */
3244                                         } /* if fp == null */
3245                                 } /* cur_upd_time >= */
3246
3247                                 //parse_conky_vars(obj->data.tail.buffer, p, cur);
3248
3249                         }
3250 #ifdef TCP_PORT_MONITOR
3251                         OBJ(tcp_portmon)
3252                         {
3253                                 /* grab a pointer to this port monitor */
3254                                 tcp_port_monitor_t * p_monitor = 
3255                                         find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
3256                                                                 obj->data.tcp_port_monitor.port_range_begin,
3257                                                                 obj->data.tcp_port_monitor.port_range_end );
3258                                 if ( !p_monitor ) {
3259                                         snprintf(p, p_max_size, "monitor not found");
3260                                         break;
3261                                 }
3262
3263                                 /* now grab the text of interest */
3264                                 if ( peek_tcp_port_monitor( p_monitor, 
3265                                                             obj->data.tcp_port_monitor.item, 
3266                                                             obj->data.tcp_port_monitor.connection_index,
3267                                                             p, p_max_size ) != 0 )
3268                                 {
3269                                         snprintf(p, p_max_size, "monitor peek error");
3270                                         break;
3271                                 }
3272                         }
3273 #endif
3274
3275                         break;
3276                 }
3277
3278                 {
3279                         unsigned int a = strlen(p);
3280                         p += a;
3281                         p_max_size -= a;
3282                 }
3283         }
3284 }
3285
3286
3287 double current_update_time, last_update_time;
3288
3289 static void generate_text()
3290 {
3291         struct information *cur = &info;
3292         char *p;
3293
3294         special_count = 0;
3295
3296         /* update info */
3297
3298         current_update_time = get_time();
3299
3300         update_stuff(cur);
3301
3302         /* add things to the buffer */
3303
3304         /* generate text */
3305
3306         p = text_buffer;
3307
3308         generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
3309
3310         if (stuff_in_upper_case) {
3311                 char *p;
3312
3313                 p = text_buffer;
3314                 while (*p) {
3315                         *p = toupper(*p);
3316                         p++;
3317                 }
3318         }
3319
3320         last_update_time = current_update_time;
3321         total_updates++;
3322         //free(p);
3323 }
3324
3325
3326 #ifdef X11
3327 static void set_font()
3328 {
3329 #ifdef XFT
3330         if (use_xft) {
3331                         if (window.xftdraw != NULL) {
3332                                 XftDrawDestroy(window.xftdraw);
3333                         }
3334                         window.xftdraw = XftDrawCreate(display, window.drawable,
3335                                         DefaultVisual(display,
3336                                                         screen),
3337                                         DefaultColormap(display,
3338                                                         screen));
3339                 } else
3340 #endif
3341 {
3342         XSetFont(display, window.gc, fonts[selected_font].font->fid);
3343 }
3344 }
3345
3346
3347 /*
3348  * text size
3349  */
3350
3351 static int text_start_x, text_start_y;  /* text start position in window */
3352 static int text_width, text_height;
3353
3354 #endif /* X11 */
3355
3356 static inline int get_string_width(const char *s)
3357 {
3358 #ifdef X11
3359         return *s ? calc_text_width(s, strlen(s)) : 0;
3360 #else
3361         return strlen(s);
3362 #endif /* X11 */
3363 }
3364
3365 static inline int get_string_width_special(char *s)
3366 {
3367         if (!s) {
3368                 return 0;
3369         }
3370 #ifdef X11
3371         char *p, *final;
3372         p = strdup(s);
3373         final = p;
3374         int index = 1;
3375         int width = 0;
3376         unsigned int i;
3377         while (*p) {
3378                 if (*p == SPECIAL_CHAR) {
3379                         /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
3380                         for (i = 0; i < strlen(p); i++) {
3381                                 *(p + i) = *(p + i + 1);
3382                         }
3383                         if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
3384                                 width += specials[special_index+index].width;
3385                         }
3386                         index++;
3387                 } else {
3388                         p++;
3389                 }
3390         }
3391         if (strlen(final) > 1) {
3392                 width += calc_text_width(final, strlen(final));
3393         }
3394         free(final);
3395         return width;
3396 #else
3397         return strlen(s);
3398 #endif /* X11 */
3399 }
3400
3401 int fontchange = 0;
3402
3403 #ifdef X11
3404 static void text_size_updater(char *s)
3405 {
3406         int w = 0;
3407         char *p;
3408         int h = font_height();
3409         /* get string widths and skip specials */
3410         p = s;
3411         while (*p) {
3412                 if (*p == SPECIAL_CHAR) {
3413                         *p = '\0';
3414                         w += get_string_width(s);
3415                         *p = SPECIAL_CHAR;
3416
3417                         if (specials[special_index].type == BAR
3418                             || specials[special_index].type == GRAPH) {
3419                                 w += specials[special_index].width;
3420                                 if (specials[special_index].height > h) {
3421                                         h = specials[special_index].height;
3422                                         h += font_ascent();
3423                                 }
3424                         }
3425                         
3426                         else if (specials[special_index].type == OFFSET) {
3427                                 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
3428                         }
3429                         else if (specials[special_index].type == VOFFSET) {
3430                                 h += specials[special_index].arg;
3431                         }
3432                         else if (specials[special_index].type == FONT) {
3433                                 fontchange = specials[special_index].font_added;
3434                                 selected_font = specials[special_index].font_added;
3435                                 h = font_height();
3436                         }
3437
3438                         
3439                         special_index++;
3440                         s = p + 1;
3441                 }
3442                 p++;
3443         }
3444                 w += get_string_width(s);
3445         if (w > text_width)
3446                 text_width = w;
3447         if (text_width > maximum_width && maximum_width)
3448                 text_width = maximum_width;
3449
3450         text_height += h;
3451         if (fontchange) {
3452                 selected_font = 0;
3453         }
3454 }
3455 #endif /* X11 */
3456
3457
3458 #ifdef X11
3459 static void update_text_area()
3460 {
3461         int x, y;
3462
3463         /* update text size if it isn't fixed */
3464 #ifdef OWN_WINDOW
3465         if (!fixed_size)
3466 #endif
3467         {
3468                 text_width = minimum_width;
3469                 text_height = 0;
3470                 special_index = 0;
3471                 for_each_line(text_buffer, text_size_updater);
3472                 text_width += 1;
3473                 if (text_height < minimum_height)
3474                         text_height = minimum_height;
3475                 if (text_width > maximum_width && maximum_width > 0)
3476                         text_width = maximum_width;
3477         }
3478
3479         /* get text position on workarea */
3480         switch (text_alignment) {
3481         case TOP_LEFT:
3482                 x = gap_x;
3483                 y = gap_y;
3484                 break;
3485
3486         case TOP_RIGHT:
3487                 x = workarea[2] - text_width - gap_x;
3488                 y = gap_y;
3489                 break;
3490
3491         default:
3492         case BOTTOM_LEFT:
3493                 x = gap_x;
3494                 y = workarea[3] - text_height - gap_y;
3495                 break;
3496
3497         case BOTTOM_RIGHT:
3498                 x = workarea[2] - text_width - gap_x;
3499                 y = workarea[3] - text_height - gap_y;
3500                 break;
3501         
3502 #ifdef OWN_WINDOW
3503         case NONE: // Let the WM manage the window
3504                 x = window.x;
3505                 y = window.y;
3506
3507                 fixed_pos  = 1;
3508                 fixed_size = 1;
3509                 break;
3510 #endif
3511         }
3512 #ifdef OWN_WINDOW
3513
3514         if (own_window && !fixed_pos) {
3515                 x += workarea[0];
3516                 y += workarea[1];
3517                 text_start_x = border_margin + 1;
3518                 text_start_y = border_margin + 1;
3519                 window.x = x - border_margin - 1;
3520                 window.y = y - border_margin - 1;
3521         } else
3522 #endif
3523         {
3524                 /* If window size doesn't match to workarea's size, then window
3525                  * probably includes panels (gnome).
3526                  * Blah, doesn't work on KDE. */
3527                 if (workarea[2] != window.width
3528                     || workarea[3] != window.height) {
3529                         y += workarea[1];
3530                         x += workarea[0];
3531                 }
3532
3533                 text_start_x = x;
3534                 text_start_y = y;
3535         }
3536 }
3537
3538 /*
3539  * drawing stuff
3540  */
3541
3542 static int cur_x, cur_y;        /* current x and y for drawing */
3543 static int draw_mode;           /* FG, BG or OUTLINE */
3544 static long current_color;
3545
3546 static inline void set_foreground_color(long c)
3547 {
3548         current_color = c;
3549         XSetForeground(display, window.gc, c);
3550 }
3551 #endif /* X11 */
3552
3553 static void draw_string(const char *s)
3554 {
3555         if (s[0] == '\0')
3556                 return;
3557         int i, i2, pos, width_of_s;
3558         int max=0;
3559         int added;
3560         width_of_s = get_string_width(s);
3561         if (out_to_console) {
3562                 printf("%s\n", s);
3563                 fflush(stdout);   /* output immediately, don't buffer */
3564         }
3565         /* daemon_run(s);  the daemon can be called here, but we need to have a buffer in daemon_run() and we need to tell it when everything is ready to be sent */
3566         memset(tmpstring1,0,TEXT_BUFFER_SIZE);
3567         memset(tmpstring2,0,TEXT_BUFFER_SIZE);
3568         strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
3569         pos = 0;
3570         added = 0;
3571         char space[2];
3572         snprintf(space, 2, " ");
3573 #ifdef X11
3574         max = ((text_width - width_of_s) / get_string_width(space));
3575 #endif /* X11 */
3576         /*
3577          * This code looks for tabs in the text and coverts them to spaces.
3578          * The trick is getting the correct number of spaces,
3579          * and not going over the window's size without forcing
3580          * the window larger.
3581          */
3582         for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
3583                 if (tmpstring1[i] == '\t')      // 9 is ascii tab
3584                 {
3585                         i2 = 0;
3586                         for (i2 = 0;
3587                              i2 < (8 - (1 + pos) % 8) && added <= max;
3588                              i2++) {
3589                                 /*
3590                                 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
3591                                         fprintf(stderr,"buffer overrun detected\n");
3592                                 */
3593                                 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
3594                                 added++;
3595                         }
3596                         pos += i2;
3597                 } else {
3598                         if (tmpstring1[i] != 9) {
3599                                 /*
3600                                 if ( pos > TEXT_BUFFER_SIZE-1 )
3601                                          fprintf(stderr,"buffer overrun detected\n");
3602                                 */
3603                                 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
3604                                 pos++;
3605                         }
3606                 }
3607         }
3608 #ifdef X11
3609         if (text_width == maximum_width) {
3610                 /* this means the text is probably pushing the limit, so we'll chop it */
3611                 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
3612                         tmpstring2[strlen(tmpstring2)-1] = '\0';
3613                 }
3614         }
3615 #endif /* X11 */
3616         s = tmpstring2;
3617 #ifdef X11
3618 #ifdef XFT
3619         if (use_xft) {
3620                 XColor c;
3621                 XftColor c2;
3622                 c.pixel = current_color;
3623                 XQueryColor(display, DefaultColormap(display, screen), &c);
3624
3625                 c2.pixel = c.pixel;
3626                 c2.color.red = c.red;
3627                 c2.color.green = c.green;
3628                 c2.color.blue = c.blue;
3629                 c2.color.alpha = fonts[selected_font].font_alpha;
3630                 if (utf8_mode) {
3631                         XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3632                                           cur_x, cur_y, (XftChar8 *) s,
3633                                           strlen(s));
3634                 } else {
3635                         XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3636                                        cur_x, cur_y, (XftChar8 *) s,
3637                                        strlen(s));
3638                 }
3639         } else
3640 #endif
3641         {
3642                 XDrawString(display, window.drawable, window.gc,
3643                             cur_x, cur_y, s, strlen(s));
3644         }
3645         cur_x += width_of_s;
3646 #endif /* X11 */
3647         memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
3648 }
3649
3650 long redmask, greenmask, bluemask;
3651
3652 void set_up_gradient()
3653 {
3654 #ifdef X11
3655         colour_depth = DisplayPlanes(display, screen);
3656 #else
3657         colour_depth = 16;
3658 #endif /* X11 */
3659         if (colour_depth != 24 && colour_depth != 16) {
3660                 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
3661         }
3662         int i;
3663         redmask = 0;
3664         greenmask = 0;
3665         bluemask = 0;
3666         for(i = (colour_depth / 3)-1; i>=0; i--) {
3667                 redmask |= 1 << i;
3668                 greenmask |= 1 << i;
3669                 bluemask |= 1 << i;
3670         }
3671         if (colour_depth%3 == 1) {
3672                 greenmask |= 1 << (colour_depth / 3);
3673         }
3674         redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
3675         greenmask = greenmask << (colour_depth / 3);
3676 }
3677
3678 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 */
3679         int tmp_color = 0;
3680         int red1, green1, blue1; // first colour
3681         int red2, green2, blue2; // second colour
3682         int red3 = 0, green3 = 0, blue3 = 0; // difference
3683         short redshift = (2*colour_depth / 3 + colour_depth%3);
3684         short greenshift = (colour_depth / 3);
3685         red1 = (first_colour & redmask) >> redshift;
3686         green1 = (first_colour & greenmask) >> greenshift;
3687         blue1 = first_colour & bluemask;
3688         red2 = (last_colour & redmask) >> redshift;
3689         green2 = (last_colour & greenmask) >> greenshift;
3690         blue2 = last_colour & bluemask;
3691         if (red1 > red2) {
3692                 red3 = -1;
3693         }
3694         if (red1 < red2) {
3695                 red3 = 1;
3696         }
3697         if (green1 > green2) {
3698                 green3 = -1;
3699         }
3700         if (green1 < green2) {
3701                 green3 = 1;
3702         }
3703         if (blue1 > blue2) {
3704                 blue3 = -1;
3705         }
3706         if (blue1 < blue2) {
3707                 blue3 = 1;
3708         }
3709         red1 += red3;
3710         green1 += green3;
3711         blue1 += blue3;
3712         if (red1 < 0) {
3713                 red1 = 0;
3714         }
3715         if (green1 < 0) {
3716                 green1 = 0;
3717         }
3718         if (blue1 < 0) {
3719                 blue1 = 0;
3720         }
3721         if (red1 > bluemask) {
3722                 red1 = bluemask;
3723         }
3724         if (green1 > bluemask) {
3725                 green1 = bluemask;
3726         }
3727         if (blue1 > bluemask) {
3728                 blue1 = bluemask;
3729         }
3730         tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
3731         return tmp_color;
3732 }
3733
3734 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
3735         if (colour_depth == 0) {
3736                 set_up_gradient();
3737         }
3738         int red1, green1, blue1; // first colour
3739         int red2, green2, blue2; // second colour
3740         long redshift = (2*colour_depth / 3 + colour_depth%3);
3741         long greenshift = (colour_depth / 3);
3742         int red3 = 0, green3 = 0, blue3 = 0; // difference
3743         red1 = (first_colour & redmask) >> redshift;
3744         green1 = (first_colour & greenmask) >> greenshift;
3745         blue1 = first_colour & bluemask;
3746         red2 = (last_colour & redmask) >> redshift;
3747         green2 = (last_colour & greenmask) >> greenshift;
3748         blue2 = last_colour & bluemask;
3749         red3 = abs(red1 - red2);
3750         green3 = abs(green1 - green2);
3751         blue3 = abs(blue1 - blue2);
3752         int max = red3;
3753         if (green3 > max)
3754                 max = green3;
3755         if (blue3 > max)
3756                 max = blue3;
3757         return max;
3758 }
3759
3760 static void draw_line(char *s)
3761 {
3762 #ifdef X11
3763         char *p;
3764         cur_x = text_start_x;
3765         cur_y += font_ascent();
3766         int cur_y_add = 0;
3767         short font_h = font_height();
3768
3769         /* find specials and draw stuff */
3770         p = s;
3771         while (*p) {
3772                 if (*p == SPECIAL_CHAR) {
3773                         int w = 0;
3774
3775                         /* draw string before special */
3776                         *p = '\0';
3777                         draw_string(s);
3778                         *p = SPECIAL_CHAR;
3779                         s = p + 1;
3780
3781                         /* draw special */
3782                         switch (specials[special_index].type) {
3783                         case HORIZONTAL_LINE:
3784                                 {
3785                                         int h =
3786                                             specials[special_index].height;
3787                                         int mid = font_ascent() / 2;
3788                                         w = text_start_x + text_width -
3789                                             cur_x;
3790
3791                                         XSetLineAttributes(display,
3792                                                            window.gc, h,
3793                                                            LineSolid,
3794                                                            CapButt,
3795                                                            JoinMiter);
3796                                         XDrawLine(display, window.drawable,
3797                                                   window.gc, cur_x,
3798                                                   cur_y - mid / 2,
3799                                                   cur_x + w,
3800                                                   cur_y - mid / 2);
3801                                 }
3802                                 break;
3803
3804                         case STIPPLED_HR:
3805                                 {
3806                                         int h =
3807                                             specials[special_index].height;
3808                                         int s =
3809                                             specials[special_index].arg;
3810                                         int mid = font_ascent() / 2;
3811                                         char ss[2] = { s, s };
3812                                         w = text_start_x + text_width -
3813                                             cur_x - 1;
3814
3815                                         XSetLineAttributes(display,
3816                                                            window.gc, h,
3817                                                            LineOnOffDash,
3818                                                            CapButt,
3819                                                            JoinMiter);
3820                                         XSetDashes(display, window.gc, 0,
3821                                                    ss, 2);
3822                                         XDrawLine(display, window.drawable,
3823                                                   window.gc, cur_x,
3824                                                   cur_y - mid / 2,
3825                                                   cur_x + w,
3826                                                   cur_y - mid / 2);
3827                                 }
3828                                 break;
3829
3830                         case BAR:
3831                                 {
3832                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3833                                                 break;
3834                                         }
3835                                         int h =
3836                                             specials[special_index].height;
3837                                         int bar_usage =
3838                                             specials[special_index].arg;
3839                                         int by;
3840
3841 #ifdef XFT
3842                                         if (use_xft) {
3843                                                 by = cur_y - (font_ascent() + h) / 2 - 1;
3844                                         } else 
3845 #endif
3846                                         {
3847                                                 by = cur_y - (font_ascent()/2) - 1;
3848                                         }
3849                                         if (h < (font_height())) {
3850                                                 by -= h / 2 - 1;
3851                                         }
3852                                         w = specials[special_index].width;
3853                                         if (w == 0)
3854                                                 w = text_start_x +
3855                                                     text_width - cur_x - 1;
3856                                         if (w < 0)
3857                                                 w = 0;
3858
3859                                         XSetLineAttributes(display,
3860                                                            window.gc, 1,
3861                                                            LineSolid,
3862                                                            CapButt,
3863                                                            JoinMiter);
3864
3865                                         XDrawRectangle(display,
3866                                                        window.drawable,
3867                                                        window.gc, cur_x,
3868                                                        by, w, h);
3869                                         XFillRectangle(display,
3870                                                        window.drawable,
3871                                                        window.gc, cur_x,
3872                                                        by,
3873                                                        w * bar_usage / 255,
3874                                                        h);
3875                                         if (specials[special_index].
3876                                             height > cur_y_add
3877                                             && specials[special_index].
3878                                             height > font_h) {
3879                                                 cur_y_add =
3880                                                     specials
3881                                                     [special_index].height;
3882                                         }
3883                                 }
3884                                 break;
3885
3886                         case GRAPH:
3887                         {
3888                                         if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3889                                                 break;
3890                                         }
3891                                         int h =
3892                                             specials[special_index].height;
3893                                         int by;
3894 #ifdef XFT
3895                                         if (use_xft) {
3896                                             by = cur_y - (font_ascent() + h) / 2 - 1;
3897                                         } else
3898 #endif
3899                                         {
3900                                                 by = cur_y - (font_ascent()/2) - 1;
3901                                         }
3902                                         if (h < (font_height())) {
3903                                                 by -= h / 2 - 1;
3904                                         }
3905                                         w = specials[special_index].width;
3906                                         if (w == 0)
3907                                                 w = text_start_x + text_width - cur_x - 1;
3908                                         if (w < 0)
3909                                                 w = 0;
3910                                         XSetLineAttributes(display,
3911                                                            window.gc, 1,
3912                                                            LineSolid,
3913                                                            CapButt,
3914                                                            JoinMiter);
3915                                         XDrawRectangle(display,
3916                                                        window.drawable,
3917                                                        window.gc, cur_x,
3918                                                        by, w, h);
3919                                         XSetLineAttributes(display,
3920                                                            window.gc, 1,
3921                                                            LineSolid,
3922                                                            CapButt,
3923                                                            JoinMiter);
3924         int i;
3925         int j = 0;
3926         int gradient_size = 0;
3927         float gradient_factor = 0;
3928         float gradient_update = 0;
3929         unsigned long tmpcolour = current_color;
3930         if (specials[special_index].first_colour != specials[special_index].last_colour) {
3931                 tmpcolour = specials[special_index].first_colour;
3932                 gradient_size = gradient_max(specials[special_index].first_colour, specials[special_index].last_colour);
3933                 gradient_factor = (float)gradient_size / (w - 3);
3934         }
3935         for (i = 0; i < w - 3; i++) {
3936                 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3937                         XSetForeground(display, window.gc, tmpcolour);
3938                         gradient_update += gradient_factor;
3939                         while (gradient_update > 0) {
3940                                 tmpcolour = do_gradient(tmpcolour, specials[special_index].last_colour);
3941                                 gradient_update--;
3942                         }
3943                 }
3944                 if (i / ((float) (w - 3) / (specials[special_index].graph_width)) > j) {
3945                         j++;
3946                                                 }
3947                                                 XDrawLine(display,  window.drawable, window.gc, cur_x + i + 2, by + h, cur_x + i + 2, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale);       /* this is mugfugly, but it works */
3948                                         }
3949                                         if (specials[special_index].
3950                                             height > cur_y_add
3951                                             && specials[special_index].
3952                                             height > font_h) {
3953                                                 cur_y_add =
3954                                                     specials
3955                                                     [special_index].height;
3956                                         }
3957                                 }
3958                                 if (draw_mode == BG) {
3959                                         set_foreground_color(default_bg_color);
3960                                 }
3961                                 else if (draw_mode == OUTLINE) {
3962                                         set_foreground_color(default_out_color);
3963                                 } else {
3964                                         set_foreground_color(default_fg_color);
3965                                 }
3966                                 break;
3967                         
3968                                 case FONT:
3969                                 if (fontchange) {
3970                                         cur_y -= font_ascent();
3971                                         selected_font = specials[special_index].font_added;
3972                                         cur_y += font_ascent();
3973 #ifdef XFT
3974                                         if (!use_xft || use_xdbe)
3975 #endif
3976                                         {
3977                                                 set_font();
3978                                         }
3979                                 }
3980                                 break;
3981                         case FG:
3982                                 if (draw_mode == FG)
3983                                         set_foreground_color(specials
3984                                                              [special_index].
3985                                                              arg);
3986                                 break;
3987
3988                         case BG:
3989                                 if (draw_mode == BG)
3990                                         set_foreground_color(specials
3991                                                              [special_index].
3992                                                              arg);
3993                                 break;
3994
3995                         case OUTLINE:
3996                                 if (draw_mode == OUTLINE)
3997                                         set_foreground_color(specials
3998                                                              [special_index].
3999                                                              arg);
4000                                 break;
4001
4002                                 case OFFSET:
4003                                 {
4004                                         w += specials[special_index].arg;
4005                                 }
4006                                 break;
4007                                 case VOFFSET:
4008                                 {
4009                                         cur_y += specials[special_index].arg;
4010                                 }
4011                                 break;
4012
4013                         case ALIGNR:
4014                                 {
4015                                         int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
4016                                         /*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);*/
4017                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
4018                                                 cur_x = pos_x - specials[special_index].arg;
4019                                 }
4020                                 }
4021                                 break;
4022
4023                         case ALIGNC:
4024                                 {
4025                                         int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
4026                                         /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
4027                                         /*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);*/
4028                                         if (pos_x >
4029                                             specials[special_index].arg)
4030                                                 w = pos_x -
4031                                                     specials
4032                                                     [special_index].arg;
4033                                 }
4034                                 break;
4035
4036                         }
4037
4038                         cur_x += w;
4039
4040                         special_index++;
4041                 }
4042
4043                 p++;
4044         }
4045 #else
4046         draw_string(s);
4047 #endif
4048 #ifdef X11
4049         if (cur_y_add > 0) {
4050                 cur_y += cur_y_add;
4051                 cur_y -= font_descent();
4052         }
4053
4054         draw_string(s);
4055
4056         cur_y += font_descent();
4057         if (fontchange) {
4058                 selected_font = 0;
4059         }
4060 #endif /* X11 */
4061 }
4062
4063 static void draw_text()
4064 {
4065 #ifdef X11
4066         cur_y = text_start_y;
4067
4068         /* draw borders */
4069         if (draw_borders && border_width > 0) {
4070                 unsigned int b = (border_width + 1) / 2;
4071
4072                 if (stippled_borders) {
4073                         char ss[2] =
4074                             { stippled_borders, stippled_borders };
4075                         XSetLineAttributes(display, window.gc,
4076                                            border_width, LineOnOffDash,
4077                                            CapButt, JoinMiter);
4078                         XSetDashes(display, window.gc, 0, ss, 2);
4079                 } else {
4080                         XSetLineAttributes(display, window.gc,
4081                                            border_width, LineSolid,
4082                                            CapButt, JoinMiter);
4083                 }
4084
4085                 XDrawRectangle(display, window.drawable, window.gc,
4086                                text_start_x - border_margin + b,
4087                                text_start_y - border_margin + b,
4088                                text_width + border_margin * 2 - 1 - b * 2,
4089                                text_height + border_margin * 2 - 1 -
4090                                b * 2);
4091         }
4092
4093         /* draw text */
4094         special_index = 0;
4095 #endif /* X11 */
4096         for_each_line(text_buffer, draw_line);
4097 }
4098
4099 static void draw_stuff()
4100 {
4101 #ifdef X11
4102         if (draw_shades && !draw_outline) {
4103                 text_start_x++;
4104                 text_start_y++;
4105                 set_foreground_color(default_bg_color);
4106                 draw_mode = BG;
4107                 draw_text();
4108                 text_start_x--;
4109                 text_start_y--;
4110         }
4111
4112         if (draw_outline) {
4113                 int i, j;
4114                 for (i = -1; i < 2; i++)
4115                         for (j = -1; j < 2; j++) {
4116                                 if (i == 0 && j == 0)
4117                                         continue;
4118                                 text_start_x += i;
4119                                 text_start_y += j;
4120                                 set_foreground_color(default_out_color);
4121                                 draw_mode = OUTLINE;
4122                                 draw_text();
4123                                 text_start_x -= i;
4124                                 text_start_y -= j;
4125                         }
4126         }
4127
4128         set_foreground_color(default_fg_color);
4129         draw_mode = FG;
4130 #endif /* X11 */
4131         draw_text();
4132 #ifdef X11
4133 #ifdef XDBE
4134         if (use_xdbe) {
4135                 XdbeSwapInfo swap;
4136                 swap.swap_window = window.window;
4137                 swap.swap_action = XdbeBackground;
4138                 XdbeSwapBuffers(display, &swap, 1);
4139         }
4140 #endif
4141 #endif /* X11 */
4142 }
4143 #ifdef X11
4144 static void clear_text(int exposures)
4145 {
4146 #ifdef XDBE
4147         if (use_xdbe) {
4148                 return;         /* The swap action is XdbeBackground, which clears */
4149         } else
4150 #endif
4151         {
4152         /* there is some extra space for borders and outlines */
4153         XClearArea(display, window.drawable,
4154                    text_start_x - border_margin - 1,
4155                    text_start_y - border_margin - 1,
4156                    text_width + border_margin * 2 + 2,
4157                    text_height + border_margin * 2 + 2,
4158                    exposures ? True : 0);
4159         }
4160 }
4161 #endif /* X11 */
4162
4163 static int need_to_update;
4164
4165 /* update_text() generates new text and clears old text area */
4166 static void update_text()
4167 {
4168         generate_text();
4169 #ifdef X11
4170         clear_text(1);
4171 #endif /* X11 */
4172         need_to_update = 1;
4173 }
4174
4175 static void main_loop()
4176 {
4177 #ifdef SIGNAL_BLOCKING
4178         sigset_t  newmask, oldmask;
4179
4180         sigemptyset(&newmask);
4181         sigaddset(&newmask,SIGINT);
4182         sigaddset(&newmask,SIGTERM);
4183         sigaddset(&newmask,SIGUSR1);
4184 #endif
4185
4186 #ifdef X11
4187         Region region = XCreateRegion();
4188 #endif /* X11 */
4189
4190         info.looped = 0;
4191         while (total_run_times == 0 || info.looped < total_run_times - 1) {
4192                 info.looped++;
4193
4194 #ifdef SIGNAL_BLOCKING
4195                 /* block signals.  we will inspect for pending signals later */
4196                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
4197                         CRIT_ERR("unable to sigprocmask()");
4198 #endif
4199
4200 #ifdef X11
4201                 XFlush(display);
4202
4203                 /* wait for X event or timeout */
4204
4205                 if (!XPending(display)) {
4206                         fd_set fdsr;
4207                         struct timeval tv;
4208                         int s;
4209                         double t =
4210                             update_interval - (get_time() -
4211                                                last_update_time);
4212
4213                         if (t < 0)
4214                                 t = 0;
4215
4216                         tv.tv_sec = (long) t;
4217                         tv.tv_usec = (long) (t * 1000000) % 1000000;
4218                         FD_ZERO(&fdsr);
4219                         FD_SET(ConnectionNumber(display), &fdsr);
4220
4221
4222                         s = select(ConnectionNumber(display) + 1, &fdsr, 0,
4223                                    0, &tv);
4224 #else
4225                         usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
4226 #endif /* X11 */
4227 #ifdef X11
4228                         if (s == -1) {
4229                                 if (errno != EINTR)
4230                                         ERR("can't select(): %s",
4231                                             strerror(errno));
4232                         } else {
4233                                 /* timeout */
4234                                 if (s == 0)
4235 #endif /* X11 */
4236                                         update_text();
4237 #ifdef X11
4238                         }
4239 #ifdef OWN_WINDOW
4240                         if (own_window) {
4241                                 set_transparent_background(window.window);
4242                         }
4243 #endif
4244                 }
4245                 
4246                 if (need_to_update) {
4247 #ifdef OWN_WINDOW
4248                         int wx = window.x, wy = window.y;
4249 #endif
4250
4251                         need_to_update = 0;
4252
4253                         update_text_area();
4254 #ifdef OWN_WINDOW
4255                         if (own_window) {
4256                                 /* resize window if it isn't right size */
4257                                 if (!fixed_size &&
4258                                     (text_width + border_margin * 2 !=
4259                                      window.width
4260                                      || text_height + border_margin * 2 !=
4261                                      window.height)) {
4262                                         window.width =
4263                                             text_width +
4264                                             border_margin * 2 + 1;
4265                                         window.height =
4266                                             text_height +
4267                                             border_margin * 2 + 1;
4268                                         XResizeWindow(display,
4269                                                       window.window,
4270                                                       window.width,
4271                                                       window.height);
4272                                      }
4273
4274                                 /* move window if it isn't in right position */
4275                                 if (!fixed_pos
4276                                     && (window.x != wx
4277                                         || window.y != wy)) {
4278                                         XMoveWindow(display, window.window,
4279                                                     window.x, window.y);
4280                                 }
4281                         }
4282 #endif
4283
4284                         clear_text(1);
4285
4286 #ifdef XDBE
4287                         if (use_xdbe) {
4288                                 XRectangle r;
4289                                 r.x = text_start_x - border_margin;
4290                                 r.y = text_start_y - border_margin;
4291                                 r.width = text_width + border_margin * 2;
4292                                 r.height = text_height + border_margin * 2;
4293                                 XUnionRectWithRegion(&r, region, region);
4294                         }
4295 #endif
4296                 }
4297
4298                 /* handle X events */
4299
4300                 while (XPending(display)) {
4301                         XEvent ev;
4302                         XNextEvent(display, &ev);
4303                         switch (ev.type) {
4304                         case Expose:
4305                                 {
4306                                         XRectangle r;
4307                                         r.x = ev.xexpose.x;
4308                                         r.y = ev.xexpose.y;
4309                                         r.width = ev.xexpose.width;
4310                                         r.height = ev.xexpose.height;
4311                                         XUnionRectWithRegion(&r, region,
4312                                                              region);
4313                                 }
4314                                 break;
4315
4316 #ifdef OWN_WINDOW
4317                         case ReparentNotify:
4318                                 /* set background to ParentRelative for all parents */
4319                                 if (own_window) {
4320                                         set_transparent_background(window.
4321                                         window);
4322                                 }
4323                                 break;
4324
4325                         case ConfigureNotify:
4326                                 if (own_window) {
4327                                         /* if window size isn't what expected, set fixed size */
4328                                         if (ev.xconfigure.width !=
4329                                             window.width
4330                                             || ev.xconfigure.height !=
4331                                             window.height) {
4332                                                 if (window.width != 0
4333                                                     && window.height != 0)
4334                                                         fixed_size = 1;
4335
4336                                                 /* clear old stuff before screwing up size and pos */
4337                                                 clear_text(1);
4338
4339                                                 {
4340                                                         XWindowAttributes
4341                                                             attrs;
4342                                                         if (XGetWindowAttributes(display, window.window, &attrs)) {
4343                                                                 window.
4344                                                                     width =
4345                                                                     attrs.
4346                                                                     width;
4347                                                                 window.
4348                                                                     height
4349                                                                     =
4350                                                                     attrs.
4351                                                                     height;
4352                                                         }
4353                                                 }
4354
4355                                                 text_width =
4356                                                     window.width -
4357                                                     border_margin * 2 - 1;
4358                                                 text_height =
4359                                                     window.height -
4360                                                     border_margin * 2 - 1;
4361                                                 if (text_width > maximum_width && maximum_width > 0)
4362                                                         text_width = maximum_width;
4363                                         }
4364
4365                                         /* if position isn't what expected, set fixed pos, total_updates
4366                                          * avoids setting fixed_pos when window is set to weird locations
4367                                          * when started */
4368                                         /*if (total_updates >= 2 this is broken
4369                                             && !fixed_pos
4370                                             && (window.x != ev.xconfigure.x
4371                                                 || window.y !=
4372                                                 ev.xconfigure.y)
4373                                             && (ev.xconfigure.x != 0
4374                                                 || ev.xconfigure.y != 0)) {
4375                                                 fixed_pos = 1;
4376                                 }*/
4377                                         set_font();
4378                                 }
4379                                 break;
4380 #endif
4381
4382                         default:
4383                                 break;
4384                         }
4385                 }
4386
4387                 /* XDBE doesn't seem to provide a way to clear the back buffer without
4388                  * interfering with the front buffer, other than passing XdbeBackground
4389                  * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
4390                  * redraw the text even if it wasn't part of the exposed area. OTOH,
4391                  * if we're not going to call draw_stuff at all, then no swap happens
4392                  * and we can safely do nothing.
4393                  */
4394
4395                 if (!XEmptyRegion(region)) {
4396 #ifdef XDBE
4397                         if (use_xdbe) {
4398                                 XRectangle r;
4399                                 r.x = text_start_x - border_margin;
4400                                 r.y = text_start_y - border_margin;
4401                                 r.width = text_width + border_margin * 2;
4402                                 r.height = text_height + border_margin * 2;
4403                                 XUnionRectWithRegion(&r, region, region);
4404                 }
4405 #endif
4406                         XSetRegion(display, window.gc, region);
4407 #ifdef XFT
4408                         if (use_xft) {
4409                                 XftDrawSetClip(window.xftdraw, region);
4410                         }
4411 #endif
4412 #endif /* X11 */
4413                         draw_stuff();
4414 #ifdef X11
4415                         XDestroyRegion(region);
4416                         region = XCreateRegion();
4417                 }
4418 #endif /* X11 */
4419
4420 #ifdef SIGNAL_BLOCKING
4421                 /* unblock signals of interest and let handler fly */
4422                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
4423                         CRIT_ERR("unable to sigprocmask()");
4424 #endif
4425
4426                 switch(g_signal_pending) {
4427                 case SIGUSR1:
4428                         {
4429                         ERR("received SIGUSR1. reloading the config file.");
4430                         reload_config();
4431                         break;
4432                         }
4433                 case SIGINT:
4434                 case SIGTERM:
4435                         {
4436                         ERR("received SIGINT or SIGTERM to terminate. bye!");
4437                         clean_up();
4438 #ifdef X11
4439                         XDestroyRegion(region);
4440                         region = NULL;
4441 #endif /* X11 */
4442                         return;  /* return from main_loop */
4443                         /*break;*/
4444                         }
4445                 default:
4446                         {
4447                         /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
4448                          * but didn't write any code to deal with it.   if you don't want to
4449                          * handle a signal, don't set a handler on it in the first place. */
4450                         if (g_signal_pending)
4451                                 ERR("ignoring signal (%d)", g_signal_pending);
4452                         break;
4453                         }
4454                 }
4455                 g_signal_pending=0;
4456         
4457         }
4458 #ifdef X11
4459         XDestroyRegion(region);
4460         region = NULL;
4461 #endif /* X11 */
4462 }
4463
4464 static void load_config_file(const char *);
4465
4466 /* reload the config file */
4467 void reload_config(void)
4468 {
4469         if (current_config) {
4470                 clear_fs_stats();
4471                 load_config_file(current_config);
4472 #ifdef X11
4473                 load_fonts();
4474                 set_font();
4475                 XClearWindow(display, RootWindow(display, screen)); // clear the window first
4476
4477 #endif /* X11 */
4478 #ifdef TCP_PORT_MONITOR
4479                 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
4480                 info.p_tcp_port_monitor_collection = NULL; 
4481 #endif
4482                 extract_variable_text(text);
4483                 free(text);
4484                 text = NULL;
4485                 update_text();
4486         }
4487 }
4488
4489 void clean_up(void)
4490 {
4491 #ifdef X11
4492 #ifdef XDBE
4493         if (use_xdbe) {
4494                 XdbeDeallocateBackBufferName(display, window.back_buffer);
4495         }
4496 #endif
4497 #ifdef OWN_WINDOW
4498         if (own_window)
4499                 XDestroyWindow(display, window.window);
4500         else
4501 #endif
4502         {
4503                 XClearWindow(display, RootWindow(display, screen));
4504                 clear_text(1);
4505                 XFlush(display);
4506         }
4507
4508         XFreeGC(display, window.gc);
4509 #endif /* X11 */
4510
4511
4512         /* it is really pointless to free() memory at the end of program but ak|ra
4513          * wants me to do this */
4514
4515         free_text_objects(text_object_count, text_objects);
4516         text_object_count = 0;
4517         text_objects = NULL;
4518
4519 #ifdef MLDONKEY 
4520         ml_cleanup();
4521 #endif /* MLDONKEY */
4522
4523         if (text != original_text)
4524                 free(text);
4525
4526         free(current_config);
4527         free(current_mail_spool);
4528 #ifdef SETI
4529         free(seti_dir);
4530 #endif
4531 #ifdef TCP_PORT_MONITOR
4532         destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
4533         info.p_tcp_port_monitor_collection = NULL;
4534 #endif
4535 }
4536
4537 static int string_to_bool(const char *s)
4538 {
4539         if (!s)
4540                 return 1;
4541         if (strcasecmp(s, "yes") == 0)
4542                 return 1;
4543         if (strcasecmp(s, "true") == 0)
4544                 return 1;
4545         if (strcasecmp(s, "1") == 0)
4546                 return 1;
4547         return 0;
4548 }
4549 #ifdef X11
4550 static enum alignment string_to_alignment(const char *s)
4551 {
4552         if (strcasecmp(s, "top_left") == 0)
4553                 return TOP_LEFT;
4554         else if (strcasecmp(s, "top_right") == 0)
4555                 return TOP_RIGHT;
4556         else if (strcasecmp(s, "bottom_left") == 0)
4557                 return BOTTOM_LEFT;
4558         else if (strcasecmp(s, "bottom_right") == 0)
4559                 return BOTTOM_RIGHT;
4560         else if (strcasecmp(s, "tl") == 0)
4561                 return TOP_LEFT;
4562         else if (strcasecmp(s, "tr") == 0)
4563                 return TOP_RIGHT;
4564         else if (strcasecmp(s, "bl") == 0)
4565                 return BOTTOM_LEFT;
4566         else if (strcasecmp(s, "br") == 0)
4567                 return BOTTOM_RIGHT;
4568         else if (strcasecmp(s, "none") == 0)
4569                 return NONE;
4570         return TOP_LEFT;
4571 }
4572 #endif /* X11 */
4573
4574
4575 static void set_default_configurations(void)
4576 {
4577         fork_to_background = 0;
4578         total_run_times = 0;
4579         info.cpu_avg_samples = 2;
4580         info.net_avg_samples = 2;
4581         info.memmax = 0;
4582         top_cpu = 0;
4583         top_mem = 0;
4584 #ifdef MPD
4585         strcpy(info.mpd.host, "localhost");
4586         info.mpd.port = 6600;
4587         info.mpd.status = "Checking status...";
4588 #endif
4589         use_spacer = 0;
4590 #ifdef X11
4591         out_to_console = 0;
4592 #else
4593         out_to_console = 1;
4594 #endif
4595 #ifdef X11
4596         default_fg_color = WhitePixel(display, screen);
4597         default_bg_color = BlackPixel(display, screen);
4598         default_out_color = BlackPixel(display, screen);
4599         draw_borders = 0;
4600         draw_shades = 1;
4601         draw_outline = 0;
4602         set_first_font("6x10");
4603         gap_x = 5;
4604         gap_y = 5;
4605         minimum_width = 5;
4606         minimum_height = 5;
4607         maximum_width = 0;
4608 #ifdef OWN_WINDOW
4609         own_window = 0;
4610         strcpy(wm_class_name, "conky"); 
4611 #endif
4612         stippled_borders = 0;
4613         border_margin = 3;
4614         border_width = 1;
4615         text_alignment = BOTTOM_LEFT;
4616         on_bottom = 1;
4617 #endif /* X11 */
4618
4619         free(current_mail_spool);
4620         {
4621                 char buf[256];
4622                 variable_substitute(MAIL_FILE, buf, 256);
4623                 if (buf[0] != '\0')
4624                         current_mail_spool = strdup(buf);
4625         }
4626
4627         no_buffers = 1;
4628         update_interval = 10.0;
4629         stuff_in_upper_case = 0;
4630 #ifdef MLDONKEY
4631         mlconfig.mldonkey_hostname = strdup("127.0.0.1");
4632         mlconfig.mldonkey_port = 4001;
4633         mlconfig.mldonkey_login = NULL;
4634         mlconfig.mldonkey_password = NULL;
4635 #endif
4636
4637 #ifdef TCP_PORT_MONITOR
4638         tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
4639         tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
4640 #endif
4641 }
4642
4643 static void load_config_file(const char *f)
4644 {
4645 #define CONF_ERR ERR("%s: %d: config file error", f, line)
4646         int line = 0;
4647         FILE *fp;
4648
4649         set_default_configurations();
4650         fp = fopen(f, "r");
4651         if (!fp)
4652                 return;
4653
4654         while (!feof(fp)) {
4655                 char buf[256], *p, *p2, *name, *value;
4656                 line++;
4657                 if (fgets(buf, 256, fp) == NULL)
4658                         break;
4659
4660                 p = buf;
4661
4662                 /* break at comment */
4663                 p2 = strchr(p, '#');
4664                 if (p2)
4665                         *p2 = '\0';
4666
4667                 /* skip spaces */
4668                 while (*p && isspace((int) *p))
4669                         p++;
4670                 if (*p == '\0')
4671                         continue;       /* empty line */
4672
4673                 name = p;
4674
4675                 /* skip name */
4676                 p2 = p;
4677                 while (*p2 && !isspace((int) *p2))
4678                         p2++;
4679                 if (*p2 != '\0') {
4680                         *p2 = '\0';     /* break at name's end */
4681                         p2++;
4682                 }
4683
4684                 /* get value */
4685                 if (*p2) {
4686                         p = p2;
4687                         while (*p && isspace((int) *p))
4688                                 p++;
4689
4690                         value = p;
4691
4692                         p2 = value + strlen(value);
4693                         while (isspace((int) *(p2 - 1)))
4694                                 *--p2 = '\0';
4695                 } else {
4696                         value = 0;
4697                 }
4698
4699 #define CONF2(a) if (strcasecmp(name, a) == 0)
4700 #define CONF(a) else CONF2(a)
4701 #define CONF3(a,b) \
4702 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
4703
4704
4705 #ifdef X11
4706                 CONF2("alignment") {
4707         if (value) {
4708                 int a = string_to_alignment(value);
4709                 if (a <= 0)
4710                         CONF_ERR;
4711                 else
4712                         text_alignment = a;
4713         } else
4714                 CONF_ERR;
4715                 }
4716                 CONF("on_bottom") {
4717                         if(value)
4718                                 on_bottom = string_to_bool(value);
4719                         else
4720                                 CONF_ERR;
4721                 }
4722                 CONF("background") {
4723                         fork_to_background = string_to_bool(value);
4724                 }
4725
4726 #else
4727                 CONF2("background") {
4728         fork_to_background = string_to_bool(value);
4729                 }
4730 #endif /* X11 */
4731 #ifdef X11
4732                 CONF("border_margin") {
4733                         if (value)
4734                                 border_margin = strtol(value, 0, 0);
4735                         else
4736                                 CONF_ERR;
4737                 }
4738                 CONF("border_width") {
4739                         if (value)
4740                                 border_width = strtol(value, 0, 0);
4741                         else
4742                                 CONF_ERR;
4743                 }
4744                 CONF("default_color") {
4745                         if (value)
4746                                 default_fg_color = get_x11_color(value);
4747                         else
4748                                 CONF_ERR;
4749                 }
4750                 CONF3("default_shade_color", "default_shadecolor") {
4751                         if (value)
4752                                 default_bg_color = get_x11_color(value);
4753                         else
4754                                 CONF_ERR;
4755                 }
4756                 CONF3("default_outline_color", "default_outlinecolor") {
4757                         if (value)
4758                                 default_out_color = get_x11_color(value);
4759                         else
4760                                 CONF_ERR;
4761                 }
4762 #endif /* X11 */
4763 #ifdef MPD
4764                 CONF("mpd_host") {
4765                         if (value)
4766                                 strcpy(info.mpd.host, value);
4767                         else
4768                                 CONF_ERR;
4769                 }
4770                 CONF("mpd_port") {
4771                         if (value) {
4772                                 info.mpd.port = strtol(value, 0, 0);
4773                                 if (info.mpd.port < 1
4774                                     || info.mpd.port > 0xffff)
4775                                         CONF_ERR;
4776                         }
4777                 }
4778 #endif
4779                 CONF("cpu_avg_samples") {
4780                         if (value) {
4781                                 cpu_avg_samples = strtol(value, 0, 0);
4782                                 if (cpu_avg_samples < 1
4783                                     || cpu_avg_samples > 14)
4784                                         CONF_ERR;
4785                                 else
4786                                         info.
4787                                             cpu_avg_samples
4788                                             = cpu_avg_samples;
4789                         } else
4790                                 CONF_ERR;
4791                 }
4792                 CONF("net_avg_samples") {
4793                         if (value) {
4794                                 net_avg_samples = strtol(value, 0, 0);
4795                                 if (net_avg_samples < 1
4796                                     || net_avg_samples > 14)
4797                                         CONF_ERR;
4798                                 else
4799                                         info.
4800                                             net_avg_samples
4801                                             = net_avg_samples;
4802                         } else
4803                                 CONF_ERR;
4804                 }
4805
4806
4807
4808
4809
4810
4811 #ifdef XDBE
4812                 CONF("double_buffer") {
4813                 use_xdbe = string_to_bool(value);
4814                 }
4815 #endif
4816 #ifdef X11
4817                 CONF("override_utf8_locale") {
4818         utf8_mode = string_to_bool(value);
4819                 }
4820
4821                 CONF("draw_borders") {
4822                         draw_borders = string_to_bool(value);
4823                 }
4824                 CONF("draw_shades") {
4825                         draw_shades = string_to_bool(value);
4826                 }
4827                 CONF("draw_outline") {
4828                         draw_outline = string_to_bool(value);
4829                 }
4830 #endif /* X11 */
4831                 CONF("out_to_console") {
4832                         out_to_console = string_to_bool(value);
4833                 }
4834                 CONF("use_spacer") {
4835                         use_spacer = string_to_bool(value);
4836                 }
4837 #ifdef X11
4838 #ifdef XFT
4839                 CONF("use_xft") {
4840                         use_xft = string_to_bool(value);
4841                 }
4842                 CONF("font") {
4843                         if (!use_xft) {
4844                                 if (value) {
4845                                         set_first_font(value);
4846                                 } else
4847                                         CONF_ERR;
4848                         }
4849                 }
4850                 CONF("xftalpha") {
4851                         if (value && font_count >= 0)
4852                                 fonts[0].font_alpha = atof(value)
4853                                     * 65535.0;
4854                         else
4855                                 CONF_ERR;
4856                 }
4857                 CONF("xftfont") {
4858 #else
4859                 CONF("use_xft") {
4860                         if (string_to_bool(value))
4861                                 ERR("Xft not enabled");
4862                 }
4863                 CONF("xftfont") {
4864                         /* xftfont silently ignored when no Xft */
4865                 }
4866                 CONF("xftalpha") {
4867                         /* xftalpha is silently ignored when no Xft */
4868                 }
4869                 CONF("font") {
4870 #endif
4871                         if (value) {
4872                                 set_first_font(value);
4873                         } else
4874                                 CONF_ERR;
4875                 }
4876                 CONF("gap_x") {
4877                         if (value)
4878                                 gap_x = atoi(value);
4879                         else
4880                                 CONF_ERR;
4881                 }
4882                 CONF("gap_y") {
4883                         if (value)
4884                                 gap_y = atoi(value);
4885                         else
4886                                 CONF_ERR;
4887                 }
4888 #endif /* X11 */
4889                 CONF("mail_spool") {
4890                         if (value) {
4891                                 char buf[256];
4892                                 variable_substitute(value, buf, 256);
4893
4894                                 if (buf[0]
4895                                     != '\0') {
4896                                         if (current_mail_spool)
4897                                                 free(current_mail_spool);
4898                                         current_mail_spool = strdup(buf);
4899                                 }
4900                         } else
4901                                 CONF_ERR;
4902                 }
4903 #ifdef X11
4904                 CONF("minimum_size") {
4905         if (value) {
4906                 if (sscanf
4907                                   (value, "%d %d", &minimum_width,
4908                                    &minimum_height) != 2)
4909                         if (sscanf
4910                                                  (value, "%d",
4911                                                    &minimum_width) != 1)
4912                                 CONF_ERR;
4913         } else
4914                 CONF_ERR;
4915                 }
4916                 CONF("maximum_width") {
4917                         if (value) {
4918                                 if (sscanf(value, "%d", &maximum_width) != 1)
4919                                         CONF_ERR;
4920                         } else
4921                                 CONF_ERR;
4922                 }
4923 #endif /* X11 */
4924                 CONF("no_buffers") {
4925                         no_buffers = string_to_bool(value);
4926                 }
4927 #ifdef MLDONKEY
4928                 CONF("mldonkey_hostname") {
4929                         if (value) {
4930                                 if (mlconfig.mldonkey_hostname != NULL) {
4931                                         free(mlconfig.mldonkey_hostname);
4932                                 }
4933                         mlconfig.mldonkey_hostname = strdup(value);
4934                         }
4935                         else
4936                                 CONF_ERR;
4937                 }
4938                 CONF("mldonkey_port") {
4939                         if (value)
4940                                 mlconfig.mldonkey_port = atoi(value);
4941                         else
4942                                 CONF_ERR;
4943                 }
4944                 CONF("mldonkey_login") {
4945                         if (value) {
4946                                 if (mlconfig.mldonkey_login != NULL) {
4947                                         free(mlconfig.mldonkey_login);
4948                                 }
4949                                 mlconfig.mldonkey_login = strdup(value);
4950                         }
4951                         else
4952                                 CONF_ERR;
4953                 }
4954                 CONF("mldonkey_password") {
4955                         if (value) {
4956                                 if (mlconfig.mldonkey_password != NULL) {
4957                                         free(mlconfig.mldonkey_password);
4958                                 }
4959                                 mlconfig.mldonkey_password = strdup(value);
4960                         }
4961                         else
4962                                 CONF_ERR;
4963                 }
4964 #endif
4965                 CONF("pad_percents") {
4966         pad_percents = atoi(value);
4967                 }
4968 #ifdef X11
4969 #ifdef OWN_WINDOW
4970                 CONF("own_window") {
4971                         own_window = string_to_bool(value);
4972                 }
4973                 CONF("wm_class_name") {
4974                         strncpy(wm_class_name, value, sizeof(wm_class_name)-1);
4975                         wm_class_name[sizeof(wm_class_name)-1] = 0;
4976                 }
4977                 CONF("own_window_transparent") {
4978                         set_transparent = string_to_bool(value);
4979                 }
4980                 CONF("own_window_colour") {
4981                         background_colour = get_x11_color(value);
4982                 }
4983 #endif
4984                 CONF("stippled_borders") {
4985                         if (value)
4986                                 stippled_borders = strtol(value, 0, 0);
4987                         else
4988                                 stippled_borders = 4;
4989                 }
4990 #endif /* X11 */
4991                 CONF("temp1") {
4992                         ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
4993                 }
4994                 CONF("temp1") {
4995                         ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
4996                 }
4997                 CONF("update_interval") {
4998                         if (value)
4999                                 update_interval = strtod(value, 0);
5000                         else
5001                                 CONF_ERR;
5002                 }
5003                 CONF("total_run_times") {
5004                         if (value)
5005                                 total_run_times = strtod(value, 0);
5006                         else
5007                                 CONF_ERR;
5008                 }
5009                 CONF("uppercase") {
5010                         stuff_in_upper_case = string_to_bool(value);
5011                 }
5012 #ifdef SETI
5013                 CONF("seti_dir") {
5014                         seti_dir = (char *)
5015                             malloc(strlen(value)
5016                                    + 1);
5017                         strcpy(seti_dir, value);
5018                 }
5019 #endif
5020                 CONF("text") {
5021                         if (text != original_text)
5022                                 free(text);
5023
5024                         text = (char *)
5025                             malloc(1);
5026                         text[0]
5027                             = '\0';
5028
5029                         while (!feof(fp)) {
5030                                 unsigned
5031                                 int l = strlen(text);
5032                                 if (fgets(buf, 256, fp) == NULL)
5033                                         break;
5034                                 text = (char *)
5035                                     realloc(text, l + strlen(buf)
5036                                             + 1);
5037                                 strcat(text, buf);
5038
5039                                 if (strlen(text) > 1024 * 8)
5040                                         break;
5041                         }
5042                         fclose(fp);
5043                         return;
5044                 }
5045 #ifdef TCP_PORT_MONITOR
5046                 CONF("min_port_monitors") 
5047                 {
5048                         if ( !value || 
5049                              (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) || 
5050                              tcp_port_monitor_collection_args.min_port_monitors < 0 )
5051                         {
5052                                 /* an error. use default, warn and continue. */
5053                                 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
5054                                 CONF_ERR;
5055                         }
5056                         else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
5057                         {
5058                                 /* no error, just use default */
5059                                 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
5060                         }
5061                         /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
5062                 }
5063                 CONF("min_port_monitor_connections") 
5064                 {
5065                         if ( !value || 
5066                              (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1) 
5067                              || tcp_port_monitor_args.min_port_monitor_connections < 0 )
5068                         {
5069                                 /* an error. use default, warni and continue. */
5070                                 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
5071                                 CONF_ERR;
5072                         }
5073                         else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
5074                         {
5075                                 /* no error, just use default */
5076                                 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
5077                         }
5078                         /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
5079                 }
5080 #endif
5081                 else
5082                 ERR("%s: %d: no such configuration: '%s'", f, line, name);
5083
5084 #undef CONF
5085 #undef CONF2
5086         }
5087
5088         fclose(fp);
5089 #undef CONF_ERR
5090 }
5091
5092                                                                                                                                                                                         /* : means that character before that takes an argument */
5093 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
5094 #ifdef X11
5095                 "x:y:w:a:f:"
5096 #ifdef OWN_WINDOW
5097     "o"
5098 #endif
5099 #ifdef XDBE
5100     "b"
5101 #endif
5102 #endif /* X11 */
5103     ;
5104
5105
5106 int main(int argc, char **argv)
5107 {
5108         struct sigaction act, oact;
5109
5110         g_signal_pending=0;
5111         memset(&info, 0, sizeof(info) );
5112
5113 #ifdef TCP_PORT_MONITOR
5114         tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
5115         tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
5116 #endif
5117                 
5118         /* handle command line parameters that don't change configs */
5119 #ifdef X11
5120         char *s;
5121         char temp[10];
5122         unsigned int x;
5123
5124         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && 
5125                      *s) || ((s = getenv("LANG")) && *s)) {
5126                 strcpy(temp, s);
5127                 for(x = 0; x < strlen(s) ; x++) {
5128                         temp[x] = tolower(s[x]);
5129                 }
5130                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5131                         utf8_mode = 1;
5132                 }
5133         }
5134         if (!setlocale(LC_CTYPE, "")) {
5135                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5136         }
5137 #endif /* X11 */
5138         while (1) {
5139                 int c = getopt(argc,
5140                                argv,
5141                                getopt_string);
5142                 if (c == -1)
5143                         break;
5144
5145                 switch (c) {
5146                 case 'v':
5147                 case 'V':
5148                         printf
5149                             ("Conky " VERSION " compiled " __DATE__ "\n");
5150                         return 0;
5151
5152                 case 'c':
5153                         /* if current_config is set to a strdup of CONFIG_FILE, free it (even
5154                          * though free() does the NULL check itself;), then load optarg value */
5155                         if (current_config)
5156                                 free(current_config);
5157                         current_config = strdup(optarg);
5158                         break;
5159
5160                 case 'h':
5161                         printf
5162                                         ("Usage: %s [OPTION]...\n"
5163                                         "Conky is a system monitor that renders text on desktop or to own transparent\n"
5164                                         "window. Command line options will override configurations defined in config\n"
5165                                         "file.\n"
5166                                         "   -V            version\n"
5167                                         "   -c FILE       config file to load instead of "
5168                                         CONFIG_FILE
5169                                         "\n"
5170                                         "   -d            daemonize, fork to background\n"
5171                                         "   -h            help\n"
5172 #ifdef X11
5173                                         "   -a ALIGNMENT  text alignment on screen, {top,bottom}_{left,right}\n"
5174                                         "   -f FONT       font to use\n"
5175 #ifdef OWN_WINDOW
5176                                         "   -o            create own window to draw\n"
5177 #endif
5178 #ifdef XDBE
5179                                         "   -b            double buffer (prevents flickering)\n"
5180 #endif
5181                                         "   -w WIN_ID     window id to draw\n"
5182                                         "   -x X          x position\n"
5183                                         "   -y Y          y position\n"
5184 #endif /* X11 */
5185                                         "   -t TEXT       text to render, remember single quotes, like -t '$uptime'\n"
5186                                         "   -u SECS       update interval\n"
5187                                         "   -i NUM        number of times to update Conky\n", argv[0]);
5188                         return 0;
5189 #ifdef X11
5190                 case 'w':
5191                         window.window = strtol(optarg, 0, 0);
5192                         break;
5193 #endif /* X11 */
5194
5195                 case '?':
5196                         exit(EXIT_FAILURE);
5197                 }
5198         }
5199 #ifdef X11
5200         /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
5201         init_X11();
5202 #endif /* X11 */
5203
5204         /* load current_config or CONFIG_FILE */
5205
5206 #ifdef CONFIG_FILE
5207         if (current_config == NULL) {
5208                 /* load default config file */
5209                 char buf[256];
5210
5211                 variable_substitute(CONFIG_FILE, buf, 256);
5212
5213                 if (buf[0] != '\0')
5214                         current_config = strdup(buf);
5215         }
5216 #endif
5217
5218         if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
5219                 load_config_file(current_config);
5220         else { 
5221                 set_default_configurations();
5222         }
5223
5224 #ifdef MAIL_FILE
5225         if (current_mail_spool == NULL) {
5226                 char buf[256];
5227                 variable_substitute(MAIL_FILE, buf, 256);
5228
5229                 if (buf[0] != '\0')
5230                         current_mail_spool = strdup(buf);
5231         }
5232 #endif
5233
5234         /* handle other command line arguments */
5235
5236 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
5237         optind = optreset = 1;
5238 #else
5239         optind = 0;
5240 #endif
5241         
5242         while (1) {
5243                 int c = getopt(argc,
5244                                argv,
5245                                getopt_string);
5246                 if (c == -1)
5247                         break;
5248
5249                 switch (c) {
5250                 case 'd':
5251                         fork_to_background = 1;
5252                         break;
5253
5254 #ifdef X11
5255                         case 'f':
5256                         set_first_font(optarg);
5257                         break;
5258                         case 'a':
5259                                 text_alignment = string_to_alignment(optarg);
5260                                 break;
5261
5262 #ifdef OWN_WINDOW
5263                 case 'o':
5264                         own_window = 1;
5265                         break;
5266 #endif
5267 #ifdef XDBE
5268                 case 'b':
5269                                 use_xdbe = 1;
5270                         break;
5271 #endif
5272 #endif /* X11 */
5273                 case 't':
5274                         if (text != original_text)
5275                                 free(text);
5276                         text = strdup(optarg);
5277                         convert_escapes(text);
5278                         break;
5279
5280                 case 'u':
5281                         update_interval = strtod(optarg, 0);
5282                         break;
5283
5284                 case 'i':
5285                         total_run_times = strtod(optarg, 0);
5286                         break;
5287 #ifdef X11
5288                 case 'x':
5289                         gap_x = atoi(optarg);
5290                         break;
5291
5292                 case 'y':
5293                         gap_y = atoi(optarg);
5294                         break;
5295 #endif /* X11 */
5296
5297                 case '?':
5298                         exit(EXIT_FAILURE);
5299                 }
5300         }
5301
5302 #ifdef X11
5303         /* load font */
5304         load_fonts();
5305 #endif /* X11 */
5306
5307         /* generate text and get initial size */
5308         extract_variable_text(text);
5309         if (text != original_text) {
5310                 free(text);
5311         }
5312         text = NULL;
5313
5314         update_uname();
5315
5316         generate_text();
5317 #ifdef X11
5318         update_text_area();     /* to get initial size of the window */
5319
5320 #if defined OWN_WINDOW
5321         init_window
5322             (own_window,
5323              wm_class_name,
5324              text_width + border_margin * 2 + 1,
5325              text_height + border_margin * 2 + 1,
5326              on_bottom, fixed_pos, set_transparent, background_colour, info.uname_s.nodename);
5327 #else
5328         init_window
5329                 (own_window,
5330                  text_width + border_margin * 2 + 1,
5331                  text_height + border_margin * 2 + 1,
5332                  on_bottom, set_transparent, background_colour, info.uname_s.nodename);
5333         
5334 #endif
5335
5336         update_text_area();     /* to position text/window on screen */
5337 #endif /* X11 */
5338
5339 /*#ifdef CAIRO
5340 // why the fuck not?
5341 //do_it();
5342 #endif*/
5343
5344 #ifdef X11
5345 #ifdef OWN_WINDOW
5346         if (own_window && !fixed_pos) {
5347                 XMoveWindow(display, window.window, window.x, window.y);
5348         }
5349         if (own_window) {
5350                 set_transparent_background(window.window);
5351         }
5352 #endif
5353
5354         create_gc();
5355
5356         set_font();
5357         draw_stuff();
5358 #endif /* X11 */
5359
5360         /* fork */
5361         if (fork_to_background) {
5362                 int ret = fork();
5363                 switch (ret) {
5364                 case -1:
5365                         ERR("can't fork() to background: %s",
5366                             strerror(errno));
5367                         break;
5368
5369                 case 0:
5370                         break;
5371
5372                 default:
5373                         fprintf
5374                             (stderr,
5375                              "Conky: forked to background, pid is %d\n",
5376                              ret);
5377                         return 0;
5378                 }
5379         }
5380
5381         /* Set signal handlers */
5382         act.sa_handler = signal_handler;
5383         sigemptyset(&act.sa_mask);
5384         act.sa_flags = 0;
5385 #ifdef SA_RESTART
5386         act.sa_flags |= SA_RESTART;
5387 #endif
5388
5389         if ( sigaction(SIGINT,&act,&oact) < 0 ||
5390              sigaction(SIGUSR1,&act,&oact) < 0 ||
5391              sigaction(SIGTERM,&act,&oact) < 0 )
5392         {
5393                 ERR("error setting signal handler: %s", strerror(errno) );
5394         }
5395
5396         main_loop();
5397
5398         return 0;
5399 }
5400
5401 void signal_handler(int sig)
5402 {
5403         /* signal handler is light as a feather, as it should be. 
5404          * we will poll g_signal_pending with each loop of conky
5405          * and do any signal processing there, NOT here.  pkovacs. */
5406         g_signal_pending=sig;
5407 }