Home | History | Annotate | Line # | Download | only in systat
globalcmds.c revision 1.9
      1  1.9  simonb /*	$NetBSD: globalcmds.c,v 1.9 2000/12/01 02:19:43 simonb Exp $ */
      2  1.1   jwise 
      3  1.1   jwise /*-
      4  1.4   jwise  * Copyright (c) 1999
      5  1.4   jwise  *      The NetBSD Foundation, Inc.  All rights reserved.
      6  1.1   jwise  *
      7  1.1   jwise  * Redistribution and use in source and binary forms, with or without
      8  1.1   jwise  * modification, are permitted provided that the following conditions
      9  1.1   jwise  * are met:
     10  1.1   jwise  * 1. Redistributions of source code must retain the above copyright
     11  1.1   jwise  *    notice, this list of conditions and the following disclaimer.
     12  1.1   jwise  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   jwise  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   jwise  *    documentation and/or other materials provided with the distribution.
     15  1.4   jwise  * 3. All advertising materials mentioning features or use of this software
     16  1.4   jwise  *    must display the following acknowledgement:
     17  1.4   jwise  *      This product includes software developed by the NetBSD Foundation.
     18  1.4   jwise  * 4. Neither the name of the Foundation nor the names of its contributors
     19  1.4   jwise  *    may be used to endorse or promote products derived from this software
     20  1.4   jwise  *    without specific prior written permission.
     21  1.1   jwise  *
     22  1.1   jwise  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.1   jwise  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1   jwise  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1   jwise  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.1   jwise  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1   jwise  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1   jwise  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1   jwise  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1   jwise  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1   jwise  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1   jwise  * SUCH DAMAGE.
     33  1.1   jwise  */
     34  1.1   jwise 
     35  1.8      ad #include <sys/cdefs.h>
     36  1.8      ad #ifndef lint
     37  1.9  simonb __RCSID("$NetBSD: globalcmds.c,v 1.9 2000/12/01 02:19:43 simonb Exp $");
     38  1.8      ad #endif /* not lint */
     39  1.8      ad 
     40  1.1   jwise #include <curses.h>
     41  1.1   jwise #include <stdlib.h>
     42  1.5   lukem #include <string.h>
     43  1.1   jwise #include <unistd.h>
     44  1.9  simonb 
     45  1.1   jwise #include "systat.h"
     46  1.1   jwise #include "extern.h"
     47  1.1   jwise 
     48  1.8      ad static char *shortname(const char *, const char *);
     49  1.6  itojun 
     50  1.6  itojun static char *
     51  1.8      ad shortname(const char *key, const char *s)
     52  1.6  itojun {
     53  1.6  itojun 	char *p, *q;
     54  1.6  itojun 	size_t l;
     55  1.6  itojun 
     56  1.6  itojun 	if (key == NULL) {
     57  1.6  itojun 		if ((p = strdup(s)) == NULL)
     58  1.6  itojun 			return NULL;
     59  1.6  itojun 		q = strchr(p, '.');
     60  1.6  itojun 		if (q && strlen(q) > 1) {
     61  1.6  itojun 			q[1] = '*';
     62  1.6  itojun 			q[2] = '\0';
     63  1.6  itojun 		}
     64  1.6  itojun 		return p;
     65  1.6  itojun 	} else if (strncmp(key, s, l = strlen(key)) == 0 && s[l] == '.') {
     66  1.6  itojun 		p = strdup(s + l + 1);
     67  1.6  itojun 		return p;
     68  1.6  itojun 	} else
     69  1.6  itojun 		return NULL;
     70  1.6  itojun }
     71  1.6  itojun 
     72  1.1   jwise void
     73  1.8      ad global_help(char *args)
     74  1.1   jwise {
     75  1.1   jwise 	int col, len;
     76  1.1   jwise 	struct mode *p;
     77  1.6  itojun 	char *cur, *prev;
     78  1.1   jwise 
     79  1.1   jwise 	move(CMDLINE, col = 0);
     80  1.6  itojun 	cur = prev = NULL;
     81  1.1   jwise 	for (p = modes; p->c_name; p++) {
     82  1.6  itojun 		if ((cur = shortname(args, p->c_name)) == NULL)
     83  1.6  itojun 			continue;
     84  1.6  itojun 		if (cur && prev && strcmp(cur, prev) == 0) {
     85  1.6  itojun 			free(cur);
     86  1.7  itojun 			cur = NULL;
     87  1.6  itojun 			continue;
     88  1.6  itojun 		}
     89  1.6  itojun 		len = strlen(cur);
     90  1.1   jwise 		if (col + len > COLS)
     91  1.1   jwise 			break;
     92  1.6  itojun 		addstr(cur); col += len;
     93  1.1   jwise 		if (col + 1 < COLS)
     94  1.1   jwise 			addch(' ');
     95  1.6  itojun 		if (prev)
     96  1.6  itojun 			free(prev);
     97  1.6  itojun 		prev = cur;
     98  1.7  itojun 		cur = NULL;
     99  1.6  itojun 	}
    100  1.6  itojun 	if (col == 0 && args) {
    101  1.6  itojun 		standout();
    102  1.7  itojun 		if (strlen(args) < COLS - 25)
    103  1.7  itojun 			printw("help: no matches for `%s.*'", args);
    104  1.7  itojun 		else
    105  1.7  itojun 			printw("help: no matches");
    106  1.6  itojun 		standend();
    107  1.1   jwise 	}
    108  1.1   jwise 	clrtoeol();
    109  1.6  itojun 	if (cur)
    110  1.6  itojun 		free(cur);
    111  1.6  itojun 	if (prev)
    112  1.6  itojun 		free(prev);
    113  1.1   jwise }
    114  1.3   jwise 
    115  1.3   jwise void
    116  1.8      ad global_interval(char *args)
    117  1.3   jwise {
    118  1.3   jwise 	int interval;
    119  1.3   jwise 
    120  1.3   jwise 	if (!args) {
    121  1.3   jwise 		interval = 5;
    122  1.3   jwise 	} else {
    123  1.3   jwise 		interval = atoi(args);
    124  1.3   jwise 	}
    125  1.3   jwise 
    126  1.3   jwise 	if (interval <= 0) {
    127  1.3   jwise 		error("%d: bad interval.", interval);
    128  1.3   jwise 		return;
    129  1.3   jwise 	}
    130  1.3   jwise 
    131  1.3   jwise 	alarm(0);
    132  1.3   jwise 	naptime = interval;
    133  1.3   jwise 	display(0);
    134  1.3   jwise 	status();
    135  1.3   jwise }
    136  1.3   jwise 
    137  1.1   jwise 
    138  1.1   jwise void
    139  1.8      ad global_load(char *args)
    140  1.1   jwise {
    141  1.1   jwise 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
    142  1.1   jwise 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
    143  1.1   jwise 	    avenrun[0], avenrun[1], avenrun[2]);
    144  1.1   jwise 	clrtoeol();
    145  1.1   jwise }
    146  1.1   jwise 
    147  1.1   jwise void
    148  1.8      ad global_quit(char *args)
    149  1.1   jwise {
    150  1.1   jwise 	die(0);
    151  1.1   jwise }
    152  1.1   jwise 
    153  1.1   jwise void
    154  1.8      ad global_stop(char *args)
    155  1.1   jwise {
    156  1.1   jwise 	alarm(0);
    157  1.1   jwise 	mvaddstr(CMDLINE, 0, "Refresh disabled.");
    158  1.1   jwise 	clrtoeol();
    159  1.1   jwise }
    160