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