; /-----------------------------------------------------------------------/
; file:		netnames.s
; function:	set the names of your local network
; opcode:		
; part of:	Infitra
;
; author:		E Kuik
; company:	Merciful Software (1997)
; /-----------------------------------------------------------------------/

.include "xlib.i"
.include "tcpip.i"

sec	=	1
min	=	sec*60
hour	=	min*60
day	=	hour*24
week	=	day*7
year	=	week*52
decenium	=	year*10

.globl main
main:
	Cconws	#Hello		; hello world
	jsr	TCP_init		; step over this, debugger will crash
	if_false	d0,No_tcpip	; no transport layer
; get default path
	Dgetdrv
	move.w	d0,d7
	addq.w	#1,d0
	add.b	#"A",d7
	rol.w	#8,d7
	move.b	#":",d7
	lea	Path,a0
	move.w	d7,(a0)+
	move.b	#0,(a0)+
; create filename (sting.inf in auto folder)
	Merge_str	#Path,#Sting
	Fopen	#0,#Path
	move.w	d0,d7
	bmi	No_inf_file
	Fread	#Path,#128,d7
	tst.l	d0
	bmi	No_inf_file1
	Fclose	d7
; create normal path (localnet.tab in module folder)
	End_str	#Path,a0
no_slash:	move.b	-(a0),d1
	cmp.b	#"\",d1
	bne	no_slash
	clr.b	1(a0)
; create filename
	Merge_str	#Path,#Table
; find the file
	Fsfirst	#Path,#0
	if_nfalse	d0,No_table
	Fgetdta
	move.l	d0,a6
	Xmalloc	#mx_stram,26(a6),a5
	if_false	d0,No_mem
; Load the def file
	Fopen	#0,#Path
	move.w	d0,d7
	bmi	No_file
	Fread	a5,26(a6),d7
	tst.l	d0
	bmi	No_file1
	cmp.l	26(a6),d0
	bne	No_file1
	Fclose	d7
	move.l	a5,-(sp)
; transform tabs to spaces (could cause error so I remove tabs)
; is not really needed
	move.l	26(a6),d0
transform_tab:
	move.b	0(a5,d0.l),d1
	cmp.b	#9,d1
	bne.b	no_tab
	move.b	#$20,0(a5,d0.l)
no_tab:	subq.l	#1,d0
	bpl.b	transform_tab
; the first (resolve [first]) from the diagram
	resolve	#R_first,#Realname,#IP_list,#64,d7
	cmp.w	#E_CANTRESOLVE,d7
	beq	Load_cache
	lea	Realname,a0
	move.l	(a0),d0
	bne	_lookp
; Load the DNS cache
Load_cache:
	resolve	#R_load,#Realname,#IP_list,#64,d7
	cmp.w	#E_CANTRESOLVE,d7
	beq	Set_names
; check the names
Check:	resolve	#R_first,#Realname,#IP_list,#64,d7
Lookup:	cmp.w	#E_CANTRESOLVE,d7
	beq	Set_names
_lookp:	lea	Realname,a4
	tst.l	(a4)
	beq	Set_names
	Find_str	(a4),a5
	bne	Resolve_next
	move.l	a0,d0
	sub.l	a5,d0
nstart:	move.b	0(a5,d0.l),d1
	cmp.b	#10,d1
	beq	Disable_name
	subq.l	#1,d0
	bmi	Disable_name
	bra	nstart
Disable_name:
	move.b	#"#",1(a5,d0.l)
Resolve_next:
	resolve	#R_next,#Realname,#IP_list,#64,d7
	bra	Lookup
Set_names:
	move.l	a5,-(sp)
	lea	R_cname,a3
; now find the names
nxtname:	Find_str	a3,a5
	bne	Set_alias
; go to the start of the line
	move.l	a0,d0
	sub.l	a5,d0
To_lstart:
	move.b	0(a5,d0.l),d1
	cmp.b	#10,d1
	beq	Lstart
	subq.l	#1,d0
	bpl	To_lstart
Lstart:	addq.l	#1,d0
	lea	0(a5,d0.l),a4
	cmp.b	#"#",(a4)
	beq	nxtname
	lea	Path,a1
; copy the line
Linecopy:
.rept 10
	move.b	(a4)+,d1
	beq	Endline
	cmp.b	#13,d1
	beq	Endline
	move.b	d1,(a1)+
.endm
	bra	Linecopy
Endline:	move.b	#0,d1
	move.b	d1,(a1)+
	move.l	a4,a5
	End_str	#Path,a0
	move.b	#$20,(a0)+
	move.l	#year,d0
	move.l	#10,d1
	jsr	ltoa
	resolve	#Path,#Realname,#IP_list,#64,d7
	addq.w	#1,New_names
	bra	nxtname
Set_alias:
	cmp.l	#R_cname,a3
	bne	Save_cache
	move.l	(sp)+,a5
	lea	R_alias,a3
	bra	nxtname
Save_cache:
; Save DNS cache
	tst.w	New_names
	beq	No_new
	resolve	#R_save,#Realname,#IP_list,#64,d7
	Copy_str	#Saved_names1,#Path
	End_str	#Path,a0
	move.w	New_names,d0
	move.w	#10,d1
	jsr	itoa
	Merge_str	#Path,#Saved_names2
	Cconws	#Path
No_new:	Cconws	#Ready
	move.l	(sp)+,a5
	Mfree	a5
	Return	#0

No_tcpip:	Cconws	#No_tcpip_mess
	Return	#-1

No_table:	Cconws	#No_table_mess
	Return	#-2

No_mem:	Cconws	#No_mem_mess
	Return	#-3

No_file1:	Fclose	d7
No_file:	Cconws	#No_file_mess
	Mfree	a5
	Return	#-4

No_inf_file1:
	Fclose	d7
No_inf_file:
	Cconws	#No_inffile_mess
	Return	#-5

; general support routines taken from my library

.globl _Get_Cookie
_Get_Cookie:
	movem.l	a0-a2,-(sp)
	move.l	d0,-(sp)		; cookie to look for
	clr.l	-(sp)
	Super	#0
	move.l	d0,-(sp)		; save this
	movem.l	a0-a1,-(sp)	; "	"
	move.l	usp,a0
	lea	6(a0),a0		; our values
	tst.l	_p_cookies.w	; exist _p_cookie?
	beq	cookie_not_found		; no Jar!
	move.l	_p_cookies.w,a1
	move.l	4(a0),d0
cookie_nxt_cookie:
	tst.l	(a1)
	beq	cookie_not_found
	cmp.l	(a1),d0
	bne	cookie_nxt_slot
	move.l	4(a1),(a0)
	bra.b	cookie_found
cookie_nxt_slot:
	addq.l	#8,a1
	bra.b	cookie_nxt_cookie
cookie_not_found:
	clr.l	4(a0)
cookie_found:
	movem.l	(sp)+,a0-a1
	move.l	(sp)+,d0
	Super	d0
	move.l	(sp)+,d1
	move.l	(sp)+,d0
	movem.l	(sp)+,a0-a2
	rts

.globl _clear_str
_clear_str:
* vult een string met nullen.
          move.l	a0,-(sp)
clear_it:
.rept 10
          tst.b     (a0)
          beq.b     cleared
          clr.b     (a0)+
.endm
          bra.b     clear_it
cleared:
          move.l	(sp)+,a0
          rts

.globl _copy_str
_copy_str:
          clr.b     (a0)
          jmp	_merge_str

.globl _end_str
_end_str:
* zoekt het einde van een string
* a0 wijst op de nul
.rept 10
	tst.b	(a0)+
	beq	_End
.endm
	tst.b	(a0)+
	bne	_end_str
_End:	sub.l     #1,a0     ;adjust
	rts

.globl _find_env
_find_env:
          movem.l   d0/a1,-(sp)
          move.l    a0,d1
next_env:
          move.l    d1,-(sp)
          bsr.b     _find_str
          move.l    (sp)+,d1
          cmp.l     a0,d1
          bne       nuls                ;found
          addq.l    #1,d0
          move.l    d0,a1
          move.b    (a1),d0
          bne       next_env
nuls:
          movem.l   (sp)+,d0/a1
          rts

.globl _find_str
_find_str:
          movem.l   d1-d2/a1-a2,-(sp)
          move.l    a0,a2               ;save
          clr.l     d1
wrong:
          clr.l     d0
nch:
          move.b    0(a1,d1.l),d2
          beq.b     not_found
          cmp.b     0(a0,d0.l),d2
          bne.b     toggle_caps
correct:  addq.l    #1,d1
          addq.l    #1,d0
          move.b    0(a0,d0.l),d2
          beq.b     found
          bra.b     nch
toggle_caps:
	eor.b	#$20,d2
          cmp.b     0(a0,d0.l),d2
          beq.b	correct
next_char:
          addq.l    #1,d1
          bra.b     wrong
not_found:
          lea       0(a1,d1.l),a0
          exg       d0,a0
          move.l    a2,a0
          moveq	#1,d0
          bra.b     out_find
found:
	moveq	#0,d0
          lea       0(a1,d1.l),a0
out_find:
          movem.l   (sp)+,d1-d2/a1-a2
	tst.l	d0
          rts

.globl _lower_str
_lower_str:
* maakt van alle letters lower case
          movem.l   d0/a0,-(sp)
lower_it:
          move.b    (a0),d0
          beq.b     lower_done
          cmp.b     #$41,d0
          blt.b     skip_l
          cmp.b     #$5a,d0
          bgt.b     skip_l
          bset      #5,d0
skip_l:
          move.b    d0,(a0)+
          bra.b     lower_it
lower_done:
          movem.l   (sp)+,d0/a0
          rts

.globl _merge_str
_merge_str:
* voegt twee strings samen str2 -> str1
* str1 -> a0 ; str2 -> a1
          movem.l   d0/a0-a1,-(sp)
          bsr       _end_str   ;zoek einde eerste string
merge:
.rept 10
          move.b    (a1)+,d0
          move.b    d0,(a0)+
          beq.b     merged
.endm
          bra.b     merge
merged:   movem.l   (sp)+,d0/a0-a1
          rts

.globl _uper_str
_uper_str:
* maakt van alle letters upper case
          movem.l   d0/a0,-(sp)
uper_it:
          move.b    (a0),d0
          beq.b     uper_done
          cmp.b     #$61,d0
          blt.b     skip_u
          cmp.b     #$7a,d0
          bgt.b     skip_u
          bclr      #5,d0
skip_u:
          move.b    d0,(a0)+
          bra.b     uper_it
uper_done:
          movem.l   (sp)+,d0/a0
          rts

/* some general subroutines */
.globl _Xmalloc
_Xmalloc:	move.w	d1,-(sp)
	move.l	d0,-(sp)
	Sversion
	cmp.w	#$1900,d0
	bge.b	ndos
	move.l	(sp)+,d0
	move.w	(sp)+,d1
	bne	_Xmerror	; Zero return on other ten st ram requests
	Malloc	d0
	rts
ndos:	move.l	(sp)+,d0
	move.w	(sp)+,d1
	Mxalloc	d1,d0
	rts
_Xmerror:	moveq	#0,d0


.data
New_names:	dc.w	0

Sting:	dc.b	"\AUTO\STING.INF",0
Table:	dc.b	"LOCALNET.TAB",0

R_load:	dc.b	"[LOAD]",0
R_save:	dc.b	"[SAVE]",0
R_first:	dc.b	"[FIRST]",0
R_next:	dc.b	"[NEXT]",0
R_cname:	dc.b	"[CNAME]",0
R_alias:	dc.b	"[ALIAS]",0

linef:	dc.b	13,10,0

Hello:		dc.b	27,"p","Local Name Setter, (c) 1997 E.Kuik",27,"q",13,10,0
Ready:		dc.b	"Local names set...",13,10,0
Saved_names1:	dc.b	"Saved ",0
Saved_names2:	dc.b	" names in DNS cache.",13,10,0
No_tcpip_mess:	dc.b	"No TCP/IP overlay found...",13,10,0
No_table_mess:	dc.b	"Route table not found...",13,10,0
No_mem_mess:	dc.b	"Not enough memory to load table...",13,10,0
No_file_mess:	dc.b	"Error loading table...",13,10,0
No_inffile_mess:	dc.b	"Error loading STiNG Info...",13,10,0

.bss
.even
Path:	ds.b	128
Realname:	ds.b	64
IP_list:	ds.l	16