home *** CD-ROM | disk | FTP | other *** search
/ WordPerfect for Linux Bible / WP4LinuxBible.iso / sdk / wpx / code / wt / wtapi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-25  |  11.6 KB  |  380 lines

  1.  
  2. /* SOURCE FILE *****************************************************
  3.  * WTAPI.C  -  Writing Tools API C-Callable Interface Functions
  4.  *******************************************************************
  5.  * Copyright (C) 1993 WordPerfect Corp., All Rights Reserved.
  6.  *******************************************************************/
  7.  
  8.  
  9. /***** DEFINE THE APPROPRIATE ITEM TO USE THE CODE FOR WRITING *****/
  10. /***** TOOL SERVERS OR WRITING TOOL CLIENTS (OR BOTH).         *****/
  11. /* define WTAPI_SERVER */
  12. /* define WTAPI_CLIENT */
  13.  
  14.  
  15. #include <string.h>
  16.  
  17. #include "wtapi.h"
  18. #include "wtcomm.h"
  19.  
  20. /*----------------------------------------------------------
  21.         External Variables
  22. ------------------------------------------------------------*/
  23. #ifdef WTAPI_SERVER
  24. extern WTCOMM comm_id;                /* Platform specific communication ID */
  25. #endif /* WTAPI_SERVER */
  26. extern WTPTR wtsess;                /* Writing Tool Session Structure */
  27.  
  28. /*----------------------------------------------------------
  29.         External Functions
  30. ------------------------------------------------------------*/
  31. MSG_ERRS SendRequestAct(WTPIPE,char *,char *,MSG_TYPE,char *,int,FUNCP);
  32.  
  33. #ifdef WTAPI_SERVER
  34. WTSTATUS WTCInitRcv(WTCINITP);
  35. WTSTATUS WTCClActiveRcv(WTCCLACTIVEP);
  36. WTSTATUS WTCClInactiveRcv(WTCCLINACTIVEP);
  37. WTSTATUS WTCRqTlTermRcv(void);
  38. #endif /* WTAPI_SERVER */
  39.  
  40. #ifdef WTAPI_CLIENT        
  41. WTSTATUS WTQInfoBlockRcv(WTPTR, WTQINFOBLOCKP, WTRINFOBLOCKP, WTBUFP);
  42. WTSTATUS WTCTlTermRcv(WTPTR);
  43. WTSTATUS WTQInfoBlockRcv(WTPTR, WTQINFOBLOCKP, WTRINFOBLOCKP, WTBUFP);
  44. WTSTATUS WTQUnitInfoRcv(WTPTR, WTRUNITINFOP);
  45. WTSTATUS WTQReadToolDataRcv(WTPTR, WTQREADTOOLDATAP, WTRREADTOOLDATAP, WTBUFP);
  46. WTSTATUS WTQWriteToolDataRcv(WTPTR, WTQWRITETOOLDATAP, WTBUFP, WTRWRITETOOLDATAP);
  47. WTSTATUS WTQTextBlockRcv(WTPTR, WTQTEXTBLOCKP, WTRTEXTBLOCKP, WTBUFP);
  48. WTSTATUS WTQNextTextBlockRcv(WTPTR, WTQNEXTTEXTBLOCKP, WTRTEXTBLOCKP, WTBUFP);
  49. WTSTATUS WTQReadTextObjectRcv(WTPTR, WTQREADTEXTOBJECTP, WTRREADTEXTOBJECTP, WTBUFP);
  50. WTSTATUS WTQWriteTextObjectRcv(WTPTR, WTQWRITETEXTOBJECTP, WTBUFP, WTRWRITETEXTOBJECTP);
  51. WTSTATUS WTQGotoRcv(WTPTR, WTQGOTOP, WTRGOTOP);
  52. WTSTATUS WTQHiliteRcv(WTPTR, WTQHILITEP, WTRHILITEP);
  53. WTSTATUS WTQDehiliteRcv(WTPTR, WTQDEHILITEP, WTRDEHILITEP);
  54. WTSTATUS WTQReplaceRcv(WTPTR, WTQREPLACEP, WTBUFP, WTRREPLACEP);
  55. WTSTATUS WTQUndoReplaceRcv(WTPTR, WTRREPLACEP);
  56. WTSTATUS WTCRqClActiveRcv(WTPTR);
  57. WTSTATUS WTCRqInitRcv(WTPTR, WTCRQINITP, WTBUFP);
  58. WTSTATUS WTCTlInactiveRcv(WTPTR);
  59. WTSTATUS WTQTlActiveRcv(WTPTR, WTRTLACTIVEP);
  60. #endif /* WTAPI_CLIENT */
  61.  
  62. /*----------------------------------------------------------
  63.         Internal Variables
  64. ------------------------------------------------------------*/
  65. static WTBUFP rtmsgptr;
  66. static WTBUFP rtbufptr;
  67.  
  68. /*----------------------------------------------------------
  69.         Internal Functions
  70. ------------------------------------------------------------*/
  71. static MSG_ERRS wtReplyReceive(char *, char *);
  72.  
  73. /* FUNCTION ********************************************************
  74.  * wtqClSend  -  Client sends a WTAPI query message
  75.  * In:        comm = comm info for this conversation
  76.  *            inmsgid = message to be sent
  77.  *            inmsg = buffer holding message
  78.  *            inmsgsize = size of message
  79.  *            inbuf = buffer holding message data
  80.  *            inbufsize = size of message data
  81.  *            rtmsgid = message to be returned
  82.  *            rtmsg = buffer to hold return message
  83.  *            rtmsgsize = size of message return buffer
  84.  *            rtbuf = buffer to hold return message data
  85.  *            rtbufsize = size of message data return buffer
  86.  * Out:        none
  87.  * Return:    WTSTATUS
  88.  * Notes:    
  89.  *******************************************************************/
  90. WTSTATUS wtqClSend (WTCOMM comm, WTMSGID inmsgid, WTBUFP inmsg,
  91.     WTSIZE inmsgsize, WTBUFP inbuf, WTSIZE inbufsize,
  92.     WTMSGID rtmsgid, WTBUFP rtmsg, WTSIZE rtmsgsize,
  93.     WTBUFP rtbuf, WTSIZE rtbufsize)
  94. {
  95. #ifdef WTAPI_CLIENT
  96.     MSG_ERRS status;
  97.     char *data;
  98.     int datasize;
  99.     
  100.     rtmsgptr = rtmsg;
  101.     rtbufptr = rtbuf;
  102.     
  103.     datasize = sizeof(WTMSGHEADER) + inmsgsize + inbufsize;
  104.     if ((data = (char *)malloc(datasize)) == 0) {
  105.         return WTS_NOMEMORY;
  106.     }
  107.     
  108.     ((WTMSGHEADERP)data)->inmsgid = inmsgid;
  109.     ((WTMSGHEADERP)data)->inmsgsize = inmsgsize;
  110.     ((WTMSGHEADERP)data)->inbufsize = inbufsize;
  111.     ((WTMSGHEADERP)data)->rtmsgid = rtmsgid;
  112.     ((WTMSGHEADERP)data)->rtmsgsize = rtmsgsize;
  113.     ((WTMSGHEADERP)data)->rtbufsize = rtbufsize;
  114.     
  115.     memcpy(data+sizeof(WTMSGHEADER), (char *)inmsg, inmsgsize);
  116.     if (inbuf) {
  117.         memcpy(data+sizeof(WTMSGHEADER)+inmsgsize, (char *)inbuf, inbufsize);
  118.     }
  119.     if (rtmsgid == WTC_NOMESSAGE) {
  120.         status = SendMsg(comm->cl_pipe, comm->client, comm->tool,
  121.             WTAPI_CLIENT_COMMAND, data, datasize);
  122.     } else {
  123.         status = SendRequestAct(comm->cl_pipe, comm->client, comm->tool,
  124.             WTAPI_CLIENT_QUERY, data, datasize, (FUNCP)wtReplyReceive);
  125.     }
  126.     free(data);
  127.     if (status != MSG_OK) {
  128.         return WTS_COMERROR;
  129.     } else {
  130.         return WTS_OK;
  131.     }
  132. #endif /* WTAPI_CLIENT */ 
  133.     return WTS_OK;
  134. } /* wtqClSend */
  135.  
  136. /* FUNCTION ********************************************************
  137.  * wtqTlSend  -  Send WTAPI msg to writing tool user and wait for a 
  138.  *               return message.
  139.  * Globals:    hOutgoingMsg is set so that WtMsgSend can send it.
  140.  *             TlMsgWaiting is used as a semaphore to know when we 
  141.  *            received the reply to the message we sent.
  142.  *             hTlDataReceived is a handle to a copy of the reply data
  143.  * Return:     WTS_OK if successful.
  144.  *             WTS_COMERROR if error occurred.
  145.  *             WTS_NOMEMORY if no memory for send buffer.
  146.  *            WTS_QUITAPI if message received to terminate API.
  147.  * Descrip:
  148.  *******************************************************************/
  149. WTSTATUS wtqTlSend (WTCOMM comm, WTMSGID inmsgid, WTBUFP inmsg,
  150.     WTSIZE inmsgsize, WTBUFP inbuf, WTSIZE inbufsize,
  151.     WTMSGID rtmsgid, WTBUFP rtmsg, WTSIZE rtmsgsize,
  152.     WTBUFP rtbuf, WTSIZE rtbufsize)
  153. {
  154. #ifdef WTAPI_SERVER
  155.     MSG_ERRS status;
  156.     char *data;
  157.     int datasize;
  158.     
  159.     if (!comm) {
  160.         return WTS_COMERROR;
  161.     }
  162.     rtmsgptr = rtmsg;
  163.     rtbufptr = rtbuf;
  164.     
  165.     datasize = sizeof(WTMSGHEADER) + inmsgsize + inbufsize;
  166.     if ((data = (char *)malloc(datasize)) == 0) {
  167.         return WTS_NOMEMORY;
  168.     }
  169.     
  170.     ((WTMSGHEADERP)data)->inmsgid = inmsgid;
  171.     ((WTMSGHEADERP)data)->inmsgsize = inmsgsize;
  172.     ((WTMSGHEADERP)data)->inbufsize = inbufsize;
  173.     ((WTMSGHEADERP)data)->rtmsgid = rtmsgid;
  174.     ((WTMSGHEADERP)data)->rtmsgsize = rtmsgsize;
  175.     ((WTMSGHEADERP)data)->rtbufsize = rtbufsize;
  176.     
  177.     memcpy(data + sizeof(WTMSGHEADER), (char *)inmsg, inmsgsize);
  178.     if (inbuf) {
  179.         memcpy(data+sizeof(WTMSGHEADER)+inmsgsize, (char *)inbuf, inbufsize);
  180.     }
  181.     
  182.     status = SendRequestAct(comm->tl_pipe, comm->tool, comm->client,
  183.         WTAPI_TOOL_QUERY, data, datasize, (FUNCP)wtReplyReceive);
  184.     free(data);
  185.     if (status) {
  186.         return WTS_COMERROR;
  187.     } else {
  188.         return WTS_OK;
  189.     }
  190. #endif /* WTAPI_SERVER */
  191.     return WTS_OK;
  192. } /* wtqTlSend */
  193.  
  194. /*COMMENT********************************************************
  195. ;wtReplyReceive
  196. Title:    Receive a writing tools API reply message
  197. In:        sender - sender name
  198.         retdata - return message data
  199. Out:    none
  200. Xin:    rtmsgptr, rtbufptr
  201. XOut:    none
  202. Return:    MSG_OK
  203. Notes:    
  204. *****************************************************************/
  205. static MSG_ERRS wtReplyReceive(char *sender, char *retdata)
  206. {
  207.     WTSIZE rtmsgsize, rtbufsize;
  208.     WTMSGHEADER header;
  209.     
  210.     if (retdata) {
  211.         memcpy((char *)&header, retdata, sizeof(WTMSGHEADER));
  212.         rtmsgsize = header.inmsgsize;
  213.         rtbufsize = header.inbufsize;
  214.         if (rtmsgptr) {
  215.             memcpy(rtmsgptr, retdata+sizeof(WTMSGHEADER), rtmsgsize);
  216.         }
  217.         if (rtbufptr) {
  218.             memcpy(rtbufptr, retdata+sizeof(WTMSGHEADER)+rtmsgsize, rtbufsize);
  219.         }
  220.     }
  221.     return MSG_OK;
  222. } /* wtReplyReceive */
  223.  
  224. /* FUNCTION ********************************************************
  225.  * wtClReceive  -  Receive client WTAPI messages
  226.  * In:        comm = comm info for this api session
  227.  *            msgid = WTMSGID of incoming message
  228.  *            inmsg = buffer containing message
  229.  *            inbuf = buffer for incoming data
  230.  * Out:        rtmsg = buffer to hold return message
  231.  *            rtbuf = buffer to hold return data
  232.  *            rtbufsizep = holds actualsize of data in rtbuf
  233.  * Return:    WTSTATUS from message handling routines
  234.  * Notes:    
  235.  *******************************************************************/
  236. WTSTATUS wtClReceive(WTCOMM comm, WTMSGID msgid, WTBUFP inmsg, WTBUFP inbuf,
  237.                         WTBUFP rtmsg, WTBUFP rtbuf, WTSIZEP rtbufsizep)
  238. {
  239.     WTSTATUS status = WTS_OK;
  240. #ifdef WTAPI_CLIENT
  241.     
  242.     *rtbufsizep = 0;
  243.     switch (msgid) {
  244.         case WTC_RQINIT:
  245.             status = WTCRqInitRcv(wtsess, (WTCRQINITP)inmsg, inbuf);
  246.         break;
  247.     
  248.         case WTQ_INFOBLOCK:
  249.             status = WTQInfoBlockRcv(wtsess, (WTQINFOBLOCKP)inmsg,
  250.                         (WTRINFOBLOCKP)rtmsg, rtbuf);
  251.             *rtbufsizep = ((WTRINFOBLOCKP)rtmsg)->winsize;
  252.         break;
  253.     
  254.         case WTQ_UNITINFO:
  255.             status = WTQUnitInfoRcv(wtsess, (WTRUNITINFOP)rtmsg);
  256.         break;
  257.     
  258.         case WTQ_READTOOLDATA:
  259.             status = WTQReadToolDataRcv(wtsess, (WTQREADTOOLDATAP)inmsg,
  260.                         (WTRREADTOOLDATAP)rtmsg, rtbuf);
  261.             *rtbufsizep = ((WTRREADTOOLDATAP)rtmsg)->size;
  262.         break;
  263.     
  264.         case WTQ_WRITETOOLDATA:
  265.             status = WTQWriteToolDataRcv(wtsess, (WTQWRITETOOLDATAP)inmsg,
  266.                         inbuf, (WTRWRITETOOLDATAP)rtmsg);
  267.         break;
  268.     
  269.         case WTQ_TEXTBLOCK:
  270.             status = WTQTextBlockRcv(wtsess, (WTQTEXTBLOCKP)inmsg,
  271.                         (WTRTEXTBLOCKP)rtmsg, rtbuf);
  272.             *rtbufsizep = ((WTRTEXTBLOCKP)rtmsg)->size;
  273.         break;
  274.     
  275.         case WTQ_NEXTTEXTBLOCK:
  276.             status = WTQNextTextBlockRcv(wtsess, (WTQNEXTTEXTBLOCKP)inmsg,
  277.                     (WTRTEXTBLOCKP)rtmsg, rtbuf);
  278.             *rtbufsizep = ((WTRTEXTBLOCKP)rtmsg)->size;
  279.         break;
  280.         
  281.         case WTQ_READTEXTOBJECT:
  282.             status = WTQReadTextObjectRcv(wtsess, (WTQREADTEXTOBJECTP)inmsg,
  283.                         (WTRREADTEXTOBJECTP)rtmsg, rtbuf);
  284.             *rtbufsizep = ((WTRREADTEXTOBJECTP)rtmsg)->size;
  285.         break;
  286.         
  287.         case WTQ_WRITETEXTOBJECT:
  288.             status = WTQWriteTextObjectRcv(wtsess, (WTQWRITETEXTOBJECTP)inmsg,
  289.                         inbuf, (WTRWRITETEXTOBJECTP)rtmsg);
  290.         break;
  291.         
  292.         case WTQ_GOTO:
  293.             status = WTQGotoRcv(wtsess, (WTQGOTOP)inmsg, (WTRGOTOP)rtmsg);
  294.         break;
  295.     
  296.         case WTQ_HILITE:
  297.             status = WTQHiliteRcv(wtsess, (WTQHILITEP)inmsg, (WTRHILITEP)rtmsg);
  298.         break;
  299.     
  300.         case WTQ_DEHILITE:
  301.             status = WTQDehiliteRcv(wtsess, (WTQDEHILITEP)inmsg, (WTRDEHILITEP)rtmsg);
  302.         break;
  303.     
  304.         case WTQ_REPLACE:
  305.             status = WTQReplaceRcv(wtsess, (WTQREPLACEP)inmsg, inbuf,
  306.                         (WTRREPLACEP)rtmsg);
  307.         break;
  308.     
  309.         case WTQ_UNDOREPLACE:
  310.             status = WTQUndoReplaceRcv(wtsess, (WTRREPLACEP)rtmsg);
  311.         break;
  312.     
  313.         case WTC_RQCLACTIVE:
  314.             status = WTCRqClActiveRcv(wtsess);
  315.         break;
  316.     
  317.         case WTC_TLINACTIVE:
  318.             status = WTCTlInactiveRcv(wtsess);
  319.         break;
  320.     
  321.         case WTQ_TLACTIVE:
  322.             status = WTQTlActiveRcv(wtsess, (WTRTLACTIVEP)rtmsg);
  323.         break;
  324.     
  325.         case WTC_TLTERM:
  326.             status = WTCTlTermRcv(wtsess);
  327.         break;
  328.     
  329.         default:
  330.             status = WTS_BADREQUEST;    /* we got something we didn't expect */
  331.         break;
  332.     }
  333. #endif /* WTAPI_CLIENT */
  334.     return status;
  335. } /* wtClReceive */
  336.  
  337. /* FUNCTION ********************************************************
  338.  * wtTlReceive  -  Receive Writing Tools API messages
  339.  * In:        comm = conversation handle
  340.  *            msgid = id of writing tools message
  341.  *            inmsg = buffer containing incoming message
  342.  *            inbuf = buffer containing incoming message data
  343.  *            rtmsg = buffer to contain outgoing message
  344.  *            rtbuf = buffer to contain outgoing message data
  345.  * Out:        rtbufsizep = will hold the actual size of the data in rtbuf
  346.  * Return:    WTSTATUS
  347.  * Notes:    
  348.  *******************************************************************/
  349. WTSTATUS wtTlReceive(WTCOMM comm, WTMSGID msgid, WTBUFP inmsg, WTBUFP inbuf,
  350.                      WTBUFP rtmsg, WTBUFP rtbuf, WTSIZEP rtbufsizep)
  351. {
  352.     WTSTATUS status = WTS_OK;
  353. #ifdef WTAPI_SERVER
  354.     
  355.     *rtbufsizep = 0;
  356.     switch (msgid) {
  357.         case WTC_INIT:
  358.             comm_id = comm;
  359.             status = WTCInitRcv((WTCINITP)inmsg);
  360.         break;        
  361.  
  362.           case WTC_CLACTIVE:
  363.             status = WTCClActiveRcv((WTCCLACTIVEP)inmsg);
  364.         break;        
  365.  
  366.         case WTC_CLINACTIVE:
  367.             status = WTCClInactiveRcv((WTCCLINACTIVEP)inmsg);
  368.         break;        
  369.  
  370.         case WTC_RQTLTERM:
  371.             status = WTCRqTlTermRcv();
  372.         break;        
  373.  
  374.         default:
  375.         break;
  376.     }
  377. #endif /* WTAPI_SERVER */
  378.     return status;
  379. } /* wtTlReceive */
  380.