: usage ( -- ) "Usage: grep -{ic} " ; : explode_pop ( x1 .. xN N -- ) dup 0 = if pop exit else swap pop 1 - explode_pop then ; : say ( s -- ) me @ swap notify ; : ambi ( -- s ) "Grep> Source specified is ambiguous" ; : unknown ( -- s ) "Grep> Source specified is unknown" ; : illegal ( -- s ) "Grep> Source specified is illegal" ; : is_wizard? ( d -- i ) me @ "C" flag? me @ "G" flag? me @ #1 dbcmp or or if 1 else 0 then ; : grep_props ( i s s i d s i -- i s s i ) propfirst begin while 5 pick "i" instr if tolower then over over getpropstr 5 pick instr over 6 pick instr or if over intostr "#" swap strcat "/" strcat over strcat ":" strcat 3 pick 3 pick getpropstr strcat say then propnext loop ; : do_grep_loop ( i s s i i -- ) 1 for dup dbref dup ok? if grep_props then pop loop ; : determine_loop_size ( s s s -- s s s i i ) swap rot dup "*" strcmp not if is_wizard? if 0 db_top 1 - exit else illegal say -1 -1 exit then then dup "#" 1 strncmp not if dup atoi dbref owner me @ dbcmp is_wizard? or if dup 1 strcut swap pop atoi dup exit else illegal say -1 -1 exit then then dup match dup #-2 dbcmp if pop -1 -1 ambi say exit then dup #-1 dbcmp if pop -1 -1 unknown say exit then dup owner me @ dbcmp is_wizard? or if atoi dup exit else illegal say pop -1 -1 exit then ; : do_grep ( s -- ) " " explode pop dup strlen 3 > if 3 explode_pop usage say exit then dup "-" 1 strncmp if 3 explode_pop usage say exit then dup strlen 3 = if dup dup "i" instr swap "c" instr and not if 3 explode_pop usage say exit then then dup strlen 2 = if dup dup "i" instr swap "c" instr or not if 3 explode_pop usage say exit then then dup "i" instr if swap tolower swap then determine_loop_size dup -1 = if 5 explode_pop exit then rot pop 0 -5 rotate do_grep_loop pop "c" instr if dup intostr " match found." strcat swap 1 = not if "matches" "match" subst then say else pop then ; : main ( s -- ) .sstrip dup "" strcmp not if pop usage say exit then dup " " explode dup 2 = if explode_pop "- " swap strcat do_grep exit else explode_pop then dup " " explode dup 3 = if explode_pop do_grep exit else explode_pop then pop usage say ;