Skip site navigation (1)Skip section navigation (2)
Date:      04 Jul 1999 14:09:47 +0200
From:      Dag-Erling Smorgrav <des@flood.ping.uio.no>
To:        Jamie Howard <howardjp@wam.umd.edu>
Cc:        freebsd-hackers@FreeBSD.ORG, tech-userlevel@netbsd.org, tech@openbsd.org
Subject:   Re: Repalcement for grep(1)
Message-ID:  <xzp4sjkwr90.fsf@flood.ping.uio.no>
In-Reply-To: Jamie Howard's message of "Sat, 3 Jul 1999 15:18:07 -0400 (EDT)"
References:  <Pine.GSO.4.10.9907031513150.17705-100000@rac9.wam.umd.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
Jamie Howard <howardjp@wam.umd.edu> 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 -<num>, set $lead and $trail to
<num>. Now for the search algorithm in Perl:

$toprint = 0;
@queue = qw();

while ($line = <INPUT>) {
    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
-<num> 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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzp4sjkwr90.fsf>