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