/* Qxw is a program to help construct and publish crosswords. Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant http://www.quinapalus.com E-mail: qxw@quinapalus.com This file is part of Qxw. Qxw is free software: you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. Qxw is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Qxw. If not, see or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include "common.h" #include "qxw.h" #include "filler.h" #include "alphabets.h" #include "dicts.h" #include "draw.h" #include "treatment.h" static FILE*dkfp=0; static int line; #define DBUFSZ 10000 char dbuf[DBUFSZ]; #define DELIMS " \t\r\n" #define DELIMEOL "\r\n" #define ENHTABSZ 4096 #define MAXENAMELEN 31 static int enhtab[ENHTABSZ]; // deck is assembled using these structs before copying across to struct entry etc. for filler struct dkentry { char name[MAXENAMELEN+1]; ABM flbm; int hashlink; }*dkentries=0; int entsalloc=0; struct dkword { int nent; struct lprop prop; int entries[MXFL]; }*dkwords=0; int wordsalloc=0; static int dknw,dkne; static int dspec; static void batcherr(char*s) { if(line) fprintf(stderr,"Error at line %d: %s\n",line,s); else fprintf(stderr,"Error: %s\n",s); } static void batchwarn(char*s) { fprintf(stderr,"Warning: %s",s); if(line) fprintf(stderr," at line %d of deck\n",line); fprintf(stderr,"\n"); } // check whether a token is a given directive; if c1!=NULL also check against abbreviated form static int cmdcmp(char*s,char*c0,char*c1) { if(s[0]!='.') return 1; if(!STRCASECMP(s+1,c0)) return 0; if(c1&&!STRCASECMP(s+1,c1)) return 0; return 2; } // 0=not a valid entry name; 1=valid static int isvalidename(char*t) { int i; if(!t) return 0; if(strlen(t)<1||strlen(t)>MAXENAMELEN) return 0; for(i=0;t[i];i++) { if(isalnum((unsigned char)t[i])) continue; if(t[i]=='$') continue; if(t[i]=='_') continue; return 0; } return 1; } static void initenhtab() {int i; for(i=0;i'9') {batcherr("Dictionary number must be 1..9"); return 16;} d=*tok-'1'; TOKEOL; if(!tok) strcpy(dfnames[d],""); // allow empty dictionary filename else strncpy(dfnames[d],tok,SLEN-1); dfnames[d][SLEN-1]=0; DEB_DE printf("dictionary %d: >%s<\n",d,dfnames[d]); dspec=1; continue; } if(!cmdcmp(tok,"FILEFILTER","FF")) { CHECKSTART("Dictionary file filter directive") NEXTTOK; if(!tok||strlen(tok)!=1||*tok<='0'||*tok>'9') {batcherr("Dictionary number must be 1..9"); return 16;} d=*tok-'1'; TOKEOL; if(!tok) strcpy(dsfilters[d],""); // allow empty file filter else strncpy(dsfilters[d],tok,SLEN-1); dsfilters[d][SLEN-1]=0; DEB_DE printf("dsfilter %d: >%s<\n",d,dsfilters[d]); dspec=1; continue; } if(!cmdcmp(tok,"ANSWERFILTER","AF")) { CHECKSTART("Dictionary answer filter directive") NEXTTOK; if(!tok||strlen(tok)!=1||*tok<='0'||*tok>'9') {batcherr("Dictionary number must be 1..9"); return 16;} d=*tok-'1'; TOKEOL; if(!tok) strcpy(dafilters[d],""); else strncpy(dafilters[d],tok,SLEN-1); dafilters[d][SLEN-1]=0; DEB_DE printf("dafilter %d: >%s<\n",d,dafilters[d]); dspec=1; continue; } if(!cmdcmp(tok,"RANDOM","RA")) { CHECKSTART("Random fill directive") NEXTTOK; if(!tok||strlen(tok)!=1||*tok<'0'||*tok>'2') {batcherr("Syntax error in random fill directive"); return 16;} afrandom=*tok-'0'; continue; } if(!cmdcmp(tok,"UNIQUE","UN")) { CHECKSTART("Unique fill directive") NEXTTOK; if(!tok||strlen(tok)!=1||*tok<'0'||*tok>'1') {batcherr("Syntax error in unique fill directive"); return 16;} afunique=*tok-'0'; continue; } if(!cmdcmp(tok,"TREATMENT","TR")) { CHECKSTART("Treatment directive") NEXTTOK; if(!tok||!isdigit((unsigned char)*tok)) {batcherr("Syntax error in treatment directive"); return 16;} u=atoi(tok); if(u<0||u>=NATREAT) {batcherr("Invalid treatment number"); return 16;} treatmode=u; continue; } if(!cmdcmp(tok,"MESSAGE","ME")) { CHECKSTART("Message directive") NEXTTOK; if(!tok||strlen(tok)!=1||*tok<'0'||*tok>='0'+NMSG) {batcherr("Syntax error in message directive"); return 16;} u=*tok-'0'; TOKEOL; if(!tok) {batcherr("Syntax error in message directive"); return 16;} strncpy(treatmsg[u],tok,sizeof(treatmsg[0])-1); treatmsg[u][sizeof(treatmsg[0])-1]=0; continue; } if(!cmdcmp(tok,"MESSAGEALLOCATE","MA")) { CHECKSTART("Message allocate directive") NEXTTOK; if(!tok||strlen(tok)!=1||*tok<'0'||*tok>='0'+NMSG) {batcherr("Syntax error in message allocate directive"); return 16;} u=*tok-'0'; NEXTTOK; if(!tok||strlen(tok)!=1||*tok<'0'||*tok>'2') {batcherr("Syntax error in message allocate directive"); return 16;} treatorder[u]=*tok-'0'; continue; } if(!cmdcmp(tok,"MESSAGECONSTRAINTS","MC")) { CHECKSTART("Message constraints directive") NEXTTOK; if(!tok||strlen(tok)!=1||*tok<'0'||*tok>='0'+NMSG) {batcherr("Syntax error in message constraints directive"); return 16;} u=*tok-'0'; TOKEOL; if(!tok) {batcherr("Syntax error in message constraints directive"); return 16;} i=strtoabms(treatcstr[u],MXFL,tok,1); for(;i'1') {batcherr("Syntax error in word constraint directive"); return 16;} tambaw=*tok-'0'; continue; } if(!cmdcmp(tok,"PLUGIN","PI")) { CHECKSTART("Plugin directive") TOKEOL; if(!tok) {batcherr("Syntax error in plugin directive"); return 16;} strncpy(tpifname,tok,SLEN-1); tpifname[SLEN-1]=0; continue; } // the following are stackable using block syntax if(!cmdcmp(tok,"USEDICTIONARY","UD")) { NEXTTOK; if(!tok) {batcherr("Syntax error in dictionary list"); return 16;} for(dmask=0,i=0;tok[i];i++) { if(tok[i]<='0'||tok[i]>'9') {batcherr("Syntax error in dictionary list"); return 16;} dmask|=1<<(tok[i]-'1'); // internally count from 0 } if(!dmask) batchwarn("Empty dictionary list"); continue; } if(!cmdcmp(tok,"ENTRYMETHOD","EM")) { NEXTTOK; if(!tok) {batcherr("Syntax error in entry method specification"); return 16;} for(emask=0,i=0;tok[i];i++) { u=tok[i]; if(isupper((unsigned char)u)) u=tolower((unsigned char)u); switch(u) { case 'f': case '>': emask|=EM_FWD; break; case 'r': case '<': emask|=EM_REV; break; case 'c': case ')': emask|=EM_CYC; break; case 'a': case '(': emask|=EM_RCY; break; case 'j': case '@': emask|=EM_JUM; break; default: batcherr("Syntax error in entry method specification"); return 16; } } if(!emask) batchwarn("No entry methods specified"); continue; } if(!cmdcmp(tok,"TREATMENTENABLE" ,"TE")) { ten=1; continue; } if(!cmdcmp(tok,"TREATMENTDISABLE","TD")) { ten=0; continue; } if(*tok=='.') {batcherr("Unrecognised directive"); return 16;} // word/entry creation if(isvalidename(tok)) { w=newword(); if(w<0) return 16; // out of memory etc. dkwords[w].prop.dmask=dmask; dkwords[w].prop.emask=emask; dkwords[w].prop.ten=ten; for(;;) { DEB_DE printf("word entry tok=>%s<\n",tok); e=findent(tok); if(e<0) return 16; // out of memory etc. if(dkwords[w].nent>=MXLE) {batcherr("Too many entries in word"); return 16;} dkwords[w].entries[dkwords[w].nent]=e; dkwords[w].nent++; ew0: NEXTTOK; if(!tok) break; if(*tok=='=') { ABM a[MXLE]; int l; char s[MAXENAMELEN+100]; l=strtoabms(a,MXLE,tok+1,1); if(l>dkwords[w].nent) {batcherr("Constraint too long"); return 16;} for(i=0;iten&&treatmode>0) ntw++; // count treated words } for(e=0;e=0) printf("H%4d>%4d\n",h,enhtab[h]); for(e=0;e%d\n",e,dkentries[e].name,dkentries[e].flbm,dkentries[e].hashlink); for(w=0;wflbmh,1); if(words[w].flistlen) { printf("\n# "); for(k=0;k0) printf("; "); ansform(t0,sizeof(t0),words[w].flist[k],words[w].wlen,words[w].lp->dmask); printf("%s",t0); } } printf("\n"); } for(;wflbmh,1); printf("\n"); } return 0; }