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