#!./wermit
#
# logserial
# 
# C-Kermit Script to copy a local shell session out the serial port in real
# time; for example to feed it to a speaking device or serial printer; in this
# case, the RC Systems DoubleTalk LT, which needs each line to be terminated
# by an ASCII NUL character.
# 
# Replace "./wermit" in the first line with the full path of your C-Kermit 9.0
# program and give this script file execute permission with chmod +x.
# 
# C-Kermit 9.0 or later is required, and it must be a version built with
# -DNOUUCP, as the Mac OS X versions are, by default, in this C-Kermit
# release.  SET SESSION-LOG NULL-PADDED is new to C-Kermit 9.0, as is the
# ability to send a log file (session, debug, packets...) to a serial port.
# 
# The device on the other end should be connected with a null modem cable or
# modem eliminator and it should be turned on before this script is executed,
# otherwise the script is likely to hang when it tries to open the serial
# port, because many versions of Unix will not open a serial port unless or
# until certain modem signals such as CD, DSR, and/or CTS are presented.
# 
# Frank da Cruz, Columbia University, October 2009
# 
# Last update: Tue Oct 27 10:59:35 2009

if < \v(version) 900299 exit 1 "FATAL - C-Kermit 9.0 required"

# Change port parameters as appropriate:

.portname = /dev/tty.KeySerial1		# Tested on a Keyspan USB adapter 
.speed = 9600
.flow = rts/cts

# Set up the serial port 

set carrier-watch off			# Don't require carrier 
set line \m(portname)			# Try to open the serial port
if fail exit 1 "FATAL - \m(portname): \v(setlinemsg)" # Stop here on error
set speed \m(speed)			# Set the required transmission speed 
if fail exit 1 FATAL - "set speed \m(speed)" rejected for \m(portname)
set flow \m(flow)			# Set the appropriate flow control 
if fail exit 1 FATAL - "set flow \m(flow)" unsupported for \m(portname)
output \13				# Send it a Carriage Return 
output \{1}4S				# Set the desired speaking speed 

# The two output commands are specific to the DoubleTalk device.  The carriage
# return (\13) activates the speaking function, and "\{1}4S" (Ctrl-A, 4, S) is
# a command to set the speaking rate.  5 is average, use a lower digit to slow
# it down, a higher digit (up to 9) to speed it up.
#
# Since Kermit can't have two terminal connections open at the same time,
# the serial port has to be closed.  We just have to hope the settings stick
# (in Mac OS X they do).

close connection			# Close the port

# In the following section we open a new connection to a pseudoterminal (pty).
# "set session-log null-padded" is also specific to the DoubleTalk device.

set session-log binary			# text might also work
set session-log null-padded             # Put ^@ after every ^J
log session \m(portname)		# Log session to serial port
if fail {
    echo "---------------------------------------------------"
    echo ERROR: SERIAL PORT \m(portname) CAN'T BE OPENED FOR
    echo LOGGING.  EITHER THE DEVICE IS NOT ACCESSIBLE OR IT
    echo IS IN USE BY ANOTHER PROCESS.
    echo "---------------------------------------------------"
    exit 1
}
set host /pty \$(SHELL)			# Start new shell in a pseudoterminal 
if fail exit 1 "FATAL - Can't spawn a new shell"

echo "---------------------------------------------------"
echo STARTING NEW SHELL SESSION LOGGING TO \m(portname)...
echo GIVE AN 'exit' COMMAND TO THE SHELL TO STOP LOGGING.
echo "---------------------------------------------------"

connect

echo "---------------------------------------------------"
echo SESSION TERMINATED, LOGGING STOPPED.
echo "---------------------------------------------------"
close session
exit 0
