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