diff options
author | H. Peter Anvin (Intel) <hpa@zytor.com> | 2019-04-25 18:00:32 -0700 |
---|---|---|
committer | H. Peter Anvin (Intel) <hpa@zytor.com> | 2019-04-25 18:00:32 -0700 |
commit | 41e9682efed7cd1df133b1b4ac806e07723f1486 (patch) | |
tree | 001a3ef33fa108a5aac02318526f401e9c4d0e71 /asm/quote.c | |
parent | 61891333636f2bda70f28c7cc5324f808412de58 (diff) | |
download | nasm-41e9682efed7cd1df133b1b4ac806e07723f1486.tar.gz |
preproc: massive cleanup of smacro expansion
The smacro expansion code was virtually impossible to understand, and
was leading to very strange failures. Clean it up, and do much better
handling of magic macros. This should also allow for recursive
macros, but recursive macros are extremely tricky in that it is very
hard to keep them from recursing forever, unless there is at least one
argument which is never expanded. They are not currently implemented.
Even so, I believe token pasting makes it possible to create infinite
loops; e.g.:
%define foo foo %+
Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Diffstat (limited to 'asm/quote.c')
-rw-r--r-- | asm/quote.c | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/asm/quote.c b/asm/quote.c index 962ae9fd..efd5cd6c 100644 --- a/asm/quote.c +++ b/asm/quote.c @@ -41,13 +41,19 @@ #include "nasmlib.h" #include "quote.h" -char *nasm_quote(const char *str, size_t len) +/* + * Create a NASM quoted string in newly allocated memory. Update the + * *lenp parameter with the output length (sans final NUL). + */ + +char *nasm_quote(const char *str, size_t *lenp) { const char *p, *ep; char c, c1, *q, *nstr; unsigned char uc; bool sq_ok, dq_ok; size_t qlen; + size_t len = *lenp; sq_ok = dq_ok = true; ep = str+len; @@ -105,7 +111,7 @@ char *nasm_quote(const char *str, size_t len) /* Use '...' or "..." */ nstr = nasm_malloc(len+3); nstr[0] = nstr[len+1] = sq_ok ? '\'' : '\"'; - nstr[len+2] = '\0'; + q = &nstr[len+2]; if (len > 0) memcpy(nstr+1, str, len); } else { @@ -174,9 +180,10 @@ char *nasm_quote(const char *str, size_t len) } } *q++ = '`'; - *q++ = '\0'; - nasm_assert((size_t)(q-nstr) == qlen+3); + nasm_assert((size_t)(q-nstr) == qlen+2); } + *q = '\0'; + *lenp = q - nstr; return nstr; } @@ -216,11 +223,16 @@ static char *emit_utf8(char *q, int32_t v) } /* - * Quote a C string + * Same as nasm_quote, but take the length of a C string; + * the lenp argument is optional. */ -char *nasm_quote_cstr(const char *str) +char *nasm_quote_cstr(const char *str, size_t *lenp) { - return nasm_quote(str, strlen(str)); + size_t len = strlen(str); + char *qstr = nasm_quote(str, &len); + if (lenp) + *lenp = len; + return qstr; } /* |