xref: /netbsd-src/usr.bin/systat/globalcmds.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: globalcmds.c,v 1.8 2000/07/05 11:03:21 ad 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 <sys/cdefs.h>
36 #ifndef lint
37 __RCSID("$NetBSD: globalcmds.c,v 1.8 2000/07/05 11:03:21 ad Exp $");
38 #endif /* not lint */
39 
40 #include <curses.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include "systat.h"
45 #include "extern.h"
46 
47 static char *shortname(const char *, const char *);
48 
49 static char *
50 shortname(const char *key, const char *s)
51 {
52 	char *p, *q;
53 	size_t l;
54 
55 	if (key == NULL) {
56 		if ((p = strdup(s)) == NULL)
57 			return NULL;
58 		q = strchr(p, '.');
59 		if (q && strlen(q) > 1) {
60 			q[1] = '*';
61 			q[2] = '\0';
62 		}
63 		return p;
64 	} else if (strncmp(key, s, l = strlen(key)) == 0 && s[l] == '.') {
65 		p = strdup(s + l + 1);
66 		return p;
67 	} else
68 		return NULL;
69 }
70 
71 void
72 global_help(char *args)
73 {
74 	int col, len;
75 	struct mode *p;
76 	char *cur, *prev;
77 
78 	move(CMDLINE, col = 0);
79 	cur = prev = NULL;
80 	for (p = modes; p->c_name; p++) {
81 		if ((cur = shortname(args, p->c_name)) == NULL)
82 			continue;
83 		if (cur && prev && strcmp(cur, prev) == 0) {
84 			free(cur);
85 			cur = NULL;
86 			continue;
87 		}
88 		len = strlen(cur);
89 		if (col + len > COLS)
90 			break;
91 		addstr(cur); col += len;
92 		if (col + 1 < COLS)
93 			addch(' ');
94 		if (prev)
95 			free(prev);
96 		prev = cur;
97 		cur = NULL;
98 	}
99 	if (col == 0 && args) {
100 		standout();
101 		if (strlen(args) < COLS - 25)
102 			printw("help: no matches for `%s.*'", args);
103 		else
104 			printw("help: no matches");
105 		standend();
106 	}
107 	clrtoeol();
108 	if (cur)
109 		free(cur);
110 	if (prev)
111 		free(prev);
112 }
113 
114 void
115 global_interval(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(char *args)
139 {
140 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
141 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
142 	    avenrun[0], avenrun[1], avenrun[2]);
143 	clrtoeol();
144 }
145 
146 void
147 global_quit(char *args)
148 {
149 	die(0);
150 }
151 
152 void
153 global_stop(char *args)
154 {
155 	alarm(0);
156 	mvaddstr(CMDLINE, 0, "Refresh disabled.");
157 	clrtoeol();
158 }
159