	NAME	mssset
; File MSSSET.ASM
	include mssdef.h
;	Copyright (C) 1982, 1995, Trustees of Columbia University in the 
;	City of New York.  The MS-DOS Kermit software may not be, in whole 
;	or in part, licensed or sold for profit as a software product itself,
;	nor may it be included in or distributed with commercial products
;	or otherwise distributed by commercial concerns to their clients 
;	or customers without written permission of the Office of Kermit 
;	Development and Distribution, Columbia University.  This copyright 
;	notice must not be removed, altered, or obscured.
;
; Edit history
; 12 Jan 1995 version 3.14
; Last edit
; 12 Jan 1995
; 27 August 1992 version 3.13
; 6 Sept 1991 version 3.11
 
	public	setcom, prmptr, dodef, setcpt, docom, stkadr, rdbuf
	public	setrx, rxtable, srvdsa, srvena, mcctab, takclos, ask
	public	askq, assign, initibm, mccptr, setinpbuf, setrollb, npages
	public	xfchtab, xftyptab, com1port, com2port, com3port, com4port
	public	flotab, popcmd, domacptr, warntab, portirq, dodecom
	public	xfertab1, xfertab2, rollwidth, setwidth, abftab
	public	getok, takeerror, macroerror, getc, dial
	public	takopen_sub, takopen_file, takopen_macro
	public	hide_assign, hide_define
ifndef	no_tcp
	public	tcpaddress,tcpsubnet,tcpdomain,tcpgateway,tcpprimens
	public	tcpsecondns,tcphost,tcpbcast,tcpport,tcppdint,tcpttbuf
	public	tcpbtpserver,tcpnewline,tcpdebug,tcpmode,tcpmss
endif	; no_tcp

braceop	equ	7bh		; opening curly brace
bracecl	equ	7dh		; closing curly brace

maketab	MACRO			; Assembler Macro to make rxtable [jrd]
cnt = 0
	rept 256
	db	cnt		; initialize table to 0 .. 255
cnt = cnt + 1
	endm
	db	0		; table off (0) or on (1) indicator
ENDM

data 	segment
	extrn	comand:byte, flags:byte, trans:byte, takadr:word, taklev:byte
	extrn	portval:word, dtrans:byte, spause:word, machnam:byte
	extrn	filtst:byte, maxtry:byte, script:byte, denyflg:word
	extrn	sloghnd:word, ploghnd:word, tloghnd:word, cardet:byte
	extrn	decbuf:byte, kstatus:word, errlev:byte, srvtmo:byte
	extrn	luser:byte, lpass:byte, destab:byte, blktab:byte
	extrn	seoftab:byte, dmpname:byte, lsesnam:byte, lpktnam:byte
	extrn	ltranam:byte, incstb:byte, inactb:byte, rxoffmsg:byte
	extrn	rxonmsg:byte, scpbuflen:word, oldtak:byte, setchtab:byte
	extrn	prnname:byte, prnhand:word, outpace:word, apctrap:byte
	extrn	protlist:byte, rcvpathflg:byte, sndpathflg:byte
	extrn	fossilflag:byte

rxtable	equ THIS BYTE		; build 256 byte Translation Input table
	maketab			; table rxtable is used by Connect mode

kerm	db	'MS-Kermit>',0		; default ASCIIZ prompt
promptbuf db	80 dup (0),0		; buffer for new ASCIIZ prompt
rdbuf	db	cmdblen dup (?)		; work space; room for macro def
					;  and for Status display line

stflg	db	0		; Says if setting SEND or RECEIVE parameter
defkind	db	0		; 0 for ASSIGN, 1 for DEFINE
ermes1	db	cr,lf,'?Too many macro names$'
ermes2	db	cr,lf,bell,'?No room for Take file buffer or Macro definition'
	db	cr,lf,bell,'$'
ermes4	db	cr,lf,'?Too many active Take files and Macros',cr,lf, bell,'$'
ermes5	db	cr,lf,'?Not implemented$'
ermes6	db	cr,lf,'?More parameters are needed$'
ermes7	db	cr,lf,'?Cannot use RTS/CTS on non-UART ports$'
errcap	db	cr,lf,'?Unable to open that file$'
erropn	db	cr,lf,'?Log file is already open$'
badrx	db	cr,lf,'?Expected ON, OFF, or \nnn$'
escerr	db	cr,lf,'?Not a control code$'
takcerr	db	cr,lf,'?Note: command is valid only in Take files and Macros$'
dmpdefnam db	'Kermit.scn',0		; asciiz default screen dump filename
prndefnam db	'PRN',0			; asciiz default printer name
xfchbad	db	cr,lf,'Warning: forcing FILE CHARACTER-SET to CP866$'
xfchbad2 db	cr,lf,'Warning: forcing FILE CHARACTER-SET to Shift-JIS$'
xfchbad3 db	cr,lf,'Warning: forcing FILE CHARACTER-SET to CP862$'
setchmsg db	cr,lf,'Warning: forcing TRANSFER CHARACTER-SET to CYRILLIC$'
setchmsg2 db	cr,lf,'Warning: forcing TRANSFER CHARACTER-SET to'
	db	' Japanese-EUC$'
setchmsg3 db  cr,lf,'Warning: forcing TRANSFER CHARACTER-SET to HEBREW-ISO$'
badcntlmsg db	cr,lf,'?Number is not in range of 0..31, 128..159$'
getdef	db	'Please respond Yes or No ',0	; ASCII, used as prompt
crlf	db	cr,lf,'$'

srvtab	db	2			; SET SERVER table
	mkeyw	'Login',1
	mkeyw	'Timeout',2

ifndef	no_network
settab	 db	63					; Set table
else
settab	 db	63 - 3					; Set table
endif	; no_network
	mkeyw	'Alarm',setalrm
	mkeyw	'Attributes',setatt
	mkeyw	'Baud',baudst
	mkeyw	'Bell',bellst
	mkeyw	'Block-check-type',blkset
	mkeyw	'Carrier',setcar
	mkeyw	'COM1',com1port
	mkeyw	'COM2',com2port
	mkeyw	'COM3',com3port
	mkeyw	'COM4',com4port
	mkeyw	'Control-character',cntlset
	mkeyw	'Count',takectr
	mkeyw	'Debug',debst
	mkeyw	'Default-disk',cwdir
	mkeyw	'Delay',setdely
	mkeyw	'Destination',desset
	mkeyw	'Display',disply
	mkeyw	'Dump',setdmp
	mkeyw	'Duplex',setdup
	mkeyw	'End-of-Line',eolset
	mkeyw	'EOF',seteof
	mkeyw	'Errorlevel',seterl
	mkeyw	'Escape-character',escset
	mkeyw	'Exit',setexitwarn
	mkeyw	'File',setfile
	mkeyw	'Flow-control',floset
	mkeyw	'Fossil',fosset
	mkeyw	'Handshake',hndset
	mkeyw	'Incomplete',abfset
	mkeyw	'Input',inpset
	mkeyw	'Key',setkey
	mkeyw	'Line',coms
	mkeyw	'Local-echo',lcal
	mkeyw	'Macro',setmacerr
	mkeyw	'Mode-line',modl
	mkeyw	'Modem',setmodem
ifndef	no_network
	mkeyw	'NetBios-name',setnbios
endif	; no_network
	mkeyw	'Output',setoutput
	mkeyw	'Parity',setpar
	mkeyw	'Port',coms
	mkeyw	'Printer',setprn
	mkeyw	'Prompt',promset
	mkeyw	'Receive',recset
	mkeyw	'Remote',remset
	mkeyw	'Repeat',repset
	mkeyw	'Retry',retryset
	mkeyw	'Rollback',setrollb
	mkeyw	'Send',sendset
	mkeyw	'Server',setsrv
	mkeyw	'Speed',baudst
	mkeyw	'Stop-bits',stopbit
	mkeyw	'Take',takset
ifndef	no_tcp
	mkeyw	'TCP/IP',tcpipset
	mkeyw	'Telnet',tcpipset
endif	; no_tcp
	mkeyw	'Terminal',vts
	mkeyw	'Timer',timset
	mkeyw	'Transfer',sxfer
	mkeyw	'xfer',sxfer		; hidden synonym
	mkeyw	'Translation',setrx
	mkeyw	'Transmit',setxmit
	mkeyw	'Unknown-character-set',unkchset
	mkeyw	'Warning',filwar
	mkeyw	'Windows',winset
 
setfitab db	6			; Set File command table
	mkeyw	'Character-Set',1
	mkeyw	'Collision',0
	mkeyw	'Display',3
	mkeyw	'Incomplete',4
	mkeyw	'Type',2
	mkeyw	'Warning',0

setrep	db	2			; SET REPEAT
	mkeyw	'Counts',0
	mkeyw	'Prefix',1

xfertab	db	3			; SET TRANSFER table
	mkeyw	'Character-set',0
	mkeyw	'Locking-shift',1
	mkeyw	'Translation',2

xfertab1 db	3			; SET TRANSFER LOCKING-SHIFT
	mkeyw	'Off',lock_disable
	mkeyw	'On',lock_enable
	mkeyw	'Forced',lock_force

xfertab2 db	2			; SET TRANSFER TRANSLATION
	mkeyw	'Readable',0
	mkeyw	'Invertible',1

xfchtab	db	6			; SET TRANSFER CHARACTER-SET
	mkeyw	'Transparent',xfr_xparent	; no translation
	mkeyw	'Latin1 ISO 8859-1',xfr_latin1	; ISO 8859-1, Latin-1
	mkeyw	'Latin2 ISO 8859-2',xfr_latin2	; ISO 8859-2, Latin-2
	mkeyw	'Hebrew ISO 8859-8',xfr_hebiso	; ISO 8859-8 Hebrew-ISO
	mkeyw	'Cyrillic ISO 8859-5',xfr_cyrillic; ISO 8859-5/Cyrillic, CP866
	mkeyw	'Japanese-EUC',xfr_japanese	; Japanese-EUC

xftyptab db	2			; SET FILE TYPE table
	mkeyw	'Binary',1		; Binary = as-is
	mkeyw	'Text',0		; Text = can change char sets

warntab	db	8			; File Warning table
	mkeyw	'Append',filecol_append		; append
	mkeyw	'Overwrite',filecol_overwrite	; overwrite
	mkeyw	'Rename',filecol_rename		; rename
	mkeyw	'Discard',filecol_discard	; discard
	mkeyw	'Update',filecol_update		; update (if incoming is newer)
	mkeyw	'No-supersede',filecol_discard	; discard
	mkeyw	'on (rename)',filecol_rename	; old form
	mkeyw	'off (overwrite)',filecol_overwrite ; old form

unkctab db	2			; unknown character-set disposition
	mkeyw	'Keep',0
	mkeyw	'Cancel',1

atttab	db	6			; SET ATTRIBUTES table
	mkeyw	'Off',00ffh		; all off
	mkeyw	'On',10ffh		; all on (high byte is on/off)
	mkeyw	'Character-set',attchr	; Character set
	mkeyw	'Date-Time',attdate	; Date and Time
	mkeyw	'Length',attlen		; Length
	mkeyw	'Type',atttype		; Type

comtab	db	2			; table of COM ports
	mkeyw	'COM3',4		; offset of COM3 address
	mkeyw	'COM4',6		; offset of COM4 address

cntltab	db	2			; SET CONTROL table
	mkeyw	'Prefixed',0		; 0 = send with prefix
	mkeyw	'Unprefixed',1		; 1 = send as-is

stsrtb	db	11			; Number of options
	mkeyw	'Packet-length',srpack
	mkeyw	'Padchar',srpad
	mkeyw	'Padding',srnpd
	mkeyw	'Pause',srpaus
	mkeyw	'Start-of-packet',srsoh
	mkeyw	'Quote',srquo
	mkeyw	'End-of-packet',sreol
	mkeyw	'Timeout',srtim
	mkeyw	'Double-char',srdbl
	mkeyw	'Ignore-char',srign
	mkeyw	'Pathnames',srpath

ontab	db	2
	mkeyw	'off',0
	mkeyw	'on',1

outputtab db	1			; Set OUTPUT
	mkeyw	'PACING',setopace

distab	db	5 			; Set Display mode
	mkeyw	'7-bit',7		; controls bit d8bit in flags.remflg
	mkeyw	'8-bit',8		; sets d8bit
	mkeyw	'Quiet',dquiet		; values defined in header file
	mkeyw	'Regular',dregular
	mkeyw	'Serial',dserial

distab2	db	3			; for SET FILE DISPLAY
	mkeyw	'Quiet',dquiet		; values defined in header file
	mkeyw	'Regular',dregular
	mkeyw	'Serial',dserial

fossiltab db	1			; Fossil
	mkeyw	'disable-on-close',1

; If abort when receiving files, can keep what we have or discard
abftab	db	2
	mkeyw	'Discard',1
	mkeyw	'Keep',0

flotab	db	5
	mkeyw	'none',0
	mkeyw	'xon/xoff',1+2		; both directions
	mkeyw	'incoming-xon/xoff',2
	mkeyw	'outgoing-xon/xoff',1
	mkeyw	'RTS/CTS',4

hndtab	db	8
	mkeyw	'none',0
	mkeyw	'bell',bell
	mkeyw	'cr',cr
	mkeyw	'esc',escape
	mkeyw	'lf',lf
	mkeyw	'xoff',xoff
	mkeyw	'xon',xon
	mkeyw	'code',0ffh		; allow general numerial code

duptab	db	2			; SET DUPLEX table
	mkeyw	'full',0
	mkeyw	'half',1

partab	db	5
	mkeyw	'none',PARNON
	mkeyw	'even',PAREVN
	mkeyw	'odd',PARODD
	mkeyw	'mark',PARMRK
	mkeyw	'space',PARSPC

exittab	db	1			; EXIT table
	mkeyw	'warning',0

gettab	db	3			; GETOK dispatch table
	mkeyw	'Yes',kssuc		; success = yes
	mkeyw	'OK',kssuc		; ditto
	mkeyw	'No',ksgen		; general failure

inptab	db	4				; Scripts. Set Input
	mkeyw	'Case',inpcas			;[jrs]
	mkeyw	'Default-timeout',inptmo	;[jrs]
	mkeyw	'Echo',inpeco			;[jrs]
	mkeyw	'Timeout-action',inpact		;[jrs]

macrotab db	1			; SET MACRO table
;;	mkeyw	'Echo',0
	mkeyw	'Error',1

taketab	db	2			; SET TAKE table
	mkeyw	'Echo',0
	mkeyw	'Error',1

xmitab	db	4			; SET TRANSMIT table
	mkeyw	'Fill-empty-line',0
	mkeyw	'Line-Feeds-sent',1
	mkeyw	'Pause',3
	mkeyw	'Prompt',2

debtab	db	4			; Set Debug command
	mkeyw	'Off',0
	mkeyw	'On',logpkt+logses
	mkeyw	'Packets',logpkt
	mkeyw	'Session',logses

logtab	db	3			; LOG command
	mkeyw	'Packets',logpkt
	mkeyw	'Session',logses
	mkeyw	'Transactions',logtrn

srvdetab db	16			; Server Enable/Disable list
	mkeyw	'All',0fffh
	mkeyw	'CD',cwdflg
	mkeyw	'CWD',cwdflg
	mkeyw	'Define',defflg
	mkeyw	'Delete',delflg
	mkeyw	'Dir',dirflg
	mkeyw	'Finish',finflg
	mkeyw	'Get',getsflg
	mkeyw	'Host',hostflg
	mkeyw	'Kermit',kerflg
	mkeyw	'Login',pasflg
	mkeyw	'Print',prtflg
	mkeyw	'Query',qryflg
	mkeyw	'Send',sndflg
	mkeyw	'Space',spcflg
	mkeyw	'Type',typflg

trnstab	db	2			; Set Translation table
	mkeyw	'Input',1
	mkeyw	'Keyboard',2

ifndef	no_tcp
tcptable db	14			; Telnet or TCP/IP command
	mkeyw	'address',1		; local Internet address
	mkeyw	'domain',2		; local domain string
	mkeyw	'broadcast',8		; broadcast of all 0's or all 1's
	mkeyw	'gateway',4		; gateway address
	mkeyw	'primary-nameserver',5	; name servers
	mkeyw	'secondary-nameserver',6
	mkeyw	'subnetmask',3		; our subnet mask
	mkeyw	'host',7		; host's IP name or IP number
	mkeyw	'Packet-Driver-interrupt',9
	mkeyw	'term-type',10		; Options term type
	mkeyw	'NewLine-mode',11 	; CR-NUL vs CRLF
	mkeyw	'mode',13		; NVT-ASCII or Binary
	mkeyw	'mss',14		; Max Segment Size
	mkeyw	'debug-Options',12	; debug Telnet Options

tcpmodetab db	2			; TCP/IP Mode
	mkeyw	'NVT-ASCII',0
	mkeyw	'Binary',1

newlinetab db	3			; TCP/IP Newline mode
	mkeyw	'off',0
	mkeyw	'on',1
	mkeyw	'raw',2

domainbad db	cr,lf,'?Bad domain name, use is such as my.domain.name$'
addressbad db	cr,lf,'?string is too long$'
hostbad	db	cr,lf,'?Bad host, use IP name or IP number$'

tcpaddress db	'bootp',(16-($-tcpaddress)) dup (0),0
tcpsubnet  db	'255.255.255.0',(16-($-tcpsubnet)) dup (0),0
tcpdomain  db	'unknown',(32-($-tcpdomain)) dup (0),0
tcpgateway db	'unknown',(32-($-tcpgateway)) dup (0),0
tcpprimens db	'unknown',(16-($-tcpprimens)) dup (0),0
tcpsecondns db	'unknown',(16-($-tcpsecondns)) dup (0),0
tcphost	db	(60 -($-tcphost)) dup (0),0
tcpbcast db	'255.255.255.255',(16-($-tcpbcast)) dup (0),0
tcpbtpserver db	17 dup (0)		; bootp server (response)
tcpport	dw	23			; TCP port
tcppdint dw	0			; Packet Driver interrupt
tcpttbuf db	32 dup (0),0		; term-type-override buffer
tcpnewline db	1			; NewLine-Mode (default is on)
tcpdebug db	0			; Options debugging (0 is off)
tcpmode db	0			; NVT-ASCII is 0, Binary is 1
tcpmss	dw	1460			; MSS
endif	; no_tcp

; MACRO DATA STRUCTURES mcctab
mcclen	equ	macmax*10		; length of mcctab
mcctab	db	0			; macro name table entries
	db	mcclen dup (0)		; room for macro structures
; END OF MACRO DATA STRUCTURES

ibmmac	db	'IBM '			; startup IBM macro definition + space
	db	'set timer on,set parity mark,set local-echo on,'
	db	'set handshake xon,set flow none,',0	; asciiz
dialmac	db	'__DIAL '			; "__DIAL "
	db	'asg \%9 \v(carrier),set carr off,'
	db	'output ATD\%1\%2\%3\%4\%5\%6\%7\%8\13,wait 90 CD,'
	db	'asg \%8 \v(status),set carr \%9,end \%8',0	; asciiz

	even
prmptr	dw	kerm			; pointer to prompt
tempptr	dw	0			; pointer into work buffer
domacptr dw	0			; pointer to DO MAC string
min	dw	0 
max	dw	0 
numerr	dw	0
numhlp	dw	0
temp	dw	0
temp1	dw	0			; Temporary storage
temp2	dw	0			; Temporary storage
askecho db	0			; ask's echo control flag
deftemp	dw	0
stkadr	dw	0	; non-zero if replacement keyboard xlator present
mccptr	dw	mcctab 			; ptr to first free byte in mcctab
macptr	dw	0			; temp to hold segment of string
npages	dw	10 			; # of pages of scrolling on each side
rollwidth dw	0			; columns to roll back 80..207
portirq	db	4 dup (0)		; user specified IRQ's for COM1..4
takeerror db	0			; Take Error (0 = off)
macroerror db	0			; Macro Error (0 = off)
unhidetmp db	6 dup (0)		; work space for unhide
data	ends

data1	segment
askhlp1	db	'Variable name  then  prompt string$'
askhlp2	db	'Prompt string$'
askhlp3	db	'Enter a line of text$'
getokhlp db	'Optional prompt string$'
filhlp	db	' Output filename for the log$'
dishlp	db	cr,lf,' Quiet (no screen writing), Regular (normal),'
	db	' Serial (non-formatted screen)'
	db	cr,lf,' and/or 7-BIT (default) or 8-BIT wide characters.$'
exitwhlp db	cr,lf,' ON or OFF. Warn if sessions are active when exiting'
	db	' Kermit$'
remhlp	db	cr,lf,' OFF to show file transfer display,'
	db	' ON for quiet screen$'
macmsg	db	' Specify macro name followed by body of macro, on same line$'
prmmsg	db	cr,lf
	db    ' Enter new prompt string or nothing to regain regular prompt.'
	db	cr,lf,' Use \123 notation for special chars (Escape is \27)$'
rspathhlp db	cr,lf,' ON retains pathnames during file transfer,'
	db	' OFF removes them$'
	
srxhlp1	db	cr,lf,' Enter   code for received byte   code for'
	db	' local byte ',cr,lf,' use ascii characters themselves or'
	db	cr,lf,' numerical equivalents of  \nnn  decimal'
	db	' or \Onnn  octal or \Xnnn  hexadecimal',cr,lf
	db	' or keywords  ON  or  OFF  (translation is initially off)'
	db	cr,lf,'$'

takchlp	db	cr,lf,'Value 0 to 65535 for COUNT in script IF COUNT command$'

nummsg1 db	cr,lf,'?Use a number between $'
nummsg2	db	' and $'
srvthlp	db	'seconds, 0-255, waiting for a transaction$'
unkchhlp db	cr,lf,' Disposition of files arriving with unknown'
	db	' character sets:',cr,lf,'  Keep (default), Cancel$'
winhelp	db	cr,lf,'Number of sliding window slots 1 (no windowing) to 32$'
eophlp	db	' Decimal number between 0 and 31$'
ctlhlp	db	' Decimal number between 0 and 31, 128 and 159$'
cntlhlp db	cr,lf,' PREFIXED <0..31, 128..159> protectively quotes this'
	db	' control code',cr,lf
	db	' UNPREFIXED <0..31, 128..159> sends control code as-is'
	db	cr,lf,' Use ALL to change all codes at once.$'
sohhlp	db	' Decimal number between 0 and 31.',cr,lf,' Special case:'
	db	' up to 126, but reduces strength of the protocol.$'
dmphlp	db	' Filename to hold screen dumps$'
prnhlp	db	' Filename for printer output (default is PRN)$'
prnerr	db	cr,lf,' Cannot open that name. Using default of PRN$'
erlhlp	db	' Decimal number between 0 and 255$'
pakerr	db	cr,lf,'?Choose a decimal number '
	db	'from 20 to 94 (normal) or to 9024 (long)$'
pakhlp	db	cr,lf,'Decimal number between 20 and 94 (normal) or '
	db	'9024 (long)$'
padhlp	db	cr,lf,' Decimal number between 0 and 31 or 127$'
pauhlp	db	' Decimal number between 0 and 65383 milliseconds$'
quohlp	db	' Decimal number between 33 and 126$'
retryhlp db	' Decimal number between 1 and 63$'
rollhlp	db	' Decimal number between 0 and 8000$'
dblhlp	db	' Decimal number between 0 and 255$'
stophlp	db	' Serial port stop bits, 1 (default) or 2$'
luserh	db	cr,lf,'Username Password from remote Kermit (0-16 chars each)$'
lpassh	db	cr,lf,'Password from remote Kermit (0-16 chars,'
	db	' spaces allowed)$'
prefhlp	db	cr,lf,' single char (def is ~) or number between 33-62 or'
	db	' 96-126$'
timhlp	db	' Decimal number between 0 and 94$'
delyhlp	db	' Delay seconds before sending file (0-63)$'
eschlp	db	cr,lf,'Press literal control keys (ex: Control ]) or'
	db	' enter in \nnn numerical form$'
hnd1hlp	db	cr,lf,'XON (\17), XOFF (\19), CR (\13), LF (\10), BELL (\7),'
	DB	' ESC (\27), NONE (\0)'
	db	cr,lf,' or "CODE" followed by decimal number$' 
intoms	db	'number of seconds to wait before timeout',cr,lf,'$'
loghlp	db	cr,lf
	db    ' PACKETS - during file transfers  (to default file PACKET.LOG)'
	db	cr,lf
	db    ' SESSION - during Connect mode   (to default file SESSION.LOG)'
	db	cr,lf
	db    ' TRANSACTIONS - files transfers (to default file TRANSACT.LOG)'
	db	cr,lf,'  followed by an optional filename for the log.$'
carhlp	db	cr,lf,' ON or OFF. Sense modem Carrier Detect and end'
	db	' connection if it drops.$'
comhlp	db	cr,lf,' Address of the COM1 - COM4 port (ex: COM3 \x02f8 or'
	db	' COM4 \x02e8)$'
irqhlp	db	cr,lf,' Enter, or IRQ of port (ex: \3)$'
debhlp	db	cr,lf,' PACKETS - during file transfers'	; Debugging
	db	cr,lf,' SESSION - during Connect mode'
	db	cr,lf,' ON - both packets and session'
	db	cr,lf,' OFF - turns off all debugging$'
dialhlp	db	' Phone number to dial$'
dohlp	db	cr,lf,'definitions of variables (\%n), or press ENTER key$'
fossilhlp db	cr,lf,' OFF to leave Fossil active (default), ON to disable'
	db	' when done with port$'
sdshlp	db	cr,lf,'DISABLE or ENABLE access to selected Server commands:'
	db	cr,lf
	db	' CD/CWD, DEFINE, DEL, DIR, FINISH (includes BYE & LOGOUT),'
	db	' GET, HOST,',cr,lf
	db	' KERMIT, LOGIN, PRINT, QUERY, SEND, SPACE, TYPE, and ALL.$'

xfchhlp	db	cr,lf,' Which character set to put on the wire during file'
	db	' transfers:',cr,lf
	db	'  TRANSPARENT (regular PC codes)',cr,lf
	db	'  LATIN1   (ISO 8859-1)',cr,lf
	db	'  LATIN2   (ISO 8859-2)',cr,lf
	db	'  HEBREW   (ISO 8859-8)',cr,lf
	db	'  CYRILLIC (ISO 8859-5)',cr,lf
	db	'  JAPANESE-EUC$'
xferhlp1 db	cr,lf,' OFF: disable feature, ON: enable (default), FORCE:'
	db	' forced on$'
xfchhlp2 db	cr,lf,' READABLE: translate some/many characters to/from'
	db	' locally readable form (def).'
	db	cr,lf,' INVERTIBLE: use codes which can be copied back to the'
	db	' host in its form.$'
xfilhlp	db	'NONE, SPACE, or filler character$'
xpmthlp db	'Host echo char acting as prompt, \1-\255$'
xpauhlp	db	'Millisec to pause between lines, 1 - 65000$'
opacehlp db	'Millisec to pause between OUTPUT bytes, 0 - 65000$'
pophlp	db	'Status value to be returned  msg, nothing if no new value$'
sethlp	db	cr,lf
	db	'  Alarm    sec from now or HH:MM:SS  '
	db	'  Local-echo        on/off           '
	db	cr,lf
	db	'  Attributes packets on/off          '
	db	'  Mode-line         on/off'
	db	cr,lf
	db	'  Bell    on/off    at end of xfers  '
	db	'  NetBios-name      (our local name)'
	db	cr,lf
	db	'  Block-check-type  checksum/CRC     '
	db	'  Output pacing (ms between bytes)  '
	db	cr,lf
	db	'  Carrier  sense modem Carrier Detect'
	db	'  Parity    even/odd/mark/space/none'
	db	cr,lf
	db	'  COM1 - COM4 port-address irq       '
	db	'  Port (or Line)    1/2/COM1/COM2/etc'
	db	cr,lf
	db	'  Control prefixed/unprefixed  code  '
	db	'  Printer filespec   for Connect mode'
	db	cr,lf
	db	'  Count   number    a loop counter   '
	db	'  Prompt  string   (new Kermit prompt)'
	db	cr,lf
	db	'  Debug   on/off    display packets  '
	db	'  Receive parameter  many things'
	db	cr,lf
	db	'  Default-disk                       '
	db	'  Repeat Counts (on/off)             '
	db	cr,lf
	db	'  Delay   secs  before Sending file  '
	db	'  Retry limit for packet send/receive'
	db	cr,lf
	db	'  Destination   Disk/Screen/Printer  '
	db	'  Rollback, terminal screens'
	db	cr,lf
	db	'  Display quiet/reg/serial show cnts?'
	db	'  Send parameter    many things'
	db	cr,lf
	db	'  Dump filespec     screen to disk   '
	db	'  Server parameter'
	db	cr,lf
	db	'  Duplex            half or full     '
	db      '  Speed or Baud     many speeds'	
	db	cr,lf
	db	'  EOF Ctrl-Z/NoCtrl-Z  ^Z ends file? '
	db	'  Stop-bits         always 1'
	db	cr,lf
	db 	'  End-of-line char  cr or whatever   '
	db	'  Take Echo or Error on/off' 
	db	cr,lf
	db	'  Errorlevel number   for DOS Batch  '
	db	'  Telnet address,mask,nameserver etc'
	db	cr,lf
	db      '  Escape-char  ^]   or whatever      '
	db	'  Terminal type and parameters'
	db	cr,lf
	db	'  Exit warning (if session active)   '
	db	'  Timer     on/off  time packet waiting'
	db	cr,lf
	db	'  File (Character-set, Type, Warning)'
	db	'  Translation IN  Connect mode rcv''d char'
	db	cr,lf
	db	'  Flow-control  none xon/xoff rts/cts'
	db	'  Transfer Character-set (on wire) '
	db	cr,lf
	db	'  Handshake xon/xoff/cr/lf/bell/esc..'
	db	'  Transmit  parameters, for scripts'
	db	cr,lf
	db	'  Incomplete file   keep/discard     '
	db	'  Unknown-character-set (keep/cancel)'
	db	cr,lf
	db	'  Input timeout, etc  (for scripts)  '
	db	'  Warning   on/off  if file renamed'
	db	cr,lf
	db	'  Key         key-ident   definition '
	db	'  Windows  number of sliding window slots'
	db	'$'

ifndef	no_tcp
hosthlp	db	cr,lf,'Internet name or number (ddd.ddd.ddd.ddd) of '
	db	'the remote machine$'
domainhlp db	cr,lf,'Name of your domain$'
subnethlp db	cr,lf,'Subnetmask, decimal ddd.ddd.ddd.ddd$'
addrhelp db	cr,lf,'Internet address, decimal ddd.ddd.ddd.ddd, of this'
	db	' machine or'
	db	cr,lf,' BOOTP, RARP, or Telebit-PPP$'
iphelp	db	cr,lf,'Internet address, decimal ddd.ddd.ddd.ddd$'
tcppdinthlp db	cr,lf,'Interrupt on PC for Packet Driver, \x60 to \x7f'
	db	' or use 0 for automatic search,'
	db	cr,lf,' or ODI to use Novell''s ODI interface$'
tcpttyhlp db	cr,lf,' Telnet Options terminal identification override '
	db	'string.'
	db	cr,lf,' This does NOT modify the real terminal type.'
	db	' Press ENTER to remove this',cr,lf 
	db	' override and report the real terminal type.$'
tcpnlhlp db	cr,lf,' ON sends CR LF for each CR, OFF sends CR NUL,'
	db	' RAW sends just CR$'
tcpmsshlp db	cr,lf,' Maximum Segment Size, 16 to 1460 bytes$'
endif	; no_tcp

data1	ends

code1	segment
	extrn	makebuf:far
code1	ends

code	segment
	extrn comnd:near, baudst:near, prompt:near, coms:near, cwdir:near
	extrn isfile:near, strlen:near, strcpy:near, cnvlin:near
	extrn katoi:near, decout:near, vts:near, setalrm:near, serrst:near
	extrn prnopen:near, pntflsh:near, takrd:near, prtasz:near
	extrn malloc:far, dec2di:near, cnvstr:near
ifndef	no_network
	extrn setnbios:near	; in MSXIBM, needs stub for other machines
endif	; no_network
extrn prtscr:near
	assume	cs:code, ds:data, es:nothing

; DO defined macro command
; DO macname variable variable   also defines variables \%1, \%2, ...\%9
DOCOM	PROC	NEAR
	mov	dx,offset mcctab	; table of macro defs
	xor	bx,bx			; help is table
	mov	ah,cmkey		; get key word (macro name)
	call	comnd			; get pointer to keyword structure
	jnc	docom1			; nc = success, bx = 16 bit data
	ret				; failure
DOCOM1:	mov	domacptr,bx		; segment of definition string
	mov	comand.cmquiet,0	; permit command echoing
	mov	bx,offset decbuf	; point to borrowed work buffer
	mov	word ptr[bx],0		; clear buffer
	mov	dx,offset dohlp		; help
	mov	comand.cmblen,length rdbuf ; length of analysis buffer
	mov	ah,cmline		; get line of text, if any
	call	comnd
	jnc	docom2			; nc = success
	ret				; failure
docom2:	mov	deftemp,ax		; save byte count of command args
	call	hidemac			; hide previous \%0..\%9 macros
	call	getname			; get name of this macro
	mov	cx,word ptr rdbuf	; length of "\%0 plus found name"
	call	dodecom			; add keyword+def using DEF MAC below
	jc	docomx			; c = failure
	call	docnv			; convert macro string
	jc	docomx			; c = failure
	mov	max,1			; temp for counting 1 + number args
	mov	word ptr rdbuf+4,' 1'	; number of first variable
docom3:	mov	word ptr rdbuf,0	; clear length field, install \%x name
	mov	word ptr rdbuf+2,'%\'	; start with '\%1 '
	mov	word ptr rdbuf+6,0	; clear text field
	mov	tempptr,offset rdbuf+6	; pointer to location of found word
	xor	ch,ch			; make cx = 1 - 9
	mov	cl,rdbuf+4		; cx = word # of interest, for getwrd
	sub	cl,'0'			; remove ascii bias
	mov	si,offset decbuf	; source = work buffer (borrowed)
	call	getwrd			; get CX-th word from  work buf (1-9)
	cmp	deftemp,0		; length of word, was it found?
	je	docom4			; e = no, end variable definition part
	add	deftemp,4		; count '\%n ' in command line length
	inc	max			; one more argument
	mov	cx,deftemp		; command length for dodecom
	call	dodecom			; add keyword+def using DEF MAC below
	jc	docomx			; c = failure
	inc	rdbuf+4			; inc number of variable in '\%n '
	cmp	rdbuf+4,'9'
	jbe	docom3			; do '1' through '9', if available

docom4:	call	takopen_macro		; create the DO the macro itself
	jc	docomx			; c = failure
	mov	bx,takadr		; point to current structure
	push	es
	mov	es,domacptr		; segment of macro definition string
	mov	[bx].takbuf,es		; remember in Take structure
	mov	cx,es:word ptr [0]	; length of definition string
	pop	es
	mov	[bx].takcnt,cx		; # of unread chars in buffer
	mov	cx,max			; 1 + number of arguments
	mov	[bx].takargc,cx
	clc				; success
	ret

docomx:	call	unhidemac		; recover hidden variables
	stc				; say failure
	ret
DOCOM	ENDP

; Create a new macro, named <0>T<taklev digit>, with the command line of the
; original macro modified to replace bare commas with Carriage Returns.
; Enter with domacptr pointing at the segment of the macro's definition.
; Return with domacptr pointing at the new buffer created by dodecom.
docnv	proc	near
	cmp	taklev,maxtak		; room in take level?
	jb	docnv1			; b = yes
	mov	dx,offset ermes4	; say too many Take files
	mov	ah,prstr		; display error message
	int	dos
	stc				; set carry for failure
	ret

docnv1:	push	es
	push	temp			; paren counter
	mov	temp,0			; none seen yet
	mov	word ptr rdbuf,3	; length of name, 3 bytes <null>Tn
	mov	rdbuf+2,0
	mov	rdbuf+3,'T'		; name of <null>Tn
	mov	al,taklev		; Take level digit
	inc	al			; for what will be next Take level
	add	al,'0'			; add ascii bias
	mov	rdbuf+4,al		; last of the name
	mov	rdbuf+5,' '		; separate dummy definition
	mov	ax,domacptr
	mov	es,ax			; seg of definition
	xor	si,si			; offset of definition
	mov	di,offset rdbuf+6	; destination
	mov	cx,es:[si]		; read count of string
	add	si,2			; point at string itself
	xor	bx,bx			; brace count
	mov	dl,braceop		; opening brace (we count them up)
	mov	dh,bracecl		; closing brace (we count them down)
	xor	bp,bp			; offset of last closing brace+1
	cmp	es:[si],dl		; starts with opening brace?
	jne	docnv2			; ne = no
	inc	si			; skip this new leading space
	dec	cx			; and don't count it either
	inc	bh			; say opening brace was found
docnv2:	mov	al,es:[si]		; read a char
	inc	si
	cmp	al,dl			; opening brace?
	jne	docnv3			; ne = no
	inc	bl			; count brace level
	jmp	short docnv5
docnv3:	cmp	al,dh			; closing brace?
	jne	docnv4			; ne = no
	dec	bl
	or	bl,bl			; below 0?
	jg	docnv4			; g = no, still in braces
	jz	docnv5			; matching interior braces
	or	bh,bh			; was there an opening brace?
	jz	docnv5			; z = no, record this one
	xor	bx,bx			; found opening match, reset to zero
	jmp	short docnv6		; omit the brace
docnv4:	
	cmp	al,'('			; opening paren?
	jne	docnv8			; ne = no
	inc	byte ptr temp		; count paren level
	jmp	short docnv5
docnv8:	cmp	al,')'			; closing paren?
	jne	docnv9			; ne = no
	dec	byte ptr temp
	cmp	byte ptr temp,0		; below 0?
	jg	docnv5			; g = no, still in paren
	mov	temp,0			; in case underrun, reset to zero
	jmp	docnv5			; matching paren
docnv9:	cmp	byte ptr temp,0		; in paren clause?
	jne	docnv5			; ne = yes, treat comma as literal
	or	bl,bl			; in braced clause?
	jnz	docnv5			; nz = yes, treat comma as literal
	cmp	al,','			; unprotected comma?
	jne	docnv5			; ne = no
	mov	al,CR			; replace bare comma with CR
docnv5:	mov	[di],al
	inc	di
docnv6:	loop	docnv2
	pop	temp
	pop	es
	cmp	byte ptr [di-1],cr	; ends on a Carriage Return?
	je	docnv7			; e = yes
	mov	byte ptr [di],cr	; insert one to do sync execution
	inc	di
docnv7:	mov	byte ptr [di],0		; null terminator, for safety
	sub	di,offset rdbuf+2	; start of string (skips count)
	mov	cx,di			; length of string
	call	dodecom			; define macro from rdbuf+2
	mov	domacptr,ax		; seg of new string
	ret
docnv	endp

; Extract CX-th word (cx = 1-9) from buffer (DI). Enter with si = source
; string and tempptr pointing at destination. Returns deftemp (count) of
; transferred characters. Allow string in curly braces to exist as a word.
; Adjacent curly braced strings are separate "words":
;    {this is word-one}{this is word-two}word-three.
; First level curly braces are removed.
; All registers preserved.
getwrd	proc	near
	push	si
	push	di
	push	es
	push	ds
	pop	es			; set es to data segment
getwr1:	push	cx			; save word counter (1-9)
	mov	deftemp,0		; no bytes transferred yet
	mov	di,tempptr		; where to store word/string
	mov	byte ptr [di],0		; clear destination
	mov	dx,si			; start looking here in work buf
	call	strlen			; cx = remaining length of work buf
	xor	ah,ah			; clear brace counter for cx=0 case
	jcxz	getwr6			; z = nothing there, quit
getwr2:	lodsb
	cmp	al,' '			; skip leading whitespace
	loope	getwr2
	dec	si			; return to break char
					; Parse curly brace delimited string
					; end with si after closing brace
	xor	ah,ah			; non-zero if leading brace seen
	xor	dl,dl			; assume "opening brace" is a null
	mov	dh,' '			; assume "closing brace" is a space
	mov	cx,1			; we are at brace level 1
	cmp	byte ptr [si],braceop	; starting with a real opening brace?
	jne	getwr3			; ne = no
	inc	si			; skip opening brace
	mov	dl,braceop		; opening brace (we count them up)
	mov	dh,bracecl		; closing brace (we count them down)
	inc	ah			; say opening brace seen
getwr3:	cld				; search forward
	lodsb				; read a char
	stosb				; store in output buffer
	or	al,al			; at end of text?
	jnz	getwr3a			; nz = no
	dec	si			; stay at null terminator
	dec	di
	jmp	short getwr6		; we are done with this "word"
getwr3a:inc	deftemp			; count copied char
	cmp	al,dl			; an opening brace?
	jne	getwr4			; ne = no
	inc	cx			; yes, increment brace level
	jmp	short getwr3		;  and continue scanning

getwr4:	cmp	al,dh			; closing brace?
	jne	getwr3			; ne = no, continue scanning
	dec	cx			; yes, decrement brace level
	cmp	byte ptr [si],0		; have we just read the last char?
	jne	getwr5			; no, continue scanning
	xor	cx,cx			; yes, this is the closing brace
getwr5:	or	cx,cx			; at level 0?
	jg	getwr3			; g = no, #opening <> #closing braces
	dec	di			; don't write closing brace
	dec	deftemp			; do not count closing brace
getwr6:	pop	cx			; recover word counter
	mov	byte ptr [di],0
	jcxz	getwr7			; just in case
	loop	getwr1			; do until desired word is copied
getwr7:	cmp	deftemp,0		; any real contents found?
	jne	getwr8			; ne = yes
	or	ah,ah			; leading brace seen?
	jz	getwr8			; z = no, nothing at all
	inc	deftemp			; count {} as an empty arg but an arg
getwr8:	pop	es
	pop	di
	pop	si
	ret
getwrd	endp

; Get macro name, given the action pointer in domacptr.
; Return rdbuf as word:length that follows, then "\%0 macro-name"
getname proc	near
	push	bx
	push	cx
	push	dx
	push	si
	push	di
	mov	dx,domacptr		; action word to be matched
	mov	bx,offset mcctab+1	; table of macro names, skip count
	mov	word ptr rdbuf,4	; name length and space
	mov	word ptr rdbuf+2,'%\'	; define '\%0 '
	mov	word ptr rdbuf+4,' 0'
	mov	cl,mcctab		; number of entries
	xor	ch,ch
	jcxz	getnam3			; z = empty table
getnam1:push	cx
	mov	cx,[bx]			; length of name
	mov	si,bx			; point at structure member
	add	si,2			; plus count
	add	si,cx			; plus length of name
	mov	ax,[si]			; get action word
	cmp	ax,dx			; correct action word?
	jne	getnam2			; ne = no
	push	es
	push	ds
	pop	es
	add	word ptr rdbuf,cx	; length of macro \%0 + name
	mov	di,offset rdbuf+6	; where to store text
	mov	si,bx
	add	si,2			; source of text
	cld
	rep	movsb			; copy name to rdbuf+6
	mov	byte ptr [di],0		; null terminator
	pop	es
	pop	cx
	jmp	short getnam3		; exit
getnam2:mov	ax,[bx]			; get length of name
	add	ax,4			; plus count and word pointer
	add	bx,ax			; point to next entry
	pop	cx
	loop	getnam1			; look at next entry
getnam3:pop	di
	pop	si
	pop	dx
	pop	cx
	pop	bx
	ret
getname	endp

; Renames macros \%0..\%9 by changing the first two characters of the name
; to be <null><taklev>. Used to preserve old \%n macros and not show them.
; Return carry set if nothing is hidden.
hidemac proc	near
	push	bx
	push	cx
	mov	bx,offset mcctab+1	; table of macro names, skip count
	mov	cl,mcctab		; number of entries
	xor	ch,ch
	jcxz	hidema3			; z = empty table
hidema1:cmp	word ptr [bx],3		; name length, do three byte names
	jne	hidema2			; ne = not three chars
	cmp	word ptr [bx+2],'%\'	; starts with correct prefix?
	jne	hidema2			; ne = no
	mov	al,byte ptr [bx+4]	; third char of name
	cmp	al,'0'			; in digits?
	jb	hidema2			; b = no
	cmp	al,'9'
	ja	hidema2
	xor	al,al			; prepare new prefix
	mov	ah,taklev		; of <null><taklev>
	mov	[bx+2],ax		; change the macro name
hidema2:mov	ax,[bx]			; get length of name
	add	ax,4			; plus count and word pointer
	add	bx,ax			; point to next entry
	loop	hidema1			; look at next entry
	clc
	pop	cx
	pop	bx
	ret
hidema3:stc				; say nothing has been hidden
	pop	cx
	pop	bx
	ret
hidemac	endp
 
; Removes all current \%0..\%9 macros and renames <null><taklev> macros by
; changing the first two characters of the names \%n from <null><taklev> to
; be "\%". Used to recover old \%n macros from hidemac.
unhidemac proc	near
	push	cx
	push	dx
	push	di     			; do rename of <null><taklev>n
	mov	di,offset unhidetmp	; work buffer
	mov	word ptr [di],3		; count for three byte name
	mov	word ptr [di+2],'%\'	; prefix
	mov	word ptr [di+4],' 9'	; digit
	mov	cx,10			; digits 0..9
unhide1:call	remtab			; remove \%digit macro
	dec	byte ptr [di+4]		; previous digit
	loop	unhide1

	mov	di,offset mcctab+1	; table of macro names, skip count
	mov	cl,mcctab		; number of entries
	xor	ch,ch
	jcxz	unhide4			; z = empty table
	xor	dl,dl			; dx = macro prefix to examine
	mov	dh,taklev		; di => length word, name string
unhide2:cmp	word ptr [di],3		; do only "<null><taklev>n" names
	jne	unhide3			; ne = not three chars
	mov	al,[di+4]		; get char of name
	cmp	al,'0'			; is it a char in range for \%0..\%9?
	jb	unhide3			; b = no
	cmp	al,'9'			; still in range?
	ja	unhide3			; a = no
	cmp	word ptr [di+2],dx	; starts with <null><taklev> prefix?
	jne	unhide3			; ne = no
	mov	[di+2],'%\'		; change the macro name back to "\%n"
unhide3:mov	ax,[di]			; get length of name
	add	ax,4			; plus count and word pointer
	add	di,ax			; point to next entry
	loop	unhide2			; look at next entry
unhide4:pop	di
	pop	dx
	pop	cx
	ret
unhidemac endp

; DEFINE and ASSIGN macro commands
; Data structures comments. Macro name is stored in table mcctab as if we
; had used macro mkeyw, such as       mkeyw 'mymac',offset my_definition.
; In detail:	dw	length of name
;		db	'name'
;		dw	segment:0 of definition string
; Mcctab begins with a byte holding the number of macros in the table; one,
;  IBM, is established at assembly time. Mcctab is 10*macmax bytes long.
; Pointer mccptr holds the offset of the next free byte in mcctab.
; Definition strings are stored in individually allocated memory as
;		dw	length of definition string below
;		db	'definition string'
; A new definition is read into buffer rdbuf+2, where word rdbuf is reserved
;  to hold the length of the macro's name during intermediate processing.
; If the definition is absent then the macro is removed from the tables.
;
; ASSIGN is equivalent to DEFINE, except in the definition string substitution
; variable names are expanded to their definitions.
; DEFINE does not expand substitution variables.
; Both commands will remove a first level curly brace pair if, and only if,
; the definition begins and ends with them (trailing whitespace is allowed).
; HIDE_ASSIGN and HIDE_DEFINE are like ASSIGN and DEFINE except the
; destination name is expanded for substitution variables.
;
HIDE_ASSIGN	proc	near
	mov	defkind,2		; flag command as ASSIGN, vs DEFINE
	jmp	short dodefcom		; common code
HIDE_ASSIGN	endp
HIDE_DEFINE	proc	near
	mov	defkind,3		; flag command as DEFINE, vs ASSIGN
	jmp	short dodefcom		; common code
HIDE_DEFINE	endp

ASSIGN	PROC	NEAR
	mov	defkind,0		; flag command as ASSIGN, vs DEFINE
	jmp	short dodefcom		; common code
ASSIGN	ENDP

DODEF	PROC	NEAR
	mov	defkind,1		; flag command as DEFINE, vs ASSIGN
DODEFCOM:
	test	defkind,2		; HIDE_assign/define?
	jnz	dodef0			; nz = yes, expand destination
	mov	comand.cmper,1		; do not react to '\%' in macro name
dodef0:	mov	ah,cmword
	mov	dx,offset rdbuf+2	; buffer for macro name
	mov	word ptr rdbuf,0
	mov	comand.cmblen,length rdbuf-2 ; length of analysis buffer
	mov	bx,offset macmsg
	call	comnd			; get macro name
	jnc	dodef1			; nc = success
	ret				; failure
dodef1:	or	ax,ax			; null entry?
	jnz	dodef2			; nz = no
	mov	dx,offset ermes6	; more parameters needed
	jmp	reterr

dodef2:	mov	bx,offset rdbuf+2	; start of string
	cmp	word ptr [bx],'%\'	; \%<char> substitution variable?
	jne	dodef2b			; ne = no
	cmp	ax,2			; count, but missing <char>?
	ja	dodef2a			; a = no
	mov	byte ptr [bx+2],'_'	; slip in an underscore
dodef2a:mov	ax,3			; limit to \%<char>, one char name

dodef2b:add	bx,ax			; point to string terminator
	mov	byte ptr [bx],' '	; replace null with space separator
	mov	word ptr [bx+1],0	; terminator, in case no command
	inc	bx			; where definition will start
	mov	ax,length rdbuf-2	; usable length of rdbuf
	sub	ax,bx			; - (current offset - offset rdbuf)
	add	ax,offset rdbuf		; ax = amount of buffer used
	mov	comand.cmblen,ax	; our new buffer length
	mov	al,defkind		; get ASSIGN/DEFINE flag
	and	al,1			; pick out source flag bit
	mov	comand.cmper,al		; react (ASSIGN) to '\%' in definition
	mov	ah,cmline		; get a line of text
	mov	dx,offset macmsg	; help, bx is buffer offset
	push	bx			; save starting offset
	call	comnd			; get macro definition text
	pop	bx
	jnc	dodef3			; nc = success
	ret				; failure
dodef3:	test	defkind,1		; ASSIGN?
	jnz	dodef4			; nz = no, DEFINE, use cmd as-is
	mov	si,bx			; source
	mov	di,bx			; destination
	push	bx
	call	cnvstr			; convert {..}
	pop	bx
	mov	ax,cx			; cx = length of returned cmd
dodef4:	add	bx,ax			; end of command line
	mov	cx,bx
	sub	cx,offset rdbuf+2	; length of command line
;;	jmp	dodecom
DODEF	ENDP

; Make a macro table entry and allocate buffer space.
; Enter with rdbuf+2 et seq = <macro name><spaces><arg><spaces><arg> ...
; and CX = byte count of line, starting at rdbuf+2.
; Word rdbuf+0 computed here as length of keyword.
; Allocates memory based on analyzed size of command line, returns memory
; segment in macptr and in AX. Returns carry set if failure.
DODECOM	PROC	NEAR
	push	si			; macro name in rdbuf+2 et seq
	push	di			; cmd line length in deftemp
	push	es
	push	ds			; address data segment
	pop	es
	mov	deftemp,cx		; cmd line len, cx = running counter
	mov	rdbuf,0			; number of chars in keyword so far
					; uppercase the keyword, look for end
	mov	si,offset rdbuf+2	; point at macro name itself
	xor	dx,dx			; a counter
	cld				; strings go forward
dode2:	lodsb				; get a byte
	cmp	al,'a'			; map lower case to upper
	jb	dode3
	cmp	al,'z'
	ja	dode3
	sub	al,'a'-'A'
	mov	[si-1],al		; uppercase if necessary
dode3:	inc	dx			; increment char count of keyword
	cmp	al,' '			; is this the break character?
	loopne	dode2			; no, loop thru rest of word
	jne	dode4			; ne = did not end with break char
	dec	dx			; yes, don't count in length
dode4:	mov	di,offset rdbuf		; point at mac name length
	mov	[di],dx			; insert length in rdbuf
	push	dx			; save length around call
	call	remtab			; remove any duplicate keyword
					; check for free space for keyword
	pop	ax			; keyword text length
	add	ax,4			; plus count and word pointer
	add	ax,mccptr		; add to free space pointer
	cmp	ax,offset mcctab+mcclen ; enough room for name?
	jb	dode5			; b = yes
	mov	dx,offset ermes1	; too many macro names
	pop	es
	pop	di
	pop	si
	jmp	reterr

dode5:	mov	dx,si			; trim off leading/trailing spaces
	call	strlen
;	mov	di,si			; si = definition source address
	jcxz	dode5a			; z = empty string
;	add	di,cx			; start + length
;	dec	di			; last char of string
;	std
;	mov	al,' '			; scan off trailing spaces
;	repe	scasb
;	cld
;	mov	byte ptr [di+2],0	; plant new terminator
dode5a:	mov	dx,si
	call	strlen			; get new length into cx
	cld
	mov	di,si			; scan after keyword name
	mov	al,' '			; remove leading spaces in string
	repe	scasb
	jne	dode6			; ne = have some text
	pop	es			; all spaces
	pop	di
	pop	si
	clc				; success
	ret
dode6:	dec	di			; offset auto increment of rep
	mov	si,di			; new start of string
	mov	dx,si			; source of definition text
	call	strlen			; get length of string into cx
	mov	deftemp,cx		; remember it here
					; install new keyword
	jcxz	dode10			; z = no def, exit now
	mov	bx,cx			; string length, in bytes
	add	bx,2+1+15		; count + null term + round up
	mov	cl,4
	shr	bx,cl			; convert to paragraphs (divide by 16)
	mov	cx,bx			; remember desired paragraphs
	mov	ah,alloc		; allocate a memory block
	int	dos
	jc	dode12			; c = error, not enough memory
 	cmp	bx,cx			; obtained vs wanted
	jae	dode7			; ae = enough
	mov	es,ax			; allocated segment
	mov	ah,freemem		; free it again
	int	dos
	jmp	short dode12		; quit here

dode7:	mov	macptr,ax		; store new segment
	mov	es,ax			; segment of string
	xor	di,di			; offset of count word
	mov	cx,deftemp		; length of definition string
	mov	ax,cx
	cld
	stosw				; store length of string
	rep	movsb			; copy string
	mov	bx,offset mcctab	
	mov	dx,offset rdbuf		; count word + name string
	call	addtab
dode10:	mov	ax,macptr		; return buffer segment to caller
	pop	es
	pop	di
	pop	si
	clc				; success
	ret
dode12:	pop	es			; no memory, clean stack
	pop	di
	pop	si
	mov	dx,offset ermes2	; no room for definition
	mov	ah,prstr
	int	dos
	stc
	ret
DODECOM	ENDP

; ASK <variable or macro name> <prompt string>
; Defines indicated variable/macro with text from user at keyboard or pipe
; (but not from a Take/macro). Prompt string is required.
; ASKQ does the same, but does not echo user's response.
ASKQ	PROC	NEAR
	mov	askecho,1		; temp to flag as Quiet version
	mov	temp,0
	jmp	short ask0		; do common code
ASKQ	ENDP

GETC	PROC	NEAR
	mov	askecho,1
	mov	temp,1			; signal as getc
	jmp	short ask0		; do common code
GETC	ENDP

ASK	PROC	NEAR
	mov	askecho,0		; temp to flag as echoing version
	mov	temp,0
ask0:					; common code for ASK and ASKQ
	mov	dx,offset decbuf+2	; point to work buffer
	mov	word ptr decbuf,0
	mov	bx,offset askhlp1	; help
	mov	comand.cmper,1		; do not expand variable name
	mov	ah,cmword		; get variable name
	call	comnd
	jnc	ask1			; nc = success
	ret				; failure
ask1:	or	ax,ax			; anything given?
	jnz	ask2			; nz = yes
	mov	dx,offset ermes6	; more parameters needed
	jmp	reterr

ask2:	cmp	word ptr decbuf+2,'%\'	; \%<char> substitution variable?
	jne	ask2b			; ne = no
	cmp	ax,2			; but missing <char>
	ja	ask2a			; a = no
	mov	rdbuf+4,'_'		; slip in an underscore
ask2a:	mov	ax,3			; limit to a single char
ask2b:	mov	bx,offset decbuf+2	; start of name
	add	bx,ax			; plus length of variable name
	mov	word ptr [bx],0+' '	; put space+NULL separator after name
					; get ASK command prompt string
	mov	bx,offset decbuf+129	; borrowed buffer for prompt
	mov	byte ptr [bx],0		; safety terminator
	mov	dx,offset askhlp2
	mov	comand.cmblen,127	; our buffer length
	sub	comand.cmblen,ax	;  minus part used above
	mov	ah,cmline		; get prompt string
	call	comnd
	jnc	ask3			; nc = success
	ret				; failure
ask3:	or	ax,ax			; anything given?
	jnz	ask4			; nz = yes
	mov	dx,offset ermes6	; more parameters needed
	jmp	reterr

ask4:	mov	word ptr [bx],0020h	; printing terminator for prompt
	mov	si,offset decbuf+129
	mov	di,si			; convert in-place
	call	cnvlin			; convert backslash items
	mov	comand.cmdirect,1	; say read directly from kbd/file
	mov	dx,si			; converted prompt string, asciiz
 	call	prompt			; use our prompt
	mov	bx,offset rdbuf+129	; use this buffer for raw user input
	mov	word ptr [bx],0		; insert terminator
	mov	dl,askecho		; get echo/quiet flag
	mov	comand.cmquiet,dl	; 0 if echoing
	mov	dx,offset askhlp3	; help for user input
	mov	comand.cmdirect,1	; say read directly from kbd/file
	cmp	temp,1			; getc?
	jne	ask8			; ne = no
ask5:	mov	dl,0ffh
	mov	ah,dconio		; read console
	int	dos
	jz	ask5			; z = nothing there
	cmp	al,3			; Control-C?
	jne	ask6			; ne = no
	mov	flags.cxzflg,'C'	; return Control-C status to parser
	stc				; return error
	ret
ask6:	or	al,al			; scan code being returned?
	jnz	ask7			; nz = no, accept
	mov	ah,dconio		; read and discard second byte
	mov	dl,0ffh
	int	dos
	mov	ah,conout		; ring the bell
	mov	dl,bell
	int	dos
	jmp	short ask5		; else unknown, ignore

ask7:	xor	ah,ah			; null terminator
	cmp	al,'('			; function delimiters?
	je	ask7a			; e = yes
	cmp	al,')'
	je	ask7a			; e = yes
	cmp	al,'{'			; string delimiters?
	je	ask7a			; e = yes
	cmp	al,'}'
	je	ask7a			; e = yes
	cmp	al,DEL			; this control?
	je	ask7a			; e = yes
	cmp	al,' '			; senstive (controls + space)?
	ja	ask7b			; a = no
ask7a:	mov	rdbuf+129,'\'		; numeric form prefix
	mov	di,offset rdbuf+130
	call	dec2di			; convert to \decimal, asciiz
	clc
	jmp	short ask9
ask7b:	mov	word ptr rdbuf+129,ax	; byte and terminator
	clc
	jmp	short ask9		; process byte

ask8:	mov	ah,cmline		; read user's input string
	mov	comand.cmper,1		; do not expand variable names
	call	comnd
ask9:	mov	comand.cmquiet,0	; permit echoing again
	mov	comand.cmdirect,0	; read normally again
	jnc	ask10			; nc = success
	ret				; Control-C, quit now
ask10:	mov	cx,ax			; length of entry
	mov	di,offset rdbuf+2
	mov	si,offset decbuf+2
	call	strcpy
	mov	dx,di
	call	strlen
	add	di,cx
	mov	si,offset rdbuf+129
	call	strcpy
 	mov	dx,offset rdbuf+2	; command buffer beginning
	call	strlen			; CX=len of <variable>< ><user string>
	jmp	DODECOM			; define the macro/variable and exit
ASK	ENDP

; GETOK <prompt>   displays prompt or default prompt, asks for affirmative
; user reply, returns success/fail status.
getok	proc	near
	mov	bx,offset rdbuf		; point to work buffer
	mov	word ptr [bx],0		; safety terminator
	mov	dx,offset getokhlp
	mov	comand.cmblen,127	; our buffer length
	mov	comand.cmcr,1		; bare cr is ok
	mov	ah,cmline		; get prompt string
	mov	comand.cmcr,0
	call	comnd
	jnc	getok1			; nc = success
	ret				; failure
getok1:	or	ax,ax			; anything given?
	jnz	getok2			; nz = yes
	mov	si,offset getdef	; default prompt
	mov	di,offset rdbuf
	call	strcpy			; paste in default prompt

getok2:	mov	si,offset rdbuf
	mov	di,si			; convert in-place
	call	cnvlin			; convert backslash items
	mov	dx,si
	call	strlen			; get length to cx
	mov	bx,cx
	mov	word ptr [si+bx],0020h	; printing terminator for prompt
getok3:	mov	dx,offset rdbuf		; converted prompt string, asciiz
	mov	comand.cmdirect,1	; force input from kbd/file
 	call	prompt			; use our prompt
	mov	flags.cxzflg,0		; clear Control-C indicator
	mov	dx,offset gettab	; use this table
	xor	bx,bx			; table is help
	mov	comand.cmcr,1		; null response permitted
	mov	ah,cmkey		; read user's input word
	call	comnd
	mov	comand.cmcr,0
	mov	comand.cmdirect,0	; end force input from kbd/file
	jnc	getok4			; nc = success
	cmp	flags.cxzflg,'C'	; did user type Control-C to quit?
	jne	short getok3		; ne = no, syntax failure, reprompt
	stc				; set carry flag again
	jmp	short getok5		; exit failure
getok4:	push	bx
	mov	comand.cmdirect,1	; force input from kbd/file
	mov	ah,cmeol		; get c/r confirmation
	call	comnd
	mov	comand.cmdirect,0	; end force input from kbd/file
	pop	bx			; recover keyword 16 bit value
getok5:	jnc	getok6			; nc = success
	mov	kstatus,ksgen		; failure
	ret				; Control-C, quit now
getok6:	mov	kstatus,bx		; return status
	ret
getok	endp

; Initialize macro IBM at Kermit startup time
initibm	proc	near
	mov	si,offset ibmmac	; text of IBM macro
	mov	di,offset rdbuf+2	; where command lines go
	call	strcpy			; copy it there
	mov	dx,di			; get length of command line
	call	strlen			; set cx to length, for dodecom
	call	dodecom			; now define the macro
	mov	rdbuf+2,0
	mov	si,offset dialmac
	mov	di,offset rdbuf+2
	call	strcpy
	mov	dx,di
	call	strlen
	inc	cx
	jmp	dodecom			; now define the macro
initibm	endp

; Open an text subsititution macro. No buffer is allocated.
takopen_sub	proc	far
	cmp	taklev,maxtak		; room in take level?
	jb	takosub1		; b = yes
	mov	dx,offset ermes4	; say too many Take files
	mov	ah,prstr		; display error message
	int	dos
	stc				; set carry for failure
	ret

takosub1:push	ax
	push	bx
	mov	bx,takadr		; previous take structure
	push	[bx].takargc		; stash argument count
	push	[bx].takctr		; stash COUNT
	add	takadr,size takinfo	; pointer to new Take structure
	inc	taklev
	mov	bx,takadr		; pointer to new Take structure
	pop	[bx].takctr		; copy in old count
	pop	[bx].takargc		; copy in old argc
	xor	ax,ax
	mov	[bx].takbuf,ax		; seg of memory block
	mov	[bx].takptr,2		; where to read first
	mov	[bx].takcnt,ax		; unread bytes in buffer
	mov	[bx].takper,al		; expand macros
	mov	[bx].takattr,al		; attribute, none
	mov	[bx].taktyp,take_sub	; kind is text substitution
	pop	bx
	pop	ax
	clc
	ret
takopen_sub	endp

; Open take structure for file input. Buffer of tbufsiz is preallocated
; and pointed to by takbuf:takptr.
takopen_file	proc	far
	call	takopen_sub		; do substitution macro busy work
	jnc	takofil1		; nc = success so far
	ret				; fail
takofil1:push	bx
	mov	bx,takadr
	mov	ax,tbufsiz		; size of buffer
	call	malloc
	jnc	takofil2		; nc = success
	pop	bx
	ret				; fail

takofil2:mov	[bx].takbuf,ax		; seg of allocated buffer
	mov	[bx].takattr,take_malloc ; remember so takclos will free it
	mov	[bx].taktyp,take_file	; disk file kind
	mov	[bx].takhnd,0		; file handle
	mov	word ptr [bx].takseek,0
	mov	word ptr [bx].takseek+2,0 ; seek distance, bytes
	or	[bx].takattr,take_autocr ; need auto CR at EOF
	pop	bx
	clc
	ret
takopen_file	endp

; Open a disk based Take file structure. No buffer is allocated
; Return carry clear for success, carry set for failure.

takopen_macro	proc	far
	push	ax
	push	bx
	push	cx
	push	dx
	cmp	taklev,maxtak		; room in take level?
	jb	takoma1			; b = yes
	mov	dx,offset ermes4	; say too many Take files
	mov	ah,prstr		; display error message
	int	dos
	stc				; set carry for failure
	jmp	takoma2

takoma1:xor	ax,ax
	xor	cx,cx
	cmp	taklev,0		; at top level now?
	je	takoma3			; e = yes
	mov	bx,takadr
	mov	ax,[bx].takargc		; get argument count
	mov	cx,[bx].takctr		; get COUNT
takoma3:add	takadr,size takinfo	; pointer to new Take structure
	inc	taklev
	mov	bx,takadr		; pointer to new Take structure
	mov	[bx].takargc,ax		; copy in old argc
	mov	[bx].takctr,cx		; copy in old count
	xor	ax,ax
	mov	[bx].takbuf,ax		; no segment of Take buffer
	mov	[bx].takcnt,ax		; number of unread bytes
	mov	[bx].takptr,2		; init pointer to definition itself
	mov	[bx].takper,al		; expand macros
	mov	[bx].takattr,take_autocr ; need auto CR at EOF
	mov	[bx].taktyp,take_macro	; macro
	clc				; carry clear for success
takoma2:pop	dx
	pop	cx
	pop	bx
	pop	ax
	ret
takopen_macro endp

; Close Take file. Enter at Take level to be closed.
; Closes disk file, pops Take level.

TAKCLOS	PROC	NEAR
	cmp	taklev,0		; anything to close?
	jle	takclo3			; le = no
	push	ax
	push	bx
	push	cx
	mov	bx,takadr
	test	[bx].takattr,take_malloc ; new malloc'd buffer in takbuf?
	jz	takclo4			; z = no
	mov	ax,[bx].takbuf
	push	es
	mov	es,ax			; seg of separately malloc'd buffer
	mov	ah,freemem		; free it
	int	dos
	pop	es
	and	[bx].takattr,not take_malloc ; no extra buffer to free
takclo4:xor	al,al			; get a null
	xchg	al,[bx].taktyp
	mov	[bx].takper,0		; macro expansion flag
	cmp	al,take_comand		; doing a parser internal command?
	je	takclo5a		; e = yes
	cmp	al,take_sub		; text subsititution?
	jne	takclo5			; ne = no
	jmp	takclo5a

takclo5:cmp	al,take_file		; disk file?
	jne	takclo6			; ne = no
	mov	word ptr [bx].takseek,0	; disk file
	mov	word ptr [bx].takseek+2,0 ; seek distance, bytes
	mov	[bx].takattr,0		; clear memory area, just in case
	mov	bx,[bx].takhnd		; get file handle
	mov	ah,close2		; close file
	int	dos
takclo5a:dec	taklev
	sub	takadr,size takinfo	; get previous Take's address
	jmp	takclo2

takclo6:mov	bx,takadr		; point to Take structure
	dec	taklev			; pop macro Take level
	sub	takadr,size takinfo	; get previous Take's address
	call	unhidemac		; rename previous \%n macros

takclo2:pop	cx
	pop	bx
	pop	ax			; close prev Take/Macro if at EOF too
 	cmp	taklev,0		; Take/macro still open?
	jle	takclo3			; le = no
	push	bx
	mov	bx,takadr		; get it's address
	cmp	[bx].takcnt,0		; is it at EOF?
	pop	bx
	ja	takclo3			; a = no
	call	takrd			; empty Take, try filling buf again
	push	bx
	mov	bx,takadr		; get it's address
	cmp	[bx].takcnt,0		; is it at EOF?
	pop	bx
	ja	takclo3			; a = not an empty Take file
	jmp	takclos			; yes, close it too
takclo3:ret
TAKCLOS	ENDP

; POP/END command. Defend against command parser closing the Take/Macro at
; the end of file. Return optional trailing number in ERRORLEVEL (errlev).
POPCMD	proc	near
	mov	ah,cmline		; get optional error value and msg
	mov	bx,offset rdbuf+1
	mov	rdbuf,'\'		; in case the user did not specify
	mov	dx,offset pophlp	; help on numerical argument
	mov	comand.cmcr,1		; bare c/r's allowed
	call	comnd
	mov	comand.cmcr,0		; restore normal state
	jc	popcmd3			; c = failure
	mov	ah,cmeol		; confirm
	call	comnd
	jc	popcmd3
	mov	si,offset rdbuf
	cmp	byte ptr [si+1],'\'	; user specified?
	jne	popcmd1			; ne = no, use ours
	inc	si			; yes, use theirs
popcmd1:call	katoi			; convert to number in ax
	jc	popcmd2			; c = not a number
	mov	errlev,al		; return value in ERRORLEVEL
	mov	kstatus,ax		; and in STATUS
	mov	di,si			; msg pointer, convert in-place
	call	cnvlin			; convert braces and numbers
popcmd4:lodsb				; read a msg char
	or	al,al			; null terminator?
	jz	popcmd2			; z = empty string
	cmp	al,' '			; leading white space?
	je	popcmd4			; be = leading white space
	dec	si			; backup to non-white char
	mov	di,si			; msg pointer, convert in-place
	call	cnvlin			; convert braces and numbers
	mov	dx,offset crlf
	mov	ah,prstr
	int	dos
	mov	dx,si			; message
	call	prtasz
popcmd2:mov	al,oldtak		; Take level previous to this cmd
	sub	al,taklev		; minus Take level now
	ja	popcmd3			; a = have exited macro already
	jmp	takclos			; close current Macro/Take
popcmd3:ret
POPCMD	endp

; add an entry to a keyword table
; enter with bx = table address, dx = ptr to new entry, macptr = string seg
; mccptr = offset of free bytes in table mcctab.
; no check is made to see if the entry fits in the table.
addtab	proc	near
	push	cx
	push	si
	push	es
	push	bp
	cld
	mov	ax,ds
	mov	es,ax		; address data segment
	mov	bp,bx		; remember where macro name table starts
	mov	cl,[bx]		; pick up length of table
	xor	ch,ch
	inc	bx		; point to actual table
	jcxz	addta4		; cx = 0 if table is presently empty

addta1:	push	cx		; preserve count
	mov	si,dx		; point to entry
	lodsw			; get length of new entry
	mov	cx,[bx]		; and length of table entry
	cmp	ax,cx		; are they the same?
	lahf			; remember result of comparison
	jae	addta2		; is new smaller? ae = no, use table length
	mov	cx,ax		; else use length of new entry
addta2:	lea	di,[bx+2]	; point to actual keyword
	repe	cmpsb		; compare strings
	pop	cx		; restore count
	jb	addta4		; below, insert before this one
	jne	addta3		; not below or same, keep going
	sahf			; same. get back result of length comparison
	jb	addta4		; if new len is smaller, insert here
	jne	addta3		; if not same size, keep going
	mov	si,bx		; else this is where entry goes
	jmp	short addta6	; no insertion required
addta3:	mov	ax,[bx]		; length of keyword
	add	bx,ax		; skip this entry
	add	bx,4		; length word and 16 bit value
	loop	addta1		; and keep looking
addta4:	mov	si,bx		; this is first location to move
	mov	di,bx
	inc	ds:byte ptr [bp] ; remember we're adding one
	jcxz	addta6		; z = no more entries, forget this stuff
addta5:	mov	bx,[di]		; get length
	lea	di,[bx+di+4]	; end is origin + length + 4 for len, value
	loop	addta5		; loop thru remaining keywords
	mov	cx,di
	sub	cx,si		; compute # of bytes to move
	push	si		; preserve loc for new entry
	mov	si,di		; first to move is last
	dec	si		; minus one
	mov	di,dx		; new entry
	mov	bx,[di]		; get length
	lea	di,[bx+si+4]	; dest is source + length of new + 4
	std			; move backward
	rep	movsb		; move the table down (compress it)
	cld			; put flag back
	pop	si
addta6:	mov	di,si		; this is where new entry goes
	mov	si,dx		; this is where it comes from
	mov	cx,[si]		; length of name
	add	cx,2		; include count byte
	add	mccptr,cx	; update free space pointer: cnt+name
	rep	movsb		; insert new entry
	mov	ax,macptr	; and string address
	stosw
	add	mccptr,2	; plus string address
	pop	bp
	pop	es
	pop	si
	pop	cx
	ret
addtab	endp

; If new keyword matches an existing one then remove existing keyword,
; its string definition, compress tables mcctab and macbuf, readjust string
; pointers for each macro name, reduce number of macro table entries by one.
; Enter with DI pointing at length word of mac name (followed by mac name).
; Otherwise, exit with no changes.  13 June 1987 [jrd]
remtab	proc	near
	push	ax
	push	bx
	push	cx
	push	si
	push	di
	push	temp			; preserve
	mov	bx,offset mcctab+1	; table of macro keywords
	mov	temp,0			; temp = current keyword
	cmp	byte ptr mcctab,0	; any macros defined?
	jne	remta1			; ne = yes
	jmp	remtax			; else exit now
remta1:					; match table keyword and text word
	mov	si,di			; pointer to user's cnt+name
	mov	cx,[si]			; length of user's macro name
	jcxz	remtax			; empty macro name to remove
	add	si,2			; point to new macro name
	cmp	cx,[bx]			; compare length vs table keyword
	jne	remta4			; ne = not equal lengths, try another
	push	si			; lengths match, how about spelling?
	push	bx
	add	bx,2			; point at start of keyword
remta2:	mov	ah,[bx]			; keyword char
	mov	al,[si]			; new text char
	cmp	al,ah			; test characters
	jne	remta3			; ne = no match
	inc 	si			; move to next char
	inc	bx
	loop	remta2			; loop through entire length
remta3:	pop	bx
	pop	si
	jcxz	remta6			; z: cx = 0, exit with match;
					;  else select next keyword
remta4:	inc	temp			; number of keyword to test next
	mov	cx,temp
	cmp	cl,mcctab		; all done? Recall, temp starts at 0
	jb	remta5			; b = not yet
	jmp	remtax			; exhausted search, unsuccessfully
remta5:	mov	ax,[bx]			; cnt (keyword length from macro)
	add	ax,4			; skip count and word pointer
	add	bx,ax			; bx = start of next keyword slot
	jmp	short remta1		; do another comparison
					; new name already present as a macro
remta6:	cld				; clear macro string and macro name
	push	ds
	pop	es			; set es to data segment
	mov	temp,bx			; save ptr to found keyword
	mov	ax,[bx]			; cnt (keyword length of macro)
	add	ax,2			; skip cnt
	add	bx,ax			; point to string segment field
	add	ax,2			; count segment field bytes
	sub	mccptr,ax		; readjust free space ptr for names
	push	bx
	push	es
	mov	es,[bx]			; segment of string
	mov	ax,ds			; check for being in our data segment
	cmp	ax,[bx]			; same as our data seg?
	je	remta7			; e = yes, don't free that
	mov	ah,freemem		; free that memory block
	int	dos
remta7:	pop	es
	pop	bx
					; clear keyword table mcctab
	add	bx,2			; compute source = next keyword
	mov	si,bx			; address of next keyword
	mov	di,temp			; address of found keyword
	mov	cx,offset mcctab+mcclen ; address of buffer end
	sub	cx,si			; amount to move
	jcxz	remtax			; cx = 0 means none
	rep	movsb			; move down keywords (deletes current)
	dec	mcctab			; one less keyword
remtax:	pop	temp			; recover temp variable
	pop	di
	pop	si
	pop	cx
	pop	bx
	pop	ax
	ret
remtab	endp

; Common Get keyword + Get Confirm sequence. Call with dx = keyword table,
; bx = help message offset. Returns result in BX. Modifies AX, BX.
; Returns carry clear if sucessful else carry set. Used in many places below.
keyend	proc	near
	mov	ah,cmkey
	call	comnd
	jnc	keyend1			; nc = success
	ret				; failure
keyend1:push	bx			; save returned results around call
	mov	ah,cmeol		; get c/r confirmation
	call	comnd
	pop	bx			; recover keyword 16 bit value
	ret				; return with carry from comnd
keyend	endp

srvdsa	proc	near			; DISABLE Server commands
	mov	dx,offset srvdetab
	mov	bx,offset sdshlp
	call	keyend
	jc	srvdsa1			; c = failure
	cmp	apctrap,0		; disable from APC
	jne	srvdsa1			; ne = yes
	or	denyflg,bx		; turn on bit (deny) for that item
srvdsa1:ret
srvdsa	endp

srvena	proc	near			; ENABLE Server commands
	mov	dx,offset srvdetab	; keyword table
	mov	bx,offset sdshlp	; help on keywords
	call	keyend
	jc	srvena1			; c = failure
	cmp	apctrap,0		; disable from APC
	jne	srvena1			; ne = yes
	not	bx			; invert bits
	and	denyflg,bx		; turn off (enable) selected item
srvena1:ret
srvena	endp

; DIAL arg list
; Invokes macro DIAL with args or, if not present, then macro "\0IAL"
; defined above, with args.
dial	proc	near
	mov	kstatus,kssuc		; assume success
	mov	word ptr decbuf,4	; length of our name
	mov	word ptr decbuf+2,'ID'	; prefix with "DIAL "
	mov	word ptr decbuf+4,'LA'
	call	dialcom			; look for macro "DIAL"
	jnc	dial1			; c =failed to find "DIAL"
	mov	si,offset dialmac	; default string "__DIAL"
	mov	di,offset decbuf+2	; destination
	push	es
	mov	ax,seg decbuf
	mov	es,ax
	mov	cx,6			; string length
	cld
	rep	movsb			; copy string
	pop	es
	mov	word ptr decbuf,6	; string length
	call	dialcom			; look for it
	jc	dialx			; c = failed, quit
dial1:	mov	domacptr,bx		; segment of definition string
	mov	comand.cmquiet,0	; permit command echoing
	mov	bx,offset decbuf	; point to borrowed work buffer
	mov	word ptr[bx],0		; clear buffer
	mov	dx,offset dialhlp	; help
	mov	comand.cmblen,length rdbuf ; length of analysis buffer
	mov	ah,cmline		; get line of text, if any
	call	comnd
	jc	dialx			; nc = success
	jmp	docom2			; execute found macro, read args too

dialx:	mov	kstatus,kstake		; Take file command failure
	stc				; fail
	ret
dial	endp

; Worker for DIAL command. Find macro whose name is in offset decbuf+2
; and whose length is in word ptr decbuf.
; Return carry clear and macro seg in BX if success, else carry set.
dialcom	proc	near
	push	si
	push	di
	push	es
	mov	ax,ds
	mov	es,ax			; check for existence of macro
	mov	bx,offset mcctab	; table of macro names
	mov	cl,[bx]			; number of names in table
	xor	ch,ch
	jcxz	dialcx			; z = empty table, do nothing
	inc	bx			; point to length of first name
dialc2:	mov	ax,[bx]			; length of this name
	cmp	ax,word ptr decbuf	; length same as desired keyword?
	jne	dialc3			; ne = no, search again
	mov	si,bx
	add	si,2			; point at first char of macro name
	push	cx			; save name counter
	push	di			; save reg
	mov	cx,word ptr decbuf	; length of name
	mov	di,offset decbuf+2	; point at desired macro name
	push	es			; save reg
	push	ds
	pop	es			; make es use data segment
	cld
	repe	cmpsb			; match strings
	pop	es			; need current si below
	pop	di			; recover saved regs
	pop	cx
	jne	dialc3			; ne = not matched
	add	bx,ax			; length of name
	add	bx,2			; count + name, points at word ptr
	mov	bx,[bx]			; return word ptr in BX
	clc				; say success
	jmp	short dialcx		; and return
	
dialc3:	add	bx,ax			; step to next name, add name length
	add	bx,4			; + count and def word ptr
	loop	dialc2			; try next name
	stc				; say failure
dialcx:	pop	es
	pop	di
	pop	si			; no macro
	ret

dialcom	endp

; This is the SET command
; Called analyzers return carry clear for success, else carry set.
SETCOM	PROC	NEAR			; Dispatch all SET commands from here
	mov	kstatus,kssuc		; global status, success
	mov	dx,offset settab	; Parse a keyword from the set table
	mov	bx,offset sethlp
	mov	ah,cmkey
	call	comnd
	jc	setcom1			; c = failure
	jmp	bx			; execute analyzer routine
setcom1:ret
SETCOM	endp

SETATT	PROC	NEAR			; Set attributes on | off
	mov	dx,offset atttab
	xor	bx,bx
	mov	ah,cmkey
	call	comnd
	jc	setatt3			; c = failure
	mov	dx,bx			; hold results in dx
	cmp	dl,0ffh			; ON/OFF (all of them)?
	je	setatt1			; e = yes
	push	dx
	mov	dx,offset ontab		; get on/off state
	xor	bx,bx
	mov	ah,cmkey
	call	comnd
	pop	dx
	jc	setatt3			; c = failure
	mov	dh,bl			; store on/off state in dh
setatt1:push	dx
	mov	ah,cmeol
	call	comnd
	pop	dx
	jc	setatt3
	mov	al,flags.attflg		; current flags
	not	dl			; all but those affected
	and	al,dl			; turn off affected flags
	or	dh,dh			; off (dh = 0)?
	jz	setatt2			; z = yes
	not	dl			; affected flags back again as ones
	or	al,dl			; turn on affected flags
setatt2:mov	flags.attflg,al
setatt3:ret
SETATT	ENDP

; SET BAUD or SET SPEED
; See system dependent routine BAUDST in file MSXxxx.ASM

; SET BELL on or off

BELLST	PROC	NEAR
	mov	dx,offset ontab		; on/off table
	xor	bx,bx			; help
	call	keyend
	jc	bellst1			; c = failure
	mov	flags.belflg,bl
bellst1:ret
BELLST	ENDP

; SET BLOCK-CHECK

BLKSET	PROC	NEAR
	mov	dx,offset blktab	; table
	xor	bx,bx			; help, use table
	call	keyend
	jc	blkset1			; c = failure
	mov	dtrans.chklen,bl	; use this char as initial checksum
blkset1:ret
BLKSET	ENDP

; SET CARRIER {ON, OFF}
setcar	proc	near
	mov	dx,offset ontab		; table
	mov	bx,offset carhlp	; help, use table
	call	keyend
	jc	setcar1			; c = failure
	mov	flags.carrier,bl	; value
	mov	cardet,0		; clear carrier detect variable
	clc
setcar1:ret
setcar	endp

; Set port addresses for COM1 .. COM4 at Kermit initialization time via
; Environment. Called by command parser while doing Environment reading in
; mssker.asm and via SET COM1 .. SET COM4.
COM1PORT proc	near
	mov	bx,0			; offset of com1 port address
	jmp	short comport
COM1PORT endp
COM2PORT proc	near
	mov	bx,2			; offset of com2 port address
	jmp	short comport
COM2PORT endp
COM3PORT proc	near
	mov	bx,4			; offset of com3 port address
	jmp	short comport
COM3PORT endp

COM4PORT proc	near
	mov	bx,6			; offset of com4 port address
;;	jmp	comport
COM4PORT endp

COMPORT	proc	near			; worker for above
	push	bx			; save offset
	mov	dx,offset rdbuf+1
	mov	word ptr rdbuf,0
	mov	bx,offset comhlp
	mov	ah,cmword		; get port address number
	call	comnd
	jnc	compor3
	pop	bx			; fail
	ret
compor3:mov	numerr,0		; no error message
	mov	min,100h		; smallest number
	mov	max,0fff0h		; largest magnitude
	mov	numhlp,0		; help
	call	numwd			; parse this word
	jnc	compor4			; nc = success, value in ax
	pop	bx
	ret
compor4:mov	temp1,ax		; save port address
	mov	dx,offset rdbuf+1
	mov	word ptr rdbuf,0
	mov	bx,offset irqhlp
	mov	temp2,0
	mov	ah,cmword		; read IRQ
	call	comnd
	jnc	compor5
	pop	bx
	ret
compor5:push	ax
	mov	ah,cmeol		; get command confirmation
	call	comnd
	pop	ax
	jnc	compor5a		; nc = success
	ret
compor5a:or	ax,ax			; anything given?
	jz	compor7			; z = no
	mov	numhlp,0		; help
	mov	numerr,0		; no error message
	mov	min,2			; smallest number
	mov	max,15			; largest magnitude
	call	numwd			; parse this word
	jnc	compor6			; nc = success
	pop	bx
	ret
compor6:mov	temp2,ax		; save IRQ
compor7:pop	bx			; recover offset
	cmp	word ptr machnam,'BI'	; check for "IBM-PC"
	jne	compor1			; ne = not this name, fail
	cmp	word ptr machnam+2,'-M'
	jne	compor1
	cmp	word ptr machnam+4,'CP'
	jne	compor1
	push	es
	mov	al,flags.comflg		; current comms port
	dec	al			; count from 0, as per Bios
	shl	al,1			; double to use word index of Bios
	cmp	al,bl			; using this port now?
	jne	compor2			; ne = no
	call	serrst			; reset the port
compor2:mov	cx,40h			; segment 40h
	mov	es,cx
	mov	ax,temp1		; port address
	mov	es:[bx],ax		; set port address
	pop	es
	shr	bl,1			; coms port offset 0,2,4,6 to 0,1,2,3
	mov	ax,temp2		; IRQ
	mov	portirq[bx],al
	clc
compor1:ret
COMPORT	endp

; Set CONTROL PREFIXED <code>, CONTROL UNPREFIXED <code>, code can be ALL
cntlset	proc	near
	push	es
	mov	di,seg decbuf		; copy protlist to work buffer decbuf
	mov	es,di
	mov	si,offset protlist
	mov	di,offset decbuf
	mov	cx,32
	cld
	rep	movsb
	pop	es

	mov	dx,offset cntltab	; table
	mov	bx,offset cntlhlp	; help
	mov	ah,cmkey
	call	comnd
	jc	cntlsetx		; c = failure
	mov	rdbuf,bl		; save operation value
cntlse1:mov	ah,cmword		; get optional error value and msg
	mov	dx,offset rdbuf+2
	mov	rdbuf+1,'\'		; in case the user did not specify
	mov	bx,offset ctlhlp	; help on numerical argument
	call	comnd
	jc	cntlsetx		; c = failure
	mov	si,offset rdbuf+2
	or	ax,ax			; anything given?
	jnz	cntlse2			; nz = yes
	mov	ah,cmeol		; confirm
	call	comnd
	jc	cntlsetx		; c = failure
	push	es			; copy work buffer decbuf to protlist
	mov	di,seg protlist
	mov	es,di
	mov	di,offset protlist
	mov	si,offset decbuf
	mov	cx,32
	cld
	rep	movsb
	pop	es
	clc
	ret

cntlse2:mov	al,[si]			; look for ALL
	or	al,al			; end of string?
	jz	cntlse1			; z = yes, get more user input
	cmp	al,','			; comma separator?
	jne	cntlse2a		; ne = no
	inc	si			; skip it
	jmp	short cntlse2
cntlse2a:or	al,20h			; to lower
	cmp	al,'a'			; a in ALL?
	je	cntlse8			; e = got ALL
	cmp	byte ptr [si],'\'	; user specified?
	je	cntlse3			; e = yes
	dec	si
	mov	byte ptr [si],'\'	; insert prefix
cntlse3:call	katoi			; convert to number in ax
	jc	cntlsety		; c = failure
	cmp	al,159			; out of range?
	ja	cntlsety		; a = yes
	cmp	al,128			; in 8-bit range?
	jae	cntlse4			; ae = yes
	cmp	al,31			; out of range?
	ja	cntlsety		; a = yes
cntlse4:mov	ah,rdbuf		; protected/unprotected pointer 0/1
	mov	bl,al			; char
	and	bl,not 80h		; strip high bit from index
	xor	bh,bh
	or	ah,ah			; protecting?
	jz	cntlse6			; z = yes
	mov	ah,1			; assume unprotecting 7-bit char
	and	al,80h			; get high bit
	jz	cntlse5			; z = no high bit
	mov	ah,80h			; set high bit flag
cntlse5:or	decbuf[bx],ah		; set unprotection bit
	jmp	cntlse2			; get more input

cntlse6:mov	ah,1			; assume protecting 7-bit char
	and	al,80h			; get high bit
	jz	cntlse7			; z = no high bit
	mov	ah,80h			; set high bit flag
cntlse7:not	ah			; invert bits
	and	decbuf[bx],ah		; clear the unprotection bit
	jmp	cntlse2			; get more input

cntlsetx:ret				; success or failure
					; process ALL
cntlse8:mov	cx,32
	xor	bx,bx
	mov	al,rdbuf		; get kind of operation
	or	al,al			; prefix (0)?
	je	cntlse9			; e = yes
	mov	al,81h			; unprefix all (7 and 8 bit)
cntlse9:mov	decbuf[bx],al		; set the state
	inc	bx
	loop	cntlse9			; do all
	jmp	cntlse1			; get more user input

cntlsety:mov	ah,cmeol		; confirm
	call	comnd
	mov	ah,prstr
	mov	dx,offset badcntlmsg	; say out of range
	int	dos
	stc
	ret
cntlset	endp

; SET COUNTER number	for script IF COUNTER number <command>
TAKECTR	PROC	NEAR
	mov	min,0			; get decimal char code
	mov	max,65535		; range is 0 to 65535 decimal
	mov	numhlp,offset takchlp	; help message
	mov	numerr,0		; error message
	call	num0			; convert number, return it in ax
	jc	takect2			; c = error
	push	ax			; save numerical code
	mov	ah,cmeol
	call	comnd			; get a confirm
	pop	ax			; recover ax
	jc	takect2			; c = failure
	cmp	taklev,0		; in a Take file?
	je	takect4			; e = no
	push	bx
	mov	bx,takadr
	mov	[bx].takctr,ax		; set COUNT value
	pop	bx
	clc				; success
takect2:ret
takect4:mov	dx,offset takcerr	; say must be in Take file
	jmp	reterr			; display msg and return carry clear
TAKECTR	ENDP

; SET DEBUG {OFF | ON | SESSSION | PACKETS}

DEBST	PROC       NEAR
	mov	dx,offset debtab
	mov	bx,offset debhlp
	call	keyend
	jnc	debst1			; nc = success
	ret				; failure
debst1:	or	flags.debug,bl		; set the mode, except for Off
	or	bx,bx			; OFF?
	jnz	debst2			; nz = no
	mov	flags.debug,bl		; set the DEBUG flags off
debst2:	clc				; success
	ret
DEBST	ENDP

; SET DESTINATION   of incoming files

DESSET	PROC	NEAR
	mov	dx,offset destab
	xor	bx,bx
	call	keyend
	jc	desset1			; c = failure
	mov	flags.destflg,bl	; set the destination flag
desset1:ret
DESSET	ENDP

; SET DEFAULT-DISK    for sending/receiving, etc
; See cwdir in file mssker

; SET DELAY seconds   Used only for SEND command in local mode
SETDELY	PROC	NEAR
	mov	min,0			; smallest acceptable value
	mov	max,63			; largest acceptable value
	mov	numhlp,offset delyhlp	; help message
	mov	numerr,0		; complaint message
	call	num0			; parse numerical input
	jc	setdly1			; c = error
	mov	trans.sdelay,al
setdly1:ret				; success or failure
SETDELY	ENDP

; SET DISPLAY Quiet/Regular/Serial/7-Bit/8-Bit (inverse of Set Remote on/off)
; Accepts two keywords in one command
disply	proc	near
	mov	ah,cmkey
	mov	dx,offset distab
	mov	bx,offset dishlp
	call	comnd
	jnc	displ0			; nc = success
	ret				; return failure
displ0:	mov	temp1,bx		; save parsed value
	mov	temp2,0ffffh		; assume no second keyword
	mov	comand.cmcr,1		; bare CR's are allowed
	mov	ah,cmkey		; parse for second keyword
	mov	dx,offset distab
	mov	bx,offset dishlp
	call	comnd
	jc	displ1			; no keyword
	mov	temp2,bx		; get key value
displ1:	mov	comand.cmcr,0		; bare CR's are not allowed
	mov	ah,cmeol
	call	comnd			; confirm
	jnc	displ2			; nc = success
	ret				; failure
displ2:	mov	ax,temp1		; examine first key value
	call	dispcom			; do common code
	mov	ax,temp2		; examine second key value

dispcom:or	ax,ax			; check range
	jle	dispc3			; le = not legal, ignore
	cmp	al,7			; 7-8 bit value?
	jge	dispc2			; ge = yes
	and	flags.remflg,not(dquiet+dregular+dserial)
	or	flags.remflg,al		; set display mode
	clc				; success
	ret				; check next key value
dispc2:	cmp	al,8			; set 8-bit wide display?
	ja	dispc3			; a = bad value
	and	flags.remflg,not d8bit	; assume want 7 bit mode
	cmp	al,7			; really want 7 bit mode?
	je	dispc3			; e = yes
	or	flags.remflg,d8bit	; set 8 bit flag
dispc3:	clc				; success
	ret				; end of display common code
disply	endp


; Set Dump filename  for saving screen images on disk.
; Puts filename in global string dmpname
setdmp	proc	near
	mov	dx,offset rdbuf		; work area
	mov	rdbuf,0			; clear it
	mov 	bx,offset dmphlp	; help message
	mov	ah,cmword		; allow paths
	call	comnd
	jc	setdmp2			; c = failure
	mov	ah,cmeol
	call	comnd
	jc	setdmp2			; c = failure
	mov	dx,offset rdbuf		; assume we will use this text
	call	strlen			; filename given?
	mov	si,dx			; for strcpy
	or	cx,cx			; length of user's filename
	jg	setdmp1			; g = filename is given
	mov	si,offset dmpdefnam	; no name, use default instead
setdmp1:mov	di,offset dmpname	; copy to globally available loc
	call	strcpy
	clc
setdmp2:ret
setdmp	endp

; Set DUPLEX {FULL, HALF}
setdup	proc	near
	xor	bx,bx
	mov	dx,offset duptab
	call	keyend
	jc	setdup1			; c = failure
	mov	si,portval
	mov	[si].duplex,bl		; set value
	mov	[si].ecoflg,0		; turn off local echoing
	or	bl,bl			; full duplex?
	jz	setdup1			; z = yes
	mov	[si].floflg,0		; no flow control for half duplex
	mov	[si].ecoflg,1		; turn on local echoing
	call	serrst			; reset port so opening uses above
setdup1:ret
setdup	endp

; SET EOF

SETEOF	PROC	NEAR
	xor	bx,bx
	mov	dx,offset seoftab
	call	keyend
	jc	seteof1			; c = failure
	mov	flags.eofcz,bl		; set value
seteof1:ret
SETEOF	ENDP

; SET End-of-Packet char (for Sent packets)
; Archic, here for downward compatibility
EOLSET	PROC	NEAR
	mov	stflg,'S'		; set send/receive flag to Send
	jmp	sreol			; use Set Send/Rec routine do the work
EOLSET	ENDP

; SET ERRORLEVEL number
SETERL	PROC	NEAR
	mov	numhlp,offset erlhlp	; help
	mov	numerr,0		; error message
	mov	min,0			; smallest number
	mov	max,255			; largest magnitude
	call	num0			; parse numerical input
	jc	seterl1			; c = error
	mov	errlev,al		; store result
	clc
seterl1:ret
SETERL	ENDP

; SET ESCAPE character.
; Accept literal control codes and \### numbers. [jrd] 18 Oct 1987
ESCSET	PROC	NEAR
	mov	ah,cmword
	mov	dx,offset rdbuf		; work space
	mov	word ptr rdbuf,0	; clear it
	mov	bx,offset eschlp	; help
	call	comnd
	jc	escse2			; c = failure
	or	ax,ax			; anything given?
	jnz	escse1			; nz = yes
	mov	dx,offset ermes6	; more parameters needed
	jmp	reterr
escse1:	mov	ah,cmeol		; get a confirm
	call	comnd
	jc	escse2			; c = failure
	mov	si,offset rdbuf		; source of chars
	call	katoi			; convert escaped numbers to binary
	cmp	ax,spc			; is it a control code?
	jae	escse3			; ae = no, complain
	or	ax,ax			; non-zero too?
	jz	escse3			; z = zero
	mov	trans.escchr,al		; save new escape char code
	clc
escse2:	ret
escse3:	mov	dx,offset escerr
	jmp	reterr
ESCSET	ENDP

; Set EXIT 
; SET WARNING {ON, OFF}
setexitwarn proc near
	mov	dx,offset exittab	; exit table
	xor	bx,bx
	mov	ah,cmkey
	call	comnd
	jc	setexit1
	or	bl,bl			; warning?
	jnz	setexit1		; nz = no
	mov	bx,offset exitwhlp	; warning help
	mov	dx,offset ontab
	call	keyend
	jc	setexit1		; c = failure
	mov	flags.exitwarn,bl	; set value
setexit1:ret
setexitwarn endp

; SET FILE {DISPLAY, WARNING, TYPE, CHARACTER-SET}
SETFILE	proc	near
	mov	dx,offset setfitab	; SET FILE table
	xor	bx,bx
	mov	ah,cmkey
	call	comnd
	jc	setfiy			; c = failure
	or	bl,bl			; Warning?
	jnz	setfi1			; nz = no
					; entry point for old SET WARNING
FILWAR:	mov	dx,offset warntab	; warning table, on, off, no-super
	xor	bx,bx
	call	keyend
	jc	setfiy			; c = failure
	mov	flags.flwflg,bl		; set the filewarning flag
setfiy:	ret

setfi1:	cmp	bl,1			; SET FILE CHARACTER-SET?
	jne	setfi2			; ne = no
	mov	dx,offset setchtab	; table of char sets
	xor	bx,bx
	call	keyend			; get the set id
	jc	setfiy			; c = error
	mov	flags.chrset,bx		; save the id
	cmp	bx,866			; setting CP866?
	jne	setfi1a			; ne = no
	cmp	dtrans.xchset,xfr_cyrillic ; using TRANSFER of Cryillic?
	je	setfi1a			; e = yes
	mov	dtrans.xchset,xfr_cyrillic ; force TRANSFER of Cyrillic
	mov	trans.xchset,xfr_cyrillic
	mov	ah,prstr
	mov	dx,offset setchmsg	; show warning
	int	dos
	clc
	ret
setfi1a:cmp	bx,932			; setting Shift-JIS?
	jne	setfi1b			; ne = no
	mov	dtrans.xchset,xfr_japanese ; force TRANSFER of Japanese-EUC
	mov	trans.xchset,xfr_japanese
	mov	ah,prstr
	mov	dx,offset setchmsg2	; show warning
	int	dos
	clc
	ret
setfi1b:cmp	bx,862			; setting CP862?
	jne	setfi1c			; ne = no
	mov	dtrans.xchset,xfr_hebiso ; force TRANSFER of Latin-Hebrew
	mov	trans.xchset,xfr_hebiso
	mov	ah,prstr
	mov	dx,offset setchmsg3	; show warning
	int	dos
setfi1c:clc
	ret

setfi2:	cmp	bl,2			; SET FILE TYPE?
	jne	setfi3			; ne = 3
	mov	dx,offset xftyptab	; table of types
	xor	bx,bx
	call	keyend
	jc	setfix			; c = error
	mov	dtrans.xtype,bl		; store transfer type
	mov	trans.xtype,bl		; store transfer type
	ret
setfi3:	cmp	bl,3			; SET FILE DISPLAY?
	jne	setfi4			; ne = no
	mov	dx,offset distab2	; table
	xor	bx,bx
	call	keyend
	jc	setfix			; c = failure
	and	flags.remflg,not(dquiet+dregular+dserial)
	or	flags.remflg,bl		; set display mode
	clc
setfix:	ret
setfi4:	cmp	bl,4			; SET FILE INCOMPLETE?
	jne	setfix			; ne = no
					; SET INCOMPLETE file disposition
ABFSET:	mov	dx,offset abftab
	xor	bx,bx
	call	keyend
	jc	abfset1			; c = failure
	mov	flags.abfflg,bl		; Set the aborted file flag
abfset1:ret
SETFILE	endp

; SET FLOW-CONTROL {NONE, XONXOFF, RTS/CTS}

FLOSET	PROC	NEAR
	mov	dx,offset flotab
  	xor	bx,bx
	call	keyend
	jc	floset3			; c = failure
	mov	si,portval
	mov	ax,floxon		; xon/xoff pair
	or	bx,bx			; any flow control?
	jz	floset1			; z = none
	test	bx,1+2			; using xon/xoff?
	jnz	floset2			; nz = xon/xoff
	cmp	flags.comflg,'F'
	je	floset1
	cmp	flags.comflg,4		; UART? (RTS/CTS case)
	ja	floset4			; a = no, error
floset1:xor	ax,ax			; clear chars for RTS/CTS and none
floset2:mov	[si].flowc,ax		; flow control values
	mov	[si].floflg,bl		; flow control kind
	clc
floset3:ret
floset4:mov	dx,offset ermes7	; error message
	jmp	reterr
FLOSET	ENDP

; SET FOSSIL CLOSE-ON-DONE
FOSSET	proc	near
	mov	dx,offset fossiltab
	xor	bx,bx
	mov	ah,cmkey
	call	comnd
	jc	fossetx			; c = failure
	mov	dx,offset ontab		; get on/off state
	mov	bx,offset fossilhlp
	mov	ah,cmkey
	call	comnd
	jnc	fosset1
	ret				; c = failure

fosset1:push	bx
	mov	ah,cmeol
	call	comnd
	pop	bx
	jc	fossetx			; failure
	mov	fossilflag,bl		; set flag
	clc
fossetx:ret
FOSSET	endp

; SET HANDSHAKE
; Add ability to accept general decimal code.
HNDSET	PROC	NEAR
	mov	dx,offset hndtab	; table to scan
	mov	bx,offset hnd1hlp	; help message
	mov	ah,cmkey
	call	comnd
	jc	hnd2			; c = failure
	cmp	bl,0ffh			; want a general char code?
	jne	hnd1			; ne = no
	mov	min,0			; get decimal char code
	mov	max,255			; range is 0 to 255 decimal
	mov	numhlp,offset erlhlp	; help msg
	mov	numerr,0		; error message
	call	num0			; convert number, return it in ax
	jc	hnd2			; c = error
	mov	bx,ax			; recover numerical code
hnd1:	push	bx			; handshake type
	mov	ah,cmeol
	call	comnd			; get a confirm
	pop	bx			; recover bx
	jc	hnd2			; c = failure
	mov	si,portval
	or	bl,bl			; setting handshake off?
	jz	hnd0			; z = yes
	mov	[si].hndflg,1		; turn on handshaking
	mov	[si].hands,bl		; use this char as the handshake
	clc				; success
	ret
hnd0:	mov	[si].hndflg,bl		; no handshaking
	clc				; success
hnd2:	ret
HNDSET	ENDP

;
; Set Input commands (default-timeout, timeout-action, case, echo)
; By Jim Strudevant [jrs]
INPSET	PROC	NEAR
	mov	ah,cmkey		; key word
	mov	dx,offset inptab	; from inputtable
	xor	bx,bx			; no hints
	call	comnd			; get the word
	jc	inpset1			; c = failure
	jmp	bx			; do the sub command
inpset1:ret
;
; Set Input Default-timeout in seconds
;
inptmo:	mov	numhlp,offset intoms	; help
	mov	numerr,0		; error message
	mov	min,0			; smallest number
	mov	max,-1			; largest magnitude
	call	num0			; parse numerical input
	jc	inptmo1			; c = error
	mov	script.indfto,ax	; store result
inptmo1:ret
;
; Set Input Timeout action (proceed or quit)
;
inpact:	mov	dx,offset inactb	; from this list
	xor	bx,bx			; no hints
	call	keyend			; get it
	jc	inpact1			; c = failure
	mov	script.inactv,bl	; save the action
inpact1:ret
;
; Set Input Echo on or off
;
inpeco:	mov	dx,offset ontab		; from this list
	xor	bx,bx			; no hints
	call	keyend			; get it
	jc	inpeco1			; c = failure
	mov	script.inecho,bl	; save the action
inpeco1:ret
;
; Set Input Case observe or ignore
;
inpcas:	mov	dx,offset incstb	; from this list
	xor	bx,bx			; no hints
	call	keyend			; get it
	jc	inpcas1			; c = failure
	mov	script.incasv,bl	; save the action
inpcas1:ret
INPSET	ENDP

; Set length of script buffer for INPUT/REINPUT at Kermit initialization
; time via Environment. Called by command parser while doing Environment
; reading in mssker.asm. Do not call after Kermit has initialized.
SETINPBUF proc	near
	mov	scpbuflen,128		; store default buffer length
	mov	numhlp,0		; no help
	mov	numerr,0		; no error message
	mov	min,2			; smallest number (must be non-zero)
	mov	max,65535		; largest magnitude (16 bits worth)
	call	num0			; parse numerical input
	jc	setinpbx		; c = error
	mov	scpbuflen,ax		; store result
	clc
setinpbx:ret
SETINPBUF endp

; SET KEY
; Jumps to new Set Key routine
setkey	proc	near		
	cmp	stkadr,0	; keyboard translator present?
	je	setk4		; e = no, use this routine
	mov	bx,stkadr	; yes, get offset of procedure
	jmp	bx		; jump to keyboard translator
setk4:	mov	dx,offset ermes5
	jmp	reterr		; else print error message
setkey	endp

; SET LOCAL-ECHO {ON | OFF}
 
LCAL	PROC	NEAR
	mov	dx,offset ontab
	xor	bx,bx
	call	keyend
	jc	lcal1			; c = failure
	mov	si,portval
	mov	[si].ecoflg,bl		; Set the local echo flag
lcal1:	ret
LCAL	ENDP

; LOG  {PACKETS | SESSION | TRANSACTION} filename

setcpt	proc	near
	mov	dx,offset logtab	; kinds of logging
	mov	bx,offset loghlp	; help on kind of logging
	mov	ah,cmkey		; parse keyword
	call	comnd
	jnc	setcp20			; nc = success
	ret				; failure
setcp20:mov	numhlp,bx		; save the parsed value
	mov	dx,offset rdbuf		; holds the complete filename
	mov	rdbuf,0			; clear buffer
	mov 	bx,offset filhlp	; ask for filename
	mov	ah,cmword		; allow paths
	call	comnd
	jnc	setcp21			; nc = success
	ret				; failure
setcp21:mov	ah,cmeol
	call	comnd			; get a confirm
	jnc	setcp22			; nc = success
	ret				; failure
setcp22:mov	bx,numhlp		; recover kind of logging
	mov	dx,offset rdbuf		; length of filename to cx
	call	strlen			; length of given filename
	test	bl,logpkt		; packet logging?
	jz	setcp2			; z = no, try others
	mov	dx,offset lpktnam	; filename
	jcxz	setcp1			; z = no filename given
	mov	si,offset rdbuf		; get new name
	mov	di,dx			; destination
	call	strcpy			; replace old name
setcp1:	cmp	ploghnd,-1		; packet log file already open?
	je	setcp6			; e = no, open it
	jmp	setcp16			; say file is open already

setcp2:	test	bl,logses		; session logging?
	jz	setcp4			; z = no, try others
	mov	dx,offset lsesnam	; use default name
	jcxz	setcp3			; z = no filename given
	mov	si,offset rdbuf		; get new name
	mov	di,dx			; destination
	call	strcpy			; replace old name
setcp3:	cmp	sloghnd,-1		; transaction file already open?
	je	setcp6			; e = no, open it
	jmp	setcp16			; say file is open already

setcp4:	test	bl,logtrn		; transaction logging?
	jz	setcp14			; z = no, error
	mov	dx,offset ltranam	; use default name
	jcxz	setcp5			; z = no filename given
	mov	si,offset rdbuf		; get new name
	mov	di,dx			; destination
	call	strcpy			; replace old name
setcp5:	cmp	tloghnd,-1		; transaction file already open?
	je	setcp6			; e = no, open it
	jmp	setcp16			; say file is open already

setcp6:	mov	ax,dx			; place for filename for isfile
	call	isfile			; does file exist already?
	jc	setcp7			; c = does not exist so use create
	test	byte ptr filtst.dta+21,1fh ; file attributes, ok to write?
	jnz	setcp14			; nz = no, use error exit	
	mov	ah,open2		; open existing file
	mov	al,1+1			;  for writing and reading
	int	dos
	jc	setcp14			; if carry then error
	mov	bx,ax			; file handle for seeking
	xor	cx,cx			; high order displacement
	xor	dx,dx			; low order part of displacement
	mov	ah,lseek		; seek to EOF (to do appending)
	mov	al,2			; says to EOF
	int	dos
	jmp	short setcp8

setcp7:	test	filtst.fstat,80h	; access problem?
	jnz	setcp14			; nz = yes, stop here
	mov	ah,creat2		; function is create
	mov	cx,20H			; turn on archive bit
	int	dos			; create the file, DOS 2.0
	jc	setcp14			; if carry bit set then error
	mov	bx,ax			; file handle

setcp8:	cmp	numhlp,logpkt		; packet logging?
	jne	setcp9			; ne = no
	mov	ploghnd,bx		; save transaction log handle here
	jmp	short setcp12
setcp9:	cmp	numhlp,logses		; session logging?
	jne	setcp10			; ne = no
	mov	sloghnd,bx		; save session log handle here
	jmp	short setcp12
setcp10:mov	tloghnd,bx		; save transaction log handle here

setcp12:mov	ax,numhlp		; kind of Logging
	or	flags.capflg,al		; accumulate kinds of logging
	clc				; success
	ret

setcp14:mov	dx,offset errcap	; give error message
	jmp	reterr			; and display it

setcp16:mov	ah,prstr		; file already open
	mov	dx,offset erropn
	int	dos
	clc				; return success
	ret
setcpt	endp
 
; SET MODE LINE

MODL	PROC	NEAR
	mov	dx,offset ontab		; parse an on or off
	xor	bx,bx			; no special help
	call	keyend
	jc	modl1			; c = failure
	mov	flags.modflg,bl		; set flag appropriately
modl1:	ret
MODL	ENDP

; SET OUTPUT commands
setoutput proc	near
	mov	dx,offset outputtab	; OUTPUT command table
	xor	bx,bx			; no special help
	mov	ah,cmkey
	call	comnd
	jc	setout1			; c = failure
	jmp	short setopace
setout1:ret	
setoutput endp

; Set OUTPUT Pacing <number millisec between chars>
setopace proc	near
	mov	numhlp,offset opacehlp	; help
	mov	numerr,0		; no error message
	mov	min,0			; smallest number
	mov	max,65535		; largest magnitude (16 bits worth)
	call	num0			; parse numerical input
	jc	setopac1		; c = error
	mov	outpace,ax		; store result
	clc
setopac1:ret
setopace endp

; Set Macro Error
setmacerr proc	near
	mov	dx,offset macrotab	; keyword
	xor	bx,bx
	mov	ah,cmkey
	call	comnd
	jnc	setmacer1
	ret				; failure
setmacer1:
	push	bx			; save first keyword ident
	mov	dx,offset ontab		; on/off table
	xor	bx,bx
	call	keyend
	pop	ax			; first keyword ident
	jnc	setmacer2		; nc = success
	ret
setmacer2:or	al,al			; "error" (0)?
	jz	setmacer3		; z = yes
	mov	macroerror,bl		; set error action flag
setmacer3:clc
	ret
setmacerr endp

; SET MODEM text    creates macro named _MODEM
setmodem proc	near
	mov	word ptr rdbuf+2,'m_'	; macro name "_modem"
	mov	word ptr rdbuf+4,'do'
	mov	word ptr rdbuf+6,'me'
	mov	rdbuf+8,' '		; separator
	mov	bx,offset rdbuf+9
	mov	ax,length rdbuf-9	; usable length of rdbuf
	add	ax,offset rdbuf		; ax = amount of buffer used
	mov	comand.cmblen,ax	; our new buffer length
	mov	ah,cmline		; get a line of text
	mov	dx,offset askhlp3	; help, bx is buffer offset
	push	bx			; save starting offset
	call	comnd			; get macro definition text
	pop	bx
	jnc	setmod1			; nc = success
	ret				; failure
setmod1:mov	cx,bx
	sub	cx,offset rdbuf+2	; length of command line
	jmp	dodecom
setmodem endp

; SET PARITY
 
SETPAR	PROC	NEAR
	mov	dx,offset partab	; parity table
	xor	bx,bx
	call	keyend
	jnc	setp1			; nc = success
	ret
setp1:	mov	si,portval
	mov	[si].parflg,bl		; store the parity flag
	clc
	ret
SETPAR	ENDP

; Set Print filename  for writing material to printers.
; Puts filename in global string prnname
setprn	proc	near
	mov	dx,offset rdbuf		; work area
	mov	rdbuf,0			; clear it
	mov 	bx,offset prnhlp	; help message
	mov	ah,cmword		; allow paths
	call	comnd
	jc	setprn3			; c = failure
	mov	ah,cmeol
	call	comnd
	jc	setprn3			; c = failure
	mov	dx,offset rdbuf		; assume we will use this text
	call	strlen			; filename given?
	mov	si,dx			; for strcpy
	or	cx,cx			; length of user's filename
	jg	setprn1			; g = filename is given
	mov	si,offset prndefnam	; no name, use default instead
setprn1:mov	di,offset prnname	; copy to globally available loc
	call	strcpy
	cmp	prnhand,0		; handle already in use?
	jle	setprn2			; le = no
	call	pntflsh			; flush current buffer
	mov	bx,prnhand		; close the file now
	cmp	bx,4			; don't close DOS PRN
	je	setprn2			; e = already available
	mov	ah,close2
	int	dos
setprn2:call	prnopen			; open printer now, may set carry
	jnc	setprn3			; nc = success
	mov	ah,prstr
	mov	dx,offset prnerr	; say can't open the file
	int	dos
	mov	si,offset prndefnam	; use default name as fallback
	mov	di,offset prnname	; copy to globally available loc
	call	strcpy
	mov	prnhand,4		; declare handle to be DOS PRN
setprn3:ret
setprn	endp

; SET PROMPT  Allow user to change the "Kermit-MS>" prompt
; {string} and \number notation permitted to represent special chars.
; String will be made asciiz

PROMSET	PROC	NEAR
	mov	ah,cmline
	mov	bx,offset rdbuf		; Read in the prompt
	mov	word ptr [bx],0		; clear buffer
	mov	dx,offset prmmsg
	mov	comand.cmblen,80	; 80 byte limit
	mov	comand.cmper,1		; do not allow variable substitutions
	call	comnd
	jc	prom2			; c = failure
	or	ax,ax			; prompt string?
	jnz	prom0			; nz = yes
	mov	ax,offset kerm		; no, restore default prompt
	jmp	short prom1
prom0:	push	si			; parse \### constants into
	push	di			;  1 byte binary numbers inline
	mov	si,offset rdbuf		; source = new prompt string
	mov	byte ptr [si-1+length rdbuf],0 ; plant null terminator
	mov	di,offset promptbuf	; destination
	call	cnvlin			; convert \### in string to binary
	pop	di
	pop	si
	mov	bx,cx			; get byte count
	mov	promptbuf[bx],0		; insert null terminator
	mov	ax,offset promptbuf
prom1:	mov	prmptr,ax		; remember it
	clc				; success
prom2:	ret
PROMSET	ENDP

; SET SERVER {LOGIN username password | TIMEOUT}

SETSRV	PROC	NEAR
	mov	dx,offset srvtab	; set server table
	xor	bx,bx			; use table for help
	mov	ah,cmkey		; get keyword
	call	comnd
	jnc	setsrv1			; c = success
	ret
setsrv1:cmp	apctrap,0		; disable from APC?
	jne	setsrvx			; ne = yes
	cmp	bl,1			; Login?
	jne	setsrv2			; ne = no
	test	flags.remflg,dserver	; acting as a server now?
	jz	setsrv3			; z = no
	stc				; fail
	ret
setsrv3:mov	dx,offset rdbuf		; where to store local username
	mov	bx,offset luserh	; help
	mov	comand.cmblen,16	; buffer length
	mov	ah,cmword		; get username
	call	comnd
	jc	setsrvx
	mov	bx,offset rdbuf+30	; where to store local password
	mov	dx,offset lpassh	; help
	mov	comand.cmblen,16	; buffer length
	mov	ah,cmline		; get password, allow spaces
	call	comnd
	jc	setsrvx
	mov	si,offset rdbuf		; only now do we transfer to the
	mov	di,offset luser		; active buffers
	call	strcpy
	mov	si,offset rdbuf+30
	mov	di,offset lpass
	call	strcpy
	clc
	ret

setsrv2:mov	min,0			; Timeout, smallest acceptable value
	mov	max,255			; largest acceptable value, one byte
	mov	numhlp,offset srvthlp	; help message
	mov	numerr,0		; complaint message
	call	num0			; parse numerical input
	jc	setsrvx			; c = error
	mov	srvtmo,al		; store timeout value
	clc				; success
setsrvx:ret
	
SETSRV	ENDP

; Set REPEAT COUNTS {ON, OFF}
repset	proc	near
	mov	dx,offset setrep	; repeat table
	xor	bx,bx
	mov	ah,cmkey
	call	comnd
	jnc	repset1
	ret
repset1:or	bx,bx			; which item (counts, prefix)
	jnz	repset4			; nz = prefix
	mov	dx,offset ontab		; on/off table
	xor	bx,bx			; use table for help
	call	keyend
	jnc	repset2			; nc = success
	ret
repset2:mov	dtrans.rptqenable,bl	; repeat quote char enable/disable
	ret
repset4:mov	ah,cmword		; get a character
	mov	dx,offset rdbuf
	mov	rdbuf,defrptq		; default repeat prefix char
	mov	bx,offset prefhlp
	call	comnd
	jc	repset5			; c = fail
	mov	ah,cmeol
	call	comnd
	jc	repset5			; c = fail
	mov	si,offset rdbuf
	mov	di,si			; convert to numeric
	call	cnvlin
	mov	al,rdbuf		; get char
	call	prechk			; check range
	jc	repset5			; c = failed
	mov	dtrans.rptq,al		; set prefix
repset5:ret
repset	endp

; Check if prefix in AL is in the proper range: 33-62, 96-126. 
; Return carry clear if in range, else return carry set.
prechk	proc	near
	cmp	al,33
	jb	prechk2			; b = out of range
	cmp	al,62
	jbe	prechk1			; be = in range 33-62
	cmp	al,96
	jb	prechk2			; b = out of range
	cmp	al,126
	ja	prechk2			; a = out of range 96-126
prechk1:clc				; carry clear for in range
	ret
prechk2:stc				; carry set for out of range
	ret
prechk	endp
; SET RETRY value. Changes the packet retry limit.

RETRYSET PROC	NEAR
	mov	min,1			; smallest acceptable value
	mov	max,63			; largest acceptable value
	mov	numhlp,offset retryhlp	; help message
	mov	numerr,0		; complaint message
	call	num0			; parse numerical input
	jc	retrys1			; c = error
	mov	maxtry,al
retrys1:ret
RETRYSET ENDP

; Set number of screens in terminal emulator rollback buffer at Kermit
; initialization time via Environment. Called by command parser while doing
; Environment reading in mssker.asm. Do not call after Kermit has initialized.
SETROLLB proc	near
	mov	numhlp,offset rollhlp	; help
	mov	numerr,0		; no error message
	mov	min,0			; smallest number
	mov	max,8000		; largest magnitude
	call	num0			; parse numerical input
	jc	setrol1			; c = error
	mov	npages,ax		; store result
	clc
setrol1:ret
SETROLLB endp

; Set width of rollback screens in terminal emulator rollback buffer at Kermit
; initialization time via Environment. Called by command parser while doing
; Environment reading in mssker.asm. Do not call after Kermit has initialized.
SETWIDTH proc	near
	mov	rollwidth,80		; default width of rolled screen
	mov	numhlp,0		; no help
	mov	numerr,0		; no error message
	mov	min,80			; smallest number
	mov	max,207			; largest magnitude (16 bits worth)
	call	num0			; parse numerical input
	jc	setwid1			; c = error
	mov	rollwidth,ax		; store result
	clc
setwid1:ret
SETWIDTH endp

; SET TAKE ECHO or TAKE ERROR on or off

TAKSET	PROC	NEAR
	mov	dx,offset taketab	; Parse a keyword
	xor	bx,bx			; no specific help
	mov	ah,cmkey
	call	comnd
	jnc	takset1
	ret				; failure
takset1:push	bx			; save command indicator
	mov	dx,offset ontab
	xor	bx,bx
	call	keyend
	pop	ax			; recover command indicator
	jnc	takset2
	ret				; failure
takset2:or	al,al			; Take Echo command?
	jnz	takset3			; nz = no
	mov	flags.takflg,bl
	clc
	ret
takset3:mov	takeerror,bl		; Take Error command
	clc
	ret
TAKSET	ENDP

; SET TIMER     on or off during file transfer

TIMSET	PROC	NEAR
	mov	dx,offset ontab
	xor	bx,bx
	call	keyend
	jc	timset1			; c = failure
	mov	flags.timflg,bl
timset1:ret
TIMSET	ENDP

; SET WINDOW number of windows
WINSET	PROC	NEAR
	mov	min,1			; smallest acceptable value
	mov	max,maxwind		; largest acceptable value
	mov	numhlp,offset winhelp	; help message
	mov	numerr,0		; complaint message
	call	num0			; parse numerical input
	jc	winse5			; c = error
	mov	dtrans.windo,al		; store default window size
	mov	trans.windo,al		; and in active variable for makebuf
	clc				; success
winse5:	ret
WINSET	ENDP

; SET SEND parameters

SENDSET	PROC	NEAR
	mov	stflg,'S'		; Setting SEND parameter 
	mov	dx,offset stsrtb	; Parse a keyword
	xor	bx,bx			; no specific help
	mov	ah,cmkey
	call	comnd
	jc	sendset1		; c = failure
	jmp	bx			; do the action routine
sendset1:ret
SENDSET	ENDP

; SET RECEIVE parameters

recset:	mov	stflg,'R'		; Setting RECEIVE paramter
	mov	dx,offset stsrtb	; Parse a keyword
	xor	bx,bx			; no specific help
	mov	ah,cmkey
	call	comnd
	jc	recset1			; c = failure
	jmp	bx			; do the action routine
recset1:ret

remset	proc	near			; Set REMOTE ON/OFF
	mov	dx,offset ontab
	mov	bx,offset remhlp
	call	keyend
	jc	remset2			; c = failure
	and	flags.remflg,not (dquiet+dserial+dregular) ; no display bits
	or	bl,bl			; want off state? (same as regular)
	jz	remset1			; z = yes
	or	flags.remflg,dquiet	; else on = quiet display
	clc
	ret
remset1:or	flags.remflg,dregular	; off = regular display
	clc
remset2:ret
remset	endp


; SET Send and Receive End-of-Packet char

sreol	PROC	NEAR
	mov	min,0			; lowest acceptable value
	mov	max,1FH			; largest acceptable value
	mov	numhlp,offset eophlp	; help message
	mov	numerr,0		; error message address
	call	num0			; get numerical input
	jc	sreol3			; c = error
	cmp	stflg,'S'		; setting SEND paramter?
	je	sreol1
	mov	trans.reol,al
	jmp	short sreol2
sreol1:	mov	dtrans.seol,al
sreol2:	mov	ah,dtrans.seol
	mov	trans.seol,ah
	clc
sreol3:	ret
sreol	ENDP


; SET SEND and RECEIVE start-of-header

srsoh:	mov	min,0
	mov	max,7eh			; allow printables (control=normal)
	mov	numhlp,offset sohhlp	; reuse help message
	mov	numerr,0		; error message
	call	num0		; Common routine for parsing numerical input
	jc	srsoh2			; c = error
	cmp	stflg,'S'		; setting SEND paramter?
	je	srsoh1
	mov	trans.rsoh,al		; set Receive soh
	clc				; success
	ret
srsoh1:	mov	trans.ssoh,al		; set Send soh
	clc				; success
	ret
srsoh2:	ret

; SET Send Double-char

srdbl	PROC	NEAR
	mov	min,0			; lowest acceptable value
	mov	max,0ffh		; largest acceptable value
	mov	numhlp,offset dblhlp	; help
	mov	numerr,0		; error message address
	call	num0			; get numerical input
	jc	sreol3			; c = error
	cmp	stflg,'R'		; setting Receive paramter?
	je	srdbl1			; e = yes, no action
	mov	trans.sdbl,al		; store character to be doubled
	mov	dtrans.sdbl,al
	clc
srdbl1:	ret
srdbl	ENDP

; SET Receive Ignore-char

srign	PROC	NEAR
	mov	min,0			; lowest acceptable value
	mov	max,0ffh		; largest acceptable value
	mov	numhlp,offset dblhlp	; reuse help
	mov	numerr,0		; error message address
	call	num0			; get numerical input
	jc	srign1			; c = error
	cmp	stflg,'S'		; setting SEND paramter?
	je	srign1			; e = yes, no action
	mov	trans.rign,al		; store character to be ignored
	mov	dtrans.rign,al
	clc
srign1:	ret
srign	ENDP

; SET SEND and	RECEIVE TIMEOUT

srtim:	mov	min,0
	mov	max,94
	mov	numhlp,offset timhlp	; Reuse help message
	mov	numerr,0		; error message
	call	num0		; Common routine for parsing numerical input
	jc	srtim3			; c = error
	cmp	stflg,'S'		; Setting SEND paramter?
	je	srtim1
	mov	trans.rtime,al
	jmp	short srtim2
srtim1:	mov	dtrans.stime,al
srtim2:	mov	ah,dtrans.stime
	mov	trans.stime,ah
	clc
srtim3:	ret

; SET SEND and RECEIVE PACKET LENGTH
; dtrans items are real, trans items are just for SHOW information

srpack:	mov	min,20
	mov	max,9024
	mov	numhlp,offset pakhlp	; help
	mov	numerr,offset pakerr	; error message
	call	num0
	jnc	srpaks0			; nc = success
	ret				; failure
srpaks0:cmp	stflg,'S'		; setting send value?
	jne	srpakr			; ne = receive
	mov	dtrans.slong,ax		; set send max value
	mov	trans.slong,ax		; store current active length
	mov	dtrans.spsiz,dspsiz	; set regular 94 byte default
	mov	trans.spsiz,dspsiz	; ditto
	cmp	ax,dspsiz		; longer than regular packet?
	jae	srpaks1			; ae = yes
	mov	dtrans.spsiz,al		; shrink regular packet size too
	mov	trans.spsiz,al		; shrink regular packet size too
srpaks1:clc				; success
	ret

srpakr:	mov	dtrans.rlong,ax		; set receive max value
	mov	trans.rlong,ax		; store active length
	mov	dtrans.rpsiz,drpsiz	; set regular to default 94 bytes
	mov	trans.rpsiz,drpsiz
	mov	trans.rpsiz,drpsiz
	cmp	ax,drpsiz		; longer than a regular packet?
	jae	srpakr1			; ae = yes
	mov	dtrans.rpsiz,al		; shrink regular packet size too
	mov	trans.rpsiz,al
srpakr1:clc				; success
	ret


; SET SEND and RECEIVE number of padding characters

srnpd:	mov	min,0
	mov	max,94
	mov	numhlp,offset timhlp	; reuse help message
	mov	numerr,0		; error message
	call	num0			; Parse numerical input
	jc	srnpd3			; c = error
	cmp	stflg,'S'		; Setting SEND paramter?
	je	srnpd1			; e = yes
	mov	trans.rpad,al		; set Receive padding
	jmp	short srnpd2
srnpd1:	mov	dtrans.spad,al		; set default Send padding
srnpd2:	mov	al,dtrans.spad
	mov	trans.spad,al    	; update active array for I and S pkts
	clc
srnpd3:	ret

; SET SEND and RECEIVE padding character

srpad:	mov	min,0
	mov	max,127
	mov	numhlp,offset padhlp
	mov	numerr,offset padhlp
	call	num0			; parse numerical input
	jc	srpad4			; c = error
	cmp	ah,127			; this is allowed
	je	srpad1
	cmp	ah,32
	jb	srpad1			; between 0 and 31 is OK too
	mov	ah,prstr
	mov	dx,offset padhlp
	int	dos
srpad1:	cmp	stflg,'S'		; Send?
	je	srpad2			; e = yes, else Receive
	mov	trans.rpadch,al		; store receive pad char
	jmp	short srpad3
srpad2:	mov	dtrans.spadch,al	; store Send pad char
srpad3:	mov	ah,dtrans.spadch
	mov	trans.spadch,ah  	; update active array for I and S pkts
	clc				; success
srpad4:	ret

; SET SEND and	RECEIVE control character prefix

srquo:	mov	min,33
	mov	max,126
	mov	numhlp,offset quohlp	; help message
	mov	numerr,0		; error message
	call	num0			; Parse numerical input
	jc	srquo3			; c = error
	cmp	stflg,'S'		; Setting outgoing quote char?
	je	srquo1			; e = yes
	mov	trans.rquote,al		; set Receive quote char
	jmp	short srquo2
srquo1:	mov	dtrans.squote,al	; set Send quote char
srquo2:	clc
srquo3:	ret

; SET SEND Pause number	of milliseconds

srpaus:	mov	min,0
	mov	max,65383
	mov	numhlp,offset pauhlp	; help
	mov	numerr,0
	call	num0			; Parse numerical input
	pushf				; save carry for error state
	cmp	stflg,'S'		; Setting SEND paramter?
	je	srpau0
	popf
	mov	dx,offset ermes5	; "Not implemented" msg
	jmp	reterr			; print error message
srpau0:	popf
	jc	srpau1			; c = error
	mov	spause,ax		; store value
srpau1:	ret

; SET RECEIVE REMOTE-PATH {enable, disable}
srpath	proc	near
	mov	dx,offset ontab
	mov	bx,offset rspathhlp
	call	keyend
	jc	srpath1
	cmp	stflg,'R'		; Setting RECEIVE paramter?
	jne	srpath1			; ne = no
	mov	rcvpathflg,bl		; update receive flag
	clc
	ret
srpath1:mov	sndpathflg,bl		; update send flag
	clc
srpath2:ret
srpath	endp

; Set stop-bits
stopbit	proc	near
	mov	min,1
	mov	max,2
	mov	numhlp,offset stophlp	; help
	mov	numerr,0
	call	num0			; Parse numerical input
	jnc	stopbit1
	ret				; c = failure
stopbit1:mov	bx,portval
	mov	[bx].stopbits,al	; stop bits
	clc
	ret	
stopbit	endp

; SET TCP/IP address nnn.nnn.nnn.nnn
; SET TCP/IP subnetmask nnn.nnn.nnn.nnn
; SET TCP/IP gateway nnn.nnn.nnn.nnn
; SET TCP/IP primary-nameserver nnn.nnn.nnn.nnn
; SET TCP/IP secondary-nameserver nnn.nnn.nnn.nnn
; SET TCP/IP domain string

ifndef	no_tcp
tcpipset proc	near
	mov	ah,cmkey		; get keyword
	mov	dx,offset tcptable	; table
	xor	bx,bx			; help
	call	comnd
	jnc	tcpse1
	ret
tcpse1:	mov	word ptr rdbuf,bx	; keyword index
	mov	comand.cmblen,17	; length of user's buffer
	cmp	bx,1			; local address?
	jne	tcpse1a			; ne = no
	mov	bx,offset addrhelp	; address help
	jmp	short tcpse4
tcpse1a:cmp	bx,2			; domain name?
	jne	tcpse2			; ne = no
	mov	bx,offset domainhlp	; domain help
	mov	comand.cmblen,32	; length of user's buffer
	jmp	short tcpse4
tcpse2:	cmp	bx,6			; subnet mask?
	jne	tcpse3			; ne = no
	mov	bx,offset subnethlp
	jmp	short tcpse4
tcpse3:	cmp	bx,7			; Host?
	jne	tcpse3a			; ne = no
	mov	bx,offset hosthlp
	mov	comand.cmblen,60	; length of user's buffer
	jmp	short tcpse4
tcpse3a:cmp	bx,9			; PD interrupt?
	jne	tcpse3b			; ne = no
	mov	bx,offset tcppdinthlp
	jmp	short tcpse4
tcpse3b:cmp	bx,10			; term type?
	jne	tcpse3c			; ne = no
	mov	bx,offset tcpttyhlp
	mov	comand.cmblen,32	; length of user's buffer
	jmp	short tcpse4
tcpse3c:cmp	bx,11			; newline mode?
	jne	tcpse3d			; ne = no
	jmp	tcpse13
tcpse3d:cmp	bx,12			; debug mode?
	jne	tcpse3e			; ne = no
	jmp	tcpse14
tcpse3e:cmp	bx,13			; binary/nvt mode?
	jne	tcpse3f			; ne = no
	jmp	tcpse15
tcpse3f:cmp	bx,14			; MSS?
	jne	tcpse3g			; ne = no
	jmp	tcpse16

tcpse3g:mov	bx,offset iphelp	; Internet number help
tcpse4:	mov	ah,cmword		; get a string
	mov	dx,offset rdbuf+2	; work buffer
	call	comnd
	jnc	tcpse5
	ret
tcpse5:	push	ax			; save string length in ax
	mov	ah,cmeol
	call	comnd
	pop	ax
	jnc	tcpse6
	ret
tcpse6:	mov	si,offset rdbuf+2	; user's string
	mov	bx,word ptr rdbuf	; comand kind
	cmp	bx,2			; domain?
	jne	tcpse8			; ne = no
	mov	di,offset tcpdomain
	cmp	ax,32			; exceeded 32 chars?
	jbe	tcpse7			; be = no
	mov	ah,prstr
	mov	dx,offset domainbad	; compain
	int	dos
	stc
	ret
tcpse7:	cmp	ax,32			; address oversized?
	jbe	tcpse9			; be = no
	mov	ah,prstr
	mov	dx,offset addressbad	; say bad address
	int	dos
	stc
	ret
tcpse8:	mov	di,offset tcpaddress
	cmp	bx,1			; local address?
	je	tcpse9			; e = yes
	mov	di,offset tcpsubnet
	cmp	bx,3			; subnet?
	je	tcpse9			; e = yes
	mov	di,offset tcpgateway
	cmp	bx,4			; gateway?
	je	tcpse9
	mov	di,offset tcpprimens
	cmp	bx,5			; primary-nameserver?
	je	tcpse9
	mov	di,offset tcpsecondns	; secondary-nameserer
	cmp	bx,6
	je	tcpse9
	mov	di,offset tcphost	; host name or number
	cmp	bx,7
	je	tcpse9
	mov	di,offset tcpbcast	; broadcast
tcpse9:	cmp	bx,9			; port or other?
	jae	tcpse10			; ae = yes
	call	strcpy
	clc
	ret
tcpse10:cmp	bx,9			; PD interrupt?
	jne	tcpse12			; ne = no
	mov	si,offset rdbuf+2
	cmp	byte ptr [si],'\'	; backslash present?
	je	tcpse11			; e = yes
	dec	si
	mov	byte ptr [si],'\'
tcpse11:mov	ax,word ptr [si+1]
	and	ax,not 2020h		; to upper case
	cmp	ax,'DO'			; ODI?
	je	tcpse11a		; e = yes, use "DO"
	push	bx
	call	katoi			; number to ax
	pop	bx
tcpse11a:mov	tcppdint,ax
	clc
	ret
tcpse12:cmp	bx,10			; term-type string?
	jne	tcpse13			; ne = no
	mov	si,offset rdbuf+2
	mov	di,offset tcpttbuf	; copy string to holding place
	call	strcpy
	clc
	ret
tcpse13:cmp	bx,11			; newline mode?
	jne	tcpse14			; ne = no
	mov	dx,offset newlinetab	; newline table
	mov	bx,offset tcpnlhlp	; help
	call	keyend
	jc	tcpse20			; fail
	mov	tcpnewline,bl		; set mode
	clc
	ret
tcpse14:cmp	bx,12			; debug mode?
	jne	tcpse15			; ne = no
	mov	dx,offset ontab		; on/off table
	xor	bx,bx			; help
	call	keyend
	jc	tcpse20			; fail
	mov	tcpdebug,bl		; set mode
	clc
	ret
tcpse15:cmp	bx,13			; binary/nvt mode?
	jne	tcpse16			; ne = no
	mov	dx,offset tcpmodetab	; mode table
	xor	bx,bx			; help
	call	keyend
	jc	tcpse20
	mov	tcpmode,bl		; set mode
	clc
	ret
tcpse16:cmp	bx,14			; MSS?
	jne	tcpse20			; ne = no
	mov	numhlp,offset tcpmsshlp	; help
	mov	min,16			; get decimal value
	mov	max,1460		; range is 16 to 1460 decimal
	mov	numerr,0		; error message
	call	num0			; convert number, return it in ax
	jc	tcpse20			; c = error
	mov	tcpmss,ax		; MSS
	clc
	ret
tcpse20:stc				; fail
	ret
tcpipset endp
endif	; no_tcp

; SET TRANSFER  CHARACTER-SET {Latin1, Shift-JIS, Transparent}
; SET TRANSFER  TRANSLATION {Readable, Invertible}
sxfer	proc	near
	mov	dx,offset xfertab	; table of TRANSFER keywords
	xor	bx,bx
	mov	ah,cmkey		; get next keyword
	call	comnd
	jc	sxfer1			; c = error
	or	bl,bl			; Character-set?
	jnz	sxfer2			; nz = no
	mov	dx,offset xfchtab	; Character-set table
	mov	bx,offset xfchhlp	; help text
	call	keyend
	jc	sxfer1			; c = error
	mov	dtrans.xchset,bl	; store transfer char set ident
	mov	trans.xchset,bl		; store transfer char set ident
	cmp	bl,xfr_cyrillic		; Cyrillic?
	jne	sxfer9			; ne = no
	mov	ax,flags.chrset		; get current file character set
	mov	flags.chrset,866	; force CP866
	cmp	ax,866			; was CP866/LATIN5 File Character set?
	je	sxfer8			; e = yes
	mov	dx,offset xfchbad	; show warning message
	mov	ah,prstr
	int	dos
sxfer8:	clc
sxfer1:	ret
sxfer9:	cmp	bl,xfr_japanese		; Japanese-EUC?
	jne	sxfer10			; ne = no
	mov	ax,flags.chrset		; get current file character set
	mov	flags.chrset,932	; force Shift-JIS
	cmp	ax,932			; was Shift-JIS File Character set?
	je	sxfer8			; e = yes
	mov	dx,offset xfchbad2	; show warning message
	mov	ah,prstr
	int	dos
	clc
	ret
sxfer10:cmp	bl,xfr_hebiso		; Hebrew-ISO?
	jne	sxfer8			; ne = no
	mov	ax,flags.chrset		; get current file character set
	mov	flags.chrset,862	; force CP 862
	cmp	ax,862			; was CP862 the File Character set?
	je	sxfer8			; e = yes
	mov	dx,offset xfchbad3	; show warning message
	mov	ah,prstr
	int	dos
	clc
	ret

sxfer2:	cmp	bx,1			; LOCKING-SHIFT?
	jne	sxfer3			; ne = no
	mov	dx,offset xfertab1	; off, on, forced table
	mov	bx,offset xferhlp1
	call	keyend
	jc	sxfer1
	mov	dtrans.lshift,bl	; save state
	mov	trans.lshift,bl
	clc
	ret
sxfer3:	mov	dx,offset xfertab2	; TRANSLATION table
	mov	bx,offset xfchhlp2	; help text
	call	keyend
	jc	sxfer1			; c = error
	mov	dtrans.xchri,bl		; store readable/invertible flag
	mov	trans.xchri,bl
	clc
	ret
sxfer	endp

; SET TRANSLATION 		  Connect mode translate characters
; SET TRANSLATION INPUT {Original-byte New-byte | ON | OFF}
; SET TRANSLATION KEYBOARD {ON | OFF}, default is ON

SETRX	PROC	NEAR			; translate incoming serial port char
	mov	ah,cmkey
	mov	dx,offset trnstab	; direction table (just one entry)
	xor	bx,bx			; no help
	call	comnd
	jnc	setrx0			; nc = success
	ret				; failure
setrx0:	cmp	bx,2			; Keyboard?
	jne	setrx0b			; ne = no
	jmp	setr11			; do keyboard
setrx0b:mov	dx,offset rdbuf		; our work space
	mov	word ptr rdbuf,0	; insert terminator
	mov	bx,offset srxhlp1	; first help message
	mov	ah,cmword		; parse a word
	call	comnd			; get incoming byte pattern
	jnc	setrx0a			; nc = success
	ret
setrx0a:or	ax,ax			; any text given?
	jz	setr6			; nz = no
	mov	temp,ax			; save byte count here
	mov	ax,word ptr rdbuf	; get first two characters
	or	ax,2020h		; convert upper to lower case
	cmp	ax,'fo'			; first part of word OFF?
	je	setr6			; e = yes, go analyze
	cmp	ax,'no'			; word ON?
	je	setr6			; e = yes, go do it
	mov	si,offset rdbuf		; convert text to number
	call	katoi			; number converter procedure, to ax
	jnc	setr1			; nc = success	
	cmp	temp,1			; just one character given?
	jne	setr6			; ne = no, so bad code
setr1:	mov	min,ax			; save byte code here
	mov	dx,offset rdbuf		; our work space
	mov	word ptr rdbuf,0	; insert terminator
	mov	bx,offset srxhlp1	; first help message
	mov	ah,cmword		; parse a word
	call	comnd			; get incoming byte pattern
	jnc	setr2			; nc = success
	ret				; failure
setr2:	or	ax,ax			; any text given?
	jz	setr6			; z = no
	mov	temp,ax			; save byte count here
	mov	si,offset rdbuf		; convert text to number
	call	katoi			; number converter procedure
	jnc	setr3			; nc = success
	cmp	temp,1			; just one character given?
	jne	setr6			; ne = no, so bad code or ON/OFF
setr3:	mov	max,ax			; save byte code here
	mov	ah,cmeol		; get a confirm
	call	comnd
	jnc	setr3a			; nc = success
	ret				; failure
setr3a:	mov	bx,min			; bl = incoming byte code
	xor	bh,bh
	mov	ax,max			; al = local (translated) byte code
	mov	rxtable [bx],al		; store in rx translate table
	clc				; success
	ret

setr6:	mov	ah,cmeol		; get a confirm
	call	comnd
	jnc	setr6a			; nc = success
	ret				; failure
setr6a:	mov	dx,offset badrx		; assume bad construction
	or	word ptr rdbuf,2020h	; convert to lower case
	or	rdbuf+2,20h		; first three chars
	cmp	word ptr rdbuf,'fo'	; key word OFF?
	jne	setr8			; ne = no
	cmp	rdbuf+2,'f'		; last letter of OFF?
	jne	setr8			; ne = no
	mov	rxtable+256,0		; OFF is status byte = zero
	mov	dx,offset rxoffmsg	; say translation is turned off
	jmp	short setr9
setr8:	cmp	word ptr rdbuf,'no'	; keyword ON?
	jne	setr9a			; ne = no, error
	mov	rxtable+256,1		; ON is status byte non-zero
	mov	dx,offset rxonmsg	; say translation is turned on
setr9:	cmp	taklev,0		; executing from a Take file?
	je	setr9a			; e = no
	cmp	flags.takflg,0		; echo contents of Take file?
	je	setr10			; e = no
setr9a:	mov	ah,prstr		; bad number message
	int	dos
setr10:	clc
	ret
setr11:	mov	ah,cmkey		; SET TRANSLATION KEYBOARD
	mov	dx,offset ontab		; on/off
	xor	bx,bx
	call	comnd
	jnc	setr12			; nc = success
	ret
setr12:	push	bx
	mov	ah,cmeol		; get a confirm
	call	comnd
	pop	bx
	jnc	setr13			; nc = success
	ret				; failure
setr13:	mov	flags.xltkbd,bl		; set keyboard translation on/off
	clc
	ret
SETRX	ENDP

; SET TRANSMIT {FILL, LF, Prompt} {ON, OFF, or value}
SETXMIT	proc	near
	mov	dx,offset xmitab	; TRANSMIT keyword table
	xor	bx,bx
	mov	ah,cmkey		; get keyword
	call	comnd
	jnc	setxmi1			; nc = success
	ret
setxmi1:cmp	bl,2			; SET TRANSMIT PROMPT?
	jne	setxmi2			; ne = no
	mov	ah,cmword
	mov	dx,offset rdbuf+1	; put answer here
	mov	bx,offset xpmthlp
	call	comnd
	jc	setxmi1d		; c = error
	push	ax			; save length
	mov	ah,cmeol		; get a confirm
	call	comnd
	pop	cx			; recover length to cx
	jc	setxmi1d		; c = failure
	mov	al,rdbuf+1
	cmp	cx,1			; a single char?
	je	setxmi1c		; e = yes, use it as the prompt char
	mov	si,offset rdbuf+1
	cmp	byte ptr [si],'\'	; already quoted?
	je	setxmi1b		; e = yes
	mov	rdbuf,'\'		; add a numerical quote
	dec	si			; point to our escape char
setxmi1b:call	katoi			; convert number in rdbuf
	jc	setxmi1d		; c = no number, error
setxmi1c:mov	script.xmitpmt,al	; store new prompt value
setxmi1d:ret

setxmi2:cmp	bl,1			; LF?
	jne	setxmi3			; ne = no
	mov	dx,offset ontab		; on or off table
	xor	bx,bx
	call	keyend
	jc	setxmi2a		; c = failure
	mov	script.xmitlf,bl	; set the xmitlf flag
setxmi2a:ret

setxmi3:cmp	bl,0			; FILL?
	jne	setxmi8			; ne = no
	mov	ah,cmword		; FILL, get a word sized token
	mov	dx,offset rdbuf+1	; put it here
	mov	bx,offset xfilhlp	; help
	call	comnd
	jc	setxmix			; c = failure
	push	ax			; save length in ah
	mov	ah,cmeol		; get a confirm
	call	comnd
	pop	ax
	jc	setxmix			; c = failure
	cmp	ax,1			; just one character?
	ja	setxmi4			; a = no, there's more
	mov	al,rdbuf+1		; get the char
	mov	script.xmitfill,al	; store Fill char
	ret
setxmi4:mov	ax,word ptr rdbuf+1
	or	ax,2020h		; to lower
	cmp	ax,'on'			; "none"?
	jne	setxmi5			; ne = no
	mov	script.xmitfill,0	; no Filling
	ret
setxmi5:cmp	ax,'ps'			; "space"?
	jne	setxmi6			; ne = no
	mov	script.xmitfill,' '	; use space as filler
	ret
setxmi6:mov	si,offset rdbuf+1
	cmp	byte ptr [si],'\'	; already quoted?
	je	setxmi7			; e = yes
	mov	rdbuf,'\'		; add a numerical quote
	dec	si			; point to our escape char
setxmi7:call	katoi			; convert number in rdbuf
	jc	setxmix			; c = no number, error
	mov	script.xmitfill,al	; set the xmitfill flag
	ret
setxmi8:mov	ah,cmword		; PAUSE milliseconds
	mov	dx,offset rdbuf+1	; put answer here
	mov	bx,offset xpauhlp
	call	comnd
	jc	setxmix			; c = error
	push	ax			; save length
	mov	ah,cmeol		; get a confirm
	call	comnd
	pop	cx			; recover length to cx
	jc	setxmix			; c = failure
	mov	si,offset rdbuf+1
	cmp	byte ptr [si],'\'	; already quoted?
	je	setxmi9			; e = yes
	mov	rdbuf,'\'		; add a numerical quote
	dec	si			; point to our escape char
setxmi9:call	katoi			; convert number in rdbuf
	jc	setxmix			; c = no number, error
	mov	script.xmitpause,ax	; set the xmitpause flag
setxmix:ret
SETXMIT	endp

; SET UNKNOWN-CHARACTER-SET {DISCARD, KEEP}, default is KEEP
unkchset proc	near
	mov	dx,offset unkctab	; keep/reject keyword table
	mov	bx,offset unkchhlp
	call	keyend
	jc	unkchx			; c = failure
	mov	flags.unkchs,bl		; 0 = keep, else reject
unkchx:	ret
unkchset endp

; Common routine for parsing numerical input
; Enter with numhlp = offset of help message, numerr = offset of optional
;  error message, min, max = allowable range of values.
; Returns value in ax, or does parse error return.
; Changes ax,bx,dx,si.			[jrd] 18 Oct 1987
num0:	mov	dx,offset rdbuf+1	; were to put text
	mov	word ptr rdbuf,0	; clear the buffer
	mov	bx,numhlp		; help text
	mov	ah,cmword		; get a word
	call	comnd
	mov	ah,cmeol
	call	comnd			; Get a confirm
	jc	num0x			; c = failure
					; second entry point
numwd:	mov	si,offset rdbuf+1
	cmp	rdbuf+1,'\'		; already quoted?
	je	num0a			; e = yes
	mov	rdbuf,'\'		; add a numerical quote
	dec	si			; point to our escape char
num0a:	call	katoi			; convert number in rdbuf
	jc	num0er			; c = no number, error
	cmp	ax,max			; largest permitted value
	ja	num0er			; a = error
	cmp	ax,min			; smallest permitted value
	jb	num0er			; b = error
	clc
num0x:	ret				; return value in ax

num0er:	mov	dx,numerr		; comand-specific error message, if any
	or	dx,dx			; was any given?
	jz	num0e1			; z = no, use generic msg
	push	ds
	mov	ax,seg data1		; where help lives
	mov	ds,ax
	mov	ah,prstr
	int	dos			; show given error message
	pop	ds
	jmp	short num0e2
num0e1:	mov	dx,offset nummsg1	; get address of numeric error message
	push	ds
	mov	ax,seg data1
	mov	ds,ax
	mov	ah,prstr
	int	dos
	pop	ds
	mov	ax,min			; smallest permitted number
	call	decout			; display decimal number in ax
	mov	ah,prstr
	push	ds
	mov	dx,seg nummsg2
	mov	ds,dx
	mov	dx,offset nummsg2	; "and"
	int	dos
	pop	ds
	mov	ax,max			; largest permitted number
	call	decout
	mov	ah,prstr
	mov	dx,offset crlf
	int	dos
num0e2:	stc
	ret

; routine to print an error message, then exit without error status
; expects message in dx
reterr	proc	near
	mov	ah,prstr
	int	dos
	clc
	ret
reterr	endp

code	ends
	end
