home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-osu / pcap2lp.shar < prev    next >
Encoding:
Text File  |  1991-04-09  |  4.9 KB  |  159 lines

  1. Path: tut.cis.ohio-state.edu!ucbvax!agate!bionet!uwm.edu!ogicse!hakanson
  2. From: hakanson@ogicse.ogi.edu (Marion Hakanson)
  3. Newsgroups: comp.sys.hp,comp.lang.perl
  4. Subject: utility: printcap to lpadmin (HP-UX)
  5. Message-ID: <19890@ogicse.ogi.edu>
  6. Date: 10 Apr 91 22:13:10 GMT
  7. Organization: Oregon Graduate Institute (formerly OGC), Beaverton, OR
  8. Lines: 147
  9. Xref: tut.cis.ohio-state.edu comp.sys.hp:8557 comp.lang.perl:5088
  10.  
  11.  
  12. Below find "pcap2lp" -- a Perl (3.0.44 or later) program which parses
  13. a printcap file and spits out a shell script of lpadmin commands which
  14. will recreate all the printers and aliases in the printcap file.
  15.  
  16. It is of limited use in that it only deals with remote printers (but
  17. most of our machines have none attached directly, so this covers a lot
  18. of cases), and in particular it assumes the printer models used by
  19. HP-UX 7.0 for supporting remote printers.  It certainly saves a lot of
  20. work in keeping our HP machines "in sync" with the BSD world they live
  21. in here.  Comments welcome.
  22.  
  23. -- 
  24. Marion Hakanson         Domain: hakanson@cse.ogi.edu
  25.                         UUCP  : {hp-pcd,tektronix}!ogicse!hakanson
  26.  
  27. ===================================================================
  28.  
  29. This script depends on a patched perl termcap.pl library file:
  30.  
  31. ===================================================================
  32. RCS file: RCS/termcap.pl,v
  33. retrieving revision 1.1
  34. diff -r1.1 termcap.pl
  35. 24c24
  36. <     if (index($TERMCAP,"|$TERM|") < $[) {
  37. ---
  38. >     if ($TERMCAP !~ /(^|\|)$TERM[:\|]/) {
  39. 36c36
  40. <         if (/\\|$TERM[:\\|]/) {
  41. ---
  42. >         if (/(^|\\|)$TERM[:\\|]/) {
  43. ===================================================================
  44. #!/usr/bin/perl
  45. ;#
  46. ;# $Id: pcap2lp,v 1.3 91/03/21 17:07:11 hakanson Exp $
  47. ;#
  48. ;# Perl program to take BSD printcap entries for remote printers and
  49. ;# produce SysV-style lpadmin commands to configure the same printers.
  50. ;#   Marion Hakanson (hakanson@cse.ogi.edu)
  51. ;#   Oregon Graduate Institute of Science and Technology
  52. ;#
  53. ;# Note that this is designed for HP-UX 7.0, which supports the BSD-style
  54. ;# lpd protocol for remote printing, but via a Sys-V interface.  It expects
  55. ;# appropriate model files to be in place.
  56.  
  57. ($ZERO = $0) =~ s#.*/##;    # Save for later
  58. $[ = 0;
  59.  
  60. unless ( $#ARGV == 0 ) {
  61.   print STDERR "usage: $ZERO <printcap-file>\n";
  62.   exit 1;
  63. }
  64.  
  65. $PRINTCAP = $ARGV[0];
  66.  
  67. ;# Need a full path for TERMCAP env. variable.
  68. if ( $PRINTCAP !~ m#^/# ) {
  69.   chop($cwd = `pwd`) || die "Can't find current directory: $!, aborted";
  70.  
  71.   if ( $PRINTCAP =~ m#.*/# ) {
  72.     chop($head = $&);        # remove trailing '/'
  73.     $tail = $';
  74.     chdir($head) || die "Can't chdir to $head: $!, aborted";
  75.     chop($head = `pwd`) || die "Can't find path of $head: $!, aborted";
  76.     chdir($cwd) || die "Can't chdir back to $cwd: $!, aborted";
  77.   } else {
  78.     $head = $cwd;
  79.     $tail = $PRINTCAP;
  80.   }
  81.   $PRINTCAP = $head . '/' . $tail;  
  82. }
  83.  
  84.     #print STDERR "$ARGV[0] -> $PRINTCAP\n";
  85. open(PRINTCAP, "<$PRINTCAP") || die "Can't open $PRINTCAP: $!, aborted";
  86.  
  87. require 'termcap.pl';        # Load the library routine.
  88. $ospeed = -1;            # termcap.pl wants this to be nonzero.
  89.  
  90. $ENV{'TERMCAP'} = $PRINTCAP;    # Tgetent() will read this file.
  91.  
  92. ;# This outer loop is the same as that in Tgetent().
  93. ;# We want to find all the printcap entries in the file, at which
  94. ;# point we'll make multiple passes through, parsing each one.
  95.  
  96. print "#!/bin/sh\n";
  97. print "\n";
  98. print "# May want to remove all printers:\n";
  99. print "#   cd /usr/spool/lp/member\n";
  100. print "#   for i in *\n";
  101. print "#     do /usr/lib/lpadmin -x\$i\n";
  102. print "#   done\n";
  103.  
  104. while ( <PRINTCAP> ) {
  105.   next if /^#/;        # comment
  106.   next if /^\t/;    # continuation line
  107.  
  108.   chop;            # strip newline
  109.   s/:.*$//;        # keep only first entry
  110.     #print STDERR "\n$_\n";
  111.   @printers = split(/\|/);
  112.  
  113.     #$, = ' '; print STDERR @printers; print STDERR "\n";
  114.  
  115.   # Throw away the last one if it contains whitespace.
  116.   pop(@printers) if ( $printers[$#printers] =~ /\s/ );
  117.  
  118.     #$, = ' '; print STDERR @printers; print STDERR "\n";
  119.  
  120.   # But printcap(5) says the second name is the most commonly
  121.   # used one, but we will use the first one instead.
  122.  
  123.   next if ( $#printers < 0 );    # Must be at least one name
  124.  
  125.   $primary = $printers[0];
  126.  
  127.   &Tgetent($primary);        # Sets %TC array
  128.  
  129.     #while ( ($key, $val) = each %TC ) {
  130.     #  print STDERR "$key $val\n";
  131.     #}
  132.  
  133.   next unless ( $TC{'rm'} );    # skip if not remote
  134.  
  135.   $TC{'rp'} = $primary unless ( $TC{'rp'} );
  136.  
  137.   foreach $printer ( @printers ) {
  138.     $cmd  = '/usr/lib/lpadmin ';
  139.     $cmd .= "-p$printer ";
  140.     $cmd .= '-mrmodel -v/dev/null -orc -ocmrcmodel -osmrsmodel ';
  141.     $cmd .= "-orm$TC{'rm'} ";
  142.     $cmd .= "-orp$TC{'rp'} ";
  143.     $cmd .= '-ob3 ';        # Don't want this if remote is HP!
  144.  
  145.     print "$cmd\n";
  146.   }
  147. }
  148.  
  149. print "# May want a default printer:\n";
  150. print "#   /usr/lib/lpadmin -dlp\n";
  151. print "# May want to enable all printers:\n";
  152. print "#   cd /usr/spool/lp/member\n";
  153. print "#   enable *\n";
  154. print "#   /usr/lib/accept *\n";
  155.  
  156. exit(0);
  157. ===================================================================
  158.  
  159.