Annotation of embedaddon/bmon/src/itemtab.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * itemtab.c           Item Tab file 
                      3:  *
                      4:  * Copyright (c) 2001-2005 Thomas Graf <tgraf@suug.ch>
                      5:  *
                      6:  * Permission is hereby granted, free of charge, to any person obtaining a
                      7:  * copy of this software and associated documentation files (the "Software"),
                      8:  * to deal in the Software without restriction, including without limitation
                      9:  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
                     10:  * and/or sell copies of the Software, and to permit persons to whom the
                     11:  * Software is furnished to do so, subject to the following conditions:
                     12:  *
                     13:  * The above copyright notice and this permission notice shall be included
                     14:  * in all copies or substantial portions of the Software.
                     15:  *
                     16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
                     17:  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     18:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
                     19:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     20:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
                     21:  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
                     22:  * DEALINGS IN THE SOFTWARE.
                     23:  */
                     24: 
                     25: #include <bmon/bmon.h>
                     26: #include <bmon/conf.h>
                     27: #include <bmon/item.h>
                     28: #include <bmon/node.h>
                     29: #include <bmon/itemtab.h>
                     30: #include <bmon/utils.h>
                     31: 
                     32: static struct it_item *items;
                     33: 
                     34: static inline struct it_item *find_item(const char *name, const char *parent)
                     35: {
                     36:        struct it_item *i;
                     37: 
                     38:        for (i = items; i; i = i->ii_next)
                     39:                if (!strcmp(name, i->ii_name) &&
                     40:                    !strcmp(parent, i->ii_parent))
                     41:                    return i;
                     42: 
                     43:        return NULL;
                     44: }
                     45: 
                     46: static void parse_option(struct it_item *item, char *opt)
                     47: {
                     48:        char *value = strchr(opt, '=');
                     49: 
                     50:        if (value) {
                     51:                *value = '\0';
                     52:                value++;
                     53:        }
                     54: 
                     55:        if (!strcasecmp(opt, "rxmax") && value)
                     56:                item->ii_rx_max = parse_size(value);
                     57:        else if (!strcasecmp(opt, "txmax") && value)
                     58:                item->ii_tx_max = parse_size(value);
                     59:        else if (!strcasecmp(opt, "max") && value) {
                     60:                item->ii_rx_max = parse_size(value);
                     61:                item->ii_tx_max = item->ii_rx_max;
                     62:        } else if (!strcasecmp(opt, "desc") && value)
                     63:                item->ii_desc = strdup(value);
                     64: }
                     65: 
                     66: static void parse_itemtab(struct it_item *item, char *opts)
                     67: {
                     68:        char *next, *current = opts;
                     69:        
                     70:        do {
                     71:                next = strchr(current, ',');
                     72:                if (next) {
                     73:                        *next = '\0';
                     74:                        ++next;
                     75:                }
                     76: 
                     77:                if (*current)
                     78:                        parse_option(item, current);
                     79:                current = next;
                     80:        } while (next);
                     81: }
                     82: 
                     83: 
                     84: static inline void parse_itemtab_line(char *data)
                     85: {
                     86:        char *buf = strdup(data);
                     87:        char *p, *item_name = buf, *parent_name;
                     88:        struct it_item *item;
                     89: 
                     90:        if (buf[strlen(buf) - 1] == '\n')
                     91:                buf[strlen(buf) - 1] = '\0';
                     92: 
                     93:        /* jump over item */
                     94:        for (p = buf; *p != ' ' && *p != '\t' && *p != '\0'; p++);
                     95:        if (*p == '\0')
                     96:                goto skip;
                     97:        *p = '\0';
                     98: 
                     99:        /* jump over gap to parent */
                    100:        for (++p; *p == ' ' || *p == '\t'; p++);
                    101:        if (*p == '\0')
                    102:                goto skip;
                    103:        parent_name = p;
                    104: 
                    105:        /* jump over parent */
                    106:        for (; *p != ' ' && *p != '\t' && *p != '\0'; p++);
                    107:        if (*p == '\0')
                    108:                goto skip;
                    109:        *p = '\0';
                    110: 
                    111:        /* jump over gap to options */
                    112:        for (++p; *p == ' ' || *p == '\t'; p++);
                    113:        if (*p == '\0')
                    114:                goto skip;
                    115: 
                    116:        item = find_item(item_name, parent_name);
                    117:        if (item != NULL)
                    118:                goto skip;
                    119: 
                    120:        item = xcalloc(1, sizeof(*item));
                    121:        item->ii_name = strdup(item_name);
                    122:        item->ii_parent = strdup(parent_name);
                    123:        item->ii_next = items;
                    124:        items = item;
                    125: 
                    126:        parse_itemtab(item, p);
                    127: 
                    128: skip:
                    129:        free(buf);
                    130: }
                    131: 
                    132: static void itemtab_read(const char *path)
                    133: {
                    134:        FILE *f = fopen(path, "r");
                    135:        char *p, buf[1024];
                    136:        
                    137:        if (f == NULL)
                    138:                return;
                    139: 
                    140:        while (fgets(buf, sizeof(buf), f)) {
                    141:                if ('#' == *buf)
                    142:                        continue;
                    143: 
                    144:                if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n')))
                    145:                        *p = '\0';
                    146: 
                    147:                if (*buf)
                    148:                        parse_itemtab_line(buf);
                    149:        }
                    150: 
                    151:        fclose(f);
                    152: }
                    153: 
                    154: void read_itemtab(void)
                    155: {
                    156:        itemtab_read(get_itemtab());
                    157: }
                    158: 
                    159: struct it_item *lookup_tab(item_t *it)
                    160: {
                    161:        item_t *parent = NULL;
                    162: 
                    163:        if (!(it->i_flags & ITEM_FLAG_LOCAL))
                    164:                return NULL;
                    165: 
                    166:        if (it->i_flags & ITEM_FLAG_IS_CHILD) {
                    167:                parent = get_item(it->i_node, it->i_parent);
                    168:                if (parent == NULL)
                    169:                        return NULL;
                    170:        }
                    171:        
                    172:        return find_item(it->i_name, parent ? parent->i_name : "<none>");
                    173: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>