#define INCL_BASE
#include <os2.h>
#include "bdcalls.h"
#include "bdatt.h"
#include "blkapifn.h"


#define ESCAPE          27
#define SEQID           43
#define MINSEQLENGTH    8

/*********************START OF SPECIFICATIONS****************************/
/* SUBROUTINE  NAME:  NLSSETBIDIPRINT                                   */
/*                                                                      */
/*  COPYRIGHT: (c) IBM Corp. 1989                                       */
/*                                                                      */
/* DESCRIPTIVE NAME:  Set Bidirectional attributes for printing         */
/*                                                                      */
/* FUNCTION: Writes an Escape sequence of known length to a file        */
/*           which is to be printed using the input file handle and     */
/*           bidirectional attributes.  This sequence is to be          */
/*           consumed by the printer monitor to produce the             */
/*           appropriate format for the file while printing             */
/*                                                                      */
/* ENTRY POINT: NlsSetBidiPrint                                         */
/*    LINKAGE : CALL FAR (dynamic link API)                             */
/*                                                                      */
/* INPUT:   (BiDiAtts,Length,Handle)                                    */
/*          BiDiAtts  DWORD   - Bidirectional attributes                */
/*          Length    DWORD   - Length of the sequence starting from    */
/*                              length to the end of the Escape sequence*/
/*                              (i.e. Length+BiDiAtts)                  */
/*          Handle    WORD    - Output file handle                      */
/*                                                                      */
/* EXIT-NORMAL:  RC=0                                                   */
/*                                                                      */
/* EXIT-ERROR:  RC = ERROR_BIDI_INVALID_LENGTH      -invalid length     */
/*              RC = ERROR_BIDI_RW_INCOMPLETE       -read/write error   */
/*              RC = ERROR_BIDI_INVALID_CSD         -invalid CSD        */
/*              RC = ERROR_BIDI_INVALID_ORIENTATION -invalid orientation*/
/*              RC = ERROR_BIDI_INVALID_NUM_SHAPE   -invalid numerics   */
/*              RC = ERROR_BIDI_INVALID_LEVEL       -invalid level      */
/*              RC = ERROR_BIDI_INVALID_TYPE_SUPPORT-invalid support    */
/*                                                                      */
/* INTERNAL REFERENCES:                                                 */
/*   LOCAL DATA DEFINITION:                                             */
/*       CHAR     @seqbuffer      - buffer for ESC sequence             */
/*       DWORD    RC              - return code from API                */
/*       WORD     i               - loop counter                        */
/*                RCDosWrite         - return code of DosWrite          */
/*                bytestowrite    - no. of bytes to be written          */
/*                                  by DosWrite                         */
/*                byteswritten    - no. of actual bytes written         */
/*                                  by DosWrite                         */
/*                                                                      */
/*                                                                      */
/*   GLOBAL DATA DEFINITION:    NONE                                    */
/*   ROUTINES:                  RCBiDiAttr - performs error checking    */
/*                                          the fields of BiDiAttributes*/
/*                              GetByte    - gets a specific byte of    */
/*                                           BiDiAtts                   */
/* EXTERNAL REFERENCES:                                                 */
/*   ROUTINES:                  DosWrite                                */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/***************************END OF SPECIFICATIONS************************/
USHORT far pascal _loadds NlsSetBidiPrint ( ULONG BidiAtts,
                                            ULONG Length,
                                            HFILE FileHandle)

{
   char          seqbuffer[10];   /* buffer of escape sequence   */
   USHORT        RC;              /* return code from RCBiDiAttr */
   int           i,               /* counter                     */
                 RCDosWr;         /* return code from DosWrite   */

   USHORT bytestowrite;      /* number of bytes to be written  */
   USHORT byteswritten;      /* number of bytes written (returned)*/

   /******************Start of error checking***************************/

        /**** this part is added to conforme with the DCR 1193 ***/

        BDPRCB retarea;
        RC = NlsQueryBidiAtt(0l,(PRETINFO) &retarea);
           if (RC==ERROR_BIDI_NO_SUPPORT)
              return RC;
          else
              RC=0;
       /********************* 19 -12- 1990 **********************/


   if (Length<MINSEQLENGTH)                /* checking on Length */
       return ERROR_BIDI_INVALID_LENGTH;

   if ( (RC=(USHORT)RCBiDiAttr(BidiAtts))!=0 )     /* checking on BidiAtts */
       return RC;
   /******************End  of error checking***************************/

   /************Start of forming the escape sequence*******************/

    seqbuffer[0] = ESCAPE;
    seqbuffer[1] = SEQID;

    for (i=0;i<4 ;i++)           /* place Length in buffer byte by byte */
       seqbuffer[i+2] = GetByte((char *)&Length,i);

    for (i=0;i<(int)Length-4 ;i++)/* place BidiAtts in buffer byte by byte */
       seqbuffer[i+6] = GetByte((char *)&BidiAtts,i);

   /************End of forming the escape sequence*******************/

   bytestowrite= (int)Length+2;         /* bytes to be written to file */

   RCDosWr = DosWrite(FileHandle,seqbuffer,bytestowrite,&byteswritten);

   if (RCDosWr)
       return (RCDosWr);

/* if  (byteswritten!=bytestowrite)
     return    ERROR_BIDI_RW_INCOMPLETE; */

/* this condition will not happen because if the RW is incomplete  */
/* DosWrite will return an error and the API will exit */

   return 0;                        /* operation completed successfully */
}
