home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5490 < prev    next >
Encoding:
Text File  |  1992-08-25  |  5.8 KB  |  180 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!sol.ctr.columbia.edu!destroyer!ubc-cs!dhami
  3. From: dhami@cs.ubc.ca (Mandeep S Dhami)
  4. Subject: Even more on fixing #!
  5. Message-ID: <1992Aug25.231300.6300@cs.ubc.ca>
  6. Summary: a small script.
  7. Keywords: #!, fixsc
  8. Sender: usenet@cs.ubc.ca (Usenet News)
  9. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  10. Date: Tue, 25 Aug 92 23:13:00 GMT
  11. Lines: 167
  12.  
  13. For my word on #!, I use the following script. It is a typical case of a
  14. script, where 10 lines of perl with all the error checking, userful ret.
  15. status, help message and RCS log becomes 100 lines long!
  16.  
  17. It scans my path for an executable with same name and substitues that
  18. in the #! line. options allow to specify new names, or arbitrary perl
  19. actions on the script name. As sometimes I am playing with my own
  20. copies/newer versions of standard programs, it is useful as it finds
  21. the first such executable on my path (and saves me a 
  22. $ perl -i~ -e 's/\#\!\s+oldname/\#\!newname/o if ($.==1)' filename # :-!).
  23. But I will type chmod +x filename(s) anyway, why not fixsc filename(s)
  24. instead.
  25.  
  26. use fixsc.pl -h for a help message :-)!
  27. shar file appended.
  28.  
  29. Mandeep.
  30. #------------------8<---- snip ------8<------- snip ----------------
  31. #! /bin/sh
  32. # This is a shell archive, meaning:
  33. # 1. Remove everything above the #! /bin/sh line.
  34. # 2. Save the resulting text in a file.
  35. # 3. Execute the file with /bin/sh (not csh) to create the files:
  36. #    fixsc.pl
  37. # This archive created: Tue Aug 25 15:46:21 1992
  38. # By:    Mandeep S Dhami ()
  39. export PATH; PATH=/bin:$PATH
  40. echo shar: extracting "'fixsc.pl'" '(3511 characters)'
  41. if test -f 'fixsc.pl'
  42. then
  43.     echo shar: will not over-write existing file "'fixsc.pl'"
  44. else
  45. sed 's/^X//' << \SHAR_EOF > 'fixsc.pl'
  46. X#!/cs/local/bin/perl -- -*-Perl-*-
  47. X$0=~s!^.*/([^/]+)$!$1!;        # for ps to display the script base name
  48. X                # instead of perl's full path
  49. X# 
  50. X# $Author: dhami $
  51. X# Mandeep S Dhami
  52. X# Univ. of BC, Vancouver.
  53. X#
  54. X# You may use it, modify it or sell it
  55. X# (as if!) at your own risk.
  56. X#
  57. X# $RCSfile: fixsc.pl,v $
  58. X# $Revision: 1.1 $
  59. X# $Date: 1992/08/25 22:44:23 $ 
  60. X#
  61. X# $Log: fixsc.pl,v $
  62. X# Revision 1.1  1992/08/25  22:44:23  dhami
  63. X# Initial revision
  64. X#
  65. X#
  66. X
  67. X$opt="hqi:cxa:n:";
  68. X$usage="$0 [-h][-qc][-i <ext>][-a <action>][-n <interpreter name>] <files>";
  69. X$help="\
  70. XThe Default action:
  71. X      For each filename, if the file begins with an \#\!, $0 finds
  72. X      the base name of the executable specified on that line and if
  73. X      an executable plain file by that name exists on the path, the
  74. X      new name is substituted in \#\! line in corresponding the output
  75. X      file.
  76. X
  77. XThe options:
  78. X  -h  for this help menu
  79. X  -q  quite mode, no error messages,
  80. X      execept in option parsing.
  81. X  -i  make backups with extension <ext>
  82. X      defaults to ~.
  83. X  -c  clobber backups if they exist (paranoid).
  84. X  -a  override the default action specified above and
  85. X      execute <action> on the interpreter name.
  86. X      It should return non zero for success for the
  87. X      warning message/return status to make any sense.
  88. X      (eg: for fixing /usr/bin/perl to /cs/local/bin/perl
  89. X           use 's/^\\/usr/\\/cs\\/local')
  90. X  -n  use the new name <name> for interpreter.
  91. X      overrides option -a <action>.
  92. X  -x  By default $0 changes mode for all files it creates
  93. X      to 0700. -x suppresses this action.
  94. X
  95. XThe return status:
  96. X   0  if no errors
  97. X   n  counts the number of errors it found in fixing
  98. X      all the files.\n\n";
  99. X
  100. X# Defaults
  101. X$opt_i="~";
  102. X$tmp="$0.$$";
  103. X
  104. Xrequire 'getopts.pl';
  105. Xif (! &Getopts($opt)) {&Uerror;}
  106. Xif ($opt_h) {&Help;}
  107. Xif ($#ARGV < $[) {&Warn("No files specified to fix\n"); &Uerror;}
  108. X
  109. X# Initialization
  110. X$opt_n || (@path=split(':',$ENV{'PATH'})); $err=0;
  111. X
  112. Xunlink($tmp);  # Just in case.
  113. Xforeach $i (@ARGV) {
  114. X    &Warn("$i$opt_i exists. Use -c option to clobber\n"), $err++, next
  115. X    if (-f $i.$opt_i && !$opt_c);
  116. X    &Warn("Can not open $i\n"), $err++, next
  117. X    unless (open(FILE,$i));
  118. X    &Warn("$i - does not begin with \#\!\n"), $err++, next
  119. X    unless (($_=<FILE>) && (/^\#\!\s*(\S+)\s+(.*)$/));
  120. X    $name=(defined($opt_n)?$opt_n:$1), $rest=$2;
  121. X    &Warn("Unsuccessful for $name\n"), $err++, next
  122. X    unless (!defined($opt_n) && &Action($name));
  123. X
  124. X    open(OUT,">$tmp") || &Error("Can not open tmp file $tmp\n");
  125. X    print OUT "\#\!$name $rest\n";
  126. X    print OUT $_ while (<FILE>);
  127. X    close OUT;
  128. X    &Warn("Can not rename $i to $i$opt_i\n"), unlink($tmp), $err++, next
  129. X    unless (rename($i,$i.$opt_i));
  130. X    &Warn("Can not make file $i, orignal in $i$opt_i\n"), $err++
  131. X    unless (rename($tmp,$i));
  132. X    unlink($tmp);
  133. X    &Warn("Unable to chmod to 700 for $i\n")
  134. X    unless ($opt_x || chmod 0700, $i);
  135. X}
  136. Xexit $err ;
  137. X
  138. Xsub Action {
  139. X    if (defined($opt_a)) {
  140. X    local($retval); $_=$_[0];
  141. X    $retval=eval $opt_a; $_[0]=$_;
  142. X    &Warn("Error in user-defined action '$opt_a'\n"), return 0  if $@;
  143. X    $retval;
  144. X    }
  145. X    else {
  146. X    $_[0]=~s!^.*\/([^/]+)$!$1!o;
  147. X    foreach (@path) {
  148. X        if (-x "$_/$_[0]" && -r _ && -f _) {
  149. X        $_[0]="$_/$_[0]";
  150. X        return 1;
  151. X        }
  152. X    }
  153. X    0;
  154. X    }
  155. X}
  156. X
  157. Xsub Error  {$opt_q || print STDERR "$0: @_"; exit 1;}
  158. Xsub Warn   {$opt_q || print STDERR "$0: @_"; 0;}
  159. Xsub Uerror {$opt_q || (&Warn("Usage - "), &Usage); exit 1;}
  160. Xsub Usage  {print STDERR "$usage\n"; 1;}
  161. Xsub Help   {print STDERR "USAGE: "; &Usage; 
  162. X        print STDERR $help; exit 0;}
  163. X
  164. X# End of file
  165. SHAR_EOF
  166. if test 3511 -ne "`wc -c < 'fixsc.pl'`"
  167. then
  168.     echo shar: error transmitting "'fixsc.pl'" '(should have been 3511 characters)'
  169. fi
  170. chmod +x 'fixsc.pl'
  171. fi # end of overwriting check
  172. #    End of shell archive
  173. exit 0
  174. #------------------8<---- snip ------8<------- snip ----------------
  175. --
  176. __________________________________________________________________
  177. Mandeep S Dhami.                  Tel: Home : 596-3564
  178. 11497 87-A Ave.                        Off  : 822-8572
  179. N.Delta, BC.                           Lab  : 822-2895
  180.