;       SCCSID = @(#)msgclose.asm 6.17 92/03/26

        PAGE    ,132
;**********************************************************************
; MSGCLOSE.ASM  Close message files on request.
;**********************************************************************
;   Modification history
;
;     D-053583 11/24/92 fgw NEW FOR BORG
;                           For performance reasons, DOSGETMESSAGE leaves
;                           the message files open.  This module provides
;                           an entry point for closing all such files.
;**********************************************************************

.XALL
.LIST
.286c


        TITLE   Message Retriever - DosCloseMessageFile
        SUBTTL  Externals
        PAGE
;/********************** START OF SPECIFICATIONS **********************/
;/* SOURCE FILE NAME:  MSGCLOSE                                       */
;/*                                                                   */
;/* DESCRIPTIVE NAME:  CLOSE MESSAGE FILES                            */
;/*                                                                   */
;/* COPYRIGHT:  (TOP SOURCE FILE ONLY)                                */
;/*                                                                   */
;/* STATUS:  RELEASE 1 LEVEL 0                                        */
;/*                                                                   */
;/* FUNCTION:  CLOSE ALL OPEN MESSAGE FILES.                          */
;/*                                                                   */
;/* NOTES:  - THE HANDLE AND ALL DESCRIPTIVE INFORMATION FOR OPEN     */
;/*           MESSAGE FILES ARE CONTAINED IN THE INSTANCE SEGMENT.    */
;/*           THIS MODULE WALKS THE "SAVE" STRUCTURES TO CLOSE ALL    */
;/*           CURRENTLY OPEN FILES.                                   */
;/*                                                                   */
;/*         - "CLOSED" FILES ARE IDENTIFIED BY x00 IN THE FIRST       */
;/*           BYTE OF THE FILENAME_S FIELD.                           */
;/*                                                                   */
;/*    DEPENDENCIES:  NONE                                            */
;/*                                                                   */
;/*    RESTRICTIONS:  NONE                                            */
;/*                                                                   */
;/*    PATCH LABEL:  (TOP SOURCE FILE ONLY)                           */
;/*                                                                   */
;/* ENTRY POINTS:  DOSCLOSEMESSAGEFILE                                */
;/*                                                                   */
;/* INTERNAL  REFERENCES:                                             */
;/*                                                                   */
;/* EXTERNAL REFERENCES:  DOSCLOSE                                    */
;/*                       DOSSEMREQUEST                               */
;/*                       DOSSEMLEAVEE                                */
;/*********************** END OF SPECIFICATIONS ***********************/


;***********************************************
; External Procedures
;
        EXTRN   DOSCLOSE:FAR
        EXTRN   DOSFSRAMSEMCLEAR:FAR
        EXTRN   DOSFSRAMSEMREQUEST:FAR


;***********************************************
; External Data (in ISEG)
;
        EXTRN   GetFSRamSem:WORD        ; ram semaphore structure
        EXTRN   SAVED:WORD              ; First SAVE area
        EXTRN   SAVED_COUNT:WORD        ; SAVE area count


        SUBTTL  Includes
        PAGE
;***********************************************
;   INCLUDES HERE
;
        INCLUDE msgfequ.inc             ;EQUATES

.XLIST
        INCLUDE msgfmac.inc             ;MACROS
        INCLUDE basemaca.inc            ;DOS MACROS

        INCLUDE struc30.inc             ;Structured MACROS

        INCL_DOSSEMAPHORES EQU  1
        INCLUDE bsedos.inc
.LIST


        SUBTTL  Segment Definitions
        PAGE
;***********************************************
; DATA SEGMENT DEFINITIONS (Shared in DOSCALL1.DLL)
;
        INCLUDE DC1SEGS.INC


;***********************************************
; CODE SEGMENT DEFINITION
;
CODE    SEGMENT
        ASSUME  CS:CODE, DS:nothing, ES:nothing


PROCEDURE DOSCLOSEMESSAGEFILE, FAR



        SUBTTL   DosCloseMessageFile Main Routine
        PAGE
;/********************** START OF SPECIFICATIONS *********************/
;/* SUBROUTINE NAME:  DOSCLOSEMESSAGEFILE                            */
;/*                                                                  */
;/* DESCRIPTIVE NAME:  CLOSE MESSAGE FILES                           */
;/*                                                                  */
;/* FUNCTION:  CLOSE ALL OPEN MESSAGE FILES.                         */
;/*                                                                  */
;/* NOTES:  IF THERE ARE ANY "SAVED" MESSAGE FILES:                  */
;/*         1. LOCKUP INSTANCE SEGMENT ACCESS VIA SEMAPHORES         */
;/*         2. DO ALL NECESSARY CLOSES                               */
;/*         3. RELEASE THE SEMAPHORE                                 */
;/*                                                                  */
;/* RESTRICTIONS:  NONE                                              */
;/*                                                                  */
;/* ENTRY POINT:  DOSCLOSEMESSAGEFILE                                */
;/*    LINKAGE:   CALL FAR                                           */
;/*                                                                  */
;/* INPUT:  NONE                                                     */
;/*                                                                  */
;/* EXIT-NORMAL:  AX = 0                                             */
;/*                                                                  */
;/* EXIT-ERROR:   AX = 0                                             */
;/*                                                                  */
;/* EFFECTS:  MESSAGE FILES CLOSED                                   */
;/*                                                                  */
;/* INTERNAL REFERENCES:                                             */
;/*    ROUTINES:                                                     */
;/*                                                                  */
;/* EXTERNAL REFERENCES:                                             */
;/*    ROUTINES:  DOSFSRAMSEMCLEAR                                   */
;/*               DOSFSRAMSEMREQUEST                                 */
;/*    DATA: (FIELDS IN INSTANCE SEGMENT "iseg")                     */
;/*            GetFSRamSem                                           */
;/*            SAVED_COUNT                                           */
;/*                                                                  */
;/*********************** END OF SPECIFICATIONS **********************/

        ENTERPROC                       ;SETUP BP
        SAVE_CALLER                     ;save environment
        cld                             ;CLEAR DIRECTION FLAG

        mov     ax,iseg                 ;Instance Segment
        mov     ds,ax                    ;
        assume  ds:iseg                  ;

        .if     <[saved_count] NE 0>
          push    ds                      ;seg GetFSRamSem
          push    offset GetFSRamSem      ;off GetFSRamSem
          mov     ax, 0FFFFh              ;indefinite wait
          push    ax                       ;
          push    ax                       ;
          call    DOSFSRAMSEMREQUEST      ;GET FSRAM SEMAPHORE
          .if     <NONZERO ax>             ;
            jmp     short MSGC_EXIT        ; got a problem here
          .endif                           ;

          call    CLOSE_SAVED_FILES               ; Go close message files

          push    ds                      ;release the semaphore
          push    offset GetFSRamSem       ;
          CALL    DOSFSRAMSEMCLEAR         ;
          .if     <NONZERO ax>             ;
            jmp     short MSGC_EXIT        ; got a problem here
          .endif                           ;
        .endif


;***********************************************
;       Exit the CloseMessageFile Routine
;
MSGC_EXIT:
        RESTORE_CALLER

        ret


DOSCLOSEMESSAGEFILE ENDP
;=======================================


        SUBTTL  Loop to Close All OPEN message files
        PAGE
;/********************** START OF SPECIFICATIONS *********************/
;/* SUBROUTINE NAME:  CLOSE_SAVED_FILES                              */
;/*                                                                  */
;/* DESCRIPTIVE NAME:  CLOSE MESSAGE FILES                           */
;/*                                                                  */
;/* FUNCTION:  WALKS THE "SAVE" STRUCTURES TO:                       */
;/*            - IDENTIFY OPEN FILES                                 */
;/*            - CLOSE THEM                                          */
;/*            - TAG THE STRUCTURE AS BEING FREE (00 -> FILENAME_S)  */
;/*                                                                  */
;/* NOTES:                                                           */
;/*                                                                  */
;/* RESTRICTIONS: NONE                                               */
;/*                                                                  */
;/* ENTRY POINT:  CLOSE_SAVED_FILES                                  */
;/*    LINKAGE:   CALL NEAR                                          */
;/*                                                                  */
;/* INPUT:  NONE                                                     */
;/*                                                                  */
;/* EXIT-NORMAL:  N/A                                                */
;/*                                                                  */
;/* EXIT-ERROR:   N/A                                                */
;/*                                                                  */
;/* EFFECTS:  MESSAGE FILES CLOSED                                   */
;/*                                                                  */
;/* INTERNAL REFERENCES:                                             */
;/*    ROUTINES:                                                     */
;/*                                                                  */
;/* EXTERNAL REFERENCES:                                             */
;/*    ROUTINES:  DOSCLOSE                                           */
;/*    DATA: (FIELDS IN INSTANCE SEGMENT "iseg")                     */
;/*            LAST_SAVE_OFFSET                                      */
;/*            SAVED_COUNT                                           */
;/*                                                                  */
;/*********************** END OF SPECIFICATIONS **********************/

CLOSE_SAVED_FILES  PROC   NEAR
                   PUBLIC CLOSE_SAVED_FILES

        xor     bx, bx
        mov     si, offset SAVED        ; offset to 1st SAVE area
        mov     cx, SAVED_COUNT         ; SAVE area count

        .repeat
          mov     ah, byte ptr save.filename_s[si]
          .if     <NONZERO ah>
            mov     byte ptr save.filename_s[si], bh
            mov     ax, word ptr save.handle_s[si]
            push    ax
            call    DOSCLOSE
          .endif
          mov     si, word ptr save.next_save[si]
        .loop

        ret

CLOSE_SAVED_FILES  ENDP


CODE    ENDS
        END
