Skip to content

Fix `exec`ing a cfg file that have no ending newline cause the last command concatenates next received command

NaitLee requested to merge NaitLee/378-exec-file-eof-no-newline into master

This branch is to fix a bad console behavior stated in #378 (closed). You may want to know more about that before reading this merge request.

The commit I have pushed fixes this issue, but probably not the best, as it reallowcates a chunk of memory, only for adding a '\n'.

So there comes a discussion. Please vote for a best solution…

  1. Modify ParseInput/Linking code to fix this “regression”

The root cause of this seemingly “bug” is that console was refactored long time ago, and introduced a new command system.

In new code, Cmd_ParseInput will mark an potentially unfinished command as “pending”, so that it may be completed by subsequent received input.

Subsequently, if a command without ending newline was given, Cmd_ParseInput will leave this command unfinished and concatenate the next given command directly after the previous.

So, It’s not a bug, it’s a feature. Probably useful feature.

Touching this part just to fix cfg-file-no-ending-newline problem seems an overkill. This part (currently) have no idea about whether it received input from a file either.

  1. (current) Manually add a '\n' after reading a cfg file if it isn’t there

Currently, the entire cfg file is read to memory, then given to Cmd_ParseInput.

So this fix is direct and easy to understand, and only done in-case, but involves memory re-allocation when that happens.

It maybe a problem when input file is way too big.

Maybe notify/warn user about missing \n in console? idk if this is ok… though good habit to finish file with \n

  1. Read & feed the cfg file line-by-line (or chunk-by-chunk), until end, and add finishing \n if the last line/chunk doesn’t contain it

This avoids memory reallocation in large chunks. But the coding will be somewhat difficult.

That said, maybe the new console behavior will help me out.

I’d try this one if you with.


Signed-off-by: NaitLee naitli@foxmail.com

Edited by NaitLee

Merge request reports