6 #define NULL ((void *)0)
8 #define WORD_LIST "/sys/games/lib/anawords"
9 #define VOWELS "aeiouy"
13 #define talloc(t) salloc(sizeof (t))
14 #define MAP(c) ((c) - 'a')
23 typedef struct word word;
33 typedef word *set[LENLIMIT];
51 fprint(err_fd, "fatal error: %s\n", s);
60 if ((p = malloc(z)) == NULL)
61 error("ran out of memory");
69 while (*s && *s != '\n')
86 if (w->length == 0 || w->length >= LENLIMIT)
90 return strchr("aio", w->text[0]) != NULL;
137 if ((s = Brdline(bp, '\n')) == NULL)
143 w->length = strlen(s);
144 w->text = strcpy(salloc(w->length+1), s);
145 w->mask = str_to_mask(s);
164 bp = Bopen(WORD_LIST, OREAD);
171 while ((w = get_word(bp)) != NULL)
173 p = &words[w->length];
180 count_letters(target *t, char *s)
184 for (i = 0; i < ALPHAS; i++)
191 t->count[MAP(*s++)]++;
197 contained(word *i, target *t)
203 if ((i->mask & t->mask) != i->mask)
206 count_letters(&it, i->text);
208 for (n = 0; n < ALPHAS; n++)
210 if (it.count[n] > t->count[n])
214 if (it.length == t->length)
217 for (v = VOWELS; *v != '\0'; v++)
219 if (t->count[MAP(*v)] > it.count[MAP(*v)])
227 prune(set in, int m, target *filter, set out)
235 for (n = 1; n < LENLIMIT; n++)
245 for (i = in[n]; i != NULL; i = i->next)
247 if (contained(i, filter))
269 for (n = 1; n < LENLIMIT; n++)
271 for (w = s[n]; w != NULL; w = w->next)
272 fprint(out_fd, "%s\n", w->text);
284 for (i = 0; i < ALPHAS; i++)
298 fprint(out_fd, "%d", (int)(e - list + 1));
300 for (p = list; p <= e; p++)
301 fprint(out_fd, " %s", (*p)->text);
303 fprint(out_fd, "\n");
312 for (i = 1; i < LENLIMIT; i++)
314 for (p = s[i]; p != NULL; p = q)
323 enumerate(word **p, target *i, set c)
332 b = (i->length + (maxwords - l - 1)) / (maxwords - l);
334 for (m = i->length; m >= b; m--)
338 for (w = h; w != NULL; w = w->next)
347 if (l == maxwords - 1)
353 for (s = w->text; *s != '\0'; s++)
356 t.mask = target_mask(t.count);
359 if (prune(c, m, &t, o))
362 enumerate(p + 1, &t, o);
376 for (p = s, q = s; *p != '\0'; p++)
380 else if (isupper(*p))
388 anagramulate(char *s)
397 else if ((maxwords = strlen(s) / 4) < 3)
400 fprint(out_fd, "%s:\n", s);
401 t.mask = str_to_mask(s);
402 count_letters(&t, s);
404 if (!prune(words,t.length, &t, subjects))
407 enumerate(&list[0], &t, subjects);
413 if ((fixed = atoi(s)) < 1)
423 Binit(&b, in_fd, OREAD);
424 while ((s = Brdline(&b, '\n')) != NULL)
426 s[Blinelen(&b)-1] = '\0';
430 fprint(out_fd, "Fixed = %d.\n", fixed);
435 fprint(out_fd, "Done.\n");
442 main(int argc, char **argv)