summaryrefslogtreecommitdiffstats
path: root/ching/printching/printching.c
blob: 3f70d9417822b873c41c030a3fd0f96b17d88572 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*	$NetBSD: printching.c,v 1.2 2005/06/30 13:44:48 martin Exp $	*/

/*
 * Copyright (c) 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Guy Harris.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
	The Regents of the University of California.  All rights reserved.\n");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)ching.phx.c	8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: printching.c,v 1.2 2005/06/30 13:44:48 martin Exp $");
#endif
#endif /* not lint */

/*
 * printching - Print NROFF/TROFF source of change, given the line values.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ching.h"
#include "pathnames.h"

int changes(void);
int codem(int a);
int doahex(void);
void phx(int hexagram, int flag);

struct {
	int	lines;		/* encoded value of lines */
	int	trinum;		/* trigram number */
} table[] = {
	{ 777, 0 },		/* 1 */
	{ 887, 1 },		/* 4 */
	{ 878, 2 },		/* 6 */
	{ 788, 3 },		/* 7 */
	{ 888, 4 },		/* 8 */
	{ 778, 5 },		/* 5 */
	{ 787, 6 },		/* 3 */
	{ 877, 7 },		/* 2 */
};

/*
 * Gives hexagram number from two component trigrams.
 */
int	crosstab[8][8] = {
	{1,  34, 5,  26, 11, 9,  14, 43},
	{25, 51, 3,  27, 24, 42, 21, 17},
	{6,  40, 29, 4,  7,  59, 64, 47},
	{33, 62, 39, 52, 15, 53, 56, 31},
	{12, 16, 8,  23, 2,  20, 35, 45},
	{44, 32, 48, 18, 46, 57, 50, 28},
	{13, 55, 63, 22, 36, 37, 30, 49},
	{10, 54, 60, 41, 19, 61, 38, 58}
};

int	trigrams[6];
int	moving[6];

FILE	*chingf;		/* stream to read the hexagram file */

/*ARGSUSED*/
int
main(int argc, char **argv)
{
	char *hexptr;		/* pointer to string of lines */
	char hexstr[6+1];	/* buffer for reading lines in */
	int i;

	if (argc < 2)
		hexptr = fgets(hexstr, 6+1, stdin);
	else
		hexptr = argv[1];
	if (hexptr == (char *)NULL || strlen(hexptr) != 6) {
		fprintf(stderr, "What kind of a change is THAT?!?\n");
		exit(1);
	}
	for (i = 0; i < 6; i++) {
		trigrams[i] = hexptr[i] - '0';
		if (trigrams[i] == 6 || trigrams[i] == 9)
			moving[i] = 1;
		else
			moving[i] = 0;
	}
	if ((chingf = fopen(_PATH_HEX, "r")) == (FILE *)NULL) {
		fprintf(stderr, "ching: can't read %s\n", _PATH_HEX);
		exit(2);
	}
	phx(doahex(), 0);
	if (changes())
		phx(doahex(), 1);
	exit(0);
}

/*
 * Compute the hexagram number, given the trigrams.
 */
int
doahex(void)
{
	int lower, upper;	/* encoded values of lower and upper trigrams */
	int lnum = 0, unum = 0;	/* indices of upper and lower trigrams */
	int i;

	lower = codem(0);
	upper = codem(3);
	for (i = 0; i < 8; i++) {
		if (table[i].lines == lower)
			 lnum = table[i].trinum;
		if (table[i].lines == upper)
			 unum = table[i].trinum;
	}
	return(crosstab[lnum][unum]);
}

/*
 * Encode a trigram as a 3-digit number; the digits, from left to right,
 * represent the lines.  7 is a solid (yang) line, 8 is a broken (yin) line.
 */
int
codem(int a)
{
	int code, i;
	int factor[3];

	factor[0] = 1;
	factor[1] = 10;
	factor[2] = 100;
	code = 0;

	for (i = a; i < a + 3; i++) {
		switch(trigrams[i]) {

		case YYANG:
		case OYANG:
			code += factor[i%3]*7;
			break;

		case OYIN:
		case YYIN:
			code += factor[i%3]*8;
			break;
		}
	}
	return(code);
}

/*
 * Compute the changes based on moving lines; return 1 if any lines moved,
 * 0 if no lines moved.
 */
int
changes(void)
{
	int cflag;
	int i;

	cflag = 0;
	for (i = 0; i < 6; i++) {
		if (trigrams[i] == OYIN) {
			trigrams[i] = YYANG;
			cflag++;
		} else if (trigrams[i] == OYANG) {
			trigrams[i] = YYIN;
			cflag++;
		}
	}
	return(cflag);
}

/*
 * Print the NROFF/TROFF source of a hexagram, given the hexagram number;
 * if flag is 0, print the entire source; if flag is 1, ignore the meanings
 * of the lines.
 */
void
phx(int hexagram, int flag)
{
	char textln[128+1];		/* buffer for text line */
	char *lp;			/* pointer into buffer */
	int thishex;			/* number of hexagram just read */
	int lineno;			/* number of line read in */
	int allmoving;			/* 1 if all lines are moving */
	int i;

	/*
	 * Search for the hexagram; it begins with a line of the form
	 * .H <hexagram number> <other data>.
	 */
	rewind(chingf);
	for (;;) {
		if (fgets(textln, sizeof(textln), chingf) == (char *)NULL) {
			fprintf(stderr, "ching: Hexagram %d missing\n",
			    hexagram);
			exit(3);
		}
		lp = &textln[0];
		if (*lp++ != '.' || *lp++ != 'H')
			continue;
		while (*lp++ == ' ')
			;
		lp--;
		thishex = atoi(lp);
		if (thishex < 1 || thishex > 64)
			continue;
		if (thishex == hexagram)
			break;
	}

	/*
	 * Print up to the line commentary, which ends with a line of the form
	 * .L <position> <value>
	 */
	fputs(textln, stdout);
	for (;;) {
		if (fgets(textln, sizeof(textln), chingf) == (char *)NULL) {
			fprintf(stderr, "ching: Hexagram %d malformed\n",
			    hexagram);
			exit(3);
		}
		lp = &textln[0];
		if (*lp++ == '.') {
			if (*lp++ == 'L')
				break;
		}
		fputs(textln, stdout);
	}

	/*
	 * Now print the line commentaries, if this is the first hexagram.
	 */
	if (flag)
		return;

	/*
	 * If a line is moving, print its commentary.
	 * The text of the commentary ends with a line either of the form
	 * .L <position> <value>
	 * or of the form
	 * .LA <value>
	 * or of the form
	 * .H <hexagram number> <other arguments>
	 */
	allmoving = 1;
	for (i = 0; i < 6; i++) {
		while (*lp++ == ' ')
			;
		lp--;
		lineno = atoi(lp);
		if (i + 1 != lineno) {
			fprintf(stderr, "ching: Hexagram %d malformed\n",
			    hexagram);
			exit(3);
		}
		if (moving[i])
			fputs(textln, stdout);
		else
			allmoving = 0;
		for (;;) {
			if (fgets(textln, sizeof(textln), chingf) == (char *)NULL)
				break;
			lp = &textln[0];
			if (*lp++ == '.' && (*lp == 'L' || *lp == 'H')) {
				lp++;
				break;
			}
			if (moving[i])
				fputs(textln, stdout);
		}
	}

	/*
	 * If all the lines are moving, print the commentary for that; it
	 * ends with a line of the form
	 * .H <hexagram number> <other arguments>
	 */
	if (*lp == 'A' && allmoving) {
		fputs(textln, stdout);
		for (;;) {
			if (fgets(textln, sizeof(textln), chingf) == (char *)NULL)
				break;
			lp = &textln[0];
			if (*lp++ == '.' || *lp++ == 'H')
				break;
			fputs(textln, stdout);
		}
	}
}