| 1 |
|
| /* |
| 2 |
|
| * eshu_bash.h — Bash/shell indentation scanner |
| 3 |
|
| * |
| 4 |
|
| * Handles keyword-pair blocks (if/fi, for/done, while/done, until/done, |
| 5 |
|
| * case/esac, select/done, function/}), brace grouping { }, and heredocs. |
| 6 |
|
| * Strings, comments, and $(...) subshells are tracked to avoid misidentifying |
| 7 |
|
| * keywords inside quoted contexts. |
| 8 |
|
| */ |
| 9 |
|
| |
| 10 |
|
| #ifndef ESHU_BASH_H |
| 11 |
|
| #define ESHU_BASH_H |
| 12 |
|
| |
| 13 |
|
| #include "eshu.h" |
| 14 |
|
| |
| 15 |
|
| #define ESHU_BASH_HEREDOC_MAX 64 |
| 16 |
|
| #define ESHU_BASH_DEPTH_MAX 64 |
| 17 |
|
| |
| 18 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 19 |
|
| * Scanner context — persists across lines |
| 20 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 21 |
|
| |
| 22 |
|
| typedef struct { |
| 23 |
|
| int depth; |
| 24 |
|
| int brace_depth; /* { } grouping nesting */ |
| 25 |
|
| int subshell_depth; /* $( ) nesting count */ |
| 26 |
|
| int arith_depth; /* $(( )) arithmetic nesting */ |
| 27 |
|
| int bracket_depth; /* [[ ]] or [ ] nesting */ |
| 28 |
|
| /* case state */ |
| 29 |
|
| int in_case; /* 1 = inside case...esac */ |
| 30 |
|
| int case_depth; /* depth at the 'case' keyword */ |
| 31 |
|
| int case_pat_open; /* 1 = just opened a pattern body */ |
| 32 |
|
| /* heredoc state */ |
| 33 |
|
| char heredoc_tag[ESHU_BASH_HEREDOC_MAX]; |
| 34 |
|
| int heredoc_len; |
| 35 |
|
| int heredoc_strip; /* 1 = <<-, strip leading tabs */ |
| 36 |
|
| int heredoc_pending; /* 1 = body starts next line */ |
| 37 |
|
| /* scanner state */ |
| 38 |
|
| enum eshu_state state; |
| 39 |
|
| eshu_config_t cfg; |
| 40 |
|
| } eshu_bash_ctx_t; |
| 41 |
|
| |
| 42 |
78 |
| static void eshu_bash_ctx_init(eshu_bash_ctx_t *ctx, const eshu_config_t *cfg) { |
| 43 |
78 |
| ctx->depth = 0; |
| 44 |
78 |
| ctx->brace_depth = 0; |
| 45 |
78 |
| ctx->subshell_depth = 0; |
| 46 |
78 |
| ctx->arith_depth = 0; |
| 47 |
78 |
| ctx->bracket_depth = 0; |
| 48 |
78 |
| ctx->in_case = 0; |
| 49 |
78 |
| ctx->case_depth = 0; |
| 50 |
78 |
| ctx->case_pat_open = 0; |
| 51 |
78 |
| ctx->heredoc_tag[0] = '\0'; |
| 52 |
78 |
| ctx->heredoc_len = 0; |
| 53 |
78 |
| ctx->heredoc_strip = 0; |
| 54 |
78 |
| ctx->heredoc_pending = 0; |
| 55 |
78 |
| ctx->state = ESHU_CODE; |
| 56 |
78 |
| ctx->cfg = *cfg; |
| 57 |
78 |
| } |
| 58 |
|
| |
| 59 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 60 |
|
| * Helpers |
| 61 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 62 |
|
| |
| 63 |
32 |
| static int eshu_bash_isword(char c) { |
| 64 |
32 |
▶ | return isalnum((unsigned char)c) || c == '_'; |
Branch: 2 branches
| Branch | True | False | Total |
| return isalnum((unsigned char)c) || c == '_'; |
21 |
11 |
32 |
| return isalnum((unsigned char)c) || c == '_'; |
3 |
18 |
21 |
|
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
|
| /* Match a keyword token at p: keyword must end on a word boundary |
| 68 |
|
| * (followed by whitespace, EOL, ';', '(', ')', '{', '}', '#', or NUL). */ |
| 69 |
9719 |
| static int eshu_bash_kw_match(const char *p, const char *end, |
| 70 |
|
| const char *kw, size_t klen) { |
| 71 |
9719 |
▶ | if ((size_t)(end - p) < klen) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)(end - p) < klen) return 0; |
2049 |
7670 |
9719 |
|
| 72 |
7670 |
▶ | if (memcmp(p, kw, klen) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(p, kw, klen) != 0) return 0; |
7503 |
167 |
7670 |
|
| 73 |
167 |
▶ | if (p + klen < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + klen < end) { |
32 |
135 |
167 |
|
| 74 |
32 |
| char nc = p[klen]; |
| 75 |
32 |
▶ | if (eshu_bash_isword(nc)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_isword(nc)) return 0; |
14 |
18 |
32 |
|
| 76 |
|
| } |
| 77 |
153 |
| return 1; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| /* Return 1 if p is at a word boundary start (preceded by whitespace, BOL, |
| 81 |
|
| * ';', '|', '&', '(', '`', or NUL). Used to guard keyword detection. */ |
| 82 |
4466 |
| static int eshu_bash_at_word_start(const char *src, const char *p) { |
| 83 |
4466 |
▶ | if (p == src) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == src) return 1; |
501 |
3965 |
4466 |
|
| 84 |
3965 |
| char prev = *(p - 1); |
| 85 |
3458 |
▶ | return prev == ' ' || prev == '\t' || prev == ';' || |
Branch: 3 branches
| Branch | True | False | Total |
| return prev == ' ' || prev == '\t' || prev == ';' || |
3458 |
0 |
3458 |
| return prev == ' ' || prev == '\t' || prev == ';' || |
3390 |
68 |
3458 |
| return prev == ' ' || prev == '\t' || prev == ';' || |
3354 |
36 |
3390 |
|
| 86 |
3354 |
▶ | prev == '|' || prev == '&' || prev == '(' || |
Branch: 3 branches
| Branch | True | False | Total |
| prev == '|' || prev == '&' || prev == '(' || |
3335 |
19 |
3354 |
| prev == '|' || prev == '&' || prev == '(' || |
3266 |
69 |
3335 |
| prev == '|' || prev == '&' || prev == '(' || |
3266 |
0 |
3266 |
|
| 87 |
7423 |
▶ | prev == '`' || prev == '\n' || prev == '{'; |
Branch: 3 branches
| Branch | True | False | Total |
| prev == '`' || prev == '\n' || prev == '{'; |
3458 |
507 |
3965 |
| prev == '`' || prev == '\n' || prev == '{'; |
3266 |
0 |
3266 |
| prev == '`' || prev == '\n' || prev == '{'; |
7 |
3259 |
3266 |
|
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
|
| /* Scan past the end of a $'...' ANSI-C quoted string. |
| 91 |
|
| * p points just past the opening '. Returns pointer past closing '. */ |
| 92 |
0 |
| static const char *eshu_bash_skip_ansi(const char *p, const char *end) { |
| 93 |
0 |
▶ | while (p < end && *p != '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { |
0 |
0 |
0 |
| while (p < end && *p != '\'') { |
0 |
0 |
0 |
|
| 94 |
0 |
▶ | if (*p == '\\') { p++; if (p < end) p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
0 |
0 |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
0 |
0 |
|
| 95 |
0 |
| else p++; |
| 96 |
|
| } |
| 97 |
0 |
▶ | if (p < end) p++; /* consume closing ' */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; /* consume closing ' */ |
0 |
0 |
0 |
|
| 98 |
0 |
| return p; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 102 |
|
| * Heredoc tag extraction helper |
| 103 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 104 |
|
| |
| 105 |
|
| /* Called when we've seen '<<' (and optionally '-'). p points to the |
| 106 |
|
| * character after the optional '-'. Extracts the tag into ctx, sets |
| 107 |
|
| * heredoc_pending = 1. Returns pointer past the tag (and closing quote |
| 108 |
|
| * if quoted). */ |
| 109 |
4 |
| static const char *eshu_bash_heredoc_start(eshu_bash_ctx_t *ctx, |
| 110 |
|
| const char *p, const char *end) { |
| 111 |
4 |
| char qchar = 0; |
| 112 |
|
| const char *tag_start; |
| 113 |
|
| int tl; |
| 114 |
|
| |
| 115 |
4 |
▶ | if (p < end && (*p == '\'' || *p == '"' || *p == '`')) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) { |
4 |
0 |
4 |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) { |
4 |
0 |
4 |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) { |
4 |
0 |
4 |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) { |
0 |
4 |
4 |
|
| 116 |
0 |
| qchar = *p++; |
| 117 |
|
| } |
| 118 |
4 |
| tag_start = p; |
| 119 |
23 |
▶ | while (p < end && *p != '\n' && *p != '\r' && |
Branch: 5 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n' && *p != '\r' && |
10 |
3 |
13 |
| while (p < end && *p != '\n' && *p != '\r' && |
10 |
0 |
10 |
| while (p < end && *p != '\n' && *p != '\r' && |
10 |
0 |
10 |
| while (p < end && *p != '\n' && *p != '\r' && |
0 |
10 |
10 |
| while (p < end && *p != '\n' && *p != '\r' && |
9 |
1 |
10 |
|
| 120 |
10 |
▶ | (qchar ? *p != qchar : (isalnum((unsigned char)*p) || *p == '_'))) |
Branch: 3 branches
| Branch | True | False | Total |
| (qchar ? *p != qchar : (isalnum((unsigned char)*p) || *p == '_'))) |
0 |
0 |
0 |
| (qchar ? *p != qchar : (isalnum((unsigned char)*p) || *p == '_'))) |
1 |
9 |
10 |
| (qchar ? *p != qchar : (isalnum((unsigned char)*p) || *p == '_'))) |
0 |
1 |
1 |
|
| 121 |
9 |
| p++; |
| 122 |
4 |
| tl = (int)(p - tag_start); |
| 123 |
4 |
▶ | if (tl > 0 && tl < ESHU_BASH_HEREDOC_MAX) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tl > 0 && tl < ESHU_BASH_HEREDOC_MAX) { |
3 |
1 |
4 |
| if (tl > 0 && tl < ESHU_BASH_HEREDOC_MAX) { |
3 |
0 |
3 |
|
| 124 |
3 |
| memcpy(ctx->heredoc_tag, tag_start, (size_t)tl); |
| 125 |
3 |
| ctx->heredoc_tag[tl] = '\0'; |
| 126 |
3 |
| ctx->heredoc_len = tl; |
| 127 |
3 |
| ctx->heredoc_pending = 1; |
| 128 |
|
| } |
| 129 |
4 |
▶ | if (qchar && p < end && *p == qchar) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| if (qchar && p < end && *p == qchar) p++; |
0 |
4 |
4 |
| if (qchar && p < end && *p == qchar) p++; |
0 |
0 |
0 |
| if (qchar && p < end && *p == qchar) p++; |
0 |
0 |
0 |
|
| 130 |
4 |
| return p; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 134 |
|
| * Per-line scanner: process one line of Bash, update ctx, emit |
| 135 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 136 |
|
| |
| 137 |
|
| /* |
| 138 |
|
| * We scan token-by-token through the line content (with leading whitespace |
| 139 |
|
| * already stripped) to: |
| 140 |
|
| * 1. Detect keywords that open (+1) or close (-1) depth. |
| 141 |
|
| * 2. Skip string/comment/subshell contexts so embedded keywords are ignored. |
| 142 |
|
| * |
| 143 |
|
| * We accumulate open/close deltas and apply them after line emission. |
| 144 |
|
| * |
| 145 |
|
| * Special logic: |
| 146 |
|
| * - Keywords that open a new block: 'then', 'do', 'function NAME {', |
| 147 |
|
| * standalone '{', and the implicit opener after 'repeat'. |
| 148 |
|
| * - Keywords that close a block: 'fi', 'done', 'esac', 'end' (not Bash, |
| 149 |
|
| * but guard anyway), and standalone '}'. |
| 150 |
|
| * - 'elif' and 'else': -1 before line, +1 after. |
| 151 |
|
| * - 'case EXPR in': +1 after. |
| 152 |
|
| * - 'esac': -1 before. |
| 153 |
|
| * - case pattern ')': track as pattern body opener (+1) inside case. |
| 154 |
|
| * - ';;'/';&'/';;&': case arm close, -1 then +0 (next arm will open). |
| 155 |
|
| */ |
| 156 |
|
| |
| 157 |
|
| /* Returned from line scanner: how many levels to adjust before/after emit */ |
| 158 |
|
| typedef struct { |
| 159 |
|
| int pre; /* apply to depth before emitting the line */ |
| 160 |
|
| int post; /* apply to depth after emitting the line */ |
| 161 |
|
| } eshu_bash_delta_t; |
| 162 |
|
| |
| 163 |
519 |
| static eshu_bash_delta_t eshu_bash_scan_line(eshu_bash_ctx_t *ctx, |
| 164 |
|
| const char *content, int len) |
| 165 |
|
| { |
| 166 |
519 |
| eshu_bash_delta_t d = {0, 0}; |
| 167 |
519 |
| const char *p = content; |
| 168 |
519 |
| const char *end = content + len; |
| 169 |
|
| |
| 170 |
|
| /* Skip shebang on first line — but ctx has no line counter, so we detect |
| 171 |
|
| * '#!' at position 0 with depth==0 as a no-op (comment branch covers it). */ |
| 172 |
|
| |
| 173 |
5779 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
5266 |
513 |
5779 |
|
| 174 |
5266 |
| char c = *p; |
| 175 |
|
| |
| 176 |
|
| /* single-quote string: '...' — no escapes except '' */ |
| 177 |
5266 |
▶ | if (c == '\'' && ctx->state == ESHU_CODE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '\'' && ctx->state == ESHU_CODE) { |
18 |
5248 |
5266 |
| if (c == '\'' && ctx->state == ESHU_CODE) { |
18 |
0 |
18 |
|
| 178 |
18 |
| p++; |
| 179 |
154 |
▶ | while (p < end && *p != '\'') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') p++; |
154 |
0 |
154 |
| while (p < end && *p != '\'') p++; |
136 |
18 |
154 |
|
| 180 |
18 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
18 |
0 |
18 |
|
| 181 |
18 |
| continue; |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
|
| /* $'...' ANSI-C string */ |
| 185 |
5248 |
▶ | if (c == '$' && p + 1 < end && *(p + 1) == '\'' && ctx->state == ESHU_CODE) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '$' && p + 1 < end && *(p + 1) == '\'' && ctx->state == ESHU_CODE) { |
30 |
5218 |
5248 |
| if (c == '$' && p + 1 < end && *(p + 1) == '\'' && ctx->state == ESHU_CODE) { |
30 |
0 |
30 |
| if (c == '$' && p + 1 < end && *(p + 1) == '\'' && ctx->state == ESHU_CODE) { |
0 |
30 |
30 |
| if (c == '$' && p + 1 < end && *(p + 1) == '\'' && ctx->state == ESHU_CODE) { |
0 |
0 |
0 |
|
| 186 |
0 |
| p += 2; |
| 187 |
0 |
| p = eshu_bash_skip_ansi(p, end); |
| 188 |
0 |
| continue; |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| /* double-quoted string: "..." — contains $var, $(cmd), ${var} */ |
| 192 |
5248 |
▶ | if (c == '"' && ctx->state == ESHU_CODE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '"' && ctx->state == ESHU_CODE) { |
242 |
5006 |
5248 |
| if (c == '"' && ctx->state == ESHU_CODE) { |
242 |
0 |
242 |
|
| 193 |
242 |
| p++; |
| 194 |
1967 |
▶ | while (p < end && *p != '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { |
1967 |
0 |
1967 |
| while (p < end && *p != '"') { |
1725 |
242 |
1967 |
|
| 195 |
1725 |
▶ | if (*p == '\\') { p++; if (p < end) p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; } |
3 |
1722 |
1725 |
| if (*p == '\\') { p++; if (p < end) p++; } |
3 |
0 |
3 |
|
| 196 |
1722 |
| else p++; |
| 197 |
|
| } |
| 198 |
242 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
242 |
0 |
242 |
|
| 199 |
242 |
| continue; |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
|
| /* backtick command substitution: `...` */ |
| 203 |
5006 |
▶ | if (c == '`' && ctx->state == ESHU_CODE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '`' && ctx->state == ESHU_CODE) { |
0 |
5006 |
5006 |
| if (c == '`' && ctx->state == ESHU_CODE) { |
0 |
0 |
0 |
|
| 204 |
0 |
| p++; |
| 205 |
0 |
▶ | while (p < end && *p != '`') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '`') { |
0 |
0 |
0 |
| while (p < end && *p != '`') { |
0 |
0 |
0 |
|
| 206 |
0 |
▶ | if (*p == '\\') { p++; if (p < end) p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
0 |
0 |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
0 |
0 |
|
| 207 |
0 |
| else p++; |
| 208 |
|
| } |
| 209 |
0 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
0 |
0 |
0 |
|
| 210 |
0 |
| continue; |
| 211 |
|
| } |
| 212 |
|
| |
| 213 |
|
| /* comment: # to EOL — only when at a word boundary (not escaped) */ |
| 214 |
5017 |
▶ | if (c == '#' && ctx->state == ESHU_CODE && |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '#' && ctx->state == ESHU_CODE && |
11 |
4995 |
5006 |
|
| 215 |
11 |
| eshu_bash_at_word_start(content, p)) { |
| 216 |
6 |
| break; |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
|
| /* heredoc start: << or <<- */ |
| 220 |
5000 |
▶ | if (c == '<' && p + 1 < end && *(p + 1) == '<' && ctx->state == ESHU_CODE) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '<' && p + 1 < end && *(p + 1) == '<' && ctx->state == ESHU_CODE) { |
18 |
4982 |
5000 |
| if (c == '<' && p + 1 < end && *(p + 1) == '<' && ctx->state == ESHU_CODE) { |
18 |
0 |
18 |
| if (c == '<' && p + 1 < end && *(p + 1) == '<' && ctx->state == ESHU_CODE) { |
4 |
14 |
18 |
| if (c == '<' && p + 1 < end && *(p + 1) == '<' && ctx->state == ESHU_CODE) { |
4 |
0 |
4 |
|
| 221 |
4 |
| p += 2; |
| 222 |
4 |
| ctx->heredoc_strip = 0; |
| 223 |
4 |
▶ | if (p < end && *p == '-') { ctx->heredoc_strip = 1; p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '-') { ctx->heredoc_strip = 1; p++; } |
4 |
0 |
4 |
| if (p < end && *p == '-') { ctx->heredoc_strip = 1; p++; } |
1 |
3 |
4 |
|
| 224 |
4 |
| p = eshu_bash_heredoc_start(ctx, p, end); |
| 225 |
4 |
| continue; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
|
| /* $(( )) arithmetic — track to avoid counting ) as closing */ |
| 229 |
4996 |
▶ | if (c == '$' && p + 1 < end && *(p + 1) == '(' && |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '$' && p + 1 < end && *(p + 1) == '(' && |
30 |
4966 |
4996 |
| if (c == '$' && p + 1 < end && *(p + 1) == '(' && |
30 |
0 |
30 |
| if (c == '$' && p + 1 < end && *(p + 1) == '(' && |
20 |
10 |
30 |
|
| 230 |
20 |
▶ | p + 2 < end && *(p + 2) == '(' && ctx->state == ESHU_CODE) { |
Branch: 3 branches
| Branch | True | False | Total |
| p + 2 < end && *(p + 2) == '(' && ctx->state == ESHU_CODE) { |
20 |
0 |
20 |
| p + 2 < end && *(p + 2) == '(' && ctx->state == ESHU_CODE) { |
17 |
3 |
20 |
| p + 2 < end && *(p + 2) == '(' && ctx->state == ESHU_CODE) { |
17 |
0 |
17 |
|
| 231 |
17 |
| ctx->arith_depth++; |
| 232 |
17 |
| p += 3; |
| 233 |
17 |
| continue; |
| 234 |
|
| } |
| 235 |
4979 |
▶ | if (c == ')' && p + 1 < end && *(p + 1) == ')' && ctx->arith_depth > 0) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == ')' && p + 1 < end && *(p + 1) == ')' && ctx->arith_depth > 0) { |
121 |
4858 |
4979 |
| if (c == ')' && p + 1 < end && *(p + 1) == ')' && ctx->arith_depth > 0) { |
89 |
32 |
121 |
| if (c == ')' && p + 1 < end && *(p + 1) == ')' && ctx->arith_depth > 0) { |
19 |
70 |
89 |
| if (c == ')' && p + 1 < end && *(p + 1) == ')' && ctx->arith_depth > 0) { |
17 |
2 |
19 |
|
| 236 |
17 |
| ctx->arith_depth--; |
| 237 |
17 |
| p += 2; |
| 238 |
17 |
| continue; |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
|
| /* $( ) subshell — track so ) doesn't look like case pattern closer */ |
| 242 |
4962 |
▶ | if (c == '$' && p + 1 < end && *(p + 1) == '(' && ctx->state == ESHU_CODE) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '$' && p + 1 < end && *(p + 1) == '(' && ctx->state == ESHU_CODE) { |
13 |
4949 |
4962 |
| if (c == '$' && p + 1 < end && *(p + 1) == '(' && ctx->state == ESHU_CODE) { |
13 |
0 |
13 |
| if (c == '$' && p + 1 < end && *(p + 1) == '(' && ctx->state == ESHU_CODE) { |
3 |
10 |
13 |
| if (c == '$' && p + 1 < end && *(p + 1) == '(' && ctx->state == ESHU_CODE) { |
3 |
0 |
3 |
|
| 243 |
3 |
| ctx->subshell_depth++; |
| 244 |
3 |
| p += 2; |
| 245 |
3 |
| continue; |
| 246 |
|
| } |
| 247 |
4959 |
▶ | if (c == ')' && ctx->subshell_depth > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == ')' && ctx->subshell_depth > 0) { |
104 |
4855 |
4959 |
| if (c == ')' && ctx->subshell_depth > 0) { |
3 |
101 |
104 |
|
| 248 |
3 |
| ctx->subshell_depth--; |
| 249 |
3 |
| p++; |
| 250 |
3 |
| continue; |
| 251 |
|
| } |
| 252 |
|
| |
| 253 |
|
| /* [[ ]] / [ ] arithmetic/test — track bracket depth */ |
| 254 |
4956 |
▶ | if (c == '[' && ctx->state == ESHU_CODE) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '[' && ctx->state == ESHU_CODE) { |
44 |
4912 |
4956 |
| if (c == '[' && ctx->state == ESHU_CODE) { |
44 |
0 |
44 |
|
| 255 |
44 |
| ctx->bracket_depth++; |
| 256 |
44 |
| p++; |
| 257 |
44 |
| continue; |
| 258 |
|
| } |
| 259 |
4912 |
▶ | if (c == ']' && ctx->bracket_depth > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == ']' && ctx->bracket_depth > 0) { |
40 |
4872 |
4912 |
| if (c == ']' && ctx->bracket_depth > 0) { |
40 |
0 |
40 |
|
| 260 |
40 |
| ctx->bracket_depth--; |
| 261 |
40 |
| p++; |
| 262 |
40 |
| continue; |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| /* ── Keyword detection: only at word-start positions ── */ |
| 266 |
4872 |
▶ | if (ctx->state == ESHU_CODE && ctx->bracket_depth == 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->state == ESHU_CODE && ctx->bracket_depth == 0 && |
4872 |
0 |
4872 |
| if (ctx->state == ESHU_CODE && ctx->bracket_depth == 0 && |
4623 |
249 |
4872 |
|
| 267 |
9078 |
▶ | ctx->subshell_depth == 0 && ctx->arith_depth == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->subshell_depth == 0 && ctx->arith_depth == 0 && |
4593 |
30 |
4623 |
|
| 268 |
4455 |
| eshu_bash_at_word_start(content, p)) { |
| 269 |
|
| |
| 270 |
|
| /* Keywords that pre-dedent: fi, done, esac, } */ |
| 271 |
1201 |
▶ | if (eshu_bash_kw_match(p, end, "fi", 2)) { d.pre--; p += 2; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "fi", 2)) { d.pre--; p += 2; continue; } |
27 |
1174 |
1201 |
|
| 272 |
1174 |
▶ | if (eshu_bash_kw_match(p, end, "done", 4)) { d.pre--; p += 4; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "done", 4)) { d.pre--; p += 4; continue; } |
28 |
1146 |
1174 |
|
| 273 |
1146 |
▶ | if (eshu_bash_kw_match(p, end, "esac", 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "esac", 4)) { |
9 |
1137 |
1146 |
|
| 274 |
|
| /* esac closes the case block and any open pattern body */ |
| 275 |
9 |
▶ | if (ctx->in_case && ctx->case_pat_open) { d.pre -= 2; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->in_case && ctx->case_pat_open) { d.pre -= 2; } |
9 |
0 |
9 |
| if (ctx->in_case && ctx->case_pat_open) { d.pre -= 2; } |
0 |
9 |
9 |
|
| 276 |
9 |
| else { d.pre--; } |
| 277 |
9 |
| ctx->in_case = 0; |
| 278 |
9 |
| ctx->case_pat_open = 0; |
| 279 |
9 |
| p += 4; |
| 280 |
9 |
| continue; |
| 281 |
|
| } |
| 282 |
|
| /* standalone } closes a brace group */ |
| 283 |
1137 |
▶ | if (c == '}' && ctx->brace_depth > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == '}' && ctx->brace_depth > 0) { |
58 |
1079 |
1137 |
| if (c == '}' && ctx->brace_depth > 0) { |
58 |
0 |
58 |
|
| 284 |
58 |
| ctx->brace_depth--; |
| 285 |
58 |
| d.pre--; |
| 286 |
58 |
| p++; |
| 287 |
58 |
| continue; |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| /* Keywords that post-open: then, do */ |
| 291 |
1079 |
▶ | if (eshu_bash_kw_match(p, end, "then", 4)) { d.post++; p += 4; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "then", 4)) { d.post++; p += 4; continue; } |
31 |
1048 |
1079 |
|
| 292 |
1048 |
▶ | if (eshu_bash_kw_match(p, end, "do", 2)) { d.post++; p += 2; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "do", 2)) { d.post++; p += 2; continue; } |
28 |
1020 |
1048 |
|
| 293 |
|
| |
| 294 |
|
| /* else / elif: -1 pre, +1 post (same depth as if/while body) */ |
| 295 |
1020 |
▶ | if (eshu_bash_kw_match(p, end, "elif", 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "elif", 4)) { |
4 |
1016 |
1020 |
|
| 296 |
4 |
| d.pre--; /* then on the same or next line provides post++ */ |
| 297 |
4 |
| p += 4; continue; |
| 298 |
|
| } |
| 299 |
1016 |
▶ | if (eshu_bash_kw_match(p, end, "else", 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "else", 4)) { |
7 |
1009 |
1016 |
|
| 300 |
7 |
| d.pre--; d.post++; |
| 301 |
7 |
| p += 4; continue; |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
|
| /* case EXPR in: post+1 to open case body */ |
| 305 |
1009 |
▶ | if (eshu_bash_kw_match(p, end, "case", 4)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "case", 4)) { |
9 |
1000 |
1009 |
|
| 306 |
9 |
| ctx->in_case = 1; |
| 307 |
9 |
| ctx->case_depth = ctx->depth + d.pre + d.post; |
| 308 |
9 |
| ctx->case_pat_open = 0; |
| 309 |
|
| /* 'in' will trigger the post+1 when we encounter it */ |
| 310 |
9 |
| p += 4; continue; |
| 311 |
|
| } |
| 312 |
1000 |
▶ | if (ctx->in_case && eshu_bash_kw_match(p, end, "in", 2)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->in_case && eshu_bash_kw_match(p, end, "in", 2)) { |
95 |
905 |
1000 |
| if (ctx->in_case && eshu_bash_kw_match(p, end, "in", 2)) { |
9 |
86 |
95 |
|
| 313 |
9 |
| d.post++; |
| 314 |
9 |
| p += 2; continue; |
| 315 |
|
| } |
| 316 |
|
| |
| 317 |
|
| /* standalone { opens a brace group */ |
| 318 |
991 |
▶ | if (c == '{' && (p + 1 >= end || *(p + 1) == ' ' || |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '{' && (p + 1 >= end || *(p + 1) == ' ' || |
60 |
931 |
991 |
| if (c == '{' && (p + 1 >= end || *(p + 1) == ' ' || |
2 |
58 |
60 |
| if (c == '{' && (p + 1 >= end || *(p + 1) == ' ' || |
0 |
2 |
2 |
|
| 319 |
0 |
▶ | *(p + 1) == '\t' || *(p + 1) == '\n' || |
Branch: 2 branches
| Branch | True | False | Total |
| *(p + 1) == '\t' || *(p + 1) == '\n' || |
0 |
0 |
0 |
| *(p + 1) == '\t' || *(p + 1) == '\n' || |
0 |
0 |
0 |
|
| 320 |
0 |
▶ | *(p + 1) == ';' || *(p + 1) == '#')) { |
Branch: 2 branches
| Branch | True | False | Total |
| *(p + 1) == ';' || *(p + 1) == '#')) { |
0 |
0 |
0 |
| *(p + 1) == ';' || *(p + 1) == '#')) { |
0 |
0 |
0 |
|
| 321 |
60 |
| ctx->brace_depth++; |
| 322 |
60 |
| d.post++; |
| 323 |
60 |
| p++; |
| 324 |
60 |
| continue; |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
|
| /* function keyword: 'function NAME' or 'NAME()' — the opening { |
| 328 |
|
| * will be caught by the standalone { branch above when it appears |
| 329 |
|
| * on the same or next line. */ |
| 330 |
931 |
▶ | if (eshu_bash_kw_match(p, end, "function", 8)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_bash_kw_match(p, end, "function", 8)) { |
1 |
930 |
931 |
|
| 331 |
1 |
| p += 8; continue; /* { handled separately */ |
| 332 |
|
| } |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
|
| /* case arm: ')' at case body depth (not subshell, not [[ ]]) */ |
| 336 |
4601 |
▶ | if (c == ')' && ctx->in_case && |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == ')' && ctx->in_case && |
101 |
4500 |
4601 |
| if (c == ')' && ctx->in_case && |
23 |
78 |
101 |
|
| 337 |
23 |
▶ | ctx->bracket_depth == 0 && ctx->subshell_depth == 0 && |
Branch: 2 branches
| Branch | True | False | Total |
| ctx->bracket_depth == 0 && ctx->subshell_depth == 0 && |
23 |
0 |
23 |
| ctx->bracket_depth == 0 && ctx->subshell_depth == 0 && |
23 |
0 |
23 |
|
| 338 |
23 |
▶ | ctx->arith_depth == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| ctx->arith_depth == 0) { |
23 |
0 |
23 |
|
| 339 |
23 |
▶ | if (ctx->case_pat_open) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->case_pat_open) { |
0 |
23 |
23 |
|
| 340 |
|
| /* close previous arm body before opening new one */ |
| 341 |
0 |
| d.pre--; |
| 342 |
|
| } |
| 343 |
23 |
| ctx->case_pat_open = 1; |
| 344 |
23 |
| d.post++; /* next lines are inside the pattern body */ |
| 345 |
23 |
| p++; |
| 346 |
23 |
| continue; |
| 347 |
|
| } |
| 348 |
|
| |
| 349 |
|
| /* ;; ;& ;;& — end of case arm body */ |
| 350 |
4578 |
▶ | if (c == ';' && p + 1 < end && *(p + 1) == ';' && ctx->in_case && |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == ';' && p + 1 < end && *(p + 1) == ';' && ctx->in_case && |
91 |
4487 |
4578 |
| if (c == ';' && p + 1 < end && *(p + 1) == ';' && ctx->in_case && |
91 |
0 |
91 |
| if (c == ';' && p + 1 < end && *(p + 1) == ';' && ctx->in_case && |
23 |
68 |
91 |
| if (c == ';' && p + 1 < end && *(p + 1) == ';' && ctx->in_case && |
23 |
0 |
23 |
|
| 351 |
23 |
▶ | ctx->subshell_depth == 0 && ctx->arith_depth == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| ctx->subshell_depth == 0 && ctx->arith_depth == 0) { |
23 |
0 |
23 |
| ctx->subshell_depth == 0 && ctx->arith_depth == 0) { |
23 |
0 |
23 |
|
| 352 |
23 |
▶ | if (ctx->case_pat_open) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->case_pat_open) { |
23 |
0 |
23 |
|
| 353 |
23 |
| d.post--; /* emit ;; at body depth, dedent after */ |
| 354 |
23 |
| ctx->case_pat_open = 0; |
| 355 |
|
| } |
| 356 |
23 |
| p += 2; |
| 357 |
|
| /* consume optional & */ |
| 358 |
23 |
▶ | if (p < end && *p == '&') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '&') p++; |
0 |
23 |
23 |
| if (p < end && *p == '&') p++; |
0 |
0 |
0 |
|
| 359 |
23 |
| continue; |
| 360 |
|
| } |
| 361 |
|
| |
| 362 |
4555 |
| p++; |
| 363 |
|
| } |
| 364 |
|
| |
| 365 |
519 |
| return d; |
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 369 |
|
| * Main indentation entry point |
| 370 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 371 |
|
| |
| 372 |
78 |
| static char *eshu_indent_bash(const char *src, size_t src_len, |
| 373 |
|
| const eshu_config_t *cfg, size_t *out_len) |
| 374 |
|
| { |
| 375 |
|
| eshu_bash_ctx_t ctx; |
| 376 |
|
| eshu_buf_t out; |
| 377 |
78 |
| const char *p = src; |
| 378 |
78 |
| const char *end = src + src_len; |
| 379 |
78 |
| int lineno = 1; |
| 380 |
|
| |
| 381 |
78 |
| eshu_bash_ctx_init(&ctx, cfg); |
| 382 |
78 |
| eshu_buf_init(&out, src_len + 512); |
| 383 |
|
| |
| 384 |
609 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
531 |
78 |
609 |
|
| 385 |
531 |
| const char *line_start = p; |
| 386 |
|
| const char *eol; |
| 387 |
|
| const char *content; |
| 388 |
|
| int content_len; |
| 389 |
|
| int is_blank; |
| 390 |
|
| eshu_bash_delta_t delta; |
| 391 |
|
| |
| 392 |
|
| /* find end of line */ |
| 393 |
531 |
| eol = p; |
| 394 |
8854 |
▶ | while (eol < end && *eol != '\n') eol++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (eol < end && *eol != '\n') eol++; |
8854 |
0 |
8854 |
| while (eol < end && *eol != '\n') eol++; |
8323 |
531 |
8854 |
|
| 395 |
|
| |
| 396 |
531 |
| content = eshu_skip_leading_ws(p); |
| 397 |
531 |
| content_len = (int)(eol - content); |
| 398 |
|
| /* trim trailing whitespace for measurement */ |
| 399 |
531 |
▶ | while (content_len > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| while (content_len > 0 && |
528 |
3 |
531 |
|
| 400 |
528 |
▶ | (content[content_len - 1] == ' ' || |
Branch: 1 branch
| Branch | True | False | Total |
| (content[content_len - 1] == ' ' || |
0 |
528 |
528 |
|
| 401 |
528 |
▶ | content[content_len - 1] == '\t')) |
Branch: 1 branch
| Branch | True | False | Total |
| content[content_len - 1] == '\t')) |
0 |
528 |
528 |
|
| 402 |
0 |
| content_len--; |
| 403 |
|
| |
| 404 |
531 |
| is_blank = (content_len == 0); |
| 405 |
|
| |
| 406 |
|
| /* ── heredoc body passthrough ── */ |
| 407 |
531 |
▶ | if (ctx.heredoc_pending) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx.heredoc_pending) { |
9 |
522 |
531 |
|
| 408 |
|
| /* check if this line is the terminator */ |
| 409 |
9 |
| const char *chk = p; |
| 410 |
9 |
▶ | if (ctx.heredoc_strip) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx.heredoc_strip) { |
3 |
6 |
9 |
|
| 411 |
5 |
▶ | while (chk < eol && *chk == '\t') chk++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (chk < eol && *chk == '\t') chk++; |
5 |
0 |
5 |
| while (chk < eol && *chk == '\t') chk++; |
2 |
3 |
5 |
|
| 412 |
|
| } |
| 413 |
9 |
▶ | if (ctx.heredoc_len > 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx.heredoc_len > 0 && |
9 |
0 |
9 |
|
| 414 |
9 |
▶ | (size_t)(eol - chk) == (size_t)ctx.heredoc_len && |
Branch: 1 branch
| Branch | True | False | Total |
| (size_t)(eol - chk) == (size_t)ctx.heredoc_len && |
3 |
6 |
9 |
|
| 415 |
3 |
▶ | memcmp(chk, ctx.heredoc_tag, (size_t)ctx.heredoc_len) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(chk, ctx.heredoc_tag, (size_t)ctx.heredoc_len) == 0) { |
3 |
0 |
3 |
|
| 416 |
|
| /* terminator line: emit verbatim, clear heredoc */ |
| 417 |
3 |
| eshu_buf_write(&out, p, (size_t)(eol - p)); |
| 418 |
3 |
▶ | if (eol < end) { eshu_buf_putc(&out, '\n'); eol++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (eol < end) { eshu_buf_putc(&out, '\n'); eol++; } |
3 |
0 |
3 |
|
| 419 |
3 |
| ctx.heredoc_pending = 0; |
| 420 |
3 |
| ctx.heredoc_len = 0; |
| 421 |
3 |
| p = eol; |
| 422 |
3 |
| lineno++; |
| 423 |
12 |
| continue; |
| 424 |
|
| } else { |
| 425 |
|
| /* heredoc body: emit verbatim */ |
| 426 |
6 |
| eshu_buf_write(&out, p, (size_t)(eol - p)); |
| 427 |
6 |
▶ | if (eol < end) { eshu_buf_putc(&out, '\n'); eol++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (eol < end) { eshu_buf_putc(&out, '\n'); eol++; } |
6 |
0 |
6 |
|
| 428 |
6 |
| p = eol; |
| 429 |
6 |
| lineno++; |
| 430 |
6 |
| continue; |
| 431 |
|
| } |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
|
| /* ── blank line: preserve, no indent change ── */ |
| 435 |
522 |
▶ | if (is_blank) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_blank) { |
3 |
519 |
522 |
|
| 436 |
3 |
▶ | if (eol < end) eshu_buf_putc(&out, '\n'); |
Branch: 1 branch
| Branch | True | False | Total |
| if (eol < end) eshu_buf_putc(&out, '\n'); |
3 |
0 |
3 |
|
| 437 |
3 |
▶ | p = (eol < end) ? eol + 1 : eol; |
Branch: 1 branch
| Branch | True | False | Total |
| p = (eol < end) ? eol + 1 : eol; |
3 |
0 |
3 |
|
| 438 |
3 |
| lineno++; |
| 439 |
3 |
| continue; |
| 440 |
|
| } |
| 441 |
|
| |
| 442 |
|
| /* ── scan line for depth deltas ── */ |
| 443 |
519 |
| delta = eshu_bash_scan_line(&ctx, content, content_len); |
| 444 |
|
| |
| 445 |
|
| /* ── apply pre-delta, clamp depth ── */ |
| 446 |
519 |
| ctx.depth += delta.pre; |
| 447 |
519 |
▶ | if (ctx.depth < 0) ctx.depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx.depth < 0) ctx.depth = 0; |
0 |
519 |
519 |
|
| 448 |
|
| |
| 449 |
|
| /* ── emit (only if in range) ── */ |
| 450 |
519 |
▶ | if (eshu_in_range(cfg, lineno)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_in_range(cfg, lineno)) { |
519 |
0 |
519 |
|
| 451 |
519 |
| eshu_emit_indent(&out, ctx.depth, cfg); |
| 452 |
|
| } else { |
| 453 |
|
| /* out-of-range: preserve original leading whitespace */ |
| 454 |
0 |
| eshu_buf_write(&out, line_start, (size_t)(content - line_start)); |
| 455 |
|
| } |
| 456 |
|
| |
| 457 |
|
| /* emit trimmed content */ |
| 458 |
519 |
| eshu_buf_write_trimmed(&out, content, (int)(eol - content)); |
| 459 |
519 |
▶ | if (eol < end) eshu_buf_putc(&out, '\n'); |
Branch: 1 branch
| Branch | True | False | Total |
| if (eol < end) eshu_buf_putc(&out, '\n'); |
519 |
0 |
519 |
|
| 460 |
|
| |
| 461 |
|
| /* ── apply post-delta ── */ |
| 462 |
519 |
| ctx.depth += delta.post; |
| 463 |
519 |
▶ | if (ctx.depth < 0) ctx.depth = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx.depth < 0) ctx.depth = 0; |
0 |
519 |
519 |
|
| 464 |
|
| |
| 465 |
519 |
▶ | p = (eol < end) ? eol + 1 : eol; |
Branch: 1 branch
| Branch | True | False | Total |
| p = (eol < end) ? eol + 1 : eol; |
519 |
0 |
519 |
|
| 466 |
519 |
| lineno++; |
| 467 |
|
| } |
| 468 |
|
| |
| 469 |
78 |
| eshu_buf_putc(&out, '\0'); |
| 470 |
78 |
| *out_len = out.len - 1; |
| 471 |
78 |
| return out.data; |
| 472 |
|
| } |
| 473 |
|
| |
| 474 |
|
| |
| 475 |
|
| #include "eshu_hl_util.h" |
| 476 |
|
| |
| 477 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 478 |
|
| * Bash keyword and builtin lists |
| 479 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 480 |
|
| |
| 481 |
|
| static const char * const eshu_hl_bash_kw[] = { |
| 482 |
|
| "break", "case", "continue", "do", "done", "elif", "else", |
| 483 |
|
| "esac", "eval", "exit", "export", "false", "fi", "for", |
| 484 |
|
| "function", "if", "in", "local", "readonly", "return", |
| 485 |
|
| "select", "set", "shift", "source", "then", "trap", |
| 486 |
|
| "true", "typeset", "unset", "until", "while", |
| 487 |
|
| NULL |
| 488 |
|
| }; |
| 489 |
|
| |
| 490 |
|
| static const char * const eshu_hl_bash_bi[] = { |
| 491 |
|
| "alias", "awk", "basename", "cat", "cd", "chmod", "chown", |
| 492 |
|
| "cp", "cut", "date", "declare", "dirname", "echo", "exec", |
| 493 |
|
| "find", "getopts", "grep", "head", "kill", "ln", "ls", |
| 494 |
|
| "mkdir", "mktemp", "mv", "printf", "pwd", "read", "rm", |
| 495 |
|
| "rmdir", "sed", "sleep", "sort", "tail", "test", "touch", |
| 496 |
|
| "tr", "uniq", "wc", "xargs", |
| 497 |
|
| NULL |
| 498 |
|
| }; |
| 499 |
|
| |
| 500 |
|
| /* ══════════════════════════════════════════════════════════════════ |
| 501 |
|
| * Bash highlighter |
| 502 |
|
| * ══════════════════════════════════════════════════════════════════ */ |
| 503 |
|
| |
| 504 |
36 |
| static char *eshu_highlight_bash(const char *src, size_t src_len, size_t *out_len) { |
| 505 |
|
| eshu_buf_t out; |
| 506 |
36 |
| const char *p = src; |
| 507 |
36 |
| const char *end = src + src_len; |
| 508 |
36 |
| const char *plain = p; |
| 509 |
36 |
| int at_bol = 1; |
| 510 |
|
| |
| 511 |
36 |
| eshu_buf_init(&out, src_len * 2 + 64); |
| 512 |
|
| |
| 513 |
|
| #define BASH_SPAN(cls, ts, te) do { \ |
| 514 |
|
| eshu_hl_flush(&out, plain, (ts)); \ |
| 515 |
|
| eshu_hl_span(&out, (cls), (ts), (te)); \ |
| 516 |
|
| p = (te); plain = p; \ |
| 517 |
|
| } while(0) |
| 518 |
|
| |
| 519 |
334 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
298 |
36 |
334 |
|
| 520 |
298 |
| char c = *p; |
| 521 |
|
| |
| 522 |
|
| /* comment: # to EOL */ |
| 523 |
298 |
▶ | if (c == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '#') { |
1 |
297 |
298 |
|
| 524 |
1 |
| const char *ts = p; |
| 525 |
20 |
▶ | while (p < end && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n') p++; |
20 |
0 |
20 |
| while (p < end && *p != '\n') p++; |
19 |
1 |
20 |
|
| 526 |
1 |
| BASH_SPAN("esh-c", ts, p); |
| 527 |
1 |
| at_bol = 0; |
| 528 |
1 |
| continue; |
| 529 |
|
| } |
| 530 |
|
| |
| 531 |
|
| /* heredoc start: <<[-] — highlight operator, body emitted as plain later */ |
| 532 |
297 |
▶ | if (c == '<' && p + 1 < end && *(p + 1) == '<') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '<' && p + 1 < end && *(p + 1) == '<') { |
2 |
295 |
297 |
| if (c == '<' && p + 1 < end && *(p + 1) == '<') { |
2 |
0 |
2 |
| if (c == '<' && p + 1 < end && *(p + 1) == '<') { |
1 |
1 |
2 |
|
| 533 |
1 |
| const char *ts = p; |
| 534 |
1 |
| p += 2; |
| 535 |
1 |
▶ | if (p < end && *p == '-') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '-') p++; |
1 |
0 |
1 |
| if (p < end && *p == '-') p++; |
0 |
1 |
1 |
|
| 536 |
|
| /* consume optional quote and tag name */ |
| 537 |
1 |
| char qc = 0; |
| 538 |
1 |
▶ | if (p < end && (*p == '\'' || *p == '"' || *p == '`')) qc = *p++; |
Branch: 4 branches
| Branch | True | False | Total |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) qc = *p++; |
1 |
0 |
1 |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) qc = *p++; |
1 |
0 |
1 |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) qc = *p++; |
1 |
0 |
1 |
| if (p < end && (*p == '\'' || *p == '"' || *p == '`')) qc = *p++; |
0 |
1 |
1 |
|
| 539 |
7 |
▶ | while (p < end && *p != '\n' && *p != '\r' && |
Branch: 5 branches
| Branch | True | False | Total |
| while (p < end && *p != '\n' && *p != '\r' && |
4 |
0 |
4 |
| while (p < end && *p != '\n' && *p != '\r' && |
3 |
1 |
4 |
| while (p < end && *p != '\n' && *p != '\r' && |
3 |
0 |
3 |
| while (p < end && *p != '\n' && *p != '\r' && |
0 |
3 |
3 |
| while (p < end && *p != '\n' && *p != '\r' && |
3 |
0 |
3 |
|
| 540 |
3 |
▶ | (qc ? *p != qc : (isalnum((unsigned char)*p) || *p == '_'))) |
Branch: 3 branches
| Branch | True | False | Total |
| (qc ? *p != qc : (isalnum((unsigned char)*p) || *p == '_'))) |
0 |
0 |
0 |
| (qc ? *p != qc : (isalnum((unsigned char)*p) || *p == '_'))) |
0 |
3 |
3 |
| (qc ? *p != qc : (isalnum((unsigned char)*p) || *p == '_'))) |
0 |
0 |
0 |
|
| 541 |
3 |
| p++; |
| 542 |
1 |
▶ | if (qc && p < end && *p == qc) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| if (qc && p < end && *p == qc) p++; |
0 |
1 |
1 |
| if (qc && p < end && *p == qc) p++; |
0 |
0 |
0 |
| if (qc && p < end && *p == qc) p++; |
0 |
0 |
0 |
|
| 543 |
1 |
| BASH_SPAN("esh-h", ts, p); |
| 544 |
1 |
| at_bol = 0; |
| 545 |
1 |
| continue; |
| 546 |
|
| } |
| 547 |
|
| |
| 548 |
|
| /* $'...' ANSI-C quoted string */ |
| 549 |
296 |
▶ | if (c == '$' && p + 1 < end && *(p + 1) == '\'') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '$' && p + 1 < end && *(p + 1) == '\'') { |
13 |
283 |
296 |
| if (c == '$' && p + 1 < end && *(p + 1) == '\'') { |
13 |
0 |
13 |
| if (c == '$' && p + 1 < end && *(p + 1) == '\'') { |
0 |
13 |
13 |
|
| 550 |
0 |
| const char *ts = p; |
| 551 |
0 |
| p += 2; |
| 552 |
0 |
▶ | while (p < end && *p != '\'') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') { |
0 |
0 |
0 |
| while (p < end && *p != '\'') { |
0 |
0 |
0 |
|
| 553 |
0 |
▶ | if (*p == '\\') { p++; if (p < end) p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
0 |
0 |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
0 |
0 |
|
| 554 |
0 |
| else p++; |
| 555 |
|
| } |
| 556 |
0 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
0 |
0 |
0 |
|
| 557 |
0 |
| BASH_SPAN("esh-s", ts, p); |
| 558 |
0 |
| at_bol = 0; |
| 559 |
0 |
| continue; |
| 560 |
|
| } |
| 561 |
|
| |
| 562 |
|
| /* double-quoted string "..." */ |
| 563 |
296 |
▶ | if (c == '"') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '"') { |
1 |
295 |
296 |
|
| 564 |
1 |
| const char *ts = p++; |
| 565 |
12 |
▶ | while (p < end && *p != '"') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '"') { |
12 |
0 |
12 |
| while (p < end && *p != '"') { |
11 |
1 |
12 |
|
| 566 |
11 |
▶ | if (*p == '\\') { p++; if (p < end) p++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
11 |
11 |
| if (*p == '\\') { p++; if (p < end) p++; } |
0 |
0 |
0 |
|
| 567 |
11 |
| else p++; |
| 568 |
|
| } |
| 569 |
1 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
1 |
0 |
1 |
|
| 570 |
1 |
| BASH_SPAN("esh-s", ts, p); |
| 571 |
1 |
| at_bol = 0; |
| 572 |
1 |
| continue; |
| 573 |
|
| } |
| 574 |
|
| |
| 575 |
|
| /* single-quoted string '...' */ |
| 576 |
295 |
▶ | if (c == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\'') { |
3 |
292 |
295 |
|
| 577 |
3 |
| const char *ts = p++; |
| 578 |
18 |
▶ | while (p < end && *p != '\'') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '\'') p++; |
18 |
0 |
18 |
| while (p < end && *p != '\'') p++; |
15 |
3 |
18 |
|
| 579 |
3 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
3 |
0 |
3 |
|
| 580 |
3 |
| BASH_SPAN("esh-s", ts, p); |
| 581 |
3 |
| at_bol = 0; |
| 582 |
3 |
| continue; |
| 583 |
|
| } |
| 584 |
|
| |
| 585 |
|
| /* variable: $VAR ${VAR} $# $@ $? $! $$ $0-$9 */ |
| 586 |
292 |
▶ | if (c == '$') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '$') { |
13 |
279 |
292 |
|
| 587 |
13 |
| const char *ts = p++; |
| 588 |
13 |
▶ | if (p < end && *p == '{') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '{') { |
13 |
0 |
13 |
| if (p < end && *p == '{') { |
2 |
11 |
13 |
|
| 589 |
2 |
| p++; |
| 590 |
18 |
▶ | while (p < end && *p != '}') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && *p != '}') p++; |
18 |
0 |
18 |
| while (p < end && *p != '}') p++; |
16 |
2 |
18 |
|
| 591 |
2 |
▶ | if (p < end) p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; |
2 |
0 |
2 |
|
| 592 |
11 |
▶ | } else if (p < end && (eshu_hl_isalpha_(*p))) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (p < end && (eshu_hl_isalpha_(*p))) { |
11 |
0 |
11 |
| } else if (p < end && (eshu_hl_isalpha_(*p))) { |
3 |
8 |
11 |
|
| 593 |
8 |
▶ | while (p < end && eshu_hl_isalnum_(*p)) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
8 |
0 |
8 |
| while (p < end && eshu_hl_isalnum_(*p)) p++; |
5 |
3 |
8 |
|
| 594 |
8 |
▶ | } else if (p < end && (*p == '#' || *p == '@' || *p == '?' || |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (p < end && (*p == '#' || *p == '@' || *p == '?' || |
8 |
0 |
8 |
| } else if (p < end && (*p == '#' || *p == '@' || *p == '?' || |
7 |
1 |
8 |
| } else if (p < end && (*p == '#' || *p == '@' || *p == '?' || |
6 |
1 |
7 |
| } else if (p < end && (*p == '#' || *p == '@' || *p == '?' || |
5 |
1 |
6 |
|
| 595 |
5 |
▶ | *p == '!' || *p == '$' || *p == '*' || |
Branch: 3 branches
| Branch | True | False | Total |
| *p == '!' || *p == '$' || *p == '*' || |
4 |
1 |
5 |
| *p == '!' || *p == '$' || *p == '*' || |
3 |
1 |
4 |
| *p == '!' || *p == '$' || *p == '*' || |
2 |
1 |
3 |
|
| 596 |
2 |
▶ | *p == '-' || isdigit((unsigned char)*p))) { |
Branch: 2 branches
| Branch | True | False | Total |
| *p == '-' || isdigit((unsigned char)*p))) { |
2 |
0 |
2 |
| *p == '-' || isdigit((unsigned char)*p))) { |
2 |
0 |
2 |
|
| 597 |
8 |
| p++; |
| 598 |
|
| } |
| 599 |
13 |
| BASH_SPAN("esh-v", ts, p); |
| 600 |
13 |
| at_bol = 0; |
| 601 |
13 |
| continue; |
| 602 |
|
| } |
| 603 |
|
| |
| 604 |
|
| /* number */ |
| 605 |
279 |
▶ | if (isdigit((unsigned char)c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isdigit((unsigned char)c)) { |
6 |
273 |
279 |
|
| 606 |
6 |
| const char *ts = p; |
| 607 |
13 |
▶ | while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
13 |
0 |
13 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
7 |
6 |
13 |
| while (p < end && (isdigit((unsigned char)*p) || *p == '_')) p++; |
0 |
6 |
6 |
|
| 608 |
6 |
| BASH_SPAN("esh-n", ts, p); |
| 609 |
6 |
| at_bol = 0; |
| 610 |
6 |
| continue; |
| 611 |
|
| } |
| 612 |
|
| |
| 613 |
|
| /* identifier: keyword, builtin, or plain name */ |
| 614 |
273 |
▶ | if (eshu_hl_isalpha_(c)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_isalpha_(c)) { |
102 |
171 |
273 |
|
| 615 |
102 |
| const char *ts = p; |
| 616 |
475 |
▶ | while (p < end && (eshu_hl_isalnum_(*p) || *p == '-')) p++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (p < end && (eshu_hl_isalnum_(*p) || *p == '-')) p++; |
472 |
3 |
475 |
| while (p < end && (eshu_hl_isalnum_(*p) || *p == '-')) p++; |
373 |
99 |
472 |
| while (p < end && (eshu_hl_isalnum_(*p) || *p == '-')) p++; |
0 |
99 |
99 |
|
| 617 |
|
| /* distinguish: word followed by '=' is an assignment, not keyword */ |
| 618 |
102 |
▶ | if (p < end && *p == '=') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < end && *p == '=') { |
99 |
3 |
102 |
| if (p < end && *p == '=') { |
5 |
94 |
99 |
|
| 619 |
|
| /* plain: variable assignment LHS */ |
| 620 |
5 |
| at_bol = 0; |
| 621 |
5 |
| continue; |
| 622 |
|
| } |
| 623 |
97 |
| eshu_hl_flush(&out, plain, ts); |
| 624 |
97 |
| plain = p; |
| 625 |
97 |
| size_t ilen = (size_t)(p - ts); |
| 626 |
97 |
▶ | if (eshu_hl_kw(ts, ilen, eshu_hl_bash_kw)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eshu_hl_kw(ts, ilen, eshu_hl_bash_kw)) { |
39 |
58 |
97 |
|
| 627 |
39 |
| eshu_hl_span(&out, "esh-k", ts, p); |
| 628 |
58 |
▶ | } else if (eshu_hl_kw(ts, ilen, eshu_hl_bash_bi)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (eshu_hl_kw(ts, ilen, eshu_hl_bash_bi)) { |
24 |
34 |
58 |
|
| 629 |
24 |
| eshu_hl_span(&out, "esh-b", ts, p); |
| 630 |
|
| } else { |
| 631 |
34 |
| eshu_hl_write_html(&out, ts, ilen); |
| 632 |
|
| } |
| 633 |
97 |
| at_bol = 0; |
| 634 |
97 |
| continue; |
| 635 |
|
| } |
| 636 |
|
| |
| 637 |
171 |
▶ | if (c == '\n') { at_bol = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '\n') { at_bol = 1; } |
35 |
136 |
171 |
|
| 638 |
136 |
▶ | else if (c != ' ' && c != '\t') { at_bol = 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c != ' ' && c != '\t') { at_bol = 0; } |
49 |
87 |
136 |
| else if (c != ' ' && c != '\t') { at_bol = 0; } |
49 |
0 |
49 |
|
| 639 |
|
| (void)at_bol; |
| 640 |
171 |
| p++; |
| 641 |
|
| } |
| 642 |
|
| |
| 643 |
36 |
| eshu_hl_flush(&out, plain, end); |
| 644 |
36 |
| eshu_buf_putc(&out, '\0'); |
| 645 |
36 |
| *out_len = out.len - 1; |
| 646 |
36 |
| return out.data; |
| 647 |
|
| |
| 648 |
|
| #undef BASH_SPAN |
| 649 |
|
| } |
| 650 |
|
| |
| 651 |
|
| #endif /* ESHU_BASH_H */ |