Debian Codegraph @

libsrc/tw/tvout.c

raw
1 /* @(#)tvout.c 19.1 (ES0-DMD) 02/25/03 13:58:15 */
2 /*===========================================================================
3 Copyright (C) 1995 European Southern Observatory (ESO)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
18 MA 02139, USA.
19
20 Corresponding concerning ESO-MIDAS should be addressed as follows:
21 Internet e-mail: [email protected]
22 Postal address: European Southern Observatory
23 Data Management Division
24 Karl-Schwarzschild-Strasse 2
25 D 85748 Garching bei Muenchen
26 GERMANY
27 ===========================================================================*/
28
29 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
31 .TYPE Module
32 .IDENTIFICATION tvout.c
33 .AUTHOR Francois Ochsenbein [ESO-IPG]
34 .LANGUAGE C
35 .KEYWORDS Terminal Independant Window Package.
36 .ENVIRONMENT TermWindows
37 .COMMENTS This module groups direct output to terminal (interfaces
38 to ost), without checks on the validity of the
39 output or cursor positionning.
40 \begin{TeX}
41 Used Capabilities:
42 $$\begin{tabular}{|lp{30em}|}\hline
43 {\tt pc=} & Padding char (for delays) \\
44 {\tt G0{\rm...}G4=} & Corners \\
45 {\tt Gu,Gd,Gl,Gr=} & Tees \\
46 {\tt Gg=} & ``Rubbish'' character \\
47 \hline
48 \end{tabular}$$
49 \end{TeX}
50 .VERSION 2.1 11-Jun-1987 Version '2' of TermWindows.
51 Extracted from previous ZTOUT.
52 .VERSION 3.0 20-Mar-1988 Version '3' of TermWindows.
53 ----------------------------------------------------------------------------*/
54
55 #define DEBUG 0 /* For debugging */
56
57 #define PM_LEVEL LEVEL_TV
58
59 #define TW_MACROS 0 /* Don't use TermWindows Macros */
60 #define TW_import 0
61 #define TW_STRUCT 0 /* Do not use window Structures */
62 #include <twset.h>
63
64 #define ms_to_pad(ms) ((terms->baud/100) * ms)/(unsigned)100
65
66
67 MID_EXTERN TERM *terms ;
68 MID_STATIC char delay_flag = EOS;
69
70 #if DEBUG
71 #define ENTER_DEBUG(x) ENTER(x)
72 #define EXIT_DEBUG(x) EXIT(x)
73 #else
74 #define ENTER_DEBUG(x)
75 #define EXIT_DEBUG(x) return(x)
76 #endif
77
78 #define TERMTEST
79
80 #define ENTER_TV(x) static int state_buffer; \
81 ENTER(x); TERMTEST; \
82 state_buffer = tv_buffer(1);
83 #define EXIT_TV(f) FIN: \
84 tv_buffer(state_buffer); \
85 EXIT(f)
86
87 /* Macros just to improve readibility */
88
89 #define BUFW (terms->bufw)
90 #define BUFS (terms->bufs)
91
92 MONITOR (TVOUT);
93
94
95 /*==========================================================================
96 * tv_feed
97 *==========================================================================*/
98 static int tv_feed()
99 /*+++
100 .PURPOSE Write the current buffer to the terminal, and reset buffer.
101 .RETURNS OK / NOK
102 .REMARKS
103 -------*/
104 {
105 register int s;
106
107 ENTER_DEBUG("tv_feed");
108 TRACE_STR2(BUFW, terms->iw);
109
110 s = OK;
111 if (terms->iw) /* Only if there is something in the buffer */
112 { if (terms->flags & TERM_NOWRITE) ;
113 else
114 {
115 #if (TW_LEVEL == 0) /* Minimal Implementation */
116 s = ostwrite(BUFW, terms->iw);
117 #else
118 if (terms->term_type) /* No terminal */
119 s = osdwrite(1, BUFW, terms->iw);
120 else
121 { s = ostwrite(BUFW, terms->iw);
122 if (terms->msw) tv_wms(terms->msw);
123 }
124 }
125 }
126 #endif
127 if (s < 0) ERROR(osmsg()), s = NOK;
128 else s = OK;
129 terms->iw = 0;
130 EXIT_DEBUG(s);
131 }
132
133 /*==========================================================================
134 * tv_buffer
135 *==========================================================================*/
136 int tv_buffer (buffered)
137 /*+++
138 .PURPOSE Start (buffered!=0) or Stop (buffered==0) terminal buffering
139 .RETURNS The previous state, i.e. 0 if terminal was unbuffered, not-zero
140 if terminal was buffered.
141 .REMARKS If unbuffering is asked for, the contents of the buffer is
142 sent to the terminal. NOT TRACED.
143 ---*/
144 int buffered; /* IN: 1 to start, 0 to stop buffering */
145 {
146 register int returned;
147
148 returned = (terms->flags) & (TERM_buffered);
149
150 if(buffered) terms->flags |= TERM_buffered;
151 else
152 { if (terms->iw) /* If there is something in the buffer */
153 tv_feed(); /* Send the buffer contents */
154 terms->flags &= ~(TERM_buffered);
155 }
156
157 return(returned);
158 }
159
160 /*==========================================================================
161 * compute_delay
162 *==========================================================================*/
163 static int compute_delay(str)
164 /*
165 .PURPOSE Compute the delay required by a sequence.
166 .RETURNS The delay converted into a number of padding chars.
167 .REMARKS Not Traced.
168 -----------------------------------------------------------------------*/
169 char *str; /* IN: The capability string */
170 {
171 register int delay;
172
173 delay = 0;
174 delay_flag = *(str+3);
175
176 if (delay_flag != '=') /* A delay is required */
177 { delay = (unsigned char)*(str+4+ *(unsigned char *)str);
178 /* Delay in ms */
179 delay = ms_to_pad(delay); /* Delay in number of padding chars */
180 }
181
182 return(delay);
183 }
184
185 /*==========================================================================
186 * tv_delay
187 *==========================================================================*/
188 int tv_delay(str)
189 /*++++++++++
190 .PURPOSE Send the delay (only) of a sequence, in principle made of NUL
191 characters.
192 .RETURNS OK / NOK
193 .REMARKS
194 -----------------------------------------------------------------------*/
195 char *str; /* IN: The capability string */
196 {
197 register int delay;
198
199 ENTER_DEBUG("tv_delay");
200
201 status = OK;
202
203 delay = compute_delay(str); /* Delay in number of padding chars */
204
205 if(delay)
206 { if (delay > terms->buf_size) delay = terms->buf_size;
207 oscfill(BUFS,delay,terms->pad_char);
208 status = tv_out(BUFS,delay);
209 }
210
211 EXIT_DEBUG(status);
212 }
213
214 /*==========================================================================
215 * tv_out
216 *==========================================================================*/
217 int tv_out(str,len)
218 /*++++++++++++
219 .PURPOSE Write any string of characters to the terminal, with buffering.
220 No care taken about control characters, cursor position, etc.
221 .RETURNS OK / NOK
222 .REMARKS The buffer is only filled, and sent to terminal when full.
223 Use tv_feed to output the buffer.
224 --------------*/
225 char *str; /* IN: String to display on the terminal */
226 int len; /* IN: Length of String */
227 {
228 register int i, l;
229 char *p, *q;
230
231
232 ENTER_DEBUG("tv_out");
233
234 status = OK;
235
236 TRACE_ED_STR2("tv_out of: ", str, len);
237
238 for (l = len, p = str, i = 0; (l > 0) && (status); l -= i, p += i)
239 { i = terms->buf_size - terms->iw;
240 i = MIN(l, i);
241 q = BUFW + terms->iw;
242 terms->iw += oscopy(q, p, i);
243 if (terms->iw >= terms->buf_size) status = tv_feed();
244 }
245
246 EXIT_DEBUG(status);
247 }
248
249 /*==========================================================================
250 * tv_send
251 *==========================================================================*/
252 int tv_send(str, times)
253 /*++++++++++
254 .PURPOSE Send a sequence n times. This sequence has the encoded form
255 of a capability (see tu_encode)
256 .RETURNS OK / NOK
257 .REMARKS The delay is made by sending additional padding chars.
258 ----------*/
259 char *str; /* IN: The string to send */
260 int times; /* IN: Number of times to send the string */
261 {
262 int ls, n;
263 int delay;
264
265 ENTER_TV("tv_send");
266
267 #if DEBUG
268 TRACE_ED_I("Sending sequence times: ", times);
269 #endif
270
271 status = OK;
272 if(times <= 0) FINISH; /* Nothing to send ... */
273 if(!str) FINISH;
274
275 ls = *str;
276 if(ls == 0) FINISH;
277
278
279 delay = compute_delay(str); /* Delay in number of padding chars */
280
281 /* Output the text in a loop */
282
283 if (delay) /* A delay is required */
284 { if (delay > terms->buf_size) delay = terms->buf_size;
285 oscfill(BUFS, delay, terms->pad_char);
286 }
287 for (n=times; --n>=0; )
288 { if (!(status = tv_out(str+4,ls))) FINISH;
289 if(delay_flag == 'D')
290 if (!(status = tv_out(BUFS,delay))) FINISH;
291 }
292 if (delay_flag == 'd')
293 status = tv_out(BUFS,delay);
294
295 EXIT_TV(status);
296 }
297
298 /*==========================================================================
299 * tv_wms(ms)
300 *==========================================================================*/
301 int tv_wms(ms)
302 /*++++++
303 .PURPOSE Simulate a waiting time (by sending padding chars)
304 .RETURNS OK
305 .REMARKS Send directly the padding chars to terminal (no buffering)
306 ------------*/
307 short unsigned int ms; /* IN: Number of ms waiting time to simulate */
308 {
309 register int delay, n;
310
311 ENTER("tv_wms");
312
313 if (terms->term_type) FINISH; /* No terminal */
314
315 delay = ms_to_pad((int)ms);
316
317 oscfill(BUFS, MIN(delay, terms->buf_size), terms->pad_char);
318
319 status = 0;
320 while ( (delay > 0) && (status >= 0) )
321 { n = MIN(delay, terms->buf_size);
322 status = ostwrite(BUFS, n);
323 delay -= n;
324 }
325 if (status < 0) ERROR(osmsg());
326
327 FIN:
328 EXIT(OK);
329 }