> >             if strsub(s,i,i)=="/" then
> 
> Shouldn't the "/" in strfindlast be the parameter c?

Yes, I've since updated and fixed a few things, it is still pretty rough.

Here is the current common.lua:

function bkm(s)
        last = strsub( s, -1)
        if last=="b" then
                return strsub(s,1,-2) * 512
        end
        if last=="k" then
                return strsub(s,1,-2) * 1024
        end
        if last=="m" then
                return strsub(s,1,-2) * 1048576
        end
        return tonumber(s)
end

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)==c then
                        return i
                end
        end
        return nil
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, options)
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) == "-" and strsub(arg[idx],2,2)
                then
                        curopt = strsub(arg[idx],2,2)
                        if optdef[curopt]
                        then
                                if optdef[curopt] > 0
                                then
                                        -- support for 'cut -f1' here 
                                        if options and options.filesonly then
                                                arg[idx] = nil
                                        end
                                        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
                        if options and options.filesonly then
                                arg[idx] = nil
                        end
                else
                        break
                end
        idx = idx + 1   
        end
        if options and options.filesonly then
                arg.n=nil
                arg[0]=nil
        end
        return opts, idx, mysteryopt
end

Reply via email to