Run latex
[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.3  2001/02/26 10:35:40  adam
49  * Updated for version YAZ 1.6 and higher.
50  *
51  * Revision 1.2  1995/05/16 09:40:55  adam
52  * LICENSE.
53  *
54  * Revision 1.1  1995/04/17  09:36:34  adam
55  * ttyemit moved from kernel directory.
56  *
57  *
58  */
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <assert.h>
62
63 #include <ttyemit.h>
64
65 static char line_buf[256];
66 static int  line_col = 0;
67 static int  esc_flag = 0;
68
69 static int  line_min = 30;
70 static int  line_max = 76;
71 static FILE *out_f = 0;
72
73 void tty_init (FILE *out, int min, int max)
74 {
75     out_f = out;
76     if (min > 0)
77         line_min = min;
78     if (max > 2)
79         line_max = max;
80     line_col = 0;
81     esc_flag = 0;
82 }
83
84 static void flush (void)
85 {
86     int j;
87
88     if (!out_f)
89         out_f = stdout;
90     for (j = 0; j<line_col; j++)
91         putc (line_buf[j], out_f);
92     putc ('\n', out_f);
93     line_col = 0;
94 }
95
96 static void split (int ch)
97 {
98     int i = line_col, j;
99
100     if (!out_f)
101         out_f = stdout;
102     while (1)
103         if (line_buf[--i] == ' ')
104         {
105             int extra = 0;
106
107             for (j = 0; j<i; j++)
108             {
109 #if 1
110                 if (j>1 && line_buf[j] >= 'A'&& line_buf[j] <= 'Z'
111                      && line_buf[j-1] == ' '
112                      && (line_buf[j-2] == '.' || line_buf[j-2] == ';')
113                      && extra < line_max-i )
114                 {
115                     extra++;
116                     putc (' ', out_f);
117                 }
118 #endif
119                 putc (line_buf[j], out_f);
120             }
121             putc ('\n', out_f);
122             j = 0;
123             ++i;
124             while (i < line_col)
125                 line_buf[j++] = line_buf[i++];
126             line_col = j;
127             break;
128         }
129         else if (i < line_min)
130             flush ();
131 }
132
133 static void escape (int ch)
134 {
135     switch (ch)
136     {
137     case 'n':
138         if (line_col >= line_max)
139             split (' ');
140         else
141             flush ();
142         break;
143     case 't':
144         do
145             line_buf[line_col++] = ' ';
146         while (line_col & 7);
147         break;
148     case 's':
149         line_buf[line_col++] = ' ';
150         break;
151     default:
152         line_buf[line_col++] = ch;
153     }
154 }
155
156 void tty_emit (int ch)
157 {
158     if (esc_flag)
159     {
160         escape (ch);
161         esc_flag = 0;
162     }
163     else if (ch == '\\')
164         esc_flag = 1;
165     else if (ch == '\n')
166         flush ();
167     else if (line_col || ch != ' ')
168     {
169         line_buf[line_col++] = ch;
170         if (line_col >= line_max)
171             split (ch);
172     }
173 }