Commit | Line | Data |
---|---|---|
0ca7a29c | 1 | #ifndef lint |
f58d0ba1 | 2 | static char sccsid[] = "@(#)main.c 1.2 (Lucasfilm) %G%"; |
0ca7a29c SL |
3 | #endif |
4 | ||
5 | #include "systat.h" | |
6 | ||
7 | struct nlist nlst[] = { | |
8 | #define X_PROC 0 | |
9 | { "_proc" }, | |
10 | #define X_NPROC 1 | |
11 | { "_nproc" }, | |
12 | #define X_CCPU 2 | |
13 | { "_ccpu" }, | |
14 | #define X_AVENRUN 3 | |
15 | { "_avenrun" }, | |
16 | #define X_USRPTMAP 4 | |
17 | { "_Usrptmap" }, | |
18 | #define X_USRPT 5 | |
19 | { "_usrpt" }, | |
20 | #define X_NSWAP 6 | |
21 | { "_nswap" }, | |
22 | #define X_SWAPMAP 7 | |
23 | { "_swapmap" }, | |
24 | #define X_NSWAPMAP 8 | |
25 | { "_nswapmap" }, | |
26 | #define X_DMMIN 9 | |
27 | { "_dmmin" }, | |
28 | #define X_DMMAX 10 | |
29 | { "_dmmax" }, | |
30 | #define X_NSWDEV 11 | |
31 | { "_nswdev" }, | |
f58d0ba1 SL |
32 | #define X_SWDEVT 12 |
33 | { "_swdevt" }, | |
0ca7a29c SL |
34 | { "" } |
35 | }; | |
36 | ||
37 | int kmem = -1; | |
38 | int mem = -1; | |
39 | int swap = -1; | |
f58d0ba1 | 40 | int naptime = 5; |
0ca7a29c SL |
41 | |
42 | int die(); | |
43 | int display(); | |
44 | int suspend(); | |
45 | ||
46 | int showpigs(), openpigs(), fetchpigs(), labelpigs(); | |
47 | int showswap(), fetchswap(), labelswap(); | |
48 | int showuser(), openuser(), fetchuser(), labeluser(); | |
49 | int shownet(), opennet(), fetchnet(), labelnet(); | |
50 | ||
51 | struct cmdtab { | |
52 | char *c_name; | |
53 | int (*c_refresh)(); | |
54 | int (*c_open)(); | |
55 | int (*c_fetch)(); | |
56 | int (*c_label)(); | |
57 | } cmdtab[] = { | |
58 | { "pigs", showpigs, openpigs, fetchpigs, | |
59 | labelpigs }, | |
60 | { "swap", showswap, openpigs, fetchswap, | |
61 | labelswap }, | |
62 | #ifdef notdef | |
63 | { "user", showuser, openuser, fetchuser, | |
64 | labeluser }, | |
65 | { "net", shownet, opennet, fetchnet, | |
66 | labelnet }, | |
67 | #endif | |
68 | { "" } | |
69 | }; | |
70 | struct cmdtab *curcmd = &cmdtab[0]; | |
71 | ||
72 | main(argc, argv) | |
73 | int argc; | |
74 | char **argv; | |
75 | { | |
76 | char ch, line[80]; | |
77 | ||
f58d0ba1 SL |
78 | argc--, argv++; |
79 | while (argc > 0) { | |
80 | if (argv[0][0] == '-') { | |
81 | struct cmdtab *p; | |
82 | ||
83 | for (p = cmdtab; *p->c_name; p++) | |
84 | if (strcmp(p->c_name, &argv[0][1]) == 0) | |
85 | break; | |
86 | if (*p->c_name == 0) { | |
87 | fprintf(stderr, "%s: unknown request\n", | |
88 | &argv[0][1]); | |
89 | exit(1); | |
90 | } | |
91 | curcmd = p; | |
92 | } else { | |
93 | naptime = atoi(argv[1]); | |
94 | if (naptime < 5) | |
95 | naptime = 5; | |
96 | } | |
97 | argc--, argv++; | |
98 | } | |
0ca7a29c SL |
99 | nlist("/vmunix", nlst); |
100 | (*curcmd->c_open)(); | |
0ca7a29c SL |
101 | signal(SIGINT, die); |
102 | signal(SIGQUIT, die); | |
103 | signal(SIGTERM, die); | |
104 | ||
105 | /* Initialize curses. */ | |
106 | initscr(); | |
107 | wnd = newwin(20, 70, 3, 5); | |
108 | ||
f58d0ba1 SL |
109 | #ifdef notdef |
110 | gethostname(hostname, sizeof (hostname)); | |
111 | #endif | |
0ca7a29c SL |
112 | lseek(kmem, nlst[X_CCPU].n_value, 0); |
113 | read(kmem, &ccpu, sizeof (ccpu)); | |
114 | lccpu = log(ccpu); | |
115 | (*curcmd->c_fetch)(); | |
0ca7a29c SL |
116 | labels(); |
117 | ||
118 | known[0].k_uid = -1; | |
119 | strcpy(known[0].k_name, "<idle>"); | |
120 | numknown = 1; | |
121 | dellave = 0.0; | |
122 | ||
123 | signal(SIGALRM, display); | |
124 | signal(SIGTSTP, suspend); | |
125 | display(); | |
126 | noecho(); | |
127 | crmode(); | |
128 | for (;;) { | |
129 | col = 0; | |
130 | move(22, 0); | |
131 | do { | |
132 | refresh(); | |
133 | ch = getch() & 0177; | |
134 | if (ch == 0177 && ferror(stdin)) { | |
135 | clearerr(stdin); | |
136 | continue; | |
137 | } | |
138 | if (ch >= 'A' && ch <= 'Z') | |
139 | ch += 'a' - 'A'; | |
140 | if (col == 0) { | |
141 | if (ch == CTRL(l)) { | |
142 | wrefresh(curscr); | |
143 | continue; | |
144 | } | |
145 | if (ch != ':') | |
146 | continue; | |
147 | move(22, 0); | |
148 | clrtoeol(); | |
149 | } | |
150 | if (ch == _tty.sg_erase && col > 0) { | |
151 | if (col == 1 && line[0] == ':') | |
152 | continue; | |
153 | col--; | |
154 | goto doerase; | |
155 | } | |
156 | if (ch == CTRL(w) && col > 0) { | |
157 | while (--col >= 0 && isspace(line[col])) | |
158 | ; | |
159 | col++; | |
160 | while (--col >= 0 && !isspace(line[col])) | |
161 | if (col == 0 && line[0] == ':') | |
162 | break; | |
163 | col++; | |
164 | goto doerase; | |
165 | } | |
166 | if (ch == _tty.sg_kill && col > 0) { | |
167 | col = 0; | |
168 | if (line[0] == ':') | |
169 | col++; | |
170 | doerase: | |
171 | move(22, col); | |
172 | clrtoeol(); | |
173 | continue; | |
174 | } | |
175 | if (isprint(ch)) { | |
176 | line[col] = ch; | |
177 | mvaddch(22, col, ch); | |
178 | col++; | |
179 | } | |
180 | } while (col == 0 || (ch != '\r' && ch != '\n')); | |
181 | line[col] = '\0'; | |
182 | command(line + 1); | |
183 | } | |
184 | } | |
185 | ||
186 | command(cmd) | |
187 | char *cmd; | |
188 | { | |
189 | register char *cp; | |
190 | register struct cmdtab *p; | |
191 | char *arg; | |
192 | ||
193 | for (cp = cmd; *cp && !isspace(*cp); cp++) | |
194 | ; | |
195 | if (*cp) | |
196 | *cp++ = '\0'; | |
197 | if (strcmp(cmd, "quit") == 0) | |
198 | die(); | |
199 | if (strcmp(cmd, "status") == 0 || strcmp(cmd, "help") == 0) { | |
200 | status(); | |
201 | return; | |
202 | } | |
f58d0ba1 SL |
203 | if (strcmp(cmd, "load") == 0) { |
204 | lseek(kmem, nlst[X_AVENRUN].n_value, L_SET); | |
205 | read(kmem, &lave, sizeof (lave)); | |
206 | mvprintw(22, 0, "%4.1f", lave); | |
207 | clrtoeol(); | |
208 | return; | |
209 | } | |
0ca7a29c SL |
210 | for (p = cmdtab; *p->c_name; p++) |
211 | if (strcmp(cmd, p->c_name) == 0) | |
212 | break; | |
213 | if (*p->c_name) { | |
214 | if (curcmd == p) | |
215 | return; | |
216 | alarm(0); | |
217 | curcmd = p; | |
f58d0ba1 SL |
218 | clear(); wclear(wnd); |
219 | labels(); | |
0ca7a29c SL |
220 | display(); |
221 | status(); | |
222 | return; | |
223 | } | |
224 | if (strcmp(cmd, "stop") == 0) { | |
225 | alarm(0); | |
226 | mvaddstr(22, 0, "Refresh disabled."); | |
227 | clrtoeol(); | |
228 | return; | |
229 | } | |
230 | /* commands with arguments */ | |
231 | for (; *cp && isspace(*cp); cp++) | |
232 | ; | |
233 | if (strcmp(cmd, "start") == 0) { | |
234 | int x; | |
235 | ||
236 | if (*cp == '\0') | |
237 | x = naptime; | |
238 | else | |
239 | x = atoi(cp); | |
240 | if (x <= 0) { | |
241 | mvprintw(22, 0, "%d: bad interval.", x); | |
242 | clrtoeol(); | |
243 | return; | |
244 | } | |
245 | alarm(0); | |
246 | naptime = x; | |
247 | display(); | |
248 | status(); | |
249 | return; | |
250 | } | |
f58d0ba1 SL |
251 | if (*cmd) { |
252 | mvprintw(22, 0, "%s: Unknown command.", cmd); | |
253 | clrtoeol(); | |
254 | } | |
0ca7a29c SL |
255 | } |
256 | ||
257 | status() | |
258 | { | |
259 | ||
260 | mvprintw(22, 0, "Showing %s, refresh every %d seconds.", | |
261 | curcmd->c_name, naptime); | |
262 | clrtoeol(); | |
263 | } | |
264 | ||
265 | suspend() | |
266 | { | |
267 | int oldmask; | |
268 | ||
f58d0ba1 | 269 | alarm(0); |
0ca7a29c SL |
270 | move(22, 0); |
271 | refresh(); | |
272 | echo(); | |
273 | nocrmode(); | |
274 | signal(SIGTSTP, SIG_DFL); | |
275 | oldmask = sigsetmask(0); | |
276 | kill(getpid(), SIGTSTP); | |
277 | sigsetmask(oldmask); | |
278 | signal(SIGTSTP, suspend); | |
279 | crmode(); | |
280 | noecho(); | |
281 | move(22, col); | |
282 | wrefresh(curscr); | |
f58d0ba1 | 283 | alarm(naptime); |
0ca7a29c SL |
284 | } |
285 | ||
286 | labels() | |
287 | { | |
288 | ||
289 | mvaddstr(2, 20, | |
290 | "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); | |
291 | mvwaddstr(wnd, 0, 0, "Load Average"); | |
292 | (*curcmd->c_label)(); | |
293 | #ifdef notdef | |
294 | mvprintw(21, 25, "CPU usage on %s", hostname); | |
295 | #endif | |
296 | refresh(); | |
297 | } | |
298 | ||
299 | display() | |
300 | { | |
301 | register int i, j; | |
302 | ||
303 | /* Get the load average over the last minute. */ | |
304 | lseek(kmem, nlst[X_AVENRUN].n_value, L_SET); | |
305 | read(kmem, &lave, sizeof (lave)); | |
306 | (*curcmd->c_fetch)(); | |
307 | j = 5.0*lave + 0.5; | |
308 | dellave -= lave; | |
309 | if (dellave >= 0.0) | |
310 | c = '<'; | |
311 | else { | |
312 | c = '>'; | |
313 | dellave = -dellave; | |
314 | } | |
315 | if (dellave < 0.1) | |
316 | c = '|'; | |
317 | dellave = lave; | |
318 | wmove(wnd, 0, 15); | |
319 | wclrtoeol(wnd); | |
320 | for (i = (j > 50)? 50 : j; i > 0; i--) | |
321 | waddch(wnd, c); | |
322 | if (j > 50) | |
323 | wprintw(wnd, " %4.1f", lave); | |
324 | (*curcmd->c_refresh)(); | |
325 | wrefresh(wnd); | |
326 | move(22, col); | |
327 | refresh(); | |
328 | alarm(naptime); | |
329 | } | |
330 | ||
331 | die() | |
332 | { | |
333 | ||
334 | endwin(); | |
335 | exit(0); | |
336 | } |