Deleted Added
full compact
top.c (146342) top.c (157842)
1char *copyright =
2 "Copyright (c) 1984 through 1996, William LeFebvre";
3
4/*
5 * Top users/processes display for Unix
6 * Version 3
7 *
8 * This program may be freely redistributed,
9 * but this entire comment MUST remain intact.
10 *
11 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
12 * Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
13 * Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
14 * Copyright (c) 1996, William LeFebvre, Group sys Consulting
15 *
1char *copyright =
2 "Copyright (c) 1984 through 1996, William LeFebvre";
3
4/*
5 * Top users/processes display for Unix
6 * Version 3
7 *
8 * This program may be freely redistributed,
9 * but this entire comment MUST remain intact.
10 *
11 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
12 * Copyright (c) 1989 - 1994, William LeFebvre, Northwestern University
13 * Copyright (c) 1994, 1995, William LeFebvre, Argonne National Laboratory
14 * Copyright (c) 1996, William LeFebvre, Group sys Consulting
15 *
16 * $FreeBSD: head/contrib/top/top.c 146342 2005-05-18 13:30:08Z keramida $
16 * $FreeBSD: head/contrib/top/top.c 157842 2006-04-18 15:26:58Z ru $
17 */
18
19/*
20 * See the file "Changes" for information on version-to-version changes.
21 */
22
23/*
24 * This file contains "main" and other high-level routines.
25 */
26
27/*
28 * The following preprocessor variables, when defined, are used to
29 * distinguish between different Unix implementations:
30 *
31 * SIGHOLD - use SVR4 sighold function when defined
32 * SIGRELSE - use SVR4 sigrelse function when defined
33 * FD_SET - macros FD_SET and FD_ZERO are used when defined
34 */
35
36#include "os.h"
37#include <errno.h>
38#include <signal.h>
39#include <setjmp.h>
40#include <ctype.h>
41#include <sys/time.h>
42
43/* includes specific to top */
44#include "display.h" /* interface to display package */
45#include "screen.h" /* interface to screen package */
46#include "top.h"
47#include "top.local.h"
48#include "boolean.h"
49#include "machine.h"
50#include "utils.h"
51
52/* Size of the stdio buffer given to stdout */
53#define Buffersize 2048
54
55/* The buffer that stdio will use */
56char stdoutbuf[Buffersize];
57
58/* build Signal masks */
59#define Smask(s) (1 << ((s) - 1))
60
61/* for getopt: */
62extern int optind;
63extern char *optarg;
64
65/* imported from screen.c */
66extern int overstrike;
67
68/* signal handling routines */
69sigret_t leave();
70sigret_t onalrm();
71sigret_t tstop();
72#ifdef SIGWINCH
73sigret_t winch();
74#endif
75
76volatile sig_atomic_t leaveflag;
77volatile sig_atomic_t tstopflag;
78volatile sig_atomic_t winchflag;
79
80/* internal routines */
81void quit();
82
83/* values which need to be accessed by signal handlers */
84static int max_topn; /* maximum displayable processes */
85
86/* miscellaneous things */
87struct process_select ps;
88char *myname = "top";
89jmp_buf jmp_int;
90
91/* routines that don't return int */
92
93char *username();
94char *ctime();
95char *kill_procs();
96char *renice_procs();
97
98#ifdef ORDER
99extern int (*compares[])();
100#else
101extern int proc_compare();
102extern int io_compare();
103#endif
104time_t time();
105
106caddr_t get_process_info();
107
108/* different routines for displaying the user's identification */
109/* (values assigned to get_userid) */
110char *username();
111char *itoa7();
112
113/* display routines that need to be predeclared */
114int i_loadave();
115int u_loadave();
116int i_procstates();
117int u_procstates();
118int i_cpustates();
119int u_cpustates();
120int i_memory();
121int u_memory();
122int i_swap();
123int u_swap();
124int i_message();
125int u_message();
126int i_header();
127int u_header();
128int i_process();
129int u_process();
130
131/* pointers to display routines */
132int (*d_loadave)() = i_loadave;
133int (*d_procstates)() = i_procstates;
134int (*d_cpustates)() = i_cpustates;
135int (*d_memory)() = i_memory;
136int (*d_swap)() = i_swap;
137int (*d_message)() = i_message;
138int (*d_header)() = i_header;
139int (*d_process)() = i_process;
140
141
142main(argc, argv)
143
144int argc;
145char *argv[];
146
147{
148 register int i;
149 register int active_procs;
150 register int change;
151
152 struct system_info system_info;
153 struct statics statics;
154 caddr_t processes;
155
156 static char tempbuf1[50];
157 static char tempbuf2[50];
158 int old_sigmask; /* only used for BSD-style signals */
159 int topn = Default_TOPN;
160 int delay = Default_DELAY;
161 int displays = 0; /* indicates unspecified */
162 int sel_ret = 0;
163 time_t curr_time;
164 char *(*get_userid)() = username;
165 char *uname_field = "USERNAME";
166 char *header_text;
167 char *env_top;
168 char **preset_argv;
169 int preset_argc = 0;
170 char **av;
171 int ac;
172 char dostates = No;
173 char do_unames = Yes;
174 char interactive = Maybe;
175 char warnings = 0;
176#if Default_TOPN == Infinity
177 char topn_specified = No;
178#endif
179 char ch;
180 char *iptr;
181 char no_command = 1;
182 struct timeval timeout;
183#ifdef ORDER
184 char *order_name = NULL;
185 int order_index = 0;
186#endif
187#ifndef FD_SET
188 /* FD_SET and friends are not present: fake it */
189 typedef int fd_set;
190#define FD_ZERO(x) (*(x) = 0)
191#define FD_SET(f, x) (*(x) = 1<<f)
192#endif
193 fd_set readfds;
194
195#ifdef ORDER
196 static char command_chars[] = "\f qh?en#sdkriIutHmSCo";
197#else
198 static char command_chars[] = "\f qh?en#sdkriIutHmSC";
199#endif
200/* these defines enumerate the "strchr"s of the commands in command_chars */
201#define CMD_redraw 0
202#define CMD_update 1
203#define CMD_quit 2
204#define CMD_help1 3
205#define CMD_help2 4
206#define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */
207#define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */
208#define CMD_number1 6
209#define CMD_number2 7
210#define CMD_delay 8
211#define CMD_displays 9
212#define CMD_kill 10
213#define CMD_renice 11
214#define CMD_idletog 12
215#define CMD_idletog2 13
216#define CMD_user 14
217#define CMD_selftog 15
218#define CMD_thrtog 16
219#define CMD_viewtog 17
220#define CMD_viewsys 18
221#define CMD_wcputog 19
222#ifdef ORDER
223#define CMD_order 20
224#endif
225
226 /* set the buffer for stdout */
227#ifdef DEBUG
228 extern FILE *debug;
229 debug = fopen("debug.run", "w");
230 setbuffer(stdout, NULL, 0);
231#else
232 setbuffer(stdout, stdoutbuf, Buffersize);
233#endif
234
235 /* get our name */
236 if (argc > 0)
237 {
238 if ((myname = strrchr(argv[0], '/')) == 0)
239 {
240 myname = argv[0];
241 }
242 else
243 {
244 myname++;
245 }
246 }
247
248 /* initialize some selection options */
249 ps.idle = Yes;
250 ps.self = -1;
251 ps.system = No;
252 ps.uid = -1;
253 ps.thread = No;
254 ps.wcpu = 1;
255 ps.command = NULL;
256
257 /* get preset options from the environment */
258 if ((env_top = getenv("TOP")) != NULL)
259 {
260 av = preset_argv = argparse(env_top, &preset_argc);
261 ac = preset_argc;
262
263 /* set the dummy argument to an explanatory message, in case
264 getopt encounters a bad argument */
265 preset_argv[0] = "while processing environment";
266 }
267
268 /* process options */
269 do {
270 /* if we're done doing the presets, then process the real arguments */
271 if (preset_argc == 0)
272 {
273 ac = argc;
274 av = argv;
275
276 /* this should keep getopt happy... */
277 optind = 1;
278 }
279
280 while ((i = getopt(ac, av, "CSIHbinquvs:d:U:m:o:t")) != EOF)
281 {
282 switch(i)
283 {
284 case 'v': /* show version number */
285 fprintf(stderr, "%s: version %s\n",
286 myname, version_string());
287 exit(1);
288 break;
289
290 case 'u': /* toggle uid/username display */
291 do_unames = !do_unames;
292 break;
293
294 case 'U': /* display only username's processes */
295 if ((ps.uid = userid(optarg)) == -1)
296 {
297 fprintf(stderr, "%s: unknown user\n", optarg);
298 exit(1);
299 }
300 break;
301
302 case 'S': /* show system processes */
303 ps.system = !ps.system;
304 break;
305
306 case 'I': /* show idle processes */
307 ps.idle = !ps.idle;
308 break;
309
310 case 'i': /* go interactive regardless */
311 interactive = Yes;
312 break;
313
314 case 'n': /* batch, or non-interactive */
315 case 'b':
316 interactive = No;
317 break;
318
319 case 'd': /* number of displays to show */
320 if ((i = atoiwi(optarg)) == Invalid || i == 0)
321 {
322 fprintf(stderr,
323 "%s: warning: display count should be positive -- option ignored\n",
324 myname);
325 warnings++;
326 }
327 else
328 {
329 displays = i;
330 }
331 break;
332
333 case 's':
334 if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0))
335 {
336 fprintf(stderr,
337 "%s: warning: seconds delay should be positive -- using default\n",
338 myname);
339 delay = Default_DELAY;
340 warnings++;
341 }
342 break;
343
344 case 'q': /* be quick about it */
345 /* only allow this if user is really root */
346 if (getuid() == 0)
347 {
348 /* be very un-nice! */
349 (void) nice(-20);
350 }
351 else
352 {
353 fprintf(stderr,
354 "%s: warning: `-q' option can only be used by root\n",
355 myname);
356 warnings++;
357 }
358 break;
359
360 case 'm': /* select display mode */
361 if (strcmp(optarg, "io") == 0) {
362 displaymode = DISP_IO;
363 } else if (strcmp(optarg, "cpu") == 0) {
364 displaymode = DISP_CPU;
365 } else {
366 fprintf(stderr,
367 "%s: warning: `-m' option can only take args "
368 "'io' or 'cpu'\n",
369 myname);
370 exit(1);
371 }
372 break;
373
374 case 'o': /* select sort order */
375#ifdef ORDER
376 order_name = optarg;
377#else
378 fprintf(stderr,
379 "%s: this platform does not support arbitrary ordering. Sorry.\n",
380 myname);
381 warnings++;
382#endif
383 break;
384
385 case 't':
386 ps.self = (ps.self == -1) ? getpid() : -1;
387 break;
388
389 case 'C':
390 ps.wcpu = !ps.wcpu;
391 break;
392
393 case 'H':
394 ps.thread = !ps.thread;
395 break;
396
397 default:
17 */
18
19/*
20 * See the file "Changes" for information on version-to-version changes.
21 */
22
23/*
24 * This file contains "main" and other high-level routines.
25 */
26
27/*
28 * The following preprocessor variables, when defined, are used to
29 * distinguish between different Unix implementations:
30 *
31 * SIGHOLD - use SVR4 sighold function when defined
32 * SIGRELSE - use SVR4 sigrelse function when defined
33 * FD_SET - macros FD_SET and FD_ZERO are used when defined
34 */
35
36#include "os.h"
37#include <errno.h>
38#include <signal.h>
39#include <setjmp.h>
40#include <ctype.h>
41#include <sys/time.h>
42
43/* includes specific to top */
44#include "display.h" /* interface to display package */
45#include "screen.h" /* interface to screen package */
46#include "top.h"
47#include "top.local.h"
48#include "boolean.h"
49#include "machine.h"
50#include "utils.h"
51
52/* Size of the stdio buffer given to stdout */
53#define Buffersize 2048
54
55/* The buffer that stdio will use */
56char stdoutbuf[Buffersize];
57
58/* build Signal masks */
59#define Smask(s) (1 << ((s) - 1))
60
61/* for getopt: */
62extern int optind;
63extern char *optarg;
64
65/* imported from screen.c */
66extern int overstrike;
67
68/* signal handling routines */
69sigret_t leave();
70sigret_t onalrm();
71sigret_t tstop();
72#ifdef SIGWINCH
73sigret_t winch();
74#endif
75
76volatile sig_atomic_t leaveflag;
77volatile sig_atomic_t tstopflag;
78volatile sig_atomic_t winchflag;
79
80/* internal routines */
81void quit();
82
83/* values which need to be accessed by signal handlers */
84static int max_topn; /* maximum displayable processes */
85
86/* miscellaneous things */
87struct process_select ps;
88char *myname = "top";
89jmp_buf jmp_int;
90
91/* routines that don't return int */
92
93char *username();
94char *ctime();
95char *kill_procs();
96char *renice_procs();
97
98#ifdef ORDER
99extern int (*compares[])();
100#else
101extern int proc_compare();
102extern int io_compare();
103#endif
104time_t time();
105
106caddr_t get_process_info();
107
108/* different routines for displaying the user's identification */
109/* (values assigned to get_userid) */
110char *username();
111char *itoa7();
112
113/* display routines that need to be predeclared */
114int i_loadave();
115int u_loadave();
116int i_procstates();
117int u_procstates();
118int i_cpustates();
119int u_cpustates();
120int i_memory();
121int u_memory();
122int i_swap();
123int u_swap();
124int i_message();
125int u_message();
126int i_header();
127int u_header();
128int i_process();
129int u_process();
130
131/* pointers to display routines */
132int (*d_loadave)() = i_loadave;
133int (*d_procstates)() = i_procstates;
134int (*d_cpustates)() = i_cpustates;
135int (*d_memory)() = i_memory;
136int (*d_swap)() = i_swap;
137int (*d_message)() = i_message;
138int (*d_header)() = i_header;
139int (*d_process)() = i_process;
140
141
142main(argc, argv)
143
144int argc;
145char *argv[];
146
147{
148 register int i;
149 register int active_procs;
150 register int change;
151
152 struct system_info system_info;
153 struct statics statics;
154 caddr_t processes;
155
156 static char tempbuf1[50];
157 static char tempbuf2[50];
158 int old_sigmask; /* only used for BSD-style signals */
159 int topn = Default_TOPN;
160 int delay = Default_DELAY;
161 int displays = 0; /* indicates unspecified */
162 int sel_ret = 0;
163 time_t curr_time;
164 char *(*get_userid)() = username;
165 char *uname_field = "USERNAME";
166 char *header_text;
167 char *env_top;
168 char **preset_argv;
169 int preset_argc = 0;
170 char **av;
171 int ac;
172 char dostates = No;
173 char do_unames = Yes;
174 char interactive = Maybe;
175 char warnings = 0;
176#if Default_TOPN == Infinity
177 char topn_specified = No;
178#endif
179 char ch;
180 char *iptr;
181 char no_command = 1;
182 struct timeval timeout;
183#ifdef ORDER
184 char *order_name = NULL;
185 int order_index = 0;
186#endif
187#ifndef FD_SET
188 /* FD_SET and friends are not present: fake it */
189 typedef int fd_set;
190#define FD_ZERO(x) (*(x) = 0)
191#define FD_SET(f, x) (*(x) = 1<<f)
192#endif
193 fd_set readfds;
194
195#ifdef ORDER
196 static char command_chars[] = "\f qh?en#sdkriIutHmSCo";
197#else
198 static char command_chars[] = "\f qh?en#sdkriIutHmSC";
199#endif
200/* these defines enumerate the "strchr"s of the commands in command_chars */
201#define CMD_redraw 0
202#define CMD_update 1
203#define CMD_quit 2
204#define CMD_help1 3
205#define CMD_help2 4
206#define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */
207#define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */
208#define CMD_number1 6
209#define CMD_number2 7
210#define CMD_delay 8
211#define CMD_displays 9
212#define CMD_kill 10
213#define CMD_renice 11
214#define CMD_idletog 12
215#define CMD_idletog2 13
216#define CMD_user 14
217#define CMD_selftog 15
218#define CMD_thrtog 16
219#define CMD_viewtog 17
220#define CMD_viewsys 18
221#define CMD_wcputog 19
222#ifdef ORDER
223#define CMD_order 20
224#endif
225
226 /* set the buffer for stdout */
227#ifdef DEBUG
228 extern FILE *debug;
229 debug = fopen("debug.run", "w");
230 setbuffer(stdout, NULL, 0);
231#else
232 setbuffer(stdout, stdoutbuf, Buffersize);
233#endif
234
235 /* get our name */
236 if (argc > 0)
237 {
238 if ((myname = strrchr(argv[0], '/')) == 0)
239 {
240 myname = argv[0];
241 }
242 else
243 {
244 myname++;
245 }
246 }
247
248 /* initialize some selection options */
249 ps.idle = Yes;
250 ps.self = -1;
251 ps.system = No;
252 ps.uid = -1;
253 ps.thread = No;
254 ps.wcpu = 1;
255 ps.command = NULL;
256
257 /* get preset options from the environment */
258 if ((env_top = getenv("TOP")) != NULL)
259 {
260 av = preset_argv = argparse(env_top, &preset_argc);
261 ac = preset_argc;
262
263 /* set the dummy argument to an explanatory message, in case
264 getopt encounters a bad argument */
265 preset_argv[0] = "while processing environment";
266 }
267
268 /* process options */
269 do {
270 /* if we're done doing the presets, then process the real arguments */
271 if (preset_argc == 0)
272 {
273 ac = argc;
274 av = argv;
275
276 /* this should keep getopt happy... */
277 optind = 1;
278 }
279
280 while ((i = getopt(ac, av, "CSIHbinquvs:d:U:m:o:t")) != EOF)
281 {
282 switch(i)
283 {
284 case 'v': /* show version number */
285 fprintf(stderr, "%s: version %s\n",
286 myname, version_string());
287 exit(1);
288 break;
289
290 case 'u': /* toggle uid/username display */
291 do_unames = !do_unames;
292 break;
293
294 case 'U': /* display only username's processes */
295 if ((ps.uid = userid(optarg)) == -1)
296 {
297 fprintf(stderr, "%s: unknown user\n", optarg);
298 exit(1);
299 }
300 break;
301
302 case 'S': /* show system processes */
303 ps.system = !ps.system;
304 break;
305
306 case 'I': /* show idle processes */
307 ps.idle = !ps.idle;
308 break;
309
310 case 'i': /* go interactive regardless */
311 interactive = Yes;
312 break;
313
314 case 'n': /* batch, or non-interactive */
315 case 'b':
316 interactive = No;
317 break;
318
319 case 'd': /* number of displays to show */
320 if ((i = atoiwi(optarg)) == Invalid || i == 0)
321 {
322 fprintf(stderr,
323 "%s: warning: display count should be positive -- option ignored\n",
324 myname);
325 warnings++;
326 }
327 else
328 {
329 displays = i;
330 }
331 break;
332
333 case 's':
334 if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0))
335 {
336 fprintf(stderr,
337 "%s: warning: seconds delay should be positive -- using default\n",
338 myname);
339 delay = Default_DELAY;
340 warnings++;
341 }
342 break;
343
344 case 'q': /* be quick about it */
345 /* only allow this if user is really root */
346 if (getuid() == 0)
347 {
348 /* be very un-nice! */
349 (void) nice(-20);
350 }
351 else
352 {
353 fprintf(stderr,
354 "%s: warning: `-q' option can only be used by root\n",
355 myname);
356 warnings++;
357 }
358 break;
359
360 case 'm': /* select display mode */
361 if (strcmp(optarg, "io") == 0) {
362 displaymode = DISP_IO;
363 } else if (strcmp(optarg, "cpu") == 0) {
364 displaymode = DISP_CPU;
365 } else {
366 fprintf(stderr,
367 "%s: warning: `-m' option can only take args "
368 "'io' or 'cpu'\n",
369 myname);
370 exit(1);
371 }
372 break;
373
374 case 'o': /* select sort order */
375#ifdef ORDER
376 order_name = optarg;
377#else
378 fprintf(stderr,
379 "%s: this platform does not support arbitrary ordering. Sorry.\n",
380 myname);
381 warnings++;
382#endif
383 break;
384
385 case 't':
386 ps.self = (ps.self == -1) ? getpid() : -1;
387 break;
388
389 case 'C':
390 ps.wcpu = !ps.wcpu;
391 break;
392
393 case 'H':
394 ps.thread = !ps.thread;
395 break;
396
397 default:
398 fprintf(stderr, "\
399Top version %s\n\
400Usage: %s [-CHISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n",
398 fprintf(stderr,
399"Top version %s\n"
400"Usage: %s [-bHIinqStuv] [-d count] [-m io | cpu] [-o field] [-s time]\n"
401" [-U username] [number]\n",
401 version_string(), myname);
402 exit(1);
403 }
404 }
405
406 /* get count of top processes to display (if any) */
407 if (optind < ac)
408 {
409 if ((topn = atoiwi(av[optind])) == Invalid)
410 {
411 fprintf(stderr,
412 "%s: warning: process display count should be non-negative -- using default\n",
413 myname);
414 warnings++;
415 }
416#if Default_TOPN == Infinity
417 else
418 {
419 topn_specified = Yes;
420 }
421#endif
422 }
423
424 /* tricky: remember old value of preset_argc & set preset_argc = 0 */
425 i = preset_argc;
426 preset_argc = 0;
427
428 /* repeat only if we really did the preset arguments */
429 } while (i != 0);
430
431 /* set constants for username/uid display correctly */
432 if (!do_unames)
433 {
434 uname_field = " UID ";
435 get_userid = itoa7;
436 }
437
438 /* initialize the kernel memory interface */
439 if (machine_init(&statics) == -1)
440 {
441 exit(1);
442 }
443
444#ifdef ORDER
445 /* determine sorting order index, if necessary */
446 if (order_name != NULL)
447 {
448 if ((order_index = string_index(order_name, statics.order_names)) == -1)
449 {
450 char **pp;
451
452 fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n",
453 myname, order_name);
454 fprintf(stderr, "\tTry one of these:");
455 pp = statics.order_names;
456 while (*pp != NULL)
457 {
458 fprintf(stderr, " %s", *pp++);
459 }
460 fputc('\n', stderr);
461 exit(1);
462 }
463 }
464#endif
465
466#ifdef no_initialization_needed
467 /* initialize the hashing stuff */
468 if (do_unames)
469 {
470 init_hash();
471 }
472#endif
473
474 /* initialize termcap */
475 init_termcap(interactive);
476
477 /* get the string to use for the process area header */
478 header_text = format_header(uname_field);
479
480 /* initialize display interface */
481 if ((max_topn = display_init(&statics)) == -1)
482 {
483 fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
484 exit(4);
485 }
486
487 /* print warning if user requested more processes than we can display */
488 if (topn > max_topn)
489 {
490 fprintf(stderr,
491 "%s: warning: this terminal can only display %d processes.\n",
492 myname, max_topn);
493 warnings++;
494 }
495
496 /* adjust for topn == Infinity */
497 if (topn == Infinity)
498 {
499 /*
500 * For smart terminals, infinity really means everything that can
501 * be displayed, or Largest.
502 * On dumb terminals, infinity means every process in the system!
503 * We only really want to do that if it was explicitly specified.
504 * This is always the case when "Default_TOPN != Infinity". But if
505 * topn wasn't explicitly specified and we are on a dumb terminal
506 * and the default is Infinity, then (and only then) we use
507 * "Nominal_TOPN" instead.
508 */
509#if Default_TOPN == Infinity
510 topn = smart_terminal ? Largest :
511 (topn_specified ? Largest : Nominal_TOPN);
512#else
513 topn = Largest;
514#endif
515 }
516
517 /* set header display accordingly */
518 display_header(topn > 0);
519
520 /* determine interactive state */
521 if (interactive == Maybe)
522 {
523 interactive = smart_terminal;
524 }
525
526 /* if # of displays not specified, fill it in */
527 if (displays == 0)
528 {
529 displays = smart_terminal ? Infinity : 1;
530 }
531
532 /* hold interrupt signals while setting up the screen and the handlers */
533#ifdef SIGHOLD
534 sighold(SIGINT);
535 sighold(SIGQUIT);
536 sighold(SIGTSTP);
537#else
538 old_sigmask = sigblock(Smask(SIGINT) | Smask(SIGQUIT) | Smask(SIGTSTP));
539#endif
540 init_screen();
541 (void) signal(SIGINT, leave);
542 (void) signal(SIGQUIT, leave);
543 (void) signal(SIGTSTP, tstop);
544#ifdef SIGWINCH
545 (void) signal(SIGWINCH, winch);
546#endif
547#ifdef SIGRELSE
548 sigrelse(SIGINT);
549 sigrelse(SIGQUIT);
550 sigrelse(SIGTSTP);
551#else
552 (void) sigsetmask(old_sigmask);
553#endif
554 if (warnings)
555 {
556 fputs("....", stderr);
557 fflush(stderr); /* why must I do this? */
558 sleep((unsigned)(3 * warnings));
559 fputc('\n', stderr);
560 }
561
562restart:
563
564 /*
565 * main loop -- repeat while display count is positive or while it
566 * indicates infinity (by being -1)
567 */
568
569 while ((displays == -1) || (displays-- > 0))
570 {
571 int (*compare)();
572
573
574 /* get the current stats */
575 get_system_info(&system_info);
576
577#ifdef ORDER
578 compare = compares[order_index];
579#else
580 if (displaymode == DISP_CPU)
581 compare = proc_compare;
582 else
583 compare = io_compare;
584#endif
585
586 /* get the current set of processes */
587 processes =
588 get_process_info(&system_info, &ps, compare);
589
590 /* display the load averages */
591 (*d_loadave)(system_info.last_pid,
592 system_info.load_avg);
593
594 /* display the current time */
595 /* this method of getting the time SHOULD be fairly portable */
596 time(&curr_time);
597 i_uptime(&system_info.boottime, &curr_time);
598 i_timeofday(&curr_time);
599
600 /* display process state breakdown */
601 (*d_procstates)(system_info.p_total,
602 system_info.procstates);
603
604 /* display the cpu state percentage breakdown */
605 if (dostates) /* but not the first time */
606 {
607 (*d_cpustates)(system_info.cpustates);
608 }
609 else
610 {
611 /* we'll do it next time */
612 if (smart_terminal)
613 {
614 z_cpustates();
615 }
616 else
617 {
618 putchar('\n');
619 }
620 dostates = Yes;
621 }
622
623 /* display memory stats */
624 (*d_memory)(system_info.memory);
625
626 /* display swap stats */
627 (*d_swap)(system_info.swap);
628
629 /* handle message area */
630 (*d_message)();
631
632 /* update the header area */
633 (*d_header)(header_text);
634
635 if (topn > 0)
636 {
637 /* determine number of processes to actually display */
638 /* this number will be the smallest of: active processes,
639 number user requested, number current screen accomodates */
640 active_procs = system_info.P_ACTIVE;
641 if (active_procs > topn)
642 {
643 active_procs = topn;
644 }
645 if (active_procs > max_topn)
646 {
647 active_procs = max_topn;
648 }
649
650 /* now show the top "n" processes. */
651 for (i = 0; i < active_procs; i++)
652 {
653 (*d_process)(i, format_next_process(processes, get_userid));
654 }
655 }
656 else
657 {
658 i = 0;
659 }
660
661 /* do end-screen processing */
662 u_endscreen(i);
663
664 /* now, flush the output buffer */
665 if (fflush(stdout) != 0)
666 {
667 new_message(MT_standout, " Write error on stdout");
668 putchar('\r');
669 quit(1);
670 /*NOTREACHED*/
671 }
672
673 /* only do the rest if we have more displays to show */
674 if (displays)
675 {
676 /* switch out for new display on smart terminals */
677 if (smart_terminal)
678 {
679 if (overstrike)
680 {
681 reset_display();
682 }
683 else
684 {
685 d_loadave = u_loadave;
686 d_procstates = u_procstates;
687 d_cpustates = u_cpustates;
688 d_memory = u_memory;
689 d_swap = u_swap;
690 d_message = u_message;
691 d_header = u_header;
692 d_process = u_process;
693 }
694 }
695
696 no_command = Yes;
697 if (!interactive)
698 {
699 /* set up alarm */
700 (void) signal(SIGALRM, onalrm);
701 (void) alarm((unsigned)delay);
702
703 /* wait for the rest of it .... */
704 pause();
705 }
706 else while (no_command)
707 {
708 /* assume valid command unless told otherwise */
709 no_command = No;
710
711 /* set up arguments for select with timeout */
712 FD_ZERO(&readfds);
713 FD_SET(0, &readfds); /* for standard input */
714 timeout.tv_sec = delay;
715 timeout.tv_usec = 0;
716
717 if (leaveflag) {
718 end_screen();
719 exit(0);
720 }
721
722 if (tstopflag) {
723 /* move to the lower left */
724 end_screen();
725 fflush(stdout);
726
727 /* default the signal handler action */
728 (void) signal(SIGTSTP, SIG_DFL);
729
730 /* unblock the signal and send ourselves one */
731#ifdef SIGRELSE
732 sigrelse(SIGTSTP);
733#else
734 (void) sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1)));
735#endif
736 (void) kill(0, SIGTSTP);
737
738 /* reset the signal handler */
739 (void) signal(SIGTSTP, tstop);
740
741 /* reinit screen */
742 reinit_screen();
743 reset_display();
744 tstopflag = 0;
745 goto restart;
746 }
747
748 if (winchflag) {
749 /* reascertain the screen dimensions */
750 get_screensize();
751
752 /* tell display to resize */
753 max_topn = display_resize();
754
755 /* reset the signal handler */
756 (void) signal(SIGWINCH, winch);
757
758 reset_display();
759 winchflag = 0;
760 goto restart;
761 }
762
763 /* wait for either input or the end of the delay period */
764 sel_ret = select(2, &readfds, NULL, NULL, &timeout);
765 if (sel_ret < 0 && errno != EINTR)
766 quit(0);
767 if (sel_ret > 0)
768 {
769 int newval;
770 char *errmsg;
771
772 /* something to read -- clear the message area first */
773 clear_message();
774
775 /* now read it and convert to command strchr */
776 /* (use "change" as a temporary to hold strchr) */
777 if (read(0, &ch, 1) != 1)
778 {
779 /* read error: either 0 or -1 */
780 new_message(MT_standout, " Read error on stdin");
781 putchar('\r');
782 quit(1);
783 /*NOTREACHED*/
784 }
785 if ((iptr = strchr(command_chars, ch)) == NULL)
786 {
787 if (ch != '\r' && ch != '\n')
788 {
789 /* illegal command */
790 new_message(MT_standout, " Command not understood");
791 }
792 putchar('\r');
793 no_command = Yes;
794 }
795 else
796 {
797 change = iptr - command_chars;
798 if (overstrike && change > CMD_OSLIMIT)
799 {
800 /* error */
801 new_message(MT_standout,
802 " Command cannot be handled by this terminal");
803 putchar('\r');
804 no_command = Yes;
805 }
806 else switch(change)
807 {
808 case CMD_redraw: /* redraw screen */
809 reset_display();
810 break;
811
812 case CMD_update: /* merely update display */
813 /* is the load average high? */
814 if (system_info.load_avg[0] > LoadMax)
815 {
816 /* yes, go home for visual feedback */
817 go_home();
818 fflush(stdout);
819 }
820 break;
821
822 case CMD_quit: /* quit */
823 quit(0);
824 /*NOTREACHED*/
825 break;
826
827 case CMD_help1: /* help */
828 case CMD_help2:
829 reset_display();
830 clear();
831 show_help();
832 standout("Hit any key to continue: ");
833 fflush(stdout);
834 (void) read(0, &ch, 1);
835 break;
836
837 case CMD_errors: /* show errors */
838 if (error_count() == 0)
839 {
840 new_message(MT_standout,
841 " Currently no errors to report.");
842 putchar('\r');
843 no_command = Yes;
844 }
845 else
846 {
847 reset_display();
848 clear();
849 show_errors();
850 standout("Hit any key to continue: ");
851 fflush(stdout);
852 (void) read(0, &ch, 1);
853 }
854 break;
855
856 case CMD_number1: /* new number */
857 case CMD_number2:
858 new_message(MT_standout,
859 "Number of processes to show: ");
860 newval = readline(tempbuf1, 8, Yes);
861 if (newval > -1)
862 {
863 if (newval > max_topn)
864 {
865 new_message(MT_standout | MT_delayed,
866 " This terminal can only display %d processes.",
867 max_topn);
868 putchar('\r');
869 }
870
871 if (newval == 0)
872 {
873 /* inhibit the header */
874 display_header(No);
875 }
876 else if (newval > topn && topn == 0)
877 {
878 /* redraw the header */
879 display_header(Yes);
880 d_header = i_header;
881 }
882 topn = newval;
883 }
884 break;
885
886 case CMD_delay: /* new seconds delay */
887 new_message(MT_standout, "Seconds to delay: ");
888 if ((i = readline(tempbuf1, 8, Yes)) > -1)
889 {
890 if ((delay = i) == 0 && getuid() != 0)
891 {
892 delay = 1;
893 }
894 }
895 clear_message();
896 break;
897
898 case CMD_displays: /* change display count */
899 new_message(MT_standout,
900 "Displays to show (currently %s): ",
901 displays == -1 ? "infinite" :
902 itoa(displays));
903 if ((i = readline(tempbuf1, 10, Yes)) > 0)
904 {
905 displays = i;
906 }
907 else if (i == 0)
908 {
909 quit(0);
910 }
911 clear_message();
912 break;
913
914 case CMD_kill: /* kill program */
915 new_message(0, "kill ");
916 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
917 {
918 if ((errmsg = kill_procs(tempbuf2)) != NULL)
919 {
920 new_message(MT_standout, "%s", errmsg);
921 putchar('\r');
922 no_command = Yes;
923 }
924 }
925 else
926 {
927 clear_message();
928 }
929 break;
930
931 case CMD_renice: /* renice program */
932 new_message(0, "renice ");
933 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
934 {
935 if ((errmsg = renice_procs(tempbuf2)) != NULL)
936 {
937 new_message(MT_standout, "%s", errmsg);
938 putchar('\r');
939 no_command = Yes;
940 }
941 }
942 else
943 {
944 clear_message();
945 }
946 break;
947
948 case CMD_idletog:
949 case CMD_idletog2:
950 ps.idle = !ps.idle;
951 new_message(MT_standout | MT_delayed,
952 " %sisplaying idle processes.",
953 ps.idle ? "D" : "Not d");
954 putchar('\r');
955 break;
956
957 case CMD_selftog:
958 ps.self = (ps.self == -1) ? getpid() : -1;
959 new_message(MT_standout | MT_delayed,
960 " %sisplaying self.",
961 (ps.self == -1) ? "D" : "Not d");
962 putchar('\r');
963 break;
964
965 case CMD_user:
966 new_message(MT_standout,
967 "Username to show: ");
968 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
969 {
970 if (tempbuf2[0] == '+' &&
971 tempbuf2[1] == '\0')
972 {
973 ps.uid = -1;
974 }
975 else if ((i = userid(tempbuf2)) == -1)
976 {
977 new_message(MT_standout,
978 " %s: unknown user", tempbuf2);
979 no_command = Yes;
980 }
981 else
982 {
983 ps.uid = i;
984 }
985 putchar('\r');
986 }
987 else
988 {
989 clear_message();
990 }
991 break;
992
993 case CMD_thrtog:
994 ps.thread = !ps.thread;
995 new_message(MT_standout | MT_delayed,
996 "Displaying threads %s",
997 ps.thread ? "separately" : "as a count");
998 header_text = format_header(uname_field);
999 reset_display();
1000 putchar('\r');
1001 break;
1002 case CMD_wcputog:
1003 ps.wcpu = !ps.wcpu;
1004 new_message(MT_standout | MT_delayed,
1005 "Displaying %sCPU",
1006 ps.wcpu ? "W" : "");
1007 header_text = format_header(uname_field);
1008 reset_display();
1009 putchar('\r');
1010 break;
1011 case CMD_viewtog:
1012 if (++displaymode == DISP_MAX)
1013 displaymode = 0;
1014 header_text = format_header(uname_field);
1015 display_header(Yes);
1016 d_header = i_header;
1017 reset_display();
1018 break;
1019 case CMD_viewsys:
1020 ps.system = !ps.system;
1021 break;
1022#ifdef ORDER
1023 case CMD_order:
1024 new_message(MT_standout,
1025 "Order to sort: ");
1026 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
1027 {
1028 if ((i = string_index(tempbuf2, statics.order_names)) == -1)
1029 {
1030 new_message(MT_standout,
1031 " %s: unrecognized sorting order", tempbuf2);
1032 no_command = Yes;
1033 }
1034 else
1035 {
1036 order_index = i;
1037 }
1038 putchar('\r');
1039 }
1040 else
1041 {
1042 clear_message();
1043 }
1044 break;
1045#endif
1046
1047 default:
1048 new_message(MT_standout, " BAD CASE IN SWITCH!");
1049 putchar('\r');
1050 }
1051 }
1052
1053 /* flush out stuff that may have been written */
1054 fflush(stdout);
1055 }
1056 }
1057 }
1058 }
1059
1060#ifdef DEBUG
1061 fclose(debug);
1062#endif
1063 quit(0);
1064 /*NOTREACHED*/
1065}
1066
1067/*
1068 * reset_display() - reset all the display routine pointers so that entire
1069 * screen will get redrawn.
1070 */
1071
1072reset_display()
1073
1074{
1075 d_loadave = i_loadave;
1076 d_procstates = i_procstates;
1077 d_cpustates = i_cpustates;
1078 d_memory = i_memory;
1079 d_swap = i_swap;
1080 d_message = i_message;
1081 d_header = i_header;
1082 d_process = i_process;
1083}
1084
1085/*
1086 * signal handlers
1087 */
1088
1089sigret_t leave() /* exit under normal conditions -- INT handler */
1090
1091{
1092 leaveflag = 1;
1093}
1094
1095sigret_t tstop(i) /* SIGTSTP handler */
1096
1097int i;
1098
1099{
1100 tstopflag = 1;
1101}
1102
1103#ifdef SIGWINCH
1104sigret_t winch(i) /* SIGWINCH handler */
1105
1106int i;
1107
1108{
1109 winchflag = 1;
1110}
1111#endif
1112
1113void quit(status) /* exit under duress */
1114
1115int status;
1116
1117{
1118 end_screen();
1119 exit(status);
1120 /*NOTREACHED*/
1121}
1122
1123sigret_t onalrm() /* SIGALRM handler */
1124
1125{
1126 /* this is only used in batch mode to break out of the pause() */
1127 /* return; */
1128}
1129
402 version_string(), myname);
403 exit(1);
404 }
405 }
406
407 /* get count of top processes to display (if any) */
408 if (optind < ac)
409 {
410 if ((topn = atoiwi(av[optind])) == Invalid)
411 {
412 fprintf(stderr,
413 "%s: warning: process display count should be non-negative -- using default\n",
414 myname);
415 warnings++;
416 }
417#if Default_TOPN == Infinity
418 else
419 {
420 topn_specified = Yes;
421 }
422#endif
423 }
424
425 /* tricky: remember old value of preset_argc & set preset_argc = 0 */
426 i = preset_argc;
427 preset_argc = 0;
428
429 /* repeat only if we really did the preset arguments */
430 } while (i != 0);
431
432 /* set constants for username/uid display correctly */
433 if (!do_unames)
434 {
435 uname_field = " UID ";
436 get_userid = itoa7;
437 }
438
439 /* initialize the kernel memory interface */
440 if (machine_init(&statics) == -1)
441 {
442 exit(1);
443 }
444
445#ifdef ORDER
446 /* determine sorting order index, if necessary */
447 if (order_name != NULL)
448 {
449 if ((order_index = string_index(order_name, statics.order_names)) == -1)
450 {
451 char **pp;
452
453 fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n",
454 myname, order_name);
455 fprintf(stderr, "\tTry one of these:");
456 pp = statics.order_names;
457 while (*pp != NULL)
458 {
459 fprintf(stderr, " %s", *pp++);
460 }
461 fputc('\n', stderr);
462 exit(1);
463 }
464 }
465#endif
466
467#ifdef no_initialization_needed
468 /* initialize the hashing stuff */
469 if (do_unames)
470 {
471 init_hash();
472 }
473#endif
474
475 /* initialize termcap */
476 init_termcap(interactive);
477
478 /* get the string to use for the process area header */
479 header_text = format_header(uname_field);
480
481 /* initialize display interface */
482 if ((max_topn = display_init(&statics)) == -1)
483 {
484 fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
485 exit(4);
486 }
487
488 /* print warning if user requested more processes than we can display */
489 if (topn > max_topn)
490 {
491 fprintf(stderr,
492 "%s: warning: this terminal can only display %d processes.\n",
493 myname, max_topn);
494 warnings++;
495 }
496
497 /* adjust for topn == Infinity */
498 if (topn == Infinity)
499 {
500 /*
501 * For smart terminals, infinity really means everything that can
502 * be displayed, or Largest.
503 * On dumb terminals, infinity means every process in the system!
504 * We only really want to do that if it was explicitly specified.
505 * This is always the case when "Default_TOPN != Infinity". But if
506 * topn wasn't explicitly specified and we are on a dumb terminal
507 * and the default is Infinity, then (and only then) we use
508 * "Nominal_TOPN" instead.
509 */
510#if Default_TOPN == Infinity
511 topn = smart_terminal ? Largest :
512 (topn_specified ? Largest : Nominal_TOPN);
513#else
514 topn = Largest;
515#endif
516 }
517
518 /* set header display accordingly */
519 display_header(topn > 0);
520
521 /* determine interactive state */
522 if (interactive == Maybe)
523 {
524 interactive = smart_terminal;
525 }
526
527 /* if # of displays not specified, fill it in */
528 if (displays == 0)
529 {
530 displays = smart_terminal ? Infinity : 1;
531 }
532
533 /* hold interrupt signals while setting up the screen and the handlers */
534#ifdef SIGHOLD
535 sighold(SIGINT);
536 sighold(SIGQUIT);
537 sighold(SIGTSTP);
538#else
539 old_sigmask = sigblock(Smask(SIGINT) | Smask(SIGQUIT) | Smask(SIGTSTP));
540#endif
541 init_screen();
542 (void) signal(SIGINT, leave);
543 (void) signal(SIGQUIT, leave);
544 (void) signal(SIGTSTP, tstop);
545#ifdef SIGWINCH
546 (void) signal(SIGWINCH, winch);
547#endif
548#ifdef SIGRELSE
549 sigrelse(SIGINT);
550 sigrelse(SIGQUIT);
551 sigrelse(SIGTSTP);
552#else
553 (void) sigsetmask(old_sigmask);
554#endif
555 if (warnings)
556 {
557 fputs("....", stderr);
558 fflush(stderr); /* why must I do this? */
559 sleep((unsigned)(3 * warnings));
560 fputc('\n', stderr);
561 }
562
563restart:
564
565 /*
566 * main loop -- repeat while display count is positive or while it
567 * indicates infinity (by being -1)
568 */
569
570 while ((displays == -1) || (displays-- > 0))
571 {
572 int (*compare)();
573
574
575 /* get the current stats */
576 get_system_info(&system_info);
577
578#ifdef ORDER
579 compare = compares[order_index];
580#else
581 if (displaymode == DISP_CPU)
582 compare = proc_compare;
583 else
584 compare = io_compare;
585#endif
586
587 /* get the current set of processes */
588 processes =
589 get_process_info(&system_info, &ps, compare);
590
591 /* display the load averages */
592 (*d_loadave)(system_info.last_pid,
593 system_info.load_avg);
594
595 /* display the current time */
596 /* this method of getting the time SHOULD be fairly portable */
597 time(&curr_time);
598 i_uptime(&system_info.boottime, &curr_time);
599 i_timeofday(&curr_time);
600
601 /* display process state breakdown */
602 (*d_procstates)(system_info.p_total,
603 system_info.procstates);
604
605 /* display the cpu state percentage breakdown */
606 if (dostates) /* but not the first time */
607 {
608 (*d_cpustates)(system_info.cpustates);
609 }
610 else
611 {
612 /* we'll do it next time */
613 if (smart_terminal)
614 {
615 z_cpustates();
616 }
617 else
618 {
619 putchar('\n');
620 }
621 dostates = Yes;
622 }
623
624 /* display memory stats */
625 (*d_memory)(system_info.memory);
626
627 /* display swap stats */
628 (*d_swap)(system_info.swap);
629
630 /* handle message area */
631 (*d_message)();
632
633 /* update the header area */
634 (*d_header)(header_text);
635
636 if (topn > 0)
637 {
638 /* determine number of processes to actually display */
639 /* this number will be the smallest of: active processes,
640 number user requested, number current screen accomodates */
641 active_procs = system_info.P_ACTIVE;
642 if (active_procs > topn)
643 {
644 active_procs = topn;
645 }
646 if (active_procs > max_topn)
647 {
648 active_procs = max_topn;
649 }
650
651 /* now show the top "n" processes. */
652 for (i = 0; i < active_procs; i++)
653 {
654 (*d_process)(i, format_next_process(processes, get_userid));
655 }
656 }
657 else
658 {
659 i = 0;
660 }
661
662 /* do end-screen processing */
663 u_endscreen(i);
664
665 /* now, flush the output buffer */
666 if (fflush(stdout) != 0)
667 {
668 new_message(MT_standout, " Write error on stdout");
669 putchar('\r');
670 quit(1);
671 /*NOTREACHED*/
672 }
673
674 /* only do the rest if we have more displays to show */
675 if (displays)
676 {
677 /* switch out for new display on smart terminals */
678 if (smart_terminal)
679 {
680 if (overstrike)
681 {
682 reset_display();
683 }
684 else
685 {
686 d_loadave = u_loadave;
687 d_procstates = u_procstates;
688 d_cpustates = u_cpustates;
689 d_memory = u_memory;
690 d_swap = u_swap;
691 d_message = u_message;
692 d_header = u_header;
693 d_process = u_process;
694 }
695 }
696
697 no_command = Yes;
698 if (!interactive)
699 {
700 /* set up alarm */
701 (void) signal(SIGALRM, onalrm);
702 (void) alarm((unsigned)delay);
703
704 /* wait for the rest of it .... */
705 pause();
706 }
707 else while (no_command)
708 {
709 /* assume valid command unless told otherwise */
710 no_command = No;
711
712 /* set up arguments for select with timeout */
713 FD_ZERO(&readfds);
714 FD_SET(0, &readfds); /* for standard input */
715 timeout.tv_sec = delay;
716 timeout.tv_usec = 0;
717
718 if (leaveflag) {
719 end_screen();
720 exit(0);
721 }
722
723 if (tstopflag) {
724 /* move to the lower left */
725 end_screen();
726 fflush(stdout);
727
728 /* default the signal handler action */
729 (void) signal(SIGTSTP, SIG_DFL);
730
731 /* unblock the signal and send ourselves one */
732#ifdef SIGRELSE
733 sigrelse(SIGTSTP);
734#else
735 (void) sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1)));
736#endif
737 (void) kill(0, SIGTSTP);
738
739 /* reset the signal handler */
740 (void) signal(SIGTSTP, tstop);
741
742 /* reinit screen */
743 reinit_screen();
744 reset_display();
745 tstopflag = 0;
746 goto restart;
747 }
748
749 if (winchflag) {
750 /* reascertain the screen dimensions */
751 get_screensize();
752
753 /* tell display to resize */
754 max_topn = display_resize();
755
756 /* reset the signal handler */
757 (void) signal(SIGWINCH, winch);
758
759 reset_display();
760 winchflag = 0;
761 goto restart;
762 }
763
764 /* wait for either input or the end of the delay period */
765 sel_ret = select(2, &readfds, NULL, NULL, &timeout);
766 if (sel_ret < 0 && errno != EINTR)
767 quit(0);
768 if (sel_ret > 0)
769 {
770 int newval;
771 char *errmsg;
772
773 /* something to read -- clear the message area first */
774 clear_message();
775
776 /* now read it and convert to command strchr */
777 /* (use "change" as a temporary to hold strchr) */
778 if (read(0, &ch, 1) != 1)
779 {
780 /* read error: either 0 or -1 */
781 new_message(MT_standout, " Read error on stdin");
782 putchar('\r');
783 quit(1);
784 /*NOTREACHED*/
785 }
786 if ((iptr = strchr(command_chars, ch)) == NULL)
787 {
788 if (ch != '\r' && ch != '\n')
789 {
790 /* illegal command */
791 new_message(MT_standout, " Command not understood");
792 }
793 putchar('\r');
794 no_command = Yes;
795 }
796 else
797 {
798 change = iptr - command_chars;
799 if (overstrike && change > CMD_OSLIMIT)
800 {
801 /* error */
802 new_message(MT_standout,
803 " Command cannot be handled by this terminal");
804 putchar('\r');
805 no_command = Yes;
806 }
807 else switch(change)
808 {
809 case CMD_redraw: /* redraw screen */
810 reset_display();
811 break;
812
813 case CMD_update: /* merely update display */
814 /* is the load average high? */
815 if (system_info.load_avg[0] > LoadMax)
816 {
817 /* yes, go home for visual feedback */
818 go_home();
819 fflush(stdout);
820 }
821 break;
822
823 case CMD_quit: /* quit */
824 quit(0);
825 /*NOTREACHED*/
826 break;
827
828 case CMD_help1: /* help */
829 case CMD_help2:
830 reset_display();
831 clear();
832 show_help();
833 standout("Hit any key to continue: ");
834 fflush(stdout);
835 (void) read(0, &ch, 1);
836 break;
837
838 case CMD_errors: /* show errors */
839 if (error_count() == 0)
840 {
841 new_message(MT_standout,
842 " Currently no errors to report.");
843 putchar('\r');
844 no_command = Yes;
845 }
846 else
847 {
848 reset_display();
849 clear();
850 show_errors();
851 standout("Hit any key to continue: ");
852 fflush(stdout);
853 (void) read(0, &ch, 1);
854 }
855 break;
856
857 case CMD_number1: /* new number */
858 case CMD_number2:
859 new_message(MT_standout,
860 "Number of processes to show: ");
861 newval = readline(tempbuf1, 8, Yes);
862 if (newval > -1)
863 {
864 if (newval > max_topn)
865 {
866 new_message(MT_standout | MT_delayed,
867 " This terminal can only display %d processes.",
868 max_topn);
869 putchar('\r');
870 }
871
872 if (newval == 0)
873 {
874 /* inhibit the header */
875 display_header(No);
876 }
877 else if (newval > topn && topn == 0)
878 {
879 /* redraw the header */
880 display_header(Yes);
881 d_header = i_header;
882 }
883 topn = newval;
884 }
885 break;
886
887 case CMD_delay: /* new seconds delay */
888 new_message(MT_standout, "Seconds to delay: ");
889 if ((i = readline(tempbuf1, 8, Yes)) > -1)
890 {
891 if ((delay = i) == 0 && getuid() != 0)
892 {
893 delay = 1;
894 }
895 }
896 clear_message();
897 break;
898
899 case CMD_displays: /* change display count */
900 new_message(MT_standout,
901 "Displays to show (currently %s): ",
902 displays == -1 ? "infinite" :
903 itoa(displays));
904 if ((i = readline(tempbuf1, 10, Yes)) > 0)
905 {
906 displays = i;
907 }
908 else if (i == 0)
909 {
910 quit(0);
911 }
912 clear_message();
913 break;
914
915 case CMD_kill: /* kill program */
916 new_message(0, "kill ");
917 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
918 {
919 if ((errmsg = kill_procs(tempbuf2)) != NULL)
920 {
921 new_message(MT_standout, "%s", errmsg);
922 putchar('\r');
923 no_command = Yes;
924 }
925 }
926 else
927 {
928 clear_message();
929 }
930 break;
931
932 case CMD_renice: /* renice program */
933 new_message(0, "renice ");
934 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
935 {
936 if ((errmsg = renice_procs(tempbuf2)) != NULL)
937 {
938 new_message(MT_standout, "%s", errmsg);
939 putchar('\r');
940 no_command = Yes;
941 }
942 }
943 else
944 {
945 clear_message();
946 }
947 break;
948
949 case CMD_idletog:
950 case CMD_idletog2:
951 ps.idle = !ps.idle;
952 new_message(MT_standout | MT_delayed,
953 " %sisplaying idle processes.",
954 ps.idle ? "D" : "Not d");
955 putchar('\r');
956 break;
957
958 case CMD_selftog:
959 ps.self = (ps.self == -1) ? getpid() : -1;
960 new_message(MT_standout | MT_delayed,
961 " %sisplaying self.",
962 (ps.self == -1) ? "D" : "Not d");
963 putchar('\r');
964 break;
965
966 case CMD_user:
967 new_message(MT_standout,
968 "Username to show: ");
969 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
970 {
971 if (tempbuf2[0] == '+' &&
972 tempbuf2[1] == '\0')
973 {
974 ps.uid = -1;
975 }
976 else if ((i = userid(tempbuf2)) == -1)
977 {
978 new_message(MT_standout,
979 " %s: unknown user", tempbuf2);
980 no_command = Yes;
981 }
982 else
983 {
984 ps.uid = i;
985 }
986 putchar('\r');
987 }
988 else
989 {
990 clear_message();
991 }
992 break;
993
994 case CMD_thrtog:
995 ps.thread = !ps.thread;
996 new_message(MT_standout | MT_delayed,
997 "Displaying threads %s",
998 ps.thread ? "separately" : "as a count");
999 header_text = format_header(uname_field);
1000 reset_display();
1001 putchar('\r');
1002 break;
1003 case CMD_wcputog:
1004 ps.wcpu = !ps.wcpu;
1005 new_message(MT_standout | MT_delayed,
1006 "Displaying %sCPU",
1007 ps.wcpu ? "W" : "");
1008 header_text = format_header(uname_field);
1009 reset_display();
1010 putchar('\r');
1011 break;
1012 case CMD_viewtog:
1013 if (++displaymode == DISP_MAX)
1014 displaymode = 0;
1015 header_text = format_header(uname_field);
1016 display_header(Yes);
1017 d_header = i_header;
1018 reset_display();
1019 break;
1020 case CMD_viewsys:
1021 ps.system = !ps.system;
1022 break;
1023#ifdef ORDER
1024 case CMD_order:
1025 new_message(MT_standout,
1026 "Order to sort: ");
1027 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
1028 {
1029 if ((i = string_index(tempbuf2, statics.order_names)) == -1)
1030 {
1031 new_message(MT_standout,
1032 " %s: unrecognized sorting order", tempbuf2);
1033 no_command = Yes;
1034 }
1035 else
1036 {
1037 order_index = i;
1038 }
1039 putchar('\r');
1040 }
1041 else
1042 {
1043 clear_message();
1044 }
1045 break;
1046#endif
1047
1048 default:
1049 new_message(MT_standout, " BAD CASE IN SWITCH!");
1050 putchar('\r');
1051 }
1052 }
1053
1054 /* flush out stuff that may have been written */
1055 fflush(stdout);
1056 }
1057 }
1058 }
1059 }
1060
1061#ifdef DEBUG
1062 fclose(debug);
1063#endif
1064 quit(0);
1065 /*NOTREACHED*/
1066}
1067
1068/*
1069 * reset_display() - reset all the display routine pointers so that entire
1070 * screen will get redrawn.
1071 */
1072
1073reset_display()
1074
1075{
1076 d_loadave = i_loadave;
1077 d_procstates = i_procstates;
1078 d_cpustates = i_cpustates;
1079 d_memory = i_memory;
1080 d_swap = i_swap;
1081 d_message = i_message;
1082 d_header = i_header;
1083 d_process = i_process;
1084}
1085
1086/*
1087 * signal handlers
1088 */
1089
1090sigret_t leave() /* exit under normal conditions -- INT handler */
1091
1092{
1093 leaveflag = 1;
1094}
1095
1096sigret_t tstop(i) /* SIGTSTP handler */
1097
1098int i;
1099
1100{
1101 tstopflag = 1;
1102}
1103
1104#ifdef SIGWINCH
1105sigret_t winch(i) /* SIGWINCH handler */
1106
1107int i;
1108
1109{
1110 winchflag = 1;
1111}
1112#endif
1113
1114void quit(status) /* exit under duress */
1115
1116int status;
1117
1118{
1119 end_screen();
1120 exit(status);
1121 /*NOTREACHED*/
1122}
1123
1124sigret_t onalrm() /* SIGALRM handler */
1125
1126{
1127 /* this is only used in batch mode to break out of the pause() */
1128 /* return; */
1129}
1130