[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Catching both the exit status and the output



I realized that using line to implement read was not altogether
trivial, as I need to check the exit status of line to distinguish
between an empty input line and end-of-file, and %backquote throws
away the exit status!  So I wrote

fn %Backquote sep cmd {
  let (tmp = /tmp/es.Bq.$pid) {
    let (status = <={$cmd >$tmp}) {
      result ($status ``$sep{cat $tmp; /bin/rm -f $tmp})}}}

which acts like %backquote except it prepends the exit status to the
front of the return value.  Maybe this functionality should be in the
primitive, and then the common ` and `` forms could chop the first
element off the list?  My solution certainly forks and execs a lot...

As an example, I now have a read function as follows:

fn read {
  let (ans = <={%Backquote \n {line}}) {
    if {result $ans(1)} {
       if {~ $#ans 1} {result ''} {result $ans(2 ...)}} {result ()}}}

It returns () on eof and '' on an empty line.  I will be happy to
receive comments on the programming style!  I am used to programming
in Scheme (and hopefully it shows), but es is new to me, and maybe I
am being needlessly circumspect.

Now, ls | while {x=<=read;!result $x} {echo ... $x} does work, but it
is slow as molasses.  We really need that read builtin, methinks.

- Harald