Skip to content

Commit c605527

Browse files
Merge pull request #59 from yassinebenaid/dev
Ad support for "$#" and "$$" special vars
2 parents 98ed2fd + b041be3 commit c605527

File tree

7 files changed

+26
-52
lines changed

7 files changed

+26
-52
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Technically speaking, **Bunster** in fact is a `shell-to-Go` [Transplier](https:
2525

2626
In addition to the shell features, We aim to add several custom features to make shell scripts feel like any modern programming language. These features are either supported or are planned to be implemented in future. (_consider contributing to help us speed up the develpment cycle_)
2727

28-
- **Different Shells support**: Bunster currently aims to be compatible `Bash` as a starting move. then additional shells in future.
29-
- **Modules**: something shell scripts lack is a module system, we aim to introduces a module system that allow you to publish and consume scripts as libraries.
28+
- **Different Shells support**: Bunster currently aims to be compatible with `bash` as a starting move. Then additional shells in future.
29+
- **Modules**: Something shell scripts lack is a module system, we aim to introduce a module system that allow you to publish and consume scripts as libraries.
3030
- **Static Asset Embedding**: This feature allows you to embed a file's content to a variable at build time. ([Go has one already](https://pkg.go.dev/embed))
3131
- **Password and Expiration Lock**: Surprisingly, some people have asked for this feature. Basically, It allows you to choose an expirity date at build time. the generated program will not work after that date. Also you can choose to lock the script using a password. whenever you try to run it, it prompts for the password.
3232

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ hero:
1919
features:
2020
- title: Native Program Generation
2121
details: |
22-
Scripts compiled with Bunster are not just wrappers around your script, nor do they rely on any external shell on your system.
23-
- title: Wite Once, Run Everywhere
22+
Scripts compiled with Bunster are not just wrappers around your script, nor do they rely on any external shells on your system.
23+
- title: Write Once, Run Everywhere
2424
details: Bunster offers static linking. Your scripts are compiled to a statically linked binary that runs on every machine.
2525
- title: Module System
26-
details: Bunsters's module system makes it easy to shares your scripts as a versioned modules to be used by others.
26+
details: Bunsters's module system makes it easy to shares and consume your scripts as libraries.
2727
---

docs/maintainers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ const members = [
2020
</script>
2121

2222
# Maintainers
23-
**Bunster** is an open source project maintained and driven by the community, But we have a dedicated team
24-
that takes the final decision on contributions and feature requests. And keeps contact with the rest of the community to ensure a healthy and friendly
25-
environment for everyone.
23+
**Bunster** is an open source project driven by the public community, But we have a dedicated team
24+
that oversees the final decisions to ensure the project's direction aligns with its vision. The team also maintains open communication with the community
25+
to sustain a welcoming and collaborative environment for everyone.
2626

2727
Say hello to our awesome team.
2828

docs/supported-features.md

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,8 @@ This page lists the features and syntax supported by **Bunster**. you must consi
33
as work-in-progress and as not-yet-supported.
44

55

6-
## Simple commands
7-
```shell
8-
command argument 'argument' "argument" $VARIABLE
9-
```
10-
11-
## Redirections
12-
### Output redirection
13-
```shell
14-
command >file.txt >|file.txt >>file.txt &>file.txt &>>file.txt
15-
```
16-
### Input redirection
17-
```shell
18-
command <file.txt <<<"foo bar"
19-
```
20-
### File descriptor duplication and closing
21-
```shell
22-
command 2>&3 3>&4- 3>&- 2>&-
23-
```
24-
### Opening files for reading and writing
25-
```shell
26-
command <>file.txt 3<>file.txt
27-
```
28-
29-
::: tip
30-
Unlick bash, you are not limited to only use file descriptors between 0 to 9. Feel free to use any valid 64-bit integer number. (learn why TODO: add this link here)
31-
:::
32-
33-
## Passing shell parameters as environment to commands
34-
```shell
35-
key=value key2="value" command
36-
```
37-
38-
## Pipelines
39-
```shell
40-
command | command2 | command3
41-
```
42-
43-
## Conditional Execution
44-
```shell
45-
command || command2 && command3
46-
```
6+
- Simple commands
7+
- Redirections
8+
- Passing shell parameters as environment to commands
9+
- Pipelines
10+
- Conditional Execution (`command || command2 && command3`)

generator/tests/0-simple-commands.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func Main(shell *runtime.Shell) {
265265

266266
#(TEST: special variables)
267267

268-
echo $1 $?
268+
echo $1 $? $$ $#
269269

270270
#(RESULT)
271271

@@ -279,6 +279,8 @@ func Main(shell *runtime.Shell) {
279279
var arguments []string
280280
arguments = append(arguments, shell.ReadSpecialVar("1"))
281281
arguments = append(arguments, shell.ReadSpecialVar("?"))
282+
arguments = append(arguments, shell.ReadSpecialVar("$"))
283+
arguments = append(arguments, shell.ReadSpecialVar("#"))
282284
var command = shell.Command(commandName, arguments...)
283285
commandFDT, err := shell.CloneFDT()
284286
if err != nil {

runtime/shell.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ import (
1010
)
1111

1212
type Shell struct {
13+
PID int
14+
1315
Stdin Stream
1416
Stdout Stream
1517
Stderr Stream
1618

1719
Args []string
1820

19-
ExitCode int
20-
2121
FDT FileDescriptorTable
2222

23+
ExitCode int
24+
2325
Main func(*Shell)
2426
}
2527

@@ -40,6 +42,10 @@ func (shell *Shell) ReadVar(name string) string {
4042

4143
func (shell *Shell) ReadSpecialVar(name string) string {
4244
switch name {
45+
case "$":
46+
return strconv.FormatInt(int64(shell.PID), 10)
47+
case "#":
48+
return strconv.FormatInt(int64(len(shell.Args))-1, 10) // -1 to substract the argument index 0, which is the program name.
4349
case "?":
4450
return strconv.FormatInt(int64(shell.ExitCode), 10)
4551
default:

stubs/main.go.stub

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
func main() {
99

1010
shell := runtime.Shell{
11+
PID: os.Getpid(),
12+
1113
Stdin: os.Stdin,
1214
Stdout: os.Stdout,
1315
Stderr: os.Stderr,

0 commit comments

Comments
 (0)