-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinout.c
executable file
·387 lines (355 loc) · 8.38 KB
/
inout.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* This file is provided by the aws2c tool and it contains input/output
routines as well as the parser. It should be compiled along with the
generated .c file obtained by the tool. This file can be customised for
specific needs, for example to write on a window in a GUI system instead
of on the terminal.
Some basic configuration can also be done by adjusting the systemdef.h file
to your needs.
Davide Bucci, October 2018-January 2022
*/
#include <stdio.h>
#include <string.h>
#ifndef CONFIG_FILENAME
#include"config.h"
#else
#include CONFIG_FILENAME
#endif
#include "systemd.h"
#include "aws.h"
#include "inout.h"
char playerInput[BUFFERSIZE];
#ifdef NROW
EFFSHORTINDEX rowc;
#endif
#ifdef NCOL
EFFSHORTINDEX colc;
#endif
unsigned int verb;
unsigned int noun1;
unsigned int noun2;
#ifndef NOADVERBS
ADVERBTYPE adve;
#endif
ACTORTYPE actor;
#ifndef NOADJECTIVES
ADJECTIVETYPE adjective;
#endif
extern word dictionary[];
/* The current position in the line */
EFFSHORTINDEX ls, lc;
#ifndef NCOL
EFFSHORTINDEX opc;
#endif
#ifdef DEFINEWTR
void wtr(const char *s) FASTCALL
{
for(; *s!='\0';++s)
fputc(*s,stdout);
}
#endif
#ifdef NCOL
void zeroc(void)
{
colc=0;
}
#endif
/** Write a string without adding a newline. Process some codes to put in
evidence the text and handle the word wrapping.
*/
char c,d;
#ifndef INTERNAL_DEF
EFFSHORTINDEX pc;
#ifdef NCOL
char wordbuffer[NCOL];
#else
#define NBUF 50
char wordbuffer[NBUF];
#endif
#endif
void writesameln(const char *line) FASTCALL
{
#ifdef INTERNAL_DEF
EFFSHORTINDEX pc;
#ifdef NCOL
char wordbuffer[NCOL];
#else
#define NBUF 50
char wordbuffer[NBUF];
#endif
#endif
pc=0;
while(1){
c=*line;
++line;
d=*line;
if(c==' ' || c=='\n' || c=='\r' ||c=='\0'
#ifndef COMPRESSED
|| (c=='\\' && d=='n')
#endif
#ifndef NCOL
|| (pc==NBUF-2)
#endif
)
{
#ifndef NCOL
if (pc==NBUF-2) {
wordbuffer[pc]=c;
wordbuffer[pc+1]='\0';
opc=pc;
} else
#endif
wordbuffer[pc]='\0';
#ifdef NCOL
if(colc>=NCOL) {
NEWLINE_PUTC();
colc=pc;
#ifdef NROW
if(++rowc>NROW) {waitkey(); zeror();};
#endif
}
#endif
PUTS(wordbuffer);
if(c=='\0')
return;
pc=0;
if(c=='\n' || c=='\r'
#ifndef COMPRESSED
|| (c=='\\' && d=='n')
#endif
)
{
#ifndef COMPRESSED
if(c=='\\')
++line;
#endif
NEWLINE_PUTC();
#ifdef NCOL
zeroc();
#endif
#ifdef NROW
if(++rowc>NROW) {waitkey(); zeror();};
#endif
} else
#ifdef NCOL
if(colc<NCOL-1)
#endif
{
#ifndef NCOL
if (opc!=NBUF-2)
#endif
PUTC(' ');
#ifdef NCOL
++colc;
#endif
}
} else {
wordbuffer[pc++]=c;
#ifdef NCOL
++colc;
#endif
}
}
}
/* Clear the screen */
void clear(void)
{
cls();
#ifdef NCOL
zeroc();
#endif
#ifdef NROW
zeror();
#endif
}
#ifdef NROW
void zeror(void)
{
rowc=0;
}
#endif
/** Same as writesameln, but adds a newline at the end of the message.
*/
void writeln(const char* line) FASTCALL
{
writesameln(line);
NEWLINE_PUTC();
#ifdef NROW
if(++rowc>NROW) {waitkey(); zeror();};
#endif
#ifdef NCOL
zeroc();
#endif
}
/* 'Eat' a carriage return that may be present at the end of a string.
*/
char *eatcr(char *s) FASTCALL
{
char *os=s;
lc=0;
for(;*s!='\0';++s, ++lc)
if(*s=='\n'||*s=='\r') {
*s='\0';
break;
}
return os;
}
/** Read a line of text and return the length of the line (remove the \n char).
*/
unsigned int readln(void)
{
inputtxt();
writesameln("> ");
GETS(playerInput,BUFFERSIZE);
end_inputtxt();
normaltxt();
// remove the '\n'
eatcr(playerInput);
#ifdef NROW
zeror();
#endif
return lc;
}
/* Having this buffer as a static variable allows to avoid running into
troubles with Cc65 that needs to have very few local variables to
compile to targets such as the 6502 processor. */
#ifndef INTERNAL_DEF
char s[BUFFERSIZE];
#endif
#ifdef DICT5BIT
/* Handle 5-bit for character compressed dictionary.
The result is a null-terminated string that substitutes the original one.
*/
void compress_5bit(char *buffer)
{
char *pcomp=buffer;
unsigned int shift=0;
unsigned int c;
while((c=*buffer)!='\0') {
*(buffer++)='\0';
//c=toupper(c); // Is not needed here, as already uppercase.
c=(c-'@')&0x1F; // 'A' is encoded with 1, since 0 is the end of string
c<<=shift;
*pcomp |=c&0x00FF;
if(shift>=3)
*(++pcomp)=(c&0xFF00)>>8;
shift=(shift+5)&0x07;
}
}
#endif
#ifdef DICTHASH
/* Store the dictionary as a 3-byte hash code for each word.
The result is a 3-character string that substitutes the original one.
*/
void compress_hash(char *buffer)
{
unsigned char c;
unsigned int i=0, j=0;
#define N 3
while((c=buffer[j++])!='\0') {
#ifdef COMMODORE8BIT
if(c>=0xc1 && c<=0xdA) c^=0x80;
buffer[j-1]=c;
#endif
if(j>N) {
c^=(0xFF -i);
buffer[i]^=c;
}
if(++i>N-1) i=0;
}
if(j<2) buffer[1]=0;
if(j<3) buffer[2]=0;
}
#endif
/** Main parser.
*/
void interrogationAndAnalysis()
{
#ifdef INTERNAL_DEF
char s[BUFFERSIZE];
#endif
unsigned int i, k;
char c;
word* te;
if(ls==0) {
readln();
}
verb=0;
noun1=0;
noun2=0;
#ifndef NOADVERBS
adve=0;
#endif
actor=0;
#ifndef NOADJECTIVES
adjective=0;
#endif
while(ls<lc) {
for(k=0; ls<lc && k<BUFFERSIZE; ++ls) {
c=playerInput[ls];
if(c==' ' || c=='\'') {
++ls;
break;
}
if(c>='a' && c<='z')
c-='a'-'A'; // Convert to uppercase
s[k++]=c;
}
s[k]='\0';
#ifdef DICT5BIT
compress_5bit(s);
#endif
#ifdef DICTHASH
compress_hash(s);
#endif
// s now contains the word. Search to find
// if it is recognized or not.
for(i=0;i<DSIZE; ++i) {
te=&dictionary[i];
#ifdef DICTHASH
if( s[0]==te->c1&&
s[1]==te->c2&&
s[2]==te->c3)
{
#else
if(strcmp(s,te->w)==0) {
#endif
k=te->code;
switch (te->t) {
case VERB:
verb=k;
break;
case NAME:
if(noun1==0) {
noun1=k;
} else {
noun2=k;
}
break;
#ifndef NOADVERBS
case ADVERB:
adve=k;
break;
#endif
case ACTOR:
actor=k;
break;
#ifndef NOADJECTIVES
case ADJECTIVE:
adjective=k;
break;
#endif
case SEPARATOR:
/* The line is not finished but it can be executed. */
return;
default:
break;
}
break;
}
}
}
/* The scanning has finished because the line is complete. Read a new line
the next time the function is called */
ls=0;
//printf("verbo=%d, nome1=%d, nome2=%d\n",verb,noun1,noun2);
}