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