xref: /netbsd/usr.bin/systat/globalcmds.c (revision bf9ec67e)
1 /*	$NetBSD: globalcmds.c,v 1.9 2000/12/01 02:19:43 simonb 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.9 2000/12/01 02:19:43 simonb 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 		return p;
68 	} else
69 		return NULL;
70 }
71 
72 void
73 global_help(char *args)
74 {
75 	int col, len;
76 	struct mode *p;
77 	char *cur, *prev;
78 
79 	move(CMDLINE, col = 0);
80 	cur = prev = NULL;
81 	for (p = modes; p->c_name; p++) {
82 		if ((cur = shortname(args, p->c_name)) == NULL)
83 			continue;
84 		if (cur && prev && strcmp(cur, prev) == 0) {
85 			free(cur);
86 			cur = NULL;
87 			continue;
88 		}
89 		len = strlen(cur);
90 		if (col + len > COLS)
91 			break;
92 		addstr(cur); col += len;
93 		if (col + 1 < COLS)
94 			addch(' ');
95 		if (prev)
96 			free(prev);
97 		prev = cur;
98 		cur = NULL;
99 	}
100 	if (col == 0 && args) {
101 		standout();
102 		if (strlen(args) < COLS - 25)
103 			printw("help: no matches for `%s.*'", args);
104 		else
105 			printw("help: no matches");
106 		standend();
107 	}
108 	clrtoeol();
109 	if (cur)
110 		free(cur);
111 	if (prev)
112 		free(prev);
113 }
114 
115 void
116 global_interval(char *args)
117 {
118 	int interval;
119 
120 	if (!args) {
121 		interval = 5;
122 	} else {
123 		interval = atoi(args);
124 	}
125 
126 	if (interval <= 0) {
127 		error("%d: bad interval.", interval);
128 		return;
129 	}
130 
131 	alarm(0);
132 	naptime = interval;
133 	display(0);
134 	status();
135 }
136 
137 
138 void
139 global_load(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(char *args)
149 {
150 	die(0);
151 }
152 
153 void
154 global_stop(char *args)
155 {
156 	alarm(0);
157 	mvaddstr(CMDLINE, 0, "Refresh disabled.");
158 	clrtoeol();
159 }
160