Run latex
[egate.git] / kernel / ttyemit.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4
5 #include "kernel.h"
6 static char line_buf[256];
7 static int  line_col = 0;
8 static int  esc_flag = 0;
9
10 static int  line_min = 30;
11 static int  line_max = 76;
12 static FILE *out_f = stdout;
13
14 void tty_init (FILE *out, int min, int max)
15 {
16     out_f = out;
17     if (min > 0)
18         line_min = min;
19     if (max > 2)
20         line_max = max;
21     line_col = 0;
22     esc_flag = 0;
23 }
24
25 static void flush (void)
26 {
27     int j;
28
29     for (j = 0; j<line_col; j++)
30         putc (line_buf[j], out_f);
31     putc ('\n', out_f);
32     line_col = 0;
33 }
34
35 static void split (int ch)
36 {
37     int i = line_col, j;
38
39     while (1)
40         if (line_buf[--i] == ' ')
41         {
42             int extra = 0;
43
44             for (j = 0; j<i; j++)
45             {
46 #if 1
47                 if (j>1 && line_buf[j] >= 'A'&& line_buf[j] <= 'Z'
48                      && line_buf[j-1] == ' '
49                      && (line_buf[j-2] == '.' || line_buf[j-2] == ';')
50                      && extra < line_max-i )
51                 {
52                     extra++;
53                     putc (' ', out_f);
54                 }
55 #endif
56                 putc (line_buf[j], out_f);
57             }
58             putc ('\n', out_f);
59             j = 0;
60             ++i;
61             while (i < line_col)
62                 line_buf[j++] = line_buf[i++];
63             line_col = j;
64             break;
65         }
66         else if (i < line_min)
67             flush ();
68 }
69
70 static void escape (int ch)
71 {
72     switch (ch)
73     {
74     case 'n':
75         if (line_col >= line_max)
76             split (' ');
77         else
78             flush ();
79         break;
80     case 't':
81         do
82             line_buf[line_col++] = ' ';
83         while (line_col & 7);
84         break;
85     case 's':
86         line_buf[line_col++] = ' ';
87         break;
88     default:
89         line_buf[line_col++] = ch;
90     }
91 }
92
93 void tty_emit (int ch)
94 {
95     if (esc_flag)
96     {
97         escape (ch);
98         esc_flag = 0;
99     }
100     else if (ch == '\\')
101         esc_flag = 1;
102     else if (ch == '\n')
103         flush ();
104     else if (line_col || ch != ' ')
105     {
106         line_buf[line_col++] = ch;
107         if (line_col >= line_max)
108             split (ch);
109     }
110 }