Don't use buffered input for stdin.
[rattatechess.git] / io.cpp
blob502e02c35942024f14514594cd0537ff17a7a082
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 if(log)
35 fwrite(buf, 1, len, log);
36 fflush(log);
40 void Engine::process_input()
42 int argc = 0;
43 char **argv;
44 int retv = 0;
45 Move mv;
47 argv = get_tokenized(&argc);
49 if(!argv)
50 exit(0);
52 // printf("%d arguments\n", argc);
53 // for(int i=0;i<argc;i++)
54 // printf("got \"%s\"\n", argv[i]);
56 if(!argc || strlen(argv[0])==0)
58 free(argv);
59 return;
62 /* is this a move? */
63 retv = parse_move(&mv, argv[0]);
65 /* not a move */
66 if(retv == 0)
68 CmdSet::iterator it = commands.lower_bound(argv[0]);
69 if( it != commands.end() &&
70 it->first.compare(0, strlen(argv[0]), argv[0]) == 0 )
72 Cmd& c = it->second;
74 if(it->first != argv[0])
75 output("%s\n", it->first.c_str());
77 if(argc-1 < c.min())
78 output("Error, not enough arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
79 else if(argc-1 > c.max())
80 output("Error, too many arguments (%d) for command '%s' (try 'help')\n", argc-1, it->first.c_str() );
81 else
82 c.run(argc-1, argv+1);
84 else
85 output("Illegal command: '%s' (try 'help')\n", argv[0]);
87 /* a legal valid move */
88 else if(retv == 1)
90 if(!io_xboard)
91 print_moves(&mv,1);
93 move(mv);
95 if(!io_xboard)
96 print_board();
98 /* a move that is illegal or ambiguous */
99 else if(retv == 2)
104 free(argv);