12.18. Using Programs To Get Printcap Information

In the lpd.conf file you can specify:

    printcap_path=|program
This will cause the LPRng software to execute the specified program, which should then provide the printcap information. The program is invoked with the standard filter options, and has the name of the printcap entry provided on STDIN. The filter should supply the printcap information on stdout and exit with a 0 (success) error code. By convention, the printcap name 'all' requests a printcap entry that lists all printers.

This technique has been used to interface to the Sun Microsystem NIS and NIS+ databases with great success. By having the invoked program a simple shell script or front end to the nismatch or ypmatch programs, the complexity of incorporating vendor specific code is avoided.

12.18.1. How to use NIS and LPRng

This note is based on material sent to the lprng@lprng.com mailing list by Paul Haldane .

We generally don't use NIS for printcap files (we've moved to hesiod) but I can show you what we've done in the past.

The input to NIS is a normal printcap file:

    # Classical printcap entry
    lp23a|lp23|lp|main printhost printer - KB, EUCS front Door:\
            :lp=lp23a@printhost:\
            :sd=/var/spool/lpr/lp23a:
    
    #lprng printcap entry
    lplabel|lpl|TEST - Labels printer:
            :lp=:rm=printhost:rp=lplabel:
            :sd=/var/spool/lpr/lplabel:
            :rg=lpadm:mx=1:


To build the NIS printcap.byname map we add the following to the NIS makefile (along the other bits and pieces that the makefile needs to know about a new map).

    PRINTCAP=${sysconfdir}/printcap
    # warning : [  ] is actually [<space><tab>] in the script
    printcap.time: $(PRINTCAP) Makefile
      if [ -f $(PRINTCAP) ]; then \
        sed < $(PRINTCAP) \
          -e 's/[   ][  ]*$$//' -e '/\\$$/s/\\$$/ /' \
        | awk '$$1 ~ /^#/{next;} $$1 ~ /^[:|]/ {printf "%s", $$0; next;} \
            {printf "\n%s", $$0 }' \
        | sed -e 's/[   ]*:[  ]*:/:/g' -e 's/[  ]*|[  ]*/|/g' \
          -e '/^[   ]*$$/d' > .printcap.$$$$; \
        cat .printcap.$$$$; \
        if [ $$? = 0 -a -s .printcap.$$$$ ]; then \
          awk <.printcap.$$$$ '{ FS=":"; OFS="\t"; } { \
              n = split($$1, names, "|"); \
              for (i=1; i<=n; i++) \
                  if (length(names[i]) > 0 \
                  && names[i] !~ /[ \t]/) \
                      print names[i], $$0; \
          }' | $(MAKEDBM) - $(YPDBDIR)/$(DOM)/printcap.byname; \
          awk <.printcap.$$$$ '{ FS=":"; OFS="\t"; } { \
              n = split($$1, names, "|"); \
              if (n && length(names[1]) > 0 && names[1] !~ /[ \t]/) \
                  print names[1], $$0; \
          }' | $(MAKEDBM) - $(YPDBDIR)/$(DOM)/printcap.bykey; \
          rm -f .printcap.$$$$; \
          touch printcap.time; echo "updated printcap"; \
        fi \
      fi
      @if [ ! $(NOPUSH) -a -f $(PRINTCAP) ]; then \
          $(YPPUSH) printcap.byname; \
          $(YPPUSH) printcap.bykey; \
          touch printcap.time; echo "pushed printcap"; \
      fi


To specify that you want YP database rather than file access, use the following entry in your /etc/lpd.conf file:

    printcap_path |/usr/local/libexec/pcfilter


Put the following shell script in /usr/local/libexec/pcfilter

    #!/bin/sh
    #/usr/local/libexec/filters/pcfilter
    read key
    # specify the full pathname to the ypmatch program
    # the location depends on the version of Solaris or your
    # system install
    /full/pathname/to/ypmatch "$key" printcap.byname


You can test this by using:

    h4: {314} # lpc client pr
    pr
     :lp=pr@server
    h4: {315} # lpc server pr
    pr
     :lp=pr@server


12.18.2. How to use NIS and LPRng - Sven Rudolph

     Date: Wed, 11 Sep 1996 00:11:02 +0200
    From: Sven Rudolph <sr1@os.inf.tu-dresden.de>
    To: lprng@lprng.com
    Subject: Using :oh=server: with NIS


When I use a cluster-wide printcap, I want the entries for each printer to appear, e.g.:

    ---------- start of printcap snippet
    lp1
     :lp=lp1@server
    lp2
     :lp=lp2@server
    lp1
     :server:oh=servername
     :sd=/var/spool/lpd/lp1
     :lp=/dev/lp1
     :mx=0
    ---------- end of printcap snippet


When I create a NIS map out of this the printer name is used as a key and must be unique. The NIS makedbm will drop all but the last entry for each printer. This makes the printer on the clients unavailable. I solved this by a hack where the second entry is called lp1.server and the NIS client script has to request the right entry.

  1. Assumptions

    Perl is available at the YP server in /usr/bin/perl. A Bourne Shell is available at all clients in /bin/sh The printcap that is to be exported is in /etc/printcap. The printcap is written in the new format. In the examples the printer is called lp1.

  2. Add the following to your YP Makefile (/var/yp/Makefile) on the YP server (these lines are for Debian GNU/Linux, other systems might require other modifications):

        ---------- start of /var/yp/Makefile snippet
        PRINTCAP  = /etc/printcap
        printcap: $(PRINTCAP)
            @echo "Updating $@..."
            $(CAT) $(PRINTCAP) | \
                /usr/lib/yp/normalize_printcap | $(DBLOAD) -i $(PRINTCAP) \
                -o $(YPMAPDIR)/$@ - $@
            @if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
            @if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi
        ---------- end of /var/yp/Makefile snippet
    


  3. Install the programs match_printcap and normalize_printcap in the /usr/lib/yp directory; normalize_printcap is only required on the YP server. The normalize_printcap processes only the LPRng printcap format.

        ---------- start of /usr/lib/yp/normalize_printcap
        #! /usr/bin/perl
        $debug = 0;
        $line = "";
        $new = "";
        while (<>) {
            chomp;
            next if ( /^\s*\#.*/ );
            s/^\s*$//;
            next if ( $_ eq '' );
            print "new: " . $_ . "\n" if $debug;;
            if (/^\s/) { # continuation line
                $line = $line.$_;
                print "continued: $line\n" if $debug;
                next;
            } else {
                $line =~ s/\s+\:/:/g;
                $line =~ s/\:\s+/:/g;
                $line =~ s/\:\s*\:/:/g;
                print "line: $line\n" if $debug;
                push(@lines, $line) if $line;
                $line = $_;
            }
        }
        $line =~ s/\s+\:/:/g;
        $line =~ s/\:\s+/:/g;
        $line =~ s/\:\s*\:/:/g;
        push(@lines,$line) if $line;
        @lines = sort(@lines);
        foreach $line (@lines) {
            ($printers) = split(/\:/,$line);
            @printers = split(/\|/,$printers);
            foreach $printer (@printers) {
              $num{$printer}++;
              push(@allprinters,$printer);
              print "allprinters: @allprinters\n" if $debug;
              print $printer."_".$num{$printer}."\t$line\n";
            }
        }
        @pr = keys %num;
        print "printers @pr\n" if $debug;
        if ($#allprinters >=0) {
            print "all_1\tall:all=".join(",",@pr)."\n";
        }
        ---------- end of /usr/lib/yp/normalize_printcap
    


    The result of processing the sample printcap file is:

        lp1_1 lp1:lp=lp1@server
        lp1_2 lp1:server:oh=servername:sd=/var/spool/lpd/lp1:lp=/dev/lp1:mx=0
        lp2_1 lp2:lp=lp2@server
        all_1 all:all=lp1,lp2
    


    Observe that each of the real printer entries has a key consisting of the printer name with a numerical suffix. This leads to the following method of extracting the printcap information using ypmatch:

        ---------- start of /usr/lib/yp/match_printcap
        #!/bin/sh
        read p
        n=1
        # specify the full pathname to ypmatch - this depends on your
        # OS version and installation
        while /full/pathname/to/ypmatch "${p}_${n}" printcap 2>/dev/null; do
            n=`expr $n + 1`
        done
        ---------- end of /usr/lib/yp/match_printcap
    


  4. Now test the YP arrangement:

        h4: {316} #  cd /var/yp; make
            # this should create the printcap map
        h4: {317} #  ypcat printcap
            # should provide the whole normalized printcap
        h4: {318} #  echo lp1 |/usr/lib/yp/match_printcap
            # yields lp1 printcap
    


  5. Modify the printcap_path entry in the lpd.conf file:

        printcap_path=|/usr/lib/yp/match_printcap
    


  6. Test the use of the printcap path entry:

        h4: {319} #  lpc client lp1 # shows the printcap for lp1
        h4: {320} #  lpc server lp1 # shows the printcap for lp1
    


  7. Restart the lpd server and check to see that it accesses the right printcap information. Use the same lpq command, and then try lpc printcap lp1.