diff --git a/amogus.h b/amogus.h index 714565b..4cab2c3 100644 --- a/amogus.h +++ b/amogus.h @@ -1 +1,15 @@ -int entry(char* regex, char** output); +// int entry(...) +// +// Parse regex. Find matching string. +// If found: +// call callback( +// matched string (only valid before callback return), +// length of the string including NUL delimiter, +// data passed in the data argument +// ) +// return 0 +// Else: +// return 1 + +typedef void (entry_callback)(char* match, int size, void* data); +int entry(char* regex, entry_callback *callback, void *data); diff --git a/meow.s b/meow.s index 6addb5e..48e1f59 100644 --- a/meow.s +++ b/meow.s @@ -10,11 +10,11 @@ _start: pop %rax mov %rax,argv(%rip) cmp $1,%rdi - jg endif + jg _start_endif mov $1,%rdi call usage jmp _exit -endif: + _start_endif: mov $0,%rdi jmp _exit @@ -58,17 +58,33 @@ write: mov $1,%rdi syscall ret + help0: .ascii "Usage: " .set help0l, .-help0 help1: .ascii " regex0 [regex1 ...]\nstdout: NUL-separated list of matching strings\n" .set help1l, .-help1 + +# rdi - exit code _exit: mov $0x3c,%rax syscall ret + entry: + push %r12 + sub $8,%r12 + mov %rsp,%r12 + entry_parse_begin: + test %rdi,%rdi + jnz entry_parse_end + mov %rdi,%rcx + inc %rdi + entry_parse_end: + mov %r12,%rsp + add $8,%r12 + pop %r12 ret .bss @@ -76,5 +92,3 @@ argc: .zero 8 argv: .zero 8 -buf: - .zero 65536 diff --git a/test.c b/test.c index c2fee51..87943c5 100644 --- a/test.c +++ b/test.c @@ -1,9 +1,17 @@ #include #include +#include +#include + +void callback(char* str, int size, void* data) { + char** out = (char**)data; + *out = (char*)malloc(size); + memcpy(str, *out, size); +} int main() { char* match; - if(!entry("meow.*|m(r(r(p..*)))",&match)) + if(!entry("meow.*|m(r(r(p..*)))",callback,&match)) return 1; printf("%s",match); }