Move build system to cmake.
[rattatechess.git] / io.cpp
blob4c72c7a3d01503711e30ac22c202d72e041b457e
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"
22 #include "search_gui.h"
24 void Engine::output(const char* fmt, ...)
26 char buf[4096];
27 int len;
28 va_list va;
29 va_start( va, fmt );
30 len = MIN(vsnprintf( buf, 4096, fmt, va), 4096);
31 va_end( va );
33 fwrite(buf, 1, len, stdout);
34 if(log)
36 fwrite(buf, 1, len, log);
37 fflush(log);
41 void Engine::process_input()
43 int argc = 0;
44 char **argv;
45 int retv = 0;
46 Move mv;
48 if(search_gui)
49 search_gui->wait_input();
50 argv = get_tokenized(&argc);
52 if(!argv)
53 exit(0);
55 if(!argc || strlen(argv[0])==0)
57 free(argv);
58 return;
61 /* is this a move? */
62 retv = parse_move(&mv, argv[0]);
64 /* not a move */
65 if(retv == 0)
67 CmdSet::iterator it = commands.lower_bound(argv[0]);
68 if( it != commands.end() &&
69 it->first.compare(0, strlen(argv[0]), argv[0]) == 0 )
71 Cmd& c = it->second;
73 if(it->first != argv[0])
74 output("%s\n", it->first.c_str());
76 if(argc-1 < c.min())
77 output("Error, not enough arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
78 else if(argc-1 > c.max())
79 output("Error, too many arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
80 else
81 c.run(argc-1, argv+1);
83 else
84 output("Illegal command: '%s' (try 'help')\n", argv[0]);
86 /* a legal valid move */
87 else if(retv == 1)
89 if(!io_xboard)
90 print_moves(&mv,1);
92 move(mv);
94 if(!io_xboard)
95 print_board();
97 /* a move that is illegal or ambiguous */
98 else if(retv == 2)
103 free(argv);