>  A bit closer to what <insert favorite deity> intended :-)

Closer to what I (as a Java programmer) would think is ideal, also, but
not valid Lua, more is the pity.  In Lua, "if" requires "then" and "end",
blocks must use "do" and "end" not curly braces, there unary not must be
"not" rather than "!",  the logical or must be "or" rather than "||".  I
have to admit I am with you that C is the One True Language, certainly
awk (considering the source, no suprise) is more C like then Lua.

I think the evidence is overwhelming at this point that Lua will be a
better choice than mawk,  for example, the "tee" program in awk wouldn't
even allow nulls in the input, is clearly uglier, and the Lua interpreter
that runs it is 15K smaller.  That is without considering that the sockets
and md5 and regex libraries for Lua may allow me to add a real ftp client,
with upload as well as download, for example, at a net space gain.

Here is my old tee-in Awk:
----------------------------------------------------------------------
#!/bin/sh
awk '
BEGIN{
        for (i = 1; i < ARGC; i++)
                copy[i] = ARGV[i]

        if (ARGV[1] == "-a") {
                append = 1
                delete ARGV[1]
                delete copy[1]
                ARGC--
        }
        if (ARGC < 2) {
                print "usage: tee [-a] file ..." > "/dev/stderr"
                exit 1
        }
        ARGV[1] = "-"
        ARGC = 2
}
{
        if (append)
                for (i in copy)
                        print >> copy[i]
        else
                for (i in copy)
                        print > copy[i]
        print
}
END{
        for (i in copy)
                close(copy[i])
}' -- "$@"
----------------------------------------------------------------------
And in Lua:
----------------------------------------------------------------------
#!/usr/bin/lua -f
s=arg[0] while strsub(s,-1)~="/" do s=strsub(s,1,-2) end dofile(s.."common.lua")
opts,narg,myst=getopts({a=0})
mode=opts.a and "a" or "w"
for i=0, narg-1 do arg[i]=nil end
if not arg[narg] then _ALERT("ERROR\n") exit(-1) end
for a,b in arg do arg[a]=openfile(b,mode) end
while 1 do
        B=read(8192)
        if not B then break end
        checkio(write(B))
        for a,b in arg
        do checkio(write(b,B)) end
end
for a,b in arg
do closefile(arg[a]) end
----------------------------------------------------------------------
P.S. for those of you who are busily writing Luas to add to tomsrtbt,
here is the current state of the common.lua I'm using in 'tee' above:
----------------------------------------------------------------------
-- strfindlast, getopts, dirname, basename, 

function fail(s)
        _ALERT("\nERROR\n")
        exit(-1)
end

function checkio(c,s)
        if not c then
                fail(s)
        end
end

function strfindlast(s,c)
        -- for i=-1, -strlen(s), -1
        for i=strlen(s), 1, -1
        do
                if strsub(s,i,i)=="/" then
                        return i
                end
        end
end

function dirname(s)
a=strsub(arg[1],1,strfindlast(s,"/")-1)
if a=="" then a="/" end
if a==nil then a="." end
return a
end

function basename(s)
a=strsub(arg[1],strfindlast(s,"/")+1)
if a=="" or a==nil then a=arg[1] end
return a
end

-- takes a table with (optchar, numarg)
-- returns a table with (optchar, value ("" if none))
--  , index of first non-option argument 
--
-- initially only supports "cut -f 1" or "cut -c 1-7" type arg
--
function getopts (optdef)
arg.n=nil
for a,b in optdef
do
end
        idx=1
        local opts={}
        lim=getn(arg)
        while (arg[idx])
        do
                if arg[idx] == "--" then
                        break
                end
                if strsub(arg[idx],1,1) == "-"
                then
                        curopt = strsub(arg[idx],2,2)
                        if optdef[curopt]
                        then
                                if optdef[curopt] > 0
                                then
                                        -- support for 'cut -f1' here 
                                        idx = idx + 1
                                        -- support for 'prog -x asdf qwer' here
                                        opts[curopt] = arg[idx]
                                else
                                        opts[curopt] = ""
                                        -- support for 'rm -rf' type arg here
                                end
                        else
                                mysteryopt = strsub(arg[idx],2)
                        end
                else
                        break
                end
        idx = idx + 1   
        end
        return opts, idx, mysteryopt
end

Reply via email to