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

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