home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / tn3270 / api / apilib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-26  |  9.1 KB  |  412 lines

  1. /*-
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)apilib.c    4.2 (Berkeley) 4/26/91";
  36. #endif /* not lint */
  37.  
  38. #include "../ctlr/api.h"
  39.  
  40. #include "apilib.h"
  41.  
  42. int
  43.     api_sup_errno = 0,            /* Supervisor error number */
  44.     api_sup_fcn_id = 0,            /* Supervisor function id (0x12) */
  45.     api_fcn_errno = 0,            /* Function error number */
  46.     api_fcn_fcn_id = 0;            /* Function ID (0x6b, etc.) */
  47.  
  48. static int
  49.     gate_sessmgr = 0,
  50.     gate_keyboard = 0,
  51.     gate_copy = 0,
  52.     gate_oiam = 0;
  53.  
  54. /*
  55.  * Issue an API request, with reg structures supplied by the caller.
  56.  *
  57.  * Only certain routines need this (supervisor services come to mind).
  58.  */
  59.  
  60. static int
  61. api_issue_regs(ah, al, bh, bl, cx, dx, parms, length, regs, sregs)
  62. int        ah, al, bh, bl, cx, dx;
  63. char         *parms;
  64. int        length;
  65. union REGS     *regs;
  66. struct SREGS     *sregs;
  67. {
  68.     char far *ourseg = parms;
  69.  
  70.     regs->h.ah = ah;
  71.     regs->h.al = al;
  72.     regs->h.bh = bh;
  73.     regs->h.bl = bl;
  74.     regs->x.cx = cx;
  75.     regs->x.dx = dx;
  76.     sregs->es = FP_SEG(ourseg);
  77.     regs->x.di = FP_OFF(ourseg);
  78.  
  79. #if    defined(MSDOS)
  80.     int86x(API_INTERRUPT_NUMBER, regs, regs, sregs);
  81. #endif    /* defined(MSDOS) */
  82. #if    defined(unix)
  83.     api_exch_api(regs, sregs, parms, length);
  84. #endif    /* defined(unix) */
  85.  
  86.     if (regs->h.cl != 0) {
  87.     api_sup_errno = regs->h.cl;
  88.     return -1;
  89.     } else {
  90.     return 0;
  91.     }
  92. }
  93.  
  94.  
  95. /*
  96.  * Issue an API request without requiring caller to supply
  97.  * registers.  Most routines use this.
  98.  */
  99.  
  100. static int
  101. api_issue(ah, al, bh, bl, cx, dx, parms, length)
  102. int
  103.     ah,
  104.     al,
  105.     bh,
  106.     bl,
  107.     cx,
  108.     dx;
  109. char *parms;
  110. int length;                /* Length of parms */
  111. {
  112.     union REGS regs;
  113.     struct SREGS sregs;
  114.  
  115.     return api_issue_regs(ah, al, bh, bl, cx, dx, parms, length, ®s, &sregs);
  116. }
  117.  
  118. /*
  119.  * Supervisor Services
  120.  */
  121.  
  122. int
  123. api_name_resolve(name)
  124. char *name;
  125. {
  126.     NameResolveParms parms;
  127.     int i;
  128.     union REGS regs;
  129.     struct SREGS sregs;
  130.  
  131.     for (i = 0; i < sizeof parms.gate_name; i++) {
  132.     if (*name) {
  133.         parms.gate_name[i] = *name++;
  134.     } else {
  135.         parms.gate_name[i] = ' ';
  136.     }
  137.     }
  138.  
  139.     if (api_issue_regs(NAME_RESOLUTION, 0, 0, 0, 0, 0, (char *) &parms,
  140.                 sizeof parms, ®s, &sregs) == -1) {
  141.     return -1;
  142.     } else {
  143.     return regs.x.dx;
  144.     }
  145. }
  146.  
  147. #if    defined(unix)
  148. /*
  149.  * Block until the oia or ps is modified.
  150.  */
  151.  
  152. int
  153. api_ps_or_oia_modified()
  154. {
  155.     union REGS regs;
  156.     struct SREGS sregs;
  157.  
  158.     if (api_issue_regs(PS_OR_OIA_MODIFIED, 0, 0, 0, 0, 0, (char *) 0,
  159.                 0, ®s, &sregs) == -1) {
  160.     return -1;
  161.     } else {
  162.     return 0;
  163.     }
  164. }
  165. #endif    /* defined(unix) */
  166.  
  167. /*
  168.  * Session Information Services
  169.  */
  170.  
  171. api_query_session_id(parms)
  172. QuerySessionIdParms *parms;
  173. {
  174.     if (api_issue(0x09, QUERY_SESSION_ID, 0x80, 0x20, 0,
  175.                     gate_sessmgr, (char *)parms, sizeof *parms) == -1) {
  176.     api_fcn_errno = 0;
  177.     api_fcn_fcn_id = 0;
  178.     return -1;
  179.     } else if (parms->rc == 0) {
  180.     return 0;
  181.     } else {
  182.     api_fcn_errno = parms->rc;
  183.     api_fcn_fcn_id = parms->function_id;
  184.     return -1;
  185.     }
  186. }
  187.  
  188.  
  189. api_query_session_parameters(parms)
  190. QuerySessionParametersParms *parms;
  191. {
  192.     if (api_issue(0x09, QUERY_SESSION_PARAMETERS, 0x80, 0x20, 0,
  193.                 gate_sessmgr, (char *)parms, sizeof *parms) == -1) {
  194.     api_fcn_errno = 0;
  195.     api_fcn_fcn_id = 0;
  196.     return -1;
  197.     } else if (parms->rc == 0) {
  198.     return 0;
  199.     } else {
  200.     api_fcn_errno = parms->rc;
  201.     api_fcn_fcn_id = parms->function_id;
  202.     return -1;
  203.     }
  204. }
  205.  
  206. api_query_session_cursor(parms)
  207. QuerySessionCursorParms *parms;
  208. {
  209.     if (api_issue(0x09, QUERY_SESSION_CURSOR, 0x80, 0x20, 0xff,
  210.             gate_sessmgr, (char *)parms, sizeof *parms) == -1) {
  211.     api_fcn_errno = 0;
  212.     api_fcn_fcn_id = 0;
  213.     return -1;
  214.     } else if (parms->rc == 0) {
  215.     return 0;
  216.     } else {
  217.     api_fcn_errno = parms->rc;
  218.     api_fcn_fcn_id = parms->function_id;
  219.     return -1;
  220.     }
  221. }
  222.  
  223. /*
  224.  * Keyboard Services
  225.  */
  226.  
  227. api_connect_to_keyboard(parms)
  228. ConnectToKeyboardParms *parms;
  229. {
  230.     if (api_issue(0x09, CONNECT_TO_KEYBOARD, 0x80, 0x20, 0,
  231.             gate_keyboard, (char *)parms, sizeof *parms) == -1) {
  232.     api_fcn_errno = 0;
  233.     api_fcn_fcn_id = 0;
  234.     return -1;
  235.     } else if (parms->rc == 0) {
  236.     return 0;
  237.     } else {
  238.     api_fcn_errno = parms->rc;
  239.     api_fcn_fcn_id = parms->function_id;
  240.     return -1;
  241.     }
  242. }
  243.  
  244.  
  245. api_disconnect_from_keyboard(parms)
  246. DisconnectFromKeyboardParms *parms;
  247. {
  248.     if (api_issue(0x09, DISCONNECT_FROM_KEYBOARD, 0x80, 0x20, 0,
  249.             gate_keyboard, (char *)parms, sizeof *parms) == -1) {
  250.     api_fcn_errno = 0;
  251.     api_fcn_fcn_id = 0;
  252.     return -1;
  253.     } else if (parms->rc == 0) {
  254.     return 0;
  255.     } else {
  256.     api_fcn_errno = parms->rc;
  257.     api_fcn_fcn_id = parms->function_id;
  258.     return -1;
  259.     }
  260. }
  261.  
  262.  
  263. api_write_keystroke(parms)
  264. WriteKeystrokeParms *parms;
  265. {
  266.     if (api_issue(0x09, WRITE_KEYSTROKE, 0x80, 0x20, 0,
  267.             gate_keyboard, (char *)parms, sizeof *parms) == -1) {
  268.     api_fcn_errno = 0;
  269.     api_fcn_fcn_id = 0;
  270.     return -1;
  271.     } else if (parms->rc == 0) {
  272.     return 0;
  273.     } else {
  274.     api_fcn_errno = parms->rc;
  275.     api_fcn_fcn_id = parms->function_id;
  276.     return -1;
  277.     }
  278. }
  279.  
  280.  
  281. api_disable_input(parms)
  282. DisableInputParms *parms;
  283. {
  284.     if (api_issue(0x09, DISABLE_INPUT, 0x80, 0x20, 0,
  285.             gate_keyboard, (char *)parms, sizeof *parms) == -1) {
  286.     api_fcn_errno = 0;
  287.     api_fcn_fcn_id = 0;
  288.     return -1;
  289.     } else if (parms->rc == 0) {
  290.     return 0;
  291.     } else {
  292.     api_fcn_errno = parms->rc;
  293.     api_fcn_fcn_id = parms->function_id;
  294.     return -1;
  295.     }
  296. }
  297.  
  298. api_enable_input(parms)
  299. EnableInputParms *parms;
  300. {
  301.     if (api_issue(0x09, ENABLE_INPUT, 0x80, 0x20, 0,
  302.             gate_keyboard, (char *)parms, sizeof *parms) == -1) {
  303.     api_fcn_errno = 0;
  304.     api_fcn_fcn_id = 0;
  305.     return -1;
  306.     } else if (parms->rc == 0) {
  307.     return 0;
  308.     } else {
  309.     api_fcn_errno = parms->rc;
  310.     api_fcn_fcn_id = parms->function_id;
  311.     return -1;
  312.     }
  313. }
  314.  
  315. /*
  316.  * Copy Services
  317.  */
  318.  
  319. api_copy_string(parms)
  320. CopyStringParms *parms;
  321. {
  322.     if (api_issue(0x09, COPY_STRING, 0x80, 0x20, 0xff,
  323.                 gate_copy, (char *)parms, sizeof *parms) == -1) {
  324.     api_fcn_errno = 0;
  325.     api_fcn_fcn_id = 0;
  326.     return -1;
  327.     } else if (parms->rc == 0) {
  328.     return 0;
  329.     } else {
  330.     api_fcn_errno = parms->rc;
  331.     api_fcn_fcn_id = parms->function_id;
  332.     return -1;
  333.     }
  334. }
  335.  
  336. /*
  337.  * Operator Information Area Services
  338.  */
  339.  
  340. api_read_oia_group(parms)
  341. ReadOiaGroupParms *parms;
  342. {
  343.     if (api_issue(0x09, READ_OIA_GROUP, 0x80, 0x20, 0xff,
  344.                 gate_oiam, (char *)parms, sizeof *parms) == -1) {
  345.     api_fcn_errno = 0;
  346.     api_fcn_fcn_id = 0;
  347.     return -1;
  348.     } else if (parms->rc == 0) {
  349.     return 0;
  350.     } else {
  351.     api_fcn_errno = parms->rc;
  352.     api_fcn_fcn_id = parms->function_id;
  353.     return -1;
  354.     }
  355. }
  356.  
  357. /*
  358.  * The "we are done" routine.  This gets called last.
  359.  */
  360.  
  361. api_finish()
  362. {
  363. #if    defined(unix)
  364.     if (api_close_api() == -1) {
  365.     return -1;
  366.     } else {
  367.     return 0;
  368.     }
  369. #endif    /* defined(unix) */
  370. }
  371.  
  372.  
  373. /*
  374.  * The initialization routine.  Be sure to call this first.
  375.  */
  376.  
  377. api_init()
  378. {
  379. #if    defined(MSDOS)
  380.     union REGS regs;
  381.     struct SREGS sregs;
  382.  
  383.     regs.h.ah = 0x35;
  384.     regs.h.al = API_INTERRUPT_NUMBER;
  385.     intdosx(®s, ®s, &sregs);
  386.  
  387.     if ((regs.x.bx == 0) && (sregs.es == 0)) {
  388.     return 0;        /* Interrupt not being handled */
  389.     }
  390. #endif    /* defined(MSDOS) */
  391. #if    defined(unix)
  392.     if (api_open_api((char *)0) == -1) {
  393.     return 0;
  394.     }
  395. #endif    /* defined(unix) */
  396.  
  397.     gate_sessmgr = api_name_resolve("SESSMGR");
  398.     gate_keyboard = api_name_resolve("KEYBOARD");
  399.     gate_copy = api_name_resolve("COPY");
  400.     gate_oiam = api_name_resolve("OIAM");
  401.  
  402.     if ((gate_sessmgr == gate_keyboard) ||
  403.     (gate_sessmgr == gate_copy) ||
  404.     (gate_sessmgr == gate_oiam) ||
  405.     (gate_keyboard == gate_copy) ||
  406.     (gate_keyboard == gate_oiam) ||
  407.     (gate_copy == gate_oiam)) {
  408.         return 0;        /* Interrupt doesn't seem correct */
  409.     }
  410.     return 1;
  411. }
  412.