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

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