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