From owner-freebsd-hackers Sun Jul 4 5:10: 0 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from flood.ping.uio.no (flood.ping.uio.no [129.240.78.31]) by hub.freebsd.org (Postfix) with ESMTP id BC50414C87 for ; Sun, 4 Jul 1999 05:09:57 -0700 (PDT) (envelope-from des@flood.ping.uio.no) Received: (from des@localhost) by flood.ping.uio.no (8.9.3/8.9.1) id OAA83072; Sun, 4 Jul 1999 14:09:48 +0200 (CEST) (envelope-from des) To: Jamie Howard Cc: freebsd-hackers@FreeBSD.ORG, tech-userlevel@netbsd.org, tech@openbsd.org Subject: Re: Repalcement for grep(1) References: From: Dag-Erling Smorgrav Date: 04 Jul 1999 14:09:47 +0200 In-Reply-To: Jamie Howard's message of "Sat, 3 Jul 1999 15:18:07 -0400 (EDT)" Message-ID: Lines: 41 X-Mailer: Gnus v5.5/Emacs 19.34 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Jamie Howard writes: > I made the version in FreeBSD 4.0 my target except for -A num, -B num, -C, > -num, and -Z. These are not required by the Single Unix Specification or > POSIX and I felt they would bloat my code too significantly. I find those quite useful, and I don't see how they'd bloat your code a lot. You need a line @queue and a $toprint counter, as well as $lead and $trail counters. $regexp is the expression to search for, and $line is a scratch variable. Initialize by setting $lead to the -A argument, $trail to the -B argument; if you encounter -C, set $lead and $trail to 2; if you encounter -, set $lead and $trail to . Now for the search algorithm in Perl: $toprint = 0; @queue = qw(); while ($line = ) { if ($toprint) { print $line; --$toprint; } else { shift @queue if (@queue > $lead); push @queue, $line; } next unless ($line ~ m/$regexp/o); while (@queue) { print shift @queue; } $toprint = $trail; } This should be trivial to translate to C. The only non-trivial part of implementing this stuff is that you have to trick getopt() to make - work. You'll have to put a : at the start of your getopt() string and examine every argument getopt() complains about. Hope this helps... keep up the good work! DES -- Dag-Erling Smorgrav - des@flood.ping.uio.no To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message