docs: Recover 10.exref 11.vitut 12.vi 13.viref USD papers
[dragonfly.git] / usr.bin / systat / main.c
blob559327053412f5267925c76f8bc64890857474ae
1 /*-
2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/systat/main.c,v 1.11.2.1 2001/06/06 20:26:01 tmm Exp $
32 * $DragonFly: src/usr.bin/systat/main.c,v 1.7 2008/11/10 04:59:45 swildner Exp $
35 #include <sys/user.h>
36 #include <sys/param.h>
37 #include <sys/time.h>
39 #include <err.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <nlist.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include "systat.h"
48 #include "extern.h"
50 static struct nlist namelist[] = {
51 #define X_FIRST 0
52 #define X_HZ 0
53 { .n_name = "_hz" },
54 #define X_STATHZ 1
55 { .n_name = "_stathz" },
56 { .n_name = "" }
58 static int dellave;
60 kvm_t *kd;
61 sig_t sigtstpdfl;
62 double avenrun[3];
63 int col;
64 double naptime = 5.0;
65 int verbose = 1; /* to report kvm read errs */
66 int hz, stathz;
67 double hertz;
68 char c;
69 char *namp;
70 char hostname[MAXHOSTNAMELEN];
71 WINDOW *wnd;
72 int CMDLINE;
74 static WINDOW *wload; /* one line window for load average */
76 int
77 main(int argc, char **argv)
79 char errbuf[_POSIX2_LINE_MAX];
81 (void) setlocale(LC_TIME, "");
83 argc--, argv++;
84 while (argc > 0) {
85 if (argv[0][0] == '-') {
86 struct cmdtab *p;
88 p = lookup(&argv[0][1]);
89 if (p == (struct cmdtab *)-1)
90 errx(1, "%s: ambiguous request", &argv[0][1]);
91 if (p == NULL)
92 errx(1, "%s: unknown request", &argv[0][1]);
93 curcmd = p;
94 } else {
95 naptime = strtod(argv[0], NULL);
96 if (naptime <= 0.0)
97 naptime = 5.0;
99 argc--, argv++;
101 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
102 if (kd == NULL) {
103 error("%s", errbuf);
104 exit(1);
106 signal(SIGINT, die);
107 signal(SIGQUIT, die);
108 signal(SIGTERM, die);
111 * Initialize display. Load average appears in a one line
112 * window of its own. Current command's display appears in
113 * an overlapping sub-window of stdscr configured by the display
114 * routines to minimize update work by curses.
116 initscr();
117 CMDLINE = LINES - 1;
118 wnd = (*curcmd->c_open)();
119 if (wnd == NULL) {
120 warnx("couldn't initialize display");
121 die(0);
123 wload = newwin(1, 0, 3, 20);
124 if (wload == NULL) {
125 warnx("couldn't set up load average window");
126 die(0);
128 if (kvm_nlist(kd, namelist)) {
129 nlisterr(namelist);
130 exit(1);
132 if (namelist[X_FIRST].n_type == 0)
133 errx(1, "couldn't read namelist");
134 gethostname(hostname, sizeof (hostname));
135 NREAD(X_HZ, &hz, sizeof(hz));
136 NREAD(X_STATHZ, &stathz, sizeof(stathz));
137 hertz = stathz ? stathz : hz;
138 (*curcmd->c_init)();
139 curcmd->c_flags |= CF_INIT;
140 labels();
142 dellave = 0.0;
144 signal(SIGALRM, display);
145 display(0);
146 noecho();
147 crmode();
148 keyboard();
149 /*NOTREACHED*/
151 return EXIT_SUCCESS;
154 void
155 labels(void)
157 if (curcmd->c_flags & CF_LOADAV) {
158 mvaddstr(2, 20,
159 "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
160 mvaddstr(3, 5, "Load Average");
162 (*curcmd->c_label)();
163 #ifdef notdef
164 mvprintw(21, 25, "CPU usage on %s", hostname);
165 #endif
166 refresh();
169 void
170 display(int signo __unused)
172 int i, j;
173 struct itimerval ctv;
175 /* Get the load average over the last minute. */
176 (void) getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
177 (*curcmd->c_fetch)();
178 if (curcmd->c_flags & CF_LOADAV) {
179 j = 5.0*avenrun[0] + 0.5;
180 dellave = avenrun[0];
181 c = '|';
182 wmove(wload, 0, 0); wclrtoeol(wload);
183 for (i = (j > 50) ? 50 : j; i > 0; i--)
184 waddch(wload, c);
185 if (j > 50)
186 wprintw(wload, " %4.1f", avenrun[0]);
188 (*curcmd->c_refresh)();
189 if (curcmd->c_flags & CF_LOADAV)
190 wrefresh(wload);
191 wrefresh(wnd);
192 move(CMDLINE, col);
193 refresh();
194 ctv.it_interval.tv_sec = 0;
195 ctv.it_interval.tv_usec = 0;
196 ctv.it_value.tv_sec = (int)naptime;
197 ctv.it_value.tv_usec = (naptime - (double)(int)naptime) * 1000000.0;
198 setitimer(ITIMER_REAL, &ctv, NULL);
201 void
202 load(void)
205 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
206 mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
207 avenrun[0], avenrun[1], avenrun[2]);
208 clrtoeol();
211 void
212 die(int signo __unused)
214 move(CMDLINE, 0);
215 clrtoeol();
216 refresh();
217 endwin();
218 exit(0);
221 #include <stdarg.h>
223 void
224 error(const char *fmt, ...)
226 va_list ap;
227 char buf[255];
228 int oy, ox;
229 va_start(ap, fmt);
231 if (wnd) {
232 getyx(stdscr, oy, ox);
233 (void) vsnprintf(buf, sizeof(buf), fmt, ap);
234 clrtoeol();
235 standout();
236 mvaddstr(CMDLINE, 0, buf);
237 standend();
238 move(oy, ox);
239 refresh();
240 } else {
241 (void) vfprintf(stderr, fmt, ap);
242 fprintf(stderr, "\n");
244 va_end(ap);
247 void
248 nlisterr(struct nlist *n_list)
250 int i, n;
252 n = 0;
253 clear();
254 mvprintw(2, 10, "systat: nlist: can't find following symbols:");
255 for (i = 0;
256 n_list[i].n_name != NULL && *n_list[i].n_name != '\0'; i++)
257 if (n_list[i].n_value == 0)
258 mvprintw(2 + ++n, 10, "%s", n_list[i].n_name);
259 move(CMDLINE, 0);
260 clrtoeol();
261 refresh();
262 endwin();
263 exit(1);