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

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