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

polserver / polserver / 21541532363

31 Jan 2026 08:14AM UTC coverage: 60.532% (+0.03%) from 60.507%
21541532363

push

github

web-flow
Tidy modernize for loops (#862)

* trigger loop convert

* Automated clang-tidy change: modernize-loop-convert

* fixed refactor

* Automated clang-tidy change: modernize-loop-convert

* compile

* first look through

* fixes and start to use a few ranges

* revert autogenerated file

* compilation fix

* second pass

* renamed loop variable

---------

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

164 of 447 new or added lines in 61 files covered. (36.69%)

6 existing lines in 5 files now uncovered.

44377 of 73312 relevant lines covered (60.53%)

499857.83 hits per line

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

5.36
/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];
92
  s[0] = ch;
×
93
  s[1] = '\0';
×
94
  return s;
×
95
}
96

97
ConsoleCommand* ConsoleCommand::find_console_command( char ch )
×
98
{
NEW
99
  for ( auto& cmd : gamestate.console_commands )
×
100
  {
101
    if ( cmd.ch == ch )
×
102
      return &cmd;
×
103
  }
104
  return nullptr;
×
105
}
106

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

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

115
  gamestate.console_commands.clear();
×
116

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

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

151

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

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

196
  std::string filename = "scripts/console/" + cmd->script;
×
197
  try
198
  {
199
    PolLock lck;
×
200
    ScriptDef sd;
×
201
    sd.quickconfig( filename + ".ecl" );
×
202
    ref_ptr<Bscript::EScriptProgram> prog =
203
        find_script2( sd, true, Plib::systemstate.config.cache_interactive_scripts );
×
204
    if ( prog.get() != nullptr )
×
205
      start_script( prog, new Bscript::String( getcmdstr( ch ) ) );
×
206
  }
×
207
  catch ( const char* msg )
×
208
  {
209
    ERROR_PRINTLN( "Command aborted due to: {}", msg );
×
210
  }
×
211
  catch ( std::string& str )
×
212
  {
213
    ERROR_PRINTLN( "Command aborted due to: {}", str );
×
214
  }  // egcs has some trouble realizing 'exception' should catch
×
215
  catch ( std::runtime_error& re )  // runtime_errors, so...
×
216
  {
217
    ERROR_PRINTLN( "Command aborted due to: {}", re.what() );
×
218
  }
×
219
  catch ( std::exception& ex )
×
220
  {
221
    ERROR_PRINTLN( "Command aborted due to: {}", ex.what() );
×
222
  }
×
223
  catch ( int xn )
×
224
  {
225
    ERROR_PRINTLN( "Command aborted due to: {}", xn );
×
226
  }
×
227
  return;
×
228
}
×
229
#ifdef _WIN32
230
void ConsoleCommand::check_console_commands()
231
{
232
  if ( kbhit() )
233
  {
234
    exec_console_cmd( static_cast<char>( getch() ) );
235
  }
236
}
237
#else
238
void ConsoleCommand::check_console_commands( Clib::KeyboardHook* kb )
9✔
239
{
240
  if ( kb->kbhit() )
9✔
241
  {
242
    exec_console_cmd( kb->getch() );
×
243
  }
244
}
9✔
245
#endif
246
}  // 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