;------------------------------------------------------------------------
;		Copyright 2010 Integrated Logic Systems
;                  Source Code is Copyright Stephen J. Carden
;
;
;			Please do not share this source!
;------------------------------------------------------------------------
;                              Error.com
;
;	This is a program that functions as a database for error codes.
;	Makemsgb creates a database.
;
;
;
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;------------------------------------------------------------------------
; Notes: o This source code MAY NOT be placed for download on any BBS's!
;        o "mea" is a macro that loads the address of the first field
;          into the pointer specified by the second field.
;------------------------------------------------------------------------
; Assembler:  MADMAC (tm) ST Cross Assembler (Atari Corp)
;             XASM   st and IBM VERSIONS
;------------------------------------------------------------------------

com_file_name:		.macro
;			dc.b   13,"filename.ext",$9b
			dc.b   12,"Error.com   ",$9b
			.endm

;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; File revision history. Version Number in hex
;
file_ver:		equ   $17
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the Month in dec for the first time this revision 
;was compiled
;
c_month:		equ   8
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the day in dec for the first time this revision 
;was compiled
c_day:			equ   28
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; the year in dec for the first time this revision 
;was compiled
c_year:			equ   2012
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Type of compiler used in program
;
; 0 = unknown Compiler
; 1 = xasm
; 2 = mac_65
; 3 = basic
; 4 = compiled basic xl
; 5 = C65
; 6 = Action
;
; We can add more as time goes on!
;
;
;
xasm:			equ	1
mac_65:			equ	2 
basic:			equ	3
basicxl:		equ	4
c65:			equ	5
action:			equ	6

file_compiler:		equ	xasm
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; is this relocatable code ?
; anyother value other than 1 or 2 would be unknown
r..yes:			equ	1
r..no:			equ	2
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define if it can be relocated
l_relocatable:		equ	r..no
;
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Gotta define our Header control Byte
;$ff =bypass intro
;$15 = Bypass Dos check
r..crl:			equ	$7d
r..crlf:		equ	$9b
r..space:		equ	$20
r..bypassDOS:		equ	$15
r..bypass:		equ	$ff

l_frstscreenbyte:	equ	r..space
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; The language the output file is in. 
;
;
; 0	=	Undifined
; 1	=	English
; 2	=	German
;
r..language:		equ	1
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

LOCKED          equ     %00000001
RECEIVED        equ     %00000010
AS_IS           equ     %00000100
UNLOCKED        equ     %11111110
msgbase         equ     $40


			.include	equates
			.include	globals
			.include	macros

			.org  $4000

win_start:
header_info:
			.include	header			; this reads info out of the header and displays to screen
;
tammy:
			lda	#$00
			sta	$52

			ldx	#disk1
			jsr	close
			ldx	#disk2
			jsr	close

			lda	comtab
			CLC 
			adc	# low ZCRNAME
			sta	cli.crunch+1
			lda	comtab+1
			adc	# high ZCRNAME
			sta	cli.crunch+2
			jsr	cli.crunch
			bne	.get_parm1				; nothing there
			jmp	parm_errors

.get_parm1:
			lda	comtab
			CLC 
			adc	# low COMFNAM
			sta	s_ptr
			lda	comtab+1
			adc	# high COMFNAM
			sta	s_ptr+1
			mea	parm_1,d_ptr
			jsr	tostr


			jsr	cli.crunch
			bne	.get_parm2
			jmp	.exit

.get_parm2:		lda	comtab
			CLC 
			adc	# low COMFNAM
			sta	s_ptr
			lda	comtab+1
			adc	# high COMFNAM
			sta	s_ptr+1
			mea	parm_2,d_ptr
			jsr	tostr


			mea	parm_1,s_ptr
			mea	basename,d_ptr
			jsr	toascii

			mea	parm_2,s_ptr
			jsr	do_num
			sta	goto.error
			stx	goto.error+1

.exit:
			lda	consol
			cmp	#consol_any
			beq	.no_tell		;beq
		
			jsr	printsi		
			dc.b	$9b," this is my cli to parm Code"
			dc.b	$9b,"Parm_1 =",-1
			mea	parm_1,pr_ptr
			jsr	echos

			jsr	printsi
			dc.b	$9b,"Parm_2 =",-1
			lda	goto.error
			jsr	pr_byte

			jsr	printsi
			dc.b	$9b,"Output File =",-1
			mea	parm_2,pr_ptr
			jsr	echos

			jsr	printsi
			dc.b	$9b,$9b,-1
			jsr	get_key

.no_tell:
			jmp	top

cli.crunch:		jmp	$0000

parm_errors:

			jsr	printsi
			dc.b	$9B,$9B
			dc.b	" Syntax for use:",$9b
			dc.b	" Error Dn:>dir>fname.ext [parm1]",$9b
			dc.b	" Parm1 = 1-255",$9b

			dc.b	$9b
			dc.b	-1

			jsr	printsi
			dc.b	$9b,$9b
			dc.b	" First Time Use Settings:",$9b
			dc.b	" Error dn:>dir>Filename.ext 0",$9b
			dc.b	" Error Filename 0",$9b
			dc.b	$9b,
			dc.b	"Press any key to continue",$9b,$9b
			dc.b	-1
			lda	#$01
			sta	goto.error
			lda	#$00
			sta	goto.error+1
			jsr	get_key
peekabyte:

			jmp     top

; based on info stored at the front of the file

HEADERSIZE:		equ     406                     ;size of message header
INDEX_CHUNKS:		equ     10                      ;size of index in chunks

aborted:		dc.b	0
invoker:		dc.b	0
move_data:      	dc.b    0
nextmsg:		dc.b	0
w_lastread:     	ds.w    1       	;PRIVATE last read message
u_pagesize:		dc.b	15
s_node:			dc.w	26		; need to change this to the dos build #

s_today:        	ds.b    14              ;today's date
cur_date:       	ds.b    15              ; Mon 13-May-87 + len_byte + $9B
cur_time:       	ds.b    15              ; 11:00:00am    + len_byte + $9B
cur_month:      	ds.b    1               ;1-12
cur_day:        	ds.b    1               ;1-31
cur_year:       	ds.b    1               ;1-99
cur_hour:       	ds.b    1               ;0-23
cur_minute:     	ds.b    1               ;0-59
cur_second:     	ds.b    1               ;0-59
cur_weekday:    	ds.b    1               ;0=sunday, 1=monday,...7=saturday

s_himsg:		dc.w	1
usernum:		dc.w	10
u_name:			dc.b	13,"RealDos Error"
         		ds.b    21              ;real name
u_vw:           	dc.b    40               ;video width
u_lastread:     	ds.w    1               ;last read message number
u_qscan:       		dc.w    $ffff,$ffff	;marked bases flags.

u_flags:       		dc.w    $ffff,$ffff	; 16 individual flags
u_seclvl:       	dc.w    $ffff,$ffff	;security level (32 bits)
u_msgread:      	ds.b    4               ;read messages?
u_msgpost:      	ds.b    4               ;post messages?
u_msgedit:      	ds.b    4               ;edit any message?
u_msgdel:       	dc.w    $ffff,$ffff               ;delete any message?

u_msgprint:     	ds.b    4               ;print any message?
s_msgbdr:       	ds.b    32              ;drives each message base is on
hi_msglo:       	ds.b    32      	;PRIVATE  32 hi message numbers (2 bytes each)
hi_msghi:       	ds.b    32      	;PRIVATE
u_handle:       	ds.b    21              ;handle
;
in_clock:		dc.b	0		;set by the Z: handler functions
tdbuff:			ds.b	30		;time/date buffer area
queue:			dc.b	0		;stuff a key into the
qout:			dc.b	0		;
qin:			dc.b	0		;
rc:			dc.b	0		; Return Code
trans:			dc.b	0		;0=atascii,1=ascii
goto.error:		dc.w	0
;
word:			ds.b    30              ;damn backward compatibility!    

parm_1:			ds.b	256
parm_2:			ds.b	256
parm_3:			ds.b	40
filename:		ds.b	40
in_str:         	ds.b    80              ;
instr:          	ds.b    80              ;
read_buff:      	ds.b    512             ;PRIVATE generic read buffer
;
;       makes a CARD value from s_ptr, returned in A/X
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

valc:			jsr     length                  ;get length
			tay
			iny
			lda     #$9b			    ;install eol
			sta     (s_ptr),y
			lda     s_ptr
			sta     inbuff
			lda     s_ptr+1
			sta     inbuff+1
			lda     #1
			sta     cix
			jsr     afp                     ;ascii --> floating pt.
			bcs     .default
			jsr     fpi                     ;floating --> integer
			bcs     .default
			lda     fr0
			ldx     fr0+1
			rts
.default:		lda     #0
			tax
			rts

;       makes a CARD value from s_ptr, returned in A/X
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
do_num:			jsr     length                  ;get length
			tay
			iny
			lda     #$9b			    ;install eol
			sta     (s_ptr),y
			lda     s_ptr
			sta     inbuff
			lda     s_ptr+1
			sta     inbuff+1
			lda     #4
			sta     cix
			jsr     afp                     ;ascii --> floating pt.
			bcs     .default
			jsr     fpi                     ;floating --> integer
			bcs     .default
			lda     fr0
			ldx     fr0+1
			rts
.default:		lda     #0
			tax
			rts

;       Return the length of string at s_ptr in accumulator
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

length:			ldy     #0
			lda     (s_ptr),y
			rts

;     checks user security level pointed to by S_PTR to see if the
;     bit specified in the A register is ON.  Returns MInus if the
;     user doesn't have the specified security level.
;     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

seclvl:			sty     .tempy
			stx     .tempx
			cmp     #0              ;sec lvl of zero?
			beq     .passed         ;yes..they have it
			tay			     ;-1 for shifting
			lda     #128
			sta     .temp           ;initialize mask
			lda     #0
			sta     .temp+1
			sta     .temp+2
			sta     .temp+3
			dey
			beq     .mask           ;if it was one, no need to shift
.shloop:		lsr     .temp           ;shift bits down to form mask
			ror     .temp+1
			ror     .temp+2
			ror     .temp+3
			dey
			bne     .shloop
			ldy     #0
.mask:			lda     .temp,y         ;now, AND off all the bits
			and     (s_ptr),y
			sta     .temp,y
			iny
			cpy     #4
			bne     .mask
.check:			lda     .temp           ;next, OR all the bytes...if any one
			ora     .temp+1         ; is > 0, then they passed (whew!)
			ora     .temp+2
			ora     .temp+3
			cmp     #0
			beq     .failed
.passed:		lda     #0
			beq     .return
.failed:		lda     #$80
.return:		php
			ldy     .tempy
			ldx     .tempx
			plp
			rts

.tempy:         dc.b    0
.tempx:         dc.b    0
.temp:          dc.b    0,0,0,0




;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Prints a byte or a card to the screen
;
;
pr_byte:
			ldx	#$00
pr_card:
			sta	fr0
			stx	fr0+1
			lda	#$00
			sta	cix
			jsr	ifp
			jsr	fasc
			ldy	#$00
.aprbloop:
			lda	(inbuff),y
			php
			and	#$7f
			iny
			jsr	echo
			plp
			bpl	.aprbloop
			rts

;       get_str    gets an input line into the specified string.
;
;       input:  address of string in in_ptr
;               A - max length
;               X - mask (0=alpha/numeric, 1=numeric)
;               Y - allow return key only (1=yes)
;
;     length of inputted string is returned in the 0th position (and A)
;    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

get_str:		pla                             ;entry here, parms on stack
			sta     pr_ptr
			pla
			sta     pr_ptr+1
			ldy     #1
			lda     (pr_ptr),y              ;get address lo
			sta     in_ptr
			iny
			lda     (pr_ptr),y              ;   address hi
			sta     in_ptr+1
			iny
			lda     (pr_ptr),y              ;   length byte
			sta     .gsa
			iny
			lda     (pr_ptr),y              ;   mask
			sta     .gsx
			iny
			lda     (pr_ptr),y              ;   return allowed?
			sta     .gsy
			lda     pr_ptr                  ;now, correct stack
			clc
			adc     #5
			sta     pr_ptr
			lda     pr_ptr+1
			adc     #0
			pha
			lda     pr_ptr
			pha
			lda     .gsa
			ldx     .gsx
			ldy     .gsy
			jmp     get_string

.gsa:			dc.b    0
.gsx:			dc.b    0
.gsy:			dc.b    0


get_string:		cmp     #83                     ;max < 83?
			bcc     .gsentry                ;yes
			lda     #80
.gsentry:		sta     .maxlth                 ;save max length
			stx     .mask
			sty     .return

			lda     #0
			sta     attract


			lda     in_ptr
			sta     .address	;hold address of string
			lda     in_ptr+1	; to input into
			sta     .address+1
			lda     #0
			sta     .instr		;zero length byte

.gsloop:		jsr     get_key		;get a keypress
;			cmp     #'\'		;can't allow this key!
;			beq     .gsloop		;yes, ignore
			cmp     #$9b		;carriage return?
			bne     .nogs		;nope
			jmp     .gscr		;yes
.nogs:			cmp     #14		;treat ^N as a C/R
			bne     .not14
			jmp     .gscr
.not14:			cmp     #126		;backspace?
			beq     .bspace
			cmp     #30
			beq     .bspace
			cmp     #24		;^X?
			bne     .nowipe
			jmp     .wipeout
.nowipe:		and     #$7f		;strip inverse bit
			sta     .key		;hold it

;       now, see if it falls within requested mask
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


			ldx     .mask		;get mask
			beq     .alphanum
			cpx     #1
			beq     .numeric

.alphanum:		cmp     #32		; < space?
			bcc     .gsloop		;yes, ignore
			cmp     #'z'+1		; > 'z'
			bcs     .gsloop		;yes, ignore
			jmp     .gsstore

.numeric:		cmp     #'0'		; < '0'?
			bcc     .gsloop		;yes, ignore
			cmp     #'9'+1		; > '9'
			bcs     .gsloop		;yes, ignore
			jmp     .gsstore

.gsstore:		lda     .instr		;get length byte
			cmp     .maxlth
			beq     .gsloop		;ignore it..at
			bcs     .gsloop		; max length now
			tay
			iny
			lda     .key
			cmp     #32		;is it a space?
			bne     .notspc		;nope
			cpy     #1		;1st char in line?
			bne     .notspc
			jmp     .gsloop		;yes..don't allow it
.notspc:		sta     .instr,y	;save key into string
			inc     .instr		;increase length
.nohide:		lda     .key
.showit:		jsr     echo
			lda     .maxlth		;one-key input?
			cmp     #1
			bne     .nohot		;nope
			lda     .instr
			cmp     #1		;1 byte entered?
			bne     .nohot
			lda     u_flags
			and     #f_hotkey	;user want hotkeys?
			beq     .nohot		;nope..
			jmp     .gscr		;yes - auto carriage return!
.nohot:			jmp     .gsloop		;keep cookin..

.bspace:		lda     .instr		;can't backspace
			cmp     #0		; if no length!
			bne     .nt0
			jmp     .gsloop
.nt0:			dec     .instr		;-1 to length byte
			lda     #126		;output a BSpace
			jsr     echo
			jmp     .gsloop

.wipeout:		lda     .instr		;can't backspace if no length
			bne     .wt0
			jmp     .gsloop
.wt0:			dec     .instr		;-1 to length byte
			lda     #126		;output a BSpace
			jsr     echo
			jmp     .wipeout	;go until line cleared

.gscr:			lda     .instr		;get length
			bne     .nonull		;its not null
			ldx     .return		;allow return only?
			bne     .nonull		;yes..exit
			jmp     .gsloop		;nope..ignore it

;       done, so move our string into where they want it.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.nonull:		lda     s_ptr
			sta     .holds
			lda     s_ptr+1
			sta     .holds+1
			lda     d_ptr
			sta     .holdd
			lda     d_ptr+1
			sta     .holdd+1
			mea     .instr,s_ptr
			lda     .address
			sta     d_ptr
			lda     .address+1
			sta     d_ptr+1
			jsr     move_str	;move to their area..
			lda     .holds
			sta     s_ptr
			lda     .holds+1
			sta     s_ptr+1
			lda     .holdd
			sta     d_ptr
			lda     .holdd+1
			sta     d_ptr+1
			lda     .instr		;return with lth in A
			rts


.address:		ds.b    2
.holds:			ds.b    2
.holdd:			ds.b    2
.maxlth:		ds.b    1
.mask:			ds.b    1
.return:		ds.b    1
.key:			ds.b    1
.instr:			ds.b    86


;       Search the string pointed to by S_PTR for the substring
;       pointed to by D_PTR.  Returns a 0 if substring not found.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

substr:			ldy     #0
			lda     (s_ptr),y
			sta     .s_lth
			lda     (d_ptr),y
			sta     .d_lth
			lda     s_ptr
			sta     .comp+1
			lda     s_ptr+1
			sta     .comp+2
			lda     d_ptr
			sta     .get+1
			lda     d_ptr+1
			sta     .get+2
			lda     #1
			sta     .loc
			ldy     #0
			ldx     #0
			jsr     .up_str
			jsr     .up_sub

.comp:			lda     $ffff,y
			jsr     upcase
			sta     .temp
.get:			lda     $ffff,x
			jsr     upcase
			cmp     .temp
			beq     .match
.nomatch:		ldx     #1
			inc     .loc
			jsr     .up_str
			jmp     .comp
.match:			jsr     .up_sub
			jsr     .up_str
			jmp     .comp

.up_str:		iny
			cpy     .s_lth
			bcc     .xup
			beq     .xup
			pla
			pla
			lda     #0
.xup:			rts

.up_sub:		inx
			cpx     .d_lth
			bcc     .xsub
			beq     .xsub
			pla
			pla
			lda     .loc
.xsub:			rts

.temp:			dc.b    0
.loc:			dc.b    0
.s_lth:			dc.b    0
.d_lth:			dc.b    0

;     Make an ASCII ($9b terminated) string out of a lth-byte string
;     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

toascii:		ldy     #0
			lda     (s_ptr),y               ;get lth byte
			sta     .lth
			beq     .null
			iny
			inc     .lth
.loop:			lda     (s_ptr),y
			dey
			sta     (d_ptr),y
			iny
			iny
			cpy     .lth
			bne     .loop
.done:			ldy     .lth
			dey
.rtn:			lda     #$9b
			sta     (d_ptr),y
			rts
.null:			ldy     #0
			beq     .rtn

.lth:			dc.b    1

;     Make a length-byte string from an ASCII ($9b terminated) string
;     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tostr:			ldy     #0
.tfe:			lda     (s_ptr),y               ;find end of string
			beq     .gotend
			cmp     #$9b
			beq     .gotend
			iny
			bne     .tfe
.gotend:		sty     .lth			    ;save length
			cpy     #0
			beq     .rtn
			ldy     #0
.loop:			lda     (s_ptr),y
			iny
			sta     (d_ptr),y
			cpy     .lth
			bcc     .loop
.rtn:			ldy     #0
			lda     .lth
			sta     (d_ptr),y
			rts

.lth:			dc.b    1


;============================================================================
;   These routines work very much like the filename crunch routines in
;   SpartaDOS.   On consecutive calls to CRUNCH, the next word from the
;   command line (pointed to by IN_PTR) will be placed into the field WORD.
;   If there are no more words on the command line, then CRUNCH will return
;   with carry set.
;     When calling the parser for the first time on a new input line, you
;   MUST zero out the BUFOFF field.  Also, extraneous spaces will be
;   compressed out of the input line, and the command word will be folded
;   to upper case.
;============================================================================


crunch:			ldy     bufoff          ;get index into line
			ldx     #0              ;zero storing index

.nonspc:		lda     instr,y         ;move buffoff to next non-space
			cmp     #$9b
			beq     .nomore         ;end of line -- signal error
			cmp     #32             ;= space?
			beq     .go_on          ;yep
			cmp     #','
			beq     .go_on
			cmp     #'-'
			beq     .go_on
			bne     .gotone         ;found one
.go_on:			iny
			bne     .nonspc         ;keep looking

.nomore:		sec                     ;signal error
			rts                     ; and return

.gotone:		lda     instr,y         ;get byte of command line
			cmp     #$9b
			beq     .end            ;end of line

			cmp     #32             ;space?
			beq     .end            ;yes
			cmp     #','
			beq     .end
			cmp     #'-'
			beq     .end

			jsr     upcase
			sta     word,x
			inx
			iny
			bne     .gotone         ;keep going

.end:			sty     bufoff          ;save new bufoff value
			lda     #$9b            ;put null byte at end of word
			sta     word,x
			clc
end_crunch:		rts



;--------------------------------------------
;printsi Routeen macro             
;this is a slow print to the screen
;usage jsr	printsi
;    .byte $9b," string to be printed",$ff
;--------------------------------------------
;
printsi:
			pla
			sta	.pstr+1
			pla
			sta	.pstr+2
.prsl:			inc	.pstr+1
			bne	.pstr
			inc	.pstr+2
.pstr:			lda	$ffff
			cmp	#$ff
			beq	.estri
			jsr	fast_output
			jmp	.prsl
.estri:			lda	.pstr+2
			pha
			lda	.pstr+1
			pha
			rts
;
;	

;
;-------------------------
;actual screen handler    
;screen offset definitions
;-------------------------
scr_offset:
			dc.w	0
			dc.w	40
			dc.w	80
			dc.w	120
			dc.w	160
			dc.w	200
			dc.w	240
			dc.w	280
			dc.w	320
			dc.w	360
			dc.w	400
			dc.w	440
			dc.w	480
			dc.w	520
			dc.w	560
			dc.w	600
			dc.w	640
			dc.w	680
			dc.w	720
			dc.w	760
			dc.w	800
			dc.w	840
			dc.w	880
			dc.w	920
;;
;sprint:
;echo:
fast_output:
			sta	$5a
			lda	sc.redirect
			cmp	#print_p
			beq	.go_cio
.leo:
			lda 	$02ff   	; CONTROL 1 FLAG
			cmp 	#$00
			bne 	.leo

			lda	$5a
			cmp	#32
			bcc	.do_look
			cmp	#125
			bcc	.do_here
.do_look:		ldy	#0
.lookup:		lda	.char_table,y
			beq	.do_here
			cmp	$5a
			beq	.go_cio
			iny
			bne	.lookup


.go_cio:		lda	$5a
			jmp	putlocal


.do_here:		ldy	#0
			lda	$5d
			sta	($5e),y
			lda	$5a
			cmp	#$9b
			beq	.docr
.notcr:			jsr	.get_adr
			lda	$5a
			and	#$7f
			cmp	#32
			bcc	.add64
			cmp	#96
			bcc	.sub32
			jmp	.asis
.add64:			clc
			adc	#64
			jmp	.asis
.sub32:			sec
			sbc	#32
.asis:			bit	$5a
			bpl	.xxlate
			ora	#$80
.xxlate:		ldy	#0
			sta	($5e),y
			inc	$55
			inc	$63
			lda	$55
			cmp	#40
			bcs	.next_row
			iny
			lda	($5e),y
			sta	$5d
			ora	#$80
			ldx	752
			bne	.nocurs1
			sta	($5e),y
.nocurs1:		inc	$5e
			bne	.nooav
			inc	$5e+1
.nooav:			rts
.docr:			lda	#0
			sta	$63
.next_row:		lda	$52
			sta	$55
			inc	$54
			ldy	$54
			cpy	#24
			bcs	.scroll
			jsr	.get_adr
			ldy	#0
			lda	($5e),y
			sta	$5d
			ldx	752
			bne	.nocurs2
			ora	#$80
			sta	($5e),y
.nocurs2:		rts

.scroll:		dec	$54
			lda	$58
			sta	$68
			clc
			adc	#40
			sta	$5e
			lda	$58+1
			sta	$68+1
			adc	#0
			sta	$5e+1
			ldy	#0
			ldx	#3
.scloop:		lda	($5e),y
			sta	($68),y
			iny
			bne	.scloop
			inc	$5e+1
			inc	$68+1
			dex
			bne	.scloop
.scloop2:		lda	($5e),y
			sta	($68),y
			iny
			cpy	#152
			bcc	.scloop2
			lda	$58
			clc
			adc	#920&$ff
			sta	$5e
			lda	$58+1
			adc	#920/256
			sta	$5e+1
			ldy	#0
			tya
.clear:			sta	($5e),y
			iny
			cpy	#40
			bcc	.clear
			ldx	752
			bne	.nocurs3
			lda	#$80
			ldy	$55
			sta	($5e),y
.nocurs3:		lda	#0
			sta	$5d
			lda	$5e
			clc
			adc	$55
			sta	$5e
			lda	$5e+1
			adc	#0
			sta	$5e+1
			rts
.get_adr:		lda	$54
			asl
			tay
			lda	$58
			clc
			adc	scr_offset,y
			sta	$5e
			lda	$58+1
			adc	scr_offset+1,y
			sta	$5e+1
			lda	$5e
			clc
			adc	$55
			sta	$5e
			lda	$5e+1
			adc	#0
			sta	$5e+1
			rts
.char_table:
			dc.b	27,28,29,30,31,125,126,127
			dc.b	156,157,158,159,253,254,255,0


putlocal:
			ldx	#11
			stx	$0342
			ldx	#0
			stx	$0348
			stx	$0349
			jmp	$e456
;
;
;------------
; PRINT BYTE 
;------------
echo:
sprint:			sta	$b8
			lda	#$04
			sta	$034a
			stx	$b6
			sty	$b7
			ldx	#$06
			lda	$e400+1,x
			sta	$b3
			lda	$e400,x
			sta	$b2
			lda	$b8
			jsr	.a
			ldx	$b6
			ldy	$b7
			lda	#$0c
			sta	$034a
			lda	$b8
			rts
;
.a:			inc	$b2
			jmp	($b2)
			rts

;  prints: prints string with address in PR_PTR, length byte in 0th byte
;  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

echos:
                	ldy     #0
                	lda     (pr_ptr),y              ;get length to print
                	beq     .xpri                   ;exit, null string
                	sta     .maxlth
                	iny
                	sty     .tempy

.pril:          	ldy     .tempy
                	lda     (pr_ptr),y
                	jsr     echo
.prnext:        	inc     .tempy
                	dec     .maxlth
                	bne     .pril
.xpri:          	rts

.tempy:         	dc.b    1
.maxlth:        	dc.b    1



;       lookup command in table (in s_ptr) and execute it
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

execute:		jsr     upcase
			sta     .key
			ldy     #0
.loop:			lda     (s_ptr),y
			beq     .found
			cmp     .key
			beq     .found
			iny
			iny
			iny
			bne     .loop
.found:			iny
			lda     (s_ptr),y
			sta     .goaddr+1
			iny
			lda     (s_ptr),y
			sta     .goaddr+2
.goaddr:		jmp     $ffff
.key:			dc.b    0

;------------------------------
;   GET KEY  ROUTEEN           
;this routeen get's one key and
;return it to the accumlator   
;------------------------------
;
get_key:		stx	$b6
			sty	$b7
			jsr	.a
			ldx	$b6
			ldy	$b7
			rts
.a:			lda	$e420+5
			pha
			lda	$e420+4
			pha
			rts



cr_lf:			lda	#$9b
			jsr	echo
			rts


open_in:		lda 	#4
			jmp 	do_open

open_out:		lda 	#8
			jmp 	do_open

open_app:		lda 	#9
			jmp 	do_open

open_updt:		lda 	#12
			jmp 	do_open

open_dir:		lda 	#6
			jmp 	do_open

open_raw:		lda 	#20     	;open raw directory
			jmp 	do_open

do_open:
			pha 		        ;open filespec pointed to by fn_ptr
			txa 		        ;
			pha
			jsr 	close		;make sure device is closed
			pla
			tax
			pla
			sta 	icaux1,x
			lda 	#ccopen
			sta 	iccom,x
			lda	fn_ptr
			sta 	icbal,x
			lda 	fn_ptr+1
			sta	icbah,x
			lda	#80
			sta	icbll,x
			lda	#0
			sta 	icblh,x
			lda 	#128		;for long directory (shouldn't hurt??)
			sta	icaux2,x
			jmp 	ciov

close:			lda 	#ccclose
			sta 	iccom,x
			jmp 	ciov


upcase:         	cmp     #'a'
			bcc     xupcase
			cmp     #'z'+1
			beq     xupcase
			bcs     xupcase
			sec
			sbc     #32
xupcase:		rts


locase:			cmp     #'A'
			bcc     xlocase
			cmp     #'Z'+1
			beq     xlocase
			bcs     xlocase
			clc
			adc     #32
xlocase:		rts


;       Compares the strings pointed to by s_ptr and d_ptr.
;       Returns with equal set if true, not equal if false.
;       The comparison IS NOT case-sensitive.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cmp_str:		ldy     #0              ;zero index
			lda     (s_ptr),y       ;get length bytes
			sta     .lth
			cmp     (d_ptr),y       ;they match?
			bne     .noteq          ;nope
			cmp     #0              ;null string?
			beq     .end            ;they match
			iny
.loop:			lda     (s_ptr),y       ;get next byte
			jsr     upcase
			sta     .temp
			lda     (d_ptr),y       ;they match?
			jsr     upcase
			cmp     .temp
			bne     .noteq          ;nope..
			cpy     .lth            ;end of string?
			beq     .end            ;yes -- they match
			iny
			bne     .loop
.noteq:			lda     #1              ;set BNE
			rts
.end:			lda     #0              ;set BEQ
			rts
.lth:			dc.b    1
.temp:			dc.b    1


y_or_n:			jsr     get_str
			dc.w   .temp
			dc.b   1,0,0
			lda     .temp+1
			jsr     upcase
			cmp     #'Y'
			beq     .yes
			cmp     #'N'
			beq     .no
			lda     #126			    ;backspace
			jsr     echo
			jmp     y_or_n			  ; and try it again
.no:			jsr     cr_lf
			lda     #'N'
			rts
.yes:			jsr     cr_lf
			lda     #'Y'
			rts
.temp:			ds.b    2

;       Concatenate the string at s_ptr onto the end of string at d_ptr
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

concat:         ldy     #0
                lda     (s_ptr),y               ;get source lth
                sta     .srclth
                beq     .end                    ;null string..exit
                lda     (d_ptr),y               ;get dest lth
                sta     .desty
                inc     .srcy
                inc     .desty
                ldy     #1                      ;always start at 1 on
                sty     .srcy                   ; the source string
.loop:          ldy     .srcy
                lda     (s_ptr),y               ;get byte from source
                ldy     .desty
                sta     (d_ptr),y               ;store into dest
                lda     .srcy
                cmp     .srclth                 ;end of string?
                beq     .end                    ;yes
                inc     .srcy
                inc     .desty                  ;up indexes
                bne     .loop                   ;and keep going
.end:           ldy     #0
                lda     (d_ptr),y               ;up the length byte
                clc                             ; of the dest string
                adc     .srclth
                sta     (d_ptr),y
                rts

.srcy:          dc.b    1
.desty:         dc.b    1
.srclth:        dc.b    1

;       print a string to device in X register
;       from the string pointed to by pr_ptr.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

printsd:        ldy     #0
                lda     (pr_ptr),y      ;install EOL at end of string
                tay
                iny
                lda     #$9b
                sta     (pr_ptr),y
                lda     #ccputrec
                sta     iccom,x
                lda     pr_ptr
                clc
                adc     #1              ;offset past length byte
                sta     icbal,x
                lda     pr_ptr+1
                adc     #0
                sta     icbah,x
                lda     #80
                sta     icbll,x
                lda     #0
                sta     icblh,x         ;zero hi byte of length
                jmp     ciov            ;get the line



;       Move string pointed to by s_ptr to string pointed to by d_ptr
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


move_str:		ldy     #0
			lda     (s_ptr),y               ;get lth byte
			tay                             ;go backwards
.loop:			lda     (s_ptr),y
			sta     (d_ptr),y
			cpy     #0
			beq     .done
			dey
			jmp     .loop
.done:			rts

datestamp:		lda     in_clock        ;hold current tdline status
			sta     .hold
			lda     #1              ;signal for the VBI to leave
			sta     in_clock        ;  the TDLINE alone..
			jsr     waitvbi         ;make sure VBI is done
			lda     #13
			sta     cur_date
			lda     #10
			sta     cur_time
			jsr     get_TD          ;get the date/time/weekday
			lda     .hold           ;restore tdline status
			sta     in_clock
			mea     cur_date,s_ptr  ;if date has changed, then
			mea     s_today,d_ptr   ; reset calls_today to 0
			jsr     cmp_str
			beq     .noupd
			jsr     move_str
.noupd:			rts

.hold:			dc.b    0


showdate:		jsr     datestamp
			mea     cur_date,pr_ptr
			jmp     echos


showtime:		jsr     datestamp
			mea     cur_time,pr_ptr
			jmp     echos



;       Wait for a VBI to occur
;       ~~~~~~~~~~~~~~~~~~~~~~~

waitvbi:		lda	$14
.lp1:			cmp	$14
			beq	.lp1
			lda	$14
.lp2:			cmp	$14
			beq	.lp2
			rts

;          get both the "formatted" and "unformatted" date and time
;          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

get_TD:			lda     portb                   ;save PORTB setting
			pha
			and     #$FE
			sta     portb			   ;enable RAM under OS
			ldy     #.tdbuff & $ff
			ldx     #.tdbuff / 256
			jsr     $FFC9			   ;get formatted time/date
			jsr     $FFC0			   ;get unformatted time/date
			pla
			sta     portb			   ;restore PORTB setting
			bcc     .noerr

.error:			lda     #0
			sta     cur_date
			sta     cur_time
			rts

;       move the formatted date into cur_date, cur_time
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.noerr:			ldy     #0
			ldx     #1
.lp:			lda     tdbuff,y
			sta     cur_date,x
			inx
			iny
			cpy     #13
			bne     .lp
			ldx     #1
			iny
.lp2:			lda     tdbuff,y
			sta     cur_time,x
			inx
			iny
			cpy     #24
			bne     .lp2

;       move the unformatted date into the byte fields
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			ldy     #13			     ;point to DATER
			lda     (comtab),y
			sta     cur_day
			iny
			lda     (comtab),y
			sta     cur_month
			iny
			lda     (comtab),y
			sta     cur_year
			iny
			lda     (comtab),y
			sta     cur_hour
			iny
			lda     (comtab),y
			sta     cur_minute
			iny
			lda     (comtab),y
			sta     cur_second

;           now, figure out what weekday it is
;           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			lda     cur_date+2              ;2nd char of the dateline
			cmp     #'o'
			beq     .monday
			cmp     #'e'
			beq     .wednesday
			cmp     #'h'
			beq     .thursday
			cmp     #'r'
			beq     .friday
			cmp     #'a'
			beq     .saturday
.tues_sun:		lda     cur_date+1
			cmp     #'T'
			beq     .tues
.sun:			lda     #0
			jmp     .exit
.tues:			lda     #2
			jmp     .exit
.monday:		lda     #1
			jmp     .exit
.wednesday:		lda     #3
			jmp     .exit
.thursday:		lda     #4
			jmp     .exit
.friday:		lda     #5
			jmp     .exit
.saturday:		lda     #6
.exit:			sta     cur_weekday
			rts

.tdbuff:		ds.b    30




inv_hold:       	dc.b 0

good_get:
			lda     inv_hold
			sta     invoker
			lda     curbase
			sta     override
			jsr     getbase
			jmp     b_a
;

top:
			tsx
			stx     msgstack
			lda     #1
			sta     ed_amsg			 ;flag valid msg
			lda     #m_data & $ff
			sta     ed_msgbuff
			lda     #m_data / 256
			sta     ed_msgbuff+1
;;;;;;;;        	mea     m_data,ed_msgbuff       ;point to our buffer
			jsr     clear_thread            ;clear thread pointers
			lda     #0
			sta     curbase
			sta     quickscan
			sta     nonstop
			sta     lastsrch
			sta     keyword




;	        	jsr     nextbase                ;find first base w/new msgs


.yequ:          	sta     override
			cmp     #0
			bne     .got_base
			lda     quickscan               ;scanning?
			bpl     msgmain                 ;nope
			jsr     printsi
			dc.b    "No new text found in your marked",$9b
			dc.b    "base list...",$9b,-1
			rts

.got_base:
;			jsr     printsi
;			dc.b   "Going to base #",-1
;			lda     override
;			jsr     pr_byte
;			jsr     cr_lf

msgmain:        	jsr     getbase
			jsr     proc_base
			jmp     msgmain


exitmsg:
			jsr     close_base
			ldx     msgstack
			txs
			rts





msgerror:
			jsr     printsi
			dc.b   $9b,"Critical Database error!",$9b,-1
			jmp     exitmsg


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


getbase:
			jsr     close_base
			jsr     open_base
			bpl     .noe1
			jmp     .baseerr
.noe1:          	jsr     readidx
			bpl     .noe2
			jmp     .baseerr
.noe2:          	lda     i_maxmsgs
			bne     .noe3
			jmp     .baseerr
.noe3:
			lda     i_totbytes
			sta     ed_maxbytes             ;set max bytes for editor
			lda     i_totbytes+1
			sta     ed_maxbytes+1
			lda     i_graphics
			sta     ed_graphics             ;same for /Graphics mode


;       update the system_hi_message table
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			ldy     #$01                  ;index into hi_msg table
			dey
			ldx     i_inuse                 ;point to last message
			lda     i_msgnlo,x
;			sta     hi_msglo,y
			lda     i_msgnhi,x
;			sta     hi_msghi,y

;~~~~~~~~~~~~~~~~right here ~~~~~~~~~~~~~~~~
;			lda     move_data
;			cmp     #'M'
;			beq     .ov1
			jsr     printsi
			dc.b    $9b,"There are ",-1
			lda     i_inuse
			jsr     pr_byte
			jsr     printsi
			dc.b    " messages: ",-1
			jsr     firnewmsg
			cmp     #0
			beq     .nonew
			jsr     printsi
			dc.b    "New messages",$9b,-1
			jmp     .ov1
.nonew:
			jsr     printsi
			dc.b    "None new",$9b,-1
.ov1:           	lda     asknum
			sta     curbase
			lda     #0


;			lda	goto.error
;			jsr	b_steve


			rts
.baseerr:       	ldx     #msgbase
			jsr     close
			jsr     printsi
			dc.b    $9b,$9b,"Unable to load Error Base",$9b,-1
			jmp     exitmsg



;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;       The main processor for the message base areas.
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

proc_base:		lda     #1
			sta     verbose
			lda     #0
			sta     topthread               ;not in a thread
			jsr     clear_thread            ;clear thread pointers
			jsr     firnewmsg               ;find first new msg

			jsr	b_steve

			tay
			cmp     #0                      ;find a new one?
			beq     .nonew                  ;nope

;			dey                             ;yes, set curmsg to -1

			jmp     .gotone
.nonew:			ldy     i_inuse                 ;no - use the in_use
.gotone:		sty     curmsg                  ;save in current msg
			lda     #1
			sta     steprate
			lda     quickscan               ;quickscanning?
			beq     do_base                 ;nope
			lda     #$9b
			jsr     stuff                   ;yes - force first read

do_base:
.not_over:		lda     curmsg                  ;make sure we're never
			cmp     i_inuse                 ; OVER the total in use
			beq     dbinrng
			bcc     dbinrng
			lda     i_inuse
			sta     curmsg
dbinrng:		lda     topthread
			beq     dbnoth
			jsr     printsi
			 dc.b   $9b,"(Thread #",-1
			lda     topthread
			jsr     pr_byte
			lda     #')'
			jsr     echo

dbnoth:
			jsr     printsi
			dc.b    $9b,"[DataBase #"-1
;			lda     curbase
;			jsr     pr_byte
			jsr     printsi
			dc.b    "] ",-1
;
;  Addeding new code

			jsr     printsi
			 dc.b   '(',-1
			lda     quickscan               ;scanning?
			beq     .dbNOscan
			jsr     printsi
			 dc.b   "Q/",-1
.dbNOscan:
			lda     curmsg
			jsr     pr_byte                 ;50/100+ General:
			lda     #'/'
			jsr     echo
			lda     i_inuse
			jsr     pr_byte
			lda     steprate
			cmp     #$ff
			bne     dbplus
			lda     #'-'
			bne     dbpr2
dbplus:			lda     #'+'
dbpr2:			jsr     echo
			jsr     printsi
			dc.b   ") ",-1
			mea     i_name,pr_ptr
			jsr     echos
			jsr     printsi
			dc.b    ": ",-1

;       if we're in a non-stop scan, fake a c/r key
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			lda     nonstop
			bpl     .no_NON
			lda     #0
			sta     instr
			jsr     cr_lf
			jsr     cr_lf
			jmp     db_force                ;force a c/r

.no_NON:		jsr     get_str
			dc.w   instr
			dc.b   1,0,1
			jsr     cr_lf                   ; 2 carriage returns
			jsr     cr_lf
db_force:		lda     instr                   ;just hit cr?
			bne     dbacmd                  ;nope
			lda     #$9b
			bne     dbover
dbacmd:

			lda     instr+1
			jsr     upcase
dbover:
			pha
			mea     basecmds,s_ptr
			pla
			jsr     execute
			jmp     do_base

b_bad:
			jsr     printsi
			dc.b    "What!  Enter ? for help",$9b,-1
			rts


b_x:

			jsr     printsi
			dc.b    "Exiting Database...",$9b,-1
			jmp     exitmsg


b_cr:			lda     topthread
			beq     .bcc1
			jmp     .thread
.bcc1:			lda     steprate
			bmi     .backw
.forw:			lda     curmsg                  ;go to next message
			cmp     i_inuse
			bcs     .atend
			inc     curmsg
			jsr     showmsg
			rts
.backw:			lda     curmsg
			cmp     #2
			bcc     .atend
			dec     curmsg
			jsr     showmsg
			rts
.atend:			lda     quickscan               ;scanning?
			beq     .no_QS                  ;nope

.no_QS:
			jsr     printsi
			dc.b    "No more messages in this direction!",$9b,-1
			lda     steprate
			bmi     .nosay
			jsr     printsi
			dc.b    "(press ""/"" for next base w/new msgs)",$9b,-1
.nosay:			rts

.thread:		lda     curmsg
			jsr     add_thread              ;add it to the stack
			jsr     nextchild               ;find next child
			cmp     #0                      ;find one?
			beq     .nochild                ;nope
.child:			sta     curmsg
			jsr     showmsg
			rts
.nochild:		jsr     clear_thread            ;blow away stack..
			jsr     printsi
			dc.b   "End of thread"
			dc.b   59                   ;';'
			dc.b   " returning to msg #",-1
			lda     topthread
			jsr     pr_byte
			jsr     cr_lf
			lda     topthread
			sta     curmsg
			lda     #0
			sta     topthread
			rts


b_a:			jsr     check0
			lda     curmsg
			bne     .goon
			jmp     b_cr
.goon:			lda     verbose                 ;hold verbose
			sta     .hold
			lda     #1
			sta     verbose
			jsr     showmsg                 ;show this message again
			lda     .hold
			sta     verbose
			rts
.hold:			dc.b    0



b_b:			lda     #$ff                    ;set direction backward
			sta     steprate
			jsr     printsi
			dc.b    "Read direction now BACKWARD",$9b,-1
			lda     topthread
			beq     .exit
			lda     #0
			sta     topthread
			jsr     printsi
			dc.b    "(thread broken)",$9b,-1
.exit:
			rts



b_d:			jsr     check0
			jsr     getmsg
			mea     u_msgdel,s_ptr
			lda     curbase                 ;if they have sysop, its ok
			jsr     seclvl
			bpl     .cando
			mea     m_from,s_ptr            ;if they wrote it, it's OK
			mea     u_handle,d_ptr
			jsr     cmp_str
			beq     .cando
.nocando:
			jsr     printsi
			dc.b    "You can't delete this message!",-1
			rts
.cando:
			jsr     showheader
			jsr     printsi
			dc.b   $9b,"Delete this message? ",-1
			jsr     y_or_n
			cmp     #'Y'
			beq     .dodel
			rts
.dodel:			lda     curmsg
			jsr     delmsg
			jsr     writeidx
			jsr     printsi
			dc.b    $9b,"Message deleted",$9b,-1
			lda     topthread
			beq     .ex
			jsr     printsi
			dc.b   "(thread broken)",$9b,-1
			lda     #0
			sta     topthread
.ex:            	rts



b_e:            	jsr     check0
			jsr     getmsg                  ;edit this message
			mea     u_msgedit,s_ptr
			lda     curbase                 ;if they have sysop, its ok
			jsr     seclvl
			bpl     .cando
			mea     u_msgpost,s_ptr
			lda     curbase                 ;make sure they can post here
			jsr     seclvl
			bmi     .nocando
			mea     m_from,s_ptr            ;if they wrote it, it's OK
			mea     u_handle,d_ptr
			jsr     cmp_str
			beq     .cando
.nocando:
			jsr     printsi
			dc.b    "You can't edit this message!",-1
			rts

.cando:
			ldx     m_lines
			inx
			stx     linenum
			mea     m_title,s_ptr
			mea     ed_title,d_ptr
			jsr     move_str
			mea     m_to,s_ptr
			mea     ed_toname,d_ptr
			jsr     move_str
			lda     m_width
			sta     ed_width
			jsr     msgedit                 ;get the message
			sta     m_lines
			jsr     cr_lf
			lda     m_lines
			bne     .notabt
			jsr     printsi
			dc.b    $9b,"Aborted...",$9b,-1
			rts
.notabt:

			lda     consol
			cmp     #$07
			bne     .plop
			mea     u_seclvl,s_ptr
			lda     #32      ;if they have sysop, its ok
			jsr     seclvl
			bpl     .plop
			lda     #1
			dc.b    $2c
.plop:			lda      #0      ; messase edited but not shone       
			sta     m_edited
			jsr     printsi
			dc.b    $9b,"Re-saving message...",-1
			lda     #0                      ;if edited, remove its
			sta     m_node                  ; network identifiers...
			sta     m_node+1
			sta     m_nodename
			sta     m_nodecity
			jsr     clear_nodes             ;clear out node list
			lda     curmsg
			jsr     writemsg                ;write it to disk...
			bmi     .noidx
			jsr     writeidx                ;re-write the index
.noidx:
			jsr     printsi
			dc.b    "Done!",$9b,-1
			rts


b_f:            	lda     #1                      ;set direction forward
			sta     steprate
			jsr     printsi
			dc.b    "Read direction now FORWARD",$9b,-1
			rts


b_steve:
			lda	goto.error
			cmp	#$00
			beq	.exit
			lda	goto.error
			sta     curmsg
			lda	#$00
			sta	goto.error
			lda	#$3f
			sta	$2fc
			lda     curmsg
.exit:          	rts




b_j:			jsr     check0
			jsr     printsi
			dc.b    "Jump to Error #",-1
			jsr     getmsgnum
;			bcs     .exit
			sta     curmsg
			jsr     showmsg
.exit:          	rts


b_k:			jsr     check0
			lda     #0
			sta     lastsrch
			sta     keyword
			jsr     printsi
			dc.b   "Enter a full or partial word to look",$9b
			dc.b	"for: ",-1
			jsr     get_str
			dc.w   keyword
			dc.b   20,0,1
			jsr     cr_lf
			jsr     cr_lf
			lda     keyword
			bne     .goon
			jsr     printsi
			dc.b   "Aborted...",$9b,-1
			rts
.goon:          	jmp     b_c



b_c:			jsr     check0
			lda     #0
			sta     aborted
			lda     keyword
			bne     .gotkey
			jmp     b_k
.gotkey:
			jsr     printsi
			dc.b   "Searching..",-1
.sloop:			lda     #'.'
			jsr     echo
			lda     aborted
			beq     .noabort
			jsr     printsi
			dc.b   $9b,"Aborted...",$9b,-1
			rts
.noabort:
			lda     lastsrch
			cmp     i_inuse
			bcc     .noend
			jmp     .atend
.noend:			inc     lastsrch
			lda     curmsg
			sta     .hold
			lda     lastsrch
			sta     curmsg
			jsr     getmsg
			lda     .hold
																														sta     curmsg

;       now, search all fields for a match
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			mea     keyword,d_ptr
			mea     m_title,s_ptr
			jsr     substr
			bne     .found

			mea     m_from,s_ptr
			jsr     substr
			bne     .found

			mea     m_to,s_ptr
			jsr     substr
			bne     .found

			mea     m_time,s_ptr
			jsr     substr
			bne     .found

			mea     m_date,s_ptr
			jsr     substr
			bne     .found

			mea     m_nodename,s_ptr
			jsr     substr
			bne     .found

			mea     m_nodecity,s_ptr
			jsr     substr
			bne     .found

			jmp     .sr_msg
.found:         	jsr     cr_lf
			lda     lastsrch
			sta     curmsg
			lda     #1
			sta     steprate
			lda     #0
			sta     topthread
			jsr     showmsg
			rts
.atend:
			jsr     printsi
		 	dc.b   $9b,$9b
		 	dc.b   "Keyword not found...",$9b,-1
			rts

.sr_msg:		lda     #1
			sta     .counter
.sr_loop:		lda     .counter
			jsr     calcptr
			lda     pr_ptr
			sta     s_ptr
			lda     pr_ptr+1
			sta     s_ptr+1
			jsr     substr
			bne     .found
			inc     .counter
			lda     .counter
			cmp     m_lines
			bcc     .sr_loop
			beq     .sr_loop
			jmp     .sloop

.counter:		dc.b    0
.hold:			dc.b    0



b_l:			jsr     check0

			mea     u_msgdel,s_ptr          ;LOCK message
			lda     curbase                 ;must have 'delete' abilities
			jsr     seclvl
			bpl     .cando
.nocando:
			jsr     printsi
			dc.b    "You can't lock messages!",-1
			lda     #$80
			rts
.cando:			ldy     curmsg
			lda     i_flag,y
			ora     #LOCKED
			sta     i_flag,y
			jsr     writeidx
			jsr     printsi
			dc.b    "Message is now LOCKED",$9b,-1
			rts



b_m:
			lda	#$00
			sta	.count
			lda	consol
			cmp	#$07
			beq	.b
.a:
			jsr	b_n
			ldy	.count
			iny
			sty	.count
			cpy	#255
			bne	.a
.b:			rts


.count:			dc.b	0





b_h:			jsr	check0
			jsr	printsi
			dc.b	"Printing",$9b,$9b,-1
			jsr	print_msg				;print out current message
			rts




b_s:
			mea	basename,s_ptr
			mea	parm_1,d_ptr
			jsr	tostr


			jsr	printsi
			dc.b	$9b,$9b,"Database File Name",$9b
			dc.b	" ==> "
			dc.b	-1
			mea	parm_1,pr_ptr
			jsr	echos
			jsr	printsi
			dc.b	$9b,$9b
			dc.b	"Press any key to Continue",-1
			jsr	get_key
			rts


;~~~~~~
;	Build an auto message
;
;~~~~~~


b_N:
.cando:
			jsr     printsi
			dc.b    "Making error code message",$9b,$9b
			dc.b    -1

			ldy     curmsg
			iny
			tya
			jsr	my.byte_sto

			lda     #0              ;break any thread
			sta     topthread


			mea	my.realdos,s_ptr
			mea	m_title,d_ptr
			jsr	move_str

.goon:			lda     #0              	;break any thread
			sta     topthread


.not_SYSOP:		mea     u_handle,s_ptr
			mea     m_from,d_ptr
			jsr     move_str

.a_SYSOP:
			mea     my.toall,s_ptr
			mea     m_to,d_ptr
			jsr     move_str

.go2:			lda     #0
			sta     linenum
			mea     m_title,s_ptr
			mea     ed_title,d_ptr
			jsr     move_str

			mea     m_to,s_ptr
			mea     ed_toname,d_ptr
			jsr     move_str

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Got the header built like I want


			jsr     my.get_message             ;get the message
			sta     m_lines



			lda     ed_width
			sta     m_width
			jsr     cr_lf
			lda     m_lines

 			jsr     printsi
			dc.b    $9b,"Saving message...",-1
			lda     #0
			sta     m_node
			sta     m_node+1
			sta     m_nodename
			sta     m_nodecity
			sta     m_edited
			sta     touser
			sta     touser+1
			sta     replymsg
			sta     replymsg+1
			jsr     clear_nodes             ;clear out node list
			lda     usernum
			sta     fromuser
			lda     usernum+1
			sta     fromuser+1
			jsr     postmsg                 ;write it to disk..
			jsr     printsi
			dc.b    "Done!",$9b,-1
			rts
.;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Prints a byte or a card to the screen
;
;
my.byte_sto:
			ldx	#$00
my.card_sto:
			sta	fr0
			stx	fr0+1
			lda	#$00
			sta	cix
			jsr	ifp
			jsr	fasc
			ldy	#$00
.aprbloop:
			lda	(inbuff),y
			php
			and	#$7f
			sta	.err_num,y
			iny
;			jsr	echo
			plp
			bpl	.aprbloop
			rts



.error:			dc.b	14,"Error #"
.err_num:		dc.b	"         ",$9b,$9b		; Gonna past the error number here

my.realdos:		dc.b	19,"RealDos Error Codes"
my.toall:		dc.b    12,"RealDos User"

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Here are the special routines I set up for this
;
;


my.get_message:		lda     u_vw
			sta     ed_width                ;set maximum length

			lda     #$01
			sta     rc             
			lda     ed_amsg        
			beq     .notmsg         ;   change vw to 77
			lda     #40
.notmsg:		jsr	header
			lda     #0                      ;start in NON graphics mode
			sta     graphics
			sta     did_wrap
			sta     pasteline
			lda     #$80
			sta     wordwrap

my.newline:		lda     linenum                 ;make sure linenum never
			bne     .ok                     ; goes below 1!
			inc     linenum

.ok:			jsr     cklength                ;calculate byte maximums
			bit     graphics
			bmi     .grmode

			jsr     printsi
			dc.b    $9b,'>',-1
			jmp     .nl1

.grmode:		jsr     printsi
			dc.b    $9b,20,-1               ;graphics prompt

.nl1:			lda     linenum
			cmp     #251                    ;only allow 250 lines
			bcs     .maxedout
			lda     bytes_left+1
			bne     .notmaxed
			lda     ed_width
			cmp     bytes_left              ;must have at least video
			bcc     .notmaxed               ; width left!

.maxedout:		lda     #1
			sta     msgline
			lda     #'/'
			sta     msgline+1
			bne     .gotaline
.notmaxed:		lda     #0
			sta     autoreturn
			lda     ed_width
			sec
			sbc     #2


			jsr     my.get_line                ;get a line



.gotaline:		lda     msgline
			cmp     #1                      ;lth = 1?
			bne     .not2                   ;nope
			lda     msgline+1
			cmp     #'/'                    ;slash?
			bne     .not1
			jsr     my.do_edit                 ;yes, do editing functions
			jmp     my.newline

.not1:			cmp     #'?'
			bne     .not2
			jsr     printsi
			dc.b    "Line #",-1
			lda     linenum
			jsr     pr_byte
			jsr     printsi
			dc.b    ", ",-1
			lda     bytes_left
			ldx     bytes_left+1
			jsr     pr_card
			jsr     printsi
			dc.b    " bytes left",-1
			jmp     my.newline
.not2:			jsr     movtobuff               ;move line to message buffer
			inc     linenum
			mea     msgline,s_ptr
			mea     pasteline,d_ptr
			jsr     move_str
			jmp     my.newline


;       the message editing commands
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

my.do_edit:		jsr     printsi
			dc.b    126,'[',-1
			lda     linenum			 ;'[xx lines] Edit: _'
			sec
			sbc     #1
			jsr     pr_byte
			jsr     printsi
			dc.b    "] Edit: ",-1
			jsr     my.auto_type		;my auto type routeen
			and     #$7f
			pha
			mea     my.editcmds,s_ptr
			pla
			jsr     execute
			rts

my.e_s:			lda     #0                      ;normal save
			sta     ed_savemode
			jmp     my.save_msg

my.e_u:			lda     #AS_IS                  ;unformatted save
			sta     ed_savemode
			jmp     my.save_msg

my.save_msg:		jsr     cklines
			jsr     printsi
			dc.b    "Save",$9b,-1
			dec     linenum			 ;linenum-1 = actual # of lines
			jsr     stripcrs
			pla
			pla
			pla
			pla
			lda     ed_width			;restore ed_width to 80
			cmp     #40			     ; after we're done...
			beq     .exit
			lda     #80
			sta     ed_width
.exit:			lda     linenum
			rts


my.e_r:			jsr     cklines
			jsr     ckmax
			jsr     printsi
			dc.b    126,126,126,126,126,126,126,126,126,126,-1
 			lda     linenum
			cmp     #101
			bcc     .not100
			lda     #126
			jsr     echo
.not100:		cmp     #11
			bcc     .enough
			lda     #126
			jsr     echo
.enough:		lda     #'>'
			jsr     echo
			lda     linenum
			sec
			sbc     #1
			jsr     calcptr
			lda     pr_ptr
			sta     s_ptr
			lda     pr_ptr+1
			sta     s_ptr+1
			jsr     echos			   ;print linenum-1
			mea     msgline,d_ptr
			jsr     move_str

			jsr     movtobuff               ;move line to message buffer
			inc     linenum
			rts

my.e_a:
			jsr     printsi
			dc.b    "Abort...You sure? ",-1
			jsr     y_or_n
			cmp     #'Y'
			bne     .xea
			pla
			pla
			pla
			pla
			lda     #$80
			sta     rc			     ; added 12/31/90
			lda     ed_width			;restore ed_width to 80
			cmp     #40			     ; after we're done...
			beq     .exit
			lda     #80
			sta     ed_width
.exit:			lda     #0
.xea:			rts

my.e_qmark:

			rts




my.e_b:
			jsr     printsi
			dc.b    "Begin Over...You sure? ",-1
			jsr     y_or_n
			cmp     #'Y'
			bne     .xea
			lda     #1
			sta     linenum
			jsr     my.e_sl			    ;clear the screen
.xea:			rts


my.e_c:			jsr     printsi
			dc.b    125,$9b,' ',-1
			lda     linenum
			sta     .temp
			lda     #1
			sta     linenum
			jsr     header2
			lda     .temp
			sta     linenum
			rts
.temp:			dc.b    0


my.e_sl:		lda     #125
			jsr     echo
			rts

my.e_d:			jsr     cklines
			jsr     printsi
			dc.b    "Delete ",-1
			lda     linenum
			sec
			sbc     #1
			sta     def_1
			sta     def_2
			jsr     get_nums			;get two numbers
			lda     nums
			beq     .delerr
			jsr     del_lines
			jsr     printsi
			dc.b    " - Done!",-1
			rts
.delerr:
			jsr     printsi
			dc.b    " - No lines deleted!",-1
			rts

my.e_e:			jsr     cklines
			jsr     ckmax
			jsr     printsi
			dc.b    "Edit ",-1
			lda     linenum
			sec
			sbc     #1
			sta     def_1
			sta     def_2
			jsr     get_nums			;get two numbers
			jsr     printsi
			dc.b    $9b,$9b,'-',-1
			lda     fromline
			jsr     calcptr
			jsr     movepaste               ;move line to paste buffer
			jsr     echos
			jsr     printsi
			dc.b    $9b,'+',-1
			lda     #$80
			sta     autoreturn
			lda     #0			      ;clear wrapped flag
			sta     did_wrap
			lda     ed_width
			sec
			sbc     #2
			jsr     my.get_line			;get new line
			lda     msgline
			beq     .nochg

;       delete old line from the buffer, then insert the new one
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			ldx     linenum
			dex			             ;if editing last line,
			cpx     fromline			; then DON'T contract/expand!
			beq     .noexp

			lda     fromline
			sta     toline			  ;delete it..
			jsr     contract
			lda     fromline
			ldx     msgline			 ;get length of new line
			inx			             ; ..plus 1
			jsr     expand			  ;expand the buffer
.noexp:			lda     fromline
			jsr     calcptr			 ;point to new buffer area
			lda     pr_ptr
			sta     d_ptr
			lda     pr_ptr+1
			sta     d_ptr+1
			mea     msgline,s_ptr
			jsr     move_str
			rts

.nochg:
			jsr     printsi
			dc.b    "Line unchanged...",-1
			rts

my.e_i: 		jsr     cklines
			jsr     ckmax
			jsr     printsi
			dc.b    "Insert Before ",-1
			lda     linenum
			sec
			sbc     #1
			sta     def_1
			sta     def_2
			jsr     get_nums			;get two numbers

			jsr     printsi
			dc.b    $9b,$9b,'+',-1
			lda     #$80
			sta     autoreturn
			lda     #0			      ;clear wrapped flag
			sta     did_wrap
			sta     pasteline
			lda     ed_width
			sec
			sbc     #2
			jsr     my.get_line			;get new line

			lda     fromline
			ldx     msgline			 ;get length of new line
			inx			             ; ..plus 1
			jsr     expand			  ;expand the buffer
			lda     fromline
			jsr     calcptr			 ;point to new buffer area
			lda     pr_ptr
			sta     d_ptr
			lda     pr_ptr+1
			sta     d_ptr+1
			mea     msgline,s_ptr
			jsr     move_str
			inc     linenum			 ;+1 to total lines
			rts




my.e_l:			jsr     cklines
			jsr     printsi
			dc.b    "List ",-1
			lda     #1
			sta     def_1
			lda     linenum
			sec
			sbc     #1
			sta     def_2
			jsr     get_nums			;get two numbers
			lda     #0			      ;no numbers
			jmp     listmsg

my.e_n:			jsr     cklines
			jsr     printsi
			dc.b    "List ",-1
			lda     #1
			sta     def_1
			lda     linenum
			sec
			sbc     #1
			sta     def_2
			jsr     get_nums			;get two numbers
			lda     #1			      ;do numbers
			jmp     listmsg


my.e_f:			jsr     cklines
			jsr     formheader
			lda     #1
			sta     fromline
			lda     linenum
			sec
			sbc     #1
			sta     toline
			lda     #0			      ;no numbers
			jmp     listmsg

my.e_bad:
			jsr     printsi
			dc.b    "Enter ? for help",-1
			rts



my.get_line:		sta     .maxlth			 ;save max length
			lda     #0
			sta     msgline			 ;zero length byte
			sta     pasteidx			;zero pasting index

;       if a word wrap occurred, move it in
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			lda     did_wrap			;did we wrap?
			beq     .no_wrap			;nope
			ldy     #0
.mvwrap:		lda     wrapword,y              ;get a byte
			beq     .no_wrap			;0 = end of word
			sta     msgline+1,y
			jsr     echo			    ;print the byte
			inc     msgline			 ;+1 to length byte
			iny
			jmp     .mvwrap

.no_wrap:		lda     #0
			sta     did_wrap
.gsloop:		jsr     my.auto_type			;my auto key
			cmp     #'\'			   ;can't allow this key!
			beq     .gsloop			 ;yes, ignore
			cmp     #$9b			    ;carriage return?
			bne     .not_cr			 ;nope
			jmp     .gscr

.not_cr:		pha			             ;save original key
			jsr	.doclrcr		;clear c/r counter
			bit     autoreturn              ;auto-return on '/'?
			bmi     .notone			 ;nope
			ldx     msgline			 ;in first position of line?
			bne     .notone			 ;nope
			and     #$7f			    ;strip inverse
			cmp     #'/'			    ;enter edit mode?
			beq     .go_edit			;yes
			cmp     #'?'			    ;request bytes-left?
			bne     .notone			 ;nope

.go_edit:		sta     msgline+1
			lda     #1
			sta     msgline
			pla
			rts

.notone:		pla			             ;get back original key
			cmp     #126			    ;backspace?
			beq     .bspace

			bit     graphics			;if in graphics mode,
			bpl     .not_grm
			sta     .key			    ; it's ok...
			jmp     .gsstore

.not_grm:		cmp     #30
			bne     .not30
			jmp     .bspace
.not30:			cmp     #24			     ;^X key?
			bne     .not24
			jmp     .wipeout
.not24:			cmp     #127			    ;TAB key?
			bne     .not127
			jmp     .tab
.not127:		cmp     #12			     ;^L (paste letter)?
			bne     .not12
			jmp     .paste			  ;yep!
.not12:			cmp     #23			     ;^W (paste word)?
			bne     .not23
			jmp     .pasteword
.not23:			cmp     #14			     ;^N (name)?
			bne     .not14
			jmp     .handle			 ;yep!
.not14:			cmp     #18			     ;^R (real name)?
			bne     .not18
			jmp     .name			   ;yep!
.not18:			and     #$7f			    ;strip inverse bit
			sta     .key			    ;hold it

;       now, see if it falls within requested mask
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.notauto:		cmp     #32			     ; < space?
			bcc     .gsloop			 ;yes, ignore
			cmp     #'z'+1			  ; > 'z'
			bcs     .gsloop			 ;yes, ignore

.gsstore:		jsr     .store_it               ;save the keypress
			jmp     .check_ww               ;see if need to word wrap

.bspace:		lda     msgline                 ;can't backspace
			cmp     #0			      ; if no length!
			bne     .bs1
			jmp     .gsloop
.bs1:			dec     msgline			 ;-1 to length byte
			lda     #126			    ;output a BSpace
			jsr     echo
			jmp     .gsloop

.wipeout:		lda     msgline			 ;get length
			bne     .wipe1
			jmp     .gsloop			 ;done
.wipe1:			dec     msgline			 ;-1 to length byte
			lda     #126			    ;output a BSpace
			jsr     echo
			jmp     .wipeout

.tab:			lda     #' '
			sta     .key
			jsr     .store_it
			jsr     .store_it
			jsr     .store_it
			jsr     .store_it
			jsr     .store_it
			jmp     .check_ww               ;see if need to word wrap

.gscr:
			inc	.numbcr
			lda	.numbcr
			cmp	#$03
			bcc	.pok
			lda	#'/'
			sta	msgline+1
			lda	#1
			sta	msgline
			rts
.pok:
	        	lda     msgline			 ;return with lth in accum.
                	rts

.paste:			ldy     pasteidx                ;get index into line
			cpy     pasteline               ;past end of paste line?
			bcs     .paignore               ;yes - ignore key
			iny
			sty     pasteidx
			lda     pasteline,y             ;get next pasteable key
			sta     .key
			jmp     .gsstore
.paignore:		jmp     .gsloop                 ;ignore the key


.pasteword:		ldy     pasteidx			;get index into line
			cpy     pasteline               ;past end of paste line?
			bcs     .pwignore               ;yes - ignore key
			iny
			sty     pasteidx
			lda     pasteline,y             ;get next pasteable key
			sta     .key
			jsr     .store_it
			lda     .key
			cmp     #' '			    ;space?
			bne     .pasteword              ;keep going
.pwignore:		jmp     .check_ww               ;end by checking wrap


.handle:		ldx     #1
			jmp     .hannx
.halp:			lda     u_handle,x
			sta     .key
			jsr     .store_it
			inx
.hannx:			cpx     u_handle
			bcc     .halp
			beq     .halp
			jmp     .check_ww

.name:			ldx     #1
			jmp     .namnx
.nalp:			lda     u_name,x
			sta     .key
			jsr     .store_it
			inx
.namnx:			cpx     u_name
			bcc     .nalp
			beq     .nalp
			jmp     .check_ww

.store_it:		lda     msgline			 ;get lth byte
			cmp     .maxlth
			beq     .xsi			    ;ignore it..at
			bcs     .xsi			    ; max length now
			tay
			iny
			lda     .key
			sta     msgline,y               ;save key into string
			inc     msgline			 ;increase length
			lda     .key
			jsr     echo
.xsi:			rts

.check_ww:		bit     graphics                ;don't word wrap if
			bmi     .xck_ww			 ; in graphics mode
			bit     autoreturn              ;auto-return on '/'?
			bmi     .xck_ww			 ;nope
			lda     wordwrap
			beq     .xck_ww
			lda     msgline
			cmp     .maxlth			 ;at max length?
			bcc     .xck_ww			 ;nope

;       first, find last space in the line
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			tay			             ;length to Y
.cwwfs:			lda     msgline,y               ;get char
			cmp     #' '			    ;a space?
			beq     .gotspace               ;yep
			dey
			bne     .cwwfs
.xck_ww:		jmp     .gsloop			 ;no words to wrap!



;       now, erase and move the last word into WRAPWORD
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.gotspace:		cpy     msgline			 ;space in last character?
			bne     .no_lc			  ;nope
			dec     msgline			 ;yes,remove end space from
			jmp     .gscr			   ; and just exit..
.no_lc:			sty     .wwstart			;save index
			iny			             ;go past space

			lda     #0			      ;clear out wrapword
			tax
.clword:		sta     wrapword,x
			inx
			cpx     #82
			bne     .clword

			ldx     #0			      ;index into wrapword
.gslp:			lda     msgline,y               ;move next char over
			sta     wrapword,x
			lda     #126
			jsr     echo			    ;print a backspace
			iny
			inx
			dec     msgline			 ;-1 to length
			lda     msgline
			cmp     .wwstart			;are we done?
			beq     .doneww
			bcs     .gslp			   ;nope
.doneww:		lda     #1
			sta     did_wrap			;signal that we wrapped
			dec     msgline			 ;remove space at end
			jmp     .gscr

.doclrcr:
			pha
			lda	#$00
			sta	.numbcr
			pla
			rts


.numbcr:		dc.b	0
.maxlth:		dc.b    1
.key:			dc.b    1
.wwstart:		dc.b    1



my.editcmds:		dc.b    'S'
			dc.w    my.e_s
			dc.b    'U'
			dc.w    my.e_u
			dc.b    'A'
			dc.w    my.e_a
			dc.b    'B'
			dc.w    my.e_b
			dc.b    'C'
			dc.w    my.e_c
			dc.b    'D'
			dc.w    my.e_d
			dc.b    'E'
			dc.w    my.e_e
			dc.b    'F'
			dc.w    my.e_f
			dc.b    'I'
			dc.w    my.e_i
			dc.b    'L'
			dc.w    my.e_l
			dc.b    'N'
			dc.w    my.e_n
			dc.b    'R'
			dc.w    my.e_r
			dc.b    '/'
			dc.w    my.e_sl
			dc.b    0
			dc.w    my.e_bad


;------------------------------
;   GET KEY  ROUTEEN           
;this routeen get's one key and
;return it to the accumlator   
;------------------------------
;
my.get_key:		stx	$b6
			sty	$b7
			jsr	.a
			ldx	$b6
			ldy	$b7
			rts
.a:			lda	$e420+5
			pha
			lda	$e420+4
			pha
			rts



my.auto_type:
			sty	.y
			stx	.x

			ldy	.cur_pos
			lda	my.auto,y
			iny
			sty	.cur_pos
			ldx	.x
			cpy	#my.auto.end-my.auto+1			; .leng+1
			bcc	.done
			ldy	#$00
			sty	.cur_pos
.done:			ldy	.y
			rts

.leng:			dc.b	my.auto.end-my.auto+1			;calc the length of string
.y:			dc.b	0
.x:			dc.b	0
.cur_pos:		dc.b	0



my.auto:
			dc.b	$9b		
			dc.b	"Error Messages can be edited"
			dc.b	$9b,$9b
			dc.b	"/s"
			dc.b	$9b
my.auto.end:		ds.b	0


my.mnum:		dc.b	0

;
;end of special routines
;













b_u:			jsr     check0
.cando:			ldy     curmsg
			lda     i_flag,y
			and     #UNLOCKED
			sta     i_flag,y
			jsr     writeidx
			jsr     printsi
			dc.b    "Message is now UNLOCKED",$9b,-1
			rts



b_minus:		jsr     check0
			lda     curmsg
			cmp     #2
			bcc     .error
			dec     curmsg
			jsr     showmsg
			rts
.error:			jmp     b_cr




b_plus:			jsr     check0
			lda     curmsg                  ;make sure we're never
			cmp     i_inuse                 ; OVER the total in use
			bcc     .inrange
.error:			jmp     b_cr
.inrange:		inc     curmsg
			jsr     showmsg
			rts





b_z:
.cando:			jsr     printsi
			dc.b   "New Name: ",-1
			jsr     get_str
			dc.w   i_name
			dc.b   20,0,0
			jsr     cr_lf
			jsr     writeidx
			jsr     printsi
			dc.b    "Name changed!",$9b,-1
			rts

b_qmark:
			jsr	Printsi
			dc.b	$7d
			dc.b	<+128>,"<Return>"
			dc.b	" reads the next message",$9b

			dc.b	<+128>,"[A]"
			dc.b	"Read the current message again",$9b
			dc.b	<+128>,"[B]"
			dc.b	"Set read direction to backward",$9b
			dc.b	<+128>,"[-]"
			dc.b	"Set read direction to backward",$9b
;			dc.b	<+128>,"[D]"
;			dc.b	"Delete the current message",$9b
			dc.b	<+128>,"[E]"
			dc.b	"Edit the current message",$9b
			dc.b	<+128>,"[F]"
			dc.b	"Set read direction to forward",$9b


			dc.b	<+128>,"[+]"
			dc.b	"Read Next Message",$9b
;			dc.b	<+128>,"[G]"
;			dc.b	"Goto a certain message number",$9b
			dc.b	<+128>,"[J]"
			dc.b	"Goto a certain message number",$9b
			dc.b	<+128>,"[L]"
			dc.b	"Lock the current message",$9b
			dc.b	<+128>,"[Consol+M]"
			dc.b	"Make Basic Database",$9b
			dc.b	<+128>,"[N]"
			dc.b	"Position at first new message",$9b
;			dc.b	<+128>,"[P]"
;			dc.b	"Post a new message",$9b
;			dc.b	<+128>,"[Q]"
;			dc.b	"Quit reading current thread",$9b
;			dc.b	<+128>,"[R]"
;			dc.b	"Reply to the current message",$9b
			dc.b	<+128>,"[S]"
			dc.b	" Show Current Database File Name",$9b
;			dc.b	<+128>,"[T]"
;			dc.b	"See thread of current message",$9b
			dc.b	<+128>,"[U]"
			dc.b	"Unlock the current message",$9b
			dc.b	<+128>,"[V]"
			dc.b	"Toggle Video Width",$9b
			dc.b	<+128>,"[X]"
			dc.b	"Exit back to the main menu",$9b
			dc.b	<+128>,"[K]"
			dc.b	"Search for a keyword",$9b
			dc.b	<+128>,"[C]"
			dc.b	"Continue keyword search",$9b
			dc.b	<+128>,"[Z]"
			dc.b	"change message base name",$9b
			dc.b	$9b,$9b
			dc.b	"Press any Key to Continue",$9b
			dc.b	-1
			jsr	get_key
			rts








postmsg:
;			jsr	message_hi		; Grab the hi count from master


			lda     i_inuse                 ;is this base already full?
			cmp     i_maxmsgs
			bcc     .notfull                ;nope

			ldy     #1                      ;yes, find first msg to delete
.pdel:			lda     i_flag,y                ;this msg locked?
			and     #LOCKED
			beq     .pd1                    ;no, we found one
			iny
			cpy     i_inuse
			bcc     .pdel
			ldy     #1                      ;all are locked, so del #1
.pd1:			tya                             ;# to delete in Y reg..
			jsr     delmsg

.notfull:		jsr     openslot                ;find an open slot
			sta     .slot                   ; and save it

			inc     i_inuse                 ;in_use := in_use + 1
			ldy     i_inuse

			lda     s_himsg
			sta     i_msgnlo,y              ;msg_number [in_use] :=
			sta     .newhi
			lda     s_himsg+1               ;    hi_msg_ctr
			sta     i_msgnhi,y
			sta     .newhi+1
			inc     s_himsg                 ;  hi_msg_ctr := +1
			bne     .ov1
			inc     s_himsg+1
.ov1:
			lda     .slot
			sta     i_msgptr,y
			lda     fromuser
			sta     i_fromlo,y
			lda     fromuser+1
			sta     i_fromhi,y
			lda     touser
			sta     i_tolo,y
			lda     touser+1
			sta     i_tohi,y
			lda     replymsg
			sta     i_replylo,y
			lda     replymsg+1
			sta     i_replyhi,y
			lda     ed_savemode
			sta     i_flag,y                ;clear LOCKED and RECVD flags

.zz1:			jsr     datestamp
			mea     cur_date,s_ptr
			mea     m_date,d_ptr
			jsr     move_str
			mea     cur_time,s_ptr
			mea     m_time,d_ptr
			jsr     move_str


			inc     i_posted                ;+1 to total posts
			bne     .nv2
			inc     i_posted+1
.nv2:
			lda     i_inuse
			jsr     writemsg                ;write it to disk...
			bmi     .noidx
			jsr     writeidx                ;re-write the index
			ldx     curbase
			dex
			lda     .newhi                  ;update the hi message
			sta     hi_msglo,x              ; buckets
			lda     .newhi+1
			sta     hi_msghi,x
.noidx:
;			jsr	message_hi		; ok time to go update the mux

			rts

.slot:			dc.b    0
.newhi:			dc.b    0,0



;       find an open slot in the index area
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


openslot:		ldx     #0
.outloop:		inx
			cpx     i_maxmsgs
			bcc     .goon
			beq     .goon
.notfound:		lda     #$f0                    ;should NEVER happen!
			rts                             ; (will cause POINT error)
.goon:			ldy     #0
.inloop:		iny
			cpy     i_inuse
			bcc     .goon2
			beq     .goon2
			jmp     .match                  ;found one!
.goon2:			lda     i_msgptr,y
			sta     .temp
			cpx     .temp                   ;found a match?
			bne     .inloop                 ;nope..
			beq     .outloop                ;yes - try next number
.match:			txa
			rts
.temp:			dc.b    0




;       read the message header in curmsg
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

getmsg:
			lda     curmsg
			jsr     readmsg                 ;read the message
			php
			plp
			rts                             ;error happenned..just exit



;       show the message header in Accum.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

showheader:		lda     #0
			sta     aborted
			sta     nextmsg
			lda     curmsg
			sta     .mnum

			lda	#$7d
			jsr	echo
			jsr	tammy-3			; display file info


			jsr     cr_lf
			lda     #8
			sta     line_count              ;header takes 7 lines
			ldy     .mnum
			lda     i_flag,y                ;this msg locked?
			and     #LOCKED
			beq     .nolock                 ;nope
			inc     line_count
			jsr     printsi
			dc.b    "*Locked*",$9b,-1
			jsr     .ck_abort
.nolock: 
			jsr     printsi
;			dc.b    "Message: ",-1
			dc.b    "Error: #",-1
			lda     .mnum
			jsr     pr_byte
;			jsr     printsi
;			dc.b    " (#",-1
;			ldy     .mnum
;			lda     i_msgnhi,y
;			tax
;			lda     i_msgnlo,y
;			jsr     pr_card
;			lda     #')'
;			jsr     echo

;			ldy     .mnum
;			lda     i_replylo,y
;			ora     i_replyhi,y
;			beq     .norep
;			jsr     printsi
;			dc.b    " - Reply to #",-1
;			ldy     .mnum
;			lda     i_replyhi,y
;			tax
;			lda     i_replylo,y
;			jsr     findparent
;			jsr     pr_card
.norep:			jsr     cr_lf
			jsr     .ck_abort
			jsr     printsi
			dc.b    "Title: ",-1
			mea     m_title,pr_ptr
			jsr     echos

			jsr     .ck_abort
;			jsr     printsi
;			dc.b    $9b
;			dc.b    " Author: ",-1
;			mea     m_from,pr_ptr
;			jsr     echos

;			jsr     .ck_abort
;			jsr     printsi
;			dc.b    $9b
;			dc.b    "     To: ",-1
;			mea     m_to,pr_ptr
;			jsr     echos
;			ldy     .mnum
;			lda     i_flag,y                ;been received yet?
;			and     #RECEIVED
;			beq     .norecv                 ;nope
;			jsr     printsi
;			dc.b    " (Recvd)",-1

.norecv:		jsr     .ck_abort
;			jsr     printsi
;			dc.b    $9b
;			dc.b    " Posted: ",-1
;			mea     m_date,pr_ptr
;			jsr     echos
;			jsr     printsi
;			dc.b    " at ",-1
;			mea     m_time,pr_ptr
;			jsr     echos
;			jsr     .ck_abort
;			jsr     printsi
;			dc.b    $9b
;			dc.b    "Replies: ",-1
;			lda     .mnum
;			jsr     numreplies              ;returns in A reg
;			jsr     pr_byte

			lda     m_edited                ;been edited?
			beq     .noedit                 ;nope
			jsr     printsi
			dc.b    " [> Edited <]",-1
.noedit:

			jsr     cr_lf
			jsr     .ck_abort
			lda     m_node                  ;from another node?
			ora     m_node+1
			beq     .no_node                ;nope
			inc     line_count              ;header now takes 2
			inc     line_count              ;  more lines..
;			jsr     printsi
;			dc.b    " Origin: ",-1
;			mea     m_nodename,pr_ptr
;			jsr     echos
;			jsr     cr_lf
;			jsr     printsi
;			dc.b    "         ",-1
;			mea     m_nodecity,pr_ptr
;			jsr     echos
;			jsr     cr_lf

;       see if we need to mark this as 'received'
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.no_node:		ldy     .mnum
			lda     i_flag,y                ;already received?
			and     #RECEIVED
			bne     .xit                    ;yes, don't bother
			lda     usernum
			ora     usernum+1               ;don't bother if visitor
			beq     .xit

			mea     u_handle,s_ptr
			mea     m_to,d_ptr
			jsr     cmp_str                 ;names match?
			bne     .xit                    ;nope
			mea     u_seclvl,s_ptr
			lda     #32      ;if they have sysop, its ok
			jsr     seclvl
			bpl     .xit
			ldy     .mnum
			lda     i_flag,y
			ora     #RECEIVED
			sta     i_flag,y                ;mark it as received
			jsr     writeidx
.xit:			rts
.mnum:			dc.b    0
.ck_abort:		lda     aborted
			bne     .quit
			lda     nextmsg
			bne     .quit
			rts
.quit:			pla
			pla
			jsr     cr_lf
			rts



;       read/display the message currently in CURMSG
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

showmsg:
			jsr     getmsg                  ;read it from disk
			lda     u_flags
			and     #f_clear                ;need to clear?
			beq     .nocl                   ;nope
			lda     u_flags
			and     #f_nonstop              ;don't clear if nonstop
			bne     .nocl
			lda     #125
			jsr     echo
.nocl:			jsr     showheader              ;show the header
			lda     verbose                 ;showing titles only?
			bne     .show_msg               ;nope
			lda     nextmsg                 ;hit the ^N?
			beq     .bye_bye                ;nope
			lda     #$9b                    ;force next read
			jsr     stuff
.bye_bye:		rts                             ;yes - we're done

.show_msg:
			jsr     disp_msg                ;display the message
			ldy     curmsg                  ;if msgnum > w_lastread then
			lda     i_msgnhi,y              ;   w_lastread := msgnum
			cmp     w_lastread+1
			beq     .cklo
			bcc     .noupdate
			bcs     .update
.cklo:			lda     i_msgnlo,y
			cmp     w_lastread
			bcc     .noupdate
.update:		lda     i_msgnlo,y
			sta     w_lastread
			lda     i_msgnhi,y
			sta     w_lastread+1
.noupdate:		lda     aborted                 ;did they ^C?
			bne     .abt                    ; yep...bag it
			lda     nextmsg                 ;same for ^N
			bne     .next
			jmp     .exit
.next:			lda     #$9b                    ;force next read
			jsr     stuff
			jmp     .exit
.abt:			lda     nonstop                 ;in a non-stop quickscan?
			beq     .exit                   ;nope
			jsr     printsi
			dc.b   $9b,$9b
			dc.b   "Abort quickscan? ",-1
			jsr     y_or_n
			cmp     #'Y'
			bne     .exit
			jsr     printsi
			dc.b   $9b,"Quickscan aborted.",$9b,-1
			lda     #0
			sta     quickscan
			sta     nonstop
.exit:
			jsr     cr_lf
			jsr     up_lines
			rts


disp_msg:		lda     #1
			sta     counter
			lda     #0
			sta     widflag
			lda     m_width
			sta     ed_width
			jsr     cr_lf
			ldy     curmsg
			lda     i_flag,y
			and     #AS_IS
			bne     .asis
			lda     u_vw
			cmp     #40
			beq     .vw40
.vw80:			lda     m_width
			cmp     #80
			beq     .asis
			jmp     show40to80
.vw40:			lda     m_width
			cmp     #40
			beq     .asis
			jmp     show80to40
.asis:			jmp     showasis


;       Show the message exactly as it is stored..
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

showasis:		lda     aborted
			bne     .abt
			lda     nextmsg
			bne     .abt
			lda     counter
			jsr     calcptr
			jsr     echos
			jsr     cr_lf
			jsr     up_lines
			inc     counter
			lda     counter
			cmp     m_lines
			bcc     showasis
			beq     showasis
.abt:			rts


;       Show a 40 column message to an 80-column user
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

show40to80:		lda     aborted
			bne     .abt
			lda     nextmsg
			bne     .abt
			lda     counter
			jsr     calcptr
			ldy     #0
			lda     (pr_ptr),y
			beq     .forcecr
			iny
			lda     (pr_ptr),y
			cmp     #' '
			beq     .asis
			lda     widflag
			beq     .nosp
			lda     #' '
			jsr     echo
			jsr     echos
			jsr     cr_lf
			jsr     up_lines
			lda     #0
			sta     widflag
			jmp     .nxtline
.nosp:			jsr     echos
			lda     #1
			sta     widflag
.nxtline:		inc     counter
			lda     counter
			cmp     m_lines
			bcc     show40to80
			beq     show40to80
.abt:			rts

.forcecr:		jsr     cr_lf
			jsr     up_lines
			lda     widflag
			beq     .ok
			jsr     cr_lf
			jsr     up_lines
.ok:			lda     #0
			sta     widflag
			jmp     .nxtline

.asis:			lda     widflag
			beq     .ok2
			jsr     cr_lf
			jsr     up_lines
.ok2:			jsr     echos
			lda     #1
			sta     widflag
			jmp     .nxtline


up_width:		lda     widflag
			beq     .up1
			lda     #0
			sta     widflag
			jsr     cr_lf
			rts
.up1:			inc     widflag
			rts



;       Show an 80 column message to a 40-column user
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

show80to40:		lda     aborted
			bne     .abt
			lda     nextmsg
			bne     .abt
			lda     counter
			jsr     calcptr
			jsr     out40
			inc     counter
			lda     counter
			cmp     m_lines
			bcc     show80to40
			beq     show80to40
.abt:			rts


out40:			lda     pr_ptr
			sta     s_ptr
			lda     pr_ptr+1
			sta     s_ptr+1
			mea     msgline,d_ptr
			jsr     move_str
			lda     msgline                 ;if null line, force C/R
			cmp     #37
			bcs     .do_format
.go_out:		jsr     print_40
			rts

.do_format:		ldy     #1
			ldx     #1
.loop:			inx
			iny
			cpy     msgline
			bcc     .notEOF
			beq     .notEOF
			jmp     .go_out
.notEOF:		cpx     #38
			bcc     .loop

;       Back up Y register to a space, install EOL
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.unwrap:		lda     msgline,y
			cmp     #'\'                   ;means a super long word!
			bne     .notsl
			jmp     .go_out
.notsl:			cmp     #' '
			beq     .gotit
			dey
			cpy     #2
			bcs     .unwrap
			jmp     .go_out
.gotit:			ldx     #1
			lda     #'\'
			sta     msgline,y
			lda     #1
			sta     .did_wrap
			jmp     .loop

.did_wrap:		dc.b    0

counter:		dc.b    0
widflag:		dc.b    0



;       output a 80 column line while checking for variable C/R's
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

print_40:		lda     #0
			sta     .idx
.loop:			inc     .idx
			ldy     .idx
			cpy     msgline                 ;done with line?
			bcc     .notdone
			beq     .notdone
.done:			jsr     cr_lf
			jsr     up_lines
			rts
.notdone:		lda     msgline,y               ;next byte of line
			cmp     #'\'
			bne     .not_cr
.var_cr:		jsr     cr_lf
			jsr     up_lines
			jmp     .loop
.not_cr:		jsr     echo
			jmp     .loop

.idx:			dc.b    0


;       routines to keep track of the "lines per page"
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

zero_lines:		lda     #0
			sta     line_count
			rts


up_lines:		lda     nonstop                 ;don't page-break if non-stop
			bne     .exit                   ; viewing..
			lda     u_pagesize              ;zero means NO page breaking
			beq     .exit
			sec
			sbc     #1
			sta     .temp
			inc     line_count              ;+1 to lines displayed
			lda     line_count
			cmp     .temp
			bcc     .exit                   ;no need to break!
			jsr     printsi
			dc.b   <+128>,"[-More-] "
			dc.b	-1
			lda     #0
			sta     line_count
			jsr     get_key                 ;get any key
			jsr     printsi
			dc.b   126,' ',126             ;[
			dc.b   126,' ',126             ;-
			dc.b   126,' ',126             ;M
			dc.b   126,' ',126             ;o
			dc.b   126,' ',126             ;r
			dc.b   126,' ',126             ;e
			dc.b   126,' ',126             ;-
			dc.b   126,' ',126             ;]
			dc.b   126,' ',126,-1          ;' '
.exit:			rts

.temp:			dc.b    0




;       open the message base in A reg
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

open_base:
			mea     basename,fn_ptr         ;open the data file
			ldx     #msgbase
			jsr     open_updt               ;open in update mode
			rts

close_base:		ldx     #msgbase
			jmp     close




;             Read index record.
;             ~~~~~~~~~~~~~~~~~~

readidx:		ldx     #msgbase
			jsr     point0                  ;to first of file
			lda     #ccgetch
			sta     iccom,x
			lda     #i_buff & $ff
			sta     icbal,x
			lda     #i_buff / 256
			sta     icbah,x
			lda     #0
			sta     icbll,x
			lda     #INDEX_CHUNKS           ;always 2560 bytes
			sta     icblh,x
			jsr     ciov
			bpl     .ok
			jmp     msgerror
.ok:			rts



;                Write index file.
;                ~~~~~~~~~~~~~~~~~

writeidx:		ldx     #msgbase
			jsr     point0
			lda     #ccputch
			sta     iccom,x
			lda     #i_buff & $ff
			sta     icbal,x
			lda     #i_buff / 256
			sta     icbah,x
			lda     #0
			sta     icbll,x
			lda     #INDEX_CHUNKS           ;always 2560 bytes
			sta     icblh,x
			jsr     ciov
			bpl     .ok
			jmp     msgerror
.ok:			rts



;            Read message number in Accum.
;            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

readmsg:		tay
			lda     i_msgptr,y              ;get the slot number
			jsr     pointmsg                ; and point to the record
			bpl     .ptok
			rts
.ptok:			ldx     #msgbase
			lda     #ccgetch
			sta     iccom,x
			lda     #m_buff & $ff
			sta     icbal,x
			lda     #m_buff / 256
			sta     icbah,x
			lda     #0
			sta     icbll,x
			lda     i_chunks
			sta     icblh,x
			jsr     ciov
			bpl     .eof
			cpy     #136                    ;EOF is OK
			beq     .eof
			jmp     msgerror
.eof:			rts



;       Write the message number in accumulator.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

writemsg:		tay
			lda     i_msgptr,y              ;get the slot number
			jsr     pointmsg                ; and point to the record
			bpl     .ptok
			rts
.ptok:			ldx     #msgbase
			lda     #ccputch
			sta     iccom,x
			lda     #m_buff & $ff
			sta     icbal,x
			lda     #m_buff / 256
			sta     icbah,x
			lda     #0
			sta     icbll,x
			lda     i_chunks
			sta     icblh,x
			jsr     ciov
			bpl     .ok
			jmp     msgerror
.ok:			rts


pointerr:
			jsr     printsi
			dc.b   $9b," Critical base error!",$9b,-1
			jmp     exitmsg


;       point to the message in A
;       ~~~~~~~~~~~~~~~~~~~~~~~~~

pointmsg:		tay                             ;slot number --> Y
			cmp     #0                      ;0 message = bad!
			beq     .error
			cmp     i_maxmsgs               ;invalid message number?
			bcc     .inrange
			beq     .inrange
.error:			jmp     pointerr
.inrange:		lda     #0
			sta     .point
			sta     .point+2
			lda     #INDEX_CHUNKS           ;always starts at byte 2560
			sta     .point+1
			jmp     .noc1                   ;check for zero first
.loop:			lda     .point+1
			clc
			adc     i_chunks                ;# of 256-byte chunks per msg
			sta     .point+1
			lda     .point+2
			adc     #0
			sta     .point+2
.noc1:			dey
			bne     .loop


;       now, do the point command
;       ~~~~~~~~~~~~~~~~~~~~~~~~~

.pt:			ldx     #msgbase
			lda     #37
			sta     iccom,x
			lda     .point
			sta     $34c,x
			lda     .point+1
			sta     $34d,x
			lda     .point+2
			sta     $34e,x
			jsr     ciov
			bpl     .okp
			jmp     msgerror
.okp:			rts

.point:			dc.b    0,0,0


point0:			lda     #37                     ;point to 0th byte
			sta     iccom,x
			lda     #0
			sta     $34c,x
			sta     $34d,x
			sta     $34e,x
			jsr     ciov
			bpl     .ok
			jmp     msgerror
.ok:			rts



ckmrange:		cmp     #0
			beq     .bad
			cmp     i_inuse                 ;make sure good msg number
			beq     .ok
			bcc     .ok
.bad:			sec
			rts
.ok:			clc
			rts




basename:		dc.b    "D1:>real>error.dat",$9b
			ds.b	80


asknum:			dc.b    0



;       delete the message number in A from the index
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

delmsg:			cmp     #0
			bne     .not0
			rts
.not0:			cmp     i_inuse         ;deleting last message?
			bne     .notlast        ;nope
			dec     i_inuse         ;yes...just move up pointer
			rts                     ; (last msg can't have any replies!)
.notlast:		cmp     i_inuse
			bcc     .godel
			rts
.godel:			tay
			lda     i_msgnlo,y              ;save msg number for later
			sta     .msgnum
			lda     i_msgnhi,y
			sta     .msgnum+1
.moveup:		lda     i_msgptr+1,y          ;now, move everything up one slot
			sta     i_msgptr,y
			lda     i_flag+1,y
			sta     i_flag,y
			lda     i_msgnlo+1,y
			sta     i_msgnlo,y
			lda     i_msgnhi+1,y
			sta     i_msgnhi,y
			lda     i_tolo+1,y
			sta     i_tolo,y
			lda     i_tohi+1,y
			sta     i_tohi,y
			lda     i_fromlo+1,y
			sta     i_fromlo,y
			lda     i_fromhi+1,y
			sta     i_fromhi,y
			lda     i_replylo+1,y
			sta     i_replylo,y
			lda     i_replyhi+1,y
			sta     i_replyhi,y
			iny                             ;done?
			cpy     i_inuse
			bcc     .moveup                 ;nope..

;       now, remove any 'replies' to this message
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			ldy     #1
.loop:			lda     i_replylo,y
			cmp     .msgnum
			bne     .next
			lda     i_replyhi,y
			cmp     .msgnum+1
			bne     .next
.match:			lda     #0
			sta     i_replylo,y
			sta     i_replyhi,y
.next:			iny
			cpy     i_inuse
			bcc     .loop
			dec     i_inuse                 ;in_use = in_use - 1

;       Move the "current msg" pointer back one..       (added 6-8-88)
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			lda     curmsg
			cmp     #2
			bcc     .nodec
			dec     curmsg
.nodec:			rts

.msgnum:		dc.b    0,0



;       calc the number of replies to the msg number in A
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

numreplies:		tay                             ;y = index
			lda     #0
			sta     .replies                ;number found
			lda     i_msgnlo,y              ;the message number we
			sta     .target                 ; are matching on
			lda     i_msgnhi,y
			sta     .target+1
.loop:			lda     i_replylo,y
			cmp     .target
			bne     .next
			lda     i_replyhi,y
			cmp     .target+1
			bne     .next
.match:			inc     .replies
.next:			iny
			cpy     i_inuse
			beq     .loop
			bcc     .loop
			lda     .replies                ;number of replies --> A
			rts

.replies:		dc.b    0
.target:		dc.b    0
.mnum:			dc.b    0
.idx:			dc.b    0


;       find the first NEW message
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~

firnewmsg:		ldy     #1                      ;y = index
.loop:			lda     u_lastread+1
			cmp     i_msgnhi,y
			beq     .cklo
			bcc     .gotone
			jmp     .next
.cklo:			lda     u_lastread
			cmp     i_msgnlo,y
			bcc     .gotone
			jmp     .next
.gotone:		tya
			rts
.next:			iny
			cpy     i_inuse
			beq     .loop
			bcc     .loop
			lda     #0
			rts


;       find the parent message of the msg in A,X
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

findparent:		sta     .temp
			stx     .temp+1
			ldy     #0
			beq     .next
.fp1:			lda     i_msgnlo,y
			cmp     .temp
			bne     .next
			lda     i_msgnhi,y
			cmp     .temp+1
			bne     .next
.match:			tya
			ldx     #0
			rts
.next:			iny
			cpy     i_inuse
			beq     .fp1
			bcc     .fp1
			lda     #0
			tax
			rts
.temp:			dc.b    0,0





;       find the FIRST reply to the msg number in A
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

findreply:		tay                             ;y = index
			lda     i_msgnlo,y              ;the message number we
			sta     .target                 ; are matching on
			lda     i_msgnhi,y
			sta     .target+1
.loop:			lda     i_replylo,y
			cmp     .target
			bne     .next
			lda     i_replyhi,y
			cmp     .target+1
			bne     .next
.match:			tya
			rts
.next:			iny
			cpy     i_inuse
			beq     .loop
			bcc     .loop
			lda     #0
			rts

.target:		dc.b    0
.mnum:			dc.b    0


;       get a message number from the user - returns carry SET if bad #
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

getmsgnum:		jsr     get_str
			dc.w   instr
			dc.b   3,1,1
			jsr     cr_lf
			lda     instr
			bne     .noex
			sec
			rts
.noex:			mea     instr,s_ptr             ;string --> integer
			jsr     valc
			sta     .temp
			jsr     ckmrange
			bcs     .badnum
			lda     .temp                   ;return it in A reg
			clc
			rts
.badnum:
			jsr     printsi
			dc.b    $9b,"Invalid message number!",$9b,-1
			sec
			rts
.temp:			dc.b    0




check0:
;			jsr	message_hi	; check the hi message counter
			lda     i_inuse
			bne     .ok1
			jsr     printsi
			dc.b   "There are no messages in this base!",$9b,-1
			pla
			pla
			rts
.ok1:			lda     curmsg
			bne     .exit
			inc     curmsg
.exit:			rts



get_message:		lda     u_vw
			sta     ed_width                ;set maximum length
			jmp     msgedit




;       IN_THREAD:  checks to see if the message number in
;                   A,X is currently in the thread stack.
;         returns:  carry clear if in the thread.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

in_thread:		sta     .num
			stx     .num+1
			ora     .num+1
			beq     .not_in
			ldy     #0
			beq     .next
.loop:			lda     stacklo,y
			cmp     .num
			bne     .next
			lda     stackhi,y
			cmp     .num+1
			beq     .foundit                ;found one!
.next:			iny
			cpy     threadidx               ;done?
			bcc     .loop
			beq     .loop
.not_in:		lda     .num
			ldx     .num+1
			sec
			rts
.foundit:		lda     .num
			ldx     .num+1
			clc
			rts
.num:			dc.b    0,0


;       CLEAR THREAD - clear out the thread data
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

clear_thread:		ldy     #0
			sty     threadidx
			tya
.lp:			sta     stacklo,y
			sta     stackhi,y
			iny
			cpy     #251
			bne     .lp
			rts

;       ADD_THREAD: add the message number in A to the stack
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

add_thread:		tay
			lda     i_msgnlo,y
			sta     .temp
			lda     i_msgnhi,y
			sta     .temp+1
			inc     threadidx
			ldy     threadidx
			lda     .temp
			sta     stacklo,y
			lda     .temp+1
			sta     stackhi,y
			rts
.temp:			dc.b    0,0



;       nextchild:  returns the message number of the next "child"
;                   message in A.  Returns 0 if no child found.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

nextchild:		lda     curmsg                  ;start at current message
			sta     .idx
			jmp     .next
.loop:			ldy     .idx
			lda     i_replyhi,y             ;hi byte
			tax
			lda     i_replylo,y             ;lo byte
			jsr     in_thread               ;is it in the thread?
			bcc     .found                  ;yes!
.next:			inc     .idx
			lda     .idx
			cmp     i_inuse
			bcc     .loop
			beq     .loop
.notfound:		lda     #0
			rts
.found:			lda     .idx
			rts
.idx:			dc.b    0




;       findtop -  find the ultimate parent of the message in A
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

findtop:		sta     .msg
.lp:			ldy     .msg
			lda     i_replylo,y
			ora     i_replyhi,y
			beq     .done
			lda     i_replyhi,y
			tax
			lda     i_replylo,y
			jsr     findparent
			sta     .msg
			cmp     #0
			bne     .lp
			lda     #0                      ;should NEVER happen!
			rts
.done:			lda     .msg
			rts
.msg:			dc.b    0



;       nextbase -  find next base (that they can access) with NEW messages
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;nextbase:		lda     curbase
;			sta     .idx
;			jmp     .next
;.loop:			ldy     .idx                    ;get current base looking at
;			dey                             ;-1 for table
;			lda     s_msgbdr,y              ;is there a message base here?
;			beq     .next                   ;no - skip it
;
;			mea     u_msgread,s_ptr         ;can they access this base?
;			lda     .idx
;			jsr     seclvl
;			bmi     .next                   ;nope!
;
;			mea     u_qscan,s_ptr           ;do they want to see this
;			lda     .idx                    ; base?
;			jsr     seclvl
;			bmi     .next                   ;nope!
;
;			ldy     .idx                    ;reget idx to be safe
;			dey
;			lda     u_lastread+1            ;is there a higher message
;			cmp     hi_msghi,y              ; number on this base?
;			bcc     .gotone                 ;yes!
;			beq     .cklo
;			jmp     .next                   ;nope..
;.cklo:			lda     u_lastread
;			cmp     hi_msglo,y
;			bcs     .next
;.gotone:		lda     .idx                    ;return next base number in A
;			rts
;.next:			inc     .idx
;			lda     .idx
;			cmp     #32
;			bcc     .loop
;			lda     #0
;			rts
;
;.idx:			dc.b    0
;
;

clear_nodes:		ldy     #0                      ;clear out the node list
			tya
.loop:			sta     m_nodelist,y
			iny
			bne     .loop
			lda     s_node                  ;and set this local node's
			sta     m_nodelist              ; number in the first bucket.
			lda     s_node+1
			sta     m_nodelist+1
			rts



stuff:			sta     queue                   ;stuff a key into the
			lda     #0                      ; keyboard que.
			sta     qout
			lda     #1
			sta     qin
			rts




;
;       print the byte currently in Accum.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
out_byte:		ldx     #0
out_card:		stx     fr0+1

out_num:		sta     fr0
			jsr     ifp                     ;float it
			jsr     fasc                    ;to ascii
			ldy     #0
			sty     .tempy
.prbloop:		ldy     .tempy
			lda     (inbuff),y
			php
			and     #$7f                    ;last byte is negative
			sta     .tempy+1,y
			inc     .tempy
			plp
			bpl     .prbloop
			mea     .tempy,s_ptr
			mea     parm_1,d_ptr
			jsr     move_str
			rts
.tempy:			ds.b    10

;
;
;
;;-----------------------------------------

basecmds:       
			dc.b    'A'             ;show again
			dc.w    b_a
			dc.b    'B'             ;go backward
			dc.w    b_b
			dc.b    'C'             ;continue search
			dc.w    b_c
;			dc.b    'D'             ;delete
;			dc.w    b_d
			dc.b    'E'             ;edit
			dc.w    b_e
			dc.b    'F'             ;go forward
			dc.w    b_f
			dc.b    'H'             ;hardcopy of message (print)
			dc.w    b_h
			dc.b    'J'             ;jump to msg
			dc.w    b_j
			dc.b    'K'             ;keyword search
			dc.w    b_k
			dc.b    'L'             ;lock message
			dc.w    b_l
			dc.b    'M'             ;Make Basic Error database
			dc.w    b_m
			dc.b    'N'             ;new message
			dc.w    b_n
;			dc.b    'P'             ;post a message
;			dc.w    b_p
;			dc.b    'Q'             ;quit thread
;			dc.w    b_q
;			dc.b    'R'             ;reply to msg
;			dc.w    b_r
			dc.b    'S'             ;show replies
			dc.w    b_s
;			dc.b    'T'             ;top of thread
;			dc.w    b_t
			dc.b    'U'             ;unlock message
			dc.w    b_u
			dc.b    'V'		; Video Width
			dc.w    e_V
			dc.b    'X'             ;exit
			dc.w    b_x
			dc.b    'Z'             ;change base name..
			dc.w    b_z
			dc.b    $9b             ;read next
			dc.w    b_cr
			dc.b    '?'             ;help
			dc.w    b_qmark
			dc.b    '-'             ;go backward
			dc.w    b_minus
			dc.b    '<'             ;go backward
			dc.w    b_minus
			dc.b    '+'             ;go forward
			dc.w    b_plus
			dc.b    '>'             ;go forward
			dc.w    b_plus
;			dc.b    '*'             ;base list
;			dc.w    b_ast
;			dc.b    '/'             ;next new base
;			dc.w    b_slash
;			dc.b    '@'             ;mark this base
;			dc.w    b_at
;			dc.b    '='             ;Goto a msgbase
;			dc.w    b_eq

			dc.b    0
			dc.w    b_bad
:


ed_amsg:		ds.b    1               ;is this a message-base message?
ed_msgbuff:		ds.w    1               ;pointer to message buff area
ed_maxbytes:		ds.w    1               ;max bytes to allow
ed_width:		dc.b    40              ;max width to allow
ed_graphics:		ds.b    1               ;$80=allow graphics, 0=don't
ed_savemode:		ds.b    1               ;will either be 0 or AS_IS
ed_title:		ds.b    31              ;title of this document
ed_toname:		ds.b    31              ;title of this document


msgedit:
			lda     #$01
			sta     rc             
			lda     ed_amsg        
			beq     .notmsg         ;   change vw to 77
			lda     ed_width
			cmp     #40
			beq     .notmsg
			lda     #77
			sta     ed_width
.notmsg:		jsr     header
			lda     #0			      ;start in NON graphics mode
			sta     graphics
			sta     did_wrap
			sta     pasteline
			lda     #$80
			sta     wordwrap

newline:		lda     linenum			 ;make sure linenum never
			bne     .ok			     ; goes below 1!
			inc     linenum

.ok:			jsr     cklength			;calculate byte maximums
			bit     graphics
			bmi     .grmode

			jsr     printsi
			dc.b    $9b,'>',-1
			jmp     .nl1

.grmode:		jsr     printsi
			dc.b    $9b,20,-1               ;graphics prompt

.nl1:			lda     linenum
			cmp     #251			    ;only allow 250 lines
			bcs     .maxedout
			lda     bytes_left+1
			bne     .notmaxed
			lda     ed_width
			cmp     bytes_left              ;must have at least video
			bcc     .notmaxed               ; width left!

.maxedout:		lda     #1
			sta     msgline
			lda     #'/'
			sta     msgline+1
			bne     .gotaline
.notmaxed:		lda     #0
			sta     autoreturn
			lda     ed_width
			sec
			sbc     #2
			jsr     get_line			;get a line

.gotaline:		lda     msgline
			cmp     #1			      ;lth = 1?
			bne     .not2			   ;nope
			lda     msgline+1
			cmp     #'/'			    ;slash?
			bne     .not1
			jsr     do_edit			 ;yes, do editing functions
			jmp     newline

.not1:			cmp     #'?'
			bne     .not2
			jsr     printsi
			dc.b    "Line #",-1
			lda     linenum
			jsr     pr_byte
			jsr     printsi
			dc.b    ", ",-1
			lda     bytes_left
			ldx     bytes_left+1
			jsr     pr_card
			jsr     printsi
			dc.b    " bytes left",-1
			jmp     newline
.not2:			jsr     movtobuff               ;move line to message buffer
			inc     linenum
			mea     msgline,s_ptr
			mea     pasteline,d_ptr
			jsr     move_str
			jmp     newline


;       the message editing commands
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

do_edit:		jsr     printsi
			dc.b    126,'[',-1
			lda     linenum			 ;'[xx lines] Edit: _'
			sec
			sbc     #1
			jsr     pr_byte
			jsr     printsi
			dc.b    "] Edit: ",-1
			jsr     get_key
			and     #$7f
			pha
			mea     editcmds,s_ptr
			pla
			jsr     execute
			rts

e_s:			lda     #0			      ;normal save
			sta     ed_savemode
			jmp     save_msg

e_u:			lda     #AS_IS			  ;unformatted save
			sta     ed_savemode
			jmp     save_msg

save_msg:		jsr     cklines
			jsr     printsi
			dc.b    "Save",$9b,-1
			dec     linenum			 ;linenum-1 = actual # of lines
			jsr     stripcrs
			pla
			pla
			pla
			pla
			lda     ed_width			;restore ed_width to 80
			cmp     #40			     ; after we're done...
			beq     .exit
			lda     #80
			sta     ed_width
.exit:			lda     linenum
			rts


e_r:			jsr     cklines
			jsr     ckmax
			jsr     printsi
			dc.b    126,126,126,126,126,126,126,126,126,126,-1
			lda     linenum
			cmp     #101
			bcc     .not100
			lda     #126
			jsr     echo
.not100:		cmp     #11
			bcc     .enough
			lda     #126
			jsr     echo
.enough:		lda     #'>'
			jsr     echo
			lda     linenum
			sec
			sbc     #1
			jsr     calcptr
			lda     pr_ptr
			sta     s_ptr
			lda     pr_ptr+1
			sta     s_ptr+1
			jsr     echos			   ;print linenum-1
			mea     msgline,d_ptr
			jsr     move_str

			jsr     movtobuff               ;move line to message buffer
			inc     linenum
			rts

e_a:
			jsr     printsi
			dc.b    "Abort...You sure? ",-1
			jsr     y_or_n
			cmp     #'Y'
			bne     .xea
			pla
			pla
			pla
			pla
			lda     #$80
			sta     rc			     ; added 12/31/90
			lda     ed_width			;restore ed_width to 80
			cmp     #40			     ; after we're done...
			beq     .exit
			lda     #80
			sta     ed_width
.exit:			lda     #0
.xea:			rts


e_b:
			jsr     printsi
			dc.b    "Begin Over...You sure? ",-1
			jsr     y_or_n
			cmp     #'Y'
			bne     .xea
			lda     #1
			sta     linenum
			jsr     e_sl			    ;clear the screen
.xea:			rts


e_c:			jsr     printsi
			dc.b    125,$9b,' ',-1
			lda     linenum
			sta     .temp
			lda     #1
			sta     linenum
			jsr     header2
			lda     .temp
			sta     linenum
			rts
.temp:			dc.b    0


e_sl:			lda     #125
			jsr     echo
			rts

e_d:			jsr     cklines
			jsr     printsi
			dc.b    "Delete ",-1
			lda     linenum
			sec
			sbc     #1
			sta     def_1
			sta     def_2
			jsr     get_nums			;get two numbers
			lda     nums
			beq     .delerr
			jsr     del_lines
			jsr     printsi
			dc.b    " - Done!",-1
			rts
.delerr:
			jsr     printsi
			dc.b    " - No lines deleted!",-1
			rts

e_e:			jsr     cklines
			jsr     ckmax
			jsr     printsi
			dc.b    "Edit ",-1
			lda     linenum
			sec
			sbc     #1
			sta     def_1
			sta     def_2
			jsr     get_nums			;get two numbers
			jsr     printsi
			dc.b    $9b,$9b,'-',-1
			lda     fromline
			jsr     calcptr
			jsr     movepaste               ;move line to paste buffer
			jsr     echos
			jsr     printsi
			dc.b    $9b,'+',-1
			lda     #$80
			sta     autoreturn
			lda     #0			      ;clear wrapped flag
			sta     did_wrap
			lda     ed_width
			sec
			sbc     #2
			jsr     get_line			;get new line
			lda     msgline
			beq     .nochg

;       delete old line from the buffer, then insert the new one
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			ldx     linenum
			dex                             ;if editing last line,
			cpx     fromline			; then DON'T contract/expand!
			beq     .noexp

			lda     fromline
			sta     toline			  ;delete it..
			jsr     contract
			lda     fromline
			ldx     msgline			 ;get length of new line
			inx			             ; ..plus 1
			jsr     expand			  ;expand the buffer
.noexp:			lda     fromline
			jsr     calcptr			 ;point to new buffer area
			lda     pr_ptr
			sta     d_ptr
			lda     pr_ptr+1
			sta     d_ptr+1
			mea     msgline,s_ptr
			jsr     move_str
			rts

.nochg:
			jsr     printsi
			dc.b    "Line unchanged...",-1
			rts

e_i:			jsr     cklines
			jsr     ckmax
			jsr     printsi
			dc.b    "Insert Before ",-1
			lda     linenum
			sec
			sbc     #1
			sta     def_1
			sta     def_2
			jsr     get_nums			;get two numbers

			jsr     printsi
			dc.b    $9b,$9b,'+',-1
			lda     #$80
			sta     autoreturn
			lda     #0			      ;clear wrapped flag
			sta     did_wrap
			sta     pasteline
			lda     ed_width
			sec
			sbc     #2
			jsr     get_line			;get new line

			lda     fromline
			ldx     msgline			 ;get length of new line
			inx			             ; ..plus 1
			jsr     expand			  ;expand the buffer
			lda     fromline
			jsr     calcptr			 ;point to new buffer area
			lda     pr_ptr
			sta     d_ptr
			lda     pr_ptr+1
			sta     d_ptr+1
			mea     msgline,s_ptr
			jsr     move_str
			inc     linenum			 ;+1 to total lines
			rts




e_l:			jsr     cklines
			jsr     printsi
			dc.b    "List ",-1
			lda     #1
			sta     def_1
			lda     linenum
			sec
			sbc     #1
			sta     def_2
			jsr     get_nums			;get two numbers
			lda     #0			      ;no numbers
			jmp     listmsg

e_n:			jsr     cklines
			jsr     printsi
			dc.b    "List ",-1
			lda     #1
			sta     def_1
			lda     linenum
			sec
			sbc     #1
			sta     def_2
			jsr     get_nums			;get two numbers
			lda     #1			      ;do numbers
			jmp     listmsg


e_f:			jsr     cklines
			jsr     formheader
			lda     #1
			sta     fromline
			lda     linenum
			sec
			sbc     #1
			sta     toline
			lda     #0			      ;no numbers
			jmp     listmsg


e_qmark:

			jsr	printsi
			dc.b	$7d,$9b
			dc.b	$9b," All commands are preceeded by a '/'"
			dc.b	$9b,"  in the first position of a line"

			dc.b	$9b,"  "
			dc.b	<+128>,"[A]"
			dc.b	" Abort this text and exit"
			dc.b	$9b,"  "
			dc.b	<+128>,"[B]"
			dc.b	" Begin this text over again"
			dc.b	$9b,"  "
			dc.b	<+128>,"[C]"
			dc.b	" Display a columnar heading"
			dc.b	$9b,"  "
			dc.b	<+128>,"[E]"
			dc.b	" Edit an individual line"
			dc.b	$9b,"  "
			dc.b	<+128>,"[F]"
			dc.b	" Formatted listing of message"
			dc.b	$9b,"  "
			dc.b	<+128>,"[G]"
			dc.b	" Toggle graphics mode on / off"
			dc.b	$9b,"  "
			dc.b	<+128>,"[I]"
			dc.b	" Insert a new line"
			dc.b	$9b,"  "
			dc.b	<+128>,"[L]"
			dc.b	" List the message"
			dc.b	$9b,"  "
			dc.b	<+128>,"[N]"
			dc.b	" List the message with numbers"
			dc.b	$9b,"  "
			dc.b	<+128>,"[R]"
			dc.b	" Repeat the last input line"
			dc.b	$9b,"  "
			dc.b	<+128>,"[S]"
			dc.b	" Save this message and exit"
			dc.b	$9b,"  "
			dc.b	<+128>,"[U]"
			dc.b	" Unformatted Save  and exit"
			dc.b	$9b,"  "
			dc.b	<+128>,"[V]"
			dc.b	" Toggle Video width 40 / 80"
			dc.b	$9b,"  "
			dc.b	<+128>,"[W]"
			dc.b	" Toggle word wrap on and off"
			dc.b	$9b,"  "
			dc.b	<+128>,"[/]"
			dc.b	" Clear the screen"
			dc.b	$9b,"          "
			dc.b	<+128>,"[Control Keys]"
			dc.b	$9b
			dc.b	$9b,"^L - Pastes a Letter from saved line"
			dc.b	$9b,"^W - Pastes a Word   from saved line"
			dc.b	$9b," Press any key to continue  ",-1
			jsr	get_key
			rts



;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;This will toggle between 40 and 80 column that was added for Walter
;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e_v:
			jsr	printsi
			dc.b	$9b," In ",-1
                	lda     ed_width 
                	cmp     #40
                	beq     .do80
			cmp	#80
			beq	.do40
			cmp	#77
			beq	.do40
			rts

.do40:			lda	#40
                	sta     ed_width
			jsr	printsi
			dc.b	"40 ",-1
			jmp	.exit

.do80:			lda     #77
                	sta     ed_width
			jsr	printsi
			dc.b	"80 ",-1
.exit:			jsr	printsi
			dc.b	"Column Mode "
			dc.b	<+128>,"Now"
			dc.b	-1
			rts





e_g:			lda     ed_graphics
			bmi     .ok
			jsr     printsi
			 dc.b   "Graphics not allowed here!",-1
			rts
.ok:			lda     trans
			bne     .nocando
			lda     graphics
			beq     .gon
.goff:			jsr     printsi
			dc.b    "Graphics mode off!",-1
			lda     #0
			sta     graphics
			rts
.gon:			jsr     printsi
			dc.b    "Graphics mode on!",-1
			lda     #$80
			sta     graphics
			rts
.nocando:		jsr     printsi
			dc.b    "You're not in ATASCII mode!",-1
			rts


e_w:			lda     graphics
			bne     .nocando
			lda     wordwrap
			beq     .gon
.goff:			jsr     printsi
			dc.b    "Word wrap off!",-1
			lda     #0
			sta     wordwrap
			rts
.gon:			jsr     printsi
			dc.b    "Word wrap on!",-1
			lda     #$80
			sta     wordwrap
			rts
.nocando:		jsr     printsi
			dc.b    "Not while in Graphics mode!",-1
			rts



e_bad:
			jsr     printsi
			dc.b    "Enter ? for help",-1
			rts



cklines:		lda     linenum
			cmp     #2
			bcs     .xck
			jsr     printsi
			dc.b    "No lines entered yet!",-1
			pla
			pla
.xck:			rts


ckmax:			lda     linenum
			cmp     #251			    ;only allow 250 lines
			bcs     .maxedout
			lda     bytes_left+1
			bne     .notmaxed
			lda     ed_width
			cmp     bytes_left              ;must have at least video
			bcc     .notmaxed               ; width left!
.maxedout:		jsr     printsi
			dc.b   "No room left!",-1
			pla
			pla
.notmaxed:		rts


;       see if they are within five lines of maximum
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cklength:		jsr     total_bytes             ;calc total used so far
			lda     ed_maxbytes
			sec
			sbc     tot_bytes
			sta     bytes_left
			lda     ed_maxbytes+1           ;calc # bytes left
			sbc     tot_bytes+1
			sta     bytes_left+1

			lda     linenum
			cmp     #246
			bne     .notnear
			jsr     printsi
			dc.b    $9b,"Just 5 lines left!",-1
			rts
.notnear:		lda     bytes_left+1
			bne     .exit
			jsr     printsi
			dc.b   $9b,"Less than 255 bytes left!",-1
.exit:
			rts




;       delete lines from FROMLINE to TOLINE (inclusive)
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

del_lines:		ldx     linenum
			dex
			stx     .numlines               ;if toline >= (linenum-1) then
			lda     toline			  ;    linenum := fromline
			cmp     .numlines
			bcc     .notend
			lda     fromline
			sta     linenum
			rts
.notend:		lda     toline
			sec
			sbc     fromline			;offset := (to - from)+1
			clc
			adc     #1
			sta     .numlines               ;# of lines to delete
			jsr     contract			;delete the lines
			lda     linenum
			sec
			sbc     .numlines
			sta     linenum			 ;update linenum
			rts

.numlines:		dc.b    0


listmsg:		sta     .nums
			jsr     cr_lf
			jsr     cr_lf
			lda     #0
			sta     aborted
			lda     fromline
			sta     .counter
.loop:			lda     .nums			   ;do numbers?
			beq     .nonums			 ;nope
			lda     .counter
			jsr     pr_byte
			jsr     printsi
			dc.b    ": ",-1
			jmp     .over
.nonums:		jsr     printsi
			dc.b    " ",-1
.over:			lda     .counter			;display the line
			jsr     calcptr
			jsr     echos
			jsr     cr_lf
			lda     aborted
			bne     .abt
			inc     .counter
			lda     .counter
			cmp     toline
			bcc     .loop
			beq     .loop
.abt:			rts
.nums:			dc.b    0
.counter:		dc.b    0



;       move the line at MSGLINE into the buffer (at LINENUM position)
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

movtobuff:		lda     linenum
			jsr     calcptr			 ;point to right buffer loc
			lda     pr_ptr
			sta     d_ptr
			lda     pr_ptr+1
			sta     d_ptr+1
			mea     msgline,s_ptr
			jsr     move_str
			rts


;       set PR_PTR to point to the message line number in A (-1)
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

calcptr:		sta     .lnum			   ;hold line num to pos at.
			lda     ed_msgbuff
			sta     pr_ptr			  ;point to start of buffer
			lda     ed_msgbuff+1
			sta     pr_ptr+1
			ldx     #0
			beq     .nextln
.loop:			ldy     #0
			lda     (pr_ptr),y              ;get length byte of this line
			clc
			adc     pr_ptr
			sta     pr_ptr
			lda     pr_ptr+1
			adc     #0
			sta     pr_ptr+1
			inc     pr_ptr
			bne     .nextln
			inc     pr_ptr+1
.nextln:		inx			             ;down to requested line?
			cpx     .lnum
			bcc     .loop
.done:			rts
.lnum:			dc.b    0			       ;line # to position at



;       gets two numbers from the user.  Returns with fromline and toline
;       set to the defaults (def_1 and def_2) if necessary.  This is
;       done if (1) the user just hits return, or (2) the user
;       enters an invalid line number.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


get_nums:		lda     #0
			sta     nums			    ;zero everything out
			lda     def_1
			sta     fromline
			lda     def_2
			sta     toline
			jsr     get_str
			dc.w   numline
			dc.b   7,0,1
			mea     numline,s_ptr
			mea     instr,d_ptr
			jsr     toascii			 ;make a $9b terminated string
			lda     #0
			sta     bufoff

			jsr     crunch                  ;get first command word
			bcc     .ok1
			jmp     .exit
.ok1:			inc     nums
			mea     word,s_ptr              ;make to lth-byte string
			mea     in_str,d_ptr
			jsr     tostr
			mea     in_str,s_ptr
			jsr     valc			    ;to a number
			sta     fromline			;and save number one

			jsr     crunch			  ;get next command word
			bcs     .exit
			inc     nums
			mea     word,s_ptr              ;make to lth-byte string
			mea     in_str,d_ptr
			jsr     tostr
			mea     in_str,s_ptr
			jsr     valc			    ;to a number
			sta     toline			  ;and save number one

.exit:			lda     fromline			;check for range errors
			beq     .n1err
			cmp     linenum
			bcc     .n1ok
.n1err:			lda     def_1
			sta     fromline
.n1ok:			lda     toline
			beq     .n2err
			cmp     linenum
			bcc     .n2ok
.n2err:			lda     def_2
			sta     toline
.n2ok:			lda     nums			    ;if only 1 line num,
			cmp     #1			      ; set toline = fromline
			bne     .asis
			lda     fromline
			sta     toline
.asis:			lda     toline			  ;if toline < fromline,
			cmp     fromline			; set toline = fromline
			beq     .as1
			bcs     .as1
			lda     fromline
			sta     toline
.as1:			rts




header:
			lda	#$7d
			jsr	echo
			jsr	tammy-3
			jsr     printsi
			dc.b    $9b
			dc.b    " / to Enter Edit Mode      /S to Save ",$9b
			dc.b    " ? for Current Line No     /? For Help",$9b
			dc.b    " Title: ",-1
			mea     ed_title,pr_ptr
			jsr     echos
			lda     ed_toname
			beq     header2
;			jsr     printsi
;			dc.b    $9b
;			dc.b    "    To: ",-1
;			mea     ed_toname,pr_ptr
;			jsr     echos
header2:		jsr     printsi
			dc.b    $9b
			dc.b    " ----+----1----+----2----+----3----+---",-1
			lda     ed_width
			cmp     #40
			beq     .col40
			jsr     printsi
			dc.b    "-4----+----5----+----6----+----7----+",-1
			lda     ed_width
			cmp     #80
			bne     .col40
			jsr     printsi
			dc.b    "---",-1
.col40:			lda     linenum
			cmp     #2
			bcc     .xhead
			jsr     printsi
			dc.b    $9b," Beginning edit at line number ",-1
			lda     linenum
			jsr     pr_byte
			jsr     cr_lf
.xhead:			rts


;       fake header for the "formatted list" command
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

formheader:		lda     ed_amsg
			bne     .goon
			lda     #125
			jsr     echo
			rts
.goon:			jsr     printsi
			dc.b    125,$9b
			dc.b    "Message: 0 (#0)",$9b
			dc.b    "  Title: ",-1
			mea     ed_title,pr_ptr
			jsr     echos
			jsr     printsi
			dc.b    $9b
			dc.b    " Author: ",-1
			mea     u_handle,pr_ptr
			jsr     echos
			jsr     printsi
			dc.b    $9b
			dc.b    "     To: ",-1
			mea     ed_toname,pr_ptr
			jsr     echos
			jsr     printsi
			dc.b    $9b
			dc.b    " Posted: ",-1
			jsr     showdate
			jsr     printsi
			dc.b    " at ",-1
			jsr     showtime
			jsr     printsi
			dc.b    $9b,"Replies: 0",$9b,-1
			rts



get_line:		sta     .maxlth			 ;save max length
			lda     #0
			sta     msgline			 ;zero length byte
			sta     pasteidx			;zero pasting index

;       if a word wrap occurred, move it in
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			lda     did_wrap			;did we wrap?
			beq     .no_wrap			;nope
			ldy     #0
.mvwrap:		lda     wrapword,y              ;get a byte
			beq     .no_wrap			;0 = end of word
			sta     msgline+1,y
			jsr     echo			    ;print the byte
			inc     msgline			 ;+1 to length byte
			iny
			jmp     .mvwrap

.no_wrap:		lda     #0
			sta     did_wrap
.gsloop:		jsr     get_key			 ;get a keypress
			cmp     #'\'			   ;can't allow this key!
			beq     .gsloop			 ;yes, ignore
			cmp     #$9b			    ;carriage return?
			bne     .not_cr			 ;nope
			jmp     .gscr

.not_cr:		pha			             ;save original key
			jsr	.doclrcr		;clear c/r counter
			bit     autoreturn              ;auto-return on '/'?
			bmi     .notone			 ;nope
			ldx     msgline			 ;in first position of line?
			bne     .notone			 ;nope
			and     #$7f			    ;strip inverse
			cmp     #'/'			    ;enter edit mode?
			beq     .go_edit			;yes
			cmp     #'?'			    ;request bytes-left?
			bne     .notone			 ;nope

.go_edit:		sta     msgline+1
			lda     #1
			sta     msgline
			pla
			rts

.notone:		pla			             ;get back original key
			cmp     #126			    ;backspace?
			beq     .bspace

			bit     graphics			;if in graphics mode,
			bpl     .not_grm
			sta     .key			    ; it's ok...
			jmp     .gsstore

.not_grm:		cmp     #30
			bne     .not30
			jmp     .bspace
.not30:			cmp     #24			     ;^X key?
			bne     .not24
			jmp     .wipeout
.not24:			cmp     #127			    ;TAB key?
			bne     .not127
			jmp     .tab
.not127:		cmp     #12			     ;^L (paste letter)?
			bne     .not12
			jmp     .paste			  ;yep!
.not12:			cmp     #23			     ;^W (paste word)?
			bne     .not23
			jmp     .pasteword
.not23:			cmp     #14			     ;^N (name)?
			bne     .not14
			jmp     .handle			 ;yep!
.not14:			cmp     #18			     ;^R (real name)?
			bne     .not18
			jmp     .name			   ;yep!
.not18:			and     #$7f			    ;strip inverse bit
			sta     .key                    ;hold it

;       now, see if it falls within requested mask
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.notauto:		cmp     #32                     ; < space?
			bcc     .gsloop			 ;yes, ignore
			cmp     #'z'+1			  ; > 'z'
			bcs     .gsloop			 ;yes, ignore

.gsstore:		jsr     .store_it               ;save the keypress
			jmp     .check_ww               ;see if need to word wrap

.bspace:		lda     msgline			 ;can't backspace
			cmp     #0			      ; if no length!
			bne     .bs1
			jmp     .gsloop
.bs1:			dec     msgline			 ;-1 to length byte
			lda     #126			    ;output a BSpace
			jsr     echo
			jmp     .gsloop

.wipeout:		lda     msgline			 ;get length
			bne     .wipe1
			jmp     .gsloop			 ;done
.wipe1:			dec     msgline			 ;-1 to length byte
			lda     #126			    ;output a BSpace
			jsr     echo
			jmp     .wipeout

.tab:			lda     #' '
			sta     .key
			jsr     .store_it
			jsr     .store_it
			jsr     .store_it
			jsr     .store_it
			jsr     .store_it
			jmp     .check_ww               ;see if need to word wrap

.gscr:
			inc	.numbcr
			lda	.numbcr
			cmp	#$03
			bcc	.pok
			lda	#'/'
			sta	msgline+1
			lda	#1
			sta	msgline
			rts
.pok:
	        	lda     msgline			 ;return with lth in accum.
			rts

.paste:         	ldy     pasteidx			;get index into line
			cpy     pasteline               ;past end of paste line?
			bcs     .paignore               ;yes - ignore key
			iny
			sty     pasteidx
			lda     pasteline,y             ;get next pasteable key
			sta     .key
			jmp     .gsstore
.paignore:		jmp     .gsloop			 ;ignore the key


.pasteword:		ldy     pasteidx			;get index into line
			cpy     pasteline               ;past end of paste line?
			bcs     .pwignore               ;yes - ignore key
			iny
			sty     pasteidx
			lda     pasteline,y             ;get next pasteable key
			sta     .key
			jsr     .store_it
			lda     .key
			cmp     #' '			    ;space?
			bne     .pasteword              ;keep going
.pwignore:		jmp     .check_ww               ;end by checking wrap


.handle:		ldx     #1
			jmp     .hannx
.halp:			lda     u_handle,x
			sta     .key
			jsr     .store_it
			inx
.hannx:			cpx     u_handle
			bcc     .halp
			beq     .halp
			jmp     .check_ww

.name:			ldx     #1
			jmp     .namnx
.nalp:			lda     u_name,x
			sta     .key
			jsr     .store_it
			inx
.namnx:			cpx     u_name
			bcc     .nalp
			beq     .nalp
			jmp     .check_ww

.store_it:		lda     msgline			 ;get lth byte
			cmp     .maxlth
			beq     .xsi			    ;ignore it..at
			bcs     .xsi			    ; max length now
			tay
			iny
			lda     .key
			sta     msgline,y               ;save key into string
			inc     msgline			 ;increase length
			lda     .key
			jsr     echo
.xsi:			rts

.check_ww:		bit     graphics			;don't word wrap if
			bmi     .xck_ww			 ; in graphics mode
			bit     autoreturn              ;auto-return on '/'?
			bmi     .xck_ww			 ;nope
			lda     wordwrap
			beq     .xck_ww
			lda     msgline
			cmp     .maxlth			 ;at max length?
			bcc     .xck_ww			 ;nope

;       first, find last space in the line
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			tay			             ;length to Y
.cwwfs:			lda     msgline,y               ;get char
			cmp     #' '			    ;a space?
			beq     .gotspace               ;yep
			dey
			bne     .cwwfs
.xck_ww:		jmp     .gsloop			 ;no words to wrap!



;       now, erase and move the last word into WRAPWORD
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.gotspace:		cpy     msgline			 ;space in last character?
			bne     .no_lc			  ;nope
			dec     msgline			 ;yes,remove end space from
			jmp     .gscr			   ; and just exit..
.no_lc:			sty     .wwstart			;save index
			iny			             ;go past space

			lda     #0			      ;clear out wrapword
			tax
.clword:		sta     wrapword,x
			inx
			cpx     #82
			bne     .clword

			ldx     #0			      ;index into wrapword
.gslp:			lda     msgline,y               ;move next char over
			sta     wrapword,x
			lda     #126
			jsr     echo			    ;print a backspace
			iny
			inx
			dec     msgline			 ;-1 to length
			lda     msgline
			cmp     .wwstart			;are we done?
			beq     .doneww
			bcs     .gslp			   ;nope
.doneww:		lda     #1
			sta     did_wrap			;signal that we wrapped
			dec     msgline			 ;remove space at end
			jmp     .gscr

.doclrcr:
			pha
			lda	#$00
			sta	.numbcr
			pla
			rts


.numbcr:		dc.b	0
.maxlth:        	dc.b    1
.key:           	dc.b    1
.wwstart:       	dc.b    1


;       remove any blank lines from the end of the msg
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

stripcrs:       	lda     linenum			 ;get current line number
			beq     .done			   ;done if no lines left
			jsr     calcptr			 ;point to the line
			ldy     #0
			lda     (pr_ptr),y              ;any data here?
			bne     .done			   ;yes - we're done
			dec     linenum
			jmp     stripcrs
.done:			rts
.temp:			dc.b    0



;       calculate the total # of bytes in this message
;       and place the total into "tot_bytes".
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

total_bytes:		lda     ed_msgbuff              ;point to the msg buffer
			sta     s_ptr
			lda     ed_msgbuff+1
			sta     s_ptr+1
			lda     linenum			 ;if no lines, then no bytes!
			cmp     #2
			bcc     .exit
			ldx     #1			      ;line counter
.loop:			ldy     #0
			lda     (s_ptr),y               ;get length byte of this line
			clc
			adc     s_ptr
			sta     s_ptr
			lda     s_ptr+1
			adc     #0
			sta     s_ptr+1
			inc     s_ptr
			bne     .noovf
			inc     s_ptr+1
.noovf:			inx			             ;continue for all lines
			cpx     linenum
			bcc     .loop
.exit:			lda     s_ptr
			sec
			sbc     ed_msgbuff              ;tot_bytes = s_ptr-ed_msgbuff
			sta     tot_bytes
			lda     s_ptr+1
			sbc     ed_msgbuff+1
			sta     tot_bytes+1
			rts
.count:			dc.b    0



;       Contract (delete) the buffer down, starting at the
;       line number in FROMLINE to the line number in TOLINE.
;       Note: the delete is INCLUSIVE...
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

contract:		lda     fromline
			jsr     calcptr			 ;set pr_ptr to point to it
			lda     pr_ptr
			sta     s_ptr			   ;s_ptr points to point of
			lda     pr_ptr+1			;  contraction...
			sta     s_ptr+1
			lda     toline
			clc
			adc     #1
			jsr     calcptr			 ;get end of contraction
			lda     pr_ptr
			sta     d_ptr			   ;d_ptr points to the source
			lda     pr_ptr+1			; of the contraction..
			sta     d_ptr+1
			lda     linenum			 ;now, find END of buffer
			jsr     calcptr			 ; into PR_PTR
			ldy     #0
.cloop:			lda     (d_ptr),y               ;move a byte down
			sta     (s_ptr),y
			inc     s_ptr
			bne     .nov1
			inc     s_ptr+1
.nov1:			inc     d_ptr
			bne     .nov2
			inc     d_ptr+1
.nov2:			lda     d_ptr+1			 ;go until d_ptr = pr_ptr
			cmp     pr_ptr+1
			bne     .cloop
			lda     d_ptr
			cmp     pr_ptr
			bne     .cloop
			rts

.lth:			dc.b    0
.start:			dc.b    0                       ;the line # to contract out



;       Expand (insert) the buffer outwards starting at the
;       line number in A for the number of bytes in X.
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

expand:			sta     .start                  ;save starting line number
			stx     .size
			lda     linenum
			jsr     calcptr			 ;find end of active buffer
			lda     pr_ptr
			sec
			sbc     #1
			sta     s_ptr			   ;s_ptr := (end_of_buf-1)
			lda     pr_ptr+1
			sbc     #0
			sta     s_ptr+1

			lda     s_ptr			   ;d_ptr := (end_of_buf-1)+size
			clc
			adc     .size
			sta     d_ptr
			lda     s_ptr+1
			adc     #0
			sta     d_ptr+1

			lda     .start			  ;pr_ptr := start_pos
			jsr     calcptr
			lda     pr_ptr
			sec
			sbc     #1
			sta     pr_ptr
			lda     pr_ptr+1
			sbc     #0
			sta     pr_ptr+1

			ldy     #0
.eloop:			lda     (s_ptr),y               ;move a byte up..
			sta     (d_ptr),y
			lda     s_ptr
			sec
			sbc     #1
			sta     s_ptr
			lda     s_ptr+1
			sbc     #0
			sta     s_ptr+1
			lda     d_ptr
			sec
			sbc     #1
			sta     d_ptr
			lda     d_ptr+1
			sbc     #0
			sta     d_ptr+1
			lda     s_ptr+1			 ;go until s_ptr = pr_ptr
			cmp     pr_ptr+1
			bne     .eloop
			lda     s_ptr
			cmp     pr_ptr
			bne     .eloop
			rts

.start:			dc.b    0			       ;the line # to contract out
.size:			dc.b    0



movepaste:		lda     pr_ptr
			sta     s_ptr
			lda     pr_ptr+1			;move line into paste buffer
			sta     s_ptr+1
			mea     pasteline,d_ptr
			jsr     move_str
			rts

print_msg:	jsr	getmsg			;read it from disk
		mea	.P_dev,fn_ptr
		ldx	#disk_2
		jsr	open_out		;open P: as output
		bpl	.op_ok			;worked..
		jsr	printsi			; and give message...
		 dc.b	"Printer is offline!",$9b,-1
		rts
.op_ok: 	mea	.null,pr_ptr		;print 2 blank lines..
		jsr	.print
		mea	.null,pr_ptr
		jsr	.print

;	print the header of the message
;	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

		mea	.title,s_ptr
		mea	in_str,d_ptr
		jsr	move_str
		mea	m_title,s_ptr
		jsr	concat
		mea	in_str,pr_ptr
		jsr	.print

		mea	.author,s_ptr
		mea	in_str,d_ptr
		jsr	move_str
		mea	m_from,s_ptr
		jsr	concat
		mea	in_str,pr_ptr
		jsr	.print

		mea	.to,s_ptr
		mea	in_str,d_ptr
		jsr	move_str
		mea	m_to,s_ptr
		jsr	concat
		mea	in_str,pr_ptr
		jsr	.print

		mea	.null,pr_ptr		;blank line
		jsr	.print


;	now print the body of the message
;	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

		lda	#1
		sta	.counter
.loop:		lda	.counter		;get current line #
		jsr	calcptr 		;point to it..
		jsr	.print			;print this line
		inc	.counter
		lda	.counter
		cmp	m_lines
		bcc	.loop
		beq	.loop
.done:		mea	.null,pr_ptr		;blank line
		jsr	.print
		ldx	#disk_2
		jsr	close
		rts


.print: 	lda	pr_ptr
		sta	s_ptr
		lda	pr_ptr+1		;move it to INSTR to print
		sta	s_ptr+1
		mea	instr,d_ptr
		jsr	move_str
		mea	instr,pr_ptr
		ldx	#disk_2
		jsr	printsd 		;print this line
		bmi	.pr_error		;damn!
		rts
.pr_error:	ldx	#disk_2
		jsr	close
		jsr	printsi
		 dc.b	"Printer error!",$9b,-1
		pla
		pla
		rts

.counter:	dc.b	0
.P_dev: 	dc.b	"P:",0
.null:		dc.b	1,' '
.title: 	dc.b	8," Title: "
.author:	dc.b	8,"Author: "
.to:		dc.b	8,"    To: "








linenum:		ds.b    1
graphics:		ds.b    1                       ;$80 = graphics on
wordwrap:		ds.b    1                       ;$80 = word wrap on
nums:			ds.b    1                       ;# of line nums entered
def_1:			ds.b    1                       ;default # 1
def_2:			ds.b    1                       ;default #2
fromline:		ds.b    1
toline:			ds.b    1
autoreturn:		ds.b    1
did_wrap:		ds.b    1                       ;did word wrap occur?
pasteidx:		ds.b    1                       ;index into recall line
tot_bytes:		ds.b    2                       ;total bytes used so far
bytes_left:		ds.b    2                       ;total bytes remaining

msgline:		ds.b    85
wrapword:		ds.b    85
pasteline:		ds.b    85                      ;line we're pasting from
numline:		ds.b    10

editcmds:		dc.b    'S'
			dc.w    e_s
			dc.b    'U'
			dc.w    e_u
			dc.b    'A'
			dc.w    e_a
			dc.b    'B'
			dc.w    e_b
			dc.b    'C'
			dc.w    e_c
			dc.b    'D'
			dc.w    e_d
			dc.b    'E'
			dc.w    e_e
			dc.b    'F'
			dc.w    e_f
			dc.b    'I'
			dc.w    e_i
			dc.b    'L'
			dc.w    e_l
			dc.b    'N'
			dc.w    e_n
			dc.b    'W'
			dc.w    e_w
			dc.b    'R'
			dc.w    e_r
			dc.b    'G'
			dc.w    e_g
			dc.b    '/'
			dc.w    e_sl
			dc.b    'V'
			dc.w	e_V
			dc.b    '?'
			dc.w    e_qmark
			dc.b    0
			dc.w    e_bad







line_count:		dc.b    0               ;number of lines output

quickscan:		dc.b    0               ;invoked with a 'Q'?
nonstop:		dc.b    0               ;non-stop scan?
override:		dc.b    0               ;if >0, do auto-load of base
curbase:		dc.b    1               ;base we're currently on
curmsg:			dc.b    1               ;current message we're on
verbose:		dc.b    0               ;if 0, only show header
steprate:		dc.b    0               ;added to increments

fromuser:		dc.b    0,0
touser:			dc.b    0,0
replymsg:		dc.b    0,0

lastsrch:		dc.b    0
keyword:		ds.b    25

topthread:		dc.b    0               ;message # that started the thread
msgstack:		dc.b    0               ;stack pointer
threadidx:		dc.b    0               ;index into the stack
stacklo:		ds.b    256
stackhi:		ds.b    256

;       Message base INDEX layout (125 messages max allowed - 1536 bytes)
;       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


i_buff:			ds.b    0               ;read into here
i_name:			ds.b    21              ;name of this message base
i_chunks:		ds.b    1               ;number of chunks per message
i_totbytes:		ds.b    2               ;max bytes allowed per message
i_maxmsgs:		dc.b    255               ;max messages allocated for
i_inuse:		dc.b    255               ;number of slots currently in use
i_posted:		dc.w    255              ;total msgs posted here
i_graphics:		ds.b    1               ;$80=allow /Graphics, 0=don't
i_msgptr:		ds.b    256             ; 250 message pointers
i_flag:			ds.b    256             ;locked/received
i_msgnlo:		ds.b    256             ; messages numbers lo
i_msgnhi:		ds.b    256             ;                  hi
i_tolo:			ds.b    256             ;to user number lo
i_tohi:			ds.b    256             ;               hi
i_fromlo:		ds.b    256             ;from user number lo
i_fromhi:		ds.b    256             ;                 hi
i_replylo:		ds.b    256             ;message number this is a reply to lo
i_replyhi:		ds.b    256             ;   hi
i_filler:		ds.b    21              ;fill out to a chunk boundary


;       Message base DATA layout
;       ~~~~~~~~~~~~~~~~~~~~~~~~

m_buff:			ds.b    0               ;read into here
m_title:		ds.b    26              ;title of this message
m_from: 		ds.b    21              ;who it's from
m_to:			ds.b    21              ;who it's to
m_time:			ds.b    11              ;the time
m_date:			ds.b    14              ;the date
m_width:		ds.b    1               ;40 or 80 columns
m_edited:		ds.b    1               ;edited?
m_lines:		ds.b    1               ;# lines this message is
m_node:			ds.b    2               ;node this msg originated from
m_nodename:		ds.b    26              ;node's name
m_nodecity:		ds.b    26              ;node's city
m_nodelist:		ds.b    256             ;nodes that this msg has appeared on
m_data:			ds.b    2200            ;the message text



			ds.b 0
win_end:
		.end

