| 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 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
#include "common.h" |
| 28 |
#include "qxw.h" |
| 29 |
#include "filler.h" |
| 30 |
#include "alphabets.h" |
| 31 |
#include "dicts.h" |
| 32 |
#include "draw.h" |
| 33 |
#include "treatment.h" |
| 34 |
|
| 35 |
static FILE*dkfp=0; |
| 36 |
static int line; |
| 37 |
#define DBUFSZ 10000 |
| 38 |
char dbuf[DBUFSZ]; |
| 39 |
#define DELIMS " \t\r\n" |
| 40 |
#define DELIMEOL "\r\n" |
| 41 |
|
| 42 |
#define ENHTABSZ 4096 |
| 43 |
#define MAXENAMELEN 31 |
| 44 |
|
| 45 |
static int enhtab[ENHTABSZ]; |
| 46 |
|
| 47 |
|
| 48 |
struct dkentry { |
| 49 |
char name[MAXENAMELEN+1]; |
| 50 |
ABM flbm; |
| 51 |
int hashlink; |
| 52 |
}*dkentries=0; |
| 53 |
int entsalloc=0; |
| 54 |
|
| 55 |
struct dkword { |
| 56 |
int nent; |
| 57 |
struct lprop prop; |
| 58 |
int entries[MXFL]; |
| 59 |
}*dkwords=0; |
| 60 |
int wordsalloc=0; |
| 61 |
|
| 62 |
static int dknw,dkne; |
| 63 |
static int dspec; |
| 64 |
|
| 65 |
static void batcherr(char*s) { |
| 66 |
if(line) fprintf(stderr,"Error at line %d: %s\n",line,s); |
| 67 |
else fprintf(stderr,"Error: %s\n",s); |
| 68 |
} |
| 69 |
|
| 70 |
static void batchwarn(char*s) { |
| 71 |
fprintf(stderr,"Warning: %s",s); |
| 72 |
if(line) fprintf(stderr," at line %d of deck\n",line); |
| 73 |
fprintf(stderr,"\n"); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
static int cmdcmp(char*s,char*c0,char*c1) { |
| 78 |
if(s[0]!='.') return 1; |
| 79 |
if(!STRCASECMP(s+1,c0)) return 0; |
| 80 |
if(c1&&!STRCASECMP(s+1,c1)) return 0; |
| 81 |
return 2; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
static int isvalidename(char*t) { |
| 86 |
int i; |
| 87 |
if(!t) return 0; |
| 88 |
if(strlen(t)<1||strlen(t)>MAXENAMELEN) return 0; |
| 89 |
for(i=0;t[i];i++) { |
| 90 |
if(isalnum((unsigned char)t[i])) continue; |
| 91 |
if(t[i]=='$') continue; |
| 92 |
if(t[i]=='_') continue; |
| 93 |
return 0; |
| 94 |
} |
| 95 |
return 1; |
| 96 |
} |
| 97 |
|
| 98 |
static void initenhtab() {int i; for(i=0;i<ENHTABSZ;i++) enhtab[i]=-1; } |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static int findent(char*t) { |
| 103 |
unsigned int h; |
| 104 |
int i,n; |
| 105 |
void*p; |
| 106 |
|
| 107 |
for(i=0,h=0;t[i];i++) h=h*113+t[i]; |
| 108 |
h=h%ENHTABSZ; |
| 109 |
n=enhtab[h]; |
| 110 |
while(n!=-1) { |
| 111 |
if(!strcmp(dkentries[n].name,t)) return n; |
| 112 |
n=dkentries[n].hashlink; |
| 113 |
} |
| 114 |
if(entsalloc<dkne+1) { |
| 115 |
entsalloc=entsalloc*2+1000; |
| 116 |
p=realloc(dkentries,sizeof(struct dkentry)*entsalloc); |
| 117 |
if(!p) {batcherr("Out of memory"); return -1;} |
| 118 |
dkentries=p; |
| 119 |
} |
| 120 |
strcpy(dkentries[dkne].name,t); |
| 121 |
dkentries[dkne].flbm=ABM_ALL; |
| 122 |
dkentries[dkne].hashlink=enhtab[h]; |
| 123 |
enhtab[h]=dkne; |
| 124 |
return dkne++; |
| 125 |
} |
| 126 |
|
| 127 |
static int newword() { |
| 128 |
void*p; |
| 129 |
if(wordsalloc<dknw+1) { |
| 130 |
wordsalloc=wordsalloc*2+1000; |
| 131 |
p=realloc(dkwords,sizeof(struct dkword)*wordsalloc); |
| 132 |
if(!p) {batcherr("Out of memory"); return -1;} |
| 133 |
dkwords=p; |
| 134 |
} |
| 135 |
memset(dkwords+dknw,0,sizeof(struct dkword)); |
| 136 |
return dknw++; |
| 137 |
} |
| 138 |
|
| 139 |
static int readblock(int depth,int dmask,int emask,int ten) { |
| 140 |
char*tok; |
| 141 |
int d,e,i,rc,u,w; |
| 142 |
#define FIRSTTOK tok=strtok(dbuf,DELIMS) |
| 143 |
#define NEXTTOK tok=strtok(0,DELIMS) |
| 144 |
#define TOKEOL tok=strtok(0,DELIMEOL); if(tok) while(isspace((unsigned char)*tok)) tok++; |
| 145 |
for(;;) { |
| 146 |
line++; |
| 147 |
if(!fgets(dbuf,DBUFSZ,dkfp)) { |
| 148 |
if(depth) {batcherr("End of file encountered within block"); return 16;} |
| 149 |
return 0; |
| 150 |
} |
| 151 |
FIRSTTOK; |
| 152 |
|
| 153 |
|
| 154 |
if(!tok) continue; |
| 155 |
if(tok[0]=='#') continue; |
| 156 |
|
| 157 |
|
| 158 |
if(!strcmp(tok,"{")) { |
| 159 |
rc=readblock(depth+1,dmask,emask,ten); |
| 160 |
if(rc) return rc; |
| 161 |
continue; |
| 162 |
} |
| 163 |
if(!strcmp(tok,"}")) { |
| 164 |
if(!depth) {batcherr("End of block encountered at top level"); return 16;} |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
#define ATSTART (dknw==0&&dkne==0) |
| 170 |
#define CHECKSTART(s) \ |
| 171 |
if(depth) {batcherr(s " encountered inside block"); return 16;} \ |
| 172 |
if(!ATSTART) {batcherr(s " must occur at beginning of deck"); return 16;} |
| 173 |
|
| 174 |
if(!cmdcmp(tok,"ALPHABET","AL")) { |
| 175 |
CHECKSTART("Alphabet directive") |
| 176 |
NEXTTOK; |
| 177 |
if(!tok) {batcherr("Syntax error in alphabet directive"); return 16;} |
| 178 |
u=initalphamapbycode(tok); |
| 179 |
if(u) {batcherr("Alphabet not recognised"); return 16;} |
| 180 |
continue; |
| 181 |
} |
| 182 |
if(!cmdcmp(tok,"DICTIONARY","DI")) { |
| 183 |
CHECKSTART("Dictionary directive") |
| 184 |
NEXTTOK; |
| 185 |
if(!tok||strlen(tok)!=1||*tok<='0'||*tok>'9') {batcherr("Dictionary number must be 1..9"); return 16;} |
| 186 |
d=*tok-'1'; |
| 187 |
TOKEOL; |
| 188 |
if(!tok) strcpy(dfnames[d],""); |
| 189 |
else strncpy(dfnames[d],tok,SLEN-1); |
| 190 |
dfnames[d][SLEN-1]=0; |
| 191 |
DEB_DE printf("dictionary %d: >%s<\n",d,dfnames[d]); |
| 192 |
dspec=1; |
| 193 |
continue; |
| 194 |
} |
| 195 |
if(!cmdcmp(tok,"FILEFILTER","FF")) { |
| 196 |
CHECKSTART("Dictionary file filter directive") |
| 197 |
NEXTTOK; |
| 198 |
if(!tok||strlen(tok)!=1||*tok<='0'||*tok>'9') {batcherr("Dictionary number must be 1..9"); return 16;} |
| 199 |
d=*tok-'1'; |
| 200 |
TOKEOL; |
| 201 |
if(!tok) strcpy(dsfilters[d],""); |
| 202 |
else strncpy(dsfilters[d],tok,SLEN-1); |
| 203 |
dsfilters[d][SLEN-1]=0; |
| 204 |
DEB_DE printf("dsfilter %d: >%s<\n",d,dsfilters[d]); |
| 205 |
dspec=1; |
| 206 |
continue; |
| 207 |
} |
| 208 |
if(!cmdcmp(tok,"ANSWERFILTER","AF")) { |
| 209 |
CHECKSTART("Dictionary answer filter directive") |
| 210 |
NEXTTOK; |
| 211 |
if(!tok||strlen(tok)!=1||*tok<='0'||*tok>'9') {batcherr("Dictionary number must be 1..9"); return 16;} |
| 212 |
d=*tok-'1'; |
| 213 |
TOKEOL; |
| 214 |
if(!tok) strcpy(dafilters[d],""); |
| 215 |
else strncpy(dafilters[d],tok,SLEN-1); |
| 216 |
dafilters[d][SLEN-1]=0; |
| 217 |
DEB_DE printf("dafilter %d: >%s<\n",d,dafilters[d]); |
| 218 |
dspec=1; |
| 219 |
continue; |
| 220 |
} |
| 221 |
if(!cmdcmp(tok,"RANDOM","RA")) { |
| 222 |
CHECKSTART("Random fill directive") |
| 223 |
NEXTTOK; |
| 224 |
if(!tok||strlen(tok)!=1||*tok<'0'||*tok>'2') {batcherr("Syntax error in random fill directive"); return 16;} |
| 225 |
afrandom=*tok-'0'; |
| 226 |
continue; |
| 227 |
} |
| 228 |
if(!cmdcmp(tok,"UNIQUE","UN")) { |
| 229 |
CHECKSTART("Unique fill directive") |
| 230 |
NEXTTOK; |
| 231 |
if(!tok||strlen(tok)!=1||*tok<'0'||*tok>'1') {batcherr("Syntax error in unique fill directive"); return 16;} |
| 232 |
afunique=*tok-'0'; |
| 233 |
continue; |
| 234 |
} |
| 235 |
if(!cmdcmp(tok,"TREATMENT","TR")) { |
| 236 |
CHECKSTART("Treatment directive") |
| 237 |
NEXTTOK; |
| 238 |
if(!tok||!isdigit((unsigned char)*tok)) {batcherr("Syntax error in treatment directive"); return 16;} |
| 239 |
u=atoi(tok); |
| 240 |
if(u<0||u>=NATREAT) {batcherr("Invalid treatment number"); return 16;} |
| 241 |
treatmode=u; |
| 242 |
continue; |
| 243 |
} |
| 244 |
if(!cmdcmp(tok,"MESSAGE","ME")) { |
| 245 |
CHECKSTART("Message directive") |
| 246 |
NEXTTOK; |
| 247 |
if(!tok||strlen(tok)!=1||*tok<'0'||*tok>='0'+NMSG) {batcherr("Syntax error in message directive"); return 16;} |
| 248 |
u=*tok-'0'; |
| 249 |
TOKEOL; |
| 250 |
if(!tok) {batcherr("Syntax error in message directive"); return 16;} |
| 251 |
strncpy(treatmsg[u],tok,sizeof(treatmsg[0])-1); |
| 252 |
treatmsg[u][sizeof(treatmsg[0])-1]=0; |
| 253 |
continue; |
| 254 |
} |
| 255 |
if(!cmdcmp(tok,"MESSAGEALLOCATE","MA")) { |
| 256 |
CHECKSTART("Message allocate directive") |
| 257 |
NEXTTOK; |
| 258 |
if(!tok||strlen(tok)!=1||*tok<'0'||*tok>='0'+NMSG) {batcherr("Syntax error in message allocate directive"); return 16;} |
| 259 |
u=*tok-'0'; |
| 260 |
NEXTTOK; |
| 261 |
if(!tok||strlen(tok)!=1||*tok<'0'||*tok>'2') {batcherr("Syntax error in message allocate directive"); return 16;} |
| 262 |
treatorder[u]=*tok-'0'; |
| 263 |
continue; |
| 264 |
} |
| 265 |
if(!cmdcmp(tok,"MESSAGECONSTRAINTS","MC")) { |
| 266 |
CHECKSTART("Message constraints directive") |
| 267 |
NEXTTOK; |
| 268 |
if(!tok||strlen(tok)!=1||*tok<'0'||*tok>='0'+NMSG) {batcherr("Syntax error in message constraints directive"); return 16;} |
| 269 |
u=*tok-'0'; |
| 270 |
TOKEOL; |
| 271 |
if(!tok) {batcherr("Syntax error in message constraints directive"); return 16;} |
| 272 |
i=strtoabms(treatcstr[u],MXFL,tok,1); |
| 273 |
for(;i<MXFL;i++) treatcstr[u][i]=ABM_ALL; |
| 274 |
continue; |
| 275 |
} |
| 276 |
if(!cmdcmp(tok,"TREATEDANSWERMUSTBEAWORD","TW")) { |
| 277 |
CHECKSTART("Word constraint directive") |
| 278 |
NEXTTOK; |
| 279 |
if(!tok||strlen(tok)!=1||*tok<'0'||*tok>'1') {batcherr("Syntax error in word constraint directive"); return 16;} |
| 280 |
tambaw=*tok-'0'; |
| 281 |
continue; |
| 282 |
} |
| 283 |
if(!cmdcmp(tok,"PLUGIN","PI")) { |
| 284 |
CHECKSTART("Plugin directive") |
| 285 |
TOKEOL; |
| 286 |
if(!tok) {batcherr("Syntax error in plugin directive"); return 16;} |
| 287 |
strncpy(tpifname,tok,SLEN-1); |
| 288 |
tpifname[SLEN-1]=0; |
| 289 |
continue; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
if(!cmdcmp(tok,"USEDICTIONARY","UD")) { |
| 294 |
NEXTTOK; |
| 295 |
if(!tok) {batcherr("Syntax error in dictionary list"); return 16;} |
| 296 |
for(dmask=0,i=0;tok[i];i++) { |
| 297 |
if(tok[i]<='0'||tok[i]>'9') {batcherr("Syntax error in dictionary list"); return 16;} |
| 298 |
dmask|=1<<(tok[i]-'1'); |
| 299 |
} |
| 300 |
if(!dmask) batchwarn("Empty dictionary list"); |
| 301 |
continue; |
| 302 |
} |
| 303 |
if(!cmdcmp(tok,"ENTRYMETHOD","EM")) { |
| 304 |
NEXTTOK; |
| 305 |
if(!tok) {batcherr("Syntax error in entry method specification"); return 16;} |
| 306 |
for(emask=0,i=0;tok[i];i++) { |
| 307 |
u=tok[i]; |
| 308 |
if(isupper((unsigned char)u)) u=tolower((unsigned char)u); |
| 309 |
switch(u) { |
| 310 |
case 'f': case '>': emask|=EM_FWD; break; |
| 311 |
case 'r': case '<': emask|=EM_REV; break; |
| 312 |
case 'c': case ')': emask|=EM_CYC; break; |
| 313 |
case 'a': case '(': emask|=EM_RCY; break; |
| 314 |
case 'j': case '@': emask|=EM_JUM; break; |
| 315 |
default: batcherr("Syntax error in entry method specification"); return 16; |
| 316 |
} |
| 317 |
} |
| 318 |
if(!emask) batchwarn("No entry methods specified"); |
| 319 |
continue; |
| 320 |
} |
| 321 |
if(!cmdcmp(tok,"TREATMENTENABLE" ,"TE")) { ten=1; continue; } |
| 322 |
if(!cmdcmp(tok,"TREATMENTDISABLE","TD")) { ten=0; continue; } |
| 323 |
|
| 324 |
if(*tok=='.') {batcherr("Unrecognised directive"); return 16;} |
| 325 |
|
| 326 |
|
| 327 |
if(isvalidename(tok)) { |
| 328 |
w=newword(); |
| 329 |
if(w<0) return 16; |
| 330 |
dkwords[w].prop.dmask=dmask; |
| 331 |
dkwords[w].prop.emask=emask; |
| 332 |
dkwords[w].prop.ten=ten; |
| 333 |
for(;;) { |
| 334 |
DEB_DE printf("word entry tok=>%s<\n",tok); |
| 335 |
e=findent(tok); |
| 336 |
if(e<0) return 16; |
| 337 |
if(dkwords[w].nent>=MXLE) {batcherr("Too many entries in word"); return 16;} |
| 338 |
dkwords[w].entries[dkwords[w].nent]=e; |
| 339 |
dkwords[w].nent++; |
| 340 |
ew0: |
| 341 |
NEXTTOK; |
| 342 |
if(!tok) break; |
| 343 |
if(*tok=='=') { |
| 344 |
ABM a[MXLE]; |
| 345 |
int l; |
| 346 |
char s[MAXENAMELEN+100]; |
| 347 |
l=strtoabms(a,MXLE,tok+1,1); |
| 348 |
if(l>dkwords[w].nent) {batcherr("Constraint too long"); return 16;} |
| 349 |
for(i=0;i<l;i++) { |
| 350 |
dkentries[dkwords[w].entries[dkwords[w].nent-l+i]].flbm&=a[i]; |
| 351 |
if(dkentries[dkwords[w].entries[dkwords[w].nent-l+i]].flbm==0) { |
| 352 |
sprintf(s,"No possible assignment to entry %s",dkentries[dkwords[w].entries[dkwords[w].nent-l+i]].name); |
| 353 |
batcherr(s); |
| 354 |
return 16; |
| 355 |
} |
| 356 |
} |
| 357 |
goto ew0; |
| 358 |
} |
| 359 |
if(!isvalidename(tok)) {batcherr("Invalid name for entry"); return 16;} |
| 360 |
} |
| 361 |
continue; |
| 362 |
} |
| 363 |
|
| 364 |
batcherr("Syntax error"); |
| 365 |
return 16; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
int loaddeck(int cldict) { |
| 371 |
int e,rc=0,w; |
| 372 |
char*p; |
| 373 |
DEB_DE printf("loaddeck() filename=%s...\n",filename); |
| 374 |
line=0; |
| 375 |
dknw=0; dkne=0; dspec=0; |
| 376 |
initenhtab(); |
| 377 |
dkfp=q_fopen(filename,"r"); |
| 378 |
if(!dkfp) {batcherr("Failed to open input deck"); return 16;} |
| 379 |
rc=readblock(0,1,1,1); |
| 380 |
fclose(dkfp); |
| 381 |
if(treatmode==TREAT_PLUGIN) { |
| 382 |
if((p=loadtpi())) { |
| 383 |
line=0; |
| 384 |
batcherr("Failed to load custom plug-in"); |
| 385 |
DEB_DE printf("%s\n",p); |
| 386 |
return 16; |
| 387 |
} |
| 388 |
} |
| 389 |
if(dspec==0&&!cldict) loaddefdicts(); |
| 390 |
else if(loaddicts(0)) return 16; |
| 391 |
|
| 392 |
initstructs(); |
| 393 |
nw=dknw; ne=dkne; |
| 394 |
|
| 395 |
words=calloc(nw+NMSG,sizeof(struct word)); |
| 396 |
if(!words) {batcherr("Out of memory"); return 16;} |
| 397 |
entries=calloc(ne+NMSG*nw,sizeof(struct entry)); |
| 398 |
if(!entries) {batcherr("Out of memory"); return 16;} |
| 399 |
|
| 400 |
for(w=0;w<nw;w++) { |
| 401 |
words[w].nent=dkwords[w].nent; |
| 402 |
words[w].wlen=dkwords[w].nent; |
| 403 |
words[w].jlen=dkwords[w].nent; |
| 404 |
for(e=0;e<dkwords[w].nent;e++) { |
| 405 |
words[w].e[e]=entries+dkwords[w].entries[e]; |
| 406 |
entries[dkwords[w].entries[e]].checking++; |
| 407 |
} |
| 408 |
words[w].wlen=dkwords[w].nent; |
| 409 |
words[w].lp=&dkwords[w].prop; |
| 410 |
if(words[w].lp->ten&&treatmode>0) ntw++; |
| 411 |
} |
| 412 |
for(e=0;e<ne;e++) { |
| 413 |
entries[e].flbm=dkentries[e].flbm; |
| 414 |
} |
| 415 |
|
| 416 |
ne0=ne; nw0=nw; |
| 417 |
addimplicitwords(); |
| 418 |
|
| 419 |
return rc; |
| 420 |
} |
| 421 |
|
| 422 |
int dumpdeck() { |
| 423 |
int h,e,w,k; |
| 424 |
char t0[MXFL*10+100]; |
| 425 |
|
| 426 |
DEB_DE { |
| 427 |
printf("dumpdeck(): dkne=%d dknw=%d\n",dkne,dknw); |
| 428 |
for(h=0;h<ENHTABSZ;h++) if(enhtab[h]>=0) printf("H%4d>%4d\n",h,enhtab[h]); |
| 429 |
for(e=0;e<dkne;e++) printf("E%3d %10s: %016llx H>%d\n",e,dkentries[e].name,dkentries[e].flbm,dkentries[e].hashlink); |
| 430 |
for(w=0;w<dknw;w++) { |
| 431 |
printf("W%2d dmask=%08x emask=%08x ten=%d:",w, |
| 432 |
dkwords[w].prop.dmask,dkwords[w].prop.emask,dkwords[w].prop.ten); |
| 433 |
for(e=0;e<dkwords[w].nent;e++) printf(" %3d",dkwords[w].entries[e]); |
| 434 |
printf("\n"); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
} |
| 443 |
DEB_DE printf("filler_status=%d\n",filler_status); |
| 444 |
if(filler_status==1) { |
| 445 |
fprintf(stderr,"No fill found\n"); |
| 446 |
return 4; |
| 447 |
} |
| 448 |
|
| 449 |
for(e=0;e<ne;e++) entries[e].flbm=entries[e].flbmh; |
| 450 |
if(filler_start(3)) { |
| 451 |
fprintf(stderr,"Internal error A\n"); |
| 452 |
return 16; |
| 453 |
} |
| 454 |
filler_wait(); |
| 455 |
|
| 456 |
DEB_DE { |
| 457 |
for(e=0;e<ne;e++) printf("E%3d flbmh=%016llx\n",e,entries[e].flbmh); |
| 458 |
} |
| 459 |
|
| 460 |
for(w=0;w<nw0;w++) { |
| 461 |
printf("W%d ",w); |
| 462 |
for(e=0;e<words[w].jlen;e++) if(dkwords[w].entries[e]<ne0) pabm(words[w].e[e]->flbmh,1); |
| 463 |
if(words[w].flistlen) { |
| 464 |
printf("\n# "); |
| 465 |
for(k=0;k<words[w].flistlen;k++) { |
| 466 |
if(k>0) printf("; "); |
| 467 |
ansform(t0,sizeof(t0),words[w].flist[k],words[w].wlen,words[w].lp->dmask); |
| 468 |
printf("%s",t0); |
| 469 |
} |
| 470 |
} |
| 471 |
printf("\n"); |
| 472 |
} |
| 473 |
for(;w<nw;w++) { |
| 474 |
printf("M%d ",w-nw0); |
| 475 |
for(e=0;e<words[w].nent;e++) pabm(words[w].e[e]->flbmh,1); |
| 476 |
printf("\n"); |
| 477 |
} |
| 478 |
|
| 479 |
return 0; |
| 480 |
} |