]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/top/display.c
top(1): avoid casting malloc
[FreeBSD/FreeBSD.git] / usr.bin / top / display.c
1 /*
2  *  Top users/processes display for Unix
3  *  Version 3
4  *
5  *  This program may be freely redistributed,
6  *  but this entire comment MUST remain intact.
7  *
8  *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
9  *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
10  *
11  * $FreeBSD$
12  */
13
14 /*
15  *  This file contains the routines that display information on the screen.
16  *  Each section of the screen has two routines:  one for initially writing
17  *  all constant and dynamic text, and one for only updating the text that
18  *  changes.  The prefix "i_" is used on all the "initial" routines and the
19  *  prefix "u_" is used for all the "updating" routines.
20  *
21  *  ASSUMPTIONS:
22  *        None of the "i_" routines use any of the termcap capabilities.
23  *        In this way, those routines can be safely used on terminals that
24  *        have minimal (or nonexistant) terminal capabilities.
25  *
26  *        The routines are called in this order:  *_loadave, i_timeofday,
27  *        *_procstates, *_cpustates, *_memory, *_message, *_header,
28  *        *_process, u_endscreen.
29  */
30
31 #include <sys/time.h>
32
33 #include <curses.h>
34 #include <ctype.h>
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <termcap.h>
41 #include <time.h>
42 #include <unistd.h>
43
44 #include "screen.h"             /* interface to screen package */
45 #include "layout.h"             /* defines for screen position layout */
46 #include "display.h"
47 #include "top.h"
48 #include "machine.h"            /* we should eliminate this!!! */
49 #include "utils.h"
50
51 #ifdef DEBUG
52 FILE *debug;
53 #endif
54
55 static int lmpid = 0;
56 static int last_hi = 0;         /* used in u_process and u_endscreen */
57 static int lastline = 0;
58 static int display_width = MAX_COLS;
59
60 #define lineindex(l) ((l)*display_width)
61
62
63 /* things initialized by display_init and used thruout */
64
65 /* buffer of proc information lines for display updating */
66 static char *screenbuf = NULL;
67
68 static char **procstate_names;
69 static char **cpustate_names;
70 static char **memory_names;
71 static char **arc_names;
72 static char **carc_names;
73 static char **swap_names;
74
75 static int num_procstates;
76 static int num_cpustates;
77 static int num_memory;
78 static int num_swap;
79
80 static int *lprocstates;
81 static int *lcpustates;
82 static int *lmemory;
83 static int *lswap;
84
85 static int num_cpus;
86 static int *cpustate_columns;
87 static int cpustate_total_length;
88 static int cpustates_column;
89
90 static enum { OFF, ON, ERASE } header_status = ON;
91
92 static int string_count(char **);
93 static void summary_format(char *, int *, char **);
94 static void line_update(char *, char *, int, int);
95
96 int  x_lastpid =        10;
97 int  y_lastpid =        0;
98 int  x_loadave =        33;
99 int  x_loadave_nompid = 15;
100 int  y_loadave =        0;
101 int  x_procstate =      0;
102 int  y_procstate =      1;
103 int  x_brkdn =          15;
104 int  y_brkdn =          1;
105 int  x_mem =            5;
106 int  y_mem =            3;
107 int  x_arc =            5;
108 int  y_arc =            4;
109 int  x_carc =           5;
110 int  y_carc =           5;
111 int  x_swap =           6;
112 int  y_swap =           4;
113 int  y_message =        5;
114 int  x_header =         0;
115 int  y_header =         6;
116 int  x_idlecursor =     0;
117 int  y_idlecursor =     5;
118 int  y_procs =          7;
119
120 int  y_cpustates =      2;
121 int  Header_lines =     7;
122
123 int
124 display_resize(void)
125 {
126     int lines;
127
128     /* first, deallocate any previous buffer that may have been there */
129     if (screenbuf != NULL)
130     {
131         free(screenbuf);
132     }
133
134     /* calculate the current dimensions */
135     /* if operating in "dumb" mode, we only need one line */
136     lines = smart_terminal ? screen_length - Header_lines : 1;
137
138     if (lines < 0)
139         lines = 0;
140     /* we don't want more than MAX_COLS columns, since the machine-dependent
141        modules make static allocations based on MAX_COLS and we don't want
142        to run off the end of their buffers */
143     display_width = screen_width;
144     if (display_width >= MAX_COLS)
145     {
146         display_width = MAX_COLS - 1;
147     }
148
149     /* now, allocate space for the screen buffer */
150     screenbuf = malloc(lines * display_width);
151     if (screenbuf == (char *)NULL)
152     {
153         /* oops! */
154         return(-1);
155     }
156
157     /* return number of lines available */
158     /* for dumb terminals, pretend like we can show any amount */
159     return(smart_terminal ? lines : Largest);
160 }
161
162 int display_updatecpus(struct statics *statics)
163 {
164     int *lp;
165     int lines;
166     int i;
167     
168     /* call resize to do the dirty work */
169     lines = display_resize();
170     if (pcpu_stats)
171         num_cpus = statics->ncpus;
172     else
173         num_cpus = 1;
174     cpustates_column = 5;       /* CPU: */
175     if (num_cpus != 1)
176     cpustates_column += 2;      /* CPU 0: */
177     for (i = num_cpus; i > 9; i /= 10)
178         cpustates_column++;
179
180     /* fill the "last" array with all -1s, to insure correct updating */
181     lp = lcpustates;
182     i = num_cpustates * num_cpus;
183     while (--i >= 0)
184     {
185         *lp++ = -1;
186     }
187     
188     return(lines);
189 }
190     
191 int display_init(struct statics * statics)
192 {
193     int lines;
194     char **pp;
195     int *ip;
196     int i;
197
198     lines = display_updatecpus(statics);
199
200     /* only do the rest if we need to */
201     if (lines > -1)
202     {
203         /* save pointers and allocate space for names */
204         procstate_names = statics->procstate_names;
205         num_procstates = string_count(procstate_names);
206         lprocstates = malloc(num_procstates * sizeof(int));
207
208         cpustate_names = statics->cpustate_names;
209
210         swap_names = statics->swap_names;
211         num_swap = string_count(swap_names);
212         lswap = malloc(num_swap * sizeof(int));
213         num_cpustates = string_count(cpustate_names);
214         lcpustates = malloc(num_cpustates * sizeof(int) * statics->ncpus);
215         cpustate_columns = malloc(num_cpustates * sizeof(int));
216
217         memory_names = statics->memory_names;
218         num_memory = string_count(memory_names);
219         lmemory = malloc(num_memory * sizeof(int));
220
221         arc_names = statics->arc_names;
222         carc_names = statics->carc_names;
223         
224         /* calculate starting columns where needed */
225         cpustate_total_length = 0;
226         pp = cpustate_names;
227         ip = cpustate_columns;
228         while (*pp != NULL)
229         {
230             *ip++ = cpustate_total_length;
231             if ((i = strlen(*pp++)) > 0)
232             {
233                 cpustate_total_length += i + 8;
234             }
235         }
236     }
237
238     /* return number of lines available */
239     return(lines);
240 }
241
242 void
243 i_loadave(int mpid, double avenrun[])
244 {
245     int i;
246
247     /* i_loadave also clears the screen, since it is first */
248     top_clear();
249
250     /* mpid == -1 implies this system doesn't have an _mpid */
251     if (mpid != -1)
252     {
253         printf("last pid: %5d;  ", mpid);
254     }
255
256     printf("load averages");
257
258     for (i = 0; i < 3; i++)
259     {
260         printf("%c %5.2f",
261             i == 0 ? ':' : ',',
262             avenrun[i]);
263     }
264     lmpid = mpid;
265 }
266
267 void
268 u_loadave(int mpid, double *avenrun)
269 {
270     int i;
271
272     if (mpid != -1)
273     {
274         /* change screen only when value has really changed */
275         if (mpid != lmpid)
276         {
277             Move_to(x_lastpid, y_lastpid);
278             printf("%5d", mpid);
279             lmpid = mpid;
280         }
281
282         /* i remembers x coordinate to move to */
283         i = x_loadave;
284     }
285     else
286     {
287         i = x_loadave_nompid;
288     }
289
290     /* move into position for load averages */
291     Move_to(i, y_loadave);
292
293     /* display new load averages */
294     /* we should optimize this and only display changes */
295     for (i = 0; i < 3; i++)
296     {
297         printf("%s%5.2f",
298             i == 0 ? "" : ", ",
299             avenrun[i]);
300     }
301 }
302
303 void
304 i_timeofday(time_t *tod)
305 {
306     /*
307      *  Display the current time.
308      *  "ctime" always returns a string that looks like this:
309      *  
310      *  Sun Sep 16 01:03:52 1973
311      *      012345678901234567890123
312      *            1         2
313      *
314      *  We want indices 11 thru 18 (length 8).
315      */
316
317     if (smart_terminal)
318     {
319         Move_to(screen_width - 8, 0);
320     }
321     else
322     {
323         fputs("    ", stdout);
324     }
325 #ifdef DEBUG
326     {
327         char *foo;
328         foo = ctime(tod);
329         fputs(foo, stdout);
330     }
331 #endif
332     printf("%-8.8s\n", &(ctime(tod)[11]));
333     lastline = 1;
334 }
335
336 static int ltotal = 0;
337 static char procstates_buffer[MAX_COLS];
338
339 /*
340  *  *_procstates(total, brkdn, names) - print the process summary line
341  *
342  *  Assumptions:  cursor is at the beginning of the line on entry
343  *                lastline is valid
344  */
345
346 void
347 i_procstates(int total, int *brkdn)
348 {
349     int i;
350
351     /* write current number of processes and remember the value */
352     printf("%d processes:", total);
353     ltotal = total;
354
355     /* put out enough spaces to get to column 15 */
356     i = digits(total);
357     while (i++ < 4)
358     {
359         putchar(' ');
360     }
361
362     /* format and print the process state summary */
363     summary_format(procstates_buffer, brkdn, procstate_names);
364     fputs(procstates_buffer, stdout);
365
366     /* save the numbers for next time */
367     memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
368 }
369
370 void
371 u_procstates(int total, int *brkdn)
372 {
373     static char new[MAX_COLS];
374     int i;
375
376     /* update number of processes only if it has changed */
377     if (ltotal != total)
378     {
379         /* move and overwrite */
380 #if (x_procstate == 0)
381         Move_to(x_procstate, y_procstate);
382 #else
383         /* cursor is already there...no motion needed */
384         /* assert(lastline == 1); */
385 #endif
386         printf("%d", total);
387
388         /* if number of digits differs, rewrite the label */
389         if (digits(total) != digits(ltotal))
390         {
391             fputs(" processes:", stdout);
392             /* put out enough spaces to get to column 15 */
393             i = digits(total);
394             while (i++ < 4)
395             {
396                 putchar(' ');
397             }
398             /* cursor may end up right where we want it!!! */
399         }
400
401         /* save new total */
402         ltotal = total;
403     }
404
405     /* see if any of the state numbers has changed */
406     if (memcmp(lprocstates, brkdn, num_procstates * sizeof(int)) != 0)
407     {
408         /* format and update the line */
409         summary_format(new, brkdn, procstate_names);
410         line_update(procstates_buffer, new, x_brkdn, y_brkdn);
411         memcpy(lprocstates, brkdn, num_procstates * sizeof(int));
412     }
413 }
414
415 void
416 i_cpustates(int *states)
417 {
418     int i = 0;
419     int value;
420     char **names;
421     char *thisname;
422     int cpu;
423
424 for (cpu = 0; cpu < num_cpus; cpu++) {
425     names = cpustate_names;
426
427     /* print tag and bump lastline */
428     if (num_cpus == 1)
429         printf("\nCPU: ");
430     else {
431         value = printf("\nCPU %d: ", cpu);
432         while (value++ <= cpustates_column)
433                 printf(" ");
434     }
435     lastline++;
436
437     /* now walk thru the names and print the line */
438     while ((thisname = *names++) != NULL)
439     {
440         if (*thisname != '\0')
441         {
442             /* retrieve the value and remember it */
443             value = *states++;
444
445             /* if percentage is >= 1000, print it as 100% */
446             printf((value >= 1000 ? "%s%4.0f%% %s" : "%s%4.1f%% %s"),
447                    (i++ % num_cpustates) == 0 ? "" : ", ",
448                    ((float)value)/10.,
449                    thisname);
450         }
451     }
452 }
453
454     /* copy over values into "last" array */
455     memcpy(lcpustates, states, num_cpustates * sizeof(int) * num_cpus);
456 }
457
458 void
459 u_cpustates(int *states)
460 {
461     int value;
462     char **names;
463     char *thisname;
464     int *lp;
465     int *colp;
466     int cpu;
467
468 for (cpu = 0; cpu < num_cpus; cpu++) {
469     names = cpustate_names;
470
471     Move_to(cpustates_column, y_cpustates + cpu);
472     lastline = y_cpustates + cpu;
473     lp = lcpustates + (cpu * num_cpustates);
474     colp = cpustate_columns;
475
476     /* we could be much more optimal about this */
477     while ((thisname = *names++) != NULL)
478     {
479         if (*thisname != '\0')
480         {
481             /* did the value change since last time? */
482             if (*lp != *states)
483             {
484                 /* yes, move and change */
485                 Move_to(cpustates_column + *colp, y_cpustates + cpu);
486                 lastline = y_cpustates + cpu;
487
488                 /* retrieve value and remember it */
489                 value = *states;
490
491                 /* if percentage is >= 1000, print it as 100% */
492                 printf((value >= 1000 ? "%4.0f" : "%4.1f"),
493                        ((double)value)/10.);
494
495                 /* remember it for next time */
496                 *lp = value;
497             }
498         }
499
500         /* increment and move on */
501         lp++;
502         states++;
503         colp++;
504     }
505 }
506 }
507
508 void
509 z_cpustates(void)
510 {
511     int i = 0;
512     char **names;
513     char *thisname;
514     int *lp;
515     int cpu, value;
516
517 for (cpu = 0; cpu < num_cpus; cpu++) {
518     names = cpustate_names;
519
520     /* show tag and bump lastline */
521     if (num_cpus == 1)
522         printf("\nCPU: ");
523     else {
524         value = printf("\nCPU %d: ", cpu);
525         while (value++ <= cpustates_column)
526                 printf(" ");
527     }
528     lastline++;
529
530     while ((thisname = *names++) != NULL)
531     {
532         if (*thisname != '\0')
533         {
534             printf("%s    %% %s", (i++ % num_cpustates) == 0 ? "" : ", ", thisname);
535         }
536     }
537 }
538
539     /* fill the "last" array with all -1s, to insure correct updating */
540     lp = lcpustates;
541     i = num_cpustates * num_cpus;
542     while (--i >= 0)
543     {
544         *lp++ = -1;
545     }
546 }
547
548 /*
549  *  *_memory(stats) - print "Memory: " followed by the memory summary string
550  *
551  *  Assumptions:  cursor is on "lastline"
552  *                for i_memory ONLY: cursor is on the previous line
553  */
554
555 static char memory_buffer[MAX_COLS];
556
557 void
558 i_memory(int *stats)
559 {
560     fputs("\nMem: ", stdout);
561     lastline++;
562
563     /* format and print the memory summary */
564     summary_format(memory_buffer, stats, memory_names);
565     fputs(memory_buffer, stdout);
566 }
567
568 void
569 u_memory(int *stats)
570 {
571     static char new[MAX_COLS];
572
573     /* format the new line */
574     summary_format(new, stats, memory_names);
575     line_update(memory_buffer, new, x_mem, y_mem);
576 }
577
578 /*
579  *  *_arc(stats) - print "ARC: " followed by the ARC summary string
580  *
581  *  Assumptions:  cursor is on "lastline"
582  *                for i_arc ONLY: cursor is on the previous line
583  */
584 static char arc_buffer[MAX_COLS];
585
586 void
587 i_arc(int *stats)
588 {
589     if (arc_names == NULL)
590         return;
591
592     fputs("\nARC: ", stdout);
593     lastline++;
594
595     /* format and print the memory summary */
596     summary_format(arc_buffer, stats, arc_names);
597     fputs(arc_buffer, stdout);
598 }
599
600 void
601 u_arc(int *stats)
602 {
603     static char new[MAX_COLS];
604
605     if (arc_names == NULL)
606         return;
607
608     /* format the new line */
609     summary_format(new, stats, arc_names);
610     line_update(arc_buffer, new, x_arc, y_arc);
611 }
612
613
614 /*
615  *  *_carc(stats) - print "Compressed ARC: " followed by the summary string
616  *
617  *  Assumptions:  cursor is on "lastline"
618  *                for i_carc ONLY: cursor is on the previous line
619  */
620 static char carc_buffer[MAX_COLS];
621
622 void
623 i_carc(int *stats)
624 {
625     if (carc_names == NULL)
626         return;
627
628     fputs("\n     ", stdout);
629     lastline++;
630
631     /* format and print the memory summary */
632     summary_format(carc_buffer, stats, carc_names);
633     fputs(carc_buffer, stdout);
634 }
635
636 void
637 u_carc(int *stats)
638 {
639     static char new[MAX_COLS];
640
641     if (carc_names == NULL)
642         return;
643
644     /* format the new line */
645     summary_format(new, stats, carc_names);
646     line_update(carc_buffer, new, x_carc, y_carc);
647 }
648  
649 /*
650  *  *_swap(stats) - print "Swap: " followed by the swap summary string
651  *
652  *  Assumptions:  cursor is on "lastline"
653  *                for i_swap ONLY: cursor is on the previous line
654  */
655
656 static char swap_buffer[MAX_COLS];
657
658 void
659 i_swap(int *stats)
660 {
661     fputs("\nSwap: ", stdout);
662     lastline++;
663
664     /* format and print the swap summary */
665     summary_format(swap_buffer, stats, swap_names);
666     fputs(swap_buffer, stdout);
667 }
668
669 void
670 u_swap(int *stats)
671 {
672     static char new[MAX_COLS];
673
674     /* format the new line */
675     summary_format(new, stats, swap_names);
676     line_update(swap_buffer, new, x_swap, y_swap);
677 }
678
679 /*
680  *  *_message() - print the next pending message line, or erase the one
681  *                that is there.
682  *
683  *  Note that u_message is (currently) the same as i_message.
684  *
685  *  Assumptions:  lastline is consistent
686  */
687
688 /*
689  *  i_message is funny because it gets its message asynchronously (with
690  *      respect to screen updates).
691  */
692
693 static char next_msg[MAX_COLS + 5];
694 static int msglen = 0;
695 /* Invariant: msglen is always the length of the message currently displayed
696    on the screen (even when next_msg doesn't contain that message). */
697
698 void
699 i_message(void)
700 {
701
702     while (lastline < y_message)
703     {
704         fputc('\n', stdout);
705         lastline++;
706     }
707     if (next_msg[0] != '\0')
708     {
709         top_standout(next_msg);
710         msglen = strlen(next_msg);
711         next_msg[0] = '\0';
712     }
713     else if (msglen > 0)
714     {
715         (void) clear_eol(msglen);
716         msglen = 0;
717     }
718 }
719
720 void
721 u_message(void)
722 {
723     i_message();
724 }
725
726 static int header_length;
727
728 /*
729  * Trim a header string to the current display width and return a newly
730  * allocated area with the trimmed header.
731  */
732
733 char *
734 trim_header(char *text)
735 {
736         char *s;
737         int width;
738
739         s = NULL;
740         width = display_width;
741         header_length = strlen(text);
742         if (header_length >= width) {
743                 s = malloc((width + 1) * sizeof(char));
744                 if (s == NULL)
745                         return (NULL);
746                 strncpy(s, text, width);
747                 s[width] = '\0';
748         }
749         return (s);
750 }
751
752 /*
753  *  *_header(text) - print the header for the process area
754  *
755  *  Assumptions:  cursor is on the previous line and lastline is consistent
756  */
757
758 void
759 i_header(char *text)
760 {
761     char *s;
762
763     s = trim_header(text);
764     if (s != NULL)
765         text = s;
766
767     if (header_status == ON)
768     {
769         putchar('\n');
770         fputs(text, stdout);
771         lastline++;
772     }
773     else if (header_status == ERASE)
774     {
775         header_status = OFF;
776     }
777     free(s);
778 }
779
780 void
781 u_header(char *text __unused)
782 {
783
784     if (header_status == ERASE)
785     {
786         putchar('\n');
787         lastline++;
788         clear_eol(header_length);
789         header_status = OFF;
790     }
791 }
792
793 /*
794  *  *_process(line, thisline) - print one process line
795  *
796  *  Assumptions:  lastline is consistent
797  */
798
799 void
800 i_process(int line, char *thisline)
801 {
802     char *p;
803     char *base;
804
805     /* make sure we are on the correct line */
806     while (lastline < y_procs + line)
807     {
808         putchar('\n');
809         lastline++;
810     }
811
812     /* truncate the line to conform to our current screen width */
813     thisline[display_width] = '\0';
814
815     /* write the line out */
816     fputs(thisline, stdout);
817
818     /* copy it in to our buffer */
819     base = smart_terminal ? screenbuf + lineindex(line) : screenbuf;
820     p = strecpy(base, thisline);
821
822     /* zero fill the rest of it */
823     bzero(p, display_width - (p - base));
824 }
825
826 void
827 u_process(int line, char *newline)
828 {
829     char *optr;
830     int screen_line = line + Header_lines;
831     char *bufferline;
832
833     /* remember a pointer to the current line in the screen buffer */
834     bufferline = &screenbuf[lineindex(line)];
835
836     /* truncate the line to conform to our current screen width */
837     newline[display_width] = '\0';
838
839     /* is line higher than we went on the last display? */
840     if (line >= last_hi)
841     {
842         /* yes, just ignore screenbuf and write it out directly */
843         /* get positioned on the correct line */
844         if (screen_line - lastline == 1)
845         {
846             putchar('\n');
847             lastline++;
848         }
849         else
850         {
851             Move_to(0, screen_line);
852             lastline = screen_line;
853         }
854
855         /* now write the line */
856         fputs(newline, stdout);
857
858         /* copy it in to the buffer */
859         optr = strecpy(bufferline, newline);
860
861         /* zero fill the rest of it */
862         bzero(optr, display_width - (optr - bufferline));
863     }
864     else
865     {
866         line_update(bufferline, newline, 0, line + Header_lines);
867     }
868 }
869
870 void
871 u_endscreen(int hi)
872 {
873     int screen_line = hi + Header_lines;
874     int i;
875
876     if (smart_terminal)
877     {
878         if (hi < last_hi)
879         {
880             /* need to blank the remainder of the screen */
881             /* but only if there is any screen left below this line */
882             if (lastline + 1 < screen_length)
883             {
884                 /* efficiently move to the end of currently displayed info */
885                 if (screen_line - lastline < 5)
886                 {
887                     while (lastline < screen_line)
888                     {
889                         putchar('\n');
890                         lastline++;
891                     }
892                 }
893                 else
894                 {
895                     Move_to(0, screen_line);
896                     lastline = screen_line;
897                 }
898
899                 if (clear_to_end)
900                 {
901                     /* we can do this the easy way */
902                     putcap(clear_to_end);
903                 }
904                 else
905                 {
906                     /* use clear_eol on each line */
907                     i = hi;
908                     while ((void) clear_eol(strlen(&screenbuf[lineindex(i++)])), i < last_hi)
909                     {
910                         putchar('\n');
911                     }
912                 }
913             }
914         }
915         last_hi = hi;
916
917         /* move the cursor to a pleasant place */
918         Move_to(x_idlecursor, y_idlecursor);
919         lastline = y_idlecursor;
920     }
921     else
922     {
923         /* separate this display from the next with some vertical room */
924         fputs("\n\n", stdout);
925     }
926 }
927
928 void
929 display_header(int t)
930 {
931
932     if (t)
933     {
934         header_status = ON;
935     }
936     else if (header_status == ON)
937     {
938         header_status = ERASE;
939     }
940 }
941
942 void
943 new_message(int type, char *msgfmt, ...)
944 {
945     va_list args;
946     size_t i;
947
948     va_start(args, msgfmt);
949
950     /* first, format the message */
951     vsnprintf(next_msg, sizeof(next_msg), msgfmt, args);
952
953     va_end(args);
954
955     if (msglen > 0)
956     {
957         /* message there already -- can we clear it? */
958         if (!overstrike)
959         {
960             /* yes -- write it and clear to end */
961             i = strlen(next_msg);
962             if ((type & MT_delayed) == 0)
963             {
964                 type & MT_standout ? top_standout(next_msg) :
965                                      fputs(next_msg, stdout);
966                 (void) clear_eol(msglen - i);
967                 msglen = i;
968                 next_msg[0] = '\0';
969             }
970         }
971     }
972     else
973     {
974         if ((type & MT_delayed) == 0)
975         {
976             type & MT_standout ? top_standout(next_msg) : fputs(next_msg, stdout);
977             msglen = strlen(next_msg);
978             next_msg[0] = '\0';
979         }
980     }
981 }
982
983 void
984 clear_message(void)
985 {
986     if (clear_eol(msglen) == 1)
987     {
988         putchar('\r');
989     }
990 }
991
992 int
993 readline(char *buffer, int size, int numeric)
994 {
995     char *ptr = buffer;
996     char ch;
997     char cnt = 0;
998     char maxcnt = 0;
999
1000     /* allow room for null terminator */
1001     size -= 1;
1002
1003     /* read loop */
1004     while ((fflush(stdout), read(0, ptr, 1) > 0))
1005     {
1006         /* newline means we are done */
1007         if ((ch = *ptr) == '\n' || ch == '\r')
1008         {
1009             break;
1010         }
1011
1012         /* handle special editing characters */
1013         if (ch == ch_kill)
1014         {
1015             /* kill line -- account for overstriking */
1016             if (overstrike)
1017             {
1018                 msglen += maxcnt;
1019             }
1020
1021             /* return null string */
1022             *buffer = '\0';
1023             putchar('\r');
1024             return(-1);
1025         }
1026         else if (ch == ch_erase)
1027         {
1028             /* erase previous character */
1029             if (cnt <= 0)
1030             {
1031                 /* none to erase! */
1032                 putchar('\7');
1033             }
1034             else
1035             {
1036                 fputs("\b \b", stdout);
1037                 ptr--;
1038                 cnt--;
1039             }
1040         }
1041         /* check for character validity and buffer overflow */
1042         else if (cnt == size || (numeric && !isdigit(ch)) ||
1043                 !isprint(ch))
1044         {
1045             /* not legal */
1046             putchar('\7');
1047         }
1048         else
1049         {
1050             /* echo it and store it in the buffer */
1051             putchar(ch);
1052             ptr++;
1053             cnt++;
1054             if (cnt > maxcnt)
1055             {
1056                 maxcnt = cnt;
1057             }
1058         }
1059     }
1060
1061     /* all done -- null terminate the string */
1062     *ptr = '\0';
1063
1064     /* account for the extra characters in the message area */
1065     /* (if terminal overstrikes, remember the furthest they went) */
1066     msglen += overstrike ? maxcnt : cnt;
1067
1068     /* return either inputted number or string length */
1069     putchar('\r');
1070     return(cnt == 0 ? -1 : numeric ? atoi(buffer) : cnt);
1071 }
1072
1073 /* internal support routines */
1074
1075 static int string_count(char **pp)
1076 {
1077     int cnt;
1078
1079     cnt = 0;
1080     while (*pp++ != NULL)
1081     {
1082         cnt++;
1083     }
1084     return(cnt);
1085 }
1086
1087 static void summary_format(char *str, int *numbers, char **names)
1088 {
1089     char *p;
1090     int num;
1091     char *thisname;
1092     char rbuf[6];
1093
1094     /* format each number followed by its string */
1095     p = str;
1096     while ((thisname = *names++) != NULL)
1097     {
1098         /* get the number to format */
1099         num = *numbers++;
1100
1101         /* display only non-zero numbers */
1102         if (num > 0)
1103         {
1104             /* is this number in kilobytes? */
1105             if (thisname[0] == 'K')
1106             {
1107                 /* yes: format it as a memory value */
1108                 p = strecpy(p, format_k(num));
1109
1110                 /* skip over the K, since it was included by format_k */
1111                 p = strecpy(p, thisname+1);
1112             }
1113             /* is this number a ratio? */
1114             else if (thisname[0] == ':')
1115             {
1116                 (void) snprintf(rbuf, sizeof(rbuf), "%.2f", 
1117                     (float)*(numbers - 2) / (float)num);
1118                 p = strecpy(p, rbuf);
1119                 p = strecpy(p, thisname);
1120             }
1121             else
1122             {
1123                 p = strecpy(p, itoa(num));
1124                 p = strecpy(p, thisname);
1125             }
1126         }
1127
1128         /* ignore negative numbers, but display corresponding string */
1129         else if (num < 0)
1130         {
1131             p = strecpy(p, thisname);
1132         }
1133     }
1134
1135     /* if the last two characters in the string are ", ", delete them */
1136     p -= 2;
1137     if (p >= str && p[0] == ',' && p[1] == ' ')
1138     {
1139         *p = '\0';
1140     }
1141 }
1142
1143 static void
1144 line_update(char *old, char *new, int start, int line)
1145 {
1146     int ch;
1147     int diff;
1148     int newcol = start + 1;
1149     int lastcol = start;
1150     char cursor_on_line = false;
1151     char *current;
1152
1153     /* compare the two strings and only rewrite what has changed */
1154     current = old;
1155 #ifdef DEBUG
1156     fprintf(debug, "line_update, starting at %d\n", start);
1157     fputs(old, debug);
1158     fputc('\n', debug);
1159     fputs(new, debug);
1160     fputs("\n-\n", debug);
1161 #endif
1162
1163     /* start things off on the right foot                   */
1164     /* this is to make sure the invariants get set up right */
1165     if ((ch = *new++) != *old)
1166     {
1167         if (line - lastline == 1 && start == 0)
1168         {
1169             putchar('\n');
1170         }
1171         else
1172         {
1173             Move_to(start, line);
1174         }
1175         cursor_on_line = true;
1176         putchar(ch);
1177         *old = ch;
1178         lastcol = 1;
1179     }
1180     old++;
1181         
1182     /*
1183      *  main loop -- check each character.  If the old and new aren't the
1184      *  same, then update the display.  When the distance from the
1185      *  current cursor position to the new change is small enough,
1186      *  the characters that belong there are written to move the
1187      *  cursor over.
1188      *
1189      *  Invariants:
1190      *      lastcol is the column where the cursor currently is sitting
1191      *          (always one beyond the end of the last mismatch).
1192      */
1193     do          /* yes, a do...while */
1194     {
1195         if ((ch = *new++) != *old)
1196         {
1197             /* new character is different from old        */
1198             /* make sure the cursor is on top of this character */
1199             diff = newcol - lastcol;
1200             if (diff > 0)
1201             {
1202                 /* some motion is required--figure out which is shorter */
1203                 if (diff < 6 && cursor_on_line)
1204                 {
1205                     /* overwrite old stuff--get it out of the old buffer */
1206                     printf("%.*s", diff, &current[lastcol-start]);
1207                 }
1208                 else
1209                 {
1210                     /* use cursor addressing */
1211                     Move_to(newcol, line);
1212                     cursor_on_line = true;
1213                 }
1214                 /* remember where the cursor is */
1215                 lastcol = newcol + 1;
1216             }
1217             else
1218             {
1219                 /* already there, update position */
1220                 lastcol++;
1221             }
1222                 
1223             /* write what we need to */
1224             if (ch == '\0')
1225             {
1226                 /* at the end--terminate with a clear-to-end-of-line */
1227                 (void) clear_eol(strlen(old));
1228             }
1229             else
1230             {
1231                 /* write the new character */
1232                 putchar(ch);
1233             }
1234             /* put the new character in the screen buffer */
1235             *old = ch;
1236         }
1237             
1238         /* update working column and screen buffer pointer */
1239         newcol++;
1240         old++;
1241             
1242     } while (ch != '\0');
1243
1244     /* zero out the rest of the line buffer -- MUST BE DONE! */
1245     diff = display_width - newcol;
1246     if (diff > 0)
1247     {
1248         bzero(old, diff);
1249     }
1250
1251     /* remember where the current line is */
1252     if (cursor_on_line)
1253     {
1254         lastline = line;
1255     }
1256 }
1257
1258 /*
1259  *  printable(str) - make the string pointed to by "str" into one that is
1260  *      printable (i.e.: all ascii), by converting all non-printable
1261  *      characters into '?'.  Replacements are done in place and a pointer
1262  *      to the original buffer is returned.
1263  */
1264
1265 char *
1266 printable(char str[])
1267 {
1268     char *ptr;
1269     char ch;
1270
1271     ptr = str;
1272     while ((ch = *ptr) != '\0')
1273     {
1274         if (!isprint(ch))
1275         {
1276             *ptr = '?';
1277         }
1278         ptr++;
1279     }
1280     return(str);
1281 }
1282
1283 void
1284 i_uptime(struct timeval *bt, time_t *tod)
1285 {
1286     time_t uptime;
1287     int days, hrs, mins, secs;
1288
1289     if (bt->tv_sec != -1) {
1290         uptime = *tod - bt->tv_sec;
1291         days = uptime / 86400;
1292         uptime %= 86400;
1293         hrs = uptime / 3600;
1294         uptime %= 3600;
1295         mins = uptime / 60;
1296         secs = uptime % 60;
1297
1298         /*
1299          *  Display the uptime.
1300          */
1301
1302         if (smart_terminal)
1303         {
1304             Move_to((screen_width - 24) - (days > 9 ? 1 : 0), 0);
1305         }
1306         else
1307         {
1308             fputs(" ", stdout);
1309         }
1310         printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);
1311     }
1312 }