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

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