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