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