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

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