home *** CD-ROM | disk | FTP | other *** search
- /* -----------------------------------------------------------------------------
-
- ApiLib ©1995 Dietmar Eilert
-
- API example library. Dice:
-
- DMAKE
-
- -------------------------------------------------------------------------------
-
- */
-
- #include "defs.h"
-
- /// "Prototype"
-
- // library functions
-
- Prototype LibCall struct APIClient *APIMountClient(__A0 struct APIMessage *, __A1 char *);
- Prototype LibCall void APICloseClient(__A0 struct APIClient *, __A1 struct APIMessage *);
- Prototype LibCall void APIBriefClient(__A0 struct APIClient *, __A1 struct APIMessage *);
-
- // private functions
-
- Prototype void Dispatch (struct APIMessage *);
- Prototype LONG CommandBeep (ULONG *, struct APIMessage *);
- Prototype LONG CommandAbout(ULONG *, struct APIMessage *);
-
- ///
- /// "library functions"
-
- LibCall struct APIClient *
- APIMountClient(__A0 struct APIMessage *apiMsg, __A1 char *args)
- {
- static struct APIClient apiClient;
- static UBYTE *apiCommands[] = { "DISPLAYBEEP", "ABOUT", NULL };
-
- apiClient.api_APIVersion = API_INTERFACE_VERSION;
- apiClient.api_Version = 1;
- apiClient.api_Name = "Rexx API";
- apiClient.api_Info = "Rexx API example";
- apiClient.api_Commands = apiCommands;
- apiClient.api_Serial = 0;
- apiClient.api_Classes = API_CLASS_COMMAND | API_CLASS_SYSTEM;
- apiClient.api_Area = NULL;
-
- return(&apiClient);
- }
-
- LibCall void
- APICloseClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
- {
- // no ressources to be freed
- }
-
- LibCall void
- APIBriefClient(__A0 struct APIClient *handle, __A1 struct APIMessage *apiMsg)
- {
- struct APIMessage *msg;
-
- // handle host's command notify
-
- for (msg = apiMsg; msg; msg = msg->api_Next) {
-
- if (msg->api_State == API_STATE_NOTIFY) {
-
- switch (msg->api_Class) {
-
- case API_CLASS_COMMAND:
-
- if (msg->api_Class == API_CLASS_COMMAND) {
-
- if (msg->api_Action == API_ACTION_COMMAND)
- Dispatch(msg);
- else
- msg->api_Error = API_ERROR_UNKNOWN;
- }
-
- break;
-
- case API_CLASS_SYSTEM:
-
- break;
-
- default:
-
- msg->api_Error = API_ERROR_UNKNOWN;
- }
- }
- }
- }
-
- ///
- /// "private functions"
-
- /* --------------------------------- Dispatch ----------------------------------
-
- Dispatch incoming command: examine command string (command part is uppercase
- already), look for handler function related to command, parse arguments (if
- command supports arguments), call handler.
-
- */
-
- void
- Dispatch(apiMsg)
-
- struct APIMessage *apiMsg;
- {
- struct RDArgs *rdArgs, *args;
-
- if (rdArgs = AllocDosObject(DOS_RDARGS, NULL)) {
-
- // table of supported commands, handlers & template strings
-
- static struct parser { char *command; LONG (*handler)(ULONG *, struct APIMessage *); char *template; } parser[] = {
-
- "DISPLAYBEEP", (APTR)CommandBeep, NULL,
- "ABOUT", (APTR)CommandAbout, NULL,
- NULL
- };
-
- ULONG n;
- UBYTE buffer[80];
- ULONG argArray[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-
- // make LF-terminated copy of command string (required by dos/readArgs):
-
- strncpy(buffer, apiMsg->api_Command, sizeof(buffer));
- strcat (buffer, "\12");
-
- for (n = 0; parser[n].command; ++n) {
-
- if (memcmp(buffer, parser[n].command, strlen(parser[n].command)) == 0) {
-
- UBYTE *arguments = buffer + strlen(parser[n].command);
-
- // tell host that message has been consumed by us:
-
- apiMsg->api_State = API_STATE_CONSUMED;
-
- rdArgs->RDA_Source.CS_Buffer = arguments;
- rdArgs->RDA_Source.CS_Length = strlen(arguments);
- rdArgs->RDA_Source.CS_CurChr = 0;
- rdArgs->RDA_DAList = NULL;
- rdArgs->RDA_Buffer = NULL;
-
- if (parser[n].template) {
-
- if (args = ReadArgs(parser[n].template, argArray, rdArgs)) {
-
- apiMsg->api_RC = (*parser[n].handler)(argArray, apiMsg);
-
- FreeArgs(args);
- }
- else {
-
- static UBYTE errorText[80 + 1];
-
- apiMsg->api_RC = RC_WARN;
- apiMsg->api_CommandError = errorText;
-
- Fault(IoErr(), "IoErr()", errorText, 80);
- }
- }
- else
- apiMsg->api_RC = (*parser[n].handler)(argArray, apiMsg);
- }
- }
-
- FreeDosObject(DOS_RDARGS, rdArgs);
- }
- }
-
-
- /* -------------------------------- CommandBeep --------------------------------
-
- DISPLAYBEEP
-
- */
-
- LONG
- CommandBeep(argArray, apiMsg)
-
- ULONG *argArray;
- struct APIMessage *apiMsg;
- {
- DisplayBeep(0);
-
- return(RC_OK);
- }
-
-
- /* ------------------------------- CommandAbout --------------------------------
-
- ABOUT (open a requester on host's screen)
-
- */
-
- LONG
- CommandAbout(argArray, apiMsg)
-
- ULONG *argArray;
- struct APIMessage *apiMsg;
- {
- static struct EasyStruct about = {sizeof(struct EasyStruct), 0, "Info", "Just some API client example code", "OK" };
-
- // note: apiMsg->api_Instance->api_Window may be NULL
-
- EasyRequestArgs(apiMsg->api_Instance->api_Window, &about, NULL, NULL);
-
- return(RC_OK);
- }
-
- ///
-