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