tok.c revision 1.8 1 1.8 dholland /* $NetBSD: tok.c,v 1.8 2008/02/03 20:01:24 dholland Exp $ */
2 1.5 christos
3 1.5 christos /* tok.c Larn is copyrighted 1986 by Noah Morgan. */
4 1.5 christos #include <sys/cdefs.h>
5 1.2 mycroft #ifndef lint
6 1.8 dholland __RCSID("$NetBSD: tok.c,v 1.8 2008/02/03 20:01:24 dholland Exp $");
7 1.5 christos #endif /* not lint */
8 1.2 mycroft
9 1.1 cgd #include <sys/types.h>
10 1.5 christos #include <string.h>
11 1.1 cgd #include <sys/ioctl.h>
12 1.5 christos #include <stdlib.h>
13 1.5 christos #include <unistd.h>
14 1.5 christos #include <sys/wait.h>
15 1.1 cgd #include "header.h"
16 1.5 christos #include "extern.h"
17 1.1 cgd
18 1.5 christos static char lastok = 0;
19 1.8 dholland int yrepcount = 0;
20 1.1 cgd #ifndef FLUSHNO
21 1.1 cgd #define FLUSHNO 5
22 1.5 christos #endif /* FLUSHNO */
23 1.5 christos static int flushno = FLUSHNO; /* input queue flushing threshold */
24 1.5 christos #define MAXUM 52 /* maximum number of user re-named monsters */
25 1.5 christos #define MAXMNAME 40 /* max length of a monster re-name */
26 1.5 christos static char usermonster[MAXUM][MAXMNAME]; /* the user named monster
27 1.5 christos * name goes here */
28 1.5 christos static u_char usermpoint = 0; /* the user monster pointer */
29 1.1 cgd
30 1.1 cgd /*
31 1.1 cgd lexical analyzer for larn
32 1.1 cgd */
33 1.5 christos int
34 1.1 cgd yylex()
35 1.5 christos {
36 1.5 christos char cc;
37 1.5 christos int ic;
38 1.5 christos if (hit2flag) {
39 1.5 christos hit2flag = 0;
40 1.5 christos yrepcount = 0;
41 1.5 christos return (' ');
42 1.5 christos }
43 1.5 christos if (yrepcount > 0) {
44 1.5 christos --yrepcount;
45 1.5 christos return (lastok);
46 1.5 christos } else
47 1.5 christos yrepcount = 0;
48 1.5 christos if (yrepcount == 0) {
49 1.5 christos bottomdo();
50 1.5 christos showplayer();
51 1.5 christos } /* show where the player is */
52 1.5 christos lflush();
53 1.5 christos while (1) {
54 1.1 cgd c[BYTESIN]++;
55 1.1 cgd if (ckpflag)
56 1.5 christos if ((c[BYTESIN] % 400) == 0) { /* check for periodic
57 1.5 christos * checkpointing */
58 1.1 cgd #ifndef DOCHECKPOINTS
59 1.5 christos savegame(ckpfile);
60 1.1 cgd #else
61 1.5 christos wait(0); /* wait for other forks to
62 1.5 christos * finish */
63 1.5 christos if (fork() == 0) {
64 1.5 christos savegame(ckpfile);
65 1.5 christos exit();
66 1.5 christos }
67 1.1 cgd #endif
68 1.1 cgd }
69 1.5 christos do { /* if keyboard input buffer is too big, flush
70 1.5 christos * some of it */
71 1.5 christos ioctl(0, FIONREAD, &ic);
72 1.5 christos if (ic > flushno)
73 1.5 christos read(0, &cc, 1);
74 1.5 christos }
75 1.5 christos while (ic > flushno);
76 1.5 christos
77 1.5 christos if (read(0, &cc, 1) != 1)
78 1.5 christos return (lastok = -1);
79 1.1 cgd
80 1.5 christos if (cc == 'Y' - 64) { /* control Y -- shell escape */
81 1.5 christos resetscroll();
82 1.5 christos clear();/* scrolling region, home, clear, no
83 1.5 christos * attributes */
84 1.5 christos if ((ic = fork()) == 0) { /* child */
85 1.6 mrg execl("/bin/csh", "/bin/csh", NULL);
86 1.5 christos exit(1);
87 1.1 cgd }
88 1.1 cgd wait(0);
89 1.5 christos if (ic < 0) { /* error */
90 1.5 christos write(2, "Can't fork off a shell!\n", 25);
91 1.5 christos sleep(2);
92 1.5 christos }
93 1.1 cgd setscroll();
94 1.5 christos return (lastok = 'L' - 64); /* redisplay screen */
95 1.5 christos }
96 1.5 christos if ((cc <= '9') && (cc >= '0')) {
97 1.5 christos yrepcount = yrepcount * 10 + cc - '0';
98 1.5 christos } else {
99 1.5 christos if (yrepcount > 0)
100 1.5 christos --yrepcount;
101 1.5 christos return (lastok = cc);
102 1.1 cgd }
103 1.1 cgd }
104 1.5 christos }
105 1.1 cgd
106 1.1 cgd /*
107 1.1 cgd * flushall() Function to flush all type-ahead in the input buffer
108 1.1 cgd */
109 1.5 christos void
110 1.1 cgd flushall()
111 1.5 christos {
112 1.5 christos char cc;
113 1.5 christos int ic;
114 1.5 christos for (;;) { /* if keyboard input buffer is too big, flush
115 1.5 christos * some of it */
116 1.5 christos ioctl(0, FIONREAD, &ic);
117 1.5 christos if (ic <= 0)
118 1.5 christos return;
119 1.5 christos while (ic > 0) {
120 1.5 christos read(0, &cc, 1);
121 1.5 christos --ic;
122 1.5 christos } /* gobble up the byte */
123 1.1 cgd }
124 1.5 christos }
125 1.1 cgd
126 1.1 cgd /*
127 1.5 christos function to set the desired hardness
128 1.1 cgd enter with hard= -1 for default hardness, else any desired hardness
129 1.1 cgd */
130 1.5 christos void
131 1.1 cgd sethard(hard)
132 1.5 christos int hard;
133 1.5 christos {
134 1.5 christos int j, k, i;
135 1.5 christos j = c[HARDGAME];
136 1.5 christos hashewon();
137 1.5 christos if (restorflag == 0) { /* don't set c[HARDGAME] if restoring game */
138 1.5 christos if (hard >= 0)
139 1.5 christos c[HARDGAME] = hard;
140 1.5 christos } else
141 1.5 christos c[HARDGAME] = j;/* set c[HARDGAME] to proper value if
142 1.5 christos * restoring game */
143 1.5 christos
144 1.5 christos if ((k = c[HARDGAME]) != 0)
145 1.5 christos for (j = 0; j <= MAXMONST + 8; j++) {
146 1.5 christos i = ((6 + k) * monster[j].hitpoints + 1) / 6;
147 1.5 christos monster[j].hitpoints = (i < 0) ? 32767 : i;
148 1.5 christos i = ((6 + k) * monster[j].damage + 1) / 5;
149 1.5 christos monster[j].damage = (i > 127) ? 127 : i;
150 1.5 christos i = (10 * monster[j].gold) / (10 + k);
151 1.5 christos monster[j].gold = (i > 32767) ? 32767 : i;
152 1.5 christos i = monster[j].armorclass - k;
153 1.5 christos monster[j].armorclass = (i < -127) ? -127 : i;
154 1.5 christos i = (7 * monster[j].experience) / (7 + k) + 1;
155 1.5 christos monster[j].experience = (i <= 0) ? 1 : i;
156 1.1 cgd }
157 1.5 christos }
158 1.1 cgd
159 1.1 cgd /*
160 1.1 cgd function to read and process the larn options file
161 1.1 cgd */
162 1.5 christos void
163 1.1 cgd readopts()
164 1.5 christos {
165 1.7 dholland const char *i;
166 1.5 christos int j, k;
167 1.5 christos int flag;
168 1.5 christos flag = 1; /* set to 0 if he specifies a name for his
169 1.5 christos * character */
170 1.5 christos if (lopen(optsfile) < 0) {
171 1.5 christos strcpy(logname, loginname);
172 1.5 christos return; /* user name if no character name */
173 1.5 christos }
174 1.1 cgd i = " ";
175 1.5 christos while (*i) {
176 1.7 dholland if ((i = lgetw()) == NULL)
177 1.5 christos break; /* check for EOF */
178 1.5 christos while ((*i == ' ') || (*i == '\t'))
179 1.5 christos i++; /* eat leading whitespace */
180 1.5 christos switch (*i) {
181 1.5 christos case 'b':
182 1.5 christos if (strcmp(i, "bold-objects") == 0)
183 1.5 christos boldon = 1;
184 1.5 christos break;
185 1.5 christos
186 1.5 christos case 'e':
187 1.5 christos if (strcmp(i, "enable-checkpointing") == 0)
188 1.5 christos ckpflag = 1;
189 1.5 christos break;
190 1.5 christos
191 1.5 christos case 'i':
192 1.5 christos if (strcmp(i, "inverse-objects") == 0)
193 1.5 christos boldon = 0;
194 1.5 christos break;
195 1.5 christos
196 1.5 christos case 'f':
197 1.5 christos if (strcmp(i, "female") == 0)
198 1.5 christos sex = 0; /* male or female */
199 1.5 christos break;
200 1.5 christos
201 1.5 christos case 'm':
202 1.5 christos if (strcmp(i, "monster:") == 0) { /* name favorite monster */
203 1.5 christos if ((i = lgetw()) == 0)
204 1.1 cgd break;
205 1.7 dholland strlcpy(usermonster[usermpoint], i, MAXMNAME);
206 1.5 christos if (usermpoint >= MAXUM)
207 1.5 christos break; /* defined all of em */
208 1.5 christos if (isalpha(j = usermonster[usermpoint][0])) {
209 1.5 christos for (k = 1; k < MAXMONST + 8; k++) /* find monster */
210 1.5 christos if (monstnamelist[k] == j) {
211 1.5 christos monster[k].name = &usermonster[usermpoint++][0];
212 1.5 christos break;
213 1.1 cgd }
214 1.5 christos }
215 1.5 christos } else if (strcmp(i, "male") == 0)
216 1.5 christos sex = 1;
217 1.5 christos break;
218 1.5 christos
219 1.5 christos case 'n':
220 1.5 christos if (strcmp(i, "name:") == 0) { /* defining players name */
221 1.5 christos if ((i = lgetw()) == 0)
222 1.1 cgd break;
223 1.7 dholland strlcpy(logname, i, LOGNAMESIZE);
224 1.5 christos flag = 0;
225 1.5 christos } else if (strcmp(i, "no-introduction") == 0)
226 1.5 christos nowelcome = 1;
227 1.5 christos else if (strcmp(i, "no-beep") == 0)
228 1.5 christos nobeep = 1;
229 1.5 christos break;
230 1.5 christos
231 1.5 christos case 'p':
232 1.5 christos if (strcmp(i, "process-name:") == 0) {
233 1.5 christos if ((i = lgetw()) == 0)
234 1.1 cgd break;
235 1.7 dholland strlcpy(psname, i, PSNAMESIZE);
236 1.8 dholland } else if (strcmp(i, "play-day-play") == 0) {
237 1.8 dholland /* bypass time restrictions: ignored */
238 1.8 dholland }
239 1.5 christos break;
240 1.5 christos
241 1.5 christos case 's':
242 1.5 christos if (strcmp(i, "savefile:") == 0) { /* defining savefilename */
243 1.5 christos if ((i = lgetw()) == 0)
244 1.1 cgd break;
245 1.5 christos strcpy(savefilename, i);
246 1.5 christos flag = 0;
247 1.5 christos }
248 1.5 christos break;
249 1.1 cgd };
250 1.1 cgd }
251 1.5 christos if (flag)
252 1.5 christos strcpy(logname, loginname);
253 1.5 christos }
254