[net.sources] New ZMODEM+YMODEM+ZMODEM Unix Programs

caf@omen.UUCP (05/20/86)

Here are the Unix/Xenix programs that implement ZMODEM as well as XMODEM
and YMODEM.  They supercede rb.c and sb.c. 

The ZMODEM protocol is described in ZMODEM.DOC, posted separately. 
ZMODEM provides high throughput with timesharing systems and networks, a
simple user interface, security verification to protect from Trojan
Horses, command download and other advanced protocol management
functions. 

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
-----cut here-----cut here-----cut here-----cut here-----
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	rz.1
#	sz.1
#	zm.c
#	zmodem.h
#	rbsb.c
# This archive created: Tue May 20 03:40:16 1986
cat << \SHAR_EOF > rz.1
'\" Revision Level 
'\" Last Delta     05-19-86
.TH RZ 1 OMEN
.SH NAME
rb, rz \- XMODEM, YMODEM, ZMODEM (Batch) file receive
.SH SYNOPSIS
.B rz
.RB [\- "\ 1bquv" ]
.br
.B rb
.RB [\- "\ 1bquv" ]
.br
.B rz
.RB [\- "\ 1bcquv" ]
.I file
.br
.RB [ \- ][ v ] rzCOMMAND
.SH DESCRIPTION
This program uses error correcting protocol to receive
files over a serial port from a variety of programs running under
PC-DOS, CP/M,
.SM Unix,
and other operating systems.

The first form of
.B rz
(Receive Batch)
receives files with ZMODEM batch protocol.
If the sending program does not support ZMODEM,
.B rz
steps down to YMODEM protocol
after 50 seconds.
This delay can be eliminated by calling the program as
.I rb .

.B Rb
accepts either standard 128 byte sectors or
1024 byte sectors
(YAM
.B -k
option).
The user should determine experimentally
the conditions under which use of 1k blocks
actually improves throughput without causing
problems.

Normally, the file contents are converted to
.SM Unix
conventions by stripping carriage returns and all characters
beginning with Control Z (CP/M end of file).
If the raw pathname ends in
".A",
".ARC",
".CCC",
".CL",
".CMD",
".COM",
".CRL",
".DAT",
".DIR",
".EXE",
".O",
".OBJ",
".OVL",
".PAG",
".REL",
".SAV",
".SUB",
".SWP",
".SYS",
".TAR",
".UTL",
".a",
".o",
".tar",
or if the first packet contains
data that suggest a binary file
(parity bits or characters in the range 000 to 006 before the first ^Z),
or if the file mode is transmitted and the 0100000
bit is set, that file will be received in binary mode anyway.
Otherwise,
if the raw pathname ends in .MSG, or .TXT, an existing file will
be appended to rather than replaced.

If extended file information (file length, etc.)
is received,
the file length controls the number of bytes written to
the output dataset (YMODEM only),
and the modify time and file mode
(iff non zero)
are set accordingly.

If no extended file information is received,
slashes in the pathname are changed to underscore,
and any trailing period in the pathname is eliminated.
This conversion is useful for files received from CP/M systems.
Normally, each file name is converted to lower case
unless it contains one or more lower case letters.


The second form of
.B rz
receives a single
.I file
with XMODEM protocol.
The user must supply the file name to both sending and receiving programs.


The third form of
.B rz
is invoked as
.B rzCOMMAND
(with an optional leading \- as generated by login(1)).
For each received file,
rz will pipe the file to ``COMMAND filename''
where filename is the name of the transmitted file
with the file contents as standard input.

Each file transfer is acknowledged when COMMAND exits with 0 status.
A non zero exit status terminates transfers.

A typical use for this form is
.I rzrmail
which calls rmail(1)
to post mail to the user specified by the transmitted file name.
For example, sending the file "caf" from a PC-DOS system to
.I rzrmail
on a
.SM Unix
system
would result in the contents of the DOS file "caf" being mailed to user "caf".

On some
.SM Unix
systems, the login directory must contain a link to
COMMAND as login sets SHELL=rsh which disallows absolute
pathnames.
If invoked with a leading ``v'',
.B rz
will report progress to LOGFILE.
The following entry works for
.SM Unix
3.0:
.ce
rzrmail::5:1::/bin:/usr/local/rzrmail
If the SHELL environment variable includes
.I "rsh"
or
.I "rksh"
(restricted shell),
rz will not accept absolute pathnames
or references to a parent directory,
will not modify an existing file, and
removes any files received in error.

If
.B rz
is invoked with stdout and stderr to different datasets,
Verbose is set to 2, causing frame by frame progress reports
to stderr.
This may be disabled with the
.B q
option.

.PP
The meanings of the available options are:
.PP
.PD 0
.TP
.B 1
Use file descriptor 1 for ioctls and reads (Unix only).
By default, file descriptor 0 is used.
This option allows
.B rz
to be used with the
.I cu
~$
command.
.TP
.B b
Transfer all files in binary
(tell it like it is)
mode.
This option disables file append.
.TP
.B c
Request 16 bit CRC
(XMODEM file transfers default to 8 bit checksum).
Batch transfers use 16 bit CRC.
.TP
.B D
Output file data to /dev/null; for testing.
.TP
.B q
Quiet suppresses verbosity.
.TP
.B v
Verbose
causes a list of file
names to be appended to
/tmp/rzlog .
More v's generate more output.
.TP
.B u
Retain upper case letters in file names.
.PD
.SH EXAMPLES
.ne 6
.RE
(Pro-YAM command)
.RS
.I <ALT-2>
.br
YAM Command:
.I "sz *.h *.c"
.br
(This automatically invokes
.I rz
on the connected system.)
.RE
.SH SEE ALSO
ZMODEM.DOC,
YMODEM.DOC,
IMP(CP/M),
cu(1),
Professional-YAM manual,
sz(omen),
usq(omen),
undos(omen)

Compile time options required
for various operating systems are described in the
source file.
.SH BUGS
Pathnames are restricted to 127 characters.
In XMODEM single file mode, the pathname given on the command line
is still processed as described above.
The CR/LF to NL translation merely deletes CR\'s;
undos(omen) performs a more intelligent translation.
.SH "VMS VERSION"
Some of the #includes with file names enclosed with angle brackets <>
may need to have the angle brackets changed to "", or vice versa.

The VMS version does not set binary mode according to the incoming
file type.
Non binary file processing consists of stripping all characters beginning
with CPMEOF (^Z).

The VMS version does not set the file time.

At high speeds,
VMS sometimes loses incoming characters, resulting in retries
and degradation of throughput.

The mysterious
VMS C Standard I/O Package and RMS may interact to modify
file contents unexpectedly.

The VMS version does not support invocation as
.B rzCOMMAND .
ZMODEM has not yet been implemented on the VMS version.
.SH "ZMODEM CAPABILITIES"
.B Rz
supports incoming ZMODEM binary (-b), ASCII (-a), and append (-+)
requests, and ZMODEM command execution.
.SH FILES
rz.c, rbsb.c, zm.c, zmodem.h source files.

/tmp/rzlog stores debugging output generated with -vv option.
SHAR_EOF
cat << \SHAR_EOF > sz.1
'\" Revision Level 
'\" Last Delta     5-19-86
.TH SZ 1 OMEN
.SH NAME
sz \- XMODEM, YMODEM, ZMODEM Batch file Send
.SH SYNOPSIS
sz
.RB [\- +1adfknqTuvy ]
.I file ...
.br
sz -X
.RB [\- 1kquv ]
.I file
.br
sz
.RB [\- 1qv ]
.B "-c COMMAND"
.br
sz
.RB [\- 1qv ]
.B "-i COMMAND"
.SH DESCRIPTION
.B Sz
uses the ZMODEM, YMODEM or XMODEM error correcting protocol to send
one or more files over a serial port to a variety of programs running under
PC-DOS, CP/M, Unix, VMS, and other operating systems.


The first form of
.B sz
sends one or more files with ZMODEM or YMODEM batch protocol.
Normally, only the file name part of the pathname is transmitted.
On
.SM Unix
systems, additional information about the file is transmitted.
If the receiving program uses this information,
the transmitted file length controls the exact number of bytes written to
the output dataset,
and the modify time and file mode
are set accordingly.

Output from another program may be piped to
.B sz
for transmission by specifying the
.B -1
option and denoting standard input by "-":
.ce
ps -ef | sz -
The program output is transmitted with the filename sPID.sz
where PID is the process ID of the
.B sz
program.
If the environment variable
.B ONAME
is set, that is used instead.
In this case, the Unix command:
.ce
ONAME=con ps -ef|sz -ay -
will send a "file" to the PC-DOS console display.
The
.B -y
bypasses the receiver\'s checking of the output filename.
The
.B -a
causes the receiver to convert Unix newlines to PC-DOS carriage returns
and linefeeds.

Unix
.B sz
supports
.B YMODEM-g
with "cbreak" tty mode, XON/XOFF flow control,
and the interrupt character set to CAN.
.B YMODEM-g
(Professional-YAM
.B g
option)
increases throughput over error free channels
(direct connection, X.PC, etc.)
by not acknowledging each transmitted sector.


The second form of
.B sz
uses the
.B -X
flag to send a single
.I file
with
.B XMODEM
protocol.
The user must supply the file name to both sending and receiving programs.

Iff
.B sz
is invoked with $SHELL set and iff that variable contains the
string
.I "rsh"
or
.I "rksh"
(restricted shell), sz operates in restricted mode.
Restricted mode restricts pathnames to the current directory and
PUBDIR (usually /usr/spool/uucppublic) and/or subdirectories
thereof.


The third form sends a single COMMAND to the receiver for execution.
.B Sz
exits with the COMMAND return value.
If COMMAND includes spaces or characters special to the shell,
it must be quoted.

The fourth form sends a single COMMAND to the receiver for execution.
.B Sz
exits as soon as the receiver has received the command, before it is executed.


If sz is invoked with stdout and stderr to different datasets,
Verbose is set to 2, causing frame by frame progress reports
to stderr.
This may be disabled with the
.B q
option.
.PP
The meanings of the available options are:
.PP
.PD 0
.TP
.B +
Force the receiver to append transmitted data to an existing file
(ZMODEM only).
.TP
.B 1
Use file descriptor 1 for ioctls and reads (Unix only).
By default, file descriptor 0 is used.
This option allows
.B sz
to be used with the
.I cu
~$
command.
.TP
.B a
Convert NL characters in the transmitted file to CR/LF.
This is done by the sender for XMODEM and YMODEM, by the receiver
for ZMODEM.
.TP
.B "c COMMAND"
Send COMMAND to the receiver for execution, return with COMMAND\'s exit status.
.TP
.B "C N"
Make N attempts at ten second intervals
to read COMMAND\'s exit status before exiting.
.TP
.B d
Change all instances of "." to "/" in the transmitted pathname.
Thus, C.omenB0000 (which is unacceptable to MSDOS or CP/M)
is transmitted as C/omenB0000.
If the resultant filename has more than 8 characters in the stem,
a "." in inserted to allow a total of eleven.
.TP
.B f
Send Full pathname.
Normally directory prefixes are stripped from the transmitted
filename.
.TP
.B "i COMMAND"
Send COMMAND to the receiver for execution, return Immediately.
.TP
.B k
Send files using YMODEM 1024 byte blocks
rather than the default 128 byte blocks.
1024 byte packets speed file transfers at high bit rates.
(ZMODEM streams the data for the best possible throughput.)
.TP
.B n
Instruct ZMODEM receiver to skip files if the length and modification times
of the source file and the file on the destination system are equal.
.TP
.B N
Instruct ZMODEM receiver to skip files if the length and CRC\'s
of the source file and the file on the destination system are equal.
.TP
.B q
Quiet suppresses verbosity.
.TP
.B r
Resume interrupted file transfer.
If the source file is longer than the destination file,
the transfer commences at the offset in the source file that equals
the length of the destination file.
.TP
.B u
Unlink the file after successful transmission.
.TP
.B v
Verbose
causes a list of file
names to be appended to
/tmp/szlog .
More v's generate more output.
.TP
.B X
Send a single file with
.B XMODEM
protocol.
.TP
.B y
Force a ZMODEM receiving program to overwrite any existing file
with the same name.
.PD
.SH EXAMPLES
.ne 7
.B "ZMODEM File Transfer"
.br
.B "$ sz -a *.c"
.br
This single command transfers all .c files in the current Unix directory
with conversion
.RB ( -a )
to PC-DOS end of line conventions.
With ZMODEM AutoDownload enabled, Professional-YAM automatically recieves
the files after performing a security challenge.

.B "ZMODEM Command Download"
.br
 cpszall:all
    sz -c "c:;cd /yam/dist"
    sz -ya $(YD)/*.me
    sz -yqb y*.exe
    sz -c "cd /yam"
    sz -i "!insms"
.br
This Makefile fragment uses
.B sz
to issue commands to Professional-YAM to change current disk and directory.
Next,
.B sz
transfers the
.I .me
files from the $YD directory, commanding the receiver to overwrite the old files
and to convert from Unix end of line conventions to PC-DOS conventions.
The third line transfers some
.I .exe
files.
The fourth and fifth lines command Pro-YAM to
change directory and execute a PC-DOS batch file
.I insms .
Since the batch file takes considerable time, the
.B "-i"
form is used to allow
.B sz
to exit immediately.

.B "XMODEM File Transfer"
.br
$
.B "sz -Xa foo.c"
.br
.B "ESC"
.br
.B "rx foo.c"
.br
The above three commands transfer a single file
from Unix to a PC and Crosstalk XVI 3.6,
translating Unix newlines to DOS CR/LF.

.SH SEE ALSO
rz(omen),
ZMODEM.DOC,
YMODEM.DOC,
Professional-YAM manual,
IMP(CP/M),
cu(1),
sq(omen),
todos(omen),
tocpm(omen),
tomac(omen),
yam(omen)

Compile time options required for various operating systems are described in
the source file.
.SH "VMS VERSION"
The VMS version does not transmit the file date.
The VMS version calculates the file length by reading the file
and counting the bytes.

The VMS version does not support YMODEM-g or ZMODEM.

When VMS is lightly loaded, the response time may be too quick for MODEM7
unless the MODEM7
.B "q"
modifier is used.

The VMS C standard i/o package and RMS sometimes interact to modify
file contents unexpectedly.
.SH FILES
sz.c, rbsb.c, zm.c, zmodem.h source files

/tmp/szlog stores debugging output (sz -vv)
.SH "TESTING FEATURE"
The command "sz -T file"
exercises the
.B Attn
sequence error recovery by commanding
errors with unterminated packets.
The receiving program should complain five times about
binary data packets being too long.
Each time
.B sz
is interrupted,
it should send a ZDATA header followed by another defective packet.
If the receiver does not detect five long data packets,
the
.B Attn
sequence is not interrupting the sender, and the
.B Myattn
string in
.B sz.c
must be modified.

After 5 packets,
.B sz
stops the "transfer" and
prints the total number of characters "sent" (Tcount).
The difference between Tcount and 5120 represents the number of characters
stored in various buffers when the Attn sequence is generated.
.SH BUGS
XMODEM transfers add up to 127 spurious bytes per file.

Circular buffering and a ZMODEM sliding window should be used
when input is from pipes instead of acknowledging frames each 1024 bytes.
.B Sz
should check for the presence of at least one accessible file before
getting hot and bothered.
The test mode leaves a zero length file on the receiving system.
SHAR_EOF
cat << \SHAR_EOF > zm.c
/*
 *   Z M . C
 *    ZMODEM protocol primitives
 *    5-18-86  Chuck Forsberg Omen Technology Inc
 *
 * Entry point Functions:
 *	zsbhdr(type, hdr) send binary header
 *	zshhdr(type, hdr) send hex header
 *	zgethdr(hdr, eflag) receive header - binary or hex
 *	zsbdata(buf, len, frameend) send binary data
 *	zrbdata(buf, len) receive binary data
 *	stohdr(pos) store position data in Txhdr
 *	long rclhdr(hdr) recover position offset from header
 */

#ifndef CANFDX
#include "zmodem.h"
int Rxtimeout = 100;		/* Tenths of seconds to wait for something */
#endif

static char *frametypes[] = {
	"TIMEOUT",
	"ERROR",
	"ZRQINIT",
	"ZRINIT",
	"ZSINIT",
	"ZACK",
	"ZFILE",
	"ZSKIP",
	"ZNAK",
	"ZABORT",
	"ZFIN",
	"ZRPOS",
	"ZDATA",
	"ZEOF",
	"ZFERR",
	"ZCRC",
	"ZCHALLENGE",
	"ZCOMPL",
	"ZCAN",
	"ZFREECNT",
	"ZCOMMAND",
	"ZCACK"
#define FRTYPES 21	/* Total number of frame types in this array */
};

/* Send ZMODEM binary header hdr of type type */
zsbhdr(type, hdr)
register char *hdr;
{
	register n;
	register unsigned short oldcrc;

	vfile("zsbhdr: %s %lx", frametypes[type+2], rclhdr(hdr));
	xsendline(ZPAD); xsendline(ZDLE); xsendline(ZBIN);

	zsendline(type); oldcrc = updcrc(type, 0);

	for (n=4; --n >= 0;) {
		zsendline(*hdr);
		oldcrc = updcrc(*hdr++, oldcrc);
	}
	oldcrc = updcrc(0,updcrc(0,oldcrc));
	zsendline(oldcrc>>8);
	zsendline(oldcrc);
	if (type != ZDATA)
		flushmo();
}

/* Send ZMODEM HEX header hdr of type type */
zshhdr(type, hdr)
register char *hdr;
{
	register n;
	register unsigned short oldcrc;

	vfile("zshhdr: %s %lx", frametypes[type+2], rclhdr(hdr));
	sendline(ZPAD); sendline(ZPAD); sendline(ZDLE); sendline(ZHEX);
	zputhex(type);

	oldcrc = updcrc(type, 0);
	for (n=4; --n >= 0;) {
		zputhex(*hdr); oldcrc = updcrc(*hdr++, oldcrc);
	}
	oldcrc = updcrc(0,updcrc(0,oldcrc));
	zputhex(oldcrc>>8); zputhex(oldcrc);

	/* Make it printable on remote machine */
	sendline(015); sendline(012);
	/*
	 * Uncork the remote in case a fake XOFF has stopped data flow
	 */
	if (type != ZFIN)
		sendline(021);
	flushmo();
}

/*
 * Send binary array buf of length length, with ending ZDLE sequence frameend
 */
zsbdata(buf, length, frameend)
register char *buf;
{
	register unsigned short oldcrc;

	vfile("zsbdata: length=%d end=%x", length, frameend);
	oldcrc = 0;
	for (;--length >= 0;) {
		zsendline(*buf);
		oldcrc = updcrc(*buf++, oldcrc);
	}
	xsendline(ZDLE); xsendline(frameend);
	oldcrc = updcrc(frameend, oldcrc);

	oldcrc = updcrc(0,updcrc(0,oldcrc));
	zsendline(oldcrc>>8);
	zsendline(oldcrc);
	if (frameend == ZCRCW)
		flushmo();
}

/*
 * Receive binary array buf of max length with ending ZDLE sequence
 *  and CRC.  Returns the ending character or error code.
 */
zrbdata(buf, length)
register char *buf;
{
	register c;
	register unsigned short oldcrc;
	register d;

	oldcrc = Rxcount = 0;
	for (;;) {
		if ((c = zdlread()) & ~0377) {
			switch (c) {
			case GOTCRCE:
			case GOTCRCG:
			case GOTCRCQ:
			case GOTCRCW:
				oldcrc = updcrc((d=c)&0377, oldcrc);
				if ((c = zdlread()) & ~0377)
					goto badcrc;
				oldcrc = updcrc(c, oldcrc);
				if ((c = zdlread()) & ~0377)
					goto badcrc;
				oldcrc = updcrc(c, oldcrc);
				if (oldcrc & 0xFFFF) {
badcrc:
					zperr("Bad data packet CRC");
					return ERROR;
				}
				vfile("zrbdata: Rxcount = %d ret = %x",
				  Rxcount, d);
				return d;
			case GOTCAN:
				zperr("ZMODEM: Sender Canceled");
				return ZCAN;
			case TIMEOUT:
				zperr("ZMODEM data packet TIMEOUT");
				return c;
			default:
				zperr("ZMODEM bad data packet ret=%x", c);
				return c;
			}
		}
		if (--length < 0) {
			zperr("ZMODEM binary data packet too long");
			return ERROR;
		}
		++Rxcount;
		*buf++ = c;
		oldcrc = updcrc(c, oldcrc);
		continue;
	}
}

/*
 * Read a ZMODEM header to hdr, either binary or hex.
 *  eflag controls local display of non zmodem characters:
 *	0:  no display
 *	1:  display printing characters only
 *	2:  display all non ZMODEM characters
 *  On success, set Zmodem to 1 and return type of header.
 *   Otherwise return negative on error
 */
zgethdr(hdr, eflag)
char *hdr;
{
	register c, n;

	n = Baudrate;	/* Max characters before start of frame */
again:
	Rxframeind = Rxtype = 0;
	switch (c = noxread7()) {
	case TIMEOUT:
		goto fifi;
	case ZDLE:
		if (noxread7() == ZDLE) {
			c = ZCAN; goto fifi;
		}
	/* **** FALL THRU TO **** */
	default:
		if ( --n == 0) {
			zperr("ZMODEM Garbage count exceeded");
			return(ERROR);
		}
		if (eflag && ((c &= 0177) & 0140))
			bttyout(c);
		else if (eflag > 1)
			bttyout(c);
		goto again;
	case ZPAD:
		break;
	}
splat:
	switch (c = noxread7()) {
	case ZPAD:
		goto splat;
	case TIMEOUT:
		goto fifi;
	default:
		goto again;
	case ZDLE:
		break;
	}

	switch (c = noxread7()) {
	case TIMEOUT:
		goto fifi;
	case ZBIN:
		Rxframeind = ZBIN;
		c =  zrbhdr(hdr);
		break;
	case ZHEX:
		Rxframeind = ZHEX;
		c =  zrhhdr(hdr);
		break;
	case ZDLE:
		if (noxread7() == ZDLE) {
			c = ZCAN; goto fifi;
		}
	/* **** FALL THRU TO **** */
	default:
		goto again;
	}
	Rxpos = hdr[ZP3] & 0377;
	Rxpos = (Rxpos<<8) + (hdr[ZP2] & 0377);
	Rxpos = (Rxpos<<8) + (hdr[ZP1] & 0377);
	Rxpos = (Rxpos<<8) + (hdr[ZP0] & 0377);
fifi:
	switch (c) {
	case GOTCAN:
		c = ZCAN;
	/* **** FALL THRU TO **** */
	case ZNAK:
	case ZCAN:
	case ERROR:
	case TIMEOUT:
		zperr("ZMODEM: Got %s header", frametypes[c+2]);
	default:
		if (c >= -2 && c <= FRTYPES)
			vfile("zgethdr: %s %lx", frametypes[c+2], Rxpos);
		else
			vfile("zgethdr: %d %lx", c, Rxpos);
	}
	return c;
}

/* Receive a binary style header (type and position) */
zrbhdr(hdr)
register char *hdr;
{
	register c, n;
	register unsigned short oldcrc;

	if ((c = zdlread()) & ~0377)
		return c;
	Rxtype = c;
	oldcrc = updcrc(c, 0);

	for (n=4; --n >= 0;) {
		if ((c = zdlread()) & ~0377)
			return c;
		oldcrc = updcrc(c, oldcrc);
		*hdr++ = c;
	}
	if ((c = zdlread()) & ~0377)
		return c;
	oldcrc = updcrc(c, oldcrc);
	if ((c = zdlread()) & ~0377)
		return c;
	oldcrc = updcrc(c, oldcrc);
	if (oldcrc & 0xFFFF) {
		zperr("Bad Header CRC"); return ERROR;
	}
	Zmodem = 1;
	return Rxtype;
}

/* Receive a hex style header (type and position) */
zrhhdr(hdr)
char *hdr;
{
	register c;
	register unsigned short oldcrc;
	register n;

	if ((c = zgethex()) < 0)
		return c;
	Rxtype = c;
	oldcrc = updcrc(c, 0);

	for (n=4; --n >= 0;) {
		if ((c = zgethex()) < 0)
			return c;
		oldcrc = updcrc(c, oldcrc);
		*hdr++ = c;
	}
	if ((c = zgethex()) < 0)
		return c;
	oldcrc = updcrc(c, oldcrc);
	if ((c = zgethex()) < 0)
		return c;
	oldcrc = updcrc(c, oldcrc);
	if (oldcrc & 0xFFFF) {
		zperr("Bad Header CRC"); return ERROR;
	}
	if (readline(1) == '\r')	/* Throw away possible cr/lf */
		readline(1);
	Zmodem = 1; return Rxtype;
}

/* Send a byte as two hex digits */
zputhex(c)
register c;
{
	static char	digits[]	= "0123456789abcdef";

	if (Verbose>4)
		vfile("zputhex: %x", c);
	sendline(digits[(c&0xF0)>>4]);
	sendline(digits[(c)&0xF]);
}

/* Send character c with ZMODEM escape sequence encoding */
zsendline(c)
register c;
{
	switch (c & 0377) {
	case ZDLE:
	case 021:
	case 023:
	case 0221:
	case 0223:
		xsendline(ZDLE);
		c ^= 0100;
	/* **** FALL THRU TO **** */
	default:
		xsendline(c);
	}
}

/* Decode two lower case hex digits into an 8 bit byte value */
zgethex()
{
	register c;

	c = zgeth1();
	if (Verbose>4)
		vfile("zgethex: %x", c);
	return c;
}
zgeth1()
{
	register c, n;

	if ((c = noxread7()) < 0)
		return c;
	n = c - '0';
	if (n > 9)
		n -= ('a' - ':');
	if (n & ~0xF)
		return ERROR;
	if ((c = noxread7()) < 0)
		return c;
	c -= '0';
	if (c > 9)
		c -= ('a' - ':');
	if (c & ~0xF)
		return ERROR;
	c += (n<<4);
	return c;
}

/*
 * Read a byte, checking for ZMODEM escape encoding
 *  including CAN-CAN which represents a quick abort
 */
zdlread()
{
	register c;

	if ((c = readline(Rxtimeout)) != ZDLE)
		return c;
	if ((c = readline(Rxtimeout)) < 0)
		return c;
	switch (c) {
	case ZDLE:
		return GOTCAN;
	case ZCRCE:
	case ZCRCG:
	case ZCRCQ:
	case ZCRCW:
		return (c | GOTOR);
	case ZRUB0:
		return 0177;
	case ZRUB1:
		return 0377;
	default:
		if ((c & 0140) ==  0100)
			return (c ^ 0100);
		break;
	}
	zperr("Got bad ZMODEM escape sequence %x", c);
	return ERROR;
}

/*
 * Read a character from the modem line with timeout.
 *  Eat parity, XON and XOFF characters.
 */
noxread7()
{
	register c;

	for (;;) {
		if ((c = readline(Rxtimeout)) < 0)
			return c;
		switch (c &= 0177) {
		case XON:
		case XOFF:
			continue;
		default:
			return c;
		}
	}
}

/* Store long integer pos in Txhdr */
stohdr(pos)
long pos;
{
	Txhdr[ZP0] = pos;
	Txhdr[ZP1] = pos>>8;
	Txhdr[ZP2] = pos>>16;
	Txhdr[ZP3] = pos>>24;
}

/* Recover a long integer from a header */
long
rclhdr(hdr)
register char *hdr;
{
	register long l;

	l = (hdr[ZP3] & 0377);
	l = (l << 8) | (hdr[ZP2] & 0377);
	l = (l << 8) | (hdr[ZP1] & 0377);
	l = (l << 8) | (hdr[ZP0] & 0377);
	return l;
}

SHAR_EOF
cat << \SHAR_EOF > zmodem.h
/*
 *   Z M O D E M . H     Manifest constants for ZMODEM
 *    application to application file transfer protocol
 *    5-16-86  Chuck Forsberg Omen Technology Inc
 */
#define ZPAD '*'	/* 052 Padding character begins frames */
#define ZDLE 030	/* Ctrl-X Zmodem escape - `ala BISYNC DLE */
#define ZDLEE (ZDLE^0100)	/* Escaped ZDLE as transmitted */
#define ZBIN 'A'	/* Binary frame indicator */
#define ZHEX 'B'	/* HEX frame indicator */

/* Frame types (see array "frametypes" in zm.c) */
#define ZRQINIT	0	/* Request receive init */
#define ZRINIT	1	/* Receive init */
#define ZSINIT 2	/* Send init sequence (optional) */
#define ZACK 3		/* ACK to above */
#define ZFILE 4		/* File name from sender */
#define ZSKIP 5		/* To sender: skip this file */
#define ZNAK 6		/* Last packet was garbled */
#define ZABORT 7	/* Abort batch transfers */
#define ZFIN 8		/* Finish session */
#define ZRPOS 9		/* Resume data trans at this position */
#define ZDATA 10	/* Data packet(s) follow */
#define ZEOF 11		/* End of file */
#define ZFERR 12	/* Fatal Read or Write error Detected */
#define ZCRC 13		/* Request for file CRC and response */
#define ZCHALLENGE 14	/* Receiver's Challenge */
#define ZCOMPL 15	/* Request is complete */
#define ZCAN 16		/* Other end canned session with CAN-CAN */
#define ZFREECNT 17	/* Request for free bytes on filesystem */
#define ZCOMMAND 18	/* Command from sending program */

/* ZDLE sequences */
#define ZCRCE 'h'	/* CRC next, frame ends, header packet follows */
#define ZCRCG 'i'	/* CRC next, frame continues nonstop */
#define ZCRCQ 'j'	/* CRC next, frame continues, ZACK expected */
#define ZCRCW 'k'	/* CRC next, ZACK expected, end of frame */
#define ZRUB0 'l'	/* Translate to rubout 0177 */
#define ZRUB1 'm'	/* Translate to rubout 0377 */

/* zdlread return values (internal) */
/* -1 is general error, -2 is timeout */
#define GOTOR 0400
#define GOTCRCE (ZCRCE|GOTOR)	/* ZDLE-ZCRCE received */
#define GOTCRCG (ZCRCG|GOTOR)	/* ZDLE-ZCRCG received */
#define GOTCRCQ (ZCRCQ|GOTOR)	/* ZDLE-ZCRCQ received */
#define GOTCRCW (ZCRCW|GOTOR)	/* ZDLE-ZCRCW received */
#define GOTCAN	(GOTOR|030)	/* CAN-CAN seen */

/* Byte positions within header array */
#define ZF0	3	/* First flags byte */
#define ZF1	2
#define ZF2	1
#define ZF3	0
#define ZP0	0	/* Low order 8 bits of position */
#define ZP1	1
#define ZP2	2
#define ZP3	3	/* High order 8 bits of file position */

/* Bit Masks for ZRINIT flags byte ZF0 */
#define CANFDX	01	/* Rx can send and receive true FDX */
#define CANOVIO	02	/* Rx can receive data during disk I/O */
#define CANBRK	04	/* Rx can send a break signal */
#define CANCRY	010	/* Receiver can decrypt */
#define CANLZW	020	/* Receiver can uncompress */

/* Parameters for ZSINIT frame */
#define ZATTNLEN 32	/* Max length of attention string */

/* Parameters for ZFILE frame */
/* Conversion options one of these in ZF0 */
#define ZCBIN	1	/* Binary transfer - inhibit conversion */
#define ZCNL	2	/* Convert NL to local end of line convention */
#define ZCRESUM	3	/* Resume interrupted file transfer */
/* Management options, one of these in ZF1 */
#define ZMNEW	1	/* Transfer if source newer or different length */
#define ZMCRC	2	/* Transfer if different file CRC or length */
#define ZMAPND	3	/* Append contents to existing file (if any) */
#define ZMCLOB	4	/* Replace existing file */
#define ZMSPARS	5	/* Encoding for sparse file */
/* Transport options, one of these in ZF2 */
#define ZTLZW	1	/* Lempel-Ziv compression */
#define ZTCRYPT	2	/* Encryption */
#define ZTRLE	3	/* Run Length encoding */

/* Parameters for ZCOMMAND frame ZF0 (otherwise 0) */
#define ZCACK1	1	/* Acknowledge, then do command */

long rclhdr();

/* Globals used by ZMODEM functions */
int Rxframeind;		/* ZBIN or ZHEX indicates type of frame received */
int Rxtype;		/* Type of header received */
int Rxcount;		/* Count of data bytes received */
extern Rxtimeout;	/* Tenths of seconds to wait for something */
char Rxhdr[4];		/* Received header */
char Txhdr[4];		/* Transmitted header */
long Rxpos;		/* Received file position */
long Txpos;		/* Transmitted file position */
char Attn[ZATTNLEN+1];	/* Attention string rx sends to tx on err */
SHAR_EOF
cat << \SHAR_EOF > rbsb.c
/* -rev 05-12-86
 * mode function and most of the rest of the system dependent
 * stuff for rb.c and sb.c   This file is #included so the includer
 * can set parameters such as HOWMANY.
 */

#ifdef V7
#include <sys/types.h>
#include <sys/stat.h>
#include <sgtty.h>
#define OS "V7"
#endif

#ifndef OS
#ifndef USG
#define USG
#endif
#endif

#ifdef USG
#include <sys/types.h>
#include <sys/stat.h>
#include <termio.h>
#include <sys/ioctl.h>
#define OS "SYS III/V"
#endif


struct {
	unsigned baudr;
	int speedcode;
} speeds[] = {
	110,	B110,
	300,	B300,
	600,	B600,
	1200,	B1200,
	2400,	B2400,
	4800,	B4800,
	9600,	B9600,
	19200,	EXTA,
	9600,	EXTB,
	0,
};

static unsigned
getspeed(code)
{
	register n;

	for (n=0; speeds[n].baudr; ++n)
		if (speeds[n].speedcode == code)
			return speeds[n].baudr;
	return 0;
}



#ifdef ICANON
struct termio oldtty, tty;
#else
struct sgttyb oldtty, tty;
struct tchars oldtch, tch;
#endif

int iofd = 0;		/* File descriptor for ioctls & reads */

/*
 * mode(n)
 *  2: set a cbreak, XON/XOFF control mode if using Pro-YAM's -g option
 *  1: save old tty stat, set raw mode 
 *  0: restore original tty mode
 */
mode(n)
{
	static did0 = FALSE;

	switch(n) {
#ifdef USG
	case 2:	/* Cbreak mode used by sb when -g detected */
		if(!did0)
			(void) ioctl(iofd, TCGETA, &oldtty);
		tty = oldtty;

		tty.c_iflag = BRKINT|IXON;

		tty.c_oflag = 0;	/* Transparent output */

		tty.c_cflag &= ~PARENB;	/* Disable parity */
		tty.c_cflag |= CS8;	/* Set character size = 8 */

#ifdef XCLUDE
		tty.c_lflag = XCLUDE | ISIG;
#else
		tty.c_lflag = ISIG;
#endif

		tty.c_cc[VINTR] = Zmodem ? 03:030;	/* Interrupt char */
		tty.c_cc[VMIN] = 1;

		(void) ioctl(iofd, TCSETAW, &tty);
		did0 = TRUE;
		return OK;
	case 1:
		if(!did0)
			(void) ioctl(iofd, TCGETA, &oldtty);
		tty = oldtty;

		tty.c_iflag = IGNBRK;

		 /* No echo, crlf mapping, INTR, QUIT, delays, no erase/kill */
		tty.c_lflag &= ~(ECHO | ICANON | ISIG);
#ifdef XCLUDE
		tty.c_lflag |= XCLUDE;
#endif

		tty.c_oflag = 0;	/* Transparent output */

		tty.c_cflag &= ~PARENB;	/* Leave baud rate alone, disable parity */
		tty.c_cflag |= CS8;	/* Set character size = 8 */
		tty.c_cc[VMIN] = HOWMANY; /* Satisfy reads when this many chars in */
		tty.c_cc[VTIME] = 1;	/* ... or in this many tenths of seconds */
		(void) ioctl(iofd, TCSETAW, &tty);
		did0 = TRUE;
		Baudrate = getspeed(tty.c_cflag & CBAUD);
		return OK;
#endif
#ifdef V7
	case 2:
		if(!did0) {
			ioctl(iofd, TIOCEXCL, 0);
			ioctl(iofd, TIOCGETP, &oldtty);
			ioctl(iofd, TIOCGETC, &oldtch);
		}
		tty = oldtty;
		tch = oldtch;
		tch.t_intrc = Zmodem ? 03:030;	/* Interrupt char */
		tty.sg_flags |= CBREAK;
		tty.sg_flags &= ~ECHO;
		ioctl(iofd, TIOCSETP, &tty);
		ioctl(iofd, TIOCSETC, &tch);
		did0 = TRUE;
		return OK;
	case 1:
		if(!did0) {
			ioctl(iofd, TIOCEXCL, 0);
			ioctl(iofd, TIOCGETP, &oldtty);
			ioctl(iofd, TIOCGETC, &oldtch);
		}
		tty = oldtty;
		tty.sg_flags |= RAW;
		tty.sg_flags &= ~ECHO;
		ioctl(iofd, TIOCSETP, &tty);
		did0 = TRUE;
		Baudrate = getspeed(tty.sg_ospeed);
		return OK;
#endif
	case 0:
		if(!did0)
			return ERROR;
#ifdef USG
		(void) ioctl(iofd, TCSBRK, 1);	/* Wait for output to drain */
		(void) ioctl(iofd, TCFLSH, 1);	/* Flush input queue */
		(void) ioctl(iofd, TCSETAW, &oldtty);	/* Restore original modes */
		(void) ioctl(iofd, TCXONC,1);	/* Restart output */
#endif
#ifdef V7
		ioctl(iofd, TIOCSETP, &oldtty);
		ioctl(iofd, TIOCSETC, &oldtch);
		ioctl(iofd, TIOCNXCL, 0);
#endif
		return OK;
	default:
		return ERROR;
	}
}

sendbrk()
{
	ioctl(iofd, TCSBRK, 0);
}
SHAR_EOF
#	End of shell archive
exit 0


   Chuck Forsberg WA7KGX  ...!tektronix!reed!omen!caf   CIS:70715,131
   Author of Professional-YAM communications Tools for PCDOS and Unix
 Omen Technology Inc     17505-V NW Sauvie Island Road Portland OR 97231
Voice: 503-621-3406 TeleGodzilla: 621-3746 300/1200 L.sys entry for omen:
omen Any ACU 1200 1-503-621-3746 se:--se: link ord: Giznoid in:--in: uucp
omen!/usr/spool/uucppublic/FILES lists all uucp-able files, updated hourly