Belofte version 2.2.0
A promising chess program using the UCI or Winboard interface
belofte.cpp
Go to the documentation of this file.
1/**
2 * @file belofte.cpp
3 * @brief main entry point, user interface
4 * @copyright This program is distributed under the GNU GPL Version 2,
5 * June 1991 General Public License. See COPYING.md for more
6 * information. SPDX-License-Identifier: GPL-2.0-only
7 * Part of belofte - A Promising Chess Program
8 * @author Yves De Billoez
9 * @date 01/10/2004 initial version: 0.1
10 * @date 12/05/2020 start of version 2.0
11 * @date 13/04/2026 latest released version: 2.2.0
12 */
13
14/**
15 * The main program
16 */
17
18#include "belofte.h"
19
20//-----------------------------------------------------------------------
21
22static void platform_ctor();
23static void platform_dtor();
24
25/**
26 * Main entry point
27 * @param argc The number of parameters
28 * @param argv The strings pointing to each parameter
29 */
30
31int main(int argc, char *argv[])
32{
33 // initialize appInstance and normalised name of application
34 App().setName(argv[0]);
35 App().setConfig("enginename", MYFULLNAME " (" MYOS "/" MYPLATFORM ")" );
36 App().setConfig("about", MYFULLNAME " by " MYAUTHOR " " DEVDATES ", " MYLICENSE);
37 App().setConfig("author", "by " MYAUTHOR " " DEVDATES ", " MYLICENSE);
38 App().setConfig("UCIdebug", 0);
39
41
42 // initialize speedy structures
43 // even if norandom selected, required for hash init
44 std::srand(static_cast<unsigned int> (std::time(nullptr)));
46
47 // initialize appInstance
48#if defined(BELOFTE_UCIMODE)
49 App().setMode("uci");
50#elif defined(BELOFTE_XBOARDMODE)
51 App().setMode("xboard");
52#else
53 App();
54#endif
55
56 // during initialisation, no interactive commands are read
58
59 // initialize static structures taking time
61
62 // avoid crash by initialising (not in spec!)
63 Game()->newGame();
66
67 if (argc > 1) {
68 for (int i = 1; i < argc; ++i) {
69 std::string arg = argv[i];
70 if (arg == "--help") {
71 /// @todo allow aliases on commands
72 AppEI()->execute("usage", argv[0]);
73 goto belofte_exit;
74 } else if (arg == "--version") {
75 /// @todo allow aliases on commands
76 AppEI()->execute("about", "");
77 goto belofte_exit;
78#if !defined(BELOFTE_UCIMODE)
79 } else if ((arg == "xboard") || (arg == "-xboard") || (arg == "--xboard")) {
80 App().setMode("xboard");
81 App().bout.endl();
82#endif
83 } else if ((arg == "bench") || (arg == "--bench")) {
84 AppEI()->execute("bench", "");
85 goto belofte_exit;
86 } else if (arg.substr(0, 1) == "@") {
87 if (arg.length() > 1) {
88 AppEI()->execute("@", arg);
89 } else {
90 // space in between @ and filename
91 if (i + 1 >= argc) {
92 App().bout << "Use filename with @";
93 App().bout.endl();
94 goto belofte_exit;
95 }
96 arg = argv[++i];
97 AppEI()->execute("@", arg);
98 }
99 } else if (arg.substr(0, 2) == "--") {
100 try {
101 /// @todo parse all next argument(s) before those preceded by --
102 AppEI()->execute(arg.substr(2), "");
103 } catch (const quitCommandException&) {
104 goto belofte_exit;
105 } catch (const std::logic_error& e) {
106 std::cout << "Logic Error: " << e.what() << std::endl;
107 } catch (...) {
108 std::cout << "Error: Unmatched exception" << std::endl;
109 }
110 } else {
111 /// @deprecated no -- or @, so filename expected
112 AppEI()->execute("exec", arg);
113 }
114 }
115 }
116
117 // default operation, used passed file or look for .rc file
118 if (!App().m_reader.isFileAttached()) {
120 // execute belofte-version-x.rc if present
121 AppEI()->execute("exec", App().getName() + ".rc");
122 }
123
124 try {
125 App().m_reader.runner();
126 } catch (const std::logic_error& e) {
127 std::cout << "Logic Error: " << e.what() << std::endl;
128 } catch (...) {
129 std::cout << "Error: Unmatched exception" << std::endl;
130 }
131
132belofte_exit:
133 // during program exit, no interactive commands are read
135
136 // call some dtor's
137 bPiece_dtor();
139
140 return 0;
141}
142
143//-----------------------------------------------------------------------
144
145#if defined(__GNUC__)
146#pragma GCC diagnostic push
147#pragma GCC diagnostic ignored "-Weffc++"
148#endif
149
151 : bout{std::cout}
152 , sout{std::cout}
153 , m_ui{nullptr}
154 , m_game{new bGame()}
155 // other members do not need to be initialised as they are auto-constructed
156{
157 m_engineInterfaces["belofte"] = new BelofteMode();
158#if defined(BELOFTE_XBOARDMODE)
159 m_engineInterfaces["xboard"] = new XboardMode();
160 setMode("xboard");
161#elif defined(BELOFTE_UCIMODE)
162 m_engineInterfaces["uci"] = new UCIMode();
163 setMode("uci");
164#else
165 m_engineInterfaces["xboard"] = new XboardMode();
166 m_engineInterfaces["uci"] = new UCIMode();
167 // set default startup mode
168 setMode("belofte");
169#endif
170}
171
172#if defined(__GNUC__)
173#pragma GCC diagnostic pop
174#endif
175
177{
178 engineInterfaces_t::iterator itr = m_engineInterfaces.begin();
179 while (itr != m_engineInterfaces.end()) {
180 delete itr->second;
181 itr = m_engineInterfaces.erase(itr);
182 }
183 m_engineInterfaces.clear();
184}
185
186std::string const appInstance::setMode(std::string const& iName)
187{
188 std::string sOldName = m_interfaceName;
189 m_interfaceName = iName;
190 m_ui = m_engineInterfaces[m_interfaceName];
191 return sOldName;
192}
193
194void appInstance::setConfig(std::string const& s, int64_t v)
195{
196 m_settings[s] = v;
197}
198
199int64_t appInstance::getConfig(std::string const& s, int64_t v)
200{
201 if (m_settings.find(s) != m_settings.end()) return m_settings[s];
202 return v;
203}
204
205void appInstance::setConfig(std::string const& s, std::string const& v)
206{
207 m_stringsettings[s] = v;
208}
209
210std::string appInstance::getConfig(std::string const& s, std::string const& v)
211{
212 if (m_stringsettings.find(s) != m_stringsettings.end()) return m_stringsettings[s];
213 return v;
214}
215
216/**
217 * @brief set name of executable based on argv[0]
218 * Remove leading / and \ characters
219 * Remove trailing .exe if present
220 */
221void appInstance::setName(char *sName)
222{
223 std::string sExename;
224
225 std::string sFile(sName);
226 sExename = sFile.substr(sFile.find_last_of("/\\") + 1);
227
228 // remove .exe
229 if (sExename.find(".exe") != std::string::npos)
230 sExename.erase(sExename.find(".exe"));
231
232 App().setConfig("exename", sExename);
233}
234
236{
237 return App().getConfig("exename", MYLCNAME "-" MYVERSION);
238}
239
240// --------------------------------------------------------------------
241
243{
244 static appInstance& instance = *new appInstance();
245 return instance;
246}
247
249{
250 return App().m_ui;
251}
252
254{
255 return App().m_game;
256}
257
258// --------------------------------------------------------------------
259
260static void platform_ctor()
261{
262#if defined(BELOFTE_NOSIGNALS)
263 // disable signals
264 signal(SIGTERM, SIG_IGN);
265 signal(SIGINT, SIG_IGN);
266#endif
267 // #if defined(_WIN32) || defined(WIN32)
268 // setlocale(LC_ALL, ".UTF8");
269 /// @todo remove following line ASAP, this violates all C++ standards
270 // SetConsoleOutputCP(CP_UTF8); // select unicode compatibility
271 // call system("chcp 65001")
272 // #endif
273}
274
275static void platform_dtor()
276{
277}
278
279// eof
int main(int argc, char *argv[])
Main entry point.
Definition belofte.cpp:31
appInstance & App()
Definition belofte.cpp:242
static void platform_ctor()
The main program.
Definition belofte.cpp:260
static void platform_dtor()
Definition belofte.cpp:275
engineInterface * AppEI()
Definition belofte.cpp:248
bGame * Game()
Definition belofte.cpp:253
This is the main include file, needs to be included before any other include.
#define MYAUTHOR
Definition belofte.h:38
appInstance & App()
Definition belofte.cpp:242
#define MYLCNAME
Definition belofte.h:33
#define MYFULLNAME
Definition belofte.h:50
#define MYLICENSE
Definition belofte.h:42
#define DEVDATES
Definition belofte.h:40
#define MYVERSION
Definition belofte.h:34
implementation of specific implementation
Singleton implementation of application.
Definition belofte.h:153
bGame * m_game
Definition belofte.h:184
outputWriter sout
normal output
Definition belofte.h:179
engineInterface * m_ui
Definition belofte.h:183
static void setName(char *sName)
set name of executable based on argv[0] Remove leading / and \ characters Remove trailing ....
Definition belofte.cpp:221
bel_hash m_hashEngine
read input
Definition belofte.h:181
static std::string getName()
Definition belofte.cpp:235
void setConfig(std::string const &s, int64_t v)
Definition belofte.cpp:194
outputWriter bout
Definition belofte.h:178
int64_t getConfig(std::string const &s, int64_t v)
Definition belofte.cpp:199
std::string const setMode(std::string const &iName)
Definition belofte.cpp:186
commandReader m_reader
searching output
Definition belofte.h:180
game representation, singleton
Definition game.h:20
void newGame()
Definition game.cpp:24
bLevel & getLevel()
Definition game.h:59
void setFENInitialPos()
Definition game.cpp:46
void setDepthCommand(depth_t const d)
Definition level.cpp:226
void delayed_ctor()
Definition bel_hash.cpp:12
void clearBatchMode()
void setBatchMode()
implementation of user interface
void execute(std::string const &command, std::string const &params)
outputWriter & endl()
constexpr depth_t DEFAULT_DEPTH
Definition level.h:12
#define MYPLATFORM
no matching platform
Definition myplatform.h:73
#define MYOS
no matching OS
Definition myplatform.h:117
void bPiece_dtor()
Definition piece.cpp:150
void bPiece_ctor()
Definition piece.cpp:46