| 1516 | |
| 1517 | |
| 1518 | UINT Script::LoadFromFile(LPCTSTR aFileSpec) |
| 1519 | // Returns the number of non-comment lines that were loaded, or LOADING_FAILED on error. |
| 1520 | { |
| 1521 | mIsReadyToExecute = mAutoExecSectionIsRunning = false; |
| 1522 | if (!aFileSpec) |
| 1523 | aFileSpec = mFileSpec; |
| 1524 | |
| 1525 | #ifndef AUTOHOTKEYSC // When not in stand-alone mode, read an external script file. |
| 1526 | DWORD attr = mKind == ScriptKindFile ? GetFileAttributes(aFileSpec) : 0; // v1.1.17: Don't check if reading script from stdin. |
| 1527 | if (attr == MAXDWORD) // File does not exist or lacking the authorization to get its attributes. |
| 1528 | { |
| 1529 | if (!g_script.mErrorStdOut) |
| 1530 | { |
| 1531 | TCHAR buf[T_MAX_PATH + 24]; |
| 1532 | sntprintf(buf, _countof(buf), _T("%s\n%s"), ERR_SCRIPT_NOT_FOUND, aFileSpec); |
| 1533 | MsgBox(buf, MB_ICONHAND); |
| 1534 | } |
| 1535 | else |
| 1536 | { |
| 1537 | Line::sSourceFile = &mFileSpec; |
| 1538 | ScriptError(ERR_SCRIPT_NOT_FOUND); |
| 1539 | } |
| 1540 | return LOADING_FAILED; |
| 1541 | } |
| 1542 | #endif |
| 1543 | |
| 1544 | // Load the main script file. This will also load any files it includes with #Include. |
| 1545 | if (!LoadIncludedFile(mKind == ScriptKindStdIn ? _T("*") : aFileSpec, false, false)) |
| 1546 | return LOADING_FAILED; |
| 1547 | |
| 1548 | #ifdef ENABLE_DLLCALL |
| 1549 | // So that (the last occuring) "#DllLoad directory" doesn't affect calls to GetDllProcAddress for run time calls to DllCall |
| 1550 | // or DllCall optimizations in Line::ExpressionToPostfix. |
| 1551 | if (!SetDllDirectory(NULL)) |
| 1552 | { |
| 1553 | ScriptError(ERR_INTERNAL_CALL); |
| 1554 | return LOADING_FAILED; |
| 1555 | } |
| 1556 | #endif |
| 1557 | // Preparse all expressions and resolve all variable references. The outer-most scope |
| 1558 | // is preparsed first, then each function, working inward through all nested functions. |
| 1559 | // All of a function's non-dynamic local variables are created before variable names |
| 1560 | // are resolved in nested functions. |
| 1561 | if (!PreparseExpressions(mFirstLine) |
| 1562 | || !PreparseExpressions(mHotFuncs) // mHotFuncs first in case they have nested functions, which would be in mFuncs. |
| 1563 | || !PreparseExpressions(mFuncs) |
| 1564 | || !PreparseVarRefs()) |
| 1565 | return LOADING_FAILED; // Error was already displayed by the above call. |
| 1566 | |
| 1567 | // Do some processing of local variables to support closures. |
| 1568 | // This must be done after PreparseExpressions() has resolved all variable references. |
| 1569 | if (!PreprocessLocalVars(mHotFuncs) // mHotFuncs first in case they have nested functions, which would be in mFuncs. |
| 1570 | || !PreprocessLocalVars(mFuncs)) |
| 1571 | return LOADING_FAILED; |
| 1572 | |
| 1573 | free(mHotFuncs.mItem); // Not needed beyond this point. |
| 1574 | |
| 1575 | // Resolve any unresolved base classes. |
no test coverage detected