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