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