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