main.c revision 1.2 1 /* $NetBSD: main.c,v 1.2 1995/01/20 08:52:00 jtc Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1992, 1993
5 * The Regents of the University of California. 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 University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
45 #endif
46 static char rcsid[] = "$NetBSD: main.c,v 1.2 1995/01/20 08:52:00 jtc Exp $";
47 #endif /* not lint */
48
49 #include <sys/param.h>
50
51 #include <nlist.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include "systat.h"
55 #include "extern.h"
56
57 static struct nlist namelist[] = {
58 #define X_FIRST 0
59 #define X_HZ 0
60 { "_hz" },
61 #define X_STATHZ 1
62 { "_stathz" },
63 { "" }
64 };
65 static int dellave;
66
67 kvm_t *kd;
68 sig_t sigtstpdfl;
69 double avenrun[3];
70 int col;
71 int naptime = 5;
72 int verbose = 1; /* to report kvm read errs */
73 int hz, stathz;
74 char c;
75 char *namp;
76 char hostname[MAXHOSTNAMELEN];
77 WINDOW *wnd;
78 int CMDLINE;
79
80 static WINDOW *wload; /* one line window for load average */
81
82 void
83 main(argc, argv)
84 int argc;
85 char **argv;
86 {
87 char errbuf[80];
88
89 argc--, argv++;
90 while (argc > 0) {
91 if (argv[0][0] == '-') {
92 struct cmdtab *p;
93
94 p = lookup(&argv[0][1]);
95 if (p == (struct cmdtab *)-1) {
96 fprintf(stderr, "%s: unknown request\n",
97 &argv[0][1]);
98 exit(1);
99 }
100 curcmd = p;
101 } else {
102 naptime = atoi(argv[0]);
103 if (naptime <= 0)
104 naptime = 5;
105 }
106 argc--, argv++;
107 }
108 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
109 if (kd == NULL) {
110 error("%s", errbuf);
111 exit(1);
112 }
113 if (kvm_nlist(kd, namelist)) {
114 nlisterr(namelist);
115 exit(1);
116 }
117 if (namelist[X_FIRST].n_type == 0) {
118 fprintf(stderr, "couldn't read namelist.\n");
119 exit(1);
120 }
121 signal(SIGINT, die);
122 signal(SIGQUIT, die);
123 signal(SIGTERM, die);
124
125 /*
126 * Initialize display. Load average appears in a one line
127 * window of its own. Current command's display appears in
128 * an overlapping sub-window of stdscr configured by the display
129 * routines to minimize update work by curses.
130 */
131 initscr();
132 CMDLINE = LINES - 1;
133 wnd = (*curcmd->c_open)();
134 if (wnd == NULL) {
135 fprintf(stderr, "Couldn't initialize display.\n");
136 die(0);
137 }
138 wload = newwin(1, 0, 3, 20);
139 if (wload == NULL) {
140 fprintf(stderr, "Couldn't set up load average window.\n");
141 die(0);
142 }
143 gethostname(hostname, sizeof (hostname));
144 NREAD(X_HZ, &hz, LONG);
145 NREAD(X_STATHZ, &stathz, LONG);
146 (*curcmd->c_init)();
147 curcmd->c_flags |= CF_INIT;
148 labels();
149
150 dellave = 0.0;
151
152 signal(SIGALRM, display);
153 display(0);
154 noecho();
155 crmode();
156 keyboard();
157 /*NOTREACHED*/
158 }
159
160 void
161 labels()
162 {
163 if (curcmd->c_flags & CF_LOADAV) {
164 mvaddstr(2, 20,
165 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
166 mvaddstr(3, 5, "Load Average");
167 }
168 (*curcmd->c_label)();
169 #ifdef notdef
170 mvprintw(21, 25, "CPU usage on %s", hostname);
171 #endif
172 refresh();
173 }
174
175 void
176 display(signo)
177 int signo;
178 {
179 register int i, j;
180
181 /* Get the load average over the last minute. */
182 (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
183 (*curcmd->c_fetch)();
184 if (curcmd->c_flags & CF_LOADAV) {
185 j = 5.0*avenrun[0] + 0.5;
186 dellave -= avenrun[0];
187 if (dellave >= 0.0)
188 c = '<';
189 else {
190 c = '>';
191 dellave = -dellave;
192 }
193 if (dellave < 0.1)
194 c = '|';
195 dellave = avenrun[0];
196 wmove(wload, 0, 0); wclrtoeol(wload);
197 for (i = (j > 50) ? 50 : j; i > 0; i--)
198 waddch(wload, c);
199 if (j > 50)
200 wprintw(wload, " %4.1f", avenrun[0]);
201 }
202 (*curcmd->c_refresh)();
203 if (curcmd->c_flags & CF_LOADAV)
204 wrefresh(wload);
205 wrefresh(wnd);
206 move(CMDLINE, col);
207 refresh();
208 alarm(naptime);
209 }
210
211 void
212 load()
213 {
214
215 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
216 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
217 avenrun[0], avenrun[1], avenrun[2]);
218 clrtoeol();
219 }
220
221 void
222 die(signo)
223 int signo;
224 {
225 move(CMDLINE, 0);
226 clrtoeol();
227 refresh();
228 endwin();
229 exit(0);
230 }
231
232 #if __STDC__
233 #include <stdarg.h>
234 #else
235 #include <varargs.h>
236 #endif
237
238 #if __STDC__
239 void
240 error(const char *fmt, ...)
241 #else
242 void
243 error(fmt, va_alist)
244 char *fmt;
245 va_dcl
246 #endif
247 {
248 va_list ap;
249 char buf[255];
250 int oy, ox;
251 #if __STDC__
252 va_start(ap, fmt);
253 #else
254 va_start(ap);
255 #endif
256
257 if (wnd) {
258 getyx(stdscr, oy, ox);
259 (void) vsprintf(buf, fmt, ap);
260 clrtoeol();
261 standout();
262 mvaddstr(CMDLINE, 0, buf);
263 standend();
264 move(oy, ox);
265 refresh();
266 } else {
267 (void) vfprintf(stderr, fmt, ap);
268 fprintf(stderr, "\n");
269 }
270 va_end(ap);
271 }
272
273 void
274 nlisterr(namelist)
275 struct nlist namelist[];
276 {
277 int i, n;
278
279 n = 0;
280 clear();
281 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
282 for (i = 0;
283 namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
284 if (namelist[i].n_value == 0)
285 mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
286 move(CMDLINE, 0);
287 clrtoeol();
288 refresh();
289 endwin();
290 exit(1);
291 }
292