/******************************************************************************/
/* E! API management functions                                                */
/******************************************************************************/

#include <dos.h>
#include <stdio.h>
#include "api.h"

/******************************************************************************/
/*  This global variable is initialized to the default value used by E! to    */
/*  install the API interrupt handler. This value may be changed dynamical-   */
/*  ly using a Profile. So the API program must also change the interrupt     */
/*  number.                                                                   */
/******************************************************************************/
int E_Vec = 0xC0;

/**************************************************/
/* Call E! function #funcno and return error code */
/**************************************************/
char   Call_E_Function ( unsigned funcno )

{
 union REGS regs;

  regs.h.ah = 0;
  regs.x.bx = funcno;
  int86(E_Vec, &regs, &regs);
  return ( regs.h.al );
}

/******************************************************************************/
/*       Returns a FAR pointer to the requested variables block               */
/******************************************************************************/
void far *Request_E_Address ( char request_name )

{
 union  REGS  regs;
 struct SREGS sregs;

 if ((request_name < 1) || (request_name > 7))
    return (NULL);
  regs.h.ah = request_name;
  int86x(E_Vec, &regs, &regs, &sregs);
  return ( MK_FP (sregs.es, regs.x.bx) );
}

/******************************************************************************/
/*              Loads status members with appropriate values                  */
/******************************************************************************/
void   Get_E_Status ( STATUSRECORD *status)

{
 union REGS regs;

 regs.h.ah = STATUS_REQUEST;
 int86(E_Vec, &regs, &regs);
 status->linenum = regs.x.cx;
 status->line    = regs.x.dx;
 status->col     = regs.h.ah;
 status->firstcol= regs.h.al;
 status->lower   = regs.h.bh;
 status->nbwin   = regs.h.bl;
}

/******************************************************************************/
/*                Request a special E! API service                            */
/******************************************************************************/
void   Request_E_Service ( char service)

{
 union REGS regs;

 regs.h.ah = service;
 int86(E_Vec, &regs, &regs);
}

/******************************************************************************/
/*                   Get E! version number                                    */
/******************************************************************************/
void   Get_E_Version ( char *major, char *minor)

{
 union REGS regs;

 regs.h.ah = VERSION_SERVICE;
 int86(E_Vec, &regs, &regs);
 *major = regs.h.ah;
 *minor = regs.h.al;
}

/******************************************************************************/
/*                Instruct E! to execute a command on return                  */
/******************************************************************************/
void   RegisterCommand ( commandline commandstr )

{
 union  REGS  regs;
 struct SREGS sregs;

 regs.h.ah = REGISTER_SERVICE;
 sregs.es  = FP_SEG ( commandstr );
 regs.x.bx = FP_OFF ( commandstr );
 int86x(E_Vec, &regs, &regs, &sregs);
}

/******************************************************************************/
/*                Call E! seek function and return error code                 */
/******************************************************************************/

char   Seek_E_Line ( unsigned line, char curpos)

{
 union REGS regs;

 regs.h.ah = SEEK_SERVICE;
 regs.x.bx = line;
 regs.h.al = curpos;
 int86(E_Vec, &regs, &regs);
 return (regs.h.al);
}

/******************************************************************************/
/*       Instruct E! to allocate memory on its own heap for the program.      */
/******************************************************************************/
BOOL   Get_E_Memory( unsigned paragraphs,
                     unsigned memID,
                     void **blockptr, /* blockptr is a pointer to a far pointer   */
                     BOOL *firsttime) /* that is, it's the address of the pointer */
                                      /* to the variables block.                  */

{
 union  REGS  regs;
 struct SREGS sregs;

 regs.h.ah = GETMEM_SERVICE;
 regs.x.dx = memID;
 regs.x.cx = paragraphs;
 int86x(E_Vec, &regs, &regs, &sregs);
 *blockptr = regs.h.al == 0 ? MK_FP (sregs.es, regs.x.bx) : NULL;
 *firsttime = regs.h.ah;
 return (regs.h.al ^ 0xff);
}

/******************************************************************************/
/*                       Free memory on E! heap                               */
/******************************************************************************/
BOOL   Free_E_Memory ( unsigned memID )

{
 union REGS regs;

 regs.h.ah = FREEMEM_SERVICE;
 regs.x.dx = memID;
 int86(E_Vec, &regs, &regs);
 return (regs.h.al ^ 0xff);
}

BOOL   getflagstatus (unsigned flagid)

{
 union REGS regs;

 regs.h.ah = flagid;
 int86(E_Vec, &regs, &regs);
 return (regs.h.al);
}

/******************************************************************************/
/*                           Is E! idle ?                                     */
/******************************************************************************/
BOOL   Is_E_Idle ()

{
 return ( getflagstatus (E_IDLE_SERVICE) );
}

/******************************************************************************/
/*                        Was the file modified ?                             */
/******************************************************************************/
BOOL   Is_File_Modified ()

{
 return ( getflagstatus (MODIF_SERVICE) );
}
