PIPELN: UNIX pipelining tool
Last modification on
Motivation
Executing programs from other programs to handle a specific task appeals to the UNIX philosophy of limiting the scope of individual programs, and using divide-and-conquer to tackle large, complex problems. Counter-intuitively, however, creating pipelines is only convenient in shell scripts.
Orchestrating pipelines from native binaries typically
involves passing a command line (originating from the
REPL
nature of shells) to a shell via -c CMD
. This forces the caller
to glob command arguments together to a single string. Furthermore, the
user input must now be sanitized to prevent unintended behavior, such as
command injection. pipeln
addresses this issue.
Features
Instead of parsing the pipeline definition from a single arg, pipeln
uses the entire argument list it is invoked with, making it trivial to
reason about how many and what arguments each invoked program will receive.
The only input validation required by the caller is to ensure that "|"
is not included in user-controlled arguments.
The program fits inside a single, small C file (~140 LOC).
The source code is available at https://git.sinitax.com/sinitax/pipeln.
Examples
Creating a pipeline using Python to find files that match a regular expression in the current directory:
#!/usr/bin/env python3
from subprocess import call
from sys import argv, exit
if len(argv) != 2 or argv[1] == "|":
exit(1)
call(["pipeln", "find", "|", "grep", "-e", argv[1]])