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