Support for $pid_state
[monky] / src / core.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 /* local headers */
32 #include "core.h"
33 #include "text_object.h"
34 #include "algebra.h"
35 #include "build.h"
36 #include "colours.h"
37 #include "combine.h"
38 #include "diskio.h"
39 #include "entropy.h"
40 #include "exec.h"
41 #include "proc.h"
42 #ifdef X11
43 #include "fonts.h"
44 #endif
45 #include "fs.h"
46 #ifdef HAVE_ICONV
47 #include "iconv_tools.h"
48 #endif
49 #include "logging.h"
50 #include "mixer.h"
51 #include "mail.h"
52 #include "mboxscan.h"
53 #include "net_stat.h"
54 #ifdef NVIDIA
55 #include "nvidia.h"
56 #endif
57 #include "read_tcp.h"
58 #include "scroll.h"
59 #include "specials.h"
60 #include "temphelper.h"
61 #include "template.h"
62 #include "tailhead.h"
63 #include "timeinfo.h"
64 #include "top.h"
65
66 #ifdef NCURSES
67 #include <ncurses.h>
68 #endif
69
70 /* check for OS and include appropriate headers */
71 #if defined(__linux__)
72 #include "linux.h"
73 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
74 #include "freebsd.h"
75 #elif defined(__OpenBSD__)
76 #include "openbsd.h"
77 #endif
78
79 #include <string.h>
80 #include <ctype.h>
81
82 /* strip a leading /dev/ if any, following symlinks first
83  *
84  * BEWARE: this function returns a pointer to static content
85  *         which gets overwritten in consecutive calls. I.e.:
86  *         this function is NOT reentrant.
87  */
88 const char *dev_name(const char *path)
89 {
90         static char buf[255];   /* should be enough for pathnames */
91         ssize_t buflen;
92
93         if (!path)
94                 return NULL;
95
96 #define DEV_NAME(x) \
97   x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
98         if ((buflen = readlink(path, buf, 254)) == -1)
99                 return DEV_NAME(path);
100         buf[buflen] = '\0';
101         return DEV_NAME(buf);
102 #undef DEV_NAME
103 }
104
105 static struct text_object *new_text_object_internal(void)
106 {
107         struct text_object *obj = malloc(sizeof(struct text_object));
108         memset(obj, 0, sizeof(struct text_object));
109         return obj;
110 }
111
112 static struct text_object *create_plain_text(const char *s)
113 {
114         struct text_object *obj;
115
116         if (s == NULL || *s == '\0') {
117                 return NULL;
118         }
119
120         obj = new_text_object_internal();
121
122         obj->type = OBJ_text;
123         obj->data.s = strndup(s, text_buffer_size);
124         return obj;
125 }
126
127 /* construct_text_object() creates a new text_object */
128 struct text_object *construct_text_object(const char *s, const char *arg, long
129                 line, void **ifblock_opaque, void *free_at_crash)
130 {
131         // struct text_object *obj = new_text_object();
132         struct text_object *obj = new_text_object_internal();
133
134         obj->line = line;
135
136 /* helper defines for internal use only */
137 #define __OBJ_HEAD(a, n) if (!strcmp(s, #a)) { \
138         obj->type = OBJ_##a; add_update_callback(n);
139 #define __OBJ_IF obj_be_ifblock_if(ifblock_opaque, obj)
140 #define __OBJ_ARG(...) if (!arg) { CRIT_ERR(obj, free_at_crash, __VA_ARGS__); }
141
142 /* defines to be used below */
143 #define OBJ(a, n) __OBJ_HEAD(a, n) {
144 #define OBJ_ARG(a, n, ...) __OBJ_HEAD(a, n) __OBJ_ARG(__VA_ARGS__) {
145 #define OBJ_IF(a, n) __OBJ_HEAD(a, n) __OBJ_IF; {
146 #define OBJ_IF_ARG(a, n, ...) __OBJ_HEAD(a, n) __OBJ_ARG(__VA_ARGS__) __OBJ_IF; {
147 #define END } } else
148
149 #ifdef X11
150         if (s[0] == '#') {
151                 obj->type = OBJ_color;
152                 obj->data.l = get_x11_color(s);
153         } else
154 #endif /* X11 */
155 #ifdef __OpenBSD__
156         OBJ(freq, 0)
157 #else
158         OBJ(acpitemp, 0)
159                 obj->data.i = open_acpi_temperature(arg);
160         END OBJ(acpiacadapter, 0)
161         END OBJ(freq, 0)
162 #endif /* !__OpenBSD__ */
163                 get_cpu_count();
164                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
165                                 || atoi(&arg[0]) > info.cpu_count) {
166                         obj->data.i = 1;
167                         /* NORM_ERR("freq: Invalid CPU number or you don't have that many CPUs! "
168                                 "Displaying the clock for CPU 1."); */
169                 } else {
170                         obj->data.i = atoi(&arg[0]);
171                 }
172         END OBJ(freq_g, 0)
173                 get_cpu_count();
174                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
175                                 || atoi(&arg[0]) > info.cpu_count) {
176                         obj->data.i = 1;
177                         /* NORM_ERR("freq_g: Invalid CPU number or you don't have that many "
178                                 "CPUs! Displaying the clock for CPU 1."); */
179                 } else {
180                         obj->data.i = atoi(&arg[0]);
181                 }
182         END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)")
183                 parse_read_tcp_arg(obj, arg, free_at_crash);
184 #if defined(__linux__)
185         END OBJ(voltage_mv, 0)
186                 get_cpu_count();
187                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
188                                 || atoi(&arg[0]) > info.cpu_count) {
189                         obj->data.i = 1;
190                         /* NORM_ERR("voltage_mv: Invalid CPU number or you don't have that many "
191                                 "CPUs! Displaying voltage for CPU 1."); */
192                 } else {
193                         obj->data.i = atoi(&arg[0]);
194                 }
195         END OBJ(voltage_v, 0)
196                 get_cpu_count();
197                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
198                                 || atoi(&arg[0]) > info.cpu_count) {
199                         obj->data.i = 1;
200                         /* NORM_ERR("voltage_v: Invalid CPU number or you don't have that many "
201                                 "CPUs! Displaying voltage for CPU 1."); */
202                 } else {
203                         obj->data.i = atoi(&arg[0]);
204                 }
205
206 #ifdef HAVE_IWLIB
207         END OBJ(wireless_essid, &update_net_stats)
208                 parse_net_stat_arg(obj, arg, free_at_crash);
209         END OBJ(wireless_mode, &update_net_stats)
210                 parse_net_stat_arg(obj, arg, free_at_crash);
211         END OBJ(wireless_bitrate, &update_net_stats)
212                 parse_net_stat_arg(obj, arg, free_at_crash);
213         END OBJ(wireless_ap, &update_net_stats)
214                 parse_net_stat_arg(obj, arg, free_at_crash);
215         END OBJ(wireless_link_qual, &update_net_stats)
216                 parse_net_stat_arg(obj, arg, free_at_crash);
217         END OBJ(wireless_link_qual_max, &update_net_stats)
218                 parse_net_stat_arg(obj, arg, free_at_crash);
219         END OBJ(wireless_link_qual_perc, &update_net_stats)
220                 parse_net_stat_arg(obj, arg, free_at_crash);
221         END OBJ(wireless_link_bar, &update_net_stats)
222                 parse_net_stat_bar_arg(obj, arg, free_at_crash);
223 #endif /* HAVE_IWLIB */
224
225 #endif /* __linux__ */
226
227 #ifndef __OpenBSD__
228         END OBJ(acpifan, 0)
229         END OBJ(battery, 0)
230                 char bat[64];
231
232                 if (arg) {
233                         sscanf(arg, "%63s", bat);
234                 } else {
235                         strcpy(bat, "BAT0");
236                 }
237                 obj->data.s = strndup(bat, text_buffer_size);
238         END OBJ(battery_short, 0)
239                 char bat[64];
240
241                 if (arg) {
242                         sscanf(arg, "%63s", bat);
243                 } else {
244                         strcpy(bat, "BAT0");
245                 }
246                 obj->data.s = strndup(bat, text_buffer_size);
247         END OBJ(battery_time, 0)
248                 char bat[64];
249
250                 if (arg) {
251                         sscanf(arg, "%63s", bat);
252                 } else {
253                         strcpy(bat, "BAT0");
254                 }
255                 obj->data.s = strndup(bat, text_buffer_size);
256         END OBJ(battery_percent, 0)
257                 char bat[64];
258
259                 if (arg) {
260                         sscanf(arg, "%63s", bat);
261                 } else {
262                         strcpy(bat, "BAT0");
263                 }
264                 obj->data.s = strndup(bat, text_buffer_size);
265         END OBJ(battery_bar, 0)
266                 char bat[64];
267                 if (arg) {
268                         arg = scan_bar(obj, arg);
269                         sscanf(arg, "%63s", bat);
270                 } else {
271                         strcpy(bat, "BAT0");
272                 }
273                 obj->data.s = strndup(bat, text_buffer_size);
274 #endif /* !__OpenBSD__ */
275
276 #if defined(__linux__)
277         END OBJ_ARG(disk_protect, 0, "disk_protect needs an argument")
278                 obj->data.s = strndup(dev_name(arg), text_buffer_size);
279         END OBJ(i8k_version, &update_i8k)
280         END OBJ(i8k_bios, &update_i8k)
281         END OBJ(i8k_serial, &update_i8k)
282         END OBJ(i8k_cpu_temp, &update_i8k)
283         END OBJ(i8k_left_fan_status, &update_i8k)
284         END OBJ(i8k_right_fan_status, &update_i8k)
285         END OBJ(i8k_left_fan_rpm, &update_i8k)
286         END OBJ(i8k_right_fan_rpm, &update_i8k)
287         END OBJ(i8k_ac_status, &update_i8k)
288         END OBJ(i8k_buttons_status, &update_i8k)
289 #if defined(IBM)
290         END OBJ(ibm_fan, 0)
291         END OBJ_ARG(ibm_temps, &get_ibm_acpi_temps, "ibm_temps: needs an argument")
292                 parse_ibm_temps_arg(obj, arg);
293         END OBJ(ibm_volume, 0)
294         END OBJ(ibm_brightness, 0)
295 #endif
296         /* information from sony_laptop kernel module
297          * /sys/devices/platform/sony-laptop */
298         END OBJ(sony_fanspeed, 0)
299         END OBJ_IF(if_gw, &update_gateway_info)
300         END OBJ_ARG(ioscheduler, 0, "get_ioscheduler needs an argument (e.g. hda)")
301                 obj->data.s = strndup(dev_name(arg), text_buffer_size);
302         END OBJ(laptop_mode, 0)
303         END OBJ_ARG(pb_battery, 0, "pb_battery: needs one argument: status, percent or time")
304                 if (strcmp(arg, "status") == EQUAL) {
305                         obj->data.i = PB_BATT_STATUS;
306                 } else if (strcmp(arg, "percent") == EQUAL) {
307                         obj->data.i = PB_BATT_PERCENT;
308                 } else if (strcmp(arg, "time") == EQUAL) {
309                         obj->data.i = PB_BATT_TIME;
310                 } else {
311                         NORM_ERR("pb_battery: illegal argument '%s', defaulting to status", arg);
312                         obj->data.i = PB_BATT_STATUS;
313                 }
314 #endif /* __linux__ */
315 #if (defined(__FreeBSD__) || defined(__linux__))
316         END OBJ_IF_ARG(if_up, 0, "if_up needs an argument")
317                 parse_if_up_arg(obj, arg);
318 #endif
319 #if defined(__OpenBSD__)
320         END OBJ_ARG(obsd_sensors_temp, 0, "obsd_sensors_temp: needs an argument")
321                 parse_obsd_sensor(obj, arg);
322         END OBJ_ARG(obsd_sensors_fan, 0, "obsd_sensors_fan: needs 2 arguments (device and sensor number)")
323                 parse_obsd_sensor(obj, arg);
324         END OBJ_ARG(obsd_sensors_volt, 0, "obsd_sensors_volt: needs 2 arguments (device and sensor number)")
325                 parse_obsd_sensor(obj, arg);
326         END OBJ(obsd_vendor, 0)
327         END OBJ(obsd_product, 0)
328 #endif /* __OpenBSD__ */
329         END OBJ(buffers, &update_meminfo)
330         END OBJ(cached, &update_meminfo)
331 #define SCAN_CPU(__arg, __var) { \
332         int __offset = 0; \
333         if (__arg && sscanf(__arg, " cpu%d %n", &__var, &__offset) > 0) \
334                 __arg += __offset; \
335         else \
336                 __var = 0; \
337 }
338         END OBJ(cpu, &update_cpu_usage)
339                 SCAN_CPU(arg, obj->data.i);
340                 DBGP2("Adding $cpu for CPU %d", obj->data.i);
341 #ifdef X11
342         END OBJ(cpugauge, &update_cpu_usage)
343                 SCAN_CPU(arg, obj->data.i);
344                 scan_gauge(obj, arg);
345                 DBGP2("Adding $cpugauge for CPU %d", obj->data.i);
346 #endif /* X11 */
347         END OBJ(cpubar, &update_cpu_usage)
348                 SCAN_CPU(arg, obj->data.i);
349                 scan_bar(obj, arg);
350                 DBGP2("Adding $cpubar for CPU %d", obj->data.i);
351 #ifdef X11
352         END OBJ(cpugraph, &update_cpu_usage)
353                 char *buf = 0;
354                 SCAN_CPU(arg, obj->data.i);
355                 buf = scan_graph(obj, arg, 100);
356                 DBGP2("Adding $cpugraph for CPU %d", obj->data.i);
357                 if (buf) free(buf);
358         END OBJ(loadgraph, &update_load_average)
359                 scan_loadgraph_arg(obj, arg);
360 #endif /* X11 */
361         END OBJ(diskio, &update_diskio)
362                 parse_diskio_arg(obj, arg);
363         END OBJ(diskio_read, &update_diskio)
364                 parse_diskio_arg(obj, arg);
365         END OBJ(diskio_write, &update_diskio)
366                 parse_diskio_arg(obj, arg);
367 #ifdef X11
368         END OBJ(diskiograph, &update_diskio)
369                 parse_diskiograph_arg(obj, arg);
370         END OBJ(diskiograph_read, &update_diskio)
371                 parse_diskiograph_arg(obj, arg);
372         END OBJ(diskiograph_write, &update_diskio)
373                 parse_diskiograph_arg(obj, arg);
374 #endif /* X11 */
375         END OBJ(color, 0)
376 #ifdef X11
377                 if (output_methods & TO_X) {
378                         obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
379                         set_current_text_color(obj->data.l);
380                 }
381 #endif /* X11 */
382 #ifdef NCURSES
383                 if (output_methods & TO_NCURSES) {
384                         obj->data.l = COLOR_WHITE;
385                         if(arg) {
386                                 if(strcasecmp(arg, "red") == 0) {
387                                         obj->data.l = COLOR_RED;
388                                 }else if(strcasecmp(arg, "green") == 0) {
389                                         obj->data.l = COLOR_GREEN;
390                                 }else if(strcasecmp(arg, "yellow") == 0) {
391                                         obj->data.l = COLOR_YELLOW;
392                                 }else if(strcasecmp(arg, "blue") == 0) {
393                                         obj->data.l = COLOR_BLUE;
394                                 }else if(strcasecmp(arg, "magenta") == 0) {
395                                         obj->data.l = COLOR_MAGENTA;
396                                 }else if(strcasecmp(arg, "cyan") == 0) {
397                                         obj->data.l = COLOR_CYAN;
398                                 }else if(strcasecmp(arg, "black") == 0) {
399                                         obj->data.l = COLOR_BLACK;
400                                 }
401                         }
402                         set_current_text_color(obj->data.l);
403                         init_pair(obj->data.l, obj->data.l, COLOR_BLACK);
404                 }
405 #endif /* NCURSES */
406         END OBJ(color0, 0)
407                 obj->data.l = color0;
408                 set_current_text_color(obj->data.l);
409         END OBJ(color1, 0)
410                 obj->data.l = color1;
411                 set_current_text_color(obj->data.l);
412         END OBJ(color2, 0)
413                 obj->data.l = color2;
414                 set_current_text_color(obj->data.l);
415         END OBJ(color3, 0)
416                 obj->data.l = color3;
417                 set_current_text_color(obj->data.l);
418         END OBJ(color4, 0)
419                 obj->data.l = color4;
420                 set_current_text_color(obj->data.l);
421         END OBJ(color5, 0)
422                 obj->data.l = color5;
423                 set_current_text_color(obj->data.l);
424         END OBJ(color6, 0)
425                 obj->data.l = color6;
426                 set_current_text_color(obj->data.l);
427         END OBJ(color7, 0)
428                 obj->data.l = color7;
429                 set_current_text_color(obj->data.l);
430         END OBJ(color8, 0)
431                 obj->data.l = color8;
432                 set_current_text_color(obj->data.l);
433         END OBJ(color9, 0)
434                 obj->data.l = color9;
435                 set_current_text_color(obj->data.l);
436 #ifdef X11
437         END OBJ(font, 0)
438                 obj->data.s = scan_font(arg);
439 #endif /* X11 */
440         END OBJ(conky_version, 0)
441         END OBJ(conky_build_date, 0)
442         END OBJ(conky_build_arch, 0)
443         END OBJ(downspeed, &update_net_stats)
444                 parse_net_stat_arg(obj, arg, free_at_crash);
445         END OBJ(downspeedf, &update_net_stats)
446                 parse_net_stat_arg(obj, arg, free_at_crash);
447 #ifdef X11
448         END OBJ(downspeedgraph, &update_net_stats)
449                 parse_net_stat_graph_arg(obj, arg, free_at_crash);
450 #endif /* X11 */
451         END OBJ(else, 0)
452                 obj_be_ifblock_else(ifblock_opaque, obj);
453         END OBJ(endif, 0)
454                 obj_be_ifblock_endif(ifblock_opaque, obj);
455         END OBJ(eval, 0)
456                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
457 #if defined(IMLIB2) && defined(X11)
458         END OBJ(image, 0)
459                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
460 #endif /* IMLIB2 */
461         END OBJ(exec, 0)
462                 scan_exec_arg(obj, arg);
463         END OBJ(execp, 0)
464                 scan_exec_arg(obj, arg);
465         END OBJ(execbar, 0)
466                 scan_exec_arg(obj, arg);
467 #ifdef X11
468         END OBJ(execgauge, 0)
469                 scan_exec_arg(obj, arg);
470         END OBJ(execgraph, 0)
471                 scan_execgraph_arg(obj, arg);
472 #endif /* X11 */
473         END OBJ_ARG(execibar, 0, "execibar needs arguments")
474                 scan_execi_arg(obj, arg);
475 #ifdef X11
476         END OBJ_ARG(execigraph, 0, "execigraph needs arguments")
477                 scan_execgraph_arg(obj, arg);
478         END OBJ_ARG(execigauge, 0, "execigauge needs arguments")
479                 scan_execi_arg(obj, arg);
480 #endif /* X11 */
481         END OBJ_ARG(execi, 0, "execi needs arguments")
482                 scan_execi_arg(obj, arg);
483         END OBJ_ARG(execpi, 0, "execpi needs arguments")
484                 scan_execi_arg(obj, arg);
485         END OBJ_ARG(texeci, 0, "texeci needs arguments")
486                 scan_execi_arg(obj, arg);
487         END OBJ(pre_exec, 0)
488                 scan_pre_exec_arg(obj, arg);
489         END OBJ(fs_bar, &update_fs_stats)
490                 init_fs_bar(obj, arg);
491         END OBJ(fs_bar_free, &update_fs_stats)
492                 init_fs_bar(obj, arg);
493         END OBJ(fs_free, &update_fs_stats)
494                 init_fs(obj, arg);
495         END OBJ(fs_used_perc, &update_fs_stats)
496                 init_fs(obj, arg);
497         END OBJ(fs_free_perc, &update_fs_stats)
498                 init_fs(obj, arg);
499         END OBJ(fs_size, &update_fs_stats)
500                 init_fs(obj, arg);
501         END OBJ(fs_type, &update_fs_stats)
502                 init_fs(obj, arg);
503         END OBJ(fs_used, &update_fs_stats)
504                 init_fs(obj, arg);
505         END OBJ(hr, 0)
506                 obj->data.i = arg ? atoi(arg) : 1;
507         END OBJ(nameserver, &update_dns_data)
508                 parse_nameserver_arg(obj, arg);
509         END OBJ(offset, 0)
510                 obj->data.i = arg ? atoi(arg) : 1;
511         END OBJ(voffset, 0)
512                 obj->data.i = arg ? atoi(arg) : 1;
513         END OBJ_ARG(goto, 0, "goto needs arguments")
514                 obj->data.i = atoi(arg);
515 #ifdef X11
516         END OBJ(tab, 0)
517                 scan_tab(obj, arg);
518 #endif /* X11 */
519 #ifdef __linux__
520         END OBJ_ARG(i2c, 0, "i2c needs arguments")
521                 parse_i2c_sensor(obj, arg);
522         END OBJ_ARG(platform, 0, "platform needs arguments")
523                 parse_platform_sensor(obj, arg);
524         END OBJ_ARG(hwmon, 0, "hwmon needs argumanets")
525                 parse_hwmon_sensor(obj, arg);
526 #endif /* __linux__ */
527
528         END
529         /* we have four different types of top (top, top_mem, top_time and top_io). To
530          * avoid having almost-same code four times, we have this special
531          * handler. */
532         if (strncmp(s, "top", 3) == EQUAL) {
533                 add_update_callback(&update_meminfo);
534                 add_update_callback(&update_top);
535                 if (!parse_top_args(s, arg, obj)) {
536                         return NULL;
537                 }
538         } else OBJ(addr, &update_net_stats)
539                 parse_net_stat_arg(obj, arg, free_at_crash);
540 #if defined(__linux__)
541         END OBJ(addrs, &update_net_stats)
542                 parse_net_stat_arg(obj, arg, free_at_crash);
543 #endif /* __linux__ */
544         END OBJ_ARG(tail, 0, "tail needs arguments")
545                 init_tailhead("tail", arg, obj, free_at_crash);
546         END OBJ_ARG(head, 0, "head needs arguments")
547                 init_tailhead("head", arg, obj, free_at_crash);
548         END OBJ_ARG(lines, 0, "lines needs an argument")
549                 obj->data.s = strndup(arg, text_buffer_size);
550         END OBJ_ARG(words, 0, "words needs a argument")
551                 obj->data.s = strndup(arg, text_buffer_size);
552         END OBJ(loadavg, &update_load_average)
553                 scan_loadavg_arg(obj, arg);
554         END OBJ_IF_ARG(if_empty, 0, "if_empty needs an argument")
555                 obj->sub = malloc(sizeof(struct text_object));
556                 extract_variable_text_internal(obj->sub, arg);
557         END OBJ_IF_ARG(if_match, 0, "if_match needs arguments")
558                 obj->sub = malloc(sizeof(struct text_object));
559                 extract_variable_text_internal(obj->sub, arg);
560         END OBJ_IF_ARG(if_existing, 0, "if_existing needs an argument or two")
561                 obj->data.s = strndup(arg, text_buffer_size);
562         END OBJ_IF_ARG(if_mounted, 0, "if_mounted needs an argument")
563                 obj->data.s = strndup(arg, text_buffer_size);
564 #ifdef __linux__
565         END OBJ_IF_ARG(if_running, &update_top, "if_running needs an argument")
566                 top_running = 1;
567                 obj->data.s = strndup(arg, text_buffer_size);
568 #else
569         END OBJ_IF_ARG(if_running, 0, "if_running needs an argument")
570                 char buf[256];
571
572                 snprintf(buf, 256, "pidof %s >/dev/null", arg);
573                 obj->data.s = strndup(buf, text_buffer_size);
574 #endif
575         END OBJ(kernel, 0)
576         END OBJ(machine, 0)
577         END OBJ(mails, 0)
578                 parse_local_mail_args(obj, arg);
579         END OBJ(new_mails, 0)
580                 parse_local_mail_args(obj, arg);
581         END OBJ(seen_mails, 0)
582                 parse_local_mail_args(obj, arg);
583         END OBJ(unseen_mails, 0)
584                 parse_local_mail_args(obj, arg);
585         END OBJ(flagged_mails, 0)
586                 parse_local_mail_args(obj, arg);
587         END OBJ(unflagged_mails, 0)
588                 parse_local_mail_args(obj, arg);
589         END OBJ(forwarded_mails, 0)
590                 parse_local_mail_args(obj, arg);
591         END OBJ(unforwarded_mails, 0)
592                 parse_local_mail_args(obj, arg);
593         END OBJ(replied_mails, 0)
594                 parse_local_mail_args(obj, arg);
595         END OBJ(unreplied_mails, 0)
596                 parse_local_mail_args(obj, arg);
597         END OBJ(draft_mails, 0)
598                 parse_local_mail_args(obj, arg);
599         END OBJ(trashed_mails, 0)
600                 parse_local_mail_args(obj, arg);
601         END OBJ(mboxscan, 0)
602                 parse_mboxscan_arg(obj, arg);
603         END OBJ(mem, &update_meminfo)
604         END OBJ(memeasyfree, &update_meminfo)
605         END OBJ(memfree, &update_meminfo)
606         END OBJ(memmax, &update_meminfo)
607         END OBJ(memperc, &update_meminfo)
608 #ifdef X11
609         END OBJ(memgauge, &update_meminfo)
610                 scan_gauge(obj, arg);
611 #endif /* X11*/
612         END OBJ(membar, &update_meminfo)
613                 scan_bar(obj, arg);
614 #ifdef X11
615         END OBJ(memgraph, &update_meminfo)
616                 char *buf = 0;
617                 buf = scan_graph(obj, arg, 100);
618
619                 if (buf) free(buf);
620 #endif /* X11*/
621         END OBJ(mixer, 0)
622                 parse_mixer_arg(obj, arg);
623         END OBJ(mixerl, 0)
624                 parse_mixer_arg(obj, arg);
625         END OBJ(mixerr, 0)
626                 parse_mixer_arg(obj, arg);
627 #ifdef X11
628         END OBJ(mixerbar, 0)
629                 scan_mixer_bar(obj, arg);
630         END OBJ(mixerlbar, 0)
631                 scan_mixer_bar(obj, arg);
632         END OBJ(mixerrbar, 0)
633                 scan_mixer_bar(obj, arg);
634 #endif
635         END OBJ_IF(if_mixer_mute, 0)
636                 parse_mixer_arg(obj, arg);
637 #ifdef X11
638         END OBJ(monitor, &update_x11info)
639         END OBJ(monitor_number, &update_x11info)
640         END OBJ(desktop, &update_x11info)
641         END OBJ(desktop_number, &update_x11info)
642         END OBJ(desktop_name, &update_x11info)
643 #endif
644         END OBJ(nodename, 0)
645         END OBJ_ARG(pid_chroot, 0, "pid_chroot needs a pid as argument")
646                 scan_pid_chroot_arg(obj, arg, free_at_crash);
647         END OBJ_ARG(pid_cmdline, 0, "pid_cmdline needs a pid as argument")
648                 scan_pid_cmdline_arg(obj, arg, free_at_crash);
649         END OBJ_ARG(pid_cwd, 0, "pid_cwd needs a pid as argument")
650                 scan_pid_cwd_arg(obj, arg, free_at_crash);
651         END OBJ_ARG(pid_environ, 0, "pid_environ needs arguments")
652                 scan_pid_environ_arg(obj, arg, free_at_crash);
653         END OBJ_ARG(pid_environ_list, 0, "pid_environ_list needs a pid as argument")
654                 scan_pid_environ_list_arg(obj, arg, free_at_crash);
655         END OBJ_ARG(pid_exe, 0, "pid_exe needs a pid as argument")
656                 scan_pid_exe_arg(obj, arg, free_at_crash);
657         END OBJ_ARG(pid_openfiles, 0, "pid_openfiles needs a pid as argument")
658                 scan_pid_openfiles_arg(obj, arg, free_at_crash);
659         END OBJ_ARG(pid_state, 0, "pid_state needs a pid as argument")
660                 scan_pid_state_arg(obj, arg, free_at_crash);
661         END OBJ_ARG(pid_stderr, 0, "pid_stderr needs a pid as argument")
662                 scan_pid_stderr_arg(obj, arg, free_at_crash);
663         END OBJ_ARG(pid_stdin, 0, "pid_stdin needs a pid as argument")
664                 scan_pid_stdin_arg(obj, arg, free_at_crash);
665         END OBJ_ARG(pid_stdout, 0, "pid_stdout needs a pid as argument")
666                 scan_pid_stdout_arg(obj, arg, free_at_crash);
667         END OBJ(processes, &update_total_processes)
668         END OBJ(running_processes, &update_running_processes)
669         END OBJ(threads, &update_threads)
670         END OBJ(shadecolor, 0)
671 #ifdef X11
672                 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
673 #endif /* X11 */
674         END OBJ(outlinecolor, 0)
675 #ifdef X11
676                 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
677 #endif /* X11 */
678         END OBJ(stippled_hr, 0)
679 #ifdef X11
680                 scan_stippled_hr(obj, arg);
681 #endif /* X11 */
682         END OBJ(swap, &update_meminfo)
683         END OBJ(swapfree, &update_meminfo)
684         END OBJ(swapmax, &update_meminfo)
685         END OBJ(swapperc, &update_meminfo)
686         END OBJ(swapbar, &update_meminfo)
687                 scan_bar(obj, arg);
688         END OBJ(sysname, 0)
689         END OBJ(time, 0)
690                 scan_time(obj, arg);
691         END OBJ(utime, 0)
692                 scan_time(obj, arg);
693         END OBJ(tztime, 0)
694                 scan_tztime(obj, arg);
695 #ifdef HAVE_ICONV
696         END OBJ_ARG(iconv_start, 0, "Iconv requires arguments")
697                 init_iconv_start(obj, free_at_crash, arg);
698         END OBJ(iconv_stop, 0)
699                 init_iconv_stop();
700 #endif
701         END OBJ(totaldown, &update_net_stats)
702                 parse_net_stat_arg(obj, arg, free_at_crash);
703         END OBJ(totalup, &update_net_stats)
704                 parse_net_stat_arg(obj, arg, free_at_crash);
705         END OBJ(updates, 0)
706         END OBJ_IF(if_updatenr, 0)
707                 obj->data.i = arg ? atoi(arg) : 0;
708                 if(obj->data.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
709                 set_updatereset(obj->data.i > get_updatereset() ? obj->data.i : get_updatereset());
710         END OBJ(alignr, 0)
711                 obj->data.i = arg ? atoi(arg) : 0;
712         END OBJ(alignc, 0)
713                 obj->data.i = arg ? atoi(arg) : 0;
714         END OBJ(upspeed, &update_net_stats)
715                 parse_net_stat_arg(obj, arg, free_at_crash);
716         END OBJ(upspeedf, &update_net_stats)
717                 parse_net_stat_arg(obj, arg, free_at_crash);
718 #ifdef X11
719         END OBJ(upspeedgraph, &update_net_stats)
720                 parse_net_stat_graph_arg(obj, arg, free_at_crash);
721 #endif
722         END OBJ(uptime_short, &update_uptime)
723         END OBJ(uptime, &update_uptime)
724         END OBJ(user_names, &update_users)
725         END OBJ(user_times, &update_users)
726         END OBJ(conky_user_time, &update_users)
727         END OBJ(user_terms, &update_users)
728         END OBJ(user_number, &update_users)
729 #if defined(__linux__)
730         END OBJ(gw_iface, &update_gateway_info)
731         END OBJ(gw_ip, &update_gateway_info)
732 #endif /* !__linux__ */
733 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
734                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
735         END OBJ(apm_adapter, 0)
736         END OBJ(apm_battery_life, 0)
737         END OBJ(apm_battery_time, 0)
738 #endif /* __FreeBSD__ */
739         END OBJ(imap_unseen, 0)
740                 parse_imap_mail_args(obj, arg);
741         END OBJ(imap_messages, 0)
742                 parse_imap_mail_args(obj, arg);
743         END OBJ(pop3_unseen, 0)
744                 parse_pop3_mail_args(obj, arg);
745         END OBJ(pop3_used, 0)
746                 parse_pop3_mail_args(obj, arg);
747 #ifdef IBM
748         END OBJ_ARG(smapi, 0, "smapi needs an argument")
749                 obj->data.s = strndup(arg, text_buffer_size);
750         END OBJ_IF_ARG(if_smapi_bat_installed, 0, "if_smapi_bat_installed needs an argument")
751                 obj->data.s = strndup(arg, text_buffer_size);
752         END OBJ_ARG(smapi_bat_perc, 0, "smapi_bat_perc needs an argument")
753                 obj->data.s = strndup(arg, text_buffer_size);
754         END OBJ_ARG(smapi_bat_temp, 0, "smapi_bat_temp needs an argument")
755                 obj->data.s = strndup(arg, text_buffer_size);
756         END OBJ_ARG(smapi_bat_power, 0, "smapi_bat_power needs an argument")
757                 obj->data.s = strndup(arg, text_buffer_size);
758 #ifdef X11
759         END OBJ_ARG(smapi_bat_bar, 0, "smapi_bat_bar needs an argument")
760                 int cnt;
761                 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
762                         NORM_ERR("first argument to smapi_bat_bar must be an integer value");
763                         obj->data.i = -1;
764                 } else
765                         arg = scan_bar(obj, arg + cnt);
766 #endif /* X11 */
767 #endif /* IBM */
768 #ifdef MPD
769 #define mpd_set_maxlen(name) \
770                 if (arg) { \
771                         int i; \
772                         sscanf(arg, "%d", &i); \
773                         if (i > 0) \
774                                 obj->data.i = i + 1; \
775                         else \
776                                 NORM_ERR(#name ": invalid length argument"); \
777                 }
778         END OBJ(mpd_artist, &update_mpd)
779                 mpd_set_maxlen(mpd_artist);
780                 init_mpd();
781         END OBJ(mpd_title, &update_mpd)
782                 mpd_set_maxlen(mpd_title);
783                 init_mpd();
784         END OBJ(mpd_random, &update_mpd) init_mpd();
785         END OBJ(mpd_repeat, &update_mpd) init_mpd();
786         END OBJ(mpd_elapsed, &update_mpd) init_mpd();
787         END OBJ(mpd_length, &update_mpd) init_mpd();
788         END OBJ(mpd_track, &update_mpd)
789                 mpd_set_maxlen(mpd_track);
790                 init_mpd();
791         END OBJ(mpd_name, &update_mpd)
792                 mpd_set_maxlen(mpd_name);
793                 init_mpd();
794         END OBJ(mpd_file, &update_mpd)
795                 mpd_set_maxlen(mpd_file);
796                 init_mpd();
797         END OBJ(mpd_percent, &update_mpd) init_mpd();
798         END OBJ(mpd_album, &update_mpd)
799                 mpd_set_maxlen(mpd_album);
800                 init_mpd();
801         END OBJ(mpd_vol, &update_mpd) init_mpd();
802         END OBJ(mpd_bitrate, &update_mpd) init_mpd();
803         END OBJ(mpd_status, &update_mpd) init_mpd();
804         END OBJ(mpd_bar, &update_mpd)
805                 scan_bar(obj, arg);
806                 init_mpd();
807         END OBJ(mpd_smart, &update_mpd)
808                 mpd_set_maxlen(mpd_smart);
809                 init_mpd();
810         END OBJ_IF(if_mpd_playing, &update_mpd)
811                 init_mpd();
812 #undef mpd_set_maxlen
813 #endif /* MPD */
814 #ifdef MOC
815         END OBJ(moc_state, &update_moc)
816         END OBJ(moc_file, &update_moc)
817         END OBJ(moc_title, &update_moc)
818         END OBJ(moc_artist, &update_moc)
819         END OBJ(moc_song, &update_moc)
820         END OBJ(moc_album, &update_moc)
821         END OBJ(moc_totaltime, &update_moc)
822         END OBJ(moc_timeleft, &update_moc)
823         END OBJ(moc_curtime, &update_moc)
824         END OBJ(moc_bitrate, &update_moc)
825         END OBJ(moc_rate, &update_moc)
826 #endif /* MOC */
827 #ifdef XMMS2
828         END OBJ(xmms2_artist, &update_xmms2)
829         END OBJ(xmms2_album, &update_xmms2)
830         END OBJ(xmms2_title, &update_xmms2)
831         END OBJ(xmms2_genre, &update_xmms2)
832         END OBJ(xmms2_comment, &update_xmms2)
833         END OBJ(xmms2_url, &update_xmms2)
834         END OBJ(xmms2_tracknr, &update_xmms2)
835         END OBJ(xmms2_bitrate, &update_xmms2)
836         END OBJ(xmms2_date, &update_xmms2)
837         END OBJ(xmms2_id, &update_xmms2)
838         END OBJ(xmms2_duration, &update_xmms2)
839         END OBJ(xmms2_elapsed, &update_xmms2)
840         END OBJ(xmms2_size, &update_xmms2)
841         END OBJ(xmms2_status, &update_xmms2)
842         END OBJ(xmms2_percent, &update_xmms2)
843 #ifdef X11
844         END OBJ(xmms2_bar, &update_xmms2)
845                 scan_bar(obj, arg);
846 #endif /* X11 */
847         END OBJ(xmms2_smart, &update_xmms2)
848         END OBJ(xmms2_playlist, &update_xmms2)
849         END OBJ(xmms2_timesplayed, &update_xmms2)
850         END OBJ_IF(if_xmms2_connected, &update_xmms2)
851 #endif
852 #ifdef AUDACIOUS
853         END OBJ(audacious_status, &update_audacious)
854         END OBJ_ARG(audacious_title, &update_audacious, "audacious_title needs an argument")
855                 sscanf(arg, "%d", &info.audacious.max_title_len);
856                 if (info.audacious.max_title_len > 0) {
857                         info.audacious.max_title_len++;
858                 } else {
859                         CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
860                 }
861         END OBJ(audacious_length, &update_audacious)
862         END OBJ(audacious_length_seconds, &update_audacious)
863         END OBJ(audacious_position, &update_audacious)
864         END OBJ(audacious_position_seconds, &update_audacious)
865         END OBJ(audacious_bitrate, &update_audacious)
866         END OBJ(audacious_frequency, &update_audacious)
867         END OBJ(audacious_channels, &update_audacious)
868         END OBJ(audacious_filename, &update_audacious)
869         END OBJ(audacious_playlist_length, &update_audacious)
870         END OBJ(audacious_playlist_position, &update_audacious)
871         END OBJ(audacious_main_volume, &update_audacious)
872 #ifdef X11
873         END OBJ(audacious_bar, &update_audacious)
874                 scan_bar(obj, arg);
875 #endif /* X11 */
876 #endif
877 #ifdef BMPX
878         END OBJ(bmpx_title, &update_bmpx)
879                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
880         END OBJ(bmpx_artist, &update_bmpx)
881                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
882         END OBJ(bmpx_album, &update_bmpx)
883                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
884         END OBJ(bmpx_track, &update_bmpx)
885                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
886         END OBJ(bmpx_uri, &update_bmpx)
887                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
888         END OBJ(bmpx_bitrate, &update_bmpx)
889                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
890 #endif
891 #ifdef EVE
892         END OBJ_ARG(eve, 0, "eve needs arguments: <userid> <apikey> <characterid>")
893                 scan_eve(obj, arg);
894 #endif
895 #ifdef HAVE_CURL
896         END OBJ_ARG(curl, 0, "curl needs arguments: <uri> <interval in minutes>")
897                 curl_parse_arg(obj, arg);
898 #endif
899 #ifdef RSS
900         END OBJ_ARG(rss, 0, "rss needs arguments: <uri> <interval in minutes> <action> [act_par] [spaces in front]")
901                 rss_scan_arg(obj, arg);
902 #endif
903 #ifdef WEATHER
904         END OBJ_ARG(weather, 0, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]")
905                 scan_weather_arg(obj, arg, free_at_crash);
906 #endif
907 #ifdef XOAP
908         END OBJ_ARG(weather_forecast, 0, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]")
909                 scan_weather_forecast_arg(obj, arg, free_at_crash);
910 #endif
911 #ifdef HAVE_LUA
912         END OBJ_ARG(lua, 0, "lua needs arguments: <function name> [function parameters]")
913                 obj->data.s = strndup(arg, text_buffer_size);
914         END OBJ_ARG(lua_parse, 0, "lua_parse needs arguments: <function name> [function parameters]")
915                 obj->data.s = strndup(arg, text_buffer_size);
916         END OBJ_ARG(lua_bar, 0, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]")
917                 arg = scan_bar(obj, arg);
918                 if(arg) {
919                         obj->data.s = strndup(arg, text_buffer_size);
920                 } else {
921                         CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
922                 }
923 #ifdef X11
924         END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]")
925                 char *buf = 0;
926                 buf = scan_graph(obj, arg, 0);
927                 if (buf) {
928                         obj->data.s = buf;
929                 } else {
930                         CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
931                 }
932         END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]")
933                 arg = scan_gauge(obj, arg);
934                 if (arg) {
935                         obj->data.s = strndup(arg, text_buffer_size);
936                 } else {
937                         CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
938                 }
939 #endif /* X11 */
940 #endif /* HAVE_LUA */
941 #ifdef HDDTEMP
942         END OBJ(hddtemp, &update_hddtemp)
943                 if (arg)
944                         obj->data.s = strndup(arg, text_buffer_size);
945 #endif /* HDDTEMP */
946 #ifdef TCP_PORT_MONITOR
947         END OBJ_ARG(tcp_portmon, &tcp_portmon_update, "tcp_portmon: needs arguments")
948                 tcp_portmon_init(obj, arg);
949 #endif /* TCP_PORT_MONITOR */
950         END OBJ(entropy_avail, &update_entropy)
951         END OBJ(entropy_perc, &update_entropy)
952         END OBJ(entropy_poolsize, &update_entropy)
953         END OBJ(entropy_bar, &update_entropy)
954                 scan_bar(obj, arg);
955         END OBJ_ARG(include, 0, "include needs a argument")
956                 struct conftree *leaf = conftree_add(currentconffile, arg);
957                 if(leaf) {
958                         if (load_config_file(arg) == TRUE) {
959                                 obj->sub = malloc(sizeof(struct text_object));
960                                 currentconffile = leaf;
961                                 extract_variable_text_internal(obj->sub, get_global_text());
962                                 currentconffile = leaf->back;
963                         } else {
964                                 NORM_ERR("Can't load configfile '%s'.", arg);
965                         }
966                 } else {
967                         NORM_ERR("You are trying to load '%s' recursively, I'm only going to load it once to prevent an infinite loop.", arg);
968                 }
969         END OBJ_ARG(blink, 0, "blink needs a argument")
970                 obj->sub = malloc(sizeof(struct text_object));
971                 extract_variable_text_internal(obj->sub, arg);
972         END OBJ_ARG(to_bytes, 0, "to_bytes needs a argument")
973                 obj->sub = malloc(sizeof(struct text_object));
974                 extract_variable_text_internal(obj->sub, arg);
975         END OBJ(scroll, 0)
976                 parse_scroll_arg(obj, arg, free_at_crash);
977         END OBJ_ARG(combine, 0, "combine needs arguments: <text1> <text2>")
978                 parse_combine_arg(obj, arg, free_at_crash);
979 #ifdef NVIDIA
980         END OBJ_ARG(nvidia, 0, "nvidia needs an argument")
981                 if (set_nvidia_type(obj, arg)) {
982                         CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
983                                  " specified: '%s'\n", arg);
984                 }
985 #endif /* NVIDIA */
986 #ifdef APCUPSD
987         END OBJ_ARG(apcupsd, &update_apcupsd, "apcupsd needs arguments: <host> <port>")
988                 char host[64];
989                 int port;
990                 if (sscanf(arg, "%63s %d", host, &port) != 2) {
991                         CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
992                 } else {
993                         info.apcupsd.port = htons(port);
994                         strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
995                 }
996         END OBJ(apcupsd_name, &update_apcupsd)
997         END OBJ(apcupsd_model, &update_apcupsd)
998         END OBJ(apcupsd_upsmode, &update_apcupsd)
999         END OBJ(apcupsd_cable, &update_apcupsd)
1000         END OBJ(apcupsd_status, &update_apcupsd)
1001         END OBJ(apcupsd_linev, &update_apcupsd)
1002         END OBJ(apcupsd_load, &update_apcupsd)
1003         END OBJ(apcupsd_loadbar, &update_apcupsd)
1004                 scan_bar(obj, arg);
1005 #ifdef X11
1006         END OBJ(apcupsd_loadgraph, &update_apcupsd)
1007                 char* buf = 0;
1008                 buf = scan_graph(obj, arg, 0);
1009                 if (buf) free(buf);
1010         END OBJ(apcupsd_loadgauge, &update_apcupsd)
1011                 scan_gauge(obj, arg);
1012 #endif /* X11 */
1013         END OBJ(apcupsd_charge, &update_apcupsd)
1014         END OBJ(apcupsd_timeleft, &update_apcupsd)
1015         END OBJ(apcupsd_temp, &update_apcupsd)
1016         END OBJ(apcupsd_lastxfer, &update_apcupsd)
1017 #endif /* APCUPSD */
1018         END {
1019                 char buf[256];
1020
1021                 NORM_ERR("unknown variable %s", s);
1022                 obj->type = OBJ_text;
1023                 snprintf(buf, 256, "${%s}", s);
1024                 obj->data.s = strndup(buf, text_buffer_size);
1025         }
1026 #undef OBJ
1027 #undef OBJ_IF
1028 #undef OBJ_ARG
1029 #undef OBJ_IF_ARG
1030 #undef __OBJ_HEAD
1031 #undef __OBJ_IF
1032 #undef __OBJ_ARG
1033 #undef END
1034
1035         return obj;
1036 }
1037
1038 /*
1039  * - assumes that *string is '#'
1040  * - removes the part from '#' to the end of line ('\n' or '\0')
1041  * - it removes the '\n'
1042  * - copies the last char into 'char *last' argument, which should be a pointer
1043  *   to a char rather than a string.
1044  */
1045 static size_t remove_comment(char *string, char *last)
1046 {
1047         char *end = string;
1048         while (*end != '\0' && *end != '\n') {
1049                 ++end;
1050         }
1051         if (last) *last = *end;
1052         if (*end == '\n') end++;
1053         strfold(string, end - string);
1054         return end - string;
1055 }
1056
1057 size_t remove_comments(char *string)
1058 {
1059         char *curplace;
1060         size_t folded = 0;
1061         for (curplace = string; *curplace != 0; curplace++) {
1062                 if (*curplace == '\\' && *(curplace + 1) == '#') {
1063                         // strcpy can't be used for overlapping strings
1064                         strfold(curplace, 1);
1065                         folded += 1;
1066                 } else if (*curplace == '#') {
1067                         folded += remove_comment(curplace, 0);
1068                 }
1069         }
1070         return folded;
1071 }
1072
1073 int extract_variable_text_internal(struct text_object *retval, const char *const_p)
1074 {
1075         struct text_object *obj;
1076         char *p, *s, *orig_p;
1077         long line;
1078         void *ifblock_opaque = NULL;
1079         char *tmp_p;
1080         char *arg = 0;
1081         size_t len = 0;
1082
1083         p = strndup(const_p, max_user_text - 1);
1084         while (text_contains_templates(p)) {
1085                 char *tmp;
1086                 tmp = find_and_replace_templates(p);
1087                 free(p);
1088                 p = tmp;
1089         }
1090         s = orig_p = p;
1091
1092         if (strcmp(p, const_p)) {
1093                 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
1094         } else {
1095                 DBGP("no templates to replace");
1096         }
1097
1098         memset(retval, 0, sizeof(struct text_object));
1099
1100         line = global_text_lines;
1101
1102         while (*p) {
1103                 if (*p == '\n') {
1104                         line++;
1105                 }
1106                 if (*p == '$') {
1107                         *p = '\0';
1108                         obj = create_plain_text(s);
1109                         if (obj != NULL) {
1110                                 append_object(retval, obj);
1111                         }
1112                         *p = '$';
1113                         p++;
1114                         s = p;
1115
1116                         if (*p != '$') {
1117                                 char buf[256];
1118                                 const char *var;
1119
1120                                 /* variable is either $foo or ${foo} */
1121                                 if (*p == '{') {
1122                                         unsigned int brl = 1, brr = 0;
1123
1124                                         p++;
1125                                         s = p;
1126                                         while (*p && brl != brr) {
1127                                                 if (*p == '{') {
1128                                                         brl++;
1129                                                 }
1130                                                 if (*p == '}') {
1131                                                         brr++;
1132                                                 }
1133                                                 p++;
1134                                         }
1135                                         p--;
1136                                 } else {
1137                                         s = p;
1138                                         if (*p == '#') {
1139                                                 p++;
1140                                         }
1141                                         while (*p && (isalnum((int) *p) || *p == '_')) {
1142                                                 p++;
1143                                         }
1144                                 }
1145
1146                                 /* copy variable to buffer */
1147                                 len = (p - s > 255) ? 255 : (p - s);
1148                                 strncpy(buf, s, len);
1149                                 buf[len] = '\0';
1150
1151                                 if (*p == '}') {
1152                                         p++;
1153                                 }
1154                                 s = p;
1155
1156                                 /* search for variable in environment */
1157
1158                                 var = getenv(buf);
1159                                 if (var) {
1160                                         obj = create_plain_text(var);
1161                                         if (obj) {
1162                                                 append_object(retval, obj);
1163                                         }
1164                                         continue;
1165                                 }
1166
1167                                 /* if variable wasn't found in environment, use some special */
1168
1169                                 arg = 0;
1170
1171                                 /* split arg */
1172                                 if (strchr(buf, ' ')) {
1173                                         arg = strchr(buf, ' ');
1174                                         *arg = '\0';
1175                                         arg++;
1176                                         while (isspace((int) *arg)) {
1177                                                 arg++;
1178                                         }
1179                                         if (!*arg) {
1180                                                 arg = 0;
1181                                         }
1182                                 }
1183
1184                                 /* lowercase variable name */
1185                                 tmp_p = buf;
1186                                 while (*tmp_p) {
1187                                         *tmp_p = tolower(*tmp_p);
1188                                         tmp_p++;
1189                                 }
1190
1191                                 obj = construct_text_object(buf, arg,
1192                                                 line, &ifblock_opaque, orig_p);
1193                                 if (obj != NULL) {
1194                                         append_object(retval, obj);
1195                                 }
1196                                 continue;
1197                         } else {
1198                                 obj = create_plain_text("$");
1199                                 s = p + 1;
1200                                 if (obj != NULL) {
1201                                         append_object(retval, obj);
1202                                 }
1203                         }
1204                 } else if (*p == '\\' && *(p+1) == '#') {
1205                         strfold(p, 1);
1206                 } else if (*p == '#') {
1207                         char c;
1208                         if (remove_comment(p, &c) && p > orig_p && c == '\n') {
1209                                 /* if remove_comment removed a newline, we need to 'back up' with p */
1210                                 p--;
1211                         }
1212                 }
1213                 p++;
1214         }
1215         obj = create_plain_text(s);
1216         if (obj != NULL) {
1217                 append_object(retval, obj);
1218         }
1219
1220         if (!ifblock_stack_empty(&ifblock_opaque)) {
1221                 NORM_ERR("one or more $endif's are missing");
1222         }
1223
1224         free(orig_p);
1225         return 0;
1226 }
1227
1228 /*
1229  * Frees the list of text objects root points to.  When internal = 1, it won't
1230  * free global objects.
1231  */
1232 void free_text_objects(struct text_object *root, int internal)
1233 {
1234         struct text_object *obj;
1235
1236         if (!root->prev) {
1237                 return;
1238         }
1239
1240 #define data obj->data
1241         for (obj = root->prev; obj; obj = root->prev) {
1242                 root->prev = obj->prev;
1243                 switch (obj->type) {
1244 #ifndef __OpenBSD__
1245                         case OBJ_acpitemp:
1246                                 close(data.i);
1247                                 break;
1248 #endif /* !__OpenBSD__ */
1249 #ifdef __linux__
1250                         case OBJ_i2c:
1251                         case OBJ_platform:
1252                         case OBJ_hwmon:
1253                                 free_sysfs_sensor(obj);
1254                                 break;
1255 #endif /* __linux__ */
1256                         case OBJ_pid_chroot:
1257                                 free(data.s);
1258                                 break;
1259                         case OBJ_pid_cmdline:
1260                                 free(data.s);
1261                                 break;
1262                         case OBJ_pid_cwd:
1263                                 free(data.s);
1264                                 break;
1265                         case OBJ_pid_environ:
1266                                 free_pid_environ(obj);
1267                                 break;
1268                         case OBJ_pid_environ_list:
1269                                 free(data.s);
1270                                 break;
1271                         case OBJ_pid_exe:
1272                                 free(data.s);
1273                                 break;
1274                         case OBJ_pid_openfiles:
1275                                 free(data.s);
1276                                 break;
1277                         case OBJ_pid_state:
1278                                 free(data.s);
1279                                 break;
1280                         case OBJ_pid_stderr:
1281                                 free(data.s);
1282                                 break;
1283                         case OBJ_pid_stdin:
1284                                 free(data.s);
1285                                 break;
1286                         case OBJ_pid_stdout:
1287                                 free(data.s);
1288                                 break;
1289                         case OBJ_read_tcp:
1290                                 free_read_tcp(obj);
1291                                 break;
1292                         case OBJ_time:
1293                         case OBJ_utime:
1294                                 free_time(obj);
1295                                 break;
1296                         case OBJ_tztime:
1297                                 free_tztime(obj);
1298                                 break;
1299                         case OBJ_mboxscan:
1300                                 free_mboxscan(obj);
1301                                 break;
1302                         case OBJ_mails:
1303                         case OBJ_new_mails:
1304                         case OBJ_seen_mails:
1305                         case OBJ_unseen_mails:
1306                         case OBJ_flagged_mails:
1307                         case OBJ_unflagged_mails:
1308                         case OBJ_forwarded_mails:
1309                         case OBJ_unforwarded_mails:
1310                         case OBJ_replied_mails:
1311                         case OBJ_unreplied_mails:
1312                         case OBJ_draft_mails:
1313                         case OBJ_trashed_mails:
1314                                 free_local_mails(obj);
1315                                 break;
1316                         case OBJ_imap_unseen:
1317                         case OBJ_imap_messages:
1318                         case OBJ_pop3_unseen:
1319                         case OBJ_pop3_used:
1320                                 free_mail_obj(obj);
1321                                 break;
1322                         case OBJ_if_empty:
1323                         case OBJ_if_match:
1324                                 free_text_objects(obj->sub, 1);
1325                                 free(obj->sub);
1326                                 break;
1327                         case OBJ_if_existing:
1328                         case OBJ_if_mounted:
1329                         case OBJ_if_running:
1330                                 free(data.s);
1331                                 break;
1332                         case OBJ_head:
1333                         case OBJ_tail:
1334                                 free_tailhead(obj);
1335                                 break;
1336                         case OBJ_text:
1337                         case OBJ_font:
1338                         case OBJ_image:
1339                         case OBJ_eval:
1340                                 free(data.s);
1341                                 break;
1342                         case OBJ_exec:
1343                         case OBJ_execbar:
1344 #ifdef X11
1345                         case OBJ_execgauge:
1346                         case OBJ_execgraph:
1347 #endif
1348                         case OBJ_execp:
1349                                 free_exec(obj);
1350                                 break;
1351 #ifdef HAVE_ICONV
1352                         case OBJ_iconv_start:
1353                                 free_iconv();
1354                                 break;
1355 #endif
1356 #ifdef __linux__
1357                         case OBJ_disk_protect:
1358                                 free(data.s);
1359                                 break;
1360                         case OBJ_if_gw:
1361                         case OBJ_gw_iface:
1362                         case OBJ_gw_ip:
1363                                 free_gateway_info();
1364                                 break;
1365                         case OBJ_ioscheduler:
1366                                 if(data.s)
1367                                         free(data.s);
1368                                 break;
1369 #endif
1370 #if (defined(__FreeBSD__) || defined(__linux__))
1371                         case OBJ_if_up:
1372                                 free_if_up(obj);
1373                                 break;
1374 #endif
1375 #ifdef XMMS2
1376                         case OBJ_xmms2_artist:
1377                                 if (info.xmms2.artist) {
1378                                         free(info.xmms2.artist);
1379                                         info.xmms2.artist = 0;
1380                                 }
1381                                 break;
1382                         case OBJ_xmms2_album:
1383                                 if (info.xmms2.album) {
1384                                         free(info.xmms2.album);
1385                                         info.xmms2.album = 0;
1386                                 }
1387                                 break;
1388                         case OBJ_xmms2_title:
1389                                 if (info.xmms2.title) {
1390                                         free(info.xmms2.title);
1391                                         info.xmms2.title = 0;
1392                                 }
1393                                 break;
1394                         case OBJ_xmms2_genre:
1395                                 if (info.xmms2.genre) {
1396                                         free(info.xmms2.genre);
1397                                         info.xmms2.genre = 0;
1398                                 }
1399                                 break;
1400                         case OBJ_xmms2_comment:
1401                                 if (info.xmms2.comment) {
1402                                         free(info.xmms2.comment);
1403                                         info.xmms2.comment = 0;
1404                                 }
1405                                 break;
1406                         case OBJ_xmms2_url:
1407                                 if (info.xmms2.url) {
1408                                         free(info.xmms2.url);
1409                                         info.xmms2.url = 0;
1410                                 }
1411                                 break;
1412                         case OBJ_xmms2_date:
1413                                 if (info.xmms2.date) {
1414                                         free(info.xmms2.date);
1415                                         info.xmms2.date = 0;
1416                                 }
1417                                 break;
1418                         case OBJ_xmms2_status:
1419                                 if (info.xmms2.status) {
1420                                         free(info.xmms2.status);
1421                                         info.xmms2.status = 0;
1422                                 }
1423                                 break;
1424                         case OBJ_xmms2_playlist:
1425                                 if (info.xmms2.playlist) {
1426                                         free(info.xmms2.playlist);
1427                                         info.xmms2.playlist = 0;
1428                                 }
1429                                 break;
1430                         case OBJ_xmms2_smart:
1431                                 if (info.xmms2.artist) {
1432                                         free(info.xmms2.artist);
1433                                         info.xmms2.artist = 0;
1434                                 }
1435                                 if (info.xmms2.title) {
1436                                         free(info.xmms2.title);
1437                                         info.xmms2.title = 0;
1438                                 }
1439                                 if (info.xmms2.url) {
1440                                         free(info.xmms2.url);
1441                                         info.xmms2.url = 0;
1442                                 }
1443                                 break;
1444 #endif
1445 #ifdef BMPX
1446                         case OBJ_bmpx_title:
1447                         case OBJ_bmpx_artist:
1448                         case OBJ_bmpx_album:
1449                         case OBJ_bmpx_track:
1450                         case OBJ_bmpx_uri:
1451                         case OBJ_bmpx_bitrate:
1452                                 break;
1453 #endif
1454 #ifdef EVE
1455                         case OBJ_eve:
1456                                 free_eve(obj);
1457                                 break;
1458 #endif
1459 #ifdef HAVE_CURL
1460                         case OBJ_curl:
1461                                 curl_obj_free(obj);
1462                                 break;
1463 #endif
1464 #ifdef RSS
1465                         case OBJ_rss:
1466                                 rss_free_obj_info(obj);
1467                                 break;
1468 #endif
1469 #ifdef WEATHER
1470                         case OBJ_weather:
1471                                 free_weather(obj);
1472                                 break;
1473 #endif
1474 #ifdef XOAP
1475                         case OBJ_weather_forecast:
1476                                 free_weather(obj);
1477                                 break;
1478 #endif
1479 #ifdef HAVE_LUA
1480                         case OBJ_lua:
1481                         case OBJ_lua_parse:
1482                         case OBJ_lua_bar:
1483 #ifdef X11
1484                         case OBJ_lua_graph:
1485                         case OBJ_lua_gauge:
1486 #endif /* X11 */
1487                                 free(data.s);
1488                                 break;
1489 #endif /* HAVE_LUA */
1490                         case OBJ_pre_exec:
1491                                 break;
1492 #ifndef __OpenBSD__
1493                         case OBJ_battery:
1494                                 free(data.s);
1495                                 break;
1496                         case OBJ_battery_short:
1497                                 free(data.s);
1498                                 break;
1499                         case OBJ_battery_time:
1500                                 free(data.s);
1501                                 break;
1502 #endif /* !__OpenBSD__ */
1503                         case OBJ_execpi:
1504                         case OBJ_execi:
1505                         case OBJ_execibar:
1506                         case OBJ_texeci:
1507 #ifdef X11
1508                         case OBJ_execigraph:
1509                         case OBJ_execigauge:
1510 #endif /* X11 */
1511                                 free_execi(obj);
1512                                 break;
1513                         case OBJ_nameserver:
1514                                 free_dns_data();
1515                                 break;
1516                         case OBJ_top:
1517                         case OBJ_top_mem:
1518                         case OBJ_top_time:
1519 #ifdef IOSTATS
1520                         case OBJ_top_io:
1521 #endif
1522                                 free_top(obj, internal);
1523                                 break;
1524 #ifdef HDDTEMP
1525                         case OBJ_hddtemp:
1526                                 if (data.s) {
1527                                         free(data.s);
1528                                         data.s = NULL;
1529                                 }
1530                                 free_hddtemp();
1531                                 break;
1532 #endif /* HDDTEMP */
1533                         case OBJ_entropy_avail:
1534                         case OBJ_entropy_perc:
1535                         case OBJ_entropy_poolsize:
1536                         case OBJ_entropy_bar:
1537                                 break;
1538                         case OBJ_user_names:
1539                                 if (info.users.names) {
1540                                         free(info.users.names);
1541                                         info.users.names = 0;
1542                                 }
1543                                 break;
1544                         case OBJ_user_terms:
1545                                 if (info.users.terms) {
1546                                         free(info.users.terms);
1547                                         info.users.terms = 0;
1548                                 }
1549                                 break;
1550                         case OBJ_user_times:
1551                                 if (info.users.times) {
1552                                         free(info.users.times);
1553                                         info.users.times = 0;
1554                                 }
1555                                 break;
1556                         case OBJ_conky_user_time:
1557                                 if (info.users.ctime) {
1558                                         free(info.users.ctime);
1559                                         info.users.ctime = 0;
1560                                 }
1561                                 break;
1562 #ifdef IBM
1563                         case OBJ_smapi:
1564                         case OBJ_smapi_bat_perc:
1565                         case OBJ_smapi_bat_temp:
1566                         case OBJ_smapi_bat_power:
1567                                 free(data.s);
1568                                 break;
1569                         case OBJ_if_smapi_bat_installed:
1570                                 free(data.s);
1571                                 break;
1572 #endif /* IBM */
1573 #ifdef NVIDIA
1574                         case OBJ_nvidia:
1575                                 free_nvidia(obj);
1576                                 break;
1577 #endif /* NVIDIA */
1578 #ifdef MPD
1579                         case OBJ_mpd_title:
1580                         case OBJ_mpd_artist:
1581                         case OBJ_mpd_album:
1582                         case OBJ_mpd_random:
1583                         case OBJ_mpd_repeat:
1584                         case OBJ_mpd_vol:
1585                         case OBJ_mpd_bitrate:
1586                         case OBJ_mpd_status:
1587                         case OBJ_mpd_bar:
1588                         case OBJ_mpd_elapsed:
1589                         case OBJ_mpd_length:
1590                         case OBJ_mpd_track:
1591                         case OBJ_mpd_name:
1592                         case OBJ_mpd_file:
1593                         case OBJ_mpd_percent:
1594                         case OBJ_mpd_smart:
1595                         case OBJ_if_mpd_playing:
1596                                 free_mpd();
1597                                 break;
1598 #endif /* MPD */
1599 #ifdef MOC
1600                         case OBJ_moc_state:
1601                         case OBJ_moc_file:
1602                         case OBJ_moc_title:
1603                         case OBJ_moc_artist:
1604                         case OBJ_moc_song:
1605                         case OBJ_moc_album:
1606                         case OBJ_moc_totaltime:
1607                         case OBJ_moc_timeleft:
1608                         case OBJ_moc_curtime:
1609                         case OBJ_moc_bitrate:
1610                         case OBJ_moc_rate:
1611                                 free_moc();
1612                                 break;
1613 #endif /* MOC */
1614                         case OBJ_include:
1615                         case OBJ_blink:
1616                         case OBJ_to_bytes:
1617                                 if(obj->sub) {
1618                                         free_text_objects(obj->sub, 1);
1619                                         free(obj->sub);
1620                                 }
1621                                 break;
1622                         case OBJ_scroll:
1623                                 free_scroll(obj);
1624                                 break;
1625                         case OBJ_combine:
1626                                 free_combine(obj);
1627                                 break;
1628 #ifdef APCUPSD
1629                         case OBJ_apcupsd:
1630                         case OBJ_apcupsd_name:
1631                         case OBJ_apcupsd_model:
1632                         case OBJ_apcupsd_upsmode:
1633                         case OBJ_apcupsd_cable:
1634                         case OBJ_apcupsd_status:
1635                         case OBJ_apcupsd_linev:
1636                         case OBJ_apcupsd_load:
1637                         case OBJ_apcupsd_loadbar:
1638 #ifdef X11
1639                         case OBJ_apcupsd_loadgraph:
1640                         case OBJ_apcupsd_loadgauge:
1641 #endif /* X11 */
1642                         case OBJ_apcupsd_charge:
1643                         case OBJ_apcupsd_timeleft:
1644                         case OBJ_apcupsd_temp:
1645                         case OBJ_apcupsd_lastxfer:
1646                                 break;
1647 #endif /* APCUPSD */
1648 #ifdef TCP_PORT_MONITOR
1649                         case OBJ_tcp_portmon:
1650                                 tcp_portmon_free(obj);
1651                                 break;
1652 #endif /* TCP_PORT_MONITOR */
1653 #ifdef X11
1654                         case OBJ_desktop:
1655                         case OBJ_desktop_number:
1656                         case OBJ_desktop_name:
1657                                 if(info.x11.desktop.name && !internal) {
1658                                   free(info.x11.desktop.name);
1659                                   info.x11.desktop.name = NULL;
1660                                 }
1661                                 if(info.x11.desktop.all_names && !internal) {
1662                                   free(info.x11.desktop.all_names);
1663                                   info.x11.desktop.all_names = NULL;
1664                                 }
1665                                 break;
1666 #endif /* X11 */
1667                 }
1668                 if(obj->special_data) free(obj->special_data);
1669                 free(obj);
1670         }
1671 #undef data
1672 }
1673