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