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