MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / IsDirective

Method IsDirective

source/script.cpp:3549–4000  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3547
3548
3549inline ResultType Script::IsDirective(LPTSTR aBuf)
3550// aBuf must be a modifiable string as it may be temporarily terminated at different points.
3551// Returns CONDITION_TRUE, CONDITION_FALSE, or FAIL.
3552// Note: Don't assume that every line in the script that starts with '#' is a directive
3553// because hotkeys can legitimately start with that as well. i.e., the following line should
3554// not be unconditionally ignored, just because it starts with '#', since it is a valid hotkey:
3555// #y::run, notepad
3556{
3557 TCHAR end_flags[] = {' ', '\t', '\0'}; // '\0' must be last.
3558 LPTSTR directive_end, parameter;
3559 if ( !(directive_end = StrChrAny(aBuf, end_flags)) )
3560 {
3561 directive_end = aBuf + _tcslen(aBuf); // Point it to the zero terminator.
3562 parameter = NULL;
3563 }
3564 else
3565 if (!*(parameter = omit_leading_whitespace(directive_end)))
3566 parameter = NULL;
3567
3568 // Make a null-terminated copy of the potential directive name, if not already terminated,
3569 // so that overlapping names such as #MaxThreads and #MaxThreadsPerHotkey won't get mixed up.
3570 // This significantly reduces code size vs. using tcslicmp() in IS_DIRECTIVE_MATCH().
3571 LPCTSTR directive_name;
3572 TCHAR directive_name_buf[32];
3573 if (directive_end - aBuf >= _countof(directive_name_buf))
3574 return CONDITION_FALSE;
3575 if (*directive_end)
3576 {
3577 tcslcpy(directive_name_buf, aBuf, directive_end - aBuf + 1);
3578 directive_name = directive_name_buf;
3579 }
3580 else
3581 directive_name = aBuf;
3582 #define IS_DIRECTIVE_MATCH(directive) (!_tcsicmp(directive_name, directive))
3583
3584 bool is_include_again = false; // Set default in case of short-circuit boolean.
3585 if (IS_DIRECTIVE_MATCH(_T("#Include")) || (is_include_again = IS_DIRECTIVE_MATCH(_T("#IncludeAgain"))))
3586 {
3587 // Standalone EXEs ignore this directive since the included files were already merged in
3588 // with the main file when the script was compiled. These should have been removed
3589 // or commented out by Ahk2Exe, but just in case, it's safest to ignore them:
3590#ifdef AUTOHOTKEYSC
3591 return CONDITION_TRUE;
3592#else
3593 if (!parameter)
3594 return ScriptError(ERR_PARAM1_REQUIRED, aBuf);
3595
3596 // Permit quote marks around the parameter. In future, quote marks might be required,
3597 // or the parameter might be evaluated as an expression.
3598 parameter = strip_quote_marks(parameter);
3599 if (!*parameter)
3600 return ScriptError(ERR_PARAM1_INVALID);
3601
3602 // v1.0.32:
3603 bool ignore_load_failure = (parameter[0] == '*' && ctoupper(parameter[1]) == 'I' && IS_SPACE_OR_TAB(parameter[2])); // Relies on short-circuit boolean order.
3604 if (ignore_load_failure)
3605 parameter += 3; // Skip over at most one space or tab, since others might be a literal part of the filename.
3606

Callers

nothing calls this directly

Calls 14

StrChrAnyFunction · 0.85
omit_leading_whitespaceFunction · 0.85
strip_quote_marksFunction · 0.85
ctoupperFunction · 0.85
GetWorkingDirFunction · 0.85
SetWorkingDirFunction · 0.85
FindHotkeyIfExprFunction · 0.85
tcscasestrFunction · 0.85
ConvertEscapeSequencesFunction · 0.85
ATOIFunction · 0.85
SendLevelIsValidFunction · 0.85
omit_trailing_whitespaceFunction · 0.85

Tested by

no test coverage detected