home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / SAMLIB.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  5KB  |  160 lines

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