Skip to content

Commit

Permalink
Better history navigation, line editor enhancements. New output subst…
Browse files Browse the repository at this point in the history
… syntax
  • Loading branch information
Edd12321 committed Apr 16, 2023
1 parent 310948a commit 148d3d8
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 123 deletions.
33 changes: 10 additions & 23 deletions .zrc
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#!/usr/bin/env zrc
echo 'Welcome to Zrc!'
echo 'The time is: $(date)'
echo 'The time is: `{date}'
set E:SHELL = 'zrc'
#echo "Disk usage:";
#df -h;

# Prompt variables
set HOSTNAME = $(hostname)
set USER = $(whoami)

set RBOLD = $(/bin/echo -ne {\e[1;31m})
set GREEN = $(/bin/echo -ne {\e[0;32m})
set RESET = $(/bin/echo -ne {\e[0m})

set HOSTNAME =`{hostname}
set USER =`{whoami}
set RBOLD =`{/bin/echo -ne {\e[1;31m}}
set GREEN =`{/bin/echo -ne {\e[0;32m}}
set RESET =`{/bin/echo -ne {\e[0m}}
fn __git_ps1 {
git tag >(2=) > /dev/null
if {$? == 0} {
Expand All @@ -21,36 +18,30 @@ fn __git_ps1 {
echo -n ')'
}
}

fn __status_ps1 {
if {[string length $status] && [string cmp $status "0"]} {
echo -n [string cat $RBOLD "($status)" $RESET]
}
}

fn prompt_cmd {
set status = $?
set PWD = $(pwd | sed 's|^$E:HOME|~|');

set PWD = `{pwd | sed 's|^$E:HOME|~|'};
title "(zrc) $PWD" > /dev/tty
return {}
}

fn title {
/bin/echo -ne {\e]0;}
echo -n $argv(1) \a
}

set PS1 = {[string cat [prompt_cmd]$GREEN $USER $RESET\@$HOSTNAME]\
[string cat $GREEN $PWD]\
[string cat \
$RESET \
$(__git_ps1) \
$(__status_ps1)% '\ ']}


`{__git_ps1} \
`{__status_ps1}% '\ '];return $status}
# Aliases
alias l "ls -hF --color"
alias lt "ls -altrhF --color"
alias la "ls -all"
alias l. "eval ls -d .*"
alias c "clear"
Expand All @@ -71,17 +62,14 @@ alias pacman "\$E:AUTH pacman"
alias calc "bc -ql"
alias emacs "emacs -nw"
alias history "eval cat ~/.zrc_history | \$E:PAGER"

# Custom tools
# https://github.com/Edd12321/{cstop,evct,zrc}
alias top+ 'cstop;reset'
alias vi+ 'evct'
set E:PATH = /usr/lib/zrc/corebuf/bin:$E:PATH
set E:MANPATH = /usr/lib/zrc/doc:$E:MANPATH

fn mkcd { mkdir $argv(1) && cd $argv(1); }
fn lcd { cd $argv(1) && l }

# Default programs
set E:AUTH = "doas"
set E:EDITOR = "emacs"
Expand All @@ -92,7 +80,6 @@ set E:AS = "fasm"
set E:CXX = "g++"
set E:CC = "gcc"
set E:BROWSER = "firefox"

if {[string cmp $E:TERM "linux"]} {
set E:TERM = "xterm-256color"
}
2 changes: 1 addition & 1 deletion doc/man1/switch.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ then the utility changes the default fallback compound block to the one found ne
word. If, at the end of this scanning process, no case statement short-circuited the switch command, the fallback compound block (initally empty) executes.
.SH EXAMPLES
.EX
set name = $(read line -p "What's your name? ")
set name = `{read line -p "What's your name? "}
switch $name {
case Johnny {
echo Hello, man!
Expand Down
16 changes: 8 additions & 8 deletions doc/man1/zrc.1
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ is an array that stores all arguments passed to the current function (or to the
.PP
.B COMMAND OUTPUT SUBSTITUTION:
Any sequence of the type
.B $(...)
.B `{...}
treats everything inside braces as a command and returns the standard output it produces. Implicitly, it erases the ending newline character for convenience's sake, but this behavior can be bypassed using quotes. Note that you can nest these:
.EX
echo $(basename a/b/c/d)
echo `{basename a/b/c/d}
# Displays 'd'
echo $(basename $(basename a/b/c))
echo `{basename `{basename a/b/c}}
# Displays 'c'
echo "$(basename $(basename a/b/c))"
echo "`{basename `{basename a/b/c}}"
# Displays 'c\\n'
.EE
.PP
Expand All @@ -133,7 +133,7 @@ As opposed to traditional shells, Zrc supports
, which means that functions and builtins (but not external programs) can return values other than the classic "status numbers" 0-255 (they can be any string). You can access the return value of a command by using square brackets:
.EX
echo [ls -all]
echo [$(echo ls) -all]
echo [`{echo ls -all}]
echo [[[]]] #echo's 127
echo $?
.EE
Expand All @@ -154,7 +154,7 @@ are simpler than the substitutions: You can group together words ­using normal
return 'a b c'
# Returns "abcdef"
return 'a'"b"cd"e"`f`
return 'a'"b"cd"e"'f'
# Doesn't evaluate to "[a]"
return '[a]'
Expand All @@ -171,8 +171,8 @@ are simpler than the substitutions: You can group together words ­using normal
.EE
There is also "brace quoting", borrowed from Tcl: Brace quoting ends the current word and begins a new one, containing everything between braces. No substitutions are allowed within braces:
.EX
# Prints "$(a)", doesn't run the command
echo {$(a)}
# Prints "`{a}", doesn't run the command
echo {`{a}}
# Nesting
echo {{{}}}
Expand Down
6 changes: 3 additions & 3 deletions examples/bdproc.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
################################
# Disable Intel CPU throttling #
################################
if {$(id -u) == 0} {
set REG = $(rdmsr -d 0x1FC)
if {`{id -u} == 0} {
set REG = `{rdmsr -d 0x1FC}
if {$REG & 1 == 0} {
if {[string cmp $argv(2) "-d"] == 0}\
exit
# Enable BD PROCHOT
inc REG;
inc REG
} else {
if {[string cmp $argv(2) "-e"] == 0}\
exit
Expand Down
2 changes: 1 addition & 1 deletion examples/fib.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn Fibonacci {

fn main {
echo -n "k="
set k = $(read line)
set k = `{read line}

# Fibonacci sequence
Fibonacci 0 1 $k;
Expand Down
2 changes: 1 addition & 1 deletion examples/gameoflife.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn neighbor {

# Generate a random unsigned integer
fn rand {
return $(shuf -i 200-6500 -n 1)
return `{shuf -i 200-6500 -n 1}
}

# Main routine
Expand Down
10 changes: 5 additions & 5 deletions examples/gui_zenity.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ if {[which pandoc > /dev/null] != 0} { die "Please install pandoc." }

zenity --info --text="Welcome! Please choose a document to convert...";

set file_old = $(zenity --file-selection --title="Pick a file...");
set file_new = $(echo -n $file_old \
set file_old = `{zenity --file-selection --title="Pick a file..."};
set file_new = `{echo -n $file_old \
| cut -f1 -d. \
| tr -d '\n' \
);
}

set format = $(
set format = `{
zenity --list --title="Convert to..." --column="Format" --column="Text" \
docx "Office DOCX" \
odt "Open Document" \
Expand All @@ -21,7 +21,7 @@ set format = $(
markdown "Markdown" \
mediawiki "MediaWiki" \
context "ConTeXt" | tr -d '\n';
);
}

if {![test -f $file_new.$format]} {
if {![zenity --question \
Expand Down
2 changes: 1 addition & 1 deletion examples/ip_tool.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn usage {
fn main {
# IP address
for {set i = 1} {$i <= 4} {inc i} {
set ip($i) = $(echo -n $argv(1) | cut -d. -f$i)
set ip($i) = `{echo -n $argv(1) | cut -d. -f$i}
set ip = "$ip[to_bin $ip($i)]"
}

Expand Down
2 changes: 1 addition & 1 deletion examples/is_prime.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn este_prim {
}

fn main {
printf "x="; set x = $(read str);
printf "x="; set x = `{read str};

while {$x} {
if [este_prim $x] {
Expand Down
4 changes: 2 additions & 2 deletions examples/man.cgi.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ if {![string cmp $q ''] || ![string cmp $q all]} {

# Display the manpage (compressed or not)
} else {
set W1 = $(echo $q | awk '-F&' {{print $1}})
set W1 = `{echo $q | awk '-F&' {{print $1}}}
if [man -w $W1 > /dev/null >(2=1)] {
echo "<h3><font color=red>No manual page for $W1!!</font></h3>"
} else {
set file = $(man -w $W1)
set file = `{man -w $W1}
if [gzip -t $file] {
cat $file | groff -mandoc -Thtml
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/mgcsquare.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn display {

fn main {
echo -n "val=";
set val = $(read str);
set val = `{read str};

if {$val % 2 == 0} {
inc val
Expand Down
4 changes: 2 additions & 2 deletions examples/oji_arhitect.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ set max = -1
fn fin {
let i {
for {set i = 1} {$i < $argc} {inc i} {
set $argv($i) = $(read str)
set $argv($i) = `{read str}
}}
}

fn main {
set n = $(read str)
set n = `{read str}
while {$n>0} {
fin x1 y1 x2 y2
set num = [expr abs(($x1-$x2)/($y1-$y2))]
Expand Down
16 changes: 8 additions & 8 deletions examples/server.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ fn serve {
<!DOCTYPE HTML>
<html>
<head>
<style>"$(css)"</style>
<style>"`{css}"</style>
</head>
<body>
<h1>Zrc dashboard demo</h1>
"$(date)"
"`{date}"

<!-----COMMANDS BEGIN----->
"[button 'remo dialog' 'Show dialog on server']"
Expand All @@ -43,13 +43,13 @@ fn serve {
<!-----COMMANDS END----->

<h2>MPStat</h2><pre>
"$(mpstat | ansifilter)"
"`{mpstat | ansifilter}"
</pre>
<h2>VMStat</h2><pre>
"$(vmstat | ansifilter)"
"`{vmstat | ansifilter}"
</pre>
<h2>IOStat</h2><pre>
"$(iostat | ansifilter)"
"`{iostat | ansifilter}"
</pre>
</body>
</html>
Expand Down Expand Up @@ -100,7 +100,7 @@ fn remo {

fn main {
echo -n "Please enter log filename: "
set LOG_FILE = $(read line)
set LOG_FILE =`{read line}

$E:BROWSER http://localhost:8080\
> /dev/null &
Expand All @@ -110,11 +110,11 @@ fn main {
}
# Main webserver loop
while 1 {
set CMD = $(echo -e "HTTP/1.1 200 OK\n\n$(serve)" \
set CMD = `{echo -e "HTTP/1.1 200 OK\n\n`{serve}" \
| nc -l -k -q 1 -p 8080 \
| grep "GET /remo" \
| sed -e 's/%20/ /g'
)
}
eval [string range $CMD 5 11]
}
}; main
2 changes: 1 addition & 1 deletion examples/trig.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set E:PATH = /bin/:$E:PATH

printf "n="
set n = $(read line)
set n = `{read line}

echo "Check file.out !!"
echo -n > "file.out"
Expand Down
2 changes: 1 addition & 1 deletion examples/truth_machine.zrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!../bin/zrc
set input = $(read line)
set input = `{read line}

switch $input {
case 0 {
Expand Down
2 changes: 1 addition & 1 deletion examples/whereis.zrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn find_files {
foreach file * {
foreach ext ".exe" ".com" ".cmd" ".zrc" {
if {[string cmp \
$file $(basename $file $ext)] != 0} {
$file `{basename $file $ext}] != 0} {
echo "Found file $file with extension $ext\!"
inc k
}
Expand Down
2 changes: 1 addition & 1 deletion examples/windowmgrmgr.zrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env zrc
# Personal script
set WM = $(echo "awesome\nxfwm4" | dmenu | tr -d '\n')
set WM = `{echo "awesome\nxfwm4" | dmenu | tr -d '\n'}
set CMD = "xfconf-query -c xfce4-panel -np /panels/panel-3/autohide-behavior -t int -s"

switch $WM {
Expand Down
8 changes: 6 additions & 2 deletions src/lex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ tokenize(std::string line, std::istream& in)
if (q == '{') p = '}';
if (q == '(') p = ')';

if (q != '(')
if (!tmp.empty() && tmp.back() == '`' && q == '{') {
tmp.pop_back();
wl.add_token(tmp);
tmp += "`{";
} else if (q != '(') {
wl.add_token(tmp);
}

for ever {
CHK_LINE;
Expand All @@ -101,7 +106,6 @@ tokenize(std::string line, std::istream& in)
// Regular quoting
case '\'':
case '"':
case '`':
q = line[i];
STR_KLUDGE(q);
wl.make_not_bare();
Expand Down
Loading

0 comments on commit 148d3d8

Please sign in to comment.