From: Christian Ludwig Subject: btrace: Multi-line strings To: Cc: Martin Pieuchot Date: Tue, 5 Nov 2024 14:03:58 +0100 Allow multi-line strings in a bt(5) script. --- regress/usr.sbin/btrace/printf.bt | 5 ++++ regress/usr.sbin/btrace/printf.ok | 1 + usr.sbin/btrace/bt_parse.y | 38 +++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/regress/usr.sbin/btrace/printf.bt b/regress/usr.sbin/btrace/printf.bt index f025d0bbc27a..f80f9209de59 100644 --- a/regress/usr.sbin/btrace/printf.bt +++ b/regress/usr.sbin/btrace/printf.bt @@ -1,4 +1,9 @@ BEGIN { $c = 0x41; // 'A' printf("%c%c%c\n", $c, 0x41, 65); + + $t = "two " // multi-line string + "times" /* with comments */ ""; + printf("%s multi-" + "line\n", $t); } diff --git a/regress/usr.sbin/btrace/printf.ok b/regress/usr.sbin/btrace/printf.ok index 43d5a8ed6ef6..663f16707e02 100644 --- a/regress/usr.sbin/btrace/printf.ok +++ b/regress/usr.sbin/btrace/printf.ok @@ -1 +1,2 @@ AAA +two times multi-line diff --git a/usr.sbin/btrace/bt_parse.y b/usr.sbin/btrace/bt_parse.y index 2497ae4eaddd..df3379d740a2 100644 --- a/usr.sbin/btrace/bt_parse.y +++ b/usr.sbin/btrace/bt_parse.y @@ -793,15 +793,10 @@ allowed_in_string(int x) return (isalnum(x) || x == '_'); } -int -yylex(void) +static int +skip(void) { - unsigned char buf[1024]; - unsigned char *ebuf, *p, *str; - int c; - - ebuf = buf + sizeof(buf); - p = buf; + int c; again: /* skip whitespaces */ @@ -838,6 +833,22 @@ again: } } + return c; +} + +int +yylex(void) +{ + unsigned char buf[1024]; + unsigned char *ebuf, *p, *str; + int c; + + ebuf = buf + sizeof(buf); + p = buf; + +again: + c = skip(); + switch (c) { case '!': case '=': @@ -962,7 +973,16 @@ again: return 0; case '"': /* parse C-like string */ - while ((c = lgetc()) != EOF && c != '"') { + while ((c = lgetc()) != EOF) { + if (c == '"') { + /* handle multi-line strings */ + c = skip(); + if (c == '"') + continue; + else + lungetc(); + break; + } if (c == '\\') { c = lgetc(); switch (c) { -- 2.34.1