Home | History | Annotate | Line # | Download | only in systat
cmds.c revision 1.11
      1  1.11     jwise /*	$NetBSD: cmds.c,v 1.11 1999/12/16 04:02:22 jwise Exp $	*/
      2   1.2       jtc 
      3   1.1       jtc /*-
      4   1.1       jtc  * Copyright (c) 1980, 1992, 1993
      5   1.1       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       jtc  *
      7   1.1       jtc  * Redistribution and use in source and binary forms, with or without
      8   1.1       jtc  * modification, are permitted provided that the following conditions
      9   1.1       jtc  * are met:
     10   1.1       jtc  * 1. Redistributions of source code must retain the above copyright
     11   1.1       jtc  *    notice, this list of conditions and the following disclaimer.
     12   1.1       jtc  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       jtc  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       jtc  *    documentation and/or other materials provided with the distribution.
     15   1.1       jtc  * 3. All advertising materials mentioning features or use of this software
     16   1.1       jtc  *    must display the following acknowledgement:
     17   1.1       jtc  *	This product includes software developed by the University of
     18   1.1       jtc  *	California, Berkeley and its contributors.
     19   1.1       jtc  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       jtc  *    may be used to endorse or promote products derived from this software
     21   1.1       jtc  *    without specific prior written permission.
     22   1.1       jtc  *
     23   1.1       jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       jtc  * SUCH DAMAGE.
     34   1.1       jtc  */
     35   1.1       jtc 
     36   1.7       mrg #include <sys/cdefs.h>
     37   1.1       jtc #ifndef lint
     38   1.2       jtc #if 0
     39   1.3       jtc static char sccsid[] = "@(#)cmds.c	8.2 (Berkeley) 4/29/95";
     40   1.2       jtc #endif
     41  1.11     jwise __RCSID("$NetBSD: cmds.c,v 1.11 1999/12/16 04:02:22 jwise Exp $");
     42   1.1       jtc #endif /* not lint */
     43   1.1       jtc 
     44   1.1       jtc #include <stdlib.h>
     45   1.1       jtc #include <unistd.h>
     46   1.1       jtc #include <signal.h>
     47   1.1       jtc #include <ctype.h>
     48   1.1       jtc #include <string.h>
     49   1.1       jtc #include "systat.h"
     50   1.1       jtc #include "extern.h"
     51   1.1       jtc 
     52   1.1       jtc void
     53   1.1       jtc command(cmd)
     54   1.5    scottr 	char *cmd;
     55   1.1       jtc {
     56  1.11     jwise 	struct command *c;
     57  1.11     jwise 	struct mode *p;
     58   1.8     lukem 	char *cp;
     59   1.5    scottr 	int interval;
     60   1.5    scottr 	sigset_t set;
     61   1.5    scottr 
     62   1.5    scottr 	sigemptyset(&set);
     63   1.5    scottr 	sigaddset(&set, SIGALRM);
     64   1.5    scottr 	sigprocmask(SIG_BLOCK, &set, NULL);
     65   1.9  christos 	for (cp = cmd; *cp && !isspace((unsigned char)*cp); cp++)
     66   1.5    scottr 		;
     67   1.5    scottr 	if (*cp)
     68   1.5    scottr 		*cp++ = '\0';
     69   1.1       jtc 	if (*cmd == '\0')
     70   1.1       jtc 		return;
     71   1.9  christos 	for (; *cp && isspace((unsigned char)*cp); cp++)
     72   1.1       jtc 		;
     73   1.1       jtc 
     74  1.11     jwise 	for (c = global_commands; c->c_name; c++) {
     75  1.11     jwise 		if (strcmp(cmd, c->c_name) == 0) {
     76  1.11     jwise 			(c->c_cmd)();
     77  1.11     jwise 			goto done;
     78   1.1       jtc 		}
     79   1.1       jtc 	}
     80  1.11     jwise 
     81   1.1       jtc 	interval = atoi(cmd);
     82   1.5    scottr 	if (interval <= 0 &&
     83   1.1       jtc 	    (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
     84   1.1       jtc 		interval = *cp ? atoi(cp) : naptime;
     85   1.5    scottr 		if (interval <= 0) {
     86   1.1       jtc 			error("%d: bad interval.", interval);
     87   1.1       jtc 			goto done;
     88   1.5    scottr 		}
     89   1.1       jtc 	}
     90   1.1       jtc 	if (interval > 0) {
     91   1.5    scottr 		alarm(0);
     92   1.5    scottr 		naptime = interval;
     93   1.5    scottr 		display(0);
     94   1.5    scottr 		status();
     95   1.1       jtc 		goto done;
     96   1.5    scottr 	}
     97   1.1       jtc 	p = lookup(cmd);
     98  1.11     jwise 	if (p == (struct mode *)-1) {
     99   1.6  matthias 		error("%s: Ambiguous command.", cmd);
    100   1.1       jtc 		goto done;
    101   1.1       jtc 	}
    102   1.5    scottr 	if (p) {
    103  1.11     jwise 		if (curmode == p)
    104   1.1       jtc 			goto done;
    105   1.5    scottr 		alarm(0);
    106  1.11     jwise 		(*curmode->c_close)(wnd);
    107   1.1       jtc 		wnd = (*p->c_open)();
    108   1.1       jtc 		if (wnd == 0) {
    109   1.1       jtc 			error("Couldn't open new display");
    110  1.11     jwise 			wnd = (*curmode->c_open)();
    111   1.1       jtc 			if (wnd == 0) {
    112   1.1       jtc 				error("Couldn't change back to previous cmd");
    113   1.1       jtc 				exit(1);
    114   1.1       jtc 			}
    115  1.11     jwise 			p = curmode;
    116   1.1       jtc 		}
    117   1.1       jtc 		if ((p->c_flags & CF_INIT) == 0) {
    118   1.1       jtc 			if ((*p->c_init)())
    119   1.1       jtc 				p->c_flags |= CF_INIT;
    120   1.1       jtc 			else
    121   1.1       jtc 				goto done;
    122   1.1       jtc 		}
    123  1.11     jwise 		curmode = p;
    124   1.1       jtc 		labels();
    125   1.5    scottr 		display(0);
    126   1.5    scottr 		status();
    127   1.1       jtc 		goto done;
    128   1.5    scottr 	}
    129  1.11     jwise 	if (curmode->c_cmd == 0 || !(*curmode->c_cmd)(cmd, cp))
    130   1.6  matthias 		error("%s: Unknown command.", cmd);
    131   1.1       jtc done:
    132   1.5    scottr 	sigprocmask(SIG_UNBLOCK, &set, NULL);
    133   1.1       jtc }
    134   1.1       jtc 
    135  1.11     jwise struct mode *
    136   1.1       jtc lookup(name)
    137   1.8     lukem 	char *name;
    138   1.1       jtc {
    139   1.8     lukem 	char *p, *q;
    140  1.11     jwise 	struct mode *c, *found;
    141   1.8     lukem 	int nmatches, longest;
    142   1.1       jtc 
    143   1.1       jtc 	longest = 0;
    144   1.1       jtc 	nmatches = 0;
    145  1.11     jwise 	found = (struct mode *) 0;
    146  1.11     jwise 	for (c = modes; (p = c->c_name); c++) {
    147   1.1       jtc 		for (q = name; *q == *p++; q++)
    148   1.1       jtc 			if (*q == 0)		/* exact match? */
    149   1.1       jtc 				return (c);
    150   1.1       jtc 		if (!*q) {			/* the name was a prefix */
    151   1.1       jtc 			if (q - name > longest) {
    152   1.1       jtc 				longest = q - name;
    153   1.1       jtc 				nmatches = 1;
    154   1.1       jtc 				found = c;
    155   1.1       jtc 			} else if (q - name == longest)
    156   1.1       jtc 				nmatches++;
    157   1.1       jtc 		}
    158   1.1       jtc 	}
    159   1.6  matthias 	if (nmatches > 1)
    160  1.11     jwise 		return ((struct mode *)-1);
    161   1.1       jtc 	return (found);
    162   1.1       jtc }
    163   1.1       jtc 
    164   1.1       jtc void
    165   1.1       jtc status()
    166   1.1       jtc {
    167  1.11     jwise 	error("Showing %s, refresh every %d seconds.", curmode->c_name, naptime);
    168   1.1       jtc }
    169   1.1       jtc 
    170  1.10  sommerfe /* case insensitive prefix comparison */
    171   1.1       jtc int
    172   1.1       jtc prefix(s1, s2)
    173   1.8     lukem 	char *s1, *s2;
    174   1.1       jtc {
    175  1.10  sommerfe 	char c1, c2;
    176   1.1       jtc 
    177  1.10  sommerfe 	while (1) {
    178  1.10  sommerfe 		c1 = *s1 >= 'A' && *s1 <= 'Z' ? *s1 + 'a' - 'A' : *s1;
    179  1.10  sommerfe 		c2 = *s2 >= 'A' && *s2 <= 'Z' ? *s2 + 'a' - 'A' : *s2;
    180  1.10  sommerfe 		if (c1 != c2)
    181  1.10  sommerfe 			break;
    182  1.10  sommerfe 		if (c1 == '\0')
    183   1.5    scottr 			return (1);
    184   1.5    scottr 		s1++, s2++;
    185   1.5    scottr 	}
    186   1.5    scottr 	return (*s1 == '\0');
    187   1.1       jtc }
    188