Fix device name parsing for diskio variables.
[monky] / src / core.c
index c0f5cd5..acb41b0 100644 (file)
@@ -10,7 +10,7 @@
  * Please see COPYING for details
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -38,6 +38,7 @@
 #include "diskio.h"
 #include "entropy.h"
 #include "exec.h"
+#include "i8k.h"
 #include "proc.h"
 #ifdef X11
 #include "fonts.h"
@@ -152,14 +153,28 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                obj->data.l = get_x11_color(s);
        } else
 #endif /* X11 */
-#ifdef __OpenBSD__
-       OBJ(freq, 0)
-#else
+#ifndef __OpenBSD__
        OBJ(acpitemp, 0)
                obj->data.i = open_acpi_temperature(arg);
        END OBJ(acpiacadapter, 0)
-       END OBJ(freq, 0)
+               if(arg) {
+#ifdef __linux__
+                       if(strpbrk(arg, "/.") != NULL) {
+                               /* 
+                                * a bit of paranoia. screen out funky paths
+                                * i hope no device will have a '.' in its name
+                                */
+                               NORM_ERR("acpiacadapter: arg must not contain '/' or '.'");
+                       } else 
+                               obj->data.opaque = strdup(arg);
+#else
+                       NORM_ERR("acpiacadapter: arg is only used on linux");
+#endif
+               }
+               if(! obj->data.opaque)
+                       obj->data.opaque = strdup("AC");
 #endif /* !__OpenBSD__ */
+       END OBJ(freq, 0)
                get_cpu_count();
                if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
                                || atoi(&arg[0]) > info.cpu_count) {
@@ -338,12 +353,10 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
        END OBJ(cpu, &update_cpu_usage)
                SCAN_CPU(arg, obj->data.i);
                DBGP2("Adding $cpu for CPU %d", obj->data.i);
-#ifdef X11
        END OBJ(cpugauge, &update_cpu_usage)
                SCAN_CPU(arg, obj->data.i);
                scan_gauge(obj, arg);
                DBGP2("Adding $cpugauge for CPU %d", obj->data.i);
-#endif /* X11 */
        END OBJ(cpubar, &update_cpu_usage)
                SCAN_CPU(arg, obj->data.i);
                scan_bar(obj, arg);
@@ -359,18 +372,18 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                scan_loadgraph_arg(obj, arg);
 #endif /* X11 */
        END OBJ(diskio, &update_diskio)
-               parse_diskio_arg(obj, arg);
+               parse_diskio_arg(obj, dev_name(arg));
        END OBJ(diskio_read, &update_diskio)
-               parse_diskio_arg(obj, arg);
+               parse_diskio_arg(obj, dev_name(arg));
        END OBJ(diskio_write, &update_diskio)
-               parse_diskio_arg(obj, arg);
+               parse_diskio_arg(obj, dev_name(arg));
 #ifdef X11
        END OBJ(diskiograph, &update_diskio)
-               parse_diskiograph_arg(obj, arg);
+               parse_diskiograph_arg(obj, dev_name(arg));
        END OBJ(diskiograph_read, &update_diskio)
-               parse_diskiograph_arg(obj, arg);
+               parse_diskiograph_arg(obj, dev_name(arg));
        END OBJ(diskiograph_write, &update_diskio)
-               parse_diskiograph_arg(obj, arg);
+               parse_diskiograph_arg(obj, dev_name(arg));
 #endif /* X11 */
        END OBJ(color, 0)
 #ifdef X11
@@ -438,8 +451,14 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                obj->data.s = scan_font(arg);
 #endif /* X11 */
        END OBJ(conky_version, 0)
+               obj->type = OBJ_text;
+               obj->data.s = strdup(VERSION);
        END OBJ(conky_build_date, 0)
+               obj->type = OBJ_text;
+               obj->data.s = strdup(BUILD_DATE);
        END OBJ(conky_build_arch, 0)
+               obj->type = OBJ_text;
+               obj->data.s = strdup(BUILD_ARCH);
        END OBJ(downspeed, &update_net_stats)
                parse_net_stat_arg(obj, arg, free_at_crash);
        END OBJ(downspeedf, &update_net_stats)
@@ -464,9 +483,9 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                scan_exec_arg(obj, arg);
        END OBJ(execbar, 0)
                scan_exec_arg(obj, arg);
-#ifdef X11
        END OBJ(execgauge, 0)
                scan_exec_arg(obj, arg);
+#ifdef X11
        END OBJ(execgraph, 0)
                scan_execgraph_arg(obj, arg);
 #endif /* X11 */
@@ -475,9 +494,9 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
 #ifdef X11
        END OBJ_ARG(execigraph, 0, "execigraph needs arguments")
                scan_execgraph_arg(obj, arg);
+#endif /* X11 */
        END OBJ_ARG(execigauge, 0, "execigauge needs arguments")
                scan_execi_arg(obj, arg);
-#endif /* X11 */
        END OBJ_ARG(execi, 0, "execi needs arguments")
                scan_execi_arg(obj, arg);
        END OBJ_ARG(execpi, 0, "execpi needs arguments")
@@ -564,9 +583,9 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                obj->data.s = strndup(arg, text_buffer_size);
 #else
        END OBJ_IF_ARG(if_running, 0, "if_running needs an argument")
-               char buf[256];
+               char buf[text_buffer_size];
 
-               snprintf(buf, 256, "pidof %s >/dev/null", arg);
+               snprintf(buf, text_buffer_size, "pidof %s >/dev/null", arg);
                obj->data.s = strndup(buf, text_buffer_size);
 #endif
        END OBJ(kernel, 0)
@@ -602,10 +621,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
        END OBJ(memfree, &update_meminfo)
        END OBJ(memmax, &update_meminfo)
        END OBJ(memperc, &update_meminfo)
-#ifdef X11
        END OBJ(memgauge, &update_meminfo)
                scan_gauge(obj, arg);
-#endif /* X11*/
        END OBJ(membar, &update_meminfo)
                scan_bar(obj, arg);
 #ifdef X11
@@ -621,14 +638,12 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                parse_mixer_arg(obj, arg);
        END OBJ(mixerr, 0)
                parse_mixer_arg(obj, arg);
-#ifdef X11
        END OBJ(mixerbar, 0)
                scan_mixer_bar(obj, arg);
        END OBJ(mixerlbar, 0)
                scan_mixer_bar(obj, arg);
        END OBJ(mixerrbar, 0)
                scan_mixer_bar(obj, arg);
-#endif
        END OBJ_IF(if_mixer_mute, 0)
                parse_mixer_arg(obj, arg);
 #ifdef X11
@@ -868,7 +883,6 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                obj->data.s = strndup(arg, text_buffer_size);
        END OBJ_ARG(smapi_bat_power, 0, "smapi_bat_power needs an argument")
                obj->data.s = strndup(arg, text_buffer_size);
-#ifdef X11
        END OBJ_ARG(smapi_bat_bar, 0, "smapi_bat_bar needs an argument")
                int cnt;
                if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
@@ -876,7 +890,6 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                        obj->data.i = -1;
                } else
                        arg = scan_bar(obj, arg + cnt);
-#endif /* X11 */
 #endif /* IBM */
 #ifdef MPD
 #define mpd_set_maxlen(name) \
@@ -953,10 +966,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
        END OBJ(xmms2_size, &update_xmms2)
        END OBJ(xmms2_status, &update_xmms2)
        END OBJ(xmms2_percent, &update_xmms2)
-#ifdef X11
        END OBJ(xmms2_bar, &update_xmms2)
                scan_bar(obj, arg);
-#endif /* X11 */
        END OBJ(xmms2_smart, &update_xmms2)
        END OBJ(xmms2_playlist, &update_xmms2)
        END OBJ(xmms2_timesplayed, &update_xmms2)
@@ -982,10 +993,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
        END OBJ(audacious_playlist_length, &update_audacious)
        END OBJ(audacious_playlist_position, &update_audacious)
        END OBJ(audacious_main_volume, &update_audacious)
-#ifdef X11
        END OBJ(audacious_bar, &update_audacious)
                scan_bar(obj, arg);
-#endif /* X11 */
 #endif
 #ifdef BMPX
        END OBJ(bmpx_title, &update_bmpx)
@@ -1042,6 +1051,7 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                } else {
                        CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
                }
+#endif /* X11 */
        END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]")
                arg = scan_gauge(obj, arg);
                if (arg) {
@@ -1049,7 +1059,6 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                } else {
                        CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
                }
-#endif /* X11 */
 #endif /* HAVE_LUA */
 #ifdef HDDTEMP
        END OBJ(hddtemp, &update_hddtemp)
@@ -1120,20 +1129,20 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
                char* buf = 0;
                buf = scan_graph(obj, arg, 0);
                if (buf) free(buf);
+#endif /* X11 */
        END OBJ(apcupsd_loadgauge, &update_apcupsd)
                scan_gauge(obj, arg);
-#endif /* X11 */
        END OBJ(apcupsd_charge, &update_apcupsd)
        END OBJ(apcupsd_timeleft, &update_apcupsd)
        END OBJ(apcupsd_temp, &update_apcupsd)
        END OBJ(apcupsd_lastxfer, &update_apcupsd)
 #endif /* APCUPSD */
        END {
-               char buf[256];
+               char buf[text_buffer_size];
 
                NORM_ERR("unknown variable %s", s);
                obj->type = OBJ_text;
-               snprintf(buf, 256, "${%s}", s);
+               snprintf(buf, text_buffer_size, "${%s}", s);
                obj->data.s = strndup(buf, text_buffer_size);
        }
 #undef OBJ
@@ -1203,9 +1212,9 @@ int extract_variable_text_internal(struct text_object *retval, const char *const
        s = orig_p = p;
 
        if (strcmp(p, const_p)) {
-               DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
+               DBGP2("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
        } else {
-               DBGP("no templates to replace");
+               DBGP2("no templates to replace");
        }
 
        memset(retval, 0, sizeof(struct text_object));
@@ -1227,7 +1236,7 @@ int extract_variable_text_internal(struct text_object *retval, const char *const
                        s = p;
 
                        if (*p != '$') {
-                               char buf[256];
+                               char buf[text_buffer_size];
                                const char *var;
 
                                /* variable is either $foo or ${foo} */
@@ -1257,7 +1266,7 @@ int extract_variable_text_internal(struct text_object *retval, const char *const
                                }
 
                                /* copy variable to buffer */
-                               len = (p - s > 255) ? 255 : (p - s);
+                               len = ((unsigned int) (p - s) > text_buffer_size - 1) ? text_buffer_size - 1 : (unsigned int) (p - s);
                                strncpy(buf, s, len);
                                buf[len] = '\0';
 
@@ -1358,6 +1367,9 @@ void free_text_objects(struct text_object *root, int internal)
                        case OBJ_acpitemp:
                                close(data.i);
                                break;
+                       case OBJ_acpiacadapter:
+                               free(data.opaque);
+                               break;
 #endif /* !__OpenBSD__ */
 #ifdef __linux__
                        case OBJ_i2c:
@@ -1477,8 +1489,8 @@ void free_text_objects(struct text_object *root, int internal)
                                break;
                        case OBJ_exec:
                        case OBJ_execbar:
-#ifdef X11
                        case OBJ_execgauge:
+#ifdef X11
                        case OBJ_execgraph:
 #endif
                        case OBJ_execp:
@@ -1618,8 +1630,8 @@ void free_text_objects(struct text_object *root, int internal)
                        case OBJ_lua_bar:
 #ifdef X11
                        case OBJ_lua_graph:
-                       case OBJ_lua_gauge:
 #endif /* X11 */
+                       case OBJ_lua_gauge:
                                free(data.s);
                                break;
 #endif /* HAVE_LUA */
@@ -1642,8 +1654,8 @@ void free_text_objects(struct text_object *root, int internal)
                        case OBJ_texeci:
 #ifdef X11
                        case OBJ_execigraph:
-                       case OBJ_execigauge:
 #endif /* X11 */
+                       case OBJ_execigauge:
                                free_execi(obj);
                                break;
                        case OBJ_nameserver:
@@ -1778,8 +1790,8 @@ void free_text_objects(struct text_object *root, int internal)
                        case OBJ_apcupsd_loadbar:
 #ifdef X11
                        case OBJ_apcupsd_loadgraph:
-                       case OBJ_apcupsd_loadgauge:
 #endif /* X11 */
+                       case OBJ_apcupsd_loadgauge:
                        case OBJ_apcupsd_charge:
                        case OBJ_apcupsd_timeleft:
                        case OBJ_apcupsd_temp: