per test
[rattatechess.git] / io.cpp
blob9239d04033a8c30f029537dcc8239d17eaafcedd
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::run_command(int argc, char** argv)
43 if(argv[0][0] == '#' || argv[0][0] == ';')
44 return;
46 CmdSet::iterator it = commands.lower_bound(argv[0]);
47 if( it != commands.end() &&
48 it->first.compare(0, strlen(argv[0]), argv[0]) == 0 )
50 Cmd& c = it->second;
52 if(it->first != argv[0])
53 output("%s\n", it->first.c_str());
55 if(argc-1 < c.min())
56 output("Error, not enough arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
57 else if(argc-1 > c.max())
58 output("Error, too many arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
59 else
60 c.run(argc-1, argv+1);
62 else
63 output("Unknown command: '%s' (try 'help')\n", argv[0]);
66 void Engine::load_ini()
68 FILE *f = fopen("rattatechess.ini", "r");
69 if(!f)
70 return;
72 int argc = 0;
73 char **argv;
75 while( (argv = fget_tokenized(f, &argc)) )
77 if(!argc || strlen(argv[0])==0)
79 free(argv);
80 continue;
82 run_command(argc, argv);
83 free(argv);
86 fclose(f);
89 void Engine::process_input()
91 int argc = 0;
92 char **argv;
93 Move mv;
95 if(search_gui)
96 search_gui->wait_input();
97 argv = get_tokenized(&argc);
99 if(!argv)
100 exit(0);
102 if(!argc || strlen(argv[0])==0)
104 free(argv);
105 return;
108 /* is this a move? */
109 mv = board.move_from_string(argv[0]);
111 if(mv == Move::Illegal())
112 output("Illegal move: %s\n", argv[0]);
113 else if(mv == Move::Ambiguous())
114 output("Illegal move (ambiguous): %s\n", argv[0]);
115 else if(mv == Move::None()) /* not a move */
116 run_command(argc, argv);
117 else /* a valid move */
119 if(!io_xboard)
120 print_moves(&mv,1);
122 move(mv);
124 if(!io_xboard)
125 print_board();
128 free(argv);