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