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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
|
/*
* Life search program - user interactions module.
* Author: David I. Bell.
*/
#include "lifesrc.h"
/*
* Local procedures
*/
static void usage();
static void getsetting();
static void clearunknowns();
static void writegen();
static STATUS loadstate();
static STATUS readfile();
static BOOL confirm();
static long getnum();
static char *getstr();
main(argc, argv)
char **argv;
{
char *str;
STATUS status;
if (--argc <= 0) {
usage();
exit(1);
}
argv++;
while (argc-- > 0) {
str = *argv++;
if (*str++ != '-') {
usage();
exit(1);
}
switch (*str++) {
case 'r': /* rows */
rowmax = atoi(str);
break;
case 'c': /* columns */
colmax = atoi(str);
break;
case 'g': /* generations */
genmax = atoi(str);
break;
case 't': /* translation */
switch (*str++) {
case 'r':
rowtrans = atoi(str);
break;
case 'c':
coltrans = atoi(str);
break;
default:
fprintf(stderr, "Bad translate\n");
exit(1);
}
break;
case 's': /* symmetry */
switch (*str++) {
case 'r':
rowsym = TRUE;
break;
case 'c':
colsym = TRUE;
break;
default:
fprintf(stderr, "Bad symmetry\n");
exit(1);
}
break;
case 'd': /* dump frequency */
dumpfreq = atol(str) * DUMPMULT;
dumpfile = DUMPFILE;
if ((argc > 0) && (**argv != '-')) {
argc--;
dumpfile = *argv++;
}
break;
case 'v': /* view frequency */
viewfreq = atol(str) * VIEWMULT;
break;
case 'l': /* load file */
if (*str == 'n')
nowait = TRUE;
if ((argc <= 0) || (**argv == '-')) {
fprintf(stderr, "Missing load file name\n");
exit(1);
}
loadfile = *argv++;
argc--;
break;
case 'i': /* initial file */
if (*str == 'a')
setall = TRUE;
if ((argc <= 0) || (**argv == '-')) {
fprintf(stderr, "Missing initial file name\n");
exit(1);
}
initfile = *argv++;
argc--;
break;
case 'o': /* output file */
if ((argc <= 0) || (**argv == '-')) {
fprintf(stderr, "Missing output file name\n");
exit(1);
}
outputfile = *argv++;
argc--;
break;
case 'm': /* max cell count */
maxcount = atoi(str);
break;
case 'p': /* find parents only */
parent = TRUE;
break;
case 'a':
allobjects = TRUE; /* find all objects */
break;
case 'D': /* debugging output */
debug = TRUE;
break;
default:
fprintf(stderr, "Unknown option -%c\n",
str[-1]);
exit(1);
}
}
if (parent && (rowtrans || coltrans)) {
fprintf(stderr, "Cannot specify -tr or -tc with -p\n");
exit(1);
}
if (ttyopen()) {
fprintf(stderr, "Cannot initialize terminal\n");
exit(1);
}
/*
* Check for loading state from file or reading initial
* object from file.
*/
if (loadfile) {
if (loadstate(loadfile) != OK) {
ttyclose();
exit(1);
}
} else {
initcells();
if (initfile) {
if (readfile(initfile) != OK) {
ttyclose();
exit(1);
}
baseset = nextset;
}
}
/*
* If we are looking for parents, then set the current generation
* to the last one so that it can be input easily. Then get the
* commands to initialize the cells, unless we were told to not wait.
*/
if (parent)
curgen = genmax - 1;
if (nowait)
printgen(0);
else
getcommands();
/*
* Initial commands are complet, now look for the object.
*/
while (TRUE) {
if (curstatus == OK)
curstatus = search();
if (dumpfreq) {
dumpcount = 0;
dumpstate(dumpfile);
}
if ((curstatus == FOUND) && !allobjects && subperiods()) {
curstatus = OK;
continue;
}
curgen = 0;
if (outputfile == NULL) {
getcommands();
continue;
}
/*
* Here if results are going to a file.
*/
if (curstatus == FOUND) {
curstatus = OK;
printgen(0);
ttystatus("Object %ld found.\n", ++foundcount);
writegen(outputfile, TRUE);
continue;
}
if (foundcount == 0) {
ttyclose();
fprintf(stderr, "No objects found\n");
exit(1);
}
ttyclose();
printf("Search completed, file \"%s\" contains %ld object%s\n",
outputfile, foundcount, (foundcount == 1) ? "" : "s");
exit(0);
}
}
/*
* Get one or more user commands.
* Commands are ended by a blank line.
*/
void
getcommands()
{
char *cp;
char *cmd;
char buf[LINESIZE];
dumpcount = 0;
viewcount = 0;
printgen(curgen);
while (TRUE) {
ttyread("> ", buf, LINESIZE);
cp = buf;
while (isblank(*cp))
cp++;
cmd = cp;
if (*cp)
cp++;
while (isblank(*cp))
cp++;
switch (*cmd) {
case 'p': /* print previous generation */
printgen((curgen + genmax - 1) % genmax);
break;
case 'n': /* print next generation */
printgen((curgen + 1) % genmax);
break;
case 's': /* add a cell setting */
getsetting(cp);
break;
case 'c': /* clear rest of generation */
if (confirm("Clear all unknown cells? "))
clearunknowns();
break;
case 'v': /* set view frequency */
viewfreq = atol(cp) * VIEWMULT;
printgen(curgen);
break;
case 'w': /* write generation to file */
writegen(cp, FALSE);
break;
case 'd': /* dump state to file */
dumpstate(cp);
break;
case 'N': /* find next object */
if (curstatus == FOUND)
curstatus = OK;
return;
case 'q': /* quit program */
case 'Q':
if (confirm("Really quit? ")) {
ttyclose();
exit(0);
}
break;
case '\n': /* return from commands */
case '\0':
return;
default:
if (isdigit(*cmd)) {
getsetting(cmd);
break;
}
ttystatus("Unknown command\n");
break;
}
}
}
/*
* Get a cell to be set in the current generation.
* The state of the cell is defaulted to ON.
* Warning: Use of this routine invalidates backing up over
* the setting, so that the setting is permanent.
*/
static void
getsetting(cp)
char *cp;
{
int row;
int col;
STATE state;
cp = getstr(cp, "Cell to set (row col [state]): ");
if (*cp == '\0')
return;
row = getnum(&cp, -1);
if (*cp == ',')
cp++;
col = getnum(&cp, -1);
if (*cp == ',')
cp++;
state = getnum(&cp, 1);
while (isblank(*cp))
cp++;
if (*cp != '\0') {
ttystatus("Bad input line format\n");
return;
}
if ((row <= 0) || (row > rowmax) || (col <= 0) || (col > colmax) ||
((state != 0) && (state != 1)))
{
ttystatus("Illegal cell value\n");
return;
}
if (proceed(findcell(row, col, curgen), state, FALSE) != OK) {
ttystatus("Inconsistent state for cell\n");
return;
}
baseset = nextset;
printgen(curgen);
}
/*
* Clear all remaining unknown cells in the current generation.
* This is used when searching for parents of a configuration.
*/
static void
clearunknowns()
{
int row;
int col;
CELL *cell;
for (row = 1; row <= rowmax; row++) {
for (col = 1; col <= colmax; col++) {
cell = findcell(row, col, curgen);
if (cell->state != UNK)
continue;
if (proceed(cell, OFF, FALSE) != OK)
{
ttystatus("Inconsistent state for cell\n");
return;
}
}
}
baseset = nextset;
printgen(curgen);
}
/*
* Print out the current status of the specified generation.
* This also sets the current generation.
*/
void
printgen(gen)
{
int row;
int col;
int count;
CELL *cell;
char *msg;
curgen = gen;
switch (curstatus) {
case NOTEXIST: msg = "No such object"; break;
case FOUND: msg = "Found object"; break;
default: msg = ""; break;
}
count = 0;
for (row = 1; row <= rowmax; row++) {
for (col = 1; col <= colmax; col++) {
count += (findcell(row, col, gen)->state == ON);
}
}
ttyhome();
ttyeeop();
ttyprintf("%s (gen %d, cells %d) -g%d", msg, gen, count, genmax);
if (rowtrans)
ttyprintf(" -tr%d", rowtrans);
if (coltrans)
ttyprintf(" -tc%d", coltrans);
if (rowsym)
ttyprintf(" -sr");
if (colsym)
ttyprintf(" -sc");
if (parent)
ttyprintf(" -p");
if (allobjects)
ttyprintf(" -a");
if (maxcount)
ttyprintf(" -m%d", maxcount);
if (viewfreq)
ttyprintf(" -v%ld", viewfreq / VIEWMULT);
if (dumpfreq)
ttyprintf(" -d%ld %s", dumpfreq / DUMPMULT, dumpfile);
if (outputfile)
ttyprintf(" -o %s", outputfile);
ttyprintf("\n");
for (row = 1; row <= rowmax; row++) {
for (col = 1; col <= colmax; col++) {
cell = findcell(row, col, gen);
switch (cell->state) {
case OFF: msg = ". "; break;
case ON: msg = "O "; break;
case UNK: msg = "? "; break;
}
/*
* If wide output, print only one character,
* else print both characters.
*/
ttywrite(msg, (colmax < 40) + 1);
}
ttywrite("\n", 1);
}
ttyhome();
ttyflush();
}
/*
* Write the current generation to the specified file.
* Empty rows and columns are not written.
* If no file is specified, it is asked for.
*/
static void
writegen(file, append)
char *file; /* file name (or NULL) */
BOOL append; /* TRUE to append instead of create */
{
FILE *fp;
CELL *cell;
int row;
int col;
int ch;
int minrow, maxrow, mincol, maxcol;
file = getstr(file, "Write object to file: ");
if (*file == '\0')
return;
fp = fopen(file, append ? "a" : "w");
if (fp == NULL) {
ttystatus("Cannot create \"%s\"\n", file);
return;
}
/*
* First find the minimum bounds on the object.
*/
minrow = rowmax;
mincol = colmax;
maxrow = 1;
maxcol = 1;
for (row = 1; row <= rowmax; row++) {
for (col = 1; col <= colmax; col++) {
cell = findcell(row, col, curgen);
if (cell->state == OFF)
continue;
if (row < minrow)
minrow = row;
if (row > maxrow)
maxrow = row;
if (col < mincol)
mincol = col;
if (col > maxcol)
maxcol = col;
}
}
if (minrow > maxrow) {
minrow = 1;
maxrow = 1;
mincol = 1;
maxcol = 1;
}
/*
* Now write out the bounded area.
*/
for (row = minrow; row <= maxrow; row++) {
for (col = mincol; col <= maxcol; col++) {
cell = findcell(row, col, curgen);
switch (cell->state) {
case OFF: ch = '.'; break;
case ON: ch = '*'; break;
case UNK: ch = '?'; break;
}
fputc(ch, fp);
}
fputc('\n', fp);
}
if (append)
fprintf(fp, "\n");
if (fclose(fp))
ttystatus("Error writing \"%s\"\n", file);
else
ttystatus("\"%s\" written\n", file);
}
/*
* Dump the current state of the search in the specified file.
* If no file is specified, it is asked for.
*/
void
dumpstate(file)
char *file;
{
FILE *fp;
CELL **set;
CELL *cell;
file = getstr(file, "Dump state to file: ");
if (*file == '\0')
return;
fp = fopen(file, "w");
if (fp == NULL) {
ttystatus("Cannot create \"%s\"\n", file);
return;
}
fprintf(fp, "V %d\n", DUMPVERSION);
fprintf(fp, "I %d %d %d %d %d %d %d %d %d %d %d %d\n", rowmax, colmax,
genmax, rowtrans, coltrans, rowsym, colsym, parent,
allobjects, maxcount, cellcount, curstatus);
set = settable;
while (set != nextset) {
cell = *set++;
fprintf(fp, "S %d %d %d %d %d\n", cell->row, cell->col,
cell->gen, cell->state, cell->free);
}
fprintf(fp, "T %d %d\n", baseset - settable, nextset - settable);
fprintf(fp, "E\n");
if (fclose(fp)) {
ttystatus("Error writing \"%s\"\n", file);
return;
}
ttystatus("State dumped to \"%s\"\n", file);
}
/*
* Load a previously dumped state from a file.
* Warning: Almost no checks are made for validity of the state.
* Returns OK on success, ERROR on failure.
*/
static STATUS
loadstate(file)
char *file;
{
FILE *fp;
char *cp;
int row;
int col;
int gen;
STATE state;
BOOL free;
CELL *cell;
char buf[LINESIZE];
file = getstr(file, "Load state from file: ");
if (*file == '\0')
return OK;
fp = fopen(file, "r");
if (fp == NULL) {
ttystatus("Cannot open state file \"%s\"\n", file);
return ERROR;
}
buf[0] = '\0';
fgets(buf, LINESIZE, fp);
if (buf[0] != 'V') {
ttystatus("Missing version line in file \"%s\"\n", file);
fclose(fp);
return ERROR;
}
cp = &buf[1];
if (getnum(&cp, 0) != DUMPVERSION) {
ttystatus("Unknown version in state file \"%s\"\n", file);
fclose(fp);
return ERROR;
}
fgets(buf, LINESIZE, fp);
if (buf[0] != 'I') {
ttystatus("Missing init line in state file\n");
fclose(fp);
return ERROR;
}
cp = &buf[1];
rowmax = getnum(&cp, 0);
colmax = getnum(&cp, 0);
genmax = getnum(&cp, 0);
rowtrans = getnum(&cp, 0);
coltrans = getnum(&cp, 0);
rowsym = getnum(&cp, 0);
colsym = getnum(&cp, 0);
parent = getnum(&cp, 0);
allobjects = getnum(&cp, 0);
maxcount = getnum(&cp, 0);
cellcount = getnum(&cp, 0);
curstatus = getnum(&cp, 0);
initcells();
newset = settable;
for (;;) {
buf[0] = '\0';
fgets(buf, LINESIZE, fp);
if (buf[0] != 'S')
break;
cp = &buf[1];
row = getnum(&cp, 0);
col = getnum(&cp, 0);
gen = getnum(&cp, 0);
state = getnum(&cp, 0);
free = getnum(&cp, 0);
cell = findcell(row, col, gen);
cell->state = state;
cell->free = free;
*newset++ = cell;
}
if (buf[0] != 'T') {
ttystatus("Missing table line in state file\n");
fclose(fp);
return ERROR;
}
cp = &buf[1];
baseset = &settable[getnum(&cp, 0)];
nextset = &settable[getnum(&cp, 0)];
fgets(buf, LINESIZE, fp);
if (buf[0] != 'E') {
ttystatus("Missing end of file line in state file\n");
fclose(fp);
return ERROR;
}
if (fclose(fp)) {
ttystatus("Error reading \"%s\"\n", file);
return ERROR;
}
ttystatus("State loaded from \"%s\"\n", file);
return OK;
}
/*
* Read a file containing initial settings for either gen 0 or the last gen.
* If setall is TRUE, both the ON and the OFF cells will be set.
* Returns OK on success, ERROR on error.
*/
static STATUS
readfile(file)
char *file;
{
FILE *fp;
char *cp;
char ch;
int row;
int col;
int gen;
STATE state;
char buf[LINESIZE];
file = getstr(file, "Read initial object from file: ");
if (*file == '\0')
return OK;
fp = fopen(file, "r");
if (fp == NULL) {
ttystatus("Cannot open \"%s\"\n", file);
return ERROR;
}
gen = (parent ? (genmax - 1) : 0);
row = 0;
while (fgets(buf, LINESIZE, fp)) {
row++;
cp = buf;
col = 0;
while (*cp && (*cp != '\n')) {
col++;
ch = *cp++;
switch (ch) {
case '?':
continue;
case '.':
case ' ':
if (!setall)
continue;
state = OFF;
break;
case 'O':
case 'o':
case '*':
state = ON;
break;
default:
ttystatus("Bad file format in line %d\n",
row);
fclose(fp);
return ERROR;
}
if (proceed(findcell(row, col, gen), state, FALSE)
!= OK)
{
ttystatus("Inconsistent state for cell %d %d\n",
row, col);
fclose(fp);
return ERROR;
}
}
}
if (fclose(fp)) {
ttystatus("Error reading \"%s\"\n", file);
return ERROR;
}
return OK;
}
/*
* Check a string for being NULL, and if so, ask the user to specify a
* value for it. Returned string may be static and thus is overwritten
* for each call. Leading spaces in the string are skipped over.
*/
static char *
getstr(str, prompt)
char *str; /* string to check for NULLness */
char *prompt; /* message to prompt with */
{
static char buf[LINESIZE];
if ((str == NULL) || (*str == '\0')) {
ttyread(prompt, buf, LINESIZE);
str = buf;
}
while (isblank(*str))
str++;
return str;
}
/*
* Confirm an action by prompting with the specified string and reading
* an answer. Entering 'y' or 'Y' indicates TRUE, everything else FALSE.
*/
static BOOL
confirm(prompt)
char *prompt;
{
char ch;
ch = *getstr(NULL, prompt);
if ((ch == 'y') || (ch == 'Y'))
return TRUE;
return FALSE;
}
/*
* Read a number from a string, eating any leading or trailing blanks.
* Returns the value, and indirectly updates the string pointer.
* Returns specified default if no number was found.
*/
static long
getnum(cpp, defnum)
char **cpp;
int defnum;
{
char *cp;
long num;
cp = *cpp;
while (isblank(*cp))
cp++;
if (!isdigit(*cp)) {
*cpp = cp;
return defnum;
}
num = 0;
while (isdigit(*cp))
num = num * 10 + (*cp++ - '0');
while (isblank(*cp))
cp++;
*cpp = cp;
return num;
}
/*
* Print usage text.
*/
static void
usage()
{
char **cpp;
static char *text[] = {
"Program to search for life oscillators or spaceships.",
"",
"lifesrc -r# -c# -g# -tr# -tc# -m# -sr -sc -p -a -v# -i[a] file -o file -d# file",
"lifesrc -l[n] file -v# -o file -d# file",
"",
" -r Number of rows",
" -c Number of columns",
" -g Number of generations",
" -tr Translate rows between last and first generation",
" -tc Translate columns between last and first generation",
" -m Maximum live cells for generation 0",
" -sr Enforce symmetry on rows",
" -sc Enforce symmetry on columns",
" -p Only look for parents of last generation",
" -a Find all objects (even those with subperiods)",
" -v View object every N thousand searches",
" -d Dump status to file every N thousand searches",
" -l Load status from file",
" -ln Load status without entering command mode",
" -i Read initial object from file setting only ON cells",
" -ia Read initial object setting both ON and OFF cells",
" -o Output found objects to file (appending)",
NULL
};
for (cpp = text; *cpp; cpp++)
fprintf(stderr, "%s\n", *cpp);
}
/* END CODE */
|