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