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