slightly smaller pluto
[rattatechess.git] / io.cpp
blobe040370cfdac7087c72520742f9f8fa0fff2abd9
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 Move mv;
47 if(search_gui)
48 search_gui->wait_input();
49 argv = get_tokenized(&argc);
51 if(!argv)
52 exit(0);
54 if(!argc || strlen(argv[0])==0)
56 free(argv);
57 return;
60 /* is this a move? */
61 mv = board.move_from_string(argv[0]);
63 if(mv == Move::Illegal())
64 output("Illegal move: %s\n", argv[0]);
65 else if(mv == Move::Ambiguous())
66 output("Illegal move (ambiguous): %s\n", argv[0]);
67 else if(mv == Move::None()) /* not a move */
69 CmdSet::iterator it = commands.lower_bound(argv[0]);
70 if( it != commands.end() &&
71 it->first.compare(0, strlen(argv[0]), argv[0]) == 0 )
73 Cmd& c = it->second;
75 if(it->first != argv[0])
76 output("%s\n", it->first.c_str());
78 if(argc-1 < c.min())
79 output("Error, not enough arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
80 else if(argc-1 > c.max())
81 output("Error, too many arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
82 else
83 c.run(argc-1, argv+1);
85 else
86 output("Unknown command: '%s' (try 'help')\n", argv[0]);
88 /* a valid move */
89 else
91 if(!io_xboard)
92 print_moves(&mv,1);
94 move(mv);
96 if(!io_xboard)
97 print_board();
100 free(argv);