Skip to main content

Bindings for the dash shell as a library

Project description

Main workflow

libdash is a fork of the Linux Kernel's dash shell that builds a linkable library with extra exposed interfaces. The primary use of libdash is to parse shell scripts, but it could be used for more.

The Python bindings are packaged as the libdash PyPi package.

The OCaml bindings---packaged as the libdash OPAM package---include two executables, shell_to_json and json_to_shell, which let you conveniently parse POSIX shell scripts into a JSON AST.

What are the dependencies?

The C code for dash should build on a wide variety of systems; it requires libtool and autotools (aclocal, autoheader, automake, autoconf). The library may not build on platforms with esoteric linkers; it's been tested on macOS and Linux.

The Python and OCaml bindings depend on being able to build the C code. See libdash.opam for details on the OCaml code's dependencies, which includes the build-time external dependencies. Python wheels have no need for these build-time dependencies, but building from a Python source distribution will only succeed when libtool and autotools are present.

The CI scripts (in .github/workflows/build.yml) give build details.

How to build libdash from source

Python

Run python3 setup.py install. On macOS, you must first install the build dependencies via brew install libtool autoconf automake.

You can test the Python bindings by running:

cd python; make test

OCaml

Install the OPAM file: opam pin add . or opam install .. This will build the OCaml library and install it in your OPAM repository. There are tests in another directory; they will only build when libdash is actually installed.

You can test the OCaml bindings by running:

cd ocaml; make test

Testing

The tests use test/round_trip.sh to ensure that every tester file in test/tests round-trips correctly through parsing and pretty printing.

Additionally, you can run tests that compare the OCaml and Python implementations (after you've installed them both):

cd test; make

How to use the parser

For Python, see python/rt.py, an example tool that does a round-trip: shell syntax to AST back to shell syntax.

For OCaml, see ocaml/shell_to_json.ml, a tool that parses shell syntax and produces JSON (using the atdgen bindings).

The ideal low-level interface to use is parsecmd_safe in parser.c; you'll need to ensure that dash's initialization routines have been called and that the stack marks are managed correctly. Parsing the POSIX shell is a complicated affair: beyond the usual locale issues, aliases affect the lexer, so one must use setalias and unalias to manage any aliases that ought to exist.

How work with the parsed nodes

The general AST is described in nodes.h. There are some tricky invariants around the precise formatting of control codes; the OCaml code shows some examples of working with the args fields in ocaml/ast.ml, which converts the C AST to an OCaml AST.

The OCaml tools shell_to_json and json_to_shell will produce JSON ASTs, allowing you to work with these ASTs in any language.

Pretty printing

The pretty printer does its best to produce valid shell scripts, but it's possible to manually construct AST nodes that don't directly correspond to valid scripts.

For example, the Python AST [[['Q', [['C', 34]]]]] represents a quoted field containing a double quote character. Translated literally, this would yield the string """, which is not a valid shell script. The pretty printer will instead automatically escape the inner quote, rendering "\"".

While the printer tries to get things right either way, you should use escapes to signal to the printer when to escape: you should use the Python AST [[['Q', [['E', 34]]]]] to mark the inner double quote as escaped.

Known issues

We currently do not escape the character ! (exclamation point). In an interactive shell, ! is likely treated as a history substitution (and so should be escaped), but in a non-interactive shell, ! is treated normally. We currently cater to non-interactive shells; eventually this behavior will be controllable.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libdash-0.3.1.tar.gz (171.0 kB view hashes)

Uploaded Source

Built Distributions

libdash-0.3.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (254.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

libdash-0.3.1-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (247.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libdash-0.3.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (156.6 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

libdash-0.3.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (254.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

libdash-0.3.1-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (247.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libdash-0.3.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (156.6 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

libdash-0.3.1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (254.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

libdash-0.3.1-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (247.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libdash-0.3.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (156.6 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

libdash-0.3.1-cp310-cp310-musllinux_1_1_x86_64.whl (250.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

libdash-0.3.1-cp310-cp310-musllinux_1_1_i686.whl (244.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

libdash-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (254.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

libdash-0.3.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (247.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libdash-0.3.1-cp310-cp310-macosx_10_9_x86_64.whl (156.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

libdash-0.3.1-cp39-cp39-musllinux_1_1_x86_64.whl (250.3 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

libdash-0.3.1-cp39-cp39-musllinux_1_1_i686.whl (244.7 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

libdash-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (254.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

libdash-0.3.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (247.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libdash-0.3.1-cp39-cp39-macosx_10_9_x86_64.whl (156.6 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

libdash-0.3.1-cp38-cp38-musllinux_1_1_x86_64.whl (250.3 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

libdash-0.3.1-cp38-cp38-musllinux_1_1_i686.whl (244.7 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

libdash-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (254.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

libdash-0.3.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (247.4 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libdash-0.3.1-cp38-cp38-macosx_10_9_x86_64.whl (156.6 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

libdash-0.3.1-cp37-cp37m-musllinux_1_1_x86_64.whl (250.3 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

libdash-0.3.1-cp37-cp37m-musllinux_1_1_i686.whl (244.7 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

libdash-0.3.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (254.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

libdash-0.3.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (247.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

libdash-0.3.1-cp37-cp37m-macosx_10_9_x86_64.whl (156.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page