turns printfs back on
[freebsd-src/fkvm-freebsd.git] / contrib / top / top.c
blob762efaa2a723d2c4b298f3876a65f548f62174d0
1 char *copyright =
2 "Copyright (c) 1984 through 1996, William LeFebvre";
4 /*
5 * Top users/processes display for Unix
6 * Version 3
8 * This program may be freely redistributed,
9 * but this entire comment MUST remain intact.
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
16 * $FreeBSD$
20 * See the file "Changes" for information on version-to-version changes.
24 * This file contains "main" and other high-level routines.
28 * The following preprocessor variables, when defined, are used to
29 * distinguish between different Unix implementations:
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
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>
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"
52 /* Size of the stdio buffer given to stdout */
53 #define Buffersize 2048
55 /* The buffer that stdio will use */
56 char stdoutbuf[Buffersize];
58 /* build Signal masks */
59 #define Smask(s) (1 << ((s) - 1))
61 /* for getopt: */
62 extern int optind;
63 extern char *optarg;
65 /* imported from screen.c */
66 extern int overstrike;
68 static int fmt_flags = 0;
69 int pcpu_stats = No;
71 /* signal handling routines */
72 sigret_t leave();
73 sigret_t onalrm();
74 sigret_t tstop();
75 #ifdef SIGWINCH
76 sigret_t winch();
77 #endif
79 volatile sig_atomic_t leaveflag;
80 volatile sig_atomic_t tstopflag;
81 volatile sig_atomic_t winchflag;
83 /* internal routines */
84 void quit();
86 /* values which need to be accessed by signal handlers */
87 static int max_topn; /* maximum displayable processes */
89 /* miscellaneous things */
90 struct process_select ps;
91 char *myname = "top";
92 jmp_buf jmp_int;
94 /* routines that don't return int */
96 char *username();
97 char *ctime();
98 char *kill_procs();
99 char *renice_procs();
101 #ifdef ORDER
102 extern int (*compares[])();
103 #else
104 extern int proc_compare();
105 extern int io_compare();
106 #endif
107 time_t time();
109 caddr_t get_process_info();
111 /* different routines for displaying the user's identification */
112 /* (values assigned to get_userid) */
113 char *username();
114 char *itoa7();
116 /* display routines that need to be predeclared */
117 int i_loadave();
118 int u_loadave();
119 int i_procstates();
120 int u_procstates();
121 int i_cpustates();
122 int u_cpustates();
123 int i_memory();
124 int u_memory();
125 int i_swap();
126 int u_swap();
127 int i_message();
128 int u_message();
129 int i_header();
130 int u_header();
131 int i_process();
132 int u_process();
134 /* pointers to display routines */
135 int (*d_loadave)() = i_loadave;
136 int (*d_procstates)() = i_procstates;
137 int (*d_cpustates)() = i_cpustates;
138 int (*d_memory)() = i_memory;
139 int (*d_swap)() = i_swap;
140 int (*d_message)() = i_message;
141 int (*d_header)() = i_header;
142 int (*d_process)() = i_process;
145 main(argc, argv)
147 int argc;
148 char *argv[];
151 register int i;
152 register int active_procs;
153 register int change;
155 struct system_info system_info;
156 struct statics statics;
157 caddr_t processes;
159 static char tempbuf1[50];
160 static char tempbuf2[50];
161 int old_sigmask; /* only used for BSD-style signals */
162 int topn = Default_TOPN;
163 int delay = Default_DELAY;
164 int displays = 0; /* indicates unspecified */
165 int sel_ret = 0;
166 time_t curr_time;
167 char *(*get_userid)() = username;
168 char *uname_field = "USERNAME";
169 char *header_text;
170 char *env_top;
171 char **preset_argv;
172 int preset_argc = 0;
173 char **av;
174 int ac;
175 char dostates = No;
176 char do_unames = Yes;
177 char interactive = Maybe;
178 char warnings = 0;
179 #if Default_TOPN == Infinity
180 char topn_specified = No;
181 #endif
182 char ch;
183 char *iptr;
184 char no_command = 1;
185 struct timeval timeout;
186 #ifdef ORDER
187 char *order_name = NULL;
188 int order_index = 0;
189 #endif
190 #ifndef FD_SET
191 /* FD_SET and friends are not present: fake it */
192 typedef int fd_set;
193 #define FD_ZERO(x) (*(x) = 0)
194 #define FD_SET(f, x) (*(x) = 1<<f)
195 #endif
196 fd_set readfds;
198 #ifdef ORDER
199 static char command_chars[] = "\f qh?en#sdkriIutHmSCajo";
200 #else
201 static char command_chars[] = "\f qh?en#sdkriIutHmSCaj";
202 #endif
203 /* these defines enumerate the "strchr"s of the commands in command_chars */
204 #define CMD_redraw 0
205 #define CMD_update 1
206 #define CMD_quit 2
207 #define CMD_help1 3
208 #define CMD_help2 4
209 #define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */
210 #define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */
211 #define CMD_number1 6
212 #define CMD_number2 7
213 #define CMD_delay 8
214 #define CMD_displays 9
215 #define CMD_kill 10
216 #define CMD_renice 11
217 #define CMD_idletog 12
218 #define CMD_idletog2 13
219 #define CMD_user 14
220 #define CMD_selftog 15
221 #define CMD_thrtog 16
222 #define CMD_viewtog 17
223 #define CMD_viewsys 18
224 #define CMD_wcputog 19
225 #define CMD_showargs 20
226 #define CMD_jidtog 21
227 #ifdef ORDER
228 #define CMD_order 22
229 #endif
231 /* set the buffer for stdout */
232 #ifdef DEBUG
233 extern FILE *debug;
234 debug = fopen("debug.run", "w");
235 setbuffer(stdout, NULL, 0);
236 #else
237 setbuffer(stdout, stdoutbuf, Buffersize);
238 #endif
240 /* get our name */
241 if (argc > 0)
243 if ((myname = strrchr(argv[0], '/')) == 0)
245 myname = argv[0];
247 else
249 myname++;
253 /* initialize some selection options */
254 ps.idle = Yes;
255 ps.self = -1;
256 ps.system = No;
257 ps.uid = -1;
258 ps.thread = No;
259 ps.wcpu = 1;
260 ps.jail = No;
261 ps.command = NULL;
263 /* get preset options from the environment */
264 if ((env_top = getenv("TOP")) != NULL)
266 av = preset_argv = argparse(env_top, &preset_argc);
267 ac = preset_argc;
269 /* set the dummy argument to an explanatory message, in case
270 getopt encounters a bad argument */
271 preset_argv[0] = "while processing environment";
274 /* process options */
275 do {
276 /* if we're done doing the presets, then process the real arguments */
277 if (preset_argc == 0)
279 ac = argc;
280 av = argv;
282 /* this should keep getopt happy... */
283 optind = 1;
286 while ((i = getopt(ac, av, "CSIHPabijnquvs:d:U:m:o:t")) != EOF)
288 switch(i)
290 case 'v': /* show version number */
291 fprintf(stderr, "%s: version %s\n",
292 myname, version_string());
293 exit(1);
294 break;
296 case 'u': /* toggle uid/username display */
297 do_unames = !do_unames;
298 break;
300 case 'U': /* display only username's processes */
301 if ((ps.uid = userid(optarg)) == -1)
303 fprintf(stderr, "%s: unknown user\n", optarg);
304 exit(1);
306 break;
308 case 'S': /* show system processes */
309 ps.system = !ps.system;
310 break;
312 case 'I': /* show idle processes */
313 ps.idle = !ps.idle;
314 break;
316 case 'i': /* go interactive regardless */
317 interactive = Yes;
318 break;
320 case 'n': /* batch, or non-interactive */
321 case 'b':
322 interactive = No;
323 break;
325 case 'a':
326 fmt_flags ^= FMT_SHOWARGS;
327 break;
329 case 'd': /* number of displays to show */
330 if ((i = atoiwi(optarg)) == Invalid || i == 0)
332 fprintf(stderr,
333 "%s: warning: display count should be positive -- option ignored\n",
334 myname);
335 warnings++;
337 else
339 displays = i;
341 break;
343 case 's':
344 if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0))
346 fprintf(stderr,
347 "%s: warning: seconds delay should be positive -- using default\n",
348 myname);
349 delay = Default_DELAY;
350 warnings++;
352 break;
354 case 'q': /* be quick about it */
355 /* only allow this if user is really root */
356 if (getuid() == 0)
358 /* be very un-nice! */
359 (void) nice(-20);
361 else
363 fprintf(stderr,
364 "%s: warning: `-q' option can only be used by root\n",
365 myname);
366 warnings++;
368 break;
370 case 'm': /* select display mode */
371 if (strcmp(optarg, "io") == 0) {
372 displaymode = DISP_IO;
373 } else if (strcmp(optarg, "cpu") == 0) {
374 displaymode = DISP_CPU;
375 } else {
376 fprintf(stderr,
377 "%s: warning: `-m' option can only take args "
378 "'io' or 'cpu'\n",
379 myname);
380 exit(1);
382 break;
384 case 'o': /* select sort order */
385 #ifdef ORDER
386 order_name = optarg;
387 #else
388 fprintf(stderr,
389 "%s: this platform does not support arbitrary ordering. Sorry.\n",
390 myname);
391 warnings++;
392 #endif
393 break;
395 case 't':
396 ps.self = (ps.self == -1) ? getpid() : -1;
397 break;
399 case 'C':
400 ps.wcpu = !ps.wcpu;
401 break;
403 case 'H':
404 ps.thread = !ps.thread;
405 break;
407 case 'j':
408 ps.jail = !ps.jail;
409 break;
411 case 'P':
412 pcpu_stats = Yes;
413 break;
415 default:
416 fprintf(stderr,
417 "Top version %s\n"
418 "Usage: %s [-abCHIijnPqStuv] [-d count] [-m io | cpu] [-o field] [-s time]\n"
419 " [-U username] [number]\n",
420 version_string(), myname);
421 exit(1);
425 /* get count of top processes to display (if any) */
426 if (optind < ac)
428 if ((topn = atoiwi(av[optind])) == Invalid)
430 fprintf(stderr,
431 "%s: warning: process display count should be non-negative -- using default\n",
432 myname);
433 warnings++;
435 #if Default_TOPN == Infinity
436 else
438 topn_specified = Yes;
440 #endif
443 /* tricky: remember old value of preset_argc & set preset_argc = 0 */
444 i = preset_argc;
445 preset_argc = 0;
447 /* repeat only if we really did the preset arguments */
448 } while (i != 0);
450 /* set constants for username/uid display correctly */
451 if (!do_unames)
453 uname_field = " UID ";
454 get_userid = itoa7;
457 /* initialize the kernel memory interface */
458 if (machine_init(&statics, do_unames) == -1)
460 exit(1);
463 #ifdef ORDER
464 /* determine sorting order index, if necessary */
465 if (order_name != NULL)
467 if ((order_index = string_index(order_name, statics.order_names)) == -1)
469 char **pp;
471 fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n",
472 myname, order_name);
473 fprintf(stderr, "\tTry one of these:");
474 pp = statics.order_names;
475 while (*pp != NULL)
477 fprintf(stderr, " %s", *pp++);
479 fputc('\n', stderr);
480 exit(1);
483 #endif
485 #ifdef no_initialization_needed
486 /* initialize the hashing stuff */
487 if (do_unames)
489 init_hash();
491 #endif
493 /* initialize termcap */
494 init_termcap(interactive);
496 /* get the string to use for the process area header */
497 header_text = format_header(uname_field);
499 /* initialize display interface */
500 if ((max_topn = display_init(&statics)) == -1)
502 fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
503 exit(4);
506 /* print warning if user requested more processes than we can display */
507 if (topn > max_topn)
509 fprintf(stderr,
510 "%s: warning: this terminal can only display %d processes.\n",
511 myname, max_topn);
512 warnings++;
515 /* adjust for topn == Infinity */
516 if (topn == Infinity)
519 * For smart terminals, infinity really means everything that can
520 * be displayed, or Largest.
521 * On dumb terminals, infinity means every process in the system!
522 * We only really want to do that if it was explicitly specified.
523 * This is always the case when "Default_TOPN != Infinity". But if
524 * topn wasn't explicitly specified and we are on a dumb terminal
525 * and the default is Infinity, then (and only then) we use
526 * "Nominal_TOPN" instead.
528 #if Default_TOPN == Infinity
529 topn = smart_terminal ? Largest :
530 (topn_specified ? Largest : Nominal_TOPN);
531 #else
532 topn = Largest;
533 #endif
536 /* set header display accordingly */
537 display_header(topn > 0);
539 /* determine interactive state */
540 if (interactive == Maybe)
542 interactive = smart_terminal;
545 /* if # of displays not specified, fill it in */
546 if (displays == 0)
548 displays = smart_terminal ? Infinity : 1;
551 /* hold interrupt signals while setting up the screen and the handlers */
552 #ifdef SIGHOLD
553 sighold(SIGINT);
554 sighold(SIGQUIT);
555 sighold(SIGTSTP);
556 #else
557 old_sigmask = sigblock(Smask(SIGINT) | Smask(SIGQUIT) | Smask(SIGTSTP));
558 #endif
559 init_screen();
560 (void) signal(SIGINT, leave);
561 (void) signal(SIGQUIT, leave);
562 (void) signal(SIGTSTP, tstop);
563 #ifdef SIGWINCH
564 (void) signal(SIGWINCH, winch);
565 #endif
566 #ifdef SIGRELSE
567 sigrelse(SIGINT);
568 sigrelse(SIGQUIT);
569 sigrelse(SIGTSTP);
570 #else
571 (void) sigsetmask(old_sigmask);
572 #endif
573 if (warnings)
575 fputs("....", stderr);
576 fflush(stderr); /* why must I do this? */
577 sleep((unsigned)(3 * warnings));
578 fputc('\n', stderr);
581 restart:
584 * main loop -- repeat while display count is positive or while it
585 * indicates infinity (by being -1)
588 while ((displays == -1) || (displays-- > 0))
590 int (*compare)();
593 /* get the current stats */
594 get_system_info(&system_info);
596 #ifdef ORDER
597 compare = compares[order_index];
598 #else
599 if (displaymode == DISP_CPU)
600 compare = proc_compare;
601 else
602 compare = io_compare;
603 #endif
605 /* get the current set of processes */
606 processes =
607 get_process_info(&system_info, &ps, compare);
609 /* display the load averages */
610 (*d_loadave)(system_info.last_pid,
611 system_info.load_avg);
613 /* display the current time */
614 /* this method of getting the time SHOULD be fairly portable */
615 time(&curr_time);
616 i_uptime(&system_info.boottime, &curr_time);
617 i_timeofday(&curr_time);
619 /* display process state breakdown */
620 (*d_procstates)(system_info.p_total,
621 system_info.procstates);
623 /* display the cpu state percentage breakdown */
624 if (dostates) /* but not the first time */
626 (*d_cpustates)(system_info.cpustates);
628 else
630 /* we'll do it next time */
631 if (smart_terminal)
633 z_cpustates();
635 else
637 putchar('\n');
639 dostates = Yes;
642 /* display memory stats */
643 (*d_memory)(system_info.memory);
645 /* display swap stats */
646 (*d_swap)(system_info.swap);
648 /* handle message area */
649 (*d_message)();
651 /* update the header area */
652 (*d_header)(header_text);
654 if (topn > 0)
656 /* determine number of processes to actually display */
657 /* this number will be the smallest of: active processes,
658 number user requested, number current screen accomodates */
659 active_procs = system_info.P_ACTIVE;
660 if (active_procs > topn)
662 active_procs = topn;
664 if (active_procs > max_topn)
666 active_procs = max_topn;
669 /* now show the top "n" processes. */
670 for (i = 0; i < active_procs; i++)
672 (*d_process)(i, format_next_process(processes, get_userid,
673 fmt_flags));
676 else
678 i = 0;
681 /* do end-screen processing */
682 u_endscreen(i);
684 /* now, flush the output buffer */
685 if (fflush(stdout) != 0)
687 new_message(MT_standout, " Write error on stdout");
688 putchar('\r');
689 quit(1);
690 /*NOTREACHED*/
693 /* only do the rest if we have more displays to show */
694 if (displays)
696 /* switch out for new display on smart terminals */
697 if (smart_terminal)
699 if (overstrike)
701 reset_display();
703 else
705 d_loadave = u_loadave;
706 d_procstates = u_procstates;
707 d_cpustates = u_cpustates;
708 d_memory = u_memory;
709 d_swap = u_swap;
710 d_message = u_message;
711 d_header = u_header;
712 d_process = u_process;
716 no_command = Yes;
717 if (!interactive)
719 /* set up alarm */
720 (void) signal(SIGALRM, onalrm);
721 (void) alarm((unsigned)delay);
723 /* wait for the rest of it .... */
724 pause();
726 else while (no_command)
728 /* assume valid command unless told otherwise */
729 no_command = No;
731 /* set up arguments for select with timeout */
732 FD_ZERO(&readfds);
733 FD_SET(0, &readfds); /* for standard input */
734 timeout.tv_sec = delay;
735 timeout.tv_usec = 0;
737 if (leaveflag) {
738 end_screen();
739 exit(0);
742 if (tstopflag) {
743 /* move to the lower left */
744 end_screen();
745 fflush(stdout);
747 /* default the signal handler action */
748 (void) signal(SIGTSTP, SIG_DFL);
750 /* unblock the signal and send ourselves one */
751 #ifdef SIGRELSE
752 sigrelse(SIGTSTP);
753 #else
754 (void) sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1)));
755 #endif
756 (void) kill(0, SIGTSTP);
758 /* reset the signal handler */
759 (void) signal(SIGTSTP, tstop);
761 /* reinit screen */
762 reinit_screen();
763 reset_display();
764 tstopflag = 0;
765 goto restart;
768 if (winchflag) {
769 /* reascertain the screen dimensions */
770 get_screensize();
772 /* tell display to resize */
773 max_topn = display_resize();
775 /* reset the signal handler */
776 (void) signal(SIGWINCH, winch);
778 reset_display();
779 winchflag = 0;
780 goto restart;
783 /* wait for either input or the end of the delay period */
784 sel_ret = select(2, &readfds, NULL, NULL, &timeout);
785 if (sel_ret < 0 && errno != EINTR)
786 quit(0);
787 if (sel_ret > 0)
789 int newval;
790 char *errmsg;
792 /* something to read -- clear the message area first */
793 clear_message();
795 /* now read it and convert to command strchr */
796 /* (use "change" as a temporary to hold strchr) */
797 if (read(0, &ch, 1) != 1)
799 /* read error: either 0 or -1 */
800 new_message(MT_standout, " Read error on stdin");
801 putchar('\r');
802 quit(1);
803 /*NOTREACHED*/
805 if ((iptr = strchr(command_chars, ch)) == NULL)
807 if (ch != '\r' && ch != '\n')
809 /* illegal command */
810 new_message(MT_standout, " Command not understood");
812 putchar('\r');
813 no_command = Yes;
815 else
817 change = iptr - command_chars;
818 if (overstrike && change > CMD_OSLIMIT)
820 /* error */
821 new_message(MT_standout,
822 " Command cannot be handled by this terminal");
823 putchar('\r');
824 no_command = Yes;
826 else switch(change)
828 case CMD_redraw: /* redraw screen */
829 reset_display();
830 break;
832 case CMD_update: /* merely update display */
833 /* is the load average high? */
834 if (system_info.load_avg[0] > LoadMax)
836 /* yes, go home for visual feedback */
837 go_home();
838 fflush(stdout);
840 break;
842 case CMD_quit: /* quit */
843 quit(0);
844 /*NOTREACHED*/
845 break;
847 case CMD_help1: /* help */
848 case CMD_help2:
849 reset_display();
850 clear();
851 show_help();
852 standout("Hit any key to continue: ");
853 fflush(stdout);
854 (void) read(0, &ch, 1);
855 break;
857 case CMD_errors: /* show errors */
858 if (error_count() == 0)
860 new_message(MT_standout,
861 " Currently no errors to report.");
862 putchar('\r');
863 no_command = Yes;
865 else
867 reset_display();
868 clear();
869 show_errors();
870 standout("Hit any key to continue: ");
871 fflush(stdout);
872 (void) read(0, &ch, 1);
874 break;
876 case CMD_number1: /* new number */
877 case CMD_number2:
878 new_message(MT_standout,
879 "Number of processes to show: ");
880 newval = readline(tempbuf1, 8, Yes);
881 if (newval > -1)
883 if (newval > max_topn)
885 new_message(MT_standout | MT_delayed,
886 " This terminal can only display %d processes.",
887 max_topn);
888 putchar('\r');
891 if (newval == 0)
893 /* inhibit the header */
894 display_header(No);
896 else if (newval > topn && topn == 0)
898 /* redraw the header */
899 display_header(Yes);
900 d_header = i_header;
902 topn = newval;
904 break;
906 case CMD_delay: /* new seconds delay */
907 new_message(MT_standout, "Seconds to delay: ");
908 if ((i = readline(tempbuf1, 8, Yes)) > -1)
910 if ((delay = i) == 0 && getuid() != 0)
912 delay = 1;
915 clear_message();
916 break;
918 case CMD_displays: /* change display count */
919 new_message(MT_standout,
920 "Displays to show (currently %s): ",
921 displays == -1 ? "infinite" :
922 itoa(displays));
923 if ((i = readline(tempbuf1, 10, Yes)) > 0)
925 displays = i;
927 else if (i == 0)
929 quit(0);
931 clear_message();
932 break;
934 case CMD_kill: /* kill program */
935 new_message(0, "kill ");
936 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
938 if ((errmsg = kill_procs(tempbuf2)) != NULL)
940 new_message(MT_standout, "%s", errmsg);
941 putchar('\r');
942 no_command = Yes;
945 else
947 clear_message();
949 break;
951 case CMD_renice: /* renice program */
952 new_message(0, "renice ");
953 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
955 if ((errmsg = renice_procs(tempbuf2)) != NULL)
957 new_message(MT_standout, "%s", errmsg);
958 putchar('\r');
959 no_command = Yes;
962 else
964 clear_message();
966 break;
968 case CMD_idletog:
969 case CMD_idletog2:
970 ps.idle = !ps.idle;
971 new_message(MT_standout | MT_delayed,
972 " %sisplaying idle processes.",
973 ps.idle ? "D" : "Not d");
974 putchar('\r');
975 break;
977 case CMD_selftog:
978 ps.self = (ps.self == -1) ? getpid() : -1;
979 new_message(MT_standout | MT_delayed,
980 " %sisplaying self.",
981 (ps.self == -1) ? "D" : "Not d");
982 putchar('\r');
983 break;
985 case CMD_user:
986 new_message(MT_standout,
987 "Username to show: ");
988 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
990 if (tempbuf2[0] == '+' &&
991 tempbuf2[1] == '\0')
993 ps.uid = -1;
995 else if ((i = userid(tempbuf2)) == -1)
997 new_message(MT_standout,
998 " %s: unknown user", tempbuf2);
999 no_command = Yes;
1001 else
1003 ps.uid = i;
1005 putchar('\r');
1007 else
1009 clear_message();
1011 break;
1013 case CMD_thrtog:
1014 ps.thread = !ps.thread;
1015 new_message(MT_standout | MT_delayed,
1016 "Displaying threads %s",
1017 ps.thread ? "separately" : "as a count");
1018 header_text = format_header(uname_field);
1019 reset_display();
1020 putchar('\r');
1021 break;
1022 case CMD_wcputog:
1023 ps.wcpu = !ps.wcpu;
1024 new_message(MT_standout | MT_delayed,
1025 "Displaying %sCPU",
1026 ps.wcpu ? "W" : "");
1027 header_text = format_header(uname_field);
1028 reset_display();
1029 putchar('\r');
1030 break;
1031 case CMD_viewtog:
1032 if (++displaymode == DISP_MAX)
1033 displaymode = 0;
1034 header_text = format_header(uname_field);
1035 display_header(Yes);
1036 d_header = i_header;
1037 reset_display();
1038 break;
1039 case CMD_viewsys:
1040 ps.system = !ps.system;
1041 break;
1042 case CMD_showargs:
1043 fmt_flags ^= FMT_SHOWARGS;
1044 break;
1045 #ifdef ORDER
1046 case CMD_order:
1047 new_message(MT_standout,
1048 "Order to sort: ");
1049 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
1051 if ((i = string_index(tempbuf2, statics.order_names)) == -1)
1053 new_message(MT_standout,
1054 " %s: unrecognized sorting order", tempbuf2);
1055 no_command = Yes;
1057 else
1059 order_index = i;
1061 putchar('\r');
1063 else
1065 clear_message();
1067 break;
1068 #endif
1069 case CMD_jidtog:
1070 ps.jail = !ps.jail;
1071 new_message(MT_standout | MT_delayed,
1072 " %sisplaying jail ID.",
1073 ps.jail ? "D" : "Not d");
1074 header_text = format_header(uname_field);
1075 reset_display();
1076 putchar('\r');
1077 break;
1079 default:
1080 new_message(MT_standout, " BAD CASE IN SWITCH!");
1081 putchar('\r');
1085 /* flush out stuff that may have been written */
1086 fflush(stdout);
1092 #ifdef DEBUG
1093 fclose(debug);
1094 #endif
1095 quit(0);
1096 /*NOTREACHED*/
1100 * reset_display() - reset all the display routine pointers so that entire
1101 * screen will get redrawn.
1104 reset_display()
1107 d_loadave = i_loadave;
1108 d_procstates = i_procstates;
1109 d_cpustates = i_cpustates;
1110 d_memory = i_memory;
1111 d_swap = i_swap;
1112 d_message = i_message;
1113 d_header = i_header;
1114 d_process = i_process;
1118 * signal handlers
1121 sigret_t leave() /* exit under normal conditions -- INT handler */
1124 leaveflag = 1;
1127 sigret_t tstop(i) /* SIGTSTP handler */
1129 int i;
1132 tstopflag = 1;
1135 #ifdef SIGWINCH
1136 sigret_t winch(i) /* SIGWINCH handler */
1138 int i;
1141 winchflag = 1;
1143 #endif
1145 void quit(status) /* exit under duress */
1147 int status;
1150 end_screen();
1151 exit(status);
1152 /*NOTREACHED*/
1155 sigret_t onalrm() /* SIGALRM handler */
1158 /* this is only used in batch mode to break out of the pause() */
1159 /* return; */