/ * Copyright (c) 2002 Pedro Bastos * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the name of his contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY PEDRO BASTOS AND HIS CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * */ #include #include #include #include #include "machmon.h" #include "conf.h" #include "extern.h" /* * Note: the structure below also defines the order that data will be displayed * on the screen, there's a function to make it customisable right away. */ struct _conf_param conf_param[] = { { "opsys" , OP_F, d_opsys }, { "date" , DA_F, d_date }, { "uptime" , UP_F, d_uptime }, { "load" , LO_F, d_load }, { "users" , US_F, d_users }, { "proc" , PR_F, d_procs }, { "mem" , ME_F, d_mbufs }, { "ifs" , IF_F, d_ifs }, { NULL , NULL, NULL }, }; static void conf_parse(char *, unsigned short int); int get_conf(char *file) { char *buf, *dbuf; FILE *conf_file; size_t l_len; if ((conf_file = (FILE *) fopen(file, "r")) == NULL) return (1); while ((buf = fgetln(conf_file, &l_len))) { if (buf[l_len - 1] == '\n') buf[--l_len] = '\0'; /* null terminate string */ else { dbuf = (char *) malloc(l_len + 1); if (dbuf == NULL) err(1, "malloc"); STRING_COPY(dbuf, buf, l_len); buf = dbuf; } if (buf[0] != '#') { /* debug_inf_w("parsing %s\n", *buf); */ conf_parse(buf, l_len); /* parse what we got */ } } (void) fclose(conf_file); return (1); } static void conf_parse(char *parse_buff, unsigned short int len) { char *argv[len >> 1]; /* assume that because of delimiters, this is * the maximal amount of tokens? */ char **token = NULL; struct _conf_param *confs; short int parse_c = 0; int n; if (len == 1) return; token = argv; while (token < &argv[len -1] && (*token = strsep(&parse_buff, " ")) != NULL) { if (**token != '\0') { token++; parse_c++; } } for (confs = conf_param; confs->refer_name != NULL; confs++) { if (!strcmp(argv[0], confs->refer_name)) { if (xatoi(argv[1], &n) == -1) errx(1, "%s: not a number", argv[1]); if (n == 1) ADD_FLAG(run_flags, confs->bitmask); else if (n == 0) REMOVE_FLAG(run_flags, confs->bitmask); } } return; }