Home | History | Annotate | Line # | Download | only in hack
hack.tty.c revision 1.13
      1  1.13  dholland /*	$NetBSD: hack.tty.c,v 1.13 2009/06/07 18:30:39 dholland Exp $	*/
      2   1.6  christos 
      3   1.1       cgd /*-
      4   1.3       cgd  * Copyright (c) 1988, 1993
      5   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.6  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.3       cgd #if 0
     35   1.6  christos static char     sccsid[] = "@(#)hack.tty.c	8.1 (Berkeley) 5/31/93";
     36   1.3       cgd #else
     37  1.13  dholland __RCSID("$NetBSD: hack.tty.c,v 1.13 2009/06/07 18:30:39 dholland Exp $");
     38   1.3       cgd #endif
     39   1.6  christos #endif				/* not lint */
     40   1.1       cgd 
     41  1.11       jsm /*
     42  1.11       jsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
     43  1.11       jsm  * Amsterdam
     44  1.11       jsm  * All rights reserved.
     45  1.11       jsm  *
     46  1.11       jsm  * Redistribution and use in source and binary forms, with or without
     47  1.11       jsm  * modification, are permitted provided that the following conditions are
     48  1.11       jsm  * met:
     49  1.11       jsm  *
     50  1.11       jsm  * - Redistributions of source code must retain the above copyright notice,
     51  1.11       jsm  * this list of conditions and the following disclaimer.
     52  1.11       jsm  *
     53  1.11       jsm  * - Redistributions in binary form must reproduce the above copyright
     54  1.11       jsm  * notice, this list of conditions and the following disclaimer in the
     55  1.11       jsm  * documentation and/or other materials provided with the distribution.
     56  1.11       jsm  *
     57  1.11       jsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
     58  1.11       jsm  * Informatica, nor the names of its contributors may be used to endorse or
     59  1.11       jsm  * promote products derived from this software without specific prior
     60  1.11       jsm  * written permission.
     61  1.11       jsm  *
     62  1.11       jsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     63  1.11       jsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     64  1.11       jsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     65  1.11       jsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     66  1.11       jsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     67  1.11       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     68  1.11       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     69  1.11       jsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     70  1.11       jsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     71  1.11       jsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     72  1.11       jsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     73  1.11       jsm  */
     74  1.11       jsm 
     75  1.11       jsm /*
     76  1.11       jsm  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     77  1.11       jsm  * All rights reserved.
     78  1.11       jsm  *
     79  1.11       jsm  * Redistribution and use in source and binary forms, with or without
     80  1.11       jsm  * modification, are permitted provided that the following conditions
     81  1.11       jsm  * are met:
     82  1.11       jsm  * 1. Redistributions of source code must retain the above copyright
     83  1.11       jsm  *    notice, this list of conditions and the following disclaimer.
     84  1.11       jsm  * 2. Redistributions in binary form must reproduce the above copyright
     85  1.11       jsm  *    notice, this list of conditions and the following disclaimer in the
     86  1.11       jsm  *    documentation and/or other materials provided with the distribution.
     87  1.11       jsm  * 3. The name of the author may not be used to endorse or promote products
     88  1.11       jsm  *    derived from this software without specific prior written permission.
     89  1.11       jsm  *
     90  1.11       jsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     91  1.11       jsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     92  1.11       jsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     93  1.11       jsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     94  1.11       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     95  1.11       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     96  1.11       jsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     97  1.11       jsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     98  1.11       jsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     99  1.11       jsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    100  1.11       jsm  */
    101  1.11       jsm 
    102   1.1       cgd /* hack.tty.c - version 1.0.3 */
    103   1.6  christos /*
    104   1.6  christos  * With thanks to the people who sent code for SYSV - hpscdi!jon,
    105   1.6  christos  * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
    106   1.6  christos  */
    107   1.1       cgd 
    108   1.6  christos #include <termios.h>
    109   1.8  christos #include <termcap.h>
    110   1.6  christos #include "hack.h"
    111   1.6  christos #include "extern.h"
    112   1.1       cgd 
    113   1.1       cgd /*
    114   1.1       cgd  * Some systems may have getchar() return EOF for various reasons, and
    115   1.1       cgd  * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
    116   1.1       cgd  */
    117   1.1       cgd #ifndef BSD
    118   1.1       cgd #define	NR_OF_EOFS	20
    119   1.6  christos #endif	/* BSD */
    120   1.1       cgd 
    121   1.6  christos static char     erase_char, kill_char;
    122   1.6  christos static boolean  settty_needed = FALSE;
    123   1.6  christos struct termios  inittyb, curttyb;
    124   1.1       cgd 
    125   1.1       cgd /*
    126   1.1       cgd  * Get initial state of terminal, set ospeed (for termcap routines)
    127   1.1       cgd  * and switch off tab expansion if necessary.
    128   1.1       cgd  * Called by startup() in termcap.c and after returning from ! or ^Z
    129   1.1       cgd  */
    130   1.6  christos void
    131  1.13  dholland gettty(void)
    132   1.6  christos {
    133   1.6  christos 	if (tcgetattr(0, &inittyb) < 0)
    134   1.1       cgd 		perror("Hack (gettty)");
    135   1.1       cgd 	curttyb = inittyb;
    136   1.4   mycroft 	ospeed = cfgetospeed(&inittyb);
    137   1.4   mycroft 	erase_char = inittyb.c_cc[VERASE];
    138   1.4   mycroft 	kill_char = inittyb.c_cc[VKILL];
    139   1.1       cgd 	getioctls();
    140   1.1       cgd 
    141   1.1       cgd 	/* do not expand tabs - they might be needed inside a cm sequence */
    142   1.6  christos 	if (curttyb.c_oflag & OXTABS) {
    143   1.4   mycroft 		curttyb.c_oflag &= ~OXTABS;
    144   1.1       cgd 		setctty();
    145   1.1       cgd 	}
    146   1.1       cgd 	settty_needed = TRUE;
    147   1.1       cgd }
    148   1.1       cgd 
    149   1.1       cgd /* reset terminal to original state */
    150   1.6  christos void
    151  1.13  dholland settty(const char *s)
    152   1.6  christos {
    153   1.1       cgd 	clear_screen();
    154   1.1       cgd 	end_screen();
    155   1.6  christos 	if (s)
    156   1.7    itojun 		printf("%s", s);
    157   1.1       cgd 	(void) fflush(stdout);
    158   1.6  christos 	if (tcsetattr(0, TCSADRAIN, &inittyb) < 0)
    159   1.1       cgd 		perror("Hack (settty)");
    160   1.4   mycroft 	flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF;
    161   1.4   mycroft 	flags.cbreak = (inittyb.c_lflag & ICANON) ? OFF : ON;
    162   1.1       cgd 	setioctls();
    163   1.1       cgd }
    164   1.1       cgd 
    165   1.6  christos void
    166  1.13  dholland setctty(void)
    167   1.6  christos {
    168   1.6  christos 	if (tcsetattr(0, TCSADRAIN, &curttyb) < 0)
    169   1.1       cgd 		perror("Hack (setctty)");
    170   1.1       cgd }
    171   1.1       cgd 
    172   1.1       cgd 
    173   1.6  christos void
    174  1.13  dholland setftty(void)
    175   1.6  christos {
    176   1.6  christos 	int             change = 0;
    177   1.1       cgd 	flags.cbreak = ON;
    178   1.1       cgd 	flags.echo = OFF;
    179   1.1       cgd 	/* Should use (ECHO|CRMOD) here instead of ECHO */
    180   1.6  christos 	if (curttyb.c_lflag & ECHO) {
    181   1.4   mycroft 		curttyb.c_lflag &= ~ECHO;
    182   1.1       cgd 		change++;
    183   1.1       cgd 	}
    184   1.6  christos 	if (curttyb.c_lflag & ICANON) {
    185   1.4   mycroft 		curttyb.c_lflag &= ~ICANON;
    186   1.1       cgd 		/* be satisfied with one character; no timeout */
    187   1.4   mycroft 		curttyb.c_cc[VMIN] = 1;
    188   1.4   mycroft 		curttyb.c_cc[VTIME] = 0;
    189   1.1       cgd 		change++;
    190   1.1       cgd 	}
    191   1.6  christos 	if (change) {
    192   1.1       cgd 		setctty();
    193   1.1       cgd 	}
    194   1.1       cgd 	start_screen();
    195   1.1       cgd }
    196   1.1       cgd 
    197   1.1       cgd 
    198   1.1       cgd /* fatal error */
    199   1.6  christos /* VARARGS1 */
    200   1.6  christos void
    201   1.6  christos error(const char *fmt, ...)
    202   1.6  christos {
    203   1.6  christos 	va_list ap;
    204   1.6  christos 
    205   1.6  christos 	va_start(ap, fmt);
    206   1.6  christos 	if (settty_needed)
    207   1.1       cgd 		settty((char *) 0);
    208   1.6  christos 	vprintf(fmt, ap);
    209   1.6  christos 	va_end(ap);
    210   1.1       cgd 	putchar('\n');
    211   1.1       cgd 	exit(1);
    212   1.1       cgd }
    213   1.1       cgd 
    214   1.1       cgd /*
    215   1.1       cgd  * Read a line closed with '\n' into the array char bufp[BUFSZ].
    216   1.1       cgd  * (The '\n' is not stored. The string is closed with a '\0'.)
    217   1.1       cgd  * Reading can be interrupted by an escape ('\033') - now the
    218   1.1       cgd  * resulting string is "\033".
    219   1.1       cgd  */
    220   1.6  christos void
    221  1.13  dholland getlin(char *bufp)
    222   1.1       cgd {
    223   1.6  christos 	char           *obufp = bufp;
    224   1.6  christos 	int             c;
    225   1.1       cgd 
    226   1.6  christos 	flags.toplin = 2;	/* nonempty, no --More-- required */
    227   1.6  christos 	for (;;) {
    228   1.1       cgd 		(void) fflush(stdout);
    229   1.6  christos 		if ((c = getchar()) == EOF) {
    230   1.1       cgd 			*bufp = 0;
    231   1.1       cgd 			return;
    232   1.1       cgd 		}
    233   1.6  christos 		if (c == '\033') {
    234   1.1       cgd 			*obufp = c;
    235   1.1       cgd 			obufp[1] = 0;
    236   1.1       cgd 			return;
    237   1.1       cgd 		}
    238   1.6  christos 		if (c == erase_char || c == '\b') {
    239   1.6  christos 			if (bufp != obufp) {
    240   1.1       cgd 				bufp--;
    241   1.6  christos 				putstr("\b \b");	/* putsym converts \b */
    242   1.6  christos 			} else
    243   1.6  christos 				bell();
    244   1.6  christos 		} else if (c == '\n') {
    245   1.1       cgd 			*bufp = 0;
    246   1.1       cgd 			return;
    247   1.6  christos 		} else if (' ' <= c && c < '\177') {
    248   1.6  christos 			/*
    249   1.6  christos 			 * avoid isprint() - some people don't have it ' ' is
    250   1.6  christos 			 * not always a printing char
    251   1.6  christos 			 */
    252   1.1       cgd 			*bufp = c;
    253   1.1       cgd 			bufp[1] = 0;
    254   1.1       cgd 			putstr(bufp);
    255   1.6  christos 			if (bufp - obufp < BUFSZ - 1 && bufp - obufp < COLNO)
    256   1.1       cgd 				bufp++;
    257   1.6  christos 		} else if (c == kill_char || c == '\177') {	/* Robert Viduya */
    258   1.6  christos 			/* this test last - @ might be the kill_char */
    259   1.6  christos 			while (bufp != obufp) {
    260   1.1       cgd 				bufp--;
    261   1.1       cgd 				putstr("\b \b");
    262   1.1       cgd 			}
    263   1.1       cgd 		} else
    264   1.1       cgd 			bell();
    265   1.1       cgd 	}
    266   1.1       cgd }
    267   1.1       cgd 
    268   1.6  christos void
    269  1.13  dholland getret(void)
    270   1.6  christos {
    271   1.1       cgd 	cgetret("");
    272   1.1       cgd }
    273   1.1       cgd 
    274   1.6  christos void
    275  1.13  dholland cgetret(const char *s)
    276   1.1       cgd {
    277   1.1       cgd 	putsym('\n');
    278   1.6  christos 	if (flags.standout)
    279   1.1       cgd 		standoutbeg();
    280   1.1       cgd 	putstr("Hit ");
    281   1.1       cgd 	putstr(flags.cbreak ? "space" : "return");
    282   1.1       cgd 	putstr(" to continue: ");
    283   1.6  christos 	if (flags.standout)
    284   1.1       cgd 		standoutend();
    285   1.1       cgd 	xwaitforspace(s);
    286   1.1       cgd }
    287   1.1       cgd 
    288   1.6  christos char            morc;		/* tell the outside world what char he used */
    289   1.1       cgd 
    290  1.13  dholland /* s = chars allowed besides space or return */
    291   1.6  christos void
    292  1.13  dholland xwaitforspace(const char *s)
    293   1.1       cgd {
    294   1.6  christos 	int             c;
    295   1.1       cgd 
    296   1.1       cgd 	morc = 0;
    297   1.1       cgd 
    298   1.6  christos 	while ((c = readchar()) != '\n') {
    299   1.6  christos 		if (flags.cbreak) {
    300   1.6  christos 			if (c == ' ')
    301   1.6  christos 				break;
    302   1.6  christos 			if (s && strchr(s, c)) {
    303   1.6  christos 				morc = c;
    304   1.6  christos 				break;
    305   1.6  christos 			}
    306   1.6  christos 			bell();
    307   1.1       cgd 		}
    308   1.1       cgd 	}
    309   1.1       cgd }
    310   1.1       cgd 
    311   1.6  christos char           *
    312  1.13  dholland parse(void)
    313   1.1       cgd {
    314   1.6  christos 	static char     inputline[COLNO];
    315   1.6  christos 	int		foo;
    316   1.1       cgd 
    317   1.1       cgd 	flags.move = 1;
    318   1.6  christos 	if (!Invisible)
    319   1.6  christos 		curs_on_u();
    320   1.6  christos 	else
    321   1.6  christos 		home();
    322   1.6  christos 	while ((foo = readchar()) >= '0' && foo <= '9')
    323   1.6  christos 		multi = 10 * multi + foo - '0';
    324   1.6  christos 	if (multi) {
    325   1.1       cgd 		multi--;
    326   1.1       cgd 		save_cm = inputline;
    327   1.1       cgd 	}
    328   1.1       cgd 	inputline[0] = foo;
    329   1.1       cgd 	inputline[1] = 0;
    330   1.6  christos 	if (foo == 'f' || foo == 'F') {
    331   1.1       cgd 		inputline[1] = getchar();
    332   1.1       cgd #ifdef QUEST
    333   1.6  christos 		if (inputline[1] == foo)
    334   1.6  christos 			inputline[2] = getchar();
    335   1.6  christos 		else
    336   1.6  christos #endif	/* QUEST */
    337   1.6  christos 			inputline[2] = 0;
    338   1.1       cgd 	}
    339   1.6  christos 	if (foo == 'm' || foo == 'M') {
    340   1.1       cgd 		inputline[1] = getchar();
    341   1.1       cgd 		inputline[2] = 0;
    342   1.1       cgd 	}
    343   1.1       cgd 	clrlin();
    344   1.6  christos 	return (inputline);
    345   1.1       cgd }
    346   1.1       cgd 
    347   1.1       cgd char
    348  1.13  dholland readchar(void)
    349   1.6  christos {
    350   1.6  christos 	int             sym;
    351   1.1       cgd 
    352   1.1       cgd 	(void) fflush(stdout);
    353   1.6  christos 	if ((sym = getchar()) == EOF)
    354   1.1       cgd #ifdef NR_OF_EOFS
    355   1.6  christos 	{			/*
    356   1.6  christos 			         * Some SYSV systems seem to return EOFs for various reasons
    357   1.6  christos 			         * (?like when one hits break or for interrupted systemcalls?),
    358   1.6  christos 			         * and we must see several before we quit.
    359   1.6  christos 			         */
    360   1.6  christos 		int             cnt = NR_OF_EOFS;
    361   1.1       cgd 		while (cnt--) {
    362   1.6  christos 			clearerr(stdin);	/* omit if clearerr is
    363   1.6  christos 						 * undefined */
    364   1.6  christos 			if ((sym = getchar()) != EOF)
    365   1.6  christos 				goto noteof;
    366   1.1       cgd 		}
    367   1.1       cgd 		end_of_input();
    368   1.6  christos noteof:	;
    369   1.1       cgd 	}
    370   1.1       cgd #else
    371   1.1       cgd 		end_of_input();
    372   1.6  christos #endif	/* NR_OF_EOFS */
    373   1.6  christos 	if (flags.toplin == 1)
    374   1.1       cgd 		flags.toplin = 2;
    375   1.6  christos 	return ((char) sym);
    376   1.1       cgd }
    377   1.1       cgd 
    378   1.6  christos void
    379  1.13  dholland end_of_input(void)
    380   1.1       cgd {
    381   1.1       cgd 	settty("End of input?\n");
    382   1.1       cgd 	clearlocks();
    383   1.1       cgd 	exit(0);
    384   1.1       cgd }
    385