home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / unshar.prl < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  1.8 KB

  1. From decwrl!purdue!gatech!cwjcc!hal!ncoast!allbery Fri Nov 18 20:44:39 PST 1988
  2. Article 727 of comp.sources.misc:
  3. Path: granite!decwrl!purdue!gatech!cwjcc!hal!ncoast!allbery
  4. From: grady@fxgrp.UUCP (Steven Grady)
  5. Newsgroups: comp.sources.misc
  6. Subject: v05i050: perl version of unshar
  7. Message-ID: <865@fxgrp.UUCP>
  8. Date: 15 Nov 88 00:32:17 GMT
  9. Sender: allbery@ncoast.UUCP
  10. Reply-To: grady@fxgrp.UUCP (Steven Grady)
  11. Organization: FX Development Group, Inc., Mountain View, CA
  12. Lines: 47
  13. Approved: allbery@ncoast.UUCP
  14.  
  15. Posting-number: Volume 5, Issue 50
  16. Submitted-by: "Steven Grady" <grady@fxgrp.UUCP>
  17. Archive-name: unshar.perl
  18.  
  19. Quick and dirty, but it works.  Additional features/bug-fixes welcome.
  20.  
  21. #! /usr/bin/perl
  22.  
  23. # usage: unshar [-v] file ... 
  24.  
  25. # Assumes the files are well-defined.
  26.  
  27. # For reasons unclear to me, the eof check prevents the
  28. # last line in the file from being executed.  This is not
  29. # a problem for most shar files.
  30.  
  31. do 'getopt.pl';
  32. do Getopt();
  33.  
  34. if ($opt_v) {
  35.     open(out, ">&stdout") || die "Can't open >&stdout";
  36. } else {
  37.     open(out, ">/dev/null") || die "Can't open >/dev/null";
  38. }
  39.  
  40. # Lots of clever changes cold be made -- find the '#!' line and
  41. # execute the program; figure out what kind of file it is
  42. # if it turns out not to be a shar; etc..  Currently it is functional.
  43.  
  44. open(sh, "|/bin/sh") || die "Can't open |/bin/sh";
  45. select(out);
  46. while (<>) {
  47.     # Give up on the shell when we see "exit 0" or at the end of the file 
  48.     if (eof || /^exit 0/) {
  49.     select(out) || die "Can't select out";
  50.     close(sh) || die "Can't close sh";
  51.     open(sh, "|/bin/sh") || die "Can't open |/bin/sh";
  52.  
  53.     # start a new shell after seeing either "/bin/sh" or "cut here"
  54.     } elsif (m#/bin/sh# || /cut here/i) {
  55.     select(sh) || die "Can't select sh";
  56.  
  57.     # otherwise, spew the output to current selection (shell, stdout, /dev/null)
  58.     } else {
  59.     print;
  60.     }
  61. }
  62.  
  63.  
  64.