xref: /netbsd-src/usr.bin/systat/globalcmds.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: globalcmds.c,v 1.13 2006/02/05 08:51:03 dsl 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.13 2006/02/05 08:51:03 dsl 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 len;
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 	}
66 
67 	len = strlen(key);
68 	if (strncmp(key, s, len) == 0 && s[len] == '.') {
69 		p = strdup(s + len + 1);
70 		if (!p)
71 			return NULL;
72 		return p;
73 	}
74 	return NULL;
75 }
76 
77 void
78 global_help(char *args)
79 {
80 	int col, len;
81 	struct mode *p;
82 	char *name, *prev;
83 
84 	move(CMDLINE, col = 0);
85 	name = prev = NULL;
86 	for (p = modes; p->c_name; p++) {
87 		if ((name = shortname(args, p->c_name)) == NULL)
88 			continue;
89 		if (name && prev && strcmp(name, prev) == 0) {
90 			free(name);
91 			name = NULL;
92 			continue;
93 		}
94 		len = strlen(name);
95 		if (col + len > COLS)
96 			break;
97 		addstr(name); col += len;
98 		if (col + 1 < COLS)
99 			addch(' ');
100 		if (prev)
101 			free(prev);
102 		prev = name;
103 		name = NULL;
104 	}
105 	if (col == 0 && args) {
106 		standout();
107 		if (strlen(args) < COLS - 25)
108 			printw("help: no matches for `%s.*'", args);
109 		else
110 			printw("help: no matches");
111 		standend();
112 	}
113 	clrtoeol();
114 	if (name)
115 		free(name);
116 	if (prev)
117 		free(prev);
118 }
119 
120 void
121 global_interval(char *args)
122 {
123 	int interval;
124 
125 	if (!args) {
126 		interval = 5;
127 	} else {
128 		interval = atoi(args);
129 	}
130 
131 	if (interval <= 0) {
132 		error("%d: bad interval.", interval);
133 		return;
134 	}
135 
136 	naptime = interval;
137 	display(0);
138 	status();
139 }
140 
141 
142 void
143 global_load(char *args)
144 {
145 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
146 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
147 	    avenrun[0], avenrun[1], avenrun[2]);
148 	clrtoeol();
149 }
150 
151 void
152 global_quit(char *args)
153 {
154 	die(0);
155 }
156 
157 void
158 global_stop(char *args)
159 {
160 	timeout(-1);
161 	mvaddstr(CMDLINE, 0, "Refresh disabled.");
162 	clrtoeol();
163 }
164