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