PicoLisp and Linux/GNU system commands

Most of the most commonly used and useful Linux commands needs a file as input, in the shell this can be circumvented by for instance a line like this: echo “hej” | md5sum.

Echo pipes into md5sum and we get the hash, if we can accomplish the same in PicoLisp we can make use of a lot of Linux’s built-ins, such as sed and awk.

With a combination of the pipe function and the out function we can:

(class +Cmd)

(dm exe> (Str AsStr . @)
   (pipe (out (rest) (prin Str)) (till NIL AsStr)) )

#(println (md5sum> '+Cmd "hej"))
(dm md5sum> (Str)
   (car (mapcar pack (split (exe> This Str NIL 'md5sum) " "))))

(dm sed> (Cmd Str)
   (exe> This Str T 'sed Cmd))

#(println (ssub> '+Cmd "hej" "pej" "hej pa dig hej"))
(dm ssub> (Search Replace Str)
   (sed> This (pack "s/" Search "/" Replace "/g") Str))

(dm awk> (Cmd Str)
   (exe> This Str T 'awk (pack "{ " Cmd " }")))

#(println (match> '+Cmd "apa" "apa hej pa dig"))
(dm match> (Search Str)
   (<> 0 (any (awk> This (pack "print match($0, /" Search "/)") Str))) )

#(println (gsub> '+Cmd "hej" "pej" "apa hej pa dig hej"))
(dm gsub> (Search Replace Str)
   (awk> This (pack "gsub(/" Search "/, \"" Replace "\"); print") Str) )

What happens in exe> is that out gets a list as its first argument, if that happens it will interpret this as a command to be executed and it will pipe the result of the second argument to this command. In our case the second argument is simply (prin Str) which will simply make our passed string as input.

Pipe will now get the output from out as input and we use the (till NIL T) command to read and return the whole thing.

If it’s just a matter of calling an external script (which accepts a string as input) in can be used.

(dm php> (Script . @)
   (in (append (list "php" (pack Script ".php")) (rest)) (till)))

The above uses PHP as an example but for instance Python or Ruby would work the same.

Related Posts

Tags: , , ,