xref: /openbsd/games/boggle/mkindex/mkindex.c (revision d415bd75)
1 /*	$OpenBSD: mkindex.c,v 1.9 2016/01/07 16:00:31 tb Exp $	*/
2 /*	$NetBSD: mkindex.c,v 1.2 1995/03/21 12:14:52 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Barry Brachman.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include "bog.h"
42 
43 char *nextword(FILE *, char *, int *, int *);
44 
45 int
46 main(int argc, char *argv[])
47 {
48 	int clen, rlen, prev;
49 	long off, start;
50 	char buf[MAXWORDLEN + 1];
51 
52 	if (pledge("stdio", NULL) == -1)
53 		err(1, "pledge");
54 
55 	prev = '\0';
56 	off = start = 0L;
57 	while (nextword(stdin, buf, &clen, &rlen) != NULL) {
58 		if (*buf != prev) {
59 			if (prev != '\0')
60 				printf("%c %6ld %6ld\n", prev, start, off - 1);
61 			prev = *buf;
62 			start = off;
63 		}
64 		off += clen + 1;
65 	}
66 	printf("%c %6ld %6ld\n", prev, start, off - 1);
67 	return 0;
68 }
69 
70 /*
71  * Return the next word in the compressed dictionary in 'buffer' or
72  * NULL on end-of-file
73  * Also set clen to the length of the compressed word (for mkindex) and
74  * rlen to the strlen() of the real word
75  */
76 char *
77 nextword(FILE *fp, char *buffer, int *clen, int *rlen)
78 {
79 	int ch, pcount;
80 	char *p, *q;
81 	static char buf[MAXWORDLEN + 1];
82 	static int first = 1;
83 	static int lastch = 0;
84 
85    	if (first) {
86 		if ((pcount = getc(fp)) == EOF)
87 			return (NULL);
88 		first = 0;
89 	}
90 	else if ((pcount = lastch) == EOF)
91 		return (NULL);
92 
93 	p = buf + (*clen = pcount);
94 
95 	while ((ch = getc(fp)) != EOF && ch >= 'a')
96 			*p++ = ch;
97 		lastch = ch;
98 	*p = '\0';
99 
100 	*rlen = (int) (p - buf);
101 	*clen = *rlen - *clen;
102 
103 	p = buf;
104 	q = buffer;
105 	while ((*q++ = *p) != '\0') {
106 		if (*p++ == 'q')
107 			*q++ = 'u';
108 	}
109 	return (buffer);
110 }
111