examples/PIPS/antiword/src/startup.c

00001 /*
00002  * startup.c
00003  * Copyright (C) 1998-2001 A.J. van Os; Released under GPL
00004  *
00005  * Description:
00006  * Try to force a single startup of !Antiword
00007  */
00008 
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <ctype.h>
00012 #include "DeskLib:Error.h"
00013 #include "DeskLib:Event.h"
00014 #include "DeskLib:SWI.h"
00015 #include "antiword.h"
00016 
00017 
00018 #if !defined(TaskManager_EnumerateTasks)
00019 #define TaskManager_EnumerateTasks      0x042681
00020 #endif /* TaskManager_EnumerateTasks */
00021 
00022 /*
00023  * bIsMatch - decide whether the two strings match
00024  *
00025  * like strcmp, but this one ignores case
00026  */
00027 static BOOL
00028 bIsMatch(const char *szStr1, const char *szStr2)
00029 {
00030         const char      *pcTmp1, *pcTmp2;
00031 
00032         for (pcTmp1 = szStr1, pcTmp2 = szStr2;
00033              *pcTmp1 != '\0';
00034              pcTmp1++, pcTmp2++) {
00035                 if (toupper(*pcTmp1) != toupper(*pcTmp2)) {
00036                         return FALSE;
00037                 }
00038         }
00039         return *pcTmp2 == '\0';
00040 } /* end of bIsMatch */
00041 
00042 /*
00043  * tGetTaskHandle - get the task handle of the given task
00044  *
00045  * returns the task handle when found, otherwise 0
00046  */
00047 static task_handle
00048 tGetTaskHandle(const char *szTaskname)
00049 {
00050         const char      *pcTmp;
00051         int     iReg0, iIndex;
00052         int     aiBuffer[4];
00053         char    szTmp[21];
00054 
00055         iReg0 = 0;
00056         do {
00057                 /* Get info on the next task */
00058                 Error_CheckFatal(SWI(3, 1, TaskManager_EnumerateTasks | XOS_Bit,
00059                         iReg0, aiBuffer, sizeof(aiBuffer), &iReg0));
00060                 /* Copy the (control character terminated) task name */
00061                 for (iIndex = 0, pcTmp = (const char *)aiBuffer[1];
00062                      iIndex < elementsof(szTmp);
00063                      iIndex++, pcTmp++) {
00064                         if (iscntrl(*pcTmp)) {
00065                                 szTmp[iIndex] = '\0';
00066                                 break;
00067                         }
00068                         szTmp[iIndex] = *pcTmp;
00069                 }
00070                 szTmp[elementsof(szTmp) - 1] = '\0';
00071                 if (bIsMatch(szTmp, szTaskname)) {
00072                         /* Task found */
00073                         return (task_handle)aiBuffer[0];
00074                 }
00075         } while (iReg0 >= 0);
00076 
00077         /* Task not found */
00078         return 0;
00079 } /* end of tGetTaskHandle */
00080 
00081 int
00082 main(int argc, char **argv)
00083 {
00084         message_block   tMsg;
00085         task_handle     tTaskHandle;
00086         size_t  tArgLen;
00087         int     aiMessages[] = {0};
00088         char    szCommand[512];
00089 
00090         Event_Initialise3("StartUp", 310, aiMessages);
00091 
00092         if (argc > 1) {
00093                 tArgLen = strlen(argv[1]);
00094         } else {
00095                 tArgLen = 0;
00096         }
00097         if (tArgLen >= sizeof(tMsg.data.dataload.filename)) {
00098                 werr(1, "Input filename too long");
00099                 return EXIT_FAILURE;
00100         }
00101 
00102         tTaskHandle = tGetTaskHandle("antiword");
00103 
00104         if (tTaskHandle == 0) {
00105                 /* Antiword is not active */
00106                 strcpy(szCommand, "chain:<Antiword$Dir>.!Antiword");
00107                 if (argc > 1) {
00108                         strcat(szCommand, " ");
00109                         strcat(szCommand, argv[1]);
00110                 }
00111 #if defined(DEBUG)
00112                 strcat(szCommand, " ");
00113                 strcat(szCommand, "2><Antiword$Dir>.Debug");
00114 #endif /* DEBUG */
00115                 system(szCommand);
00116                 /* If we reach here something has gone wrong */
00117                 return EXIT_FAILURE;
00118         }
00119 
00120         /* Antiword is active */
00121         if (argc > 1) {
00122                 /*
00123                  * Send the argument to Antiword by imitating a
00124                  * drag-and-drop to Antiword's iconbar icon
00125                  */
00126                 memset(&tMsg, 0, sizeof(tMsg));
00127                 tMsg.header.size = ROUND4(offsetof(message_block, data) +
00128                                         offsetof(message_dataload, filename) +
00129                                         1 + tArgLen);
00130                 tMsg.header.yourref = 0;
00131                 tMsg.header.action = message_DATALOAD;
00132                 tMsg.data.dataload.window = window_ICONBAR;
00133                 tMsg.data.dataload.icon = -1;
00134                 tMsg.data.dataload.size = 0;
00135                 tMsg.data.dataload.filetype = FILETYPE_MSWORD;
00136                 strcpy(tMsg.data.dataload.filename, argv[1]);
00137                 Error_CheckFatal(Wimp_SendMessage(event_SEND,
00138                                                 &tMsg, tTaskHandle, 0));
00139                 return EXIT_SUCCESS;
00140         } else {
00141                 /* Give an error message and return */
00142                 werr(1, "Antiword is already running");
00143                 return EXIT_FAILURE;
00144         }
00145 } /* end of main */

Generated by  doxygen 1.6.2