/* * XXX - Filenames with spaces will break here, * do we want to support them? I vote for "no" * - Lukas */ void cmd_pgnsave(void) { if ( strlen(token[1]) > 0 ) PGNSaveToFile (token[1], ""); else printf(_("Invalid filename.\n")); }
void InputCmd () /************************************************************************* * * This is the main user command interface driver. * *************************************************************************/ { const char *color[2] = { "White", "Black" }; int suffix; int i; leaf *ptr; int ncmds; char *x,*trim; CLEAR (flags, THINK); memset(userinput,0,sizeof(userinput)); memset(cmd,0,sizeof(cmd)); #ifndef HAVE_LIBREADLINE /* Why is this necessary anyway? */ memset(inputstr,0,sizeof(inputstr)); #endif #ifdef HAVE_LIBREADLINE if (isatty(STDIN_FILENO)) { sprintf(s,"%s (%d) %c ", color[board.side], (GameCnt+1)/2 + 1, prompt); inputstr = readline(s); if (inputstr == NULL) return; if (*inputstr) { add_history(inputstr); } if (strlen(inputstr) > INPUT_SIZE-1) { printf("Warning: Input line truncated to %d characters.\n", INPUT_SIZE -1 ); inputstr[INPUT_SIZE-1] = '\000'; } } else { inputstr = malloc(INPUT_SIZE); if (inputstr == NULL) { perror("InputCmd"); exit(EXIT_FAILURE); } fgets(inputstr, INPUT_SIZE, stdin); if (inputstr[0]) { inputstr[strlen(inputstr)-1] = 0; } } #else /* !HAVE_LIBREADLINE */ if (!(flags & XBOARD)) { printf ("%s (%d) %c ", color[board.side], (GameCnt+1)/2 + 1, prompt); fflush(stdout); } fgets (inputstr, INPUT_SIZE, stdin) ; #endif /* HAVE_LIBREADLINE */ cmd[0] = '\n'; strcpy(userinput,inputstr); sscanf (inputstr, "%s %[^\n]", cmd, inputstr); if (cmd[0] == '\n') goto done; cmd[0] = subcmd[0] = setting[0] = subsetting[0] = '\0'; ncmds = sscanf (userinput,"%s %s %s %[^\n]", cmd,subcmd,setting,subsetting); /* Put options after command back in inputstr - messy */ sprintf(inputstr,"%s %s %s",subcmd,setting,subsetting); trim = inputstr + strlen(inputstr) - 1; while ( trim>=inputstr && *trim==' ') *trim--='\0'; if (strcmp (cmd, "quit") == 0 || strcmp (cmd, "exit") == 0) SET (flags, QUIT); else if (strcmp (cmd, "help") == 0) ShowHelp (inputstr); else if (strcmp (cmd, "show") == 0) ShowCmd (inputstr); else if (strncmp (cmd, "book", 4) == 0) { if (strncmp(inputstr, "add",3) == 0) { sscanf (inputstr, "add %s", file); if (access(file,F_OK) < 0) { printf("The syntax to add a new book is:\n\n\tbook add file.pgn\n"); } else { BookPGNReadFromFile (file); } } else if (strncmp (inputstr, "on", 2) == 0 || strncmp (inputstr, "prefer", 6) == 0) { bookmode = BOOKPREFER; printf("book now on.\n"); } else if (strncmp (inputstr, "off", 3) == 0) { bookmode = BOOKOFF; printf("book now off.\n"); } else if (strncmp (inputstr, "best", 4) == 0) { bookmode = BOOKBEST; printf("book now best.\n"); } else if (strncmp (inputstr, "worst", 5) == 0) { bookmode = BOOKWORST; printf("book now worst.\n"); } else if (strncmp (inputstr, "random", 6) == 0) { bookmode = BOOKRAND; printf("book now random.\n"); } } else if (strcmp (cmd, "test") == 0) TestCmd (inputstr); else if (strcmp (cmd, "version") == 0) ShowVersion (); else if (strcmp (cmd, "pgnsave") == 0) { if ( strlen(inputstr) > 0 && strlen(inputstr) < INPUT_SIZE ) PGNSaveToFile (inputstr,""); else printf("Invalid filename.\n"); } else if (strcmp (cmd, "pgnload") == 0) PGNReadFromFile (inputstr); else if (strcmp (cmd, "manual") == 0) SET (flags, MANUAL); else if (strcmp (cmd, "debug") == 0) { SET (flags, DEBUGG); Debugmvl = 0; if (strcmp (inputstr, "debug") == 0) { while (strcmp (inputstr, s)) { sscanf (inputstr, "%s %[^\n]", s, inputstr); ptr = ValidateMove (s); Debugmv[Debugmvl++] = ptr->move; MakeMove (board.side, &ptr->move); } i = Debugmvl; while (i) { UnmakeMove (board.side, &Debugmv[--i]); } } } else if (strcmp (cmd, "force") == 0) SET (flags, MANUAL); else if (strcmp (cmd, "white") == 0) ; else if (strcmp (cmd, "black") == 0) ; else if (strcmp (cmd, "hard") == 0) ; else if (strcmp (cmd, "easy") == 0) ; else if (strcmp (cmd, "list") == 0) { if (inputstr[0] == '?') { printf("name - list known players alphabetically\n"); printf("score - list by GNU best result first \n"); printf("reverse - list by GNU worst result first\n"); } else { sscanf (inputstr, "%s %[^\n]", cmd, inputstr); if (inputstr == '\000') DBListPlayer("rscore"); else DBListPlayer(inputstr); } } else if (strcmp (cmd, "post") == 0) SET (flags, POST); else if (strcmp (cmd, "nopost") == 0) CLEAR (flags, POST); else if (strcmp (cmd, "name") == 0) { strcpy(name, inputstr); x = name; while (*x != '\000') { if (*x == ' ') { *x = '\000'; break; } x++; } suffix = 0; for (;;) { sprintf(logfile,"log.%03d",suffix); sprintf(gamefile,"game.%03d",suffix); if (access(logfile,F_OK) < 0) { ofp = fopen(logfile,"w"); break; } else suffix++; } } else if (strcmp (cmd, "result") == 0) { if (ofp != stdout) { fprintf(ofp, "result: %s\n",inputstr); fclose(ofp); ofp = stdout; printf("Save to %s\n",gamefile); PGNSaveToFile (gamefile, inputstr); DBUpdatePlayer (name, inputstr); } } else if (strcmp (cmd, "rating") == 0) { sscanf(inputstr,"%d %d",&myrating,&opprating); fprintf(ofp,"my rating = %d, opponent rating = %d\n",myrating,opprating); /* Change randomness of book based on opponent rating. */ /* Basically we play narrower book the higher the opponent */ if (opprating >= 1700) bookfirstlast = 2; else if (opprating >= 1700) bookfirstlast = 2; else bookfirstlast = 2; } else if (strcmp (cmd, "activate") == 0) { CLEAR (flags, TIMEOUT); CLEAR (flags, ENDED); } else if (strcmp (cmd, "new") == 0) { InitVars (); NewPosition (); CLEAR (flags, MANUAL); CLEAR (flags, THINK); myrating = opprating = 0; } else if (strcmp (cmd, "time") == 0) { sscanf (inputstr, "%s %[^\n]", s, inputstr); TimeLimit[1^board.side] = atoi(s) / 100.0f ; } else if (strcmp (cmd, "otim") == 0) ; else if (strcmp (cmd, "random") == 0) ; else if (strcmp (cmd, "hash") == 0) { sscanf (inputstr, "%s %[^\n]", cmd, inputstr); if (strcmp (cmd, "off") == 0) CLEAR (flags, USEHASH); else if (strcmp (cmd, "on") == 0) SET (flags, USEHASH); printf ("Hashing %s\n", flags & USEHASH ? "on" : "off"); } else if (strcmp (cmd, "hashsize") == 0) { if (inputstr[0] == 0) { printf("Current HashSize is %u slots\n", HashSize); } else { i = atoi (inputstr); TTHashMask = 0; while ((i >>= 1) > 0) { TTHashMask <<= 1; TTHashMask |= 1; } HashSize = TTHashMask + 1; printf ("Adjusting HashSize to %u slots\n", HashSize); InitHashTable (); } } else if (strcmp (cmd, "null") == 0)