#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
The Regents of the University of California. All rights reserved.");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)ching.phx.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: printching.c,v 1.5 2011/08/31 16:24:55 plunky Exp $");
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ching.h"
#include "pathnames.h"
static int changes(void);
static int codem(int a);
static int doahex(void);
static void phx(int hexagram, int flag);
static const struct {
int lines;
int trinum;
} table[] = {
{ 777, 0 },
{ 887, 1 },
{ 878, 2 },
{ 788, 3 },
{ 888, 4 },
{ 778, 5 },
{ 787, 6 },
{ 877, 7 },
};
static const 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}
};
static int trigrams[6];
static int moving[6];
static FILE *chingf;
int
main(int argc, char **argv)
{
char *hexptr;
char hexstr[6+1];
int i;
if (argc < 2)
hexptr = fgets(hexstr, 6+1, stdin);
else
hexptr = argv[1];
if (hexptr == 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")) == NULL) {
fprintf(stderr, "ching: can't read %s\n", _PATH_HEX);
exit(2);
}
phx(doahex(), 0);
if (changes())
phx(doahex(), 1);
exit(0);
}
static int
doahex(void)
{
int lower, upper;
int lnum = 0, unum = 0;
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]);
}
static 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);
}
static 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);
}
static void
phx(int hexagram, int flag)
{
char textln[128+1];
char *lp;
int thishex;
int lineno;
int allmoving;
int i;
rewind(chingf);
for (;;) {
if (fgets(textln, sizeof(textln), chingf) == 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;
}
fputs(textln, stdout);
for (;;) {
if (fgets(textln, sizeof(textln), chingf) == NULL) {
fprintf(stderr, "ching: Hexagram %d malformed\n",
hexagram);
exit(3);
}
lp = &textln[0];
if (*lp++ == '.') {
if (*lp++ == 'L')
break;
}
fputs(textln, stdout);
}
if (flag)
return;
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) == NULL)
break;
lp = &textln[0];
if (*lp++ == '.' && (*lp == 'L' || *lp == 'H')) {
lp++;
break;
}
if (moving[i])
fputs(textln, stdout);
}
}
if (*lp == 'A' && allmoving) {
fputs(textln, stdout);
for (;;) {
if (fgets(textln, sizeof(textln), chingf) == NULL)
break;
lp = &textln[0];
if (*lp++ == '.' || *lp++ == 'H')
break;
fputs(textln, stdout);
}
}
}