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