xref: /netbsd/games/ching/printching/printching.c (revision 6550d01e)
1 /*	$NetBSD: printching.c,v 1.4 2009/08/12 05:40:04 dholland Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Guy Harris.
9  *
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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
42  The Regents of the University of California.  All rights reserved.");
43 #endif /* not lint */
44 
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)ching.phx.c	8.1 (Berkeley) 5/31/93";
48 #else
49 __RCSID("$NetBSD: printching.c,v 1.4 2009/08/12 05:40:04 dholland Exp $");
50 #endif
51 #endif /* not lint */
52 
53 /*
54  * printching - Print NROFF/TROFF source of change, given the line values.
55  */
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include "ching.h"
60 #include "pathnames.h"
61 
62 static int changes(void);
63 static int codem(int a);
64 static int doahex(void);
65 static void phx(int hexagram, int flag);
66 
67 static const struct {
68 	int	lines;		/* encoded value of lines */
69 	int	trinum;		/* trigram number */
70 } table[] = {
71 	{ 777, 0 },		/* 1 */
72 	{ 887, 1 },		/* 4 */
73 	{ 878, 2 },		/* 6 */
74 	{ 788, 3 },		/* 7 */
75 	{ 888, 4 },		/* 8 */
76 	{ 778, 5 },		/* 5 */
77 	{ 787, 6 },		/* 3 */
78 	{ 877, 7 },		/* 2 */
79 };
80 
81 /*
82  * Gives hexagram number from two component trigrams.
83  */
84 static const int crosstab[8][8] = {
85 	{1,  34, 5,  26, 11, 9,  14, 43},
86 	{25, 51, 3,  27, 24, 42, 21, 17},
87 	{6,  40, 29, 4,  7,  59, 64, 47},
88 	{33, 62, 39, 52, 15, 53, 56, 31},
89 	{12, 16, 8,  23, 2,  20, 35, 45},
90 	{44, 32, 48, 18, 46, 57, 50, 28},
91 	{13, 55, 63, 22, 36, 37, 30, 49},
92 	{10, 54, 60, 41, 19, 61, 38, 58}
93 };
94 
95 static int trigrams[6];
96 static int moving[6];
97 
98 static FILE *chingf;		/* stream to read the hexagram file */
99 
100 /*ARGSUSED*/
101 int
102 main(int argc, char **argv)
103 {
104 	char *hexptr;		/* pointer to string of lines */
105 	char hexstr[6+1];	/* buffer for reading lines in */
106 	int i;
107 
108 	if (argc < 2)
109 		hexptr = fgets(hexstr, 6+1, stdin);
110 	else
111 		hexptr = argv[1];
112 	if (hexptr == (char *)NULL || strlen(hexptr) != 6) {
113 		fprintf(stderr, "What kind of a change is THAT?!?\n");
114 		exit(1);
115 	}
116 	for (i = 0; i < 6; i++) {
117 		trigrams[i] = hexptr[i] - '0';
118 		if (trigrams[i] == 6 || trigrams[i] == 9)
119 			moving[i] = 1;
120 		else
121 			moving[i] = 0;
122 	}
123 	if ((chingf = fopen(_PATH_HEX, "r")) == (FILE *)NULL) {
124 		fprintf(stderr, "ching: can't read %s\n", _PATH_HEX);
125 		exit(2);
126 	}
127 	phx(doahex(), 0);
128 	if (changes())
129 		phx(doahex(), 1);
130 	exit(0);
131 }
132 
133 /*
134  * Compute the hexagram number, given the trigrams.
135  */
136 static int
137 doahex(void)
138 {
139 	int lower, upper;	/* encoded values of lower and upper trigrams */
140 	int lnum = 0, unum = 0;	/* indices of upper and lower trigrams */
141 	int i;
142 
143 	lower = codem(0);
144 	upper = codem(3);
145 	for (i = 0; i < 8; i++) {
146 		if (table[i].lines == lower)
147 			 lnum = table[i].trinum;
148 		if (table[i].lines == upper)
149 			 unum = table[i].trinum;
150 	}
151 	return(crosstab[lnum][unum]);
152 }
153 
154 /*
155  * Encode a trigram as a 3-digit number; the digits, from left to right,
156  * represent the lines.  7 is a solid (yang) line, 8 is a broken (yin) line.
157  */
158 static int
159 codem(int a)
160 {
161 	int code, i;
162 	int factor[3];
163 
164 	factor[0] = 1;
165 	factor[1] = 10;
166 	factor[2] = 100;
167 	code = 0;
168 
169 	for (i = a; i < a + 3; i++) {
170 		switch(trigrams[i]) {
171 
172 		case YYANG:
173 		case OYANG:
174 			code += factor[i%3]*7;
175 			break;
176 
177 		case OYIN:
178 		case YYIN:
179 			code += factor[i%3]*8;
180 			break;
181 		}
182 	}
183 	return(code);
184 }
185 
186 /*
187  * Compute the changes based on moving lines; return 1 if any lines moved,
188  * 0 if no lines moved.
189  */
190 static int
191 changes(void)
192 {
193 	int cflag;
194 	int i;
195 
196 	cflag = 0;
197 	for (i = 0; i < 6; i++) {
198 		if (trigrams[i] == OYIN) {
199 			trigrams[i] = YYANG;
200 			cflag++;
201 		} else if (trigrams[i] == OYANG) {
202 			trigrams[i] = YYIN;
203 			cflag++;
204 		}
205 	}
206 	return(cflag);
207 }
208 
209 /*
210  * Print the NROFF/TROFF source of a hexagram, given the hexagram number;
211  * if flag is 0, print the entire source; if flag is 1, ignore the meanings
212  * of the lines.
213  */
214 static void
215 phx(int hexagram, int flag)
216 {
217 	char textln[128+1];		/* buffer for text line */
218 	char *lp;			/* pointer into buffer */
219 	int thishex;			/* number of hexagram just read */
220 	int lineno;			/* number of line read in */
221 	int allmoving;			/* 1 if all lines are moving */
222 	int i;
223 
224 	/*
225 	 * Search for the hexagram; it begins with a line of the form
226 	 * .H <hexagram number> <other data>.
227 	 */
228 	rewind(chingf);
229 	for (;;) {
230 		if (fgets(textln, sizeof(textln), chingf) == (char *)NULL) {
231 			fprintf(stderr, "ching: Hexagram %d missing\n",
232 			    hexagram);
233 			exit(3);
234 		}
235 		lp = &textln[0];
236 		if (*lp++ != '.' || *lp++ != 'H')
237 			continue;
238 		while (*lp++ == ' ')
239 			;
240 		lp--;
241 		thishex = atoi(lp);
242 		if (thishex < 1 || thishex > 64)
243 			continue;
244 		if (thishex == hexagram)
245 			break;
246 	}
247 
248 	/*
249 	 * Print up to the line commentary, which ends with a line of the form
250 	 * .L <position> <value>
251 	 */
252 	fputs(textln, stdout);
253 	for (;;) {
254 		if (fgets(textln, sizeof(textln), chingf) == (char *)NULL) {
255 			fprintf(stderr, "ching: Hexagram %d malformed\n",
256 			    hexagram);
257 			exit(3);
258 		}
259 		lp = &textln[0];
260 		if (*lp++ == '.') {
261 			if (*lp++ == 'L')
262 				break;
263 		}
264 		fputs(textln, stdout);
265 	}
266 
267 	/*
268 	 * Now print the line commentaries, if this is the first hexagram.
269 	 */
270 	if (flag)
271 		return;
272 
273 	/*
274 	 * If a line is moving, print its commentary.
275 	 * The text of the commentary ends with a line either of the form
276 	 * .L <position> <value>
277 	 * or of the form
278 	 * .LA <value>
279 	 * or of the form
280 	 * .H <hexagram number> <other arguments>
281 	 */
282 	allmoving = 1;
283 	for (i = 0; i < 6; i++) {
284 		while (*lp++ == ' ')
285 			;
286 		lp--;
287 		lineno = atoi(lp);
288 		if (i + 1 != lineno) {
289 			fprintf(stderr, "ching: Hexagram %d malformed\n",
290 			    hexagram);
291 			exit(3);
292 		}
293 		if (moving[i])
294 			fputs(textln, stdout);
295 		else
296 			allmoving = 0;
297 		for (;;) {
298 			if (fgets(textln, sizeof(textln), chingf) == (char *)NULL)
299 				break;
300 			lp = &textln[0];
301 			if (*lp++ == '.' && (*lp == 'L' || *lp == 'H')) {
302 				lp++;
303 				break;
304 			}
305 			if (moving[i])
306 				fputs(textln, stdout);
307 		}
308 	}
309 
310 	/*
311 	 * If all the lines are moving, print the commentary for that; it
312 	 * ends with a line of the form
313 	 * .H <hexagram number> <other arguments>
314 	 */
315 	if (*lp == 'A' && allmoving) {
316 		fputs(textln, stdout);
317 		for (;;) {
318 			if (fgets(textln, sizeof(textln), chingf) == (char *)NULL)
319 				break;
320 			lp = &textln[0];
321 			if (*lp++ == '.' || *lp++ == 'H')
322 				break;
323 			fputs(textln, stdout);
324 		}
325 	}
326 }
327