@namzu/sdk 69.4.0 → 71.0.0
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/CHANGELOG.md +161 -0
- package/dist/authorization/bare-repository.d.ts +21 -0
- package/dist/authorization/bare-repository.d.ts.map +1 -0
- package/dist/authorization/bare-repository.js +62 -0
- package/dist/authorization/bare-repository.js.map +1 -0
- package/dist/authorization/command-line.d.ts.map +1 -1
- package/dist/authorization/command-line.js +10 -3
- package/dist/authorization/command-line.js.map +1 -1
- package/dist/authorization/gate.d.ts +7 -0
- package/dist/authorization/gate.d.ts.map +1 -1
- package/dist/authorization/gate.js +4 -0
- package/dist/authorization/gate.js.map +1 -1
- package/dist/authorization/protected-paths.d.ts +24 -6
- package/dist/authorization/protected-paths.d.ts.map +1 -1
- package/dist/authorization/protected-paths.js +83 -10
- package/dist/authorization/protected-paths.js.map +1 -1
- package/dist/authorization/readonly-commands.d.ts +17 -5
- package/dist/authorization/readonly-commands.d.ts.map +1 -1
- package/dist/authorization/readonly-commands.js +77 -6
- package/dist/authorization/readonly-commands.js.map +1 -1
- package/dist/authorization/readonly-flags.d.ts.map +1 -1
- package/dist/authorization/readonly-flags.js +98 -0
- package/dist/authorization/readonly-flags.js.map +1 -1
- package/dist/authorization/shell-lexer.d.ts +15 -0
- package/dist/authorization/shell-lexer.d.ts.map +1 -1
- package/dist/authorization/shell-lexer.js +370 -2
- package/dist/authorization/shell-lexer.js.map +1 -1
- package/dist/authorization/shell-paths.d.ts.map +1 -1
- package/dist/authorization/shell-paths.js +276 -0
- package/dist/authorization/shell-paths.js.map +1 -1
- package/dist/authorization/shell-session-state.d.ts +47 -0
- package/dist/authorization/shell-session-state.d.ts.map +1 -0
- package/dist/authorization/shell-session-state.js +70 -0
- package/dist/authorization/shell-session-state.js.map +1 -0
- package/dist/authorization/shell-session.d.ts +9 -0
- package/dist/authorization/shell-session.d.ts.map +1 -0
- package/dist/authorization/shell-session.js +19 -0
- package/dist/authorization/shell-session.js.map +1 -0
- package/dist/authorization/skill-grant.d.ts.map +1 -1
- package/dist/authorization/skill-grant.js +1 -0
- package/dist/authorization/skill-grant.js.map +1 -1
- package/dist/compaction/extractor.d.ts.map +1 -1
- package/dist/compaction/extractor.js +2 -0
- package/dist/compaction/extractor.js.map +1 -1
- package/dist/compaction/hooks.d.ts +16 -0
- package/dist/compaction/hooks.d.ts.map +1 -1
- package/dist/compaction/hooks.js +26 -1
- package/dist/compaction/hooks.js.map +1 -1
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +6 -1
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/restore.d.ts.map +1 -1
- package/dist/compaction/restore.js +3 -2
- package/dist/compaction/restore.js.map +1 -1
- package/dist/config/runtime.d.ts +2 -0
- package/dist/config/runtime.d.ts.map +1 -1
- package/dist/config/runtime.js +7 -0
- package/dist/config/runtime.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +7 -0
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +54 -4
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/connector/web/guarded-fetch.d.ts +30 -0
- package/dist/connector/web/guarded-fetch.d.ts.map +1 -1
- package/dist/connector/web/guarded-fetch.js +85 -0
- package/dist/connector/web/guarded-fetch.js.map +1 -1
- package/dist/connector/web/index.d.ts +1 -1
- package/dist/connector/web/index.d.ts.map +1 -1
- package/dist/connector/web/index.js +1 -1
- package/dist/connector/web/index.js.map +1 -1
- package/dist/connector/web/types.d.ts +8 -0
- package/dist/connector/web/types.d.ts.map +1 -1
- package/dist/constants/compaction/index.d.ts.map +1 -1
- package/dist/constants/compaction/index.js +1 -1
- package/dist/constants/compaction/index.js.map +1 -1
- package/dist/execution/code-runtime/worker-program.d.ts +1 -1
- package/dist/execution/code-runtime/worker-program.d.ts.map +1 -1
- package/dist/execution/code-runtime/worker-program.js +9 -5
- package/dist/execution/code-runtime/worker-program.js.map +1 -1
- package/dist/execution/local.d.ts.map +1 -1
- package/dist/execution/local.js +6 -1
- package/dist/execution/local.js.map +1 -1
- package/dist/peers/index.d.ts +2 -2
- package/dist/peers/index.d.ts.map +1 -1
- package/dist/peers/index.js +1 -1
- package/dist/peers/index.js.map +1 -1
- package/dist/peers/record.d.ts +74 -0
- package/dist/peers/record.d.ts.map +1 -1
- package/dist/peers/record.js +41 -1
- package/dist/peers/record.js.map +1 -1
- package/dist/peers/registry.d.ts.map +1 -1
- package/dist/peers/registry.js +20 -3
- package/dist/peers/registry.js.map +1 -1
- package/dist/plugin/bin-path.d.ts +41 -0
- package/dist/plugin/bin-path.d.ts.map +1 -0
- package/dist/plugin/bin-path.js +146 -0
- package/dist/plugin/bin-path.js.map +1 -0
- package/dist/plugin/common-layout/read.js +3 -3
- package/dist/plugin/common-layout/read.js.map +1 -1
- package/dist/plugin/common-layout-names.d.ts.map +1 -1
- package/dist/plugin/common-layout-names.js +0 -1
- package/dist/plugin/common-layout-names.js.map +1 -1
- package/dist/plugin/hook-module/capabilities.d.ts +12 -0
- package/dist/plugin/hook-module/capabilities.d.ts.map +1 -1
- package/dist/plugin/hook-module/capabilities.js +82 -10
- package/dist/plugin/hook-module/capabilities.js.map +1 -1
- package/dist/plugin/hook-module/consent.d.ts +11 -0
- package/dist/plugin/hook-module/consent.d.ts.map +1 -1
- package/dist/plugin/hook-module/consent.js +31 -0
- package/dist/plugin/hook-module/consent.js.map +1 -1
- package/dist/plugin/hook-module/host.d.ts.map +1 -1
- package/dist/plugin/hook-module/host.js +69 -23
- package/dist/plugin/hook-module/host.js.map +1 -1
- package/dist/plugin/hook-module/index.d.ts +1 -1
- package/dist/plugin/hook-module/index.d.ts.map +1 -1
- package/dist/plugin/hook-module/index.js +1 -1
- package/dist/plugin/hook-module/index.js.map +1 -1
- package/dist/plugin/hook-module/scan.d.ts +16 -0
- package/dist/plugin/hook-module/scan.d.ts.map +1 -1
- package/dist/plugin/hook-module/scan.js +68 -3
- package/dist/plugin/hook-module/scan.js.map +1 -1
- package/dist/plugin/hook-module/worker-source.d.ts +5 -4
- package/dist/plugin/hook-module/worker-source.d.ts.map +1 -1
- package/dist/plugin/hook-module/worker-source.js +254 -28
- package/dist/plugin/hook-module/worker-source.js.map +1 -1
- package/dist/plugin/lifecycle.d.ts +21 -0
- package/dist/plugin/lifecycle.d.ts.map +1 -1
- package/dist/plugin/lifecycle.js +64 -0
- package/dist/plugin/lifecycle.js.map +1 -1
- package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -1
- package/dist/plugin/shell-hook-tool-names.js +1 -0
- package/dist/plugin/shell-hook-tool-names.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +1 -1
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +22 -4
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/public-runtime.d.ts +11 -9
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +11 -8
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +7 -3
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +7 -3
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +2 -2
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/jobs/monitor-stream.d.ts +9 -1
- package/dist/runtime/jobs/monitor-stream.d.ts.map +1 -1
- package/dist/runtime/jobs/monitor-stream.js +13 -4
- package/dist/runtime/jobs/monitor-stream.js.map +1 -1
- package/dist/runtime/jobs/registry.d.ts +4 -0
- package/dist/runtime/jobs/registry.d.ts.map +1 -1
- package/dist/runtime/jobs/registry.js +12 -2
- package/dist/runtime/jobs/registry.js.map +1 -1
- package/dist/runtime/query/auto-review/git-status.d.ts.map +1 -1
- package/dist/runtime/query/auto-review/git-status.js +3 -2
- package/dist/runtime/query/auto-review/git-status.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +16 -6
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +135 -15
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +2 -2
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/outstanding-work.d.ts.map +1 -1
- package/dist/runtime/query/iteration/outstanding-work.js +14 -4
- package/dist/runtime/query/iteration/outstanding-work.js.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.js +11 -1
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.d.ts +2 -0
- package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js +19 -8
- package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
- package/dist/runtime/query/path-escalation.d.ts +6 -0
- package/dist/runtime/query/path-escalation.d.ts.map +1 -1
- package/dist/runtime/query/path-escalation.js +58 -4
- package/dist/runtime/query/path-escalation.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +21 -3
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +39 -21
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/steering.d.ts +11 -2
- package/dist/runtime/query/steering.d.ts.map +1 -1
- package/dist/runtime/query/steering.js +18 -4
- package/dist/runtime/query/steering.js.map +1 -1
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +3 -1
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/schedules/describe.d.ts.map +1 -1
- package/dist/schedules/describe.js +2 -0
- package/dist/schedules/describe.js.map +1 -1
- package/dist/schedules/evaluate.d.ts +4 -0
- package/dist/schedules/evaluate.d.ts.map +1 -1
- package/dist/schedules/evaluate.js +26 -1
- package/dist/schedules/evaluate.js.map +1 -1
- package/dist/schedules/index.d.ts +3 -1
- package/dist/schedules/index.d.ts.map +1 -1
- package/dist/schedules/index.js +2 -1
- package/dist/schedules/index.js.map +1 -1
- package/dist/schedules/next-fire.d.ts.map +1 -1
- package/dist/schedules/next-fire.js +10 -0
- package/dist/schedules/next-fire.js.map +1 -1
- package/dist/schedules/spec.d.ts +9 -0
- package/dist/schedules/spec.d.ts.map +1 -1
- package/dist/schedules/spec.js +33 -0
- package/dist/schedules/spec.js.map +1 -1
- package/dist/schedules/types.d.ts +32 -4
- package/dist/schedules/types.d.ts.map +1 -1
- package/dist/schedules/types.js +3 -2
- package/dist/schedules/types.js.map +1 -1
- package/dist/schedules/watch.d.ts +23 -0
- package/dist/schedules/watch.d.ts.map +1 -0
- package/dist/schedules/watch.js +46 -0
- package/dist/schedules/watch.js.map +1 -0
- package/dist/session/workspace/git-worktree.d.ts.map +1 -1
- package/dist/session/workspace/git-worktree.js +2 -1
- package/dist/session/workspace/git-worktree.js.map +1 -1
- package/dist/skills/arguments.d.ts +58 -0
- package/dist/skills/arguments.d.ts.map +1 -0
- package/dist/skills/arguments.js +208 -0
- package/dist/skills/arguments.js.map +1 -0
- package/dist/skills/index.d.ts +3 -1
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +1 -0
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/inline-shell-host.d.ts +6 -0
- package/dist/skills/inline-shell-host.d.ts.map +1 -1
- package/dist/skills/inline-shell-host.js +8 -0
- package/dist/skills/inline-shell-host.js.map +1 -1
- package/dist/skills/inline-shell.d.ts +26 -1
- package/dist/skills/inline-shell.d.ts.map +1 -1
- package/dist/skills/inline-shell.js +1308 -22
- package/dist/skills/inline-shell.js.map +1 -1
- package/dist/tools/builtins/bash-cwd.d.ts.map +1 -1
- package/dist/tools/builtins/bash-cwd.js +2 -0
- package/dist/tools/builtins/bash-cwd.js.map +1 -1
- package/dist/tools/builtins/bash.d.ts.map +1 -1
- package/dist/tools/builtins/bash.js +79 -11
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/exit-semantics.d.ts +6 -0
- package/dist/tools/builtins/exit-semantics.d.ts.map +1 -1
- package/dist/tools/builtins/exit-semantics.js +10 -0
- package/dist/tools/builtins/exit-semantics.js.map +1 -1
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +68 -13
- package/dist/tools/builtins/glob.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +37 -12
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/index.d.ts +11 -0
- package/dist/tools/builtins/index.d.ts.map +1 -1
- package/dist/tools/builtins/index.js +14 -0
- package/dist/tools/builtins/index.js.map +1 -1
- package/dist/tools/builtins/job.js +1 -1
- package/dist/tools/builtins/job.js.map +1 -1
- package/dist/tools/builtins/monitor.d.ts +1 -1
- package/dist/tools/builtins/monitor.d.ts.map +1 -1
- package/dist/tools/builtins/monitor.js +4 -2
- package/dist/tools/builtins/monitor.js.map +1 -1
- package/dist/tools/builtins/notebook-cells.d.ts +31 -0
- package/dist/tools/builtins/notebook-cells.d.ts.map +1 -0
- package/dist/tools/builtins/notebook-cells.js +160 -0
- package/dist/tools/builtins/notebook-cells.js.map +1 -0
- package/dist/tools/builtins/notebook-edit.d.ts +8 -0
- package/dist/tools/builtins/notebook-edit.d.ts.map +1 -0
- package/dist/tools/builtins/notebook-edit.js +249 -0
- package/dist/tools/builtins/notebook-edit.js.map +1 -0
- package/dist/tools/builtins/notebook-guard.d.ts.map +1 -1
- package/dist/tools/builtins/notebook-guard.js +2 -1
- package/dist/tools/builtins/notebook-guard.js.map +1 -1
- package/dist/tools/builtins/push-notification.d.ts +49 -0
- package/dist/tools/builtins/push-notification.d.ts.map +1 -0
- package/dist/tools/builtins/push-notification.js +107 -0
- package/dist/tools/builtins/push-notification.js.map +1 -0
- package/dist/tools/builtins/read-file.d.ts.map +1 -1
- package/dist/tools/builtins/read-file.js +8 -3
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/read-media.d.ts.map +1 -1
- package/dist/tools/builtins/read-media.js +2 -1
- package/dist/tools/builtins/read-media.js.map +1 -1
- package/dist/tools/builtins/read-render.d.ts.map +1 -1
- package/dist/tools/builtins/read-render.js +5 -3
- package/dist/tools/builtins/read-render.js.map +1 -1
- package/dist/tools/builtins/search-support.d.ts +6 -4
- package/dist/tools/builtins/search-support.d.ts.map +1 -1
- package/dist/tools/builtins/search-support.js +15 -3
- package/dist/tools/builtins/search-support.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +30 -8
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/tools/builtins/sleep-guard.d.ts +20 -0
- package/dist/tools/builtins/sleep-guard.d.ts.map +1 -1
- package/dist/tools/builtins/sleep-guard.js +125 -1
- package/dist/tools/builtins/sleep-guard.js.map +1 -1
- package/dist/tools/builtins/wait-for-job.js +1 -1
- package/dist/tools/builtins/wait-for-job.js.map +1 -1
- package/dist/tools/builtins/web.d.ts +2 -0
- package/dist/tools/builtins/web.d.ts.map +1 -1
- package/dist/tools/builtins/web.js +97 -3
- package/dist/tools/builtins/web.js.map +1 -1
- package/dist/tools/command-shell.d.ts +50 -0
- package/dist/tools/command-shell.d.ts.map +1 -1
- package/dist/tools/command-shell.js +145 -0
- package/dist/tools/command-shell.js.map +1 -1
- package/dist/tools/env-scrub.d.ts +2 -0
- package/dist/tools/env-scrub.d.ts.map +1 -1
- package/dist/tools/env-scrub.js +15 -1
- package/dist/tools/env-scrub.js.map +1 -1
- package/dist/tools/git-config-env.d.ts +55 -0
- package/dist/tools/git-config-env.d.ts.map +1 -0
- package/dist/tools/git-config-env.js +130 -0
- package/dist/tools/git-config-env.js.map +1 -0
- package/dist/tools/schedules/schedule-tool.d.ts +7 -0
- package/dist/tools/schedules/schedule-tool.d.ts.map +1 -1
- package/dist/tools/schedules/schedule-tool.js +72 -3
- package/dist/tools/schedules/schedule-tool.js.map +1 -1
- package/dist/tools/schedules/types.d.ts +38 -2
- package/dist/tools/schedules/types.d.ts.map +1 -1
- package/dist/types/connector/mcp.d.ts +24 -0
- package/dist/types/connector/mcp.d.ts.map +1 -1
- package/dist/types/message/index.d.ts +21 -1
- package/dist/types/message/index.d.ts.map +1 -1
- package/dist/types/message/index.js +37 -2
- package/dist/types/message/index.js.map +1 -1
- package/dist/types/permission/index.d.ts +11 -2
- package/dist/types/permission/index.d.ts.map +1 -1
- package/dist/types/permission/index.js +12 -2
- package/dist/types/permission/index.js.map +1 -1
- package/dist/types/plugin/index.d.ts +10 -2
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/tool/index.d.ts +13 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/utils/plain-terminal-text.d.ts +21 -0
- package/dist/utils/plain-terminal-text.d.ts.map +1 -0
- package/dist/utils/plain-terminal-text.js +156 -0
- package/dist/utils/plain-terminal-text.js.map +1 -0
- package/package.json +1 -1
- package/src/authorization/bare-repository.ts +70 -0
- package/src/authorization/command-line.ts +18 -4
- package/src/authorization/gate.ts +11 -0
- package/src/authorization/protected-paths.ts +89 -10
- package/src/authorization/readonly-commands.ts +75 -6
- package/src/authorization/readonly-flags.ts +103 -0
- package/src/authorization/shell-lexer.ts +367 -2
- package/src/authorization/shell-paths.ts +386 -0
- package/src/authorization/shell-session-state.ts +97 -0
- package/src/authorization/shell-session.ts +23 -0
- package/src/authorization/skill-grant.ts +1 -0
- package/src/compaction/extractor.ts +1 -0
- package/src/compaction/hooks.ts +43 -1
- package/src/compaction/manual.ts +6 -0
- package/src/compaction/restore.ts +3 -2
- package/src/config/runtime.ts +7 -0
- package/src/connector/mcp/client.ts +61 -4
- package/src/connector/web/guarded-fetch.ts +92 -0
- package/src/connector/web/index.ts +6 -1
- package/src/connector/web/types.ts +8 -0
- package/src/constants/compaction/index.ts +1 -1
- package/src/execution/code-runtime/worker-program.ts +9 -5
- package/src/execution/local.ts +6 -1
- package/src/peers/index.ts +8 -2
- package/src/peers/record.ts +45 -1
- package/src/peers/registry.ts +21 -5
- package/src/plugin/bin-path.ts +149 -0
- package/src/plugin/common-layout/read.ts +3 -3
- package/src/plugin/common-layout-names.ts +0 -1
- package/src/plugin/hook-module/capabilities.ts +94 -10
- package/src/plugin/hook-module/consent.ts +36 -0
- package/src/plugin/hook-module/host.ts +72 -21
- package/src/plugin/hook-module/index.ts +2 -0
- package/src/plugin/hook-module/scan.ts +76 -4
- package/src/plugin/hook-module/worker-source.ts +255 -28
- package/src/plugin/lifecycle.ts +67 -0
- package/src/plugin/shell-hook-tool-names.ts +1 -0
- package/src/plugin/shell-hook.ts +23 -4
- package/src/public-runtime.ts +27 -0
- package/src/public-tools.ts +36 -2
- package/src/public-types.ts +3 -1
- package/src/runtime/jobs/monitor-stream.ts +18 -4
- package/src/runtime/jobs/registry.ts +22 -2
- package/src/runtime/query/auto-review/git-status.ts +3 -2
- package/src/runtime/query/executor.ts +153 -12
- package/src/runtime/query/index.ts +5 -2
- package/src/runtime/query/iteration/outstanding-work.ts +22 -4
- package/src/runtime/query/iteration/phases/compaction.ts +14 -1
- package/src/runtime/query/iteration/phases/tool-review.ts +24 -7
- package/src/runtime/query/path-escalation.ts +52 -4
- package/src/runtime/query/review-policy.ts +47 -15
- package/src/runtime/query/steering.ts +32 -4
- package/src/sandbox/provider/local.ts +3 -1
- package/src/schedules/describe.ts +2 -0
- package/src/schedules/evaluate.ts +29 -1
- package/src/schedules/index.ts +9 -1
- package/src/schedules/next-fire.ts +9 -0
- package/src/schedules/spec.ts +42 -0
- package/src/schedules/types.ts +33 -3
- package/src/schedules/watch.ts +71 -0
- package/src/session/workspace/git-worktree.ts +3 -1
- package/src/skills/arguments.ts +250 -0
- package/src/skills/index.ts +10 -0
- package/src/skills/inline-shell-host.ts +16 -0
- package/src/skills/inline-shell.ts +1358 -22
- package/src/tools/builtins/bash-cwd.ts +2 -0
- package/src/tools/builtins/bash.ts +93 -13
- package/src/tools/builtins/exit-semantics.ts +10 -0
- package/src/tools/builtins/glob.ts +79 -15
- package/src/tools/builtins/grep.ts +37 -10
- package/src/tools/builtins/index.ts +15 -0
- package/src/tools/builtins/job.ts +1 -1
- package/src/tools/builtins/monitor.ts +4 -2
- package/src/tools/builtins/notebook-cells.ts +196 -0
- package/src/tools/builtins/notebook-edit.ts +280 -0
- package/src/tools/builtins/notebook-guard.ts +2 -1
- package/src/tools/builtins/push-notification.ts +138 -0
- package/src/tools/builtins/read-file.ts +9 -3
- package/src/tools/builtins/read-media.ts +2 -1
- package/src/tools/builtins/read-render.ts +5 -3
- package/src/tools/builtins/search-support.ts +17 -3
- package/src/tools/builtins/skill.ts +29 -7
- package/src/tools/builtins/sleep-guard.ts +128 -1
- package/src/tools/builtins/wait-for-job.ts +1 -1
- package/src/tools/builtins/web.ts +100 -3
- package/src/tools/command-shell.ts +166 -0
- package/src/tools/env-scrub.ts +13 -1
- package/src/tools/git-config-env.ts +143 -0
- package/src/tools/schedules/schedule-tool.ts +79 -3
- package/src/tools/schedules/types.ts +47 -9
- package/src/types/connector/mcp.ts +25 -0
- package/src/types/message/index.ts +53 -1
- package/src/types/permission/index.ts +20 -3
- package/src/types/plugin/index.ts +10 -2
- package/src/types/tool/index.ts +13 -0
- package/src/utils/plain-terminal-text.ts +142 -0
|
@@ -236,13 +236,331 @@ export class InlineShellError extends Error {
|
|
|
236
236
|
}
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
/** Stands where the value goes while a command is read for what surrounds it. */
|
|
240
|
+
const VALUE_MARK = '\uE001'
|
|
241
|
+
/** Stands for a word the shell computes: an expansion, a substitution, a decoded string. */
|
|
242
|
+
const COMPUTED_MARK = '\uE000'
|
|
243
|
+
|
|
244
|
+
interface CanonicalCommand {
|
|
245
|
+
/** The command up to the value, comments gone, line continuations joined, here-document bodies dropped. */
|
|
246
|
+
prefix: string
|
|
247
|
+
/** The rest of the command, the same way. */
|
|
248
|
+
suffix: string
|
|
249
|
+
/** The command cannot be read with certainty (an open quote or here-document, a comment or string holding the value). */
|
|
250
|
+
refuse: boolean
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Read a command the way the shell does, once, so everything after it works on one text:
|
|
255
|
+
* a `#` comment is dropped (an apostrophe in it is not a quote), a backslash before a
|
|
256
|
+
* newline joins the lines except inside a comment or single quotes, `$'...'` is read to
|
|
257
|
+
* its real end (`\'` is an escape in it), a quote inside a `$( )` inside double quotes is
|
|
258
|
+
* its own, and a closed here-document body is dropped. Anything it cannot read with
|
|
259
|
+
* certainty sets `refuse`: a quote, here-document or group that never closes, a value
|
|
260
|
+
* inside a comment, an ANSI-C string or a here-document body, and a `case` inside a
|
|
261
|
+
* substitution.
|
|
262
|
+
*/
|
|
263
|
+
function canonicalize(before: string, after: string): CanonicalCommand {
|
|
264
|
+
const text = `${before}${VALUE_MARK}${after}`
|
|
265
|
+
const markAt = before.length
|
|
266
|
+
interface Frame {
|
|
267
|
+
kind: 'code' | 'sub' | 'dq' | 'param'
|
|
268
|
+
/** A `$(`, `<(` or `>(` : the word goes on after its `)`. */
|
|
269
|
+
dollar: boolean
|
|
270
|
+
/** A `${` inside double quotes, where single quotes are ordinary characters. */
|
|
271
|
+
inDq: boolean
|
|
272
|
+
}
|
|
273
|
+
const frames: Frame[] = [{ kind: 'code', dollar: false, inDq: false }]
|
|
274
|
+
let out = ''
|
|
275
|
+
let markOut = -1
|
|
276
|
+
let refuse = false
|
|
277
|
+
let lex: 'norm' | 'single' | 'ansi' | 'comment' | 'backtick' = 'norm'
|
|
278
|
+
let escaped = false
|
|
279
|
+
let ansiBody = ''
|
|
280
|
+
let prev = '\n'
|
|
281
|
+
const pending: Array<{ delim: string; strip: boolean }> = []
|
|
282
|
+
const push = (kind: Frame['kind'], dollar: boolean, inDq = false): void => {
|
|
283
|
+
frames.push({ kind, dollar, inDq })
|
|
284
|
+
}
|
|
285
|
+
for (let i = 0; i < text.length; i++) {
|
|
286
|
+
const c = text[i] as string
|
|
287
|
+
if (i === markAt) {
|
|
288
|
+
markOut = out.length
|
|
289
|
+
if (lex === 'comment' || lex === 'ansi') refuse = true
|
|
290
|
+
prev = 'x'
|
|
291
|
+
continue
|
|
292
|
+
}
|
|
293
|
+
if (lex === 'single') {
|
|
294
|
+
out += c
|
|
295
|
+
if (c === "'") {
|
|
296
|
+
lex = 'norm'
|
|
297
|
+
prev = "'"
|
|
298
|
+
}
|
|
299
|
+
continue
|
|
300
|
+
}
|
|
301
|
+
if (lex === 'ansi') {
|
|
302
|
+
ansiBody += c
|
|
303
|
+
if (escaped) escaped = false
|
|
304
|
+
else if (c === '\\') escaped = true
|
|
305
|
+
else if (c === "'") {
|
|
306
|
+
const body = ansiBody.slice(0, -1)
|
|
307
|
+
out += body.includes('\\') ? `'${COMPUTED_MARK}'` : `'${body}'`
|
|
308
|
+
lex = 'norm'
|
|
309
|
+
prev = "'"
|
|
310
|
+
}
|
|
311
|
+
continue
|
|
312
|
+
}
|
|
313
|
+
if (lex === 'comment') {
|
|
314
|
+
if (c !== '\n') continue
|
|
315
|
+
lex = 'norm'
|
|
316
|
+
}
|
|
317
|
+
if (lex === 'backtick') {
|
|
318
|
+
out += c
|
|
319
|
+
if (escaped) escaped = false
|
|
320
|
+
else if (c === '\\') escaped = true
|
|
321
|
+
else if (c === '`') {
|
|
322
|
+
lex = 'norm'
|
|
323
|
+
prev = 'x'
|
|
324
|
+
}
|
|
325
|
+
continue
|
|
326
|
+
}
|
|
327
|
+
const top = frames[frames.length - 1] as Frame
|
|
328
|
+
const next = text[i + 1]
|
|
329
|
+
if (top.kind === 'dq' || top.kind === 'param') {
|
|
330
|
+
if (c === '\\') {
|
|
331
|
+
if (next === '\n') {
|
|
332
|
+
i += 1
|
|
333
|
+
continue
|
|
334
|
+
}
|
|
335
|
+
out += c
|
|
336
|
+
if (next !== undefined && next !== VALUE_MARK) {
|
|
337
|
+
out += next
|
|
338
|
+
i += 1
|
|
339
|
+
}
|
|
340
|
+
continue
|
|
341
|
+
}
|
|
342
|
+
if (top.kind === 'dq' && c === '"') {
|
|
343
|
+
frames.pop()
|
|
344
|
+
out += c
|
|
345
|
+
prev = '"'
|
|
346
|
+
continue
|
|
347
|
+
}
|
|
348
|
+
if (top.kind === 'param') {
|
|
349
|
+
if (c === '}') {
|
|
350
|
+
frames.pop()
|
|
351
|
+
out += c
|
|
352
|
+
prev = 'x'
|
|
353
|
+
continue
|
|
354
|
+
}
|
|
355
|
+
if (c === '"') {
|
|
356
|
+
push('dq', false)
|
|
357
|
+
out += c
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
360
|
+
if (c === "'" && !top.inDq) {
|
|
361
|
+
lex = 'single'
|
|
362
|
+
out += c
|
|
363
|
+
continue
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
if (c === '$' && next === '(') {
|
|
367
|
+
push('sub', true)
|
|
368
|
+
out += '$('
|
|
369
|
+
i += 1
|
|
370
|
+
prev = '('
|
|
371
|
+
continue
|
|
372
|
+
}
|
|
373
|
+
if (c === '$' && next === '{') {
|
|
374
|
+
push('param', false, top.kind === 'dq' || top.inDq)
|
|
375
|
+
out += '${'
|
|
376
|
+
i += 1
|
|
377
|
+
continue
|
|
378
|
+
}
|
|
379
|
+
if (c === '`') {
|
|
380
|
+
lex = 'backtick'
|
|
381
|
+
escaped = false
|
|
382
|
+
out += c
|
|
383
|
+
continue
|
|
384
|
+
}
|
|
385
|
+
out += c
|
|
386
|
+
continue
|
|
387
|
+
}
|
|
388
|
+
// Code: the top level, or inside a `$( )`, `( )` or `<( )`.
|
|
389
|
+
if (c === '\\') {
|
|
390
|
+
if (next === '\n') {
|
|
391
|
+
i += 1
|
|
392
|
+
continue
|
|
393
|
+
}
|
|
394
|
+
out += c
|
|
395
|
+
if (next !== undefined && next !== VALUE_MARK) {
|
|
396
|
+
out += next
|
|
397
|
+
i += 1
|
|
398
|
+
}
|
|
399
|
+
prev = 'x'
|
|
400
|
+
continue
|
|
401
|
+
}
|
|
402
|
+
if (c === "'") {
|
|
403
|
+
lex = 'single'
|
|
404
|
+
out += c
|
|
405
|
+
continue
|
|
406
|
+
}
|
|
407
|
+
if (c === '$' && next === "'") {
|
|
408
|
+
lex = 'ansi'
|
|
409
|
+
ansiBody = ''
|
|
410
|
+
escaped = false
|
|
411
|
+
i += 1
|
|
412
|
+
continue
|
|
413
|
+
}
|
|
414
|
+
if (c === '"') {
|
|
415
|
+
push('dq', false)
|
|
416
|
+
out += c
|
|
417
|
+
prev = '"'
|
|
418
|
+
continue
|
|
419
|
+
}
|
|
420
|
+
if (c === '`') {
|
|
421
|
+
lex = 'backtick'
|
|
422
|
+
escaped = false
|
|
423
|
+
out += c
|
|
424
|
+
continue
|
|
425
|
+
}
|
|
426
|
+
if (c === '$' && next === '(') {
|
|
427
|
+
push('sub', true)
|
|
428
|
+
out += '$('
|
|
429
|
+
i += 1
|
|
430
|
+
prev = '('
|
|
431
|
+
continue
|
|
432
|
+
}
|
|
433
|
+
if (c === '$' && next === '{') {
|
|
434
|
+
push('param', false)
|
|
435
|
+
out += '${'
|
|
436
|
+
i += 1
|
|
437
|
+
continue
|
|
438
|
+
}
|
|
439
|
+
if ((c === '<' || c === '>') && next === '(') {
|
|
440
|
+
push('sub', true)
|
|
441
|
+
out += `${c}(`
|
|
442
|
+
i += 1
|
|
443
|
+
prev = '('
|
|
444
|
+
continue
|
|
445
|
+
}
|
|
446
|
+
if (c === '(') {
|
|
447
|
+
push('sub', false)
|
|
448
|
+
out += c
|
|
449
|
+
prev = '('
|
|
450
|
+
continue
|
|
451
|
+
}
|
|
452
|
+
if (c === ')') {
|
|
453
|
+
if (frames.length > 1 && top.kind === 'sub') {
|
|
454
|
+
frames.pop()
|
|
455
|
+
prev = top.dollar ? 'x' : ')'
|
|
456
|
+
} else prev = ')'
|
|
457
|
+
out += c
|
|
458
|
+
continue
|
|
459
|
+
}
|
|
460
|
+
if (c === '#' && /[\s;|&()<>]/.test(prev)) {
|
|
461
|
+
lex = 'comment'
|
|
462
|
+
continue
|
|
463
|
+
}
|
|
464
|
+
if (
|
|
465
|
+
c === 'c' &&
|
|
466
|
+
text.startsWith('case', i) &&
|
|
467
|
+
/\s/.test(text[i + 4] ?? 'x') &&
|
|
468
|
+
/[\s;|&(]/.test(prev) &&
|
|
469
|
+
frames.some((f) => f.kind === 'sub')
|
|
470
|
+
) {
|
|
471
|
+
// Where a pattern's `)` ends is not something to guess inside a substitution.
|
|
472
|
+
refuse = true
|
|
473
|
+
}
|
|
474
|
+
if (c === '<' && next === '<' && text[i + 2] !== '<' && text[i - 1] !== '<') {
|
|
475
|
+
let j = i + 2
|
|
476
|
+
let strip = false
|
|
477
|
+
if (text[j] === '-') {
|
|
478
|
+
strip = true
|
|
479
|
+
j += 1
|
|
480
|
+
}
|
|
481
|
+
while (text[j] === ' ' || text[j] === '\t') j += 1
|
|
482
|
+
let delim = ''
|
|
483
|
+
while (j < text.length) {
|
|
484
|
+
const d = text[j] as string
|
|
485
|
+
if (d === "'" || d === '"') {
|
|
486
|
+
const close = text.indexOf(d, j + 1)
|
|
487
|
+
if (close === -1) {
|
|
488
|
+
refuse = true
|
|
489
|
+
j = text.length
|
|
490
|
+
break
|
|
491
|
+
}
|
|
492
|
+
delim += text.slice(j + 1, close)
|
|
493
|
+
j = close + 1
|
|
494
|
+
continue
|
|
495
|
+
}
|
|
496
|
+
if (d === '\\' && j + 1 < text.length) {
|
|
497
|
+
delim += text[j + 1]
|
|
498
|
+
j += 2
|
|
499
|
+
continue
|
|
500
|
+
}
|
|
501
|
+
if (/[\s;|&()<>]/.test(d)) break
|
|
502
|
+
delim += d
|
|
503
|
+
j += 1
|
|
504
|
+
}
|
|
505
|
+
if (delim === '' || delim.includes(VALUE_MARK)) refuse = true
|
|
506
|
+
if (markAt > i && markAt < j) {
|
|
507
|
+
refuse = true
|
|
508
|
+
markOut = out.length
|
|
509
|
+
}
|
|
510
|
+
pending.push({ delim: delim.replaceAll(VALUE_MARK, ''), strip })
|
|
511
|
+
out += '<h'
|
|
512
|
+
prev = 'x'
|
|
513
|
+
i = j - 1
|
|
514
|
+
continue
|
|
515
|
+
}
|
|
516
|
+
if (c === '\n') {
|
|
517
|
+
out += c
|
|
518
|
+
prev = '\n'
|
|
519
|
+
if (pending.length > 0) {
|
|
520
|
+
let pos = i + 1
|
|
521
|
+
for (const heredoc of pending) {
|
|
522
|
+
let closed = false
|
|
523
|
+
while (pos <= text.length) {
|
|
524
|
+
const end = text.indexOf('\n', pos)
|
|
525
|
+
const lineEnd = end === -1 ? text.length : end
|
|
526
|
+
const line = text.slice(pos, lineEnd)
|
|
527
|
+
if (markAt >= pos && markAt <= lineEnd) {
|
|
528
|
+
refuse = true
|
|
529
|
+
if (markOut === -1) markOut = out.length
|
|
530
|
+
}
|
|
531
|
+
pos = end === -1 ? text.length + 1 : end + 1
|
|
532
|
+
if ((heredoc.strip ? line.replace(/^\t+/, '') : line) === heredoc.delim) {
|
|
533
|
+
closed = true
|
|
534
|
+
break
|
|
535
|
+
}
|
|
536
|
+
if (end === -1) break
|
|
537
|
+
}
|
|
538
|
+
if (!closed) refuse = true
|
|
539
|
+
}
|
|
540
|
+
pending.length = 0
|
|
541
|
+
i = pos - 1
|
|
542
|
+
}
|
|
543
|
+
continue
|
|
544
|
+
}
|
|
545
|
+
out += c
|
|
546
|
+
prev = c
|
|
547
|
+
}
|
|
548
|
+
if (lex !== 'norm' && lex !== 'comment') refuse = true
|
|
549
|
+
if (frames.length > 1 || pending.length > 0) refuse = true
|
|
550
|
+
if (markOut === -1) {
|
|
551
|
+
refuse = true
|
|
552
|
+
markOut = out.length
|
|
553
|
+
}
|
|
554
|
+
return { prefix: out.slice(0, markOut), suffix: out.slice(markOut), refuse }
|
|
555
|
+
}
|
|
556
|
+
|
|
239
557
|
/** Where a value sits in the text of a command, as far as the shell reads it. */
|
|
240
558
|
type ShellContext = 'none' | 'single' | 'double' | 'unsafe'
|
|
241
559
|
|
|
242
560
|
/**
|
|
243
561
|
* How the shell reads the position at the end of `prefix`, the text of one
|
|
244
562
|
* command up to a value: outside any quote, inside single or double quotes, or
|
|
245
|
-
* somewhere a value cannot be made safe (a comment, a dangling backslash
|
|
563
|
+
* somewhere a value cannot be made safe (a comment, a dangling backslash or a bare `$`, a here-document, a `${...}`, an
|
|
246
564
|
* arithmetic or backtick expansion, an ANSI-C string that is not closed).
|
|
247
565
|
*
|
|
248
566
|
* A `$( ... )` starts a fresh reading of its own, so a value inside one is
|
|
@@ -290,7 +608,25 @@ function shellContextAt(prefix: string): ShellContext {
|
|
|
290
608
|
if (c === '$') {
|
|
291
609
|
const next = prefix[i + 1]
|
|
292
610
|
if (next === '(') {
|
|
293
|
-
if (prefix[i + 2] === '(')
|
|
611
|
+
if (prefix[i + 2] === '(') {
|
|
612
|
+
// A closed arithmetic expansion is one word; a value inside one is not quotable.
|
|
613
|
+
let depth = 0
|
|
614
|
+
let end = -1
|
|
615
|
+
for (let j = i + 2; j < prefix.length; j++) {
|
|
616
|
+
if (prefix[j] === '(') depth += 1
|
|
617
|
+
else if (prefix[j] === ')') {
|
|
618
|
+
depth -= 1
|
|
619
|
+
if (depth === 0) {
|
|
620
|
+
end = j
|
|
621
|
+
break
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (end === -1 || prefix[end + 1] !== ')') return 'unsafe'
|
|
626
|
+
i = end + 1
|
|
627
|
+
wordStart = false
|
|
628
|
+
continue
|
|
629
|
+
}
|
|
294
630
|
stack.push('sub')
|
|
295
631
|
i += 1
|
|
296
632
|
wordStart = true
|
|
@@ -309,7 +645,9 @@ function shellContextAt(prefix: string): ShellContext {
|
|
|
309
645
|
i = end
|
|
310
646
|
wordStart = false
|
|
311
647
|
continue
|
|
312
|
-
}
|
|
648
|
+
} // A `$` right before the value would join its first character: `$` and a
|
|
649
|
+
// quote read as an ANSI-C string, `$` and `(` or a name as an expansion.
|
|
650
|
+
if (i === prefix.length - 1) return 'unsafe'
|
|
313
651
|
}
|
|
314
652
|
if (c === '`') {
|
|
315
653
|
const end = skipTo(i + 1, '`')
|
|
@@ -342,7 +680,14 @@ function shellContextAt(prefix: string): ShellContext {
|
|
|
342
680
|
wordStart = true
|
|
343
681
|
continue
|
|
344
682
|
}
|
|
345
|
-
|
|
683
|
+
// A here-string (`<<<`) takes an ordinary word, so the whole run is skipped before a
|
|
684
|
+
// here-document (`<<`) is looked for.
|
|
685
|
+
if (c === '<' && prefix.startsWith('<<<', i)) {
|
|
686
|
+
i += 2
|
|
687
|
+
wordStart = true
|
|
688
|
+
continue
|
|
689
|
+
}
|
|
690
|
+
if (c === '<' && prefix[i + 1] === '<') return 'unsafe'
|
|
346
691
|
if (c === '#' && wordStart) {
|
|
347
692
|
const end = prefix.indexOf('\n', i)
|
|
348
693
|
// A value inside a comment would meet a newline that ends the comment.
|
|
@@ -357,6 +702,851 @@ function shellContextAt(prefix: string): ShellContext {
|
|
|
357
702
|
return stack[stack.length - 1] === 'double' ? 'double' : 'none'
|
|
358
703
|
}
|
|
359
704
|
|
|
705
|
+
/** Builtins whose arguments the shell reads again as shell text, a subscript or arithmetic. */
|
|
706
|
+
const REREAD_BUILTINS = new Set([
|
|
707
|
+
'trap',
|
|
708
|
+
'eval',
|
|
709
|
+
'let',
|
|
710
|
+
'source',
|
|
711
|
+
'.',
|
|
712
|
+
'mapfile',
|
|
713
|
+
'readarray',
|
|
714
|
+
'unset',
|
|
715
|
+
'getopts',
|
|
716
|
+
])
|
|
717
|
+
/** Programs that hand a string to a shell of their own. */
|
|
718
|
+
const REREAD_PROGRAMS = new Set(['ssh', 'su', 'watch', 'parallel'])
|
|
719
|
+
/** Builtins that take names: a value in the name position is read as a name with a subscript. */
|
|
720
|
+
const NAME_BUILTINS = new Set(['declare', 'typeset', 'local', 'readonly', 'export'])
|
|
721
|
+
const SHELL_NAMES = new Set([
|
|
722
|
+
'sh',
|
|
723
|
+
'bash',
|
|
724
|
+
'zsh',
|
|
725
|
+
'dash',
|
|
726
|
+
'ksh',
|
|
727
|
+
'ash',
|
|
728
|
+
'fish',
|
|
729
|
+
'rbash',
|
|
730
|
+
'csh',
|
|
731
|
+
'tcsh',
|
|
732
|
+
'mksh',
|
|
733
|
+
'yash',
|
|
734
|
+
'pwsh',
|
|
735
|
+
'powershell',
|
|
736
|
+
])
|
|
737
|
+
/** Programs that run a `-c` string like a shell does. */
|
|
738
|
+
const STRING_RUNNERS = new Set(['flock', 'script', 'runuser', 'sg'])
|
|
739
|
+
/** Words that only begin a statement; the command follows them. */
|
|
740
|
+
const SYNTAX_PREFIXES = new Set(['!', '{', 'if', 'then', 'else', 'elif', 'while', 'until', 'do'])
|
|
741
|
+
/** Words that run the next command, with options of their own before it. */
|
|
742
|
+
const COMMAND_WRAPPERS = new Set([
|
|
743
|
+
...SYNTAX_PREFIXES,
|
|
744
|
+
'time',
|
|
745
|
+
'command',
|
|
746
|
+
'builtin',
|
|
747
|
+
'exec',
|
|
748
|
+
'env',
|
|
749
|
+
'sudo',
|
|
750
|
+
'doas',
|
|
751
|
+
'nohup',
|
|
752
|
+
'nice',
|
|
753
|
+
'ionice',
|
|
754
|
+
'timeout',
|
|
755
|
+
'xargs',
|
|
756
|
+
'setsid',
|
|
757
|
+
'stdbuf',
|
|
758
|
+
'coproc',
|
|
759
|
+
])
|
|
760
|
+
/** Programs that start another command from their arguments, with options not listed here. */
|
|
761
|
+
const OTHER_WRAPPERS = new Set([
|
|
762
|
+
'taskset',
|
|
763
|
+
'unshare',
|
|
764
|
+
'setpriv',
|
|
765
|
+
'bwrap',
|
|
766
|
+
'chroot',
|
|
767
|
+
'nsenter',
|
|
768
|
+
'strace',
|
|
769
|
+
'ltrace',
|
|
770
|
+
'systemd-run',
|
|
771
|
+
'tmux',
|
|
772
|
+
'screen',
|
|
773
|
+
'firejail',
|
|
774
|
+
'faketime',
|
|
775
|
+
'fakeroot',
|
|
776
|
+
'chrt',
|
|
777
|
+
'proot',
|
|
778
|
+
'busybox',
|
|
779
|
+
'find',
|
|
780
|
+
])
|
|
781
|
+
/** The options of each wrapper that take the next word as their argument. */
|
|
782
|
+
const WRAPPER_OPTION_ARGS: Readonly<Record<string, readonly string[]>> = {
|
|
783
|
+
sudo: ['-u', '-g', '-h', '-p', '-C', '-D', '-R', '-T', '-U', '-r', '-t'],
|
|
784
|
+
doas: ['-u', '-C'],
|
|
785
|
+
env: ['-u', '-C', '-a'],
|
|
786
|
+
nice: ['-n'],
|
|
787
|
+
ionice: ['-c', '-n', '-p', '-P', '-u'],
|
|
788
|
+
timeout: ['-s', '-k'],
|
|
789
|
+
xargs: ['-a', '-d', '-E', '-e', '-I', '-L', '-l', '-n', '-P', '-s'],
|
|
790
|
+
stdbuf: ['-i', '-o', '-e'],
|
|
791
|
+
exec: ['-a'],
|
|
792
|
+
time: ['-f', '-o'],
|
|
793
|
+
}
|
|
794
|
+
/** Names a wrapper may be followed by, anywhere, that read a string as shell text. */
|
|
795
|
+
const REREAD_AFTER_WRAPPER = new Set(['eval', 'trap', 'source', 'let'])
|
|
796
|
+
const ASSIGNMENT_WORD = /^[A-Za-z_]\w*(\[[^\]]*\])?\+?=/
|
|
797
|
+
/** A redirection with its target in the same word, or a bare operator whose target is the next word. */
|
|
798
|
+
const REDIRECTION_WORD = /^(\d*|&)(>>?|<<?<?|>&|<&|>\||<>|&>>?)/
|
|
799
|
+
const REDIRECTION_OPERATOR = /^(\d*|&)(>>?|<<?<?|>&|<&|>\||<>|&>>?)$/
|
|
800
|
+
/** A shell option that carries the command string. */
|
|
801
|
+
const COMMAND_STRING_FLAG = /^(-[a-zA-Z]*[cC][a-zA-Z]*|--command)$/
|
|
802
|
+
const ARITHMETIC_TEST = /(?:^|\s)-(?:eq|ne|lt|gt|le|ge|v|R)(?:\s|$)/
|
|
803
|
+
/** A later use, in the same command, that evaluates a variable as arithmetic or prompt text. */
|
|
804
|
+
const EVALUATES_LATER =
|
|
805
|
+
/\$\(\(|\$\[|\(\(|\blet\b|\bset\s+(?:-\w*x|-o\s+xtrace)|\b(?:declare|typeset|integer)\s+-\w*i|\[\[[^\n]*?\s-(?:eq|ne|lt|gt|le|ge)\s/
|
|
806
|
+
/** Variables the shell expands as text, with command substitution, every time they are used. */
|
|
807
|
+
const EXPANDED_VARIABLES =
|
|
808
|
+
/^(?:PS[0-4]|PROMPT_COMMAND|BASH_ENV|ENV|SHELLOPTS|BASHOPTS|RANDOM|SRANDOM|OPTIND)\+?=/
|
|
809
|
+
/** A word that is not a literal: it holds an expansion, a substitution or a brace list. */
|
|
810
|
+
const DYNAMIC_WORD = /[${\uE000\uE002]/
|
|
811
|
+
/** A word that may name a program only after the shell expands it: a glob in a name. */
|
|
812
|
+
const GLOB_WORD = /[*?[]|[@+!]\(/
|
|
813
|
+
/** A word the shell computes: an expansion, a substitution, a process substitution. */
|
|
814
|
+
const COMPUTED_WORD = /[$\uE000\uE002]/
|
|
815
|
+
/** An expansion that reads a variable as a subscript, an offset, an indirect name or prompt text. */
|
|
816
|
+
const PARAM_REREAD =
|
|
817
|
+
/\$\{(?:!|#?\w+\[(?!\s*(?:@|\*|\d+)\s*\])|\w+:(?![-+=?]|\s*\d+(?::\s*\d+)?\s*\})|\w+@[PQEAaKk])/
|
|
818
|
+
/** An assignment to a subscript that is not a number: the subscript is read as arithmetic. */
|
|
819
|
+
const SUBSCRIPT_ASSIGNMENT = /^[A-Za-z_]\w*\[(?!\d+\])[^\]]+\]\+?=/
|
|
820
|
+
|
|
821
|
+
/** A statement that a substitution inside it sits in, paused where the substitution began. */
|
|
822
|
+
interface Ancestor {
|
|
823
|
+
/** The words so far, the last one the word the substitution is part of (may be empty). */
|
|
824
|
+
words: string[]
|
|
825
|
+
/** The substitution is a process substitution (`<( )`, `>( )`). */
|
|
826
|
+
psub: boolean
|
|
827
|
+
/** A process substitution that is the target of a redirection operator (`< <( )`). */
|
|
828
|
+
afterRedirect: boolean
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
interface StatementWords {
|
|
832
|
+
words: string[]
|
|
833
|
+
/** The value would join the last word, which is not finished (an open quote counts). */
|
|
834
|
+
mid: boolean
|
|
835
|
+
/** The statements around the one the value is in, innermost last. */
|
|
836
|
+
ancestors: Ancestor[]
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
/**
|
|
840
|
+
* The words of the statement the end of `prefix` sits in, quotes removed. With `collect`,
|
|
841
|
+
* every statement of the text is handed to it as well, each as its words.
|
|
842
|
+
*/
|
|
843
|
+
function statementWordsAt(prefix: string, collect?: string[][]): StatementWords {
|
|
844
|
+
interface Frame {
|
|
845
|
+
words: string[]
|
|
846
|
+
cur: string
|
|
847
|
+
has: boolean
|
|
848
|
+
double: boolean
|
|
849
|
+
sub: boolean
|
|
850
|
+
psub: boolean
|
|
851
|
+
ancestor?: Ancestor
|
|
852
|
+
}
|
|
853
|
+
const fresh = (sub: boolean, psub = false, ancestor?: Ancestor): Frame => ({
|
|
854
|
+
words: [],
|
|
855
|
+
cur: '',
|
|
856
|
+
has: false,
|
|
857
|
+
double: false,
|
|
858
|
+
sub,
|
|
859
|
+
psub,
|
|
860
|
+
ancestor,
|
|
861
|
+
})
|
|
862
|
+
const stack: Frame[] = [fresh(false)]
|
|
863
|
+
let single = false
|
|
864
|
+
const top = (): Frame => stack[stack.length - 1] as Frame
|
|
865
|
+
const endWord = (f: Frame): void => {
|
|
866
|
+
if (f.has || f.cur !== '') f.words.push(f.cur)
|
|
867
|
+
f.cur = ''
|
|
868
|
+
f.has = false
|
|
869
|
+
}
|
|
870
|
+
const reset = (f: Frame): void => {
|
|
871
|
+
if (collect !== undefined && f.words.length > 0) collect.push(f.words)
|
|
872
|
+
f.words = []
|
|
873
|
+
}
|
|
874
|
+
const open = (f: Frame, psub: boolean): void => {
|
|
875
|
+
const cur = psub ? f.cur.replace(/\d*[<>]$/, '') : f.cur
|
|
876
|
+
const finished = f.words[f.words.length - 1] ?? ''
|
|
877
|
+
stack.push(
|
|
878
|
+
fresh(true, psub, {
|
|
879
|
+
words: [...f.words, cur],
|
|
880
|
+
psub,
|
|
881
|
+
afterRedirect: psub && REDIRECTION_OPERATOR.test(finished),
|
|
882
|
+
}),
|
|
883
|
+
)
|
|
884
|
+
}
|
|
885
|
+
const close = (): void => {
|
|
886
|
+
const child = stack.pop() as Frame
|
|
887
|
+
const parent = top()
|
|
888
|
+
if (child.psub) parent.cur = parent.cur.replace(/\d*[<>]$/, '')
|
|
889
|
+
parent.cur += child.psub ? '' : ''
|
|
890
|
+
parent.has = true
|
|
891
|
+
}
|
|
892
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
893
|
+
const c = prefix[i] as string
|
|
894
|
+
const f = top()
|
|
895
|
+
if (single) {
|
|
896
|
+
if (c === "'") single = false
|
|
897
|
+
else f.cur += c
|
|
898
|
+
continue
|
|
899
|
+
}
|
|
900
|
+
if (c === '\\') {
|
|
901
|
+
f.cur += prefix[i + 1] ?? ''
|
|
902
|
+
f.has = true
|
|
903
|
+
i += 1
|
|
904
|
+
continue
|
|
905
|
+
}
|
|
906
|
+
if (c === '`') {
|
|
907
|
+
const end = prefix.indexOf('`', i + 1)
|
|
908
|
+
f.cur += ''
|
|
909
|
+
f.has = true
|
|
910
|
+
if (end === -1) break
|
|
911
|
+
i = end
|
|
912
|
+
continue
|
|
913
|
+
}
|
|
914
|
+
if (c === '$' && prefix[i + 1] === '(') {
|
|
915
|
+
if (prefix[i + 2] === '(') {
|
|
916
|
+
// Arithmetic: one computed word, unless it is still open.
|
|
917
|
+
let depth = 0
|
|
918
|
+
let end = -1
|
|
919
|
+
for (let j = i + 2; j < prefix.length; j++) {
|
|
920
|
+
if (prefix[j] === '(') depth += 1
|
|
921
|
+
else if (prefix[j] === ')') {
|
|
922
|
+
depth -= 1
|
|
923
|
+
if (depth === 0) {
|
|
924
|
+
end = j
|
|
925
|
+
break
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
if (end === -1) break
|
|
930
|
+
if (prefix[end + 1] === ')') {
|
|
931
|
+
f.cur += ''
|
|
932
|
+
f.has = true
|
|
933
|
+
i = end + 1
|
|
934
|
+
continue
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
open(f, false)
|
|
938
|
+
i += 1
|
|
939
|
+
continue
|
|
940
|
+
}
|
|
941
|
+
if (f.double) {
|
|
942
|
+
if (c === '"') f.double = false
|
|
943
|
+
else f.cur += c
|
|
944
|
+
continue
|
|
945
|
+
}
|
|
946
|
+
if (c === "'") {
|
|
947
|
+
single = true
|
|
948
|
+
f.has = true
|
|
949
|
+
continue
|
|
950
|
+
}
|
|
951
|
+
if (c === '"') {
|
|
952
|
+
f.double = true
|
|
953
|
+
f.has = true
|
|
954
|
+
continue
|
|
955
|
+
}
|
|
956
|
+
if (/\s/.test(c)) {
|
|
957
|
+
endWord(f)
|
|
958
|
+
if (c === '\n') reset(f)
|
|
959
|
+
continue
|
|
960
|
+
}
|
|
961
|
+
// `>&2`, `2>&1`, `&>f` and `<&3` are redirections, not a statement break.
|
|
962
|
+
if (c === '&' && (prefix[i - 1] === '>' || prefix[i - 1] === '<' || prefix[i + 1] === '>')) {
|
|
963
|
+
f.cur += c
|
|
964
|
+
continue
|
|
965
|
+
}
|
|
966
|
+
if (c === ';' || c === '|' || c === '&') {
|
|
967
|
+
endWord(f)
|
|
968
|
+
reset(f)
|
|
969
|
+
continue
|
|
970
|
+
}
|
|
971
|
+
if (c === '(') {
|
|
972
|
+
if (/^\d*[<>]$/.test(f.cur) && !f.has) {
|
|
973
|
+
open(f, true)
|
|
974
|
+
} else if (f.cur === '' && !f.has) {
|
|
975
|
+
reset(f)
|
|
976
|
+
if (prefix[i + 1] === '(') {
|
|
977
|
+
f.words = ['((']
|
|
978
|
+
i += 1
|
|
979
|
+
}
|
|
980
|
+
} else f.cur += c
|
|
981
|
+
continue
|
|
982
|
+
}
|
|
983
|
+
if (c === ')') {
|
|
984
|
+
if (f.sub && stack.length > 1) {
|
|
985
|
+
endWord(f)
|
|
986
|
+
reset(f)
|
|
987
|
+
close()
|
|
988
|
+
} else {
|
|
989
|
+
endWord(f)
|
|
990
|
+
reset(f)
|
|
991
|
+
}
|
|
992
|
+
continue
|
|
993
|
+
}
|
|
994
|
+
f.cur += c
|
|
995
|
+
}
|
|
996
|
+
if (collect !== undefined) {
|
|
997
|
+
for (const frame of stack) {
|
|
998
|
+
endWord(frame)
|
|
999
|
+
if (frame.words.length > 0) collect.push(frame.words)
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
const f = top()
|
|
1003
|
+
const words = [...f.words]
|
|
1004
|
+
const mid = f.has || f.cur !== '' || single
|
|
1005
|
+
if (mid) words.push(f.cur)
|
|
1006
|
+
const ancestors = stack.slice(1).map((frame) => frame.ancestor as Ancestor)
|
|
1007
|
+
return { words, mid, ancestors }
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
interface Regions {
|
|
1011
|
+
/** A subscript (`a[`, `arr=([`, `$[`) is still open. */
|
|
1012
|
+
subscript: boolean
|
|
1013
|
+
/** Where an unclosed `[[` starts, or -1. */
|
|
1014
|
+
doubleBracketAt: number
|
|
1015
|
+
/** An arithmetic command (`((`, `for ((`) is still open. */
|
|
1016
|
+
arithmetic: boolean
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* Where the end of `prefix` sits in a bracketed region the shell reads as an
|
|
1021
|
+
* expression: a subscript, a `[[ ... ]]` test, a `(( ... ))` command. A `[`
|
|
1022
|
+
* inside quotes is text, except `$[`; every other `[` that follows a name, a
|
|
1023
|
+
* parenthesis, or sits in an array initialiser opens a subscript, whatever
|
|
1024
|
+
* space follows it.
|
|
1025
|
+
*/
|
|
1026
|
+
function regionsAt(prefix: string): Regions {
|
|
1027
|
+
let single = false
|
|
1028
|
+
// Double quotes and the `$( )` opened inside them, innermost last.
|
|
1029
|
+
const quotes: Array<{ kind: 'dq' } | { kind: 'sub'; parens: number }> = []
|
|
1030
|
+
const inDouble = (): boolean => quotes[quotes.length - 1]?.kind === 'dq'
|
|
1031
|
+
let open = 0
|
|
1032
|
+
let doubleBracketAt = -1
|
|
1033
|
+
let arithmetic = false
|
|
1034
|
+
let arithmeticDepth = 0
|
|
1035
|
+
let wordStart = true
|
|
1036
|
+
const parens: boolean[] = []
|
|
1037
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
1038
|
+
const c = prefix[i] as string
|
|
1039
|
+
const prev = prefix[i - 1] ?? ''
|
|
1040
|
+
if (single) {
|
|
1041
|
+
if (c === "'") single = false
|
|
1042
|
+
continue
|
|
1043
|
+
}
|
|
1044
|
+
if (c === '\\') {
|
|
1045
|
+
i += 1
|
|
1046
|
+
wordStart = false
|
|
1047
|
+
continue
|
|
1048
|
+
}
|
|
1049
|
+
if (inDouble()) {
|
|
1050
|
+
if (c === '"') quotes.pop()
|
|
1051
|
+
else if (c === '$' && prefix[i + 1] === '[') {
|
|
1052
|
+
open += 1
|
|
1053
|
+
i += 1
|
|
1054
|
+
} else if (c === '$' && prefix[i + 1] === '(') {
|
|
1055
|
+
quotes.push({ kind: 'sub', parens: parens.length })
|
|
1056
|
+
parens.push(false)
|
|
1057
|
+
i += 1
|
|
1058
|
+
wordStart = true
|
|
1059
|
+
}
|
|
1060
|
+
continue
|
|
1061
|
+
}
|
|
1062
|
+
if (c === "'") {
|
|
1063
|
+
single = true
|
|
1064
|
+
wordStart = false
|
|
1065
|
+
continue
|
|
1066
|
+
}
|
|
1067
|
+
if (c === '"') {
|
|
1068
|
+
quotes.push({ kind: 'dq' })
|
|
1069
|
+
wordStart = false
|
|
1070
|
+
continue
|
|
1071
|
+
}
|
|
1072
|
+
if (arithmetic) {
|
|
1073
|
+
if (c === '(') arithmeticDepth += 1
|
|
1074
|
+
else if (c === ')') {
|
|
1075
|
+
if (arithmeticDepth > 0) arithmeticDepth -= 1
|
|
1076
|
+
else if (prefix[i + 1] === ')') {
|
|
1077
|
+
arithmetic = false
|
|
1078
|
+
i += 1
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
continue
|
|
1082
|
+
}
|
|
1083
|
+
if (c === '$' && prefix[i + 1] === '[') {
|
|
1084
|
+
open += 1
|
|
1085
|
+
i += 1
|
|
1086
|
+
wordStart = false
|
|
1087
|
+
continue
|
|
1088
|
+
}
|
|
1089
|
+
if (c === '(') {
|
|
1090
|
+
if (prefix[i + 1] === '(' && prev !== '$') {
|
|
1091
|
+
arithmetic = true
|
|
1092
|
+
arithmeticDepth = 0
|
|
1093
|
+
i += 1
|
|
1094
|
+
} else parens.push(prev === '=')
|
|
1095
|
+
wordStart = true
|
|
1096
|
+
continue
|
|
1097
|
+
}
|
|
1098
|
+
if (c === ')') {
|
|
1099
|
+
parens.pop()
|
|
1100
|
+
const frame = quotes[quotes.length - 1]
|
|
1101
|
+
if (frame?.kind === 'sub' && frame.parens === parens.length) quotes.pop()
|
|
1102
|
+
wordStart = false
|
|
1103
|
+
continue
|
|
1104
|
+
}
|
|
1105
|
+
if (c === '[') {
|
|
1106
|
+
if (open > 0) open += 1
|
|
1107
|
+
else if (prefix[i + 1] === '[' && wordStart && /\s/.test(prefix[i + 2] ?? ' ')) {
|
|
1108
|
+
if (doubleBracketAt === -1) doubleBracketAt = i
|
|
1109
|
+
i += 1
|
|
1110
|
+
} else if (/[\w)\]]/.test(prev) || (wordStart && parens[parens.length - 1] === true))
|
|
1111
|
+
open += 1
|
|
1112
|
+
wordStart = false
|
|
1113
|
+
continue
|
|
1114
|
+
}
|
|
1115
|
+
if (c === ']') {
|
|
1116
|
+
if (
|
|
1117
|
+
doubleBracketAt !== -1 &&
|
|
1118
|
+
prefix[i + 1] === ']' &&
|
|
1119
|
+
wordStart &&
|
|
1120
|
+
/[\s;|&)]/.test(prefix[i + 2] ?? ' ')
|
|
1121
|
+
) {
|
|
1122
|
+
doubleBracketAt = -1
|
|
1123
|
+
i += 1
|
|
1124
|
+
} else if (open > 0) open -= 1
|
|
1125
|
+
wordStart = false
|
|
1126
|
+
continue
|
|
1127
|
+
}
|
|
1128
|
+
wordStart = /[\s;|&(]/.test(c)
|
|
1129
|
+
}
|
|
1130
|
+
return { subscript: open > 0, doubleBracketAt, arithmetic }
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
/** The name of a command word: its path stripped. */
|
|
1134
|
+
function baseName(word: string): string {
|
|
1135
|
+
return word.slice(word.lastIndexOf('/') + 1)
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/** A command word that is not a literal name: it holds an expansion, or a glob that picks the program. */
|
|
1139
|
+
function unknownWord(name: string): boolean {
|
|
1140
|
+
return DYNAMIC_WORD.test(name) || (name !== '[' && name !== '[[' && GLOB_WORD.test(name))
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
interface CommandPosition {
|
|
1144
|
+
/** Where the command word is. */
|
|
1145
|
+
at: number
|
|
1146
|
+
/** Where the first program that starts another command is, or -1. */
|
|
1147
|
+
wrapper: number
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/** Where the command word of `words` is, past assignments, redirections, wrappers and their options. */
|
|
1151
|
+
function commandIndex(words: readonly string[]): CommandPosition {
|
|
1152
|
+
let at = 0
|
|
1153
|
+
let wrapper = -1
|
|
1154
|
+
while (at < words.length) {
|
|
1155
|
+
const w = words[at] as string
|
|
1156
|
+
if (REDIRECTION_OPERATOR.test(w)) {
|
|
1157
|
+
at += 2
|
|
1158
|
+
continue
|
|
1159
|
+
}
|
|
1160
|
+
if (REDIRECTION_WORD.test(w) || ASSIGNMENT_WORD.test(w)) {
|
|
1161
|
+
at += 1
|
|
1162
|
+
continue
|
|
1163
|
+
}
|
|
1164
|
+
const base = baseName(w)
|
|
1165
|
+
if (base === 'function') {
|
|
1166
|
+
// `function name {`: the name is not a command.
|
|
1167
|
+
at += 2
|
|
1168
|
+
if (words[at] === '()') at += 1
|
|
1169
|
+
continue
|
|
1170
|
+
}
|
|
1171
|
+
if (!COMMAND_WRAPPERS.has(base)) break
|
|
1172
|
+
if (!SYNTAX_PREFIXES.has(base) && wrapper === -1) wrapper = at
|
|
1173
|
+
at += 1
|
|
1174
|
+
// `coproc NAME { ... }`: the name is not a command.
|
|
1175
|
+
if (base === 'coproc' && words[at + 1] === '{') at += 1
|
|
1176
|
+
const takes = WRAPPER_OPTION_ARGS[base] ?? []
|
|
1177
|
+
while (at < words.length && /^-/.test(words[at] as string)) {
|
|
1178
|
+
const option = words[at] as string
|
|
1179
|
+
at += 1
|
|
1180
|
+
if (takes.includes(option) && at < words.length) at += 1
|
|
1181
|
+
}
|
|
1182
|
+
if (base === 'timeout' && /^\d[\d.]*[smhd]?$/.test(words[at] ?? '')) at += 1
|
|
1183
|
+
}
|
|
1184
|
+
const command = words[at]
|
|
1185
|
+
if (wrapper === -1 && command !== undefined && OTHER_WRAPPERS.has(baseName(command))) wrapper = at
|
|
1186
|
+
return { at, wrapper }
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* Whether the words after a shell's name, with a value to follow (joining the last word
|
|
1191
|
+
* when `mid`), make the value the command string of the shell: `-c` seen among the
|
|
1192
|
+
* options (which end at the first operand or `--`, `-o` and `-O` taking a word), and no
|
|
1193
|
+
* operand yet before the value.
|
|
1194
|
+
*/
|
|
1195
|
+
function shellRunsValueAsScript(rest: readonly string[], mid: boolean): boolean {
|
|
1196
|
+
let sawCommand = false
|
|
1197
|
+
let i = 0
|
|
1198
|
+
for (; i < rest.length; i++) {
|
|
1199
|
+
const w = rest[i] as string
|
|
1200
|
+
if (w === '--') {
|
|
1201
|
+
i += 1
|
|
1202
|
+
break
|
|
1203
|
+
}
|
|
1204
|
+
if (!/^[-+]./.test(w)) break
|
|
1205
|
+
if (COMMAND_STRING_FLAG.test(w)) sawCommand = true
|
|
1206
|
+
if (/^(?:[-+][a-zA-Z]*[oO]|--rcfile|--init-file)$/.test(w)) i += 1
|
|
1207
|
+
}
|
|
1208
|
+
if (!sawCommand) return false
|
|
1209
|
+
const operands = Math.max(0, rest.length - i)
|
|
1210
|
+
return operands - (mid ? 1 : 0) <= 0
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
/** The same for a program that runs a `-c` string like a shell, whose options may follow an operand. */
|
|
1214
|
+
function runnerRunsValueAsScript(rest: readonly string[], mid: boolean): boolean {
|
|
1215
|
+
const flag = rest.findIndex((w) => COMMAND_STRING_FLAG.test(w))
|
|
1216
|
+
if (flag === -1) return false
|
|
1217
|
+
return rest.length - flag - 1 - (mid ? 1 : 0) <= 0
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/** Whether a shell-like name, its words after it, and a value to follow, make the value a script. */
|
|
1221
|
+
function runsValueAsScript(name: string, rest: readonly string[], mid: boolean): boolean {
|
|
1222
|
+
if (rest.some((w) => w === '<<<' || w.includes(''))) return true
|
|
1223
|
+
return STRING_RUNNERS.has(name)
|
|
1224
|
+
? runnerRunsValueAsScript(rest, mid)
|
|
1225
|
+
: shellRunsValueAsScript(rest, mid)
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
/**
|
|
1229
|
+
* Whether, after a wrapper program, a later word is a shell or a command that reads a
|
|
1230
|
+
* string again and the value would be its script. The wrapper's own options and their
|
|
1231
|
+
* arguments are not told apart: every later word is looked at.
|
|
1232
|
+
*/
|
|
1233
|
+
function wrappedRereads(words: readonly string[], wrapper: number, mid: boolean): boolean {
|
|
1234
|
+
let from = wrapper + 1
|
|
1235
|
+
if (baseName(words[wrapper] as string) === 'find') {
|
|
1236
|
+
const exec = words.findIndex((w, k) => k > wrapper && /^-(?:exec|execdir|ok|okdir)$/.test(w))
|
|
1237
|
+
if (exec === -1) return false
|
|
1238
|
+
from = exec + 1
|
|
1239
|
+
}
|
|
1240
|
+
for (let k = from; k < words.length; k++) {
|
|
1241
|
+
const name = baseName(words[k] as string)
|
|
1242
|
+
if (SHELL_NAMES.has(name) || STRING_RUNNERS.has(name) || unknownWord(name)) {
|
|
1243
|
+
if (runsValueAsScript(name, words.slice(k + 1), mid)) return true
|
|
1244
|
+
continue
|
|
1245
|
+
}
|
|
1246
|
+
if (REREAD_AFTER_WRAPPER.has(name) || REREAD_PROGRAMS.has(name)) return true
|
|
1247
|
+
}
|
|
1248
|
+
return false
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Whether the pipeline stage a value feeds, in the rest of its command, ends in a shell that
|
|
1253
|
+
* reads its input as commands (`| sh`, `| bash -s`).
|
|
1254
|
+
*/
|
|
1255
|
+
function feedsShell(suffix: string): boolean {
|
|
1256
|
+
const stage = suffix.split(/\n|;|&&|\|\||\)/)[0] ?? ''
|
|
1257
|
+
const stages = stage.split(/\|&?/).slice(1)
|
|
1258
|
+
for (const piece of stages) {
|
|
1259
|
+
const { words } = statementWordsAt(piece)
|
|
1260
|
+
const at = commandIndex(words).at
|
|
1261
|
+
const command = words[at]
|
|
1262
|
+
if (command === undefined) continue
|
|
1263
|
+
const isShell = SHELL_NAMES.has(baseName(command)) || unknownWord(baseName(command))
|
|
1264
|
+
if (!isShell) continue
|
|
1265
|
+
const args = words.slice(at + 1)
|
|
1266
|
+
if (args.some((w) => COMMAND_STRING_FLAG.test(w))) continue
|
|
1267
|
+
if (args.some((w) => /^-[a-zA-Z]*s/.test(w)) || args.every((w) => w.startsWith('-')))
|
|
1268
|
+
return true
|
|
1269
|
+
}
|
|
1270
|
+
return false
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
/** Whether a `mapfile` or `readarray` with these words would read the value as its callback or array name. */
|
|
1274
|
+
function mapfileTakesValue(rest: readonly string[], mid: boolean): boolean {
|
|
1275
|
+
let operands = 0
|
|
1276
|
+
for (let i = 0; i < rest.length; i++) {
|
|
1277
|
+
const w = rest[i] as string
|
|
1278
|
+
if (/^-/.test(w)) {
|
|
1279
|
+
if (/^-[a-zA-Z]*[Cc]/.test(w)) return true
|
|
1280
|
+
if (/^-[dnOsu]$/.test(w)) i += 1
|
|
1281
|
+
continue
|
|
1282
|
+
}
|
|
1283
|
+
operands += 1
|
|
1284
|
+
}
|
|
1285
|
+
return operands - (mid ? 1 : 0) <= 0
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* Whether a value at the end of `prefix` would sit, in the statement made of `words`
|
|
1290
|
+
* (`mid`: the value joins its last word), where the shell reads a word again: the first
|
|
1291
|
+
* word is not a literal, it evaluates its arguments (`trap`, `eval`, `let`, `source`, `.`,
|
|
1292
|
+
* a shell run with `-c`, `printf -v`, `read` names, `[ -v`), it takes names (`declare`,
|
|
1293
|
+
* `unset`, `export` in the name position), or the value is assigned to a variable the
|
|
1294
|
+
* shell evaluates.
|
|
1295
|
+
*/
|
|
1296
|
+
function statementRereads(
|
|
1297
|
+
words: readonly string[],
|
|
1298
|
+
mid: boolean,
|
|
1299
|
+
prefix: string,
|
|
1300
|
+
suffix: string,
|
|
1301
|
+
): boolean {
|
|
1302
|
+
const last = mid ? (words[words.length - 1] as string) : undefined
|
|
1303
|
+
if (last !== undefined && EXPANDED_VARIABLES.test(last)) return true
|
|
1304
|
+
if (
|
|
1305
|
+
last !== undefined &&
|
|
1306
|
+
ASSIGNMENT_WORD.test(last) &&
|
|
1307
|
+
(EVALUATES_LATER.test(suffix) || /\b(?:declare|typeset|integer|local)\s+-\w*i/.test(prefix))
|
|
1308
|
+
)
|
|
1309
|
+
return true
|
|
1310
|
+
for (let i = 0; i < words.length; i++) {
|
|
1311
|
+
if (
|
|
1312
|
+
/^env$/.test(baseName(words[i] as string)) &&
|
|
1313
|
+
/^(-[a-zA-Z]*S|--split-string)/.test(words[i + 1] ?? '')
|
|
1314
|
+
)
|
|
1315
|
+
return true
|
|
1316
|
+
}
|
|
1317
|
+
const { at, wrapper } = commandIndex(words)
|
|
1318
|
+
// The value itself is the program to run (`env $ARGUMENTS`, `sudo $ARGUMENTS`,
|
|
1319
|
+
// `xargs $ARGUMENTS`, or a bare `$ARGUMENTS`): quoting it still lets it choose the program.
|
|
1320
|
+
if (mid ? at === words.length - 1 : at >= words.length && wrapper !== -1) return true
|
|
1321
|
+
const first = words[at]
|
|
1322
|
+
if (first === undefined) return false
|
|
1323
|
+
const rest = words.slice(at + 1)
|
|
1324
|
+
const name = baseName(first)
|
|
1325
|
+
// A first word that is not a literal cannot be shown not to be `eval`, `trap` or a shell.
|
|
1326
|
+
if (unknownWord(name)) return true
|
|
1327
|
+
if (wrapper !== -1 && wrappedRereads(words, wrapper, mid)) return true
|
|
1328
|
+
if (!first.includes('/') && REREAD_BUILTINS.has(name)) {
|
|
1329
|
+
if (name !== 'mapfile' && name !== 'readarray') return true
|
|
1330
|
+
if (mapfileTakesValue(rest, mid)) return true
|
|
1331
|
+
}
|
|
1332
|
+
if (REREAD_PROGRAMS.has(name)) return true
|
|
1333
|
+
if (name === 'printf' && /^-v/.test(rest[0] ?? '')) {
|
|
1334
|
+
// Only the word after `-v` is a name; later words are printed.
|
|
1335
|
+
const attached = (rest[0] as string).length > 2
|
|
1336
|
+
if (mid && rest.length === 1) return true
|
|
1337
|
+
if (!attached && (mid ? rest.length === 2 : rest.length === 1)) return true
|
|
1338
|
+
}
|
|
1339
|
+
if ((name === 'compgen' || name === 'complete') && rest.some((w) => /^-[a-zA-Z]*[CFW]/.test(w)))
|
|
1340
|
+
return true
|
|
1341
|
+
if (name === 'read') {
|
|
1342
|
+
// Every operand of `read` is a name, which the shell reads with its subscript; the
|
|
1343
|
+
// word after a redirection is a file.
|
|
1344
|
+
const redirectTarget =
|
|
1345
|
+
last !== undefined
|
|
1346
|
+
? REDIRECTION_WORD.test(last)
|
|
1347
|
+
: REDIRECTION_OPERATOR.test(words[words.length - 1] ?? '')
|
|
1348
|
+
if (!redirectTarget && !last?.startsWith('-')) return true
|
|
1349
|
+
}
|
|
1350
|
+
if ((name === '[' || name === 'test') && rest.some((w) => w === '-v' || w === '-R')) return true
|
|
1351
|
+
if (NAME_BUILTINS.has(name)) {
|
|
1352
|
+
if (rest.some((w) => /^-[a-zA-Z]*[inI]/.test(w))) return true
|
|
1353
|
+
// Where a value follows `=` it is a value; anywhere else it is a name.
|
|
1354
|
+
if (last === undefined || !last.includes('=')) return true
|
|
1355
|
+
}
|
|
1356
|
+
// A shell, or a program that runs a string like one, handed the value as its command string.
|
|
1357
|
+
if (SHELL_NAMES.has(name) || STRING_RUNNERS.has(name)) {
|
|
1358
|
+
if (runsValueAsScript(name, rest, mid)) return true
|
|
1359
|
+
}
|
|
1360
|
+
return false
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
/** Whether a statement, wherever it is, reads a variable it is given as shell text, a subscript or a name. */
|
|
1364
|
+
function statementIsSink(words: readonly string[]): boolean {
|
|
1365
|
+
if (words.some((w) => SUBSCRIPT_ASSIGNMENT.test(w))) return true
|
|
1366
|
+
if (words[0] === '((') return true
|
|
1367
|
+
const { at, wrapper } = commandIndex(words)
|
|
1368
|
+
const first = words[at]
|
|
1369
|
+
if (first === undefined) return false
|
|
1370
|
+
const rest = words.slice(at + 1)
|
|
1371
|
+
const name = baseName(first)
|
|
1372
|
+
const computed = (w: string | undefined): boolean => w !== undefined && COMPUTED_WORD.test(w)
|
|
1373
|
+
if (wrapper !== -1) {
|
|
1374
|
+
for (let k = wrapper + 1; k < words.length; k++) {
|
|
1375
|
+
const n = baseName(words[k] as string)
|
|
1376
|
+
if (SHELL_NAMES.has(n) || STRING_RUNNERS.has(n) || REREAD_AFTER_WRAPPER.has(n)) return true
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
if (!first.includes('/') && (name === 'source' || name === '.')) {
|
|
1380
|
+
return rest.some((w) => computed(w) || /^\/dev\//.test(w) || w === '-')
|
|
1381
|
+
}
|
|
1382
|
+
if (!first.includes('/') && (name === 'eval' || name === 'trap' || name === 'let')) {
|
|
1383
|
+
return rest.some((w) => computed(w))
|
|
1384
|
+
}
|
|
1385
|
+
if (SHELL_NAMES.has(name) || STRING_RUNNERS.has(name)) {
|
|
1386
|
+
let sawCommand = false
|
|
1387
|
+
let sawStdin = false
|
|
1388
|
+
let i = 0
|
|
1389
|
+
for (; i < rest.length; i++) {
|
|
1390
|
+
const w = rest[i] as string
|
|
1391
|
+
if (w === '--') {
|
|
1392
|
+
i += 1
|
|
1393
|
+
break
|
|
1394
|
+
}
|
|
1395
|
+
if (!/^[-+]./.test(w)) break
|
|
1396
|
+
if (COMMAND_STRING_FLAG.test(w)) sawCommand = true
|
|
1397
|
+
if (/^-[a-zA-Z]*s/.test(w)) sawStdin = true
|
|
1398
|
+
if (/^(?:[-+][a-zA-Z]*[oO]|--rcfile|--init-file)$/.test(w)) i += 1
|
|
1399
|
+
}
|
|
1400
|
+
const operand = rest[i]
|
|
1401
|
+
if (sawCommand) return operand === undefined || computed(operand)
|
|
1402
|
+
return sawStdin || operand === undefined || operand === '-' || /^\/dev\//.test(operand)
|
|
1403
|
+
}
|
|
1404
|
+
if (name === 'printf' && /^-v/.test(rest[0] ?? '')) return computed(rest[0]) || computed(rest[1])
|
|
1405
|
+
if (
|
|
1406
|
+
NAME_BUILTINS.has(name) ||
|
|
1407
|
+
name === 'unset' ||
|
|
1408
|
+
name === 'getopts' ||
|
|
1409
|
+
name === 'read' ||
|
|
1410
|
+
name === 'mapfile' ||
|
|
1411
|
+
name === 'readarray' ||
|
|
1412
|
+
name === 'compgen' ||
|
|
1413
|
+
name === 'complete' ||
|
|
1414
|
+
name === 'alias'
|
|
1415
|
+
) {
|
|
1416
|
+
// A name reference or integer reads the whole value as a name or arithmetic.
|
|
1417
|
+
if (rest.some((w) => /^-[a-zA-Z]*[inI]/.test(w)) && rest.some((w) => computed(w))) return true
|
|
1418
|
+
return rest.some((w) => computed((w.split('=')[0] as string) || undefined))
|
|
1419
|
+
}
|
|
1420
|
+
if ((name === '[' || name === 'test') && rest.some((w) => w === '-v' || w === '-R'))
|
|
1421
|
+
return rest.some((w) => computed(w))
|
|
1422
|
+
return false
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
/** Whether a statement runs something from a file or a shell: where a value written to a file would be read as commands. */
|
|
1426
|
+
function statementRunsFile(words: readonly string[]): boolean {
|
|
1427
|
+
const { at } = commandIndex(words)
|
|
1428
|
+
const first = words[at]
|
|
1429
|
+
if (first === undefined) return false
|
|
1430
|
+
const name = baseName(first)
|
|
1431
|
+
return (
|
|
1432
|
+
SHELL_NAMES.has(name) ||
|
|
1433
|
+
STRING_RUNNERS.has(name) ||
|
|
1434
|
+
name === 'source' ||
|
|
1435
|
+
name === '.' ||
|
|
1436
|
+
name === 'chmod' ||
|
|
1437
|
+
name === 'exec'
|
|
1438
|
+
)
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
/** How a value goes on from where it sits: into a variable, a pipe, an input word or a file. */
|
|
1442
|
+
interface Flow {
|
|
1443
|
+
data: boolean
|
|
1444
|
+
file: boolean
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
function flowOf(
|
|
1448
|
+
frames: ReadonlyArray<{ words: readonly string[]; mid: boolean }>,
|
|
1449
|
+
suffix: string,
|
|
1450
|
+
): Flow {
|
|
1451
|
+
const flow: Flow = { data: false, file: false }
|
|
1452
|
+
for (const { words, mid } of frames) {
|
|
1453
|
+
const last = mid ? words[words.length - 1] : undefined
|
|
1454
|
+
if (last !== undefined && ASSIGNMENT_WORD.test(last)) flow.data = true
|
|
1455
|
+
if (words.some((w) => /^[A-Za-z_]\w*\+?=\(/.test(w))) flow.data = true
|
|
1456
|
+
const finished = words[(mid ? words.length - 1 : words.length) - 1]
|
|
1457
|
+
if (finished !== undefined && /^<<<$/.test(finished)) flow.data = true
|
|
1458
|
+
if (last !== undefined && /^<<</.test(last)) flow.data = true
|
|
1459
|
+
const { at } = commandIndex(words)
|
|
1460
|
+
const name = baseName(words[at] ?? '')
|
|
1461
|
+
if (name === 'set' || name === 'select' || (name === 'for' && words.includes('in')))
|
|
1462
|
+
flow.data = true
|
|
1463
|
+
if (name === 'tee' || words.some((w) => /^\d*>>?(?!&)/.test(w) && !/^\d*>&/.test(w)))
|
|
1464
|
+
flow.file = true
|
|
1465
|
+
}
|
|
1466
|
+
const stage = suffix.split(/\n|;|&&|\|\||\)/)[0] ?? ''
|
|
1467
|
+
if (/\|/.test(stage)) flow.data = true
|
|
1468
|
+
if (/(?:^|[^\d&>])>>?\s*[^&\s]/.test(stage) || /\btee\b/.test(stage)) flow.file = true
|
|
1469
|
+
return flow
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
/**
|
|
1473
|
+
* Whether the rest of a command, where a value has gone into a variable or a stream,
|
|
1474
|
+
* reads a variable as shell text, a name, a subscript, an offset or arithmetic.
|
|
1475
|
+
*/
|
|
1476
|
+
function rereadsLater(whole: string, suffix: string, flow: Flow): boolean {
|
|
1477
|
+
const statements: string[][] = []
|
|
1478
|
+
statementWordsAt(whole, statements)
|
|
1479
|
+
if (flow.data) {
|
|
1480
|
+
if (EVALUATES_LATER.test(suffix) || PARAM_REREAD.test(whole)) return true
|
|
1481
|
+
if (statements.some(statementIsSink)) return true
|
|
1482
|
+
}
|
|
1483
|
+
if (flow.file && statements.some(statementRunsFile)) return true
|
|
1484
|
+
return false
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
/**
|
|
1488
|
+
* Whether a value at the end of `rawPrefix` (with `rawSuffix` the rest of the same command)
|
|
1489
|
+
* would sit where the shell reads a word again: a subscript that is still open
|
|
1490
|
+
* (`a[`, `a[ `, `arr=( [`, `$[`), an arithmetic command or a `[[` test with an arithmetic
|
|
1491
|
+
* operator, a command that evaluates its arguments (`trap`, `eval`, `let`, `source`, `.`,
|
|
1492
|
+
* a shell run with `-c` however many options come first, `printf -v`, `read` names, `[ -v`),
|
|
1493
|
+
* one that takes names (`declare`, `unset`, `export` in the name position), a first word
|
|
1494
|
+
* that is not a literal, a wrapper in front of any of these, a shell that reads the value
|
|
1495
|
+
* from a pipe, an alias, or a variable the value was put into that is evaluated later.
|
|
1496
|
+
* Quoting cannot make a value data there, so the load is refused.
|
|
1497
|
+
*
|
|
1498
|
+
* Both texts are the command as {@link canonicalize} read it.
|
|
1499
|
+
*/
|
|
1500
|
+
function rereadsAt(prefix: string, suffix: string): boolean {
|
|
1501
|
+
const regions = regionsAt(prefix)
|
|
1502
|
+
if (regions.subscript || regions.arithmetic) return true
|
|
1503
|
+
if (regions.doubleBracketAt !== -1) {
|
|
1504
|
+
const close = suffix.search(/\s\]\]/)
|
|
1505
|
+
const region =
|
|
1506
|
+
prefix.slice(regions.doubleBracketAt) + (close === -1 ? suffix : suffix.slice(0, close))
|
|
1507
|
+
if (ARITHMETIC_TEST.test(region)) return true
|
|
1508
|
+
}
|
|
1509
|
+
const whole = `${prefix}x${suffix}`
|
|
1510
|
+
// An alias is expanded as a command word, so any word may be a re-reading builtin.
|
|
1511
|
+
if (/expand_aliases/.test(whole)) return true
|
|
1512
|
+
if (feedsShell(suffix)) return true
|
|
1513
|
+
const state = statementWordsAt(prefix)
|
|
1514
|
+
if (statementRereads(state.words, state.mid, prefix, suffix)) return true
|
|
1515
|
+
for (const ancestor of state.ancestors) {
|
|
1516
|
+
const { at } = commandIndex(ancestor.words)
|
|
1517
|
+
const name = baseName(ancestor.words[at] ?? '')
|
|
1518
|
+
if (ancestor.psub && ancestor.afterRedirect) {
|
|
1519
|
+
// `sh < <( ... )` reads the output as commands; any other program reads it as data.
|
|
1520
|
+
if (SHELL_NAMES.has(name) || unknownWord(name)) return true
|
|
1521
|
+
continue
|
|
1522
|
+
}
|
|
1523
|
+
if (ancestor.psub && SHELL_NAMES.has(name)) return true
|
|
1524
|
+
if (statementRereads(ancestor.words, true, prefix, suffix)) return true
|
|
1525
|
+
}
|
|
1526
|
+
const flow = flowOf(
|
|
1527
|
+
[
|
|
1528
|
+
{ words: state.words, mid: state.mid },
|
|
1529
|
+
...state.ancestors.map((a) => ({ words: a.words, mid: true })),
|
|
1530
|
+
],
|
|
1531
|
+
suffix,
|
|
1532
|
+
)
|
|
1533
|
+
return (flow.data || flow.file) && rereadsLater(whole, suffix, flow)
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* Whether the command text defines a shell function (`name() { ... }`, `name ( )`,
|
|
1538
|
+
* `function name { ... }`). A function can hand its argument to `trap`, `declare -i`,
|
|
1539
|
+
* arithmetic or `eval` where this reader cannot follow it, so a value in any command that
|
|
1540
|
+
* defines one is refused. Text that merely looks like a definition inside quotes is
|
|
1541
|
+
* refused too: the quotes are not read, so a definition can never be hidden behind them.
|
|
1542
|
+
*/
|
|
1543
|
+
function definesFunction(whole: string): boolean {
|
|
1544
|
+
return (
|
|
1545
|
+
/(?:^|[;&|(){}\n]|\b(?:then|do|else)\b)\s*function\s+[^\s;&|(){}<>]/.test(whole) ||
|
|
1546
|
+
/[^\s;&|(){}<>$=`]\s*\(\s*\)/.test(whole)
|
|
1547
|
+
)
|
|
1548
|
+
}
|
|
1549
|
+
|
|
360
1550
|
/** One word as the shell reads it literally. */
|
|
361
1551
|
function shellQuoteWord(word: string): string {
|
|
362
1552
|
return `'${word.replaceAll("'", "'\\''")}'`
|
|
@@ -427,32 +1617,75 @@ function typedWords(value: string): string[] {
|
|
|
427
1617
|
* end the command where it is read), and neither can one where no quoting
|
|
428
1618
|
* holds (a here-document, `${...}`): the load is refused.
|
|
429
1619
|
*/
|
|
430
|
-
function quoteForCommand(
|
|
1620
|
+
function quoteForCommand(
|
|
1621
|
+
value: string,
|
|
1622
|
+
prefix: string,
|
|
1623
|
+
dialect: ShellDialect,
|
|
1624
|
+
whole = false,
|
|
1625
|
+
suffix = '',
|
|
1626
|
+
): string {
|
|
431
1627
|
if (dialect === 'cmd') {
|
|
432
1628
|
// No quoting here is known to hold in the Windows command interpreter, so only words
|
|
433
1629
|
// made of characters it reads literally go in.
|
|
434
|
-
const words = typedWords(value)
|
|
1630
|
+
const words = whole ? [value] : typedWords(value)
|
|
435
1631
|
if (words.every((word) => /^[\w.\\/:=+,@-]+$/.test(word))) return words.join(' ')
|
|
436
1632
|
throw new InlineShellError(
|
|
437
1633
|
'A value given to this skill or command has characters that cannot be put safely inside a command for the Windows command interpreter, so it was not loaded.',
|
|
438
1634
|
'injection',
|
|
439
1635
|
)
|
|
440
1636
|
}
|
|
441
|
-
|
|
1637
|
+
// Digits are one inert word wherever they stand, arithmetic and subscripts included:
|
|
1638
|
+
// nothing in them is shell syntax. Only a `$` or a backslash right before would join them.
|
|
1639
|
+
if (/^[0-9]+$/.test(value) && !/[$\\]$/.test(prefix)) return value
|
|
1640
|
+
const command = canonicalize(prefix, suffix)
|
|
1641
|
+
if (command.refuse) {
|
|
1642
|
+
throw new InlineShellError(
|
|
1643
|
+
'A value given to this skill or command would land in a shell command whose quotes, here-documents or groups cannot be read with certainty, so it was not loaded.',
|
|
1644
|
+
'injection',
|
|
1645
|
+
)
|
|
1646
|
+
}
|
|
1647
|
+
const context = shellContextAt(command.prefix)
|
|
442
1648
|
if (context === 'unsafe' || value.includes('`')) {
|
|
443
1649
|
throw new InlineShellError(
|
|
444
1650
|
context === 'unsafe'
|
|
445
|
-
? 'A value given to this skill or command would land inside a here-document
|
|
1651
|
+
? 'A value given to this skill or command would land inside a here-document, a ${...} or arithmetic expansion, or right after a bare $ of a shell command, where it cannot be quoted, so it was not loaded.'
|
|
446
1652
|
: 'A value given to this skill or command contains a backtick, which cannot be put inside a shell command, so it was not loaded.',
|
|
447
1653
|
'injection',
|
|
448
1654
|
)
|
|
449
1655
|
}
|
|
1656
|
+
if (definesFunction(`${command.prefix}x${command.suffix}`)) {
|
|
1657
|
+
throw new InlineShellError(
|
|
1658
|
+
'A value given to this skill or command would land in a shell command that defines a shell function, which could hand the value to the shell to read again as code, so it was not loaded. Move the value out of the command that defines the function, or write the template without a function.',
|
|
1659
|
+
'injection',
|
|
1660
|
+
)
|
|
1661
|
+
}
|
|
1662
|
+
if (rereadsAt(command.prefix, command.suffix)) {
|
|
1663
|
+
throw new InlineShellError(
|
|
1664
|
+
'A value given to this skill or command would land where the shell reads it again as a command, a subscript or arithmetic (trap, eval, a subscript, a shell run with -c and the like), where it cannot be made safe, so it was not loaded.',
|
|
1665
|
+
'injection',
|
|
1666
|
+
)
|
|
1667
|
+
}
|
|
450
1668
|
if (context === 'single') return value.replaceAll("'", "'\\''")
|
|
451
1669
|
if (context === 'double') return value.replace(/[\\"$]/g, '\\$&')
|
|
452
|
-
|
|
1670
|
+
// A value that is already one word (a positional argument) stays one argument, even
|
|
1671
|
+
// when it holds spaces; the whole text a person typed is read back into its words.
|
|
1672
|
+
return whole ? shellQuoteWord(value) : typedWords(value).map(shellQuoteWord).join(' ')
|
|
453
1673
|
}
|
|
454
1674
|
|
|
1675
|
+
/**
|
|
1676
|
+
* What a {@link PlaceholderReader} found at a position of the template: a token whose
|
|
1677
|
+
* value goes in (`whole` when the value is one word), or text that stands for itself
|
|
1678
|
+
* and replaces `length` characters of the template.
|
|
1679
|
+
*/
|
|
1680
|
+
export type PlaceholderRead =
|
|
1681
|
+
| { readonly kind: 'value'; readonly token: string; readonly whole?: boolean }
|
|
1682
|
+
| { readonly kind: 'literal'; readonly text: string; readonly length: number }
|
|
1683
|
+
|
|
1684
|
+
/** Reads the placeholder that starts at `at`, or answers `undefined` when none does. */
|
|
1685
|
+
export type PlaceholderReader = (text: string, at: number) => PlaceholderRead | undefined
|
|
1686
|
+
|
|
455
1687
|
interface Occurrence {
|
|
1688
|
+
readonly whole: boolean
|
|
456
1689
|
readonly token: string
|
|
457
1690
|
/** Where the value goes in the neutral text. */
|
|
458
1691
|
readonly at: number
|
|
@@ -470,34 +1703,48 @@ interface Occurrence {
|
|
|
470
1703
|
* reads it as data ({@link quoteForCommand}), by where it sits: a person's
|
|
471
1704
|
* `-n 5` stays two arguments, and `x; touch f #` stays words of an argument
|
|
472
1705
|
* list. Throws {@link InlineShellError} when a value cannot be made safe, or the
|
|
473
|
-
* substituted text
|
|
1706
|
+
* substituted text does not hold exactly the template's commands, in order.
|
|
474
1707
|
*/
|
|
475
1708
|
export function substituteEscaped(
|
|
476
1709
|
template: string,
|
|
477
1710
|
values: Readonly<Record<string, string>>,
|
|
478
|
-
options: { readonly dialect?: ShellDialect } = {},
|
|
1711
|
+
options: { readonly dialect?: ShellDialect; readonly reader?: PlaceholderReader } = {},
|
|
479
1712
|
): string {
|
|
480
1713
|
const normalized = normalizeInlineShellText(template)
|
|
481
1714
|
const tokens = Object.keys(values).filter((token) => token.length > 0)
|
|
482
|
-
if (tokens.length === 0) return normalized
|
|
1715
|
+
if (tokens.length === 0 && options.reader === undefined) return normalized
|
|
483
1716
|
// Longest first, so `$ARGUMENTS_X` is never read as `$ARGUMENTS` plus a tail.
|
|
484
1717
|
tokens.sort((a, b) => b.length - a.length)
|
|
1718
|
+
const read: PlaceholderReader =
|
|
1719
|
+
options.reader ??
|
|
1720
|
+
((text, at) => {
|
|
1721
|
+
const token = tokens.find((candidate) => text.startsWith(candidate, at))
|
|
1722
|
+
return token === undefined ? undefined : { kind: 'value', token }
|
|
1723
|
+
})
|
|
485
1724
|
// The template with a word where each value goes: the commands the author wrote.
|
|
486
1725
|
let neutral = ''
|
|
487
1726
|
const pieces: Array<string | Occurrence> = []
|
|
488
1727
|
let text = ''
|
|
489
1728
|
let i = 0
|
|
490
1729
|
while (i < normalized.length) {
|
|
491
|
-
const
|
|
492
|
-
if (
|
|
1730
|
+
const found = read(normalized, i)
|
|
1731
|
+
if (found === undefined) {
|
|
493
1732
|
text += normalized[i]
|
|
494
1733
|
neutral += normalized[i]
|
|
495
1734
|
i += 1
|
|
496
1735
|
continue
|
|
497
1736
|
}
|
|
1737
|
+
if (found.kind === 'literal') {
|
|
1738
|
+
text += found.text
|
|
1739
|
+
neutral += found.text
|
|
1740
|
+
i += found.length
|
|
1741
|
+
continue
|
|
1742
|
+
}
|
|
1743
|
+
const token = found.token
|
|
498
1744
|
if (text !== '') pieces.push(text)
|
|
499
1745
|
text = ''
|
|
500
1746
|
pieces.push({
|
|
1747
|
+
whole: found.whole === true,
|
|
501
1748
|
token,
|
|
502
1749
|
at: neutral.length,
|
|
503
1750
|
before: normalized[i - 1] ?? '',
|
|
@@ -508,29 +1755,58 @@ export function substituteEscaped(
|
|
|
508
1755
|
}
|
|
509
1756
|
if (text !== '') pieces.push(text)
|
|
510
1757
|
const markers = findInlineShell(neutral)
|
|
511
|
-
const commandAt = (at: number): number | undefined => {
|
|
1758
|
+
const commandAt = (at: number): { from: number; end: number } | undefined => {
|
|
512
1759
|
const marker = markers.find((m) => m.start <= at && at < m.end)
|
|
513
1760
|
if (marker === undefined) return undefined
|
|
514
|
-
return marker.kind === 'inline'
|
|
1761
|
+
return marker.kind === 'inline'
|
|
1762
|
+
? { from: marker.start + 2, end: marker.end - 1 }
|
|
1763
|
+
: {
|
|
1764
|
+
from: neutral.indexOf('\n', marker.start) + 1,
|
|
1765
|
+
// Up to the line that closes the fence, which is not part of the command.
|
|
1766
|
+
end: marker.start + marker.raw.lastIndexOf('\n'),
|
|
1767
|
+
}
|
|
515
1768
|
}
|
|
516
1769
|
let out = ''
|
|
1770
|
+
// Where each value landed: its place in the neutral text (one character wide) and in the output.
|
|
1771
|
+
const landed: Array<{ at: number; outAt: number; length: number }> = []
|
|
517
1772
|
for (const piece of pieces) {
|
|
518
1773
|
if (typeof piece === 'string') {
|
|
519
1774
|
out += piece
|
|
520
1775
|
continue
|
|
521
1776
|
}
|
|
522
1777
|
const value = values[piece.token] as string
|
|
523
|
-
const
|
|
524
|
-
|
|
525
|
-
|
|
1778
|
+
const span = commandAt(piece.at)
|
|
1779
|
+
const from = span?.from
|
|
1780
|
+
const put =
|
|
1781
|
+
from === undefined || span === undefined
|
|
526
1782
|
? escapeBetween(value, piece.before, piece.after)
|
|
527
1783
|
: quoteForCommand(
|
|
528
1784
|
value,
|
|
529
1785
|
neutral.slice(from, piece.at),
|
|
530
1786
|
options.dialect ?? hostCommandShell().dialect,
|
|
1787
|
+
piece.whole,
|
|
1788
|
+
neutral.slice(piece.at + 1, span.end),
|
|
531
1789
|
)
|
|
1790
|
+
landed.push({ at: piece.at, outAt: out.length, length: put.length })
|
|
1791
|
+
out += put
|
|
1792
|
+
}
|
|
1793
|
+
// A neutral offset (a marker's edge) in the output text.
|
|
1794
|
+
const inOutput = (at: number): number => {
|
|
1795
|
+
let shift = 0
|
|
1796
|
+
for (const place of landed) {
|
|
1797
|
+
if (place.at >= at) break
|
|
1798
|
+
shift += place.length - 1
|
|
1799
|
+
}
|
|
1800
|
+
return at + shift
|
|
532
1801
|
}
|
|
533
|
-
|
|
1802
|
+
// The commands the author wrote, values quoted in, are what must be there: the same
|
|
1803
|
+
// ones, in the same order. A count would let a value that masks one command and
|
|
1804
|
+
// creates another through.
|
|
1805
|
+
const expected = markers.map(
|
|
1806
|
+
(m) => `${m.kind}\u0000${out.slice(inOutput(m.start), inOutput(m.end))}`,
|
|
1807
|
+
)
|
|
1808
|
+
const found = findInlineShell(out).map((m) => `${m.kind}\u0000${m.raw}`)
|
|
1809
|
+
if (expected.length !== found.length || expected.some((raw, index) => raw !== found[index])) {
|
|
534
1810
|
throw new InlineShellError(
|
|
535
1811
|
'A value given to this skill or command contains text that would start a shell command, so it was not loaded.',
|
|
536
1812
|
'injection',
|
|
@@ -571,6 +1847,12 @@ export type InlineShellHookVerdict =
|
|
|
571
1847
|
export type InlineShellOutcome =
|
|
572
1848
|
/** The permission rules said no, or would have asked a person who is not there. */
|
|
573
1849
|
| { readonly kind: 'denied'; readonly reason: string }
|
|
1850
|
+
/**
|
|
1851
|
+
* Not run here: the session's auto-mode reviewer judges only the commands the model
|
|
1852
|
+
* runs itself, so the text asks the model to run this one in its own `bash` call and
|
|
1853
|
+
* the load goes on. Only a host with a model to hand it to answers this.
|
|
1854
|
+
*/
|
|
1855
|
+
| { readonly kind: 'handoff' }
|
|
574
1856
|
/** The hook, which runs after permission and before the shell, refused or stopped. */
|
|
575
1857
|
| { readonly kind: 'hook'; readonly verdict: Exclude<InlineShellHookVerdict, { kind: 'run' }> }
|
|
576
1858
|
| {
|
|
@@ -672,7 +1954,10 @@ export async function expandInlineShell(
|
|
|
672
1954
|
}
|
|
673
1955
|
}
|
|
674
1956
|
|
|
675
|
-
|
|
1957
|
+
// A string is the text for that spot; a number is the place of a hand-off, whose note
|
|
1958
|
+
// is final once the count is known. Never a marker inside the text, which could collide.
|
|
1959
|
+
const replacements: (string | number)[] = []
|
|
1960
|
+
const handedOff: string[] = []
|
|
676
1961
|
let total = 0
|
|
677
1962
|
for (const marker of markers) {
|
|
678
1963
|
options.signal?.throwIfAborted()
|
|
@@ -688,6 +1973,11 @@ export async function expandInlineShell(
|
|
|
688
1973
|
'permission',
|
|
689
1974
|
)
|
|
690
1975
|
}
|
|
1976
|
+
if (outcome.kind === 'handoff') {
|
|
1977
|
+
handedOff.push(marker.command)
|
|
1978
|
+
replacements.push(handedOff.length - 1)
|
|
1979
|
+
continue
|
|
1980
|
+
}
|
|
691
1981
|
if (outcome.kind === 'hook') {
|
|
692
1982
|
const verdict = outcome.verdict
|
|
693
1983
|
if (verdict.kind === 'stop') {
|
|
@@ -731,7 +2021,53 @@ export async function expandInlineShell(
|
|
|
731
2021
|
replacements.push(shaped)
|
|
732
2022
|
}
|
|
733
2023
|
let n = 0
|
|
734
|
-
|
|
2024
|
+
if (handedOff.length === 0) {
|
|
2025
|
+
return replaceMarkers(source, markers, () => replacements[n++] as string)
|
|
2026
|
+
}
|
|
2027
|
+
const { header, spot } = handOffNotes(handedOff)
|
|
2028
|
+
const body = replaceMarkers(source, markers, () => {
|
|
2029
|
+
const r = replacements[n++] as string | number
|
|
2030
|
+
return typeof r === 'number' ? spot(r) : r
|
|
2031
|
+
})
|
|
2032
|
+
return header === '' ? body : `${header}\n\n${body}`
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
/** A command as it is written in a hand-off note: in backticks, or fenced when it holds a backtick or a newline. */
|
|
2036
|
+
function fenced(command: string): string {
|
|
2037
|
+
const longest = Math.max(0, ...(command.match(/`+/g) ?? []).map((run) => run.length))
|
|
2038
|
+
const fence = '`'.repeat(Math.max(3, longest + 1))
|
|
2039
|
+
return `${fence}\n${command}\n${fence}`
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
/**
|
|
2043
|
+
* The hand-off notes for the commands a model is asked to run itself. One command: its
|
|
2044
|
+
* note stands where the command was. Several: a short numbered list goes first (the
|
|
2045
|
+
* header), and each spot names its number.
|
|
2046
|
+
*/
|
|
2047
|
+
function handOffNotes(commands: readonly string[]): {
|
|
2048
|
+
header: string
|
|
2049
|
+
spot: (index: number) => string
|
|
2050
|
+
} {
|
|
2051
|
+
const plain = (command: string) => !/[`\n]/.test(command)
|
|
2052
|
+
if (commands.length === 1) {
|
|
2053
|
+
const command = commands[0] as string
|
|
2054
|
+
const lead = '[run this first, exactly as written, with the bash tool and use its output:'
|
|
2055
|
+
const note = plain(command) ? `${lead} \`${command}\`]` : `${lead}]\n${fenced(command)}`
|
|
2056
|
+
return { header: '', spot: () => note }
|
|
2057
|
+
}
|
|
2058
|
+
const head = `[Run these ${commands.length} commands first, exactly as written, with the bash tool, and use their output where each is named below. Run them one per call.]`
|
|
2059
|
+
const list = commands.map((command, i) =>
|
|
2060
|
+
plain(command) ? `${i + 1}. \`${command}\`` : `${i + 1}.\n${fenced(command)}`,
|
|
2061
|
+
)
|
|
2062
|
+
return {
|
|
2063
|
+
header: [head, ...list].join('\n'),
|
|
2064
|
+
spot: (i) => {
|
|
2065
|
+
const command = commands[i] as string
|
|
2066
|
+
return plain(command)
|
|
2067
|
+
? `[output of command ${i + 1}, \`${command}\`]`
|
|
2068
|
+
: `[output of command ${i + 1}]`
|
|
2069
|
+
},
|
|
2070
|
+
}
|
|
735
2071
|
}
|
|
736
2072
|
|
|
737
2073
|
function replaceMarkers(
|