xref: /netbsd/usr.bin/systat/globalcmds.c (revision 6550d01e)
1 /*	$NetBSD: globalcmds.c,v 1.15 2009/11/01 22:08:14 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.15 2009/11/01 22:08:14 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);
98 		col += len + 1;
99 		if (col + 1 < COLS)
100 			addch(' ');
101 		if (prev)
102 			free(prev);
103 		prev = name;
104 		name = NULL;
105 	}
106 	if (col == 0 && args) {
107 		standout();
108 		if ((int)strlen(args) < COLS - 25)
109 			printw("help: no matches for `%s.*'", args);
110 		else
111 			printw("help: no matches");
112 		standend();
113 	}
114 	clrtoeol();
115 	if (name)
116 		free(name);
117 	if (prev)
118 		free(prev);
119 }
120 
121 void
122 global_interval(char *args)
123 {
124 	int interval;
125 
126 	if (!args) {
127 		interval = 5;
128 	} else {
129 		interval = atoi(args);
130 	}
131 
132 	if (interval <= 0) {
133 		error("%d: bad interval.", interval);
134 		return;
135 	}
136 
137 	naptime = interval;
138 	display(0);
139 	status();
140 }
141 
142 
143 void
144 global_load(char *args)
145 {
146 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
147 	mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
148 	    avenrun[0], avenrun[1], avenrun[2]);
149 	clrtoeol();
150 }
151 
152 void
153 global_quit(char *args)
154 {
155 	die(0);
156 }
157 
158 void
159 global_stop(char *args)
160 {
161 	timeout(-1);
162 	mvaddstr(CMDLINE, 0, "Refresh disabled.");
163 	clrtoeol();
164 }
165