Skip to content

Commit 5fe1f4f

Browse files
committed
fix(discover): detect extensionless scripts by shebang
Signed-off-by: ibaldr89 <163388887+ibaldr89@users.noreply.github.com>
1 parent d698db8 commit 5fe1f4f

4 files changed

Lines changed: 480 additions & 1 deletion

File tree

‎src/discover/discover.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,11 @@ static const char *file_skip_reason(const char *entry_name, const char *rel_path
651651
static CBMLanguage detect_file_language(const char *entry_name, const char *abs_path) {
652652
CBMLanguage lang = cbm_language_for_filename(entry_name);
653653
if (lang == CBM_LANG_COUNT) {
654-
return CBM_LANG_COUNT;
654+
/* Filename/extension detection failed: fall back to a conservative
655+
* shebang probe so extensionless scripts get indexed (#1199). Filename
656+
* detection stays authoritative — this runs only when it returns
657+
* unknown. */
658+
return cbm_language_from_shebang(abs_path);
655659
}
656660
/* Special: .m files need content-based disambiguation */
657661
const char *dot = strrchr(entry_name, '.');

‎src/discover/discover.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ CBMLanguage cbm_disambiguate_cls(const char *path);
5050
* On read failure, defaults to CBM_LANG_BITBAKE. */
5151
CBMLanguage cbm_disambiguate_inc(const char *path);
5252

53+
/* Detect a supported script language from a file's shebang (#!...) first line.
54+
* Conservative fallback used only when filename/extension detection is unknown
55+
* (see detect_file_language); it never overrides extension or special-filename
56+
* matches. Opens the file read-only and reads only a bounded first line.
57+
* Recognizes the interpreter *basename* (not the parent path):
58+
* python / python2 / python3 / dotted versions (python3.12) -> CBM_LANG_PYTHON
59+
* sh / bash / dash / ksh / zsh -> CBM_LANG_BASH
60+
* node / nodejs -> CBM_LANG_JAVASCRIPT
61+
* ruby -> RUBY, perl -> PERL, php -> PHP, lua -> LUA
62+
* Handles direct paths, "env <interp>", "env -S <interp> <args>", and CRLF.
63+
* Fails closed (returns CBM_LANG_COUNT) on read error, missing/malformed
64+
* shebang, an embedded NUL in the first line, or an unknown interpreter. */
65+
CBMLanguage cbm_language_from_shebang(const char *path);
66+
5367
/* ── Gitignore pattern matching ──────────────────────────────────── */
5468

5569
typedef struct cbm_gitignore cbm_gitignore_t;

‎src/discover/language.c‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,172 @@ const char *cbm_language_name(CBMLanguage lang) {
945945
return LANG_NAMES[lang] ? LANG_NAMES[lang] : "Unknown";
946946
}
947947

948+
/* ── Shebang interpreter detection (extensionless scripts) ────────── */
949+
950+
/* Basename of an interpreter path: the segment after the last '/'. Shebangs
951+
* are a POSIX convention, so only '/' is treated as a separator. */
952+
static const char *interp_basename(const char *path) {
953+
const char *slash = strrchr(path, '/');
954+
return slash ? slash + SKIP_ONE : path;
955+
}
956+
957+
/* "python" optionally followed by an explicit numeric version (digits and dots
958+
* only, e.g. "python3", "python3.12"). Bounded and explicit so arbitrary
959+
* suffixes like "python-wrapper" are rejected. */
960+
static bool is_python_interp(const char *base) {
961+
if (strncmp(base, "python", SLEN("python")) != 0) {
962+
return false;
963+
}
964+
const char *version = base + SLEN("python");
965+
if (*version == '\0') {
966+
return true;
967+
}
968+
969+
/* Each numeric component must contain at least one digit. */
970+
bool need_digit = true;
971+
for (const char *v = version; *v; v++) {
972+
if (isdigit((unsigned char)*v)) {
973+
need_digit = false;
974+
} else if (*v == '.' && !need_digit) {
975+
need_digit = true;
976+
} else {
977+
return false;
978+
}
979+
}
980+
return !need_digit;
981+
}
982+
983+
/* Map an interpreter basename to a language, or CBM_LANG_COUNT if unrecognized.
984+
* Non-python interpreters are matched exactly (no prefix/suffix logic). */
985+
static CBMLanguage lang_for_interpreter(const char *base) {
986+
if (is_python_interp(base)) {
987+
return CBM_LANG_PYTHON;
988+
}
989+
static const struct {
990+
const char *name;
991+
CBMLanguage lang;
992+
} INTERP_TABLE[] = {
993+
{"sh", CBM_LANG_BASH}, {"bash", CBM_LANG_BASH}, {"dash", CBM_LANG_BASH},
994+
{"ksh", CBM_LANG_BASH}, {"zsh", CBM_LANG_BASH}, {"node", CBM_LANG_JAVASCRIPT},
995+
{"nodejs", CBM_LANG_JAVASCRIPT}, {"ruby", CBM_LANG_RUBY}, {"perl", CBM_LANG_PERL},
996+
{"php", CBM_LANG_PHP}, {"lua", CBM_LANG_LUA},
997+
};
998+
for (size_t i = 0; i < sizeof(INTERP_TABLE) / sizeof(INTERP_TABLE[0]); i++) {
999+
if (strcmp(base, INTERP_TABLE[i].name) == 0) {
1000+
return INTERP_TABLE[i].lang;
1001+
}
1002+
}
1003+
return CBM_LANG_COUNT;
1004+
}
1005+
1006+
/* Advance *cursor past leading blanks and return the next whitespace-delimited
1007+
* token (NUL-terminated in place), or NULL when the line is exhausted. */
1008+
static char *shebang_next_token(char **cursor) {
1009+
char *p = *cursor;
1010+
while (*p == ' ' || *p == '\t') {
1011+
p++;
1012+
}
1013+
if (*p == '\0') {
1014+
*cursor = p;
1015+
return NULL;
1016+
}
1017+
char *start = p;
1018+
while (*p && *p != ' ' && *p != '\t') {
1019+
p++;
1020+
}
1021+
if (*p) {
1022+
*p = '\0';
1023+
p++;
1024+
}
1025+
*cursor = p;
1026+
return start;
1027+
}
1028+
1029+
CBMLanguage cbm_language_from_shebang(const char *path) {
1030+
if (!path) {
1031+
return CBM_LANG_COUNT;
1032+
}
1033+
1034+
FILE *f = cbm_fopen(path, "rb");
1035+
if (!f) {
1036+
return CBM_LANG_COUNT; /* fail closed on read error */
1037+
}
1038+
1039+
/* Read only a bounded first line. */
1040+
char buf[CBM_SZ_256];
1041+
size_t n = fread(buf, SKIP_ONE, sizeof(buf) - SKIP_ONE, f);
1042+
1043+
/* Fail closed on any read error rather than parsing a partial buffer. */
1044+
if (ferror(f)) {
1045+
(void)fclose(f);
1046+
return CBM_LANG_COUNT;
1047+
}
1048+
1049+
/* If the bounded buffer filled without containing a newline, the first
1050+
* line may extend past our bound. Probe a single extra byte to tell an
1051+
* exact EOF (the whole file is <= 255 bytes) from a truncated longer
1052+
* line: any surviving byte -- including a newline just beyond the bound --
1053+
* means the first line was cut off, so fail closed. A probe read error
1054+
* fails closed too. This keeps the read bounded (no unbounded line read
1055+
* or allocation). */
1056+
bool have_newline = (memchr(buf, '\n', n) != NULL);
1057+
if (!have_newline && n == sizeof(buf) - SKIP_ONE) {
1058+
int probe = fgetc(f);
1059+
if (probe != EOF || ferror(f)) {
1060+
(void)fclose(f);
1061+
return CBM_LANG_COUNT;
1062+
}
1063+
}
1064+
(void)fclose(f);
1065+
1066+
/* Must begin with "#!". */
1067+
if (n < PAIR_LEN || buf[0] != '#' || buf[1] != '!') {
1068+
return CBM_LANG_COUNT;
1069+
}
1070+
1071+
/* Isolate the first line; reject an embedded NUL before the newline. */
1072+
size_t line_len = 0;
1073+
while (line_len < n && buf[line_len] != '\n') {
1074+
if (buf[line_len] == '\0') {
1075+
return CBM_LANG_COUNT; /* embedded NUL — treat as binary */
1076+
}
1077+
line_len++;
1078+
}
1079+
/* Trim a trailing CR so CRLF first lines parse. */
1080+
if (line_len > 0 && buf[line_len - SKIP_ONE] == '\r') {
1081+
line_len--;
1082+
}
1083+
buf[line_len] = '\0';
1084+
1085+
/* First token after "#!" is the interpreter (or env). */
1086+
char *cursor = buf + PAIR_LEN;
1087+
char *interp = shebang_next_token(&cursor);
1088+
if (!interp) {
1089+
return CBM_LANG_COUNT;
1090+
}
1091+
const char *base = interp_basename(interp);
1092+
1093+
/* "env [-S] <interp> [args...]": the real interpreter is the next token.
1094+
* Only the plain "env <interp>" and "env -S/--split-string <interp> [args]"
1095+
* shapes are supported. After the optional -S, the interpreter token must
1096+
* be a real command, so reject option tokens (leading '-') and NAME=value
1097+
* assignments (containing '=') -- e.g. "env PYTHON=/usr/bin/python
1098+
* python-wrapper", where env would treat the first token as an env-var
1099+
* setting rather than the program to run. */
1100+
if (strcmp(base, "env") == 0) {
1101+
char *tok = shebang_next_token(&cursor);
1102+
if (tok && (strcmp(tok, "-S") == 0 || strcmp(tok, "--split-string") == 0)) {
1103+
tok = shebang_next_token(&cursor);
1104+
}
1105+
if (!tok || tok[0] == '-' || strchr(tok, '=') != NULL) {
1106+
return CBM_LANG_COUNT;
1107+
}
1108+
base = interp_basename(tok);
1109+
}
1110+
1111+
return lang_for_interpreter(base);
1112+
}
1113+
9481114
/* ── .m file disambiguation ──────────────────────────────────────── */
9491115

9501116
/* Simple substring search helper */

0 commit comments

Comments
 (0)