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