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