Source:Hack 1.0/hack.termcap.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to hack.termcap.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.termcap.c#line123]], for example.

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

Screenshots and source code from Hack are used under the CWI license.

1.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2.    
3.    #include <stdio.h>
4.    #include "config.h"	/* for ROWNO and COLNO */
5.    extern char *tgetstr(), *tgoto(), *getenv();
6.    extern long *alloc();
7.    
8.    short ospeed;		/* terminal baudrate; used by tputs */
9.    char tbuf[512];
10.   char *HO, *CL, *CE, *UP, *CM, *ND, *XD, *BC, *SO, *SE;
11.   char PC = '\0';
12.   
13.   startup()
14.   {
15.   	register char *tmp;
16.   	register char *tptr;
17.   	char *tbufptr, *pc;
18.   
19.   	gettty();	/* sets ospeed */
20.   
21.   	tptr = (char *) alloc(1024);
22.   
23.   	tbufptr = tbuf;
24.   	if(!(tmp = getenv("TERM")))
25.   		error("Can't get TERM.");
26.   	if(tgetent(tptr,tmp) < 1)
27.   		error("Unknown terminal type: %s.", tmp);
28.   	if(pc = tgetstr("pc",&tbufptr))
29.   		PC = *pc;
30.   	if(!(BC = tgetstr("bc",&tbufptr))) {	
31.   		if(!tgetflag("bs"))
32.   			error("Terminal must backspace.");
33.   		BC = tbufptr;
34.   		tbufptr += 2;
35.   		*BC = '\b';
36.   	}
37.   	HO = tgetstr("ho", &tbufptr);
38.   	if(tgetnum("co") < COLNO || tgetnum("li") < ROWNO+2)
39.   		error("Screen must be at least %d by %d!",
40.   			ROWNO+2, COLNO);
41.   	if(!(CL = tgetstr("cl",&tbufptr)) || !(CE = tgetstr("ce",&tbufptr)) ||
42.   		!(ND = tgetstr("nd", &tbufptr)) ||
43.   		!(UP = tgetstr("up",&tbufptr)) || tgetflag("os"))
44.   		error("Hack needs CL, CE, UP, ND, and no OS.");
45.   	if(!(CM = tgetstr("cm",&tbufptr)))
46.   		printf("Use of hack on terminals without CM is suspect...\n");
47.   	XD = tgetstr("xd",&tbufptr);
48.   	SO = tgetstr("so",&tbufptr);
49.   	SE = tgetstr("se",&tbufptr);
50.   	if(!SO || !SE) SO = SE = 0;
51.   	if(tbufptr-tbuf > sizeof(tbuf)) error("TERMCAP entry too big...\n");
52.   	free(tptr);
53.   }
54.   
55.   /* Cursor movements */
56.   extern xchar curx, cury;
57.   
58.   curs(x,y)
59.   register int x,y;	/* not xchar: perhaps xchar is unsigned and
60.   			   curx-x would be unsigned as well */
61.   {
62.   
63.   	if (y == cury && x == curx) return;
64.   	if(abs(cury-y)<= 3 && abs(curx-x)<= 3) nocmov(x,y);
65.   	else if((x <= 3 && abs(cury-y)<= 3) || (!CM && x<abs(curx-x))) {
66.   		(void) putchar('\r');
67.   		curx = 1;
68.   		nocmov(x,y);
69.   	} else if(!CM) nocmov(x,y);
70.    else cmov(x,y);
71.   }
72.   
73.   nocmov(x,y)
74.   {
75.   	if (curx < x) {		/* Go to the right. */
76.   		while (curx < x) {
77.   			xputs(ND);
78.   			curx++;
79.   		}
80.   	} else if (curx > x) {
81.   		while (curx > x) {	/* Go to the left. */
82.   			xputs(BC);
83.   			curx--;
84.   		}
85.   	}
86.   	if (cury > y) {
87.   		if(UP) {
88.   			while (cury > y) {	/* Go up. */
89.   				xputs(UP);
90.   				cury--;
91.   			}
92.   		} else cmov(x,y);
93.   	} else if (cury < y) {
94.   		if(XD) {
95.   			while(cury<y) {
96.   				xputs(XD);
97.   				cury++;
98.   			}
99.   		} else cmov(x,y);
100.  	}
101.  }
102.  
103.  cmov(x,y)
104.  register x,y;
105.  {
106.  	if(!CM) error("Tries to cmov from %d %d to %d %d\n",curx,cury,x,y);
107.  	xputs(tgoto(CM,x-1,y-1));
108.  	cury = y;
109.  	curx = x;
110.  }
111.  
112.  xputc(c) char c; {
113.  	(void) fputc(c,stdout);
114.  }
115.  
116.  xputs(s) char *s; {
117.  	tputs(s, 1, xputc);
118.  }
119.  
120.  cl_end() {
121.  	xputs(CE);
122.  }
123.  
124.  clear_screen() {
125.  	xputs(CL);
126.  	curx = cury = 1;
127.  }
128.  
129.  home()
130.  {
131.  	if(HO) xputs(HO);
132.  	else xputs(tgoto(CM,0,0));
133.  	curx = cury = 1;
134.  }
135.  
136.  standoutbeg()
137.  {
138.  	if(SO) xputs(SO);
139.  }
140.  
141.  standoutend()
142.  {
143.  	if(SE) xputs(SE);
144.  }
145.  
146.  backsp()
147.  {
148.  	xputs(BC);
149.  	curx--;
150.  }
151.  
152.  bell()
153.  {
154.  	putsym('\007');
155.  }
156.  
157.  delay_output() {
158.  	/* delay 40 ms - could also use a 'nap'-system call */
159.  	tputs("40", 1, xputc);
160.  }