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