main
output.c
1/* Copyright (c) 1985 Ceriel J.H. Jacobs */
2
3/*
4 * Handle output to screen
5 */
6
7#include "in_all.h"
8#include "output.h"
9#include "main.h"
10
11int _ocnt;
12char *_optr;
13
14#define OBUFSIZ 64 * 128
15
16static char _outbuf[OBUFSIZ];
17
18void
19flush()
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
29void
30nflush()
31{ /* Flush output buffer, ignoring it */
32
33 _ocnt = OBUFSIZ;
34 _optr = _outbuf;
35}
36
37int
38fputch(int ch)
39{ /* print a character */
40 putch(ch);
41 return ch;
42}
43
44void
45putline(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
59void
60cputline(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
77void
78prnum(long n)
79{
80
81 putline(getnum(n));
82}
83
84static char *
85fillnum(long n, char *p)
86{
87 if (n >= 10) {
88 p = fillnum(n / 10, p);
89 }
90 *p++ = (int)(n % 10) + '0';
91 *p = '\0';
92 return p;
93}
94
95char *
96getnum(long n)
97{
98 static char buf[20];
99
100 fillnum(n, buf);
101 return buf;
102}