Belofte version 2.1.9
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 19/10/2024 latest released version: 2.1.8.1
12 * @date 05/05/2025 work in progress on 2.1.9
13 */
14
15/**
16 * The main program
17 */
18
19#include "belofte.h"
20
21//-----------------------------------------------------------------------
22
23static void platform_ctor();
24static void platform_dtor();
25
26/**
27 * Main entry point
28 * @param argc The number of parameters
29 * @param argv The strings pointing to each parameter
30 */
31
32int main(int argc, char *argv[])
33{
34 // initialize appInstance and normalised name of application
35 App().setName(argv[0]);
36 App().setConfig("enginename", MYFULLNAME " (" MYOS "/" MYPLATFORM ")" );
37 App().setConfig("about", MYFULLNAME " by " MYAUTHOR " " DEVDATES ", " MYLICENSE);
38 App().setConfig("author", "by " MYAUTHOR " " DEVDATES ", " MYLICENSE);
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#else
51 App();
52#endif
53
54 // during initialisation, no interactive commands are read
56
57 // initialize static structures taking time
59
60 // avoid crash by initialising (not in spec!)
61 Game()->newGame();
64
65 if (argc > 1) {
66 for (int i = 1; i < argc; ++i) {
67 std::string arg = argv[i];
68 if (arg == "--help") {
69 /// @todo allow aliases on commands
70 AppEI()->execute("usage", argv[0]);
71 goto belofte_exit;
72 } else if (arg == "--version") {
73 /// @todo allow aliases on commands
74 AppEI()->execute("about", "");
75 goto belofte_exit;
76 } else if (arg == "-xboard") {
77 // Arena default parameter for belofte is -xboard instead of --xboard
78 AppEI()->execute("xboard", "");
79 } else if ((arg == "bench") || (arg == "--bench")) {
80 AppEI()->execute("bench", "");
81 goto belofte_exit;
82 } else if (arg.substr(0, 1) == "@") {
83 if (arg.length() > 1) {
84 AppEI()->execute("@", arg);
85 } else {
86 // space in between @ and filename
87 if (i + 1 >= argc) {
88 App().bout << "Use filename with @" << std::endl;
89 goto belofte_exit;
90 }
91 arg = argv[++i];
92 AppEI()->execute("@", arg);
93 }
94 } else if (arg.substr(0, 2) == "--") {
95 try {
96 /// @todo parse all next argument(s) before those preceded by --
97 AppEI()->execute(arg.substr(2), "");
98 } catch (const quitCommandException&) {
99 goto belofte_exit;
100 } catch (const std::logic_error& e) {
101 std::cout << "Logic Error: " << e.what() << std::endl;
102 } catch (...) {
103 std::cout << "Error: Unmatched exception" << std::endl;
104 }
105 } else {
106 /// @deprecated no -- or @, so filename expected
107 AppEI()->execute("exec", arg);
108 }
109 }
110 }
111
112 // default operation, used passed file or look for .rc file
113 if (!App().m_reader.isFileAttached()) {
115 // execute belofte-version-x.rc if present
116 AppEI()->execute("exec", App().getName() + ".rc");
117 }
118
119 try {
120 App().m_reader.runner();
121 } catch (const std::logic_error& e) {
122 std::cout << "Logic Error: " << e.what() << std::endl;
123 } catch (...) {
124 std::cout << "Error: Unmatched exception" << std::endl;
125 }
126
127belofte_exit:
128 // during program exit, no interactive commands are read
130
131 // call some dtor's
132 bPiece_dtor();
134
135 return 0;
136}
137
138//-----------------------------------------------------------------------
139
141 : bout{std::cout}
142 , sout{std::cout}
143 , m_reader{}
144 , m_hashEngine{}
146 , m_ui{nullptr}
147 , m_game{new bGame()}
148 , m_settings{{}}
149 , m_stringsettings{{}}
150 , m_engineInterfaces{{}}
151 , m_interfaceName{}
152{
153 m_engineInterfaces["belofte"] = new BelofteMode();
154 m_engineInterfaces["xboard"] = new XboardMode();
155 m_engineInterfaces["uci"] = new UCIMode();
156 // set default startup mode
157 setMode("belofte");
158}
159
161{
162 engineInterfaces_t::iterator itr = m_engineInterfaces.begin();
163 while (itr != m_engineInterfaces.end()) {
164 delete itr->second;
165 itr = m_engineInterfaces.erase(itr);
166 }
167 m_engineInterfaces.clear();
168}
169
170std::string const appInstance::setMode(std::string const& iName)
171{
172 std::string sOldName = m_interfaceName;
173 m_interfaceName = iName;
174 m_ui = m_engineInterfaces[m_interfaceName];
175 return sOldName;
176}
177
178void appInstance::setConfig(std::string const& s, int64_t v)
179{
180 m_settings[s] = v;
181}
182
183int64_t appInstance::getConfig(std::string const& s, int64_t v)
184{
185 if (m_settings.find(s) != m_settings.end()) return m_settings[s];
186 return v;
187}
188
189void appInstance::setConfig(std::string const& s, std::string const& v)
190{
191 m_stringsettings[s] = v;
192}
193
194std::string appInstance::getConfig(std::string const& s, std::string const& v)
195{
196 if (m_stringsettings.find(s) != m_stringsettings.end()) return m_stringsettings[s];
197 return v;
198}
199
200/** @brief set name of executable based on argv[0]
201 * Remove leading / and \ characters
202 * Remove trailing .exe if present
203 */
204void appInstance::setName(char *sName)
205{
206 std::string sExename;
207
208 std::string sFile(sName);
209 sExename = sFile.substr(sFile.find_last_of("/\\") + 1);
210
211 // remove .exe
212 if (sExename.find(".exe") != std::string::npos)
213 sExename.erase(sExename.find(".exe"));
214
215 App().setConfig("exename", sExename);
216}
217
218std::string appInstance::getName() const
219{
220 return App().getConfig("exename", MYLCNAME "-" MYVERSION);
221}
222
223// --------------------------------------------------------------------
224
226{
227 static appInstance& instance = *new appInstance();
228 return instance;
229}
230
232{
233 return App().m_ui;
234}
235
237{
238 return App().m_game;
239}
240
241// --------------------------------------------------------------------
242
243static void platform_ctor()
244{
245#if defined(BELOFTE_NOSIGNALS)
246 // disable signals
247 signal(SIGTERM, SIG_IGN);
248 signal(SIGINT, SIG_IGN);
249#endif
250 // #if defined(_WIN32) || defined(WIN32)
251 // setlocale(LC_ALL, ".UTF8");
252 /// @todo remove following line ASAP, this violates all C++ standards
253 // SetConsoleOutputCP(CP_UTF8); // select unicode compatibility
254 // call system("chcp 65001")
255 // #endif
256}
257
258static void platform_dtor()
259{
260}
261
262// eof
int main(int argc, char *argv[])
Main entry point.
Definition belofte.cpp:32
appInstance & App()
Definition belofte.cpp:225
static void platform_ctor()
The main program.
Definition belofte.cpp:243
static void platform_dtor()
Definition belofte.cpp:258
engineInterface * AppEI()
Definition belofte.cpp:231
bGame * Game()
Definition belofte.cpp:236
This is the main include file, needs to be included before any other include.
#define MYAUTHOR
Definition belofte.h:37
appInstance & App()
Definition belofte.cpp:225
#define MYLCNAME
Definition belofte.h:32
#define MYFULLNAME
Definition belofte.h:49
#define MYLICENSE
Definition belofte.h:41
#define DEVDATES
Definition belofte.h:39
#define MYVERSION
Definition belofte.h:33
implementation of specific implementation
Singleton implementation of application.
Definition belofte.h:148
bGame * m_game
Definition belofte.h:179
outputWriter sout
normal output
Definition belofte.h:174
engineInterface * m_ui
Definition belofte.h:178
void setName(char *sName)
set name of executable based on argv[0] Remove leading / and \ characters Remove trailing ....
Definition belofte.cpp:204
bel_debug m_debuginterface
Definition belofte.h:177
bel_hash m_hashEngine
read input
Definition belofte.h:176
void setConfig(std::string const &s, int64_t v)
Definition belofte.cpp:178
outputWriter bout
Definition belofte.h:173
int64_t getConfig(std::string const &s, int64_t v)
Definition belofte.cpp:183
std::string getName() const
Definition belofte.cpp:218
std::string const setMode(std::string const &iName)
Definition belofte.cpp:170
commandReader m_reader
searching output
Definition belofte.h:175
game representation, singleton
Definition game.h:20
void newGame()
Definition game.cpp:24
bLevel & getLevel()
Definition game.h:59
void setFENInitialPos()
Definition game.cpp:42
void setDepthCommand(depth_t const d)
Definition level.cpp:226
void delayed_ctor()
Definition bel_hash.cpp:12
void runner(void)
implementation of user interface
void execute(std::string const &command, std::string const &params)
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