Port onto libakstdlib 2b79aca and convert the eight bool predicates
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m27s
akbasic CI Build / coverage (push) Failing after 3m44s
akbasic CI Build / sanitizers (push) Failing after 4m43s
akbasic CI Build / mutation_test (push) Failing after 3m45s
akbasic CI Build / akgl_build (push) Failing after 4m51s
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m27s
akbasic CI Build / coverage (push) Failing after 3m44s
akbasic CI Build / sanitizers (push) Failing after 4m43s
akbasic CI Build / mutation_test (push) Failing after 3m45s
akbasic CI Build / akgl_build (push) Failing after 4m51s
akbasic's src/ now calls libakstdlib 313 times and raw libc 7 -- 2.2% bypassed, against 86.4% on the same tree before this. The submodule bump 669b2b3 -> 2b79aca needed no source change of its own: the release is drop-in for what akbasic already used. Seven of the eight sites the earlier port left on raw libc change their own signature rather than swallowing an error, per andrew's ruling on libakstdlib#38. word_is, the is_waiting_for pair, the scanner's is_at_end, peek, peek_next and match_next_char, format.c's overflow, and sink_akgl's scroll/newline/putchar_at/echo_line/edit_key chain all return an akerr_ErrorContext * and hand the answer back through an out parameter. is_waiting_for and is_waiting_for_any are a public header change; every call site that used one as a term in a condition hoists it into a statement first. verb_compare is the eighth and stays on strcmp. bsearch(3) fixes the comparator's signature, so there is no out parameter to report through -- which is what libakstdlib#38 concluded. It carries a comment saying so and saying why the bypass is safe there. Six snprintf sites stay raw because they want truncation as an answer rather than an error, and aksl_snprintf cannot express that until libakstdlib#34 hands the required length back. Each of the six says so at the site. Two of them, in host.c, are a latent defect rather than a decision: a host type name over 31 characters truncates silently and two sharing a prefix then collide, where structtype.c refuses the same case. DLOAD leaked a file descriptor. Its read loop sat inside an ATTEMPT and the PASS in it returned past CLEANUP, so a scan error left the file open. Hoisting the loop into its own helper to convert fgets fixes it. Refs libakstdlib#26, libakstdlib#38 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,9 +15,8 @@
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <akerror.h>
|
||||
#include <akstdlib.h>
|
||||
|
||||
#include <akgl/controller.h>
|
||||
#include <akgl/draw.h>
|
||||
@@ -32,15 +31,24 @@
|
||||
/** @brief Bytes per row, including the terminator. */
|
||||
#define SINK_MAX_COLUMNS 256
|
||||
|
||||
/** @brief Scroll the grid up by one row, dropping the top one. */
|
||||
static void scroll(akbasic_AkglSink *state)
|
||||
/**
|
||||
* @brief Scroll the grid up by one row, dropping the top one.
|
||||
*
|
||||
* Returns a context rather than `void` because the row moves can fail and there
|
||||
* was nowhere to say so. Everything on this chain -- scroll, newline,
|
||||
* putchar_at, echo_line, edit_key -- returns one for the same reason, and the
|
||||
* chain already ends at sink_write, sink_writeln and sink_readline, which
|
||||
* returned one all along. See libakstdlib #38.
|
||||
*/
|
||||
static akerr_ErrorContext *scroll(akbasic_AkglSink *state)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int row = 0;
|
||||
|
||||
for ( row = 0; row < SINK_MAX_ROWS - 1; row++ ) {
|
||||
memcpy(state->text[row], state->text[row + 1], SINK_MAX_COLUMNS);
|
||||
PASS(errctx, aksl_memcpy(state->text[row], state->text[row + 1], SINK_MAX_COLUMNS));
|
||||
}
|
||||
memset(state->text[SINK_MAX_ROWS - 1], 0, SINK_MAX_COLUMNS);
|
||||
PASS(errctx, aksl_memset(state->text[SINK_MAX_ROWS - 1], 0, SINK_MAX_COLUMNS));
|
||||
if ( state->cursorrow > 0 ) {
|
||||
state->cursorrow -= 1;
|
||||
}
|
||||
@@ -59,13 +67,16 @@ static void scroll(akbasic_AkglSink *state)
|
||||
}
|
||||
|
||||
/** @brief Move to the start of the next row, scrolling if that runs off the end. */
|
||||
static void newline(akbasic_AkglSink *state)
|
||||
static akerr_ErrorContext *newline(akbasic_AkglSink *state)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
state->cursorcol = 0;
|
||||
state->cursorrow += 1;
|
||||
if ( state->cursorrow >= state->rows || state->cursorrow >= SINK_MAX_ROWS ) {
|
||||
scroll(state);
|
||||
PASS(errctx, scroll(state));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,16 +100,17 @@ static void newline(akbasic_AkglSink *state)
|
||||
* cleared between rows, so the gap holds the tail of some longer row that used
|
||||
* to be here.
|
||||
*/
|
||||
static void putchar_at(akbasic_AkglSink *state, char c)
|
||||
static akerr_ErrorContext *putchar_at(akbasic_AkglSink *state, char c)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int col = 0;
|
||||
|
||||
if ( c == '\n' ) {
|
||||
newline(state);
|
||||
return;
|
||||
PASS(errctx, newline(state));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
if ( state->cursorcol >= state->columns || state->cursorcol >= SINK_MAX_COLUMNS - 1 ) {
|
||||
newline(state);
|
||||
PASS(errctx, newline(state));
|
||||
}
|
||||
for ( col = 0; col < state->cursorcol; col++ ) {
|
||||
if ( state->text[state->cursorrow][col] == '\0' ) {
|
||||
@@ -111,6 +123,7 @@ static void putchar_at(akbasic_AkglSink *state, char c)
|
||||
state->text[state->cursorrow][state->cursorcol] = c;
|
||||
state->cursorcol += 1;
|
||||
state->text[state->cursorrow][state->cursorcol] = '\0';
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
static akerr_ErrorContext *sink_write(akbasic_TextSink *self, const char *text)
|
||||
@@ -125,7 +138,7 @@ static akerr_ErrorContext *sink_write(akbasic_TextSink *self, const char *text)
|
||||
FAIL_ZERO_RETURN(errctx, (state != NULL), AKERR_NULLPOINTER,
|
||||
"akgl sink has no state");
|
||||
for ( i = 0; text[i] != '\0'; i++ ) {
|
||||
putchar_at(state, text[i]);
|
||||
PASS(errctx, putchar_at(state, text[i]));
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -137,7 +150,7 @@ static akerr_ErrorContext *sink_writeln(akbasic_TextSink *self, const char *text
|
||||
|
||||
PASS(errctx, sink_write(self, text));
|
||||
state = (akbasic_AkglSink *)self->self;
|
||||
newline(state);
|
||||
PASS(errctx, newline(state));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -151,8 +164,9 @@ static akerr_ErrorContext *sink_writeln(akbasic_TextSink *self, const char *text
|
||||
* memory, and the host draws it when it draws its frame -- so the cost is a
|
||||
* couple of memcpy-sized loops per keystroke.
|
||||
*/
|
||||
static void echo_line(akbasic_AkglSink *state)
|
||||
static akerr_ErrorContext *echo_line(akbasic_AkglSink *state)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
int i = 0;
|
||||
int erasedto = 0;
|
||||
int endedat = 0;
|
||||
@@ -160,14 +174,14 @@ static void echo_line(akbasic_AkglSink *state)
|
||||
state->cursorrow = state->editrow;
|
||||
state->cursorcol = state->editcol;
|
||||
for ( i = 0; i < state->echolen; i++ ) {
|
||||
putchar_at(state, ' ');
|
||||
PASS(errctx, putchar_at(state, ' '));
|
||||
}
|
||||
erasedto = state->cursorrow;
|
||||
|
||||
state->cursorrow = state->editrow;
|
||||
state->cursorcol = state->editcol;
|
||||
for ( i = 0; i < state->editlen; i++ ) {
|
||||
putchar_at(state, state->editline[i]);
|
||||
PASS(errctx, putchar_at(state, state->editline[i]));
|
||||
}
|
||||
endedat = state->cursorrow;
|
||||
state->echolen = state->editlen;
|
||||
@@ -186,6 +200,7 @@ static void echo_line(akbasic_AkglSink *state)
|
||||
for ( i = endedat + 1; i <= erasedto && i < SINK_MAX_ROWS; i++ ) {
|
||||
state->text[i][0] = '\0';
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/** @brief Append one byte to the line being typed, if there is room for it. */
|
||||
@@ -215,14 +230,15 @@ static void edit_append(akbasic_AkglSink *state, char c)
|
||||
* Backspace and Escape are keys rather than characters and several of them
|
||||
* compose to text SDL would otherwise hand straight through.
|
||||
*/
|
||||
static void edit_key(akbasic_AkglSink *state, const akgl_Keystroke *key, bool *submitted)
|
||||
static akerr_ErrorContext *edit_key(akbasic_AkglSink *state, const akgl_Keystroke *key, bool *submitted)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
size_t i = 0;
|
||||
|
||||
if ( key->key == SDLK_RETURN || key->key == SDLK_KP_ENTER ||
|
||||
key->key == '\r' || key->key == '\n' ) {
|
||||
*submitted = true;
|
||||
return;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
if ( key->key == SDLK_BACKSPACE || key->key == '\b' || key->key == 0x7f ) {
|
||||
if ( state->editlen > 0 ) {
|
||||
@@ -235,15 +251,15 @@ static void edit_key(akbasic_AkglSink *state, const akgl_Keystroke *key, bool *s
|
||||
*/
|
||||
state->editlen -= 1;
|
||||
state->editline[state->editlen] = '\0';
|
||||
echo_line(state);
|
||||
PASS(errctx, echo_line(state));
|
||||
}
|
||||
return;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
if ( key->key == SDLK_ESCAPE || key->key == 0x1b ) {
|
||||
state->editlen = 0;
|
||||
state->editline[0] = '\0';
|
||||
echo_line(state);
|
||||
return;
|
||||
PASS(errctx, echo_line(state));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
if ( key->text[0] != '\0' ) {
|
||||
@@ -257,8 +273,8 @@ static void edit_key(akbasic_AkglSink *state, const akgl_Keystroke *key, bool *s
|
||||
edit_append(state, key->text[i]);
|
||||
}
|
||||
}
|
||||
echo_line(state);
|
||||
return;
|
||||
PASS(errctx, echo_line(state));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -277,14 +293,15 @@ static void edit_key(akbasic_AkglSink *state, const akgl_Keystroke *key, bool *s
|
||||
*/
|
||||
if ( key->key >= 0x20 && key->key < 0x7f ) {
|
||||
edit_append(state, (char)toupper((unsigned char)key->key));
|
||||
echo_line(state);
|
||||
return;
|
||||
PASS(errctx, echo_line(state));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Neither: a cursor key, a function key or a bare modifier. Not an editing
|
||||
* command here -- a script's own GET loop is what wants those.
|
||||
*/
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -312,7 +329,7 @@ static akerr_ErrorContext AKERR_NOIGNORE *edit_loop(akbasic_AkglSink *state, boo
|
||||
*/
|
||||
PASS(errctx, akgl_controller_poll_keystroke(&key, &available));
|
||||
if ( available ) {
|
||||
edit_key(state, &key, &submitted);
|
||||
PASS(errctx, edit_key(state, &key, &submitted));
|
||||
continue;
|
||||
}
|
||||
/*
|
||||
@@ -375,9 +392,8 @@ static akerr_ErrorContext *sink_readline(akbasic_TextSink *self, char *dest, siz
|
||||
if ( *eof ) {
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
strncpy(dest, state->editline, len - 1);
|
||||
dest[len - 1] = '\0';
|
||||
newline(state);
|
||||
PASS(errctx, aksl_strncpy(dest, len, state->editline, len - 1));
|
||||
PASS(errctx, newline(state));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -391,7 +407,7 @@ static akerr_ErrorContext *sink_clear(akbasic_TextSink *self)
|
||||
state = (akbasic_AkglSink *)self->self;
|
||||
FAIL_ZERO_RETURN(errctx, (state != NULL), AKERR_NULLPOINTER,
|
||||
"akgl sink has no state");
|
||||
memset(state->text, 0, sizeof(state->text));
|
||||
PASS(errctx, aksl_memset(state->text, 0, sizeof(state->text)));
|
||||
state->cursorcol = 0;
|
||||
state->cursorrow = 0;
|
||||
SUCCEED_RETURN(errctx);
|
||||
@@ -600,7 +616,7 @@ akerr_ErrorContext *akbasic_sink_init_akgl(akbasic_TextSink *obj, akbasic_AkglSi
|
||||
"A %dx%d text area has no room for a %dx%d character",
|
||||
w, h, cellw, cellh);
|
||||
|
||||
memset(state, 0, sizeof(*state));
|
||||
PASS(errctx, aksl_memset(state, 0, sizeof(*state)));
|
||||
state->renderer = renderer;
|
||||
state->font = font;
|
||||
state->color.r = 0xff;
|
||||
|
||||
Reference in New Issue
Block a user