master xplshn/aruu / cmd / extra / yap / output.c
  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
 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  putline(getnum(n));
 81}
 82
 83static char *
 84fillnum(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
 94char *
 95getnum(long n)
 96{
 97  static char buf[20];
 98
 99  fillnum(n, buf);
100  return buf;
101}