]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/top/top.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 static int fmt_flags = 0;
69 int pcpu_stats = No;
70
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
78
79 volatile sig_atomic_t leaveflag;
80 volatile sig_atomic_t tstopflag;
81 volatile sig_atomic_t winchflag;
82
83 /* internal routines */
84 void quit();
85
86 /* values which need to be accessed by signal handlers */
87 static int max_topn;            /* maximum displayable processes */
88
89 /* miscellaneous things */
90 struct process_select ps;
91 char *myname = "top";
92 jmp_buf jmp_int;
93
94 /* routines that don't return int */
95
96 char *username();
97 char *ctime();
98 char *kill_procs();
99 char *renice_procs();
100
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();
108
109 caddr_t get_process_info();
110
111 /* different routines for displaying the user's identification */
112 /* (values assigned to get_userid) */
113 char *username();
114 char *itoa7();
115
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();
133
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;
143
144
145 main(argc, argv)
146
147 int  argc;
148 char *argv[];
149
150 {
151     register int i;
152     register int active_procs;
153     register int change;
154
155     struct system_info system_info;
156     struct statics statics;
157     caddr_t processes;
158
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;
197
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
230
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
239
240     /* get our name */
241     if (argc > 0)
242     {
243         if ((myname = strrchr(argv[0], '/')) == 0)
244         {
245             myname = argv[0];
246         }
247         else
248         {
249             myname++;
250         }
251     }
252
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;
262
263     /* get preset options from the environment */
264     if ((env_top = getenv("TOP")) != NULL)
265     {
266         av = preset_argv = argparse(env_top, &preset_argc);
267         ac = preset_argc;
268
269         /* set the dummy argument to an explanatory message, in case
270            getopt encounters a bad argument */
271         preset_argv[0] = "while processing environment";
272     }
273
274     /* process options */
275     do {
276         /* if we're done doing the presets, then process the real arguments */
277         if (preset_argc == 0)
278         {
279             ac = argc;
280             av = argv;
281
282             /* this should keep getopt happy... */
283             optind = 1;
284         }
285
286         while ((i = getopt(ac, av, "CSIHPabijnquvs:d:U:m:o:t")) != EOF)
287         {
288             switch(i)
289             {
290               case 'v':                 /* show version number */
291                 fprintf(stderr, "%s: version %s\n",
292                         myname, version_string());
293                 exit(1);
294                 break;
295
296               case 'u':                 /* toggle uid/username display */
297                 do_unames = !do_unames;
298                 break;
299
300               case 'U':                 /* display only username's processes */
301                 if ((ps.uid = userid(optarg)) == -1)
302                 {
303                     fprintf(stderr, "%s: unknown user\n", optarg);
304                     exit(1);
305                 }
306                 break;
307
308               case 'S':                 /* show system processes */
309                 ps.system = !ps.system;
310                 break;
311
312               case 'I':                   /* show idle processes */
313                 ps.idle = !ps.idle;
314                 break;
315
316               case 'i':                 /* go interactive regardless */
317                 interactive = Yes;
318                 break;
319
320               case 'n':                 /* batch, or non-interactive */
321               case 'b':
322                 interactive = No;
323                 break;
324
325               case 'a':
326                 fmt_flags ^= FMT_SHOWARGS;
327                 break;
328
329               case 'd':                 /* number of displays to show */
330                 if ((i = atoiwi(optarg)) == Invalid || i == 0)
331                 {
332                     fprintf(stderr,
333                         "%s: warning: display count should be positive -- option ignored\n",
334                         myname);
335                     warnings++;
336                 }
337                 else
338                 {
339                     displays = i;
340                 }
341                 break;
342
343               case 's':
344                 if ((delay = atoi(optarg)) < 0 || (delay == 0 && getuid() != 0))
345                 {
346                     fprintf(stderr,
347                         "%s: warning: seconds delay should be positive -- using default\n",
348                         myname);
349                     delay = Default_DELAY;
350                     warnings++;
351                 }
352                 break;
353
354               case 'q':         /* be quick about it */
355                 /* only allow this if user is really root */
356                 if (getuid() == 0)
357                 {
358                     /* be very un-nice! */
359                     (void) nice(-20);
360                 }
361                 else
362                 {
363                     fprintf(stderr,
364                         "%s: warning: `-q' option can only be used by root\n",
365                         myname);
366                     warnings++;
367                 }
368                 break;
369
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);
381                 }
382                 break;
383
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;
394
395               case 't':
396                 ps.self = (ps.self == -1) ? getpid() : -1;
397                 break;
398
399               case 'C':
400                 ps.wcpu = !ps.wcpu;
401                 break;
402
403               case 'H':
404                 ps.thread = !ps.thread;
405                 break;
406
407               case 'j':
408                 ps.jail = !ps.jail;
409                 break;
410
411               case 'P':
412                 pcpu_stats = Yes;
413                 break;
414
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);
422             }
423         }
424
425         /* get count of top processes to display (if any) */
426         if (optind < ac)
427         {
428             if ((topn = atoiwi(av[optind])) == Invalid)
429             {
430                 fprintf(stderr,
431                         "%s: warning: process display count should be non-negative -- using default\n",
432                         myname);
433                 warnings++;
434             }
435 #if Default_TOPN == Infinity
436             else
437             {
438                 topn_specified = Yes;
439             }
440 #endif
441         }
442
443         /* tricky:  remember old value of preset_argc & set preset_argc = 0 */
444         i = preset_argc;
445         preset_argc = 0;
446
447     /* repeat only if we really did the preset arguments */
448     } while (i != 0);
449
450     /* set constants for username/uid display correctly */
451     if (!do_unames)
452     {
453         uname_field = "   UID  ";
454         get_userid = itoa7;
455     }
456
457     /* initialize the kernel memory interface */
458     if (machine_init(&statics, do_unames) == -1)
459     {
460         exit(1);
461     }
462
463 #ifdef ORDER
464     /* determine sorting order index, if necessary */
465     if (order_name != NULL)
466     {
467         if ((order_index = string_index(order_name, statics.order_names)) == -1)
468         {
469             char **pp;
470
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)
476             {
477                 fprintf(stderr, " %s", *pp++);
478             }
479             fputc('\n', stderr);
480             exit(1);
481         }
482     }
483 #endif
484
485 #ifdef no_initialization_needed
486     /* initialize the hashing stuff */
487     if (do_unames)
488     {
489         init_hash();
490     }
491 #endif
492
493     /* initialize termcap */
494     init_termcap(interactive);
495
496     /* get the string to use for the process area header */
497     header_text = format_header(uname_field);
498
499     /* initialize display interface */
500     if ((max_topn = display_init(&statics)) == -1)
501     {
502         fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
503         exit(4);
504     }
505     
506     /* print warning if user requested more processes than we can display */
507     if (topn > max_topn)
508     {
509         fprintf(stderr,
510                 "%s: warning: this terminal can only display %d processes.\n",
511                 myname, max_topn);
512         warnings++;
513     }
514
515     /* adjust for topn == Infinity */
516     if (topn == Infinity)
517     {
518         /*
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.
527          */
528 #if Default_TOPN == Infinity
529         topn = smart_terminal ? Largest :
530                     (topn_specified ? Largest : Nominal_TOPN);
531 #else
532         topn = Largest;
533 #endif
534     }
535
536     /* set header display accordingly */
537     display_header(topn > 0);
538
539     /* determine interactive state */
540     if (interactive == Maybe)
541     {
542         interactive = smart_terminal;
543     }
544
545     /* if # of displays not specified, fill it in */
546     if (displays == 0)
547     {
548         displays = smart_terminal ? Infinity : 1;
549     }
550
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)
574     {
575         fputs("....", stderr);
576         fflush(stderr);                 /* why must I do this? */
577         sleep((unsigned)(3 * warnings));
578         fputc('\n', stderr);
579     }
580
581 restart:
582
583     /*
584      *  main loop -- repeat while display count is positive or while it
585      *          indicates infinity (by being -1)
586      */
587
588     while ((displays == -1) || (displays-- > 0))
589     {
590         int (*compare)();
591
592             
593         /* get the current stats */
594         get_system_info(&system_info);
595
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
604
605         /* get the current set of processes */
606         processes =
607                 get_process_info(&system_info, &ps, compare);
608
609         /* display the load averages */
610         (*d_loadave)(system_info.last_pid,
611                      system_info.load_avg);
612
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);
618
619         /* display process state breakdown */
620         (*d_procstates)(system_info.p_total,
621                         system_info.procstates);
622
623         /* display the cpu state percentage breakdown */
624         if (dostates)   /* but not the first time */
625         {
626             (*d_cpustates)(system_info.cpustates);
627         }
628         else
629         {
630             /* we'll do it next time */
631             if (smart_terminal)
632             {
633                 z_cpustates();
634             }
635             else
636             {
637                 putchar('\n');
638             }
639             dostates = Yes;
640         }
641
642         /* display memory stats */
643         (*d_memory)(system_info.memory);
644
645         /* display swap stats */
646         (*d_swap)(system_info.swap);
647
648         /* handle message area */
649         (*d_message)();
650
651         /* update the header area */
652         (*d_header)(header_text);
653     
654         if (topn > 0)
655         {
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)
661             {
662                 active_procs = topn;
663             }
664             if (active_procs > max_topn)
665             {
666                 active_procs = max_topn;
667             }
668
669             /* now show the top "n" processes. */
670             for (i = 0; i < active_procs; i++)
671             {
672                 (*d_process)(i, format_next_process(processes, get_userid,
673                              fmt_flags));
674             }
675         }
676         else
677         {
678             i = 0;
679         }
680
681         /* do end-screen processing */
682         u_endscreen(i);
683
684         /* now, flush the output buffer */
685         if (fflush(stdout) != 0)
686         {
687             new_message(MT_standout, " Write error on stdout");
688             putchar('\r');
689             quit(1);
690             /*NOTREACHED*/
691         }
692
693         /* only do the rest if we have more displays to show */
694         if (displays)
695         {
696             /* switch out for new display on smart terminals */
697             if (smart_terminal)
698             {
699                 if (overstrike)
700                 {
701                     reset_display();
702                 }
703                 else
704                 {
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;
713                 }
714             }
715     
716             no_command = Yes;
717             if (!interactive)
718             {
719                 /* set up alarm */
720                 (void) signal(SIGALRM, onalrm);
721                 (void) alarm((unsigned)delay);
722     
723                 /* wait for the rest of it .... */
724                 pause();
725             }
726             else while (no_command)
727             {
728                 /* assume valid command unless told otherwise */
729                 no_command = No;
730
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;
736
737                 if (leaveflag) {
738                     end_screen();
739                     exit(0);
740                 }
741
742                 if (tstopflag) {
743                     /* move to the lower left */
744                     end_screen();
745                     fflush(stdout);
746
747                     /* default the signal handler action */
748                     (void) signal(SIGTSTP, SIG_DFL);
749
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);
757
758                     /* reset the signal handler */
759                     (void) signal(SIGTSTP, tstop);
760
761                     /* reinit screen */
762                     reinit_screen();
763                     reset_display();
764                     tstopflag = 0;
765                     goto restart;
766                 }
767
768                 if (winchflag) {
769                     /* reascertain the screen dimensions */
770                     get_screensize();
771
772                     /* tell display to resize */
773                     max_topn = display_resize();
774
775                     /* reset the signal handler */
776                     (void) signal(SIGWINCH, winch);
777
778                     reset_display();
779                     winchflag = 0;
780                     goto restart;
781                 }
782
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)
788                 {
789                     int newval;
790                     char *errmsg;
791     
792                     /* something to read -- clear the message area first */
793                     clear_message();
794
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)
798                     {
799                         /* read error: either 0 or -1 */
800                         new_message(MT_standout, " Read error on stdin");
801                         putchar('\r');
802                         quit(1);
803                         /*NOTREACHED*/
804                     }
805                     if ((iptr = strchr(command_chars, ch)) == NULL)
806                     {
807                         if (ch != '\r' && ch != '\n')
808                         {
809                             /* illegal command */
810                             new_message(MT_standout, " Command not understood");
811                         }
812                         putchar('\r');
813                         no_command = Yes;
814                     }
815                     else
816                     {
817                         change = iptr - command_chars;
818                         if (overstrike && change > CMD_OSLIMIT)
819                         {
820                             /* error */
821                             new_message(MT_standout,
822                             " Command cannot be handled by this terminal");
823                             putchar('\r');
824                             no_command = Yes;
825                         }
826                         else switch(change)
827                         {
828                             case CMD_redraw:    /* redraw screen */
829                                 reset_display();
830                                 break;
831     
832                             case CMD_update:    /* merely update display */
833                                 /* is the load average high? */
834                                 if (system_info.load_avg[0] > LoadMax)
835                                 {
836                                     /* yes, go home for visual feedback */
837                                     go_home();
838                                     fflush(stdout);
839                                 }
840                                 break;
841             
842                             case CMD_quit:      /* quit */
843                                 quit(0);
844                                 /*NOTREACHED*/
845                                 break;
846             
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;
856         
857                             case CMD_errors:    /* show errors */
858                                 if (error_count() == 0)
859                                 {
860                                     new_message(MT_standout,
861                                         " Currently no errors to report.");
862                                     putchar('\r');
863                                     no_command = Yes;
864                                 }
865                                 else
866                                 {
867                                     reset_display();
868                                     clear();
869                                     show_errors();
870                                     standout("Hit any key to continue: ");
871                                     fflush(stdout);
872                                     (void) read(0, &ch, 1);
873                                 }
874                                 break;
875         
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)
882                                 {
883                                     if (newval > max_topn)
884                                     {
885                                         new_message(MT_standout | MT_delayed,
886                                           " This terminal can only display %d processes.",
887                                           max_topn);
888                                         putchar('\r');
889                                     }
890
891                                     if (newval == 0)
892                                     {
893                                         /* inhibit the header */
894                                         display_header(No);
895                                     }
896                                     else if (newval > topn && topn == 0)
897                                     {
898                                         /* redraw the header */
899                                         display_header(Yes);
900                                         d_header = i_header;
901                                     }
902                                     topn = newval;
903                                 }
904                                 break;
905             
906                             case CMD_delay:     /* new seconds delay */
907                                 new_message(MT_standout, "Seconds to delay: ");
908                                 if ((i = readline(tempbuf1, 8, Yes)) > -1)
909                                 {
910                                     if ((delay = i) == 0 && getuid() != 0)
911                                     {
912                                         delay = 1;
913                                     }
914                                 }
915                                 clear_message();
916                                 break;
917         
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)
924                                 {
925                                     displays = i;
926                                 }
927                                 else if (i == 0)
928                                 {
929                                     quit(0);
930                                 }
931                                 clear_message();
932                                 break;
933     
934                             case CMD_kill:      /* kill program */
935                                 new_message(0, "kill ");
936                                 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
937                                 {
938                                     if ((errmsg = kill_procs(tempbuf2)) != NULL)
939                                     {
940                                         new_message(MT_standout, "%s", errmsg);
941                                         putchar('\r');
942                                         no_command = Yes;
943                                     }
944                                 }
945                                 else
946                                 {
947                                     clear_message();
948                                 }
949                                 break;
950             
951                             case CMD_renice:    /* renice program */
952                                 new_message(0, "renice ");
953                                 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
954                                 {
955                                     if ((errmsg = renice_procs(tempbuf2)) != NULL)
956                                     {
957                                         new_message(MT_standout, "%s", errmsg);
958                                         putchar('\r');
959                                         no_command = Yes;
960                                     }
961                                 }
962                                 else
963                                 {
964                                     clear_message();
965                                 }
966                                 break;
967
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;
976
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;
984
985                             case CMD_user:
986                                 new_message(MT_standout,
987                                     "Username to show: ");
988                                 if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
989                                 {
990                                     if (tempbuf2[0] == '+' &&
991                                         tempbuf2[1] == '\0')
992                                     {
993                                         ps.uid = -1;
994                                     }
995                                     else if ((i = userid(tempbuf2)) == -1)
996                                     {
997                                         new_message(MT_standout,
998                                             " %s: unknown user", tempbuf2);
999                                         no_command = Yes;
1000                                     }
1001                                     else
1002                                     {
1003                                         ps.uid = i;
1004                                     }
1005                                     putchar('\r');
1006                                 }
1007                                 else
1008                                 {
1009                                     clear_message();
1010                                 }
1011                                 break;
1012             
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)
1050                                 {
1051                                   if ((i = string_index(tempbuf2, statics.order_names)) == -1)
1052                                         {
1053                                           new_message(MT_standout,
1054                                               " %s: unrecognized sorting order", tempbuf2);
1055                                           no_command = Yes;
1056                                     }
1057                                     else
1058                                     {
1059                                         order_index = i;
1060                                     }
1061                                     putchar('\r');
1062                                 }
1063                                 else
1064                                 {
1065                                     clear_message();
1066                                 }
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;
1078             
1079                             default:
1080                                 new_message(MT_standout, " BAD CASE IN SWITCH!");
1081                                 putchar('\r');
1082                         }
1083                     }
1084
1085                     /* flush out stuff that may have been written */
1086                     fflush(stdout);
1087                 }
1088             }
1089         }
1090     }
1091
1092 #ifdef DEBUG
1093     fclose(debug);
1094 #endif
1095     quit(0);
1096     /*NOTREACHED*/
1097 }
1098
1099 /*
1100  *  reset_display() - reset all the display routine pointers so that entire
1101  *      screen will get redrawn.
1102  */
1103
1104 reset_display()
1105
1106 {
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;
1115 }
1116
1117 /*
1118  *  signal handlers
1119  */
1120
1121 sigret_t leave()        /* exit under normal conditions -- INT handler */
1122
1123 {
1124     leaveflag = 1;
1125 }
1126
1127 sigret_t tstop(i)       /* SIGTSTP handler */
1128
1129 int i;
1130
1131 {
1132     tstopflag = 1;
1133 }
1134
1135 #ifdef SIGWINCH
1136 sigret_t winch(i)               /* SIGWINCH handler */
1137
1138 int i;
1139
1140 {
1141     winchflag = 1;
1142 }
1143 #endif
1144
1145 void quit(status)               /* exit under duress */
1146
1147 int status;
1148
1149 {
1150     end_screen();
1151     exit(status);
1152     /*NOTREACHED*/
1153 }
1154
1155 sigret_t onalrm()       /* SIGALRM handler */
1156
1157 {
1158     /* this is only used in batch mode to break out of the pause() */
1159     /* return; */
1160 }
1161