1 /* $NetBSD: printching.c,v 1.3 2008/07/20 01:03:21 lukem Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
39 #include <sys/cdefs.h>
41 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
42 The Regents of the University of California. All rights reserved.");
47 static char sccsid
[] = "@(#)ching.phx.c 8.1 (Berkeley) 5/31/93";
49 __RCSID("$NetBSD: printching.c,v 1.3 2008/07/20 01:03:21 lukem Exp $");
54 * printching - Print NROFF/TROFF source of change, given the line values.
60 #include "pathnames.h"
62 static int changes(void);
63 static int codem(int a
);
64 static int doahex(void);
65 static void phx(int hexagram
, int flag
);
68 int lines
; /* encoded value of lines */
69 int trinum
; /* trigram number */
82 * Gives hexagram number from two component trigrams.
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}
95 static int trigrams
[6];
98 static FILE *chingf
; /* stream to read the hexagram file */
102 main(int argc
, char **argv
)
104 char *hexptr
; /* pointer to string of lines */
105 char hexstr
[6+1]; /* buffer for reading lines in */
109 hexptr
= fgets(hexstr
, 6+1, stdin
);
112 if (hexptr
== (char *)NULL
|| strlen(hexptr
) != 6) {
113 fprintf(stderr
, "What kind of a change is THAT?!?\n");
116 for (i
= 0; i
< 6; i
++) {
117 trigrams
[i
] = hexptr
[i
] - '0';
118 if (trigrams
[i
] == 6 || trigrams
[i
] == 9)
123 if ((chingf
= fopen(_PATH_HEX
, "r")) == (FILE *)NULL
) {
124 fprintf(stderr
, "ching: can't read %s\n", _PATH_HEX
);
134 * Compute the hexagram number, given the trigrams.
139 int lower
, upper
; /* encoded values of lower and upper trigrams */
140 int lnum
= 0, unum
= 0; /* indices of upper and lower trigrams */
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
;
151 return(crosstab
[lnum
][unum
]);
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.
169 for (i
= a
; i
< a
+ 3; i
++) {
170 switch(trigrams
[i
]) {
174 code
+= factor
[i
%3]*7;
179 code
+= factor
[i
%3]*8;
187 * Compute the changes based on moving lines; return 1 if any lines moved,
188 * 0 if no lines moved.
197 for (i
= 0; i
< 6; i
++) {
198 if (trigrams
[i
] == OYIN
) {
201 } else if (trigrams
[i
] == OYANG
) {
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
215 phx(int hexagram
, int flag
)
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 */
225 * Search for the hexagram; it begins with a line of the form
226 * .H <hexagram number> <other data>.
230 if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
) {
231 fprintf(stderr
, "ching: Hexagram %d missing\n",
236 if (*lp
++ != '.' || *lp
++ != 'H')
242 if (thishex
< 1 || thishex
> 64)
244 if (thishex
== hexagram
)
249 * Print up to the line commentary, which ends with a line of the form
250 * .L <position> <value>
252 fputs(textln
, stdout
);
254 if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
) {
255 fprintf(stderr
, "ching: Hexagram %d malformed\n",
264 fputs(textln
, stdout
);
268 * Now print the line commentaries, if this is the first hexagram.
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>
280 * .H <hexagram number> <other arguments>
283 for (i
= 0; i
< 6; i
++) {
288 if (i
+ 1 != lineno
) {
289 fprintf(stderr
, "ching: Hexagram %d malformed\n",
294 fputs(textln
, stdout
);
298 if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
)
301 if (*lp
++ == '.' && (*lp
== 'L' || *lp
== 'H')) {
306 fputs(textln
, stdout
);
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>
315 if (*lp
== 'A' && allmoving
) {
316 fputs(textln
, stdout
);
318 if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
)
321 if (*lp
++ == '.' || *lp
++ == 'H')
323 fputs(textln
, stdout
);