Expand PMF_FN_* macros.
[netbsd-mini2440.git] / games / boggle / mkindex / mkindex.c
blob28aa8105792d88096b5f3183e844b79acaa1d482
1 /* $NetBSD: mkindex.c,v 1.10 2005/07/01 16:38:24 jmc Exp $ */
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Barry Brachman.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifndef lint
36 static char copyright[] = "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
39 #if 0
40 static char sccsid[] = "@(#)mkindex.c 8.1 (Berkeley) 6/11/93";
41 #else
42 static char rcsid[] =
43 "$NetBSD: mkindex.c,v 1.10 2005/07/01 16:38:24 jmc Exp $";
44 #endif
45 #endif /* not lint */
47 #include <stdio.h>
48 #include <stdlib.h>
50 #include "bog.h"
52 static char *nextword(FILE *, char *, int *, int *);
54 int
55 main(void)
57 int clen, rlen, prev, i;
58 long off, start;
59 char buf[MAXWORDLEN + 1];
61 prev = '\0';
62 off = start = 0L;
63 while (nextword(stdin, buf, &clen, &rlen) != NULL) {
64 if (*buf != prev) {
66 * Boggle expects a full index even if the dictionary
67 * had no words beginning with some letters.
68 * So we write out entries for every letter from prev
69 * to *buf.
71 if (prev != '\0')
72 printf("%c %6ld %6ld\n", prev, start, off - 1);
73 for (i = (prev ? prev + 1 : 'a'); i < *buf; i++)
74 printf("%c %6ld %6ld\n", i, off, off - 1);
75 prev = *buf;
76 start = off;
78 off += clen + 1;
80 printf("%c %6ld %6ld\n", prev, start, off - 1);
81 for (i = prev + 1; i <= 'z'; i++)
82 printf("%c %6ld %6ld\n", i, off, off - 1);
83 fflush(stdout);
84 if (ferror(stdout)) {
85 perror("error writing standard output");
86 exit(1);
88 exit(0);
92 * Return the next word in the compressed dictionary in 'buffer' or
93 * NULL on end-of-file
94 * Also set clen to the length of the compressed word (for mkindex) and
95 * rlen to the strlen() of the real word
97 static char *
98 nextword(FILE *fp, char *buffer, int *clen, int *rlen)
100 int ch, pcount;
101 char *p, *q;
102 static char buf[MAXWORDLEN + 1];
103 static int first = 1;
104 static int lastch = 0;
106 if (first) {
107 if ((pcount = getc(fp)) == EOF)
108 return (NULL);
109 first = 0;
111 else if ((pcount = lastch) == EOF)
112 return (NULL);
114 p = buf + (*clen = pcount);
116 while ((ch = getc(fp)) != EOF && ch >= 'a')
117 *p++ = ch;
118 lastch = ch;
119 *p = '\0';
121 *rlen = (int) (p - buf);
122 *clen = *rlen - *clen;
124 p = buf;
125 q = buffer;
126 while ((*q++ = *p) != '\0') {
127 if (*p++ == 'q')
128 *q++ = 'u';
130 return (buffer);