Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)main.c   8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/systat/main.c,v 1.11.2.1 2001/06/06 20:26:01 tmm Exp $
36  * $DragonFly: src/usr.bin/systat/main.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
37  */
38
39 #include <sys/param.h>
40
41 #include <err.h>
42 #include <limits.h>
43 #include <locale.h>
44 #include <nlist.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include "systat.h"
50 #include "extern.h"
51
52 static struct nlist namelist[] = {
53 #define X_FIRST         0
54 #define X_HZ            0
55         { "_hz" },
56 #define X_STATHZ                1
57         { "_stathz" },
58         { "" }
59 };
60 static int     dellave;
61
62 kvm_t *kd;
63 sig_t   sigtstpdfl;
64 double avenrun[3];
65 int     col;
66 int     naptime = 5;
67 int     verbose = 1;                    /* to report kvm read errs */
68 int     hz, stathz;
69 double  hertz;
70 char    c;
71 char    *namp;
72 char    hostname[MAXHOSTNAMELEN];
73 WINDOW  *wnd;
74 int     CMDLINE;
75
76 static  WINDOW *wload;                  /* one line window for load average */
77
78 int
79 main(argc, argv)
80         int argc;
81         char **argv;
82 {
83         char errbuf[_POSIX2_LINE_MAX];
84
85         (void) setlocale(LC_TIME, "");
86
87         argc--, argv++;
88         while (argc > 0) {
89                 if (argv[0][0] == '-') {
90                         struct cmdtab *p;
91
92                         p = lookup(&argv[0][1]);
93                         if (p == (struct cmdtab *)-1)
94                                 errx(1, "%s: ambiguous request", &argv[0][1]);
95                         if (p == (struct cmdtab *)0)
96                                 errx(1, "%s: unknown request", &argv[0][1]);
97                         curcmd = p;
98                 } else {
99                         naptime = atoi(argv[0]);
100                         if (naptime <= 0)
101                                 naptime = 5;
102                 }
103                 argc--, argv++;
104         }
105         kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
106         if (kd == NULL) {
107                 error("%s", errbuf);
108                 exit(1);
109         }
110         signal(SIGINT, die);
111         signal(SIGQUIT, die);
112         signal(SIGTERM, die);
113
114         /*
115          * Initialize display.  Load average appears in a one line
116          * window of its own.  Current command's display appears in
117          * an overlapping sub-window of stdscr configured by the display
118          * routines to minimize update work by curses.
119          */
120         initscr();
121         CMDLINE = LINES - 1;
122         wnd = (*curcmd->c_open)();
123         if (wnd == NULL) {
124                 warnx("couldn't initialize display");
125                 die(0);
126         }
127         wload = newwin(1, 0, 3, 20);
128         if (wload == NULL) {
129                 warnx("couldn't set up load average window");
130                 die(0);
131         }
132         if (kvm_nlist(kd, namelist)) {
133                 nlisterr(namelist);
134                 exit(1);
135         }
136         if (namelist[X_FIRST].n_type == 0)
137                 errx(1, "couldn't read namelist");
138         gethostname(hostname, sizeof (hostname));
139         NREAD(X_HZ, &hz, sizeof(hz));
140         NREAD(X_STATHZ, &stathz, sizeof(stathz));
141         hertz = stathz ? stathz : hz;
142         (*curcmd->c_init)();
143         curcmd->c_flags |= CF_INIT;
144         labels();
145
146         dellave = 0.0;
147
148         signal(SIGALRM, display);
149         display(0);
150         noecho();
151         crmode();
152         keyboard();
153         /*NOTREACHED*/
154
155         return EXIT_SUCCESS;
156 }
157
158 void
159 labels()
160 {
161         if (curcmd->c_flags & CF_LOADAV) {
162                 mvaddstr(2, 20,
163                     "/0   /1   /2   /3   /4   /5   /6   /7   /8   /9   /10");
164                 mvaddstr(3, 5, "Load Average");
165         }
166         (*curcmd->c_label)();
167 #ifdef notdef
168         mvprintw(21, 25, "CPU usage on %s", hostname);
169 #endif
170         refresh();
171 }
172
173 void
174 display(signo)
175         int signo;
176 {
177         register int i, j;
178
179         /* Get the load average over the last minute. */
180         (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
181         (*curcmd->c_fetch)();
182         if (curcmd->c_flags & CF_LOADAV) {
183                 j = 5.0*avenrun[0] + 0.5;
184                 dellave -= avenrun[0];
185                 if (dellave >= 0.0)
186                         c = '<';
187                 else {
188                         c = '>';
189                         dellave = -dellave;
190                 }
191                 if (dellave < 0.1)
192                         c = '|';
193                 dellave = avenrun[0];
194                 wmove(wload, 0, 0); wclrtoeol(wload);
195                 for (i = (j > 50) ? 50 : j; i > 0; i--)
196                         waddch(wload, c);
197                 if (j > 50)
198                         wprintw(wload, " %4.1f", avenrun[0]);
199         }
200         (*curcmd->c_refresh)();
201         if (curcmd->c_flags & CF_LOADAV)
202                 wrefresh(wload);
203         wrefresh(wnd);
204         move(CMDLINE, col);
205         refresh();
206         alarm(naptime);
207 }
208
209 void
210 load()
211 {
212
213         (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
214         mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
215             avenrun[0], avenrun[1], avenrun[2]);
216         clrtoeol();
217 }
218
219 void
220 die(signo)
221         int signo;
222 {
223         move(CMDLINE, 0);
224         clrtoeol();
225         refresh();
226         endwin();
227         exit(0);
228 }
229
230 #if __STDC__
231 #include <stdarg.h>
232 #else
233 #include <varargs.h>
234 #endif
235
236 #if __STDC__
237 void
238 error(const char *fmt, ...)
239 #else
240 void
241 error(fmt, va_alist)
242         char *fmt;
243         va_dcl
244 #endif
245 {
246         va_list ap;
247         char buf[255];
248         int oy, ox;
249 #if __STDC__
250         va_start(ap, fmt);
251 #else
252         va_start(ap);
253 #endif
254
255         if (wnd) {
256                 getyx(stdscr, oy, ox);
257                 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
258                 clrtoeol();
259                 standout();
260                 mvaddstr(CMDLINE, 0, buf);
261                 standend();
262                 move(oy, ox);
263                 refresh();
264         } else {
265                 (void) vfprintf(stderr, fmt, ap);
266                 fprintf(stderr, "\n");
267         }
268         va_end(ap);
269 }
270
271 void
272 nlisterr(namelist)
273         struct nlist namelist[];
274 {
275         int i, n;
276
277         n = 0;
278         clear();
279         mvprintw(2, 10, "systat: nlist: can't find following symbols:");
280         for (i = 0;
281             namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
282                 if (namelist[i].n_value == 0)
283                         mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
284         move(CMDLINE, 0);
285         clrtoeol();
286         refresh();
287         endwin();
288         exit(1);
289 }