;
;	Startup code for Editor.mcc
;

VERSION:	equ 1		;Remember to set the ID string also
REVISION:	equ 1

execbase:	equ 4

	include "exec/exec.i"

	XREF _class_init	;These are in libinit.c
	XREF _class_term
	XREF _mcc

start:
	moveq.l	#-1,d0		;Dumb code for dumb users
	rts

romtag:	dc.w	RTC_MATCHWORD	;The romtag - very important
	dc.l	romtag
	dc.l	endcode
	dc.b	RTF_AUTOINIT
	dc.b	VERSION
	dc.b	NT_LIBRARY
	dc.b	0
	dc.l	libname
	dc.l	libid
	dc.l	inittable
libname:	dc.b "Editor.mcc",0
		dc.b "$VER: "
libid:		dc.b "Editor.mcc V1.1 (8/10/95)",0
	even

inittable:
	dc.l	LIB_SIZE
	dc.l	functable
	dc.l	datatable
	dc.l	initroutine

functable:
	dc.l open		;The functions this library supports
	dc.l close
	dc.l expunge
	dc.l null
	dc.l MCC_GetClass
	dc.l -1

datatable:			;We're using autoinit...
	INITBYTE	LN_TYPE,NT_LIBRARY
	INITLONG	LN_NAME,libname
	INITBYTE	LIB_FLAGS,LIBF_SUMUSED|LIBF_CHANGED
	INITWORD	LIB_VERSION,VERSION
	INITWORD	LIB_REVISION,REVISION
	INITLONG	LIB_IDSTRING,libid
	dc.l		0


;	library is initialized and added to the system list

initroutine:
	movem.l	d1-d7/a0-a6,-(sp)
	move.l	d0,a6
	move.l	a0,seglist
	jsr	_class_init	;Call code from libinit.c
	tst.l	d0
	bne	init_failed
init_ok:
	move.l	a6,d0
	movem.l	(sp)+,d1-d7/a0-a6
	rts
init_failed:
	move.l	#0,d0
	movem.l	(sp)+,d1-d7/a0-a6
	rts


;	open,close,expunge,null standard functions

open:	cmp.l	#VERSION,d0
	bcs	open_failed	;We need a higher version!
	add.w	#1,LIB_OPENCNT(a6)
	bclr	#LIBB_DELEXP,LIB_FLAGS(a6)
	move.l	a6,d0
	rts
open_failed:
	move.l	#0,d0
	rts

close:
	move.l	#0,d0
	sub.w	#1,LIB_OPENCNT(a6)
	bne	close_end
	btst	#LIBB_DELEXP,LIB_FLAGS(a6)
	bne	expunge			;opencount=0, wants an expunge...
close_end:
	rts

expunge:
	movem.l	d1-d7/a0-a6,-(sp)
	tst.w	LIB_OPENCNT(a6)
	bne	expunge_failed		;Library is still open
	move.l	a6,a1
	REMOVE				;Remove lib (in a1) from list
	move.l	#0,d0
	move.l	a6,a1
	move.w	LIB_NEGSIZE(a6),d0
	sub.l	d0,a1
	add.w	LIB_POSSIZE(a6),d0
	move.l	execbase,a6
	jsr	_LVOFreeMem(a6);	;Free base memory
	move.l	seglist,d0		;Return value
	movem.l	(sp)+,d1-d7/a0-a6
	rts
expunge_failed:
	bset	#LIBB_DELEXP,LIB_FLAGS(a6)	;Set flag for delayed exp.
	movem.l	(sp)+,d1-d7/a0-a6
null:					;This is also the NULL function...
	move.l	#0,d0
	rts

MCC_GetClass:
	move.l	_mcc,d0			;Returns the MUI_CustomClass ptr
	rts

seglist:	dc.l 0
endcode:
