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