home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d325 / farprint.lha / FarPrint / FarCom.c < prev    next >
C/C++ Source or Header  |  1990-02-27  |  5KB  |  234 lines

  1. /* FarCom.c *****************************************************************
  2. *
  3. *    FarCom --------    Debugging functions for programs which don't
  4. *            have links to their environment.
  5. *
  6. *            FarPrint communication routines.
  7. *
  8. *    Author --------    Olaf Barthel of MXM
  9. *            Brabeckstrasse 35
  10. *            D-3000 Hannover 71
  11. *
  12. *            Federal Republic of Germany.
  13. *
  14. *    This program truly is in the PUBLIC DOMAIN. Written on a sunny
  15. *    September day in 1989, updated 27 January 1990.
  16. *
  17. *    Compiled using Aztec C 3.6a, CygnusEd Professional 2 & ARexx.
  18. *
  19. ****************************************************************************/
  20.  
  21. #include <exec/memory.h>
  22. #include <exec/ports.h>
  23.  
  24.     /* Forward declarations. */
  25.  
  26. extern struct MsgPort    *FindPort();
  27. extern struct MsgPort    *CreatePort();
  28. extern void        *AllocMem();
  29.  
  30.     /* Global task <-> task communication stuff. */
  31.  
  32. #define FM_ADDTXT    0
  33. #define FM_REQTXT    1
  34. #define FM_REQNUM    2
  35.  
  36. struct FarMessage
  37. {
  38.     struct Message    fm_ExecMessage;
  39.  
  40.     USHORT        fm_Command;
  41.     STRPTR        fm_Identifier;
  42.     STRPTR        fm_Text;
  43. };
  44.  
  45.     /* SendIt(MessageText,Identifier,Command):
  46.      *
  47.      *    Sends message/request to FarPort, completely
  48.      *    reentrant, shouldn't work from interrupt code
  49.      *    since we wait for the reply.
  50.      */
  51.  
  52. BOOL
  53. SendIt(MessageText,Identifier,Command)
  54. STRPTR MessageText,Identifier;
  55. USHORT Command;
  56. {
  57.     struct MsgPort *ReplyPort,*FarPort;
  58.     struct FarMessage FarMessage;
  59.  
  60.         /* Destination port present? */
  61.  
  62.     if(FarPort = (struct MsgPort *)FindPort("FarPort"))
  63.     {
  64.             /* Initialize custom message structure. */
  65.  
  66.         FarMessage . fm_ExecMessage . mn_Length        = sizeof(struct FarMessage);
  67.         FarMessage . fm_Identifier            = Identifier;
  68.         FarMessage . fm_Text                = MessageText;
  69.         FarMessage . fm_Command                = Command;
  70.  
  71.             /* This is a text to be added. */
  72.  
  73.         if(Command == FM_ADDTXT)
  74.         {
  75.             struct FarMessage *TempMessage = (struct FarMessage *)AllocMem(sizeof(struct FarMessage),MEMF_PUBLIC);
  76.             STRPTR TempString = (STRPTR)AllocMem(strlen(MessageText) + 1,MEMF_PUBLIC);
  77.  
  78.                 /* In this case both text and message
  79.                  * structure will be cloned (we won't have
  80.                  * to wait for a reply).
  81.                  */
  82.  
  83.             if(TempString)
  84.             {
  85.                 strcpy(TempString,MessageText);
  86.  
  87.                 if(TempMessage)
  88.                 {
  89.                     FarMessage . fm_ExecMessage . mn_ReplyPort    = NULL;
  90.                     FarMessage . fm_Text                = TempString;
  91.  
  92.                     CopyMem(&FarMessage,TempMessage,sizeof(struct FarMessage));
  93.                 
  94.                     PutMsg(FarPort,TempMessage);
  95.  
  96.                     return(TRUE);
  97.                 }
  98.  
  99.                 FreeMem(TempString,strlen(MessageText) + 1);
  100.             }
  101.  
  102.             if(TempMessage)
  103.                 FreeMem(TempMessage,sizeof(struct FarMessage));
  104.  
  105.             return(FALSE);
  106.         }
  107.  
  108.             /* Create a replyport. */
  109.  
  110.         if(ReplyPort = (struct MsgPort *)CreatePort(NULL,0))
  111.         {
  112.             FarMessage . fm_ExecMessage . mn_ReplyPort = ReplyPort;
  113.  
  114.                 /* Send it and wait for a reply. */
  115.  
  116.             PutMsg(FarPort,&FarMessage);
  117.             Wait(1 << ReplyPort -> mp_SigBit);
  118.  
  119.                 /* Remove the replyport. */
  120.  
  121.             DeletePort(ReplyPort);
  122.  
  123.             return(TRUE);
  124.         }
  125.     }
  126.  
  127.     return(FALSE);
  128. }
  129.  
  130.     /* AddText(MessageText):
  131.      *
  132.      *    Subroutine called by SendText, adds a Text to
  133.      *    the list of messages FarPrint maintains.
  134.      */
  135.  
  136. BOOL
  137. AddText(MessageText)
  138. STRPTR MessageText;
  139. {
  140.     return(SendIt(MessageText,NULL,FM_ADDTXT));
  141. }
  142.  
  143.     /* RequestNumber(Identifier):
  144.      *
  145.      *    Requests a number to be entered by the user.
  146.      *    Identifier points to a string which is to
  147.      *    identify the calling process.
  148.      */
  149.  
  150. long
  151. RequestNumber(Identifier)
  152. STRPTR Identifier;
  153. {
  154.     UBYTE NumBuff[81];
  155.  
  156.     setmem(NumBuff,81,0);
  157.  
  158.     SendIt(NumBuff,Identifier,FM_REQNUM);
  159.  
  160.     return(atoi(NumBuff));
  161. }
  162.  
  163.     /* RequestString(Identifier):
  164.      *
  165.      *    Requests a string to be entered by the user,
  166.      *    Identifier points to a string which is to
  167.      *    identify the calling process. Note: this one
  168.      *    isn't reentrant since it uses a temporary
  169.      *    string buffer. This could easily be fixed
  170.      *    by using a second argument to be used as
  171.      *    a destination string buffer.
  172.      */
  173.  
  174. STRPTR
  175. RequestString(Identifier,TxtBuff)
  176. STRPTR Identifier,TxtBuff;
  177. {
  178.     if(SendIt(TxtBuff,Identifier,FM_REQTXT))
  179.         return(TxtBuff);
  180.     else
  181.         return(NULL);
  182. }
  183.  
  184.     /* This embedded assembly fragment handles the SendText()
  185.      * function. It basically consists of a call to the
  186.      * exec RawDoFmt() function to build the argument string
  187.      * it passes to AddText(). Original Author: Justin V. McCormick.
  188.      */
  189.  
  190. #ifdef AZTEC_C
  191.  
  192. #asm
  193. _LVORawDoFmt    EQU    $FFFFFDF6
  194.  
  195.         XDEF    kput1
  196.  
  197. kput1:        MOVE.B    D0,(A3)+
  198.         RTS
  199.  
  200.         XDEF    _SendText
  201.  
  202. _SendText:    LINK    A5,#-512
  203.         MOVEM.L    D1/A0-A3,-(SP)
  204.  
  205.         MOVEA.L    8(A5),A0
  206.         LEA    12(A5),A1
  207.         LEA    kput1(PC),A2
  208.         LEA    -512(A5),A3
  209.         MOVE.L    4,A6
  210.         JSR    _LVORawDoFmt(A6)
  211.     
  212.         LEA    -512(A5),A0
  213.  
  214.         MOVE.L    A5,-(SP)
  215.         MOVE.L    A0,-(SP)
  216.  
  217.         JSR    _geta4#    ; Use this call only with small code
  218.                 ; memory model or it will pull half of the
  219.                 ; standard library code into your
  220.                 ; program (devilish when developing
  221.                 ; a shared library!)
  222.  
  223.         JSR    _AddText
  224.  
  225.         ADD.W    #4,SP
  226.         MOVE.L    (SP)+,A5
  227.  
  228.         MOVEM.L    (SP)+,D1/A0-A3
  229.         UNLK    A5
  230.         RTS
  231. #endasm
  232.  
  233. #endif AZTEC_C
  234.