From de05b12da8140efd6b26e927347689f9f131e8ff Mon Sep 17 00:00:00 2001 From: Eric Smith Date: Tue, 7 Nov 2023 10:13:52 -0400 Subject: [PATCH] Change the way xxd generated files are used, so that we do not have to append a 0 to them (and thus can use xxd itself to generate the file rather than piping through sed). The advantage is that if xxd is missing the build fails cleanly with no 0 length files left behind --- Changelog.txt | 1 + Makefile | 4 ++-- backends/asm/outasm.c | 12 +++++++++++- backends/brkdebug.c | 2 +- backends/nucode/nuir.c | 2 +- frontends/common.h | 1 + frontends/lexer.c | 9 +++++++-- frontends/lexer.h | 2 +- spinc.c | 21 +++++++++++++-------- sys/bytecode_rom.spin.h | 2 +- sys/common.spin.h | 2 +- sys/common_pasm.spin.h | 2 +- sys/float.spin.h | 2 +- sys/gc_bytecode.spin.h | 2 +- sys/gc_pasm.spin.h | 2 +- sys/gcalloc.spin.h | 2 +- sys/lmm_cache.spin.h | 2 +- sys/lmm_compress.spin.h | 2 +- sys/lmm_orig.spin.h | 2 +- sys/lmm_slow.spin.h | 2 +- sys/lmm_trace.spin.h | 2 +- sys/nucode_util.spin.h | 2 +- sys/nuinterp.spin.h | 2 +- sys/p1_code.spin.h | 2 +- sys/p2_brkdebug.spin.h | 2 +- sys/p2_code.spin.h | 2 +- testlex.c | 10 +++++----- util/strdupcat.c | 7 +++++++ util/util.h | 8 ++++++++ 29 files changed, 75 insertions(+), 38 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index fe5682308..ff56260dd 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -2,6 +2,7 @@ Version 6.6.2 - Added warning (-Warray-index) for constant array indices out of bounds - Allow warnings and optimizations to be turned off with a `no-` prefix (so `warn(no-init-vars)` is like `warn(!init-vars)`). - Added some peephole optimizations to remove unnecessary sign extension +- Some internal changes to make recovering from missing xxd easier Version 6.6.1 - Changed type of `dat` array in C++ output to `unsigned char` diff --git a/Makefile b/Makefile index 41e67dd0d..e43018698 100644 --- a/Makefile +++ b/Makefile @@ -255,9 +255,9 @@ $(BUILD)/%.o: %.c # which is what the sed script will do # sys/%.spin.h: sys/%.spin - xxd -i $< | sed 's/\([0-9a-f]\)$$/\0, 0x00/' > $@ + xxd -i $< $@ sys/%.bas.h: sys/%.bas - xxd -i $< | sed 's/\([0-9a-f]\)$$/\0, 0x00/' > $@ + xxd -i $< $@ # # automatic dependencies diff --git a/backends/asm/outasm.c b/backends/asm/outasm.c index 11b3b2bbc..e89729b46 100644 --- a/backends/asm/outasm.c +++ b/backends/asm/outasm.c @@ -6332,32 +6332,42 @@ EmitBuiltins(IRList *irl) { if (HUB_CODE) { const char *builtin_lmm = NULL; + size_t builtin_lmm_len = 0; + if (gl_p2) { if (gl_fcache_size > 0) { builtin_lmm = builtin_fcache_p2; + builtin_lmm_len = strlen(builtin_fcache_p2); } } else { switch(gl_lmm_kind) { case LMM_KIND_SLOW: builtin_lmm = (const char *)sys_lmm_slow_spin; + builtin_lmm_len = sys_lmm_slow_spin_len; break; case LMM_KIND_TRACE: builtin_lmm = (const char *)sys_lmm_trace_spin; + builtin_lmm_len = sys_lmm_trace_spin_len; break; case LMM_KIND_CACHE: builtin_lmm = (const char *)sys_lmm_cache_spin; + builtin_lmm_len = sys_lmm_cache_spin_len; break; case LMM_KIND_COMPRESS: builtin_lmm = (const char *)sys_lmm_compress_spin; + builtin_lmm_len = sys_lmm_compress_spin_len; break; default: builtin_lmm = (const char *)sys_lmm_orig_spin; + builtin_lmm_len = sys_lmm_orig_spin_len; break; } } if (builtin_lmm) { + /* the header files are not zero terminated, so do that here */ Operand *loop; - loop = NewOperand(IMM_STRING, builtin_lmm, 0); + char *lmm_zero_str = strndup(builtin_lmm, builtin_lmm_len); + loop = NewOperand(IMM_STRING, lmm_zero_str, 0); EmitOp1(irl, OPC_LITERAL, loop); } } diff --git a/backends/brkdebug.c b/backends/brkdebug.c index 126890d01..520e1080e 100644 --- a/backends/brkdebug.c +++ b/backends/brkdebug.c @@ -371,7 +371,7 @@ Flexbuf CompileBrkDebugger(size_t appsize) { current = D; D->Lptr = (LexStream *)calloc(sizeof(*systemModule->Lptr), 1); D->Lptr->flags |= LEXSTREAM_FLAG_NOSRC; - strToLex(D->Lptr, (const char *)sys_p2_brkdebug_spin, "__brkdebug__", LANG_SPIN_SPIN2); + strToLex(D->Lptr, (const char *)sys_p2_brkdebug_spin, sys_p2_brkdebug_spin_len, "__brkdebug__", LANG_SPIN_SPIN2); spinyyparse(); ProcessModule(D); // We good now? diff --git a/backends/nucode/nuir.c b/backends/nucode/nuir.c index e509e651c..4f490204e 100644 --- a/backends/nucode/nuir.c +++ b/backends/nucode/nuir.c @@ -98,7 +98,7 @@ static void CreateBuiltinOp(NuIrOpcode op, const char *impl) { } void NuIrInit(NuContext *ctxt) { - const char *ptr = (char *)sys_nuinterp_spin; + const char *ptr = strndup((char *)sys_nuinterp_spin, sys_nuinterp_spin_len); const char *linestart; int c; int i; diff --git a/frontends/common.h b/frontends/common.h index c4f2a687f..f0fc34b34 100644 --- a/frontends/common.h +++ b/frontends/common.h @@ -37,6 +37,7 @@ typedef enum SpinExprState { struct lexstream { void *ptr; /* current pointer */ void *arg; /* original value of pointer */ + size_t maxbytes; int (*getcf)(LexStream *); #define UNGET_MAX 16 /* we can ungetc this many times */ int ungot[UNGET_MAX]; diff --git a/frontends/lexer.c b/frontends/lexer.c index 660b42139..fb37b8b16 100644 --- a/frontends/lexer.c +++ b/frontends/lexer.c @@ -125,8 +125,12 @@ strgetc(LexStream *L) { char *s; int c; - + size_t delta; s = (char *)L->ptr; + delta = s - (char *)L->arg; + if (delta >= L->maxbytes) { + return EOF; + } c = (*s++) & 0x00ff; if (c == '\r') { c = (*s++) & 0x00ff; @@ -141,13 +145,14 @@ strgetc(LexStream *L) } /* open a stream from a string s */ -void strToLex(LexStream *L, const char *s, const char *name, int language) +void strToLex(LexStream *L, const char *s, size_t maxBytes, const char *name, int language) { if (!L) { current->Lptr = L = (LexStream *)malloc(sizeof(*L)); } memset(L, 0, sizeof(*L)); L->arg = L->ptr = (void *)s; + L->maxbytes = maxBytes; L->getcf = strgetc; L->pendingLine = 1; L->fileName = name ? name : ""; diff --git a/frontends/lexer.h b/frontends/lexer.h index 81eeab9f6..ff0a88a8c 100644 --- a/frontends/lexer.h +++ b/frontends/lexer.h @@ -22,7 +22,7 @@ extern void resetLineState(LexStream *L); /* * function to open a lexer stream from a string */ -void strToLex(LexStream *lex, const char *s, const char *name, int language); +void strToLex(LexStream *lex, const char *s, size_t maxBytes, const char *name, int language); /* * function to open a lexer stream from a FILE diff --git a/spinc.c b/spinc.c index a68f5f26b..129154da1 100644 --- a/spinc.c +++ b/spinc.c @@ -107,6 +107,7 @@ InitGlobalModule(void) int oldtmpnum; int saveyydebug; const char *syscode = ""; + size_t syscode_len = 0; int debugVal = gl_debug; int bcVal = (gl_output == OUTPUT_BYTECODE); @@ -194,9 +195,11 @@ InitGlobalModule(void) switch (gl_interp_kind) { case INTERP_KIND_P1ROM: syscode = (const char *)sys_bytecode_rom_spin; + syscode_len = sys_bytecode_rom_spin_len; break; case INTERP_KIND_NUCODE: syscode = (const char *)sys_nucode_util_spin; + syscode_len = sys_nucode_util_spin_len; break; default: ERROR(NULL, "No internal code for bytecode type\n"); @@ -205,32 +208,34 @@ InitGlobalModule(void) } else { if (gl_p2) { syscode = (const char *)sys_p2_code_spin; + syscode_len = sys_p2_code_spin_len; } else { syscode = (const char *)sys_p1_code_spin; + syscode_len = sys_p1_code_spin_len; } } gl_normalizeIdents = 0; systemModule->Lptr = (LexStream *)calloc(sizeof(*systemModule->Lptr), 1); systemModule->Lptr->flags |= LEXSTREAM_FLAG_NOSRC; - strToLex(systemModule->Lptr, syscode, "_system_", LANG_SPIN_SPIN1); + strToLex(systemModule->Lptr, syscode, syscode_len, "_system_", LANG_SPIN_SPIN1); spinyyparse(); // add common PASM code if (gl_output != OUTPUT_BYTECODE) { - strToLex(systemModule->Lptr, (const char *)sys_common_pasm_spin, "_common_pasm_", LANG_SPIN_SPIN1); + strToLex(systemModule->Lptr, (const char *)sys_common_pasm_spin, sys_common_pasm_spin_len, "_common_pasm_", LANG_SPIN_SPIN1); spinyyparse(); } - strToLex(systemModule->Lptr, (const char *)sys_common_spin, "_common_", LANG_SPIN_SPIN1); + strToLex(systemModule->Lptr, (const char *)sys_common_spin, sys_common_spin_len, "_common_", LANG_SPIN_SPIN1); spinyyparse(); - strToLex(systemModule->Lptr, (const char *)sys_float_spin, "_float_", LANG_SPIN_SPIN1); + strToLex(systemModule->Lptr, (const char *)sys_float_spin, sys_float_spin_len, "_float_", LANG_SPIN_SPIN1); spinyyparse(); - strToLex(systemModule->Lptr, (const char *)sys_gcalloc_spin, "_gc_", LANG_SPIN_SPIN1); + strToLex(systemModule->Lptr, (const char *)sys_gcalloc_spin, sys_gcalloc_spin_len, "_gc_", LANG_SPIN_SPIN1); spinyyparse(); if (gl_output == OUTPUT_BYTECODE) { - strToLex(systemModule->Lptr, (const char *)sys_gc_bytecode_spin, "_platform_", LANG_SPIN_SPIN1); + strToLex(systemModule->Lptr, (const char *)sys_gc_bytecode_spin, sys_gc_bytecode_spin_len, "_platform_", LANG_SPIN_SPIN1); spinyyparse(); } else { - strToLex(systemModule->Lptr, (const char *)sys_gc_pasm_spin, "_platform_", LANG_SPIN_SPIN1); + strToLex(systemModule->Lptr, (const char *)sys_gc_pasm_spin, sys_gc_pasm_spin_len, "_platform_", LANG_SPIN_SPIN1); spinyyparse(); } ProcessModule(systemModule); @@ -761,7 +766,7 @@ doParseFile(const char *name, Module *P, int *is_dup, AST *paramlist) parseString = pp_finish(&gl_pp); pp_restore_define_state(&gl_pp, defineState); } - strToLex(NULL, parseString, fname, language); + strToLex(NULL, parseString, strlen(parseString), fname, language); doparse(language); free(parseString); } else { diff --git a/sys/bytecode_rom.spin.h b/sys/bytecode_rom.spin.h index 592ff3899..981da3202 100644 --- a/sys/bytecode_rom.spin.h +++ b/sys/bytecode_rom.spin.h @@ -1319,6 +1319,6 @@ unsigned char sys_bytecode_rom_spin[] = { 0x20, 0x28, 0x61, 0x68, 0x69, 0x20, 0x7e, 0x3e, 0x20, 0x33, 0x31, 0x29, 0x0a, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x61, 0x6c, 0x6f, 0x29, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x72, 0x20, 0x7c, 0x3d, 0x20, 0x31, 0x0a, 0x0a, - 0x20, 0x20, 0x0a, 0x00 + 0x20, 0x20, 0x0a }; unsigned int sys_bytecode_rom_spin_len = 15843; diff --git a/sys/common.spin.h b/sys/common.spin.h index c299e97ec..873cff2c7 100644 --- a/sys/common.spin.h +++ b/sys/common.spin.h @@ -1393,6 +1393,6 @@ unsigned char sys_common_spin[] = { 0x20, 0x76, 0x61, 0x6c, 0x3e, 0x3e, 0x32, 0x34, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x65, 0x6c, 0x73, 0x65, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x5b, 0x70, 0x5d, 0x20, 0x3a, 0x3d, 0x20, 0x76, - 0x61, 0x6c, 0x0a, 0x00 + 0x61, 0x6c, 0x0a }; unsigned int sys_common_spin_len = 16731; diff --git a/sys/common_pasm.spin.h b/sys/common_pasm.spin.h index 0ada3f70a..f12f11181 100644 --- a/sys/common_pasm.spin.h +++ b/sys/common_pasm.spin.h @@ -172,6 +172,6 @@ unsigned char sys_common_pasm_spin[] = { 0x5f, 0x75, 0x6e, 0x6c, 0x6f, 0x63, 0x6b, 0x6d, 0x65, 0x6d, 0x28, 0x61, 0x64, 0x64, 0x72, 0x29, 0x20, 0x7c, 0x20, 0x6f, 0x6c, 0x64, 0x6c, 0x6f, 0x63, 0x6b, 0x0a, 0x20, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x5b, 0x61, 0x64, - 0x64, 0x72, 0x5d, 0x20, 0x3a, 0x3d, 0x20, 0x30, 0x0a, 0x20, 0x0a, 0x00 + 0x64, 0x72, 0x5d, 0x20, 0x3a, 0x3d, 0x20, 0x30, 0x0a, 0x20, 0x0a }; unsigned int sys_common_pasm_spin_len = 2087; diff --git a/sys/float.spin.h b/sys/float.spin.h index 3c0e8c43c..6a21ca0df 100644 --- a/sys/float.spin.h +++ b/sys/float.spin.h @@ -1103,6 +1103,6 @@ unsigned char sys_float_spin[] = { 0x77, 0x5f, 0x6e, 0x28, 0x62, 0x3d, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x2c, 0x20, 0x61, 0x3d, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x2c, 0x20, 0x6e, 0x3d, 0x6c, 0x6f, 0x6e, 0x67, 0x29, 0x20, 0x3a, 0x20, 0x72, 0x3d, 0x66, 0x6c, - 0x6f, 0x61, 0x74, 0x0a, 0x00 + 0x6f, 0x61, 0x74, 0x0a }; unsigned int sys_float_spin_len = 13252; diff --git a/sys/gc_bytecode.spin.h b/sys/gc_bytecode.spin.h index f41fe50c5..3e04b8bef 100644 --- a/sys/gc_bytecode.spin.h +++ b/sys/gc_bytecode.spin.h @@ -46,6 +46,6 @@ unsigned char sys_gc_bytecode_spin[] = { 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x20, 0x43, 0x4f, 0x47, 0x20, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x20, 0x75, 0x73, 0x65, 0x64, 0x0a, 0x70, 0x72, 0x69, 0x20, 0x5f, 0x67, 0x63, 0x5f, 0x6d, 0x61, 0x72, 0x6b, 0x63, 0x6f, - 0x67, 0x0a, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x0a, 0x00 + 0x67, 0x0a, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x0a }; unsigned int sys_gc_bytecode_spin_len = 575; diff --git a/sys/gc_pasm.spin.h b/sys/gc_pasm.spin.h index 1cb8321f5..d6bac687a 100644 --- a/sys/gc_pasm.spin.h +++ b/sys/gc_pasm.spin.h @@ -62,6 +62,6 @@ unsigned char sys_gc_pasm_spin[] = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x77, 0x6f, 0x72, 0x64, 0x5b, 0x70, 0x74, 0x72, 0x20, 0x2b, 0x20, 0x4f, 0x46, 0x46, 0x5f, 0x46, 0x4c, 0x41, 0x47, 0x53, 0x5d, 0x20, 0x7c, 0x3d, 0x20, 0x47, 0x43, 0x5f, - 0x46, 0x4c, 0x41, 0x47, 0x5f, 0x49, 0x4e, 0x55, 0x53, 0x45, 0x0a, 0x00 + 0x46, 0x4c, 0x41, 0x47, 0x5f, 0x49, 0x4e, 0x55, 0x53, 0x45, 0x0a }; unsigned int sys_gc_pasm_spin_len = 767; diff --git a/sys/gcalloc.spin.h b/sys/gcalloc.spin.h index 7ebcad8e4..6dfd1466d 100644 --- a/sys/gcalloc.spin.h +++ b/sys/gcalloc.spin.h @@ -974,6 +974,6 @@ unsigned char sys_gcalloc_spin[] = { 0x6e, 0x65, 0x78, 0x74, 0x70, 0x74, 0x72, 0x20, 0x3c, 0x3e, 0x20, 0x30, 0x29, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x28, 0x6e, 0x65, 0x78, 0x74, 0x70, 0x74, 0x72, 0x20, 0x3c, 0x20, 0x65, 0x6e, 0x64, 0x68, 0x65, 0x61, 0x70, - 0x29, 0x0a, 0x20, 0x20, 0x0a, 0x00 + 0x29, 0x0a, 0x20, 0x20, 0x0a }; unsigned int sys_gcalloc_spin_len = 11705; diff --git a/sys/lmm_cache.spin.h b/sys/lmm_cache.spin.h index 8ca2ef538..08e207a1f 100644 --- a/sys/lmm_cache.spin.h +++ b/sys/lmm_cache.spin.h @@ -396,6 +396,6 @@ unsigned char sys_lmm_cache_spin[] = { 0x53, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x34, 0x2a, 0x28, 0x4c, 0x4d, 0x4d, 0x5f, 0x46, 0x43, 0x41, 0x43, 0x48, 0x45, 0x5f, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x20, 0x4c, 0x4d, 0x4d, 0x5f, 0x46, 0x43, 0x41, 0x43, - 0x48, 0x45, 0x5f, 0x53, 0x54, 0x41, 0x52, 0x54, 0x29, 0x0a, 0x09, 0x0a, 0x00 + 0x48, 0x45, 0x5f, 0x53, 0x54, 0x41, 0x52, 0x54, 0x29, 0x0a, 0x09, 0x0a }; unsigned int sys_lmm_cache_spin_len = 4776; diff --git a/sys/lmm_compress.spin.h b/sys/lmm_compress.spin.h index 61eacdb1d..78c2c0d08 100644 --- a/sys/lmm_compress.spin.h +++ b/sys/lmm_compress.spin.h @@ -377,6 +377,6 @@ unsigned char sys_lmm_compress_spin[] = { 0x5f, 0x72, 0x65, 0x74, 0x5f, 0x74, 0x6f, 0x5f, 0x63, 0x6f, 0x67, 0x0a, 0x4c, 0x4d, 0x4d, 0x5f, 0x4e, 0x45, 0x57, 0x5f, 0x50, 0x43, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x0a, 0x73, 0x61, 0x76, 0x65, 0x5f, - 0x63, 0x7a, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x0a, 0x0a, 0x00 + 0x63, 0x7a, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x0a, 0x0a }; unsigned int sys_lmm_compress_spin_len = 4548; diff --git a/sys/lmm_orig.spin.h b/sys/lmm_orig.spin.h index c771c9240..a1c889cc9 100644 --- a/sys/lmm_orig.spin.h +++ b/sys/lmm_orig.spin.h @@ -160,6 +160,6 @@ unsigned char sys_lmm_orig_spin[] = { 0x6e, 0x67, 0x20, 0x30, 0x0a, 0x41, 0x44, 0x44, 0x52, 0x5f, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x30, 0x0a, 0x46, 0x43, 0x4f, 0x55, 0x4e, 0x54, 0x5f, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x6f, - 0x6e, 0x67, 0x20, 0x30, 0x0a, 0x00 + 0x6e, 0x67, 0x20, 0x30, 0x0a }; unsigned int sys_lmm_orig_spin_len = 1937; diff --git a/sys/lmm_slow.spin.h b/sys/lmm_slow.spin.h index 0d9ec6f5c..0dd6dbbb6 100644 --- a/sys/lmm_slow.spin.h +++ b/sys/lmm_slow.spin.h @@ -50,6 +50,6 @@ unsigned char sys_lmm_slow_spin[] = { 0x67, 0x20, 0x40, 0x40, 0x40, 0x68, 0x75, 0x62, 0x5f, 0x72, 0x65, 0x74, 0x5f, 0x74, 0x6f, 0x5f, 0x63, 0x6f, 0x67, 0x0a, 0x4c, 0x4d, 0x4d, 0x5f, 0x4e, 0x45, 0x57, 0x5f, 0x50, 0x43, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x6c, - 0x6f, 0x6e, 0x67, 0x20, 0x20, 0x20, 0x30, 0x0a, 0x00 + 0x6f, 0x6e, 0x67, 0x20, 0x20, 0x20, 0x30, 0x0a }; unsigned int sys_lmm_slow_spin_len = 620; diff --git a/sys/lmm_trace.spin.h b/sys/lmm_trace.spin.h index 6f934570e..4326c53bb 100644 --- a/sys/lmm_trace.spin.h +++ b/sys/lmm_trace.spin.h @@ -530,6 +530,6 @@ unsigned char sys_lmm_trace_spin[] = { 0x73, 0x74, 0x70, 0x63, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x0a, 0x6c, 0x70, 0x63, 0x6e, 0x74, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x0a, 0x6c, 0x6d, 0x6d, 0x5f, 0x66, 0x6c, 0x61, 0x67, 0x73, - 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x0a, 0x00 + 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x0a }; unsigned int sys_lmm_trace_spin_len = 6381; diff --git a/sys/nucode_util.spin.h b/sys/nucode_util.spin.h index d7612548c..86089abf3 100644 --- a/sys/nucode_util.spin.h +++ b/sys/nucode_util.spin.h @@ -895,6 +895,6 @@ unsigned char sys_nucode_util_spin[] = { 0x78, 0x2c, 0x20, 0x70, 0x6f, 0x6c, 0x79, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x64, 0x6a, 0x6e, 0x7a, 0x20, 0x20, 0x20, 0x63, 0x6e, 0x74, 0x2c, 0x20, 0x23, 0x2e, 0x6c, 0x6f, 0x6f, 0x70, 0x0a, 0x20, 0x20, 0x65, 0x6e, 0x64, - 0x0a, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x78, 0x0a, 0x00 + 0x0a, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x78, 0x0a }; unsigned int sys_nucode_util_spin_len = 10764; diff --git a/sys/nuinterp.spin.h b/sys/nuinterp.spin.h index eacdb2521..1e4e022d3 100644 --- a/sys/nuinterp.spin.h +++ b/sys/nuinterp.spin.h @@ -1820,6 +1820,6 @@ unsigned char sys_nuinterp_spin[] = { 0x69, 0x7a, 0x65, 0x0a, 0x0a, 0x0a, 0x27, 0x20, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x20, 0x73, 0x70, 0x61, 0x63, 0x65, 0x0a, 0x01, 0x35, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x09, 0x30, 0x09, 0x27, 0x20, 0x73, 0x74, 0x61, 0x63, - 0x6b, 0x0a, 0x7d, 0x0a, 0x0a, 0x01, 0x33, 0x0a, 0x00 + 0x6b, 0x0a, 0x7d, 0x0a, 0x0a, 0x01, 0x33, 0x0a }; unsigned int sys_nuinterp_spin_len = 21860; diff --git a/sys/p1_code.spin.h b/sys/p1_code.spin.h index 471ce2d05..9bb072212 100644 --- a/sys/p1_code.spin.h +++ b/sys/p1_code.spin.h @@ -937,6 +937,6 @@ unsigned char sys_p1_code_spin[] = { 0x66, 0x5f, 0x7a, 0x20, 0x20, 0x6d, 0x6f, 0x76, 0x20, 0x20, 0x72, 0x2c, 0x20, 0x23, 0x30, 0x0a, 0x69, 0x66, 0x5f, 0x6e, 0x7a, 0x20, 0x6e, 0x65, 0x67, 0x63, 0x20, 0x72, 0x2c, 0x20, 0x23, 0x31, 0x0a, 0x20, 0x20, 0x65, - 0x6e, 0x64, 0x61, 0x73, 0x6d, 0x0a, 0x0a, 0x00 + 0x6e, 0x64, 0x61, 0x73, 0x6d, 0x0a, 0x0a }; unsigned int sys_p1_code_spin_len = 11263; diff --git a/sys/p2_brkdebug.spin.h b/sys/p2_brkdebug.spin.h index 90ab3f586..b0fd09725 100644 --- a/sys/p2_brkdebug.spin.h +++ b/sys/p2_brkdebug.spin.h @@ -3901,6 +3901,6 @@ unsigned char sys_p2_brkdebug_spin[] = { 0x65, 0x2c, 0x20, 0x65, 0x61, 0x63, 0x68, 0x20, 0x61, 0x72, 0x67, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x63, 0x61, 0x6e, 0x20, 0x62, 0x65, 0x20, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x20, 0x6f, 0x72, 0x20, - 0x23, 0x69, 0x6d, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x00 + 0x23, 0x69, 0x6d, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x74, 0x65 }; unsigned int sys_p2_brkdebug_spin_len = 46834; diff --git a/sys/p2_code.spin.h b/sys/p2_code.spin.h index 047e81012..b1d9b7b8e 100644 --- a/sys/p2_code.spin.h +++ b/sys/p2_code.spin.h @@ -1065,6 +1065,6 @@ unsigned char sys_p2_code_spin[] = { 0x7a, 0x0a, 0x69, 0x66, 0x5f, 0x7a, 0x20, 0x20, 0x6d, 0x6f, 0x76, 0x20, 0x20, 0x72, 0x2c, 0x20, 0x23, 0x30, 0x0a, 0x69, 0x66, 0x5f, 0x6e, 0x7a, 0x20, 0x6e, 0x65, 0x67, 0x63, 0x20, 0x72, 0x2c, 0x20, 0x23, 0x31, 0x0a, - 0x20, 0x20, 0x65, 0x6e, 0x64, 0x61, 0x73, 0x6d, 0x0a, 0x0a, 0x00 + 0x20, 0x20, 0x65, 0x6e, 0x64, 0x61, 0x73, 0x6d, 0x0a, 0x0a }; unsigned int sys_p2_code_spin_len = 12802; diff --git a/testlex.c b/testlex.c index 0b439f316..d79bba23a 100644 --- a/testlex.c +++ b/testlex.c @@ -88,7 +88,7 @@ testNumber(const char *str, uint32_t val) Token t; int c; printf("testing number[%s]...", str); - strToLex(&L, str, NULL, LANG_DEFAULT); + strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT); t = getSpinToken(&L, &ast); EXPECTEQ(t, SP_NUM); c = lexgetc(&L); @@ -115,7 +115,7 @@ testFloat(const char *str, float fval) val = v.i; printf("testing number[%s]...", str); - strToLex(&L, str, NULL, LANG_DEFAULT); + strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT); t = getSpinToken(&L, &ast); EXPECTEQ(t, SP_FLOATNUM); c = lexgetc(&L); @@ -133,7 +133,7 @@ testIdentifier(const char *str, const char *expect) AST *ast; Token t; - strToLex(&L, str, NULL, LANG_DEFAULT); + strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT); t = getSpinToken(&L, &ast); EXPECTEQ(t, SP_IDENTIFIER); assert(ast != NULL); @@ -151,7 +151,7 @@ testTokenStream(const char *str, int *tokens, int numtokens) Token t; printf("testing tokens [%s]...", str); fflush(stdout); - strToLex(&L, str, NULL, LANG_DEFAULT); + strToLex(&L, str, strlen(str), NULL, LANG_DEFAULT); for (i = 0; i < numtokens; i++) { t = getSpinToken(&L, &ast); EXPECTEQ(t, tokens[i]); @@ -168,7 +168,7 @@ testTokenStream2(const char *str, int *tokens, int numtokens) Token t; printf("testing tokens [%s]...", str); fflush(stdout); - strToLex(&L, str, NULL, LANG_SPIN_SPIN2); + strToLex(&L, str, strlen(str), NULL, LANG_SPIN_SPIN2); for (i = 0; i < numtokens; i++) { t = getSpinToken(&L, &ast); EXPECTEQ(t, tokens[i]); diff --git a/util/strdupcat.c b/util/strdupcat.c index 2009748aa..d66898146 100644 --- a/util/strdupcat.c +++ b/util/strdupcat.c @@ -10,3 +10,10 @@ char *strdupcat(const char *a, const char *b) strcat(c, b); return c; } + +char *strndup(const char *src, size_t n) +{ + char *r = (char *)calloc(1, n+1); + strncpy(r, src, n); + return r; +} diff --git a/util/util.h b/util/util.h index 0748ea523..1715862f8 100644 --- a/util/util.h +++ b/util/util.h @@ -69,6 +69,14 @@ char *strrev(char *origstr); // create a new string with the concatenation of two old ones char *strdupcat(const char *a, const char *b); +#ifdef strndup +#undef strndup +#endif +// duplicate a part of a string +// this is standard in POSIX, but not elsewhere, so we provide our own +// definition +extern char *strndup(const char *, size_t); + // convert wide character wcorig to utf-8 in s, returns count size_t to_utf8(char *s, wchar_t wcorig);