Debian Codegraph @

libsrc/os/vms/ost.c

raw
1 /* @(#)ost.c 19.1 (ES0-DMD) 02/25/03 13:56:14 */
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 .TYPE Module
31 .IDENTIFICATION ost
32 .AUTHOR Guido Russo [ST-ECF], Francois Ochsenbein [ESO-IPG]
33 .LANGUAGE C
34 .KEYWORDS Terminal i/o
35 .ENVIRONMENT VAX / VMS Version
36 .COMMENTS Adapted from Toolpack TIE WINDOWS.
37 .VERSION 1.0 30-Jan-1986: Creation
38 .VERSION 1.1 19-Jun-1986: Cosmetic changes (F. Ochsenbein).
39 Redefined I/O functions as #define's.
40 Traced system errors.
41 Modified the Terminator Mask as a short expression.
42 .VERSION 1.2 01-Jul-1986: Since QIO is used for output and QIOW
43 for input, 2 DIFFERENT i/o status blocks are required
44 for read and write operations !!!
45 .VERSION 1.3 14-Aug-1986: The baud rate is also returned.
46 .VERSION 1.4 10-Oct-1986: A new function, xv_close, does
47 the required wait until all text is displayed.
48 .VERSION 1.5 11-Jun-1987: Cosmetic modifications
49 .VERSION 1.6 16-Jun-1987: Adapted for Ibm-PC, G. Russo, M. Albrecht.
50 .VERSION 1.7 22-Jun-1987: Adapted for UNIX
51 .VERSION 1.8 19-Jan-1988: Transformed to os routine ostinfo, ostopen,
52 ostclose, ostread, ostwrite. A time-out is ALWAYS used.
53 ostin returns the number of bytes in type-ahead buffer.
54 .VERSION 2.0 09-Mar-1988: Added interrupt catching, to be sure
55 that the terminal is correcly reset.
56 .VERSION 2.1 28-Oct-1988: No ^C catching in VMS version.
57 .VERSION 2.2 18-May-1989: Added vt300_series terminal type to ostinfo.
58 When terminal is unknown, the string "unknown"
59 is returned. M. Albrecht [ESIS]
60 .VERSION 2.3 24-Jul-1989: ostint returns previously defined interrupt function
61 .VERSION 2.4 03-Aug-1990: ostint works also if terminal was not opened.
62 .VERSION 2.5 08-Sep-1990: allow long output
63 .VERSION 2.6 17-Oct-1990: Removed GNU-nonunderstandable code...
64 .VERSION 2.65 06-Nov-1990: Added ostwinch (Window Change)
65 ----------------------------------------------------------------------------*/
66
67 #define DEBUG 0
68
69 #include <osdefos.h>
70 #include <macrogen.h> /* Classical macros */
71 #include <osterm.h> /* Terminal Definitions */
72 #include <signal.h>
73 #include <errno.h>
74 #ifdef EOF
75 #undef EOF
76 #endif
77
78 typedef int (*FUNCTION)(); /* Pointer to Function */
79
80 #ifndef SIG_ERR
81 #define SIG_ERR (FUNCTION)-1
82 #endif
83
84 int oststop();
85 static int (*f_int)() = NULL_FCT(int); /* Interrupt function */
86 static char buf_name[24];
87
88
89 # include <ssdef.h>
90 # include <iodef.h>
91 # include <dcdef.h>
92 # include <ttdef.h>
93 # include <tt2def.h>
94 # define dsc$w_length len
95 # define dsc$a_pointer s
96 # include <descrip.h>
97 # define ENTER() oserror = 0, vmserror = SS$_NORMAL
98 # define EXIT(x) FIN: if (!(vmserror&1)) \
99 if (oserror == 0) oserror = EIO;\
100 return (oserror ? -1 : x);
101 # define CALL$(f) if (!((vmserror = f)&1)) FINISH;
102
103 # define func_ssm IO$_SENSEMODE
104 # define func_sen IO$_SENSEMODE|IO$M_TYPEAHDCNT
105 # define func_out IO$_WRITELBLK|IO$M_NOFORMAT
106 # define func_cin IO$_READLBLK|IO$M_NOFILTR|IO$M_NOECHO|IO$M_TIMED
107 int SYS$WAITFR();
108 static int tt_chan = 0; /* Channel number */
109 static int tt_efn_r = 0; /* Event flag read */
110 static int tt_efn_w = 0; /* Event flag write */
111 static int mask[2] = {0,0}; /* Short Mask, no terminator char */
112 static unsigned short iosb_r[4]= {0}; /* I/O status block for read */
113 static unsigned short iosb_w[4]= {SS$_NORMAL, 0, 0, 0};
114 /* I/O status block for write */
115 static unsigned short io_sm[4] = {0};
116
117 static char tt_des_s[] = "TT:";
118 static struct dsc$descriptor_s tt_des =
119 { sizeof(tt_des_s)-1, DSC$K_DTYPE_T, DSC$K_CLASS_S, tt_des_s };
120
121
122 #define FINISH goto FIN
123
124 MID_EXTERN int oserror;
125 MID_EXTERN char *oserrmsg;
126 MID_EXTERN int vmserror; /* VMS-specific error code */
127
128 /*==========================================================================
129 * VMS-specific internal routines
130 *==========================================================================*/
131 static int assign()
132 /*+++++
133 .PURPOSE Assign channel to terminal + event flags
134 .RETURNS 0 (sucess) / -1 (failure), oserror explains
135 .REMARKS If no terminal attached, returns -1.
136 --------------*/
137 {
138 ENTER();
139
140 if (tt_chan == 0)
141 { CALL$(LIB$GET_EF(&tt_efn_r)); /* Get an event flag for read */
142 CALL$(SYS$CLREF(tt_efn_r)); /* ... and clear it */
143
144 CALL$(LIB$GET_EF(&tt_efn_w)); /* Get an event flag for write */
145 CALL$(SYS$CLREF(tt_efn_w)); /* ... and clear it */
146
147 CALL$(SYS$ASSIGN(&tt_des,&tt_chan,0,0)); /* Assign a channel to TT */
148 }
149
150 EXIT(0);
151 }
152
153 /*==========================================================================
154 * ostinfo
155 *==========================================================================*/
156 int ostinfo (tstat)
157 /*+++++
158 .PURPOSE Get info about the terminal in termstatus structure (see osterm.h),
159 i.e. terminal name, number of lines, of columns, baud rate,
160 and interrupt control characters.
161 .RETURNS 0 (sucess) / -1 (failure), oserror explains
162 .REMARKS If no terminal attached, returns -1.
163 --------------*/
164 struct termstatus *tstat; /* OUT: Terminal description */
165 {
166 register int i;
167 register char *p, *q;
168
169 ENTER();
170
171 tstat->lines = 0; tstat->columns = 0;
172
173 if (assign() < 0) FINISH;
174 CALL$(SYS$QIOW(tt_efn_r,tt_chan,func_ssm,iosb_r,0,0,
175 io_sm,sizeof(io_sm),0,0,0,0)); /* Get device characteristics */
176
177 tstat->lines = io_sm[3] >> 8; /* Screen length */
178 tstat->columns = io_sm[1]; /* Width of screen */
179
180 /* Get the baud rate --- However, some rounding is sufficient */
181
182 switch(iosb_r[1] & 0xFF)
183 { case TT$C_BAUD_150:
184 case TT$C_BAUD_134: i = 150; break;
185 case TT$C_BAUD_110: i = 110; break;
186 case TT$C_BAUD_50: i = 50 ; break;
187 case TT$C_BAUD_75: i = 75 ; break;
188 case TT$C_BAUD_300: i = 300; break;
189 case TT$C_BAUD_600: i = 600; break;
190 default:
191 case TT$C_BAUD_1200:
192 case TT$C_BAUD_1800: i = 1200; break;
193 case TT$C_BAUD_2000: i = 2000; break;
194 case TT$C_BAUD_2400: i = 2400; break;
195 case TT$C_BAUD_3600: i = 3600; break;
196 case TT$C_BAUD_4800: i = 4800; break;
197 case TT$C_BAUD_7200: i = 7200; break;
198 case TT$C_BAUD_9600: i = 9600; break;
199 case TT$C_BAUD_19200: i = 19200; break;
200 }
201
202 /* Set terminal type according to physical device */
203 switch(io_sm[0] >> 8) /* VMS-supplied terminal type */
204 {
205 case TT$_VT52:
206 case TT$_VT55: p = "vt52"; /* VT52 class */
207 break;
208 case TT$_VT100:
209 case TT$_VT101:
210 case TT$_VT105:
211 case TT$_VT125: p = "vt100"; /* VT100 class */
212 break;
213 case TT$_VT102:
214 case TT$_VT131:
215 case TT$_VT132: p = "vt102"; /* VT102 class */
216 break;
217 case TT$_VT200_SERIES:
218 p = "vt200"; /* VT200 class */
219 break;
220 case TT$_VT300_SERIES:
221 p = "vt300"; /* VT300 class */
222 break;
223 case TT$_LA12:
224 case TT$_LA34:
225 case TT$_LA36:
226 case TT$_LA38:
227 case TT$_LA120:
228 case TT$_LA100: p = "ht"; /* Hardcopy */
229 break;
230 default: p = "unknown"; /* unknown */
231 break;
232 }
233 tstat->cc_INT = Control_('C');
234 tstat->cc_QUIT = Control_('Y');
235
236 tstat->baud_rate = i;
237 /* Copy terminal name to buffer */
238 i = strlen(p);
239 if (i > sizeof(buf_name)-1) i = sizeof(buf_name)-1;
240 for (q = buf_name; --i >= 0; ) *(q++) = *(p++);
241 *q = EOS;
242 tstat->termname = buf_name;
243
244 EXIT(0);
245 }
246
247 /*==========================================================================
248 * ostopen
249 *==========================================================================*/
250 int ostopen ()
251 /*+++++++++++++
252 .PURPOSE Initialize the terminal.
253 .RETURNS 0 (sucess) / -1 (failure), oserror explains
254 .REMARKS If no terminal attached, returns -1.
255 --------------*/
256 {
257 ENTER();
258
259 if (assign() < 0) FINISH;
260
261 CALL$(SYS$QIOW(tt_efn_r,tt_chan,func_ssm,iosb_r,0,0,
262 io_sm,sizeof(io_sm),0,0,0,0)); /* Get device characteristics */
263 #if 0 /* Don't catch ^C */
264 CALL$(SYS$QIOW(tt_efn_w,tt_chan,IO$_SETMODE|IO$M_CTRLCAST,iosb_w,0,0,
265 f_int,SIGINT,0,0,0,0)); /* Enable Control C */
266 #endif
267
268 EXIT(0);
269 }
270
271 /*==========================================================================
272 * ostclose
273 *==========================================================================*/
274 int ostclose ()
275 /*+++++++
276 .PURPOSE Close the terminal (reset it to normal)
277 .RETURNS 0 / -1 on failure
278 ----------*/
279 {
280 register int l;
281
282 ENTER();
283
284 l = 0;
285
286 #if DEBUG
287 puts("\n....Closed Terminal");
288 #endif
289
290 EXIT(l);
291 }
292
293 /*==========================================================================
294 * oststop
295 *==========================================================================*/
296 int oststop (s)
297 /*++++++++++++
298 .PURPOSE Terminate program in case of interrput
299 .RETURNS To OS
300 ----------------------------------------------------------------------------*/
301 int s; /* IN: Signal identification */
302 {
303 ENTER();
304
305 ostclose();
306
307 SYS$EXIT(SS$_NORMAL);
308 EXIT(0);
309 }
310
311 /*==========================================================================
312 * ostset
313 *==========================================================================*/
314 int ostset (tstat)
315 /*++++
316 .PURPOSE Set Terminal.
317 .RETURNS 0 / -1
318 .REMARKS Only interrupt / quit controls are reset.
319 ----------------------------------------------------------------------------*/
320 struct termstatus *tstat; /* IN: Status structure with controls\
321 to modify */
322 {
323 ENTER();
324
325 EXIT(0);
326 }
327
328 /*==========================================================================
329 * ostraw
330 *==========================================================================*/
331 int ostraw (op)
332 /*++++
333 .PURPOSE Set Terminal to raw (op = 1), to Normal (0)
334 .RETURNS 0 / -1
335 .REMARKS Does nothing...
336 ---------------------------------*/
337 int op; /* IN: 0 to reset to standard, 1 to reset to raw */
338 {
339 ENTER();
340
341 EXIT(0);
342 }
343
344 /*==========================================================================
345 * ostint
346 *==========================================================================*/
347 FUNCTION ostint (f)
348 /*++++
349 .PURPOSE Define a function for interrupt
350 .RETURNS Previous function
351 ----------------------------------------------------------------------------*/
352 FUNCTION f; /* IN: Function to call in case of Interrupt */
353 {
354 FUNCTION old_f;
355
356 ENTER();
357
358 old_f = f_int;
359 f_int = f;
360
361 if (!tt_chan) assign();
362 CALL$(SYS$QIOW(tt_efn_w,tt_chan,IO$_SETMODE|IO$M_CTRLCAST,
363 iosb_w,0,0,0,0,0,0,0,0)); /* Disable Control C */
364 CALL$(SYS$QIOW(tt_efn_w,tt_chan,IO$_SETMODE|IO$M_CTRLCAST,
365 iosb_w,0,0,f_int,SIGINT,0,0,0,0)); /* Enable Control C */
366 CALL$(SYS$QIOW(tt_efn_w,tt_chan,IO$_SETMODE|IO$M_CTRLCAST,
367 iosb_w,0,0,SYS$WAITFR,tt_efn_w,0,0,0,0)); /* Called before */
368
369 FIN:
370 return(old_f);
371 }
372
373 FUNCTION ostwinch (f)
374 /*++++
375 .PURPOSE Define a function for Window Change
376 .RETURNS Previous function
377 ----------------------------------------------------------------------------*/
378 FUNCTION f; /* IN: Function to call in case of Window Change */
379 {
380 FUNCTION old_f;
381
382 ENTER();
383
384 old_f = SIG_ERR;
385 oserror = -1, oserrmsg = "WINCH not available";
386
387 return(old_f);
388 }
389
390 /*==========================================================================
391 * ostin
392 *==========================================================================*/
393 int ostin()
394 /*+++
395 .PURPOSE Retrieve the number of characters waiting for input (type-ahead buffer)
396 .RETURNS Number of bytes waiting in type-ahead buffer / -1 for error.
397 .REMARKS No actual read is performed.
398 --------*/
399 {
400 register int l;
401
402 ENTER();
403
404 /* Call system routine to get number of characters
405 * in type-ahead buffer */
406
407 CALL$(SYS$QIOW(tt_efn_r,tt_chan,func_sen,iosb_r,0,0,
408 io_sm,sizeof(io_sm),0,0,0,0));
409
410 l = io_sm[0];
411
412 EXIT(l);
413 }
414
415 /*==========================================================================
416 * ostread
417 *==========================================================================*/
418 int ostread(string, length, timeout)
419 /*+++
420 .PURPOSE Input from the terminal (a timeout is REQUIRED!).
421 A null timeout performs the reading of what is in the
422 type-ahead buffer only.
423
424 \begin{TeX}
425 This routine must perform binary input --- \ie the input is not
426 echoed or edited, and the program receives the user's input
427 without having to wait for a RETURN / newline / etc.
428
429 If the user has not
430 typed anything within the maximum time passed (in seconds)
431 it should return with the value zero.
432 It is desirable (but not
433 strictly necessary) that the "wait" be cut short if input is
434 received after the routine call but before the expiry of the
435 time period --- if this is not the case then the calling routines
436 may simply be a bit slow.
437 \end{TeX}
438 .RETURNS Number of bytes read / -1 (End of File or Failure)
439 -----------------------------------------------------------------------*/
440 char *string; /* OUT: string read */
441 int length; /* IN: size of string */
442 int timeout; /* IN: Timeout time in seconds */
443 {
444 register int l, time;
445 static long int num_char;
446
447 ENTER();
448
449 if (length <=0) /* Impossible to get 0 char */
450 { oserror = EINVAL;
451 FINISH;
452 }
453 /* Test timeout */
454 time = (timeout < 0 ? 0 : timeout);
455
456
457 /* Call system routine to get number of characters
458 * in type-ahead buffer */
459
460 CALL$(SYS$QIOW(tt_efn_r,tt_chan,func_sen,iosb_r,0,0,
461 io_sm,sizeof(io_sm),0,0,0,0));
462
463 /* if buffer empty, input 1 character */
464 l = (io_sm[0] ? io_sm[0] : 1 );
465 l = MIN(l, length); /* read as many as the buffer */
466
467 /* Call system routine to actually perform read
468 * from the type-ahead buffer */
469
470 CALL$(SYS$QIOW(tt_efn_r,tt_chan,func_cin,iosb_r,0,0,string,l,
471 time,mask,0,0));
472
473 /* Check for successfull completion.
474 * Note that timeout is not an error,
475 * and number of read bytes need not to be identical with request.
476 */
477
478 l = iosb_r[1]; /* actual number of char read */
479
480 switch(iosb_r[0])
481 { case SS$_NORMAL: case SS$_TIMEOUT: /* successful completion */
482 vmserror = SS$_NORMAL;
483 default: break; /* Report system error */
484 }
485
486 EXIT(l);
487 }
488
489 /*==========================================================================
490 * ostwrite
491 *==========================================================================*/
492 int ostwrite (string, length)
493 /*+++
494 .PURPOSE Physical output of a string to the terminal.
495 .RETURNS Number of bytes written / -1 (failure, oserror explains)
496 -----------------------------------------------------------------------------*/
497 char *string; /* IN: string to be written */
498 int length; /* IN: length of string */
499 {
500 register int l;
501 register char *p, *pe;
502
503 ENTER();
504
505 for (p = string, pe = p + length; p < pe; p += l) {
506 l = pe - p;
507 if (l > 512) l = 512; /* Limit */
508 CALL$(SYS$QIOW(tt_efn_w,tt_chan,func_out,iosb_w,0,0,
509 p,l,0,0,0,0));
510 }
511
512 EXIT(l);
513 }
514