- char line[GTP_BUFSIZE];
- char command[GTP_BUFSIZE];
- char *p;
- int i;
- int n;
- int status = GTP_OK;
-
- gtp_output_file = gtp_output;
-
- while (status == GTP_OK) {
- /* Read a line from gtp_input. */
- if (!fgets(line, GTP_BUFSIZE, gtp_input))
- break; /* EOF or some error */
-
- if (gtp_dump_commands) {
- fputs(line, gtp_dump_commands);
- fflush(gtp_dump_commands);
- }
-
- /* Preprocess the line. */
- for (i = 0, p = line; line[i]; i++) {
- char c = line[i];
- /* Convert HT (9) to SPACE (32). */
- if (c == 9)
- *p++ = 32;
- /* Remove CR (13) and all other control characters except LF (10). */
- else if ((c > 0 && c <= 9)
- || (c >= 11 && c <= 31)
- || c == 127)
- continue;
- /* Remove comments. */
- else if (c == '#')
- break;
- /* Keep ordinary text. */
- else
- *p++ = c;
- }
- /* Terminate string. */
- *p = 0;
-
- p = line;
-
- /* Look for an identification number. */
- if (sscanf(p, "%d%n", ¤t_id, &n) == 1)
- p += n;
- else
- current_id = -1; /* No identification number. */
-
- /* Look for command name. */
- if (sscanf(p, " %s %n", command, &n) < 1)
- continue; /* Whitespace only on this line, ignore. */
- p += n;
-
- /* Search the list of commands and call the corresponding function
+ char line[GTP_BUFSIZE];
+ char command[GTP_BUFSIZE];
+ char* p;
+ int i;
+ int n;
+ int status = GTP_OK;
+
+ gtp_output_file = gtp_output;
+
+ while (status == GTP_OK) {
+ /* Read a line from gtp_input. */
+ if (!fgets(line, GTP_BUFSIZE, gtp_input))
+ break; /* EOF or some error */
+
+ if (gtp_dump_commands) {
+ fputs(line, gtp_dump_commands);
+ fflush(gtp_dump_commands);
+ }
+
+ /* Preprocess the line. */
+ for (i = 0, p = line; line[i]; i++) {
+ char c = line[i];
+ /* Convert HT (9) to SPACE (32). */
+ if (c == 9)
+ *p++ = 32;
+ /* Remove CR (13) and all other control characters except LF (10). */
+ else if ((c > 0 && c <= 9)
+ || (c >= 11 && c <= 31)
+ || c == 127)
+ continue;
+ /* Remove comments. */
+ else if (c == '#')
+ break;
+ /* Keep ordinary text. */
+ else
+ *p++ = c;
+ }
+ /* Terminate string. */
+ *p = 0;
+
+ p = line;
+
+ /* Look for an identification number. */
+ if (sscanf(p, "%d%n", ¤t_id, &n) == 1)
+ p += n;
+ else
+ current_id = -1; /* No identification number. */
+
+ /* Look for command name. */
+ if (sscanf(p, " %s %n", command, &n) < 1)
+ continue; /* Whitespace only on this line, ignore. */
+ p += n;
+
+ /* Search the list of commands and call the corresponding function
- for (i = 0; commands[i].name != NULL; i++) {
- if (strcmp(command, commands[i].name) == 0) {
- status = (*commands[i].function)(p);
- break;
- }
+ for (i = 0; commands[i].name != NULL; i++) {
+ if (strcmp(command, commands[i].name) == 0) {
+ status = (*commands[i].function)(p);
+ break;
+ }
+ }
+ if (commands[i].name == NULL)
+ gtp_failure("unknown command");
+
+ if (status == GTP_FATAL)
+ gtp_panic();
- va_list ap;
- va_start(ap, fmt);
-
- for (; *fmt; ++fmt) {
- if (*fmt == '%') {
- switch (*++fmt) {
- case 'c':
- {
- /* rules of promotion => passed as int, not char */
- int c = va_arg(ap, int);
- putc(c, gtp_output_file);
- break;
- }
- case 'd':
- {
- int d = va_arg(ap, int);
- fprintf(gtp_output_file, "%d", d);
- break;
- }
- case 'f':
- {
- double f = va_arg(ap, double); /* passed as double, not float */
- fprintf(gtp_output_file, "%f", f);
- break;
- }
- case 's':
- {
- char *s = va_arg(ap, char *);
- fputs(s, gtp_output_file);
- break;
- }
- case 'm':
- {
- int m = va_arg(ap, int);
- int n = va_arg(ap, int);
- gtp_print_vertex(m, n);
- break;
- }
- case 'C':
- {
- int color = va_arg(ap, int);
- if (color == WHITE)
- fputs("white", gtp_output_file);
- else if (color == BLACK)
- fputs("black", gtp_output_file);
- else
- fputs("empty", gtp_output_file);
- break;
- }
- default:
- /* FIXME: Should go to `stderr' instead? */
- fprintf(gtp_output_file, "\n\nUnknown format character '%c'\n", *fmt);
- break;
- }
+ va_list ap;
+ va_start(ap, fmt);
+
+ for (; *fmt; ++fmt) {
+ if (*fmt == '%') {
+ switch (*++fmt) {
+ case 'c': {
+ /* rules of promotion => passed as int, not char */
+ int c = va_arg(ap, int);
+ putc(c, gtp_output_file);
+ break;
+ }
+ case 'd': {
+ int d = va_arg(ap, int);
+ fprintf(gtp_output_file, "%d", d);
+ break;
+ }
+ case 'f': {
+ double f = va_arg(ap, double); /* passed as double, not float */
+ fprintf(gtp_output_file, "%f", f);
+ break;
+ }
+ case 's': {
+ char* s = va_arg(ap, char*);
+ fputs(s, gtp_output_file);
+ break;
+ }
+ case 'm': {
+ int m = va_arg(ap, int);
+ int n = va_arg(ap, int);
+ gtp_print_vertex(m, n);
+ break;
+ }
+ case 'C': {
+ int color = va_arg(ap, int);
+ if (color == WHITE)
+ fputs("white", gtp_output_file);
+ else if (color == BLACK)
+ fputs("black", gtp_output_file);
+ else
+ fputs("empty", gtp_output_file);
+ break;
+ }
+ default:
+ /* FIXME: Should go to `stderr' instead? */
+ fprintf(gtp_output_file, "\n\nUnknown format character '%c'\n", *fmt);
+ break;
+ }
+ } else
+ putc(*fmt, gtp_output_file);
/* Convert a string describing a color, "b", "black", "w", or "white",
* to GNU Go's integer representation of colors. Return the number of
* characters read from the string s.
*/
/* Convert a string describing a color, "b", "black", "w", or "white",
* to GNU Go's integer representation of colors. Return the number of
* characters read from the string s.
*/
- char color_string[7];
- int i;
- int n;
-
- assert(gtp_boardsize > 0);
-
- if (sscanf(s, "%6s%n", color_string, &n) != 1)
- return 0;
-
- for (i = 0; i < (int) strlen(color_string); i++)
- color_string[i] = tolower((int) color_string[i]);
-
- if (strcmp(color_string, "b") == 0
- || strcmp(color_string, "black") == 0)
- *color = BLACK;
- else if (strcmp(color_string, "w") == 0
- || strcmp(color_string, "white") == 0)
- *color = WHITE;
- else
- return 0;
-
- return n;
-}
+ char color_string[7];
+ int i;
+ int n;
+
+ assert(gtp_boardsize > 0);
+
+ if (sscanf(s, "%6s%n", color_string, &n) != 1)
+ return 0;
}
/* Convert a move, i.e. "b" or "w" followed by a vertex to a color and
* coordinates. Return the number of characters read from the string
* s. The vertex may be "pass" and then the coordinates are set to (-1, -1).
*/
}
/* Convert a move, i.e. "b" or "w" followed by a vertex to a color and
* coordinates. Return the number of characters read from the string
* s. The vertex may be "pass" and then the coordinates are set to (-1, -1).
*/
- int n1, n2;
- int k;
-
- assert(gtp_boardsize > 0);
-
- n1 = gtp_decode_color(s, color);
- if (n1 == 0)
- return 0;
-
- n2 = gtp_decode_coord(s + n1, i, j);
- if (n2 == 0) {
- char buf[6];
- if (sscanf(s + n1, "%5s%n", buf, &n2) != 1)
- return 0;
- for (k = 0; k < (int) strlen(buf); k++)
- buf[k] = tolower((int) buf[k]);
- if (strcmp(buf, "pass") != 0)
- return 0;
- *i = -1;
- *j = -1;
- }
-
- return n1 + n2;
+ int n1, n2;
+ int k;
+
+ assert(gtp_boardsize > 0);
+
+ n1 = gtp_decode_color(s, color);
+ if (n1 == 0)
+ return 0;
+
+ n2 = gtp_decode_coord(s + n1, i, j);
+ if (n2 == 0) {
+ char buf[6];
+ if (sscanf(s + n1, "%5s%n", buf, &n2) != 1)
+ return 0;
+ for (k = 0; k < (int)strlen(buf); k++)
+ buf[k] = tolower((int)buf[k]);
+ if (strcmp(buf, "pass") != 0)
+ return 0;
+ *i = -1;
+ *j = -1;
+ }
+
+ return n1 + n2;
- int k;
- int ri, rj;
-
- assert(gtp_boardsize > 0);
-
- sort_moves(n, movei, movej);
- for (k = 0; k < n; k++) {
- if (k > 0)
- gtp_printf(" ");
- if (movei[k] == -1 && movej[k] == -1)
- gtp_printf("PASS");
- else if (movei[k] < 0 || movei[k] >= gtp_boardsize
- || movej[k] < 0 || movej[k] >= gtp_boardsize)
- gtp_printf("??");
- else {
- if (vertex_transform_output_hook != NULL)
- (*vertex_transform_output_hook)(movei[k], movej[k], &ri, &rj);
- else {
- ri = movei[k];
- rj = movej[k];
- }
- gtp_printf("%c%d", 'A' + rj + (rj >= 8), gtp_boardsize - ri);
+ int k;
+ int ri, rj;
+
+ assert(gtp_boardsize > 0);
+
+ sort_moves(n, movei, movej);
+ for (k = 0; k < n; k++) {
+ if (k > 0)
+ gtp_printf(" ");
+ if (movei[k] == -1 && movej[k] == -1)
+ gtp_printf("PASS");
+ else if (movei[k] < 0 || movei[k] >= gtp_boardsize
+ || movej[k] < 0 || movej[k] >= gtp_boardsize)
+ gtp_printf("??");
+ else {
+ if (vertex_transform_output_hook != NULL)
+ (*vertex_transform_output_hook)(movei[k], movej[k], &ri, &rj);
+ else {
+ ri = movei[k];
+ rj = movej[k];
+ }
+ gtp_printf("%c%d", 'A' + rj + (rj >= 8), gtp_boardsize - ri);
+ }