Port onto libakstdlib 2b79aca and convert the eight bool predicates
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:
@@ -22,7 +22,6 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <akerror.h>
|
||||
#include <akstdlib.h>
|
||||
@@ -44,7 +43,7 @@ akerr_ErrorContext *akbasic_disk_state_init(akbasic_DiskState *obj)
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL), AKERR_NULLPOINTER, "NULL disk state in init");
|
||||
memset(obj, 0, sizeof(*obj));
|
||||
PASS(errctx, aksl_memset(obj, 0, sizeof(*obj)));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -103,7 +102,12 @@ static akerr_ErrorContext *string_arg(akbasic_Runtime *obj, akbasic_ASTLeaf *arg
|
||||
PASS(errctx, akbasic_runtime_evaluate(obj, arg, &value));
|
||||
FAIL_NONZERO_RETURN(errctx, (value->valuetype != AKBASIC_TYPE_STRING), AKBASIC_ERR_TYPE,
|
||||
"%s expected a file name", verb);
|
||||
snprintf(dest, len, "%s", value->stringval);
|
||||
/*
|
||||
* A plain copy, so aksl_strcpy rather than aksl_snprintf: it refuses rather
|
||||
* than truncates, and every caller here hands in a buffer the size of a
|
||||
* string value, so a name that did not fit could only ever be a bug.
|
||||
*/
|
||||
PASS(errctx, aksl_strcpy(dest, len, value->stringval));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
@@ -157,7 +161,7 @@ static akerr_ErrorContext *open_file(akbasic_Runtime *obj, akbasic_ASTLeaf *expr
|
||||
}
|
||||
|
||||
PASS(errctx, aksl_fopen(name, mode, &channel->fp));
|
||||
snprintf(channel->name, sizeof(channel->name), "%s", name);
|
||||
PASS(errctx, aksl_strcpy(channel->name, sizeof(channel->name), name));
|
||||
channel->writing = writing;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -265,13 +269,24 @@ akerr_ErrorContext *akbasic_cmd_record(akbasic_Runtime *obj, akbasic_ASTLeaf *ex
|
||||
akerr_ErrorContext *akbasic_cmd_scratch(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akerr_ErrorContext *failure = NULL;
|
||||
char name[AKBASIC_MAX_STRING_LENGTH];
|
||||
|
||||
(void)lval; (void)rval;
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && dest != NULL), AKERR_NULLPOINTER, "NULL argument in SCRATCH");
|
||||
PASS(errctx, string_arg(obj, akbasic_leaf_first_argument(expr), "SCRATCH", name, sizeof(name)));
|
||||
FAIL_ZERO_RETURN(errctx, (remove(name) == 0), AKERR_IO,
|
||||
"SCRATCH could not delete \"%s\"", name);
|
||||
/*
|
||||
* Caught and restated rather than passed. aksl_remove raises the bare errno,
|
||||
* and "No such file or directory" on its own does not say which file or
|
||||
* which verb wanted it -- so the wrapper's error is retired here and the
|
||||
* message the program sees keeps naming both.
|
||||
*/
|
||||
failure = aksl_remove(name);
|
||||
if ( failure != NULL ) {
|
||||
failure->handled = true;
|
||||
IGNORE(akerr_release_error(failure));
|
||||
FAIL_RETURN(errctx, AKERR_IO, "SCRATCH could not delete \"%s\"", name);
|
||||
}
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -279,6 +294,7 @@ akerr_ErrorContext *akbasic_cmd_scratch(akbasic_Runtime *obj, akbasic_ASTLeaf *e
|
||||
akerr_ErrorContext *akbasic_cmd_rename(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akerr_ErrorContext *failure = NULL;
|
||||
akbasic_ASTLeaf *arg = NULL;
|
||||
char from[AKBASIC_MAX_STRING_LENGTH];
|
||||
char to[AKBASIC_MAX_STRING_LENGTH];
|
||||
@@ -288,8 +304,13 @@ akerr_ErrorContext *akbasic_cmd_rename(akbasic_Runtime *obj, akbasic_ASTLeaf *ex
|
||||
arg = akbasic_leaf_first_argument(expr);
|
||||
PASS(errctx, string_arg(obj, arg, "RENAME", from, sizeof(from)));
|
||||
PASS(errctx, string_arg(obj, (arg != NULL ? arg->next : NULL), "RENAME", to, sizeof(to)));
|
||||
FAIL_ZERO_RETURN(errctx, (rename(from, to) == 0), AKERR_IO,
|
||||
"RENAME could not rename \"%s\" to \"%s\"", from, to);
|
||||
/* Caught and restated, for the reason SCRATCH gives: the message names both files. */
|
||||
failure = aksl_rename(from, to);
|
||||
if ( failure != NULL ) {
|
||||
failure->handled = true;
|
||||
IGNORE(akerr_release_error(failure));
|
||||
FAIL_RETURN(errctx, AKERR_IO, "RENAME could not rename \"%s\" to \"%s\"", from, to);
|
||||
}
|
||||
SUCCEED_TRUE(obj, dest);
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -486,6 +507,7 @@ akerr_ErrorContext *akbasic_disk_write(akbasic_Runtime *obj, int64_t number, con
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akbasic_Channel *channel = NULL;
|
||||
size_t length = 0;
|
||||
size_t put = 0;
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, (obj != NULL && text != NULL), AKERR_NULLPOINTER,
|
||||
@@ -493,7 +515,8 @@ akerr_ErrorContext *akbasic_disk_write(akbasic_Runtime *obj, int64_t number, con
|
||||
PASS(errctx, open_channel(obj, number, &channel));
|
||||
FAIL_ZERO_RETURN(errctx, channel->writing, AKBASIC_ERR_STATE,
|
||||
"Channel %" PRId64 " was opened for reading", number);
|
||||
PASS(errctx, aksl_fwrite(text, 1, strlen(text), channel->fp, &put));
|
||||
PASS(errctx, aksl_strlen(text, &length));
|
||||
PASS(errctx, aksl_fwrite(text, 1, length, channel->fp, &put));
|
||||
PASS(errctx, aksl_fwrite("\n", 1, 1, channel->fp, &put));
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
@@ -525,7 +548,7 @@ akerr_ErrorContext *akbasic_disk_readline(akbasic_Runtime *obj, int64_t number,
|
||||
*eof = true;
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
newline = strchr(dest, '\n');
|
||||
PASS(errctx, aksl_strchr(dest, '\n', &newline));
|
||||
if ( newline != NULL ) {
|
||||
*newline = '\0';
|
||||
}
|
||||
@@ -582,7 +605,7 @@ akerr_ErrorContext *akbasic_cmd_input_channel(akbasic_Runtime *obj, akbasic_ASTL
|
||||
* variable reading an empty line gets zero, the same as a C128.
|
||||
*/
|
||||
PASS(errctx, akbasic_leaf_init(&literal, AKBASIC_LEAF_LITERAL_STRING));
|
||||
snprintf(literal.literal_string, sizeof(literal.literal_string), "%s", text);
|
||||
PASS(errctx, aksl_strcpy(literal.literal_string, sizeof(literal.literal_string), text));
|
||||
if ( akbasic_leaf_identifier_type(identifier) == AKBASIC_TYPE_INTEGER ) {
|
||||
long long converted = 0;
|
||||
|
||||
@@ -610,13 +633,69 @@ akerr_ErrorContext *akbasic_cmd_input_channel(akbasic_Runtime *obj, akbasic_ASTL
|
||||
|
||||
/* ---------------------------------------------------------------- VERIFY -- */
|
||||
|
||||
/**
|
||||
* @brief Count the lines of @p fp that differ from the program in memory.
|
||||
*
|
||||
* Its own function so that the comparison can be reached with a single CATCH.
|
||||
* Written inline it would be a loop inside VERIFY's ATTEMPT, where neither form
|
||||
* is available: CATCH expands to a break that would escape only the loop, and
|
||||
* PASS returns past the CLEANUP that closes the file. One call is neither, so
|
||||
* the wrappers below can PASS in the ordinary way.
|
||||
*/
|
||||
static akerr_ErrorContext *verify_lines(akbasic_Runtime *obj, FILE *fp, int64_t *dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
akerr_ErrorContext *got = NULL;
|
||||
char line[AKBASIC_MAX_LINE_LENGTH];
|
||||
const char *filetext = NULL;
|
||||
char *newline = NULL;
|
||||
int64_t lineno = 0;
|
||||
size_t length = 0;
|
||||
int cmp = 0;
|
||||
|
||||
*dest = 0;
|
||||
for ( lineno = 0; lineno < AKBASIC_MAX_SOURCE_LINES; lineno++ ) {
|
||||
if ( obj->source[lineno].code[0] == '\0' ) {
|
||||
continue;
|
||||
}
|
||||
got = aksl_fgets(line, sizeof(line), fp, &length);
|
||||
if ( got != NULL ) {
|
||||
got->handled = true;
|
||||
IGNORE(akerr_release_error(got));
|
||||
*dest += 1;
|
||||
break;
|
||||
}
|
||||
PASS(errctx, aksl_strchr(line, '\n', &newline));
|
||||
if ( newline != NULL ) {
|
||||
*newline = '\0';
|
||||
}
|
||||
/*
|
||||
* The stored line has had its number stripped, so the file's copy is
|
||||
* compared from past its own number.
|
||||
*/
|
||||
filetext = line;
|
||||
while ( *filetext == ' ' ) {
|
||||
filetext += 1;
|
||||
}
|
||||
while ( *filetext >= '0' && *filetext <= '9' ) {
|
||||
filetext += 1;
|
||||
}
|
||||
while ( *filetext == ' ' ) {
|
||||
filetext += 1;
|
||||
}
|
||||
PASS(errctx, aksl_strcmp(filetext, obj->source[lineno].code, &cmp));
|
||||
if ( cmp != 0 ) {
|
||||
*dest += 1;
|
||||
}
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *akbasic_cmd_dverify(akbasic_Runtime *obj, akbasic_ASTLeaf *expr, akbasic_Value *lval, akbasic_Value *rval, akbasic_Value **dest)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
FILE *fp = NULL;
|
||||
char name[AKBASIC_MAX_STRING_LENGTH];
|
||||
char line[AKBASIC_MAX_LINE_LENGTH];
|
||||
int64_t lineno = 0;
|
||||
int64_t mismatch = 0;
|
||||
|
||||
(void)lval; (void)rval;
|
||||
@@ -632,47 +711,7 @@ akerr_ErrorContext *akbasic_cmd_dverify(akbasic_Runtime *obj, akbasic_ASTLeaf *e
|
||||
*/
|
||||
PASS(errctx, aksl_fopen(name, "r", &fp));
|
||||
ATTEMPT {
|
||||
for ( lineno = 0; lineno < AKBASIC_MAX_SOURCE_LINES; lineno++ ) {
|
||||
size_t length = 0;
|
||||
akerr_ErrorContext *got = NULL;
|
||||
char *newline = NULL;
|
||||
|
||||
if ( obj->source[lineno].code[0] == '\0' ) {
|
||||
continue;
|
||||
}
|
||||
got = aksl_fgets(line, sizeof(line), fp, &length);
|
||||
if ( got != NULL ) {
|
||||
got->handled = true;
|
||||
IGNORE(akerr_release_error(got));
|
||||
mismatch += 1;
|
||||
break;
|
||||
}
|
||||
newline = strchr(line, '\n');
|
||||
if ( newline != NULL ) {
|
||||
*newline = '\0';
|
||||
}
|
||||
/*
|
||||
* The stored line has had its number stripped, so the file's copy is
|
||||
* compared from past its own number. PASS rather than CATCH: this is
|
||||
* a loop.
|
||||
*/
|
||||
{
|
||||
const char *filetext = line;
|
||||
|
||||
while ( *filetext == ' ' ) {
|
||||
filetext += 1;
|
||||
}
|
||||
while ( *filetext >= '0' && *filetext <= '9' ) {
|
||||
filetext += 1;
|
||||
}
|
||||
while ( *filetext == ' ' ) {
|
||||
filetext += 1;
|
||||
}
|
||||
if ( strcmp(filetext, obj->source[lineno].code) != 0 ) {
|
||||
mismatch += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
CATCH(errctx, verify_lines(obj, fp, &mismatch));
|
||||
} CLEANUP {
|
||||
IGNORE(aksl_fclose(fp));
|
||||
} PROCESS(errctx) {
|
||||
|
||||
Reference in New Issue
Block a user