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