| 3258 | } |
| 3259 | |
| 3260 | static void sh_makefile(sh_pass_t *p, const char *rel, const char *src, size_t len) { |
| 3261 | if (!ts_parser_set_language(p->parser, cbm_ts_language(CBM_LANG_MAKEFILE))) { |
| 3262 | return; |
| 3263 | } |
| 3264 | TSTree *tree = ts_parser_parse_string(p->parser, NULL, src, (uint32_t)len); |
| 3265 | if (!tree) { |
| 3266 | return; |
| 3267 | } |
| 3268 | CBMArena a; |
| 3269 | cbm_arena_init(&a); |
| 3270 | const char *base = sh_basename(rel); |
| 3271 | mk_t m = {.a = &a, |
| 3272 | .src = src, |
| 3273 | .vars = cbm_ht_create(0), |
| 3274 | .makefile = strcmp(base, "Makefile") == 0 || strcmp(base, "GNUmakefile") == 0 || |
| 3275 | strcmp(base, "makefile") == 0}; |
| 3276 | if (m.vars) { |
| 3277 | mk_collect(&m, ts_tree_root_node(tree), false); |
| 3278 | } |
| 3279 | if (m.nlines > SKIP_ONE) { |
| 3280 | qsort(m.lines, (size_t)m.nlines, sizeof(*m.lines), mk_cmp_line); |
| 3281 | } |
| 3282 | for (int r = 0; m.vars && r < m.nlines; r++) { |
| 3283 | if (r > 0 && m.lines[r].b == m.lines[r - SKIP_ONE].b) { |
| 3284 | continue; /* nested nodes covered it already */ |
| 3285 | } |
| 3286 | uint32_t b = m.lines[r].b; |
| 3287 | uint32_t e = m.lines[r].e; |
| 3288 | const char *raw = cbm_arena_strndup(&a, src + b, e - b); |
| 3289 | const char *text = raw ? mk_expand(&m, raw) : NULL; |
| 3290 | if (!text || (!strchr(text, '/') && !strstr(text, "sh") && !strstr(text, "make") && |
| 3291 | !sh_mentions_deploy_tool(text, strlen(text)))) { |
| 3292 | continue; /* no deploy, program by path, shell or make */ |
| 3293 | } |
| 3294 | size_t tl = strlen(text); |
| 3295 | char *buf = cbm_arena_alloc(&a, tl + CBM_SZ_16); |
| 3296 | if (!buf) { |
| 3297 | continue; |
| 3298 | } |
| 3299 | memcpy(buf, text, tl); |
| 3300 | memset(buf + tl, 0, CBM_SZ_16); |
| 3301 | sh_t s; |
| 3302 | sh_state_init(&s, p, &a, rel); |
| 3303 | if (s.env) { |
| 3304 | s.line_base = (int)m.lines[r].row; |
| 3305 | sh_val_t *root = cbm_arena_alloc(&a, sizeof(*root)); |
| 3306 | sh_val_t *cur = cbm_arena_alloc(&a, sizeof(*cur)); |
| 3307 | if (root && cur) { |
| 3308 | *root = (sh_val_t){SH_ROOT, NULL, 0}; |
| 3309 | *cur = (sh_val_t){sh_anchor(&a, s.sdir), NULL, 0}; |
| 3310 | cbm_ht_set(s.env, "__cbm_root", root); |
| 3311 | if (m.makefile) { |
| 3312 | cbm_ht_set(s.env, "__cbm_curdir", cur); |
| 3313 | } |
| 3314 | } |
| 3315 | s.cwd = m.makefile ? (sh_cwd_t){SH_AT, s.sdir} : (sh_cwd_t){SH_FREE, NULL}; |
| 3316 | sh_run(&s, p->parser, buf, tl); |
| 3317 | } |
no test coverage detected