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