# experimental string match with pcre
.pcc_sub _main prototyped
    .param var argv
.include "library/pcre.imc"
    .sym int argc
    argc = argv
    if argc < 3 goto usage

    .sym string s
    .sym string pat
    s = argv[1]
    pat = argv[2]
    print s
    print " =~ /"
    print pat
    print "/\n"
    .sym var PCRE_LIB
    .PCRE_INIT(PCRE_LIB)
    .sym string error
    .sym int errptr
    .sym var code
    .PCRE_COMPILE(pat, 0, code, error, errptr)
    $I0 = defined code
    unless $I0 goto match_err

    .sym int ok
    .sym var result
    .PCRE_MATCH(code, s, 0, 0, ok, result)
    if ok < 0 goto nomatch
    print ok
    print " match(es):\n"
    .sym int i
    i = 0
    .sym string match
lp: .PCRE_DOLLAR(s, ok, result, i, match)
    print match
    print "\n"
    inc i
    if i < ok goto lp
    end
nomatch:
    print "no match\n"
    end
match_err:
    print "error in regex: "
    #print error
    print "at: '"
    length $I0, pat
    $I0 = $I0 - errptr
    substr $S0, pat, errptr, $I0
    print $S0
    print "'\n"
    exit 1
usage:
    .sym string prog
    prog = argv[0]
    print prog
    print " string pattern\n"
    exit 1
.end

