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