• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polserver / polserver / 21108840797

18 Jan 2026 08:35AM UTC coverage: 60.508% (+0.02%) from 60.492%
21108840797

push

github

web-flow
ClangTidy readability-else-after-return (#857)

* trigger tidy

* Automated clang-tidy change: readability-else-after-return

* compile test

* rerun

* Automated clang-tidy change: readability-else-after-return

* trigger..

* Automated clang-tidy change: readability-else-after-return

* manually removed a few

* Automated clang-tidy change: readability-else-after-return

* removed duplicate code

* fix remaining warnings

* fixed scope

---------

Co-authored-by: Clang Tidy <clang-tidy@users.noreply.github.com>

837 of 1874 new or added lines in 151 files covered. (44.66%)

46 existing lines in 25 files now uncovered.

44448 of 73458 relevant lines covered (60.51%)

525066.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

5.26
/pol-core/pol/console.cpp
1
/** @file
2
 *
3
 * @par History
4
 */
5

6

7
#include "console.h"
8

9
#include <ctype.h>
10
#include <stddef.h>
11

12
#include "../bscript/eprog.h"
13
#include "../bscript/impstr.h"
14
#include "../clib/cfgelem.h"
15
#include "../clib/cfgfile.h"
16
#include "../clib/fileutil.h"
17
#include "../clib/kbhit.h"
18
#include "../clib/logfacility.h"
19
#include "../clib/refptr.h"
20
#include "../clib/stlutil.h"
21
#include "../plib/systemstate.h"
22
#include "globals/state.h"
23
#include "globals/uvars.h"
24
#include "polsem.h"
25
#include "polsig.h"
26
#include "scrdef.h"
27
#include "scrsched.h"
28
#include "scrstore.h"
29
#include <fmt/format.h>
30

31
#ifdef _WIN32
32
#include <conio.h>
33
#endif
34

35
#include <exception>
36
#include <stdexcept>
37
#include <string>
38

39

40
namespace Pol::Core
41
{
42
bool ConsoleCommand::console_locked = true;
43
char ConsoleCommand::unlock_char;
44

45
ConsoleCommand::ConsoleCommand( Clib::ConfigElem& elem, const std::string& cmd )
×
46
{
47
  ISTRINGSTREAM is( cmd );
×
48
  std::string charspec;
×
49
  if ( !( is >> charspec >> script ) )
×
50
  {
51
    elem.throw_error( "Ill-formed console command desc: " + cmd );
×
52
  }
53
  if ( charspec[0] == '^' && charspec.size() == 2 )
×
54
  {
55
    ch = static_cast<char>( toupper( charspec[1] ) ) - 'A' + 1;
×
56
  }
57
  else if ( charspec.size() == 1 )
×
58
  {
59
    ch = charspec[0];
×
60
  }
61
  else
62
  {
63
    elem.throw_error( "Ill-formed console command char: " + charspec );
×
64
  }
65
  getline( is, description );
×
66
  while ( !description.empty() && isspace( description[0] ) )
×
67
  {
68
    description.erase( 0, 1 );
×
69
  }
70

71
  if ( script == "[unlock]" || script == "[lock/unlock]" )
×
72
    ConsoleCommand::unlock_char = ch;
×
73
}
×
74

75
size_t ConsoleCommand::estimateSize() const
×
76
{
77
  return sizeof( char ) + script.capacity() + description.capacity();
×
78
}
79

80
std::string getcmdstr( char ch )
×
81
{
82
  if ( iscntrl( ch ) )
×
83
  {
84
    char s[3];
85
    s[0] = '^';
×
86
    s[1] = ch + 'A' - 1;
×
87
    s[2] = '\0';
×
88
    return s;
×
89
  }
90

91
  char s[2];
NEW
92
  s[0] = ch;
×
NEW
93
  s[1] = '\0';
×
NEW
94
  return s;
×
95
}
96

97
ConsoleCommand* ConsoleCommand::find_console_command( char ch )
×
98
{
99
  for ( unsigned i = 0; i < gamestate.console_commands.size(); ++i )
×
100
  {
101
    ConsoleCommand& cmd = gamestate.console_commands[i];
×
102
    if ( cmd.ch == ch )
×
103
      return &cmd;
×
104
  }
105
  return nullptr;
×
106
}
107

108
void ConsoleCommand::load_console_commands()
37✔
109
{
110
  if ( !Clib::FileExists( "config/console.cfg" ) )
37✔
111
    return;
37✔
112

113
  Clib::ConfigFile cf( "config/console.cfg", "Commands" );
×
114
  Clib::ConfigElem elem;
×
115

116
  gamestate.console_commands.clear();
×
117

118
  while ( cf.read( elem ) )
×
119
  {
120
    std::string tmp;
×
121
    while ( elem.remove_prop( "CMD", &tmp ) )
×
122
    {
123
      ConsoleCommand cmd( elem, tmp );
×
124
      gamestate.console_commands.push_back( cmd );
×
125
    }
×
126
  }
×
127
}
×
128

129
void ConsoleCommand::exec_console_cmd( char ch )
×
130
{
131
#ifdef WIN32
132
  // cope with function keys.
133
  if ( ch == 0 || static_cast<unsigned char>( ch ) == 0xE0 )
134
  {
135
    getch();
136
    ch = '?';
137
  }
138
#endif
139
  if ( ch == '?' )
×
140
  {
141
    std::string tmp = "Commands: \n";
×
142
    for ( unsigned i = 0; i < gamestate.console_commands.size(); ++i )
×
143
    {
144
      ConsoleCommand& cmd = gamestate.console_commands[i];
×
145
      std::string sc = getcmdstr( cmd.ch );
×
146
      tmp += fmt::format( "{}{}: {}\n", sc.size() == 1 ? "  " : " ", sc, cmd.description );
×
147
    }
×
148
    tmp += "  ?: Help (This list)";
×
149
    INFO_PRINTLN( tmp );
×
150
    return;
×
151
  }
×
152

153

154
  ConsoleCommand* cmd = find_console_command( ch );
×
155
  if ( !cmd )
×
156
  {
157
    INFO_PRINTLN( "Unknown console command: '{}'", getcmdstr( ch ) );
×
158
    return;
×
159
  }
160
  if ( cmd->script == "[lock]" )
×
161
  {
162
    ConsoleCommand::console_locked = true;
×
163
    INFO_PRINTLN( "Console is now locked." );
×
164
    return;
×
165
  }
166
  if ( cmd->script == "[unlock]" )
×
167
  {
168
    ConsoleCommand::console_locked = true;
×
169
    INFO_PRINTLN( "Console is now unlocked." );
×
170
    return;
×
171
  }
172
  if ( cmd->script == "[lock/unlock]" )
×
173
  {
174
    ConsoleCommand::console_locked = !ConsoleCommand::console_locked;
×
175
    if ( ConsoleCommand::console_locked )
×
176
      INFO_PRINTLN( "Console is now locked." );
×
177
    else
178
      INFO_PRINTLN( "Console is now unlocked." );
×
179
    return;
×
180
  }
181
  if ( cmd->script == "[threadstatus]" )
×
182
  {
183
    stateManager.polsig.report_status_signalled = true;
×
184
    return;
×
185
  }
186
  if ( cmd->script == "[crash]" )
×
187
  {
188
    std::abort();
×
189
    return;
190
  }
191

192
  if ( ConsoleCommand::console_locked )
×
193
  {
194
    INFO_PRINTLN( "Console is locked.  Press '{}' to unlock.", ConsoleCommand::unlock_char );
×
195
    return;
×
196
  }
197

198
  std::string filename = "scripts/console/" + cmd->script;
×
199
  try
200
  {
201
    PolLock lck;
×
202
    ScriptDef sd;
×
203
    sd.quickconfig( filename + ".ecl" );
×
204
    ref_ptr<Bscript::EScriptProgram> prog =
205
        find_script2( sd, true, Plib::systemstate.config.cache_interactive_scripts );
×
206
    if ( prog.get() != nullptr )
×
207
      start_script( prog, new Bscript::String( getcmdstr( ch ) ) );
×
208
  }
×
209
  catch ( const char* msg )
×
210
  {
211
    ERROR_PRINTLN( "Command aborted due to: {}", msg );
×
212
  }
×
213
  catch ( std::string& str )
×
214
  {
215
    ERROR_PRINTLN( "Command aborted due to: {}", str );
×
216
  }  // egcs has some trouble realizing 'exception' should catch
×
217
  catch ( std::runtime_error& re )  // runtime_errors, so...
×
218
  {
219
    ERROR_PRINTLN( "Command aborted due to: {}", re.what() );
×
220
  }
×
221
  catch ( std::exception& ex )
×
222
  {
223
    ERROR_PRINTLN( "Command aborted due to: {}", ex.what() );
×
224
  }
×
225
  catch ( int xn )
×
226
  {
227
    ERROR_PRINTLN( "Command aborted due to: {}", xn );
×
228
  }
×
229
  return;
×
230
}
×
231
#ifdef _WIN32
232
void ConsoleCommand::check_console_commands()
233
{
234
  if ( kbhit() )
235
  {
236
    exec_console_cmd( static_cast<char>( getch() ) );
237
  }
238
}
239
#else
240
void ConsoleCommand::check_console_commands( Clib::KeyboardHook* kb )
9✔
241
{
242
  if ( kb->kbhit() )
9✔
243
  {
244
    exec_console_cmd( kb->getch() );
×
245
  }
246
}
9✔
247
#endif
248
}  // namespace Pol::Core
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc