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