[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.13

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