Debian Codegraph @

libsrc/os/unix/ost.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2010 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*++++++++++++++++++++
29 .TYPE Module
30 .IDENTIFICATION ost
31 .AUTHOR Guido Russo [ST-ECF], Francois Ochsenbein [ESO-IPG]
32 .LANGUAGE C
33 .KEYWORDS Terminal i/o
34 .ENVIRONMENT POSIX complaint.
35 .COMMENTS Adapted from Toolpack TIE WINDOWS.
36 There is a switch here, named TIMEOUT, for BSD version,
37 which inhibates the timeout in ostread.
38 .VERSION 1.0 30-Jan-1986: Creation
39 .VERSION 1.6 16-Jun-1987: Adapted for Ibm-PC, G. Russo, M. Albrecht.
40 .VERSION 1.7 22-Jun-1987: Adapted for UNIX
41 .VERSION 1.8 19-Jan-1988: Transformed to os MIDAS routines.
42 .VERSION 2.3 28-Aug-1990: Use file descriptor 2 if 0 or 1 is not a tty.
43 Added option 2 for ostraw (only input raw)
44 .VERSION 2.6 06-Nov-1990: Added ostwinch (Window Change)
45 .VERSION 4.0 05-Apr-1993: POSIX compliant.
46
47 100422 last modif
48 ----------------------------------------------------------------------------*/
49
50 /*
51 * signal.h produces compilation error if POSIX_SOURCE is defined on Solaris
52 * 2.3 (I believe a bug). If this happens, remove the POSIX_SOURCE definition
53 * or move the "#include signal.h" before the POSIX_SOURCE.
54 */
55
56 /*
57 * Define _POSIX_SOURCE to indicate
58 * that this is a POSIX program
59 */
60 #define _POSIX_SOURCE 1
61
62 #include <termios.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <signal.h>
66 #include <sys/types.h>
67 #include <unistd.h>
68 #include <setjmp.h>
69 #include <errno.h>
70 #include <proto_os.h> /* ANSI-C prototyping */
71
72 #include <osterm.h> /* Terminal Definitions */
73
74 #ifdef OSERROR_D /* oserror already defined (Silicon G)*/
75 #define oserror midaserror
76 #endif
77
78 extern char *oserrmsg;
79 extern int oserror;
80
81 void winsize();
82 char *getenv();
83 /* int ospexit(); */
84 int oscopy(/*dest, source, len*/);
85 typedef void (*FUNCTION)(); /* Pointer to Function */
86
87 #define MIN(x,y) ((x) <= (y) ? (x):(y)) /* Minimum */
88 #define WAITING 1 /* 1s of Timeout */
89 #define AHEAD 127 /* Size of type-ahead buffer */
90
91
92 /* sigjmp_buf is not defined in Linux SLS 1.03 0.99pl9
93 * but it is defined in Linux SLACKWARE 1.2, 0.99pl12
94 * We can not use jmp_buf, because in Linux it does not save the sig_mask
95 */
96 static sigjmp_buf env;
97 static char buf_name[24];
98 static struct termios tty0;
99 static struct termios tty1;
100 static char ahead_buffer[AHEAD] = "";
101 static int nahead = 0; /* Number of char. in ahead_buffer */
102 static char opened_term = 0;
103 static int mytty = STDERR_FILENO; /* File Descriptor used as terminal */
104 static int myout = STDERR_FILENO; /* File Descriptor used as output */
105
106 #ifdef __STDC__
107 /* ARGSUSED */
108 static void ostalarm(int sig)
109 #else
110 static void ostalarm(sig)
111 /*++++++++++++++++
112 .PURPOSE Function called at end of time-out
113 .RETURNS Within ostread function. Internal use only.
114 ---------*/
115 int sig;
116 #endif
117 {
118 siglongjmp(env,1); /* return with the saved signal mask from sigsetjmp */
119 }
120
121 /*static void oststop(s) */
122 void oststop(s)
123 /*++++++++++++
124 .PURPOSE Terminate program in case of interrput
125 .RETURNS To OS
126 ----------------------------------------------------------------------------*/
127 int s; /* IN: Signal identification */
128 {
129 static char msg[] = "\r\n**** Killed by signal ";
130 static char asig[]= " (XX)\r\n\n";
131 char *p;
132
133 oserror = 0;
134
135 (void) ostclose();
136
137 (void) write(myout, msg, sizeof(msg)-1);
138 switch(s) {
139 #ifdef SIGBUS
140 case SIGBUS: p = "BUS"; break;
141 #endif
142 case SIGPIPE: p = "PIPE"; break;
143 case SIGTSTP: p = "TSTP"; break;
144 case SIGTERM: p = "TERM"; break;
145 case SIGHUP: p = "HUP"; break;
146 case SIGALRM: p = "ALARM"; break;
147 default: p = ""; break;
148 }
149 (void) write(myout, p, strlen(p));
150
151 asig[2] = '0' + s/10;
152 asig[3] = '0' + s%10;
153 (void) write(myout, asig, sizeof(asig)-1);
154
155 ospexit(oserror);
156 }
157
158 static FUNCTION f_int = oststop; /* Interrupt function */
159
160
161 #ifdef __STDC__
162 static int inhibit(void)
163 #else
164 static int inhibit()
165 /*+++++++++++
166 .PURPOSE Inhibit the Quit and SUSP keyword
167 .RETURNS 0 (sucess) / -1 (failure)
168 -------------*/
169 #endif
170 {
171
172 /*
173 * 930630: On PC_SCO _POSIX_VDISABLE is defined as (unsigned char)-1
174 * which can not be used in the next condition inclusion.
175 */
176 #if defined(PC_SCO)
177 # undef _POSIX_VDISABLE
178 # define _POSIX_VDISABLE -1
179 #endif
180
181 #if defined(_POSIX_VDISABLE)
182 if (_POSIX_VDISABLE != -1) {
183
184 /* The symbol is defined so we can just use it */
185 tty1.c_cc[VQUIT] = _POSIX_VDISABLE;
186 tty1.c_cc[VSUSP] = _POSIX_VDISABLE;
187
188 } else
189 #endif
190 {
191 long temp;
192
193 errno = 0; /* Make sure we can tell if fpathconf() changes errno. */
194 /*
195 * See if it is defined for the terminal
196 */
197 temp = fpathconf(mytty,_PC_VDISABLE);
198 if (temp != -1) { /* temp != -1. This is the value to use */
199 tty1.c_cc[VQUIT] = (cc_t)temp;
200 tty1.c_cc[VSUSP] = (cc_t)temp;
201 return(0);
202 }
203 /*
204 * We get here if fpathconf() returned -1. If
205 * errno is changed then ther was a real error
206 */
207 if (errno != 0) return(-1);
208 /* We get here if we cannot disable the suspend character.
209 * Fall back on the unlikely character.
210 */
211 tty1.c_cc[VQUIT] = 0377;
212 tty1.c_cc[VSUSP] = 0377;
213
214 }
215
216 return(0);
217 }
218
219 int ostopen ()
220 /*+++++++++++
221 .PURPOSE Initialize the terminal.
222 .RETURNS 0 (sucess) / -1 (failure), oserror explains
223 .REMARKS If no terminal attached, returns -1.
224 -------------*/
225 {
226 struct sigaction act, oact;
227 oserror = 0;
228
229 /*
230 * Get the terminal status
231 */
232 if (!isatty(STDERR_FILENO)) {
233 mytty = STDIN_FILENO, myout = STDOUT_FILENO;
234 if (isatty(mytty) && isatty(myout)) ;
235 else {
236 oserror = -1, oserrmsg = "No attached terminal";
237 return(-1);
238 }
239 }
240
241 if (tcgetattr(mytty,&tty0) != 0) { oserror = errno; return(-1); }
242 (void) tcgetattr(mytty,&tty1);
243
244 /*
245 * Change Echo and Raw modes
246 */
247 tty1.c_lflag &= ~(ICANON|ECHO);
248 tty1.c_iflag &= ~(ICRNL); /* Don't change CR to NL */
249 tty1.c_cc[VMIN] = 0; /* Length of type-ahead buffer */
250 tty1.c_cc[VTIME] = WAITING*10;/* Waiting time in .1 sec */
251 /*
252 * Inhibate Quit and SUSP characters
253 */
254 if (inhibit() != 0) { oserror = errno; return(-1); }
255
256 #if DEBUG
257 printf("Operation on files: input=%d / output=%d\n", mytty, myout);
258 printf("Initial parameters: oflag=%-7o VMIN=%3d VTIME=%3d\n",
259 tty0.c_oflag, tty0.c_cc[VMIN], tty0.c_cc[VTIME]);
260 printf(" .. will be set to: %-7o %3d %3d\n",
261 tty1.c_oflag, tty1.c_cc[VMIN], tty1.c_cc[VTIME]);
262 ospwait(20);
263 #endif
264
265 act.sa_handler = oststop;
266 sigemptyset(&act.sa_mask);
267 act.sa_flags = 0;
268 if (sigaction(SIGPIPE,&act,&oact) != 0) { oserror = errno; return(-1); }
269 if (sigaction(SIGTSTP,&act,&oact) != 0) { oserror = errno; return(-1); }
270 if (sigaction(SIGHUP,&act,&oact) != 0) { oserror = errno; return(-1); }
271 if (sigaction(SIGTERM,&act,&oact) != 0) { oserror = errno; return(-1); }
272 if (sigaction(SIGQUIT,&act,&oact) != 0) { oserror = errno; return(-1); }
273 #ifdef SIGBUS /* No-POSIX signal */
274 if (sigaction(SIGBUS,&act,&oact) != 0) { oserror = errno; return(-1); }
275 #endif
276 /*
277 * Define the trap routine in case of Interrupt
278 */
279 act.sa_handler = f_int;
280 if (sigaction(SIGINT,&act,&oact) != 0) { oserror = errno; return(-1); }
281 /*
282 * By default ignore ALRM signals
283 * Otherwise it could be killed by intermail() - /monit/prepa.c
284 */
285 act.sa_handler = SIG_IGN;
286 if (sigaction(SIGALRM,&act,&oact) != 0) { oserror = errno; return(-1); }
287
288
289
290 if (tcsetattr(mytty,TCSAFLUSH, &tty1)) { oserror = errno; return(-1); }
291
292 opened_term = 1;
293
294 #if DEBUG
295 (void) write(myout,"ostopen executed\r\n", 18);
296 #endif
297 return(0);
298 }
299
300 int ostclose()
301 /*+++++++
302 .PURPOSE Close the terminal (reset it to normal)
303 .RETURNS 0 / -1 on failure
304 --------------*/
305 {
306 oserror = 0;
307
308 #if DEBUG
309 printf("\nostclose: were : VMIN=%d,VTIME=%d. =>(init) %d, %d\n\r",
310 tty1.c_cc[VMIN], tty1.c_cc[VTIME],
311 tty0.c_cc[VMIN], tty0.c_cc[VTIME]);
312 #endif
313
314 if (opened_term) /* Reset Terminal */
315 if (tcsetattr(mytty,TCSAFLUSH, &tty0)) { oserror = errno; return(-1); }
316
317 opened_term = 0;
318
319 return(0);
320 }
321
322 int ostset(tstat)
323 /*++++
324 .PURPOSE Set Terminal.
325 .RETURNS 0 / -1
326 .REMARKS Only interrupt / quit controls are reset.
327 ---------------------------------*/
328 struct termstatus *tstat; /* IN: Status structure with controls to modify */
329 {
330 oserror = 0;
331
332 if (isatty(mytty) && isatty(myout)) {
333 if (tcgetattr(mytty,&tty1) != 0) { oserror = errno; return(-1); }
334 tty1.c_cc[VINTR] = tstat->cc_INT;
335 tty1.c_cc[VQUIT] = tstat->cc_QUIT;
336 if (tcsetattr(mytty,TCSANOW, &tty1)) { oserror = errno; return(-1); }
337 }
338 else {
339 oserror = -1;
340 oserrmsg = "Can't change terminal setup";
341 return(-1);
342 }
343 return(0);
344 }
345
346 int ostraw(op)
347 /*++++
348 .PURPOSE Set Terminal to raw (op = 1), to Normal (0), to OutputRaw (2)
349 .RETURNS 0 / -1
350 .REMARKS The 2 mode (OutputRaw) sets VTIME to 0 and VMIN to 1 (untimeout).
351 ---------------------------------*/
352 int op; /* IN: 0 to reset to standard, 1 to reset to raw */
353 {
354 oserror = 0;
355
356 if (tcgetattr(mytty,&tty1) != 0) { oserror = errno; return(-1); }
357
358 #if DEBUG
359 printf("ostraw: op=%d. Initial: VMIN=%d,VTIME=%d. Current %d,%d.",op,
360 tty0.c_cc[VMIN], tty0.c_cc[VTIME],
361 tty1.c_cc[VMIN], tty1.c_cc[VTIME]);
362 #endif
363 switch(op) {
364 default: /* ==> to RAW mode */
365 tty1.c_lflag &= ~(ICANON|ECHO);
366 tty1.c_iflag &= ~ICRNL; /* Don't change CR to NL */
367 tty1.c_cc[VMIN] = 0; /* Length of type-ahead buffer */
368 tty1.c_cc[VTIME] = WAITING*10; /* Waiting time in .1 sec */
369 break;
370 case 0: /* ==> to NORMAL mode */
371 tty1.c_cc[VMIN] = tty0.c_cc[VMIN] ;
372 tty1.c_cc[VTIME] = tty0.c_cc[VTIME];
373 tty1.c_lflag = tty0.c_lflag;
374 tty1.c_iflag = tty0.c_iflag;
375 break;
376 case 2: /* ==> Untimeout input */
377 tty1.c_lflag &= ~(ICANON|ECHO);
378 tty1.c_iflag &= ~ICRNL; /* Don't change CR to NL */
379 tty1.c_cc[VMIN] = 1; /* Length of type-ahead buffer */
380 tty1.c_cc[VTIME] = 0; /* No timer */
381 break;
382 }
383 # if DEBUG
384 printf("Now %d,%d\n",
385 tty1.c_cc[VMIN], tty1.c_cc[VTIME]);
386 # endif
387
388 if (tcsetattr(mytty,TCSANOW, &tty1)) { oserror = errno; return(-1); }
389
390 return(0);
391 }
392
393 FUNCTION ostint(f)
394 /*++++
395 .PURPOSE Define a function for interrupt
396 .RETURNS Previous function
397 ----------------------------------------------------------------------------*/
398 FUNCTION f; /* IN: Function to call in case of Interrupt */
399 {
400 struct sigaction act, oact;
401
402 oserror = 0;
403
404 act.sa_handler = f;
405 sigemptyset(&act.sa_mask);
406 act.sa_flags = 0;
407 if (sigaction(SIGINT,&act,&oact) != 0) { oserror = errno; return(SIG_ERR); };
408 f_int = f;
409 return(oact.sa_handler);
410 }
411
412 FUNCTION ostwinch(f)
413 /*++++
414 .PURPOSE Define a function for Window Change
415 .RETURNS Previous function
416 ----------------------------------------------------------------------------*/
417 FUNCTION f; /* IN: Function to call in case of Window Change */
418 {
419 struct sigaction act, oact;
420
421 oserror = 0;
422
423 #ifdef SIGWINCH
424 act.sa_handler = f;
425 sigemptyset(&act.sa_mask);
426 act.sa_flags = 0;
427 if (sigaction(SIGWINCH,&act,&oact) != 0) { oserror = errno; return(SIG_ERR); }
428 return(oact.sa_handler);
429 #else
430 # ifdef ENOSYS
431 oserror = ENOSYS;
432 # else
433 oserror = -1, oserrmsg = "WINCH not available";
434 # endif
435 return(SIG_ERR);
436 #endif
437 }
438
439 int ostin()
440 /*+++
441 .PURPOSE Retrieve the number of characters waiting for input (type-ahead buffer)
442 .RETURNS Number of bytes waiting in type-ahead buffer / -1 for error.
443 .REMARKS Only possible when terminal is in RAW mode.
444 --------*/
445 {
446 int icanon=0;
447 # if DEBUG
448 printf("Now %d,%d\n",
449 tty1.c_cc[VMIN], tty1.c_cc[VTIME]);
450 # endif
451
452 oserror = 0;
453
454 if (nahead < 0) nahead = 0;
455 if (nahead == 0) {
456 /* If terminal in ICANON mode then set to RAW */
457 if (tty1.c_lflag & ICANON) {
458 icanon=1;
459 ostraw(1);
460 }
461 /* with VMIN & VTIME to 0, read() returns as much data without waiting */
462 tty1.c_cc[VTIME] = 0;
463 if (tcsetattr(mytty,TCSANOW, &tty1)) { oserror = errno; return(-1); }
464 if ((nahead = read(mytty, ahead_buffer, AHEAD)) < 0) oserror = errno;
465 /* Reset to previous values */
466 if (tcsetattr(mytty,TCSANOW, &tty1)) { oserror = errno; return(-1); }
467 if (icanon) ostraw(0); /* set back to ICANON mode */
468 }
469 return(nahead);
470 }
471
472 int ostread(string, length, timeout)
473 /*+++
474 .PURPOSE Input from the terminal (a timeout is REQUIRED!).
475 A null timeout performs the reading of what is in the
476 type-ahead buffer only.
477
478 \begin{TeX}
479 This routine must perform binary input --- \ie the input is not
480 echoed or edited, and the program receives the user's input
481 without having to wait for a RETURN / newline / etc.
482
483 If the user has not
484 typed anything within the maximum time passed (in seconds and max. 25)
485 it should return with the value zero.
486 It is desirable (but not
487 strictly necessary) that the "wait" be cut short if input is
488 received after the routine call but before the expiry of the
489 time period --- if this is not the case then the calling routines
490 may simply be a bit slow.
491 \end{TeX}
492 .RETURNS Number of bytes read / -1 (End of File or Failure)
493 -----------------------------------------------------------------------*/
494 char *string; /* OUT: string read */
495 int length; /* IN: size of string */
496 int timeout; /* IN: Timeout time in seconds, max 25 */
497 {
498 register int l;
499 unsigned int time;
500 struct sigaction act, oact;
501
502 oserror = 0;
503
504 if (length <=0) /* Impossible to get 0 char */
505 { oserror = EINVAL; return(-1); }
506 /* Test timeout. Max 25 sec */
507 time = (timeout < 0 ? 0 : timeout);
508 time = (time > 25 ? 25 : time);
509
510 if (nahead > 0) {
511 l = MIN(nahead, length);
512 oscopy(string, ahead_buffer, l);
513 nahead -= l; /* Remaining bytes */
514 if (nahead) oscopy(&ahead_buffer[0], &ahead_buffer[l], nahead);
515 return(l);
516 }
517
518
519 tty1.c_cc[VTIME] = (unsigned char)time*10; /* Timeout in seconds */
520 if (tcsetattr(mytty,TCSANOW, &tty1)) { oserror = errno; return(-1); }
521
522 act.sa_handler = ostalarm;
523 sigemptyset(&act.sa_mask);
524 act.sa_flags = 0;
525 (void) sigaction(SIGALRM,&act,&oact);
526 if ( sigsetjmp(env,1) == 0 ) {
527 for (;;) {
528 if ( (l = read(mytty, string, length)) < 0) {
529 if (errno == EINTR) continue;
530 else oserror = errno;
531 }
532 break;
533 }
534 }
535 else l = 0; /* Was interrupted */
536 (void) sigaction(SIGALRM,&oact,&act); /* set SIGALRM to previous value */
537
538 tty1.c_cc[VTIME] = WAITING*10; /* Reset 1s time-out */
539 if (tcsetattr(mytty,TCSANOW, &tty1)) { oserror = errno; return(-1); }
540
541 return(l);
542 }
543
544
545 int ostwrite(string, length)
546 /*+++
547 .PURPOSE Physical output of a string to the terminal.
548 .RETURNS Number of bytes written / -1 (failure, oserror explains)
549 -----------------------------------------------------------------------------*/
550 char *string; /* IN: string to be written */
551 int length; /* IN: length of string */
552 {
553 register int l;
554
555 oserror = 0;
556
557 if ((l = write(myout, string, length)) < 0) { oserror = errno; return(-1); }
558 return(l);
559 }
560
561 int ostinfo(tstat)
562 /*+++++
563 .PURPOSE Get info about the terminal in termstatus structure (see osterm.h),
564 i.e. terminal name, number of lines, of columns, baud rate,
565 and interrupt control characters.
566 .RETURNS 0 (sucess) / -1 (failure), oserror explains
567 .REMARKS If no terminal attached, returns -1.
568 --------------*/
569 struct termstatus *tstat; /* OUT: Terminal description */
570 {
571 register speed_t baud;
572 register int i;
573 register char *p, *q;
574 unsigned short col, row;
575
576 oserror = 0;
577
578 /*
579 * Get the terminal status
580 */
581 if (!isatty(STDERR_FILENO)) {
582 mytty = STDIN_FILENO;
583 myout = STDOUT_FILENO;
584 if (isatty(mytty) && isatty(myout)) ;
585 else {
586 oserror = -1, oserrmsg = "No attached terminal";
587 return(-1);
588 }
589 }
590
591 if (tcgetattr(mytty,&tty1) != 0) { oserror = errno; return(-1); }
592 baud = cfgetispeed(&tty1);
593
594 switch(baud) {
595 case B150:
596 case B200:
597 case B134: i = 150; break;
598 case B110: i = 110; break;
599 case B50: i = 50 ; break;
600 case B75: i = 75 ; break;
601 case B300: i = 300; break;
602 case B600: i = 600; break;
603 case B1200: i = 1200; break;
604 case B1800: i = 1800; break;
605 default:
606 case B2400: i = 2400; break;
607 case B4800: i = 4800; break;
608 case B9600: i = 9600; break;
609 case B19200: i = 19200; break;
610 case B38400: i = 38400; break;
611 }
612 tstat->baud_rate = i;
613
614 tstat->cc_INT = tty1.c_cc[VINTR];
615 tstat->cc_QUIT = tty1.c_cc[VQUIT];
616
617 winsize(mytty,&col,&row);
618 tstat->lines = row;
619 tstat->columns = col;
620
621 /*
622 * Copy terminal name to buffer
623 */
624 p = getenv("TERM"); /* Get Terminal Type */
625 if (!p) p = getenv("term");
626 if (!p) p = "unknown"; /* Default terminal name */
627
628 /* for edit/table a VT100 terminal seems to be needed,
629 so we cheat a bit... KB 100422 */
630 if (strcmp(p,"xterm") == 0) (void) strcpy(p,"vt100");
631 tstat->termname = p;
632
633 p = tstat->termname;
634 i = strlen(p);
635 if (i > sizeof(buf_name)-1) i = sizeof(buf_name)-1;
636 for (q = buf_name; --i >= 0; ) *(q++) = *(p++);
637 *q = '\0'; /* End Of String */
638 tstat->termname = buf_name;
639
640 return(0);
641 }