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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
33 #include "text_object.h"
45 #include "iconv_tools.h"
58 #include "temphelper.h"
68 /* check for OS and include appropriate headers */
69 #if defined(__linux__)
71 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
73 #elif defined(__OpenBSD__)
77 /* OS specific prototypes to be implemented by linux.c & Co. */
78 void update_entropy(void);
83 /* strip a leading /dev/ if any, following symlinks first
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.
89 const char *dev_name(const char *path)
91 static char buf[255]; /* should be enough for pathnames */
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);
102 return DEV_NAME(buf);
106 static struct text_object *new_text_object_internal(void)
108 struct text_object *obj = malloc(sizeof(struct text_object));
109 memset(obj, 0, sizeof(struct text_object));
113 static struct text_object *create_plain_text(const char *s)
115 struct text_object *obj;
117 if (s == NULL || *s == '\0') {
121 obj = new_text_object_internal();
123 obj->type = OBJ_text;
124 obj->data.s = strndup(s, text_buffer_size);
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)
132 // struct text_object *obj = new_text_object();
133 struct text_object *obj = new_text_object_internal();
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__); }
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; {
152 obj->type = OBJ_color;
153 obj->data.l = get_x11_color(s);
160 obj->data.i = open_acpi_temperature(arg);
161 END OBJ(acpiacadapter, 0)
163 #endif /* !__OpenBSD__ */
165 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
166 || atoi(&arg[0]) > info.cpu_count) {
168 /* NORM_ERR("freq: Invalid CPU number or you don't have that many CPUs! "
169 "Displaying the clock for CPU 1."); */
171 obj->data.i = atoi(&arg[0]);
175 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
176 || atoi(&arg[0]) > info.cpu_count) {
178 /* NORM_ERR("freq_g: Invalid CPU number or you don't have that many "
179 "CPUs! Displaying the clock for CPU 1."); */
181 obj->data.i = atoi(&arg[0]);
183 END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)")
184 parse_read_tcp_arg(obj, arg, free_at_crash);
185 #if defined(__linux__)
186 END OBJ(voltage_mv, 0)
188 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
189 || atoi(&arg[0]) > info.cpu_count) {
191 /* NORM_ERR("voltage_mv: Invalid CPU number or you don't have that many "
192 "CPUs! Displaying voltage for CPU 1."); */
194 obj->data.i = atoi(&arg[0]);
196 END OBJ(voltage_v, 0)
198 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
199 || atoi(&arg[0]) > info.cpu_count) {
201 /* NORM_ERR("voltage_v: Invalid CPU number or you don't have that many "
202 "CPUs! Displaying voltage for CPU 1."); */
204 obj->data.i = atoi(&arg[0]);
208 END OBJ(wireless_essid, &update_net_stats)
209 parse_net_stat_arg(obj, arg, free_at_crash);
210 END OBJ(wireless_mode, &update_net_stats)
211 parse_net_stat_arg(obj, arg, free_at_crash);
212 END OBJ(wireless_bitrate, &update_net_stats)
213 parse_net_stat_arg(obj, arg, free_at_crash);
214 END OBJ(wireless_ap, &update_net_stats)
215 parse_net_stat_arg(obj, arg, free_at_crash);
216 END OBJ(wireless_link_qual, &update_net_stats)
217 parse_net_stat_arg(obj, arg, free_at_crash);
218 END OBJ(wireless_link_qual_max, &update_net_stats)
219 parse_net_stat_arg(obj, arg, free_at_crash);
220 END OBJ(wireless_link_qual_perc, &update_net_stats)
221 parse_net_stat_arg(obj, arg, free_at_crash);
222 END OBJ(wireless_link_bar, &update_net_stats)
223 parse_net_stat_bar_arg(obj, arg, free_at_crash);
224 #endif /* HAVE_IWLIB */
226 #endif /* __linux__ */
234 sscanf(arg, "%63s", bat);
238 obj->data.s = strndup(bat, text_buffer_size);
239 END OBJ(battery_short, 0)
243 sscanf(arg, "%63s", bat);
247 obj->data.s = strndup(bat, text_buffer_size);
248 END OBJ(battery_time, 0)
252 sscanf(arg, "%63s", bat);
256 obj->data.s = strndup(bat, text_buffer_size);
257 END OBJ(battery_percent, 0)
261 sscanf(arg, "%63s", bat);
265 obj->data.s = strndup(bat, text_buffer_size);
266 END OBJ(battery_bar, 0)
269 arg = scan_bar(obj, arg);
270 sscanf(arg, "%63s", bat);
274 obj->data.s = strndup(bat, text_buffer_size);
275 #endif /* !__OpenBSD__ */
277 #if defined(__linux__)
278 END OBJ_ARG(disk_protect, 0, "disk_protect needs an argument")
279 obj->data.s = strndup(dev_name(arg), text_buffer_size);
280 END OBJ(i8k_version, &update_i8k)
281 END OBJ(i8k_bios, &update_i8k)
282 END OBJ(i8k_serial, &update_i8k)
283 END OBJ(i8k_cpu_temp, &update_i8k)
284 END OBJ(i8k_left_fan_status, &update_i8k)
285 END OBJ(i8k_right_fan_status, &update_i8k)
286 END OBJ(i8k_left_fan_rpm, &update_i8k)
287 END OBJ(i8k_right_fan_rpm, &update_i8k)
288 END OBJ(i8k_ac_status, &update_i8k)
289 END OBJ(i8k_buttons_status, &update_i8k)
292 END OBJ_ARG(ibm_temps, &get_ibm_acpi_temps, "ibm_temps: needs an argument")
293 parse_ibm_temps_arg(obj, arg);
294 END OBJ(ibm_volume, 0)
295 END OBJ(ibm_brightness, 0)
297 /* information from sony_laptop kernel module
298 * /sys/devices/platform/sony-laptop */
299 END OBJ(sony_fanspeed, 0)
300 END OBJ_IF(if_gw, &update_gateway_info)
301 END OBJ_ARG(ioscheduler, 0, "get_ioscheduler needs an argument (e.g. hda)")
302 obj->data.s = strndup(dev_name(arg), text_buffer_size);
303 END OBJ(laptop_mode, 0)
304 END OBJ_ARG(pb_battery, 0, "pb_battery: needs one argument: status, percent or time")
305 if (strcmp(arg, "status") == EQUAL) {
306 obj->data.i = PB_BATT_STATUS;
307 } else if (strcmp(arg, "percent") == EQUAL) {
308 obj->data.i = PB_BATT_PERCENT;
309 } else if (strcmp(arg, "time") == EQUAL) {
310 obj->data.i = PB_BATT_TIME;
312 NORM_ERR("pb_battery: illegal argument '%s', defaulting to status", arg);
313 obj->data.i = PB_BATT_STATUS;
315 #endif /* __linux__ */
316 #if (defined(__FreeBSD__) || defined(__linux__))
317 END OBJ_IF_ARG(if_up, 0, "if_up needs an argument")
318 parse_if_up_arg(obj, arg);
320 #if defined(__OpenBSD__)
321 END OBJ_ARG(obsd_sensors_temp, 0, "obsd_sensors_temp: needs an argument")
322 parse_obsd_sensor(obj, arg);
323 END OBJ_ARG(obsd_sensors_fan, 0, "obsd_sensors_fan: needs 2 arguments (device and sensor number)")
324 parse_obsd_sensor(obj, arg);
325 END OBJ_ARG(obsd_sensors_volt, 0, "obsd_sensors_volt: needs 2 arguments (device and sensor number)")
326 parse_obsd_sensor(obj, arg);
327 END OBJ(obsd_vendor, 0)
328 END OBJ(obsd_product, 0)
329 #endif /* __OpenBSD__ */
330 END OBJ(buffers, &update_meminfo)
331 END OBJ(cached, &update_meminfo)
332 #define SCAN_CPU(__arg, __var) { \
334 if (__arg && sscanf(__arg, " cpu%d %n", &__var, &__offset) > 0) \
339 END OBJ(cpu, &update_cpu_usage)
340 SCAN_CPU(arg, obj->data.i);
341 DBGP2("Adding $cpu for CPU %d", obj->data.i);
343 END OBJ(cpugauge, &update_cpu_usage)
344 SCAN_CPU(arg, obj->data.i);
345 scan_gauge(obj, arg);
346 DBGP2("Adding $cpugauge for CPU %d", obj->data.i);
348 END OBJ(cpubar, &update_cpu_usage)
349 SCAN_CPU(arg, obj->data.i);
351 DBGP2("Adding $cpubar for CPU %d", obj->data.i);
353 END OBJ(cpugraph, &update_cpu_usage)
355 SCAN_CPU(arg, obj->data.i);
356 buf = scan_graph(obj, arg);
357 DBGP2("Adding $cpugraph for CPU %d", obj->data.i);
359 END OBJ(loadgraph, &update_load_average)
360 scan_loadgraph_arg(obj, arg);
362 END OBJ(diskio, &update_diskio)
363 parse_diskio_arg(obj, arg);
364 END OBJ(diskio_read, &update_diskio)
365 parse_diskio_arg(obj, arg);
366 END OBJ(diskio_write, &update_diskio)
367 parse_diskio_arg(obj, arg);
369 END OBJ(diskiograph, &update_diskio)
370 parse_diskiograph_arg(obj, arg);
371 END OBJ(diskiograph_read, &update_diskio)
372 parse_diskiograph_arg(obj, arg);
373 END OBJ(diskiograph_write, &update_diskio)
374 parse_diskiograph_arg(obj, arg);
378 if (output_methods & TO_X) {
379 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
380 set_current_text_color(obj->data.l);
384 if (output_methods & TO_NCURSES) {
385 obj->data.l = COLOR_WHITE;
387 if(strcasecmp(arg, "red") == 0) {
388 obj->data.l = COLOR_RED;
389 }else if(strcasecmp(arg, "green") == 0) {
390 obj->data.l = COLOR_GREEN;
391 }else if(strcasecmp(arg, "yellow") == 0) {
392 obj->data.l = COLOR_YELLOW;
393 }else if(strcasecmp(arg, "blue") == 0) {
394 obj->data.l = COLOR_BLUE;
395 }else if(strcasecmp(arg, "magenta") == 0) {
396 obj->data.l = COLOR_MAGENTA;
397 }else if(strcasecmp(arg, "cyan") == 0) {
398 obj->data.l = COLOR_CYAN;
399 }else if(strcasecmp(arg, "black") == 0) {
400 obj->data.l = COLOR_BLACK;
403 set_current_text_color(obj->data.l);
404 init_pair(obj->data.l, obj->data.l, COLOR_BLACK);
408 obj->data.l = color0;
409 set_current_text_color(obj->data.l);
411 obj->data.l = color1;
412 set_current_text_color(obj->data.l);
414 obj->data.l = color2;
415 set_current_text_color(obj->data.l);
417 obj->data.l = color3;
418 set_current_text_color(obj->data.l);
420 obj->data.l = color4;
421 set_current_text_color(obj->data.l);
423 obj->data.l = color5;
424 set_current_text_color(obj->data.l);
426 obj->data.l = color6;
427 set_current_text_color(obj->data.l);
429 obj->data.l = color7;
430 set_current_text_color(obj->data.l);
432 obj->data.l = color8;
433 set_current_text_color(obj->data.l);
435 obj->data.l = color9;
436 set_current_text_color(obj->data.l);
439 obj->data.s = scan_font(arg);
441 END OBJ(conky_version, 0)
442 END OBJ(conky_build_date, 0)
443 END OBJ(conky_build_arch, 0)
444 END OBJ(downspeed, &update_net_stats)
445 parse_net_stat_arg(obj, arg, free_at_crash);
446 END OBJ(downspeedf, &update_net_stats)
447 parse_net_stat_arg(obj, arg, free_at_crash);
449 END OBJ(downspeedgraph, &update_net_stats)
450 parse_net_stat_graph_arg(obj, arg, free_at_crash);
453 obj_be_ifblock_else(ifblock_opaque, obj);
455 obj_be_ifblock_endif(ifblock_opaque, obj);
457 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
458 #if defined(IMLIB2) && defined(X11)
460 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
463 scan_exec_arg(obj, arg);
465 scan_exec_arg(obj, arg);
467 scan_exec_arg(obj, arg);
469 END OBJ(execgauge, 0)
470 scan_exec_arg(obj, arg);
471 END OBJ(execgraph, 0)
472 scan_execgraph_arg(obj, arg);
474 END OBJ_ARG(execibar, 0, "execibar needs arguments")
475 scan_execi_arg(obj, arg);
477 END OBJ_ARG(execigraph, 0, "execigraph needs arguments")
478 scan_execgraph_arg(obj, arg);
479 END OBJ_ARG(execigauge, 0, "execigauge needs arguments")
480 scan_execi_arg(obj, arg);
482 END OBJ_ARG(execi, 0, "execi needs arguments")
483 scan_execi_arg(obj, arg);
484 END OBJ_ARG(execpi, 0, "execpi needs arguments")
485 scan_execi_arg(obj, arg);
486 END OBJ_ARG(texeci, 0, "texeci needs arguments")
487 scan_execi_arg(obj, arg);
489 scan_pre_exec_arg(obj, arg);
490 END OBJ(fs_bar, &update_fs_stats)
491 init_fs_bar(obj, arg);
492 END OBJ(fs_bar_free, &update_fs_stats)
493 init_fs_bar(obj, arg);
494 END OBJ(fs_free, &update_fs_stats)
496 END OBJ(fs_used_perc, &update_fs_stats)
498 END OBJ(fs_free_perc, &update_fs_stats)
500 END OBJ(fs_size, &update_fs_stats)
502 END OBJ(fs_type, &update_fs_stats)
504 END OBJ(fs_used, &update_fs_stats)
507 obj->data.i = arg ? atoi(arg) : 1;
508 END OBJ(nameserver, &update_dns_data)
509 parse_nameserver_arg(obj, arg);
511 obj->data.i = arg ? atoi(arg) : 1;
513 obj->data.i = arg ? atoi(arg) : 1;
514 END OBJ_ARG(goto, 0, "goto needs arguments")
515 obj->data.i = atoi(arg);
519 END OBJ_ARG(i2c, 0, "i2c needs arguments")
520 parse_i2c_sensor(obj, arg);
521 END OBJ_ARG(platform, 0, "platform needs arguments")
522 parse_platform_sensor(obj, arg);
523 END OBJ_ARG(hwmon, 0, "hwmon needs argumanets")
524 parse_hwmon_sensor(obj, arg);
525 #endif /* __linux__ */
528 /* we have four different types of top (top, top_mem, top_time and top_io). To
529 * avoid having almost-same code four times, we have this special
531 if (strncmp(s, "top", 3) == EQUAL) {
532 add_update_callback(&update_meminfo);
533 add_update_callback(&update_top);
534 if (!parse_top_args(s, arg, obj)) {
537 } else OBJ(addr, &update_net_stats)
538 parse_net_stat_arg(obj, arg, free_at_crash);
539 #if defined(__linux__)
540 END OBJ(addrs, &update_net_stats)
541 parse_net_stat_arg(obj, arg, free_at_crash);
542 #endif /* __linux__ */
543 END OBJ_ARG(tail, 0, "tail needs arguments")
544 init_tailhead("tail", arg, obj, free_at_crash);
545 END OBJ_ARG(head, 0, "head needs arguments")
546 init_tailhead("head", arg, obj, free_at_crash);
547 END OBJ_ARG(lines, 0, "lines needs an argument")
548 obj->data.s = strndup(arg, text_buffer_size);
549 END OBJ_ARG(words, 0, "words needs a argument")
550 obj->data.s = strndup(arg, text_buffer_size);
551 END OBJ(loadavg, &update_load_average)
552 scan_loadavg_arg(obj, arg);
553 END OBJ_IF_ARG(if_empty, 0, "if_empty needs an argument")
554 obj->sub = malloc(sizeof(struct text_object));
555 extract_variable_text_internal(obj->sub, arg);
556 END OBJ_IF_ARG(if_match, 0, "if_match needs arguments")
557 obj->sub = malloc(sizeof(struct text_object));
558 extract_variable_text_internal(obj->sub, arg);
559 END OBJ_IF_ARG(if_existing, 0, "if_existing needs an argument or two")
560 char buf1[256], buf2[256];
561 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
564 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
565 obj->data.ifblock.str = NULL;
567 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
568 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
570 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
571 END OBJ_IF_ARG(if_mounted, 0, "if_mounted needs an argument")
572 obj->data.ifblock.s = strndup(arg, text_buffer_size);
574 END OBJ_IF_ARG(if_running, &update_top, "if_running needs an argument")
576 obj->data.ifblock.s = strndup(arg, text_buffer_size);
578 END OBJ_IF_ARG(if_running, 0, "if_running needs an argument")
581 snprintf(buf, 256, "pidof %s >/dev/null", arg);
582 obj->data.ifblock.s = strndup(buf, text_buffer_size);
587 parse_local_mail_args(obj, arg);
588 END OBJ(new_mails, 0)
589 parse_local_mail_args(obj, arg);
590 END OBJ(seen_mails, 0)
591 parse_local_mail_args(obj, arg);
592 END OBJ(unseen_mails, 0)
593 parse_local_mail_args(obj, arg);
594 END OBJ(flagged_mails, 0)
595 parse_local_mail_args(obj, arg);
596 END OBJ(unflagged_mails, 0)
597 parse_local_mail_args(obj, arg);
598 END OBJ(forwarded_mails, 0)
599 parse_local_mail_args(obj, arg);
600 END OBJ(unforwarded_mails, 0)
601 parse_local_mail_args(obj, arg);
602 END OBJ(replied_mails, 0)
603 parse_local_mail_args(obj, arg);
604 END OBJ(unreplied_mails, 0)
605 parse_local_mail_args(obj, arg);
606 END OBJ(draft_mails, 0)
607 parse_local_mail_args(obj, arg);
608 END OBJ(trashed_mails, 0)
609 parse_local_mail_args(obj, arg);
611 parse_mboxscan_arg(obj, arg);
612 END OBJ(mem, &update_meminfo)
613 END OBJ(memeasyfree, &update_meminfo)
614 END OBJ(memfree, &update_meminfo)
615 END OBJ(memmax, &update_meminfo)
616 END OBJ(memperc, &update_meminfo)
618 END OBJ(memgauge, &update_meminfo)
619 scan_gauge(obj, arg);
621 END OBJ(membar, &update_meminfo)
624 END OBJ(memgraph, &update_meminfo)
626 buf = scan_graph(obj, arg);
631 parse_mixer_arg(obj, arg);
633 parse_mixer_arg(obj, arg);
635 parse_mixer_arg(obj, arg);
638 scan_mixer_bar(obj, arg);
639 END OBJ(mixerlbar, 0)
640 scan_mixer_bar(obj, arg);
641 END OBJ(mixerrbar, 0)
642 scan_mixer_bar(obj, arg);
644 END OBJ_IF(if_mixer_mute, 0)
645 parse_mixer_arg(obj, arg);
647 END OBJ(monitor, &update_x11info)
648 END OBJ(monitor_number, &update_x11info)
649 END OBJ(desktop, &update_x11info)
650 END OBJ(desktop_number, &update_x11info)
651 END OBJ(desktop_name, &update_x11info)
654 END OBJ(processes, &update_total_processes)
655 END OBJ(running_processes, &update_running_processes)
656 END OBJ(shadecolor, 0)
658 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
660 END OBJ(outlinecolor, 0)
662 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
664 END OBJ(stippled_hr, 0)
666 scan_stippled_hr(obj, arg);
668 END OBJ(swap, &update_meminfo)
669 END OBJ(swapfree, &update_meminfo)
670 END OBJ(swapmax, &update_meminfo)
671 END OBJ(swapperc, &update_meminfo)
672 END OBJ(swapbar, &update_meminfo)
680 scan_tztime(obj, arg);
682 END OBJ_ARG(iconv_start, 0, "Iconv requires arguments")
683 init_iconv_start(obj, free_at_crash, arg);
684 END OBJ(iconv_stop, 0)
687 END OBJ(totaldown, &update_net_stats)
688 parse_net_stat_arg(obj, arg, free_at_crash);
689 END OBJ(totalup, &update_net_stats)
690 parse_net_stat_arg(obj, arg, free_at_crash);
692 END OBJ_IF(if_updatenr, 0)
693 obj->data.ifblock.i = arg ? atoi(arg) : 0;
694 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
695 set_updatereset(obj->data.ifblock.i > get_updatereset() ? obj->data.ifblock.i : get_updatereset());
697 obj->data.i = arg ? atoi(arg) : 0;
699 obj->data.i = arg ? atoi(arg) : 0;
700 END OBJ(upspeed, &update_net_stats)
701 parse_net_stat_arg(obj, arg, free_at_crash);
702 END OBJ(upspeedf, &update_net_stats)
703 parse_net_stat_arg(obj, arg, free_at_crash);
705 END OBJ(upspeedgraph, &update_net_stats)
706 parse_net_stat_graph_arg(obj, arg, free_at_crash);
708 END OBJ(uptime_short, &update_uptime)
709 END OBJ(uptime, &update_uptime)
710 END OBJ(user_names, &update_users)
711 END OBJ(user_times, &update_users)
712 END OBJ(user_terms, &update_users)
713 END OBJ(user_number, &update_users)
714 #if defined(__linux__)
715 END OBJ(gw_iface, &update_gateway_info)
716 END OBJ(gw_ip, &update_gateway_info)
717 #endif /* !__linux__ */
719 END OBJ(adt746xcpu, 0)
720 END OBJ(adt746xfan, 0)
721 #endif /* !__OpenBSD__ */
722 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
723 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
724 END OBJ(apm_adapter, 0)
725 END OBJ(apm_battery_life, 0)
726 END OBJ(apm_battery_time, 0)
727 #endif /* __FreeBSD__ */
728 END OBJ(imap_unseen, 0)
729 parse_imap_mail_args(obj, arg);
730 END OBJ(imap_messages, 0)
731 parse_imap_mail_args(obj, arg);
732 END OBJ(pop3_unseen, 0)
733 parse_pop3_mail_args(obj, arg);
734 END OBJ(pop3_used, 0)
735 parse_pop3_mail_args(obj, arg);
737 END OBJ_ARG(smapi, 0, "smapi needs an argument")
738 obj->data.s = strndup(arg, text_buffer_size);
739 END OBJ_IF_ARG(if_smapi_bat_installed, 0, "if_smapi_bat_installed needs an argument")
740 obj->data.ifblock.s = strndup(arg, text_buffer_size);
741 END OBJ_ARG(smapi_bat_perc, 0, "smapi_bat_perc needs an argument")
742 obj->data.s = strndup(arg, text_buffer_size);
743 END OBJ_ARG(smapi_bat_temp, 0, "smapi_bat_temp needs an argument")
744 obj->data.s = strndup(arg, text_buffer_size);
745 END OBJ_ARG(smapi_bat_power, 0, "smapi_bat_power needs an argument")
746 obj->data.s = strndup(arg, text_buffer_size);
748 END OBJ_ARG(smapi_bat_bar, 0, "smapi_bat_bar needs an argument")
750 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
751 NORM_ERR("first argument to smapi_bat_bar must be an integer value");
754 arg = scan_bar(obj, arg + cnt);
758 #define mpd_set_maxlen(name) \
761 sscanf(arg, "%d", &i); \
763 obj->data.i = i + 1; \
765 NORM_ERR(#name ": invalid length argument"); \
767 END OBJ(mpd_artist, &update_mpd)
768 mpd_set_maxlen(mpd_artist);
770 END OBJ(mpd_title, &update_mpd)
771 mpd_set_maxlen(mpd_title);
773 END OBJ(mpd_random, &update_mpd) init_mpd();
774 END OBJ(mpd_repeat, &update_mpd) init_mpd();
775 END OBJ(mpd_elapsed, &update_mpd) init_mpd();
776 END OBJ(mpd_length, &update_mpd) init_mpd();
777 END OBJ(mpd_track, &update_mpd)
778 mpd_set_maxlen(mpd_track);
780 END OBJ(mpd_name, &update_mpd)
781 mpd_set_maxlen(mpd_name);
783 END OBJ(mpd_file, &update_mpd)
784 mpd_set_maxlen(mpd_file);
786 END OBJ(mpd_percent, &update_mpd) init_mpd();
787 END OBJ(mpd_album, &update_mpd)
788 mpd_set_maxlen(mpd_album);
790 END OBJ(mpd_vol, &update_mpd) init_mpd();
791 END OBJ(mpd_bitrate, &update_mpd) init_mpd();
792 END OBJ(mpd_status, &update_mpd) init_mpd();
793 END OBJ(mpd_bar, &update_mpd)
796 END OBJ(mpd_smart, &update_mpd)
797 mpd_set_maxlen(mpd_smart);
799 END OBJ_IF(if_mpd_playing, &update_mpd)
801 #undef mpd_set_maxlen
804 END OBJ(moc_state, &update_moc)
805 END OBJ(moc_file, &update_moc)
806 END OBJ(moc_title, &update_moc)
807 END OBJ(moc_artist, &update_moc)
808 END OBJ(moc_song, &update_moc)
809 END OBJ(moc_album, &update_moc)
810 END OBJ(moc_totaltime, &update_moc)
811 END OBJ(moc_timeleft, &update_moc)
812 END OBJ(moc_curtime, &update_moc)
813 END OBJ(moc_bitrate, &update_moc)
814 END OBJ(moc_rate, &update_moc)
817 END OBJ(xmms2_artist, &update_xmms2)
818 END OBJ(xmms2_album, &update_xmms2)
819 END OBJ(xmms2_title, &update_xmms2)
820 END OBJ(xmms2_genre, &update_xmms2)
821 END OBJ(xmms2_comment, &update_xmms2)
822 END OBJ(xmms2_url, &update_xmms2)
823 END OBJ(xmms2_tracknr, &update_xmms2)
824 END OBJ(xmms2_bitrate, &update_xmms2)
825 END OBJ(xmms2_date, &update_xmms2)
826 END OBJ(xmms2_id, &update_xmms2)
827 END OBJ(xmms2_duration, &update_xmms2)
828 END OBJ(xmms2_elapsed, &update_xmms2)
829 END OBJ(xmms2_size, &update_xmms2)
830 END OBJ(xmms2_status, &update_xmms2)
831 END OBJ(xmms2_percent, &update_xmms2)
833 END OBJ(xmms2_bar, &update_xmms2)
836 END OBJ(xmms2_smart, &update_xmms2)
837 END OBJ(xmms2_playlist, &update_xmms2)
838 END OBJ(xmms2_timesplayed, &update_xmms2)
839 END OBJ_IF(if_xmms2_connected, &update_xmms2)
842 END OBJ(audacious_status, &update_audacious)
843 END OBJ_ARG(audacious_title, &update_audacious, "audacious_title needs an argument")
844 sscanf(arg, "%d", &info.audacious.max_title_len);
845 if (info.audacious.max_title_len > 0) {
846 info.audacious.max_title_len++;
848 CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
850 END OBJ(audacious_length, &update_audacious)
851 END OBJ(audacious_length_seconds, &update_audacious)
852 END OBJ(audacious_position, &update_audacious)
853 END OBJ(audacious_position_seconds, &update_audacious)
854 END OBJ(audacious_bitrate, &update_audacious)
855 END OBJ(audacious_frequency, &update_audacious)
856 END OBJ(audacious_channels, &update_audacious)
857 END OBJ(audacious_filename, &update_audacious)
858 END OBJ(audacious_playlist_length, &update_audacious)
859 END OBJ(audacious_playlist_position, &update_audacious)
860 END OBJ(audacious_main_volume, &update_audacious)
862 END OBJ(audacious_bar, &update_audacious)
867 END OBJ(bmpx_title, &update_bmpx)
868 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
869 END OBJ(bmpx_artist, &update_bmpx)
870 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
871 END OBJ(bmpx_album, &update_bmpx)
872 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
873 END OBJ(bmpx_track, &update_bmpx)
874 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
875 END OBJ(bmpx_uri, &update_bmpx)
876 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
877 END OBJ(bmpx_bitrate, &update_bmpx)
878 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
881 END OBJ_ARG(eve, 0, "eve needs arguments: <userid> <apikey> <characterid>")
885 END OBJ_ARG(curl, 0, "curl needs arguments: <uri> <interval in minutes>")
886 curl_parse_arg(obj, arg);
889 END OBJ_ARG(rss, 0, "rss needs arguments: <uri> <interval in minutes> <action> [act_par] [spaces in front]")
890 rss_scan_arg(obj, arg);
893 END OBJ_ARG(weather, 0, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]")
894 scan_weather_arg(obj, arg, free_at_crash);
897 END OBJ_ARG(weather_forecast, 0, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]")
898 scan_weather_forecast_arg(obj, arg, free_at_crash);
901 END OBJ_ARG(lua, 0, "lua needs arguments: <function name> [function parameters]")
902 obj->data.s = strndup(arg, text_buffer_size);
903 END OBJ_ARG(lua_parse, 0, "lua_parse needs arguments: <function name> [function parameters]")
904 obj->data.s = strndup(arg, text_buffer_size);
905 END OBJ_ARG(lua_bar, 0, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]")
906 arg = scan_bar(obj, arg);
908 obj->data.s = strndup(arg, text_buffer_size);
910 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
913 END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]")
915 buf = scan_graph(obj, arg);
919 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
921 END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]")
922 arg = scan_gauge(obj, arg);
924 obj->data.s = strndup(arg, text_buffer_size);
926 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
929 #endif /* HAVE_LUA */
931 END OBJ(hddtemp, &update_hddtemp)
933 obj->data.s = strndup(arg, text_buffer_size);
935 #ifdef TCP_PORT_MONITOR
936 END OBJ_ARG(tcp_portmon, &tcp_portmon_update, "tcp_portmon: needs arguments")
937 tcp_portmon_init(obj, arg);
938 #endif /* TCP_PORT_MONITOR */
939 END OBJ(entropy_avail, &update_entropy)
940 END OBJ(entropy_perc, &update_entropy)
941 END OBJ(entropy_poolsize, &update_entropy)
942 END OBJ(entropy_bar, &update_entropy)
944 END OBJ_ARG(include, 0, "include needs a argument")
945 struct conftree *leaf = conftree_add(currentconffile, arg);
947 if (load_config_file(arg) == TRUE) {
948 obj->sub = malloc(sizeof(struct text_object));
949 currentconffile = leaf;
950 extract_variable_text_internal(obj->sub, get_global_text());
951 currentconffile = leaf->back;
953 NORM_ERR("Can't load configfile '%s'.", arg);
956 NORM_ERR("You are trying to load '%s' recursively, I'm only going to load it once to prevent an infinite loop.", arg);
958 END OBJ_ARG(blink, 0, "blink needs a argument")
959 obj->sub = malloc(sizeof(struct text_object));
960 extract_variable_text_internal(obj->sub, arg);
961 END OBJ_ARG(to_bytes, 0, "to_bytes needs a argument")
962 obj->sub = malloc(sizeof(struct text_object));
963 extract_variable_text_internal(obj->sub, arg);
965 parse_scroll_arg(obj, arg, free_at_crash);
966 END OBJ_ARG(combine, 0, "combine needs arguments: <text1> <text2>")
967 parse_combine_arg(obj, arg, free_at_crash);
969 END OBJ_ARG(nvidia, 0, "nvidia needs an argument")
970 if (set_nvidia_type(obj, arg)) {
971 CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
972 " specified: '%s'\n", arg);
976 END OBJ_ARG(apcupsd, &update_apcupsd, "apcupsd needs arguments: <host> <port>")
979 if (sscanf(arg, "%63s %d", host, &port) != 2) {
980 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
982 info.apcupsd.port = htons(port);
983 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
985 END OBJ(apcupsd_name, &update_apcupsd)
986 END OBJ(apcupsd_model, &update_apcupsd)
987 END OBJ(apcupsd_upsmode, &update_apcupsd)
988 END OBJ(apcupsd_cable, &update_apcupsd)
989 END OBJ(apcupsd_status, &update_apcupsd)
990 END OBJ(apcupsd_linev, &update_apcupsd)
991 END OBJ(apcupsd_load, &update_apcupsd)
992 END OBJ(apcupsd_loadbar, &update_apcupsd)
995 END OBJ(apcupsd_loadgraph, &update_apcupsd)
997 buf = scan_graph(obj, arg);
999 END OBJ(apcupsd_loadgauge, &update_apcupsd)
1000 scan_gauge(obj, arg);
1002 END OBJ(apcupsd_charge, &update_apcupsd)
1003 END OBJ(apcupsd_timeleft, &update_apcupsd)
1004 END OBJ(apcupsd_temp, &update_apcupsd)
1005 END OBJ(apcupsd_lastxfer, &update_apcupsd)
1006 #endif /* APCUPSD */
1010 NORM_ERR("unknown variable %s", s);
1011 obj->type = OBJ_text;
1012 snprintf(buf, 256, "${%s}", s);
1013 obj->data.s = strndup(buf, text_buffer_size);
1028 * - assumes that *string is '#'
1029 * - removes the part from '#' to the end of line ('\n' or '\0')
1030 * - it removes the '\n'
1031 * - copies the last char into 'char *last' argument, which should be a pointer
1032 * to a char rather than a string.
1034 static size_t remove_comment(char *string, char *last)
1037 while (*end != '\0' && *end != '\n') {
1040 if (last) *last = *end;
1041 if (*end == '\n') end++;
1042 strfold(string, end - string);
1043 return end - string;
1046 size_t remove_comments(char *string)
1050 for (curplace = string; *curplace != 0; curplace++) {
1051 if (*curplace == '\\' && *(curplace + 1) == '#') {
1052 // strcpy can't be used for overlapping strings
1053 strfold(curplace, 1);
1055 } else if (*curplace == '#') {
1056 folded += remove_comment(curplace, 0);
1062 int extract_variable_text_internal(struct text_object *retval, const char *const_p)
1064 struct text_object *obj;
1065 char *p, *s, *orig_p;
1067 void *ifblock_opaque = NULL;
1072 p = strndup(const_p, max_user_text - 1);
1073 while (text_contains_templates(p)) {
1075 tmp = find_and_replace_templates(p);
1081 if (strcmp(p, const_p)) {
1082 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
1084 DBGP("no templates to replace");
1087 memset(retval, 0, sizeof(struct text_object));
1089 line = global_text_lines;
1097 obj = create_plain_text(s);
1099 append_object(retval, obj);
1109 /* variable is either $foo or ${foo} */
1111 unsigned int brl = 1, brr = 0;
1115 while (*p && brl != brr) {
1130 while (*p && (isalnum((int) *p) || *p == '_')) {
1135 /* copy variable to buffer */
1136 len = (p - s > 255) ? 255 : (p - s);
1137 strncpy(buf, s, len);
1145 /* search for variable in environment */
1149 obj = create_plain_text(var);
1151 append_object(retval, obj);
1156 /* if variable wasn't found in environment, use some special */
1161 if (strchr(buf, ' ')) {
1162 arg = strchr(buf, ' ');
1165 while (isspace((int) *arg)) {
1173 /* lowercase variable name */
1176 *tmp_p = tolower(*tmp_p);
1180 obj = construct_text_object(buf, arg,
1181 line, &ifblock_opaque, orig_p);
1183 append_object(retval, obj);
1187 obj = create_plain_text("$");
1190 append_object(retval, obj);
1193 } else if (*p == '\\' && *(p+1) == '#') {
1195 } else if (*p == '#') {
1197 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
1198 /* if remove_comment removed a newline, we need to 'back up' with p */
1204 obj = create_plain_text(s);
1206 append_object(retval, obj);
1209 if (!ifblock_stack_empty(&ifblock_opaque)) {
1210 NORM_ERR("one or more $endif's are missing");
1218 * Frees the list of text objects root points to. When internal = 1, it won't
1219 * free global objects.
1221 void free_text_objects(struct text_object *root, int internal)
1223 struct text_object *obj;
1229 #define data obj->data
1230 for (obj = root->prev; obj; obj = root->prev) {
1231 root->prev = obj->prev;
1232 switch (obj->type) {
1237 #endif /* !__OpenBSD__ */
1242 free_sysfs_sensor(obj);
1244 #endif /* __linux__ */
1260 case OBJ_seen_mails:
1261 case OBJ_unseen_mails:
1262 case OBJ_flagged_mails:
1263 case OBJ_unflagged_mails:
1264 case OBJ_forwarded_mails:
1265 case OBJ_unforwarded_mails:
1266 case OBJ_replied_mails:
1267 case OBJ_unreplied_mails:
1268 case OBJ_draft_mails:
1269 case OBJ_trashed_mails:
1270 free_local_mails(obj);
1272 case OBJ_imap_unseen:
1273 case OBJ_imap_messages:
1274 case OBJ_pop3_unseen:
1280 free_text_objects(obj->sub, 1);
1283 case OBJ_if_existing:
1284 case OBJ_if_mounted:
1285 case OBJ_if_running:
1286 free(data.ifblock.s);
1287 free(data.ifblock.str);
1307 case OBJ_iconv_start:
1312 case OBJ_disk_protect:
1316 free(data.ifblock.s);
1317 free(data.ifblock.str);
1320 free_gateway_info();
1322 case OBJ_ioscheduler:
1327 #if (defined(__FreeBSD__) || defined(__linux__))
1333 case OBJ_xmms2_artist:
1334 if (info.xmms2.artist) {
1335 free(info.xmms2.artist);
1336 info.xmms2.artist = 0;
1339 case OBJ_xmms2_album:
1340 if (info.xmms2.album) {
1341 free(info.xmms2.album);
1342 info.xmms2.album = 0;
1345 case OBJ_xmms2_title:
1346 if (info.xmms2.title) {
1347 free(info.xmms2.title);
1348 info.xmms2.title = 0;
1351 case OBJ_xmms2_genre:
1352 if (info.xmms2.genre) {
1353 free(info.xmms2.genre);
1354 info.xmms2.genre = 0;
1357 case OBJ_xmms2_comment:
1358 if (info.xmms2.comment) {
1359 free(info.xmms2.comment);
1360 info.xmms2.comment = 0;
1364 if (info.xmms2.url) {
1365 free(info.xmms2.url);
1369 case OBJ_xmms2_date:
1370 if (info.xmms2.date) {
1371 free(info.xmms2.date);
1372 info.xmms2.date = 0;
1375 case OBJ_xmms2_status:
1376 if (info.xmms2.status) {
1377 free(info.xmms2.status);
1378 info.xmms2.status = 0;
1381 case OBJ_xmms2_playlist:
1382 if (info.xmms2.playlist) {
1383 free(info.xmms2.playlist);
1384 info.xmms2.playlist = 0;
1387 case OBJ_xmms2_smart:
1388 if (info.xmms2.artist) {
1389 free(info.xmms2.artist);
1390 info.xmms2.artist = 0;
1392 if (info.xmms2.title) {
1393 free(info.xmms2.title);
1394 info.xmms2.title = 0;
1396 if (info.xmms2.url) {
1397 free(info.xmms2.url);
1403 case OBJ_bmpx_title:
1404 case OBJ_bmpx_artist:
1405 case OBJ_bmpx_album:
1406 case OBJ_bmpx_track:
1408 case OBJ_bmpx_bitrate:
1423 rss_free_obj_info(obj);
1432 case OBJ_weather_forecast:
1446 #endif /* HAVE_LUA */
1453 case OBJ_battery_short:
1456 case OBJ_battery_time:
1459 #endif /* !__OpenBSD__ */
1465 case OBJ_execigraph:
1466 case OBJ_execigauge:
1470 case OBJ_nameserver:
1479 free_top(obj, internal);
1489 #endif /* HDDTEMP */
1490 case OBJ_entropy_avail:
1491 case OBJ_entropy_perc:
1492 case OBJ_entropy_poolsize:
1493 case OBJ_entropy_bar:
1495 case OBJ_user_names:
1496 if (info.users.names) {
1497 free(info.users.names);
1498 info.users.names = 0;
1501 case OBJ_user_terms:
1502 if (info.users.terms) {
1503 free(info.users.terms);
1504 info.users.terms = 0;
1507 case OBJ_user_times:
1508 if (info.users.times) {
1509 free(info.users.times);
1510 info.users.times = 0;
1515 case OBJ_smapi_bat_perc:
1516 case OBJ_smapi_bat_temp:
1517 case OBJ_smapi_bat_power:
1520 case OBJ_if_smapi_bat_installed:
1521 free(data.ifblock.s);
1522 free(data.ifblock.str);
1532 case OBJ_mpd_artist:
1534 case OBJ_mpd_random:
1535 case OBJ_mpd_repeat:
1537 case OBJ_mpd_bitrate:
1538 case OBJ_mpd_status:
1540 case OBJ_mpd_elapsed:
1541 case OBJ_mpd_length:
1545 case OBJ_mpd_percent:
1547 case OBJ_if_mpd_playing:
1555 case OBJ_moc_artist:
1558 case OBJ_moc_totaltime:
1559 case OBJ_moc_timeleft:
1560 case OBJ_moc_curtime:
1561 case OBJ_moc_bitrate:
1570 free_text_objects(obj->sub, 1);
1582 case OBJ_apcupsd_name:
1583 case OBJ_apcupsd_model:
1584 case OBJ_apcupsd_upsmode:
1585 case OBJ_apcupsd_cable:
1586 case OBJ_apcupsd_status:
1587 case OBJ_apcupsd_linev:
1588 case OBJ_apcupsd_load:
1589 case OBJ_apcupsd_loadbar:
1591 case OBJ_apcupsd_loadgraph:
1592 case OBJ_apcupsd_loadgauge:
1594 case OBJ_apcupsd_charge:
1595 case OBJ_apcupsd_timeleft:
1596 case OBJ_apcupsd_temp:
1597 case OBJ_apcupsd_lastxfer:
1599 #endif /* APCUPSD */
1600 #ifdef TCP_PORT_MONITOR
1601 case OBJ_tcp_portmon:
1602 tcp_portmon_free(obj);
1604 #endif /* TCP_PORT_MONITOR */
1607 case OBJ_desktop_number:
1608 case OBJ_desktop_name:
1609 if(info.x11.desktop.name && !internal) {
1610 free(info.x11.desktop.name);
1611 info.x11.desktop.name = NULL;
1613 if(info.x11.desktop.all_names && !internal) {
1614 free(info.x11.desktop.all_names);
1615 info.x11.desktop.all_names = NULL;