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