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