[Coco] Printing utility for linux users

Gene Heskett gene.heskett at verizon.net
Fri May 16 16:54:19 EDT 2008


On Friday 16 May 2008, John W. Linville wrote:
>On Fri, May 16, 2008 at 06:56:58AM -0400, Gene Heskett wrote:
>> Because many of us do not have decent printers that still understand ascii
>> from the coco's we all love, here is a couple of utils that can restore
>> your ability to make hard copy of your program listings and such.  And no,
>> I didn't write very much of this, a fellow named Jon LaBadie on the
>> amanda-user list did 95% of it.  He is a script genius.
>
>Hey, Gene!  That's cool.
>
>A casual glance suggests that this works for ASCII text only.

Support for that would have to come from cups I believe.  Text was my instant 
need, so I scratched that 'itch' with Jon's help.

It, I find out when I start hammering on it, needs two more lines of code, so 
the latest is below.  It hasn't had a chance to cool for more than hour, but 
works much more correctly now.

>Anyone put any thought to DMP or CGP (or Epson or whatever) emulation?
>
>John
--------------------
#!/bin/bash

#### Initialize Variables ####

SEP="<<<END>>>"		# special line indicating the end of a transmitted file
SWriteCtl=0	## Unused?

    # output file name

CollectDir=/tmp/CoCo
BaseFN=CoCoFile
SeqNo=0			# start at zero, incremented before first opening

    # input data source

DefDev=/dev/ttyUSB1	# if not given as argument 1 on cmd line
InDev=${1:-${DefDev}}
exec 0< ${InDev}        # changes input for while read inp

    # sub command

CoCoPrCmd=/usr/local/libexec/coco_print

    # timed read constants, reduce till it breaks

MaxTime=5	# seconds
MaxRpts=2	# number of repeat timeouts

#### define functions ####

rename_output_file () {
	# deal with currently open tmp file (if any)
	if (( SeqNo > 0 ))
	then
		1>&-	# close stdout (TmpOutFile)
		mv ${TmpOutFile} ${OutFile}
	# no reason the above should not work, but
	# if it doesn't, restore your version below
	#	mv ${TmpOutFile} ${OutFile}
	fi
}

next_output_file () {
	# determine name of new tmp file
	SeqNo=$(( SeqNo + 1))
	OutFile=${CollectDir}/${BaseFN}-${SeqNo}
	echo "OutFile="$OutFile >&2
	TmpOutFile=${OutFile}.tmp
	echo "TmpOutFile="$TmpOutFile >&2
	# open/create new tmp file as stdout
	exec 1> ${TmpOutFile}
	StartedWriting=0	# false
	TimeOuts=0
}

next_output_file

while :		# infinite loop
do
	# At start of each output file need to do a
	# blocked read, subsequently a timed read
	# Reads are from stdin, the $InDev above
	if (( StartedWriting ))
	then
		echo "using timed read of "$MaxTime" seconds" >&2
		IFS= read -t $MaxTime inp
	else
		echo using blocking read >&2
		IFS= read  inp
	fi

	ReadStatus=$?

	# after the above read there are several situations possible
	#   1) read exits 0 (successful)
	#     a) a line of file data was received
	#     b) the special file separator line was received
	#   2) read exits 1 (non-zero, failed)
	#     a) partial line of file data received
	#     b) no file data received
	#     c) EOF meaning communications link was dropped
	#        (not handled in current code)

	if (( ReadStatus == 0 ))
	then				# got a whole line
		if [[ $inp == $SEP ]]
		then
			rename_output_file
			${CoCoPrCmd} $OutFile &
			next_output_file
		else
			printf "%s\n" "$inp"
			StartedWriting=1
			TimeOuts=0
# need to clear inp here too, else last line is doubled
			inp=""
		fi
		continue
	else				# timeout of read cmd
		if (( ${#inp} > 0 ))
		then			# received part line
			printf "%s" "$inp"	# no \n
			StartedWriting=1
			TimeOuts=0
# need to clear inp here, else last line repeats forever
			inp=""
		else			# no data read in
			TimeOuts=$(( TimeOuts + 1))
			if (( TimeOuts == MaxRpts ))
			then
				echo "Timeout "$CoCoPrCmd $TmpOutFile >&2
				rename_output_file
				${CoCoPrCmd} $OutFile &
				next_output_file
				TimeOuts=0
			fi
		fi
		continue
	fi
done

------------------

-- 
Cheers, Gene
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
I'm having a MID-WEEK CRISIS!



More information about the Coco mailing list