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