[comp.sources.misc] v05i050: perl version of unshar

grady@fxgrp.UUCP (Steven Grady) (11/15/88)

Posting-number: Volume 5, Issue 50
Submitted-by: "Steven Grady" <grady@fxgrp.UUCP>
Archive-name: unshar.perl

Quick and dirty, but it works.  Additional features/bug-fixes welcome.

#! /usr/bin/perl

# usage: unshar [-v] file ... 

# Assumes the files are well-defined.

# For reasons unclear to me, the eof check prevents the
# last line in the file from being executed.  This is not
# a problem for most shar files.

do 'getopt.pl';
do Getopt();

if ($opt_v) {
    open(out, ">&stdout") || die "Can't open >&stdout";
} else {
    open(out, ">/dev/null") || die "Can't open >/dev/null";
}

# Lots of clever changes cold be made -- find the '#!' line and
# execute the program; figure out what kind of file it is
# if it turns out not to be a shar; etc..  Currently it is functional.

open(sh, "|/bin/sh") || die "Can't open |/bin/sh";
select(out);
while (<>) {
    # Give up on the shell when we see "exit 0" or at the end of the file 
    if (eof || /^exit 0/) {
	select(out) || die "Can't select out";
	close(sh) || die "Can't close sh";
	open(sh, "|/bin/sh") || die "Can't open |/bin/sh";

    # start a new shell after seeing either "/bin/sh" or "cut here"
    } elsif (m#/bin/sh# || /cut here/i) {
	select(sh) || die "Can't select sh";

    # otherwise, spew the output to current selection (shell, stdout, /dev/null)
    } else {
	print;
    }
}