Skip to content

Commit

Permalink
added brace range expansion; update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mitnk committed May 13, 2019
1 parent 1c0b40b commit 8bef57f
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 30 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Change Logs

## 0.9.6 - master
## 0.9.6

- Added `if`, `for`, `while` expression into cicada scripting ability.
- Added new braces range expansion: `{1..10}`.
- Fixed a parsing issue for: `alias foo-bar='echo foo bar'`.
- Fixed cannot define single-char-long env/variable.

Expand Down
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Cicada is a simple Unix shell written in Rust.
## Documents

- [Environment Variables](https://github.com/mitnk/cicada/tree/master/docs/envs.md)
- [Built-in Commands](https://github.com/mitnk/cicada/tree/master/docs/built-in-cmd.md)
- [Cicada Builtins](https://github.com/mitnk/cicada/tree/master/docs/builtins.md)
- [Completion](https://github.com/mitnk/cicada/tree/master/docs/completion.md)
- [RC File](https://github.com/mitnk/cicada/tree/master/docs/rc-file.md)
- [History](https://github.com/mitnk/cicada/tree/master/docs/history.md)
Expand All @@ -34,7 +34,7 @@ Desktop
Documents
Downloads
$ echo foo bar | awk -F " " '{print $2, $1}'
$ echo foo,bar | awk -F "," '{print $2, $1}'
bar foo
```

Expand Down Expand Up @@ -71,19 +71,6 @@ $ echo foo || echo bar
foo
```

### Shell expansions

```
$ echo sp{el,il,al}l
spell spill spall
$ echo $SHELL
/usr/local/bin/cicada
$ echo *
Cargo.lock Cargo.toml LICENSE Makefile README.md src target
```

### Math arithmetic directly in the shell!

```
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Cicada is a simple bash-like Unix shell.

- [Install cicada](https://github.com/mitnk/cicada/blob/master/docs/install.md)
- [Environment Variables](https://github.com/mitnk/cicada/tree/master/docs/envs.md)
- [Built-in Commands](https://github.com/mitnk/cicada/tree/master/docs/built-in-cmd.md)
- [Cicada Builtins](https://github.com/mitnk/cicada/tree/master/docs/builtins.md)
- [Completion](https://github.com/mitnk/cicada/tree/master/docs/completion.md)
- [RC File](https://github.com/mitnk/cicada/tree/master/docs/rc-file.md)
- [History](https://github.com/mitnk/cicada/tree/master/docs/history.md)
Expand All @@ -30,7 +30,7 @@ Desktop
Documents
Downloads
$ echo foo bar | awk -F " " '{print $2, $1}'
$ echo foo,bar | awk -F "," '{print $2, $1}'
bar foo
```

Expand Down
98 changes: 97 additions & 1 deletion docs/built-in-cmd.md → docs/builtins.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# Cicada Built-in Commands
# Cicada Shell Builtins

- Builtin Commands
- [alias](#alias)
- [bg](#bg)
- [cd](#cd)
- [cinfo](#cinfo)
- [exec](#exec)
- [exit](#exit)
- [export](#export)
- [fg](#fg)
- [history](#history)
- [jobs](#jobs)
- [source](#source)
- [unalias](#unalias)
- [vox](#vox)
- [Builtin Shell Expansions](#builtin-shell-expansions)

## alias

Expand Down Expand Up @@ -135,3 +151,83 @@ Exit (deactivate) your env:
(my-project) $ vox exit
$ # now you're clean
```

## Builtin Shell Expansions

### Brace Expansion

```sh
$ echo sp{el,il,al}l
spell spill spall

$ cp foo.txt{,.bak}
# equal to `cp foo.txt foo.txt.bak`

$ echo {1..5}
1 2 3 4 5

$ echo {1..5..2}
1 3 5
```

### Tilde Expansion

```
$ echo ~/foo
# equal to echo $HOME/foo
```

### Parameter Expansion

Currently only works in scripting.

```sh
$ cat foo.sh
echo "the args are: $@"
echo $3 $1 $2
echo $0

$ cicada foo.sh a b c
the args are: a b c
c a b
foo.sh
```

### Command Substitution

Command substitution allows the output of a command to replace the command
itself. Command substitution occurs when a command is enclosed as follows:

```
$(command)
```

or
```
`command`
```

### Filename Expansion

```
$ echo src/*.rs
src/build.rs src/execute.rs src/history.rs src/jobc.rs ...
```

### Special Expansions

```sh
# current session process ID
$ echo $$
26653

# last command exit status
$ echo $?
0

$ cat /etc/some-config

# last command substitution
$ sudo !!
sudo cat /etc/some-config
```
2 changes: 1 addition & 1 deletion docs/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export HISTORY_SIZE=100000
export HISTORY_TABLE="cicada_history"
```

See more on [history built-in command](https://github.com/mitnk/cicada/blob/master/docs/built-in-cmd.md#history)
See more on [history built-in command](https://github.com/mitnk/cicada/blob/master/docs/builtins.md#history)
See more on [Environment Variables](https://github.com/mitnk/cicada/blob/master/docs/envs.md#history_size)
2 changes: 1 addition & 1 deletion docs/prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Note you can also use regular environment variables that not in the list, like `

## Python Virtual Env in Prompt

See also [builtin vox](https://github.com/mitnk/cicada/blob/master/docs/built-in-cmd.md#vox)
See also the [vox](https://github.com/mitnk/cicada/blob/master/docs/builtins.md#vox) builtin.

When you enter a virtual env, the prompt will prefix by `(pyenv-name)`. e.g.
`(evn-test)mitnk:mbp: pip$ `.
Expand Down
2 changes: 1 addition & 1 deletion docs/rc-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ or with the `--login`/`-l` option, it first reads and executes commands from
the file `~/.cicadarc`, if that file exists.

> Hint: In non-login shell mode, you can apply RC file with
> [source](https://github.com/mitnk/cicada/blob/master/docs/built-in-cmd.md#source):
> [source](https://github.com/mitnk/cicada/blob/master/docs/builtins.md#source):
> `$ source ~/.cicadarc`.
Here is a sample RC file:
Expand Down
41 changes: 39 additions & 2 deletions docs/scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ File content of `~/hello.sh`:
```sh
#!/usr/local/bin/cicada
echo hello scripting
# spliting command into multiple lines
echo hi \
there
echo "the args are: $@"
echo $3 $1 $2
date
Expand Down Expand Up @@ -50,6 +53,7 @@ Either way, the output looks like this:

```
hello scripting
hi there
runing /home/mitnk/hello.sh with args: foo bar baz
baz foo bar
Sat Apr 27 17:14:36 CST 2019
Expand Down Expand Up @@ -89,6 +93,29 @@ not found bar
no a and no b
```
### Use test `[` command
The [test](https://linux.die.net/man/1/test) command `[` is a convenient tool
to use in `if` and `while` statements.
```
foo=35
if [ $foo -gt 10 ]
echo "foo is great than 10"
else
echo "foo is less than 10"
fi

if [ $(uname -s) = 'Darwin' ]
echo "you're in Mac OS"
fi
```
For for details, please check out `[ --help` in your shell.
**Note:** Compare strings with `[ $str1 > $str2 ]` is not supported in cicada.
The `>` would be treated as output redirections.
## For Statements
In cicada, `for` statement loop the space splitted strings. In each iteration,
Expand All @@ -111,6 +138,10 @@ done
for f in src/builtins/ex*.rs
echo source file $f
done
for x in {1..10..2}
echo "x = $x"
done
```
The output of above script is:
Expand All @@ -130,6 +161,12 @@ sh script
source file src/builtins/exec.rs
source file src/builtins/exit.rs
source file src/builtins/export.rs
x = 1
x = 3
x = 5
x = 7
x = 9
```
## While Statements
Expand Down Expand Up @@ -209,7 +246,7 @@ counter = 22
## The source Builtin
> See also [the source builtin](https://github.com/mitnk/cicada/blob/master/docs/built-in-cmd.md#source).
> See also [the source builtin](https://github.com/mitnk/cicada/blob/master/docs/builtins.md#source).
Command like `$ cicada foo.sh` would create a new session and run the commands
of file `foo.sh`. If you want to run them in current shell session, you
Expand All @@ -218,7 +255,7 @@ can run it with `$ source foo.sh`.
## Using Builtins
In scripts, you could also use cicada's
[builtins](https://github.com/mitnk/cicada/blob/master/docs/built-in-cmd.md).
[builtins](https://github.com/mitnk/cicada/blob/master/docs/builtins.md).
For example, you can include extra RC configs with `source` at the end of
`~/.cicadarc` file:
([RC file](https://github.com/mitnk/cicada/blob/master/docs/rc-file.md)
Expand Down
14 changes: 13 additions & 1 deletion src/scripting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{Read, Write};
use std::path::Path;

use pest::iterators::Pair;
use regex::Regex;
use regex::{Regex, RegexBuilder};

use crate::execute;
use crate::libs;
Expand Down Expand Up @@ -58,6 +58,18 @@ pub fn run_script(sh: &mut shell::Shell, args: &Vec<String>) -> i32 {
}
}

let re;
match RegexBuilder::new(r#" +\\\n +"#).multi_line(true).build() {
Ok(x) => {
re = x;
}
Err(e) => {
println_stderr!("cicada: re build error: {:?}", e);
return 1;
}
}
text = re.replace_all(&text, " ").to_string();

match parsers::locust::parse_lines(&text) {
Ok(pairs_exp) => {
for pair in pairs_exp {
Expand Down
Loading

0 comments on commit 8bef57f

Please sign in to comment.