[BACK]Return to top.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / top

Annotation of src/usr.bin/top/top.c, Revision 1.10

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