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