Allow ~/... and $HOME/... paths for more stuff.
[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;
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                                                 #ifdef HAVE_OPENMP
3918                                                 #pragma omp parallel for
3919                                                 #endif /* HAVE_OPENMP */
3920                                                 for(i=i /* cheats */; i < obj->a; i++) {
3921                                                         *(p+i)='_';
3922                                                 }
3923                                                 *(p+i)=0;
3924 #ifdef X11
3925                                         }
3926 #endif
3927                                 }
3928                         }
3929 #ifdef X11
3930                         OBJ(execgraph) {
3931                                 char showaslog = FALSE;
3932                                 char tempgrad = FALSE;
3933                                 double barnum;
3934                                 char *cmd = obj->data.s;
3935
3936                                 if (strncasecmp(obj->data.execi.cmd, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
3937                                         showaslog = TRUE;
3938                                         cmd = cmd + strlen(LOGGRAPH" ") * sizeof(char);
3939                                 } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
3940                                         cmd = cmd + strlen(NORMGRAPH" ") * sizeof(char);
3941                                 }
3942                                 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
3943                                         tempgrad = TRUE;
3944                                         cmd += strlen(" "TEMPGRAD);
3945                                 }
3946                                 read_exec(cmd, p, text_buffer_size);
3947                                 barnum = get_barnum(p);
3948
3949                                 if (barnum >= 0.0) {
3950                                         new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
3951                                                         100, 1, showaslog, tempgrad);
3952                                 }
3953                         }
3954                         OBJ(execibar) {
3955                                 if (current_update_time - obj->data.execi.last_update
3956                                                 >= obj->data.execi.interval) {
3957                                         double barnum;
3958
3959                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
3960                                         barnum = get_barnum(p);
3961
3962                                         if (barnum >= 0.0) {
3963                                                 obj->f = 255 * barnum / 100.0;
3964                                         }
3965                                         obj->data.execi.last_update = current_update_time;
3966                                 }
3967                                 new_bar(p, obj->a, obj->b, round_to_int(obj->f));
3968                         }
3969                         OBJ(execigraph) {
3970                                 if (current_update_time - obj->data.execi.last_update
3971                                                 >= obj->data.execi.interval) {
3972                                         double barnum;
3973                                         char showaslog = FALSE;
3974                                         char tempgrad = FALSE;
3975                                         char *cmd = obj->data.execi.cmd;
3976
3977                                         if (strncasecmp(obj->data.execi.cmd, LOGGRAPH" ", strlen(LOGGRAPH" ")) == EQUAL) {
3978                                                 showaslog = TRUE;
3979                                                 cmd = cmd + strlen(LOGGRAPH" ") * sizeof(char);
3980                                         } else if(strncasecmp(obj->data.s, NORMGRAPH" ", strlen(NORMGRAPH" ")) == EQUAL) {
3981                                                 cmd = cmd + strlen(NORMGRAPH" ") * sizeof(char);
3982                                         }
3983                                         if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
3984                                                 tempgrad = TRUE;
3985                                                 cmd += strlen(" "TEMPGRAD);
3986                                         }
3987                                         obj->char_a = showaslog;
3988                                         obj->char_b = tempgrad;
3989                                         read_exec(cmd, p, text_buffer_size);
3990                                         barnum = get_barnum(p);
3991
3992                                         if (barnum >= 0.0) {
3993                                                 obj->f = barnum;
3994                                         }
3995                                         obj->data.execi.last_update = current_update_time;
3996                                 }
3997                                 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
3998                         }
3999                         OBJ(execigauge) {
4000                                 if (current_update_time - obj->data.execi.last_update
4001                                                 >= obj->data.execi.interval) {
4002                                         double barnum;
4003
4004                                         read_exec(obj->data.execi.cmd, p, text_buffer_size);
4005                                         barnum = get_barnum(p);
4006
4007                                         if (barnum >= 0.0) {
4008                                                 obj->f = 255 * barnum / 100.0;
4009                                         }
4010                                         obj->data.execi.last_update = current_update_time;
4011                                 }
4012                                 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4013                         }
4014 #endif
4015                         OBJ(execi) {
4016                                 if (current_update_time - obj->data.execi.last_update
4017                                                 >= obj->data.execi.interval
4018                                                 && obj->data.execi.interval != 0) {
4019                                         read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4020                                                 text_buffer_size);
4021                                         obj->data.execi.last_update = current_update_time;
4022                                 }
4023                                 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4024                         }
4025                         OBJ(execpi) {
4026                                 struct text_object subroot;
4027                                 struct information *tmp_info =
4028                                         malloc(sizeof(struct information));
4029                                 memcpy(tmp_info, cur, sizeof(struct information));
4030
4031                                 if (current_update_time - obj->data.execi.last_update
4032                                                 < obj->data.execi.interval
4033                                                 || obj->data.execi.interval == 0) {
4034                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4035                                 } else {
4036                                         char *output = obj->data.execi.buffer;
4037                                         FILE *fp = popen(obj->data.execi.cmd, "r");
4038                                         int length = fread(output, 1, text_buffer_size, fp);
4039
4040                                         pclose(fp);
4041
4042                                         output[length] = '\0';
4043                                         if (length > 0 && output[length - 1] == '\n') {
4044                                                 output[length - 1] = '\0';
4045                                         }
4046
4047                                         parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4048                                         obj->data.execi.last_update = current_update_time;
4049                                 }
4050                                 free_text_objects(&subroot, 1);
4051                                 free(tmp_info);
4052                         }
4053                         OBJ(texeci) {
4054                                 if (!obj->data.texeci.p_timed_thread) {
4055                                         obj->data.texeci.p_timed_thread =
4056                                                 timed_thread_create(&threaded_exec,
4057                                                 (void *) obj, obj->data.texeci.interval * 1000000);
4058                                         if (!obj->data.texeci.p_timed_thread) {
4059                                                 ERR("Error creating texeci timed thread");
4060                                         }
4061                                         timed_thread_register(obj->data.texeci.p_timed_thread,
4062                                                 &obj->data.texeci.p_timed_thread);
4063                                         if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4064                                                 ERR("Error running texeci timed thread");
4065                                         }
4066                                 } else {
4067                                         timed_thread_lock(obj->data.texeci.p_timed_thread);
4068                                         snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4069                                         timed_thread_unlock(obj->data.texeci.p_timed_thread);
4070                                 }
4071                         }
4072 #endif /* HAVE_POPEN */
4073                         OBJ(imap_unseen) {
4074                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4075
4076                                 if (mail && mail->p_timed_thread) {
4077                                         timed_thread_lock(mail->p_timed_thread);
4078                                         snprintf(p, p_max_size, "%lu", mail->unseen);
4079                                         timed_thread_unlock(mail->p_timed_thread);
4080                                 }
4081                         }
4082                         OBJ(imap_messages) {
4083                                 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4084
4085                                 if (mail && mail->p_timed_thread) {
4086                                         timed_thread_lock(mail->p_timed_thread);
4087                                         snprintf(p, p_max_size, "%lu", mail->messages);
4088                                         timed_thread_unlock(mail->p_timed_thread);
4089                                 }
4090                         }
4091                         OBJ(pop3_unseen) {
4092                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4093
4094                                 if (mail && mail->p_timed_thread) {
4095                                         timed_thread_lock(mail->p_timed_thread);
4096                                         snprintf(p, p_max_size, "%lu", mail->unseen);
4097                                         timed_thread_unlock(mail->p_timed_thread);
4098                                 }
4099                         }
4100                         OBJ(pop3_used) {
4101                                 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4102
4103                                 if (mail && mail->p_timed_thread) {
4104                                         timed_thread_lock(mail->p_timed_thread);
4105                                         snprintf(p, p_max_size, "%.1f",
4106                                                 mail->used / 1024.0 / 1024.0);
4107                                         timed_thread_unlock(mail->p_timed_thread);
4108                                 }
4109                         }
4110 #ifdef X11
4111                         OBJ(fs_bar) {
4112                                 if (obj->data.fs != NULL) {
4113                                         if (obj->data.fs->size == 0) {
4114                                                 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4115                                         } else {
4116                                                 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4117                                                         (int) (255 - obj->data.fsbar.fs->avail * 255 /
4118                                                         obj->data.fs->size));
4119                                         }
4120                                 }
4121                         }
4122 #endif
4123                         OBJ(fs_free) {
4124                                 if (obj->data.fs != NULL) {
4125                                         human_readable( (obj->data.fs->free ? obj->data.fs->free :
4126                                                                 obj->data.fs->avail), p, 255);
4127                                 }
4128                         }
4129                         OBJ(fs_free_perc) {
4130                                 if (obj->data.fs != NULL) {
4131                                         int val = 0;
4132
4133                                         if (obj->data.fs->size) {
4134                                                 val = (obj->data.fs->free ? obj->data.fs->free :
4135                                                                 obj->data.fs->avail) * 100 /
4136                                                         obj->data.fs->size;
4137                                         }
4138
4139                                         percent_print(p, p_max_size, val);
4140                                 }
4141                         }
4142                         OBJ(fs_size) {
4143                                 if (obj->data.fs != NULL) {
4144                                         human_readable(obj->data.fs->size, p, 255);
4145                                 }
4146                         }
4147                         OBJ(fs_type) {
4148                                 if (obj->data.fs != NULL)
4149                                         snprintf(p, p_max_size, "%s", obj->data.fs->type);
4150                         }
4151                         OBJ(fs_used) {
4152                                 if (obj->data.fs != NULL) {
4153                                         human_readable(obj->data.fs->size - (obj->data.fs->free
4154                                                 ? obj->data.fs->free : obj->data.fs->avail), p, 255);
4155                                 }
4156                         }
4157 #ifdef X11
4158                         OBJ(fs_bar_free) {
4159                                 if (obj->data.fs != NULL) {
4160                                         if (obj->data.fs->size == 0) {
4161                                                 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4162                                         } else {
4163                                                 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4164                                                         (int) (obj->data.fsbar.fs->avail * 255 /
4165                                                         obj->data.fs->size));
4166                                         }
4167                                 }
4168                         }
4169 #endif
4170                         OBJ(fs_used_perc) {
4171                                 if (obj->data.fs != NULL) {
4172                                         int val = 0;
4173
4174                                         if (obj->data.fs->size) {
4175                                                 val = (obj->data.fs->free ? obj->data.fs->free :
4176                                                                 obj->data.fs->avail) * 100 /
4177                                                         obj->data.fs->size;
4178                                         }
4179
4180                                         percent_print(p, p_max_size, 100 - val);
4181                                 }
4182                         }
4183                         OBJ(loadavg) {
4184                                 float *v = info.loadavg;
4185
4186                                 if (obj->data.loadavg[2]) {
4187                                         snprintf(p, p_max_size, "%.2f %.2f %.2f",
4188                                                 v[obj->data.loadavg[0] - 1],
4189                                                 v[obj->data.loadavg[1] - 1],
4190                                                 v[obj->data.loadavg[2] - 1]);
4191                                 } else if (obj->data.loadavg[1]) {
4192                                         snprintf(p, p_max_size, "%.2f %.2f",
4193                                                 v[obj->data.loadavg[0] - 1],
4194                                                 v[obj->data.loadavg[1] - 1]);
4195                                 } else if (obj->data.loadavg[0]) {
4196                                         snprintf(p, p_max_size, "%.2f",
4197                                                 v[obj->data.loadavg[0] - 1]);
4198                                 }
4199                         }
4200                         OBJ(goto) {
4201                                 new_goto(p, obj->data.i);
4202                         }
4203                         OBJ(tab) {
4204                                 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4205                         }
4206 #ifdef X11
4207                         OBJ(hr) {
4208                                 new_hr(p, obj->data.i);
4209                         }
4210 #endif
4211                         OBJ(nameserver) {
4212                                 if (cur->nameserver_info.nscount > obj->data.i)
4213                                         snprintf(p, p_max_size, "%s",
4214                                                         cur->nameserver_info.ns_list[obj->data.i]);
4215                         }
4216 #ifdef EVE
4217                         OBJ(eve) {
4218                                 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4219                                 snprintf(p, p_max_size, "%s", skill);
4220                         }
4221 #endif
4222 #ifdef RSS
4223                         OBJ(rss) {
4224                                 PRSS *data = get_rss_info(obj->data.rss.uri,
4225                                         obj->data.rss.delay);
4226                                 char *str;
4227
4228                                 if (data == NULL) {
4229                                         snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4230                                 } else {
4231                                         if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4232                                                 str = data->title;
4233                                                 // remove trailing new line if one exists
4234                                                 if (str[strlen(str) - 1] == '\n') {
4235                                                         str[strlen(str) - 1] = 0;
4236                                                 }
4237                                                 snprintf(p, p_max_size, "%s", str);
4238                                         } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4239                                                 if (obj->data.rss.act_par < data->item_count) {
4240                                                         str = data->items[obj->data.rss.act_par].title;
4241                                                         // remove trailing new line if one exists
4242                                                         if (str[strlen(str) - 1] == '\n') {
4243                                                                 str[strlen(str) - 1] = 0;
4244                                                         }
4245                                                         snprintf(p, p_max_size, "%s", str);
4246                                                 }
4247                                         } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4248                                                 if (obj->data.rss.act_par < data->item_count) {
4249                                                         str =
4250                                                                 data->items[obj->data.rss.act_par].description;
4251                                                         // remove trailing new line if one exists
4252                                                         if (str[strlen(str) - 1] == '\n') {
4253                                                                 str[strlen(str) - 1] = 0;
4254                                                         }
4255                                                         snprintf(p, p_max_size, "%s", str);
4256                                                 }
4257                                         } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4258                                                 if (data->item_count > 0) {
4259                                                         int itmp;
4260                                                         int show;
4261                                                         //'tmpspaces' is a string with spaces too be placed in front of each title
4262                                                         char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
4263                                                         memset(tmpspaces, ' ', obj->data.rss.nrspaces);
4264                                                         tmpspaces[obj->data.rss.nrspaces]=0;
4265
4266                                                         p[0] = 0;
4267
4268                                                         if (obj->data.rss.act_par > data->item_count) {
4269                                                                 show = data->item_count;
4270                                                         } else {
4271                                                                 show = obj->data.rss.act_par;
4272                                                         }
4273                                                         for (itmp = 0; itmp < show; itmp++) {
4274                                                                 PRSS_Item *item = &data->items[itmp];
4275
4276                                                                 str = item->title;
4277                                                                 if (str) {
4278                                                                         // don't add new line before first item
4279                                                                         if (itmp > 0) {
4280                                                                                 strncat(p, "\n", p_max_size);
4281                                                                         }
4282                                                                         /* remove trailing new line if one exists,
4283                                                                          * we have our own */
4284                                                                         if (str[strlen(str) - 1] == '\n') {
4285                                                                                 str[strlen(str) - 1] = 0;
4286                                                                         }
4287                                                                         strncat(p, tmpspaces, p_max_size);
4288                                                                         strncat(p, str, p_max_size);
4289                                                                 }
4290                                                         }
4291                                                         free(tmpspaces);
4292                                                 }
4293                                         }
4294                                 }
4295                         }
4296 #endif
4297 #ifdef HAVE_LUA
4298                         OBJ(lua) {
4299                                 char *str = llua_getstring(obj->data.s);
4300                                 if (str) {
4301                                         snprintf(p, p_max_size, "%s", str);
4302                                         free(str);
4303                                 }
4304                         }
4305                         OBJ(lua_parse) {
4306                                 char *str = llua_getstring(obj->data.s);
4307                                 if (str) {
4308                                         struct information *tmp_info;
4309                                         struct text_object subroot;
4310
4311                                         tmp_info = malloc(sizeof(struct information));
4312                                         memcpy(tmp_info, cur, sizeof(struct information));
4313                                         parse_conky_vars(&subroot, str, p, tmp_info);
4314
4315                                         free_text_objects(&subroot, 1);
4316                                         free(tmp_info);
4317                                         free(str);
4318                                 }
4319                         }
4320                         OBJ(lua_read_parse) {
4321                                 struct information *tmp_info;
4322                                 struct text_object subroot, subroot2;
4323                                 char func[64];
4324                                 char *text, *str;
4325                                 sscanf(obj->data.s, "%64s", func);
4326                                 text = obj->data.s + strlen(func) + 1;
4327
4328                                 tmp_info = malloc(sizeof(struct information));
4329                                 memcpy(tmp_info, cur, sizeof(struct information));
4330                                 parse_conky_vars(&subroot, text, p, tmp_info);
4331                                 DBGP("evaluated '%s' to '%s'", text, p);
4332
4333                                 str = llua_getstring_read(func, p);
4334                                 if (str) {
4335                                         parse_conky_vars(&subroot2, str, p, tmp_info);
4336                                         DBGP("evaluated '%s' to '%s'", str, p);
4337
4338                                         free(str);
4339                                         free_text_objects(&subroot2, 1);
4340                                 }
4341                                 free_text_objects(&subroot, 1);
4342                                 free(tmp_info);
4343                         }
4344 #ifdef X11
4345                         OBJ(lua_bar) {
4346                                 int per;
4347                                 if (llua_getinteger(obj->data.s, &per)) {
4348                                         new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4349                                 }
4350                         }
4351                         OBJ(lua_graph) {
4352                                 int per;
4353                                 if (llua_getinteger(obj->data.s, &per)) {
4354                                         new_graph(p, obj->a, obj->b, obj->c, obj->d,
4355                                                         (per/100.0 * 255), 100, 1, obj->char_a, obj->char_b);
4356                                 }
4357                         }
4358                         OBJ(lua_gauge) {
4359                                 int per;
4360                                 if (llua_getinteger(obj->data.s, &per)) {
4361                                         new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4362                                 }
4363                         }
4364 #endif /* X11 */
4365 #endif /* HAVE_LUA */
4366 #ifdef HDDTEMP
4367                         OBJ(hddtemp) {
4368                                 char *endptr, unit;
4369                                 long val;
4370                                 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4371                                         if (obj->data.hddtemp.temp)
4372                                                 free(obj->data.hddtemp.temp);
4373                                         obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4374                                                         obj->data.hddtemp.addr, obj->data.hddtemp.port);
4375                                         obj->data.hddtemp.update_time = current_update_time;
4376                                 }
4377                                 if (!obj->data.hddtemp.temp) {
4378                                         snprintf(p, p_max_size, "N/A");
4379                                 } else {
4380                                         val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4381                                         unit = obj->data.hddtemp.temp[0];
4382
4383                                         if (*endptr != '\0')
4384                                                 snprintf(p, p_max_size, "N/A");
4385                                         else if (unit == 'C')
4386                                                 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4387                                         else if (unit == 'F')
4388                                                 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4389                                         else
4390                                                 snprintf(p, p_max_size, "N/A");
4391                                 }
4392                         }
4393 #endif
4394                         OBJ(offset) {
4395                                 new_offset(p, obj->data.i);
4396                         }
4397                         OBJ(voffset) {
4398                                 new_voffset(p, obj->data.i);
4399                         }
4400 #ifdef __linux__
4401                         OBJ(i2c) {
4402                                 double r;
4403
4404                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4405                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4406
4407                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4408
4409                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4410                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4411                                 } else if (r >= 100.0 || r == 0) {
4412                                         snprintf(p, p_max_size, "%d", (int) r);
4413                                 } else {
4414                                         snprintf(p, p_max_size, "%.1f", r);
4415                                 }
4416                         }
4417                         OBJ(platform) {
4418                                 double r;
4419
4420                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4421                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4422
4423                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4424
4425                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4426                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4427                                 } else if (r >= 100.0 || r == 0) {
4428                                         snprintf(p, p_max_size, "%d", (int) r);
4429                                 } else {
4430                                         snprintf(p, p_max_size, "%.1f", r);
4431                                 }
4432                         }
4433                         OBJ(hwmon) {
4434                                 double r;
4435
4436                                 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4437                                         obj->data.sysfs.devtype, obj->data.sysfs.type);
4438
4439                                 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4440
4441                                 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4442                                         temp_print(p, p_max_size, r, TEMP_CELSIUS);
4443                                 } else if (r >= 100.0 || r == 0) {
4444                                         snprintf(p, p_max_size, "%d", (int) r);
4445                                 } else {
4446                                         snprintf(p, p_max_size, "%.1f", r);
4447                                 }
4448                         }
4449 #endif /* __linux__ */
4450                         OBJ(alignr) {
4451                                 new_alignr(p, obj->data.i);
4452                         }
4453                         OBJ(alignc) {
4454                                 new_alignc(p, obj->data.i);
4455                         }
4456                         OBJ(if_empty) {
4457                                 char buf[max_user_text];
4458                                 struct information *tmp_info =
4459                                         malloc(sizeof(struct information));
4460                                 memcpy(tmp_info, cur, sizeof(struct information));
4461                                 generate_text_internal(buf, max_user_text,
4462                                                        *obj->sub, tmp_info);
4463
4464                                 if (strlen(buf) != 0) {
4465                                         DO_JUMP;
4466                                 }
4467                                 free(tmp_info);
4468                         }
4469                         OBJ(if_match) {
4470                                 char expression[max_user_text];
4471                                 int val;
4472                                 struct information *tmp_info;
4473
4474                                 tmp_info = malloc(sizeof(struct information));
4475                                 memcpy(tmp_info, cur, sizeof(struct information));
4476                                 generate_text_internal(expression, max_user_text,
4477                                                        *obj->sub, tmp_info);
4478                                 DBGP("parsed arg into '%s'", expression);
4479
4480                                 val = compare(expression);
4481                                 if (val == -2) {
4482                                         ERR("compare failed for expression '%s'",
4483                                                         expression);
4484                                 } else if (!val) {
4485                                         DO_JUMP;
4486                                 }
4487                                 free(tmp_info);
4488                         }
4489                         OBJ(if_existing) {
4490                                 if (obj->data.ifblock.str
4491                                     && !check_contains(obj->data.ifblock.s,
4492                                                        obj->data.ifblock.str)) {
4493                                         DO_JUMP;
4494                                 } else if (obj->data.ifblock.s
4495                                            && access(obj->data.ifblock.s, F_OK)) {
4496                                         DO_JUMP;
4497                                 }
4498                         }
4499                         OBJ(if_mounted) {
4500                                 if ((obj->data.ifblock.s)
4501                                                 && (!check_mount(obj->data.ifblock.s))) {
4502                                         DO_JUMP;
4503                                 }
4504                         }
4505                         OBJ(if_running) {
4506                                 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4507                                         DO_JUMP;
4508                                 }
4509                         }
4510 #if defined(__linux__)
4511                         OBJ(ioscheduler) {
4512                                 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4513                         }
4514 #endif
4515                         OBJ(kernel) {
4516                                 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4517                         }
4518                         OBJ(machine) {
4519                                 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4520                         }
4521
4522                         /* memory stuff */
4523                         OBJ(mem) {
4524                                 human_readable(cur->mem * 1024, p, 255);
4525                         }
4526                         OBJ(memeasyfree) {
4527                                 human_readable(cur->memeasyfree * 1024, p, 255);
4528                         }
4529                         OBJ(memfree) {
4530                                 human_readable(cur->memfree * 1024, p, 255);
4531                         }
4532                         OBJ(memmax) {
4533                                 human_readable(cur->memmax * 1024, p, 255);
4534                         }
4535                         OBJ(memperc) {
4536                                 if (cur->memmax)
4537                                         percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4538                         }
4539 #ifdef X11
4540                         OBJ(memgauge){
4541                                 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4542                                         cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4543                         }
4544                         OBJ(membar) {
4545                                 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4546                                         cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4547                         }
4548                         OBJ(memgraph) {
4549                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4550                                         cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
4551                                         100, 1, obj->char_a, obj->char_b);
4552                         }
4553 #endif
4554                         /* mixer stuff */
4555                         OBJ(mixer) {
4556                                 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
4557                         }
4558                         OBJ(mixerl) {
4559                                 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
4560                         }
4561                         OBJ(mixerr) {
4562                                 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
4563                         }
4564 #ifdef X11
4565                         OBJ(mixerbar) {
4566                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4567                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
4568                         }
4569                         OBJ(mixerlbar) {
4570                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4571                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
4572                         }
4573                         OBJ(mixerrbar) {
4574                                 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
4575                                         mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
4576                         }
4577 #endif
4578                         OBJ(if_mixer_mute) {
4579                                 if (!mixer_is_mute(obj->data.ifblock.i)) {
4580                                         DO_JUMP;
4581                                 }
4582                         }
4583 #ifdef X11
4584                         OBJ(monitor) {
4585                                 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
4586                         }
4587                         OBJ(monitor_number) {
4588                                 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
4589                         }
4590 #endif
4591
4592                         /* mail stuff */
4593                         OBJ(mails) {
4594                                 update_mail_count(&obj->data.local_mail);
4595                                 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
4596                         }
4597                         OBJ(new_mails) {
4598                                 update_mail_count(&obj->data.local_mail);
4599                                 snprintf(p, p_max_size, "%d",
4600                                         obj->data.local_mail.new_mail_count);
4601                         }
4602                         OBJ(seen_mails) {
4603                                 update_mail_count(&obj->data.local_mail);
4604                                 snprintf(p, p_max_size, "%d",
4605                                         obj->data.local_mail.seen_mail_count);
4606                         }
4607                         OBJ(unseen_mails) {
4608                                 update_mail_count(&obj->data.local_mail);
4609                                 snprintf(p, p_max_size, "%d",
4610                                         obj->data.local_mail.unseen_mail_count);
4611                         }
4612                         OBJ(flagged_mails) {
4613                                 update_mail_count(&obj->data.local_mail);
4614                                 snprintf(p, p_max_size, "%d",
4615                                         obj->data.local_mail.flagged_mail_count);
4616                         }
4617                         OBJ(unflagged_mails) {
4618                                 update_mail_count(&obj->data.local_mail);
4619                                 snprintf(p, p_max_size, "%d",
4620                                         obj->data.local_mail.unflagged_mail_count);
4621                         }
4622                         OBJ(forwarded_mails) {
4623                                 update_mail_count(&obj->data.local_mail);
4624                                 snprintf(p, p_max_size, "%d",
4625                                         obj->data.local_mail.forwarded_mail_count);
4626                         }
4627                         OBJ(unforwarded_mails) {
4628                                 update_mail_count(&obj->data.local_mail);
4629                                 snprintf(p, p_max_size, "%d",
4630                                         obj->data.local_mail.unforwarded_mail_count);
4631                         }
4632                         OBJ(replied_mails) {
4633                                 update_mail_count(&obj->data.local_mail);
4634                                 snprintf(p, p_max_size, "%d",
4635                                         obj->data.local_mail.replied_mail_count);
4636                         }
4637                         OBJ(unreplied_mails) {
4638                                 update_mail_count(&obj->data.local_mail);
4639                                 snprintf(p, p_max_size, "%d",
4640                                         obj->data.local_mail.unreplied_mail_count);
4641                         }
4642                         OBJ(draft_mails) {
4643                                 update_mail_count(&obj->data.local_mail);
4644                                 snprintf(p, p_max_size, "%d",
4645                                         obj->data.local_mail.draft_mail_count);
4646                         }
4647                         OBJ(trashed_mails) {
4648                                 update_mail_count(&obj->data.local_mail);
4649                                 snprintf(p, p_max_size, "%d",
4650                                         obj->data.local_mail.trashed_mail_count);
4651                         }
4652                         OBJ(mboxscan) {
4653                                 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
4654                                         text_buffer_size);
4655                                 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
4656                         }
4657                         OBJ(nodename) {
4658                                 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
4659                         }
4660                         OBJ(outlinecolor) {
4661                                 new_outline(p, obj->data.l);
4662                         }
4663                         OBJ(processes) {
4664                                 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
4665                         }
4666                         OBJ(running_processes) {
4667                                 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
4668                         }
4669                         OBJ(text) {
4670                                 snprintf(p, p_max_size, "%s", obj->data.s);
4671                         }
4672 #ifdef X11
4673                         OBJ(shadecolor) {
4674                                 new_bg(p, obj->data.l);
4675                         }
4676                         OBJ(stippled_hr) {
4677                                 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
4678                         }
4679 #endif
4680                         OBJ(swap) {
4681                                 human_readable(cur->swap * 1024, p, 255);
4682                         }
4683                         OBJ(swapmax) {
4684                                 human_readable(cur->swapmax * 1024, p, 255);
4685                         }
4686                         OBJ(swapperc) {
4687                                 if (cur->swapmax == 0) {
4688                                         strncpy(p, "No swap", p_max_size);
4689                                 } else {
4690                                         percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
4691                                 }
4692                         }
4693 #ifdef X11
4694                         OBJ(swapbar) {
4695                                 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4696                                         cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
4697                         }
4698 #endif
4699                         OBJ(sysname) {
4700                                 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4701                         }
4702                         OBJ(time) {
4703                                 time_t t = time(NULL);
4704                                 struct tm *tm = localtime(&t);
4705
4706                                 setlocale(LC_TIME, "");
4707                                 strftime(p, p_max_size, obj->data.s, tm);
4708                         }
4709                         OBJ(utime) {
4710                                 time_t t = time(NULL);
4711                                 struct tm *tm = gmtime(&t);
4712
4713                                 strftime(p, p_max_size, obj->data.s, tm);
4714                         }
4715                         OBJ(tztime) {
4716                                 char *oldTZ = NULL;
4717                                 time_t t;
4718                                 struct tm *tm;
4719
4720                                 if (obj->data.tztime.tz) {
4721                                         oldTZ = getenv("TZ");
4722                                         setenv("TZ", obj->data.tztime.tz, 1);
4723                                         tzset();
4724                                 }
4725                                 t = time(NULL);
4726                                 tm = localtime(&t);
4727
4728                                 setlocale(LC_TIME, "");
4729                                 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4730                                 if (oldTZ) {
4731                                         setenv("TZ", oldTZ, 1);
4732                                         tzset();
4733                                 } else {
4734                                         unsetenv("TZ");
4735                                 }
4736                                 // Needless to free oldTZ since getenv gives ptr to static data
4737                         }
4738                         OBJ(totaldown) {
4739                                 human_readable(obj->data.net->recv, p, 255);
4740                         }
4741                         OBJ(totalup) {
4742                                 human_readable(obj->data.net->trans, p, 255);
4743                         }
4744                         OBJ(updates) {
4745                                 snprintf(p, p_max_size, "%d", total_updates);
4746                         }
4747                         OBJ(if_updatenr) {
4748                                 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
4749                                         DO_JUMP;
4750                                 }
4751                         }
4752                         OBJ(upspeed) {
4753                                 human_readable(obj->data.net->trans_speed, p, 255);
4754                         }
4755                         OBJ(upspeedf) {
4756                                 spaced_print(p, p_max_size, "%.1f", 8,
4757                                         obj->data.net->trans_speed / 1024.0);
4758                         }
4759 #ifdef X11
4760                         OBJ(upspeedgraph) {
4761                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4762                                         obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4763                         }
4764 #endif
4765                         OBJ(uptime_short) {
4766                                 format_seconds_short(p, p_max_size, (int) cur->uptime);
4767                         }
4768                         OBJ(uptime) {
4769                                 format_seconds(p, p_max_size, (int) cur->uptime);
4770                         }
4771                         OBJ(user_names) {
4772                                 snprintf(p, p_max_size, "%s", cur->users.names);
4773                         }
4774                         OBJ(user_terms) {
4775                                 snprintf(p, p_max_size, "%s", cur->users.terms);
4776                         }
4777                         OBJ(user_times) {
4778                                 snprintf(p, p_max_size, "%s", cur->users.times);
4779                         }
4780                         OBJ(user_number) {
4781                                 snprintf(p, p_max_size, "%d", cur->users.number);
4782                         }
4783 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
4784                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
4785                         OBJ(apm_adapter) {
4786                                 char *msg;
4787
4788                                 msg = get_apm_adapter();
4789                                 snprintf(p, p_max_size, "%s", msg);
4790                                 free(msg);
4791                         }
4792                         OBJ(apm_battery_life) {
4793                                 char *msg;
4794
4795                                 msg = get_apm_battery_life();
4796                                 snprintf(p, p_max_size, "%s", msg);
4797                                 free(msg);
4798                         }
4799                         OBJ(apm_battery_time) {
4800                                 char *msg;
4801
4802                                 msg = get_apm_battery_time();
4803                                 snprintf(p, p_max_size, "%s", msg);
4804                                 free(msg);
4805                         }
4806 #endif /* __FreeBSD__ __OpenBSD__ */
4807
4808 #ifdef MPD
4809 #define mpd_printf(fmt, val) \
4810         snprintf(p, p_max_size, fmt, mpd_get_info()->val)
4811 #define mpd_sprintf(val) { \
4812         if (!obj->data.i || obj->data.i > p_max_size) \
4813                 mpd_printf("%s", val); \
4814         else \
4815                 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
4816 }
4817                         OBJ(mpd_title)
4818                                 mpd_sprintf(title);
4819                         OBJ(mpd_artist)
4820                                 mpd_sprintf(artist);
4821                         OBJ(mpd_album)
4822                                 mpd_sprintf(album);
4823                         OBJ(mpd_random)
4824                                 mpd_printf("%s", random);
4825                         OBJ(mpd_repeat)
4826                                 mpd_printf("%s", repeat);
4827                         OBJ(mpd_track)
4828                                 mpd_sprintf(track);
4829                         OBJ(mpd_name)
4830                                 mpd_sprintf(name);
4831                         OBJ(mpd_file)
4832                                 mpd_sprintf(file);
4833                         OBJ(mpd_vol)
4834                                 mpd_printf("%d", volume);
4835                         OBJ(mpd_bitrate)
4836                                 mpd_printf("%d", bitrate);
4837                         OBJ(mpd_status)
4838                                 mpd_printf("%s", status);
4839                         OBJ(mpd_elapsed) {
4840                                 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
4841                         }
4842                         OBJ(mpd_length) {
4843                                 format_media_player_time(p, p_max_size, mpd_get_info()->length);
4844                         }
4845                         OBJ(mpd_percent) {
4846                                 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
4847                         }
4848 #ifdef X11
4849                         OBJ(mpd_bar) {
4850                                 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4851                                         (int) (mpd_get_info()->progress * 255.0f));
4852                         }
4853 #endif
4854                         OBJ(mpd_smart) {
4855                                 struct mpd_s *mpd = mpd_get_info();
4856                                 int len = obj->data.i;
4857                                 if (len == 0 || len > p_max_size)
4858                                         len = p_max_size;
4859
4860                                 memset(p, 0, p_max_size);
4861                                 if (mpd->artist && *mpd->artist &&
4862                                     mpd->title && *mpd->title) {
4863                                         snprintf(p, len, "%s - %s", mpd->artist,
4864                                                 mpd->title);
4865                                 } else if (mpd->title && *mpd->title) {
4866                                         snprintf(p, len, "%s", mpd->title);
4867                                 } else if (mpd->artist && *mpd->artist) {
4868                                         snprintf(p, len, "%s", mpd->artist);
4869                                 } else if (mpd->file && *mpd->file) {
4870                                         snprintf(p, len, "%s", mpd->file);
4871                                 } else {
4872                                         *p = 0;
4873                                 }
4874                         }
4875                         OBJ(if_mpd_playing) {
4876                                 if (!mpd_get_info()->is_playing) {
4877                                         DO_JUMP;
4878                                 }
4879                         }
4880 #undef mpd_sprintf
4881 #undef mpd_printf
4882 #endif
4883
4884 #ifdef XMMS2
4885     free_xmms2();
4886 #endif
4887
4888 #ifdef MOC
4889 #define MOC_PRINT(t, a) \
4890         snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
4891                         OBJ(moc_state) {
4892                                 MOC_PRINT(state, "??");
4893                         }
4894                         OBJ(moc_file) {
4895                                 MOC_PRINT(file, "no file");
4896                         }
4897                         OBJ(moc_title) {
4898                                 MOC_PRINT(title, "no title");
4899                         }
4900                         OBJ(moc_artist) {
4901                                 MOC_PRINT(artist, "no artist");
4902                         }
4903                         OBJ(moc_song) {
4904                                 MOC_PRINT(song, "no song");
4905                         }
4906                         OBJ(moc_album) {
4907                                 MOC_PRINT(album, "no album");
4908                         }
4909                         OBJ(moc_totaltime) {
4910                                 MOC_PRINT(totaltime, "0:00");
4911                         }
4912                         OBJ(moc_timeleft) {
4913                                 MOC_PRINT(timeleft, "0:00");
4914                         }
4915                         OBJ(moc_curtime) {
4916                                 MOC_PRINT(curtime, "0:00");
4917                         }
4918                         OBJ(moc_bitrate) {
4919                                 MOC_PRINT(bitrate, "0Kbps");
4920                         }
4921                         OBJ(moc_rate) {
4922                                 MOC_PRINT(rate, "0KHz");
4923                         }
4924 #undef MOC_PRINT
4925 #endif /* MOC */
4926 #ifdef XMMS2
4927                         OBJ(xmms2_artist) {
4928                                 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4929                         }
4930                         OBJ(xmms2_album) {
4931                                 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4932                         }
4933                         OBJ(xmms2_title) {
4934                                 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4935                         }
4936                         OBJ(xmms2_genre) {
4937                                 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4938                         }
4939                         OBJ(xmms2_comment) {
4940                                 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4941                         }
4942                         OBJ(xmms2_url) {
4943                                 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4944                         }
4945                         OBJ(xmms2_status) {
4946                                 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4947                         }
4948                         OBJ(xmms2_date) {
4949                                 snprintf(p, p_max_size, "%s", cur->xmms2.date);
4950                         }
4951                         OBJ(xmms2_tracknr) {
4952                                 if (cur->xmms2.tracknr != -1) {
4953                                         snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4954                                 }
4955                         }
4956                         OBJ(xmms2_bitrate) {
4957                                 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4958                         }
4959                         OBJ(xmms2_id) {
4960                                 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4961                         }
4962                         OBJ(xmms2_size) {
4963                                 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4964                         }
4965                         OBJ(xmms2_elapsed) {
4966                                 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
4967                                         (cur->xmms2.elapsed / 1000) % 60);
4968                         }
4969                         OBJ(xmms2_duration) {
4970                                 snprintf(p, p_max_size, "%02d:%02d",
4971                                         cur->xmms2.duration / 60000,
4972                                         (cur->xmms2.duration / 1000) % 60);
4973                         }
4974                         OBJ(xmms2_percent) {
4975                                 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
4976                         }
4977                         OBJ(xmms2_bar) {
4978                                 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4979                                         (int) (cur->xmms2.progress * 255.0f));
4980                         }
4981                         OBJ(xmms2_playlist) {
4982                                 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
4983                         }
4984                         OBJ(xmms2_timesplayed) {
4985                                 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
4986                         }
4987                         OBJ(xmms2_smart) {
4988                                 if (strlen(cur->xmms2.title) < 2
4989                                                 && strlen(cur->xmms2.title) < 2) {
4990                                         snprintf(p, p_max_size, "%s", cur->xmms2.url);
4991                                 } else {
4992                                         snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
4993                                                 cur->xmms2.title);
4994                                 }
4995                         }
4996                         OBJ(if_xmms2_connected) {
4997                                 if (cur->xmms2.conn_state != 1) {
4998                                         DO_JUMP;
4999                                 }
5000                         }
5001 #endif
5002 #ifdef AUDACIOUS
5003                         OBJ(audacious_status) {
5004                                 snprintf(p, p_max_size, "%s",
5005                                         cur->audacious.items[AUDACIOUS_STATUS]);
5006                         }
5007                         OBJ(audacious_title) {
5008                                 snprintf(p, cur->audacious.max_title_len > 0
5009                                         ? cur->audacious.max_title_len : p_max_size, "%s",
5010                                         cur->audacious.items[AUDACIOUS_TITLE]);
5011                         }
5012                         OBJ(audacious_length) {
5013                                 snprintf(p, p_max_size, "%s",
5014                                         cur->audacious.items[AUDACIOUS_LENGTH]);
5015                         }
5016                         OBJ(audacious_length_seconds) {
5017                                 snprintf(p, p_max_size, "%s",
5018                                         cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5019                         }
5020                         OBJ(audacious_position) {
5021                                 snprintf(p, p_max_size, "%s",
5022                                         cur->audacious.items[AUDACIOUS_POSITION]);
5023                         }
5024                         OBJ(audacious_position_seconds) {
5025                                 snprintf(p, p_max_size, "%s",
5026                                         cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5027                         }
5028                         OBJ(audacious_bitrate) {
5029                                 snprintf(p, p_max_size, "%s",
5030                                         cur->audacious.items[AUDACIOUS_BITRATE]);
5031                         }
5032                         OBJ(audacious_frequency) {
5033                                 snprintf(p, p_max_size, "%s",
5034                                         cur->audacious.items[AUDACIOUS_FREQUENCY]);
5035                         }
5036                         OBJ(audacious_channels) {
5037                                 snprintf(p, p_max_size, "%s",
5038                                         cur->audacious.items[AUDACIOUS_CHANNELS]);
5039                         }
5040                         OBJ(audacious_filename) {
5041                                 snprintf(p, p_max_size, "%s",
5042                                         cur->audacious.items[AUDACIOUS_FILENAME]);
5043                         }
5044                         OBJ(audacious_playlist_length) {
5045                                 snprintf(p, p_max_size, "%s",
5046                                         cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5047                         }
5048                         OBJ(audacious_playlist_position) {
5049                                 snprintf(p, p_max_size, "%s",
5050                                         cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5051                         }
5052                         OBJ(audacious_main_volume) {
5053                                 snprintf(p, p_max_size, "%s",
5054                                         cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5055                         }
5056                         OBJ(audacious_bar) {
5057                                 double progress;
5058
5059                                 progress =
5060                                         atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5061                                         atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5062                                 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5063                         }
5064 #endif
5065
5066 #ifdef BMPX
5067                         OBJ(bmpx_title) {
5068                                 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5069                         }
5070                         OBJ(bmpx_artist) {
5071                                 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5072                         }
5073                         OBJ(bmpx_album) {
5074                                 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5075                         }
5076                         OBJ(bmpx_uri) {
5077                                 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5078                         }
5079                         OBJ(bmpx_track) {
5080                                 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5081                         }
5082                         OBJ(bmpx_bitrate) {
5083                                 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5084                         }
5085 #endif
5086                         /* we have three different types of top (top, top_mem
5087                          * and top_time). To avoid having almost-same code three
5088                          * times, we have this special handler. */
5089                         break;
5090                         case OBJ_top:
5091                                 parse_top_args("top", obj->data.top.s, obj);
5092                                 if (!needed) needed = cur->cpu;
5093                         case OBJ_top_mem:
5094                                 parse_top_args("top_mem", obj->data.top.s, obj);
5095                                 if (!needed) needed = cur->memu;
5096                         case OBJ_top_time:
5097                                 parse_top_args("top_time", obj->data.top.s, obj);
5098                                 if (!needed) needed = cur->time;
5099
5100                                 if (needed[obj->data.top.num]) {
5101                                         char *timeval;
5102
5103                                         switch (obj->data.top.type) {
5104                                                 case TOP_NAME:
5105                                                         snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5106                                                                         needed[obj->data.top.num]->name);
5107                                                         break;
5108                                                 case TOP_CPU:
5109                                                         snprintf(p, 7, "%6.2f",
5110                                                                         needed[obj->data.top.num]->amount);
5111                                                         break;
5112                                                 case TOP_PID:
5113                                                         snprintf(p, 6, "%5i",
5114                                                                         needed[obj->data.top.num]->pid);
5115                                                         break;
5116                                                 case TOP_MEM:
5117                                                         snprintf(p, 7, "%6.2f",
5118                                                                         needed[obj->data.top.num]->totalmem);
5119                                                         break;
5120                                                 case TOP_TIME:
5121                                                         timeval = format_time(
5122                                                                         needed[obj->data.top.num]->total_cpu_time, 9);
5123                                                         snprintf(p, 10, "%9s", timeval);
5124                                                         free(timeval);
5125                                                         break;
5126                                                 case TOP_MEM_RES:
5127                                                         human_readable(needed[obj->data.top.num]->rss,
5128                                                                         p, 255);
5129                                                         break;
5130                                                 case TOP_MEM_VSIZE:
5131                                                         human_readable(needed[obj->data.top.num]->vsize,
5132                                                                         p, 255);
5133                                                         break;
5134                                         }
5135                                 }
5136                         OBJ(tail)
5137                                 print_tail_object(obj, p, p_max_size);
5138                         OBJ(head)
5139                                 print_head_object(obj, p, p_max_size);
5140                         OBJ(lines) {
5141                                 FILE *fp = open_file(obj->data.s, &obj->a);
5142
5143                                 if(fp != NULL) {
5144 /* FIXME: use something more general (see also tail.c, head.c */
5145 #define BUFSZ 0x1000
5146                                         char buf[BUFSZ];
5147                                         int j, lines;
5148
5149                                         lines = 0;
5150                                         while(fgets(buf, BUFSZ, fp) != NULL){
5151                                                 for(j = 0; buf[j] != 0; j++) {
5152                                                         if(buf[j] == '\n') {
5153                                                                 lines++;
5154                                                         }
5155                                                 }
5156                                         }
5157                                         sprintf(p, "%d", lines);
5158                                         fclose(fp);
5159                                 } else {
5160                                         sprintf(p, "File Unreadable");
5161                                 }
5162                         }
5163
5164                         OBJ(words) {
5165                                 FILE *fp = open_file(obj->data.s, &obj->a);
5166
5167                                 if(fp != NULL) {
5168                                         char buf[BUFSZ];
5169                                         int j, words;
5170                                         char inword = FALSE;
5171
5172                                         words = 0;
5173                                         while(fgets(buf, BUFSZ, fp) != NULL){
5174                                                 for(j = 0; buf[j] != 0; j++) {
5175                                                         if(!isspace(buf[j])) {
5176                                                                 if(inword == FALSE) {
5177                                                                         words++;
5178                                                                         inword = TRUE;
5179                                                                 }
5180                                                         } else {
5181                                                                 inword = FALSE;
5182                                                         }
5183                                                 }
5184                                         }
5185                                         sprintf(p, "%d", words);
5186                                         fclose(fp);
5187                                 } else {
5188                                         sprintf(p, "File Unreadable");
5189                                 }
5190                         }
5191 #ifdef TCP_PORT_MONITOR
5192                         OBJ(tcp_portmon) {
5193                                 tcp_portmon_action(p, p_max_size,
5194                                                    &obj->data.tcp_port_monitor);
5195                         }
5196 #endif
5197
5198 #ifdef HAVE_ICONV
5199                         OBJ(iconv_start) {
5200                                 iconv_converting = 1;
5201                                 iconv_selected = obj->a;
5202                         }
5203                         OBJ(iconv_stop) {
5204                                 iconv_converting = 0;
5205                                 iconv_selected = 0;
5206                         }
5207 #endif
5208
5209                         OBJ(entropy_avail) {
5210                                 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5211                         }
5212                         OBJ(entropy_poolsize) {
5213                                 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5214                         }
5215 #ifdef X11
5216                         OBJ(entropy_bar) {
5217                                 double entropy_perc;
5218
5219                                 entropy_perc = (double) cur->entropy.entropy_avail /
5220                                         (double) cur->entropy.poolsize;
5221                                 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5222                         }
5223 #endif
5224 #ifdef IBM
5225                         OBJ(smapi) {
5226                                 char *s;
5227                                 if(obj->data.s) {
5228                                         s = smapi_get_val(obj->data.s);
5229                                         snprintf(p, p_max_size, "%s", s);
5230                                         free(s);
5231                                 }
5232                         }
5233                         OBJ(if_smapi_bat_installed) {
5234                                 int idx;
5235                                 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5236                                         if(!smapi_bat_installed(idx)) {
5237                                                 DO_JUMP;
5238                                         }
5239                                 } else
5240                                         ERR("argument to if_smapi_bat_installed must be an integer");
5241                         }
5242                         OBJ(smapi_bat_perc) {
5243                                 int idx, val;
5244                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5245                                         val = smapi_bat_installed(idx) ?
5246                                                 smapi_get_bat_int(idx, "remaining_percent") : 0;
5247                                         percent_print(p, p_max_size, val);
5248                                 } else
5249                                         ERR("argument to smapi_bat_perc must be an integer");
5250                         }
5251                         OBJ(smapi_bat_temp) {
5252                                 int idx, val;
5253                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5254                                         val = smapi_bat_installed(idx) ?
5255                                                 smapi_get_bat_int(idx, "temperature") : 0;
5256                                         /* temperature is in milli degree celsius */
5257                                         temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5258                                 } else
5259                                         ERR("argument to smapi_bat_temp must be an integer");
5260                         }
5261                         OBJ(smapi_bat_power) {
5262                                 int idx, val;
5263                                 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5264                                         val = smapi_bat_installed(idx) ?
5265                                                 smapi_get_bat_int(idx, "power_now") : 0;
5266                                         /* power_now is in mW, set to W with one digit precision */
5267                                         snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5268                                 } else
5269                                         ERR("argument to smapi_bat_power must be an integer");
5270                         }
5271                         OBJ(smapi_bat_bar) {
5272                                 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5273                                         new_bar(p, obj->a, obj->b, (int)
5274                                                         (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5275                                 else
5276                                         new_bar(p, obj->a, obj->b, 0);
5277                         }
5278 #endif /* IBM */
5279                         OBJ(scroll) {
5280                                 unsigned int j;
5281                                 char *tmp, buf[max_user_text];
5282                                 generate_text_internal(buf, max_user_text,
5283                                                        *obj->sub, cur);
5284
5285                                 if (strlen(buf) <= obj->data.scroll.show) {
5286                                         snprintf(p, p_max_size, "%s", buf);
5287                                         break;
5288                                 }
5289 #define LINESEPARATOR '|'
5290                                 //place all the lines behind each other with LINESEPARATOR between them
5291                                 for(j = 0; buf[j] != 0; j++) {
5292                                         if(buf[j]=='\n') {
5293                                                 buf[j]=LINESEPARATOR;
5294                                         }
5295                                 }
5296                                 //scroll the output obj->data.scroll.start places by copying that many chars from
5297                                 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5298                                 //at the back of the string
5299                                 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5300                                 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5301                                 for(j = obj->data.scroll.start; buf[j] != 0; j++){
5302                                         buf[j - obj->data.scroll.start] = buf[j];
5303                                 }
5304                                 strcpy(&buf[j - obj->data.scroll.start], tmp);
5305                                 free(tmp);
5306                                 //only show the requested number of chars
5307                                 if(obj->data.scroll.show < j) {
5308                                         buf[obj->data.scroll.show] = 0;
5309                                 }
5310                                 //next time, scroll a place more or reset scrolling if we are at the end
5311                                 obj->data.scroll.start += obj->data.scroll.step;
5312                                 if(obj->data.scroll.start >= j){
5313                                          obj->data.scroll.start = 0;
5314                                 }
5315                                 snprintf(p, p_max_size, "%s", buf);
5316                         }
5317                         OBJ(combine) {
5318                                 char buf[2][max_user_text];
5319                                 int i, j;
5320                                 long longest=0;
5321                                 int nextstart;
5322                                 int nr_rows[2];
5323                                 struct llrows {
5324                                         char* row;
5325                                         struct llrows* next;
5326                                 };
5327                                 struct llrows *ll_rows[2], *current[2];
5328                                 struct text_object * objsub = obj->sub;
5329
5330                                 p[0]=0;
5331                                 #ifdef HAVE_OPENMP
5332                                 #pragma omp parallel for
5333                                 #endif /* HAVE_OPENMP */
5334                                 for(i=0; i<2; i++) {
5335                                         nr_rows[i] = 1;
5336                                         nextstart = 0;
5337                                         ll_rows[i] = malloc(sizeof(struct llrows));
5338                                         current[i] = ll_rows[i];
5339                                         #ifdef HAVE_OPENMP
5340                                         #pragma omp parallel for
5341                                         #endif /* HAVE_OPENMP */
5342                                         for(j=0; j<i; j++) objsub = objsub->sub;
5343                                         generate_text_internal(buf[i], max_user_text, *objsub, cur);
5344                                         /* doesnut work
5345                                          * #ifdef HAVE_OPENMP
5346                                          * #pragma omp parallel for reduction (+:nr_rows[i])
5347                                          * #endif  HAVE_OPENMP */
5348                                         for(j=0; buf[i][j] != 0; j++) {
5349                                                 if(buf[i][j] == '\t') buf[i][j] = ' ';
5350                                                 if(buf[i][j] == '\n') {
5351                                                         buf[i][j] = 0;
5352                                                         current[i]->row = strdup(buf[i]+nextstart);
5353                                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5354                                                         current[i]->next = malloc(sizeof(struct llrows));
5355                                                         current[i] = current[i]->next;
5356                                                         nextstart = j + 1;
5357                                                         nr_rows[i]++;
5358                                                 }
5359                                         }
5360                                         current[i]->row = strdup(buf[i]+nextstart);
5361                                         if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5362                                         current[i]->next = NULL;
5363                                         current[i] = ll_rows[i];
5364                                 }
5365                                 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5366                                         if(current[0]) {
5367                                                 strcat(p, current[0]->row);
5368                                                 i=strlen(current[0]->row);
5369                                         }else i = 0;
5370                                         while(i < longest) {
5371                                                 strcat(p, " ");
5372                                                 i++;
5373                                         }
5374                                         if(current[1]) {
5375                                                 strcat(p, obj->data.combine.seperation);
5376                                                 strcat(p, current[1]->row);
5377                                         }
5378                                         strcat(p, "\n");
5379                                         #ifdef HAVE_OPENMP
5380                                         #pragma omp parallel for
5381                                         #endif /* HAVE_OPENMP */
5382                                         for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5383                                 }
5384                                 #ifdef HAVE_OPENMP
5385                                 #pragma omp parallel for
5386                                 #endif /* HAVE_OPENMP */
5387                                 for(i=0; i<2; i++) {
5388                                         while(ll_rows[i] != NULL) {
5389                                                 current[i]=ll_rows[i];
5390                                                 free(current[i]->row);
5391                                                 ll_rows[i]=current[i]->next;
5392                                                 free(current[i]);
5393                                         }
5394                                 }
5395                         }
5396 #ifdef NVIDIA
5397                         OBJ(nvidia) {
5398                                 int value = get_nvidia_value(obj->data.nvidia.type, display);
5399                                 if(value == -1)
5400                                         snprintf(p, p_max_size, "N/A");
5401                                 else if (obj->data.nvidia.type == NV_TEMP)
5402                                         temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5403                                 else if (obj->data.nvidia.print_as_float &&
5404                                                 value > 0 && value < 100)
5405                                         snprintf(p, p_max_size, "%.1f", (float)value);
5406                                 else
5407                                         snprintf(p, p_max_size, "%d", value);
5408                         }
5409 #endif /* NVIDIA */
5410 #ifdef APCUPSD
5411                         OBJ(apcupsd) {
5412                                 /* This is just a meta-object to set host:port */
5413                         }
5414                         OBJ(apcupsd_name) {
5415                                 snprintf(p, p_max_size, "%s",
5416                                                  cur->apcupsd.items[APCUPSD_NAME]);
5417                         }
5418                         OBJ(apcupsd_model) {
5419                                 snprintf(p, p_max_size, "%s",
5420                                                  cur->apcupsd.items[APCUPSD_MODEL]);
5421                         }
5422                         OBJ(apcupsd_upsmode) {
5423                                 snprintf(p, p_max_size, "%s",
5424                                                  cur->apcupsd.items[APCUPSD_UPSMODE]);
5425                         }
5426                         OBJ(apcupsd_cable) {
5427                                 snprintf(p, p_max_size, "%s",
5428                                                  cur->apcupsd.items[APCUPSD_CABLE]);
5429                         }
5430                         OBJ(apcupsd_status) {
5431                                 snprintf(p, p_max_size, "%s",
5432                                                  cur->apcupsd.items[APCUPSD_STATUS]);
5433                         }
5434                         OBJ(apcupsd_linev) {
5435                                 snprintf(p, p_max_size, "%s",
5436                                                  cur->apcupsd.items[APCUPSD_LINEV]);
5437                         }
5438                         OBJ(apcupsd_load) {
5439                                 snprintf(p, p_max_size, "%s",
5440                                                  cur->apcupsd.items[APCUPSD_LOAD]);
5441                         }
5442 #ifdef X11
5443                         OBJ(apcupsd_loadbar) {
5444                                 double progress;
5445                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5446                                 new_bar(p, obj->a, obj->b, (int)progress);
5447                         }
5448                         OBJ(apcupsd_loadgraph) {
5449                                 double progress;
5450                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]);
5451                                 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5452                                                   (int)progress, 100, 1, obj->char_a, obj->char_b);
5453                         }
5454                         OBJ(apcupsd_loadgauge) {
5455                                 double progress;
5456                                 progress =      atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5457                                 new_gauge(p, obj->a, obj->b,
5458                                                   (int)progress);
5459                         }
5460 #endif
5461                         OBJ(apcupsd_charge) {
5462                                 snprintf(p, p_max_size, "%s",
5463                                                  cur->apcupsd.items[APCUPSD_CHARGE]);
5464                         }
5465                         OBJ(apcupsd_timeleft) {
5466                                 snprintf(p, p_max_size, "%s",
5467                                                  cur->apcupsd.items[APCUPSD_TIMELEFT]);
5468                         }
5469                         OBJ(apcupsd_temp) {
5470                                 snprintf(p, p_max_size, "%s",
5471                                                  cur->apcupsd.items[APCUPSD_TEMP]);
5472                         }
5473                         OBJ(apcupsd_lastxfer) {
5474                                 snprintf(p, p_max_size, "%s",
5475                                                  cur->apcupsd.items[APCUPSD_LASTXFER]);
5476                         }
5477 #endif /* APCUPSD */
5478                         break;
5479                 }
5480 #undef DO_JUMP
5481
5482
5483                 {
5484                         unsigned int a = strlen(p);
5485
5486 #ifdef HAVE_ICONV
5487                         if (a > 0 && iconv_converting && iconv_selected > 0
5488                                         && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
5489                                 int bytes;
5490                                 size_t dummy1, dummy2;
5491                                 char *ptr = buff_in;
5492                                 char *outptr = p;
5493
5494                                 dummy1 = dummy2 = a;
5495
5496                                 strncpy(buff_in, p, p_max_size);
5497
5498                                 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
5499                                 while (dummy1 > 0) {
5500                                         bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
5501                                                         &outptr, &dummy2);
5502                                         if (bytes == -1) {
5503                                                 ERR("Iconv codeset conversion failed");
5504                                                 break;
5505                                         }
5506                                 }
5507
5508                                 /* It is nessecary when we are converting from multibyte to
5509                                  * singlebyte codepage */
5510                                 a = outptr - p;
5511                         }
5512 #endif /* HAVE_ICONV */
5513                         p += a;
5514                         p_max_size -= a;
5515                 }
5516                 obj = obj->next;
5517         }
5518 }
5519
5520 double current_update_time, next_update_time, last_update_time;
5521
5522 static void generate_text(void)
5523 {
5524         struct information *cur = &info;
5525         char *p;
5526
5527         special_count = 0;
5528
5529         /* update info */
5530
5531         current_update_time = get_time();
5532
5533         update_stuff();
5534
5535         /* add things to the buffer */
5536
5537         /* generate text */
5538
5539         p = text_buffer;
5540
5541         generate_text_internal(p, max_user_text, global_root_object, cur);
5542
5543         if (stuff_in_upper_case) {
5544                 char *tmp_p;
5545
5546                 tmp_p = text_buffer;
5547                 while (*tmp_p) {
5548                         *tmp_p = toupper(*tmp_p);
5549                         tmp_p++;
5550                 }
5551         }
5552
5553         next_update_time += update_interval;
5554         if (next_update_time < get_time()) {
5555                 next_update_time = get_time() + update_interval;
5556         } else if (next_update_time > get_time() + update_interval) {
5557                 next_update_time = get_time() + update_interval;
5558         }
5559         last_update_time = current_update_time;
5560         total_updates++;
5561         // free(p);
5562 }
5563
5564 static inline int get_string_width(const char *s)
5565 {
5566 #ifdef X11
5567         if (output_methods & TO_X) {
5568                 return *s ? calc_text_width(s, strlen(s)) : 0;
5569         }
5570 #endif /* X11 */
5571         return strlen(s);
5572 }
5573
5574 static inline int get_string_width_special(char *s)
5575 {
5576 #ifdef X11
5577         char *p, *final;
5578         int idx = 1;
5579         int width = 0;
5580         long i;
5581
5582         if ((output_methods & TO_X) == 0) {
5583 #endif
5584                 return (s) ? strlen(s) : 0;
5585 #ifdef X11
5586         }
5587
5588         if (!s) {
5589                 return 0;
5590         }
5591
5592         p = strndup(s, text_buffer_size);
5593         final = p;
5594
5595         while (*p) {
5596                 if (*p == SPECIAL_CHAR) {
5597                         /* shift everything over by 1 so that the special char
5598                          * doesn't mess up the size calculation */
5599                         for (i = 0; i < (long)strlen(p); i++) {
5600                                 *(p + i) = *(p + i + 1);
5601                         }
5602                         if (specials[special_index + idx].type == GRAPH
5603                                         || specials[special_index + idx].type == GAUGE
5604                                         || specials[special_index + idx].type == BAR) {
5605                                 width += specials[special_index + idx].width;
5606                         }
5607                         idx++;
5608                 } else {
5609                         p++;
5610                 }
5611         }
5612         if (strlen(final) > 1) {
5613                 width += calc_text_width(final, strlen(final));
5614         }
5615         free(final);
5616         return width;
5617 #endif /* X11 */
5618 }
5619
5620 #ifdef X11
5621 static void text_size_updater(char *s);
5622
5623 int last_font_height;
5624 static void update_text_area(void)
5625 {
5626         int x = 0, y = 0;
5627
5628         if ((output_methods & TO_X) == 0)
5629                 return;
5630         /* update text size if it isn't fixed */
5631 #ifdef OWN_WINDOW
5632         if (!fixed_size)
5633 #endif
5634         {
5635                 text_width = minimum_width;
5636                 text_height = 0;
5637                 special_index = 0;
5638                 last_font_height = font_height();
5639                 for_each_line(text_buffer, text_size_updater);
5640                 text_width += 1;
5641                 if (text_height < minimum_height) {
5642                         text_height = minimum_height;
5643                 }
5644                 if (text_width > maximum_width && maximum_width > 0) {
5645                         text_width = maximum_width;
5646                 }
5647         }
5648
5649         /* get text position on workarea */
5650         switch (text_alignment) {
5651                 case TOP_LEFT:
5652                         x = gap_x;
5653                         y = gap_y;
5654                         break;
5655
5656                 case TOP_RIGHT:
5657                         x = workarea[2] - text_width - gap_x;
5658                         y = gap_y;
5659                         break;
5660
5661                 case TOP_MIDDLE:
5662                         x = workarea[2] / 2 - text_width / 2 - gap_x;
5663                         y = gap_y;
5664                         break;
5665
5666                 default:
5667                 case BOTTOM_LEFT:
5668                         x = gap_x;
5669                         y = workarea[3] - text_height - gap_y;
5670                         break;
5671
5672                 case BOTTOM_RIGHT:
5673                         x = workarea[2] - text_width - gap_x;
5674                         y = workarea[3] - text_height - gap_y;
5675                         break;
5676
5677                 case BOTTOM_MIDDLE:
5678                         x = workarea[2] / 2 - text_width / 2 - gap_x;
5679                         y = workarea[3] - text_height - gap_y;
5680                         break;
5681
5682                 case MIDDLE_LEFT:
5683                         x = gap_x;
5684                         y = workarea[3] / 2 - text_height / 2 - gap_y;
5685                         break;
5686
5687                 case MIDDLE_RIGHT:
5688                         x = workarea[2] - text_width - gap_x;
5689                         y = workarea[3] / 2 - text_height / 2 - gap_y;
5690                         break;
5691
5692 #ifdef OWN_WINDOW
5693                 case NONE:      // Let the WM manage the window
5694                         x = window.x;
5695                         y = window.y;
5696
5697                         fixed_pos = 1;
5698                         fixed_size = 1;
5699                         break;
5700 #endif
5701         }
5702 #ifdef OWN_WINDOW
5703
5704         if (own_window && !fixed_pos) {
5705                 x += workarea[0];
5706                 y += workarea[1];
5707                 text_start_x = border_margin + 1;
5708                 text_start_y = border_margin + 1;
5709                 window.x = x - border_margin - 1;
5710                 window.y = y - border_margin - 1;
5711         } else
5712 #endif
5713         {
5714                 /* If window size doesn't match to workarea's size,
5715                  * then window probably includes panels (gnome).
5716                  * Blah, doesn't work on KDE. */
5717                 if (workarea[2] != window.width || workarea[3] != window.height) {
5718                         y += workarea[1];
5719                         x += workarea[0];
5720                 }
5721
5722                 text_start_x = x;
5723                 text_start_y = y;
5724         }
5725 }
5726
5727 /* drawing stuff */
5728
5729 static int cur_x, cur_y;        /* current x and y for drawing */
5730 #endif
5731 //draw_mode also without X11 because we only need to print to stdout with FG
5732 static int draw_mode;           /* FG, BG or OUTLINE */
5733 #ifdef X11
5734 static long current_color;
5735
5736 static void text_size_updater(char *s)
5737 {
5738         int w = 0;
5739         char *p;
5740
5741         if ((output_methods & TO_X) == 0)
5742                 return;
5743         /* get string widths and skip specials */
5744         p = s;
5745         while (*p) {
5746                 if (*p == SPECIAL_CHAR) {
5747                         *p = '\0';
5748                         w += get_string_width(s);
5749                         *p = SPECIAL_CHAR;
5750
5751                         if (specials[special_index].type == BAR
5752                                         || specials[special_index].type == GAUGE
5753                                         || specials[special_index].type == GRAPH) {
5754                                 w += specials[special_index].width;
5755                                 if (specials[special_index].height > last_font_height) {
5756                                         last_font_height = specials[special_index].height;
5757                                         last_font_height += font_height();
5758                                 }
5759                         } else if (specials[special_index].type == OFFSET) {
5760                                 if (specials[special_index].arg > 0) {
5761                                         w += specials[special_index].arg;
5762                                 }
5763                         } else if (specials[special_index].type == VOFFSET) {
5764                                 last_font_height += specials[special_index].arg;
5765                         } else if (specials[special_index].type == GOTO) {
5766                                 if (specials[special_index].arg > cur_x) {
5767                                         w = (int) specials[special_index].arg;
5768                                 }
5769                         } else if (specials[special_index].type == TAB) {
5770                                 int start = specials[special_index].arg;
5771                                 int step = specials[special_index].width;
5772
5773                                 if (!step || step < 0) {
5774                                         step = 10;
5775                                 }
5776                                 w += step - (cur_x - text_start_x - start) % step;
5777                         } else if (specials[special_index].type == FONT) {
5778                                 selected_font = specials[special_index].font_added;
5779                                 if (font_height() > last_font_height) {
5780                                         last_font_height = font_height();
5781                                 }
5782                         }
5783
5784                         special_index++;
5785                         s = p + 1;
5786                 }
5787                 p++;
5788         }
5789         w += get_string_width(s);
5790         if (w > text_width) {
5791                 text_width = w;
5792         }
5793         if (text_width > maximum_width && maximum_width) {
5794                 text_width = maximum_width;
5795         }
5796
5797         text_height += last_font_height;
5798         last_font_height = font_height();
5799 }
5800
5801 static inline void set_foreground_color(long c)
5802 {
5803         if ((output_methods & TO_X) == 0)
5804                 return;
5805         current_color = c;
5806         XSetForeground(display, window.gc, c);
5807 }
5808 #endif /* X11 */
5809
5810 static void draw_string(const char *s)
5811 {
5812         int i, i2, pos, width_of_s;
5813         int max = 0;
5814         int added;
5815
5816         if (s[0] == '\0') {
5817                 return;
5818         }
5819
5820         width_of_s = get_string_width(s);
5821         if ((output_methods & TO_STDOUT) && draw_mode == FG) {
5822                 printf("%s\n", s);
5823                 fflush(stdout); /* output immediately, don't buffer */
5824         }
5825         if ((output_methods & TO_STDERR) && draw_mode == FG) {
5826                 fprintf(stderr, "%s\n", s);
5827                 fflush(stderr); /* output immediately, don't buffer */
5828         }
5829         if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
5830                 fprintf(overwrite_fpointer, "%s\n", s);
5831         }
5832         if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
5833                 fprintf(append_fpointer, "%s\n", s);
5834         }
5835         memset(tmpstring1, 0, text_buffer_size);
5836         memset(tmpstring2, 0, text_buffer_size);
5837         strncpy(tmpstring1, s, text_buffer_size - 1);
5838         pos = 0;
5839         added = 0;
5840
5841 #ifdef X11
5842         if (output_methods & TO_X) {
5843                 max = ((text_width - width_of_s) / get_string_width(" "));
5844         }
5845 #endif /* X11 */
5846         /* This code looks for tabs in the text and coverts them to spaces.
5847          * The trick is getting the correct number of spaces, and not going
5848          * over the window's size without forcing the window larger. */
5849         for (i = 0; i < (int) text_buffer_size; i++) {
5850                 if (tmpstring1[i] == '\t') {
5851                         i2 = 0;
5852                         for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
5853                                 /* guard against overrun */
5854                                 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
5855                                 added++;
5856                         }
5857                         pos += i2;
5858                 } else {
5859                         /* guard against overrun */
5860                         tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
5861                         pos++;
5862                 }
5863         }
5864 #ifdef X11
5865         if (output_methods & TO_X) {
5866                 if (text_width == maximum_width) {
5867                         /* this means the text is probably pushing the limit,
5868                          * so we'll chop it */
5869                         while (cur_x + get_string_width(tmpstring2) - text_start_x
5870                                         > maximum_width && strlen(tmpstring2) > 0) {
5871                                 tmpstring2[strlen(tmpstring2) - 1] = '\0';
5872                         }
5873                 }
5874         }
5875 #endif /* X11 */
5876         s = tmpstring2;
5877 #ifdef X11
5878         if (output_methods & TO_X) {
5879 #ifdef XFT
5880                 if (use_xft) {
5881                         XColor c;
5882                         XftColor c2;
5883
5884                         c.pixel = current_color;
5885                         XQueryColor(display, DefaultColormap(display, screen), &c);
5886
5887                         c2.pixel = c.pixel;
5888                         c2.color.red = c.red;
5889                         c2.color.green = c.green;
5890                         c2.color.blue = c.blue;
5891                         c2.color.alpha = fonts[selected_font].font_alpha;
5892                         if (utf8_mode) {
5893                                 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5894                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5895                         } else {
5896                                 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5897                                         cur_x, cur_y, (const XftChar8 *) s, strlen(s));
5898                         }
5899                 } else
5900 #endif
5901                 {
5902                         XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
5903                                 strlen(s));
5904                 }
5905                 cur_x += width_of_s;
5906         }
5907 #endif /* X11 */
5908         memcpy(tmpstring1, s, text_buffer_size);
5909 }
5910
5911 static void draw_line(char *s)
5912 {
5913 #ifdef X11
5914         char *p;
5915         int cur_y_add = 0;
5916         int font_h;
5917         char *tmp_str;
5918 #endif /* X11 */
5919
5920         if ((output_methods & TO_X) == 0) {
5921                 draw_string(s);
5922                 return;
5923         }
5924 #ifdef X11
5925         cur_x = text_start_x;
5926         cur_y += font_ascent();
5927         font_h = font_height();
5928
5929         /* find specials and draw stuff */
5930         p = s;
5931         while (*p) {
5932                 if (*p == SPECIAL_CHAR) {
5933                         int w = 0;
5934
5935                         /* draw string before special */
5936                         *p = '\0';
5937                         draw_string(s);
5938                         *p = SPECIAL_CHAR;
5939                         s = p + 1;
5940
5941                         /* draw special */
5942                         switch (specials[special_index].type) {
5943                                 case HORIZONTAL_LINE:
5944                                 {
5945                                         int h = specials[special_index].height;
5946                                         int mid = font_ascent() / 2;
5947
5948                                         w = text_start_x + text_width - cur_x;
5949
5950                                         XSetLineAttributes(display, window.gc, h, LineSolid,
5951                                                 CapButt, JoinMiter);
5952                                         XDrawLine(display, window.drawable, window.gc, cur_x,
5953                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
5954                                         break;
5955                                 }
5956
5957                                 case STIPPLED_HR:
5958                                 {
5959                                         int h = specials[special_index].height;
5960                                         int tmp_s = specials[special_index].arg;
5961                                         int mid = font_ascent() / 2;
5962                                         char ss[2] = { tmp_s, tmp_s };
5963
5964                                         w = text_start_x + text_width - cur_x - 1;
5965                                         XSetLineAttributes(display, window.gc, h, LineOnOffDash,
5966                                                 CapButt, JoinMiter);
5967                                         XSetDashes(display, window.gc, 0, ss, 2);
5968                                         XDrawLine(display, window.drawable, window.gc, cur_x,
5969                                                 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
5970                                         break;
5971                                 }
5972
5973                                 case BAR:
5974                                 {
5975                                         int h, bar_usage, by;
5976                                         if (cur_x - text_start_x > maximum_width
5977                                                         && maximum_width > 0) {
5978                                                 break;
5979                                         }
5980                                         h = specials[special_index].height;
5981                                         bar_usage = specials[special_index].arg;
5982                                         by = cur_y - (font_ascent() / 2) - 1;
5983
5984                                         if (h < font_h) {
5985                                                 by -= h / 2 - 1;
5986                                         }
5987                                         w = specials[special_index].width;
5988                                         if (w == 0) {
5989                                                 w = text_start_x + text_width - cur_x - 1;
5990                                         }
5991                                         if (w < 0) {
5992                                                 w = 0;
5993                                         }
5994
5995                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
5996                                                 CapButt, JoinMiter);
5997
5998                                         XDrawRectangle(display, window.drawable, window.gc, cur_x,
5999                                                 by, w, h);
6000                                         XFillRectangle(display, window.drawable, window.gc, cur_x,
6001                                                 by, w * bar_usage / 255, h);
6002                                         if (h > cur_y_add
6003                                                         && h > font_h) {
6004                                                 cur_y_add = h;
6005                                         }
6006                                         break;
6007                                 }
6008
6009                                 case GAUGE: /* new GAUGE  */
6010                                 {
6011                                         int h, by = 0;
6012                                         unsigned long last_colour = current_color;
6013                                         float angle, px, py;
6014                                         int usage;
6015
6016                                         if (cur_x - text_start_x > maximum_width
6017                                                         && maximum_width > 0) {
6018                                                 break;
6019                                         }
6020
6021                                         h = specials[special_index].height;
6022                                         by = cur_y - (font_ascent() / 2) - 1;
6023
6024                                         if (h < font_h) {
6025                                                 by -= h / 2 - 1;
6026                                         }
6027                                         w = specials[special_index].width;
6028                                         if (w == 0) {
6029                                                 w = text_start_x + text_width - cur_x - 1;
6030                                         }
6031                                         if (w < 0) {
6032                                                 w = 0;
6033                                         }
6034
6035                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6036                                                         CapButt, JoinMiter);
6037
6038                                         XDrawArc(display, window.drawable, window.gc,
6039                                                         cur_x, by, w, h * 2, 0, 180*64);
6040
6041 #ifdef MATH
6042                                         usage = specials[special_index].arg;
6043                                         angle = (M_PI)*(float)(usage)/255.;
6044                                         px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6045                                         py = (float)(by+(h))-(float)(h)*sin(angle);
6046
6047                                         XDrawLine(display, window.drawable, window.gc,
6048                                                         cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6049 #endif /* MATH */
6050
6051                                         if (h > cur_y_add
6052                                                         && h > font_h) {
6053                                                 cur_y_add = h;
6054                                         }
6055
6056                                         set_foreground_color(last_colour);
6057
6058                                         break;
6059
6060                                 }
6061
6062                                 case GRAPH:
6063                                 {
6064                                         int h, by, i, j = 0;
6065                                         int colour_idx = 0;
6066                                         unsigned long last_colour = current_color;
6067                                         unsigned long *tmpcolour = 0;
6068                                         if (cur_x - text_start_x > maximum_width
6069                                                         && maximum_width > 0) {
6070                                                 break;
6071                                         }
6072                                         h = specials[special_index].height;
6073                                         by = cur_y - (font_ascent() / 2) - 1;
6074
6075                                         if (h < font_h) {
6076                                                 by -= h / 2 - 1;
6077                                         }
6078                                         w = specials[special_index].width;
6079                                         if (w == 0) {
6080                                                 w = text_start_x + text_width - cur_x - 1;
6081                                         }
6082                                         if (w < 0) {
6083                                                 w = 0;
6084                                         }
6085                                         if (draw_graph_borders) {
6086                                                 XSetLineAttributes(display, window.gc, 1, LineSolid,
6087                                                         CapButt, JoinMiter);
6088                                                 XDrawRectangle(display, window.drawable, window.gc,
6089                                                         cur_x, by, w, h);
6090                                         }
6091                                         XSetLineAttributes(display, window.gc, 1, LineSolid,
6092                                                 CapButt, JoinMiter);
6093
6094                                         if (specials[special_index].last_colour != 0
6095                                                         || specials[special_index].first_colour != 0) {
6096                                                 float gradient_factor, gradient_update = 0;
6097                                                 int gradient_size =
6098                                                         gradient_max(specials[special_index].last_colour,
6099                                                                         specials[special_index].first_colour);
6100                                                 gradient_factor = (float) gradient_size / (w - 1);
6101                                                 tmpcolour = malloc((w - 1) * sizeof(unsigned long));
6102                                                 tmpcolour[0] = specials[special_index].last_colour;
6103                                                 gradient_update += gradient_factor;
6104                                                 for (colour_idx = 1; colour_idx < w - 1; colour_idx++) {
6105                                                         tmpcolour[colour_idx] = tmpcolour[colour_idx - 1];
6106                                                         gradient_update += gradient_factor;
6107                                                         while (gradient_update > 0) {
6108                                                                 tmpcolour[colour_idx] = do_gradient(tmpcolour[colour_idx],
6109                                                                                 specials[special_index].first_colour);
6110                                                                 gradient_update--;
6111                                                         }
6112                                                 }
6113                                         }
6114                                         colour_idx = 0;
6115                                         for (i = w - 2; i > -1; i--) {
6116                                                 if (specials[special_index].last_colour != 0
6117                                                                 || specials[special_index].first_colour != 0) {
6118                                                         if (specials[special_index].tempgrad) {
6119                                                                 XSetForeground(display, window.gc, tmpcolour[
6120                                                                                 (int)((float)(w - 1) - specials[special_index].graph[j] *
6121                                                                                  (w - 2) / (float)specials[special_index].graph_scale) - 1]);
6122                                                         } else {
6123                                                                 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6124                                                         }
6125                                                 }
6126                                                 if ((w - i) / ((float) (w - 2) /
6127                                                                 (specials[special_index].graph_width)) > j
6128                                                                 && j < MAX_GRAPH_DEPTH - 3) {
6129                                                         j++;
6130                                                 }
6131                                                 /* this is mugfugly, but it works */
6132                                                 XDrawLine(display, window.drawable, window.gc,
6133                                                         cur_x + i + 1, by + h, cur_x + i + 1,
6134                                                         by + h - specials[special_index].graph[j] *
6135                                                         (h - 1) / specials[special_index].graph_scale);
6136                                         }
6137                                         if (tmpcolour) free(tmpcolour);
6138                                         if (h > cur_y_add
6139                                                         && h > font_h) {
6140                                                 cur_y_add = h;
6141                                         }
6142                                         /* if (draw_mode == BG) {
6143                                                 set_foreground_color(default_bg_color);
6144                                         } else if (draw_mode == OUTLINE) {
6145                                                 set_foreground_color(default_out_color);
6146                                         } else {
6147                                                 set_foreground_color(default_fg_color);
6148                                         } */
6149                                         if (show_graph_range) {
6150                                                 int tmp_x = cur_x;
6151                                                 int tmp_y = cur_y;
6152                                                 unsigned short int seconds = update_interval * w;
6153                                                 char *tmp_day_str;
6154                                                 char *tmp_hour_str;
6155                                                 char *tmp_min_str;
6156                                                 char *tmp_sec_str;
6157                                                 unsigned short int timeunits;
6158                                                 if (seconds != 0) {
6159                                                         timeunits = seconds / 86400; seconds %= 86400;
6160                                                         if (timeunits > 0) {
6161                                                                 asprintf(&tmp_day_str, "%dd", timeunits);
6162                                                         } else {
6163                                                                 tmp_day_str = strdup("");
6164                                                         }
6165                                                         timeunits = seconds / 3600; seconds %= 3600;
6166                                                         if (timeunits > 0) {
6167                                                                 asprintf(&tmp_hour_str, "%dh", timeunits);
6168                                                         } else {
6169                                                                 tmp_hour_str = strdup("");
6170                                                         }
6171                                                         timeunits = seconds / 60; seconds %= 60;
6172                                                         if (timeunits > 0) {
6173                                                                 asprintf(&tmp_min_str, "%dm", timeunits);
6174                                                         } else {
6175                                                                 tmp_min_str = strdup("");
6176                                                         }
6177                                                         if (seconds > 0) {
6178                                                                 asprintf(&tmp_sec_str, "%ds", seconds);
6179                                                         } else {
6180                                                                 tmp_sec_str = strdup("");
6181                                                         }
6182                                                         asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6183                                                         free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6184                                                 } else {
6185                                                         asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6186                                                 }
6187                                                 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6188                                                 cur_y += font_h / 2;
6189                                                 draw_string(tmp_str);
6190                                                 free(tmp_str);
6191                                                 cur_x = tmp_x;
6192                                                 cur_y = tmp_y;
6193                                         }
6194 #ifdef MATH
6195                                         if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6196                                                 int tmp_x = cur_x;
6197                                                 int tmp_y = cur_y;
6198                                                 cur_x += font_ascent() / 2;
6199                                                 cur_y += font_h / 2;
6200                                                 tmp_str = (char *)
6201                                                         calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6202                                                                         sizeof(char));
6203                                                 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6204                                                 draw_string(tmp_str);
6205                                                 free(tmp_str);
6206                                                 cur_x = tmp_x;
6207                                                 cur_y = tmp_y;
6208                                         }
6209 #endif
6210                                         set_foreground_color(last_colour);
6211                                         break;
6212                                 }
6213
6214                                 case FONT:
6215                                 {
6216                                         int old = font_ascent();
6217
6218                                         cur_y -= font_ascent();
6219                                         selected_font = specials[special_index].font_added;
6220                                         if (cur_y + font_ascent() < cur_y + old) {
6221                                                 cur_y += old;
6222                                         } else {
6223                                                 cur_y += font_ascent();
6224                                         }
6225                                         set_font();
6226                                         font_h = font_height();
6227                                         break;
6228                                 }
6229                                 case FG:
6230                                         if (draw_mode == FG) {
6231                                                 set_foreground_color(specials[special_index].arg);
6232                                         }
6233                                         break;
6234
6235                                 case BG:
6236                                         if (draw_mode == BG) {
6237                                                 set_foreground_color(specials[special_index].arg);
6238                                         }
6239                                         break;
6240
6241                                 case OUTLINE:
6242                                         if (draw_mode == OUTLINE) {
6243                                                 set_foreground_color(specials[special_index].arg);
6244                                         }
6245                                         break;
6246
6247                                 case OFFSET:
6248                                         w += specials[special_index].arg;
6249                                         break;
6250
6251                                 case VOFFSET:
6252                                         cur_y += specials[special_index].arg;
6253                                         break;
6254
6255                                 case GOTO:
6256                                         if (specials[special_index].arg >= 0) {
6257                                                 cur_x = (int) specials[special_index].arg;
6258                                         }
6259                                         break;
6260
6261                                 case TAB:
6262                                 {
6263                                         int start = specials[special_index].arg;
6264                                         int step = specials[special_index].width;
6265
6266                                         if (!step || step < 0) {
6267                                                 step = 10;
6268                                         }
6269                                         w = step - (cur_x - text_start_x - start) % step;
6270                                         break;
6271                                 }
6272
6273                                 case ALIGNR:
6274                                 {
6275                                         /* TODO: add back in "+ border_margin" to the end of
6276                                          * this line? */
6277                                         int pos_x = text_start_x + text_width -
6278                                                 get_string_width_special(s);
6279
6280                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6281                                                 "get_string_width(p) %i gap_x %i "
6282                                                 "specials[special_index].arg %i border_margin %i "
6283                                                 "border_width %i\n", pos_x, text_start_x, text_width,
6284                                                 cur_x, get_string_width_special(s), gap_x,
6285                                                 specials[special_index].arg, border_margin,
6286                                                 border_width); */
6287                                         if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6288                                                 cur_x = pos_x - specials[special_index].arg;
6289                                         }
6290                                         break;
6291                                 }
6292
6293                                 case ALIGNC:
6294                                 {
6295                                         int pos_x = (text_width) / 2 - get_string_width_special(s) /
6296                                                 2 - (cur_x - text_start_x);
6297                                         /* int pos_x = text_start_x + text_width / 2 -
6298                                                 get_string_width_special(s) / 2; */
6299
6300                                         /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6301                                                 "get_string_width(p) %i gap_x %i "
6302                                                 "specials[special_index].arg %i\n", pos_x, text_start_x,
6303                                                 text_width, cur_x, get_string_width(s), gap_x,
6304                                                 specials[special_index].arg); */
6305                                         if (pos_x > specials[special_index].arg) {
6306                                                 w = pos_x - specials[special_index].arg;
6307                                         }
6308                                         break;
6309                                 }
6310                         }
6311
6312                         cur_x += w;
6313
6314                         special_index++;
6315                 }
6316
6317                 p++;
6318         }
6319
6320         if (cur_y_add > 0) {
6321                 cur_y += cur_y_add;
6322         }
6323         draw_string(s);
6324         cur_y += font_descent();
6325
6326 #endif /* X11 */
6327 }
6328
6329 static void draw_text(void)
6330 {
6331 #ifdef X11
6332         if (output_methods & TO_X) {
6333                 cur_y = text_start_y;
6334
6335                 /* draw borders */
6336                 if (draw_borders && border_width > 0) {
6337                         unsigned int b = (border_width + 1) / 2;
6338
6339                         if (stippled_borders) {
6340                                 char ss[2] = { stippled_borders, stippled_borders };
6341                                 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
6342                                         CapButt, JoinMiter);
6343                                 XSetDashes(display, window.gc, 0, ss, 2);
6344                         } else {
6345                                 XSetLineAttributes(display, window.gc, border_width, LineSolid,
6346                                         CapButt, JoinMiter);
6347                         }
6348
6349                         XDrawRectangle(display, window.drawable, window.gc,
6350                                 text_start_x - border_margin + b, text_start_y - border_margin + b,
6351                                 text_width + border_margin * 2 - 1 - b * 2,
6352                                 text_height + border_margin * 2 - 1 - b * 2);
6353                 }
6354
6355                 /* draw text */
6356                 special_index = 0;
6357         }
6358 #endif /* X11 */
6359         for_each_line(text_buffer, draw_line);
6360 }
6361
6362 static void draw_stuff(void)
6363 {
6364         if (overwrite_file) {
6365                 overwrite_fpointer = fopen(overwrite_file, "w");
6366                 if(!overwrite_fpointer)
6367                         ERR("Can't overwrite '%s' anymore", overwrite_file);
6368         }
6369         if (append_file) {
6370                 append_fpointer = fopen(append_file, "a");
6371                 if(!append_fpointer)
6372                         ERR("Can't append '%s' anymore", append_file);
6373         }
6374 #ifdef X11
6375         if (output_methods & TO_X) {
6376                 selected_font = 0;
6377                 if (draw_shades && !draw_outline) {
6378                         text_start_x++;
6379                         text_start_y++;
6380                         set_foreground_color(default_bg_color);
6381                         draw_mode = BG;
6382                         draw_text();
6383                         text_start_x--;
6384                         text_start_y--;
6385                 }
6386
6387                 if (draw_outline) {
6388                         int i, j;
6389                         selected_font = 0;
6390
6391                         for (i = -1; i < 2; i++) {
6392                                 for (j = -1; j < 2; j++) {
6393                                         if (i == 0 && j == 0) {
6394                                                 continue;
6395                                         }
6396                                         text_start_x += i;
6397                                         text_start_y += j;
6398                                         set_foreground_color(default_out_color);
6399                                         draw_mode = OUTLINE;
6400                                         draw_text();
6401                                         text_start_x -= i;
6402                                         text_start_y -= j;
6403                                 }
6404                         }
6405                 }
6406
6407                 set_foreground_color(default_fg_color);
6408         }
6409 #endif /* X11 */
6410         draw_mode = FG;
6411         draw_text();
6412 #ifdef X11
6413         if (output_methods & TO_X) {
6414 #ifdef HAVE_XDBE
6415                 if (use_xdbe) {
6416                         XdbeSwapInfo swap;
6417
6418                         swap.swap_window = window.window;
6419                         swap.swap_action = XdbeBackground;
6420                         XdbeSwapBuffers(display, &swap, 1);
6421                 }
6422 #endif
6423         }
6424 #endif /* X11 */
6425         if(overwrite_fpointer) {
6426                 fclose(overwrite_fpointer);
6427                 overwrite_fpointer = 0;
6428         }
6429         if(append_fpointer) {
6430                 fclose(append_fpointer);
6431                 append_fpointer = 0;
6432         }
6433 }
6434
6435 #ifdef X11
6436 static void clear_text(int exposures)
6437 {
6438 #ifdef HAVE_XDBE
6439         if (use_xdbe) {
6440                 /* The swap action is XdbeBackground, which clears */
6441                 return;
6442         } else
6443 #endif
6444         {
6445                 /* there is some extra space for borders and outlines */
6446                 XClearArea(display, window.window, text_start_x - border_margin - 1,
6447                         text_start_y - border_margin - 1,
6448                         text_width + border_margin * 2 + 2,
6449                         text_height + border_margin * 2 + 2, exposures ? True : 0);
6450         }
6451 }
6452 #endif /* X11 */
6453
6454 static int need_to_update;
6455
6456 /* update_text() generates new text and clears old text area */
6457 static void update_text(void)
6458 {
6459 #ifdef IMLIB2
6460         cimlib_cleanup();
6461 #endif /* IMLIB2 */
6462         generate_text();
6463 #ifdef X11
6464         if (output_methods & TO_X)
6465                 clear_text(1);
6466 #endif /* X11 */
6467         need_to_update = 1;
6468 }
6469
6470 #ifdef HAVE_SYS_INOTIFY_H
6471 int inotify_fd;
6472 #endif
6473
6474 static void main_loop(void)
6475 {
6476 #ifdef SIGNAL_BLOCKING
6477         sigset_t newmask, oldmask;
6478 #endif
6479         double t;
6480 #ifdef HAVE_SYS_INOTIFY_H
6481         int inotify_config_wd = 0;
6482 #define INOTIFY_EVENT_SIZE  (sizeof(struct inotify_event))
6483 #define INOTIFY_BUF_LEN     (20 * (INOTIFY_EVENT_SIZE + 16))
6484         char inotify_buff[INOTIFY_BUF_LEN];
6485 #endif /* HAVE_SYS_INOTIFY_H */
6486
6487
6488 #ifdef SIGNAL_BLOCKING
6489         sigemptyset(&newmask);
6490         sigaddset(&newmask, SIGINT);
6491         sigaddset(&newmask, SIGTERM);
6492         sigaddset(&newmask, SIGUSR1);
6493 #endif
6494
6495         next_update_time = last_update_time = get_time();
6496         info.looped = 0;
6497         while (total_run_times == 0 || info.looped < total_run_times) {
6498                 info.looped++;
6499
6500 #ifdef SIGNAL_BLOCKING
6501                 /* block signals.  we will inspect for pending signals later */
6502                 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
6503                         CRIT_ERR("unable to sigprocmask()");
6504                 }
6505 #endif
6506
6507 #ifdef X11
6508                 if (output_methods & TO_X) {
6509                         XFlush(display);
6510
6511                         /* wait for X event or timeout */
6512
6513                         if (!XPending(display)) {
6514                                 fd_set fdsr;
6515                                 struct timeval tv;
6516                                 int s;
6517                                 t = next_update_time - get_time();
6518
6519                                 if (t < 0) {
6520                                         t = 0;
6521                                 } else if (t > update_interval) {
6522                                         t = update_interval;
6523                                 }
6524
6525                                 tv.tv_sec = (long) t;
6526                                 tv.tv_usec = (long) (t * 1000000) % 1000000;
6527                                 FD_ZERO(&fdsr);
6528                                 FD_SET(ConnectionNumber(display), &fdsr);
6529
6530                                 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
6531                                 if (s == -1) {
6532                                         if (errno != EINTR) {
6533                                                 ERR("can't select(): %s", strerror(errno));
6534                                         }
6535                                 } else {
6536                                         /* timeout */
6537                                         if (s == 0) {
6538                                                 update_text();
6539                                         }
6540                                 }
6541                         }
6542
6543                         if (need_to_update) {
6544 #ifdef OWN_WINDOW
6545                                 int wx = window.x, wy = window.y;
6546 #endif
6547
6548                                 need_to_update = 0;
6549                                 selected_font = 0;
6550                                 update_text_area();
6551 #ifdef OWN_WINDOW
6552                                 if (own_window) {
6553                                         /* resize window if it isn't right size */
6554                                         if (!fixed_size
6555                                                 && (text_width + border_margin * 2 + 1 != window.width
6556                                                 || text_height + border_margin * 2 + 1 != window.height)) {
6557                                                         window.width = text_width + border_margin * 2 + 1;
6558                                                         window.height = text_height + border_margin * 2 + 1;
6559                                                         XResizeWindow(display, window.window, window.width,
6560                                                                 window.height);
6561                                                         if (own_window) {
6562                                                                 set_transparent_background(window.window);
6563                                                         }
6564                                         }
6565
6566                                         /* move window if it isn't in right position */
6567                                         if (!fixed_pos && (window.x != wx || window.y != wy)) {
6568                                                 XMoveWindow(display, window.window, window.x, window.y);
6569                                         }
6570                                 }
6571 #endif
6572
6573                                 clear_text(1);
6574
6575 #ifdef HAVE_XDBE
6576                                 if (use_xdbe) {
6577                                         XRectangle r;
6578
6579                                         r.x = text_start_x - border_margin;
6580                                         r.y = text_start_y - border_margin;
6581                                         r.width = text_width + border_margin * 2;
6582                                         r.height = text_height + border_margin * 2;
6583                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
6584                                 }
6585 #endif
6586                         }
6587
6588                         /* handle X events */
6589                         while (XPending(display)) {
6590                                 XEvent ev;
6591
6592                                 XNextEvent(display, &ev);
6593                                 switch (ev.type) {
6594                                         case Expose:
6595                                         {
6596                                                 XRectangle r;
6597                                                 r.x = ev.xexpose.x;
6598                                                 r.y = ev.xexpose.y;
6599                                                 r.width = ev.xexpose.width;
6600                                                 r.height = ev.xexpose.height;
6601                                                 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
6602                                                 break;
6603                                         }
6604
6605 #ifdef OWN_WINDOW
6606                                         case ReparentNotify:
6607                                                 /* set background to ParentRelative for all parents */
6608                                                 if (own_window) {
6609                                                         set_transparent_background(window.window);
6610                                                 }
6611                                                 break;
6612
6613                                         case ConfigureNotify:
6614                                                 if (own_window) {
6615                                                         /* if window size isn't what expected, set fixed size */
6616                                                         if (ev.xconfigure.width != window.width
6617                                                                         || ev.xconfigure.height != window.height) {
6618                                                                 if (window.width != 0 && window.height != 0) {
6619                                                                         fixed_size = 1;
6620                                                                 }
6621
6622                                                                 /* clear old stuff before screwing up
6623                                                                  * size and pos */
6624                                                                 clear_text(1);
6625
6626                                                                 {
6627                                                                         XWindowAttributes attrs;
6628                                                                         if (XGetWindowAttributes(display,
6629                                                                                         window.window, &attrs)) {
6630                                                                                 window.width = attrs.width;
6631                                                                                 window.height = attrs.height;
6632                                                                         }
6633                                                                 }
6634
6635                                                                 text_width = window.width - border_margin * 2 - 1;
6636                                                                 text_height = window.height - border_margin * 2 - 1;
6637                                                                 if (text_width > maximum_width
6638                                                                                 && maximum_width > 0) {
6639                                                                         text_width = maximum_width;
6640                                                                 }
6641                                                         }
6642
6643                                                         /* if position isn't what expected, set fixed pos
6644                                                          * total_updates avoids setting fixed_pos when window
6645                                                          * is set to weird locations when started */
6646                                                         /* // this is broken
6647                                                         if (total_updates >= 2 && !fixed_pos
6648                                                                         && (window.x != ev.xconfigure.x
6649                                                                         || window.y != ev.xconfigure.y)
6650                                                                         && (ev.xconfigure.x != 0
6651                                                                         || ev.xconfigure.y != 0)) {
6652                                                                 fixed_pos = 1;
6653                                                         } */
6654                                                         set_font();
6655                                                 }
6656                                                 break;
6657
6658                                         case ButtonPress:
6659                                                 if (own_window) {
6660                                                         /* if an ordinary window with decorations */
6661                                                         if ((window.type == TYPE_NORMAL)
6662                                                                 && (!TEST_HINT(window.hints,
6663                                                                 HINT_UNDECORATED))) {
6664                                                                 /* allow conky to hold input focus. */
6665                                                                 break;
6666                                                         } else {
6667                                                                 /* forward the click to the desktop window */
6668                                                                 XUngrabPointer(display, ev.xbutton.time);
6669                                                                 ev.xbutton.window = window.desktop;
6670                                                                 XSendEvent(display, ev.xbutton.window, False,
6671                                                                         ButtonPressMask, &ev);
6672                                                                 XSetInputFocus(display, ev.xbutton.window,
6673                                                                         RevertToParent, ev.xbutton.time);
6674                                                         }
6675                                                 }
6676                                                 break;
6677
6678                                         case ButtonRelease:
6679                                                 if (own_window) {
6680                                                         /* if an ordinary window with decorations */
6681                                                         if ((window.type == TYPE_NORMAL)
6682                                                                         && (!TEST_HINT(window.hints,
6683                                                                         HINT_UNDECORATED))) {
6684                                                                 /* allow conky to hold input focus. */
6685                                                                 break;
6686                                                         } else {
6687                                                                 /* forward the release to the desktop window */
6688                                                                 ev.xbutton.window = window.desktop;
6689                                                                 XSendEvent(display, ev.xbutton.window, False,
6690                                                                         ButtonReleaseMask, &ev);
6691                                                         }
6692                                                 }
6693                                                 break;
6694
6695 #endif
6696
6697                                         default:
6698 #ifdef HAVE_XDAMAGE
6699                                                 if (ev.type == x11_stuff.event_base + XDamageNotify) {
6700                                                         XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
6701
6702                                                         XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
6703                                                         XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
6704                                                 }
6705 #endif /* HAVE_XDAMAGE */
6706                                                 break;
6707                                 }
6708                         }
6709
6710 #ifdef HAVE_XDAMAGE
6711                         XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
6712                         XFixesSetRegion(display, x11_stuff.region2, 0, 0);
6713 #endif /* HAVE_XDAMAGE */
6714
6715                         /* XDBE doesn't seem to provide a way to clear the back buffer without
6716                          * interfering with the front buffer, other than passing XdbeBackground
6717                          * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6718                          * redraw the text even if it wasn't part of the exposed area. OTOH,
6719                          * if we're not going to call draw_stuff at all, then no swap happens
6720                          * and we can safely do nothing. */
6721
6722                         if (!XEmptyRegion(x11_stuff.region)) {
6723 #ifdef HAVE_XDBE
6724                                 if (use_xdbe) {
6725                                         XRectangle r;
6726
6727                                         r.x = text_start_x - border_margin;
6728                                         r.y = text_start_y - border_margin;
6729                                         r.width = text_width + border_margin * 2;
6730                                         r.height = text_height + border_margin * 2;
6731                                         XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
6732                                 }
6733 #endif
6734                                 XSetRegion(display, window.gc, x11_stuff.region);
6735 #ifdef XFT
6736                                 if (use_xft) {
6737                                         XftDrawSetClip(window.xftdraw, x11_stuff.region);
6738                                 }
6739 #endif
6740 #ifdef IMLIB2
6741                                 cimlib_render(text_start_x, text_start_y, window.width, window.height);
6742 #endif /* IMLIB2 */
6743                                 draw_stuff();
6744                                 XDestroyRegion(x11_stuff.region);
6745                                 x11_stuff.region = XCreateRegion();
6746                         }
6747                 } else {
6748 #endif /* X11 */
6749                         t = (next_update_time - get_time()) * 1000000;
6750                         if(t > 0) usleep((useconds_t)t);
6751                         update_text();
6752                         draw_stuff();
6753 #ifdef X11
6754                 }
6755 #endif /* X11 */
6756
6757 #ifdef SIGNAL_BLOCKING
6758                 /* unblock signals of interest and let handler fly */
6759                 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
6760                         CRIT_ERR("unable to sigprocmask()");
6761                 }
6762 #endif
6763
6764                 switch (g_signal_pending) {
6765                         case SIGHUP:
6766                         case SIGUSR1:
6767                                 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
6768                                 reload_config();
6769                                 break;
6770                         case SIGINT:
6771                         case SIGTERM:
6772                                 ERR("received SIGINT or SIGTERM to terminate. bye!");
6773                                 clean_up();
6774 #ifdef X11
6775                                 if (output_methods & TO_X) {
6776                                         XDestroyRegion(x11_stuff.region);
6777                                         x11_stuff.region = NULL;
6778 #ifdef HAVE_XDAMAGE
6779                                         XDamageDestroy(display, x11_stuff.damage);
6780                                         XFixesDestroyRegion(display, x11_stuff.region2);
6781                                         XFixesDestroyRegion(display, x11_stuff.part);
6782 #endif /* HAVE_XDAMAGE */
6783                                 }
6784 #endif /* X11 */
6785                                 if(overwrite_file) {
6786                                         free(overwrite_file);
6787                                         overwrite_file = 0;
6788                                 }
6789                                 if(append_file) {
6790                                         free(append_file);
6791                                         append_file = 0;
6792                                 }
6793                                 return; /* return from main_loop */
6794                                 /* break; */
6795                         default:
6796                                 /* Reaching here means someone set a signal
6797                                  * (SIGXXXX, signal_handler), but didn't write any code
6798                                  * to deal with it.
6799                                  * If you don't want to handle a signal, don't set a handler on
6800                                  * it in the first place. */
6801                                 if (g_signal_pending) {
6802                                         ERR("ignoring signal (%d)", g_signal_pending);
6803                                 }
6804                                 break;
6805                 }
6806 #ifdef HAVE_SYS_INOTIFY_H
6807                 if (inotify_fd && !inotify_config_wd) {
6808                         inotify_config_wd = inotify_add_watch(inotify_fd,
6809                                         current_config,
6810                                         IN_MODIFY);
6811                 }
6812                 if (inotify_fd && inotify_config_wd) {
6813                         int len = 0, idx = 0;
6814                         fd_set descriptors;
6815                         struct timeval time_to_wait;
6816
6817                         FD_ZERO (&descriptors);
6818                         FD_SET(inotify_fd, &descriptors);
6819
6820                         time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
6821
6822                         select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
6823                         if (FD_ISSET(inotify_fd, &descriptors)) {
6824                                 /* process inotify events */
6825                                 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
6826                                 while (len > 0 && idx < len) {
6827                                         struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
6828                                         if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
6829                                                 /* current_config should be reloaded */
6830                                                 ERR("'%s' modified, reloading...", current_config);
6831                                                 reload_config();
6832                                                 if (ev->mask & IN_IGNORED) {
6833                                                         /* for some reason we get IN_IGNORED here
6834                                                          * sometimes, so we need to re-add the watch */
6835                                                         inotify_config_wd = inotify_add_watch(inotify_fd,
6836                                                                         current_config,
6837                                                                         IN_MODIFY);
6838                                                 }
6839                                         }
6840 #ifdef HAVE_LUA
6841                                         else {
6842                                                 llua_inotify_query(ev->wd, ev->mask);
6843                                         }
6844 #endif /* HAVE_LUA */
6845                                         idx += INOTIFY_EVENT_SIZE + ev->len;
6846                                 }
6847                         }
6848                 }
6849 #endif /* HAVE_SYS_INOTIFY_H */
6850
6851                 g_signal_pending = 0;
6852         }
6853
6854 #ifdef HAVE_SYS_INOTIFY_H
6855         if (inotify_fd) {
6856                 inotify_rm_watch(inotify_fd, inotify_config_wd);
6857                 close(inotify_fd);
6858                 inotify_fd = inotify_config_wd = 0;
6859         }
6860 #endif /* HAVE_SYS_INOTIFY_H */
6861
6862 #if defined(X11) && defined(HAVE_XDAMAGE)
6863         X11_destroy_window();
6864 #endif /* X11 && HAVE_XDAMAGE */
6865 }
6866
6867 static void load_config_file(const char *);
6868 static void load_config_file_x11(const char *);
6869
6870 /* reload the config file */
6871 static void reload_config(void)
6872 {
6873         timed_thread_destroy_registered_threads();
6874
6875         if (info.cpu_usage) {
6876                 free(info.cpu_usage);
6877                 info.cpu_usage = NULL;
6878         }
6879
6880         if (info.mail) {
6881                 free(info.mail);
6882         }
6883
6884 #ifdef X11
6885         free_fonts();
6886 #endif /* X11 */
6887
6888 #ifdef TCP_PORT_MONITOR
6889         tcp_portmon_clear();
6890 #endif
6891
6892 #ifdef HAVE_LUA
6893         llua_close();
6894 #endif /* HAVE_LUA */
6895
6896 #ifdef X11
6897         X11_destroy_window();
6898 #endif /* X11 */
6899
6900         if (current_config) {
6901                 clear_fs_stats();
6902                 load_config_file(current_config);
6903                 load_config_file_x11(current_config);
6904
6905                 /* re-init specials array */
6906                 if ((specials = realloc((void *) specials,
6907                                 sizeof(struct special_t) * max_specials)) == 0) {
6908                         ERR("failed to realloc specials array");
6909                 }
6910
6911 #ifdef X11
6912                 x_initialised = NO;
6913 #endif /* X11 */
6914                 extract_variable_text(global_text);
6915                 free(global_text);
6916                 global_text = NULL;
6917                 if (tmpstring1) {
6918                         free(tmpstring1);
6919                 }
6920                 tmpstring1 = malloc(text_buffer_size);
6921                 memset(tmpstring1, 0, text_buffer_size);
6922                 if (tmpstring2) {
6923                         free(tmpstring2);
6924                 }
6925                 tmpstring2 = malloc(text_buffer_size);
6926                 memset(tmpstring2, 0, text_buffer_size);
6927                 if (text_buffer) {
6928                         free(text_buffer);
6929                 }
6930                 text_buffer = malloc(max_user_text);
6931                 memset(text_buffer, 0, max_user_text);
6932                 update_text();
6933 #ifdef X11
6934                 X11_create_window();
6935 #endif /* X11 */
6936         }
6937 }
6938
6939 static void clean_up(void)
6940 {
6941         timed_thread_destroy_registered_threads();
6942
6943         if (info.cpu_usage) {
6944                 free(info.cpu_usage);
6945                 info.cpu_usage = NULL;
6946         }
6947 #ifdef X11
6948         if (output_methods & TO_X) {
6949 #ifdef HAVE_XDBE
6950                 if (use_xdbe) {
6951                         XdbeDeallocateBackBufferName(display, window.back_buffer);
6952                 }
6953 #endif
6954 #ifdef OWN_WINDOW
6955                 if (own_window) {
6956                         XDestroyWindow(display, window.window);
6957                         XClearWindow(display, RootWindow(display, screen));
6958                         XFlush(display);
6959                 } else
6960 #endif
6961                 {
6962                         XClearWindow(display, RootWindow(display, screen));
6963                         clear_text(1);
6964                         XFlush(display);
6965                 }
6966
6967                 XFreeGC(display, window.gc);
6968                 free_fonts();
6969         }
6970
6971 #endif /* X11 */
6972
6973         free_text_objects(&global_root_object, 0);
6974         if (tmpstring1) {
6975                 free(tmpstring1);
6976                 tmpstring1 = 0;
6977         }
6978         if (tmpstring2) {
6979                 free(tmpstring2);
6980                 tmpstring2 = 0;
6981         }
6982         if (text_buffer) {
6983                 free(text_buffer);
6984                 text_buffer = 0;
6985         }
6986
6987         if (global_text) {
6988                 free(global_text);
6989                 global_text = 0;
6990         }
6991
6992         free(current_config);
6993
6994 #ifdef TCP_PORT_MONITOR
6995         tcp_portmon_clear();
6996 #endif
6997 #ifdef RSS
6998         free_rss_info();
6999 #endif
7000 #ifdef HAVE_LUA
7001         llua_close();
7002 #endif /* HAVE_LUA */
7003
7004         if (specials) {
7005                 unsigned int i;
7006
7007                 for (i = 0; i < special_count; i++) {
7008                         if (specials[i].type == GRAPH) {
7009                                 free(specials[i].graph);
7010                         }
7011                 }
7012                 free(specials);
7013                 specials = NULL;
7014         }
7015
7016         clear_diskio_stats();
7017 }
7018
7019 static int string_to_bool(const char *s)
7020 {
7021         if (!s) {
7022                 // Assumes an option without a true/false means true
7023                 return 1;
7024         } else if (strcasecmp(s, "yes") == EQUAL) {
7025                 return 1;
7026         } else if (strcasecmp(s, "true") == EQUAL) {
7027                 return 1;
7028         } else if (strcasecmp(s, "1") == EQUAL) {
7029                 return 1;
7030         }
7031         return 0;
7032 }
7033
7034 #ifdef X11
7035 static enum alignment string_to_alignment(const char *s)
7036 {
7037         if (strcasecmp(s, "top_left") == EQUAL) {
7038                 return TOP_LEFT;
7039         } else if (strcasecmp(s, "top_right") == EQUAL) {
7040                 return TOP_RIGHT;
7041         } else if (strcasecmp(s, "top_middle") == EQUAL) {
7042                 return TOP_MIDDLE;
7043         } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7044                 return BOTTOM_LEFT;
7045         } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7046                 return BOTTOM_RIGHT;
7047         } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7048                 return BOTTOM_MIDDLE;
7049         } else if (strcasecmp(s, "middle_left") == EQUAL) {
7050                 return MIDDLE_LEFT;
7051         } else if (strcasecmp(s, "middle_right") == EQUAL) {
7052                 return MIDDLE_RIGHT;
7053         } else if (strcasecmp(s, "tl") == EQUAL) {
7054                 return TOP_LEFT;
7055         } else if (strcasecmp(s, "tr") == EQUAL) {
7056                 return TOP_RIGHT;
7057         } else if (strcasecmp(s, "tm") == EQUAL) {
7058                 return TOP_MIDDLE;
7059         } else if (strcasecmp(s, "bl") == EQUAL) {
7060                 return BOTTOM_LEFT;
7061         } else if (strcasecmp(s, "br") == EQUAL) {
7062                 return BOTTOM_RIGHT;
7063         } else if (strcasecmp(s, "bm") == EQUAL) {
7064                 return BOTTOM_MIDDLE;
7065         } else if (strcasecmp(s, "ml") == EQUAL) {
7066                 return MIDDLE_LEFT;
7067         } else if (strcasecmp(s, "mr") == EQUAL) {
7068                 return MIDDLE_RIGHT;
7069         } else if (strcasecmp(s, "none") == EQUAL) {
7070                 return NONE;
7071         }
7072         return TOP_LEFT;
7073 }
7074 #endif /* X11 */
7075
7076 #ifdef X11
7077 static void set_default_configurations_for_x(void)
7078 {
7079         default_fg_color = WhitePixel(display, screen);
7080         default_bg_color = BlackPixel(display, screen);
7081         default_out_color = BlackPixel(display, screen);
7082         color0 = default_fg_color;
7083         color1 = default_fg_color;
7084         color2 = default_fg_color;
7085         color3 = default_fg_color;
7086         color4 = default_fg_color;
7087         color5 = default_fg_color;
7088         color6 = default_fg_color;
7089         color7 = default_fg_color;
7090         color8 = default_fg_color;
7091         color9 = default_fg_color;
7092 }
7093 #endif /* X11 */
7094
7095 static void set_default_configurations(void)
7096 {
7097         update_uname();
7098         fork_to_background = 0;
7099         total_run_times = 0;
7100         info.cpu_avg_samples = 2;
7101         info.net_avg_samples = 2;
7102         info.diskio_avg_samples = 2;
7103         info.memmax = 0;
7104         top_cpu = 0;
7105         cpu_separate = 0;
7106         short_units = 0;
7107         top_mem = 0;
7108         top_time = 0;
7109 #ifdef MPD
7110         mpd_set_host("localhost");
7111         mpd_set_port("6600");
7112 #endif
7113 #ifdef XMMS2
7114         info.xmms2.artist = NULL;
7115         info.xmms2.album = NULL;
7116         info.xmms2.title = NULL;
7117         info.xmms2.genre = NULL;
7118         info.xmms2.comment = NULL;
7119         info.xmms2.url = NULL;
7120         info.xmms2.status = NULL;
7121         info.xmms2.playlist = NULL;
7122 #endif
7123         use_spacer = NO_SPACER;
7124 #ifdef X11
7125         output_methods = TO_X;
7126 #else
7127         output_methods = TO_STDOUT;
7128 #endif
7129 #ifdef X11
7130         show_graph_scale = 0;
7131         show_graph_range = 0;
7132         template[0] = strdup("");
7133         template[1] = strdup("");
7134         template[2] = strdup("");
7135         template[3] = strdup("");
7136         template[4] = strdup("");
7137         template[5] = strdup("");
7138         template[6] = strdup("");
7139         template[7] = strdup("");
7140         template[8] = strdup("");
7141         template[9] = strdup("");
7142         draw_shades = 1;
7143         draw_borders = 0;
7144         draw_graph_borders = 1;
7145         draw_outline = 0;
7146         set_first_font("6x10");
7147         gap_x = 5;
7148         gap_y = 60;
7149         minimum_width = 5;
7150         minimum_height = 5;
7151         maximum_width = 0;
7152 #ifdef OWN_WINDOW
7153         own_window = 0;
7154         window.type = TYPE_NORMAL;
7155         window.hints = 0;
7156         strcpy(window.class_name, PACKAGE_NAME);
7157         sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7158 #endif
7159         stippled_borders = 0;
7160         border_margin = 3;
7161         border_width = 1;
7162         text_alignment = BOTTOM_LEFT;
7163         info.x11.monitor.number = 1;
7164         info.x11.monitor.current = 0;
7165 #endif /* X11 */
7166
7167         free(current_mail_spool);
7168         {
7169                 char buf[256];
7170
7171                 variable_substitute(MAIL_FILE, buf, 256);
7172                 if (buf[0] != '\0') {
7173                         current_mail_spool = strndup(buf, text_buffer_size);
7174                 }
7175         }
7176
7177         no_buffers = 1;
7178         update_interval = 3.0;
7179         info.music_player_interval = 1.0;
7180         stuff_in_upper_case = 0;
7181         info.users.number = 1;
7182
7183 #ifdef TCP_PORT_MONITOR
7184         /* set default connection limit */
7185         tcp_portmon_set_max_connections(0);
7186 #endif
7187 }
7188
7189 /* returns 1 if you can overwrite or create the file at 'path' */
7190 static _Bool overwrite_works(const char *path)
7191 {
7192         FILE *filepointer;
7193
7194         if (!(filepointer = fopen(path, "w")))
7195                 return 0;
7196         fclose(filepointer);
7197         return 1;
7198 }
7199
7200 /* returns 1 if you can append or create the file at 'path' */
7201 static _Bool append_works(const char *path)
7202 {
7203         FILE *filepointer;
7204
7205         if (!(filepointer = fopen(path, "a")))
7206                 return 0;
7207         fclose(filepointer);
7208         return 1;
7209 }
7210
7211 #ifdef X11
7212 static void X11_initialisation(void)
7213 {
7214         if (x_initialised == YES) return;
7215         output_methods |= TO_X;
7216         init_X11(disp);
7217         set_default_configurations_for_x();
7218         x_initialised = YES;
7219 }
7220
7221 static void X11_destroy_window(void)
7222 {
7223         /* this function only exists for the sake of consistency */
7224         if (output_methods & TO_X) {
7225 #ifdef HAVE_XDAMAGE
7226                 XDamageDestroy(display, x11_stuff.damage);
7227                 XFixesDestroyRegion(display, x11_stuff.region2);
7228                 XFixesDestroyRegion(display, x11_stuff.part);
7229                 XDestroyRegion(x11_stuff.region);
7230                 x11_stuff.region = NULL;
7231 #endif /* HAVE_XDAMAGE */
7232                 destroy_window();
7233         }
7234 }
7235
7236 static char **xargv = 0;
7237 static int xargc = 0;
7238
7239 static void X11_create_window(void)
7240 {
7241         if (output_methods & TO_X) {
7242 #ifdef OWN_WINDOW
7243                 init_window(own_window, text_width + border_margin * 2 + 1,
7244                                 text_height + border_margin * 2 + 1, set_transparent, background_colour,
7245                                 xargv, xargc);
7246 #else /* OWN_WINDOW */
7247                 init_window(0, text_width + border_margin * 2 + 1,
7248                                 text_height + border_margin * 2 + 1, set_transparent, 0,
7249                                 xargv, xargc);
7250 #endif /* OWN_WINDOW */
7251
7252                 selected_font = 0;
7253                 load_fonts();
7254                 update_text_area();     /* to position text/window on screen */
7255
7256 #ifdef OWN_WINDOW
7257                 if (own_window && !fixed_pos) {
7258                         XMoveWindow(display, window.window, window.x, window.y);
7259                 }
7260                 if (own_window) {
7261                         set_transparent_background(window.window);
7262                 }
7263 #endif
7264
7265                 create_gc();
7266
7267                 set_font();
7268                 draw_stuff();
7269
7270                 x11_stuff.region = XCreateRegion();
7271 #ifdef HAVE_XDAMAGE
7272                 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7273                         ERR("Xdamage extension unavailable");
7274                 }
7275                 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7276                 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7277                 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7278 #endif /* HAVE_XDAMAGE */
7279
7280                 selected_font = 0;
7281                 update_text_area();     /* to get initial size of the window */
7282         }
7283 }
7284 #endif /* X11 */
7285
7286 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7287 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
7288 #define CONF2(a) if (strcasecmp(name, a) == 0)
7289 #define CONF(a) else CONF2(a)
7290 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7291                 || strcasecmp(name, b) == 0)
7292 #define CONF_CONTINUE 1
7293 #define CONF_BREAK 2
7294 #define CONF_BUFF_SIZE 512
7295
7296 static FILE *open_config_file(const char *f)
7297 {
7298 #ifdef CONFIG_OUTPUT
7299         if (!strcmp(f, "==builtin==")) {
7300 #ifdef HAVE_FOPENCOOKIE
7301                 return fopencookie(NULL, "r", conf_cookie);
7302 #endif /* HAVE_FOPENCOOKIE */
7303         } else
7304 #endif /* CONFIG_OUTPUT */
7305                 return fopen(f, "r");
7306 }
7307
7308 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
7309 {
7310         char *p, *p2;
7311         (*line)++;
7312         if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7313                 return CONF_BREAK;
7314         }
7315         remove_comments(buf);
7316
7317         p = buf;
7318
7319         /* skip spaces */
7320         while (*p && isspace((int) *p)) {
7321                 p++;
7322         }
7323         if (*p == '\0') {
7324                 return CONF_CONTINUE;   /* empty line */
7325         }
7326
7327         *name = p;
7328
7329         /* skip name */
7330         p2 = p;
7331         while (*p2 && !isspace((int) *p2)) {
7332                 p2++;
7333         }
7334         if (*p2 != '\0') {
7335                 *p2 = '\0';     /* break at name's end */
7336                 p2++;
7337         }
7338
7339         /* get value */
7340         if (*p2) {
7341                 p = p2;
7342                 while (*p && isspace((int) *p)) {
7343                         p++;
7344                 }
7345
7346                 *value = p;
7347
7348                 p2 = *value + strlen(*value);
7349                 while (isspace((int) *(p2 - 1))) {
7350                         *--p2 = '\0';
7351                 }
7352         } else {
7353                 *value = 0;
7354         }
7355         return 0;
7356 }
7357
7358 static void load_config_file(const char *f)
7359 {
7360         int line = 0;
7361         FILE *fp;
7362
7363         set_default_configurations();
7364         fp = open_config_file(f);
7365         if (!fp) {
7366                 return;
7367         }
7368         DBGP("reading contents from config file '%s'", f);
7369
7370         while (!feof(fp)) {
7371                 char buff[CONF_BUFF_SIZE], *name, *value;
7372                 int ret = do_config_step(&line, fp, buff, &name, &value);
7373                 if (ret == CONF_BREAK) {
7374                         break;
7375                 } else if (ret == CONF_CONTINUE) {
7376                         continue;
7377                 }
7378
7379 #ifdef X11
7380                 CONF2("out_to_x") {
7381                         /* don't listen if X is already initialised or
7382                          * if we already know we don't want it */
7383                         if(x_initialised == NO) {
7384                                 if (string_to_bool(value)) {
7385                                         output_methods &= TO_X;
7386                                 } else {
7387                                         output_methods &= ~TO_X;
7388                                         x_initialised = NEVER;
7389                                 }
7390                         }
7391                 }
7392                 CONF("display") {
7393                         if (!value || x_initialised == YES) {
7394                                 CONF_ERR;
7395                         } else {
7396                                 if (disp)
7397                                         free(disp);
7398                                 disp = strdup(value);
7399                         }
7400                 }
7401                 CONF("alignment") {
7402                         if (window.type == TYPE_DOCK)
7403                                 ;
7404                         else if (value) {
7405                                 int a = string_to_alignment(value);
7406
7407                                 if (a <= 0) {
7408                                         CONF_ERR;
7409                                 } else {
7410                                         text_alignment = a;
7411                                 }
7412                         } else {
7413                                 CONF_ERR;
7414                         }
7415                 }
7416                 CONF("background") {
7417                         fork_to_background = string_to_bool(value);
7418                 }
7419 #else
7420                 CONF2("background") {
7421                         fork_to_background = string_to_bool(value);
7422                 }
7423 #endif /* X11 */
7424 #ifdef X11
7425                 CONF("show_graph_scale") {
7426                         show_graph_scale = string_to_bool(value);
7427                 }
7428                 CONF("show_graph_range") {
7429                         show_graph_range = string_to_bool(value);
7430                 }
7431                 CONF("border_margin") {
7432                         if (value) {
7433                                 border_margin = strtol(value, 0, 0);
7434                         } else {
7435                                 CONF_ERR;
7436                         }
7437                 }
7438                 CONF("border_width") {
7439                         if (value) {
7440                                 border_width = strtol(value, 0, 0);
7441                         } else {
7442                                 CONF_ERR;
7443                         }
7444                 }
7445 #define TEMPLATE_CONF(n) \
7446                 CONF("template"#n) { \
7447                         if (value) { \
7448                                 free(template[n]); \
7449                                 template[n] = strdup(value); \
7450                         } else { \
7451                                 CONF_ERR; \
7452                         } \
7453                 }
7454                 TEMPLATE_CONF(0)
7455                 TEMPLATE_CONF(1)
7456                 TEMPLATE_CONF(2)
7457                 TEMPLATE_CONF(3)
7458                 TEMPLATE_CONF(4)
7459                 TEMPLATE_CONF(5)
7460                 TEMPLATE_CONF(6)
7461                 TEMPLATE_CONF(7)
7462                 TEMPLATE_CONF(8)
7463                 TEMPLATE_CONF(9)
7464 #endif /* X11 */
7465                 CONF("imap") {
7466                         if (value) {
7467                                 info.mail = parse_mail_args(IMAP_TYPE, value);
7468                         } else {
7469                                 CONF_ERR;
7470                         }
7471                 }
7472                 CONF("pop3") {
7473                         if (value) {
7474                                 info.mail = parse_mail_args(POP3_TYPE, value);
7475                         } else {
7476                                 CONF_ERR;
7477                         }
7478                 }
7479                 CONF("default_bar_size") {
7480                         char err = 0;
7481                         if (value) {
7482                                 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
7483                                         err = 1;
7484                                 }
7485                         } else {
7486                                 err = 1;
7487                         }
7488                         if (err) {
7489                                 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
7490                         }
7491                 }
7492 #ifdef X11
7493                 CONF("default_graph_size") {
7494                         char err = 0;
7495                         if (value) {
7496                                 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
7497                                         err = 1;
7498                                 }
7499                         } else {
7500                                 err = 1;
7501                         }
7502                         if (err) {
7503                                 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
7504                         }
7505                 }
7506                 CONF("default_gauge_size") {
7507                         char err = 0;
7508                         if (value) {
7509                                 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
7510                                         err = 1;
7511                                 }
7512                         } else {
7513                                 err = 1;
7514                         }
7515                         if (err) {
7516                                 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
7517                         }
7518                 }
7519 #endif
7520 #ifdef MPD
7521                 CONF("mpd_host") {
7522                         if (value) {
7523                                 mpd_set_host(value);
7524                         } else {
7525                                 CONF_ERR;
7526                         }
7527                 }
7528                 CONF("mpd_port") {
7529                         if (value && mpd_set_port(value)) {
7530                                 CONF_ERR;
7531                         }
7532                 }
7533                 CONF("mpd_password") {
7534                         if (value) {
7535                                 mpd_set_password(value);
7536                         } else {
7537                                 CONF_ERR;
7538                         }
7539                 }
7540 #endif
7541                 CONF("music_player_interval") {
7542                         if (value) {
7543                                 info.music_player_interval = strtod(value, 0);
7544                         } else {
7545                                 CONF_ERR;
7546                         }
7547                 }
7548 #ifdef __OpenBSD__
7549                 CONF("sensor_device") {
7550                         if (value) {
7551                                 sensor_device = strtol(value, 0, 0);
7552                         } else {
7553                                 CONF_ERR;
7554                         }
7555                 }
7556 #endif
7557                 CONF("cpu_avg_samples") {
7558                         if (value) {
7559                                 cpu_avg_samples = strtol(value, 0, 0);
7560                                 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
7561                                         CONF_ERR;
7562                                 } else {
7563                                         info.cpu_avg_samples = cpu_avg_samples;
7564                                 }
7565                         } else {
7566                                 CONF_ERR;
7567                         }
7568                 }
7569                 CONF("net_avg_samples") {
7570                         if (value) {
7571                                 net_avg_samples = strtol(value, 0, 0);
7572                                 if (net_avg_samples < 1 || net_avg_samples > 14) {
7573                                         CONF_ERR;
7574                                 } else {
7575                                         info.net_avg_samples = net_avg_samples;
7576                                 }
7577                         } else {
7578                                 CONF_ERR;
7579                         }
7580                 }
7581                 CONF("diskio_avg_samples") {
7582                         if (value) {
7583                                 diskio_avg_samples = strtol(value, 0, 0);
7584                                 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
7585                                         CONF_ERR;
7586                                 } else {
7587                                         info.diskio_avg_samples = diskio_avg_samples;
7588                                 }
7589                         } else {
7590                                 CONF_ERR;
7591                         }
7592                 }
7593
7594 #ifdef HAVE_XDBE
7595                 CONF("double_buffer") {
7596                         use_xdbe = string_to_bool(value);
7597                 }
7598 #endif
7599 #ifdef X11
7600                 CONF("override_utf8_locale") {
7601                         utf8_mode = string_to_bool(value);
7602                 }
7603                 CONF("draw_borders") {
7604                         draw_borders = string_to_bool(value);
7605                 }
7606                 CONF("draw_graph_borders") {
7607                         draw_graph_borders = string_to_bool(value);
7608                 }
7609                 CONF("draw_shades") {
7610                         draw_shades = string_to_bool(value);
7611                 }
7612                 CONF("draw_outline") {
7613                         draw_outline = string_to_bool(value);
7614                 }
7615 #endif /* X11 */
7616                 CONF("out_to_console") {
7617                         if(string_to_bool(value))
7618                                 output_methods |= TO_STDOUT;
7619                 }
7620                 CONF("out_to_stderr") {
7621                         if(string_to_bool(value))
7622                                 output_methods |= TO_STDERR;
7623                 }
7624                 CONF("overwrite_file") {
7625                         if(overwrite_file) {
7626                                 free(overwrite_file);
7627                                 overwrite_file = 0;
7628                         }
7629                         if(overwrite_works(value)) {
7630                                 overwrite_file = strdup(value);
7631                                 output_methods |= OVERWRITE_FILE;
7632                         } else
7633                                 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
7634                 }
7635                 CONF("append_file") {
7636                         if(append_file) {
7637                                 free(append_file);
7638                                 append_file = 0;
7639                         }
7640                         if(append_works(value)) {
7641                                 append_file = strdup(value);
7642                                 output_methods |= APPEND_FILE;
7643                         } else
7644                                 ERR("append_file won't be able to create/append '%s'", value);
7645                 }
7646                 CONF("use_spacer") {
7647                         if (value) {
7648                                 if (strcasecmp(value, "left") == EQUAL) {
7649                                         use_spacer = LEFT_SPACER;
7650                                 } else if (strcasecmp(value, "right") == EQUAL) {
7651                                         use_spacer = RIGHT_SPACER;
7652                                 } else if (strcasecmp(value, "none") == EQUAL) {
7653                                         use_spacer = NO_SPACER;
7654                                 } else {
7655                                         use_spacer = string_to_bool(value);
7656                                         ERR("use_spacer should have an argument of left, right, or"
7657                                                 " none.  '%s' seems to be some form of '%s', so"
7658                                                 " defaulting to %s.", value,
7659                                                 use_spacer ? "true" : "false",
7660                                                 use_spacer ? "right" : "none");
7661                                         if (use_spacer) {
7662                                                 use_spacer = RIGHT_SPACER;
7663                                         } else {
7664                                                 use_spacer = NO_SPACER;
7665                                         }
7666                                 }
7667                         } else {
7668                                 ERR("use_spacer should have an argument. Defaulting to right.");
7669                                 use_spacer = RIGHT_SPACER;
7670                         }
7671                 }
7672 #ifdef X11
7673 #ifdef XFT
7674                 CONF("use_xft") {
7675                         use_xft = string_to_bool(value);
7676                 }
7677                 CONF("font") {
7678                         if (value) {
7679                                 set_first_font(value);
7680                         }
7681                 }
7682                 CONF("xftalpha") {
7683                         if (value && font_count >= 0) {
7684                                 fonts[0].font_alpha = atof(value) * 65535.0;
7685                         }
7686                 }
7687                 CONF("xftfont") {
7688                         if (use_xft) {
7689 #else
7690                 CONF("use_xft") {
7691                         if (string_to_bool(value)) {
7692                                 ERR("Xft not enabled");
7693                         }
7694                 }
7695                 CONF("xftfont") {
7696                         /* xftfont silently ignored when no Xft */
7697                 }
7698                 CONF("xftalpha") {
7699                         /* xftalpha is silently ignored when no Xft */
7700                 }
7701                 CONF("font") {
7702 #endif
7703                         if (value) {
7704                                 set_first_font(value);
7705                         }
7706 #ifdef XFT
7707                         }
7708 #endif
7709                 }
7710                 CONF("gap_x") {
7711                         if (value) {
7712                                 gap_x = atoi(value);
7713                         } else {
7714                                 CONF_ERR;
7715                         }
7716                 }
7717                 CONF("gap_y") {
7718                         if (value) {
7719                                 gap_y = atoi(value);
7720                         } else {
7721                                 CONF_ERR;
7722                         }
7723                 }
7724 #endif /* X11 */
7725                 CONF("mail_spool") {
7726                         if (value) {
7727                                 char buffer[256];
7728
7729                                 variable_substitute(value, buffer, 256);
7730
7731                                 if (buffer[0] != '\0') {
7732                                         if (current_mail_spool) {
7733                                                 free(current_mail_spool);
7734                                         }
7735                                         current_mail_spool = strndup(buffer, text_buffer_size);
7736                                 }
7737                         } else {
7738                                 CONF_ERR;
7739                         }
7740                 }
7741 #ifdef X11
7742                 CONF("minimum_size") {
7743                         if (value) {
7744                                 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
7745                                                 != 2) {
7746                                         if (sscanf(value, "%d", &minimum_width) != 1) {
7747                                                 CONF_ERR;
7748                                         }
7749                                 }
7750                         } else {
7751                                 CONF_ERR;
7752                         }
7753                 }
7754                 CONF("maximum_width") {
7755                         if (value) {
7756                                 if (sscanf(value, "%d", &maximum_width) != 1) {
7757                                         CONF_ERR;
7758                                 }
7759                         } else {
7760                                 CONF_ERR;
7761                         }
7762                 }
7763 #endif /* X11 */
7764                 CONF("no_buffers") {
7765                         no_buffers = string_to_bool(value);
7766                 }
7767                 CONF("top_name_width") {
7768                         if (value) {
7769                                 if (sscanf(value, "%u", &top_name_width) != 1) {
7770                                         CONF_ERR;
7771                                 }
7772                         } else {
7773                                 CONF_ERR;
7774                         }
7775                         if (top_name_width >= max_user_text) {
7776                                 top_name_width = max_user_text - 1;
7777                         }
7778                 }
7779                 CONF("top_cpu_separate") {
7780                         cpu_separate = string_to_bool(value);
7781                 }
7782                 CONF("short_units") {
7783                         short_units = string_to_bool(value);
7784                 }
7785                 CONF("pad_percents") {
7786                         pad_percents = atoi(value);
7787                 }
7788 #ifdef X11
7789 #ifdef OWN_WINDOW
7790                 CONF("own_window") {
7791                         if (value) {
7792                                 own_window = string_to_bool(value);
7793                         }
7794                 }
7795                 CONF("own_window_class") {
7796                         if (value) {
7797                                 memset(window.class_name, 0, sizeof(window.class_name));
7798                                 strncpy(window.class_name, value,
7799                                                 sizeof(window.class_name) - 1);
7800                         }
7801                 }
7802                 CONF("own_window_title") {
7803                         if (value) {
7804                                 memset(window.title, 0, sizeof(window.title));
7805                                 strncpy(window.title, value, sizeof(window.title) - 1);
7806                         }
7807                 }
7808                 CONF("own_window_transparent") {
7809                         if (value) {
7810                                 set_transparent = string_to_bool(value);
7811                         }
7812                 }
7813                 CONF("own_window_hints") {
7814                         if (value) {
7815                                 char *p_hint, *p_save;
7816                                 char delim[] = ", ";
7817
7818                                 /* tokenize the value into individual hints */
7819                                 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
7820                                         do {
7821                                                 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
7822                                                 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
7823                                                         SET_HINT(window.hints, HINT_UNDECORATED);
7824                                                 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
7825                                                         SET_HINT(window.hints, HINT_BELOW);
7826                                                 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
7827                                                         SET_HINT(window.hints, HINT_ABOVE);
7828                                                 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
7829                                                         SET_HINT(window.hints, HINT_STICKY);
7830                                                 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
7831                                                         SET_HINT(window.hints, HINT_SKIP_TASKBAR);
7832                                                 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
7833                                                         SET_HINT(window.hints, HINT_SKIP_PAGER);
7834                                                 } else {
7835                                                         CONF_ERR;
7836                                                 }
7837
7838                                                 p_hint = strtok_r(NULL, delim, &p_save);
7839                                         } while (p_hint != NULL);
7840                                 }
7841                         } else {
7842                                 CONF_ERR;
7843                         }
7844                 }
7845                 CONF("own_window_type") {
7846                         if (value) {
7847                                 if (strncmp(value, "normal", 6) == EQUAL) {
7848                                         window.type = TYPE_NORMAL;
7849                                 } else if (strncmp(value, "desktop", 7) == EQUAL) {
7850                                         window.type = TYPE_DESKTOP;
7851                                 } else if (strncmp(value, "dock", 7) == EQUAL) {
7852                                         window.type = TYPE_DOCK;
7853                                         text_alignment = TOP_LEFT;
7854                                 } else if (strncmp(value, "override", 8) == EQUAL) {
7855                                         window.type = TYPE_OVERRIDE;
7856                                 } else {
7857                                         CONF_ERR;
7858                                 }
7859                         } else {
7860                                 CONF_ERR;
7861                         }
7862                 }
7863 #endif
7864                 CONF("stippled_borders") {
7865                         if (value) {
7866                                 stippled_borders = strtol(value, 0, 0);
7867                         } else {
7868                                 stippled_borders = 4;
7869                         }
7870                 }
7871 #ifdef IMLIB2
7872                 CONF("imlib_cache_size") {
7873                         if (value) {
7874                                 cimlib_set_cache_size(atoi(value));
7875                         }
7876                 }
7877 #endif /* IMLIB2 */
7878 #endif /* X11 */
7879                 CONF("update_interval") {
7880                         if (value) {
7881                                 update_interval = strtod(value, 0);
7882                         } else {
7883                                 CONF_ERR;
7884                         }
7885                         if (info.music_player_interval == 0) {
7886                                 // default to update_interval
7887                                 info.music_player_interval = update_interval;
7888                         }
7889                 }
7890                 CONF("total_run_times") {
7891                         if (value) {
7892                                 total_run_times = strtod(value, 0);
7893                         } else {
7894                                 CONF_ERR;
7895                         }
7896                 }
7897                 CONF("uppercase") {
7898                         stuff_in_upper_case = string_to_bool(value);
7899                 }
7900                 CONF("max_specials") {
7901                         if (value) {
7902                                 max_specials = atoi(value);
7903                         } else {
7904                                 CONF_ERR;
7905                         }
7906                 }
7907                 CONF("max_user_text") {
7908                         if (value) {
7909                                 max_user_text = atoi(value);
7910                         } else {
7911                                 CONF_ERR;
7912                         }
7913                 }
7914                 CONF("text_buffer_size") {
7915                         if (value) {
7916                                 text_buffer_size = atoi(value);
7917                                 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
7918                                         ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
7919                                         text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
7920                                 }
7921                         } else {
7922                                 CONF_ERR;
7923                         }
7924                 }
7925                 CONF("text") {
7926 #ifdef X11
7927                         if(output_methods & TO_X) {
7928                                 X11_initialisation();
7929                         }
7930 #endif
7931
7932                         if (global_text) {
7933                                 free(global_text);
7934                                 global_text = 0;
7935                         }
7936
7937                         global_text = (char *) malloc(1);
7938                         global_text[0] = '\0';
7939
7940                         while (!feof(fp)) {
7941                                 unsigned int l = strlen(global_text);
7942                                 unsigned int bl;
7943                                 char buf[CONF_BUFF_SIZE];
7944
7945                                 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7946                                         break;
7947                                 }
7948
7949                                 /* Remove \\-\n. */
7950                                 bl = strlen(buf);
7951                                 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
7952                                         buf[bl-2] = '\0';
7953                                         bl -= 2;
7954                                         if (bl == 0) {
7955                                                 continue;
7956                                         }
7957                                 }
7958
7959                                 /* Check for continuation of \\-\n. */
7960                                 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
7961                                         global_text[l-1] = '\0';
7962                                         continue;
7963                                 }
7964
7965                                 global_text = (char *) realloc(global_text, l + bl + 1);
7966                                 strcat(global_text, buf);
7967
7968                                 if (strlen(global_text) > max_user_text) {
7969                                         break;
7970                                 }
7971                         }
7972                         fclose(fp);
7973                         if (strlen(global_text) < 1) {
7974                                 CRIT_ERR("no text supplied in configuration; exiting");
7975                         }
7976                         global_text_lines = line + 1;
7977                         return;
7978                 }
7979 #ifdef TCP_PORT_MONITOR
7980                 CONF("max_port_monitor_connections") {
7981                         int max;
7982                         if (!value || (sscanf(value, "%d", &max) != 1)) {
7983                                 /* an error. use default, warn and continue. */
7984                                 tcp_portmon_set_max_connections(0);
7985                                 CONF_ERR;
7986                         } else if (tcp_portmon_set_max_connections(max)) {
7987                                 /* max is < 0, default has been set*/
7988                                 CONF_ERR;
7989                         }
7990                 }
7991 #endif
7992                 CONF("if_up_strictness") {
7993                         if (!value) {
7994                                 ERR("incorrect if_up_strictness value, defaulting to 'up'");
7995                                 ifup_strictness = IFUP_UP;
7996                         } else if (strcasecmp(value, "up") == EQUAL) {
7997                                 ifup_strictness = IFUP_UP;
7998                         } else if (strcasecmp(value, "link") == EQUAL) {
7999                                 ifup_strictness = IFUP_LINK;
8000                         } else if (strcasecmp(value, "address") == EQUAL) {
8001                                 ifup_strictness = IFUP_ADDR;
8002                         } else {
8003                                 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8004                                 ifup_strictness = IFUP_UP;
8005                         }
8006                 }
8007
8008                 CONF("temperature_unit") {
8009                         if (!value) {
8010                                 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8011                         } else if (set_temp_output_unit(value)) {
8012                                 ERR("temperature_unit: incorrect argument");
8013                         }
8014                 }
8015
8016                 CONF("alias") {
8017                         if (value) {
8018                                 size_t maxlength = strlen(value);       //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8019                                 char *skey = malloc(maxlength);
8020                                 char *svalue = malloc(maxlength);
8021                                 char *oldvalue;
8022                                 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8023                                         oldvalue = getenv(skey);
8024                                         if (oldvalue == NULL) {
8025                                                 setenv(skey, svalue, 0);
8026                                         }
8027                                         //PS: Don't free oldvalue, it's the real envvar, not a copy
8028                                 } else {
8029                                         CONF_ERR;
8030                                 }
8031                                 free(skey);
8032                                 free(svalue);
8033                         } else {
8034                                 CONF_ERR;
8035                         }
8036                 }
8037 #ifdef HAVE_LUA
8038                 CONF("lua_load") {
8039                         llua_init();
8040                         if(value) {
8041                                 char *ptr = strtok(value, " ");
8042                                 while(ptr) {
8043                                         llua_load(ptr);
8044                                         ptr = strtok(NULL, " ");
8045                                 }
8046                         } else {
8047                                 CONF_ERR;
8048                         }
8049                 }
8050 #endif /* HAVE_LUA */
8051
8052                 CONF("color0"){}
8053                 CONF("color1"){}
8054                 CONF("color2"){}
8055                 CONF("color3"){}
8056                 CONF("color4"){}
8057                 CONF("color5"){}
8058                 CONF("color6"){}
8059                 CONF("color7"){}
8060                 CONF("color8"){}
8061                 CONF("color9"){}
8062                 CONF("default_color"){}
8063                 CONF3("default_shade_color", "default_shadecolor"){}
8064                 CONF3("default_outline_color", "default_outlinecolor") {}
8065                 CONF("own_window_colour") {}
8066
8067                 else {
8068                         ERR("%s: %d: no such configuration: '%s'", f, line, name);
8069                 }
8070         }
8071
8072         fclose(fp);
8073
8074         if (info.music_player_interval == 0) {
8075                 // default to update_interval
8076                 info.music_player_interval = update_interval;
8077         }
8078         if (!global_text) { // didn't supply any text
8079                 CRIT_ERR("missing text block in configuration; exiting");
8080         }
8081 }
8082
8083 static void load_config_file_x11(const char *f)
8084 {
8085         int line = 0;
8086         FILE *fp;
8087
8088         fp = open_config_file(f);
8089         if (!fp) {
8090                 return;
8091         }
8092         DBGP("reading contents from config file '%s'", f);
8093
8094         while (!feof(fp)) {
8095                 char buff[CONF_BUFF_SIZE], *name, *value;
8096                 int ret = do_config_step(&line, fp, buff, &name, &value);
8097                 if (ret == CONF_BREAK) {
8098                         break;
8099                 } else if (ret == CONF_CONTINUE) {
8100                         continue;
8101                 }
8102
8103 #ifdef X11
8104                 CONF2("color0") {
8105                         X11_initialisation();
8106                         if (x_initialised == YES) {
8107                                 if (value) {
8108                                         color0 = get_x11_color(value);
8109                                 } else {
8110                                         CONF_ERR;
8111                                 }
8112                         }
8113                 }
8114                 CONF("color1") {
8115                         X11_initialisation();
8116                         if (x_initialised == YES) {
8117                                 if (value) {
8118                                         color1 = get_x11_color(value);
8119                                 } else {
8120                                         CONF_ERR;
8121                                 }
8122                         }
8123                 }
8124                 CONF("color2") {
8125                         X11_initialisation();
8126                         if (x_initialised == YES) {
8127                                 if (value) {
8128                                         color2 = get_x11_color(value);
8129                                 } else {
8130                                         CONF_ERR;
8131                                 }
8132                         }
8133                 }
8134                 CONF("color3") {
8135                         X11_initialisation();
8136                         if (x_initialised == YES) {
8137                                 if (value) {
8138                                         color3 = get_x11_color(value);
8139                                 } else {
8140                                         CONF_ERR;
8141                                 }
8142                         }
8143                 }
8144                 CONF("color4") {
8145                         X11_initialisation();
8146                         if (x_initialised == YES) {
8147                                 if (value) {
8148                                         color4 = get_x11_color(value);
8149                                 } else {
8150                                         CONF_ERR;
8151                                 }
8152                         }
8153                 }
8154                 CONF("color5") {
8155                         X11_initialisation();
8156                         if (x_initialised == YES) {
8157                                 if (value) {
8158                                         color5 = get_x11_color(value);
8159                                 } else {
8160                                         CONF_ERR;
8161                                 }
8162                         }
8163                 }
8164                 CONF("color6") {
8165                         X11_initialisation();
8166                         if (x_initialised == YES) {
8167                                 if (value) {
8168                                         color6 = get_x11_color(value);
8169                                 } else {
8170                                         CONF_ERR;
8171                                 }
8172                         }
8173                 }
8174                 CONF("color7") {
8175                         X11_initialisation();
8176                         if (x_initialised == YES) {
8177                                 if (value) {
8178                                         color7 = get_x11_color(value);
8179                                 } else {
8180                                         CONF_ERR;
8181                                 }
8182                         }
8183                 }
8184                 CONF("color8") {
8185                         X11_initialisation();
8186                         if (x_initialised == YES) {
8187                                 if (value) {
8188                                         color8 = get_x11_color(value);
8189                                 } else {
8190                                         CONF_ERR;
8191                                 }
8192                         }
8193                 }
8194                 CONF("color9") {
8195                         X11_initialisation();
8196                         if (x_initialised == YES) {
8197                                 if (value) {
8198                                         color9 = get_x11_color(value);
8199                                 } else {
8200                                         CONF_ERR;
8201                                 }
8202                         }
8203                 }
8204                 CONF("default_color") {
8205                         X11_initialisation();
8206                         if (x_initialised == YES) {
8207                                 if (value) {
8208                                         default_fg_color = get_x11_color(value);
8209                                 } else {
8210                                         CONF_ERR;
8211                                 }
8212                         }
8213                 }
8214                 CONF3("default_shade_color", "default_shadecolor") {
8215                         X11_initialisation();
8216                         if (x_initialised == YES) {
8217                                 if (value) {
8218                                         default_bg_color = get_x11_color(value);
8219                                 } else {
8220                                         CONF_ERR;
8221                                 }
8222                         }
8223                 }
8224                 CONF3("default_outline_color", "default_outlinecolor") {
8225                         X11_initialisation();
8226                         if (x_initialised == YES) {
8227                                 if (value) {
8228                                         default_out_color = get_x11_color(value);
8229                                 } else {
8230                                         CONF_ERR;
8231                                 }
8232                         }
8233                 }
8234 #ifdef OWN_WINDOW
8235                 CONF("own_window_colour") {
8236                         X11_initialisation();
8237                         if (x_initialised == YES) {
8238                                 if (value) {
8239                                         background_colour = get_x11_color(value);
8240                                 } else {
8241                                         ERR("Invalid colour for own_window_colour (try omitting the "
8242                                                 "'#' for hex colours");
8243                                 }
8244                         }
8245                 }
8246 #endif
8247                 CONF("text") {
8248                         //initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod)
8249                         if(output_methods & TO_X) {
8250                                 X11_initialisation();
8251                         }
8252                 }
8253 #endif /* X11 */
8254 #undef CONF
8255 #undef CONF2
8256 #undef CONF3
8257 #undef CONF_ERR
8258 #undef CONF_ERR2
8259 #undef CONF_BREAK
8260 #undef CONF_CONTINUE
8261 #undef CONF_BUFF_SIZE
8262         }
8263
8264         fclose(fp);
8265
8266 }
8267
8268 static void print_help(const char *prog_name) {
8269         printf("Usage: %s [OPTION]...\n"
8270                         PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8271                         "window. Command line options will override configurations defined in config\n"
8272                         "file.\n"
8273                         "   -v, --version             version\n"
8274                         "   -q, --quiet               quiet mode\n"
8275                         "   -D, --debug               increase debugging output\n"
8276                         "   -c, --config=FILE         config file to load\n"
8277 #ifdef CONFIG_OUTPUT
8278                         "   -C, --print-config        print the builtin default config to stdout\n"
8279                         "                             e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
8280 #endif
8281                         "   -d, --daemonize           daemonize, fork to background\n"
8282                         "   -h, --help                help\n"
8283 #ifdef X11
8284                         "   -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8285                         "   -f, --font=FONT           font to use\n"
8286                         "   -X, --display=DISPLAY     X11 display to use\n"
8287 #ifdef OWN_WINDOW
8288                         "   -o, --own-window          create own window to draw\n"
8289 #endif
8290 #ifdef HAVE_XDBE
8291                         "   -b, --double-buffer       double buffer (prevents flickering)\n"
8292 #endif
8293                         "   -w, --window-id=WIN_ID    window id to draw\n"
8294                         "   -x X                      x position\n"
8295                         "   -y Y                      y position\n"
8296 #endif /* X11 */
8297                         "   -t, --text=TEXT           text to render, remember single quotes, like -t '$uptime'\n"
8298                         "   -u, --interval=SECS       update interval\n"
8299                         "   -i COUNT                  number of times to update "PACKAGE_NAME" (and quit)\n",
8300                         prog_name
8301         );
8302 }
8303
8304 /* : means that character before that takes an argument */
8305 static const char *getopt_string = "vVqdDt:u:i:hc:"
8306 #ifdef X11
8307         "x:y:w:a:f:X:"
8308 #ifdef OWN_WINDOW
8309         "o"
8310 #endif
8311 #ifdef HAVE_XDBE
8312         "b"
8313 #endif
8314 #endif /* X11 */
8315 #ifdef CONFIG_OUTPUT
8316         "C"
8317 #endif
8318         ;
8319
8320 static const struct option longopts[] = {
8321         { "help", 0, NULL, 'h' },
8322         { "version", 0, NULL, 'V' },
8323         { "debug", 0, NULL, 'D' },
8324         { "config", 1, NULL, 'c' },
8325 #ifdef CONFIG_OUTPUT
8326         { "print-config", 0, NULL, 'C' },
8327 #endif
8328         { "daemonize", 0, NULL, 'd' },
8329 #ifdef X11
8330         { "alignment", 1, NULL, 'a' },
8331         { "font", 1, NULL, 'f' },
8332         { "display", 1, NULL, 'X' },
8333 #ifdef OWN_WINDOW
8334         { "own-window", 0, NULL, 'o' },
8335 #endif
8336 #ifdef HAVE_XDBE
8337         { "double-buffer", 0, NULL, 'b' },
8338 #endif
8339         { "window-id", 1, NULL, 'w' },
8340 #endif /* X11 */
8341         { "text", 1, NULL, 't' },
8342         { "interval", 0, NULL, 'u' },
8343         { 0, 0, 0, 0 }
8344 };
8345
8346 int main(int argc, char **argv)
8347 {
8348 #ifdef X11
8349         char *s, *temp;
8350         unsigned int x;
8351 #endif
8352         struct sigaction act, oact;
8353
8354         g_signal_pending = 0;
8355         memset(&info, 0, sizeof(info));
8356         clear_net_stats();
8357
8358 #ifdef TCP_PORT_MONITOR
8359         /* set default connection limit */
8360         tcp_portmon_set_max_connections(0);
8361 #endif
8362
8363         /* handle command line parameters that don't change configs */
8364 #ifdef X11
8365         if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
8366                         || ((s = getenv("LANG")) && *s)) {
8367                 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
8368                 if (temp == NULL) {
8369                         ERR("malloc failed");
8370                 }
8371                 for (x = 0; x < strlen(s); x++) {
8372                         temp[x] = tolower(s[x]);
8373                 }
8374                 temp[x] = 0;
8375                 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
8376                         utf8_mode = 1;
8377                 }
8378
8379                 free(temp);
8380         }
8381         if (!setlocale(LC_CTYPE, "")) {
8382                 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
8383         }
8384 #endif /* X11 */
8385         while (1) {
8386                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8387
8388                 if (c == -1) {
8389                         break;
8390                 }
8391
8392                 switch (c) {
8393                         case 'v':
8394                         case 'V':
8395                                 print_version();
8396                         case 'c':
8397                                 if (current_config) {
8398                                         free(current_config);
8399                                 }
8400                                 current_config = strndup(optarg, max_user_text);
8401                                 break;
8402                         case 'q':
8403                                 freopen("/dev/null", "w", stderr);
8404                                 break;
8405                         case 'h':
8406                                 print_help(argv[0]);
8407                                 return 0;
8408 #ifdef CONFIG_OUTPUT
8409                         case 'C':
8410                                 print_defconfig();
8411                                 return 0;
8412 #endif
8413 #ifdef X11
8414                         case 'w':
8415                                 window.window = strtol(optarg, 0, 0);
8416                                 break;
8417 #endif /* X11 */
8418
8419                         case '?':
8420                                 exit(EXIT_FAILURE);
8421                 }
8422         }
8423
8424         /* check if specified config file is valid */
8425         if (current_config) {
8426                 struct stat sb;
8427                 if (stat(current_config, &sb) ||
8428                                 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
8429                         ERR("invalid configuration file '%s'\n", current_config);
8430                         free(current_config);
8431                         current_config = 0;
8432                 }
8433         }
8434
8435         /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
8436
8437         if (!current_config) {
8438                 /* load default config file */
8439                 char buf[DEFAULT_TEXT_BUFFER_SIZE];
8440                 FILE *fp;
8441
8442                 /* Try to use personal config file first */
8443                 to_real_path(buf, CONFIG_FILE);
8444                 if (buf[0] && (fp = fopen(buf, "r"))) {
8445                         current_config = strndup(buf, max_user_text);
8446                         fclose(fp);
8447                 }
8448
8449                 /* Try to use system config file if personal config not readable */
8450                 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
8451                         current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
8452                         fclose(fp);
8453                 }
8454
8455                 /* No readable config found */
8456                 if (!current_config) {
8457 #ifdef CONFIG_OUTPUT
8458                         current_config = strdup("==builtin==");
8459                         ERR("no readable personal or system-wide config file found,"
8460                                         " using builtin default");
8461 #else
8462                         CRIT_ERR("no readable personal or system-wide config file found");
8463 #endif /* ! CONF_OUTPUT */
8464                 }
8465         }
8466 #ifdef HAVE_SYS_INOTIFY_H
8467         inotify_fd = inotify_init();
8468 #endif /* HAVE_SYS_INOTIFY_H */
8469
8470         load_config_file(current_config);
8471
8472         /* init specials array */
8473         if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
8474                 ERR("failed to create specials array");
8475         }
8476
8477 #ifdef MAIL_FILE
8478         if (current_mail_spool == NULL) {
8479                 char buf[256];
8480
8481                 variable_substitute(MAIL_FILE, buf, 256);
8482
8483                 if (buf[0] != '\0') {
8484                         current_mail_spool = strndup(buf, text_buffer_size);
8485                 }
8486         }
8487 #endif
8488
8489         /* handle other command line arguments */
8490
8491 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
8492                 || defined(__NetBSD__)
8493         optind = optreset = 1;
8494 #else
8495         optind = 0;
8496 #endif
8497
8498 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8499         if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
8500                         "kvm_open")) == NULL) {
8501                 CRIT_ERR("cannot read kvm");
8502         }
8503 #endif
8504
8505         while (1) {
8506                 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
8507
8508                 if (c == -1) {
8509                         break;
8510                 }
8511
8512                 switch (c) {
8513                         case 'd':
8514                                 fork_to_background = 1;
8515                                 break;
8516                         case 'D':
8517                                 global_debug_level++;
8518                                 break;
8519 #ifdef X11
8520                         case 'f':
8521                                 set_first_font(optarg);
8522                                 break;
8523                         case 'a':
8524                                 text_alignment = string_to_alignment(optarg);
8525                                 break;
8526                         case 'X':
8527                                 if (disp)
8528                                         free(disp);
8529                                 disp = strdup(optarg);
8530                                 break;
8531
8532 #ifdef OWN_WINDOW
8533                         case 'o':
8534                                 own_window = 1;
8535                                 break;
8536 #endif
8537 #ifdef HAVE_XDBE
8538                         case 'b':
8539                                 use_xdbe = 1;
8540                                 break;
8541 #endif
8542 #endif /* X11 */
8543                         case 't':
8544                                 if (global_text) {
8545                                         free(global_text);
8546                                         global_text = 0;
8547                                 }
8548                                 global_text = strndup(optarg, max_user_text);
8549                                 convert_escapes(global_text);
8550                                 break;
8551
8552                         case 'u':
8553                                 update_interval = strtod(optarg, 0);
8554                                 if (info.music_player_interval == 0) {
8555                                         // default to update_interval
8556                                         info.music_player_interval = update_interval;
8557                                 }
8558                                 break;
8559
8560                         case 'i':
8561                                 total_run_times = strtod(optarg, 0);
8562                                 break;
8563 #ifdef X11
8564                         case 'x':
8565                                 gap_x = atoi(optarg);
8566                                 break;
8567
8568                         case 'y':
8569                                 gap_y = atoi(optarg);
8570                                 break;
8571 #endif /* X11 */
8572
8573                         case '?':
8574                                 exit(EXIT_FAILURE);
8575                 }
8576         }
8577
8578 #ifdef X11
8579         /* load font */
8580         if (output_methods & TO_X) {
8581                 load_config_file_x11(current_config);
8582                 load_fonts();
8583         }
8584 #endif /* X11 */
8585
8586         /* generate text and get initial size */
8587         extract_variable_text(global_text);
8588         if (global_text) {
8589                 free(global_text);
8590                 global_text = 0;
8591         }
8592         global_text = NULL;
8593         /* fork */
8594         if (fork_to_background) {
8595                 int pid = fork();
8596
8597                 switch (pid) {
8598                         case -1:
8599                                 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
8600                                         strerror(errno));
8601                                 break;
8602
8603                         case 0:
8604                                 /* child process */
8605                                 usleep(25000);
8606                                 fprintf(stderr, "\n");
8607                                 fflush(stderr);
8608                                 break;
8609
8610                         default:
8611                                 /* parent process */
8612                                 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
8613                                         pid);
8614                                 fflush(stderr);
8615                                 return 0;
8616                 }
8617         }
8618
8619         text_buffer = malloc(max_user_text);
8620         memset(text_buffer, 0, max_user_text);
8621         tmpstring1 = malloc(text_buffer_size);
8622         memset(tmpstring1, 0, text_buffer_size);
8623         tmpstring2 = malloc(text_buffer_size);
8624         memset(tmpstring2, 0, text_buffer_size);
8625
8626 #ifdef X11
8627         xargc = argc;
8628         xargv = argv;
8629         X11_create_window();
8630 #endif /* X11 */
8631
8632         /* Set signal handlers */
8633         act.sa_handler = signal_handler;
8634         sigemptyset(&act.sa_mask);
8635         act.sa_flags = 0;
8636 #ifdef SA_RESTART
8637         act.sa_flags |= SA_RESTART;
8638 #endif
8639
8640         if (            sigaction(SIGINT,  &act, &oact) < 0
8641                         ||      sigaction(SIGUSR1, &act, &oact) < 0
8642                         ||      sigaction(SIGHUP,  &act, &oact) < 0
8643                         ||      sigaction(SIGTERM, &act, &oact) < 0) {
8644                 ERR("error setting signal handler: %s", strerror(errno));
8645         }
8646
8647         main_loop();
8648
8649 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
8650         kvm_close(kd);
8651 #endif
8652
8653         return 0;
8654 }
8655
8656 static void signal_handler(int sig)
8657 {
8658         /* signal handler is light as a feather, as it should be.
8659          * we will poll g_signal_pending with each loop of conky
8660          * and do any signal processing there, NOT here. */
8661         g_signal_pending = sig;
8662 }