Source:NetHack 3.0.0/getline.c

From NetHackWiki
Revision as of 04:40, 4 March 2008 by Kernigh bot (talk | contribs) (NetHack 3.0.0/getline.c moved to Source:NetHack 3.0.0/getline.c: Robot: moved page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Below is the full text to getline.c from the source code of NetHack 3.0.0. To link to a particular line, write [[NetHack 3.0.0/getline.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: @(#)getline.c	3.0	89/06/16
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    #include "hack.h"
6.    #include "func_tab.h"
7.    
8.    /*
9.     * Some systems may have getchar() return EOF for various reasons, and
10.    * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
11.    */
12.   #ifdef SYSV
13.   #define	NR_OF_EOFS	20
14.   #endif
15.   
16.   char morc = 0;	/* tell the outside world what char he used */
17.   
18.   extern char erase_char, kill_char;	/* from appropriate tty.c file */
19.   
20.   /*
21.    * Read a line closed with '\n' into the array char bufp[BUFSZ].
22.    * (The '\n' is not stored. The string is closed with a '\0'.)
23.    * Reading can be interrupted by an escape ('\033') - now the
24.    * resulting string is "\033".
25.    */
26.   void
27.   getlin(bufp)
28.   register char *bufp;
29.   {
30.   	register char *obufp = bufp;
31.   	register int c;
32.   
33.   	flags.toplin = 2;		/* nonempty, no --More-- required */
34.   	for(;;) {
35.   		(void) fflush(stdout);
36.   		if((c = Getchar()) == EOF) {
37.   			*bufp = 0;
38.   			return;
39.   		}
40.   		if(c == '\033') {
41.   			*obufp = c;
42.   			obufp[1] = 0;
43.   			return;
44.   		}
45.   		if(c == erase_char || c == '\b') {
46.   			if(bufp != obufp) {
47.   				bufp--;
48.   				putstr("\b \b");/* putsym converts \b */
49.   			} else	bell();
50.   		} else if(c == '\n') {
51.   			*bufp = 0;
52.   			return;
53.   		} else if(' ' <= c && c < '\177' && 
54.   			    (bufp-obufp < BUFSZ-1 || bufp-obufp < COLNO)) {
55.   				/* avoid isprint() - some people don't have it
56.   				   ' ' is not always a printing char */
57.   			*bufp = c;
58.   			bufp[1] = 0;
59.   			putstr(bufp);
60.   			bufp++;
61.   		} else if(c == kill_char || c == '\177') { /* Robert Viduya */
62.   				/* this test last - @ might be the kill_char */
63.   			while(bufp != obufp) {
64.   				bufp--;
65.   				if(curx == 1 && cury > 1) {
66.   					putstr("\b \b\b");
67.   					curx = CO;
68.   				} else putstr("\b \b");
69.   			}
70.   		} else
71.   			bell();
72.   	}
73.   }
74.   
75.   void
76.   getret() {
77.   	cgetret("");
78.   }
79.   
80.   void
81.   cgetret(s)
82.   register char *s;
83.   {
84.   	putsym('\n');
85.   	if(flags.standout)
86.   		standoutbeg();
87.   	putstr("Hit ");
88.   	putstr(flags.cbreak ? "space" : "return");
89.   	putstr(" to continue: ");
90.   	if(flags.standout)
91.   		standoutend();
92.   	xwaitforspace(s);
93.   }
94.   
95.   
96.   void
97.   xwaitforspace(s)
98.   register char *s;	/* chars allowed besides space or return */
99.   {
100.  	register int c;
101.  
102.  	morc = 0;
103.  
104.  	while((c = readchar()) != '\n') {
105.  	    if(flags.cbreak) {
106.  		if(c == ' ') break;
107.  		if(s && index(s,c)) {
108.  			morc = c;
109.  			break;
110.  		}
111.  		bell();
112.  	    }
113.  	}
114.  }
115.  
116.  static int last_multi;
117.  
118.  char *
119.  parse()
120.  {
121.  #ifdef LINT	/* static char in_line[COLNO]; */
122.  	char in_line[COLNO];
123.  #else
124.  	static char in_line[COLNO];
125.  #endif
126.  	register int foo;
127.  
128.  	multi = 0;
129.  	flags.move = 1;
130.  	curs_on_u();
131.  
132.  	if (!flags.num_pad || (foo = readchar()) == 'n')
133.  	    while((foo = readchar()) >= '0' && foo <= '9') {
134.  		multi = 10*multi+foo-'0';
135.  		if (multi < 0 || multi > LARGEST_INT)
136.  			multi = LARGEST_INT;
137.  		if (multi > 9) {
138.  			remember_topl();
139.  			home();
140.  			cl_end();
141.  			Printf("Count: %d", multi);
142.  		}
143.  		last_multi = multi;
144.  	    }
145.  # ifdef REDO
146.  	if (foo == DOAGAIN || in_doagain)
147.  		multi = last_multi;
148.  	else {
149.  		savech(0);	/* reset input queue */
150.  		savech(foo);
151.  	}
152.  # endif
153.  	if(multi) {
154.  		multi--;
155.  		save_cm = in_line;
156.  	}
157.  	in_line[0] = foo;
158.  	in_line[1] = 0;
159.  	if(foo == 'g' || foo == 'G'){
160.  		in_line[1] = Getchar();
161.  #ifdef REDO
162.  		savech(in_line[1]);
163.  #endif
164.  		in_line[2] = 0;
165.  	}
166.  	if(foo == 'm' || foo == 'M'){
167.  		in_line[1] = Getchar();
168.  #ifdef REDO
169.  		savech(in_line[1]);
170.  #endif
171.  		in_line[2] = 0;
172.  	}
173.  	clrlin();
174.  	return(in_line);
175.  }
176.  
177.  #ifdef UNIX
178.  static void
179.  end_of_input()
180.  {
181.  	settty("End of input?\n");
182.  	clearlocks();
183.  	exit(0);
184.  }
185.  #endif
186.  
187.  char
188.  readchar() {
189.  	register int sym;
190.  
191.  	(void) fflush(stdout);
192.  #ifdef UNIX
193.  	if((sym = Getchar()) == EOF)
194.  # ifdef NR_OF_EOFS
195.  	{ /*
196.  	   * Some SYSV systems seem to return EOFs for various reasons
197.  	   * (?like when one hits break or for interrupted systemcalls?),
198.  	   * and we must see several before we quit.
199.  	   */
200.  		register int cnt = NR_OF_EOFS;
201.  		while (cnt--) {
202.  		    clearerr(stdin);	/* omit if clearerr is undefined */
203.  		    if((sym = Getchar()) != EOF) goto noteof;
204.  		}
205.  		end_of_input();
206.  	     noteof:	;
207.  	}
208.  # else
209.  		end_of_input();
210.  # endif /* NR_OF_EOFS /**/
211.  #else
212.  	sym = Getchar();
213.  #endif /* UNIX */
214.  	if(flags.toplin == 1)
215.  		flags.toplin = 2;
216.  	return((char) sym);
217.  }
218.  
219.  #ifdef COM_COMPL
220.  /* Read in an extended command - doing command line completion for
221.   * when enough characters have been entered to make a unique command.
222.   * This is just a modified getlin().   -jsb
223.   */
224.  void
225.  get_ext_cmd(bufp)
226.  register char *bufp;
227.  {
228.  	register char *obufp = bufp;
229.  	register int c;
230.  	int com_index, oindex;
231.  
232.  	flags.toplin = 2;		/* nonempty, no --More-- required */
233.  
234.  	for(;;) {
235.  		(void) fflush(stdout);
236.  		if((c = readchar()) == EOF) {
237.  			*bufp = 0;
238.  			return;
239.  		}
240.  		if(c == '\033') {
241.  			*obufp = c;
242.  			obufp[1] = 0;
243.  			return;
244.  		}
245.  		if(c == erase_char || c == '\b') {
246.  			if(bufp != obufp) {
247.  				bufp--;
248.  				putstr("\b \b"); /* putsym converts \b */
249.  			} else	bell();
250.  		} else if(c == '\n') {
251.  			*bufp = 0;
252.  			return;
253.  		} else if(' ' <= c && c < '\177') {
254.  				/* avoid isprint() - some people don't have it
255.  				   ' ' is not always a printing char */
256.  			*bufp = c;
257.  			bufp[1] = 0;
258.  			oindex = 0;
259.  			com_index = -1;
260.  
261.  			while(extcmdlist[oindex].ef_txt != NULL){
262.  				if(!strncmp(obufp, extcmdlist[oindex].ef_txt,
263.  				    strlen(obufp)))
264.  					if(com_index == -1) /* No matches yet*/
265.  					    com_index = oindex;
266.  					else /* More than 1 match */
267.  					    com_index = -2;
268.  				oindex++;
269.  			}
270.  			if(com_index >= 0){
271.  				Strcpy(obufp, extcmdlist[com_index].ef_txt);
272.  				/* finish printing our string */
273.  				putstr(bufp);
274.  				bufp = obufp; /* reset it */
275.  				if(strlen(obufp) < BUFSIZ-1 &&
276.  				 strlen(obufp) < COLNO)
277.  					/* set bufp at the end of our string */
278.  					bufp += strlen(obufp);
279.  			} else {
280.  				putstr(bufp);
281.  				if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
282.  					bufp++;
283.  			}
284.  		} else if(c == kill_char || c == '\177') { /* Robert Viduya */
285.  				/* this test last - @ might be the kill_char */
286.  			while(bufp != obufp) {
287.  				bufp--;
288.  				putstr("\b \b");
289.  			}
290.  		} else
291.  			bell();
292.  	}
293.  
294.  }
295.  #endif /* COM_COMPL */