Skip to content

Commit 359e21e

Browse files
author
H. Peter Anvin
committed
preproc: implement %strlen as a preprocessor function
Implement a %strlen() preprocessor function, equivalent to the %strlen directive. Signed-off-by: H. Peter Anvin <[email protected]>
1 parent 4150848 commit 359e21e

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

asm/preproc.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6907,18 +6907,52 @@ stdmac_substr(const SMacro *s, Token **params, int nparams)
69076907
return pp_substr(expand_smacro_noreset(params[0]), s->name);
69086908
}
69096909

6910+
/* Expand a the argument and enforce it being a single quoted string */
6911+
static Token *expand_to_string(Token *tlist, const char *dname)
6912+
{
6913+
Token *t = zap_white(expand_smacro_noreset(tlist));
6914+
6915+
if (!tok_is(t, TOKEN_STR)) {
6916+
nasm_nonfatal("`%s' requires string as parameter", dname);
6917+
return NULL;
6918+
}
6919+
6920+
t->next = zap_white(t->next);
6921+
if (t->next) {
6922+
nasm_nonfatal("`%s' requires exactly one string as parameter", dname);
6923+
return NULL;
6924+
}
6925+
6926+
return t;
6927+
}
6928+
6929+
/* %strlen() function */
6930+
static Token *
6931+
stdmac_strlen(const SMacro *s, Token **params, int nparams)
6932+
{
6933+
Token *t;
6934+
6935+
(void)nparams;
6936+
6937+
t = expand_to_string(params[0], s->name);
6938+
if (!t)
6939+
return NULL;
6940+
6941+
unquote_token(t);
6942+
return make_tok_num(NULL, t->len);
6943+
}
6944+
69106945
/* %tok() function */
69116946
static Token *
69126947
stdmac_tok(const SMacro *s, Token **params, int nparams)
69136948
{
6914-
Token *t = expand_smacro_noreset(params[0]);
6949+
Token *t;
69156950

69166951
(void)nparams;
69176952

6918-
if (!tok_is(t, TOKEN_STR)) {
6919-
nasm_nonfatal("`%s' requires string as parameter", s->name);
6953+
t = expand_to_string(params[0], s->name);
6954+
if (!t)
69206955
return NULL;
6921-
}
69226956

69236957
return reverse_tokens(tokenize(unquote_token_cstr(t)));
69246958
}
@@ -6941,6 +6975,7 @@ static void pp_add_magic_stdmac(void)
69416975
{ "%eval", false, 1, SPARM_EVAL|SPARM_VARADIC, stdmac_join },
69426976
{ "%str", false, 1, SPARM_GREEDY|SPARM_STR, stdmac_join },
69436977
{ "%strcat", false, 1, SPARM_GREEDY, stdmac_strcat },
6978+
{ "%strlen", false, 1, 0, stdmac_strlen },
69446979
{ "%substr", false, 1, SPARM_GREEDY, stdmac_substr },
69456980
{ "%tok", false, 1, 0, stdmac_tok },
69466981
{ NULL, false, 0, 0, NULL }

0 commit comments

Comments
 (0)