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