home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / programme / GoldED / developer / examples / api / startup / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  4.4 KB  |  209 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. Prototype LibCall void              APIFree       (__A0 struct APIClient  *, __A1 struct APIOrder   *);
  32.  
  33. // private
  34.  
  35. Prototype ULONG *SendRexxCommand(UBYTE *, UBYTE *, struct MessagePort *);
  36. Prototype void   StartupHandler(void);
  37.  
  38. // globals
  39.  
  40. UWORD Handlers;
  41.  
  42. ///
  43. /// "library functions"
  44.  
  45. LibCall struct APIClient *
  46. APIMountClient(__A0 struct APIMessage *apiMsg, __A1 char *args)
  47. {
  48.     static struct APIClient apiClient;
  49.  
  50.     apiClient.api_APIVersion = API_INTERFACE_VERSION;
  51.     apiClient.api_Version    = 1;
  52.     apiClient.api_Name       = "Startup API";
  53.     apiClient.api_Info       = "Startup handler (STARTUP.API <ARGUMENTS>)";
  54.     apiClient.api_Commands   = NULL;
  55.     apiClient.api_Serial     = 0;
  56.     apiClient.api_Classes    = API_CLASS_SYSTEM;
  57.     apiClient.api_Area       = NULL;
  58.  
  59.     // <args> names the macro to be executed
  60.  
  61.     if (args && *args) {
  62.  
  63.         UBYTE *command;
  64.  
  65.         if (command = AllocVec(strlen(args) + 1, MEMF_CLEAR | MEMF_PUBLIC)) {
  66.  
  67.             struct Task *task;
  68.  
  69.             Forbid();
  70.  
  71.             if (task = (struct Task *)CreateNewProcTags(NP_Entry, (APTR)StartupHandler, NP_Name, "STARTUP", NP_Priority, 1, TAG_DONE)) {
  72.  
  73.                 ++Handlers;
  74.  
  75.                 task->tc_UserData = (APTR)strcpy(command, args);
  76.             }
  77.  
  78.             Permit();
  79.         }
  80.  
  81.     }
  82.  
  83.     return(&apiClient);
  84. }
  85.  
  86. LibCall void
  87. APICloseClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  88. {
  89.     // wait for termination of running tasks
  90.  
  91.     while (Handlers)
  92.  
  93.         Delay(25);
  94. }
  95.  
  96. LibCall void
  97. APIBriefClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  98. {
  99.     // ignore notifies
  100. }
  101.  
  102. LibCall void
  103. APIFree(__A0 struct APIClient *handle, __A1 struct APIOrder *apiOrder)
  104. {
  105.     // no ressources to be freed
  106. }
  107.  
  108. ///
  109. /// "ARexx"
  110.  
  111. /* ---------------------------------- SendRexxCommand -------------------------
  112.  
  113.  Send ARexx message & wait for answer. Return pointer to result or NULL.
  114.  
  115. */
  116.  
  117. __geta4 ULONG *
  118. SendRexxCommand(port, cmd, replyPort)
  119.  
  120. UBYTE              *port;
  121. UBYTE              *cmd;
  122. struct MessagePort *replyPort;
  123. {
  124.     struct MsgPort *rexxport;
  125.  
  126.     Forbid();
  127.  
  128.     if (rexxport = FindPort(port)) {
  129.  
  130.         struct RexxMsg *rexxMsg, *answer;
  131.  
  132.         if (rexxMsg = CreateRexxMsg(replyPort, NULL, NULL)) {
  133.  
  134.             if (rexxMsg->rm_Args[0] = CreateArgstring(cmd, strlen(cmd))) {
  135.  
  136.                 static ULONG result;
  137.  
  138.                 rexxMsg->rm_Action = RXCOMM | RXFF_RESULT;
  139.  
  140.                 PutMsg(rexxport, &rexxMsg->rm_Node);
  141.  
  142.                 do {
  143.  
  144.                     WaitPort(replyPort);
  145.  
  146.                     if (answer = (struct RexxMsg *)GetMsg(replyPort))
  147.                         result = answer->rm_Result1;
  148.  
  149.                 } while (!answer);
  150.  
  151.                 Permit();
  152.  
  153.                 if (answer->rm_Result1 == RC_OK)
  154.  
  155.                     if (answer->rm_Result2)
  156.  
  157.                         DeleteArgstring((UBYTE *)answer->rm_Result2);
  158.  
  159.                 DeleteArgstring((UBYTE *)ARG0(answer));
  160.  
  161.                 DeleteRexxMsg(answer);
  162.  
  163.                 return(&result);
  164.             }
  165.         }
  166.     }
  167.  
  168.     Permit();
  169.  
  170.     return(NULL);
  171. }
  172.  
  173. ///
  174. /// "startup handler"
  175.  
  176. /* ------------------------------ StartupHandler -------------------------------
  177.  
  178.  Send <command> to the ARexx server. Run as a task (we are not allowed to block
  179.  GoldED). The command string is expected in the task's tc_UserData slot.
  180.  
  181. */
  182.  
  183. __geta4 void
  184. StartupHandler()
  185. {
  186.     UBYTE *command;
  187.  
  188.     if (command = FindTask(NULL)->tc_UserData) {
  189.  
  190.         struct MessagePort *msgPort;
  191.  
  192.         if (msgPort = CreateMsgPort()) {
  193.  
  194.             SendRexxCommand("AREXX", command, msgPort);
  195.  
  196.             DeleteMsgPort(msgPort);
  197.         }
  198.  
  199.         FreeVec(command);
  200.  
  201.         Forbid();
  202.  
  203.         --Handlers;
  204.     }
  205. }
  206.  
  207.  
  208. ///
  209.