| 1633 | } |
| 1634 | |
| 1635 | CBMLanguage cbm_language_classify_with(const cbm_userconfig_t *config, const char *filename, |
| 1636 | const unsigned char *head, size_t head_len, bool more, |
| 1637 | bool readable) { |
| 1638 | if (!filename) { |
| 1639 | return CBM_LANG_COUNT; |
| 1640 | } |
| 1641 | if (!readable || !head) { |
| 1642 | head = NULL; |
| 1643 | head_len = 0; |
| 1644 | } |
| 1645 | CBMLanguage lang = lang_for_filename(config, filename); |
| 1646 | if (lang == CBM_LANG_COUNT) { |
| 1647 | /* The name says nothing: a conservative shebang probe, so extensionless |
| 1648 | * scripts get indexed (#1199). The name stays authoritative otherwise. */ |
| 1649 | return readable ? lang_shebang(head, head_len, more) : CBM_LANG_COUNT; |
| 1650 | } |
| 1651 | const char *ext = lang_probe_extension(filename); |
| 1652 | if (ext) { |
| 1653 | char buf[LANG_PROBE_CFC + SKIP_ONE]; |
| 1654 | bool cfc = strcmp(ext, ".cfc") == 0; |
| 1655 | lang_head_text(buf, cfc ? LANG_PROBE_CFC : LANG_PROBE_HEAD, head, head_len); |
| 1656 | if (strcmp(ext, ".m") == 0) { |
| 1657 | /* Objective-C, Magma or MATLAB */ |
| 1658 | lang = readable ? lang_m_text(buf) : CBM_LANG_MATLAB; |
| 1659 | } else if (strcmp(ext, ".cls") == 0) { |
| 1660 | /* ObjectScript UDL, Apex or a Visual Basic 6 class module */ |
| 1661 | lang = readable ? lang_cls_text(buf) : CBM_LANG_APEX; |
| 1662 | } else if (strcmp(ext, ".inc") == 0) { |
| 1663 | /* BitBake or an ObjectScript include */ |
| 1664 | lang = readable ? lang_inc_text(buf) : CBM_LANG_BITBAKE; |
| 1665 | } else if (cfc) { |
| 1666 | /* script or tag dialect */ |
| 1667 | lang = readable ? lang_cfc_text(buf) : CBM_LANG_CFSCRIPT; |
| 1668 | } else if (strcmp(ext, ".frm") == 0) { |
| 1669 | /* FORM or a Visual Basic 6 form (#721) */ |
| 1670 | lang = readable ? lang_frm_text(buf) : CBM_LANG_FORM; |
| 1671 | } else { |
| 1672 | /* .res: ReScript, or a binary Godot / Windows resource (#2176) */ |
| 1673 | lang = readable ? lang_res_bytes(head, head_len) : CBM_LANG_RESCRIPT; |
| 1674 | } |
| 1675 | } |
| 1676 | if (lang == CBM_LANG_XML && readable && lang_objectscript_export(head, head_len)) { |
| 1677 | return CBM_LANG_OBJECTSCRIPT_EXPORT; |
| 1678 | } |
| 1679 | if (lang == CBM_LANG_JSON && lang_name_in(filename, IGNORED_JSON_FILES)) { |
| 1680 | return CBM_LANG_COUNT; |
| 1681 | } |
| 1682 | return lang; |
| 1683 | } |
| 1684 | |
| 1685 | size_t cbm_language_probe_bytes(const char *filename) { |
| 1686 | return cbm_language_probe_bytes_with(cbm_get_user_lang_config(), filename); |