* Database example machine code program.
* Example 2: Demonstrate EXPORT by means of a job.
* Uses MCTEST_DBS generated in TEST_MC1_ASM
         INCLUDE  flp2_QDOS_TRAP_IN
         INCLUDE  flp2_QDOS_VECT_IN
         DATA     1024
SD.CURE  EQU      $E
SD.CURS  EQU      $F
*
         INCLUDE  flp2_QDOS_DATA_IN
* Organise layout of dataspace.
         OFFSET   0
WIND     DS.L     1                 Screen window channel ID
DSBRT    DS.L     1                 Database manager vector
DCHAN    DS.L     1                 Database channel ID
DBASE    DS.L     1                 Database ID
EXPORT   DS.L     1                 Export channel ID
WORK
*
         SECTION  MAIN
* Job header:
         BRA.L    START
         DC.W     0,$4AFB,11,'EXPORT Demo'
* Various pieces of data
FILENAME DC.W     15,'FLP2_MCTEST_DBS'  File name
EXPTNAME DC.W     15,'FLP2_MCTEST_EXP'  Export file name
DBOR.NUM EQU      1                 Number of order parameters
DBORDER  DC.W     1,1               Order field on 1 ascending
*
WINDOW   DC.B     7,1,0,4               Window definition
         DC.W     224,186,288,26

*                        123456789 123456789 123456789 12345
MESSE    DC.W        22,'Press any key to exit '
MESS1    DC.B     00,22,'EXPORT demonstration.',$0A
         DS.W     0                 Align on a word boundary
MESS2    DC.W        33,'Overwrite FLP2_MCTEST_EXP (Y/N)? '
MESS3    DC.B     00,25,'Contents of export file:',$0A
         DC.W     0
MESS4    DC.B     00,15,'Release memory',$0A
         DS.W     0
MESS5    DC.B     00,16,'Allocate memory',$0A
         DS.W     0
* Start by clearing my dataspace
START
         LEA      0(A6,A5.L),SP     Set SP to top of dataspace
         LEA      0(A6,A4.L),A6     Set A6 to bottom
         MOVE.L   A6,A4
CLR_LOOP CLR.B    (A4)+             Clear it out
         CMPA.L   A4,SP
         BNE.S    CLR_LOOP
* Open a window to use for user IO
         LEA      WINDOW(PC),A1
         MOVE.W   UT.CON,A2         Open window
         JSR      (A2)
         BEQ.S    PRT_MES1
         BRA.S    SUICIDE

* Print error message and wait for keypress
SUI_PRT
         MOVE.L   WIND(A6),A0
         MOVE.W   UT.ERR,A2         Print the error message
         JSR      (A2)
* Exit point to quit
SUI_QUIT
         MOVE.L   WIND(A6),A0
         MOVE.L   D0,-(SP)          Save error code
         LEA      MESSE(PC),A1      Print error quit message
         MOVE.W   UT.MTEXT,A2
         JSR      (A2)              (Returns D3=-1)
         BNE.S    SUI_CONT

         MOVEQ    #SD.CURE,D0       Enable cursor
         TRAP     #3

         MOVEQ    #IO.FBYTE,D0      Wait for keypress
         TRAP     #3
SUI_CONT
         MOVE.L   (SP)+,D0
* Quit program
SUICIDE
         MOVE.L   D0,D3             Return error message to activator job
         MOVEQ    #-1,D1            Remove myself
         MOVEQ    #MT.FRJOB,D0
         TRAP     #1
* Send message 1 to window
PRT_MES1
         MOVE.L   A0,WIND(A6)
         LEA      MESS1(PC),A1
         MOVE.W   UT.MTEXT,A2
         JSR      (A2)
         BNE.S    SUICIDE
* Open export channel - Not overwrite
         LEA      EXPTNAME(PC),A0   Filename ptr
         MOVEQ    #-1,D1            For myself
         MOVEQ    #IO.NEW,D3        New file
         MOVEQ    #IO.OPEN,D0       Open the channel
         TRAP     #2
         TST.L    D0
         BEQ.S    OP_DB_OK          Jump if opened OK
         CMPI.L   #ERR.EX,D0        Quit if error not "already exists"
         BNE.S    SUI_PRT
* Print "overwrite?" question, and get answer.
         MOVE.L   WIND(A6),A0
         LEA      MESS2(PC),A1
         MOVE.W   UT.MTEXT,A2       Print message
         JSR      (A2)
         BNE.S    SUICIDE
*
         MOVEQ    #SD.CURE,D0       Enable cursor
         TRAP     #3
*
FT_OVERQ
         MOVEQ    #IO.FBYTE,D0      Fetch byte
         TRAP     #3
         TST.L    D0
         BNE.S    SUI_PRT
*
         MOVEQ    #ERR.EX,D0
         BCLR     #5,D1             Make uppercase
         CMPI.B   #'Y',D1
         BEQ.S    FT_OVERW          Overwrite if Y pressed
         CMPI.B   #'N',D1
         BEQ.S    SUI_PRT           Exit if N or ESC pressed
         CMPI.B   #$27,D1
         BEQ.L    SUI_PRT
         BRA.S    FT_OVERQ          Otherwise get another char.
* Print Y<LF>, turn cursor off.
FT_OVERW
         MOVEQ    #IO.SBYTE,D0      PRINT Y
         TRAP     #3
         TST.L    D0
         BNE.L    SUI_PRT

         MOVEQ    #SD.CURS,D0
         TRAP     #3

         MOVEQ    #$0A,D1           PRINT LF
         MOVEQ    #IO.SBYTE,D0
         TRAP     #3
         TST.L    D0
         BNE.L    SUI_PRT
* Open the file OVERWRITE if possible.
         LEA      EXPTNAME(PC),A0   Filename ptr
         MOVEQ    #-1,D1            For myself
         MOVEQ    #IO.OVERW,D3      Overwrite file
         MOVEQ    #IO.OPEN,D0       Open the channel
         TRAP     #2
         TST.L    D0
         BNE.L    SUI_PRT
OP_DB_OK
         MOVE.L   A0,EXPORT(A6)     Save channel ID
* Now open database file
         LEA      FILENAME(PC),A0
         MOVEQ    #-1,D1
         MOVEQ    #IO.SHARE,D3      Open Read Only
         MOVEQ    #IO.OPEN,D0
         TRAP     #2
         TST.L    D0
         BNE.L    SUI_PRT
         MOVE.L   A0,DCHAN(A6)      Save channel ID
* Now find out database manager
         MOVEQ    #-1,D3
         MOVEQ    #FS.DBASE,D0      Get the vector
         TRAP     #3
         TST.L    D0
         BNE.L    SUI_PRT
         MOVE.L   A1,DSBRT(A6)      Save vector address
         MOVE.L   A1,A4
* Now OPEN a database
* Use special memory routines: set A2 to address
         LEA      MEMORY(PC),A2     Memory allocate/release address
         JSR      FSD.OPEN(A4)
         BNE.L    SUI_PRT
         MOVE.L   A0,DBASE(A6)      Save database address
* Order the database
         MOVEQ    #DBOR.NUM,D1
         LEA      DBORDER,A1
         JSR      FSD.ORDR(A4)
         BNE.L    SUI_PRT
* Now EXPORT the 1st,2nd,2nd fields
         MOVE.L   EXPORT(A6),A1     Get export channel ID
         LEA      EXLIST(PC),A2     Get export parameter list
         JSR      FSD.EXPT(A4)
         BNE.L    SUI_PRT
* Send "contents of export file" message
         MOVE.L   WIND(A6),A0
         LEA      MESS3(PC),A1
         MOVE.W   UT.MTEXT,A2       Write message
         JSR      (A2)
         BNE.L    SUI_PRT           Returns D3.W=-1
* Now dump the export file to the window
         MOVE.L   A0,A2
         MOVE.L   EXPORT(A6),A0
         MOVEQ    #0,D1
         MOVEQ    #FS.POSAB,D0      First rewind the file
         TRAP     #3
         TST.L    D0
         BNE.L    SUI_PRT
*
COPYLOOP
         MOVEQ    #IO.FBYTE,D0      Get an export file byte
         TRAP     #3
         CMPI.L   #ERR.EF,D0        finished if EOF
         BEQ.S    DONE
         TST.L    D0
         BNE.L    SUI_PRT
*
         EXG      A0,A2
         MOVEQ    #IO.SBYTE,D0      Print the byte to the screen
         TRAP     #3
         TST.L    D0
         BNE.L    SUI_PRT
         EXG      A0,A2
         BRA.S    COPYLOOP
DONE
         MOVE.L   WIND(A6),A0
         MOVEQ    #$0A,D1
         MOVEQ    #IO.SBYTE,D0
         TRAP     #3
         MOVEQ    #$0A,D1
         MOVEQ    #IO.SBYTE,D0
         TRAP     #3
         BRA.L    SUI_QUIT
* Export parameter list
EXLIST   DC.W     3                 3 Names in name list
         DC.W     5,'IntW1'           1st Occurrence of field 1
         DC.W     7,'IntW2n1'         1st Occurrence of field 2
         DC.W     7,'IntW2n2'         2nd Occurrence of field 2
         DC.W     0                 End marker
         DC.W     3                 3 Fields in field list
         DC.W     1,2,2
         DC.W     0                 End marker
* Memory allocation routines
MEMORY   BRA.S    ALLOC
         NOP
         NOP
* Release memory (code at MEMORY+6)
         MOVE.L   A0,-(SP)
         MOVE.L   WIND(A6),A0
         LEA      MESS4(PC),A1
         MOVE.W   UT.MTEXT,A2       Print that this is happening
         JSR      (A2)
*
         MOVE.L   (SP)+,A0
         MOVEQ    #MT.RECHP,D0
         TRAP     #1
         RTS
* Allocate memory
ALLOC
         MOVEM.L  D1/D2,-(SP)
         MOVE.L   WIND(A6),A0
         LEA      MESS5(PC),A1
         MOVE.W   UT.MTEXT,A2       Print that this is happening
         JSR      (A2)
*
         MOVEM.L  (SP)+,D1/D2
         MOVEQ    #MT.ALCHP,D0
         TRAP     #1
         RTS
         END
