* Copyright (c) 1988 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
"@(#) Copyright (c) 1988 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)ching.phx.c 5.3 (Berkeley) %G%";
* phx - Print NROFF/TROFF source of change, given the line values.
int lines
; /* encoded value of lines */
int trinum
; /* trigram number */
* Gives hexagram number from two component trigrams.
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,
FILE *chingf
; /* stream to read the hexagram file */
register int hexagram
; /* hexagram number */
register char *hexptr
; /* pointer to string of lines */
char hexstr
[6+1]; /* buffer for reading lines in */
if (hexptr
== (char *)NULL
|| strlen(hexptr
) != 6) {
fprintf(stderr
, "What kind of a change is THAT?!?\n");
for (i
= 0; i
< 6; i
++) {
trigrams
[i
] = hexptr
[i
] - '0';
if (trigrams
[i
] == 6 || trigrams
[i
] == 9)
if ((chingf
= fopen(_PATH_HEX
, "r")) == (FILE *)NULL
) {
fprintf(stderr
, "ching: can't read %s\n", _PATH_HEX
);
* Compute the hexagram number, given the trigrams.
int lower
, upper
; /* encoded values of lower and upper trigrams */
int lnum
, unum
; /* indices of upper and lower trigrams */
for (i
= 0; i
< 8; i
++) {
if (table
[i
].lines
== lower
)
if (table
[i
].lines
== upper
)
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.
for (i
= a
; i
< a
+ 3; i
++) {
* Compute the changes based on moving lines; return 1 if any lines moved,
for (i
= 0; i
< 6; i
++) {
if (trigrams
[i
] == OYIN
) {
} else if (trigrams
[i
] == OYANG
) {
* 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
char textln
[128+1]; /* buffer for text line */
register char *lp
; /* pointer into buffer */
register int thishex
; /* number of hexagram just read */
int lineno
; /* number of line read in */
int allmoving
; /* 1 if all lines are moving */
* Search for the hexagram; it begins with a line of the form
* .H <hexagram number> <other data>.
if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
) {
fprintf(stderr
, "ching: Hexagram %d missing\n",
if (*lp
++ != '.' || *lp
++ != 'H')
if (thishex
< 1 || thishex
> 64)
* Print up to the line commentary, which ends with a line of the form
if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
) {
fprintf(stderr
, "ching: Hexagram %d malformed\n",
* Now print the line commentaries, if this is the first hexagram.
* If a line is moving, print its commentary.
* The text of the commentary ends with a line either of the form
* .H <hexagram number> <other arguments>
for (i
= 0; i
< 6; i
++) {
fprintf(stderr
, "ching: Hexagram %d malformed\n",
if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
)
if (*lp
++ == '.' && (*lp
== 'L' || *lp
== 'H')) {
* 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
) {
if (fgets(textln
, sizeof(textln
), chingf
) == (char *)NULL
)
if (*lp
++ == '.' || *lp
++ == 'H')