home *** CD-ROM | disk | FTP | other *** search
/ Amiga Times / AmigaTimes.iso / programme / GoldED / developer / examples / api / rexx / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-06  |  5.6 KB  |  222 lines

  1. /* -----------------------------------------------------------------------------
  2.  
  3.  ApiLib ©1995 Dietmar Eilert
  4.  
  5.  API example library. Dice:
  6.  
  7.  DMAKE
  8.  
  9.  -------------------------------------------------------------------------------
  10.  
  11. */
  12.  
  13. #include "defs.h"
  14.  
  15. /// "Prototype"
  16.  
  17. // library functions
  18.  
  19. Prototype LibCall struct APIClient *APIMountClient(__A0 struct APIMessage *, __A1 char *);
  20. Prototype LibCall void              APICloseClient(__A0 struct APIClient *, __A1 struct APIMessage *);
  21. Prototype LibCall void              APIBriefClient(__A0 struct APIClient *, __A1 struct APIMessage *);
  22. Prototype LibCall void              APIFree       (__A0 struct APIClient  *, __A1 struct APIOrder   *);
  23.  
  24. // private functions
  25.  
  26. Prototype void Dispatch    (struct APIMessage *);
  27. Prototype LONG CommandBeep (ULONG  *, struct APIMessage *);
  28. Prototype LONG CommandAbout(ULONG  *, struct APIMessage *);
  29.  
  30. ///
  31. /// "library functions"
  32.  
  33. LibCall struct APIClient *
  34. APIMountClient(__A0 struct APIMessage *apiMsg, __A1 char *args)
  35. {
  36.     static struct APIClient apiClient;
  37.     static UBYTE           *apiCommands[] = { "DISPLAYBEEP", "ABOUT", NULL };
  38.  
  39.     apiClient.api_APIVersion = API_INTERFACE_VERSION;
  40.     apiClient.api_Version    = 1;
  41.     apiClient.api_Name       = "Rexx API";
  42.     apiClient.api_Info       = "Rexx API example";
  43.     apiClient.api_Commands   = apiCommands;
  44.     apiClient.api_Serial     = 0;
  45.     apiClient.api_Classes    = API_CLASS_COMMAND | API_CLASS_SYSTEM;
  46.     apiClient.api_Area       = NULL;
  47.  
  48.     return(&apiClient);
  49. }
  50.  
  51. LibCall void
  52. APICloseClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  53. {
  54.     // no ressources to be freed
  55. }
  56.  
  57. LibCall void
  58. APIBriefClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
  59. {
  60.     struct APIMessage *msg;
  61.  
  62.     // handle host's command notify
  63.  
  64.     for (msg = apiMsg; msg; msg = msg->api_Next) {
  65.  
  66.         if (msg->api_State == API_STATE_NOTIFY) {
  67.  
  68.             switch (msg->api_Class) {
  69.  
  70.                 case API_CLASS_COMMAND:
  71.  
  72.                     if (msg->api_Class == API_CLASS_COMMAND) {
  73.  
  74.                         if (msg->api_Action == API_ACTION_COMMAND)
  75.                             Dispatch(msg);
  76.                         else
  77.                             msg->api_Error = API_ERROR_UNKNOWN;
  78.                     }
  79.  
  80.                     break;
  81.  
  82.                 case API_CLASS_SYSTEM:
  83.  
  84.                     break;
  85.  
  86.                 default:
  87.  
  88.                     msg->api_Error = API_ERROR_UNKNOWN;
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. LibCall void
  95. APIFree(__A0 struct APIClient *handle, __A1 struct APIOrder *apiOrder)
  96. {
  97.     // no ressources to be freed
  98. }
  99.  
  100. ///
  101. /// "private functions"
  102.  
  103. /* --------------------------------- Dispatch ----------------------------------
  104.  
  105.  Dispatch incoming command: examine command string (command part is uppercase
  106.  already), look for handler function related to command, parse arguments (if
  107.  command supports arguments), call handler.
  108.  
  109. */
  110.  
  111. void
  112. Dispatch(apiMsg)
  113.  
  114. struct APIMessage *apiMsg;
  115. {
  116.     struct RDArgs *rdArgs, *args;
  117.  
  118.     if (rdArgs = AllocDosObject(DOS_RDARGS, NULL)) {
  119.  
  120.         // table of supported commands, handlers & template strings
  121.  
  122.         static struct parser { char *command; LONG (*handler)(ULONG *, struct APIMessage *); char *template; } parser[] = {
  123.  
  124.             "DISPLAYBEEP",  (APTR)CommandBeep,  NULL,
  125.             "ABOUT",        (APTR)CommandAbout, NULL,
  126.              NULL
  127.         };
  128.  
  129.         ULONG n;
  130.         UBYTE buffer[80];
  131.         ULONG argArray[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  132.  
  133.         // make LF-terminated copy of command string (required by dos/readArgs):
  134.  
  135.         strncpy(buffer, apiMsg->api_Command, sizeof(buffer));
  136.         strcat (buffer, "\12");
  137.  
  138.         for (n = 0; parser[n].command; ++n) {
  139.  
  140.             if (memcmp(buffer, parser[n].command, strlen(parser[n].command)) == 0) {
  141.  
  142.                 UBYTE *arguments = buffer + strlen(parser[n].command);
  143.  
  144.                 // tell host that message has been consumed by us:
  145.  
  146.                 apiMsg->api_State = API_STATE_CONSUMED;
  147.  
  148.                 rdArgs->RDA_Source.CS_Buffer = arguments;
  149.                 rdArgs->RDA_Source.CS_Length = strlen(arguments);
  150.                 rdArgs->RDA_Source.CS_CurChr = 0;
  151.                 rdArgs->RDA_DAList           = NULL;
  152.                 rdArgs->RDA_Buffer           = NULL;
  153.  
  154.                 if (parser[n].template) {
  155.  
  156.                     if (args = ReadArgs(parser[n].template, argArray, rdArgs)) {
  157.  
  158.                         apiMsg->api_RC = (*parser[n].handler)(argArray, apiMsg);
  159.  
  160.                         FreeArgs(args);
  161.                     }
  162.                     else {
  163.  
  164.                         static UBYTE errorText[80 + 1];
  165.  
  166.                         apiMsg->api_RC           = RC_WARN;
  167.                         apiMsg->api_CommandError = errorText;
  168.  
  169.                         Fault(IoErr(), "IoErr()", errorText, 80);
  170.                     }
  171.                 }
  172.                 else
  173.                     apiMsg->api_RC = (*parser[n].handler)(argArray, apiMsg);
  174.             }
  175.         }
  176.  
  177.         FreeDosObject(DOS_RDARGS, rdArgs);
  178.     }
  179. }
  180.  
  181.  
  182. /* -------------------------------- CommandBeep --------------------------------
  183.  
  184.  DISPLAYBEEP
  185.  
  186. */
  187.  
  188. LONG
  189. CommandBeep(argArray, apiMsg)
  190.  
  191. ULONG  *argArray;
  192. struct APIMessage *apiMsg;
  193. {
  194.     DisplayBeep(0);
  195.  
  196.     return(RC_OK);
  197. }
  198.  
  199.  
  200. /* ------------------------------- CommandAbout --------------------------------
  201.  
  202.  ABOUT (open a requester on host's screen)
  203.  
  204. */
  205.  
  206. LONG
  207. CommandAbout(argArray, apiMsg)
  208.  
  209. ULONG  *argArray;
  210. struct APIMessage *apiMsg;
  211. {
  212.     static struct EasyStruct about = {sizeof(struct EasyStruct), 0, "Info", "Just some API client example code", "OK" };
  213.  
  214.     // note: apiMsg->api_Instance->api_Window may be NULL
  215.  
  216.     EasyRequestArgs(apiMsg->api_Instance->api_Window, &about, NULL, NULL);
  217.  
  218.     return(RC_OK);
  219. }
  220.  
  221. ///
  222.