py-posix-shell 0.1.0__tar.gz
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.
- py_posix_shell-0.1.0/.gitignore +12 -0
- py_posix_shell-0.1.0/LICENSE +21 -0
- py_posix_shell-0.1.0/PKG-INFO +93 -0
- py_posix_shell-0.1.0/README.md +69 -0
- py_posix_shell-0.1.0/pyproject.toml +43 -0
- py_posix_shell-0.1.0/src/py_posix_shell/__init__.py +4 -0
- py_posix_shell-0.1.0/src/py_posix_shell/__main__.py +6 -0
- py_posix_shell-0.1.0/src/py_posix_shell/builtins.py +418 -0
- py_posix_shell-0.1.0/src/py_posix_shell/cli.py +60 -0
- py_posix_shell-0.1.0/src/py_posix_shell/errors.py +26 -0
- py_posix_shell-0.1.0/src/py_posix_shell/expansion.py +346 -0
- py_posix_shell-0.1.0/src/py_posix_shell/lexer.py +293 -0
- py_posix_shell-0.1.0/src/py_posix_shell/parser.py +184 -0
- py_posix_shell-0.1.0/src/py_posix_shell/shell.py +467 -0
- py_posix_shell-0.1.0/tests/test_shell.py +82 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 GGN_2015
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-posix-shell
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small cross-platform POSIX-style shell written in Python.
|
|
5
|
+
Author: GGN_2015
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: cross-platform,linux,macos,posix,shell,windows
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: MacOS
|
|
14
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Topic :: System :: Shells
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# py-posix-shell
|
|
26
|
+
|
|
27
|
+
`py-posix-shell` is a small POSIX-style shell implemented in pure Python. It is
|
|
28
|
+
intended to run on Windows, Linux, and macOS with the same Python package and the
|
|
29
|
+
same console entry point.
|
|
30
|
+
|
|
31
|
+
The project focuses on a practical, compact subset of POSIX shell behavior:
|
|
32
|
+
|
|
33
|
+
- command lists with `;`, `&&`, `||`, and background `&`
|
|
34
|
+
- pipelines with `|`
|
|
35
|
+
- quoting with single quotes, double quotes, and backslash escapes
|
|
36
|
+
- parameter expansion such as `$HOME`, `$?`, `${name:-word}`, and `${name:=word}`
|
|
37
|
+
- command substitution with `$(...)` and backticks
|
|
38
|
+
- field splitting, pathname expansion, and tilde expansion
|
|
39
|
+
- redirection with `<`, `>`, `>>`, `2>`, `2>>`, `>&`, and `<&`
|
|
40
|
+
- shell variables, exported environment variables, and positional parameters
|
|
41
|
+
- common builtins including `cd`, `pwd`, `exit`, `export`, `unset`, `set`,
|
|
42
|
+
`shift`, `echo`, `printf`, `read`, `type`, `command`, `env`, `test`, and `[`
|
|
43
|
+
|
|
44
|
+
It deliberately does not try to be a full replacement for `dash`, `bash`, or
|
|
45
|
+
`zsh`. Complex POSIX features such as functions, aliases, job control, traps,
|
|
46
|
+
arithmetic expansion, case/esac, for/while/until loops, and here-documents are
|
|
47
|
+
not implemented yet.
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
From this repository:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install .
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
For editable development:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install -e ".[dev]"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
Start an interactive shell:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pysh
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Run one command:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
pysh -c "name=world; echo \"hello $name\""
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Run a script:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pysh ./script.sh arg1 arg2
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The package also installs the longer `py-posix-shell` command.
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
Run the tests with:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
pytest
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The implementation uses only the Python standard library at runtime.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# py-posix-shell
|
|
2
|
+
|
|
3
|
+
`py-posix-shell` is a small POSIX-style shell implemented in pure Python. It is
|
|
4
|
+
intended to run on Windows, Linux, and macOS with the same Python package and the
|
|
5
|
+
same console entry point.
|
|
6
|
+
|
|
7
|
+
The project focuses on a practical, compact subset of POSIX shell behavior:
|
|
8
|
+
|
|
9
|
+
- command lists with `;`, `&&`, `||`, and background `&`
|
|
10
|
+
- pipelines with `|`
|
|
11
|
+
- quoting with single quotes, double quotes, and backslash escapes
|
|
12
|
+
- parameter expansion such as `$HOME`, `$?`, `${name:-word}`, and `${name:=word}`
|
|
13
|
+
- command substitution with `$(...)` and backticks
|
|
14
|
+
- field splitting, pathname expansion, and tilde expansion
|
|
15
|
+
- redirection with `<`, `>`, `>>`, `2>`, `2>>`, `>&`, and `<&`
|
|
16
|
+
- shell variables, exported environment variables, and positional parameters
|
|
17
|
+
- common builtins including `cd`, `pwd`, `exit`, `export`, `unset`, `set`,
|
|
18
|
+
`shift`, `echo`, `printf`, `read`, `type`, `command`, `env`, `test`, and `[`
|
|
19
|
+
|
|
20
|
+
It deliberately does not try to be a full replacement for `dash`, `bash`, or
|
|
21
|
+
`zsh`. Complex POSIX features such as functions, aliases, job control, traps,
|
|
22
|
+
arithmetic expansion, case/esac, for/while/until loops, and here-documents are
|
|
23
|
+
not implemented yet.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
From this repository:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install .
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
For editable development:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install -e ".[dev]"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
Start an interactive shell:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pysh
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Run one command:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pysh -c "name=world; echo \"hello $name\""
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Run a script:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pysh ./script.sh arg1 arg2
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The package also installs the longer `py-posix-shell` command.
|
|
60
|
+
|
|
61
|
+
## Development
|
|
62
|
+
|
|
63
|
+
Run the tests with:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
pytest
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The implementation uses only the Python standard library at runtime.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "py-posix-shell"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A small cross-platform POSIX-style shell written in Python."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "GGN_2015" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["shell", "posix", "cross-platform", "windows", "macos", "linux"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Environment :: Console",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: MacOS",
|
|
22
|
+
"Operating System :: Microsoft :: Windows",
|
|
23
|
+
"Operating System :: POSIX :: Linux",
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
27
|
+
"Topic :: System :: Shells",
|
|
28
|
+
]
|
|
29
|
+
dependencies = []
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
dev = ["pytest>=8"]
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
pysh = "py_posix_shell.cli:main"
|
|
36
|
+
py-posix-shell = "py_posix_shell.cli:main"
|
|
37
|
+
|
|
38
|
+
[tool.hatch.build.targets.wheel]
|
|
39
|
+
packages = ["src/py_posix_shell"]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
testpaths = ["tests"]
|
|
43
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
"""Builtin utilities."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
from typing import Callable, TextIO
|
|
8
|
+
|
|
9
|
+
from .errors import ShellExit
|
|
10
|
+
from .lexer import is_name
|
|
11
|
+
|
|
12
|
+
Builtin = Callable[[object, list[str], TextIO, TextIO, TextIO], int]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
SPECIAL_BUILTINS = {
|
|
16
|
+
":",
|
|
17
|
+
".",
|
|
18
|
+
"break",
|
|
19
|
+
"continue",
|
|
20
|
+
"eval",
|
|
21
|
+
"exec",
|
|
22
|
+
"exit",
|
|
23
|
+
"export",
|
|
24
|
+
"readonly",
|
|
25
|
+
"return",
|
|
26
|
+
"set",
|
|
27
|
+
"shift",
|
|
28
|
+
"times",
|
|
29
|
+
"trap",
|
|
30
|
+
"unset",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def builtin_colon(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
35
|
+
return 0
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def builtin_true(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
39
|
+
return 0
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def builtin_false(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
43
|
+
return 1
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def builtin_echo(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
47
|
+
args = argv[1:]
|
|
48
|
+
newline = True
|
|
49
|
+
if args and args[0] == "-n":
|
|
50
|
+
newline = False
|
|
51
|
+
args = args[1:]
|
|
52
|
+
stdout.write(" ".join(args))
|
|
53
|
+
if newline:
|
|
54
|
+
stdout.write("\n")
|
|
55
|
+
return 0
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def builtin_pwd(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
59
|
+
stdout.write(os.getcwd() + "\n")
|
|
60
|
+
return 0
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def builtin_cd(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
64
|
+
if len(argv) > 2:
|
|
65
|
+
stderr.write("cd: too many arguments\n")
|
|
66
|
+
return 2
|
|
67
|
+
target = argv[1] if len(argv) == 2 else shell.get_parameter("HOME")
|
|
68
|
+
if not target:
|
|
69
|
+
stderr.write("cd: HOME not set\n")
|
|
70
|
+
return 1
|
|
71
|
+
print_new_path = False
|
|
72
|
+
if target == "-":
|
|
73
|
+
target = shell.get_parameter("OLDPWD")
|
|
74
|
+
if not target:
|
|
75
|
+
stderr.write("cd: OLDPWD not set\n")
|
|
76
|
+
return 1
|
|
77
|
+
print_new_path = True
|
|
78
|
+
oldpwd = os.getcwd()
|
|
79
|
+
try:
|
|
80
|
+
os.chdir(os.path.expanduser(target))
|
|
81
|
+
except OSError as exc:
|
|
82
|
+
stderr.write(f"cd: {target}: {exc.strerror or exc}\n")
|
|
83
|
+
return 1
|
|
84
|
+
newpwd = os.getcwd()
|
|
85
|
+
shell.set_parameter("OLDPWD", oldpwd, export=True)
|
|
86
|
+
shell.set_parameter("PWD", newpwd, export=True)
|
|
87
|
+
if print_new_path:
|
|
88
|
+
stdout.write(newpwd + "\n")
|
|
89
|
+
return 0
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def builtin_exit(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
93
|
+
if len(argv) > 2:
|
|
94
|
+
stderr.write("exit: too many arguments\n")
|
|
95
|
+
return 2
|
|
96
|
+
if len(argv) == 1:
|
|
97
|
+
raise ShellExit(shell.last_status)
|
|
98
|
+
try:
|
|
99
|
+
status = int(argv[1], 10) & 0xFF
|
|
100
|
+
except ValueError:
|
|
101
|
+
stderr.write(f"exit: {argv[1]}: numeric argument required\n")
|
|
102
|
+
raise ShellExit(2)
|
|
103
|
+
raise ShellExit(status)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def builtin_export(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
107
|
+
if len(argv) == 1 or argv[1:] == ["-p"]:
|
|
108
|
+
for name in sorted(shell.env):
|
|
109
|
+
stdout.write(f"export {name}={quote_for_display(shell.env[name])}\n")
|
|
110
|
+
return 0
|
|
111
|
+
|
|
112
|
+
status = 0
|
|
113
|
+
for arg in argv[1:]:
|
|
114
|
+
if arg == "-p":
|
|
115
|
+
continue
|
|
116
|
+
if "=" in arg:
|
|
117
|
+
name, value = arg.split("=", 1)
|
|
118
|
+
else:
|
|
119
|
+
name = arg
|
|
120
|
+
value = shell.get_parameter(name)
|
|
121
|
+
if not is_name(name):
|
|
122
|
+
stderr.write(f"export: {arg}: not a valid identifier\n")
|
|
123
|
+
status = 1
|
|
124
|
+
continue
|
|
125
|
+
shell.set_parameter(name, value, export=True)
|
|
126
|
+
return status
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def builtin_unset(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
130
|
+
status = 0
|
|
131
|
+
for name in argv[1:]:
|
|
132
|
+
if not is_name(name):
|
|
133
|
+
stderr.write(f"unset: {name}: not a valid identifier\n")
|
|
134
|
+
status = 1
|
|
135
|
+
continue
|
|
136
|
+
shell.unset_parameter(name)
|
|
137
|
+
return status
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def builtin_set(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
141
|
+
if len(argv) == 1:
|
|
142
|
+
merged = dict(shell.env)
|
|
143
|
+
merged.update(shell.vars)
|
|
144
|
+
for name in sorted(merged):
|
|
145
|
+
stdout.write(f"{name}={quote_for_display(merged[name])}\n")
|
|
146
|
+
return 0
|
|
147
|
+
if argv[1] == "--":
|
|
148
|
+
shell.positional = argv[2:]
|
|
149
|
+
return 0
|
|
150
|
+
stderr.write("set: only 'set' and 'set -- args...' are implemented\n")
|
|
151
|
+
return 2
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def builtin_shift(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
155
|
+
if len(argv) > 2:
|
|
156
|
+
stderr.write("shift: too many arguments\n")
|
|
157
|
+
return 2
|
|
158
|
+
try:
|
|
159
|
+
count = int(argv[1], 10) if len(argv) == 2 else 1
|
|
160
|
+
except ValueError:
|
|
161
|
+
stderr.write(f"shift: {argv[1]}: numeric argument required\n")
|
|
162
|
+
return 2
|
|
163
|
+
if count < 0 or count > len(shell.positional):
|
|
164
|
+
stderr.write("shift: shift count out of range\n")
|
|
165
|
+
return 1
|
|
166
|
+
del shell.positional[:count]
|
|
167
|
+
return 0
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def builtin_printf(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
171
|
+
if len(argv) == 1:
|
|
172
|
+
return 0
|
|
173
|
+
fmt = decode_escapes(argv[1])
|
|
174
|
+
args = argv[2:] or [""]
|
|
175
|
+
index = 0
|
|
176
|
+
while index < len(args):
|
|
177
|
+
wrote_conversion = False
|
|
178
|
+
i = 0
|
|
179
|
+
while i < len(fmt):
|
|
180
|
+
char = fmt[i]
|
|
181
|
+
if char != "%":
|
|
182
|
+
stdout.write(char)
|
|
183
|
+
i += 1
|
|
184
|
+
continue
|
|
185
|
+
if i + 1 < len(fmt) and fmt[i + 1] == "%":
|
|
186
|
+
stdout.write("%")
|
|
187
|
+
i += 2
|
|
188
|
+
continue
|
|
189
|
+
spec_start = i
|
|
190
|
+
i += 1
|
|
191
|
+
while i < len(fmt) and fmt[i] in "#0- +0123456789.":
|
|
192
|
+
i += 1
|
|
193
|
+
if i >= len(fmt):
|
|
194
|
+
stdout.write(fmt[spec_start:])
|
|
195
|
+
break
|
|
196
|
+
spec = fmt[i]
|
|
197
|
+
i += 1
|
|
198
|
+
arg = args[index] if index < len(args) else ""
|
|
199
|
+
index += 1
|
|
200
|
+
wrote_conversion = True
|
|
201
|
+
if spec == "s":
|
|
202
|
+
stdout.write(arg)
|
|
203
|
+
elif spec == "b":
|
|
204
|
+
stdout.write(decode_escapes(arg))
|
|
205
|
+
elif spec in "diu":
|
|
206
|
+
try:
|
|
207
|
+
stdout.write(str(int(arg, 0)))
|
|
208
|
+
except ValueError:
|
|
209
|
+
stdout.write("0")
|
|
210
|
+
elif spec in "xX":
|
|
211
|
+
try:
|
|
212
|
+
value = int(arg, 0)
|
|
213
|
+
except ValueError:
|
|
214
|
+
value = 0
|
|
215
|
+
stdout.write(format(value, spec))
|
|
216
|
+
else:
|
|
217
|
+
stdout.write("%" + spec)
|
|
218
|
+
if not wrote_conversion:
|
|
219
|
+
break
|
|
220
|
+
return 0
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def builtin_read(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
224
|
+
args = argv[1:]
|
|
225
|
+
raw = False
|
|
226
|
+
if args and args[0] == "-r":
|
|
227
|
+
raw = True
|
|
228
|
+
args = args[1:]
|
|
229
|
+
if not args:
|
|
230
|
+
args = ["REPLY"]
|
|
231
|
+
line = stdin.readline()
|
|
232
|
+
if line == "":
|
|
233
|
+
return 1
|
|
234
|
+
line = line.rstrip("\n")
|
|
235
|
+
if not raw:
|
|
236
|
+
line = line.replace("\\\n", "")
|
|
237
|
+
values = line.split()
|
|
238
|
+
for index, name in enumerate(args):
|
|
239
|
+
if not is_name(name):
|
|
240
|
+
stderr.write(f"read: {name}: not a valid identifier\n")
|
|
241
|
+
return 2
|
|
242
|
+
if index == len(args) - 1:
|
|
243
|
+
value = " ".join(values[index:])
|
|
244
|
+
else:
|
|
245
|
+
value = values[index] if index < len(values) else ""
|
|
246
|
+
shell.set_parameter(name, value)
|
|
247
|
+
return 0
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def builtin_type(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
251
|
+
if len(argv) == 1:
|
|
252
|
+
stderr.write("type: missing operand\n")
|
|
253
|
+
return 2
|
|
254
|
+
status = 0
|
|
255
|
+
for name in argv[1:]:
|
|
256
|
+
if shell.is_builtin(name):
|
|
257
|
+
stdout.write(f"{name} is a shell builtin\n")
|
|
258
|
+
continue
|
|
259
|
+
path = shell.which(name)
|
|
260
|
+
if path:
|
|
261
|
+
stdout.write(f"{name} is {path}\n")
|
|
262
|
+
else:
|
|
263
|
+
stderr.write(f"type: {name}: not found\n")
|
|
264
|
+
status = 1
|
|
265
|
+
return status
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def builtin_command(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
269
|
+
args = argv[1:]
|
|
270
|
+
if not args:
|
|
271
|
+
return 0
|
|
272
|
+
if args[0] in {"-v", "-V"}:
|
|
273
|
+
status = 0
|
|
274
|
+
for name in args[1:]:
|
|
275
|
+
if shell.is_builtin(name):
|
|
276
|
+
stdout.write(f"{name}\n" if args[0] == "-v" else f"{name} is a shell builtin\n")
|
|
277
|
+
continue
|
|
278
|
+
path = shell.which(name)
|
|
279
|
+
if path:
|
|
280
|
+
stdout.write(path + "\n")
|
|
281
|
+
else:
|
|
282
|
+
status = 1
|
|
283
|
+
return status
|
|
284
|
+
return shell.run_preexpanded(args, stdin=stdin, stdout=stdout, stderr=stderr)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def builtin_env(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
288
|
+
args = argv[1:]
|
|
289
|
+
env = {} if args and args[0] == "-i" else dict(shell.env)
|
|
290
|
+
if args and args[0] == "-i":
|
|
291
|
+
args = args[1:]
|
|
292
|
+
while args and "=" in args[0]:
|
|
293
|
+
name, value = args[0].split("=", 1)
|
|
294
|
+
env[name] = value
|
|
295
|
+
args = args[1:]
|
|
296
|
+
if not args:
|
|
297
|
+
for name in sorted(env):
|
|
298
|
+
stdout.write(f"{name}={env[name]}\n")
|
|
299
|
+
return 0
|
|
300
|
+
return shell.run_external(args, env=env, stdin=stdin, stdout=stdout, stderr=stderr)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def builtin_test(shell, argv: list[str], stdin: TextIO, stdout: TextIO, stderr: TextIO) -> int:
|
|
304
|
+
args = argv[1:]
|
|
305
|
+
if argv[0] == "[":
|
|
306
|
+
if not args or args[-1] != "]":
|
|
307
|
+
stderr.write("[: missing closing ]\n")
|
|
308
|
+
return 2
|
|
309
|
+
args = args[:-1]
|
|
310
|
+
return 0 if eval_test(args) else 1
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def eval_test(args: list[str]) -> bool:
|
|
314
|
+
if not args:
|
|
315
|
+
return False
|
|
316
|
+
if len(args) == 1:
|
|
317
|
+
return args[0] != ""
|
|
318
|
+
if len(args) == 2:
|
|
319
|
+
op, value = args
|
|
320
|
+
if op == "!":
|
|
321
|
+
return not eval_test([value])
|
|
322
|
+
if op == "-n":
|
|
323
|
+
return value != ""
|
|
324
|
+
if op == "-z":
|
|
325
|
+
return value == ""
|
|
326
|
+
if op == "-e":
|
|
327
|
+
return os.path.exists(value)
|
|
328
|
+
if op == "-f":
|
|
329
|
+
return os.path.isfile(value)
|
|
330
|
+
if op == "-d":
|
|
331
|
+
return os.path.isdir(value)
|
|
332
|
+
if len(args) == 3:
|
|
333
|
+
left, op, right = args
|
|
334
|
+
if op == "=":
|
|
335
|
+
return left == right
|
|
336
|
+
if op == "!=":
|
|
337
|
+
return left != right
|
|
338
|
+
if op in {"-eq", "-ne", "-gt", "-ge", "-lt", "-le"}:
|
|
339
|
+
try:
|
|
340
|
+
a = int(left, 10)
|
|
341
|
+
b = int(right, 10)
|
|
342
|
+
except ValueError:
|
|
343
|
+
return False
|
|
344
|
+
return {
|
|
345
|
+
"-eq": a == b,
|
|
346
|
+
"-ne": a != b,
|
|
347
|
+
"-gt": a > b,
|
|
348
|
+
"-ge": a >= b,
|
|
349
|
+
"-lt": a < b,
|
|
350
|
+
"-le": a <= b,
|
|
351
|
+
}[op]
|
|
352
|
+
return False
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def quote_for_display(value: str) -> str:
|
|
356
|
+
return "'" + value.replace("'", "'\\''") + "'"
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def decode_escapes(value: str) -> str:
|
|
360
|
+
result: list[str] = []
|
|
361
|
+
i = 0
|
|
362
|
+
while i < len(value):
|
|
363
|
+
char = value[i]
|
|
364
|
+
if char != "\\":
|
|
365
|
+
result.append(char)
|
|
366
|
+
i += 1
|
|
367
|
+
continue
|
|
368
|
+
if i + 1 >= len(value):
|
|
369
|
+
result.append("\\")
|
|
370
|
+
break
|
|
371
|
+
nxt = value[i + 1]
|
|
372
|
+
mapping = {
|
|
373
|
+
"a": "\a",
|
|
374
|
+
"b": "\b",
|
|
375
|
+
"f": "\f",
|
|
376
|
+
"n": "\n",
|
|
377
|
+
"r": "\r",
|
|
378
|
+
"t": "\t",
|
|
379
|
+
"v": "\v",
|
|
380
|
+
"\\": "\\",
|
|
381
|
+
}
|
|
382
|
+
if nxt in mapping:
|
|
383
|
+
result.append(mapping[nxt])
|
|
384
|
+
i += 2
|
|
385
|
+
continue
|
|
386
|
+
if nxt in "01234567":
|
|
387
|
+
j = i + 1
|
|
388
|
+
while j < len(value) and j < i + 4 and value[j] in "01234567":
|
|
389
|
+
j += 1
|
|
390
|
+
result.append(chr(int(value[i + 1 : j], 8)))
|
|
391
|
+
i = j
|
|
392
|
+
continue
|
|
393
|
+
result.append(nxt)
|
|
394
|
+
i += 2
|
|
395
|
+
return "".join(result)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
BUILTINS: dict[str, Builtin] = {
|
|
399
|
+
":": builtin_colon,
|
|
400
|
+
"true": builtin_true,
|
|
401
|
+
"false": builtin_false,
|
|
402
|
+
"echo": builtin_echo,
|
|
403
|
+
"pwd": builtin_pwd,
|
|
404
|
+
"cd": builtin_cd,
|
|
405
|
+
"exit": builtin_exit,
|
|
406
|
+
"export": builtin_export,
|
|
407
|
+
"unset": builtin_unset,
|
|
408
|
+
"set": builtin_set,
|
|
409
|
+
"shift": builtin_shift,
|
|
410
|
+
"printf": builtin_printf,
|
|
411
|
+
"read": builtin_read,
|
|
412
|
+
"type": builtin_type,
|
|
413
|
+
"command": builtin_command,
|
|
414
|
+
"env": builtin_env,
|
|
415
|
+
"test": builtin_test,
|
|
416
|
+
"[": builtin_test,
|
|
417
|
+
}
|
|
418
|
+
|