Debian Codegraph @

treatment.c

raw symbols SCIP
1 /*
2 Qxw is a program to help construct and publish crosswords.
3
4 Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant
5 http://www.quinapalus.com
6 E-mail: [email protected]
7
8 This file is part of Qxw.
9
10 Qxw is free software: you can redistribute it and/or modify
11 it under the terms of version 2 of the GNU General Public License
12 as published by the Free Software Foundation.
13
14 Qxw is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Qxw. If not, see <http://www.gnu.org/licenses/> or
21 write to the Free Software Foundation, Inc., 51 Franklin Street,
22 Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25 #include "common.h"
26 #include "qxw.h"
27 #include "dicts.h"
28 #include "treatment.h"
29 #include "gui.h"
30
31 #ifdef _WIN32 // using wrapper for Windows dynamic linking functions
32 #include "pfdlfcn.h"
33 #else
34 #include <dlfcn.h>
35 #endif
36
37 #define HTABSZ 4194304
38
39 // INITIAL FEASIBLE LIST GENERATION
40
41 static int curans,curem,curten,curdm;
42
43 int treatmode=0,treatorder[NMSG]={0,0};
44 char tpifname[SLEN]="";
45 ABM treatcstr[NMSG][MXFL]={{0},{0}};
46 int tambaw=0;
47
48 char treatmsg [NMSG][MXLT*8+1]; // unfiltered, as provided by GUI, utf-8
49 uchar treatmsgU [NMSG][MXLT+1]; // converted to uchars
50 char treatmsgICC [NMSG][MXLT+1]; // converted to ICCs
51 char treatmsgICCAZ [NMSG][MXLT+1]; // alphabetic only
52 char treatmsgICCAZ09[NMSG][MXLT+1]; // alphabetic+digits
53 uchar treatmsgUAZ [NMSG][MXLT+1]; // converted to uchars
54 uchar treatmsgUAZ09 [NMSG][MXLT+1]; // converted to uchars
55 char treatmsgAZ [NMSG][MXLT+1]; // 7-bit clean *no transliteration*
56 char treatmsgAZ09 [NMSG][MXLT+1];
57
58 char *treatmessage [NMSG]={"",""}; // pointer copies of above
59 uchar*treatmessageU [NMSG];
60 char *treatmessageICC [NMSG];
61 char *treatmessageICCAZ [NMSG];
62 char *treatmessageICCAZ09[NMSG];
63 uchar*treatmessageUAZ [NMSG];
64 uchar*treatmessageUAZ09 [NMSG];
65 char *treatmessageAZ [NMSG];
66 char *treatmessageAZ09 [NMSG];
67
68 char msgchar[NMSG];
69 uchar msgcharU[NMSG]; // converted to uchar
70 uchar msgcharUAZ[NMSG]; // converted to uchar, alphabetic, made upper case
71 uchar msgcharUAZ09[NMSG]; // ... plus digits
72 char msgcharICC[NMSG]; // converted to ICC
73 char msgcharICCAZ[NMSG];
74 char msgcharICCAZ09[NMSG];
75 char msgcharAZ[NMSG]; // 7-bit clean *no transliteration*
76 char msgcharAZ09[NMSG];
77
78 const char*answerICC;
79 const uchar*answerU;
80
81 int clueorderindex;
82 int gridorderindex[MXLE];
83 int checking[MXLE];
84 int lightlength;
85 int lightx;
86 int lighty;
87 int lightdir;
88
89 // STATICS FOR VARIOUS TREATMENTS
90
91 // Playfair square
92 static char psq[25]; // square position to ICC, 0 if cell not filled
93 static int psc[MAXICC]; // ICC to square position, -1 if not present
94
95 static int*tfl=0; // temporary feasible list
96 static int ctfl,ntfl;
97
98 static int clts;
99 static int hstab[HTABSZ];
100 static int haestab[HTABSZ];
101 static struct memblk*lstrings=0;
102 static struct memblk*lmp=0;
103 static int lml=MEMBLK;
104
105 static void dohistdata(struct light*l) {
106 int i,j,m,n;
107 memset(l->hist,0,sizeof(l->hist));
108 l->lbm=0;
109 m=0;
110 n=strlen(l->s);
111 if(l->tagged) n-=NMSG;
112 for(i=0;i<n;i++) {
113 j=(int)l->s[i];
114 l->hist[j]++;
115 if(l->hist[j]>m) m=l->hist[j];
116 l->lbm|=ICCTOABM(j);
117 }
118 }
119
120 // return index of light, creating if it doesn't exist; -1 on no memory
121 static int findlight(const char*s,int tagged,int a,int e) {
122 unsigned int h0,h1;
123 int f,i,u,l0;
124 int l;
125 int len0,len1;
126 struct light*p;
127 struct memblk*q;
128
129 len0=strlen(s);
130 if(tagged) len0-=NMSG;
131 assert(len0>0);
132 h0=0;
133 for(i=0;i<len0;i++) h0=h0*113+s[i];
134 h1=h0*113+a*27+e;
135 h0=h0%HTABSZ; // h0 is hash of string only
136 h1=h1%HTABSZ; // h1 is hash of string+treatment+entry method
137 l=haestab[h1];
138 while(l!=-1) {
139 if(lts[l].ans==a&&lts[l].em==e&&!strcmp(s,lts[l].s)) return l; // exact hit in all particulars? return it
140 l=lts[l].hashaeslink;
141 }
142 if(ltotal>=clts) { // out of space to store light structures? (always happens first time)
143 clts=clts*2+5000; // try again a bit bigger
144 p=realloc(lts,clts*sizeof(struct light));
145 if(!p) return -1;
146 lts=p;
147 DEB_FL printf("lts realloc: %d\n",clts);
148 }
149 l=hstab[h0];
150 u=-1; // look for the light string, independent of how it arose
151 f=0;
152 while(l!=-1) {
153 len1=strlen(lts[l].s);
154 if(lts[l].tagged) len1-=NMSG;
155 assert(len1>0);
156 if(len0==len1&&!strncmp(s,lts[l].s,len0)) { // match as far as non-tag part is concerned
157 u=lts[l].uniq;
158 if(!strcmp(s,lts[l].s)) {f=1; break;} // exact match including possible tags
159 }
160 l=lts[l].hashslink;
161 }
162 if(f==0) { // we do not have a full-string match
163 l0=strlen(s)+1;
164 if(lml+l0>MEMBLK) { // make space to store copy of light string
165 DEB_FL printf("memblk alloc\n");
166 q=(struct memblk*)malloc(sizeof(struct memblk));
167 if(!q) return -1;
168 q->next=NULL;
169 if(lmp==NULL) lstrings=q; else lmp->next=q; // link into list
170 lmp=q;
171 lml=0;
172 }
173 if(u==-1) u=ultotal++; // allocate new uniquifying number if needed
174 lts[ltotal].s=lmp->s+lml;
175 strcpy(lmp->s+lml,s);lml+=l0;
176 } else {
177 lts[ltotal].s=lts[l].s;
178 }
179 lts[ltotal].ans=a;
180 lts[ltotal].em=e;
181 lts[ltotal].uniq=u;
182 lts[ltotal].tagged=tagged;
183 lts[ltotal].hashslink =hstab [h0]; hstab [h0]=ltotal; // insert into hash tables
184 lts[ltotal].hashaeslink=haestab[h1]; haestab[h1]=ltotal;
185 dohistdata(lts+ltotal);
186 return ltotal++;
187 }
188
189 // is word in dictionaries specified by curdm?
190 int iswordICC(const char*s) { return iswordindm(s,curdm); }
191
192 // as above, but s converted from uchars
193 int iswordU(const uchar*s) {
194 int i,j,u;
195 char s0[MXFL+1];
196 for(i=0,j=0;s[i]&&j<MXFL;i++) {
197 u=uchartoICC(s[i]);
198 if(u) s0[j++]=u;
199 }
200 s0[j]=0;
201 return iswordICC(s0);
202 }
203
204 // as above, but s converted from UTF-8 (or just 7-bit ASCII): compatible with previous versions
205 int isword(const char*s) {
206 uchar s0[MXFL+1];
207 utf8touchars(s0,s,MXFL+1);
208 return iswordU(s0);
209 }
210
211 int ICCtoclass(char c) {
212 if(c<0||c>MAXICC) return -1;
213 return icctogroup[(int)c];
214 }
215
216 // add light to feasible list: s=text of light (in internal character code), a=answer from which treated (-ve for msgword), e=entry method
217 // returns 0 if OK, !=0 on (out of memory) error
218 static int addlight(const char*s,int a,int e) {
219 int l;
220 int*p;
221 char t[MXFL+1]; // curten should never be set when adding msgword[]:s (got from msglprop); as MXLE+NMSG<=MXFL this never overflows
222
223 l=strlen(s);
224 if(l<1) return 0; // is this test needed?
225 memcpy(t,s,l);
226 if(curten) memcpy(t+l,msgcharICC,NMSG),l+=NMSG; // append tag characters if any
227 t[l]=0;
228 l=findlight(t,curten,a,e);
229 if(l<0) return l;
230 if(ntfl>=ctfl) {
231 ctfl=ctfl*3/2+500;
232 p=realloc(tfl,ctfl*sizeof(int));
233 if(!p) return -1;
234 tfl=p;
235 DEB_FL printf("tfl realloc: %d\n",ctfl);
236 }
237 tfl[ntfl++]=l;
238 return 0;
239 }
240
241 // Add treated answer (in internal character code) to feasible light list if suitable
242 // returns !=0 for error
243 int treatedanswerICC(const char*s) {
244 char s0[MXFL+1];
245 int j,k,l,u;
246
247 l=strlen(s);
248 // DEB_FL printf("treatedanswerICC l=%d\n",l);
249 if(l!=lightlength) return 0;
250 DEB_FL assert(l>0);
251 if(tambaw&&!iswordICC(s)) return 0;
252 if(curem&EM_JUM) { // jumbled entry method
253 return addlight(s,curans,4); // just store normal entry
254 }
255 if(curem&EM_FWD) { // forwards entry method
256 u=addlight(s,curans,0);if(u) return u;
257 }
258 if(curem&EM_REV) {
259 for(j=0;j<l;j++) s0[j]=s[l-j-1]; // reversed entry method
260 s0[j]=0;
261 u=addlight(s0,curans,1);if(u) return u;
262 }
263 if(curem&EM_CYC) { // cyclic permutation
264 if(!(l==2&&(curem&EM_REV))) { // not if already covered by reversal
265 for(k=1;k<l;k++) {
266 for(j=0;j<l;j++) s0[j]=s[(j+k)%l];
267 s0[j]=0;
268 u=addlight(s0,curans,2);if(u) return u;
269 }
270 }
271 }
272 if(curem&EM_RCY) { // reversed cyclic permutation
273 if(!(l==2&&(curem&EM_FWD))) { // not if already covered by forwards entry
274 for(k=1;k<l;k++) {
275 for(j=0;j<l;j++) s0[j]=s[(l-j-1+k)%l];
276 s0[j]=0;
277 u=addlight(s0,curans,3);if(u) return u;
278 }
279 }
280 }
281 return 0;
282 }
283
284 // as above, but s converted from uchars
285 int treatedanswerU(const uchar*s) {
286 int i,j,u;
287 char s0[MXFL+1];
288 for(i=0,j=0;s[i]&&j<MXFL;i++) {
289 u=uchartoICC(s[i]);
290 if(u) s0[j++]=u;
291 }
292 if(j!=lightlength) return 0;
293 s0[j]=0;
294 return treatedanswerICC(s0);
295 }
296
297 // as above, but s converted from UTF-8 (or just 7-bit ASCII): compatible with previous versions
298 int treatedanswer(const char*s) {
299 uchar s0[MXFL+1];
300 DEB_TR printf("treatedanswer(\"%s\")\n",s);
301 utf8touchars(s0,s,MXFL+1);
302 return treatedanswerU(s0);
303 }
304
305 // returns !=0 on error
306 static int inittreat(void) {int i,k;
307 int c;
308
309 switch(treatmode) {
310 case 1: // Playfair
311 for(i=1;i<MAXICC;i++) psc[i]=-1; // generate code square
312 for(i=0;i<25;i++) psq[i]=0;
313 for(i=0,k=0;treatmsgICC[0][i]&&k<25;i++) {
314 c=treatmsgICC[0][i];
315 if(icctouchar[c]=='J'&&uchartoICC('I')!=0) c=uchartoICC('I'); // replace J by I if possible
316 if(psc[c]==-1) {psc[c]=k;psq[k]=c;k++;} // character not placed in square yet? add it
317 }
318 for(i=0;i<niccused&&k<25;i++) { // fill up the rest of the square
319 c=iccused[i];
320 if(c==0) continue; // not used in alphabet?
321 if(icctouchar[c]=='J'&&uchartoICC('I')!=0) continue; // skip J if we have I
322 if(psc[c]==-1) {psc[c]=k;psq[k]=c;k++;}
323 }
324 if(uchartoICC('I')!=0&&uchartoICC('J')!=0) psc[(int)uchartoICC('J')]=psc[(int)uchartoICC('I')];
325 DEB_TR for(i=0;i<25;i++) {
326 printf("%s ",icctoutf8[(int)psq[i]]);
327 if(i%5==4) printf("\n");
328 }
329 return 0;
330 case 9: // custom plug-in
331 for(i=0;i<NMSG;i++) {
332 treatmessage [i]=treatmsg [i];
333 treatmessageU [i]=treatmsgU [i];
334 treatmessageICC [i]=treatmsgICC [i];
335 treatmessageICCAZ [i]=treatmsgICCAZ [i];
336 treatmessageICCAZ09[i]=treatmsgICCAZ09[i];
337 treatmessageUAZ [i]=treatmsgUAZ [i];
338 treatmessageUAZ09 [i]=treatmsgUAZ09 [i];
339 treatmessageAZ [i]=treatmsgAZ [i];
340 treatmessageAZ09 [i]=treatmsgAZ09 [i];
341 }
342 break;
343 default:break;
344 }
345 return 0;
346 }
347
348 static void finittreat(void) {}
349
350
351 static void *tpih=0;
352 static int (*treatf)(const char*)=0;
353
354 // returns error string or 0 for OK
355 char*loadtpi(void) {
356 int (*f)(void);
357 unloadtpi();
358 dlerror(); // clear any existing error
359 DEB_TR printf("dlopen(\"%s\")\n",tpifname);
360 tpih=dlopen(tpifname,RTLD_LAZY);
361 if(!tpih) return dlerror();
362 dlerror();
363 *(void**)(&f)=dlsym(tpih,"init"); // see man dlopen for the logic behind this
364 if(!dlerror()) (*f)(); // initialise the plug-in
365 *(void**)(&treatf)=dlsym(tpih,"treat");
366 return dlerror();
367 }
368
369 void unloadtpi(void) {void (*f)();
370 if(tpih) {
371 dlerror(); // clear any existing error
372 *(void**)(&f)=dlsym(tpih,"finit");
373 if(!dlerror()) (*f)(); // finalise it before unloading
374 dlclose(tpih);
375 }
376 tpih=0;
377 treatf=0;
378 }
379
380 void reloadtpi(void) {
381 char s[300],*p;
382 if(treatmode==TREAT_PLUGIN) {
383 if((p=loadtpi())) {
384 sprintf(s,"Error loading custom plug-in\n%.200s",p);
385 reperr(s);
386 }
387 }
388 else unloadtpi();
389 }
390
391 // offset-encrypt c0 by c1: "group" of result will be same as group of c0
392 // works entirely on "iccused" indices
393 static int offsetenc(int c0,int c1) {
394 int g0,g1,n,u;
395 g0=iccusedtogroup[c0];
396 g1=iccusedtogroup[c1];
397 if(g0<0||g0>1||g1<0||g1>1) return c0; // don't encode symbols
398 u=iccgroupstart[g0];
399 n=iccgroupstart[g0+1]-u; // size of group g0
400 assert(n>0);
401 return u+(c0-u+c1-iccgroupstart[g1])%n;
402 }
403
404 // returns !=0 on error
405 // s points to answer to be treated in internal character code
406 static int treatans(const char*s) {
407 int c0,c1,c2,d0,d1,g,i,j,l,l0,l1,o,u;
408 char ansutf8[MXFL*8],*p;
409 uchar ansU[MXFL+1];
410 char t[MXLE+2]; // enough for "insert single character"
411 l=strlen(s);
412 // printf("treatans("); printICCs(s); printf(")");fflush(stdout);
413 for(i=0;s[i];i++) if(s[i]>=MAXICC) return 0;
414 switch(treatmode) {
415 case 0: // no treatment
416 return treatedanswerICC(s);
417 case 1: // Playfair
418 if(l!=lightlength) return 0;
419 strcpy(t,s);
420 for(i=0;i<l-1;i+=2) {
421 c0=s[i];c1=s[i+1]; // letter pair to encode
422 l0=psc[c0];l1=psc[c1]; // positions of chars in the square
423 if(l0==-1||l1==-1) continue; // leave unencoded if either letter is not in square
424 if(l0==l1) continue; // leave double letters (including 'IJ' etc.) unencoded
425 if (l0/5==l1/5) d0=psq[ l0/5 *5+(l0+1)%5],d1=psq[ l1/5 *5+(l1+1)%5]; // same row
426 else if(l0%5==l1%5) d0=psq[(l0/5+1)%5*5+ l0 %5],d1=psq[(l1/5+1)%5*5+ l1 %5]; // same col
427 else d0=psq[ l0/5 *5+ l1 %5],d1=psq[ l1/5 *5+ l0 %5]; // rectangle
428 if(d0!=0&&d1!=0) t[i]=d0,t[i+1]=d1; // successfully encoded?
429 } // if l is odd last character is not encoded
430 return treatedanswerICC(t);
431 case 2: // substitution
432 if(l!=lightlength) return 0;
433 l0=strlen(treatmsgICC[0]);
434 strcpy(t,s);
435 for(i=0;s[i];i++) {
436 c0=icctousedindex[(int)s[i]];
437 if(c0<0) continue;
438 g=iccusedtogroup[c0];
439 if(g<0||g>1) continue; // only affect letters and digits
440 j=c0-iccgroupstart[g];
441 if(j>=l0) continue;
442 t[i]=treatmsgICC[0][j];
443 }
444 return treatedanswerICC(t);
445 case 3: // fixed Caesar/Vigenère
446 if(l!=lightlength) return 0;
447 l0=strlen(treatmsgICC[0]);
448 if(l0==0) return treatedanswerICC(s); // no keyword, so leave as plaintext
449 if(niccused<1) return treatedanswerICC(s); // prevent divide-by-0
450 strcpy(t,s);
451 for(i=0;s[i];i++) {
452 c0=icctousedindex[(int)s[i]];
453 c1=icctousedindex[(int)treatmsgICC[0][i%l0]];
454 if(c0>=0&&c1>=0) t[i]=iccused[offsetenc(c0,c1)];
455 }
456 return treatedanswerICC(t);
457 case 4: // variable Caesar
458 if(l!=lightlength) return 0;
459 if(niccused<1) return treatedanswerICC(s); // prevent divide-by-0
460 if(treatorder[0]==0) { // for backwards compatibility
461 l0=strlen(treatmsgICC[0]);
462 if(l0==0) return treatedanswerICC(s); // no keyword, so leave as plaintext
463 o=treatmsgICC[0][clueorderindex%l0];
464 } else {
465 o=msgcharICC[0];
466 if(o==ICC_DASH) return treatedanswerICC(s); // leave as plaintext
467 }
468 c1=icctousedindex[o];
469 if(c1<0) return treatedanswerICC(s);
470 strcpy(t,s);
471 for(i=0;s[i];i++) {
472 c0=icctousedindex[(int)s[i]];
473 if(c0>=0) t[i]=iccused[offsetenc(c0,c1)];
474 }
475 return treatedanswerICC(t);
476 case 10: // misprint, correct letters specified
477 if(l!=lightlength) return 0;
478 c0=msgcharICC[0];
479 if(c0==ICC_DASH) return treatedanswerICC(s); // unmisprinted
480 c1=0;
481 goto misp0;
482 case 11: // misprint, misprinted letters specified
483 if(l!=lightlength) return 0;
484 c1=msgcharICC[0];
485 if(c1==ICC_DASH) return treatedanswerICC(s); // unmisprinted
486 c0=0;
487 goto misp0;
488 case 5: // misprint
489 if(l!=lightlength) return 0;
490 l0=ucharslen(treatmsgU[0]);
491 l1=ucharslen(treatmsgU[1]);
492 c0=0; if(clueorderindex<l0) c0=uchartoICC(treatmsgU[0][clueorderindex]); // will be left at 0 if not a recognised character
493 c1=0; if(clueorderindex<l1) c1=uchartoICC(treatmsgU[1][clueorderindex]);
494 if(c0==0&&c1==0) return treatedanswerICC(s); // allowing this case would slow things down too much for now
495 misp0: // here we want to misprint c0 as c1, where 0 indicates any character
496 strcpy(t,s);
497 for(i=0;s[i];i++) if(c0==0||s[i]==c0) {
498 if(c1==0) {
499 for(j=0;j<niccused;j++) {
500 c2=iccused[j];
501 if(s[i]==c2) continue; // not a *mis*print
502 t[i]=c2;
503 u=treatedanswerICC(t); if(u) return u;
504 t[i]=s[i]; // restore modified character
505 }
506 } else {
507 if(c0==0&&s[i]==c1) continue; // not a *mis*print unless specifically instructed otherwise
508 t[i]=c1;
509 u=treatedanswerICC(t); if(u) return u;
510 t[i]=s[i]; // restore modified character
511 if(c0==c1) break; // only one entry for the `misprint as self' case
512 }
513 }
514 return 0;
515 case 6: // delete single occurrence
516 c0=msgcharICC[0];
517 if(c0==ICC_DASH) {
518 if(l==lightlength) return treatedanswerICC(s);
519 return 0;
520 }
521 if(l!=lightlength+1) return 0;
522 for(i=0;s[i];i++) if(s[i]==c0) {
523 for(j=0;j<i;j++) t[j]=s[j];
524 for(;s[j+1];j++) t[j]=s[j+1];
525 t[j]=0;
526 u=treatedanswerICC(t); if(u) return u;
527 while(s[i+1]==c0) i++; // skip duplicate outputs
528 }
529 return 0;
530 case 7: // delete all occurrences (letters latent)
531 c0=msgcharICC[0];
532 if(c0==ICC_DASH) {
533 if(l==lightlength) return treatedanswerICC(s);
534 return 0;
535 }
536 if(l<=lightlength) return 0;
537 for(i=0,j=0;s[i];i++) if(s[i]!=c0) t[j++]=s[i];
538 t[j]=0;
539 if(j!=lightlength) return 0; // not necessary, but improves speed slightly
540 return treatedanswerICC(t);
541 case 8: // insert single character
542 c0=msgcharICC[0];
543 if(c0==ICC_DASH) {
544 if(l==lightlength) return treatedanswerICC(s);
545 return 0;
546 }
547 if(l!=lightlength-1) return 0;
548 for(i=0;i<=l;i++) { // try inserting c0 before position i
549 for(j=0;j<i;j++) t[j]=s[j];
550 t[j++]=c0;
551 for(;s[j-1];j++) t[j]=s[j-1];
552 t[j]=0;
553 u=treatedanswerICC(t); if(u) return u;
554 while(s[i]==c0) i++; // skip duplicate outputs
555 }
556 return 0;
557 case 9: // custom plug-in
558 answerICC=s;
559 for(i=0,p=ansutf8;s[i];i++) strcpy(p,icctoutf8[(int)s[i]]),p+=strlen(p),ansU[i]=icctouchar[(int)s[i]];
560 ansU[i]=0;
561 answerU=ansU;
562 DEB_TR printf("ansutf8=>%s<\n",ansutf8);
563 if(treatf) return (*treatf)(ansutf8);
564 return 1;
565 default:break;
566 }
567 return 0;
568 }
569
570 int pregetinitflist(void) {
571 int i;
572 struct memblk*p;
573 while(lstrings) {p=lstrings->next;free(lstrings);lstrings=p;} lmp=0; lml=MEMBLK;
574 FREEX(tfl);ctfl=0;ntfl=0;
575 FREEX(lts);clts=0;ltotal=0;ultotal=0;
576 for(i=0;i<HTABSZ;i++) hstab[i]=-1,haestab[i]=-1;
577 if(inittreat()) return 1;
578 return 0;
579 }
580
581 // returns !=0 for error
582 int postgetinitflist(void) {
583 finittreat();
584 FREEX(tfl);ctfl=0;
585 return 0;
586 }
587
588 // construct an initial list of feasible lights for a given length and set of light properties
589 // caller's responsibility to free(*l)
590 // returns !=0 on error; -5 on abort
591 int getinitflist(int**l,int*ll,struct lprop*lp,int llen) {
592 int g,i,j,u;
593 ABM mfl[NMSG],ml[NMSG],b;
594
595 ntfl=0;
596 curdm=lp->dmask,curem=lp->emask,curten=lp->ten;
597 for(i=0;i<NMSG;i++) if(curdm&(1<<(MAXNDICTS+i))) { // "special" word for message spreading/jumble?
598 DEB_FL {
599 printf("msgword[%d]=<",i);
600 printICCs(msgword[i]);
601 printf(">\n");
602 }
603 u=addlight(msgword[i],-1-i,0);
604 if(u) return u;
605 goto ex0;
606 }
607 if((curem&EM_ALL)==0) curem=EM_FWD; // force normal entry to be allowed if all are disabled
608 lightlength=llen;
609 DEB_FL printf("getinitflist(%p) llen=%d dmask=%08x emask=%08x ten=%d:\n",(void*)lp,llen,curdm,curem,curten);
610 memset(mfl,0,sizeof(mfl));
611 for(i=0;i<NMSG;i++) {
612 if(clueorderindex<(int)strlen(treatmsg [i])) msgchar [i]=treatmsg [i][clueorderindex]; else msgchar [i]='-'; // we set these up even if treatment is not enabled
613 if(clueorderindex< ucharslen(treatmsgU [i])) msgcharU [i]=treatmsgU [i][clueorderindex]; else msgcharU [i]='-';
614 if(clueorderindex< ucharslen(treatmsgUAZ [i])) msgcharUAZ [i]=treatmsgUAZ [i][clueorderindex]; else msgcharUAZ [i]='-';
615 if(clueorderindex< ucharslen(treatmsgUAZ09 [i])) msgcharUAZ09 [i]=treatmsgUAZ09 [i][clueorderindex]; else msgcharUAZ09 [i]='-';
616 if(clueorderindex<(int)strlen(treatmsgICC [i])) msgcharICC [i]=treatmsgICC [i][clueorderindex]; else msgcharICC [i]=ICC_DASH;
617 if(clueorderindex<(int)strlen(treatmsgICCAZ [i])) msgcharICCAZ [i]=treatmsgICCAZ [i][clueorderindex]; else msgcharICCAZ [i]=ICC_DASH;
618 if(clueorderindex<(int)strlen(treatmsgICCAZ09[i])) msgcharICCAZ09[i]=treatmsgICCAZ09[i][clueorderindex]; else msgcharICCAZ09[i]=ICC_DASH;
619 if(clueorderindex<(int)strlen(treatmsgAZ [i])) msgcharAZ [i]=treatmsgAZ [i][clueorderindex]; else msgcharAZ [i]='-';
620 if(clueorderindex<(int)strlen(treatmsgAZ09 [i])) msgcharAZ09 [i]=treatmsgAZ09 [i][clueorderindex]; else msgcharAZ09 [i]='-';
621 if(curten&&treatorder[i]>0) { // using discretionary mode - i.e., potentially shuffling treatment message?
622 for(j=0;treatmsgICC[i][j];j++) mfl[i]|=ICCTOABM((int)treatmsgICC[i][j]); // bitmap of all letters present in message
623 if(ntw>(int)strlen(treatmsgICC[i])) mfl[i]|=ABM_DASH; // add in "-" if message not long enough
624 if(clueorderindex<MXFL) mfl[i]&=treatcstr[i][clueorderindex]; // apply constraints
625 ml[i]=mfl[i]&~(mfl[i]-1); // find bottom set bit to initialise "counter"
626 if(ml[i]==0) goto ex0; // no possibilities: exit with empty list
627 }
628 }
629 for(;;) { // loop over all combinations of msgchar:s
630 for(i=0;i<NMSG;i++) if(curten&&treatorder[i]>0) {
631 j=abmtoicc(ml[i]); // extract msgchar:s from counter if in discretionary mode (overwriting previous values of msgcharICC and friends)
632 u=icctouchar[j];
633 msgcharICC[i]=j;
634 msgcharU[i]=u;
635 g=icctogroup[j];
636 if((g==0||g==1)&&u<128) msgcharAZ09[i]=u; // alphanumeric and 7-bit clean?
637 else msgcharAZ09[i]='-'; // otherwise
638 }
639 DEB_FL { printf(" building list with msgcharICC[]="); for(i=0;i<NMSG;i++) printf("%s",icctoutf8[(int)msgcharICC[i]]); printf("\n"); }
640 for(i=0;i<atotal;i++) {
641 curans=i;
642 if((curdm&ansp[curans]->dmask)==0) continue; // not in a valid dictionary
643 if(ansp[curans]->banned) continue;
644 if(curten) u=treatans(ansp[curans]->ul);
645 else u=treatedanswerICC(ansp[curans]->ul);
646 if(u) return u;
647 if(abort_flag) return -5;
648 }
649 for(i=0;i<NMSG;i++) if(curten&&treatorder[i]>0) { // this increments the NMSG-digit "counter" in ml[i] where valid digits are the set bits in mfl[i]
650 b=mfl[i]&~(ml[i]|(ml[i]-1)); // clear bits mf[] and below
651 b&=~(b-1); // find new bottom set bit
652 if(b) {ml[i]=b; break;} // try next feasible character
653 ml[i]=mfl[i]&~(mfl[i]-1); // reset to bottom set bit and proceed to advance next character
654 }
655 if(i==NMSG) break; // finish when all combinations done
656 }
657 ex0:
658 *l=malloc(ntfl*sizeof(int)+1); // ensure we don't execute malloc(0)
659 if(*l==0) return 1;
660 memcpy(*l,tfl,ntfl*sizeof(int));
661 *ll=ntfl;
662 DEB_FL printf("%d entries\n",ntfl);
663 return 0;
664 }