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