program.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* This file contains code for X-CHESS.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. This file is part of X-CHESS.
  4. X-CHESS is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the X-CHESS General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. X-CHESS, but only under the conditions described in the
  12. X-CHESS General Public License. A copy of this license is
  13. supposed to have been given to you along with X-CHESS so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* RCS Info: $Revision: 1.2 $ on $Date: 86/11/23 17:18:10 $
  18. * $Source: /users/faustus/xchess/RCS/program.c,v $
  19. * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
  20. * Permission is granted to do anything with this code except sell it
  21. * or remove this message.
  22. *
  23. * The interface to whichever chess playing program we are using...
  24. */
  25. #include "xchess.h"
  26. #include <signal.h>
  27. #include <sys/time.h>
  28. static int pid;
  29. static FILE *from, *to;
  30. bool
  31. program_init(name)
  32. char *name;
  33. {
  34. int toprog[2], fromprog[2];
  35. char buf[BSIZE];
  36. pipe(toprog);
  37. pipe(fromprog);
  38. if (!(pid = fork())) {
  39. /* Start up the program. */
  40. dup2(toprog[0], 0);
  41. dup2(fromprog[1], 1);
  42. close(toprog[0]);
  43. close(toprog[1]);
  44. close(fromprog[0]);
  45. close(fromprog[1]);
  46. if (proghost)
  47. execl("/usr/ucb/rsh", "rsh", proghost, name,
  48. (char *) NULL);
  49. else
  50. execl(name, name, (char *) NULL);
  51. perror(name);
  52. exit(1);
  53. }
  54. close(toprog[0]);
  55. close(fromprog[1]);
  56. from = fdopen(fromprog[0], "r");
  57. to = fdopen(toprog[1], "w");
  58. /* Get the first line... */
  59. fgets(buf, BSIZE, from);
  60. if (debug)
  61. fprintf(stderr, "program says %s", buf);
  62. if (blackflag) {
  63. fputs("switch\n", to);
  64. fflush(to);
  65. fgets(buf, BSIZE, from);
  66. if (debug)
  67. fprintf(stderr, "program says %s", buf);
  68. message_add(win1, "GNU Chess playing white\n", false);
  69. } else
  70. message_add(win1, "GNU Chess playing black\n", false);
  71. return (true);
  72. }
  73. void
  74. program_end()
  75. {
  76. fclose(from);
  77. fclose(to);
  78. kill(pid, SIGTERM);
  79. return;
  80. }
  81. void
  82. program_send(m)
  83. move *m;
  84. {
  85. char buf[BSIZE];
  86. if ((m->type == MOVE) || (m->type == CAPTURE))
  87. sprintf(buf, "%c%d%c%d\n", 'a' + m->fromx, SIZE - m->fromy,
  88. 'a' + m->tox, SIZE - m->toy);
  89. else if (m->type == KCASTLE)
  90. strcpy(buf, (m->piece.color == WHITE) ? "e1g1\n" : "e8g8\n");
  91. else if (m->type == QCASTLE)
  92. strcpy(buf, (m->piece.color == WHITE) ? "e1c1\n" : "e8c8\n");
  93. if (debug)
  94. fprintf(stderr, "sending program %s", buf);
  95. fputs(buf, to);
  96. fflush(to);
  97. /* One junk line... */
  98. fgets(buf, BSIZE, from);
  99. if (debug)
  100. fprintf(stderr, "program says %s", buf);
  101. return;
  102. }
  103. move *
  104. program_get()
  105. {
  106. int rfd = (1 << fileno(from)), wfd = 0, xfd = 0;
  107. static struct timeval notime = { 0, 0 };
  108. char buf[BSIZE], *s;
  109. move *m;
  110. int i, n;
  111. /* Do a poll... */
  112. if (!(i = select(32, &rfd, &wfd, &xfd, &notime)) &&
  113. !from->_cnt) { /* Bad stuff... */
  114. if (debug)
  115. fprintf(stderr, "poll: nothing\n");
  116. return (NULL);
  117. }
  118. if (i == -1) {
  119. perror("select");
  120. return (NULL);
  121. }
  122. fgets(buf, BSIZE, from);
  123. if (debug)
  124. fprintf(stderr, "got from program %s", buf);
  125. for (s = buf; !isalpha(*s); s++)
  126. ;
  127. m = parse_imove(chessboard, s, nexttomove);
  128. if (!valid_move(m, chessboard)) {
  129. fprintf(stderr, "Error: move %s is invalid!!\n", buf);
  130. return (NULL);
  131. }
  132. /*
  133. fgets(buf, BSIZE, from);
  134. if (debug)
  135. fprintf(stderr, "program says %s", buf);
  136. */
  137. message_add(win1, buf, false);
  138. return (m);
  139. }
  140. void
  141. program_undo()
  142. {
  143. fputs("undo\n", to);
  144. return;
  145. }