output.c (1264B)
1 /* copyright (c) 1985 ceriel J.H. jacobs */ 2 3 /* 4 * handle output to screen 5 */ 6 7 #include "output.h" 8 #include "in_all.h" 9 #include "main.h" 10 11 int _ocnt; 12 char *_optr; 13 14 #define OBUFSIZ 64 * 128 15 16 static char _outbuf[OBUFSIZ]; 17 18 void 19 flush(void) 20 { /* flush output buffer, by writing it */ 21 char *p = _outbuf; 22 23 _ocnt = OBUFSIZ; 24 if (_optr) 25 (void)write(1, p, _optr - p); 26 _optr = p; 27 } 28 29 void 30 nflush(void) 31 { /* flush output buffer, ignoring it */ 32 33 _ocnt = OBUFSIZ; 34 _optr = _outbuf; 35 } 36 37 int 38 fputch(int ch) 39 { /* print a character */ 40 putch(ch); 41 return ch; 42 } 43 44 void 45 putline(char *s) 46 { /* print string s */ 47 48 if (!s) 49 return; 50 while (*s) { 51 putch(*s++); 52 } 53 } 54 55 /* 56 * a safe version of putline. all control characters are echoed as ^x 57 */ 58 59 void 60 cputline(char *s) 61 { 62 int c; 63 64 while ((c = *s++) != 0) { 65 if ((unsigned char)c < ' ' || (unsigned char)c == 0x7f) { 66 putch('^'); 67 c ^= 0x40; 68 } 69 putch(c); 70 } 71 } 72 73 /* 74 * simple minded routine to print a number 75 */ 76 77 void 78 prnum(long n) 79 { 80 putline(getnum(n)); 81 } 82 83 static char * 84 fillnum(long n, char *p) 85 { 86 if (n >= 10) { 87 p = fillnum(n / 10, p); 88 } 89 *p++ = (int)(n % 10) + '0'; 90 *p = '\0'; 91 return p; 92 } 93 94 char * 95 getnum(long n) 96 { 97 static char buf[20]; 98 99 fillnum(n, buf); 100 return buf; 101 }