@andorra-labs/code 0.1.12 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/account-presentation.d.ts +1 -37
- package/dist/src/account-presentation.js +6 -43
- package/dist/src/activity.d.ts +0 -25
- package/dist/src/activity.js +1 -23
- package/dist/src/agent-definitions.d.ts +0 -26
- package/dist/src/agent-definitions.js +0 -43
- package/dist/src/agent-execution.d.ts +0 -2
- package/dist/src/agent-execution.js +347 -554
- package/dist/src/agent-mcp-catalog.d.ts +0 -8
- package/dist/src/agent-mcp-catalog.js +0 -2
- package/dist/src/agent-mcp.d.ts +0 -56
- package/dist/src/agent-mcp.js +11 -155
- package/dist/src/agent-modifiers.d.ts +0 -1
- package/dist/src/agent-modifiers.js +25 -68
- package/dist/src/analytics.d.ts +0 -66
- package/dist/src/analytics.js +0 -45
- package/dist/src/andorra/endpoint.d.ts +7 -0
- package/dist/src/andorra/endpoint.js +46 -0
- package/dist/src/andorra/entry.d.ts +21 -0
- package/dist/src/andorra/entry.js +100 -0
- package/dist/src/andorra/errors.d.ts +2 -0
- package/dist/src/andorra/errors.js +39 -0
- package/dist/src/andorra/hosted.d.ts +52 -0
- package/dist/src/andorra/hosted.js +126 -0
- package/dist/src/andorra/monitor.d.ts +151 -0
- package/dist/src/andorra/monitor.js +393 -0
- package/dist/src/andorra/notices.d.ts +7 -0
- package/dist/src/andorra/notices.js +15 -0
- package/dist/src/andorra/opt-in.d.ts +58 -0
- package/dist/src/andorra/opt-in.js +112 -0
- package/dist/src/andorra/request-watch.d.ts +6 -0
- package/dist/src/andorra/request-watch.js +10 -0
- package/dist/src/andorra/state.d.ts +86 -0
- package/dist/src/andorra/state.js +238 -0
- package/dist/src/andorra/terminal.d.ts +68 -0
- package/dist/src/andorra/terminal.js +125 -0
- package/dist/src/andorra/usage-format.d.ts +9 -0
- package/dist/src/andorra/usage-format.js +19 -0
- package/dist/src/anthropic-console-auth.d.ts +0 -24
- package/dist/src/anthropic-console-auth.js +0 -37
- package/dist/src/anthropic-credentials.d.ts +0 -25
- package/dist/src/anthropic-credentials.js +9 -29
- package/dist/src/app-server/protocol.d.ts +18 -246
- package/dist/src/app-server/protocol.js +1 -132
- package/dist/src/app-server/reasoning.d.ts +0 -31
- package/dist/src/app-server/reasoning.js +0 -54
- package/dist/src/app-server/terminal.d.ts +0 -36
- package/dist/src/app-server/terminal.js +3 -39
- package/dist/src/app-server/workspace.d.ts +0 -167
- package/dist/src/app-server/workspace.js +4 -406
- package/dist/src/app-server.d.ts +52 -925
- package/dist/src/app-server.js +828 -3076
- package/dist/src/artifacts.d.ts +0 -10
- package/dist/src/artifacts.js +1 -11
- package/dist/src/attachments.d.ts +4 -98
- package/dist/src/attachments.js +10 -130
- package/dist/src/auth/AuthManager.d.ts +9 -93
- package/dist/src/auth/AuthManager.js +164 -186
- package/dist/src/auth/callback-page.d.ts +0 -1
- package/dist/src/auth/callback-page.js +0 -4
- package/dist/src/auth/callback.d.ts +0 -25
- package/dist/src/auth/callback.js +1 -29
- package/dist/src/auth/deployment.d.ts +1 -16
- package/dist/src/auth/deployment.js +1 -16
- package/dist/src/auth/index.d.ts +1 -12
- package/dist/src/auth/index.js +1 -12
- package/dist/src/auth/lock.d.ts +0 -1
- package/dist/src/auth/lock.js +0 -1
- package/dist/src/auth/store.d.ts +33 -108
- package/dist/src/auth/store.js +55 -130
- package/dist/src/auth/types.d.ts +0 -8
- package/dist/src/auth-commands.d.ts +0 -5
- package/dist/src/auth-commands.js +0 -1
- package/dist/src/automation-runner.d.ts +0 -9
- package/dist/src/automation-runner.js +0 -11
- package/dist/src/automation-service.d.ts +0 -10
- package/dist/src/automation-service.js +3 -17
- package/dist/src/automation-tool.js +0 -5
- package/dist/src/automation-worker-shutdown.d.ts +0 -1
- package/dist/src/automation-worker-shutdown.js +0 -3
- package/dist/src/automations.d.ts +23 -2
- package/dist/src/automations.js +91 -12
- package/dist/src/background-tasks.d.ts +0 -23
- package/dist/src/background-tasks.js +0 -11
- package/dist/src/boot-trace.d.ts +0 -13
- package/dist/src/boot-trace.js +1 -14
- package/dist/src/browser-activity.js +0 -2
- package/dist/src/browser-bridge.d.ts +0 -2
- package/dist/src/browser-bridge.js +0 -27
- package/dist/src/browser-captcha-tool.d.ts +0 -13
- package/dist/src/browser-captcha-tool.js +3 -35
- package/dist/src/browser-extensions-tool.d.ts +24 -0
- package/dist/src/browser-extensions-tool.js +137 -0
- package/dist/src/browser-extensions.d.ts +127 -0
- package/dist/src/browser-extensions.js +729 -0
- package/dist/src/browser-observation.d.ts +0 -15
- package/dist/src/browser-observation.js +1 -22
- package/dist/src/browser-panel-tool.d.ts +1 -37
- package/dist/src/browser-panel-tool.js +3 -73
- package/dist/src/browser-runner.d.ts +0 -1
- package/dist/src/browser-runner.js +5 -46
- package/dist/src/browser-server.d.ts +0 -6
- package/dist/src/browser-server.js +6 -229
- package/dist/src/browser-session.d.ts +0 -25
- package/dist/src/browser-session.js +1 -71
- package/dist/src/browser-stealth.d.ts +19 -13
- package/dist/src/browser-stealth.js +22 -38
- package/dist/src/browser-ui/app.js +7 -174
- package/dist/src/browser-ui/style.css +0 -68
- package/dist/src/browser-ui/tokens.css +15 -97
- package/dist/src/browser-view.d.ts +106 -295
- package/dist/src/browser-view.js +551 -587
- package/dist/src/capabilities.d.ts +0 -8
- package/dist/src/capabilities.js +0 -29
- package/dist/src/capsolver.d.ts +0 -13
- package/dist/src/capsolver.js +0 -15
- package/dist/src/clarification-ui.d.ts +0 -7
- package/dist/src/clarification-ui.js +0 -8
- package/dist/src/claude-executable.d.ts +0 -1
- package/dist/src/claude-executable.js +1 -2
- package/dist/src/claude-provider.d.ts +6 -19
- package/dist/src/claude-provider.js +155 -63
- package/dist/src/cli-executable.d.ts +0 -16
- package/dist/src/cli-executable.js +0 -7
- package/dist/src/cli-process.d.ts +0 -26
- package/dist/src/cli-process.js +0 -18
- package/dist/src/cli-sign-in.d.ts +0 -20
- package/dist/src/cli-sign-in.js +1 -17
- package/dist/src/cli.d.ts +1 -55
- package/dist/src/cli.js +118 -742
- package/dist/src/code-intelligence/CodeIntelligenceManager.d.ts +4 -59
- package/dist/src/code-intelligence/CodeIntelligenceManager.js +26 -99
- package/dist/src/code-intelligence/ContextLedger.d.ts +0 -4
- package/dist/src/code-intelligence/ContextLedger.js +0 -10
- package/dist/src/code-intelligence/ContextRouter.d.ts +0 -6
- package/dist/src/code-intelligence/ContextRouter.js +0 -8
- package/dist/src/code-intelligence/codegraph/CodeGraphProjectSession.d.ts +0 -2
- package/dist/src/code-intelligence/codegraph/CodeGraphProjectSession.js +5 -12
- package/dist/src/code-intelligence/codegraph/CodeGraphProvider.d.ts +0 -6
- package/dist/src/code-intelligence/codegraph/CodeGraphProvider.js +0 -6
- package/dist/src/code-intelligence/codegraph/CodeGraphSyncBarrier.d.ts +0 -5
- package/dist/src/code-intelligence/codegraph/CodeGraphSyncBarrier.js +1 -17
- package/dist/src/code-intelligence/codegraph/repoExploreTool.d.ts +0 -5
- package/dist/src/code-intelligence/codegraph/repoExploreTool.js +0 -5
- package/dist/src/code-intelligence/repository.d.ts +0 -9
- package/dist/src/code-intelligence/repository.js +1 -11
- package/dist/src/code-intelligence/types.d.ts +0 -8
- package/dist/src/codex-auth.d.ts +0 -44
- package/dist/src/codex-auth.js +0 -45
- package/dist/src/codex-browser-auth.d.ts +0 -13
- package/dist/src/codex-browser-auth.js +3 -20
- package/dist/src/codex-errors.js +0 -2
- package/dist/src/codex-executable.d.ts +0 -2
- package/dist/src/codex-executable.js +1 -7
- package/dist/src/codex-provider.d.ts +0 -13
- package/dist/src/codex-provider.js +0 -28
- package/dist/src/codex-rpc.d.ts +0 -10
- package/dist/src/codex-rpc.js +0 -12
- package/dist/src/coding-tools.d.ts +4 -26
- package/dist/src/coding-tools.js +161 -181
- package/dist/src/command-metrics.d.ts +0 -4
- package/dist/src/command-output-tap.d.ts +0 -11
- package/dist/src/command-output-tap.js +0 -3
- package/dist/src/command-screens.d.ts +41 -1
- package/dist/src/command-screens.js +316 -113
- package/dist/src/commands.d.ts +12 -63
- package/dist/src/commands.js +98 -127
- package/dist/src/completion-gates.d.ts +0 -47
- package/dist/src/completion-gates.js +0 -14
- package/dist/src/completion.d.ts +0 -98
- package/dist/src/completion.js +2 -239
- package/dist/src/config.d.ts +15 -115
- package/dist/src/config.js +123 -172
- package/dist/src/context-budget.d.ts +0 -22
- package/dist/src/context-budget.js +0 -44
- package/dist/src/context-compaction.d.ts +29 -6
- package/dist/src/context-compaction.js +94 -25
- package/dist/src/context-selection.d.ts +0 -24
- package/dist/src/context-selection.js +0 -34
- package/dist/src/dashboard-view.d.ts +2 -2
- package/dist/src/dashboard-view.js +11 -37
- package/dist/src/dashboard.d.ts +0 -2
- package/dist/src/dashboard.js +0 -9
- package/dist/src/deadline.d.ts +0 -45
- package/dist/src/deadline.js +0 -54
- package/dist/src/decision/ContinuationAdapter.d.ts +0 -5
- package/dist/src/decision/ContinuationAdapter.js +0 -5
- package/dist/src/decision/DecisionEngine.d.ts +0 -4
- package/dist/src/decision/DecisionManager.d.ts +0 -1
- package/dist/src/decision/DecisionManager.js +2 -7
- package/dist/src/decision/DecisionPolicy.d.ts +0 -1
- package/dist/src/decision/DecisionPolicy.js +0 -1
- package/dist/src/decision/FactContinuations.d.ts +11 -43
- package/dist/src/decision/FactContinuations.js +126 -95
- package/dist/src/decision/FactServiceGate.d.ts +0 -1
- package/dist/src/decision/FactServiceGate.js +0 -4
- package/dist/src/decision/PreparedSteps.d.ts +0 -15
- package/dist/src/decision/PreparedSteps.js +1 -11
- package/dist/src/decision/applyToolMenu.d.ts +0 -1
- package/dist/src/decision/applyToolMenu.js +2 -6
- package/dist/src/decision/checks.d.ts +15 -28
- package/dist/src/decision/checks.js +118 -49
- package/dist/src/decision/facts.d.ts +0 -16
- package/dist/src/decision/facts.js +0 -30
- package/dist/src/decision/guidancePolicy.js +0 -1
- package/dist/src/decision/hostedPolicy.d.ts +0 -6
- package/dist/src/decision/hostedPolicy.js +0 -7
- package/dist/src/decision/hostedSkills.d.ts +0 -19
- package/dist/src/decision/hostedSkills.js +0 -19
- package/dist/src/decision/optimizationPolicy.d.ts +0 -3
- package/dist/src/decision/optimizationPolicy.js +1 -4
- package/dist/src/decision/providers/AndorraDecisionProvider.d.ts +0 -7
- package/dist/src/decision/providers/AndorraDecisionProvider.js +4 -27
- package/dist/src/decision/providers/HttpDecisionProvider.d.ts +0 -2
- package/dist/src/decision/providers/HttpDecisionProvider.js +0 -6
- package/dist/src/decision/routeTools.d.ts +0 -8
- package/dist/src/decision/routeTools.js +1 -8
- package/dist/src/decision/toolFamilies.d.ts +0 -1
- package/dist/src/decision/toolFamilies.js +1 -2
- package/dist/src/decision/toolMenu.d.ts +0 -3
- package/dist/src/decision/toolMenu.js +0 -4
- package/dist/src/decision/types.d.ts +1 -6
- package/dist/src/default-graph.d.ts +0 -9
- package/dist/src/default-graph.js +0 -9
- package/dist/src/delegation-changes.d.ts +0 -56
- package/dist/src/delegation-changes.js +4 -130
- package/dist/src/diff-preview.d.ts +0 -1
- package/dist/src/diff-preview.js +0 -17
- package/dist/src/duration.d.ts +0 -50
- package/dist/src/duration.js +0 -50
- package/dist/src/efficiency.d.ts +0 -39
- package/dist/src/efficiency.js +0 -16
- package/dist/src/engine/protocol.d.ts +0 -2
- package/dist/src/engine/protocol.js +0 -2
- package/dist/src/engine-streams.d.ts +0 -9
- package/dist/src/extensions.d.ts +3 -2
- package/dist/src/extensions.js +63 -35
- package/dist/src/favicons.d.ts +0 -26
- package/dist/src/favicons.js +0 -34
- package/dist/src/file-diff.d.ts +2 -58
- package/dist/src/file-diff.js +0 -32
- package/dist/src/file-read-key.d.ts +0 -7
- package/dist/src/file-read-key.js +0 -7
- package/dist/src/file-read-state.d.ts +0 -1
- package/dist/src/file-read-state.js +0 -4
- package/dist/src/firefox-cookies.d.ts +0 -34
- package/dist/src/firefox-cookies.js +1 -55
- package/dist/src/git-identity-audit.d.ts +27 -0
- package/dist/src/git-identity-audit.js +286 -0
- package/dist/src/git-identity-guard.d.ts +4 -0
- package/dist/src/git-identity-guard.js +998 -0
- package/dist/src/git-identity-hold.d.ts +12 -0
- package/dist/src/git-identity-hold.js +81 -0
- package/dist/src/git-summary.d.ts +0 -58
- package/dist/src/git-summary.js +2 -212
- package/dist/src/goal-completion-verification.d.ts +2 -2
- package/dist/src/goal-completion-verification.js +3 -6
- package/dist/src/graph-account.d.ts +0 -21
- package/dist/src/graph-account.js +0 -17
- package/dist/src/graph-editor-session.d.ts +0 -1
- package/dist/src/graph-editor-session.js +0 -3
- package/dist/src/graph-health.d.ts +9 -61
- package/dist/src/graph-health.js +34 -59
- package/dist/src/graph-manager-chat.d.ts +36 -153
- package/dist/src/graph-manager-chat.js +161 -182
- package/dist/src/graph-provider-portability.d.ts +1 -21
- package/dist/src/graph-provider-portability.js +6 -45
- package/dist/src/graph-quota.d.ts +0 -33
- package/dist/src/graph-quota.js +1 -44
- package/dist/src/graph-runner.d.ts +0 -1
- package/dist/src/graph-runner.js +58 -391
- package/dist/src/graph-selection.d.ts +0 -14
- package/dist/src/graph-selection.js +2 -15
- package/dist/src/graph-server.d.ts +2 -10
- package/dist/src/graph-server.js +39 -101
- package/dist/src/graph-studio-features.d.ts +0 -5
- package/dist/src/graph-studio-features.js +2 -42
- package/dist/src/graph-sync.d.ts +0 -78
- package/dist/src/graph-sync.js +5 -201
- package/dist/src/graph-tool.d.ts +0 -18
- package/dist/src/graph-tool.js +0 -27
- package/dist/src/graph-ui/app.js +176 -492
- package/dist/src/graph-ui/compat.js +0 -7
- package/dist/src/graph-ui/controls.css +0 -48
- package/dist/src/graph-ui/controls.js +0 -88
- package/dist/src/graph-ui/drafts.js +0 -12
- package/dist/src/graph-ui/edge-router.js +0 -62
- package/dist/src/graph-ui/embed.js +0 -59
- package/dist/src/graph-ui/icons.js +0 -8
- package/dist/src/graph-ui/library.css +0 -5
- package/dist/src/graph-ui/manager-markdown.js +0 -11
- package/dist/src/graph-ui/manager-state.js +1 -78
- package/dist/src/graph-ui/manager.css +0 -18
- package/dist/src/graph-ui/manager.js +43 -185
- package/dist/src/graph-ui/model-picker.js +0 -29
- package/dist/src/graph-ui/sidebar.js +2 -3
- package/dist/src/graph-ui/skill-picker.js +373 -39
- package/dist/src/graph-ui/studio-canvas.js +3 -38
- package/dist/src/graph-ui/studio.css +0 -18
- package/dist/src/graph-ui/studio.js +3 -71
- package/dist/src/graph-ui/style.css +16 -136
- package/dist/src/graph-ui/theme.js +1 -26
- package/dist/src/graph-ui/tokens.css +19 -118
- package/dist/src/graphs.d.ts +6 -157
- package/dist/src/graphs.js +66 -269
- package/dist/src/grep-regex-worker.js +0 -2
- package/dist/src/grep-regex.d.ts +1 -5
- package/dist/src/grep-regex.js +6 -8
- package/dist/src/grok-provider.d.ts +0 -17
- package/dist/src/grok-provider.js +1 -26
- package/dist/src/handoffs.d.ts +0 -11
- package/dist/src/handoffs.js +0 -13
- package/dist/src/hardened-runtime.d.ts +0 -10
- package/dist/src/hardened-runtime.js +9 -47
- package/dist/src/harness-home.d.ts +21 -0
- package/dist/src/harness-home.js +87 -0
- package/dist/src/host-process-guard.d.ts +0 -50
- package/dist/src/host-process-guard.js +0 -641
- package/dist/src/limits.d.ts +0 -64
- package/dist/src/limits.js +0 -62
- package/dist/src/managed-sign-in.d.ts +0 -12
- package/dist/src/managed-sign-in.js +0 -16
- package/dist/src/markdown.d.ts +0 -28
- package/dist/src/markdown.js +1 -175
- package/dist/src/mcp.d.ts +0 -99
- package/dist/src/mcp.js +4 -170
- package/dist/src/membership-errors.d.ts +0 -14
- package/dist/src/membership-errors.js +1 -14
- package/dist/src/membership.d.ts +9 -59
- package/dist/src/membership.js +20 -91
- package/dist/src/memory/LocalToolMemory.d.ts +0 -1
- package/dist/src/memory/LocalToolMemory.js +2 -7
- package/dist/src/memory/MemoryDistiller.js +0 -15
- package/dist/src/memory/MemoryManager.d.ts +0 -3
- package/dist/src/memory/MemoryManager.js +6 -18
- package/dist/src/memory/MemoryPolicy.d.ts +0 -2
- package/dist/src/memory/MemoryPolicy.js +0 -2
- package/dist/src/memory/MemoryProvider.d.ts +0 -6
- package/dist/src/memory/MemoryRetriever.js +0 -7
- package/dist/src/memory/PermissionMemoryProvider.d.ts +0 -1
- package/dist/src/memory/PermissionMemoryProvider.js +0 -1
- package/dist/src/memory/ProjectMemory.d.ts +5 -40
- package/dist/src/memory/ProjectMemory.js +22 -47
- package/dist/src/memory/ToolFailureLessons.d.ts +0 -3
- package/dist/src/memory/ToolFailureLessons.js +0 -6
- package/dist/src/memory/providers/NoOpMemoryProvider.d.ts +0 -1
- package/dist/src/memory/providers/NoOpMemoryProvider.js +0 -1
- package/dist/src/memory/redaction.d.ts +0 -5
- package/dist/src/memory/redaction.js +0 -11
- package/dist/src/memory/repository.d.ts +0 -6
- package/dist/src/memory/repository.js +2 -12
- package/dist/src/memory/types.d.ts +0 -1
- package/dist/src/model-context-windows.d.ts +2 -0
- package/dist/src/model-context-windows.js +24 -0
- package/dist/src/model-health-cache.d.ts +0 -38
- package/dist/src/model-health-cache.js +2 -50
- package/dist/src/model-metadata.d.ts +1 -18
- package/dist/src/model-metadata.js +42 -45
- package/dist/src/model-override.d.ts +0 -36
- package/dist/src/model-override.js +0 -43
- package/dist/src/model-tiers.d.ts +0 -13
- package/dist/src/model-tiers.js +0 -23
- package/dist/src/opencode-catalog.d.ts +0 -2
- package/dist/src/opencode-catalog.js +0 -11
- package/dist/src/opencode-cli-provider.d.ts +0 -35
- package/dist/src/opencode-cli-provider.js +1 -44
- package/dist/src/opencode-provider.d.ts +0 -2
- package/dist/src/opencode-provider.js +1 -16
- package/dist/src/package-name.d.ts +0 -2
- package/dist/src/package-name.js +0 -2
- package/dist/src/paged-transcript.d.ts +0 -11
- package/dist/src/paged-transcript.js +2 -20
- package/dist/src/palette.d.ts +1 -63
- package/dist/src/palette.js +1 -64
- package/dist/src/permissions.d.ts +0 -15
- package/dist/src/permissions.js +17 -21
- package/dist/src/powershell-pool.d.ts +0 -9
- package/dist/src/powershell-pool.js +3 -26
- package/dist/src/process-warnings.d.ts +4 -0
- package/dist/src/process-warnings.js +103 -0
- package/dist/src/profile.d.ts +0 -43
- package/dist/src/profile.js +0 -43
- package/dist/src/project-reservation.d.ts +11 -0
- package/dist/src/project-reservation.js +143 -0
- package/dist/src/provider-connection.d.ts +0 -18
- package/dist/src/provider-connection.js +15 -39
- package/dist/src/provider-engine.d.ts +0 -21
- package/dist/src/provider-engine.js +1 -96
- package/dist/src/provider-import.d.ts +0 -49
- package/dist/src/provider-import.js +0 -60
- package/dist/src/provider-keys.d.ts +1 -36
- package/dist/src/provider-keys.js +5 -48
- package/dist/src/provider-settings.d.ts +16 -3
- package/dist/src/provider-settings.js +36 -52
- package/dist/src/providers.d.ts +14 -219
- package/dist/src/providers.js +69 -326
- package/dist/src/pull-requests.d.ts +0 -173
- package/dist/src/pull-requests.js +5 -195
- package/dist/src/qa/browser/PlaywrightRunner.d.ts +0 -7
- package/dist/src/qa/browser/PlaywrightRunner.js +2 -16
- package/dist/src/qa/command-policy.d.ts +0 -23
- package/dist/src/qa/command-policy.js +0 -30
- package/dist/src/qa/guidance.d.ts +0 -21
- package/dist/src/qa/guidance.js +0 -20
- package/dist/src/qa/profile/ProjectInspector.d.ts +0 -4
- package/dist/src/qa/profile/ProjectInspector.js +0 -6
- package/dist/src/qa/profile/QaConfigLoader.d.ts +0 -7
- package/dist/src/qa/profile/QaConfigLoader.js +0 -6
- package/dist/src/qa/profile/QaProfile.d.ts +0 -7
- package/dist/src/qa/profile/QaProfile.js +0 -7
- package/dist/src/qa/profile/yaml.d.ts +0 -9
- package/dist/src/qa/profile/yaml.js +0 -13
- package/dist/src/qa/qa-authorize.d.ts +0 -19
- package/dist/src/qa/qa-authorize.js +4 -11
- package/dist/src/qa/runtime/ProcessManager.d.ts +0 -31
- package/dist/src/qa/runtime/ProcessManager.js +4 -41
- package/dist/src/qa/runtime/PtyManager.d.ts +0 -4
- package/dist/src/qa/runtime/PtyManager.js +1 -7
- package/dist/src/qa/runtime/changes.d.ts +0 -11
- package/dist/src/qa/runtime/changes.js +1 -3
- package/dist/src/qa/tools.d.ts +0 -10
- package/dist/src/qa/tools.js +0 -12
- package/dist/src/qa/types.d.ts +0 -13
- package/dist/src/react-runtime.d.ts +2 -21
- package/dist/src/react-runtime.js +107 -422
- package/dist/src/read-document-doc.d.ts +0 -6
- package/dist/src/read-document-doc.js +0 -55
- package/dist/src/read-document-docx.d.ts +0 -16
- package/dist/src/read-document-docx.js +2 -69
- package/dist/src/read-document-text.d.ts +0 -17
- package/dist/src/read-document-text.js +0 -22
- package/dist/src/read-document-tool.d.ts +0 -31
- package/dist/src/read-document-tool.js +0 -30
- package/dist/src/read-document.d.ts +0 -49
- package/dist/src/read-document.js +1 -67
- package/dist/src/read-image.d.ts +0 -32
- package/dist/src/read-image.js +0 -33
- package/dist/src/remote/cert.d.ts +0 -18
- package/dist/src/remote/cert.js +0 -44
- package/dist/src/remote/client.d.ts +0 -70
- package/dist/src/remote/client.js +1 -116
- package/dist/src/remote/command.js +0 -17
- package/dist/src/remote/engine-pool.d.ts +0 -152
- package/dist/src/remote/engine-pool.js +22 -339
- package/dist/src/remote/errors.d.ts +0 -15
- package/dist/src/remote/errors.js +0 -7
- package/dist/src/remote/network.d.ts +0 -26
- package/dist/src/remote/network.js +1 -34
- package/dist/src/remote/pairing.d.ts +16 -51
- package/dist/src/remote/pairing.js +50 -57
- package/dist/src/remote/pem.d.ts +0 -4
- package/dist/src/remote/pem.js +0 -4
- package/dist/src/remote/roots.d.ts +0 -20
- package/dist/src/remote/roots.js +0 -8
- package/dist/src/remote/serve.d.ts +0 -47
- package/dist/src/remote/serve.js +0 -30
- package/dist/src/remote/server.d.ts +0 -14
- package/dist/src/remote/server.js +1 -79
- package/dist/src/remote/types.d.ts +1 -32
- package/dist/src/remote/types.js +0 -10
- package/dist/src/run-control.d.ts +2 -9
- package/dist/src/run-control.js +9 -13
- package/dist/src/run-progress.d.ts +0 -170
- package/dist/src/run-progress.js +0 -285
- package/dist/src/run-records.d.ts +0 -443
- package/dist/src/run-records.js +14 -687
- package/dist/src/run-temp.d.ts +12 -0
- package/dist/src/run-temp.js +50 -0
- package/dist/src/runtime.d.ts +12 -228
- package/dist/src/runtime.js +2 -128
- package/dist/src/sandbox-child.js +0 -1
- package/dist/src/save-lock.d.ts +0 -12
- package/dist/src/save-lock.js +0 -28
- package/dist/src/screen-selection.d.ts +0 -23
- package/dist/src/screen-selection.js +0 -22
- package/dist/src/secret-redaction.d.ts +0 -12
- package/dist/src/secret-redaction.js +0 -45
- package/dist/src/session-changes.d.ts +4 -80
- package/dist/src/session-changes.js +25 -130
- package/dist/src/session-goal.d.ts +129 -0
- package/dist/src/session-goal.js +230 -0
- package/dist/src/session-monitor.d.ts +1 -10
- package/dist/src/session-monitor.js +2 -39
- package/dist/src/session-reaper.d.ts +0 -15
- package/dist/src/session-reaper.js +0 -14
- package/dist/src/session-skills.d.ts +15 -44
- package/dist/src/session-skills.js +75 -60
- package/dist/src/session-summary.d.ts +0 -26
- package/dist/src/session-summary.js +0 -32
- package/dist/src/session-title.d.ts +0 -1
- package/dist/src/session-title.js +0 -2
- package/dist/src/sessions.d.ts +3 -122
- package/dist/src/sessions.js +9 -178
- package/dist/src/shell.d.ts +3 -20
- package/dist/src/shell.js +8 -72
- package/dist/src/side-panel.d.ts +5 -99
- package/dist/src/side-panel.js +76 -133
- package/dist/src/sign-in-detection.d.ts +0 -39
- package/dist/src/sign-in-detection.js +0 -43
- package/dist/src/sign-in-offer.d.ts +1 -19
- package/dist/src/sign-in-offer.js +0 -13
- package/dist/src/skill-commands.d.ts +0 -1
- package/dist/src/skill-commands.js +1 -4
- package/dist/src/skills/bundled/control-browser/SKILL.md +1 -1
- package/dist/src/skills/bundled/graph-manager/errors.md +1 -1
- package/dist/src/skills/bundled/provenance.json +738 -738
- package/dist/src/skills/invocable-skills.d.ts +0 -18
- package/dist/src/skills/invocable-skills.js +0 -17
- package/dist/src/skills/project-skills.d.ts +0 -1
- package/dist/src/skills/project-skills.js +0 -3
- package/dist/src/skills/prompt-skills.js +0 -14
- package/dist/src/skills/runtime-skills.d.ts +0 -13
- package/dist/src/skills/runtime-skills.js +0 -37
- package/dist/src/stall-recovery.d.ts +0 -25
- package/dist/src/stall-recovery.js +0 -38
- package/dist/src/stray-files.d.ts +0 -9
- package/dist/src/stray-files.js +0 -16
- package/dist/src/structured-step.d.ts +0 -22
- package/dist/src/structured-step.js +0 -22
- package/dist/src/subagent-run.d.ts +2 -163
- package/dist/src/subagent-run.js +18 -376
- package/dist/src/subagent-tree.d.ts +32 -0
- package/dist/src/subagent-tree.js +128 -0
- package/dist/src/telemetry/aggregate.d.ts +0 -37
- package/dist/src/telemetry/aggregate.js +0 -40
- package/dist/src/telemetry/capture-core.d.ts +0 -87
- package/dist/src/telemetry/capture-core.js +0 -67
- package/dist/src/telemetry/capture.d.ts +0 -1
- package/dist/src/telemetry/capture.js +0 -3
- package/dist/src/telemetry/classify.d.ts +0 -58
- package/dist/src/telemetry/classify.js +0 -60
- package/dist/src/telemetry/decision-capture.d.ts +0 -1
- package/dist/src/telemetry/emitter.d.ts +0 -30
- package/dist/src/telemetry/emitter.js +1 -62
- package/dist/src/telemetry/gate.d.ts +0 -29
- package/dist/src/telemetry/gate.js +0 -22
- package/dist/src/telemetry/identity.d.ts +0 -13
- package/dist/src/telemetry/identity.js +0 -29
- package/dist/src/telemetry/outbox.d.ts +0 -64
- package/dist/src/telemetry/outbox.js +1 -108
- package/dist/src/telemetry/schema.d.ts +1 -49
- package/dist/src/telemetry/schema.js +3 -84
- package/dist/src/telemetry/state.d.ts +0 -53
- package/dist/src/telemetry/state.js +0 -64
- package/dist/src/telemetry/surface.d.ts +0 -31
- package/dist/src/telemetry/surface.js +1 -39
- package/dist/src/telemetry/uploader.d.ts +0 -11
- package/dist/src/telemetry/uploader.js +0 -46
- package/dist/src/telemetry/uploads.d.ts +0 -11
- package/dist/src/telemetry/uploads.js +0 -11
- package/dist/src/telemetry/version.d.ts +0 -4
- package/dist/src/telemetry/version.js +0 -14
- package/dist/src/telemetry/workspace.d.ts +0 -7
- package/dist/src/telemetry/workspace.js +1 -22
- package/dist/src/terminal-ui.d.ts +33 -521
- package/dist/src/terminal-ui.js +145 -884
- package/dist/src/test-edit-guard.d.ts +74 -0
- package/dist/src/test-edit-guard.js +269 -0
- package/dist/src/text.d.ts +0 -1
- package/dist/src/text.js +0 -1
- package/dist/src/time-limit.d.ts +0 -36
- package/dist/src/time-limit.js +0 -37
- package/dist/src/timers.d.ts +0 -161
- package/dist/src/timers.js +0 -126
- package/dist/src/todos.d.ts +0 -1
- package/dist/src/todos.js +0 -3
- package/dist/src/token-rate.d.ts +0 -94
- package/dist/src/token-rate.js +0 -154
- package/dist/src/tool-activity.d.ts +0 -6
- package/dist/src/tool-activity.js +0 -7
- package/dist/src/tool-availability.d.ts +0 -13
- package/dist/src/tool-availability.js +1 -14
- package/dist/src/tool-call-clock.d.ts +1 -4
- package/dist/src/tool-call-clock.js +0 -13
- package/dist/src/tool-catalog.d.ts +0 -11
- package/dist/src/tool-catalog.js +0 -23
- package/dist/src/tool-dependencies.d.ts +0 -5
- package/dist/src/tool-dependencies.js +0 -4
- package/dist/src/tool-diff-stats.d.ts +0 -23
- package/dist/src/tool-diff-stats.js +0 -49
- package/dist/src/tool-errors.d.ts +9 -7
- package/dist/src/tool-errors.js +13 -6
- package/dist/src/tool-failure-tracker.d.ts +3 -11
- package/dist/src/tool-failure-tracker.js +19 -27
- package/dist/src/tool-names.d.ts +0 -16
- package/dist/src/tool-names.js +0 -12
- package/dist/src/tool-profile.d.ts +6 -0
- package/dist/src/tool-profile.js +23 -0
- package/dist/src/tool-result-filter.d.ts +0 -19
- package/dist/src/tool-result-filter.js +1 -66
- package/dist/src/tool-schema-compatibility.d.ts +0 -6
- package/dist/src/tool-schema-compatibility.js +0 -9
- package/dist/src/tools.d.ts +0 -4
- package/dist/src/tools.js +11 -114
- package/dist/src/trajectory.d.ts +0 -13
- package/dist/src/trajectory.js +23 -79
- package/dist/src/transcript.d.ts +0 -41
- package/dist/src/transcript.js +5 -60
- package/dist/src/types.d.ts +4 -168
- package/dist/src/untracked-file-removal.js +0 -20
- package/dist/src/update-check.d.ts +0 -14
- package/dist/src/update-check.js +1 -15
- package/dist/src/update.d.ts +0 -35
- package/dist/src/update.js +0 -100
- package/dist/src/usage-analytics.d.ts +17 -17
- package/dist/src/usage-analytics.js +168 -209
- package/dist/src/web-search.d.ts +0 -23
- package/dist/src/web-search.js +1 -27
- package/dist/src/widgets.d.ts +0 -25
- package/dist/src/widgets.js +0 -24
- package/dist/src/workspace-facts.d.ts +0 -12
- package/dist/src/workspace-facts.js +1 -18
- package/dist/src/workspace-git-metadata.d.ts +0 -16
- package/dist/src/workspace-git-metadata.js +0 -21
- package/dist/src/workspace-instructions.d.ts +3 -15
- package/dist/src/workspace-instructions.js +6 -19
- package/package.json +13 -2
|
@@ -1,40 +1,16 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Refusing a command that would stop the app this engine runs inside.
|
|
3
|
-
*
|
|
4
|
-
* A desktop engine is a child of the desktop shell, and its tools run with the user's own rights,
|
|
5
|
-
* so one command can end the window, the run, and every other run that window hosts. That is not
|
|
6
|
-
* hypothetical: on 2026-11-12 an agent testing the desktop app launched a second copy, the copy
|
|
7
|
-
* handed itself to the window already open (they shared an application identifier), and the agent
|
|
8
|
-
* then stopped every process named `andorra-desktop.exe` to clean up "its own" instance. The only
|
|
9
|
-
* such process was its host.
|
|
10
|
-
*
|
|
11
|
-
* The shell tells the engine who its host is (`ANDORRA_DESKTOP_HOST_PID` and
|
|
12
|
-
* `ANDORRA_DESKTOP_HOST_NAME`, set in `desktop/src-tauri/src/engine.rs`). A command is refused when
|
|
13
|
-
* it both terminates processes and names the host: by its image name, which a by-name kill would
|
|
14
|
-
* match, or by its process id. The engine's own process id is protected the same way. "Terminates"
|
|
15
|
-
* means a kill the shell would actually run, not the words in a quoted search pattern or message.
|
|
16
|
-
*
|
|
17
|
-
* This is a guard against an honest mistake, not a security boundary: a command built at run time
|
|
18
|
-
* or hidden behind a script can still do it. What it buys is that the plain form of the mistake
|
|
19
|
-
* comes back as an instruction (stop the process you started, by its id) instead of a dead window.
|
|
20
|
-
*/
|
|
21
1
|
function positiveInteger(value) {
|
|
22
2
|
if (!value || !/^[0-9]{1,10}$/.test(value.trim()))
|
|
23
3
|
return undefined;
|
|
24
4
|
const number = Number(value.trim());
|
|
25
5
|
return Number.isSafeInteger(number) && number > 0 ? number : undefined;
|
|
26
6
|
}
|
|
27
|
-
/** The host as the shell declared it. Outside the desktop app only the engine's own id is known. */
|
|
28
7
|
export function hostProcess(env = process.env, self = process.pid) {
|
|
29
8
|
const pid = positiveInteger(env.ANDORRA_DESKTOP_HOST_PID);
|
|
30
9
|
const name = env.ANDORRA_DESKTOP_HOST_NAME?.trim();
|
|
31
10
|
return { self, ...(pid === undefined ? {} : { pid }), ...(name ? { name } : {}) };
|
|
32
11
|
}
|
|
33
|
-
/** Programs and cmdlets whose whole job is ending a process, by the name they are invoked under. */
|
|
34
12
|
const KILLERS = new Set(['stop-process', 'spps', 'kill', 'pkill', 'killall', 'taskkill', 'tskill', 'remove-ciminstance', 'remove-wmiobject', 'rcim', 'rwmi']);
|
|
35
|
-
/** The killers that are PowerShell cmdlets, which take `-WhatIf`; `kill` is Stop-Process's alias. */
|
|
36
13
|
const WHATIF_CMDLETS = new Set(['stop-process', 'spps', 'kill', 'remove-ciminstance', 'remove-wmiobject', 'rcim', 'rwmi']);
|
|
37
|
-
/** Whether a cmdlet's arguments switch `-WhatIf` on, and nothing later switches it off. */
|
|
38
14
|
function previewsOnly(args) {
|
|
39
15
|
let preview = false;
|
|
40
16
|
for (const argument of args) {
|
|
@@ -44,18 +20,10 @@ function previewsOnly(args) {
|
|
|
44
20
|
}
|
|
45
21
|
return preview;
|
|
46
22
|
}
|
|
47
|
-
/** Tools that end a process only when asked to: `wmic … delete`, `Invoke-CimMethod … Terminate`. */
|
|
48
23
|
const CONDITIONAL_KILLERS = new Set(['wmic', 'invoke-cimmethod', 'invoke-wmimethod', 'icim', 'iwmi']);
|
|
49
|
-
/** Programs that run their argument as code or as another command line. */
|
|
50
24
|
const INTERPRETERS = new Set(['node', 'nodejs', 'python', 'python3', 'py', 'powershell', 'pwsh', 'bash', 'sh', 'zsh', 'cmd', 'iex', 'invoke-expression', 'eval', 'ruby', 'perl', 'deno', 'bun']);
|
|
51
|
-
/** Words that only introduce the command that follows them. */
|
|
52
25
|
const PREFIXES = new Set(['&', '.', 'sudo', 'doas', 'exec', 'command', 'builtin', 'noglob', 'nocorrect', 'nohup', 'env', 'time', 'timeout', 'xargs', 'call', 'start', 'nice', 'ionice', 'stdbuf', 'setsid',
|
|
53
26
|
'if', 'elseif', 'else', 'then', 'do', 'while', 'until', 'for', 'foreach', 'try', 'catch', 'finally', 'return', '!', 'foreach-object', '%', 'where-object', '?']);
|
|
54
|
-
/**
|
|
55
|
-
* A wrapper's options that take a value, lower-cased: `env -u NAME`, `sudo -u USER`,
|
|
56
|
-
* `timeout -s SIGNAL`. The value is not the wrapped command. Where an option only sometimes takes
|
|
57
|
-
* one, both readings are judged, so getting the table wrong can only refuse more.
|
|
58
|
-
*/
|
|
59
27
|
const WRAPPER_VALUES = {
|
|
60
28
|
env: new Set(['-u', '--unset', '-c', '--chdir']),
|
|
61
29
|
sudo: new Set(['-u', '--user', '-g', '--group', '-c', '--close-from', '-d', '--chdir', '-h', '--host', '-p', '--prompt', '-r', '--role', '-t', '--type', '--other-user', '--command-timeout', '--chroot']),
|
|
@@ -69,60 +37,29 @@ const WRAPPER_VALUES = {
|
|
|
69
37
|
time: new Set(['-f', '--format', '-o', '--output']),
|
|
70
38
|
start: new Set(['/d']),
|
|
71
39
|
};
|
|
72
|
-
/** Start-Process parameters that take a value, other than the program and its arguments. */
|
|
73
40
|
const START_PROCESS_VALUES = /^-(?:workingdirectory|wd|verb|windowstyle|credential|redirectstandard(?:error|input|output)|environment)$/;
|
|
74
|
-
/** A process ended from code: .NET's `.Kill()`, WMI's `.Terminate()`, Node's and Python's kill calls. */
|
|
75
|
-
// JavaScript may call through optional chaining: `process?.kill?.(…)`, `p?.Kill?.()`.
|
|
76
|
-
// Ruby calls without parentheses: `Process.kill 15, 80632`.
|
|
77
|
-
// Python and the others allow blanks around the dot: `os . kill(…)`.
|
|
78
41
|
const KILL_CALL = /\.\s*(?:Kill|Terminate)\s*(?:\?\.\s*)?\(|\bprocess\s*\??\.\s*kill\s*(?:\?\.\s*)?\(|\bProcess\s*\.\s*kill[ \t]+[^\s=]|\bos\s*\.\s*kill\s*\(|\bkillpg\s*\(/i;
|
|
79
|
-
/** The same calls with signal 0, which only asks whether the process exists. */
|
|
80
42
|
const PROBE_CALL = /\b(?:process|os)\s*\??\.\s*kill\s*(?:\?\.\s*)?\([^(),]*,\s*0\s*\)/gi;
|
|
81
|
-
/**
|
|
82
|
-
* Ruby's probe names the signal first: `Process.kill(0, pid)`, `Process.kill 0, pid`. Case-sensitive,
|
|
83
|
-
* since Node's `process.kill(0, 9)` signals process group 0.
|
|
84
|
-
*/
|
|
85
|
-
// Without parentheses the probe's arguments are a comma list of simple values; whatever follows
|
|
86
|
-
// (`and Process.kill 15, pid`) is another expression and is still judged.
|
|
87
43
|
const RUBY_PROBE_CALL = /\bProcess\s*\.\s*kill(?:\s*\(\s*0\s*,[^()]*\)|[ \t]+0[ \t]*,[ \t]*[\w$@.:]+(?:[ \t]*,[ \t]*[\w$@.:]+)*)/g;
|
|
88
|
-
/**
|
|
89
|
-
* The texts derived while judging one command — rendered `printf` and `echo` output, pipeline
|
|
90
|
-
* input, interpreter code. The host's id may appear only there (`printf 'kill %d' 0x13af8`), so the
|
|
91
|
-
* final check searches them too. Set for the length of one top-level check.
|
|
92
|
-
*/
|
|
93
44
|
let evidence;
|
|
94
45
|
function note(text) {
|
|
95
46
|
if (evidence !== undefined && evidence.length < 500)
|
|
96
47
|
evidence.push(text);
|
|
97
48
|
}
|
|
98
|
-
/** Whether code ends a process through a kill call; a signal-0 probe is not one. */
|
|
99
49
|
function callsKill(code) {
|
|
100
50
|
return KILL_CALL.test(code.replace(PROBE_CALL, '').replace(RUBY_PROBE_CALL, ''));
|
|
101
51
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Whether a `kill`, `pkill` or `killall` invocation only probes, by that command's own syntax.
|
|
104
|
-
* Signal 0 delivers nothing; it reports whether the process exists. `kill` and `killall` name a
|
|
105
|
-
* signal with `-s` (and `kill` with `-n`); to `pkill`, `-s` is a session filter, so
|
|
106
|
-
* `pkill -s 0 -f name` signals every match in the caller's session and is no probe.
|
|
107
|
-
*/
|
|
108
52
|
function probesOnly(word, args) {
|
|
109
53
|
const selectors = word === 'kill' ? ['-s', '-n', '--signal'] : word === 'killall' ? ['-s', '--signal'] : ['--signal'];
|
|
110
|
-
// `kill` may be the shell's builtin, which takes the first signal option, or an external program
|
|
111
|
-
// (`/bin/kill`, or run through `env`, `sudo`, `xargs`), which takes the last: `kill -s 0 -s TERM`
|
|
112
|
-
// sends TERM there. It only probes when both readings say signal 0.
|
|
113
54
|
if (word === 'kill')
|
|
114
55
|
return builtinKillProbes(args, selectors) && lastSignal(args, selectors) === '0';
|
|
115
56
|
return lastSignal(args, selectors) === '0';
|
|
116
57
|
}
|
|
117
|
-
/** Whether the shell's builtin `kill` sends signal 0: its first option names the signal. */
|
|
118
58
|
function builtinKillProbes(args, selectors) {
|
|
119
59
|
for (let index = 0; index < args.length; index += 1) {
|
|
120
60
|
const argument = args[index];
|
|
121
|
-
// Past `--` every argument is an operand: `kill -- 80632 -0` signals 80632 and group 0.
|
|
122
61
|
if (argument === '--')
|
|
123
62
|
return false;
|
|
124
|
-
// kill reads its signal from its first option, and its options end at the first operand:
|
|
125
|
-
// `kill -9 -0 80632` sends SIGKILL to group 0 and to 80632, `kill 80632 -0` SIGTERM to both.
|
|
126
63
|
if (selectors.includes(argument))
|
|
127
64
|
return args[index + 1] === '0';
|
|
128
65
|
if (/^--signal=/.test(argument))
|
|
@@ -133,10 +70,6 @@ function builtinKillProbes(args, selectors) {
|
|
|
133
70
|
}
|
|
134
71
|
return false;
|
|
135
72
|
}
|
|
136
|
-
/**
|
|
137
|
-
* The last signal an option-order-free killer is given — pkill, killall, the external kill — which
|
|
138
|
-
* is the one it sends: `pkill -0 --signal TERM -f name` sends TERM.
|
|
139
|
-
*/
|
|
140
73
|
function lastSignal(args, selectors) {
|
|
141
74
|
let signal;
|
|
142
75
|
for (let index = 0; index < args.length; index += 1) {
|
|
@@ -151,32 +84,18 @@ function lastSignal(args, selectors) {
|
|
|
151
84
|
}
|
|
152
85
|
else if (selectors.some(selector => selector.length === 2 && argument.startsWith(selector) && argument.length > 2))
|
|
153
86
|
signal = argument.slice(2);
|
|
154
|
-
// `-9`, `-0`, `-TERM`, `-SIGKILL`; a single capital letter is another option (`-P`, `-U`).
|
|
155
87
|
else if (/^-(?:\d+|(?:SIG)?[A-Z]{2,}[0-9+-]*)$/.test(argument))
|
|
156
88
|
signal = argument.slice(1);
|
|
157
89
|
}
|
|
158
90
|
return signal;
|
|
159
91
|
}
|
|
160
|
-
/**
|
|
161
|
-
* A string literal handed to something that runs it: `execSync('…')`, `spawn('…'`,
|
|
162
|
-
* `subprocess.run('…'` or `run(['…'`, `os.system('…')`. The literal is the first argument, or the
|
|
163
|
-
* first element of an array that is. The capture is the literal's slot in the scanned text.
|
|
164
|
-
*/
|
|
165
|
-
// Python may pass the command by keyword, after other keywords: `subprocess.run(args=["kill", …])`.
|
|
166
92
|
const RUNS_LITERAL = /(\.?)\b(exec|execSync|execFile|execFileSync|spawn|spawnSync|system|popen|Popen|run|call|check_call|check_output|getoutput|shell_exec|passthru)\s*(?:\?\.\s*)?\(\s*(?:[^()]*?\bargs\s*=\s*)?([[(]?)\s*\u0000(\d+)\u0000/g;
|
|
167
|
-
/** The names [RUNS_LITERAL] reads as launchers, whole. */
|
|
168
93
|
const LAUNCHER_NAMES = /^(?:exec|execSync|execFile|execFileSync|spawn|spawnSync|system|popen|Popen|run|call|check_call|check_output|getoutput|shell_exec|passthru)$/;
|
|
169
|
-
/** JavaScript's evaluators of a string: `eval`, `Function`, `vm.runIn…Context`, string timers. */
|
|
170
94
|
const JS_EVAL = /\b(?:eval|Function|runInThisContext|runInNewContext|runInContext|compileFunction|setTimeout|setInterval|setImmediate)\s*(?:\?\.\s*)?\(/g;
|
|
171
|
-
/** The slot of the call's first argument when that argument is a single literal, or -1. */
|
|
172
95
|
function firstArgumentSlot(text, start) {
|
|
173
96
|
const first = /^\s*\u0000(\d+)\u0000\s*(?:[,)])/.exec(text.slice(start));
|
|
174
97
|
return first ? Number(first[1]) : -1;
|
|
175
98
|
}
|
|
176
|
-
/**
|
|
177
|
-
* The slots of the literal arguments of the call whose argument list starts at `start` (just past
|
|
178
|
-
* its `(`), at the call's own level: `Function('x', 'body')` gives both.
|
|
179
|
-
*/
|
|
180
99
|
function callLiterals(text, start) {
|
|
181
100
|
const slots = [];
|
|
182
101
|
let depth = 1;
|
|
@@ -196,20 +115,9 @@ function callLiterals(text, start) {
|
|
|
196
115
|
}
|
|
197
116
|
return slots;
|
|
198
117
|
}
|
|
199
|
-
/** Python's, Ruby's and Perl's: `exec`, `eval`, `compile`, `instance_eval`, Perl's `eval '…'`. */
|
|
200
118
|
const HASH_EVAL = /\b(?:exec|eval|instance_eval|class_eval|module_eval)\s*\(?\s*\u0000(\d+)\u0000/g;
|
|
201
|
-
/** Python's `compile('…')`, which only builds a code object: its source runs once `exec` or `eval` is called. */
|
|
202
119
|
const PY_COMPILE = /\bcompile\s*\(\s*\u0000(\d+)\u0000/g;
|
|
203
|
-
/** A method looked up by a string: `process['kill'](…)`. The capture is the literal's slot. */
|
|
204
120
|
const INDEXED_CALL = /\[\s*\u0000(\d+)\u0000\s*\]\s*(?:\?\.\s*)?\(([^()]*)\)?/g;
|
|
205
|
-
/**
|
|
206
|
-
* Whether a call matched by [RUNS_LITERAL] starts a process, as far as its name and receiver say.
|
|
207
|
-
*
|
|
208
|
-
* Most of the names only ever mean that. Three are shared with unrelated APIs and need more:
|
|
209
|
-
* `exec` is also `RegExp.prototype.exec`, so as a method it counts only in code that uses
|
|
210
|
-
* `child_process` and not on a regular-expression literal; `run` and `call` count only in code
|
|
211
|
-
* that uses `subprocess`.
|
|
212
|
-
*/
|
|
213
121
|
function startsProcess(name, isMethod, before, code, regexes) {
|
|
214
122
|
if (name === 'exec') {
|
|
215
123
|
if (!isMethod)
|
|
@@ -221,11 +129,9 @@ function startsProcess(name, isMethod, before, code, regexes) {
|
|
|
221
129
|
return /\bsubprocess\b/.test(code);
|
|
222
130
|
return true;
|
|
223
131
|
}
|
|
224
|
-
/** The shell a tool's [ShellKind] runs: PowerShell, or a POSIX shell (Bash or `/bin/sh`). */
|
|
225
132
|
export function hostShellOf(kind) {
|
|
226
133
|
return kind === 'powershell' ? 'powershell' : 'posix';
|
|
227
134
|
}
|
|
228
|
-
/** The rules an interpreter reads its code by. */
|
|
229
135
|
function dialectOf(word) {
|
|
230
136
|
if (word === 'node' || word === 'nodejs' || word === 'deno' || word === 'bun')
|
|
231
137
|
return 'js';
|
|
@@ -241,30 +147,21 @@ function dialectOf(word) {
|
|
|
241
147
|
return 'cmd';
|
|
242
148
|
return 'posix';
|
|
243
149
|
}
|
|
244
|
-
/** The character that escapes the next one: Bash's backslash, PowerShell's backtick, cmd's caret. */
|
|
245
150
|
function escapeOf(dialect) {
|
|
246
151
|
return dialect === 'powershell' ? '`' : dialect === 'cmd' ? '^' : '\\';
|
|
247
152
|
}
|
|
248
|
-
/** Whether the escape character works outside quotes: in the shells, not in program code. */
|
|
249
153
|
function escapesOutside(dialect) {
|
|
250
154
|
return dialect === 'posix' || dialect === 'powershell' || dialect === 'cmd';
|
|
251
155
|
}
|
|
252
|
-
/**
|
|
253
|
-
* The index of the quote that closes the span opening at `index`, the text's length when it never
|
|
254
|
-
* closes, or `undefined` when the character there opens no span in this dialect.
|
|
255
|
-
*/
|
|
256
156
|
function quoteEnd(text, index, dialect) {
|
|
257
157
|
const quote = text[index];
|
|
258
158
|
const opens = quote === '"' || (quote === "'" && dialect !== 'cmd') || (quote === '`' && dialect === 'js');
|
|
259
159
|
if (!opens)
|
|
260
160
|
return undefined;
|
|
261
|
-
// Bash escapes only in "…" and $'…'; PowerShell only in "…"; cmd never; program code always.
|
|
262
161
|
const escapes = dialect === 'js' || dialect === 'hash' || dialect === 'ruby' || dialect === 'perl' ? true
|
|
263
162
|
: dialect === 'posix' ? quote === '"' || text[index - 1] === '$'
|
|
264
163
|
: dialect === 'powershell' ? quote === '"' : false;
|
|
265
164
|
const escape = escapeOf(dialect);
|
|
266
|
-
// Python's `"""…"""` and `'''…'''` hold single quotes of the same kind; the span ends at the
|
|
267
|
-
// closing triple, and the index returned is its last quote.
|
|
268
165
|
if (dialect === 'hash' && (quote === '"' || quote === "'") && text.startsWith(quote.repeat(3), index)) {
|
|
269
166
|
const closing = quote.repeat(3);
|
|
270
167
|
let at = index + 3;
|
|
@@ -287,19 +184,16 @@ function quoteEnd(text, index, dialect) {
|
|
|
287
184
|
continue;
|
|
288
185
|
}
|
|
289
186
|
if (character === quote) {
|
|
290
|
-
// PowerShell doubles a quote to put one inside the same kind of string.
|
|
291
187
|
if (dialect === 'powershell' && text[end + 1] === quote) {
|
|
292
188
|
end += 2;
|
|
293
189
|
continue;
|
|
294
190
|
}
|
|
295
191
|
return end;
|
|
296
192
|
}
|
|
297
|
-
// A template literal's `${ … }` is code, and may hold quotes and templates of its own.
|
|
298
193
|
if (quote === '`' && character === '$' && text[end + 1] === '{') {
|
|
299
194
|
end = braceEnd(text, end + 1, dialect);
|
|
300
195
|
continue;
|
|
301
196
|
}
|
|
302
|
-
// So is Bash's `$( … )` inside "…": its quotes nest, so `"$(echo "a)b")"` is one string.
|
|
303
197
|
if (dialect === 'posix' && quote === '"' && character === '$' && text[end + 1] === '(') {
|
|
304
198
|
end = parenEnd(text, end + 1);
|
|
305
199
|
continue;
|
|
@@ -308,7 +202,6 @@ function quoteEnd(text, index, dialect) {
|
|
|
308
202
|
}
|
|
309
203
|
return text.length;
|
|
310
204
|
}
|
|
311
|
-
/** The index just past the `)` that closes the `(` at `open` in a POSIX line, skipping quoted spans inside. */
|
|
312
205
|
function parenEnd(text, open) {
|
|
313
206
|
let depth = 0;
|
|
314
207
|
let index = open;
|
|
@@ -336,7 +229,6 @@ function parenEnd(text, open) {
|
|
|
336
229
|
}
|
|
337
230
|
return text.length;
|
|
338
231
|
}
|
|
339
|
-
/** The index just past the `}` that closes the `{` at `open`, skipping quoted spans inside. */
|
|
340
232
|
function braceEnd(text, open, dialect) {
|
|
341
233
|
let depth = 0;
|
|
342
234
|
let index = open;
|
|
@@ -360,18 +252,14 @@ function braceEnd(text, open, dialect) {
|
|
|
360
252
|
}
|
|
361
253
|
return text.length;
|
|
362
254
|
}
|
|
363
|
-
/** Whether a `/` can open a regular-expression literal here, judged by the code before it. */
|
|
364
255
|
function regexMayStart(before) {
|
|
365
256
|
const trimmed = before.trimEnd();
|
|
366
257
|
if (trimmed === '')
|
|
367
258
|
return true;
|
|
368
259
|
if (/[(,=:[!&|?{;+\-*%<>~^/]$/.test(trimmed))
|
|
369
260
|
return true;
|
|
370
|
-
// After a block's `}` a statement, so a regex, may start; after an object literal's `}` a `/`
|
|
371
|
-
// divides. Which one it is takes a parser to know, so [codeTerminates] reads such code both ways.
|
|
372
261
|
if (trimmed.endsWith('}'))
|
|
373
262
|
return braceStartsRegex;
|
|
374
|
-
// `if (x) /re/.test(…)`: after a control-flow header's `)` a statement starts, which may be a regex.
|
|
375
263
|
if (trimmed.endsWith(')')) {
|
|
376
264
|
let depth = 0;
|
|
377
265
|
for (let index = trimmed.length - 1; index >= 0; index -= 1) {
|
|
@@ -384,10 +272,6 @@ function regexMayStart(before) {
|
|
|
384
272
|
}
|
|
385
273
|
return /\b(?:return|typeof|case|do|else|in|of|new|delete|void|throw|yield|await)$/.test(trimmed);
|
|
386
274
|
}
|
|
387
|
-
/**
|
|
388
|
-
* The index just past a regular-expression literal opening at `start`, with its flags, or -1 when
|
|
389
|
-
* no literal closes on that line: then the slash was division, or the code does not parse at all.
|
|
390
|
-
*/
|
|
391
275
|
function regexEnd(text, start) {
|
|
392
276
|
let index = start + 1;
|
|
393
277
|
let inClass = false;
|
|
@@ -411,22 +295,12 @@ function regexEnd(text, start) {
|
|
|
411
295
|
}
|
|
412
296
|
return -1;
|
|
413
297
|
}
|
|
414
|
-
/** Whether a regular-expression literal opens at `index` in JavaScript, given the code before it. */
|
|
415
298
|
function regexAt(text, index, before) {
|
|
416
299
|
if (text[index] !== '/' || text[index + 1] === '/' || text[index + 1] === '*' || !regexMayStart(before))
|
|
417
300
|
return undefined;
|
|
418
301
|
const end = regexEnd(text, index);
|
|
419
302
|
return end === -1 ? undefined : end;
|
|
420
303
|
}
|
|
421
|
-
/**
|
|
422
|
-
* Remove comments by the dialect's own rules, leaving quoted text untouched.
|
|
423
|
-
*
|
|
424
|
-
* A comment never runs: `// process.kill(80632)` above `console.log('ok')` only prints. Each
|
|
425
|
-
* dialect has its own: `#` at the start of a word in the shells and in Python, Ruby and Perl;
|
|
426
|
-
* `<# … #>` in PowerShell; `//` and `/* … *\/` in JavaScript, and nowhere else — in Python `//`
|
|
427
|
-
* divides, and on a Git Bash line it is a switch such as `taskkill //PID`. cmd.exe has none here.
|
|
428
|
-
* An escaped character is never the start of a comment, and an escaped blank does not end a word.
|
|
429
|
-
*/
|
|
430
304
|
function stripComments(text, dialect) {
|
|
431
305
|
if (dialect === 'cmd')
|
|
432
306
|
return text;
|
|
@@ -474,8 +348,6 @@ function stripComments(text, dialect) {
|
|
|
474
348
|
out += ' ';
|
|
475
349
|
continue;
|
|
476
350
|
}
|
|
477
|
-
// Python and Ruby start a comment at any `#` outside a string (`x = 1# note`). Perl does not: `$#a`
|
|
478
|
-
// and `s#a#b#` use it as syntax, so there it must begin a word.
|
|
479
351
|
const anywhere = dialect === 'hash' || dialect === 'ruby';
|
|
480
352
|
if (dialect !== 'js' && character === '#' && (wordStart || anywhere)) {
|
|
481
353
|
const end = text.indexOf('\n', index);
|
|
@@ -487,22 +359,8 @@ function stripComments(text, dialect) {
|
|
|
487
359
|
}
|
|
488
360
|
return out;
|
|
489
361
|
}
|
|
490
|
-
/**
|
|
491
|
-
* Whether code handed to an interpreter ends a process.
|
|
492
|
-
*
|
|
493
|
-
* Code for a shell (`bash -c`, `powershell -Command`, `cmd /c`) is a command line in that shell.
|
|
494
|
-
* In program code a string literal is data — `console.log('kill 80632')` prints — so kill calls are
|
|
495
|
-
* looked for outside the literals, with comments removed. A literal counts where the code runs it:
|
|
496
|
-
* handed to an exec function, used as a method name (`process['kill']`), or interpolated
|
|
497
|
-
* (`${ … }`, an f-string's `{ … }`). The code is also read as a command line, which catches a
|
|
498
|
-
* statement such as Perl's `kill 9, $pid`.
|
|
499
|
-
*/
|
|
500
|
-
/** How a `/` after `}` is read in the current pass of [codeTerminates]: as a regex, or as division. */
|
|
501
362
|
let braceStartsRegex = true;
|
|
502
363
|
function codeTerminates(source, depth, dialect) {
|
|
503
|
-
// `{} / 1; process.kill(…); /a/` and `if (x) {} /"/.test(s); process.kill(…)` need opposite
|
|
504
|
-
// readings of the slash after `}`, and either reading may hide a call inside a false literal, so
|
|
505
|
-
// JavaScript with such a slash is judged both ways.
|
|
506
364
|
if (dialect === 'js' && /\}\s*\/(?![/*])/.test(source)) {
|
|
507
365
|
const saved = braceStartsRegex;
|
|
508
366
|
try {
|
|
@@ -518,38 +376,26 @@ function codeTerminates(source, depth, dialect) {
|
|
|
518
376
|
}
|
|
519
377
|
return codeTerminatesAs(source, depth, dialect);
|
|
520
378
|
}
|
|
521
|
-
/** [codeTerminates] for one reading of the source. */
|
|
522
379
|
function codeTerminatesAs(written, depth, dialect) {
|
|
523
380
|
if (depth > 4)
|
|
524
381
|
return false;
|
|
525
382
|
note(written);
|
|
526
383
|
if (dialect === 'posix' || dialect === 'powershell' || dialect === 'cmd')
|
|
527
384
|
return terminates(written, depth + 1, dialect);
|
|
528
|
-
// Perl's `q[…]`, `qq{…}`, `qw(…)` and `qx[…]` are its quotes and backticks with other delimiters:
|
|
529
|
-
// `system q[kill 80632]` is `system 'kill 80632'`, and `qx[kill 80632]` runs it.
|
|
530
385
|
const source = dialect === 'perl' ? perlQuotesAsQuotes(written) : written;
|
|
531
386
|
const code = stripComments(source, dialect);
|
|
532
387
|
const scanned = scan(code, dialect);
|
|
533
388
|
const { quoted, embedded, regexes, interpolated } = scanned;
|
|
534
|
-
// JavaScript names an identifier the same with `\u…` escapes: `process.\u006bill` is
|
|
535
|
-
// `process.kill`. They are decoded outside strings only, where they can only spell identifiers;
|
|
536
|
-
// a string's escapes were resolved by [scan] as the string's own text.
|
|
537
|
-
// Ruby and Perl call without parentheses: `system "kill 80632"` is `system("kill 80632"`, which is
|
|
538
|
-
// what [RUNS_LITERAL] reads.
|
|
539
389
|
const named = dialect === 'js' ? decodeEscapes(scanned.bare)
|
|
540
390
|
: dialect === 'ruby' || dialect === 'perl' ? scanned.bare.replace(/\b(system|exec|spawn|popen)[ \t]+(?=\u0000\d+\u0000)/g, '$1(')
|
|
541
391
|
: scanned.bare;
|
|
542
|
-
// A launcher looked up by a string, `cp['execSync']('kill 80632')`, is read as `cp.execSync(…)`.
|
|
543
392
|
const bare = named.replace(/\[\s*\u0000(\d+)\u0000\s*\](?=\s*(?:\?\.\s*)?\()/g, (whole, slot) => {
|
|
544
393
|
const name = quoted[Number(slot)] ?? '';
|
|
545
394
|
return LAUNCHER_NAMES.test(name) ? `.${name}` : whole;
|
|
546
395
|
});
|
|
547
396
|
if (callsKill(bare))
|
|
548
397
|
return true;
|
|
549
|
-
// Perl's kill is a function usable anywhere in an expression: `print kill 15, $pid`,
|
|
550
|
-
// `my $n = kill(15, $pid)`, `use constant X => kill(…)`. Signal 0 (or ZERO) only probes.
|
|
551
398
|
if (dialect === 'perl') {
|
|
552
|
-
// `CORE::kill` (and `CORE::GLOBAL::kill`) is the same builtin, named explicitly.
|
|
553
399
|
const unqualified = bare.replace(/\bCORE::(?:GLOBAL::)?(?=kill\b)/g, ' ');
|
|
554
400
|
for (const match of unqualified.matchAll(/(?<![\w$@%&:>.-])kill\b\s*\(?\s*([^,;()]*?)\s*,/g)) {
|
|
555
401
|
const signal = match[1].replace(/\u0000(\d+)\u0000/g, (_, slot) => quoted[Number(slot)] ?? '').trim();
|
|
@@ -558,24 +404,15 @@ function codeTerminatesAs(written, depth, dialect) {
|
|
|
558
404
|
return true;
|
|
559
405
|
}
|
|
560
406
|
}
|
|
561
|
-
// Read as a command line with every string and regular expression blanked, so a statement such as
|
|
562
|
-
// Perl's `kill 9, $pid` counts and a literal's contents do not.
|
|
563
407
|
if (terminates(bare.replace(/\u0000\d+\u0000/g, "''"), depth + 1, 'posix', false))
|
|
564
408
|
return true;
|
|
565
409
|
if (embedded.some(inner => codeTerminates(inner, depth + 1, dialect)))
|
|
566
410
|
return true;
|
|
567
|
-
// A string that interpolates runs the code in it: a Python f-string's `{ … }`, Ruby's "#{ … }".
|
|
568
|
-
// (A JavaScript template's `${ … }` is in `embedded`.) Braces in any other string are text.
|
|
569
411
|
for (const [slot, pattern] of interpolated) {
|
|
570
|
-
// A pattern may match text that is not a field (a Python `{{` is a literal brace): no capture.
|
|
571
412
|
for (const match of (quoted[slot] ?? '').matchAll(pattern))
|
|
572
413
|
if (match[1] !== undefined && (callsKill(match[1]) || codeTerminates(match[1], depth + 1, dialect)))
|
|
573
414
|
return true;
|
|
574
415
|
}
|
|
575
|
-
// A literal handed to the language's own evaluator is code in that language, at the positions
|
|
576
|
-
// that evaluator runs: every argument of `Function` (its parameters' defaults run too, and the last
|
|
577
|
-
// is the body), the first of `eval`, the timers and `vm`. A timer's later arguments are data for
|
|
578
|
-
// its callback, and `eval` ignores any after the first.
|
|
579
416
|
if (dialect === 'js') {
|
|
580
417
|
for (const match of bare.matchAll(JS_EVAL)) {
|
|
581
418
|
const slots = callLiterals(bare, match.index + match[0].length);
|
|
@@ -591,7 +428,6 @@ function codeTerminatesAs(written, depth, dialect) {
|
|
|
591
428
|
if (codeTerminates(quoted[Number(match[1])] ?? '', depth + 1, dialect))
|
|
592
429
|
return true;
|
|
593
430
|
}
|
|
594
|
-
// Compiled source runs only when the code calls an executor too: `exec(compile('…', 'f', 'exec'))`.
|
|
595
431
|
if (/\b(?:exec|eval)\s*\(/.test(bare)) {
|
|
596
432
|
for (const match of bare.matchAll(PY_COMPILE)) {
|
|
597
433
|
if (codeTerminates(quoted[Number(match[1])] ?? '', depth + 1, dialect))
|
|
@@ -602,7 +438,6 @@ function codeTerminatesAs(written, depth, dialect) {
|
|
|
602
438
|
for (const match of bare.matchAll(INDEXED_CALL)) {
|
|
603
439
|
if (!/^(?:kill|terminate)$/i.test(quoted[Number(match[1])] ?? ''))
|
|
604
440
|
continue;
|
|
605
|
-
// `process['kill'](pid, 0)` only asks whether the process exists.
|
|
606
441
|
if (match[2] !== undefined && /^[^,]*,\s*0\s*$/.test(match[2]) && /^kill$/i.test(quoted[Number(match[1])] ?? ''))
|
|
607
442
|
continue;
|
|
608
443
|
return true;
|
|
@@ -611,17 +446,11 @@ function codeTerminatesAs(written, depth, dialect) {
|
|
|
611
446
|
if (!startsProcess(match[2], match[1] === '.', bare.slice(0, match.index), code, regexes))
|
|
612
447
|
continue;
|
|
613
448
|
const named = quoted[Number(match[4])] ?? '';
|
|
614
|
-
// An array is an argument vector: `spawn('bash', ['-c', …])`, or Python's `run(['bash', '-c', …])`.
|
|
615
449
|
const after = bare.slice(match.index + match[0].length);
|
|
616
|
-
// Ruby and Perl pass the arguments as further literals: `system("bash", "-c", "…")`.
|
|
617
450
|
const positional = /^((?:\s*,\s*\u0000\d+\u0000)*)/;
|
|
618
451
|
const list = match[3] ? positional.exec(after)
|
|
619
452
|
: /^\s*,\s*\[((?:\s*,?\s*\u0000\d+\u0000)*)/.exec(after) ?? (dialect === 'ruby' || dialect === 'perl' ? positional.exec(after) : null);
|
|
620
453
|
const args = [...(list?.[1] ?? '').matchAll(/\u0000(\d+)\u0000/g)].map(slot => quoted[Number(slot[1])] ?? '');
|
|
621
|
-
// Launch options that ask for a shell (Node's `{ shell: true }`, Python's `shell=True`) join the
|
|
622
|
-
// program and arguments into a command line for that shell to read.
|
|
623
|
-
// The rest of the call, to its own closing parenthesis: options may hold calls of their own,
|
|
624
|
-
// such as `{ cwd: require('os').tmpdir(), shell: true }`. Strings are placeholders here.
|
|
625
454
|
let open = 1;
|
|
626
455
|
let close = 0;
|
|
627
456
|
for (; close < after.length && open > 0; close += 1) {
|
|
@@ -630,12 +459,6 @@ function codeTerminatesAs(written, depth, dialect) {
|
|
|
630
459
|
else if (after[close] === ')')
|
|
631
460
|
open -= 1;
|
|
632
461
|
}
|
|
633
|
-
// A key may be quoted (`{ 'shell': true }`): literals are put back before the option is looked for.
|
|
634
|
-
// Only the launch options argument, after the argument list: a string inside the list that
|
|
635
|
-
// reads `shell: true` is the child's data, not an option.
|
|
636
|
-
// Python's `executable=` runs that program with the argument vector, whose first element is only
|
|
637
|
-
// the name it is given: `run(["echo", "80632"], executable="/bin/kill")` runs kill.
|
|
638
|
-
// With shell=True, executable= names the shell instead, and the command stays the first element.
|
|
639
462
|
const viaShell = shellOption(after.slice(list?.[0].length ?? 0, close), quoted);
|
|
640
463
|
const override = dialect === 'hash' && match[3] && !viaShell ? /\bexecutable\s*=\s*\u0000(\d+)\u0000/.exec(after.slice(0, close)) : null;
|
|
641
464
|
const program = override ? quoted[Number(override[1])] ?? named : named;
|
|
@@ -646,24 +469,13 @@ function codeTerminatesAs(written, depth, dialect) {
|
|
|
646
469
|
return true;
|
|
647
470
|
continue;
|
|
648
471
|
}
|
|
649
|
-
// A string alone is a command line, run by /bin/sh or by cmd.exe on Windows. Both readings
|
|
650
|
-
// are tried: a stricter reading only ever refuses more.
|
|
651
472
|
if (terminates(program, depth + 1, 'posix') || terminates(program, depth + 1, 'cmd'))
|
|
652
473
|
return true;
|
|
653
474
|
}
|
|
654
475
|
return false;
|
|
655
476
|
}
|
|
656
|
-
/**
|
|
657
|
-
* Whether a launch's options turn its shell on: a top-level `shell` property of the options object
|
|
658
|
-
* (Node: `{ shell: true }`, `{ 'shell': '/bin/bash' }`) or a `shell=` keyword argument (Python).
|
|
659
|
-
* A `shell` nested inside another property (`{ env: { … } }`) and text inside strings are not it.
|
|
660
|
-
* `options` is the rest of the call with strings as placeholders, resolved through `quoted`.
|
|
661
|
-
*/
|
|
662
477
|
function shellOption(rest, quoted) {
|
|
663
|
-
// The argument list's own closing bracket comes first; depth is counted from after it.
|
|
664
478
|
const options = rest.replace(/^\s*\]/, '');
|
|
665
|
-
// A literal options object is read by its shape. Options built some other way
|
|
666
|
-
// (`Object.assign({}, a, { shell: true })`) have no shape to read, so a `shell` key at any depth counts.
|
|
667
479
|
const shaped = /^\s*(?:,\s*)?(?:\{|\)|$|[A-Za-z_]\w*\s*=)/.test(options);
|
|
668
480
|
let depth = 0;
|
|
669
481
|
for (let index = 0; index < options.length; index += 1) {
|
|
@@ -676,7 +488,6 @@ function shellOption(rest, quoted) {
|
|
|
676
488
|
depth -= 1;
|
|
677
489
|
continue;
|
|
678
490
|
}
|
|
679
|
-
// Python's keyword arguments sit at the call's own level; Node's options object one deeper.
|
|
680
491
|
if (shaped && depth !== 0 && depth !== 1)
|
|
681
492
|
continue;
|
|
682
493
|
const key = /^(?:shell\b|\u0000(\d+)\u0000)\s*([:=])/.exec(options.slice(index));
|
|
@@ -694,14 +505,6 @@ function shellOption(rest, quoted) {
|
|
|
694
505
|
}
|
|
695
506
|
return false;
|
|
696
507
|
}
|
|
697
|
-
/**
|
|
698
|
-
* Whether an argument list launched through a shell ends a process, by the launching API's rules.
|
|
699
|
-
*
|
|
700
|
-
* Node joins the program and arguments with spaces, unquoted, into one command line for the shell.
|
|
701
|
-
* Python does not: on POSIX the first element is the command and the rest are the shell's own
|
|
702
|
-
* arguments (`$0`, `$1`, …), which it does not run; on Windows each element is quoted where it
|
|
703
|
-
* needs to be (`list2cmdline`) and the result goes to cmd.exe, so an element stays one argument.
|
|
704
|
-
*/
|
|
705
508
|
function shellLaunchTerminates(program, args, depth, dialect) {
|
|
706
509
|
if (dialect === 'hash') {
|
|
707
510
|
const quoted = [program, ...args].map(item => item === '' || /[\s"]/.test(item) ? `"${item.replace(/"/g, '\\"')}"` : item).join(' ');
|
|
@@ -710,22 +513,16 @@ function shellLaunchTerminates(program, args, depth, dialect) {
|
|
|
710
513
|
const line = [program, ...args].join(' ');
|
|
711
514
|
return terminates(line, depth, 'posix') || terminates(line, depth, 'cmd');
|
|
712
515
|
}
|
|
713
|
-
/** Options of each interpreter that take the next argument as their value: not the script, not code. */
|
|
714
516
|
const INTERPRETER_VALUE_OPTIONS = {
|
|
715
517
|
python: new Set(['-W', '-X', '--check-hash-based-pycs']),
|
|
716
518
|
python3: new Set(['-W', '-X', '--check-hash-based-pycs']),
|
|
717
519
|
py: new Set(['-W', '-X', '--check-hash-based-pycs']),
|
|
718
520
|
ruby: new Set(['-I', '-r', '-C', '-E', '--encoding', '-F']),
|
|
719
|
-
// -M, -m, -x and -C take their value attached (`-Mstrict`, `-C7`) or none; only -I may take the next word.
|
|
720
521
|
perl: new Set(['-I']),
|
|
721
522
|
bash: new Set(['-o', '+o', '-O', '+O', '--rcfile', '--init-file']),
|
|
722
523
|
sh: new Set(['-o', '+o']),
|
|
723
524
|
zsh: new Set(['-o', '+o']),
|
|
724
525
|
};
|
|
725
|
-
/**
|
|
726
|
-
* Whether a shell was started not to execute what it reads: `bash -n`, `sh -nc …`, `bash -o noexec`.
|
|
727
|
-
* Its options are read up to the code or the script, and a later `+n` or `+o noexec` undoes them.
|
|
728
|
-
*/
|
|
729
526
|
function shellNoExec(word, args) {
|
|
730
527
|
if (!SHELLS.has(word))
|
|
731
528
|
return false;
|
|
@@ -738,7 +535,6 @@ function shellNoExec(word, args) {
|
|
|
738
535
|
index += 1;
|
|
739
536
|
continue;
|
|
740
537
|
}
|
|
741
|
-
// An option's value (`-O extglob`, `--rcfile f`) is not where the options end.
|
|
742
538
|
if (INTERPRETER_VALUE_OPTIONS[word]?.has(argument)) {
|
|
743
539
|
index += 1;
|
|
744
540
|
continue;
|
|
@@ -749,14 +545,9 @@ function shellNoExec(word, args) {
|
|
|
749
545
|
break;
|
|
750
546
|
if (argument.includes('n'))
|
|
751
547
|
noexec = argument.startsWith('-');
|
|
752
|
-
// `-c` does not end the options: `bash -nc +n 'kill …'` turns execution back on before the string.
|
|
753
548
|
}
|
|
754
549
|
return noexec;
|
|
755
550
|
}
|
|
756
|
-
/**
|
|
757
|
-
* Whether Node is told to check syntax only: `-c` or `--check` among its own options, before the
|
|
758
|
-
* script, `-` or `--`. After those it is an argument of the script: `node - --check` runs stdin.
|
|
759
|
-
*/
|
|
760
551
|
function nodeChecksOnly(args) {
|
|
761
552
|
for (let index = 0; index < args.length; index += 1) {
|
|
762
553
|
const argument = args[index];
|
|
@@ -769,10 +560,6 @@ function nodeChecksOnly(args) {
|
|
|
769
560
|
}
|
|
770
561
|
return false;
|
|
771
562
|
}
|
|
772
|
-
/**
|
|
773
|
-
* Whether Ruby is told to check syntax only: `-c`, alone or bundled with flags that take no value
|
|
774
|
-
* (`-wc`, `-ce`). A bundle that starts with a value option (`-rcsv` requires csv) is not one.
|
|
775
|
-
*/
|
|
776
563
|
function rubySyntaxOnly(word, args) {
|
|
777
564
|
if (word !== 'ruby')
|
|
778
565
|
return false;
|
|
@@ -787,16 +574,9 @@ function rubySyntaxOnly(word, args) {
|
|
|
787
574
|
}
|
|
788
575
|
return false;
|
|
789
576
|
}
|
|
790
|
-
/** Whether an argument is a shell's `+` option, which turns a flag off (`bash +o posix`, `bash +x`). */
|
|
791
577
|
function shellPlusOption(word, argument) {
|
|
792
578
|
return SHELLS.has(word) && /^\+[A-Za-z]+$/.test(argument);
|
|
793
579
|
}
|
|
794
|
-
/**
|
|
795
|
-
* Whether `argument` is the flag after which this interpreter runs its next argument as code.
|
|
796
|
-
* Node's `-p` prints an expression; Perl's and Ruby's `-p` is a read-print loop that takes no
|
|
797
|
-
* value, and their `-c` only checks syntax, so each interpreter has its own (Perl's and Ruby's
|
|
798
|
-
* `-e` bundles are read where they are matched).
|
|
799
|
-
*/
|
|
800
580
|
function evalFlag(word, argument) {
|
|
801
581
|
if (word === 'perl' || word === 'ruby')
|
|
802
582
|
return false;
|
|
@@ -806,22 +586,10 @@ function evalFlag(word, argument) {
|
|
|
806
586
|
return NODE_EVAL.has(argument);
|
|
807
587
|
return EVAL_FLAGS.has(argument);
|
|
808
588
|
}
|
|
809
|
-
/** Node and the runtimes that take its options. */
|
|
810
589
|
const NODE_FAMILY = new Set(['node', 'nodejs', 'bun', 'deno']);
|
|
811
|
-
/** Shells, whose `-c` may be bundled with other single-letter flags (`bash -lc`). */
|
|
812
590
|
const SHELLS = new Set(['bash', 'sh', 'zsh']);
|
|
813
|
-
/** The flags after which a language interpreter runs its next argument as code. */
|
|
814
591
|
const EVAL_FLAGS = new Set(['-e', '--eval', '-p', '--print', '-pe', '-c']);
|
|
815
|
-
/** PowerShell options that take a value, which is therefore not the command. */
|
|
816
592
|
const POWERSHELL_VALUE_OPTIONS = /^-(?:executionpolicy|ex|ep|windowstyle|w|version|v|inputformat|if|outputformat|of|workingdirectory|wd|configurationname|config|settingsfile|settings)$/i;
|
|
817
|
-
/**
|
|
818
|
-
* The arguments an interpreter runs as code, out of everything on its command line.
|
|
819
|
-
*
|
|
820
|
-
* Only those are judged. The rest is data the program receives: in
|
|
821
|
-
* `node -e 'console.log(process.argv)' -- 'kill 80632'` the last argument is printed, not run.
|
|
822
|
-
* An interpreter stops reading its own options at `--` and at the first argument that is not an
|
|
823
|
-
* option (the script to run); nothing after that is its code.
|
|
824
|
-
*/
|
|
825
593
|
function interpreterCode(word, args) {
|
|
826
594
|
if (word === 'iex' || word === 'invoke-expression' || word === 'eval')
|
|
827
595
|
return [...args];
|
|
@@ -832,7 +600,6 @@ function interpreterCode(word, args) {
|
|
|
832
600
|
if (word === 'powershell' || word === 'pwsh') {
|
|
833
601
|
for (let index = 0; index < args.length; index += 1) {
|
|
834
602
|
const argument = args[index];
|
|
835
|
-
// `-Command` and its unambiguous prefixes; everything after it is the command.
|
|
836
603
|
if (/^-c(?:o(?:m(?:m(?:a(?:nd?)?)?)?)?)?$/i.test(argument))
|
|
837
604
|
return [args.slice(index + 1).join(' ')];
|
|
838
605
|
if (/^-(?:f(?:i(?:le?)?)?|e(?:n(?:c(?:odedcommand)?)?)?|ec)$/i.test(argument))
|
|
@@ -841,7 +608,6 @@ function interpreterCode(word, args) {
|
|
|
841
608
|
index += 1;
|
|
842
609
|
continue;
|
|
843
610
|
}
|
|
844
|
-
// With no `-Command` or `-File`, the first argument that is not an option starts the command.
|
|
845
611
|
if (!argument.startsWith('-'))
|
|
846
612
|
return [args.slice(index).join(' ')];
|
|
847
613
|
}
|
|
@@ -854,10 +620,8 @@ function interpreterCode(word, args) {
|
|
|
854
620
|
const argument = args[index];
|
|
855
621
|
if (argument === '--')
|
|
856
622
|
break;
|
|
857
|
-
// `-` reads the script from stdin; what follows is the script's arguments: `node - -e …`.
|
|
858
623
|
if (argument === '-')
|
|
859
624
|
break;
|
|
860
|
-
// A quoted value is scanned as a token of its own, so `--eval='…'` arrives as `--eval=` and the code.
|
|
861
625
|
if (argument === '--eval=' || argument === '--print=') {
|
|
862
626
|
if (args[index + 1] !== undefined)
|
|
863
627
|
code.push(args[index + 1]);
|
|
@@ -868,13 +632,8 @@ function interpreterCode(word, args) {
|
|
|
868
632
|
code.push(argument.slice(argument.indexOf('=') + 1));
|
|
869
633
|
continue;
|
|
870
634
|
}
|
|
871
|
-
// Perl's `-E` is `-e` with its optional features on; to Python and Ruby `-E` is something else.
|
|
872
|
-
// Perl and Ruby bundle switches before `-e`: `perl -lne '…'`, `ruby -we '…'`, Perl's `-wE`.
|
|
873
635
|
const evaluates = evalFlag(word, argument) || (word === 'perl' && /^-[A-Za-z]*[eE]$/.test(argument)) || (word === 'ruby' && /^-[A-Za-z]*e$/.test(argument)) || (SHELLS.has(word) && /^-[A-Za-z]*c[A-Za-z]*$/.test(argument));
|
|
874
636
|
if (evaluates) {
|
|
875
|
-
// A shell reads all its options first: the command string is the first operand after them and
|
|
876
|
-
// an optional `--` (`bash -c -- 'kill …'`, `bash -c -x 'kill …'`). Other interpreters take the
|
|
877
|
-
// next argument as their code, whatever it looks like.
|
|
878
637
|
if (SHELLS.has(word)) {
|
|
879
638
|
let at = index + 1;
|
|
880
639
|
while (at < args.length) {
|
|
@@ -900,14 +659,10 @@ function interpreterCode(word, args) {
|
|
|
900
659
|
if (args[index + 1] !== undefined)
|
|
901
660
|
code.push(args[index + 1]);
|
|
902
661
|
index += 1;
|
|
903
|
-
// Python's `-c cmd` and a shell's `-c cmd` end the option list: what follows is the program's
|
|
904
|
-
// `sys.argv` or `$0 $1 …`. Node, Ruby and Perl take several `-e`.
|
|
905
662
|
if (word === 'python' || word === 'python3' || word === 'py' || SHELLS.has(word))
|
|
906
663
|
break;
|
|
907
664
|
continue;
|
|
908
665
|
}
|
|
909
|
-
// Python takes its code attached or after bundled flags (`-c'…'`, `-Ic '…'`), Ruby and Perl their
|
|
910
|
-
// `-e` code attached (`-e'…'`).
|
|
911
666
|
const python = word === 'python' || word === 'python3' || word === 'py';
|
|
912
667
|
const bundled = python ? /^-[bBdEhiIOqsSuvRP]*c([\s\S]*)$/.exec(argument) : undefined;
|
|
913
668
|
if (bundled) {
|
|
@@ -918,49 +673,39 @@ function interpreterCode(word, args) {
|
|
|
918
673
|
code.push(args[index + 1]);
|
|
919
674
|
break;
|
|
920
675
|
}
|
|
921
|
-
// Code attached to `-e`, also after value-less switches in one bundle: `perl -we'kill …'`.
|
|
922
676
|
const attached = word === 'perl' ? /^-[acflnpsStTuUvwWX]*[eE]([\s\S]+)$/.exec(argument)
|
|
923
677
|
: word === 'ruby' ? /^-[adlnpsvwy]*e([\s\S]+)$/.exec(argument) : null;
|
|
924
678
|
if (attached) {
|
|
925
679
|
code.push(attached[1]);
|
|
926
680
|
continue;
|
|
927
681
|
}
|
|
928
|
-
// Node's preload given as a `data:` URL is code: `--import data:text/javascript,…`.
|
|
929
682
|
const node = NODE_FAMILY.has(word);
|
|
930
683
|
const preload = !node ? undefined : nodePreloadCode(argument, args[index + 1]);
|
|
931
684
|
if (preload !== undefined)
|
|
932
685
|
code.push(preload);
|
|
933
|
-
// Node's value options are Node's: Perl's `-C` takes no following value.
|
|
934
686
|
if ((node && NODE_VALUE_OPTIONS.has(argument)) || INTERPRETER_VALUE_OPTIONS[word]?.has(argument)) {
|
|
935
687
|
index += 1;
|
|
936
688
|
continue;
|
|
937
689
|
}
|
|
938
|
-
// `python -m module` runs a module: no inline code follows, and its arguments are its own.
|
|
939
690
|
if ((word === 'python' || word === 'python3' || word === 'py') && argument === '-m')
|
|
940
691
|
break;
|
|
941
692
|
if (argument.startsWith('-') || shellPlusOption(word, argument))
|
|
942
693
|
continue;
|
|
943
|
-
// A word after a Node option this guard does not know may be its value: read on.
|
|
944
694
|
if ((word === 'node' || word === 'nodejs' || word === 'bun') && unknownLongOption(args[index - 1]))
|
|
945
695
|
continue;
|
|
946
|
-
// `deno eval <code>` is the one interpreter whose code follows a subcommand.
|
|
947
696
|
if (word === 'deno' && argument === 'eval' && args[index + 1] !== undefined)
|
|
948
697
|
code.push(args[index + 1]);
|
|
949
698
|
break;
|
|
950
699
|
}
|
|
951
|
-
// `perl -c` compiles without running; only its BEGIN, UNITCHECK and CHECK blocks run.
|
|
952
|
-
// The `-e` fragments are one program, so a block may span them: `-e 'BEGIN {' -e 'kill …' -e '}'`.
|
|
953
700
|
if (word === 'perl' && perlSyntaxOnly(args))
|
|
954
701
|
return (code.length > 1 ? [...code, code.join('\n')] : code).flatMap(compileTimeBlocks);
|
|
955
702
|
return code;
|
|
956
703
|
}
|
|
957
|
-
/** Whether Perl is told to check syntax only: `-c`, alone or bundled with switches that take no value (`-wc`). */
|
|
958
704
|
function perlSyntaxOnly(args) {
|
|
959
705
|
for (let index = 0; index < args.length; index += 1) {
|
|
960
706
|
const argument = args[index];
|
|
961
707
|
if (argument === '--' || !argument.startsWith('-') || argument === '-')
|
|
962
708
|
return false;
|
|
963
|
-
// `-c` alone or in a bundle, also one ending in `e` with the code attached: `-ce'BEGIN { … }'`.
|
|
964
709
|
const bundle = /^-([acflnpsStTuUvwWX]*)(?:[eE][\s\S]*)?$/.exec(argument);
|
|
965
710
|
if (bundle && bundle[1].includes('c'))
|
|
966
711
|
return true;
|
|
@@ -969,10 +714,6 @@ function perlSyntaxOnly(args) {
|
|
|
969
714
|
}
|
|
970
715
|
return false;
|
|
971
716
|
}
|
|
972
|
-
/**
|
|
973
|
-
* Where the Perl statement that starts at `start` ends: its own `;`, or a closing brace or
|
|
974
|
-
* bracket that encloses it, past nested blocks (`do { 1; kill … }`), quoted text and comments.
|
|
975
|
-
*/
|
|
976
717
|
function statementEnd(source, start) {
|
|
977
718
|
let depth = 0;
|
|
978
719
|
for (let at = start; at < source.length; at += 1) {
|
|
@@ -1000,18 +741,9 @@ function statementEnd(source, start) {
|
|
|
1000
741
|
}
|
|
1001
742
|
return source.length;
|
|
1002
743
|
}
|
|
1003
|
-
/**
|
|
1004
|
-
* The part of a program an interpreter runs when it reads it from its input or a here-document:
|
|
1005
|
-
* all of it, except under `perl -c`, which runs only its compile-time code.
|
|
1006
|
-
*/
|
|
1007
744
|
function inputProgram(word, args, program) {
|
|
1008
745
|
return word === 'perl' && perlSyntaxOnly(args) ? compileTimeBlocks(program).join('\n') : program;
|
|
1009
746
|
}
|
|
1010
|
-
/**
|
|
1011
|
-
* Perl source with each `q`, `qq`, `qw` and `qx` literal written as the quote it stands for: `'…'`,
|
|
1012
|
-
* `"…"`, `'…'` and a backtick command. Ordinary quotes, comments and the regex operators are left
|
|
1013
|
-
* as they are.
|
|
1014
|
-
*/
|
|
1015
747
|
function perlQuotesAsQuotes(source) {
|
|
1016
748
|
const pairs = { '(': ')', '[': ']', '{': '}', '<': '>' };
|
|
1017
749
|
const close = (from, open) => {
|
|
@@ -1056,16 +788,10 @@ function perlQuotesAsQuotes(source) {
|
|
|
1056
788
|
}
|
|
1057
789
|
return out;
|
|
1058
790
|
}
|
|
1059
|
-
/**
|
|
1060
|
-
* `source` with Perl's quoted text and comments blanked to spaces, the same length: '…', "…",
|
|
1061
|
-
* `…`, the quote-like operators (`q[…]`, `qq{…}`, `qw(…)`, `m/…/`, `s{…}{…}`, `tr/…/…/`) and
|
|
1062
|
-
* `#` comments. Code is found in what is left, so a `}` or a `BEGIN` inside a string is text.
|
|
1063
|
-
*/
|
|
1064
791
|
function blankPerlLiterals(source) {
|
|
1065
792
|
const pairs = { '(': ')', '[': ']', '{': '}', '<': '>' };
|
|
1066
793
|
let out = '';
|
|
1067
794
|
let at = 0;
|
|
1068
|
-
// From `from` (just past an opening delimiter), the index just past its closing one.
|
|
1069
795
|
const close = (from, open) => {
|
|
1070
796
|
const shut = pairs[open] ?? open;
|
|
1071
797
|
let depth = 1;
|
|
@@ -1087,10 +813,8 @@ function blankPerlLiterals(source) {
|
|
|
1087
813
|
const character = source[at];
|
|
1088
814
|
const before = at === 0 ? '' : source[at - 1];
|
|
1089
815
|
const operator = /[\w$@%&>-]/.test(before) ? null : /^(qq|qw|qx|qr|q|m|s|tr|y)\s*([^\w\s])/.exec(source.slice(at, at + 12));
|
|
1090
|
-
// `y => 1` and `s => …` are hash keys, not operators.
|
|
1091
816
|
if (operator && !(operator[2] === '=' && source[at + operator[0].length] === '>')) {
|
|
1092
817
|
let index = close(at + operator[0].length, operator[2]);
|
|
1093
|
-
// s, tr and y take a second part: with brackets it has its own delimiters, otherwise it shares the middle one.
|
|
1094
818
|
if (['s', 'tr', 'y'].includes(operator[1])) {
|
|
1095
819
|
if (pairs[operator[2]]) {
|
|
1096
820
|
while (/\s/.test(source[index] ?? ''))
|
|
@@ -1106,7 +830,6 @@ function blankPerlLiterals(source) {
|
|
|
1106
830
|
at = index;
|
|
1107
831
|
continue;
|
|
1108
832
|
}
|
|
1109
|
-
// A bare regex (`/}/`, `split /,/`) where an operand may start is literal text too.
|
|
1110
833
|
if (character === '/' && /(?:^|[(,;{}=!~&|?:]|\b(?:split|grep|if|unless|and|or|not|return|when))\s*$/.test(out)) {
|
|
1111
834
|
const index = close(at + 1, '/');
|
|
1112
835
|
out += source.slice(at, index).replace(/[^\n]/g, ' ');
|
|
@@ -1131,11 +854,6 @@ function blankPerlLiterals(source) {
|
|
|
1131
854
|
}
|
|
1132
855
|
return out;
|
|
1133
856
|
}
|
|
1134
|
-
/**
|
|
1135
|
-
* The bodies of Perl's compile-time blocks in `source`: `BEGIN { … }`, `UNITCHECK { … }`, `CHECK { … }`,
|
|
1136
|
-
* and the arguments of `use` and `no` declarations. They are found outside quoted text and comments,
|
|
1137
|
-
* and their extent is measured the same way; the text returned is the source's own.
|
|
1138
|
-
*/
|
|
1139
857
|
function compileTimeBlocks(source) {
|
|
1140
858
|
const code = blankPerlLiterals(source);
|
|
1141
859
|
const blocks = [];
|
|
@@ -1151,24 +869,14 @@ function compileTimeBlocks(source) {
|
|
|
1151
869
|
}
|
|
1152
870
|
blocks.push(source.slice(start, depth === 0 ? at - 1 : at));
|
|
1153
871
|
}
|
|
1154
|
-
// `use Module LIST` and `no Module LIST` evaluate LIST at compile time: `use constant X => kill(…)`.
|
|
1155
872
|
for (const match of code.matchAll(/\b(?:use|no)\s+[\w:]+/g)) {
|
|
1156
873
|
const start = match.index + match[0].length;
|
|
1157
874
|
blocks.push(source.slice(start, statementEnd(code, start)));
|
|
1158
875
|
}
|
|
1159
876
|
return blocks;
|
|
1160
877
|
}
|
|
1161
|
-
/** PowerShell's backtick escapes inside "…". */
|
|
1162
878
|
const POWERSHELL_ESCAPES = { '0': '\0', a: '\u0007', b: '\b', e: '\u001b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v' };
|
|
1163
|
-
/** Bash's one-letter escapes inside $'…'. */
|
|
1164
879
|
const ANSI_ESCAPES = { a: '\u0007', b: '\b', e: '\u001b', E: '\u001b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\', "'": "'", '"': '"', '?': '?' };
|
|
1165
|
-
/**
|
|
1166
|
-
* What an escape inside a shell string stands for, and how many characters it spans.
|
|
1167
|
-
*
|
|
1168
|
-
* Bash's "…" removes a backslash only before `$`, a backtick, `"`, `\` and a line break; before
|
|
1169
|
-
* anything else the backslash stays, so `"…\n…"` reaches a program as backslash-n for the program
|
|
1170
|
-
* to decode. `$'…'` decodes C escapes. PowerShell's "…" turns `` `n `` into a line break.
|
|
1171
|
-
*/
|
|
1172
880
|
function shellEscape(dialect, quote, raw, at) {
|
|
1173
881
|
const next = raw[at + 1];
|
|
1174
882
|
if (dialect === 'posix' && quote === '"') {
|
|
@@ -1191,13 +899,7 @@ function shellEscape(dialect, quote, raw, at) {
|
|
|
1191
899
|
return { text: POWERSHELL_ESCAPES[next] ?? next, length: 2 };
|
|
1192
900
|
return { text: next, length: 2 };
|
|
1193
901
|
}
|
|
1194
|
-
/** What the one-letter escapes of JavaScript and Python strings stand for. */
|
|
1195
902
|
const PROGRAM_ESCAPES = { n: '\n', t: '\t', r: '\r', v: '\v', f: '\f', b: '\b', '0': '\0', '\n': '' };
|
|
1196
|
-
/**
|
|
1197
|
-
* The bodies of the `$( … )` substitutions in `text`, each to its own matching parenthesis: a
|
|
1198
|
-
* call such as `console.log(1)` inside one does not end it. Quoted spans inside are skipped as
|
|
1199
|
-
* the shell skips them. `text` has its escaped characters blanked already.
|
|
1200
|
-
*/
|
|
1201
903
|
function substitutionBodies(text) {
|
|
1202
904
|
const bodies = [];
|
|
1203
905
|
for (let start = text.indexOf('$('); start !== -1; start = text.indexOf('$(', start + 2)) {
|
|
@@ -1224,21 +926,17 @@ function substitutionBodies(text) {
|
|
|
1224
926
|
}
|
|
1225
927
|
return bodies;
|
|
1226
928
|
}
|
|
1227
|
-
/** The character a `\u` or `\x` escape at `at` names, and how long the escape is; undefined if it is none. */
|
|
1228
929
|
function escapeAt(text, at) {
|
|
1229
930
|
const match = /^\\(?:u\{([0-9a-fA-F]{1,6})\}|u([0-9a-fA-F]{4})|x([0-9a-fA-F]{2})|([0-3][0-7]{0,2}|[4-7][0-7]?))/.exec(text.slice(at));
|
|
1230
931
|
if (!match)
|
|
1231
932
|
return undefined;
|
|
1232
|
-
// Python, Ruby, Perl and sloppy-mode JavaScript read `\153` as octal: k.
|
|
1233
933
|
if (match[4] !== undefined)
|
|
1234
934
|
return { text: String.fromCharCode(parseInt(match[4], 8)), length: match[0].length };
|
|
1235
935
|
const point = parseInt(match[1] ?? match[2] ?? match[3], 16);
|
|
1236
|
-
// An escape beyond Unicode names no character; it stays as written.
|
|
1237
936
|
if (point > 0x10ffff)
|
|
1238
937
|
return undefined;
|
|
1239
938
|
return { text: String.fromCodePoint(point), length: match[0].length };
|
|
1240
939
|
}
|
|
1241
|
-
/** Decode every valid `\u` escape in identifier text; an invalid one stays as written. */
|
|
1242
940
|
function decodeEscapes(text) {
|
|
1243
941
|
let out = '';
|
|
1244
942
|
for (let index = 0; index < text.length;) {
|
|
@@ -1253,22 +951,12 @@ function decodeEscapes(text) {
|
|
|
1253
951
|
}
|
|
1254
952
|
return out;
|
|
1255
953
|
}
|
|
1256
|
-
/**
|
|
1257
|
-
* Separate what is read as syntax from what is only passed along, by the dialect's rules.
|
|
1258
|
-
*
|
|
1259
|
-
* A quoted span becomes a placeholder with no blanks around it, so a word that a quote merely
|
|
1260
|
-
* splits — `k'i'll` — is still one word. An escaped character outside quotes loses its meaning:
|
|
1261
|
-
* kept when it is a letter or digit (`k\ill` is `kill`), replaced by `_` otherwise, so `\;` is no
|
|
1262
|
-
* separator and `\'` opens no string.
|
|
1263
|
-
*/
|
|
1264
954
|
function scan(command, dialect) {
|
|
1265
955
|
const result = { bare: '', quoted: [], embedded: [], regexes: new Set(), interpolated: new Map() };
|
|
1266
956
|
const escape = escapeOf(dialect);
|
|
1267
957
|
let index = 0;
|
|
1268
958
|
while (index < command.length) {
|
|
1269
959
|
const character = command[index];
|
|
1270
|
-
// A regular expression is a literal like a string: a quote inside it opens nothing, and a call
|
|
1271
|
-
// spelled inside it is not made.
|
|
1272
960
|
const regex = dialect === 'js' ? regexAt(command, index, result.bare) : undefined;
|
|
1273
961
|
if (regex !== undefined) {
|
|
1274
962
|
result.quoted.push(command.slice(index, regex));
|
|
@@ -1279,7 +967,6 @@ function scan(command, dialect) {
|
|
|
1279
967
|
}
|
|
1280
968
|
if (escapesOutside(dialect) && character === escape) {
|
|
1281
969
|
const next = command[index + 1] ?? '';
|
|
1282
|
-
// An escaped line break continues the line: `node \` then `-e …` is one command.
|
|
1283
970
|
if (next === '\n') {
|
|
1284
971
|
index += 2;
|
|
1285
972
|
continue;
|
|
@@ -1298,17 +985,12 @@ function scan(command, dialect) {
|
|
|
1298
985
|
index += 1;
|
|
1299
986
|
continue;
|
|
1300
987
|
}
|
|
1301
|
-
// A Python triple-quoted string's text is inside its three quotes on each side.
|
|
1302
988
|
const triple = dialect === 'hash' && (character === '"' || character === "'") && command.startsWith(character.repeat(3), index) && close - index >= 5;
|
|
1303
989
|
const raw = triple ? command.slice(index + 3, close - 2) : command.slice(index + 1, close);
|
|
1304
|
-
// The span's text as the program receives it, and the same with every escaped character
|
|
1305
|
-
// blanked, so `\$(…)` is not read as a substitution.
|
|
1306
990
|
let content = '';
|
|
1307
991
|
let live = '';
|
|
1308
|
-
// A Python string's prefix (`r"…"`, `b'…'`, `rb"…"`) touches its quote. `r` keeps backslashes.
|
|
1309
992
|
const prefix = dialect === 'hash' && (character === '"' || character === "'") ? /(?:^|[^\w])([rRbBuUfF]{1,2})$/.exec(result.bare)?.[1] : undefined;
|
|
1310
993
|
const rawString = prefix !== undefined && /r/i.test(prefix);
|
|
1311
|
-
// Ruby's and Perl's single quotes decode only \\ and \': `'ok\nkill'` keeps its backslash.
|
|
1312
994
|
const singleQuoted = (dialect === 'ruby' || dialect === 'perl') && character === "'";
|
|
1313
995
|
const escapes = character === '"' || dialect === 'js' || dialect === 'hash' || dialect === 'ruby' || dialect === 'perl' || (dialect === 'posix' && command[index - 1] === '$');
|
|
1314
996
|
for (let at = 0; at < raw.length; at += 1) {
|
|
@@ -1320,7 +1002,6 @@ function scan(command, dialect) {
|
|
|
1320
1002
|
at += 1;
|
|
1321
1003
|
continue;
|
|
1322
1004
|
}
|
|
1323
|
-
// In program code, `\u006b` and `\x6b` in a string are the character they name.
|
|
1324
1005
|
const named = (dialect === 'js' || dialect === 'hash' || dialect === 'ruby' || dialect === 'perl') && raw[at] === '\\' ? escapeAt(raw, at) : undefined;
|
|
1325
1006
|
if (named) {
|
|
1326
1007
|
content += named.text;
|
|
@@ -1328,8 +1009,6 @@ function scan(command, dialect) {
|
|
|
1328
1009
|
at += named.length - 1;
|
|
1329
1010
|
continue;
|
|
1330
1011
|
}
|
|
1331
|
-
// `\n` in a JavaScript or Python string is a line break, so `execSync('echo ok\nkill …')` runs
|
|
1332
|
-
// two commands; a backslash before a line break continues the string.
|
|
1333
1012
|
const simple = (dialect === 'js' || dialect === 'hash' || dialect === 'ruby' || dialect === 'perl') && raw[at] === '\\' ? PROGRAM_ESCAPES[raw[at + 1] ?? ''] : undefined;
|
|
1334
1013
|
if (simple !== undefined) {
|
|
1335
1014
|
content += simple;
|
|
@@ -1361,7 +1040,6 @@ function scan(command, dialect) {
|
|
|
1361
1040
|
result.embedded.push(match[1]);
|
|
1362
1041
|
}
|
|
1363
1042
|
if (character === '`' && dialect === 'js') {
|
|
1364
|
-
// Only an unescaped `${` interpolates; `\${…}` is the literal text.
|
|
1365
1043
|
for (let at = 0; at < raw.length; at += 1) {
|
|
1366
1044
|
if (raw[at] === '\\') {
|
|
1367
1045
|
at += 1;
|
|
@@ -1374,25 +1052,18 @@ function scan(command, dialect) {
|
|
|
1374
1052
|
at = end - 1;
|
|
1375
1053
|
}
|
|
1376
1054
|
}
|
|
1377
|
-
// The \`$\` of Bash's \`$'…'\` and \`$"…"\` is part of the quoting, not text: \`$'\x6bill'\` is \`kill\`.
|
|
1378
1055
|
if (dialect === 'posix' && result.bare.endsWith('$') && command[index - 1] === '$')
|
|
1379
1056
|
result.bare = result.bare.slice(0, -1);
|
|
1380
1057
|
result.quoted.push(content);
|
|
1381
1058
|
const slot = result.quoted.length - 1;
|
|
1382
|
-
// A Python f-string: the prefix touches the quote, or the previous span of a `'''` opening.
|
|
1383
1059
|
if (dialect === 'hash' && /(?:^|[^\w])[rRbB]?[fF][rRbB]?(?:\u0000\d+\u0000)*$/.test(result.bare))
|
|
1384
1060
|
result.interpolated.set(slot, /\{\{|\}\}|\{([^{}]*)\}/g);
|
|
1385
|
-
// The prefix is part of the literal, so `os.system(r"…")` reads as a call with a literal argument.
|
|
1386
1061
|
if (prefix !== undefined)
|
|
1387
1062
|
result.bare = result.bare.slice(0, -prefix.length);
|
|
1388
|
-
// Ruby runs "#{…}" in a double-quoted string; Python prints it.
|
|
1389
1063
|
if (dialect === 'ruby' && character === '"')
|
|
1390
1064
|
result.interpolated.set(slot, /#\{([^{}]*)\}/g);
|
|
1391
|
-
// Perl runs code inside a double-quoted string's `@{[ … ]}`.
|
|
1392
1065
|
if (dialect === 'perl' && character === '"')
|
|
1393
1066
|
result.interpolated.set(slot, /@\{\[([^\]]*)\]\}/g);
|
|
1394
|
-
// Python and Ruby join adjacent literals at compile time: `os.system("ki" "ll 80632")` runs
|
|
1395
|
-
// `kill 80632`. The span joins the literal before it, which keeps its slot.
|
|
1396
1067
|
const previous = dialect === 'hash' || dialect === 'ruby' ? /\u0000(\d+)\u0000\s*$/.exec(result.bare) : null;
|
|
1397
1068
|
if (previous && !result.regexes.has(Number(previous[1]))) {
|
|
1398
1069
|
const earlier = Number(previous[1]);
|
|
@@ -1411,7 +1082,6 @@ function scan(command, dialect) {
|
|
|
1411
1082
|
}
|
|
1412
1083
|
return result;
|
|
1413
1084
|
}
|
|
1414
|
-
/** The command a line runs last, by name: what a here-document on that line is fed to. */
|
|
1415
1085
|
function receiver(line) {
|
|
1416
1086
|
const tokens = (line.split(/[;|&]/).pop() ?? '').trim().split(/\s+/).filter(Boolean);
|
|
1417
1087
|
let at = 0;
|
|
@@ -1419,32 +1089,18 @@ function receiver(line) {
|
|
|
1419
1089
|
at += 1;
|
|
1420
1090
|
if (at >= tokens.length)
|
|
1421
1091
|
return { word: '', args: [] };
|
|
1422
|
-
// Simple quoting is enough here: the arguments only decide whether the reader runs its input.
|
|
1423
1092
|
const args = tokens.slice(at + 1).map(token => token.replace(/^(['"])([\s\S]*)\1$/, '$2'));
|
|
1424
1093
|
return { word: commandName(tokens[at]), args };
|
|
1425
1094
|
}
|
|
1426
|
-
/**
|
|
1427
|
-
* Take here-document and here-string bodies out of a command.
|
|
1428
|
-
*
|
|
1429
|
-
* A body is input to the command that reads it, not a list of statements: `cat <<'EOF'` followed
|
|
1430
|
-
* by `kill 80632` prints that line. Left in place, every line of it would be judged as a command
|
|
1431
|
-
* of its own. Two things in a body do run and are handed back: the whole body when the reader is
|
|
1432
|
-
* an interpreter, and `$( … )` or backtick substitutions when the delimiter is unquoted (and in a
|
|
1433
|
-
* PowerShell `@" … "@` string), which the shell expands before the reader sees them. POSIX shells
|
|
1434
|
-
* have here-documents and PowerShell has here-strings; cmd.exe has neither.
|
|
1435
|
-
*/
|
|
1436
1095
|
function withoutDocuments(command, shell) {
|
|
1437
1096
|
const result = { text: command, code: [], shell: [] };
|
|
1438
1097
|
const substitutions = (body) => {
|
|
1439
|
-
// An escaped `$` or backtick is literal text, in a here-document as in a quoted string.
|
|
1440
1098
|
const live = body.replace(/[\\`][$`]/g, ' ');
|
|
1441
1099
|
result.shell.push(...substitutionBodies(live));
|
|
1442
1100
|
for (const match of live.matchAll(/`([^`]+)`/g))
|
|
1443
1101
|
result.shell.push(match[1]);
|
|
1444
1102
|
};
|
|
1445
1103
|
if (shell === 'powershell') {
|
|
1446
|
-
// The body stays as a single-quoted literal, so a command that evaluates its argument
|
|
1447
|
-
// (`Invoke-Expression @'…'@`) is judged on it, and any other command takes it as data.
|
|
1448
1104
|
const literal = (body) => `'${body.replace(/'/g, "''")}'`;
|
|
1449
1105
|
result.text = result.text.replace(/@'[ \t]*\r?\n([\s\S]*?)\r?\n'@/g, (_, body) => literal(body));
|
|
1450
1106
|
result.text = result.text.replace(/@"[ \t]*\r?\n([\s\S]*?)\r?\n"@/g, (_, body) => { substitutions(body); return literal(body); });
|
|
@@ -1452,11 +1108,8 @@ function withoutDocuments(command, shell) {
|
|
|
1452
1108
|
}
|
|
1453
1109
|
if (shell !== 'posix')
|
|
1454
1110
|
return result;
|
|
1455
|
-
// `<<<` is a here-string, a single quoted or bare word, and stays.
|
|
1456
|
-
// The delimiter is a shell word: `END-OF-TEXT`, `'my.eof'`, `\\EOF`, not only word characters.
|
|
1457
1111
|
const opener = /(?<!<)<<(-?)[ \t]*(?:(['"])([^'"\n]+)\2|(\\?)([^\s;|&<>()'"`$\\]+))/g;
|
|
1458
1112
|
for (let match = opener.exec(result.text); match; match = opener.exec(result.text)) {
|
|
1459
|
-
// `echo "a <<EOF"` opens nothing: the operator only counts where the shell reads syntax.
|
|
1460
1113
|
if (insideLiteral(result.text, match.index))
|
|
1461
1114
|
continue;
|
|
1462
1115
|
const delimiter = match[3] ?? match[5];
|
|
@@ -1466,23 +1119,16 @@ function withoutDocuments(command, shell) {
|
|
|
1466
1119
|
if (lineEnd === -1)
|
|
1467
1120
|
break;
|
|
1468
1121
|
const lines = result.text.slice(lineEnd + 1).split('\n');
|
|
1469
|
-
// The closing line is the delimiter exactly: `EOF ` with a trailing blank is body text. `<<-`
|
|
1470
|
-
// strips only leading tabs; a CRLF line's \r is the line ending, not text.
|
|
1471
1122
|
const closing = lines.findIndex(line => (match[1] ? line.replace(/^\t+/, '') : line).replace(/\r$/, '') === delimiter);
|
|
1472
1123
|
if (closing === -1)
|
|
1473
1124
|
continue;
|
|
1474
1125
|
const body = lines.slice(0, closing).join('\n');
|
|
1475
1126
|
const { word, args } = receiver(result.text.slice(lineStart, match.index));
|
|
1476
|
-
// The body is code only for an interpreter that reads its input (`bash <<EOF`, not
|
|
1477
|
-
// `bash -c 'cat' <<EOF`); the outer shell expands an unquoted body's substitutions either way.
|
|
1478
1127
|
const runsBody = INTERPRETERS.has(word) && readsInput(word, withoutRedirections(args));
|
|
1479
1128
|
if (runsBody)
|
|
1480
1129
|
result.code.push({ word, body: inputProgram(word, withoutRedirections(args), body) });
|
|
1481
1130
|
if (!literal)
|
|
1482
1131
|
substitutions(body);
|
|
1483
|
-
// For any other reader the body is its input, which a pipeline may carry on to an interpreter
|
|
1484
|
-
// (`cat <<'EOF' | bash`): it stays on the line as a here-string, single-quoted so nothing in it
|
|
1485
|
-
// is read as syntax.
|
|
1486
1132
|
const operator = runsBody ? '' : `<<<'${body.replace(/'/g, "'\\''")}'`;
|
|
1487
1133
|
const line = result.text.slice(0, match.index) + operator + result.text.slice(match.index + match[0].length, lineEnd + 1);
|
|
1488
1134
|
result.text = line + lines.slice(closing + 1).join('\n');
|
|
@@ -1490,10 +1136,6 @@ function withoutDocuments(command, shell) {
|
|
|
1490
1136
|
}
|
|
1491
1137
|
return result;
|
|
1492
1138
|
}
|
|
1493
|
-
/**
|
|
1494
|
-
* Whether `position` in a POSIX command line falls inside literal text — a quoted span, an escaped
|
|
1495
|
-
* character, or a comment — where an operator is not an operator.
|
|
1496
|
-
*/
|
|
1497
1139
|
function insideLiteral(text, position) {
|
|
1498
1140
|
let index = 0;
|
|
1499
1141
|
let afterEscape = false;
|
|
@@ -1526,24 +1168,13 @@ function insideLiteral(text, position) {
|
|
|
1526
1168
|
}
|
|
1527
1169
|
return false;
|
|
1528
1170
|
}
|
|
1529
|
-
/** A command word as it is looked up: no directory, no `.exe`, lower case. */
|
|
1530
1171
|
function commandName(word) {
|
|
1531
1172
|
const name = word.replace(/^.*[\\/]/, '').replace(/\.(?:exe|cmd|bat|ps1)$/i, '').toLowerCase();
|
|
1532
|
-
// An interpreter named with its version (`python3.12`, `ruby3.2`, `perl5.36`) is that interpreter.
|
|
1533
1173
|
const versioned = /^(python|ruby|perl|node|nodejs|pwsh|bash|zsh)[\d.]+$/.exec(name);
|
|
1534
1174
|
if (!versioned)
|
|
1535
1175
|
return name;
|
|
1536
1176
|
return versioned[1] === 'python' ? 'python3' : versioned[1];
|
|
1537
1177
|
}
|
|
1538
|
-
/**
|
|
1539
|
-
* Whether `command` ends a process when run in `shell`, as opposed to merely containing the words.
|
|
1540
|
-
*
|
|
1541
|
-
* The command is cut into statements at the separators and grouping characters, and each
|
|
1542
|
-
* statement is judged by the word in command position — after any assignment, keyword, or wrapper
|
|
1543
|
-
* such as `sudo`, `xargs`, or `Start-Process`. An interpreter's code argument is judged by that
|
|
1544
|
-
* interpreter's rules. `comments` is off for text whose comments were already removed by another
|
|
1545
|
-
* language's rules. `depth` bounds the recursion.
|
|
1546
|
-
*/
|
|
1547
1178
|
function terminates(command, depth, shell, comments = true) {
|
|
1548
1179
|
if (depth > 4)
|
|
1549
1180
|
return false;
|
|
@@ -1553,16 +1184,12 @@ function terminates(command, depth, shell, comments = true) {
|
|
|
1553
1184
|
return true;
|
|
1554
1185
|
if (documents.shell.some(inner => terminates(inner, depth + 1, shell)))
|
|
1555
1186
|
return true;
|
|
1556
|
-
// A shell comment runs nothing: `echo built # then kill 80632`.
|
|
1557
1187
|
const { bare, quoted, embedded } = scan(comments ? stripComments(documents.text, shell) : documents.text, shell);
|
|
1558
1188
|
if (callsKill(bare))
|
|
1559
1189
|
return true;
|
|
1560
1190
|
if (embedded.some(inner => terminates(inner, depth + 1, shell)))
|
|
1561
1191
|
return true;
|
|
1562
1192
|
const unquote = (token) => token.replace(/\u0000(\d+)\u0000/g, (_, slot) => quoted[Number(slot)] ?? '');
|
|
1563
|
-
// The `&` of a redirection (`2>&1`, `&>file`) joins it to its operand rather than ending a statement.
|
|
1564
|
-
// A redirection needs no blank before it: `kill>/dev/null 80632` runs `kill`. A word that is
|
|
1565
|
-
// only a descriptor number (`2>`) stays attached to its operator.
|
|
1566
1193
|
const joined = bare.replace(/([<>])&/g, '$1\u0002').replace(/&>/g, '\u0002>')
|
|
1567
1194
|
.replace(/(?<=[^\s\d<>\u0002;|&(){}])(?=[<>])/g, ' ');
|
|
1568
1195
|
if (pipedIntoInterpreter(joined, unquote, depth))
|
|
@@ -1572,18 +1199,12 @@ function terminates(command, depth, shell, comments = true) {
|
|
|
1572
1199
|
if (wordsTerminate(raw, raw.map(unquote), depth, shell))
|
|
1573
1200
|
return true;
|
|
1574
1201
|
}
|
|
1575
|
-
// Backtick substitution in a POSIX shell runs its contents.
|
|
1576
1202
|
if (shell === 'posix')
|
|
1577
1203
|
for (const match of bare.matchAll(/`([^`]*)`/g))
|
|
1578
1204
|
if (terminates(match[1], depth + 1, shell))
|
|
1579
1205
|
return true;
|
|
1580
1206
|
return false;
|
|
1581
1207
|
}
|
|
1582
|
-
/**
|
|
1583
|
-
* Whether an interpreter runs what arrives on its input: given no code and no script file
|
|
1584
|
-
* (`bash`, `node`, `python3`), or told to read it (`-`, `bash -s`, `powershell -Command -`), or
|
|
1585
|
-
* `iex` with no argument, which runs its pipeline input. `node script.js` reads a file instead.
|
|
1586
|
-
*/
|
|
1587
1208
|
function readsInput(word, args) {
|
|
1588
1209
|
if (shellNoExec(word, args) || rubySyntaxOnly(word, args))
|
|
1589
1210
|
return false;
|
|
@@ -1591,7 +1212,6 @@ function readsInput(word, args) {
|
|
|
1591
1212
|
return args.length === 0;
|
|
1592
1213
|
if (word === 'eval')
|
|
1593
1214
|
return false;
|
|
1594
|
-
// `node --check` parses its input without running it.
|
|
1595
1215
|
if ((word === 'node' || word === 'nodejs') && nodeChecksOnly(args))
|
|
1596
1216
|
return false;
|
|
1597
1217
|
if (word === 'cmd')
|
|
@@ -1621,7 +1241,6 @@ function readsInput(word, args) {
|
|
|
1621
1241
|
}
|
|
1622
1242
|
return true;
|
|
1623
1243
|
}
|
|
1624
|
-
/** The text a statement's here-strings feed to its command: `<<< 'text'` or `<<<text`. */
|
|
1625
1244
|
function hereStrings(words) {
|
|
1626
1245
|
const fed = [];
|
|
1627
1246
|
for (let index = 0; index < words.length; index += 1) {
|
|
@@ -1637,7 +1256,6 @@ function hereStrings(words) {
|
|
|
1637
1256
|
}
|
|
1638
1257
|
return fed;
|
|
1639
1258
|
}
|
|
1640
|
-
/** A statement's arguments with its redirections and their operands removed: they are not arguments. */
|
|
1641
1259
|
function withoutRedirections(words) {
|
|
1642
1260
|
const kept = [];
|
|
1643
1261
|
for (let index = 0; index < words.length; index += 1) {
|
|
@@ -1651,20 +1269,12 @@ function withoutRedirections(words) {
|
|
|
1651
1269
|
}
|
|
1652
1270
|
return kept;
|
|
1653
1271
|
}
|
|
1654
|
-
/** Commands whose output is their arguments: what they print is what a pipe passes on. */
|
|
1655
1272
|
const PRINTERS = new Set(['echo', 'write-output', 'write', 'echo.']);
|
|
1656
|
-
/** An argument quoted as Bash's `printf %q` quotes it: every character that means something escaped. */
|
|
1657
1273
|
function shellQuote(value) {
|
|
1658
1274
|
if (value === '')
|
|
1659
1275
|
return "''";
|
|
1660
1276
|
return value.replace(/[^A-Za-z0-9_\/.,:=@%+-]/g, character => character === '\n' ? "$'\\n'" : `\\${character}`);
|
|
1661
1277
|
}
|
|
1662
|
-
/**
|
|
1663
|
-
* What `echo` writes. Its leading `-n`, `-e` and `-E` options are not output. Backslash escapes
|
|
1664
|
-
* are expanded unless the last of `-e`/`-E` is `-E`: Bash expands them with `-e`, and `sh` (dash)
|
|
1665
|
-
* and `zsh` always do, so expanding when unsure can only show more lines. PowerShell's printers
|
|
1666
|
-
* expand nothing here; their escapes were resolved as the string's own text.
|
|
1667
|
-
*/
|
|
1668
1278
|
function echoOutput(word, args) {
|
|
1669
1279
|
let at = 0;
|
|
1670
1280
|
let expand = word === 'echo' || word === 'echo.';
|
|
@@ -1685,18 +1295,13 @@ function echoOutput(word, args) {
|
|
|
1685
1295
|
const text = args.slice(at).join(' ');
|
|
1686
1296
|
if (!expand)
|
|
1687
1297
|
return text;
|
|
1688
|
-
// Bash's echo prints escapes as written unless told `-e`; dash's and zsh's expand them. Either
|
|
1689
|
-
// reading may be the dangerous one (`\x27; kill …; \x27` is quoting only once expanded), so
|
|
1690
|
-
// an echo that says neither writes both, each on its own lines.
|
|
1691
1298
|
if (word === 'echo' && !explicit) {
|
|
1692
1299
|
const expanded = echoEscapes(text);
|
|
1693
1300
|
return expanded === text ? text : `${text}\n${expanded}`;
|
|
1694
1301
|
}
|
|
1695
1302
|
return echoEscapes(text);
|
|
1696
1303
|
}
|
|
1697
|
-
/** `echo -e`'s escapes, decoded: `\0NNN`, `\xHH`, `\uHHHH`, `\UHHHHHHHH` and the letters. */
|
|
1698
1304
|
function echoEscapes(text) {
|
|
1699
|
-
// Bash's echo -e (and printf's %b) also decodes \uHHHH and \UHHHHHHHH: `\u006bill` is `kill`.
|
|
1700
1305
|
return text.replace(/\\(?:0([0-7]{0,3})|x([0-9a-fA-F]{1,2})|u([0-9a-fA-F]{1,4})|U([0-9a-fA-F]{1,8})|([abefnrtv\\]))/g, (whole, octal, hex, short, long, letter) => {
|
|
1701
1306
|
if (octal !== undefined)
|
|
1702
1307
|
return String.fromCharCode(parseInt(octal || '0', 8));
|
|
@@ -1710,10 +1315,6 @@ function echoEscapes(text) {
|
|
|
1710
1315
|
return { a: '\u0007', b: '\b', e: '\u001b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\' }[letter];
|
|
1711
1316
|
});
|
|
1712
1317
|
}
|
|
1713
|
-
/**
|
|
1714
|
-
* A printf format's escapes, which differ from `echo -e`'s: `\NNN` is octal with one to three
|
|
1715
|
-
* digits and no leading zero (`\153` is k), and `\uHHHH` and `\UHHHHHHHH` name code points.
|
|
1716
|
-
*/
|
|
1717
1318
|
function printfEscapes(format) {
|
|
1718
1319
|
return format.replace(/\\(?:([0-7]{1,3})|x([0-9a-fA-F]{1,2})|u([0-9a-fA-F]{1,4})|U([0-9a-fA-F]{1,8})|([abefnrtv\\"'?]))/g, (whole, octal, hex, short, long, letter) => {
|
|
1719
1320
|
if (octal !== undefined)
|
|
@@ -1728,33 +1329,20 @@ function printfEscapes(format) {
|
|
|
1728
1329
|
return { a: '\u0007', b: '\b', e: '\u001b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\', '"': '"', "'": "'", '?': '?' }[letter];
|
|
1729
1330
|
});
|
|
1730
1331
|
}
|
|
1731
|
-
/**
|
|
1732
|
-
* What `printf FORMAT ARGS…` writes: each conversion in the format takes the next argument, the
|
|
1733
|
-
* format is reused while arguments remain, and `\n`, `\t` and `%%` become what they name.
|
|
1734
|
-
*/
|
|
1735
1332
|
function printfOutput(args) {
|
|
1736
|
-
// The format's own escapes are expanded; an argument's are not, except through %b.
|
|
1737
1333
|
const [written = '', ...values] = args;
|
|
1738
1334
|
const format = printfEscapes(written);
|
|
1739
1335
|
const conversion = /%(?:(%)|([-+ #0']*)(\*|\d*)(?:\.(\*|\d*))?([bcdiouxXeEfFgGsq]))/g;
|
|
1740
1336
|
let out = '';
|
|
1741
1337
|
let next = 0;
|
|
1742
|
-
// The format is reused only while a pass consumes arguments: `%%` consumes none, so
|
|
1743
|
-
// `printf 'echo %%s' x` is one pass. Each pass takes at least one, so this ends.
|
|
1744
1338
|
for (;;) {
|
|
1745
1339
|
const before = next;
|
|
1746
1340
|
out += format.replace(conversion, (_, percent, flags, width, precision, kind) => {
|
|
1747
1341
|
if (percent)
|
|
1748
1342
|
return '%';
|
|
1749
|
-
// A `*` width or precision takes its value from the next argument.
|
|
1750
|
-
// Widths and numeric precisions are bounded: they only pad with blanks or zeros, and rendering
|
|
1751
|
-
// a ten-million-character field would stall the engine's thread before approval. A string's
|
|
1752
|
-
// precision (`%.5000s`) only cuts its argument, which is already in memory, so it is taken as
|
|
1753
|
-
// written: bounding it would drop text that the shell prints and runs.
|
|
1754
1343
|
const bound = (value) => Math.max(-PRINTF_FIELD_MAX, Math.min(PRINTF_FIELD_MAX, Number.isFinite(value) ? Math.trunc(value) : 0));
|
|
1755
1344
|
const size = bound(width === '*' ? Number(values[next++] ?? 0) : width === '' ? 0 : Number(width));
|
|
1756
1345
|
const written = precision === undefined ? undefined : precision === '*' ? Number(values[next++] ?? 0) : precision === '' ? 0 : Number(precision);
|
|
1757
|
-
// A negative precision (`%.*s` given -1) counts as none: the whole argument is printed.
|
|
1758
1346
|
const places = written === undefined || written < 0 ? undefined
|
|
1759
1347
|
: /[sbq]/.test(kind) ? (Number.isFinite(written) ? Math.max(0, Math.trunc(written)) : undefined)
|
|
1760
1348
|
: Math.max(0, bound(written));
|
|
@@ -1762,27 +1350,20 @@ function printfOutput(args) {
|
|
|
1762
1350
|
});
|
|
1763
1351
|
if (next === before || next >= values.length)
|
|
1764
1352
|
break;
|
|
1765
|
-
// Past the budget the rest is not rendered, but it is still printed: keep the remaining
|
|
1766
|
-
// arguments as lines of their own, so a command among them is still judged.
|
|
1767
1353
|
if (out.length > PRINTF_OUTPUT_MAX)
|
|
1768
1354
|
return `${out}\n${values.slice(next).join('\n')}`;
|
|
1769
1355
|
}
|
|
1770
1356
|
return out;
|
|
1771
1357
|
}
|
|
1772
|
-
/** One printf conversion of one argument, as Bash renders it. */
|
|
1773
|
-
/** The widest printf field rendered, and the most output kept, before the rest is judged unread. */
|
|
1774
1358
|
const PRINTF_FIELD_MAX = 4096;
|
|
1775
1359
|
const PRINTF_OUTPUT_MAX = 200_000;
|
|
1776
|
-
/** The first `count` UTF-8 bytes of `text`. */
|
|
1777
1360
|
function bytePrefix(text, count) {
|
|
1778
1361
|
return Buffer.from(text, 'utf8').subarray(0, count).toString('utf8');
|
|
1779
1362
|
}
|
|
1780
1363
|
function formatConversion(kind, value, precision) {
|
|
1781
|
-
// A zero integer with precision 0 prints no digits at all: `printf '%.0dkill' 0` is `kill`.
|
|
1782
1364
|
if (precision === 0 && /[diouxX]/.test(kind) && printfInteger(value) === 0)
|
|
1783
1365
|
return '';
|
|
1784
1366
|
switch (kind) {
|
|
1785
|
-
// A string's precision counts bytes, as printf does: `%.2s` of `é"` is the two bytes of é.
|
|
1786
1367
|
case 's': return precision === undefined ? value : bytePrefix(value, precision);
|
|
1787
1368
|
case 'b': {
|
|
1788
1369
|
const text = percentBEscapes(value);
|
|
@@ -1809,12 +1390,7 @@ function formatConversion(kind, value, precision) {
|
|
|
1809
1390
|
default: return String(Number(value) || 0);
|
|
1810
1391
|
}
|
|
1811
1392
|
}
|
|
1812
|
-
/**
|
|
1813
|
-
* A `%b` argument's escapes: those of `echo -e`, and octal both as `\0NNN` and as `\NNN` with one to
|
|
1814
|
-
* three digits (`\153` is k), which echo does not take.
|
|
1815
|
-
*/
|
|
1816
1393
|
function percentBEscapes(value) {
|
|
1817
|
-
// One pass, so a backslash an escape produces is not decoded again and `\\153` stays `\153`.
|
|
1818
1394
|
return value.replace(/\\(?:0([0-7]{0,3})|([1-7][0-7]{0,2})|x([0-9a-fA-F]{1,2})|u([0-9a-fA-F]{1,4})|U([0-9a-fA-F]{1,8})|([abefnrtv\\]))/g, (whole, zero, octal, hex, short, long, letter) => {
|
|
1819
1395
|
if (zero !== undefined || octal !== undefined)
|
|
1820
1396
|
return String.fromCharCode(parseInt(zero || octal || '0', 8) & 0xff);
|
|
@@ -1828,7 +1404,6 @@ function percentBEscapes(value) {
|
|
|
1828
1404
|
return { a: '\u0007', b: '\b', e: '\u001b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\' }[letter];
|
|
1829
1405
|
});
|
|
1830
1406
|
}
|
|
1831
|
-
/** An argument read as printf reads an integer: decimal, `0x` hex, leading-zero octal, or `'c` a character's code. */
|
|
1832
1407
|
function printfInteger(value) {
|
|
1833
1408
|
const text = value.trim();
|
|
1834
1409
|
const character = /^['"]([\s\S])/.exec(text);
|
|
@@ -1841,7 +1416,6 @@ function printfInteger(value) {
|
|
|
1841
1416
|
const number = parseInt(text, 10);
|
|
1842
1417
|
return Number.isNaN(number) ? 0 : number;
|
|
1843
1418
|
}
|
|
1844
|
-
/** Pad to a printf width: left with spaces, right with `-`, or with zeros for a number with `0`. */
|
|
1845
1419
|
function pad(text, width, flags, numeric) {
|
|
1846
1420
|
if (text.length >= Math.abs(width))
|
|
1847
1421
|
return text;
|
|
@@ -1851,11 +1425,6 @@ function pad(text, width, flags, numeric) {
|
|
|
1851
1425
|
return text.startsWith('-') ? `-${text.slice(1).padStart(width - 1, '0')}` : text.padStart(width, '0');
|
|
1852
1426
|
return text.padStart(width);
|
|
1853
1427
|
}
|
|
1854
|
-
/**
|
|
1855
|
-
* The text a pipeline stage writes, when it can be read from the stage itself: the arguments of
|
|
1856
|
-
* `echo` or `printf`, the here-string `cat` copies, or — in PowerShell — a stage that is nothing
|
|
1857
|
-
* but literals, which outputs them. Undefined when the output is not written out in the command.
|
|
1858
|
-
*/
|
|
1859
1428
|
function producedText(raw, words) {
|
|
1860
1429
|
if (raw.length === 0)
|
|
1861
1430
|
return undefined;
|
|
@@ -1872,13 +1441,6 @@ function producedText(raw, words) {
|
|
|
1872
1441
|
}
|
|
1873
1442
|
return undefined;
|
|
1874
1443
|
}
|
|
1875
|
-
/**
|
|
1876
|
-
* Whether a regular expression could take super-linear time: a group with a repeat after it
|
|
1877
|
-
* (`(a+)+`, `(a|a)*`), a back-reference, or more than one repeat of any kind (`a*a*b` is cubic on a
|
|
1878
|
-
* run of a's, `a?a?a?aaa` exponential). Matching runs on the engine's thread before approval, so a
|
|
1879
|
-
* pattern that might stall is not run: the input is kept whole instead. Bracket contents and
|
|
1880
|
-
* escaped characters are not operators and are skipped.
|
|
1881
|
-
*/
|
|
1882
1444
|
function mayBacktrack(source) {
|
|
1883
1445
|
let open = 0;
|
|
1884
1446
|
for (let index = 0; index < source.length; index += 1) {
|
|
@@ -1902,25 +1464,16 @@ function mayBacktrack(source) {
|
|
|
1902
1464
|
}
|
|
1903
1465
|
if (character === ')' && /[*+?{]/.test(source[index + 1] ?? ''))
|
|
1904
1466
|
return true;
|
|
1905
|
-
// A quantifier; `*?` and `+?` are one lazy quantifier, not two.
|
|
1906
1467
|
if ((character === '*' || character === '+' || character === '?' || (character === '{' && /^\{\d*,?\d*\}/.test(source.slice(index))))
|
|
1907
1468
|
&& !/[*+?}]/.test(source[index - 1] ?? ''))
|
|
1908
1469
|
open += 1;
|
|
1909
1470
|
}
|
|
1910
1471
|
return open > 1;
|
|
1911
1472
|
}
|
|
1912
|
-
/** The ranges POSIX bracket classes stand for, written for a JavaScript character class. */
|
|
1913
1473
|
const POSIX_CLASSES = {
|
|
1914
1474
|
alpha: 'A-Za-z', digit: '0-9', alnum: 'A-Za-z0-9', upper: 'A-Z', lower: 'a-z', space: ' \\t\\n\\r\\f\\v',
|
|
1915
1475
|
blank: ' \\t', punct: '!-\\/:-@\\[-`{-~', xdigit: '0-9A-Fa-f', cntrl: '\\x00-\\x1f\\x7f', print: '\\x20-\\x7e', graph: '\\x21-\\x7e',
|
|
1916
1476
|
};
|
|
1917
|
-
/**
|
|
1918
|
-
* A grep pattern as a JavaScript regular expression, or undefined when it uses something this
|
|
1919
|
-
* translation does not reproduce. Basic syntax (the default) treats `+ ? | ( ) { }` as literals
|
|
1920
|
-
* and their backslashed forms as operators; extended syntax (`-E`) the reverse. Bracket
|
|
1921
|
-
* expressions keep their POSIX meaning: `[[:digit:]]` is a digit, and a backslash inside one is a
|
|
1922
|
-
* backslash. `\<` and `\>` are word edges.
|
|
1923
|
-
*/
|
|
1924
1477
|
function grepPattern(pattern, extended) {
|
|
1925
1478
|
let out = '';
|
|
1926
1479
|
for (let index = 0; index < pattern.length; index += 1) {
|
|
@@ -1942,8 +1495,6 @@ function grepPattern(pattern, extended) {
|
|
|
1942
1495
|
out += next;
|
|
1943
1496
|
continue;
|
|
1944
1497
|
}
|
|
1945
|
-
// Other letters and digits, and \` and \', are GNU extensions or unspecified (\` anchors the
|
|
1946
|
-
// start): not translated, so the input passes whole. Escaped punctuation is the character.
|
|
1947
1498
|
if (/[A-Za-z0-9`']/.test(next))
|
|
1948
1499
|
return undefined;
|
|
1949
1500
|
out += `\\${next}`;
|
|
@@ -1975,7 +1526,6 @@ function grepPattern(pattern, extended) {
|
|
|
1975
1526
|
return undefined;
|
|
1976
1527
|
const opened = pattern.slice(index + 1, index + 1 + (pattern[index + 1] === '^' ? 1 : 0));
|
|
1977
1528
|
const literalClose = pattern[index + 1 + opened.length] === ']' ? '\\]' : '';
|
|
1978
|
-
// The leading `]` was skipped before the members were collected, so `body` holds all the rest.
|
|
1979
1529
|
out += `[${opened}${literalClose}${body}]`;
|
|
1980
1530
|
index = close;
|
|
1981
1531
|
continue;
|
|
@@ -1986,16 +1536,9 @@ function grepPattern(pattern, extended) {
|
|
|
1986
1536
|
}
|
|
1987
1537
|
out += character;
|
|
1988
1538
|
}
|
|
1989
|
-
// GNU's `{,M}` is `{0,M}`; JavaScript would read `{,M}` as literal text.
|
|
1990
1539
|
return out.replace(/(^|[^\\])\{,(\d+)\}/g, '$1{0,$2}');
|
|
1991
1540
|
}
|
|
1992
|
-
/** Stages that pass their input on unchanged, or only drop or reorder lines of it. */
|
|
1993
1541
|
const FORWARDERS = new Set(['cat', 'tee', 'sort', 'uniq', 'head', 'tail', 'more', 'less', 'out-string', 'tee-object', 'sort-object', 'select-object']);
|
|
1994
|
-
/**
|
|
1995
|
-
* What `head -c N` or `tail -c N` keeps of its input: the first N bytes (all but the last N for
|
|
1996
|
-
* `head -c -N`), the last N (from byte N on for `tail -c +N`). Sizes take K, M, G suffixes. A file
|
|
1997
|
-
* operand is read instead of the input, so the output is then unknown.
|
|
1998
|
-
*/
|
|
1999
1542
|
function bytesKept(word, args, upstream) {
|
|
2000
1543
|
let size;
|
|
2001
1544
|
for (let index = 0; index < args.length; index += 1) {
|
|
@@ -2021,26 +1564,17 @@ function bytesKept(word, args, upstream) {
|
|
|
2021
1564
|
if (input === undefined)
|
|
2022
1565
|
return undefined;
|
|
2023
1566
|
const parsed = /^([+-]?)(\d+)(b|kB|K|KiB|MB|M|MiB|GB|G|GiB)?$/.exec(size ?? '');
|
|
2024
|
-
// A size this does not read keeps the whole input, which can only refuse more.
|
|
2025
1567
|
if (!parsed)
|
|
2026
1568
|
return input;
|
|
2027
1569
|
const unit = { b: 512, kB: 1000, K: 1024, KiB: 1024, MB: 1e6, M: 1048576, MiB: 1048576, GB: 1e9, G: 1073741824, GiB: 1073741824 }[parsed[3] ?? ''] ?? 1;
|
|
2028
1570
|
const count = Number(parsed[2]) * unit;
|
|
2029
|
-
// Counted in UTF-8 bytes, as the tools count: `é` is two.
|
|
2030
1571
|
const bytes = Buffer.from(input, 'utf8');
|
|
2031
1572
|
const kept = word === 'head'
|
|
2032
1573
|
? (parsed[1] === '-' ? bytes.subarray(0, Math.max(0, bytes.length - count)) : bytes.subarray(0, count))
|
|
2033
1574
|
: (parsed[1] === '+' ? bytes.subarray(Math.max(0, count - 1)) : count === 0 ? bytes.subarray(0, 0) : bytes.subarray(-count));
|
|
2034
1575
|
return kept.toString('utf8');
|
|
2035
1576
|
}
|
|
2036
|
-
/** Stages that keep the lines of their input that match a pattern. */
|
|
2037
1577
|
const FILTERS = new Set(['grep', 'egrep', 'fgrep', 'select-string', 'sls', 'findstr']);
|
|
2038
|
-
/**
|
|
2039
|
-
* The text stage `at` of a pipeline writes, when it can be known from the command line: what a
|
|
2040
|
-
* printer writes, carried through stages that forward their input (`cat`, `tee`, `sort`, `head`,
|
|
2041
|
-
* … — taken to forward all of it) and through `grep`, whose pattern is applied. Anything else
|
|
2042
|
-
* (`sed`, `awk`, a command that reads files) makes the text unknown.
|
|
2043
|
-
*/
|
|
2044
1578
|
function stageOutput(stages, at, unquote) {
|
|
2045
1579
|
if (at < 0)
|
|
2046
1580
|
return undefined;
|
|
@@ -2049,7 +1583,6 @@ function stageOutput(stages, at, unquote) {
|
|
|
2049
1583
|
const produced = producedText(raw, raw.map(unquote));
|
|
2050
1584
|
if (produced !== undefined)
|
|
2051
1585
|
return produced;
|
|
2052
|
-
// A wrapped stage (`env cat`, `sudo -u me tee`) produces what the command it runs produces.
|
|
2053
1586
|
if (words[0] !== undefined && (PREFIXES.has(commandName(words[0])) || /^[A-Za-z_]\w*=/.test(words[0]))) {
|
|
2054
1587
|
for (const reader of stageReaders(words)) {
|
|
2055
1588
|
if (PREFIXES.has(reader.word))
|
|
@@ -2064,16 +1597,12 @@ function stageOutput(stages, at, unquote) {
|
|
|
2064
1597
|
}
|
|
2065
1598
|
const word = commandName(words[0] ?? '');
|
|
2066
1599
|
const args = words.slice(1);
|
|
2067
|
-
// A stage's input is its own here-string or here-document when it has one, the stage before it
|
|
2068
|
-
// otherwise.
|
|
2069
1600
|
const fed = hereStrings(raw.map(unquote).slice(1));
|
|
2070
1601
|
const upstream = () => fed.length > 0 ? fed.join('\n') : stageOutput(stages, at - 1, unquote);
|
|
2071
1602
|
if ((word === 'head' || word === 'tail') && args.some(argument => /^(?:-c|--bytes)(?:=|$)|^-c./.test(argument))) {
|
|
2072
|
-
// `tail -c 10` and `head -c 4` cut bytes, which can strip a prefix: `xkill 80632` becomes `kill 80632`.
|
|
2073
1603
|
return bytesKept(word, args, upstream);
|
|
2074
1604
|
}
|
|
2075
1605
|
if (FORWARDERS.has(word)) {
|
|
2076
|
-
// A forwarder named with a file reads the file, not its input.
|
|
2077
1606
|
if ((word === 'cat' || word === 'sort' || word === 'uniq' || word === 'head' || word === 'tail') && args.some(argument => !argument.startsWith('-') && !/^\d+$/.test(argument)))
|
|
2078
1607
|
return undefined;
|
|
2079
1608
|
return upstream();
|
|
@@ -2090,7 +1619,6 @@ function stageOutput(stages, at, unquote) {
|
|
|
2090
1619
|
'--line-number': 'n', '--byte-offset': 'b', '--with-filename': 'H', '--no-filename': 'h', '--line-regexp': 'x', '--word-regexp': 'w',
|
|
2091
1620
|
'--invert-match': 'v', '--ignore-case': 'i', '--fixed-strings': 'F', '--extended-regexp': 'E', '--perl-regexp': 'P', '--null-data': 'z',
|
|
2092
1621
|
};
|
|
2093
|
-
// `-e` may be given several times, and a line is kept if it matches any of them.
|
|
2094
1622
|
const patterns = [];
|
|
2095
1623
|
let named = false;
|
|
2096
1624
|
let maxCount = Number.POSITIVE_INFINITY;
|
|
@@ -2114,7 +1642,6 @@ function stageOutput(stages, at, unquote) {
|
|
|
2114
1642
|
named = true;
|
|
2115
1643
|
continue;
|
|
2116
1644
|
}
|
|
2117
|
-
// Patterns from a file are not on the command line.
|
|
2118
1645
|
if (argument === '-f' || argument.startsWith('--file'))
|
|
2119
1646
|
return input;
|
|
2120
1647
|
if (powershell && /^-notmatch$/i.test(argument)) {
|
|
@@ -2127,8 +1654,6 @@ function stageOutput(stages, at, unquote) {
|
|
|
2127
1654
|
}
|
|
2128
1655
|
if (powershell && /^-casesensitive$/i.test(argument))
|
|
2129
1656
|
continue;
|
|
2130
|
-
// Options that take a value: the value is not the pattern. `-m` limits the lines kept; context
|
|
2131
|
-
// options add lines around each match, so the input is then taken to pass whole.
|
|
2132
1657
|
const long = /^--(max-count|after-context|before-context|context|devices|directories|binary-files|colou?r|label|include|exclude|exclude-dir|exclude-from|group-separator)(?:=([\s\S]*))?$/.exec(argument);
|
|
2133
1658
|
if (long) {
|
|
2134
1659
|
const value = long[2] ?? args[++index] ?? '';
|
|
@@ -2138,7 +1663,6 @@ function stageOutput(stages, at, unquote) {
|
|
|
2138
1663
|
return input;
|
|
2139
1664
|
continue;
|
|
2140
1665
|
}
|
|
2141
|
-
// `--no-ignore-case` undoes an earlier `-i`: options apply in order.
|
|
2142
1666
|
if (argument === '--no-ignore-case') {
|
|
2143
1667
|
flags.delete('i');
|
|
2144
1668
|
continue;
|
|
@@ -2153,7 +1677,6 @@ function stageOutput(stages, at, unquote) {
|
|
|
2153
1677
|
const letters = argument.slice(1);
|
|
2154
1678
|
for (let at = 0; at < letters.length; at += 1) {
|
|
2155
1679
|
const letter = letters[at];
|
|
2156
|
-
// `-2` is two lines of context.
|
|
2157
1680
|
if (/\d/.test(letter))
|
|
2158
1681
|
return input;
|
|
2159
1682
|
if (!'mABCdDef'.includes(letter)) {
|
|
@@ -2185,36 +1708,25 @@ function stageOutput(stages, at, unquote) {
|
|
|
2185
1708
|
pattern = argument;
|
|
2186
1709
|
continue;
|
|
2187
1710
|
}
|
|
2188
|
-
// `-` names standard input, which is the pipeline's.
|
|
2189
1711
|
if (argument === '-')
|
|
2190
1712
|
continue;
|
|
2191
|
-
// A file after the pattern: grep reads it instead of its input.
|
|
2192
1713
|
return undefined;
|
|
2193
1714
|
}
|
|
2194
1715
|
if (pattern !== undefined)
|
|
2195
1716
|
patterns.push(pattern);
|
|
2196
|
-
// With -z a record ends at a NUL, not a line break, so a match keeps every line of its record.
|
|
2197
1717
|
if (flags.has('z'))
|
|
2198
1718
|
return input;
|
|
2199
|
-
// Perl syntax (`\Q…\E`, `(?i)`, `\K`) can compile in JavaScript yet match differently, so a
|
|
2200
|
-
// `-P` pattern is not run: the input passes whole.
|
|
2201
1719
|
if (flags.has('P'))
|
|
2202
1720
|
return input;
|
|
2203
1721
|
if (patterns.length === 0)
|
|
2204
1722
|
return input;
|
|
2205
|
-
// grep reads a pattern argument's lines as separate patterns too.
|
|
2206
1723
|
const each = patterns.flatMap(item => item.split('\n'));
|
|
2207
1724
|
const pieces = each.map(item => flags.has('F') ? item.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
2208
1725
|
: flags.has('P') ? item
|
|
2209
1726
|
: grepPattern(item, flags.has('E') || word === 'egrep' || powershell));
|
|
2210
|
-
// A pattern this reading cannot translate faithfully is taken to keep everything: concluding that
|
|
2211
|
-
// nothing gets through would be the unsafe guess.
|
|
2212
1727
|
if (pieces.some(piece => piece === undefined))
|
|
2213
1728
|
return input;
|
|
2214
1729
|
const translated = pieces.length === 1 ? pieces[0] : pieces.map(piece => `(?:${piece})`).join('|');
|
|
2215
|
-
// Matching runs on the engine's thread, so a pattern that could backtrack without bound is not
|
|
2216
|
-
// run at all: a quantified group, more than two open-ended repeats, or a very long line. The
|
|
2217
|
-
// input is then taken to pass whole, which can only refuse more.
|
|
2218
1730
|
if (pieces.some(piece => mayBacktrack(piece)) || input.length > 20_000 || input.split('\n').some(line => line.length > 2_000))
|
|
2219
1731
|
return input;
|
|
2220
1732
|
let source = translated;
|
|
@@ -2227,13 +1739,11 @@ function stageOutput(stages, at, unquote) {
|
|
|
2227
1739
|
matcher = new RegExp(source, flags.has('i') || powershell || word === 'findstr' ? 'gi' : 'g');
|
|
2228
1740
|
}
|
|
2229
1741
|
catch {
|
|
2230
|
-
// A pattern this reading cannot compile is taken to keep everything.
|
|
2231
1742
|
return input;
|
|
2232
1743
|
}
|
|
2233
1744
|
const lines = input.split('\n');
|
|
2234
1745
|
const hits = lines.map((line, number) => ({ line, number: number + 1, matched: new RegExp(matcher.source, matcher.flags.replace('g', '')).test(line) !== flags.has('v') }));
|
|
2235
1746
|
const kept = hits.filter(hit => hit.matched).slice(0, Number.isFinite(maxCount) && maxCount >= 0 ? maxCount : undefined);
|
|
2236
|
-
// What grep writes, for the flags that change it.
|
|
2237
1747
|
if (flags.has('q'))
|
|
2238
1748
|
return '';
|
|
2239
1749
|
if (flags.has('l'))
|
|
@@ -2250,11 +1760,6 @@ function stageOutput(stages, at, unquote) {
|
|
|
2250
1760
|
}
|
|
2251
1761
|
return kept.map(hit => prefix(hit.number) + hit.line).join('\n');
|
|
2252
1762
|
}
|
|
2253
|
-
/**
|
|
2254
|
-
* The commands that may read a pipeline stage's input: its first word, or the command a wrapper
|
|
2255
|
-
* runs (`env bash`, `sudo -u me bash`, `timeout 5 bash`). Where a wrapper's option may or may not
|
|
2256
|
-
* take a value, both readings are returned. `xargs` turns its input into arguments, not input.
|
|
2257
|
-
*/
|
|
2258
1763
|
function stageReaders(words, depth = 0) {
|
|
2259
1764
|
let at = 0;
|
|
2260
1765
|
while (at < words.length && /^[A-Za-z_]\w*=/.test(words[at]))
|
|
@@ -2285,11 +1790,6 @@ function stageReaders(words, depth = 0) {
|
|
|
2285
1790
|
}
|
|
2286
1791
|
return readers;
|
|
2287
1792
|
}
|
|
2288
|
-
/**
|
|
2289
|
-
* A pipeline stage's words without the grouping around them: `(echo …`, `bash)`, `{ echo …`. A
|
|
2290
|
-
* subshell or a `{ …; }` group passes its input and output through, so `(echo 'kill 80632' | bash)`
|
|
2291
|
-
* and `{ echo … | bash; }` are the same pipeline.
|
|
2292
|
-
*/
|
|
2293
1793
|
function ungrouped(words) {
|
|
2294
1794
|
const out = [...words];
|
|
2295
1795
|
while (out.length > 0 && (out[0] === '{' || out[0].startsWith('('))) {
|
|
@@ -2303,7 +1803,6 @@ function ungrouped(words) {
|
|
|
2303
1803
|
}
|
|
2304
1804
|
while (out.length > 0 && (out[out.length - 1] === '}' || out[out.length - 1].endsWith(')'))) {
|
|
2305
1805
|
const last = out.length - 1;
|
|
2306
|
-
// A word that opens its own parenthesis keeps the close: `$(date)`, `f()`.
|
|
2307
1806
|
if (out[last] === '}') {
|
|
2308
1807
|
out.pop();
|
|
2309
1808
|
continue;
|
|
@@ -2312,7 +1811,6 @@ function ungrouped(words) {
|
|
|
2312
1811
|
const closed = (out[last].match(/\)/g) ?? []).length;
|
|
2313
1812
|
if (closed <= opened)
|
|
2314
1813
|
break;
|
|
2315
|
-
// Strip only the closes that have no open in the word itself: `bash)` → `bash`, `$(date))` → `$(date)`.
|
|
2316
1814
|
let word = out[last];
|
|
2317
1815
|
for (let excess = closed - opened; excess > 0 && word.endsWith(')'); excess -= 1)
|
|
2318
1816
|
word = word.slice(0, -1);
|
|
@@ -2322,27 +1820,11 @@ function ungrouped(words) {
|
|
|
2322
1820
|
}
|
|
2323
1821
|
return out;
|
|
2324
1822
|
}
|
|
2325
|
-
/**
|
|
2326
|
-
* Stages that may drop, reorder or repeat whole lines of their input. Filters (grep and the like)
|
|
2327
|
-
* count as well: where their output is not modelled exactly (`-P`, `-A`, `-z`) it is taken to be
|
|
2328
|
-
* the whole input, which may keep a quote-only line that the real filter drops.
|
|
2329
|
-
*/
|
|
2330
1823
|
const LINE_SELECTORS = new Set(['head', 'tail', 'sort', 'uniq', 'tac', 'shuf', 'rev', 'more', 'less', 'select-object', 'sort-object', 'get-unique']);
|
|
2331
|
-
/**
|
|
2332
|
-
* Whether the stages before a reader may hand it lines regrouped: one that selects or reorders
|
|
2333
|
-
* lines, or an `echo` with neither `-e` nor `-E`, whose two readings are written one after the other.
|
|
2334
|
-
*/
|
|
2335
1824
|
function linesMayRegroup(stages, unquote) {
|
|
2336
1825
|
return stages.some(stage => stageReaders(stage.map(unquote)).some(({ word, args }) => LINE_SELECTORS.has(word) || FILTERS.has(word) || (word === 'echo' && !args.some(argument => /^-[neE]*[eE][neE]*$/.test(argument)))));
|
|
2337
1826
|
}
|
|
2338
|
-
/**
|
|
2339
|
-
* Whether a pipeline feeds code to an interpreter that reads it from its input:
|
|
2340
|
-
* `echo 'kill 80632' | bash`, `printf '…' | python3`, `'Stop-Process -Id 80632' | iex`. An
|
|
2341
|
-
* interpreter reads its input as code when it was given no code of its own, or `-`.
|
|
2342
|
-
*/
|
|
2343
1827
|
function pipedIntoInterpreter(line, unquote, depth) {
|
|
2344
|
-
// A line break after `|` continues the pipeline: `echo 'kill 80632' |` then `bash` is one pipeline.
|
|
2345
|
-
// Bash's `|&` pipes standard error too; it is a pipe, not a `|` and a background `&`.
|
|
2346
1828
|
const continued = line.replace(/\|&/g, '| ').replace(/(?<!\|)\|[ \t]*(?:\r?\n[ \t]*)+/g, '| ');
|
|
2347
1829
|
for (const pipeline of continued.split(/;|\n|&&|\|\||&/)) {
|
|
2348
1830
|
const stages = pipeline.split('|').map(stage => ungrouped(stage.trim().split(/\s+/).filter(Boolean)));
|
|
@@ -2350,26 +1832,16 @@ function pipedIntoInterpreter(line, unquote, depth) {
|
|
|
2350
1832
|
for (const { word, args } of stageReaders(stages[at].map(unquote))) {
|
|
2351
1833
|
if (!INTERPRETERS.has(word))
|
|
2352
1834
|
continue;
|
|
2353
|
-
// Its own input redirection (`bash </dev/null`, `0<file`, a here-string or here-document)
|
|
2354
|
-
// replaces the pipe: what the stage before writes never reaches it. `<(…)` is a file argument.
|
|
2355
|
-
// `0<&0` and `<&3` duplicate a descriptor that may still be the pipe, so they keep it judged;
|
|
2356
|
-
// `<&-` closes standard input.
|
|
2357
1835
|
if (args.some(argument => /^0?<(?![(&\u0002])/.test(argument) || /^0?<[&\u0002]-$/.test(argument)))
|
|
2358
1836
|
continue;
|
|
2359
1837
|
if (!readsInput(word, withoutRedirections(args)))
|
|
2360
1838
|
continue;
|
|
2361
|
-
// Bash drops NUL bytes from what it reads: `echo -e 'ki\0ll 80632' | bash` runs `kill 80632`.
|
|
2362
|
-
// Other readers reject or stop at them, so dropping them can only refuse more.
|
|
2363
1839
|
const piped = stageOutput(stages, at - 1, unquote)?.replace(/\0/g, '');
|
|
2364
1840
|
if (piped === undefined)
|
|
2365
1841
|
continue;
|
|
2366
1842
|
const fed = inputProgram(word, withoutRedirections(args), piped);
|
|
2367
1843
|
if (codeTerminates(fed, depth + 1, dialectOf(word)))
|
|
2368
1844
|
return true;
|
|
2369
|
-
// A stage that drops or reorders lines (`tail -n 1`, `sort`, `uniq`) can remove a line whose
|
|
2370
|
-
// open quote swallowed the next ones in the text as modelled, and an echo that may or may not
|
|
2371
|
-
// expand writes two readings whose quotes must not pair; then each line is judged alone too.
|
|
2372
|
-
// Unchanged input keeps its quoting: a multi-line string stays one string.
|
|
2373
1845
|
const lines = fed.split('\n');
|
|
2374
1846
|
if (lines.length > 1 && linesMayRegroup(stages.slice(0, at), unquote)
|
|
2375
1847
|
&& lines.slice(0, 500).some(each => each.trim() !== '' && codeTerminates(each, depth + 1, dialectOf(word))))
|
|
@@ -2379,14 +1851,6 @@ function pipedIntoInterpreter(line, unquote, depth) {
|
|
|
2379
1851
|
}
|
|
2380
1852
|
return false;
|
|
2381
1853
|
}
|
|
2382
|
-
/**
|
|
2383
|
-
* Whether one statement ends a process, given its words as the shell passes them (`words`) and as
|
|
2384
|
-
* they were written (`raw`, where a fully quoted word is one placeholder).
|
|
2385
|
-
*
|
|
2386
|
-
* The command is the first word after any assignment (`$x = …`, `NAME=value`). A wrapper
|
|
2387
|
-
* (`sudo`, `env`, `timeout`, `xargs`, a keyword such as `do`) hands on to the command after its own
|
|
2388
|
-
* options, which [wrappedTerminates] skips by each wrapper's syntax.
|
|
2389
|
-
*/
|
|
2390
1854
|
function wordsTerminate(raw, words, depth, shell) {
|
|
2391
1855
|
if (depth > 8)
|
|
2392
1856
|
return false;
|
|
@@ -2401,8 +1865,6 @@ function wordsTerminate(raw, words, depth, shell) {
|
|
|
2401
1865
|
at += 1;
|
|
2402
1866
|
continue;
|
|
2403
1867
|
}
|
|
2404
|
-
// A redirection may come first: `2>/dev/null kill …`, `> out kill …`. An operator standing
|
|
2405
|
-
// alone takes the next word as its operand.
|
|
2406
1868
|
const redirect = /^(?:\d+|\*|\u0002)?(?:>>?|<<?|>\||<>)(.*)$/.exec(token);
|
|
2407
1869
|
if (redirect) {
|
|
2408
1870
|
at += redirect[1] ? 1 : 2;
|
|
@@ -2414,8 +1876,6 @@ function wordsTerminate(raw, words, depth, shell) {
|
|
|
2414
1876
|
return false;
|
|
2415
1877
|
const word = commandName(words[at]);
|
|
2416
1878
|
const rest = words.slice(at + 1);
|
|
2417
|
-
// PowerShell's `-WhatIf` (or any prefix of it, `-WhatIf:$true`) only describes what would happen.
|
|
2418
|
-
// To POSIX `kill`, `-WhatIf` is a bad operand after the pid has already been signalled.
|
|
2419
1879
|
if (shell === 'powershell' && KILLERS.has(word) && WHATIF_CMDLETS.has(word) && previewsOnly(rest))
|
|
2420
1880
|
return false;
|
|
2421
1881
|
if (KILLERS.has(word))
|
|
@@ -2424,22 +1884,17 @@ function wordsTerminate(raw, words, depth, shell) {
|
|
|
2424
1884
|
return rest.some(argument => /\b(?:terminate|delete)\b/i.test(argument));
|
|
2425
1885
|
if (INTERPRETERS.has(word)) {
|
|
2426
1886
|
const args = withoutRedirections(rest);
|
|
2427
|
-
// `bash <<< 'kill …'`: the here-string is the interpreter's input, which it runs only when it
|
|
2428
|
-
// was given no code or script of its own (`bash -c 'cat' <<< …` prints it).
|
|
2429
1887
|
if (readsInput(word, args) && hereStrings(rest).some(code => codeTerminates(inputProgram(word, args, code), depth, dialectOf(word))))
|
|
2430
1888
|
return true;
|
|
2431
1889
|
const codes = interpreterCode(word, args);
|
|
2432
|
-
// Ruby and Perl join their `-e` fragments into one program, one per line: a call may span them.
|
|
2433
1890
|
if ((word === 'ruby' || word === 'perl') && codes.length > 1)
|
|
2434
1891
|
codes.push(codes.join('\n'));
|
|
2435
1892
|
return codes.some(code => codeTerminates(code, depth, dialectOf(word)));
|
|
2436
1893
|
}
|
|
2437
1894
|
if (word === 'start-process' || word === 'saps')
|
|
2438
1895
|
return startProcessTerminates(rest, depth);
|
|
2439
|
-
// `git -c 'alias.x=!kill 80632' x`: a shell alias is a command line Git hands to sh.
|
|
2440
1896
|
if (word === 'git')
|
|
2441
1897
|
return gitShellAliases(rest).some(alias => terminates(alias, depth + 1, 'posix'));
|
|
2442
|
-
// `trap 'kill …' EXIT` runs its handler later, in this shell: the handler is shell code.
|
|
2443
1898
|
if (word === 'trap') {
|
|
2444
1899
|
const handler = rest.find(argument => !/^-[lp]$/.test(argument) && argument !== '--');
|
|
2445
1900
|
return handler !== undefined && handler !== '-' && terminates(handler, depth + 1, shell);
|
|
@@ -2448,7 +1903,6 @@ function wordsTerminate(raw, words, depth, shell) {
|
|
|
2448
1903
|
return false;
|
|
2449
1904
|
return wrappedTerminates(word, raw.slice(at + 1), rest, depth, shell);
|
|
2450
1905
|
}
|
|
2451
|
-
/** Whether the command a wrapper runs ends a process, past the wrapper's own options. */
|
|
2452
1906
|
function wrappedTerminates(wrapper, raw, words, depth, shell) {
|
|
2453
1907
|
const values = WRAPPER_VALUES[wrapper];
|
|
2454
1908
|
let titled = false;
|
|
@@ -2459,7 +1913,6 @@ function wrappedTerminates(wrapper, raw, words, depth, shell) {
|
|
|
2459
1913
|
return wordsTerminate(raw.slice(at + 1), words.slice(at + 1), depth + 1, shell);
|
|
2460
1914
|
if (option === '-')
|
|
2461
1915
|
continue;
|
|
2462
|
-
// `env -S 'kill 80632'` splits its value into a command line and runs it.
|
|
2463
1916
|
if (wrapper === 'env') {
|
|
2464
1917
|
const split = option === '-S' || lower === '--split-string' ? words[at + 1]
|
|
2465
1918
|
: /^-S./.test(option) ? option.slice(2) : lower.startsWith('--split-string=') ? option.slice(option.indexOf('=') + 1) : undefined;
|
|
@@ -2470,13 +1923,11 @@ function wrappedTerminates(wrapper, raw, words, depth, shell) {
|
|
|
2470
1923
|
continue;
|
|
2471
1924
|
}
|
|
2472
1925
|
}
|
|
2473
|
-
// cmd's `start` takes a quoted window title before the program: `start "" taskkill …`.
|
|
2474
1926
|
if (wrapper === 'start' && !titled && /^\u0000\d+\u0000$/.test(raw[at] ?? '')) {
|
|
2475
1927
|
titled = true;
|
|
2476
1928
|
continue;
|
|
2477
1929
|
}
|
|
2478
1930
|
if (values?.has(lower)) {
|
|
2479
|
-
// Read both ways: the next word as this option's value, and as the command.
|
|
2480
1931
|
if (wordsTerminate(raw.slice(at + 1), words.slice(at + 1), depth + 1, shell))
|
|
2481
1932
|
return true;
|
|
2482
1933
|
at += 1;
|
|
@@ -2484,18 +1935,12 @@ function wrappedTerminates(wrapper, raw, words, depth, shell) {
|
|
|
2484
1935
|
}
|
|
2485
1936
|
if (/^[-/][\w?-]+(?:[:=][\s\S]*)?$/.test(option))
|
|
2486
1937
|
continue;
|
|
2487
|
-
// `timeout 5 kill …`: the duration comes before the command.
|
|
2488
1938
|
if (wrapper === 'timeout' && /^\d+(?:\.\d+)?[smhd]?$/.test(option))
|
|
2489
1939
|
continue;
|
|
2490
1940
|
return wordsTerminate(raw.slice(at), words.slice(at), depth + 1, shell);
|
|
2491
1941
|
}
|
|
2492
1942
|
return false;
|
|
2493
1943
|
}
|
|
2494
|
-
/**
|
|
2495
|
-
* Whether PowerShell's `Start-Process` is asked to start a process that ends one: the program from
|
|
2496
|
-
* `-FilePath` or the first positional argument, with `-ArgumentList` or the rest. The program and
|
|
2497
|
-
* its arguments are read as one command line, both as POSIX and as PowerShell would.
|
|
2498
|
-
*/
|
|
2499
1944
|
function startProcessTerminates(args, depth) {
|
|
2500
1945
|
let program;
|
|
2501
1946
|
let argumentList;
|
|
@@ -2529,27 +1974,14 @@ function startProcessTerminates(args, depth) {
|
|
|
2529
1974
|
return terminates(line, depth + 1, 'posix') || terminates(line, depth + 1, 'powershell');
|
|
2530
1975
|
}
|
|
2531
1976
|
const escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
2532
|
-
/** The image name without its extension: `andorra-desktop.exe` and `andorra-desktop` are one name. */
|
|
2533
|
-
/**
|
|
2534
|
-
* Whether a name-selecting killer in `text` is given a pattern that matches the host's name, by its
|
|
2535
|
-
* own matching rules: pkill's and pgrep's regular expressions match anywhere in the name
|
|
2536
|
-
* (`pkill -f 'andorra.*desktop'`), `killall -r` likewise, and PowerShell's and taskkill's wildcards
|
|
2537
|
-
* match the whole name (`Stop-Process -Name andorra*`, `taskkill /IM andorra*`).
|
|
2538
|
-
*/
|
|
2539
1977
|
function selectsByPattern(text, stem) {
|
|
2540
1978
|
const names = [stem, `${stem}.exe`];
|
|
2541
|
-
// The killer may be named by its path: `/usr/bin/pkill`, `C:\Windows\System32\taskkill.exe`.
|
|
2542
1979
|
for (const match of text.matchAll(/(?:^|[\s;|&(`{])(?:[^\s;|&()'"]*[\\/])?(pkill|pgrep|killall|stop-process|spps|taskkill|get-process|gps|kill)(?:\.exe)?(?=\s)((?:'[^']*'|"[^"]*"|[^;|&\n'"])*)/gi)) {
|
|
2543
1980
|
const killer = match[1].toLowerCase();
|
|
2544
|
-
// A quoted `|` or `;` belongs to the argument: `pkill -f 'andorra-(desktop|other)'`.
|
|
2545
1981
|
const tokens = [...match[2].matchAll(/(?:'[^']*'|"[^"]*"|[^\s'"])+/g)].map(token => token[0].replace(/'([^']*)'|"([^"]*)"/g, '$1$2'));
|
|
2546
1982
|
const regex = killer === 'pkill' || killer === 'pgrep' || (killer === 'killall' && tokens.some(word => word === '-r' || word === '--regexp'));
|
|
2547
|
-
// `Get-Process -Name andorra* | Stop-Process` selects with Get-Process's wildcards.
|
|
2548
|
-
// PowerShell's `kill` is Stop-Process when it is given `-Name`; the POSIX kill takes no names.
|
|
2549
1983
|
const powershellKill = killer === 'kill' && tokens.some(token => /^-n(?:a(?:m(?:e)?)?)?$/i.test(token));
|
|
2550
1984
|
const glob = killer === 'stop-process' || killer === 'spps' || killer === 'taskkill' || killer === 'get-process' || killer === 'gps' || powershellKill;
|
|
2551
|
-
// PowerShell takes several names as an array: `-Name 'node*','andorra*'` and `@('node*', 'andorra*')`
|
|
2552
|
-
// are two selectors each.
|
|
2553
1985
|
const words = killer === 'taskkill' || !glob ? tokens
|
|
2554
1986
|
: tokens.flatMap(token => token.replace(/^@?\(/, '').replace(/\)$/, '').split(',')).filter(word => word !== '');
|
|
2555
1987
|
if (!regex && !glob)
|
|
@@ -2557,8 +1989,6 @@ function selectsByPattern(text, stem) {
|
|
|
2557
1989
|
for (const word of words) {
|
|
2558
1990
|
if (word === '' || /^[-/]/.test(word) && !/[*?]/.test(word))
|
|
2559
1991
|
continue;
|
|
2560
|
-
// pkill's patterns are POSIX extended regular expressions (`[[:punct:]]` is punctuation); one the
|
|
2561
|
-
// translation does not reproduce is taken to match when its literal parts appear in the name.
|
|
2562
1992
|
const translated = glob ? undefined : grepPattern(word, true);
|
|
2563
1993
|
if (!glob && translated === undefined) {
|
|
2564
1994
|
if (names.some(name => literalsInOrder(word, name)))
|
|
@@ -2574,18 +2004,12 @@ function selectsByPattern(text, stem) {
|
|
|
2574
2004
|
catch {
|
|
2575
2005
|
continue;
|
|
2576
2006
|
}
|
|
2577
|
-
// A pattern that could backtrack without bound is not run on the engine's thread: it is taken to
|
|
2578
|
-
// match when its literal parts appear in the name in order, which a pattern built from them may.
|
|
2579
2007
|
if (mayBacktrack(pattern.source) ? names.some(name => literalsInOrder(glob ? word.replace(/[*?]/g, '.') : word, name)) : names.some(name => pattern.test(name)))
|
|
2580
2008
|
return true;
|
|
2581
2009
|
}
|
|
2582
2010
|
}
|
|
2583
2011
|
return false;
|
|
2584
2012
|
}
|
|
2585
|
-
/**
|
|
2586
|
-
* A PowerShell or taskkill wildcard as a regular expression's source: `*` any run, `?` one
|
|
2587
|
-
* character, `[a-c]` one of a set, everything else itself.
|
|
2588
|
-
*/
|
|
2589
2013
|
function wildcardSource(word) {
|
|
2590
2014
|
let out = '';
|
|
2591
2015
|
for (let index = 0; index < word.length; index += 1) {
|
|
@@ -2608,21 +2032,15 @@ function wildcardSource(word) {
|
|
|
2608
2032
|
}
|
|
2609
2033
|
return out;
|
|
2610
2034
|
}
|
|
2611
|
-
/** Whether the literal runs of a pattern (the text between its operators) appear in `name` in order. */
|
|
2612
2035
|
function literalsInOrder(pattern, name) {
|
|
2613
|
-
// With alternatives (`(andorra|other)+`) only one branch need appear; which one is not worked out
|
|
2614
|
-
// here, so such a pattern is taken to match.
|
|
2615
2036
|
if (pattern.replace(/\\./g, '').includes('|'))
|
|
2616
2037
|
return true;
|
|
2617
|
-
// Parts that may occur zero times are not required: `(other)?andorra`, `x*andorra`, `(ab){0,2}`.
|
|
2618
2038
|
let optional = pattern.replace(/\\./g, '.');
|
|
2619
2039
|
for (let previous = ''; previous !== optional;) {
|
|
2620
2040
|
previous = optional;
|
|
2621
2041
|
optional = optional.replace(/\([^()]*\)(?:\?|\*|\{0(?:,\d*)?\})/g, '.').replace(/[^.\])](?:\?|\*|\{0(?:,\d*)?\})/g, '.');
|
|
2622
2042
|
}
|
|
2623
2043
|
pattern = optional;
|
|
2624
|
-
// A repetition count (`{30}`, `{2,5}`) is an operator, not text.
|
|
2625
|
-
// A bracket expression (`[[:punct:]]`, `[a-z]`) stands for one character, whatever its text.
|
|
2626
2044
|
const runs = pattern.replace(/\\./g, '.').replace(/\[\^?\]?(?:\[:[a-z]+:\]|[^\]])*\]/g, '.').replace(/\{\d*,?\d*\}/g, '.').split(/[\^$.|?*+()[\]{}]+/).filter(Boolean).map(run => run.toLowerCase());
|
|
2627
2045
|
let from = 0;
|
|
2628
2046
|
const lower = name.toLowerCase();
|
|
@@ -2637,20 +2055,7 @@ function literalsInOrder(pattern, name) {
|
|
|
2637
2055
|
function imageStem(name) {
|
|
2638
2056
|
return name.replace(/\.exe$/i, '').trim();
|
|
2639
2057
|
}
|
|
2640
|
-
/**
|
|
2641
|
-
* Why `command` must not run, or `undefined` when it may.
|
|
2642
|
-
*
|
|
2643
|
-
* Two things must both be true. The command must end a process when run — a kill in command
|
|
2644
|
-
* position, not the words inside a search pattern or a message (`rg 'Stop-Process.*andorra-desktop'`
|
|
2645
|
-
* is a search). And it must name the host anywhere, quoted text included, because the usual shape
|
|
2646
|
-
* is a name filter in one statement feeding a kill in another.
|
|
2647
|
-
*
|
|
2648
|
-
* A process id only counts where it stands alone as a number, so `8063` does not match host
|
|
2649
|
-
* `80632` and a port or a line number that happens to equal the id is the one false positive
|
|
2650
|
-
* accepted: it only arises in a command that also terminates something.
|
|
2651
|
-
*/
|
|
2652
2058
|
export function hostKillRefusal(command, host = hostProcess(), shell = 'any') {
|
|
2653
|
-
// A tool whose shell is not known is read both ways: what either shell would run counts.
|
|
2654
2059
|
const shells = shell === 'any' ? ['posix', 'powershell'] : [shell];
|
|
2655
2060
|
evidence = [];
|
|
2656
2061
|
try {
|
|
@@ -2662,8 +2067,6 @@ export function hostKillRefusal(command, host = hostProcess(), shell = 'any') {
|
|
|
2662
2067
|
evidence = undefined;
|
|
2663
2068
|
}
|
|
2664
2069
|
}
|
|
2665
|
-
/** The refusal for text already known to terminate something, when that text names the host. */
|
|
2666
|
-
/** Text with its common escapes expanded: `\n`, `\t`, `\xHH`, `\0nnn`, `\uXXXX`, `\u{…}`. */
|
|
2667
2070
|
function expandEscapes(text) {
|
|
2668
2071
|
return text.replace(/\\(?:x([0-9a-fA-F]{1,2})|0([0-7]{0,3})|u\{([0-9a-fA-F]{1,6})\}|u([0-9a-fA-F]{4})|([ntr]))/g, (whole, hex, octal, wide, short, letter) => {
|
|
2669
2072
|
if (hex !== undefined)
|
|
@@ -2676,7 +2079,6 @@ function expandEscapes(text) {
|
|
|
2676
2079
|
return letter === 'n' ? '\n' : letter === 't' ? '\t' : '\r';
|
|
2677
2080
|
});
|
|
2678
2081
|
}
|
|
2679
|
-
/** The text with each numeric literal written in plain decimal: `0x13af8` and `80_632` become 80632. */
|
|
2680
2082
|
function numbersInDecimal(text) {
|
|
2681
2083
|
return text.replace(/(?<![\w.])(0[xX][0-9a-fA-F](?:_?[0-9a-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*|0|[1-9](?:_?\d)*)n?(?![\w.])/g, (whole, literal) => {
|
|
2682
2084
|
try {
|
|
@@ -2687,10 +2089,6 @@ function numbersInDecimal(text) {
|
|
|
2687
2089
|
}
|
|
2688
2090
|
});
|
|
2689
2091
|
}
|
|
2690
|
-
/**
|
|
2691
|
-
* The text with each decimal number written as the integer it is worth, where it is one: `080632`
|
|
2692
|
-
* (kill reads decimal), `8.0632e4` and `80632.0` (JavaScript and Python numbers) become 80632.
|
|
2693
|
-
*/
|
|
2694
2092
|
function numbersAsValues(text) {
|
|
2695
2093
|
return text.replace(/(?<![\w.])(\d+(?:\.\d*)?(?:[eE][+-]?\d+)?)(?![\w.])/g, (whole, literal) => {
|
|
2696
2094
|
const value = Number(literal);
|
|
@@ -2699,18 +2097,10 @@ function numbersAsValues(text) {
|
|
|
2699
2097
|
}
|
|
2700
2098
|
function refusalFor(command, host, derived = []) {
|
|
2701
2099
|
const stem = host.name ? imageStem(host.name) : '';
|
|
2702
|
-
// The command as written, and with its escapes expanded: `kill\x2080632` names 80632 once
|
|
2703
|
-
// `echo -e` or a string literal has turned `\x20` into a space.
|
|
2704
|
-
// Also with the shell's quote removal applied: `kill 80'632'` and `andorra'-desktop'` are the
|
|
2705
|
-
// id and the name once the quotes and escapes are gone.
|
|
2706
|
-
// A backslash before a line break joins the lines (`kill 80\\` then `632` is `kill 80632`), and
|
|
2707
|
-
// PowerShell's backtick before one does the same.
|
|
2708
2100
|
const joined = command.replace(/[\\`]\r?\n/g, '');
|
|
2709
2101
|
const unquoted = joined.replace(/\\([\s\S])/g, '$1').replace(/`([A-Za-z0-9-])/g, '$1').replace(/['"^]/g, '');
|
|
2710
2102
|
const written = [command, joined, expandEscapes(command), unquoted, ...derived];
|
|
2711
|
-
// Code may spell the id as any numeric literal: `80_632`, `0x13af8`, `0o235370`, `0b…`, `80632n`.
|
|
2712
2103
|
const texts = [...written, ...written.map(numbersInDecimal), ...written.map(numbersAsValues)];
|
|
2713
|
-
// Three characters is the shortest name specific enough to match on.
|
|
2714
2104
|
const namesHost = stem.length >= 3 && texts.some(text => new RegExp(`(?<![A-Za-z0-9_-])${escapeRegExp(stem)}(?![A-Za-z0-9_-])`, 'i').test(text)
|
|
2715
2105
|
|| selectsByPattern(text, stem));
|
|
2716
2106
|
const numbered = (pid) => pid !== undefined && texts.some(text => new RegExp(`(?<![0-9A-Za-z_.])${pid}(?![0-9A-Za-z_.])`).test(text));
|
|
@@ -2731,13 +2121,7 @@ function refusalFor(command, host, derived = []) {
|
|
|
2731
2121
|
'If a second copy of the app did not appear when you launched it, it was handed to the window already open: launch test copies under their own application identifier (see AGENTS.md, "Launching a desktop test instance").',
|
|
2732
2122
|
].join(' ');
|
|
2733
2123
|
}
|
|
2734
|
-
/**
|
|
2735
|
-
* The text of a `data:` URL — percent-decoded, or base64-decoded when it says so — or undefined
|
|
2736
|
-
* when the value is not one. Node imports such a URL as a module, so its text is code.
|
|
2737
|
-
*/
|
|
2738
|
-
/** Node's options that load a module: their `data:` URL value is code. `--title data:…` is a title. */
|
|
2739
2124
|
const NODE_PRELOAD_OPTIONS = new Set(['--import', '--loader', '--experimental-loader', '-r', '--require']);
|
|
2740
|
-
/** The code a Node preload option loads from a `data:` URL, separate (`--import data:…`) or attached (`--import=data:…`). */
|
|
2741
2125
|
function nodePreloadCode(argument, next) {
|
|
2742
2126
|
if (NODE_PRELOAD_OPTIONS.has(argument))
|
|
2743
2127
|
return dataUrlCode(next);
|
|
@@ -2757,13 +2141,10 @@ function dataUrlCode(value) {
|
|
|
2757
2141
|
return match[2];
|
|
2758
2142
|
}
|
|
2759
2143
|
}
|
|
2760
|
-
/** Whether an argument is a `--long` option, with no `=value`, that the value table does not list. */
|
|
2761
2144
|
function unknownLongOption(argument) {
|
|
2762
2145
|
return argument !== undefined && /^--[\w-]+$/.test(argument) && !NODE_VALUE_OPTIONS.has(argument) && !NODE_EVAL.has(argument);
|
|
2763
2146
|
}
|
|
2764
|
-
/** The flags after which Node runs its next argument as code. */
|
|
2765
2147
|
const NODE_EVAL = new Set(['-e', '--eval', '-p', '--print', '-pe']);
|
|
2766
|
-
/** Node options that take the next argument as their value, which is therefore not the script. */
|
|
2767
2148
|
const NODE_VALUE_OPTIONS = new Set(['-r', '--require', '--import', '--loader', '--experimental-loader', '-C', '--conditions', '--input-type',
|
|
2768
2149
|
'--env-file', '--title', '--inspect-port', '--max-http-header-size', '--stack-trace-limit', '--watch-path', '--test-name-pattern', '--test-reporter',
|
|
2769
2150
|
'--unhandled-rejections', '--env-file-if-exists', '--disable-warning', '--dns-result-order', '--diagnostic-dir', '--redirect-warnings',
|
|
@@ -2772,18 +2153,7 @@ const NODE_VALUE_OPTIONS = new Set(['-r', '--require', '--import', '--loader', '
|
|
|
2772
2153
|
'--secure-heap-min', '--trace-event-categories', '--trace-event-file-pattern', '--use-largepages', '--experimental-config-file',
|
|
2773
2154
|
'--localstorage-file', '--watch-kill-signal', '--test-reporter-destination', '--test-shard', '--test-skip-pattern', '--test-concurrency',
|
|
2774
2155
|
'--test-timeout', '--experimental-sea-config', '--snapshot-blob', '--build-snapshot-config', '--max-old-space-size', '--max-semi-space-size']);
|
|
2775
|
-
/**
|
|
2776
|
-
* The refusal for a program started directly with an argument array, with no shell in between.
|
|
2777
|
-
*
|
|
2778
|
-
* Nothing there is shell syntax: an argument such as `; kill 80632` is one literal string handed
|
|
2779
|
-
* to the program, so joining the array and reading it as a command line would invent statements
|
|
2780
|
-
* that never run. Only an argument the program itself executes is judged — the code after Node's
|
|
2781
|
-
* `-e` or `-p` — and only that argument is searched for the host. Node stops reading its own
|
|
2782
|
-
* options at `--` and at the script's path; everything after either belongs to the program being
|
|
2783
|
-
* run, so a later `-e` there is data too.
|
|
2784
|
-
*/
|
|
2785
2156
|
export function hostKillRefusalForArgv(program, args, host = hostProcess()) {
|
|
2786
|
-
// Git runs a command-line shell alias through sh, so the alias's text is a shell command.
|
|
2787
2157
|
if (commandName(program) === 'git') {
|
|
2788
2158
|
for (const alias of gitShellAliases(args)) {
|
|
2789
2159
|
const refusal = hostKillRefusal(alias, host, 'posix');
|
|
@@ -2796,11 +2166,8 @@ export function hostKillRefusalForArgv(program, args, host = hostProcess()) {
|
|
|
2796
2166
|
return undefined;
|
|
2797
2167
|
for (let index = 0; index < args.length; index += 1) {
|
|
2798
2168
|
const argument = args[index];
|
|
2799
|
-
// The end of Node's options: the marker, or the first argument that is not an option.
|
|
2800
2169
|
if (argument === '--')
|
|
2801
2170
|
break;
|
|
2802
|
-
// A word after an option this table does not know may be that option's value
|
|
2803
|
-
// (`--some-option value -e …`), so the options are read on rather than stopping there.
|
|
2804
2171
|
if (!argument.startsWith('-')) {
|
|
2805
2172
|
if (unknownLongOption(args[index - 1]))
|
|
2806
2173
|
continue;
|
|
@@ -2808,10 +2175,8 @@ export function hostKillRefusalForArgv(program, args, host = hostProcess()) {
|
|
|
2808
2175
|
}
|
|
2809
2176
|
const inline = /^--(?:eval|print)=/.test(argument) ? argument.slice(argument.indexOf('=') + 1) : undefined;
|
|
2810
2177
|
const follows = NODE_EVAL.has(argument);
|
|
2811
|
-
// A preload given as a `data:` URL is code too: `--import data:text/javascript,process.kill(…)`.
|
|
2812
2178
|
const preload = nodePreloadCode(argument, args[index + 1]);
|
|
2813
2179
|
const code = inline ?? (follows ? args[index + 1] : preload);
|
|
2814
|
-
// The value an option consumes is neither an option nor the script.
|
|
2815
2180
|
if (follows || NODE_VALUE_OPTIONS.has(argument))
|
|
2816
2181
|
index += 1;
|
|
2817
2182
|
if (code === undefined)
|
|
@@ -2831,10 +2196,6 @@ export function hostKillRefusalForArgv(program, args, host = hostProcess()) {
|
|
|
2831
2196
|
}
|
|
2832
2197
|
return undefined;
|
|
2833
2198
|
}
|
|
2834
|
-
/**
|
|
2835
|
-
* The shell aliases a Git command line defines for itself: `-c alias.NAME=!command`, which Git runs
|
|
2836
|
-
* through sh when NAME is invoked. Every such value is returned, invoked or not.
|
|
2837
|
-
*/
|
|
2838
2199
|
function gitShellAliases(args) {
|
|
2839
2200
|
const aliases = [];
|
|
2840
2201
|
for (let index = 0; index < args.length; index += 1) {
|
|
@@ -2845,13 +2206,11 @@ function gitShellAliases(args) {
|
|
|
2845
2206
|
}
|
|
2846
2207
|
return aliases;
|
|
2847
2208
|
}
|
|
2848
|
-
/** Throw the refusal, for a tool that is about to start `program` with `args` directly. */
|
|
2849
2209
|
export function assertNotHostKillArgv(program, args, host = hostProcess()) {
|
|
2850
2210
|
const refusal = hostKillRefusalForArgv(program, args, host);
|
|
2851
2211
|
if (refusal)
|
|
2852
2212
|
throw new Error(refusal);
|
|
2853
2213
|
}
|
|
2854
|
-
/** Throw the refusal, for a tool that is about to run `command` in `shell`. */
|
|
2855
2214
|
export function assertNotHostKill(command, shell = 'any', host = hostProcess()) {
|
|
2856
2215
|
const refusal = hostKillRefusal(command, host, shell);
|
|
2857
2216
|
if (refusal)
|