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

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