[BACK]Return to display.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / top

Annotation of src/usr.bin/top/display.c, Revision 1.38

1.38    ! otto        1: /* $OpenBSD: display.c,v 1.37 2010/02/05 10:21:10 otto Exp $    */
1.1       downsj      2:
                      3: /*
                      4:  *  Top users/processes display for Unix
                      5:  *  Version 3
                      6:  *
1.11      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.11      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:
                     31: /*
                     32:  *  This file contains the routines that display information on the screen.
                     33:  *  Each section of the screen has two routines:  one for initially writing
                     34:  *  all constant and dynamic text, and one for only updating the text that
                     35:  *  changes.  The prefix "i_" is used on all the "initial" routines and the
                     36:  *  prefix "u_" is used for all the "updating" routines.
                     37:  *
                     38:  *  ASSUMPTIONS:
                     39:  *        None of the "i_" routines use any of the termcap capabilities.
                     40:  *        In this way, those routines can be safely used on terminals that
1.18      otto       41:  *        have minimal (or nonexistent) terminal capabilities.
1.1       downsj     42:  *
                     43:  *        The routines are called in this order:  *_loadave, i_timeofday,
                     44:  *        *_procstates, *_cpustates, *_memory, *_message, *_header,
                     45:  *        *_process, u_endscreen.
                     46:  */
                     47:
1.2       downsj     48: #include <sys/types.h>
1.20      millert    49: #include <sys/sched.h>
1.25      otto       50: #include <curses.h>
                     51: #include <errno.h>
1.2       downsj     52: #include <stdio.h>
1.1       downsj     53: #include <ctype.h>
1.20      millert    54: #include <err.h>
1.25      otto       55: #include <signal.h>
1.2       downsj     56: #include <stdlib.h>
                     57: #include <string.h>
                     58: #include <unistd.h>
1.1       downsj     59:
                     60: #include "screen.h"            /* interface to screen package */
                     61: #include "layout.h"            /* defines for screen position layout */
                     62: #include "display.h"
                     63: #include "top.h"
                     64: #include "boolean.h"
                     65: #include "machine.h"           /* we should eliminate this!!! */
                     66: #include "utils.h"
                     67:
                     68: #ifdef DEBUG
1.14      deraadt    69: FILE           *debug;
1.1       downsj     70: #endif
                     71:
1.14      deraadt    72: static int      display_width = MAX_COLS;
                     73:
1.20      millert    74: static char    *cpustates_tag(int);
1.14      deraadt    75: static int      string_count(char **);
1.16      millert    76: static void     summary_format(char *, size_t, int *, char **);
1.32      otto       77: static int     readlinedumb(char *, int);
1.2       downsj     78:
1.1       downsj     79: #define lineindex(l) ((l)*display_width)
                     80:
1.18      otto       81: /* things initialized by display_init and used throughout */
1.1       downsj     82:
                     83: /* buffer of proc information lines for display updating */
1.14      deraadt    84: char           *screenbuf = NULL;
1.1       downsj     85:
1.14      deraadt    86: static char   **procstate_names;
                     87: static char   **cpustate_names;
                     88: static char   **memory_names;
                     89:
                     90: static int      num_cpustates;
                     91:
                     92: static int     *cpustate_columns;
                     93: static int      cpustate_total_length;
                     94:
1.20      millert    95: /* display ips */
                     96: int y_mem;
                     97: int y_message;
                     98: int y_header;
                     99: int y_idlecursor;
                    100: int y_procs;
                    101: extern int ncpu;
1.34      tedu      102: extern int combine_cpus;
1.20      millert   103:
1.33      otto      104: int header_status = Yes;
1.1       downsj    105:
1.25      otto      106: static int
                    107: empty(void)
                    108: {
                    109:        return OK;
                    110: }
                    111:
                    112: static int
                    113: myfputs(const char *s)
                    114: {
                    115:        return fputs(s, stdout);
                    116: }
                    117:
                    118: static int (*addstrp)(const char *);
                    119: static int (*printwp)(const char *, ...);
                    120: static int (*standoutp)(void);
                    121: static int (*standendp)(void);
                    122:
1.12      pvalchev  123: int
                    124: display_resize(void)
1.1       downsj    125: {
1.14      deraadt   126:        int display_lines;
1.34      tedu      127:        int cpu_lines = (combine_cpus ? 1 : ncpu);
                    128:
                    129:        y_mem = 2 + cpu_lines;
                    130:        y_header = 4 + cpu_lines;
                    131:        y_procs = 5 + cpu_lines;
1.1       downsj    132:
1.14      deraadt   133:        /* calculate the current dimensions */
                    134:        /* if operating in "dumb" mode, we only need one line */
1.37      otto      135:        display_lines = smart_terminal ? screen_length - y_procs : 1;
1.14      deraadt   136:
1.34      tedu      137:        y_idlecursor = y_message = 3 + (combine_cpus ? 1 : ncpu);
1.30      otto      138:        if (screen_length <= y_message)
                    139:                y_idlecursor = y_message = screen_length - 1;
                    140:
1.14      deraadt   141:        /*
                    142:         * we don't want more than MAX_COLS columns, since the
                    143:         * machine-dependent modules make static allocations based on
                    144:         * MAX_COLS and we don't want to run off the end of their buffers
                    145:         */
                    146:        display_width = screen_width;
                    147:        if (display_width >= MAX_COLS)
                    148:                display_width = MAX_COLS - 1;
                    149:
                    150:        /* return number of lines available */
                    151:        /* for dumb terminals, pretend like we can show any amount */
                    152:        return (smart_terminal ? display_lines : Largest);
1.1       downsj    153: }
                    154:
1.12      pvalchev  155: int
1.14      deraadt   156: display_init(struct statics * statics)
1.1       downsj    157: {
1.34      tedu      158:        int display_lines, *ip, i;
1.14      deraadt   159:        char **pp;
                    160:
1.25      otto      161:        if (smart_terminal) {
                    162:                addstrp = addstr;
                    163:                printwp = (int(*)(const char *, ...))printw;
                    164:                standoutp = standout;
                    165:                standendp = standend;
                    166:        } else {
                    167:                addstrp = myfputs;
                    168:                printwp = printf;
                    169:                standoutp = empty;
                    170:                standendp = empty;
                    171:        }
                    172:
1.14      deraadt   173:        /* call resize to do the dirty work */
                    174:        display_lines = display_resize();
                    175:
                    176:        /* only do the rest if we need to */
1.30      otto      177:        /* save pointers and allocate space for names */
                    178:        procstate_names = statics->procstate_names;
                    179:
                    180:        cpustate_names = statics->cpustate_names;
                    181:        num_cpustates = string_count(cpustate_names);
                    182:
                    183:        cpustate_columns = calloc(num_cpustates, sizeof(int));
                    184:        if (cpustate_columns == NULL)
                    185:                err(1, NULL);
                    186:
                    187:        memory_names = statics->memory_names;
                    188:
                    189:        /* calculate starting columns where needed */
                    190:        cpustate_total_length = 0;
                    191:        pp = cpustate_names;
                    192:        ip = cpustate_columns;
                    193:        while (*pp != NULL) {
                    194:                if ((i = strlen(*pp++)) > 0) {
                    195:                        *ip++ = cpustate_total_length;
                    196:                        cpustate_total_length += i + 8;
1.20      millert   197:                }
1.30      otto      198:        }
1.14      deraadt   199:
1.30      otto      200:        if (display_lines < 0)
                    201:                display_lines = 0;
1.14      deraadt   202:
                    203:        /* return number of lines available */
                    204:        return (display_lines);
1.1       downsj    205: }
                    206:
1.12      pvalchev  207: void
                    208: i_loadave(pid_t mpid, double *avenrun)
1.1       downsj    209: {
1.30      otto      210:        if (screen_length > 1 || !smart_terminal) {
                    211:                int i;
1.14      deraadt   212:
1.30      otto      213:                move(0, 0);
                    214:                clrtoeol();
1.1       downsj    215:
1.30      otto      216:                addstrp("load averages");
                    217:                /* mpid == -1 implies this system doesn't have an _mpid */
                    218:                if (mpid != -1)
                    219:                        printwp("last pid: %5ld;  ", (long) mpid);
1.1       downsj    220:
1.30      otto      221:                for (i = 0; i < 3; i++)
                    222:                        printwp("%c %5.2f", i == 0 ? ':' : ',', avenrun[i]);
                    223:        }
1.1       downsj    224: }
                    225:
1.14      deraadt   226: /*
                    227:  *  Display the current time.
                    228:  *  "ctime" always returns a string that looks like this:
                    229:  *
                    230:  *     Sun Sep 16 01:03:52 1973
                    231:  *      012345678901234567890123
                    232:  *               1         2
                    233:  *
                    234:  *  We want indices 11 thru 18 (length 8).
                    235:  */
                    236:
1.12      pvalchev  237: void
1.14      deraadt   238: i_timeofday(time_t * tod)
1.1       downsj    239: {
1.30      otto      240:        if (screen_length > 1 || !smart_terminal) {
                    241:                if (smart_terminal) {
                    242:                        move(0, screen_width - 8);
                    243:                } else {
                    244:                        if (fputs("    ", stdout) == EOF)
                    245:                                exit(1);
                    246:                }
1.1       downsj    247: #ifdef DEBUG
1.30      otto      248:                {
                    249:                        char *foo;
                    250:                        foo = ctime(tod);
                    251:                        addstrp(foo);
                    252:                }
                    253: #endif
                    254:                printwp("%-8.8s", &(ctime(tod)[11]));
                    255:                putn();
1.14      deraadt   256:        }
1.1       downsj    257: }
                    258:
                    259: /*
                    260:  *  *_procstates(total, brkdn, names) - print the process summary line
                    261:  *
                    262:  *  Assumptions:  cursor is at the beginning of the line on entry
                    263:  */
1.12      pvalchev  264: void
                    265: i_procstates(int total, int *brkdn)
1.1       downsj    266: {
1.30      otto      267:        if (screen_length > 2 || !smart_terminal) {
                    268:                int i;
                    269:                char procstates_buffer[MAX_COLS];
                    270:
                    271:                move(1, 0);
                    272:                clrtoeol();
                    273:                /* write current number of processes and remember the value */
                    274:                printwp("%d processes:", total);
1.1       downsj    275:
1.30      otto      276:                if (smart_terminal)
                    277:                        move(1, 15);
                    278:                else {
                    279:                        /* put out enough spaces to get to column 15 */
                    280:                        i = digits(total);
                    281:                        while (i++ < 4) {
                    282:                                if (putchar(' ') == EOF)
                    283:                                        exit(1);
                    284:                        }
1.25      otto      285:                }
1.14      deraadt   286:
1.30      otto      287:                /* format and print the process state summary */
                    288:                summary_format(procstates_buffer, sizeof(procstates_buffer), brkdn,
                    289:                    procstate_names);
1.1       downsj    290:
1.30      otto      291:                addstrp(procstates_buffer);
                    292:                putn();
                    293:        }
1.1       downsj    294: }
                    295:
                    296: /*
                    297:  *  *_cpustates(states, names) - print the cpu state percentages
                    298:  *
                    299:  *  Assumptions:  cursor is on the PREVIOUS line
                    300:  */
                    301:
                    302: /* cpustates_tag() calculates the correct tag to use to label the line */
                    303:
1.12      pvalchev  304: static char *
1.20      millert   305: cpustates_tag(int cpu)
1.1       downsj    306: {
1.30      otto      307:        if (screen_length > 3 || !smart_terminal) {
                    308:                static char *tag;
                    309:                static int cpulen, old_width;
                    310:                int i;
                    311:
                    312:                if (cpulen == 0 && ncpu > 1) {
                    313:                        /* compute length of the cpu string */
                    314:                        for (i = ncpu; i > 0; cpulen++, i /= 10)
                    315:                                continue;
                    316:                }
1.20      millert   317:
1.30      otto      318:                if (old_width == screen_width) {
                    319:                        if (ncpu > 1) {
                    320:                                /* just store the cpu number in the tag */
                    321:                                i = tag[3 + cpulen];
                    322:                                snprintf(tag + 3, cpulen + 1, "%.*d", cpulen, cpu);
                    323:                                tag[3 + cpulen] = i;
                    324:                        }
                    325:                } else {
                    326:                        /*
                    327:                         * use a long tag if it will fit, otherwise use short one.
                    328:                         */
                    329:                        free(tag);
                    330:                        if (cpustate_total_length + 10 + cpulen >= screen_width)
                    331:                                i = asprintf(&tag, "CPU%.*d: ", cpulen, cpu);
                    332:                        else
                    333:                                i = asprintf(&tag, "CPU%.*d states: ", cpulen, cpu);
                    334:                        if (i == -1)
                    335:                                tag = NULL;
                    336:                        else
                    337:                                old_width = screen_width;
1.20      millert   338:                }
1.30      otto      339:                return (tag);
                    340:        } else
1.34      tedu      341:                return ("\0");
1.1       downsj    342: }
                    343:
1.12      pvalchev  344: void
1.20      millert   345: i_cpustates(int64_t *ostates)
1.1       downsj    346: {
1.34      tedu      347:        int i, first, cpu;
                    348:        double value;
1.20      millert   349:        int64_t *states;
1.34      tedu      350:        char **names, *thisname;
                    351:
                    352:        if (combine_cpus) {
                    353:                static double *values;
                    354:                if (!values) {
                    355:                        values = calloc(num_cpustates, sizeof(*values));
                    356:                        if (!values)
                    357:                                err(1, NULL);
                    358:                }
                    359:                memset(values, 0, num_cpustates * sizeof(*values));
                    360:                for (cpu = 0; cpu < ncpu; cpu++) {
                    361:                        names = cpustate_names;
                    362:                        states = ostates + (CPUSTATES * cpu);
                    363:                        i = 0;
                    364:                        while ((thisname = *names++) != NULL) {
                    365:                                if (*thisname != '\0') {
                    366:                                        /* retrieve the value and remember it */
                    367:                                        values[i++] += *states++;
                    368:                                }
                    369:                        }
                    370:                }
                    371:                if (screen_length > 2 || !smart_terminal) {
                    372:                        names = cpustate_names;
                    373:                        i = 0;
                    374:                        first = 0;
                    375:                        move(2, 0);
                    376:                        clrtoeol();
                    377:                        addstrp("All CPUs: ");
1.14      deraadt   378:
1.34      tedu      379:                        while ((thisname = *names++) != NULL) {
                    380:                                if (*thisname != '\0') {
                    381:                                        value = values[i++] / ncpu;
                    382:                                        /* if percentage is >= 1000, print it as 100% */
                    383:                                        printwp((value >= 1000 ? "%s%4.0f%% %s" :
                    384:                                            "%s%4.1f%% %s"), first++ == 0 ? "" : ", ",
                    385:                                            value / 10., thisname);
                    386:                                }
                    387:                        }
                    388:                        putn();
                    389:                }
                    390:                return;
                    391:        }
1.20      millert   392:        for (cpu = 0; cpu < ncpu; cpu++) {
                    393:                /* now walk thru the names and print the line */
                    394:                names = cpustate_names;
1.34      tedu      395:                first = 0;
1.20      millert   396:                states = ostates + (CPUSTATES * cpu);
1.30      otto      397:
                    398:                if (screen_length > 2 + cpu || !smart_terminal) {
                    399:                        move(2 + cpu, 0);
                    400:                        clrtoeol();
                    401:                        addstrp(cpustates_tag(cpu));
                    402:
                    403:                        while ((thisname = *names++) != NULL) {
                    404:                                if (*thisname != '\0') {
                    405:                                        /* retrieve the value and remember it */
                    406:                                        value = *states++;
                    407:
                    408:                                        /* if percentage is >= 1000, print it as 100% */
                    409:                                        printwp((value >= 1000 ? "%s%4.0f%% %s" :
1.34      tedu      410:                                            "%s%4.1f%% %s"), first++ == 0 ? "" : ", ",
                    411:                                            value / 10., thisname);
1.30      otto      412:                                }
1.20      millert   413:                        }
1.30      otto      414:                        putn();
1.14      deraadt   415:                }
1.1       downsj    416:        }
                    417: }
                    418:
                    419: /*
                    420:  *  *_memory(stats) - print "Memory: " followed by the memory summary string
                    421:  */
1.12      pvalchev  422: void
                    423: i_memory(int *stats)
1.1       downsj    424: {
1.30      otto      425:        if (screen_length > y_mem || !smart_terminal) {
                    426:                char memory_buffer[MAX_COLS];
                    427:
                    428:                move(y_mem, 0);
                    429:                clrtoeol();
                    430:                addstrp("Memory: ");
1.25      otto      431:
1.30      otto      432:                /* format and print the memory summary */
                    433:                summary_format(memory_buffer, sizeof(memory_buffer), stats,
                    434:                    memory_names);
                    435:                addstrp(memory_buffer);
                    436:                putn();
                    437:        }
1.1       downsj    438: }
                    439:
                    440: /*
                    441:  *  *_message() - print the next pending message line, or erase the one
                    442:  *                that is there.
                    443:  */
                    444:
                    445: /*
                    446:  *  i_message is funny because it gets its message asynchronously (with
                    447:  *     respect to screen updates).
                    448:  */
                    449:
1.14      deraadt   450: static char     next_msg[MAX_COLS + 5];
1.31      otto      451: static int      msgon = 0;
1.1       downsj    452:
1.12      pvalchev  453: void
                    454: i_message(void)
1.1       downsj    455: {
1.25      otto      456:        move(y_message, 0);
1.14      deraadt   457:        if (next_msg[0] != '\0') {
1.25      otto      458:                standoutp();
                    459:                addstrp(next_msg);
                    460:                standendp();
                    461:                clrtoeol();
1.31      otto      462:                msgon = TRUE;
1.14      deraadt   463:                next_msg[0] = '\0';
1.31      otto      464:        } else if (msgon) {
1.25      otto      465:                clrtoeol();
1.31      otto      466:                msgon = FALSE;
1.14      deraadt   467:        }
1.1       downsj    468: }
                    469:
                    470: /*
                    471:  *  *_header(text) - print the header for the process area
                    472:  */
                    473:
1.12      pvalchev  474: void
                    475: i_header(char *text)
1.1       downsj    476: {
1.33      otto      477:        if (header_status == Yes && (screen_length > y_header
1.30      otto      478:               || !smart_terminal)) {
1.25      otto      479:                if (!smart_terminal) {
                    480:                        putn();
                    481:                        if (fputs(text, stdout) == EOF)
                    482:                                exit(1);
                    483:                        putn();
                    484:                } else {
                    485:                        move(y_header, 0);
                    486:                        clrtoeol();
                    487:                        addstrp(text);
                    488:                }
1.14      deraadt   489:        }
1.1       downsj    490: }
                    491:
                    492: /*
                    493:  *  *_process(line, thisline) - print one process line
                    494:  */
                    495:
1.12      pvalchev  496: void
1.25      otto      497: i_process(int line, char *thisline, int hl)
1.1       downsj    498: {
1.14      deraadt   499:        /* make sure we are on the correct line */
1.25      otto      500:        move(y_procs + line, 0);
1.1       downsj    501:
1.14      deraadt   502:        /* truncate the line to conform to our current screen width */
                    503:        thisline[display_width] = '\0';
1.1       downsj    504:
1.14      deraadt   505:        /* write the line out */
1.25      otto      506:        if (hl && smart_terminal)
                    507:                standoutp();
                    508:        addstrp(thisline);
                    509:        if (hl && smart_terminal)
                    510:                standendp();
                    511:        putn();
                    512:        clrtoeol();
1.1       downsj    513: }
                    514:
1.12      pvalchev  515: void
1.27      deraadt   516: u_endscreen(void)
1.1       downsj    517: {
1.14      deraadt   518:        if (smart_terminal) {
1.25      otto      519:                clrtobot();
1.14      deraadt   520:                /* move the cursor to a pleasant place */
1.25      otto      521:                move(y_idlecursor, x_idlecursor);
1.14      deraadt   522:        } else {
                    523:                /*
                    524:                 * separate this display from the next with some vertical
                    525:                 * room
                    526:                 */
                    527:                if (fputs("\n\n", stdout) == EOF)
                    528:                        exit(1);
1.1       downsj    529:        }
                    530: }
                    531:
1.12      pvalchev  532: void
1.33      otto      533: display_header(int status)
1.1       downsj    534: {
1.33      otto      535:        header_status = status;
1.1       downsj    536: }
                    537:
1.12      pvalchev  538: void
1.14      deraadt   539: new_message(int type, const char *msgfmt,...)
                    540: {
                    541:        va_list ap;
                    542:
                    543:        va_start(ap, msgfmt);
                    544:        /* first, format the message */
                    545:        vsnprintf(next_msg, sizeof(next_msg), msgfmt, ap);
                    546:        va_end(ap);
                    547:
1.31      otto      548:        if (next_msg[0] != '\0') {
1.14      deraadt   549:                /* message there already -- can we clear it? */
1.25      otto      550:                /* yes -- write it and clear to end */
                    551:                if ((type & MT_delayed) == 0) {
                    552:                        move(y_message, 0);
                    553:                        if (type & MT_standout)
                    554:                                standoutp();
                    555:                        addstrp(next_msg);
                    556:                        if (type & MT_standout)
                    557:                                standendp();
                    558:                        clrtoeol();
1.31      otto      559:                        msgon = TRUE;
1.14      deraadt   560:                        next_msg[0] = '\0';
1.31      otto      561:                        if (smart_terminal)
                    562:                                refresh();
1.5       deraadt   563:                }
1.1       downsj    564:        }
                    565: }
                    566:
1.12      pvalchev  567: void
                    568: clear_message(void)
1.1       downsj    569: {
1.25      otto      570:        move(y_message, 0);
                    571:        clrtoeol();
1.1       downsj    572: }
                    573:
1.25      otto      574:
                    575: static int
1.32      otto      576: readlinedumb(char *buffer, int size)
1.1       downsj    577: {
1.14      deraadt   578:        char *ptr = buffer, ch, cnt = 0, maxcnt = 0;
1.17      deraadt   579:        extern volatile sig_atomic_t leaveflag;
                    580:        ssize_t len;
1.14      deraadt   581:
                    582:        /* allow room for null terminator */
                    583:        size -= 1;
                    584:
                    585:        /* read loop */
1.19      deraadt   586:        while ((fflush(stdout), (len = read(STDIN_FILENO, ptr, 1)) > 0)) {
1.17      deraadt   587:
                    588:                if (len == 0 || leaveflag) {
                    589:                        end_screen();
                    590:                        exit(0);
                    591:                }
                    592:
1.14      deraadt   593:                /* newline means we are done */
                    594:                if ((ch = *ptr) == '\n')
                    595:                        break;
                    596:
                    597:                /* handle special editing characters */
                    598:                if (ch == ch_kill) {
                    599:                        /* return null string */
                    600:                        *buffer = '\0';
1.25      otto      601:                        putr();
1.14      deraadt   602:                        return (-1);
                    603:                } else if (ch == ch_erase) {
                    604:                        /* erase previous character */
                    605:                        if (cnt <= 0) {
                    606:                                /* none to erase! */
                    607:                                if (putchar('\7') == EOF)
                    608:                                        exit(1);
                    609:                        } else {
                    610:                                if (fputs("\b \b", stdout) == EOF)
                    611:                                        exit(1);
                    612:                                ptr--;
                    613:                                cnt--;
                    614:                        }
                    615:                }
                    616:                /* check for character validity and buffer overflow */
1.32      otto      617:                else if (cnt == size || !isprint(ch)) {
1.14      deraadt   618:                        /* not legal */
                    619:                        if (putchar('\7') == EOF)
                    620:                                exit(1);
                    621:                } else {
                    622:                        /* echo it and store it in the buffer */
                    623:                        if (putchar(ch) == EOF)
                    624:                                exit(1);
                    625:                        ptr++;
                    626:                        cnt++;
                    627:                        if (cnt > maxcnt)
                    628:                                maxcnt = cnt;
                    629:                }
1.1       downsj    630:        }
                    631:
1.14      deraadt   632:        /* all done -- null terminate the string */
                    633:        *ptr = '\0';
                    634:
                    635:        /* return either inputted number or string length */
1.25      otto      636:        putr();
1.32      otto      637:        return (cnt == 0 ? -1 : cnt);
1.25      otto      638: }
                    639:
                    640: int
1.32      otto      641: readline(char *buffer, int size)
1.25      otto      642: {
                    643:        size_t cnt;
                    644:
                    645:        /* allow room for null terminator */
                    646:        size -= 1;
                    647:
                    648:        if (smart_terminal)
                    649:                getnstr(buffer, size);
                    650:        else
1.32      otto      651:                return readlinedumb(buffer, size);
1.25      otto      652:
                    653:        cnt = strlen(buffer);
                    654:        if (cnt > 0 && buffer[cnt - 1] == '\n')
                    655:                buffer[cnt - 1] = '\0';
1.32      otto      656:        return (cnt == 0 ? -1 : cnt);
1.1       downsj    657: }
                    658:
                    659: /* internal support routines */
1.12      pvalchev  660: static int
                    661: string_count(char **pp)
1.1       downsj    662: {
1.14      deraadt   663:        int cnt;
1.1       downsj    664:
1.14      deraadt   665:        cnt = 0;
                    666:        while (*pp++ != NULL)
                    667:                cnt++;
                    668:        return (cnt);
1.1       downsj    669: }
                    670:
1.16      millert   671: #define        COPYLEFT(to, from)                              \
                    672:        do {                                            \
                    673:                len = strlcpy((to), (from), left);      \
                    674:                if (len >= left)                        \
                    675:                        return;                         \
                    676:                p += len;                               \
                    677:                left -= len;                            \
                    678:        } while (0)
                    679:
1.12      pvalchev  680: static void
1.16      millert   681: summary_format(char *buf, size_t left, int *numbers, char **names)
1.1       downsj    682: {
1.14      deraadt   683:        char *p, *thisname;
1.16      millert   684:        size_t len;
1.14      deraadt   685:        int num;
1.1       downsj    686:
1.14      deraadt   687:        /* format each number followed by its string */
1.16      millert   688:        p = buf;
1.14      deraadt   689:        while ((thisname = *names++) != NULL) {
                    690:                /* get the number to format */
                    691:                num = *numbers++;
                    692:
                    693:                if (num >= 0) {
                    694:                        /* is this number in kilobytes? */
                    695:                        if (thisname[0] == 'K') {
                    696:                                /* yes: format it as a memory value */
1.16      millert   697:                                COPYLEFT(p, format_k(num));
1.14      deraadt   698:
                    699:                                /*
                    700:                                 * skip over the K, since it was included by
                    701:                                 * format_k
                    702:                                 */
1.16      millert   703:                                COPYLEFT(p, thisname + 1);
1.14      deraadt   704:                        } else if (num > 0) {
1.16      millert   705:                                len = snprintf(p, left, "%d%s", num, thisname);
                    706:                                if (len == (size_t)-1 || len >= left)
                    707:                                        return;
                    708:                                p += len;
                    709:                                left -= len;
1.14      deraadt   710:                        }
1.16      millert   711:                } else {
                    712:                        /*
                    713:                         * Ignore negative numbers, but display corresponding
                    714:                         * string.
                    715:                         */
                    716:                        COPYLEFT(p, thisname);
1.14      deraadt   717:                }
1.1       downsj    718:        }
                    719:
1.14      deraadt   720:        /* if the last two characters in the string are ", ", delete them */
                    721:        p -= 2;
1.16      millert   722:        if (p >= buf && p[0] == ',' && p[1] == ' ')
1.14      deraadt   723:                *p = '\0';
1.1       downsj    724: }
                    725:
                    726: /*
                    727:  *  printable(str) - make the string pointed to by "str" into one that is
                    728:  *     printable (i.e.: all ascii), by converting all non-printable
                    729:  *     characters into '?'.  Replacements are done in place and a pointer
                    730:  *     to the original buffer is returned.
                    731:  */
1.12      pvalchev  732: char *
                    733: printable(char *str)
1.1       downsj    734: {
1.14      deraadt   735:        char *ptr, ch;
1.1       downsj    736:
1.14      deraadt   737:        ptr = str;
                    738:        while ((ch = *ptr) != '\0') {
                    739:                if (!isprint(ch))
                    740:                        *ptr = '?';
                    741:                ptr++;
1.1       downsj    742:        }
1.14      deraadt   743:        return (str);
1.25      otto      744: }
                    745:
                    746:
                    747: /*
                    748:  *  show_help() - display the help screen; invoked in response to
                    749:  *             either 'h' or '?'.
                    750:  */
                    751: void
                    752: show_help(void)
                    753: {
                    754:        if (smart_terminal) {
                    755:                clear();
                    756:                nl();
                    757:        }
                    758:        printwp("These single-character commands are available:\n"
                    759:            "\n"
                    760:            "^L           - redraw screen\n"
                    761:            "+            - reset any g, p, or u filters\n"
1.35      jmc       762:            "1            - display CPU statistics on a single line\n"
1.25      otto      763:            "C            - toggle the display of command line arguments\n"
                    764:            "d count      - show `count' displays, then exit\n"
                    765:            "e            - list errors generated by last \"kill\" or \"renice\" command\n"
                    766:            "h | ?        - help; show this text\n"
                    767:            "g string     - filter on command name (g+ selects all commands)\n"
                    768:            "I | i        - toggle the display of idle processes\n"
                    769:            "k [-sig] pid - send signal `-sig' to process `pid'\n"
                    770:            "n|# count    - show `count' processes\n"
1.36      tedu      771:            "o field      - specify sort order (size, res, cpu, time, pri, pid, command)\n"
1.25      otto      772:            "P pid        - highlight process `pid' (P+ switches highlighting off)\n"
                    773:            "p pid        - display process by `pid' (p+ selects all processes)\n"
                    774:            "q            - quit\n"
                    775:            "r count pid  - renice process `pid' to nice value `count'\n"
                    776:            "S            - toggle the display of system processes\n"
                    777:            "s time       - change delay between displays to `time' seconds\n"
                    778:            "T            - toggle the display of threads\n"
                    779:            "u user       - display processes for `user' (u+ selects all users)\n"
                    780:            "\n");
                    781:
                    782:        if (smart_terminal) {
                    783:                nonl();
                    784:                refresh();
                    785:        }
                    786: }
                    787:
                    788: /*
                    789:  *  show_errors() - display on stdout the current log of errors.
                    790:  */
                    791: void
                    792: show_errors(void)
                    793: {
                    794:        struct errs *errp = errs;
                    795:        int cnt = 0;
                    796:
                    797:        if (smart_terminal) {
                    798:                clear();
                    799:                nl();
                    800:        }
                    801:        printwp("%d error%s:\n\n", errcnt, errcnt == 1 ? "" : "s");
                    802:        while (cnt++ < errcnt) {
1.38    ! otto      803:                printwp("%5s: %s\n", errp->arg,
1.25      otto      804:                    errp->err == 0 ? "Not a number" : strerror(errp->err));
                    805:                errp++;
                    806:        }
1.38    ! otto      807:        printwp("\n");
1.25      otto      808:        if (smart_terminal) {
                    809:                nonl();
                    810:                refresh();
                    811:        }
                    812: }
                    813:
                    814: void
                    815: anykey(void)
                    816: {
                    817:        int ch;
1.28      deraadt   818:        ssize_t len;
1.25      otto      819:
                    820:        standoutp();
                    821:        addstrp("Hit any key to continue: ");
                    822:        standendp();
                    823:        if (smart_terminal)
                    824:                refresh();
                    825:        else
                    826:                fflush(stdout);
                    827:        while (1) {
                    828:                len = read(STDIN_FILENO, &ch, 1);
                    829:                if (len == -1 && errno == EINTR)
                    830:                        continue;
                    831:                if (len == 0)
                    832:                        exit(1);
                    833:                break;
                    834:        }
1.1       downsj    835: }