Fix device name parsing for diskio variables.
[monky] / src / core.c
index e174174..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.
  *
@@ -153,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) {
@@ -358,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
@@ -569,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)
@@ -1124,11 +1138,11 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
        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
@@ -1198,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));
@@ -1222,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} */
@@ -1252,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';
 
@@ -1353,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: