Skip to content

Commit

Permalink
Shsub 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyx committed Jul 29, 2023
1 parent 60af82f commit f05a4de
Show file tree
Hide file tree
Showing 87 changed files with 669 additions and 735 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/shsub
/shsub.1
.DS_Store
*.tmp
*.dSYM
*.swp
/cli
/tc
/testenv
/testinst
44 changes: 26 additions & 18 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
The MIT License (MIT)
Shsub
Porject Homepage: <https://github.com/dongyx/shsub>
Copyright (c) 2022 DONG Yuxuan <https://www.dyx.name>

Copyright(C) DONG Yuxuan <https://www.dyx.name>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 changes: 12 additions & 49 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,64 +1,27 @@
.PHONY: all install test clean

name = shsub
prefix = /usr/local
bindir = $(prefix)/bin
libdir = $(prefix)/lib
datarootdir = $(prefix)/share
mandir = $(datarootdir)/man
INSTALL = install
CC = cc
CFLAGS = -std=c89 \
-D_POSIX_C_SOURCE=200809L \
-pedantic-errors \
-Wno-error=all

all: cli tc
all: shsub

install: all
$(INSTALL) -d \
$(bindir) \
$(libdir)/shsub \
$(mandir)/man1
$(INSTALL) cli tc $(libdir)/shsub/
$(INSTALL) -m644 version LICENSE $(libdir)/shsub/
$(INSTALL) -m644 shsub.1 $(mandir)/man1/
@echo append version information to manpage
@( \
printf "\n%s\n\n" ".SH VERSION"; \
printf "shsub "; \
cat version; \
printf "\n"; \
cat LICENSE; \
) >>$(mandir)/man1/shsub.1
@echo generate "`echo $(bindir)/shsub`"
@( \
echo '#!/bin/sh'; \
echo exec `echo $(libdir)/shsub/cli` '"$$@"'; \
) >$(bindir)/shsub
@chmod 755 $(bindir)/shsub
name='$(name)' ./shsub shsub.1.tpl >shsub.1
mkdir -p $(bindir) $(mandir)/man1
$(INSTALL) shsub $(bindir)/$(name)
$(INSTALL) -m644 shsub.1 $(mandir)/man1/$(name).1

test: all
@set -e; \
rm -rf testenv testinst; \
make install prefix="`pwd`/testinst"; \
cp -R test testenv; \
for t in testenv/*; do \
echo running test $$t...; \
( \
cd $$t; \
PATH="../../testinst/bin:$$PATH"; \
chmod +x run; \
./run \
); \
done; \
echo all tests passed

cli: cli.sh
cp $< $@
chmod +x $@

tc: tc.c
shsub: shsub.c
$(CC) $(CFLAGS) -o $@ $<

test: all
chmod +x test
./test

clean:
rm -rf cli tc testenv testinst *.dSYM
rm -rf shsub shsub.1
118 changes: 64 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,97 @@
shsub
Shsub
=====

`shsub` is a template engine of the shell language,
implemented in C and Shell.
Shsub is a template engine of the shell language,
implemented in C.

The following file `lsnotes.html.st` is a simple shell template:
The following file `notes.html.tpl` demonstrates a simple template:

<ul>
<% for i in notes/*.md; do -%>
<% title=`grep '^# ' "$i" | head -n1` -%>
<% if [ -n "$title" ]; then -%>
<li>
<%=$title %>
</li>
<% fi -%>
<% done -%>
<%for i in notes/*.md; do-%>
<% title="$(grep '^# ' "$i" | head -n1)"-%>
<li><%="$title"%></li>
<%done-%>
</ul>

Running `shsub lsnotes.html.st` prints a HTML list reporting titles of
your Markdown notes.
Calling `shsub notes.html.tpl`
prints a HTML list reporting titles of your Markdown notes.

Template Syntax
---------------
**Key Features**

The `shsub` program compiles the template to a shell script and execute it.
- Fast template compiling;
- Low memory footprint;
- Light-weight, containing only a standalone executable.

- if the first line of the shell template begins with `#!` (*shebang*),
it will be ignored
Template Syntax
---------------

- `<%`*cmd*`%>` is compiled to *cmd*
- Shell commands are surrounded by `<%` and `%>` and
are compiled to the commands themselves;

- `<%=`*expr*`%>` is compiled to `printf %s "`*expr*`"`
- Shell expressions are surrounded by `<%=` and `%>` and each `<%=expr%>` is compiled to `printf %s expr`;

Leading and trailing spaces, tabs, and newlines of *expr*
are removed.
Double quotes in *expr* are automatically escaped.
- Ordinary text is compiled to the command printing that text;

- `-%>` can be used instead of `%>` to ignore the following newline
- A template can include other templates by `<%+filename%>`;

- `<%%` and `%%>` are escaping tokens representing literal `<%` and `%>`
- If `-%>` is used instead of `%>`,
the following newline character will be ignored;

- ordinary text is compiled to the command that prints the text
- `<%%` and `%%>` are compiled to literal `<%` and `%>`.

Installation
------------

Download the source tarball of the latest version from
the [release list](https://github.com/dongyx/shsub/releases).
Unpack the tarball and
execute the following commands in the source tree:
It would be better to select a version from the
[release page](https://github.com/dongyx/shsub/releases)
than downloading the working code,
unless you understand the status of the working code.
The latest stable release is always recommended.

$ make
$ sudo make install

By default, Shsub is installed to `/usr/local`.
You could call `shsub --version` to check the installation.

Further Documentation
---------------------

make test
sudo make install
Calling Shsub with `--help` prints a brief of the command-line options.
The complete description is documented in the man page shipped with the installation.

`shsub` is installed to `/usr/local` by default.
The implementation is explained in the [paper](https://www.dyx.name/notes/shsub-impl.html).
If you attempt to contribute to this project, it may help.

Building `shsub` requires:
Migrating from 1.x
------------------

- `cc`(1): any C compiler which is compatible with `clang` or `gcc`
The breaking changes of Shsub 2.0.0 are:

Usage
-----
- The special treatment for shebang comments are deprecated;
If an executable is required,
compile the template to a shell script using the `-c` option;

`shsub` \[*options*\] \[*file*\] \[*argument* ...\]
- Expressions are no longer automatically quoted and escaped;

If *file* is `-` or omitted, `shsub` reads the template from stdin.
- The `progname` environment variable is no longer set;
If the template needs to use `$0`,
compile it to a shell script using the `-c` option;

options:
To make parallel installations of Shsub 1.x and the later version,
use the `name` installation variable:

- `-s` *sh* specify the shell to execute the compiled script,
`/bin/sh` by default
- `-h` print the brief usage
- `-v` print the version information
sudo make install name=shsub2

The detailed information is documented in the man page `shsub`(1)
which is included in the installation.
You could read it anytime by typing `man shsub`.
This will install with the name `shsub2`
instead of the default `shsub`.
Both the executable and the man page will use the specified name.

Uninstallation
--------------
Credits
-------

For default installation in `/usr/local`:
Shsub is created by [DONG Yuxuan](https://www.dyx.name) in 2022.

sudo rm /usr/local/bin/shsub
sudo rm -r /usr/local/lib/shsub/
sudo rm /usr/local/share/man/man1/shsub.1
The syntax is inspired by
Jakub Jirutka's [ESH](https://github.com/jirutka/esh)
whose syntax is based on ERB (Embedded Ruby).
1 change: 1 addition & 0 deletions cases/argv/echo.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3
1 change: 1 addition & 0 deletions cases/argv/echo.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%="$*"%>
5 changes: 5 additions & 0 deletions cases/argv/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -e
shsub echo.tpl 1 2 3 >echo.tmp
diff -u echo.out echo.tmp
1 change: 1 addition & 0 deletions cases/compile/echo.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3
1 change: 1 addition & 0 deletions cases/compile/echo.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%="$*"%>
6 changes: 6 additions & 0 deletions cases/compile/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -e
shsub -co echo.sh.tmp -s /bin/sh echo.tpl
./echo.sh.tmp 1 2 3 >echo.tmp
diff -u echo.out echo.tmp
10 changes: 10 additions & 0 deletions cases/letter/letter.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
A Letter from Shsub
Hi, dear user:
Shsub is a template engine of the shell language, implemented in C.

**Key Features**

- Fast template compiling;
- Low memory footprint;
- Light-weight, containing only a standalone executable.
- Shsub Developer
17 changes: 17 additions & 0 deletions cases/letter/letter.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%+vars.tpl-%>
<%. "$(pwd)/utils.sh"-%>
<%w=80-%>
<%(-%>
A Letter from Shsub
<%)|center $w-%>
Hi, <%="$name"%>:
<%(-%>
Shsub is a template engine of the shell language, implemented in C.

**Key Features**

- Fast template compiling;
- Low memory footprint;
- Light-weight, containing only a standalone executable.
<%) | fold -w$(($w-8)) | awk '{print "\t" $0}'-%>
<%( %>- Shsub Developer<% ) | xargs -0 printf %${w}s%>
3 changes: 3 additions & 0 deletions cases/letter/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
shsub letter.tpl >letter.tmp
diff -u letter.out letter.tmp
16 changes: 16 additions & 0 deletions cases/letter/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
center()
{
local w
local p
local l

w=$1
while read l; do
p=$(((w - ${#l})/2))
while [ $p -gt 0 ]; do
printf ' '
p=$((p-1))
done
printf %s\\n "$l"
done
}
1 change: 1 addition & 0 deletions cases/letter/vars.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%name="dear user"-%>
4 changes: 4 additions & 0 deletions cases/lexhack/lexhack.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................<%%%%%%>
........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
xy
xy
Loading

0 comments on commit f05a4de

Please sign in to comment.