Annotation of src/usr.bin/top/display.c, Revision 1.22
1.22 ! jaredy 1: /* $OpenBSD: display.c,v 1.21 2005/06/17 12:12:28 markus 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/time.h>
50: #include <sys/sched.h>
1.2 downsj 51: #include <stdio.h>
1.1 downsj 52: #include <ctype.h>
1.20 millert 53: #include <err.h>
1.2 downsj 54: #include <stdlib.h>
55: #include <string.h>
1.17 deraadt 56: #include <signal.h>
1.2 downsj 57: #include <term.h>
1.1 downsj 58: #include <time.h>
1.2 downsj 59: #include <unistd.h>
1.13 pvalchev 60: #include <stdarg.h>
1.1 downsj 61:
62: #include "screen.h" /* interface to screen package */
63: #include "layout.h" /* defines for screen position layout */
64: #include "display.h"
65: #include "top.h"
66: #include "top.local.h"
67: #include "boolean.h"
68: #include "machine.h" /* we should eliminate this!!! */
69: #include "utils.h"
70:
71: #ifdef DEBUG
1.14 deraadt 72: FILE *debug;
1.1 downsj 73: #endif
74:
1.14 deraadt 75: static pid_t lmpid = 0;
76: static int last_hi = 0; /* used in u_process and u_endscreen */
77: static int lastline = 0;
78: static int display_width = MAX_COLS;
79:
1.20 millert 80: static char *cpustates_tag(int);
1.14 deraadt 81: static int string_count(char **);
1.16 millert 82: static void summary_format(char *, size_t, int *, char **);
1.14 deraadt 83: static void line_update(char *, char *, int, int);
1.2 downsj 84:
1.1 downsj 85: #define lineindex(l) ((l)*display_width)
86:
1.18 otto 87: /* things initialized by display_init and used throughout */
1.1 downsj 88:
89: /* buffer of proc information lines for display updating */
1.14 deraadt 90: char *screenbuf = NULL;
1.1 downsj 91:
1.14 deraadt 92: static char **procstate_names;
93: static char **cpustate_names;
94: static char **memory_names;
95:
96: static int num_procstates;
97: static int num_cpustates;
98:
99: static int *lprocstates;
1.20 millert 100: static int64_t **lcpustates;
1.14 deraadt 101:
102: static int *cpustate_columns;
103: static int cpustate_total_length;
104:
1.20 millert 105: /* display ips */
106: int y_mem;
107: int y_message;
108: int y_header;
109: int y_idlecursor;
110: int y_procs;
111: extern int ncpu;
112: int Header_lines;
113:
1.14 deraadt 114: static enum {
115: OFF, ON, ERASE
116: } header_status = ON;
1.1 downsj 117:
1.12 pvalchev 118: int
119: display_resize(void)
1.1 downsj 120: {
1.14 deraadt 121: int display_lines;
1.1 downsj 122:
1.14 deraadt 123: /* first, deallocate any previous buffer that may have been there */
124: if (screenbuf != NULL)
125: free(screenbuf);
126:
127: /* calculate the current dimensions */
128: /* if operating in "dumb" mode, we only need one line */
129: display_lines = smart_terminal ? screen_length - Header_lines : 1;
130:
131: /*
132: * we don't want more than MAX_COLS columns, since the
133: * machine-dependent modules make static allocations based on
134: * MAX_COLS and we don't want to run off the end of their buffers
135: */
136: display_width = screen_width;
137: if (display_width >= MAX_COLS)
138: display_width = MAX_COLS - 1;
139:
140: /* now, allocate space for the screen buffer */
1.22 ! jaredy 141: screenbuf = malloc(display_lines * display_width + 1);
1.20 millert 142: if (screenbuf == NULL)
1.14 deraadt 143: return (-1);
144:
145: /* return number of lines available */
146: /* for dumb terminals, pretend like we can show any amount */
147: return (smart_terminal ? display_lines : Largest);
1.1 downsj 148: }
149:
1.12 pvalchev 150: int
1.14 deraadt 151: display_init(struct statics * statics)
1.1 downsj 152: {
1.20 millert 153: int display_lines, *ip, i, cpu;
1.14 deraadt 154: char **pp;
155:
1.20 millert 156: y_mem = 2 + ncpu;
157: y_message = 3 + ncpu;
158: y_header = 4 + ncpu;
159: y_idlecursor = 3 + ncpu;
160: y_procs = 5 + ncpu;
161: Header_lines = 5 + ncpu;
162:
1.14 deraadt 163: /* call resize to do the dirty work */
164: display_lines = display_resize();
165:
166: /* only do the rest if we need to */
167: if (display_lines > -1) {
168: /* save pointers and allocate space for names */
169: procstate_names = statics->procstate_names;
170: num_procstates = string_count(procstate_names);
1.20 millert 171: lprocstates = malloc(num_procstates * sizeof(int));
172: if (lprocstates == NULL)
173: err(1, NULL);
1.14 deraadt 174:
175: cpustate_names = statics->cpustate_names;
176: num_cpustates = string_count(cpustate_names);
1.20 millert 177: lcpustates = malloc(ncpu * sizeof(int64_t *));
178: if (lcpustates == NULL)
179: err(1, NULL);
180: for (cpu = 0; cpu < ncpu; cpu++) {
181: lcpustates[cpu] = malloc(num_cpustates * sizeof(int64_t));
182: if (lcpustates[cpu] == NULL)
183: err(1, NULL);
184: }
185:
186: cpustate_columns = malloc(num_cpustates * sizeof(int));
187: if (cpustate_columns == NULL)
188: err(1, NULL);
1.14 deraadt 189:
190: memory_names = statics->memory_names;
191:
192: /* calculate starting columns where needed */
193: cpustate_total_length = 0;
194: pp = cpustate_names;
195: ip = cpustate_columns;
196: while (*pp != NULL) {
197: if ((i = strlen(*pp++)) > 0) {
198: *ip++ = cpustate_total_length;
199: cpustate_total_length += i + 8;
200: }
201: }
1.1 downsj 202: }
1.14 deraadt 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.14 deraadt 210: int i;
211:
212: /* i_loadave also clears the screen, since it is first */
213: clear();
1.1 downsj 214:
1.14 deraadt 215: /* mpid == -1 implies this system doesn't have an _mpid */
216: if (mpid != -1)
217: printf("last pid: %5ld; ", (long) mpid);
1.1 downsj 218:
1.14 deraadt 219: printf("load averages");
220:
221: for (i = 0; i < 3; i++)
222: printf("%c %5.2f", i == 0 ? ':' : ',', avenrun[i]);
223:
224: lmpid = mpid;
1.1 downsj 225: }
226:
1.12 pvalchev 227: void
228: u_loadave(pid_t mpid, double *avenrun)
1.1 downsj 229: {
1.14 deraadt 230: int i;
1.1 downsj 231:
1.14 deraadt 232: if (mpid != -1) {
233: /* change screen only when value has really changed */
234: if (mpid != lmpid) {
235: Move_to(x_lastpid, y_lastpid);
236: printf("%5ld", (long) mpid);
237: lmpid = mpid;
238: }
239: /* i remembers x coordinate to move to */
240: i = x_loadave;
241: } else
242: i = x_loadave_nompid;
243:
244: /* move into position for load averages */
245: Move_to(i, y_loadave);
246:
247: /* display new load averages */
248: /* we should optimize this and only display changes */
249: for (i = 0; i < 3; i++)
250: printf("%s%5.2f", i == 0 ? "" : ", ", avenrun[i]);
1.1 downsj 251: }
252:
1.14 deraadt 253: /*
254: * Display the current time.
255: * "ctime" always returns a string that looks like this:
256: *
257: * Sun Sep 16 01:03:52 1973
258: * 012345678901234567890123
259: * 1 2
260: *
261: * We want indices 11 thru 18 (length 8).
262: */
263:
1.12 pvalchev 264: void
1.14 deraadt 265: i_timeofday(time_t * tod)
1.1 downsj 266: {
1.14 deraadt 267:
268: if (smart_terminal) {
269: Move_to(screen_width - 8, 0);
270: } else {
271: if (fputs(" ", stdout) == EOF)
272: exit(1);
273: }
1.1 downsj 274: #ifdef DEBUG
1.14 deraadt 275: {
276: char *foo;
277: foo = ctime(tod);
278: if (fputs(foo, stdout) == EOF)
279: exit(1);
280: }
1.1 downsj 281: #endif
1.14 deraadt 282: printf("%-8.8s\n", &(ctime(tod)[11]));
283: lastline = 1;
1.1 downsj 284: }
285:
1.14 deraadt 286: static int ltotal = 0;
1.21 markus 287: static char procstates_buffer[MAX_COLS];
1.1 downsj 288:
289: /*
290: * *_procstates(total, brkdn, names) - print the process summary line
291: *
292: * Assumptions: cursor is at the beginning of the line on entry
293: * lastline is valid
294: */
1.12 pvalchev 295: void
296: i_procstates(int total, int *brkdn)
1.1 downsj 297: {
1.14 deraadt 298: int i;
1.1 downsj 299:
1.14 deraadt 300: /* write current number of processes and remember the value */
301: printf("%d processes:", total);
302: ltotal = total;
303:
304: /* put out enough spaces to get to column 15 */
305: i = digits(total);
306: while (i++ < 4) {
307: if (putchar(' ') == EOF)
308: exit(1);
309: }
310:
311: /* format and print the process state summary */
1.16 millert 312: summary_format(procstates_buffer, sizeof(procstates_buffer), brkdn,
313: procstate_names);
1.14 deraadt 314: if (fputs(procstates_buffer, stdout) == EOF)
1.5 deraadt 315: exit(1);
1.1 downsj 316:
1.14 deraadt 317: /* save the numbers for next time */
318: memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
1.1 downsj 319: }
320:
1.12 pvalchev 321: void
322: u_procstates(int total, int *brkdn)
1.1 downsj 323: {
1.21 markus 324: static char new[MAX_COLS];
1.14 deraadt 325: int i;
1.1 downsj 326:
1.14 deraadt 327: /* update number of processes only if it has changed */
328: if (ltotal != total) {
329: /* move and overwrite */
1.1 downsj 330: #if (x_procstate == 0)
1.14 deraadt 331: Move_to(x_procstate, y_procstate);
1.1 downsj 332: #else
1.14 deraadt 333: /* cursor is already there...no motion needed */
334: /* assert(lastline == 1); */
1.1 downsj 335: #endif
1.14 deraadt 336: printf("%d", total);
1.1 downsj 337:
1.14 deraadt 338: /* if number of digits differs, rewrite the label */
339: if (digits(total) != digits(ltotal)) {
340: if (fputs(" processes:", stdout) == EOF)
341: exit(1);
342: /* put out enough spaces to get to column 15 */
343: i = digits(total);
344: while (i++ < 4) {
345: if (putchar(' ') == EOF)
346: exit(1);
347: }
348: /* cursor may end up right where we want it!!! */
349: }
350: /* save new total */
351: ltotal = total;
352: }
353: /* see if any of the state numbers has changed */
354: if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0) {
355: /* format and update the line */
1.16 millert 356: summary_format(new, sizeof(new), brkdn, procstate_names);
1.14 deraadt 357: line_update(procstates_buffer, new, x_brkdn, y_brkdn);
358: memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
1.1 downsj 359: }
360: }
361:
362: /*
363: * *_cpustates(states, names) - print the cpu state percentages
364: *
365: * Assumptions: cursor is on the PREVIOUS line
366: */
367:
1.14 deraadt 368: static int cpustates_column;
1.1 downsj 369:
370: /* cpustates_tag() calculates the correct tag to use to label the line */
371:
1.12 pvalchev 372: static char *
1.20 millert 373: cpustates_tag(int cpu)
1.1 downsj 374: {
1.20 millert 375: static char *tag;
376: static int cpulen, old_width;
377: int i;
1.14 deraadt 378:
1.20 millert 379: if (cpulen == 0 && ncpu > 1) {
380: /* compute length of the cpu string */
381: for (i = ncpu; i > 0; cpulen++, i /= 10)
382: continue;
383: }
384:
385: if (old_width == screen_width) {
386: if (ncpu > 1) {
387: /* just store the cpu number in the tag */
388: i = tag[3 + cpulen];
389: snprintf(tag + 3, cpulen + 1, "%.*d", cpulen, cpu);
390: tag[3 + cpulen] = i;
391: }
392: } else {
393: /*
394: * use a long tag if it will fit, otherwise use short one.
395: */
396: free(tag);
397: if (cpustate_total_length + 10 + cpulen >= screen_width)
398: i = asprintf(&tag, "CPU%.*d: ", cpulen, cpu);
399: else
400: i = asprintf(&tag, "CPU%.*d states: ", cpulen, cpu);
401: if (i == -1)
402: tag = NULL;
403: else {
404: cpustates_column = strlen(tag);
405: old_width = screen_width;
406: }
407: }
408: return (tag);
1.1 downsj 409: }
410:
1.12 pvalchev 411: void
1.20 millert 412: i_cpustates(int64_t *ostates)
1.1 downsj 413: {
1.20 millert 414: int i, cpu, value;
415: int64_t *states;
1.14 deraadt 416: char **names = cpustate_names, *thisname;
417:
1.20 millert 418: for (cpu = 0; cpu < ncpu; cpu++) {
419: /* print tag and bump lastline */
420: printf("\n%s", cpustates_tag(cpu));
421: lastline++;
422:
423: /* now walk thru the names and print the line */
424: names = cpustate_names;
425: i = 0;
426: states = ostates + (CPUSTATES * cpu);
427: while ((thisname = *names++) != NULL) {
428: if (*thisname != '\0') {
429: /* retrieve the value and remember it */
430: value = *states++;
431:
432: /* if percentage is >= 1000, print it as 100% */
433: printf((value >= 1000 ? "%s%4.0f%% %s" :
434: "%s%4.1f%% %s"), i++ == 0 ? "" : ", ",
435: ((float) value) / 10., thisname);
436: }
1.14 deraadt 437: }
1.20 millert 438:
439: /* copy over values into "last" array */
440: memcpy(lcpustates[cpu], ostates, num_cpustates * sizeof(int64_t));
1.1 downsj 441: }
442: }
443:
1.12 pvalchev 444: void
1.20 millert 445: u_cpustates(int64_t *ostates)
1.1 downsj 446: {
1.20 millert 447: char **names, *thisname;
448: int cpu, value, *colp;
449: int64_t *lp, *states;
450:
451: for (cpu = 0; cpu < ncpu; cpu++) {
452: lastline = y_cpustates + cpu;
453: states = ostates + (CPUSTATES * cpu);
454: Move_to(cpustates_column, lastline);
455: lp = lcpustates[cpu];
456: colp = cpustate_columns;
457:
458: /* we could be much more optimal about this */
459: names = cpustate_names;
460: while ((thisname = *names++) != NULL) {
461: if (*thisname != '\0') {
462: /* did the value change since last time? */
463: if (*lp != *states) {
464: /* yes, move and change */
465: lastline = y_cpustates + cpu;
466: Move_to(cpustates_column + *colp,
467: lastline);
468:
469: /* retrieve value and remember it */
470: value = *states;
471:
472: /* if percentage is >= 1000,
473: * print it as 100%
474: */
475: printf((value >= 1000 ? "%4.0f" :
476: "%4.1f"), ((double) value) / 10.);
477:
478: /* remember it for next time */
479: *lp = *states;
480: }
1.14 deraadt 481: }
1.20 millert 482: /* increment and move on */
483: lp++;
484: states++;
485: colp++;
1.14 deraadt 486: }
487: }
1.1 downsj 488: }
489:
1.12 pvalchev 490: void
491: z_cpustates(void)
1.1 downsj 492: {
1.20 millert 493: char **names, *thisname;
494: int cpu, i;
495: int64_t *lp;
496:
497: for (cpu = 0; cpu < ncpu; cpu++) {
498: /* show tag and bump lastline */
499: printf("\n%s", cpustates_tag(cpu));
500: lastline++;
501:
502: names = cpustate_names;
503: i = 0;
504: while ((thisname = *names++) != NULL) {
505: if (*thisname != '\0')
506: printf("%s %% %s", i++ == 0 ? "" : ", ",
507: thisname);
508: }
1.14 deraadt 509:
1.20 millert 510: /* fill the "last" array with all -1s, to ensure correct updating */
511: lp = lcpustates[cpu];
512: i = num_cpustates;
513: while (--i >= 0)
514: *lp++ = -1;
515: }
1.1 downsj 516: }
517:
1.14 deraadt 518: static char memory_buffer[MAX_COLS];
519:
1.1 downsj 520: /*
521: * *_memory(stats) - print "Memory: " followed by the memory summary string
522: *
523: * Assumptions: cursor is on "lastline"
524: * for i_memory ONLY: cursor is on the previous line
525: */
1.12 pvalchev 526: void
527: i_memory(int *stats)
1.1 downsj 528: {
1.14 deraadt 529: if (fputs("\nMemory: ", stdout) == EOF)
530: exit(1);
531: lastline++;
532:
533: /* format and print the memory summary */
1.16 millert 534: summary_format(memory_buffer, sizeof(memory_buffer), stats,
535: memory_names);
1.14 deraadt 536: if (fputs(memory_buffer, stdout) == EOF)
537: exit(1);
1.1 downsj 538: }
539:
1.12 pvalchev 540: void
541: u_memory(int *stats)
1.1 downsj 542: {
1.14 deraadt 543: static char new[MAX_COLS];
1.1 downsj 544:
1.14 deraadt 545: /* format the new line */
1.16 millert 546: summary_format(new, sizeof(new), stats, memory_names);
1.14 deraadt 547: line_update(memory_buffer, new, x_mem, y_mem);
1.1 downsj 548: }
549:
550: /*
551: * *_message() - print the next pending message line, or erase the one
552: * that is there.
553: *
554: * Note that u_message is (currently) the same as i_message.
555: *
556: * Assumptions: lastline is consistent
557: */
558:
559: /*
560: * i_message is funny because it gets its message asynchronously (with
561: * respect to screen updates).
562: */
563:
1.14 deraadt 564: static char next_msg[MAX_COLS + 5];
565: static int msglen = 0;
566: /*
567: * Invariant: msglen is always the length of the message currently displayed
568: * on the screen (even when next_msg doesn't contain that message).
569: */
1.1 downsj 570:
1.12 pvalchev 571: void
572: i_message(void)
1.1 downsj 573: {
1.14 deraadt 574: while (lastline < y_message) {
575: if (fputc('\n', stdout) == EOF)
576: exit(1);
577: lastline++;
578: }
579: if (next_msg[0] != '\0') {
580: standout(next_msg);
581: msglen = strlen(next_msg);
582: next_msg[0] = '\0';
583: } else if (msglen > 0) {
584: (void) clear_eol(msglen);
585: msglen = 0;
586: }
1.1 downsj 587: }
588:
1.12 pvalchev 589: void
590: u_message(void)
1.1 downsj 591: {
1.14 deraadt 592: i_message();
1.1 downsj 593: }
594:
1.14 deraadt 595: static int header_length;
1.1 downsj 596:
597: /*
598: * *_header(text) - print the header for the process area
599: *
600: * Assumptions: cursor is on the previous line and lastline is consistent
601: */
602:
1.12 pvalchev 603: void
604: i_header(char *text)
1.1 downsj 605: {
1.14 deraadt 606: header_length = strlen(text);
607: if (header_status == ON) {
608: if (putchar('\n') == EOF)
609: exit(1);
610: if (fputs(text, stdout) == EOF)
611: exit(1);
612: lastline++;
613: } else if (header_status == ERASE) {
614: header_status = OFF;
615: }
1.1 downsj 616: }
617:
1.14 deraadt 618: /* ARGSUSED */
1.12 pvalchev 619: void
620: u_header(char *text)
1.1 downsj 621: {
1.14 deraadt 622: if (header_status == ERASE) {
623: if (putchar('\n') == EOF)
624: exit(1);
625: lastline++;
626: clear_eol(header_length);
627: header_status = OFF;
628: }
1.1 downsj 629: }
630:
631: /*
632: * *_process(line, thisline) - print one process line
633: *
634: * Assumptions: lastline is consistent
635: */
636:
1.12 pvalchev 637: void
638: i_process(int line, char *thisline)
1.1 downsj 639: {
1.16 millert 640: char *base;
641: size_t len;
1.1 downsj 642:
1.14 deraadt 643: /* make sure we are on the correct line */
644: while (lastline < y_procs + line) {
645: if (putchar('\n') == EOF)
646: exit(1);
647: lastline++;
648: }
1.1 downsj 649:
1.14 deraadt 650: /* truncate the line to conform to our current screen width */
651: thisline[display_width] = '\0';
1.1 downsj 652:
1.14 deraadt 653: /* write the line out */
654: if (fputs(thisline, stdout) == EOF)
655: exit(1);
1.1 downsj 656:
1.14 deraadt 657: /* copy it in to our buffer */
658: base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
1.16 millert 659: len = strlcpy(base, thisline, display_width);
660: if (len < (size_t)display_width) {
661: /* zero fill the rest of it */
662: memset(base + len, 0, display_width - len);
663: }
1.1 downsj 664: }
665:
1.12 pvalchev 666: void
667: u_process(int linenum, char *linebuf)
1.1 downsj 668: {
1.14 deraadt 669: int screen_line = linenum + Header_lines;
1.16 millert 670: char *bufferline;
671: size_t len;
1.14 deraadt 672:
673: /* remember a pointer to the current line in the screen buffer */
674: bufferline = &screenbuf[lineindex(linenum)];
675:
676: /* truncate the line to conform to our current screen width */
677: linebuf[display_width] = '\0';
1.22 ! jaredy 678: bufferline[display_width] = '\0';
1.14 deraadt 679:
680: /* is line higher than we went on the last display? */
681: if (linenum >= last_hi) {
682: /* yes, just ignore screenbuf and write it out directly */
683: /* get positioned on the correct line */
684: if (screen_line - lastline == 1) {
685: if (putchar('\n') == EOF)
686: exit(1);
687: lastline++;
688: } else {
689: Move_to(0, screen_line);
690: lastline = screen_line;
691: }
1.1 downsj 692:
1.14 deraadt 693: /* now write the line */
694: if (fputs(linebuf, stdout) == EOF)
695: exit(1);
1.1 downsj 696:
1.14 deraadt 697: /* copy it in to the buffer */
1.16 millert 698: len = strlcpy(bufferline, linebuf, display_width);
699: if (len < (size_t)display_width) {
700: /* zero fill the rest of it */
701: memset(bufferline + len, 0, display_width - len);
702: }
1.14 deraadt 703: } else {
704: line_update(bufferline, linebuf, 0, linenum + Header_lines);
705: }
1.1 downsj 706: }
707:
1.12 pvalchev 708: void
709: u_endscreen(int hi)
1.1 downsj 710: {
1.14 deraadt 711: int screen_line = hi + Header_lines, i;
1.1 downsj 712:
1.14 deraadt 713: if (smart_terminal) {
714: if (hi < last_hi) {
715: /* need to blank the remainder of the screen */
716: /*
717: * but only if there is any screen left below this
718: * line
719: */
720: if (lastline + 1 < screen_length) {
721: /*
722: * efficiently move to the end of currently
723: * displayed info
724: */
725: if (screen_line - lastline < 5) {
726: while (lastline < screen_line) {
727: if (putchar('\n') == EOF)
728: exit(1);
729: lastline++;
730: }
731: } else {
732: Move_to(0, screen_line);
733: lastline = screen_line;
734: }
735:
736: if (clear_to_end) {
737: /* we can do this the easy way */
738: putcap(clear_to_end);
739: } else {
740: /* use clear_eol on each line */
741: i = hi;
742: while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi) {
743: if (putchar('\n') == EOF)
744: exit(1);
745: }
746: }
747: }
1.1 downsj 748: }
1.14 deraadt 749: last_hi = hi;
1.1 downsj 750:
1.14 deraadt 751: /* move the cursor to a pleasant place */
752: Move_to(x_idlecursor, y_idlecursor);
753: lastline = y_idlecursor;
754: } else {
755: /*
756: * separate this display from the next with some vertical
757: * room
758: */
759: if (fputs("\n\n", stdout) == EOF)
760: exit(1);
1.1 downsj 761: }
762: }
763:
1.12 pvalchev 764: void
765: display_header(int t)
1.1 downsj 766: {
1.14 deraadt 767: if (t) {
768: header_status = ON;
769: } else if (header_status == ON) {
770: header_status = ERASE;
771: }
1.1 downsj 772: }
773:
1.12 pvalchev 774: void
1.14 deraadt 775: new_message(int type, const char *msgfmt,...)
776: {
777: va_list ap;
778: int i;
779:
780: va_start(ap, msgfmt);
781: /* first, format the message */
782: vsnprintf(next_msg, sizeof(next_msg), msgfmt, ap);
783: va_end(ap);
784:
785: if (msglen > 0) {
786: /* message there already -- can we clear it? */
787: if (!overstrike) {
788: /* yes -- write it and clear to end */
789: i = strlen(next_msg);
790: if ((type & MT_delayed) == 0) {
791: if (type & MT_standout)
792: standout(next_msg);
793: else {
794: if (fputs(next_msg, stdout) == EOF)
795: exit(1);
796: }
797: (void) clear_eol(msglen - i);
798: msglen = i;
799: next_msg[0] = '\0';
800: }
801: }
802: } else {
803: if ((type & MT_delayed) == 0) {
804: if (type & MT_standout)
805: standout(next_msg);
806: else {
807: if (fputs(next_msg, stdout) == EOF)
808: exit(1);
809: }
810: msglen = strlen(next_msg);
811: next_msg[0] = '\0';
1.5 deraadt 812: }
1.1 downsj 813: }
814: }
815:
1.12 pvalchev 816: void
817: clear_message(void)
1.1 downsj 818: {
1.14 deraadt 819: if (clear_eol(msglen) == 1) {
820: if (putchar('\r') == EOF)
821: exit(1);
822: }
1.1 downsj 823: }
824:
1.12 pvalchev 825: int
826: readline(char *buffer, int size, int numeric)
1.1 downsj 827: {
1.14 deraadt 828: char *ptr = buffer, ch, cnt = 0, maxcnt = 0;
1.17 deraadt 829: extern volatile sig_atomic_t leaveflag;
830: ssize_t len;
1.14 deraadt 831:
832: /* allow room for null terminator */
833: size -= 1;
834:
835: /* read loop */
1.19 deraadt 836: while ((fflush(stdout), (len = read(STDIN_FILENO, ptr, 1)) > 0)) {
1.17 deraadt 837:
838: if (len == 0 || leaveflag) {
839: end_screen();
840: exit(0);
841: }
842:
1.14 deraadt 843: /* newline means we are done */
844: if ((ch = *ptr) == '\n')
845: break;
846:
847: /* handle special editing characters */
848: if (ch == ch_kill) {
849: /* kill line -- account for overstriking */
850: if (overstrike)
851: msglen += maxcnt;
852:
853: /* return null string */
854: *buffer = '\0';
855: if (putchar('\r') == EOF)
856: exit(1);
857: return (-1);
858: } else if (ch == ch_erase) {
859: /* erase previous character */
860: if (cnt <= 0) {
861: /* none to erase! */
862: if (putchar('\7') == EOF)
863: exit(1);
864: } else {
865: if (fputs("\b \b", stdout) == EOF)
866: exit(1);
867: ptr--;
868: cnt--;
869: }
870: }
871: /* check for character validity and buffer overflow */
872: else if (cnt == size || (numeric && !isdigit(ch)) ||
873: !isprint(ch)) {
874: /* not legal */
875: if (putchar('\7') == EOF)
876: exit(1);
877: } else {
878: /* echo it and store it in the buffer */
879: if (putchar(ch) == EOF)
880: exit(1);
881: ptr++;
882: cnt++;
883: if (cnt > maxcnt)
884: maxcnt = cnt;
885: }
1.1 downsj 886: }
887:
1.14 deraadt 888: /* all done -- null terminate the string */
889: *ptr = '\0';
890:
891: /* account for the extra characters in the message area */
892: /* (if terminal overstrikes, remember the furthest they went) */
893: msglen += overstrike ? maxcnt : cnt;
894:
895: /* return either inputted number or string length */
896: if (putchar('\r') == EOF)
1.5 deraadt 897: exit(1);
1.14 deraadt 898: return (cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1.1 downsj 899: }
900:
901: /* internal support routines */
1.12 pvalchev 902: static int
903: string_count(char **pp)
1.1 downsj 904: {
1.14 deraadt 905: int cnt;
1.1 downsj 906:
1.14 deraadt 907: cnt = 0;
908: while (*pp++ != NULL)
909: cnt++;
910: return (cnt);
1.1 downsj 911: }
912:
1.16 millert 913: #define COPYLEFT(to, from) \
914: do { \
915: len = strlcpy((to), (from), left); \
916: if (len >= left) \
917: return; \
918: p += len; \
919: left -= len; \
920: } while (0)
921:
1.12 pvalchev 922: static void
1.16 millert 923: summary_format(char *buf, size_t left, int *numbers, char **names)
1.1 downsj 924: {
1.14 deraadt 925: char *p, *thisname;
1.16 millert 926: size_t len;
1.14 deraadt 927: int num;
1.1 downsj 928:
1.14 deraadt 929: /* format each number followed by its string */
1.16 millert 930: p = buf;
1.14 deraadt 931: while ((thisname = *names++) != NULL) {
932: /* get the number to format */
933: num = *numbers++;
934:
935: if (num >= 0) {
936: /* is this number in kilobytes? */
937: if (thisname[0] == 'K') {
938: /* yes: format it as a memory value */
1.16 millert 939: COPYLEFT(p, format_k(num));
1.14 deraadt 940:
941: /*
942: * skip over the K, since it was included by
943: * format_k
944: */
1.16 millert 945: COPYLEFT(p, thisname + 1);
1.14 deraadt 946: } else if (num > 0) {
1.16 millert 947: len = snprintf(p, left, "%d%s", num, thisname);
948: if (len == (size_t)-1 || len >= left)
949: return;
950: p += len;
951: left -= len;
1.14 deraadt 952: }
1.16 millert 953: } else {
954: /*
955: * Ignore negative numbers, but display corresponding
956: * string.
957: */
958: COPYLEFT(p, thisname);
1.14 deraadt 959: }
1.1 downsj 960: }
961:
1.14 deraadt 962: /* if the last two characters in the string are ", ", delete them */
963: p -= 2;
1.16 millert 964: if (p >= buf && p[0] == ',' && p[1] == ' ')
1.14 deraadt 965: *p = '\0';
1.1 downsj 966: }
967:
1.12 pvalchev 968: static void
969: line_update(char *old, char *new, int start, int line)
1.1 downsj 970: {
1.14 deraadt 971: int ch, diff, newcol = start + 1, lastcol = start;
972: char cursor_on_line = No, *current;
1.1 downsj 973:
1.14 deraadt 974: /* compare the two strings and only rewrite what has changed */
975: current = old;
1.1 downsj 976: #ifdef DEBUG
1.14 deraadt 977: fprintf(debug, "line_update, starting at %d\n", start);
978: fputs(old, debug);
979: fputc('\n', debug);
980: fputs(new, debug);
981: fputs("\n-\n", debug);
1.1 downsj 982: #endif
983:
1.14 deraadt 984: /* start things off on the right foot */
985: /* this is to make sure the invariants get set up right */
986: if ((ch = *new++) != *old) {
987: if (line - lastline == 1 && start == 0) {
988: if (putchar('\n') == EOF)
989: exit(1);
990: } else
991: Move_to(start, line);
992:
993: cursor_on_line = Yes;
994: if (putchar(ch) == EOF)
995: exit(1);
996: *old = ch;
997: lastcol = 1;
1.1 downsj 998: }
999: old++;
1000:
1.14 deraadt 1001: /*
1002: * main loop -- check each character. If the old and new aren't the
1003: * same, then update the display. When the distance from the
1004: * current cursor position to the new change is small enough,
1005: * the characters that belong there are written to move the
1006: * cursor over.
1007: *
1008: * Invariants:
1009: * lastcol is the column where the cursor currently is sitting
1010: * (always one beyond the end of the last mismatch).
1011: */
1012: do {
1013: if ((ch = *new++) != *old) {
1014: /* new character is different from old */
1015: /* make sure the cursor is on top of this character */
1016: diff = newcol - lastcol;
1017: if (diff > 0) {
1018: /*
1019: * some motion is required--figure out which
1020: * is shorter
1021: */
1022: if (diff < 6 && cursor_on_line) {
1023: /*
1024: * overwrite old stuff--get it out of
1025: * the old buffer
1026: */
1027: printf("%.*s", diff, ¤t[lastcol - start]);
1028: } else {
1029: /* use cursor addressing */
1030: Move_to(newcol, line);
1031: cursor_on_line = Yes;
1032: }
1033: /* remember where the cursor is */
1034: lastcol = newcol + 1;
1035: } else {
1036: /* already there, update position */
1037: lastcol++;
1038: }
1039:
1040: /* write what we need to */
1041: if (ch == '\0') {
1042: /*
1043: * at the end--terminate with a
1044: * clear-to-end-of-line
1045: */
1046: (void) clear_eol(strlen(old));
1047: } else {
1048: /* write the new character */
1049: if (putchar(ch) == EOF)
1050: exit(1);
1051: }
1052: /* put the new character in the screen buffer */
1053: *old = ch;
1054: }
1055: /* update working column and screen buffer pointer */
1056: newcol++;
1057: old++;
1058: } while (ch != '\0');
1059:
1060: /* zero out the rest of the line buffer -- MUST BE DONE! */
1061: diff = display_width - newcol;
1062: if (diff > 0)
1063: memset(old, 0, diff);
1064:
1065: /* remember where the current line is */
1066: if (cursor_on_line)
1067: lastline = line;
1.1 downsj 1068: }
1069:
1070: /*
1071: * printable(str) - make the string pointed to by "str" into one that is
1072: * printable (i.e.: all ascii), by converting all non-printable
1073: * characters into '?'. Replacements are done in place and a pointer
1074: * to the original buffer is returned.
1075: */
1.12 pvalchev 1076: char *
1077: printable(char *str)
1.1 downsj 1078: {
1.14 deraadt 1079: char *ptr, ch;
1.1 downsj 1080:
1.14 deraadt 1081: ptr = str;
1082: while ((ch = *ptr) != '\0') {
1083: if (!isprint(ch))
1084: *ptr = '?';
1085: ptr++;
1.1 downsj 1086: }
1.14 deraadt 1087: return (str);
1.1 downsj 1088: }