Deleted log file from repository
[rattatechess.git] / io.cpp
blobffa9a0ed153bd09ab376d41c792798b967a2a626
1 /***************************************************************************
2 main.cpp - description
3 -------------------
4 begin : Dom Oct 9 2005
5 copyright : (C) 2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include <stdarg.h>
19 #include <string.h>
20 #include "engine.h"
21 #include "commands.h"
23 void Engine::output(const char* fmt, ...)
25 char buf[4096];
26 int len;
27 va_list va;
28 va_start( va, fmt );
29 len = MIN(vsnprintf( buf, 4096, fmt, va), 4096);
30 va_end( va );
32 fwrite(buf, 1, len, stdout);
33 fwrite(buf, 1, len, log);
34 fflush(log);
37 void Engine::process_input()
39 char str[4096];
40 int argc = 0;
41 char *argv[256];
42 char *tmp;
43 int retv = 0;
44 Move mv;
46 if(!fgets(str,4096,stdin))
47 exit(0);
49 fwrite("--> ", 1, 4, log);
50 fwrite(str, 1, strlen(str), log);
51 fflush(log);
53 for(int i=0;i<256;i++)
55 char *t = strtok_r( i ? NULL : str, " \t\r\n", &tmp);
56 if(t)
57 argv[argc++] = t;
58 else
59 break;
62 if(!argc || strlen(argv[0])==0)
63 return;
65 /* is this a move? */
66 retv = parse_move(&mv,str);
68 /* not a move */
69 if(retv == 0)
71 CmdSet::iterator it = commands.lower_bound(argv[0]);
72 if( it != commands.end() &&
73 it->first.compare(0, strlen(argv[0]), argv[0]) == 0 )
75 Cmd& c = it->second;
77 if(it->first != argv[0])
78 output("%s\n", it->first.c_str());
80 if(argc-1 < c.min())
81 output("Error, not enough arguments for command '%s' (try 'help')\n", it->first.c_str() );
82 else if(argc-1 > c.max())
83 output("Error, too many arguments for command '%s' (try 'help')\n", it->first.c_str() );
84 else
85 c.run(argc-1, argv+1);
87 else
88 output("Illegal command: '%s' (try 'help')\n", argv[0]);
90 /* a legal valid move */
91 else if(retv == 1)
93 if(!io_xboard)
94 print_moves(&mv,1);
96 move(mv);
98 if(!io_xboard)
99 print_board();
101 /* a move that is illegal or ambiguous */
102 else if(retv == 2)