Minor changes.
[egate.git] / util / ttyemit.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /* Gateway tty print utility
45  * Europagate, 1995
46  *
47  * $Log: ttyemit.c,v $
48  * Revision 1.2  1995/05/16 09:40:55  adam
49  * LICENSE.
50  *
51  * Revision 1.1  1995/04/17  09:36:34  adam
52  * ttyemit moved from kernel directory.
53  *
54  *
55  */
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <assert.h>
59
60 #include <ttyemit.h>
61
62 static char line_buf[256];
63 static int  line_col = 0;
64 static int  esc_flag = 0;
65
66 static int  line_min = 30;
67 static int  line_max = 76;
68 static FILE *out_f = stdout;
69
70 void tty_init (FILE *out, int min, int max)
71 {
72     out_f = out;
73     if (min > 0)
74         line_min = min;
75     if (max > 2)
76         line_max = max;
77     line_col = 0;
78     esc_flag = 0;
79 }
80
81 static void flush (void)
82 {
83     int j;
84
85     for (j = 0; j<line_col; j++)
86         putc (line_buf[j], out_f);
87     putc ('\n', out_f);
88     line_col = 0;
89 }
90
91 static void split (int ch)
92 {
93     int i = line_col, j;
94
95     while (1)
96         if (line_buf[--i] == ' ')
97         {
98             int extra = 0;
99
100             for (j = 0; j<i; j++)
101             {
102 #if 1
103                 if (j>1 && line_buf[j] >= 'A'&& line_buf[j] <= 'Z'
104                      && line_buf[j-1] == ' '
105                      && (line_buf[j-2] == '.' || line_buf[j-2] == ';')
106                      && extra < line_max-i )
107                 {
108                     extra++;
109                     putc (' ', out_f);
110                 }
111 #endif
112                 putc (line_buf[j], out_f);
113             }
114             putc ('\n', out_f);
115             j = 0;
116             ++i;
117             while (i < line_col)
118                 line_buf[j++] = line_buf[i++];
119             line_col = j;
120             break;
121         }
122         else if (i < line_min)
123             flush ();
124 }
125
126 static void escape (int ch)
127 {
128     switch (ch)
129     {
130     case 'n':
131         if (line_col >= line_max)
132             split (' ');
133         else
134             flush ();
135         break;
136     case 't':
137         do
138             line_buf[line_col++] = ' ';
139         while (line_col & 7);
140         break;
141     case 's':
142         line_buf[line_col++] = ' ';
143         break;
144     default:
145         line_buf[line_col++] = ch;
146     }
147 }
148
149 void tty_emit (int ch)
150 {
151     if (esc_flag)
152     {
153         escape (ch);
154         esc_flag = 0;
155     }
156     else if (ch == '\\')
157         esc_flag = 1;
158     else if (ch == '\n')
159         flush ();
160     else if (line_col || ch != ' ')
161     {
162         line_buf[line_col++] = ch;
163         if (line_col >= line_max)
164             split (ch);
165     }
166 }