Source:NetHack 3.0.0/unixtty.c

From NetHackWiki
(Redirected from NetHack 3.0.0/unixtty.c)
Jump to navigation Jump to search

Below is the full text to unixtty.c from the source code of NetHack 3.0.0. To link to a particular line, write [[NetHack 3.0.0/unixtty.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1.    /*	SCCS Id: @(#)unixtty.c	3.0	88/05/03
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    /* tty.c - (Unix) version */
5.    
6.    /* With thanks to the people who sent code for SYSV - hpscdi!jon,
7.     * arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others.
8.     */
9.    
10.   /* block some unused #defines to avoid overloading some cpp's */
11.   #define MONATTK_H
12.   #define ONAMES_H
13.   #include "hack.h"
14.   
15.   /*
16.    * The distinctions here are not BSD - rest but rather USG - rest, as
17.    * BSD still has the old sgttyb structure, but SYSV has termio. Thus:
18.    */
19.   #if defined(BSD) || defined(ULTRIX)
20.   #define	V7
21.   #else
22.   #define USG
23.   #endif
24.   
25.   
26.   #ifdef USG
27.   
28.   #include	<termio.h>
29.   #define termstruct	termio
30.   #define kill_sym	c_cc[VKILL]
31.   #define erase_sym	c_cc[VERASE]
32.   #define intr_sym	c_cc[VINTR]
33.   #define EXTABS		TAB3
34.   #define tabflgs		c_oflag
35.   #define echoflgs	c_lflag
36.   #define cbrkflgs	c_lflag
37.   #define CBRKMASK	ICANON
38.   #define CBRKON		! /* reverse condition */
39.   #define OSPEED(x)	((x).c_cflag & CBAUD)
40.   #define GTTY(x)		(ioctl(0, TCGETA, x))
41.   /* STTY now modified to run under Sys V R3.	- may have to be #ifdef'ed */
42.   #define STTY(x)		(ioctl(0, TCSETAW, x))	/* TCSETAF? TCSETAW? */
43.   #define GTTY2(x)	1
44.   #define STTY2(x)	1
45.   #define nonesuch	0
46.   #define inittyb2	inittyb
47.   #define curttyb2	curttyb
48.   
49.   #else	/* V7 */
50.   
51.   #include	<sgtty.h>
52.   #define termstruct	sgttyb
53.   #define	kill_sym	sg_kill
54.   #define	erase_sym	sg_erase
55.   #define	intr_sym	t_intrc
56.   #define EXTABS		XTABS
57.   #define tabflgs		sg_flags
58.   #define echoflgs	sg_flags
59.   #define cbrkflgs	sg_flags
60.   #define CBRKMASK	CBREAK
61.   #define CBRKON		/* empty */
62.   #define OSPEED(x)	(x).sg_ospeed
63.   #define GTTY(x)		(gtty(0, x))
64.   #define STTY(x)		(stty(0, x))
65.   #define GTTY2(x)	(ioctl(0, TIOCGETC, (char *)x))
66.   #define STTY2(x)	(ioctl(0, TIOCSETC, (char *)x))
67.   #define nonesuch	-1
68.   struct tchars inittyb2, curttyb2;
69.   
70.   #endif
71.   
72.   extern short ospeed;
73.   char erase_char, intr_char, kill_char;
74.   static boolean settty_needed = FALSE;
75.   struct termstruct inittyb, curttyb;
76.   
77.   static void
78.   setctty(){
79.   	if(STTY(&curttyb) < 0 || STTY2(&curttyb2) < 0)
80.   		perror("NetHack (setctty)");
81.   }
82.   
83.   /*
84.    * Get initial state of terminal, set ospeed (for termcap routines)
85.    * and switch off tab expansion if necessary.
86.    * Called by startup() in termcap.c and after returning from ! or ^Z
87.    */
88.   void
89.   gettty(){
90.   	if(GTTY(&inittyb) < 0 || GTTY2(&inittyb2) < 0)
91.   		perror("NetHack (gettty)");
92.   	curttyb = inittyb;
93.   	curttyb2 = inittyb2;
94.   	ospeed = OSPEED(inittyb);
95.   	erase_char = inittyb.erase_sym;
96.   	kill_char = inittyb.kill_sym;
97.   	intr_char = inittyb2.intr_sym;
98.   	getioctls();
99.   
100.  	/* do not expand tabs - they might be needed inside a cm sequence */
101.  	if(curttyb.tabflgs & EXTABS) {
102.  		curttyb.tabflgs &= ~EXTABS;
103.  		setctty();
104.  	}
105.  	settty_needed = TRUE;
106.  }
107.  
108.  /* reset terminal to original state */
109.  void
110.  settty(s)
111.  char *s;
112.  {
113.  	clear_screen();
114.  	end_screen();
115.  	if(s) Printf(s);
116.  	(void) fflush(stdout);
117.  	if(STTY(&inittyb) < 0 || STTY2(&inittyb2) < 0)
118.  		perror("NetHack (settty)");
119.  	flags.echo = (inittyb.echoflgs & ECHO) ? ON : OFF;
120.  	flags.cbreak = (CBRKON(inittyb.cbrkflgs & CBRKMASK)) ? ON : OFF;
121.  	setioctls();
122.  }
123.  
124.  void
125.  setftty(){
126.  register int ef = 0;			/* desired value of flags & ECHO */
127.  #ifdef LINT	/* cf = CBRKON(CBRKMASK); const expr to initialize is ok */
128.  register int cf = 0;
129.  #else
130.  register int cf = CBRKON(CBRKMASK);	/* desired value of flags & CBREAK */
131.  #endif
132.  register int change = 0;
133.  	flags.cbreak = ON;
134.  	flags.echo = OFF;
135.  	/* Should use (ECHO|CRMOD) here instead of ECHO */
136.  	if((curttyb.echoflgs & ECHO) != ef){
137.  		curttyb.echoflgs &= ~ECHO;
138.  /*		curttyb.echoflgs |= ef;					*/
139.  		change++;
140.  	}
141.  	if((curttyb.cbrkflgs & CBRKMASK) != cf){
142.  		curttyb.cbrkflgs &= ~CBRKMASK;
143.  		curttyb.cbrkflgs |= cf;
144.  #ifdef USG
145.  		/* be satisfied with one character; no timeout */
146.  		curttyb.c_cc[VMIN] = 1;		/* was VEOF */
147.  		curttyb.c_cc[VTIME] = 0;	/* was VEOL */
148.  #endif
149.  		change++;
150.  	}
151.  	/* If an interrupt character is used, it will be overriden and
152.  	 * set to ^C.
153.  	 */
154.  	if(intr_char != nonesuch && curttyb2.intr_sym != '\003') {
155.  	    curttyb2.intr_sym = '\003';
156.  	    change++;
157.  	}
158.  
159.  	if(change) setctty();
160.  	start_screen();
161.  }
162.  
163.  void
164.  intron() {		/* enable kbd interupts if enabled when game started */
165.  
166.  	if(intr_char != nonesuch && curttyb2.intr_sym != '\003') {
167.  	    curttyb2.intr_sym = '\003';
168.  	    setctty();
169.  	}
170.  }
171.  
172.  void
173.  introff() {		/* disable kbd interrupts if required*/
174.  
175.  	if(curttyb2.intr_sym != nonesuch) {
176.  	    curttyb2.intr_sym = nonesuch;
177.  	    setctty();
178.  	}
179.  }
180.  
181.  
182.  /* fatal error */
183.  /*VARARGS1*/
184.  void
185.  error(s, x, y)
186.  char *s, *x, *y;
187.  {
188.  	if(settty_needed)
189.  		settty(NULL);
190.  	Printf(s,x,y);
191.  	(void) putchar('\n');
192.  	exit(1);
193.  }