home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0090 - 0099 / ibm0090-0099 / ibm0099.tar / ibm0099 / SAMV11-1.ZIP / API / SAMLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-23  |  4.3 KB  |  160 lines

  1. /*
  2.  * samlib.c
  3.  *
  4.  * This contains functions to init and access the API contained
  5.  * in resident program SAMAPI.EXE.
  6.  *
  7.  * SAMAPI.EXE is a resident program that provides an application program
  8.  * interface to the SAM Amateur Radio Callsign Database.
  9.  *
  10.  * Three functions in here:
  11.  *  LocateSam     - determines SAMAPI is loaded
  12.  *  CallSam       - call the API, passing a command parameter block and
  13.  *                  a buffer for result/response.
  14.  *  SetCountrySam - set the country for search, 0 = USA, 1 = Canada
  15.  *                  Note that normally automatic for simple call lookup
  16.  *
  17.  * The API is accessed via int 2fh, ah = dynamic muxid (default is 0x99).
  18.  * This is commonly called the "multiplex interrupt" interface.
  19.  * Per convention, int 2fh, ah = muxid, al = 0 is an install check, and
  20.  * al comes back non-zero if the API is installed.  For other API
  21.  * functions:
  22.  *
  23.  *      ah = muxid
  24.  *      al = 1
  25.  *   ds:si = pointer to command block
  26.  *   es:di = pointer to result block
  27.  *
  28.  * No registers are changed by SAMAPI (except al when input al == 0)
  29.  *
  30.  * See samapi.h for details on the command/response data structures,
  31.  * command codes, error codes, and so on.
  32.  *
  33.  * LocateSam looks for an environment string: SAMAPI=number.
  34.  * If found, "number" becomes the muxid.  SAMAPI.EXE does the same thing
  35.  * when it loads so muxid can be changed when conflicts arise.
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <dos.h>
  42.  
  43. #include "samapi.h"
  44.  
  45.  
  46. int SamMuxid = DEFAULT_SAMMUX;
  47. int SamCountry = 0;                /* 0 == USA, 1 == Canada */
  48.  
  49. /**************************
  50.  * LocateSam
  51.  **************************
  52.  *
  53.  * Finds the resident SAMAPI code.  This MUST be called before
  54.  * other functions are used!.  It checks for the environment
  55.  * string SAMAPI=number to see if there is an override for the
  56.  * muxid (SAMAPI.EXE does the same thing).
  57.  *
  58.  * returns: 0 if resident code found and can be used, -1 if not
  59.  */
  60.  
  61. int LocateSam(void)
  62. {
  63.     int v;
  64.     char *p;
  65.     static union REGS r;
  66.  
  67.     p = getenv("SAMAPI");
  68.     if (p != NULL)
  69.     {
  70.         v = atoi(p);
  71.         if (v > 0 && v < 255)
  72.             SamMuxid = v;
  73.     }
  74.     r.h.al = 0;
  75.     r.h.ah = SamMuxid;
  76.     int86(0x2f, &r, &r);
  77.     return (r.h.al == 1) ? 0 : -1;
  78. }
  79.  
  80. /**************************
  81.  * CallSam
  82.  **************************
  83.  *
  84.  * Call the resident SAMAPI code.
  85.  *
  86.  * This expects a filled-in (all but header) command buffer
  87.  * and a to-be-filled-in response buffer.  cmd is stuffed into
  88.  * cmdbuf and the resident code is called (via int 0x2f).
  89.  *
  90.  * returns: error code from response buffer header (0 usually
  91.  *          means no error)
  92.  */
  93.  
  94. int CallSam(int cmd, void far *cmdbuf, void far *rspbuf)
  95. {
  96.     static union REGS r;
  97.     static struct SREGS sr;
  98.     int i;
  99.  
  100.     ((chdr_t far *) cmdbuf)->cmd = cmd;
  101.  
  102.     /* make reserved fields 0 for future compatability. */
  103.     for (i = 0; i < 2; i++)
  104.         ((chdr_t far *) cmdbuf)->fill[i] = 0;
  105.  
  106.     /* select which database */
  107.     ((chdr_t far *) cmdbuf)->country = SamCountry;
  108.  
  109.     r.x.si = FP_OFF(cmdbuf);        /* ds:si is ptr to command */
  110.     sr.ds = FP_SEG(cmdbuf);
  111.     r.x.di = FP_OFF(rspbuf);        /* es:di is ptr to result buf */
  112.     sr.es = FP_SEG(rspbuf);
  113.     r.h.al = 1;
  114.     r.h.ah = SamMuxid;                /* ax = (muxid << 8) + 1 */
  115.     int86x(0x2f, &r, &r, &sr);        /* do the int 0x2f */
  116.     return ((rhdr_t far *) rspbuf)->err;    /* return error code in rsp buf */
  117. }
  118.  
  119. /**************************
  120.  * SetCountrySam
  121.  **************************
  122.  *
  123.  * Selects the country for subsequent operations.  i.e., selects database
  124.  *
  125.  *  0 = USA
  126.  *  1 = Canada
  127.  *
  128.  * returns 0 if success, else !0 if error.  Possible errors are
  129.  * illegal country code or Canada database not installed.
  130.  *
  131.  * Note that for lookup by call, country selection is automatic unless
  132.  * samapi was started with /noauto option.  However, you need to select
  133.  * country to access database by record number, name record number,
  134.  * qth searches, etc.
  135.  */
  136.  
  137. int SetCountrySam(int country)
  138. {
  139.     chdr_t cmd;
  140.     rspnumrecs_t resp;
  141.     int old;
  142.  
  143.     if (LocateSam())
  144.         return -1;
  145.  
  146.     old = SamCountry;        /* save in case can't switch */
  147.     SamCountry = country;
  148.  
  149.     /* get Number of records for new country ...
  150.        * if no error, must have been ok to switch
  151.        */
  152.  
  153.     if (CallSam(SamGetNumRecs, &cmd, &resp))
  154.     {
  155.         SamCountry = old;        /* error, so don't switch */
  156.         return -1;
  157.     }
  158.     return 0;
  159. }
  160.