[comp.lang.perl] print spool creation

dglo@ADS.COM (Dave Glowacki) (04/12/91)

Here's 'setup-print-spools', which is what we use to create all the
printer directories and files.  It's *real* handy for setting up new
workstations.

The script checks for the spool directory (sd), error logging file (lf)
and output device name (lp) and creates them (owned by daemon) if they
don't exist.

The output device is created as a null file, which is the right thing to
do for remote printers but probably not what you want if the printer is
local.

If you wanted to be anal, you could probably check for the existance of
any specified filters (af,cf,df,gf,etc.), but this creates everything a
Sun workstation needs to print.
--
#!/usr/local/bin/perl

# common location of printer capability file
#
$PRINTCAP = '/etc/printcap';

# owner of printer spools and files
#
$PRINTOWNER = 'daemon';

##############################################################################
#
#	Process a line from a file with #-style comments and \-continued lines
#
#	parameters:
#		$_		next line from the file
#
#	external variables:
#		MultiLineBuffer	buffer holding parts of multiple lines
#
#	return value:
#		'' or the next full line
#
$MultiLineBuffer = '';
$CommentAnywhere = 0;
sub process_std_file {
  local($_) = @_;
  local($i, $l, $s, $t);

  # lose trailing NEWLINE
  #
  s/\n$//;

  # get rid of any comment
  #
  if ($CommentAnywhere) {
    s/#.*$//;
  } else {
    s/^#.*$//;
  }

  # lose excessive whitespace
  #
  s/^[ \t]*//;		# chop off leading blanks
  s/[ \t]*$//;		# chop off trailing blanks
  s/[ \t]+/ /;		# lose any lengthy internal whitespace

  # if line ends with continuation character,
  # stick it in the buffer
  #
  if (s/\\$//) {
    $MultiLineBuffer = $MultiLineBuffer . $_;
    $_ = '';
  } else {
    $_ = $MultiLineBuffer . $_;
    $MultiLineBuffer = '';
  }

  # return line
  #
  $_;
}

# immediately flush STDOUT
#
$| = 1;

$CommentAnywhere = 0;
print 'Reading ',$PRINTCAP,' ';
$result = open(PRINTCAP, $PRINTCAP) ||
	die "Couldn't read printer capability file!\n";

while (<PRINTCAP>) {

  # get each entry in master printcap file as a single line
  #
  $line = &process_std_file($_);
  if ($line) {

    # process each variable in this entry
    #
    $name = $spooldir = $lockfile = $logfile = '';
    foreach $_ (split(/:/, $line)) {

      if (! $name) {
	# get default printer name
	#
	s/\|.*$//;
	$name = $_;
      } else {
	if (/^sd=/) {
	  # spool directory
	  #
	  $sd = $';
	}
	elsif (/^lf=/) {
	  # log file
	  #
	  $lf = $';
	}
	elsif (/^lp=/) {
	  # lock file
	  #
	  $lp = $';
	}
      }
    }

    # save this printer name on the list
    #
    if ($name) {
      push(@printer, $name);
      $exception{$name} = 1 if ($sd !~ /^\/usr\/spool\/$name$/);
      $spooldir{$name} = $sd if ($sd);
      $logfile{$name} = $lf if ($lf);
      $lockfile{$name} = $lp if ($lp);
      if (defined($exception{$p})) {
	print '*';
      } else {
	print '.';
      }
    } else {
      print '!';
    }
  }
}
close(PRINTCAP);
print " done\n";

# get UID & GID for $PRINTOWNER
#
($junk,$junk,$d_uid,$d_gid) = getpwnam($PRINTOWNER);

foreach $p (@printer) {
  print $p;
  if (defined($spooldir{$p})) {
    $spooldir = $spooldir{$p};
    if ( ! -d $spooldir) {
      print '.';
      if (mkdir($spooldir, 0x2775) == 0) {
	print STDERR "mkdir '$spooldir' failed: '$!'\n";
      } else {
        print '.';
	$result = chown $d_uid,$d_gid,$spooldir;
	print STDERR "Couldn't change owner of '$spooldir' to $PRINTOWNER\n"
		if ($result != 1);
      }
    }
    $lprlock = $spooldir . '/lock';
    if ( ! -e $lprlock) {
      print '.';
      $fakefile = '>' . $lprlock;
      $result = open(FILE, $fakefile);
      if ($result) {
	print '.';
	$result = chown $d_uid,$d_gid,$lprlock;
	print STDERR "Couldn't change owner of '$lprlock' to $PRINTOWNER"
		if ($result != 1);
      } else {
	print STDERR "Couldn't create lockfile '$lprlock' for $p\n";
      }
    }
    if (defined($logfile{$p})) {
      $logfile = $logfile{$p};
      if ( ! -e $logfile) {
        print '.';
	$result = open(FILE, ">$logfile");
	if ($result) {
	  close(FILE);
          print '.';
	  $result = chown $d_uid,$d_gid,$logfile;
	  if ($result != 1) {
	    print STDERR "Couldn't change owner of '$logfile'";
	    print STDERR " to $PRINTOWNER\n";
	  }
	} else {
	  print STDERR "Couldn't create logfile '$logfile' for $p\n";
	}
      }
    }
    if (defined($lockfile{$p})) {
      $lockfile = $lockfile{$p};
      if ( ! -e $lockfile) {
        print '.';
	$result = open(FILE, ">$lockfile");
	if ($result) {
	  close(FILE);
          print '.';
	  $result = chown $d_uid,$d_gid,$lockfile;
	  print STDERR "Couldn't change owner of '$lockfile' to $PRINTOWNER\n"
		if ($result != 1);
	} else {
	  print STDERR "Couldn't create lockfile '$lockfile' for $p\n";
	}
      }
    }
  }
  print " done\n";
}
--
Dave Glowacki          dglo@ads.com          Advanced Decision Systems