Added support for $pid_chroot
[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_stderr, 0, "pid_stderr needs a pid as argument")
660                 scan_pid_stderr_arg(obj, arg, free_at_crash);
661         END OBJ_ARG(pid_stdin, 0, "pid_stdin needs a pid as argument")
662                 scan_pid_stdin_arg(obj, arg, free_at_crash);
663         END OBJ_ARG(pid_stdout, 0, "pid_stdout needs a pid as argument")
664                 scan_pid_stdout_arg(obj, arg, free_at_crash);
665         END OBJ(processes, &update_total_processes)
666         END OBJ(running_processes, &update_running_processes)
667         END OBJ(threads, &update_threads)
668         END OBJ(shadecolor, 0)
669 #ifdef X11
670                 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
671 #endif /* X11 */
672         END OBJ(outlinecolor, 0)
673 #ifdef X11
674                 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
675 #endif /* X11 */
676         END OBJ(stippled_hr, 0)
677 #ifdef X11
678                 scan_stippled_hr(obj, arg);
679 #endif /* X11 */
680         END OBJ(swap, &update_meminfo)
681         END OBJ(swapfree, &update_meminfo)
682         END OBJ(swapmax, &update_meminfo)
683         END OBJ(swapperc, &update_meminfo)
684         END OBJ(swapbar, &update_meminfo)
685                 scan_bar(obj, arg);
686         END OBJ(sysname, 0)
687         END OBJ(time, 0)
688                 scan_time(obj, arg);
689         END OBJ(utime, 0)
690                 scan_time(obj, arg);
691         END OBJ(tztime, 0)
692                 scan_tztime(obj, arg);
693 #ifdef HAVE_ICONV
694         END OBJ_ARG(iconv_start, 0, "Iconv requires arguments")
695                 init_iconv_start(obj, free_at_crash, arg);
696         END OBJ(iconv_stop, 0)
697                 init_iconv_stop();
698 #endif
699         END OBJ(totaldown, &update_net_stats)
700                 parse_net_stat_arg(obj, arg, free_at_crash);
701         END OBJ(totalup, &update_net_stats)
702                 parse_net_stat_arg(obj, arg, free_at_crash);
703         END OBJ(updates, 0)
704         END OBJ_IF(if_updatenr, 0)
705                 obj->data.i = arg ? atoi(arg) : 0;
706                 if(obj->data.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
707                 set_updatereset(obj->data.i > get_updatereset() ? obj->data.i : get_updatereset());
708         END OBJ(alignr, 0)
709                 obj->data.i = arg ? atoi(arg) : 0;
710         END OBJ(alignc, 0)
711                 obj->data.i = arg ? atoi(arg) : 0;
712         END OBJ(upspeed, &update_net_stats)
713                 parse_net_stat_arg(obj, arg, free_at_crash);
714         END OBJ(upspeedf, &update_net_stats)
715                 parse_net_stat_arg(obj, arg, free_at_crash);
716 #ifdef X11
717         END OBJ(upspeedgraph, &update_net_stats)
718                 parse_net_stat_graph_arg(obj, arg, free_at_crash);
719 #endif
720         END OBJ(uptime_short, &update_uptime)
721         END OBJ(uptime, &update_uptime)
722         END OBJ(user_names, &update_users)
723         END OBJ(user_times, &update_users)
724         END OBJ(conky_user_time, &update_users)
725         END OBJ(user_terms, &update_users)
726         END OBJ(user_number, &update_users)
727 #if defined(__linux__)
728         END OBJ(gw_iface, &update_gateway_info)
729         END OBJ(gw_ip, &update_gateway_info)
730 #endif /* !__linux__ */
731 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
732                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
733         END OBJ(apm_adapter, 0)
734         END OBJ(apm_battery_life, 0)
735         END OBJ(apm_battery_time, 0)
736 #endif /* __FreeBSD__ */
737         END OBJ(imap_unseen, 0)
738                 parse_imap_mail_args(obj, arg);
739         END OBJ(imap_messages, 0)
740                 parse_imap_mail_args(obj, arg);
741         END OBJ(pop3_unseen, 0)
742                 parse_pop3_mail_args(obj, arg);
743         END OBJ(pop3_used, 0)
744                 parse_pop3_mail_args(obj, arg);
745 #ifdef IBM
746         END OBJ_ARG(smapi, 0, "smapi needs an argument")
747                 obj->data.s = strndup(arg, text_buffer_size);
748         END OBJ_IF_ARG(if_smapi_bat_installed, 0, "if_smapi_bat_installed needs an argument")
749                 obj->data.s = strndup(arg, text_buffer_size);
750         END OBJ_ARG(smapi_bat_perc, 0, "smapi_bat_perc needs an argument")
751                 obj->data.s = strndup(arg, text_buffer_size);
752         END OBJ_ARG(smapi_bat_temp, 0, "smapi_bat_temp needs an argument")
753                 obj->data.s = strndup(arg, text_buffer_size);
754         END OBJ_ARG(smapi_bat_power, 0, "smapi_bat_power needs an argument")
755                 obj->data.s = strndup(arg, text_buffer_size);
756 #ifdef X11
757         END OBJ_ARG(smapi_bat_bar, 0, "smapi_bat_bar needs an argument")
758                 int cnt;
759                 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
760                         NORM_ERR("first argument to smapi_bat_bar must be an integer value");
761                         obj->data.i = -1;
762                 } else
763                         arg = scan_bar(obj, arg + cnt);
764 #endif /* X11 */
765 #endif /* IBM */
766 #ifdef MPD
767 #define mpd_set_maxlen(name) \
768                 if (arg) { \
769                         int i; \
770                         sscanf(arg, "%d", &i); \
771                         if (i > 0) \
772                                 obj->data.i = i + 1; \
773                         else \
774                                 NORM_ERR(#name ": invalid length argument"); \
775                 }
776         END OBJ(mpd_artist, &update_mpd)
777                 mpd_set_maxlen(mpd_artist);
778                 init_mpd();
779         END OBJ(mpd_title, &update_mpd)
780                 mpd_set_maxlen(mpd_title);
781                 init_mpd();
782         END OBJ(mpd_random, &update_mpd) init_mpd();
783         END OBJ(mpd_repeat, &update_mpd) init_mpd();
784         END OBJ(mpd_elapsed, &update_mpd) init_mpd();
785         END OBJ(mpd_length, &update_mpd) init_mpd();
786         END OBJ(mpd_track, &update_mpd)
787                 mpd_set_maxlen(mpd_track);
788                 init_mpd();
789         END OBJ(mpd_name, &update_mpd)
790                 mpd_set_maxlen(mpd_name);
791                 init_mpd();
792         END OBJ(mpd_file, &update_mpd)
793                 mpd_set_maxlen(mpd_file);
794                 init_mpd();
795         END OBJ(mpd_percent, &update_mpd) init_mpd();
796         END OBJ(mpd_album, &update_mpd)
797                 mpd_set_maxlen(mpd_album);
798                 init_mpd();
799         END OBJ(mpd_vol, &update_mpd) init_mpd();
800         END OBJ(mpd_bitrate, &update_mpd) init_mpd();
801         END OBJ(mpd_status, &update_mpd) init_mpd();
802         END OBJ(mpd_bar, &update_mpd)
803                 scan_bar(obj, arg);
804                 init_mpd();
805         END OBJ(mpd_smart, &update_mpd)
806                 mpd_set_maxlen(mpd_smart);
807                 init_mpd();
808         END OBJ_IF(if_mpd_playing, &update_mpd)
809                 init_mpd();
810 #undef mpd_set_maxlen
811 #endif /* MPD */
812 #ifdef MOC
813         END OBJ(moc_state, &update_moc)
814         END OBJ(moc_file, &update_moc)
815         END OBJ(moc_title, &update_moc)
816         END OBJ(moc_artist, &update_moc)
817         END OBJ(moc_song, &update_moc)
818         END OBJ(moc_album, &update_moc)
819         END OBJ(moc_totaltime, &update_moc)
820         END OBJ(moc_timeleft, &update_moc)
821         END OBJ(moc_curtime, &update_moc)
822         END OBJ(moc_bitrate, &update_moc)
823         END OBJ(moc_rate, &update_moc)
824 #endif /* MOC */
825 #ifdef XMMS2
826         END OBJ(xmms2_artist, &update_xmms2)
827         END OBJ(xmms2_album, &update_xmms2)
828         END OBJ(xmms2_title, &update_xmms2)
829         END OBJ(xmms2_genre, &update_xmms2)
830         END OBJ(xmms2_comment, &update_xmms2)
831         END OBJ(xmms2_url, &update_xmms2)
832         END OBJ(xmms2_tracknr, &update_xmms2)
833         END OBJ(xmms2_bitrate, &update_xmms2)
834         END OBJ(xmms2_date, &update_xmms2)
835         END OBJ(xmms2_id, &update_xmms2)
836         END OBJ(xmms2_duration, &update_xmms2)
837         END OBJ(xmms2_elapsed, &update_xmms2)
838         END OBJ(xmms2_size, &update_xmms2)
839         END OBJ(xmms2_status, &update_xmms2)
840         END OBJ(xmms2_percent, &update_xmms2)
841 #ifdef X11
842         END OBJ(xmms2_bar, &update_xmms2)
843                 scan_bar(obj, arg);
844 #endif /* X11 */
845         END OBJ(xmms2_smart, &update_xmms2)
846         END OBJ(xmms2_playlist, &update_xmms2)
847         END OBJ(xmms2_timesplayed, &update_xmms2)
848         END OBJ_IF(if_xmms2_connected, &update_xmms2)
849 #endif
850 #ifdef AUDACIOUS
851         END OBJ(audacious_status, &update_audacious)
852         END OBJ_ARG(audacious_title, &update_audacious, "audacious_title needs an argument")
853                 sscanf(arg, "%d", &info.audacious.max_title_len);
854                 if (info.audacious.max_title_len > 0) {
855                         info.audacious.max_title_len++;
856                 } else {
857                         CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
858                 }
859         END OBJ(audacious_length, &update_audacious)
860         END OBJ(audacious_length_seconds, &update_audacious)
861         END OBJ(audacious_position, &update_audacious)
862         END OBJ(audacious_position_seconds, &update_audacious)
863         END OBJ(audacious_bitrate, &update_audacious)
864         END OBJ(audacious_frequency, &update_audacious)
865         END OBJ(audacious_channels, &update_audacious)
866         END OBJ(audacious_filename, &update_audacious)
867         END OBJ(audacious_playlist_length, &update_audacious)
868         END OBJ(audacious_playlist_position, &update_audacious)
869         END OBJ(audacious_main_volume, &update_audacious)
870 #ifdef X11
871         END OBJ(audacious_bar, &update_audacious)
872                 scan_bar(obj, arg);
873 #endif /* X11 */
874 #endif
875 #ifdef BMPX
876         END OBJ(bmpx_title, &update_bmpx)
877                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
878         END OBJ(bmpx_artist, &update_bmpx)
879                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
880         END OBJ(bmpx_album, &update_bmpx)
881                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
882         END OBJ(bmpx_track, &update_bmpx)
883                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
884         END OBJ(bmpx_uri, &update_bmpx)
885                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
886         END OBJ(bmpx_bitrate, &update_bmpx)
887                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
888 #endif
889 #ifdef EVE
890         END OBJ_ARG(eve, 0, "eve needs arguments: <userid> <apikey> <characterid>")
891                 scan_eve(obj, arg);
892 #endif
893 #ifdef HAVE_CURL
894         END OBJ_ARG(curl, 0, "curl needs arguments: <uri> <interval in minutes>")
895                 curl_parse_arg(obj, arg);
896 #endif
897 #ifdef RSS
898         END OBJ_ARG(rss, 0, "rss needs arguments: <uri> <interval in minutes> <action> [act_par] [spaces in front]")
899                 rss_scan_arg(obj, arg);
900 #endif
901 #ifdef WEATHER
902         END OBJ_ARG(weather, 0, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]")
903                 scan_weather_arg(obj, arg, free_at_crash);
904 #endif
905 #ifdef XOAP
906         END OBJ_ARG(weather_forecast, 0, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]")
907                 scan_weather_forecast_arg(obj, arg, free_at_crash);
908 #endif
909 #ifdef HAVE_LUA
910         END OBJ_ARG(lua, 0, "lua needs arguments: <function name> [function parameters]")
911                 obj->data.s = strndup(arg, text_buffer_size);
912         END OBJ_ARG(lua_parse, 0, "lua_parse needs arguments: <function name> [function parameters]")
913                 obj->data.s = strndup(arg, text_buffer_size);
914         END OBJ_ARG(lua_bar, 0, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]")
915                 arg = scan_bar(obj, arg);
916                 if(arg) {
917                         obj->data.s = strndup(arg, text_buffer_size);
918                 } else {
919                         CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
920                 }
921 #ifdef X11
922         END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]")
923                 char *buf = 0;
924                 buf = scan_graph(obj, arg, 0);
925                 if (buf) {
926                         obj->data.s = buf;
927                 } else {
928                         CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
929                 }
930         END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]")
931                 arg = scan_gauge(obj, arg);
932                 if (arg) {
933                         obj->data.s = strndup(arg, text_buffer_size);
934                 } else {
935                         CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
936                 }
937 #endif /* X11 */
938 #endif /* HAVE_LUA */
939 #ifdef HDDTEMP
940         END OBJ(hddtemp, &update_hddtemp)
941                 if (arg)
942                         obj->data.s = strndup(arg, text_buffer_size);
943 #endif /* HDDTEMP */
944 #ifdef TCP_PORT_MONITOR
945         END OBJ_ARG(tcp_portmon, &tcp_portmon_update, "tcp_portmon: needs arguments")
946                 tcp_portmon_init(obj, arg);
947 #endif /* TCP_PORT_MONITOR */
948         END OBJ(entropy_avail, &update_entropy)
949         END OBJ(entropy_perc, &update_entropy)
950         END OBJ(entropy_poolsize, &update_entropy)
951         END OBJ(entropy_bar, &update_entropy)
952                 scan_bar(obj, arg);
953         END OBJ_ARG(include, 0, "include needs a argument")
954                 struct conftree *leaf = conftree_add(currentconffile, arg);
955                 if(leaf) {
956                         if (load_config_file(arg) == TRUE) {
957                                 obj->sub = malloc(sizeof(struct text_object));
958                                 currentconffile = leaf;
959                                 extract_variable_text_internal(obj->sub, get_global_text());
960                                 currentconffile = leaf->back;
961                         } else {
962                                 NORM_ERR("Can't load configfile '%s'.", arg);
963                         }
964                 } else {
965                         NORM_ERR("You are trying to load '%s' recursively, I'm only going to load it once to prevent an infinite loop.", arg);
966                 }
967         END OBJ_ARG(blink, 0, "blink needs a argument")
968                 obj->sub = malloc(sizeof(struct text_object));
969                 extract_variable_text_internal(obj->sub, arg);
970         END OBJ_ARG(to_bytes, 0, "to_bytes needs a argument")
971                 obj->sub = malloc(sizeof(struct text_object));
972                 extract_variable_text_internal(obj->sub, arg);
973         END OBJ(scroll, 0)
974                 parse_scroll_arg(obj, arg, free_at_crash);
975         END OBJ_ARG(combine, 0, "combine needs arguments: <text1> <text2>")
976                 parse_combine_arg(obj, arg, free_at_crash);
977 #ifdef NVIDIA
978         END OBJ_ARG(nvidia, 0, "nvidia needs an argument")
979                 if (set_nvidia_type(obj, arg)) {
980                         CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
981                                  " specified: '%s'\n", arg);
982                 }
983 #endif /* NVIDIA */
984 #ifdef APCUPSD
985         END OBJ_ARG(apcupsd, &update_apcupsd, "apcupsd needs arguments: <host> <port>")
986                 char host[64];
987                 int port;
988                 if (sscanf(arg, "%63s %d", host, &port) != 2) {
989                         CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
990                 } else {
991                         info.apcupsd.port = htons(port);
992                         strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
993                 }
994         END OBJ(apcupsd_name, &update_apcupsd)
995         END OBJ(apcupsd_model, &update_apcupsd)
996         END OBJ(apcupsd_upsmode, &update_apcupsd)
997         END OBJ(apcupsd_cable, &update_apcupsd)
998         END OBJ(apcupsd_status, &update_apcupsd)
999         END OBJ(apcupsd_linev, &update_apcupsd)
1000         END OBJ(apcupsd_load, &update_apcupsd)
1001         END OBJ(apcupsd_loadbar, &update_apcupsd)
1002                 scan_bar(obj, arg);
1003 #ifdef X11
1004         END OBJ(apcupsd_loadgraph, &update_apcupsd)
1005                 char* buf = 0;
1006                 buf = scan_graph(obj, arg, 0);
1007                 if (buf) free(buf);
1008         END OBJ(apcupsd_loadgauge, &update_apcupsd)
1009                 scan_gauge(obj, arg);
1010 #endif /* X11 */
1011         END OBJ(apcupsd_charge, &update_apcupsd)
1012         END OBJ(apcupsd_timeleft, &update_apcupsd)
1013         END OBJ(apcupsd_temp, &update_apcupsd)
1014         END OBJ(apcupsd_lastxfer, &update_apcupsd)
1015 #endif /* APCUPSD */
1016         END {
1017                 char buf[256];
1018
1019                 NORM_ERR("unknown variable %s", s);
1020                 obj->type = OBJ_text;
1021                 snprintf(buf, 256, "${%s}", s);
1022                 obj->data.s = strndup(buf, text_buffer_size);
1023         }
1024 #undef OBJ
1025 #undef OBJ_IF
1026 #undef OBJ_ARG
1027 #undef OBJ_IF_ARG
1028 #undef __OBJ_HEAD
1029 #undef __OBJ_IF
1030 #undef __OBJ_ARG
1031 #undef END
1032
1033         return obj;
1034 }
1035
1036 /*
1037  * - assumes that *string is '#'
1038  * - removes the part from '#' to the end of line ('\n' or '\0')
1039  * - it removes the '\n'
1040  * - copies the last char into 'char *last' argument, which should be a pointer
1041  *   to a char rather than a string.
1042  */
1043 static size_t remove_comment(char *string, char *last)
1044 {
1045         char *end = string;
1046         while (*end != '\0' && *end != '\n') {
1047                 ++end;
1048         }
1049         if (last) *last = *end;
1050         if (*end == '\n') end++;
1051         strfold(string, end - string);
1052         return end - string;
1053 }
1054
1055 size_t remove_comments(char *string)
1056 {
1057         char *curplace;
1058         size_t folded = 0;
1059         for (curplace = string; *curplace != 0; curplace++) {
1060                 if (*curplace == '\\' && *(curplace + 1) == '#') {
1061                         // strcpy can't be used for overlapping strings
1062                         strfold(curplace, 1);
1063                         folded += 1;
1064                 } else if (*curplace == '#') {
1065                         folded += remove_comment(curplace, 0);
1066                 }
1067         }
1068         return folded;
1069 }
1070
1071 int extract_variable_text_internal(struct text_object *retval, const char *const_p)
1072 {
1073         struct text_object *obj;
1074         char *p, *s, *orig_p;
1075         long line;
1076         void *ifblock_opaque = NULL;
1077         char *tmp_p;
1078         char *arg = 0;
1079         size_t len = 0;
1080
1081         p = strndup(const_p, max_user_text - 1);
1082         while (text_contains_templates(p)) {
1083                 char *tmp;
1084                 tmp = find_and_replace_templates(p);
1085                 free(p);
1086                 p = tmp;
1087         }
1088         s = orig_p = p;
1089
1090         if (strcmp(p, const_p)) {
1091                 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
1092         } else {
1093                 DBGP("no templates to replace");
1094         }
1095
1096         memset(retval, 0, sizeof(struct text_object));
1097
1098         line = global_text_lines;
1099
1100         while (*p) {
1101                 if (*p == '\n') {
1102                         line++;
1103                 }
1104                 if (*p == '$') {
1105                         *p = '\0';
1106                         obj = create_plain_text(s);
1107                         if (obj != NULL) {
1108                                 append_object(retval, obj);
1109                         }
1110                         *p = '$';
1111                         p++;
1112                         s = p;
1113
1114                         if (*p != '$') {
1115                                 char buf[256];
1116                                 const char *var;
1117
1118                                 /* variable is either $foo or ${foo} */
1119                                 if (*p == '{') {
1120                                         unsigned int brl = 1, brr = 0;
1121
1122                                         p++;
1123                                         s = p;
1124                                         while (*p && brl != brr) {
1125                                                 if (*p == '{') {
1126                                                         brl++;
1127                                                 }
1128                                                 if (*p == '}') {
1129                                                         brr++;
1130                                                 }
1131                                                 p++;
1132                                         }
1133                                         p--;
1134                                 } else {
1135                                         s = p;
1136                                         if (*p == '#') {
1137                                                 p++;
1138                                         }
1139                                         while (*p && (isalnum((int) *p) || *p == '_')) {
1140                                                 p++;
1141                                         }
1142                                 }
1143
1144                                 /* copy variable to buffer */
1145                                 len = (p - s > 255) ? 255 : (p - s);
1146                                 strncpy(buf, s, len);
1147                                 buf[len] = '\0';
1148
1149                                 if (*p == '}') {
1150                                         p++;
1151                                 }
1152                                 s = p;
1153
1154                                 /* search for variable in environment */
1155
1156                                 var = getenv(buf);
1157                                 if (var) {
1158                                         obj = create_plain_text(var);
1159                                         if (obj) {
1160                                                 append_object(retval, obj);
1161                                         }
1162                                         continue;
1163                                 }
1164
1165                                 /* if variable wasn't found in environment, use some special */
1166
1167                                 arg = 0;
1168
1169                                 /* split arg */
1170                                 if (strchr(buf, ' ')) {
1171                                         arg = strchr(buf, ' ');
1172                                         *arg = '\0';
1173                                         arg++;
1174                                         while (isspace((int) *arg)) {
1175                                                 arg++;
1176                                         }
1177                                         if (!*arg) {
1178                                                 arg = 0;
1179                                         }
1180                                 }
1181
1182                                 /* lowercase variable name */
1183                                 tmp_p = buf;
1184                                 while (*tmp_p) {
1185                                         *tmp_p = tolower(*tmp_p);
1186                                         tmp_p++;
1187                                 }
1188
1189                                 obj = construct_text_object(buf, arg,
1190                                                 line, &ifblock_opaque, orig_p);
1191                                 if (obj != NULL) {
1192                                         append_object(retval, obj);
1193                                 }
1194                                 continue;
1195                         } else {
1196                                 obj = create_plain_text("$");
1197                                 s = p + 1;
1198                                 if (obj != NULL) {
1199                                         append_object(retval, obj);
1200                                 }
1201                         }
1202                 } else if (*p == '\\' && *(p+1) == '#') {
1203                         strfold(p, 1);
1204                 } else if (*p == '#') {
1205                         char c;
1206                         if (remove_comment(p, &c) && p > orig_p && c == '\n') {
1207                                 /* if remove_comment removed a newline, we need to 'back up' with p */
1208                                 p--;
1209                         }
1210                 }
1211                 p++;
1212         }
1213         obj = create_plain_text(s);
1214         if (obj != NULL) {
1215                 append_object(retval, obj);
1216         }
1217
1218         if (!ifblock_stack_empty(&ifblock_opaque)) {
1219                 NORM_ERR("one or more $endif's are missing");
1220         }
1221
1222         free(orig_p);
1223         return 0;
1224 }
1225
1226 /*
1227  * Frees the list of text objects root points to.  When internal = 1, it won't
1228  * free global objects.
1229  */
1230 void free_text_objects(struct text_object *root, int internal)
1231 {
1232         struct text_object *obj;
1233
1234         if (!root->prev) {
1235                 return;
1236         }
1237
1238 #define data obj->data
1239         for (obj = root->prev; obj; obj = root->prev) {
1240                 root->prev = obj->prev;
1241                 switch (obj->type) {
1242 #ifndef __OpenBSD__
1243                         case OBJ_acpitemp:
1244                                 close(data.i);
1245                                 break;
1246 #endif /* !__OpenBSD__ */
1247 #ifdef __linux__
1248                         case OBJ_i2c:
1249                         case OBJ_platform:
1250                         case OBJ_hwmon:
1251                                 free_sysfs_sensor(obj);
1252                                 break;
1253 #endif /* __linux__ */
1254                         case OBJ_pid_chroot:
1255                                 free(data.s);
1256                                 break;
1257                         case OBJ_pid_cmdline:
1258                                 free(data.s);
1259                                 break;
1260                         case OBJ_pid_cwd:
1261                                 free(data.s);
1262                                 break;
1263                         case OBJ_pid_environ:
1264                                 free_pid_environ(obj);
1265                                 break;
1266                         case OBJ_pid_environ_list:
1267                                 free(data.s);
1268                                 break;
1269                         case OBJ_pid_exe:
1270                                 free(data.s);
1271                                 break;
1272                         case OBJ_pid_openfiles:
1273                                 free(data.s);
1274                                 break;
1275                         case OBJ_pid_stderr:
1276                                 free(data.s);
1277                                 break;
1278                         case OBJ_pid_stdin:
1279                                 free(data.s);
1280                                 break;
1281                         case OBJ_pid_stdout:
1282                                 free(data.s);
1283                                 break;
1284                         case OBJ_read_tcp:
1285                                 free_read_tcp(obj);
1286                                 break;
1287                         case OBJ_time:
1288                         case OBJ_utime:
1289                                 free_time(obj);
1290                                 break;
1291                         case OBJ_tztime:
1292                                 free_tztime(obj);
1293                                 break;
1294                         case OBJ_mboxscan:
1295                                 free_mboxscan(obj);
1296                                 break;
1297                         case OBJ_mails:
1298                         case OBJ_new_mails:
1299                         case OBJ_seen_mails:
1300                         case OBJ_unseen_mails:
1301                         case OBJ_flagged_mails:
1302                         case OBJ_unflagged_mails:
1303                         case OBJ_forwarded_mails:
1304                         case OBJ_unforwarded_mails:
1305                         case OBJ_replied_mails:
1306                         case OBJ_unreplied_mails:
1307                         case OBJ_draft_mails:
1308                         case OBJ_trashed_mails:
1309                                 free_local_mails(obj);
1310                                 break;
1311                         case OBJ_imap_unseen:
1312                         case OBJ_imap_messages:
1313                         case OBJ_pop3_unseen:
1314                         case OBJ_pop3_used:
1315                                 free_mail_obj(obj);
1316                                 break;
1317                         case OBJ_if_empty:
1318                         case OBJ_if_match:
1319                                 free_text_objects(obj->sub, 1);
1320                                 free(obj->sub);
1321                                 break;
1322                         case OBJ_if_existing:
1323                         case OBJ_if_mounted:
1324                         case OBJ_if_running:
1325                                 free(data.s);
1326                                 break;
1327                         case OBJ_head:
1328                         case OBJ_tail:
1329                                 free_tailhead(obj);
1330                                 break;
1331                         case OBJ_text:
1332                         case OBJ_font:
1333                         case OBJ_image:
1334                         case OBJ_eval:
1335                                 free(data.s);
1336                                 break;
1337                         case OBJ_exec:
1338                         case OBJ_execbar:
1339 #ifdef X11
1340                         case OBJ_execgauge:
1341                         case OBJ_execgraph:
1342 #endif
1343                         case OBJ_execp:
1344                                 free_exec(obj);
1345                                 break;
1346 #ifdef HAVE_ICONV
1347                         case OBJ_iconv_start:
1348                                 free_iconv();
1349                                 break;
1350 #endif
1351 #ifdef __linux__
1352                         case OBJ_disk_protect:
1353                                 free(data.s);
1354                                 break;
1355                         case OBJ_if_gw:
1356                         case OBJ_gw_iface:
1357                         case OBJ_gw_ip:
1358                                 free_gateway_info();
1359                                 break;
1360                         case OBJ_ioscheduler:
1361                                 if(data.s)
1362                                         free(data.s);
1363                                 break;
1364 #endif
1365 #if (defined(__FreeBSD__) || defined(__linux__))
1366                         case OBJ_if_up:
1367                                 free_if_up(obj);
1368                                 break;
1369 #endif
1370 #ifdef XMMS2
1371                         case OBJ_xmms2_artist:
1372                                 if (info.xmms2.artist) {
1373                                         free(info.xmms2.artist);
1374                                         info.xmms2.artist = 0;
1375                                 }
1376                                 break;
1377                         case OBJ_xmms2_album:
1378                                 if (info.xmms2.album) {
1379                                         free(info.xmms2.album);
1380                                         info.xmms2.album = 0;
1381                                 }
1382                                 break;
1383                         case OBJ_xmms2_title:
1384                                 if (info.xmms2.title) {
1385                                         free(info.xmms2.title);
1386                                         info.xmms2.title = 0;
1387                                 }
1388                                 break;
1389                         case OBJ_xmms2_genre:
1390                                 if (info.xmms2.genre) {
1391                                         free(info.xmms2.genre);
1392                                         info.xmms2.genre = 0;
1393                                 }
1394                                 break;
1395                         case OBJ_xmms2_comment:
1396                                 if (info.xmms2.comment) {
1397                                         free(info.xmms2.comment);
1398                                         info.xmms2.comment = 0;
1399                                 }
1400                                 break;
1401                         case OBJ_xmms2_url:
1402                                 if (info.xmms2.url) {
1403                                         free(info.xmms2.url);
1404                                         info.xmms2.url = 0;
1405                                 }
1406                                 break;
1407                         case OBJ_xmms2_date:
1408                                 if (info.xmms2.date) {
1409                                         free(info.xmms2.date);
1410                                         info.xmms2.date = 0;
1411                                 }
1412                                 break;
1413                         case OBJ_xmms2_status:
1414                                 if (info.xmms2.status) {
1415                                         free(info.xmms2.status);
1416                                         info.xmms2.status = 0;
1417                                 }
1418                                 break;
1419                         case OBJ_xmms2_playlist:
1420                                 if (info.xmms2.playlist) {
1421                                         free(info.xmms2.playlist);
1422                                         info.xmms2.playlist = 0;
1423                                 }
1424                                 break;
1425                         case OBJ_xmms2_smart:
1426                                 if (info.xmms2.artist) {
1427                                         free(info.xmms2.artist);
1428                                         info.xmms2.artist = 0;
1429                                 }
1430                                 if (info.xmms2.title) {
1431                                         free(info.xmms2.title);
1432                                         info.xmms2.title = 0;
1433                                 }
1434                                 if (info.xmms2.url) {
1435                                         free(info.xmms2.url);
1436                                         info.xmms2.url = 0;
1437                                 }
1438                                 break;
1439 #endif
1440 #ifdef BMPX
1441                         case OBJ_bmpx_title:
1442                         case OBJ_bmpx_artist:
1443                         case OBJ_bmpx_album:
1444                         case OBJ_bmpx_track:
1445                         case OBJ_bmpx_uri:
1446                         case OBJ_bmpx_bitrate:
1447                                 break;
1448 #endif
1449 #ifdef EVE
1450                         case OBJ_eve:
1451                                 free_eve(obj);
1452                                 break;
1453 #endif
1454 #ifdef HAVE_CURL
1455                         case OBJ_curl:
1456                                 curl_obj_free(obj);
1457                                 break;
1458 #endif
1459 #ifdef RSS
1460                         case OBJ_rss:
1461                                 rss_free_obj_info(obj);
1462                                 break;
1463 #endif
1464 #ifdef WEATHER
1465                         case OBJ_weather:
1466                                 free_weather(obj);
1467                                 break;
1468 #endif
1469 #ifdef XOAP
1470                         case OBJ_weather_forecast:
1471                                 free_weather(obj);
1472                                 break;
1473 #endif
1474 #ifdef HAVE_LUA
1475                         case OBJ_lua:
1476                         case OBJ_lua_parse:
1477                         case OBJ_lua_bar:
1478 #ifdef X11
1479                         case OBJ_lua_graph:
1480                         case OBJ_lua_gauge:
1481 #endif /* X11 */
1482                                 free(data.s);
1483                                 break;
1484 #endif /* HAVE_LUA */
1485                         case OBJ_pre_exec:
1486                                 break;
1487 #ifndef __OpenBSD__
1488                         case OBJ_battery:
1489                                 free(data.s);
1490                                 break;
1491                         case OBJ_battery_short:
1492                                 free(data.s);
1493                                 break;
1494                         case OBJ_battery_time:
1495                                 free(data.s);
1496                                 break;
1497 #endif /* !__OpenBSD__ */
1498                         case OBJ_execpi:
1499                         case OBJ_execi:
1500                         case OBJ_execibar:
1501                         case OBJ_texeci:
1502 #ifdef X11
1503                         case OBJ_execigraph:
1504                         case OBJ_execigauge:
1505 #endif /* X11 */
1506                                 free_execi(obj);
1507                                 break;
1508                         case OBJ_nameserver:
1509                                 free_dns_data();
1510                                 break;
1511                         case OBJ_top:
1512                         case OBJ_top_mem:
1513                         case OBJ_top_time:
1514 #ifdef IOSTATS
1515                         case OBJ_top_io:
1516 #endif
1517                                 free_top(obj, internal);
1518                                 break;
1519 #ifdef HDDTEMP
1520                         case OBJ_hddtemp:
1521                                 if (data.s) {
1522                                         free(data.s);
1523                                         data.s = NULL;
1524                                 }
1525                                 free_hddtemp();
1526                                 break;
1527 #endif /* HDDTEMP */
1528                         case OBJ_entropy_avail:
1529                         case OBJ_entropy_perc:
1530                         case OBJ_entropy_poolsize:
1531                         case OBJ_entropy_bar:
1532                                 break;
1533                         case OBJ_user_names:
1534                                 if (info.users.names) {
1535                                         free(info.users.names);
1536                                         info.users.names = 0;
1537                                 }
1538                                 break;
1539                         case OBJ_user_terms:
1540                                 if (info.users.terms) {
1541                                         free(info.users.terms);
1542                                         info.users.terms = 0;
1543                                 }
1544                                 break;
1545                         case OBJ_user_times:
1546                                 if (info.users.times) {
1547                                         free(info.users.times);
1548                                         info.users.times = 0;
1549                                 }
1550                                 break;
1551                         case OBJ_conky_user_time:
1552                                 if (info.users.ctime) {
1553                                         free(info.users.ctime);
1554                                         info.users.ctime = 0;
1555                                 }
1556                                 break;
1557 #ifdef IBM
1558                         case OBJ_smapi:
1559                         case OBJ_smapi_bat_perc:
1560                         case OBJ_smapi_bat_temp:
1561                         case OBJ_smapi_bat_power:
1562                                 free(data.s);
1563                                 break;
1564                         case OBJ_if_smapi_bat_installed:
1565                                 free(data.s);
1566                                 break;
1567 #endif /* IBM */
1568 #ifdef NVIDIA
1569                         case OBJ_nvidia:
1570                                 free_nvidia(obj);
1571                                 break;
1572 #endif /* NVIDIA */
1573 #ifdef MPD
1574                         case OBJ_mpd_title:
1575                         case OBJ_mpd_artist:
1576                         case OBJ_mpd_album:
1577                         case OBJ_mpd_random:
1578                         case OBJ_mpd_repeat:
1579                         case OBJ_mpd_vol:
1580                         case OBJ_mpd_bitrate:
1581                         case OBJ_mpd_status:
1582                         case OBJ_mpd_bar:
1583                         case OBJ_mpd_elapsed:
1584                         case OBJ_mpd_length:
1585                         case OBJ_mpd_track:
1586                         case OBJ_mpd_name:
1587                         case OBJ_mpd_file:
1588                         case OBJ_mpd_percent:
1589                         case OBJ_mpd_smart:
1590                         case OBJ_if_mpd_playing:
1591                                 free_mpd();
1592                                 break;
1593 #endif /* MPD */
1594 #ifdef MOC
1595                         case OBJ_moc_state:
1596                         case OBJ_moc_file:
1597                         case OBJ_moc_title:
1598                         case OBJ_moc_artist:
1599                         case OBJ_moc_song:
1600                         case OBJ_moc_album:
1601                         case OBJ_moc_totaltime:
1602                         case OBJ_moc_timeleft:
1603                         case OBJ_moc_curtime:
1604                         case OBJ_moc_bitrate:
1605                         case OBJ_moc_rate:
1606                                 free_moc();
1607                                 break;
1608 #endif /* MOC */
1609                         case OBJ_include:
1610                         case OBJ_blink:
1611                         case OBJ_to_bytes:
1612                                 if(obj->sub) {
1613                                         free_text_objects(obj->sub, 1);
1614                                         free(obj->sub);
1615                                 }
1616                                 break;
1617                         case OBJ_scroll:
1618                                 free_scroll(obj);
1619                                 break;
1620                         case OBJ_combine:
1621                                 free_combine(obj);
1622                                 break;
1623 #ifdef APCUPSD
1624                         case OBJ_apcupsd:
1625                         case OBJ_apcupsd_name:
1626                         case OBJ_apcupsd_model:
1627                         case OBJ_apcupsd_upsmode:
1628                         case OBJ_apcupsd_cable:
1629                         case OBJ_apcupsd_status:
1630                         case OBJ_apcupsd_linev:
1631                         case OBJ_apcupsd_load:
1632                         case OBJ_apcupsd_loadbar:
1633 #ifdef X11
1634                         case OBJ_apcupsd_loadgraph:
1635                         case OBJ_apcupsd_loadgauge:
1636 #endif /* X11 */
1637                         case OBJ_apcupsd_charge:
1638                         case OBJ_apcupsd_timeleft:
1639                         case OBJ_apcupsd_temp:
1640                         case OBJ_apcupsd_lastxfer:
1641                                 break;
1642 #endif /* APCUPSD */
1643 #ifdef TCP_PORT_MONITOR
1644                         case OBJ_tcp_portmon:
1645                                 tcp_portmon_free(obj);
1646                                 break;
1647 #endif /* TCP_PORT_MONITOR */
1648 #ifdef X11
1649                         case OBJ_desktop:
1650                         case OBJ_desktop_number:
1651                         case OBJ_desktop_name:
1652                                 if(info.x11.desktop.name && !internal) {
1653                                   free(info.x11.desktop.name);
1654                                   info.x11.desktop.name = NULL;
1655                                 }
1656                                 if(info.x11.desktop.all_names && !internal) {
1657                                   free(info.x11.desktop.all_names);
1658                                   info.x11.desktop.all_names = NULL;
1659                                 }
1660                                 break;
1661 #endif /* X11 */
1662                 }
1663                 if(obj->special_data) free(obj->special_data);
1664                 free(obj);
1665         }
1666 #undef data
1667 }
1668