/******************************************************
 * Listing 1  hc_api.c
 *
 * Functions to handle calls to HLLAPI.
 *
 *****************************************************/

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include "hc.h"

/*****************************************************
 *
 * connect_ps()  Connect presentation space.
 *               Uses the PSID.
 * return: 0 = successful
 *         1 = invalid PSID
 *         4 = successful, but host busy
 *         5 = successful, but input inhibited
 *         9 = system error
 *         11= resource unavailable
 *             (only under IBM Entry Level Emulator)
 *
 *****************************************************/

int connect_ps (void)
{
        char *api_str = "A";    /* IBM PC/3270  */
        int api_len   = 1;      /* 1 is implied */
        int api_retc  = 0;      /* return code */
        int api_func  = 1;      /* connect pres space */
        if (hllapi_level >=4)
                return (0);        /* simulated */
        else if (hllapi_level == 1)
                *api_str = 'E';    /* IBM Entry Level */
        else if (hllapi_level == 2)
                *api_str = 'B';    /* Attachmate */
        else if (hllapi_level == 3)
                *api_str='1';      /* RabbitGATE */

        HLLC(&api_func, api_str, &api_len, &api_retc);

        return(api_retc);
}

/*****************************************************
 *
 * send_key()  Send a string of keystrokes to the host
 *
 * return: 0 = successful
 *         1 = not connected to a host session
 *         2 = invalid parameter passed to HLLAPI
 *         4 = host busy, not all keystrokes sent
 *         5 = input inhibited or rejected
 *         9 = system error
 *
 *****************************************************/

int send_key (char *keys)
{
        int api_len;            /* no. chars to send */
        int api_retc  = 0;      /* return coded */
        int api_func  = 3;      /* send key */
        if (hllapi_level >=4) return (0);

        api_len = strlen(keys);

        HLLC(&api_func, keys, &api_len, &api_retc);

        return(api_retc);
}

/*****************************************************
 *
 * copy_ps()  Copy presentation space to ps.
 *
 * return: 0 = successful
 *         1 = not connected to a host session
 *         4 = successful, waiting for host response
 *         5 = successful, keyboard is locked
 *         9 = system error
 *
 *****************************************************/

int copy_ps (char *ps)
{
        int api_len   = 0;      /* length implied */
        int api_retc  = 0;      /* return code */
        int api_func  = 5;      /* copy pres space */
        if (hllapi_level >=4) return (0);

        HLLC(&api_func, ps, &api_len, &api_retc);

        return(api_retc);

}

/*****************************************************
 *
 * search_ps()  Search presentation space for first
 *              occurance of str.
 *
 * If str_pos == 0, string not found, otherwise,
 * str_pos is the offset of the string in the pres.
 * space. First position in the pres. space is 1; not
 * the C convention of 0.
 *
 * return: 0 = successful
 *         1 = not connected to a host session
 *         2 = error in specifying parameters
 *         9 = system error
 *
 *****************************************************/

int search_ps(char *str, int *str_pos)
{
        int api_func = 6;     /* search pres. space */

        /* api_len contains length of search string on
         * call; and position of string on return
         */
        int api_len;

        int api_retc = 0;       /* return code */
        if (hllapi_level >=4) {
                *str_pos = 0;
                return (0);
        }

        api_len = strlen(str);

        HLLC(&api_func, str, &api_len, &api_retc);

        /* 0 and 24 indicate successful execution of
         * function
         */
        if (api_retc == 0 || api_retc == 24) {
                *str_pos = api_len;
                return(0);
        }

        return(api_retc);
}


/*****************************************************
 *
 * reset()  Reset host connection and disconnect
 *          presentation space.
 *
 * return: 0 = successful
 *         9 = system error
 *
 *****************************************************/

int reset(void)
{
        char *api_str = "";     /* not required */
        int api_len   = 0;      /* not required */
        int api_retc  = 0;      /* return code */
        int api_func  = 21;     /* reset system */
        if (hllapi_level >=4) return (0);

        HLLC(&api_func, api_str, &api_len, &api_retc);

        return(api_retc);
}


/*****************************************************
 *
 * wait()  Wait until host is not busy, i.e., the
 *         communication line is clear.
 *
 * return: 0 = system ready for input
 *         1 = not connected to a host session
 *         4 = timeout while waiting, system not ready
 *             (1 minute by default)
 *         5 = keyboard locked
 *         9 = system error
 *
 *****************************************************/

int wait (void)
{
        char *api_str = "";     /* not required */
        int api_len   = 0;      /* not required */
        int api_retc  = 0;      /* return code */
        int api_func  = 4;      /* wait */
        if (hllapi_level >=4) return (0);

        HLLC(&api_func, api_str, &api_len, &api_retc);

        return(api_retc);
}


/*****************************************************
 *
 * dspy_cursor() Determine cursor position and display.
 *
 * Uses 2 HLLAPI calls: query cursor location (7) and
 * convert cursor position (99).  Function 99 uses
 * the PSID.
 *
 * return: 0 = successful
 *         1 = session not connected
 *         2 = invalid PSID or data string
 *         9 = system error encountered
 *
 *****************************************************/

int dspy_cursor (void)
{
        /* query cursor location */
        char api_str[3] = "";   /* not req for func 7;
                                 * req for 99 */
        int api_len     = 0;    /* not required */
        int api_retc    = 0;    /* return code */
        int api_func    = 7;    /* query cursor loc */
        if (hllapi_level >=4) return (0);

        HLLC(&api_func, api_str, &api_len, &api_retc);

        if (api_retc != 0)
                return(api_retc);

        /* Convert cursor positon to row/column.  For
         * function 99 the data string consists of
         * the PSID and the letter P when converting
         * from host pres space to row/column
         */

        if (hllapi_level == 0)
                strcpy(api_str, "AP"); /* IBM PC/3270 */
        else if (hllapi_level == 1)
                strcpy(api_str, "EP"); /* IBM Entry  */
        else if (hllapi_level == 2)
                strcpy(api_str, "BP"); /* Attachmate */
        else
                strcpy(api_str, "1P"); /* RabbitGATE */

        api_func = 99;          /* Convert position */
        api_retc = api_len;     /* host pres. position
                                 * passed in 4th parm
                                 */

        HLLC(&api_func, api_str, &api_len, &api_retc);

        switch (api_retc) {
                case 0:    /* bad column or PS psn */
                case 9998: /* bad PSID or not con'd */
                case 9999:/* 2nd char not P or R */
                        return (2);
                default:
                        gotoxy(api_retc, api_len);
        }
        return (0);
}


/*****************************************************
 *
 * copy_oia() Copy the Operator Information Area to
 *            api_str.
 *
 * Function must be called with pointer to at least
 * 103 bytes--the length of the returned string.
 *
 *      0 = successful
 *      1 = not connected to a host session
 *      4 = host busy
 *      5 = keyboard locked
 *      9 = system error
 *
 *****************************************************/

int copy_oia (unsigned char *api_str)
{
        int api_len   = 103;    /* length of OIA str */
        int api_retc  = 0;      /* return code */
        int api_func  = 13;     /* Copy OIA */
        if (hllapi_level >=4) return (0);

        HLLC(&api_func, api_str, &api_len, &api_retc);

        return(api_retc);
}
