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