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