Even more conversion, lots of superfluous macro removal

This commit is contained in:
iikorni
2026-08-29 23:14:23 -05:00
parent 70e5c02355
commit de75b014b3
102 changed files with 7060 additions and 8179 deletions
+90 -232
View File
@@ -29,8 +29,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "quakedef.hpp"
void Cmd_ForwardToServer(void);
constexpr int CMDLINE_LENGTH = 256;
constexpr int MAX_ARGS = 80;
@@ -57,7 +55,7 @@ namespace command {
* bind g "impulse 5 ; +attack ; wait ; -attack ; impulse 2"
* ```
*/
void set_wait() {
void _set_wait() {
waiting = true;
}
@@ -111,13 +109,11 @@ namespace command {
}
void _alias() {
char cmd[1024];
switch (argc()) {
case 1: {
//list all aliases
auto count = 0;
for (auto &[name, alias]: command::ALIASES) {
for (auto &[name, alias]: ALIASES) {
Con_SafePrintf(" %s: %s", name.c_str(), alias.value.c_str());
count++;
}
@@ -130,7 +126,7 @@ namespace command {
case 2: {
//output current alias string
const auto lookup = argv(1).value();
for (auto &[name, alias]: command::ALIASES)
for (auto &[name, alias]: ALIASES)
if (name.starts_with(lookup))
Con_Printf(" %s: %s", name.c_str(), alias.value.c_str());
break;
@@ -138,14 +134,14 @@ namespace command {
default: {
//set alias string
const auto new_name = argv(1).value();
if (new_name.length() >= command::MAX_ALIAS_LENGTH) {
if (new_name.length() >= MAX_ALIAS_LENGTH) {
Con_Printf("Alias name is too long\n");
return;
}
std::optional<command::alias> new_alias = std::nullopt;
std::optional<alias> new_alias = std::nullopt;
// if the alias already exists, reuse it
for (auto &[name, alias]: command::ALIASES) {
for (auto &[name, alias]: ALIASES) {
if (name == new_name) {
new_alias = alias;
alias.value.clear();
@@ -154,26 +150,21 @@ namespace command {
}
if (!new_alias.has_value()) {
new_alias = command::alias{};
new_alias = alias{};
}
new_alias->name = new_name;
// copy the rest of the command line
cmd[0] = 0; // start out with a null string
auto c = argc();
// TODO: ew
for (auto i = 2; i < c; i++) {
q_strlcat(cmd, argv(i).value().c_str(), sizeof(cmd));
if (i != c - 1)
q_strlcat(cmd, " ", sizeof(cmd));
auto cmd = std::string{};
const auto count = argc();
for (auto i = 2; i < count; i++) {
cmd += argv(i).value();
if (i != count - 1) {
cmd += ' ';
}
}
if (q_strlcat(cmd, "\n", sizeof(cmd)) >= sizeof(cmd)) {
Con_Printf("alias value too long!\n");
cmd[0] = '\n'; // nullify the string
cmd[1] = 0;
}
new_alias->value = Z_Strdup(cmd);
cmd += '\n';
new_alias->value = cmd;
break;
}
}
@@ -195,9 +186,9 @@ namespace command {
*/
void _stuffcmds() {
char cmds[CMDLINE_LENGTH];
int i, j, plus;
int i, j;
plus = false; // On Unix, argv[0] is command name
auto plus = false; // On Unix, argv[0] is command name
for (i = 0, j = 0; cmdline.string[i]; i++) {
if (cmdline.string[i] == '+') {
@@ -214,33 +205,32 @@ namespace command {
}
cmds[j] = 0;
Cbuf_InsertText(cmds);
buffer::insert_text(cmds);
}
#include "default_cfg.hpp"
void _exec() {
const char *f;
int mark;
if (argc() != 2) {
Con_Printf("exec <filename> : execute a script file\n");
return;
}
mark = Hunk_LowMark();
auto filename = argv(1);
f = (const char *) COM_LoadHunkFile(filename->c_str(), NULL);
if (!f && !strcmp(argv(1)->c_str(), "default.cfg")) {
const auto mark = Hunk_LowMark();
const auto filename = argv(1);
auto f = reinterpret_cast<const char *>(COM_LoadHunkFile(filename->c_str(), nullptr));
if (f == nullptr && argv(1) == "default.cfg") {
f = default_cfg; /* see above.. */
}
if (!f) {
if (f == nullptr) {
Con_Printf("couldn't exec %s\n", argv(1)->c_str());
return;
}
Con_Printf("execing %s\n", argv(1)->c_str());
Cbuf_InsertText(f);
buffer::insert_text(f);
if (f != default_cfg) {
Hunk_FreeToLowMark(mark);
}
@@ -252,7 +242,7 @@ namespace command {
Con_Printf("\n");
}
char *tint_substring(const char *in, const char *substr, char *out, size_t outsize) {
char *tint_substring(const char *in, const char *substr, char *out, const size_t outsize) {
int l;
char *m;
q_strlcpy(out, in, outsize);
@@ -311,8 +301,8 @@ namespace command {
add("exec", _exec);
add("echo", _echo);
add("alias", _alias);
add("cmd", Cmd_ForwardToServer);
add("wait", set_wait);
add("cmd", forward_to_server);
add("wait", _set_wait);
add("apropos", _apropos);
add("find", _apropos);
@@ -424,7 +414,7 @@ namespace command {
if (argc() == 0)
return; // no tokens
auto needle = argv(0).value();
const auto needle = argv(0).value();
// check functions
for (const auto &[name, ref]: REFS) {
if (!q_strcasecmp(needle.c_str(), name.c_str())) {
@@ -436,7 +426,7 @@ namespace command {
// check alias
for (const auto &[name, alias]: ALIASES) {
if (!q_strcasecmp(argv(0).value().c_str(), name.c_str())) {
Cbuf_InsertText(alias.value.c_str());
buffer::insert_text(alias.value);
return;
}
}
@@ -473,9 +463,6 @@ namespace command {
alias_view get_aliases() {
return std::ranges::views::values(ALIASES);
}
void print(std::string text) {
}
}
@@ -490,201 +477,72 @@ namespace command {
=============================================================================
*/
sizebuf_t cmd_text;
/*
============
Cbuf_Init
============
*/
void Cbuf_Init(void) {
SZ_Alloc(&cmd_text, 1 << 18);
// space for commands and script files. spike -- was 8192, but modern configs can be _HUGE_, at least if they contain lots of comments/docs for things.
}
/*
============
Cbuf_AddText
Adds command text at the end of the buffer
============
*/
void Cbuf_AddText(const char *text) {
int l;
l = Q_strlen(text);
if (cmd_text.cursize + l >= cmd_text.maxsize) {
Con_Printf("Cbuf_AddText: overflow\n");
return;
namespace command::buffer {
namespace {
constexpr std::size_t MAX_BUFFER_SIZE = 1 << 18;
std::string _buffer;
}
SZ_Write(&cmd_text, text, Q_strlen(text));
}
/*
============
Cbuf_InsertText
Adds command text immediately after the current command
Adds a \n to the text
FIXME: actually change the command buffer to do less copying
============
*/
void Cbuf_InsertText(const char *text) {
char *temp;
int templen;
// copy off any commands still remaining in the exec buffer
templen = cmd_text.cursize;
if (templen) {
temp = (char *) Z_Malloc(templen);
Q_memcpy(temp, cmd_text.data, templen);
SZ_Clear(&cmd_text);
} else
temp = NULL; // shut up compiler
// add the entire text of the file
Cbuf_AddText(text);
SZ_Write(&cmd_text, "\n", 1);
// add the copied off data
if (templen) {
SZ_Write(&cmd_text, temp, templen);
Z_Free(temp);
void init() {
_buffer = std::string{};
_buffer.reserve(MAX_BUFFER_SIZE);
}
}
/*
============
Cbuf_Execute
============
*/
void Cbuf_Execute(void) {
int i;
char *text;
char line[1024];
int quotes;
void add_text(const std::string &text) {
if (_buffer.length() + text.length() >= MAX_BUFFER_SIZE) {
Con_Printf("command::buffer::add_text(): overflow\n");
return;
}
while (cmd_text.cursize) {
// find a \n or ; line break
text = (char *) cmd_text.data;
_buffer.append(text);
}
quotes = 0;
for (i = 0; i < cmd_text.cursize; i++) {
if (text[i] == '"')
quotes++;
if (!(quotes & 1) && text[i] == ';')
break; // don't break if inside a quoted string
if (text[i] == '\n')
void insert_text(const std::string &text) {
if (_buffer.length() + text.length() >= MAX_BUFFER_SIZE) {
Con_Printf("command::buffer::insert_text(): overflow\n");
return;
}
_buffer = text + _buffer;
}
void execute() {
while (!_buffer.empty()) {
std::string line{};
auto quotes = 0;
for (const auto c: _buffer) {
if (c == '"') {
quotes++;
}
if (!(quotes & 1) && c == ';') {
break; // don't break if inside a quoted string
}
if (c == '\n') {
break;
}
line += c;
}
// delete the text from the command buffer and move remaining commands down
// this is necessary because commands (exec, alias) can insert data at the
// beginning of the text buffer
if (line.length() + 1 == _buffer.length()) {
_buffer.clear();
} else {
_buffer = _buffer.substr(line.length() + 1);
}
// execute the command line
execute_string(line, source::command);
if (is_waiting()) {
// skip out while text still remains in buffer, leaving it
// for next frame
stop_waiting();
break;
}
if (i > (int) sizeof(line) - 1) {
memcpy(line, text, sizeof(line) - 1);
line[sizeof(line) - 1] = 0;
} else {
memcpy(line, text, i);
line[i] = 0;
}
// delete the text from the command buffer and move remaining commands down
// this is necessary because commands (exec, alias) can insert data at the
// beginning of the text buffer
if (i == cmd_text.cursize)
cmd_text.cursize = 0;
else {
i++;
cmd_text.cursize -= i;
memmove(text, text + i, cmd_text.cursize);
}
// execute the command line
command::execute_string(line, command::source::command);
if (command::is_waiting()) {
// skip out while text still remains in buffer, leaving it
// for next frame
command::stop_waiting();
break;
}
}
}
}
/*
==============================================================================
SCRIPT COMMANDS
==============================================================================
*/
/* id1/pak0.pak from 2021 re-release doesn't have a default.cfg
* embedding Quakespasm's customized default.cfg for that... */
/*
=============================================================================
COMMAND EXECUTION
=============================================================================
*/
/*
============
Cmd_AddCommand
============
*/
/*
============
Cmd_Exists
============
*/
/*
============
Cmd_CompleteCommand
============
*/
/*
============
Cmd_ExecuteString
A complete command line has been parsed, so try to execute it
FIXME: lookupnoadd the token to speed search?
============
*/
/*
===================
Cmd_ForwardToServer
Sends the entire command line over to the server
===================
*/
void Cmd_ForwardToServer(void) {
}
/*
================
Cmd_CheckParm
Returns the position (1 to argc-1) in the command's argument list
where the given parameter apears, or 0 if not present
================
*/
int Cmd_CheckParm(const char *parm) {
}