Rename resolved→done (#1-4, #11), closed→done (#13, #18), open (post-core)→open (#7), mark #9 as superseded by #21.
43 lines
1.3 KiB
Markdown
43 lines
1.3 KiB
Markdown
# 03 — Implement direct command execution runtime (no shell)
|
|
|
|
**Status:** done
|
|
**Blocked by:** #02
|
|
**Blocks:** #06, #07
|
|
|
|
Implement the C runtime that executes commands when a backtick expression is evaluated.
|
|
|
|
**Critical constraint:** do NOT spawn a shell (no `bash`, `dash`, `sh`, `system()`, `popen()`).
|
|
|
|
## Return value
|
|
|
|
Backtick expressions return a table:
|
|
|
|
```lua
|
|
local r = `ls -lha`
|
|
r.code -- exit code (integer)
|
|
r.stdout -- captured stdout (string)
|
|
r.stderr -- captured stderr (string)
|
|
```
|
|
|
|
## Implementation approach
|
|
|
|
- `fork()` + `execvp()` on Unix/macOS
|
|
- Set up two pipes: one for stdout, one for stderr
|
|
- In child: `dup2()` pipes onto fd 1 and fd 2, then `execvp(argv[0], argv)`
|
|
- In parent: read both pipes into buffers, `waitpid()` for exit status
|
|
- Build a Lua table with `code`, `stdout`, `stderr` fields and push onto stack
|
|
- Depends on #04 (argv parsing) to tokenize the command string into argv[]
|
|
|
|
## Error handling
|
|
|
|
- Command not found → set `code` to an error value, `stderr` to error message
|
|
- Permission denied → same
|
|
- `fork()` failure → raise a Lua error
|
|
|
|
## Open questions
|
|
|
|
- New file (`lcmd.c` / `lcmd.h`) or add to an existing lib?
|
|
- Signal handling during child execution
|
|
- Binary output — return raw string bytes as-is?
|
|
- Set `__tostring` metamethod on result table?
|