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

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