1 #!/bin/sh
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License, Version 1.0 only
   7 # (the "License").  You may not use this file except in compliance
   8 # with the License.
   9 #
  10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  11 # or http://www.opensolaris.org/os/licensing.
  12 # See the License for the specific language governing permissions
  13 # and limitations under the License.
  14 #
  15 # When distributing Covered Code, include this CDDL HEADER in each
  16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  17 # If applicable, add the following below this CDDL HEADER, with the
  18 # fields enclosed by brackets "[]" replaced with your own identifying
  19 # information: Portions Copyright [yyyy] [name of copyright owner]
  20 #
  21 # CDDL HEADER END
  22 #
  23 #
  24 # Copyright (c) 1994, 1995, 1996 by Sun Microsystems, Inc.
  25 # All Rights Reserved
  26 #
  27 # ident "%Z%%M% %I%     %E% SMI"
  28 #
  29 #  Printcap <-> Printers.conf conversion utility...
  30 #
  31 #       Usage: conv_lpd [ -c (printers|printcap) ] [ -n ] (file)
  32 #
  33 
  34 TEXTDOMAIN="SUNW_OST_OSCMD"
  35 export TEXTDOMAIN
  36 
  37 USAGE=`gettext "Usage: conv_lpd [ -c (printers|printcap) ] [ -n ] (file)\n"`
  38 
  39 PATH=/usr/bin:/bin:/usr/sbin export PATH
  40 conversion="printers"
  41 namelist=0
  42 
  43 umask 022
  44 
  45 for i in $*
  46 do
  47         case $1 in
  48                 -c*)
  49                         conversion=$2;
  50                         shift ; shift ;
  51                 ;;
  52                 -n*)
  53                         namelist=1;
  54                         shift ;
  55                 ;;
  56                 *)
  57                         break ;
  58                 ;;
  59         esac
  60 done
  61 
  62 TMPF1=/tmp/tinput1.$$
  63 TMPF2=/tmp/tinput2.$$
  64 FILE=/tmp/input.$$
  65 
  66 # Any remaining arg is the "file" specification.  It is a required arg.
  67 if [ -z "$1" ]; then
  68         echo $USAGE
  69         exit 1
  70 else
  71         cp $1 $TMPF1
  72 fi
  73 
  74 echo  >>$TMPF1
  75 echo "_done" >>$TMPF1
  76 
  77 #
  78 # First, strip all continuation characters, leaving one, single line
  79 # for each printer entry.
  80 #
  81         CONV_FIX=/usr/lib/print/conv_fix
  82 
  83         if [ -f $CONV_FIX ]; then
  84                 $CONV_FIX -f $TMPF1 -o $TMPF2
  85                 if [ $? != 0 ]; then
  86                         echo "$0:"
  87                         gettext "Fatal Error: $CONV_FIX failed.\n"
  88                         gettext "Please contact your Sun support representative.\n"
  89                         exit 1
  90                 fi
  91         else
  92                 gettext "$0: Fatal: Cannot locate $CONV_FIX binary.\n"
  93                 gettext "Please contact your Sun support representative.\n"
  94                 exit 1
  95         fi
  96 
  97 #
  98 # Continuation characters are now stripped.  Continue processing.
  99 #
 100         /bin/sed -e "s/:[       ]*:/:/g" $TMPF2 > $FILE
 101 
 102 #
 103 # Empty colons ":[ <TAB>]*:" are now stripped.  Continue processing.
 104 #
 105 
 106 nawk '
 107 BEGIN {
 108         "uname -n" | getline ; 
 109         host = $0 ;
 110         found = 0 ;
 111         local_pr = 0;
 112 }
 113 
 114 {
 115         FS=":"; OFS=":" ;
 116         if ($0 !~ /^#/)
 117          {
 118                         if ($0 ~ /^[_a-zA-Z0-9_]/) {     # New entry
 119                                 if ( found != 0 ) {
 120                                     if ( "'$namelist'" == 1 )
 121                                         printer = names ;
 122                                     else
 123                                         printer = name[1] ;
 124 
 125                                     if ( "'$conversion'" == "printers" ) {
 126                                         printf "\n%s:", names ;
 127                                         for (key in values) {
 128                                                 if ((key != "rp") &&
 129                                                     (key != "rm")) {
 130                                                 printf "\\\n\t:%s=%s:", 
 131                                                         key, values[key] ;
 132                                                 delete values[key];
 133                                                 }
 134                                         }
 135                                         if (values["rm"] != "") {
 136                                                 printf "\\\n\t:bsdaddr=%s,%s:", \
 137                                                         values["rm"], \
 138                                                         values["rp"] ;
 139                                                 if (values["rm"] == host) local_pr++;
 140                                         } else {
 141                                                 printf "\\\n\t:bsdaddr=%s,%s:", \
 142                                                         host, printer ;
 143                                                 local_pr++;
 144                                         }
 145                                         delete values["rp"];
 146                                         delete values["rm"];
 147                                     } else {
 148                                         printf "\n%s:", names ;
 149                                         for (key in values) {
 150                                                 if (key == "bsdaddr") {
 151                                                         split(values[key],
 152                                                                 pair, ",");
 153                                                         printf "\\\n\t:%s=%s:", \
 154                                                                 "rm", pair[1] ;
 155                                                         if (pair[2] == "")
 156                                                                 pair[2] = printer;
 157                                                         printf "\\\n\t:%s=%s:", \
 158                                                                 "rp", pair[2] ;
 159                                                         
 160                                                 } else if ((key == "br") || \
 161                                                            (key == "fc") || \
 162                                                            (key == "fs") || \
 163                                                            (key == "mc") || \
 164                                                            (key == "mx") || \
 165                                                            (key == "pc") || \
 166                                                            (key == "pl") || \
 167                                                            (key == "pw") || \
 168                                                            (key == "px") || \
 169                                                            (key == "py") || \
 170                                                            (key == "xc") || \
 171                                                            (key == "xs"))
 172                                                         printf "\\\n\t:%s#%s:", \
 173                                                                 key, \
 174                                                                 values[key] ;
 175                                                 else if (values[key] == "true")
 176                                                         printf "\\\n\t:%s:", \
 177                                                                 key ;
 178                                                 else if (values[key] != "false")
 179                                                         printf "\\\n\t:%s=%s:", \
 180                                                                 key, \
 181                                                                 values[key] ;
 182                                                 delete values[key];
 183                                         }
 184                                     }
 185                                 }
 186                                 split( $1, name, "|");
 187                                 names = $1 ;
 188                                 found++;
 189                         }
 190 
 191                         for ( i = 1 ; i <= NF ; i++ ) {
 192                                 if (($i == names) || ($i == "") || \
 193                                     ($i == "\t"))
 194                                         continue ;
 195                                 if ((split( $i, pair, "=" ) != 2) && \
 196                                     (split( $i, pair, "\#") != 2))
 197                                         pair[2] = "true";
 198                                 
 199                                 if (pair[1] != "" && pair[1] != "       ")
 200                                         values[pair[1]] = pair[2] ;
 201                         }
 202         }
 203 }
 204 END {
 205          printf "\n" ;
 206          if (local_pr != 0)
 207                  printf  "One or more printers are local, you may want to run lpadmin to configure LP server side operation\n" | "cat 1>&2" ;
 208 
 209 }' ${FILE}
 210 
 211 /bin/rm -f ${TMPF1} ${TMP2} ${FILE}
 212 
 213 exit 0