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

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