#! /bin/bash # # Server to get and print files from remote queue. # # File: printer_server # Author: Bob Walton # Date: Sun Sep 8 21:41:16 EDT 2019 # # The authors have placed this program in the public # domain; they make no warranty and accept no liability # for this program. case "$1" in "" | -* ) echo " printer_server QUEUE-DIRECTORY Act as a server for printer_client: see that program for user documentation. This program is intended to be invoked from an .ssh/authorized_keys file line of the form: command=\"printer_server QUEUE-DIRECTORY\" ... The QUEUE-DIRECTORY name must be absolute. The QUEUE-DIRECTORY holds files to be printed. When it is being written, such a file has the .out extension. When it is ready for printing, the extension name is changed to .ps. When it has been printed, the extension name is changed to .done. The printer_server reads commands from its standard input and executes them. In these commands FILE refers to a file name that cannot contain a slash (/) or begin with a dot (.). The commands are: get FILE Return the QUEUE-DIRECTORY/FILE file in the standard output. sum FILE Return the MD5 sum of QUEUE-DIRECTORY/FILE file in the standard output. done FILE Change the extension of QUEUE-DIRECTORY/FILE to .done. The file must previously have had extension .ps or .done. ready FILE Ditto but change the extension to .ps instead of to .done. remove FILE Remove QUEUE-DIRECTORY/FILE. File must have extension .out, .ps, or .done. listfiles List all the files in the QUEUE-DIRECTORY, one line per file name, in lexical order. listdir List QUEUE-DIRECTORY as per \`ls -ltr'. Error messages are treated as standard output After producing any required output, each of these commands outputs a line containing just: %%-HPCM_DONE-%% Any errors terminate the command and output: %%-HPCM_ERROR-%% error messages %%-HPCM_DONE-%% It is assumed %%-HPCM_XXX-%% lines never appear in a postscript file or as a file name. " | less -F -K exit 1 ;; esac dir="$1" WS='[\ ]' NWS='[^\ ]' while read -r do case "$REPLY" in listfiles ) out="`if cd $dir 2>&1; then ls -1; \ else exit 1; fi`" if [ $? -ne 0 ] then echo '%%-HPCM_ERROR-%%' fi if [[ "$out" =~ ^${WS}*$ ]] then x=x # do nothing else echo "$out" fi echo '%%-HPCM_DONE-%%' ;; listdir ) (cd $dir; ls -ltr 2>&1 ) echo '%%-HPCM_DONE-%%' ;; get* | sum* ) [[ "$REPLY" =~ ^(get|sum) ]] op=${BASH_REMATCH[1]} if [[ "$REPLY" =~ \ ^${op}${WS}+(.*${NWS})${WS}*$ ]] then file="${BASH_REMATCH[1]}" if [[ "$file" =~ ^.*/.*$ ]] then echo '%%-HPCM_ERROR-%%' echo "$file contains a slash (/)" elif [[ "$file" =~ ^\. ]] then echo '%%-HPCM_ERROR-%%' echo "$file begins with a dot (.)" elif test -r "$dir/$file" then if [ $op = "get" ] then cat "$dir/$file" else [[ `md5sum "$dir/$file"` \ =~ ^([^\ \t]*)${WS} ]] echo "${BASH_REMATCH[1]}" fi else echo '%%-HPCM_ERROR-%%' echo Cannot read "$dir/$file" fi else echo '%%-HPCM_ERROR-%%' echo Badly Formed Command: $REPLY fi echo '%%-HPCM_DONE-%%' ;; done* | ready* | remove* ) [[ "$REPLY" =~ ^(done|ready|remove) ]] op=${BASH_REMATCH[1]} if [ $op = "done" ] then src=ps des=done else src=done des=ps fi if [[ "$REPLY" =~ \ ^${op}${WS}+(.*${NWS})${WS}*$ ]] then file="${BASH_REMATCH[1]}" if [[ "$file" =~ ^.*/.*$ ]] then echo '%%-HPCM_ERROR-%%' echo "$file contains a slash (/)" elif [[ "$file" =~ ^\. ]] then echo '%%-HPCM_ERROR-%%' echo "$file begins with a dot (.)" elif [[ "$file" =~ ^(.*)(\.${NWS}*)$ ]] then base="${BASH_REMATCH[1]}" ext="${BASH_REMATCH[2]}" case $op in remove) if [[ "$ext" =~ (.out|.ps|.done) ]] then rm -f "$dir/$file" else echo '%%-HPCM_ERROR-%%' echo "extension of $file is" \ "not .out, .ps, or .done" fi ;; done | ready ) if [[ "$ext" =~ (.ps|.done) ]] then if [ -e "$dir/${base}.$src" ] then mv -f "$dir/${base}.$src" \ "$dir/${base}.$des" \ >& /dev/null elif [ ! -e \ "$dir/${base}.$des" ] then echo '%%-HPCM_ERROR-%%' echo "${base}.{ps,done}" \ does not exist fi else echo '%%-HPCM_ERROR-%%' echo "extension of $file is" \ "not .ps or .done" fi ;; esac else echo '%%-HPCM_ERROR-%%' echo "$file" does not have \ extension fi else echo '%%-HPCM_ERROR-%%' echo Badly Formed Command: $REPLY fi echo '%%-HPCM_DONE-%%' ;; * ) echo '%%-HPCM_ERROR-%%' echo Cannot understand: $REPLY echo '%%-HPCM_DONE-%%' ;; esac done exit 0