home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / Editor / GED403R.LZX / Install / Install.run / GOLDEDDATA / developer / examples / api / startup / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-30  |  4.2 KB  |  202 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.  ApiLib ©1995 Dietmar Eilert
  4.  
  5.  PURPOSE
  6.  
  7.  Allows you to run a macro every time a new text is loaded.
  8.  
  9.  USAGE
  10.  
  11.  Add STARTUP.API to the list of your API clients (local configuration). Enter
  12.  the macro to be executed in the "Arguments" gadget.
  13.  
  14.  Dice:
  15.  
  16.  DMAKE
  17.  
  18.  -------------------------------------------------------------------------------
  19.  
  20. */
  21.  
  22. #include "defs.h"
  23.  
  24. /// "Prototype"
  25.  
  26. // library functions
  27.  
  28. Prototype LibCall struct APIClient *APIMountClient(__A0 struct APIMessage *, __A1 char *);
  29. Prototype LibCall void              APICloseClient(__A0 struct APIClient *, __A1 struct APIMessage *);
  30. Prototype LibCall void              APIBriefClient(__A0 struct APIClient *, __A1 struct APIMessage *);
  31.  
  32. // private
  33.  
  34. Prototype ULONG *SendRexxCommand(UBYTE *, UBYTE *, struct MessagePort *);
  35. Prototype void   StartupHandler(void);
  36.  
  37. // globals
  38.  
  39. UWORD Handlers;
  40.  
  41. ///
  42. /// "library functions"
  43.  
  44. LibCall struct APIClient *
  45. APIMountClient(__A0 struct APIMessage *apiMsg, __A1 char *args)
  46. {
  47.     static struct APIClient apiClient;
  48.  
  49.     apiClient.api_APIVersion = API_INTERFACE_VERSION;
  50.     apiClient.api_Version    = 1;
  51.     apiClient.api_Name       = "Startup API";
  52.     apiClient.api_Info       = "Startup handler (STARTUP.API <ARGUMENTS>)";
  53.     apiClient.api_Commands   = NULL;
  54.     apiClient.api_Serial     = 0;
  55.     apiClient.api_Classes    = API_CLASS_SYSTEM;
  56.     apiClient.api_Area       = NULL;
  57.  
  58.     // <args> names the macro to be executed
  59.  
  60.     if (args && *args) {
  61.  
  62.         UBYTE *command;
  63.  
  64.         if (command = AllocVec(strlen(args) + 1, MEMF_CLEAR | MEMF_PUBLIC)) {
  65.  
  66.             struct Task *task;
  67.  
  68.             Forbid();
  69.  
  70.             if (task = (struct Task *)CreateNewProcTags(NP_Entry, (APTR)StartupHandler, NP_Name, "STARTUP", NP_Priority, 1, TAG_DONE)) {
  71.  
  72.                 ++Handlers;
  73.  
  74.                 task->tc_UserData = (APTR)strcpy(command, args);
  75.             }
  76.  
  77.             Permit();
  78.         }
  79.  
  80.     }
  81.  
  82.     return(&apiClient);
  83. }
  84.  
  85. LibCall void
  86. APICloseClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  87. {
  88.     // wait for termination of running tasks
  89.  
  90.     while (Handlers)
  91.  
  92.         Delay(25);
  93. }
  94.  
  95. LibCall void
  96. APIBriefClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  97. {
  98.     // ignore notifies
  99. }
  100.  
  101. ///
  102. /// "ARexx"
  103.  
  104. /* ---------------------------------- SendRexxCommand -------------------------
  105.  
  106.  Send ARexx message & wait for answer. Return pointer to result or NULL.
  107.  
  108. */
  109.  
  110. __geta4 ULONG *
  111. SendRexxCommand(port, cmd, replyPort)
  112.  
  113. UBYTE              *port;
  114. UBYTE              *cmd;
  115. struct MessagePort *replyPort;
  116. {
  117.     struct MsgPort *rexxport;
  118.  
  119.     Forbid();
  120.  
  121.     if (rexxport = FindPort(port)) {
  122.  
  123.         struct RexxMsg *rexxMsg, *answer;
  124.  
  125.         if (rexxMsg = CreateRexxMsg(replyPort, NULL, NULL)) {
  126.  
  127.             if (rexxMsg->rm_Args[0] = CreateArgstring(cmd, strlen(cmd))) {
  128.  
  129.                 static ULONG result;
  130.  
  131.                 rexxMsg->rm_Action = RXCOMM | RXFF_RESULT;
  132.  
  133.                 PutMsg(rexxport, &rexxMsg->rm_Node);
  134.  
  135.                 do {
  136.  
  137.                     WaitPort(replyPort);
  138.  
  139.                     if (answer = (struct RexxMsg *)GetMsg(replyPort))
  140.                         result = answer->rm_Result1;
  141.  
  142.                 } while (!answer);
  143.  
  144.                 Permit();
  145.  
  146.                 if (answer->rm_Result1 == RC_OK)
  147.  
  148.                     if (answer->rm_Result2)
  149.  
  150.                         DeleteArgstring((UBYTE *)answer->rm_Result2);
  151.  
  152.                 DeleteArgstring((UBYTE *)ARG0(answer));
  153.  
  154.                 DeleteRexxMsg(answer);
  155.  
  156.                 return(&result);
  157.             }
  158.         }
  159.     }
  160.  
  161.     Permit();
  162.  
  163.     return(NULL);
  164. }
  165.  
  166. ///
  167. /// "startup handler"
  168.  
  169. /* ------------------------------ StartupHandler -------------------------------
  170.  
  171.  Send <command> to the ARexx server. Run as a task (we are not allowed to block
  172.  GoldED). The command string is expected in the task's tc_UserData slot.
  173.  
  174. */
  175.  
  176. __geta4 void
  177. StartupHandler()
  178. {
  179.     UBYTE *command;
  180.  
  181.     if (command = FindTask(NULL)->tc_UserData) {
  182.  
  183.         struct MessagePort *msgPort;
  184.  
  185.         if (msgPort = CreateMsgPort()) {
  186.  
  187.             SendRexxCommand("AREXX", command, msgPort);
  188.  
  189.             DeleteMsgPort(msgPort);
  190.         }
  191.  
  192.         FreeVec(command);
  193.  
  194.         Forbid();
  195.  
  196.         --Handlers;
  197.     }
  198. }
  199.  
  200.  
  201. ///
  202.