[comp.lang.perl] xargs in perl?

brister@decwrl.dec.com (James Brister) (01/23/91)

Has anyone written a version of xargs in perl? Our binary versio is broken,
and before I frustrate myself by trying to write it in perl, I thought I
check here.

Mail or posting is fine, but mail will reduce net.usage. If you want a copy
of what I recieve from generous donors then send me mail.

Thanks.

James
--
James Brister                                           brister@decwrl.dec.com
DEC Western Software Lab., Palo Alto, CA    {uunet,sun,pyramid}!decwrl!brister

evans@decvax.DEC.COM (Marc Evans) (01/29/91)

In article <BRISTER.91Jan22163253@saratoga.decwrl.dec.com>, brister@decwrl.dec.com (James Brister) writes:
|> Has anyone written a version of xargs in perl? Our binary versio is broken,
|> and before I frustrate myself by trying to write it in perl, I thought I
|> check here.

Well, here is an implementation which isn't quite up to X/Open specs, but it
should be close enough for most peoples needs. Note the magic number 50 for
$maxarg. You can override this with the -n option, but you may want to adjust
the default value for use on your system. Also note that the -i option isn't
quite the same as X/Open defines, in that it doesn't take the optional argument
(I'm just being lazy and using Getopts).

- Marc
========8<========8<========cut=here========8<========8<========
#!/usr/bin/perl -P

# Catch for sh/csh on systems without #! ability
eval '(exit $?0)' && eval 'exec /usr/dec/perl -P -S $0 ${1+"$@"}"'
& eval 'exec /usr/dec/perl -P -S $0 $argv:q'
if 0;

#include "getopts.pl"

$program  = $0;
$program  =~ s?.*/??;

&Getopts('tpie:n:');

$shell  = defined($ENV{'SHELL'}) ? $ENV{'SHELL'} : '/bin/sh';
$cmd    = ($#ARGV >= 0) ? "@ARGV" : '/bin/echo';
$trace  = defined($opt_t) ? 1 : 0;
$prompt = defined($opt_p) ? 1 : 0;
$insert = defined($opt_i) ? 1 : 0;
$eof    = defined($opt_e) ? $opt_e : '_';
$maxarg = defined($opt_n) ? $opt_n : 50;

open(IN,"</dev/tty") || die("Unable to open /dev/tty : $!\n");
open(PIPE,"|$shell") || die("Unable to invoke $shell : $!\n");
$line = "$cmd";
$nline = 1;

while (<STDIN>)
{   chop;
    last if (/^$eof$/);
    next if (/^\s*$/ || ! &doprompt($_,$trace,$prompt));
    if ($insert) { $line =~ s/{}/$_/g; }
    else         { $line .= " $_"; }
    if ($insert || $nline == $maxarg)
    {   print PIPE "$line\n";
        $nline = 0;
        $line = "$cmd";
    }
    $nline++;
}
print PIPE "$line\n" if ($nline > 1);
close(PIPE);
close(IN);
exit(0);

sub doprompt
{   local($Line,$Trace,$Prompt) = @_;
    local($Rval,$Ans) = (1,'n');
    print STDOUT ($Prompt) ? "$Line ? " : "$Line\n" if ($Trace || $Prompt);
    if ($Prompt)
    {   chop($Ans=<IN>);
        $Rval = ($Ans =~ /^\s*[Yy]/) ? 1 : 0;
    }
    return($Rval);
}

-- 
===========================================================================
Marc Evans - WB1GRH - evans@decvax.DEC.COM  | Synergytics     (603)635-8876
      Unix and X Software Contractor        | 21 Hinds Ln, Pelham, NH 03076
===========================================================================