home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / startup.c < prev    next >
C/C++ Source or Header  |  2001-04-22  |  4KB  |  160 lines

  1. /*
  2.  * startup.c
  3.  * Copyright (C) 1998-2001 A.J. van Os; Released under GPL
  4.  *
  5.  * Description:
  6.  * Try to force a single startup of !Antiword
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include "kernel.h"
  13. #include "swis.h"
  14. #include "wimp.h"
  15. #include "wimpt.h"
  16. #include "antiword.h"
  17.  
  18.  
  19. #if !defined(TaskManager_EnumerateTasks)
  20. #define TaskManager_EnumerateTasks    0x042681
  21. #endif /* TaskManager_EnumerateTasks */
  22.  
  23. /*
  24.  * bIsMatch - decide whether the two strings match
  25.  *
  26.  * like strcmp, but this one ignores case
  27.  */
  28. static BOOL
  29. bIsMatch(const char *szStr1, const char *szStr2)
  30. {
  31.     const char    *pcTmp1, *pcTmp2;
  32.  
  33.     for (pcTmp1 = szStr1, pcTmp2 = szStr2;
  34.          *pcTmp1 != '\0';
  35.          pcTmp1++, pcTmp2++) {
  36.         if (toupper(*pcTmp1) != toupper(*pcTmp2)) {
  37.             return FALSE;
  38.         }
  39.     }
  40.     return *pcTmp2 == '\0';
  41. } /* end of bIsMatch */
  42.  
  43. /*
  44.  * tGetTaskHandle - get the task handle of the given task
  45.  *
  46.  * returns the task handle when found, otherwise 0
  47.  */
  48. static wimp_t
  49. tGetTaskHandle(const char *szTaskname, int iOSVersion)
  50. {
  51.     _kernel_swi_regs    regs;
  52.     _kernel_oserror        *e;
  53.     const char    *pcTmp;
  54.     int    iIndex;
  55.     int    aiBuffer[4];
  56.     char    szTmp[21];
  57.  
  58.     if (iOSVersion < 310) {
  59.         /*
  60.          * SWI TaskManager_EnumerateTasks does not
  61.          * exist in earlier versions of RISC OS
  62.          */
  63.         return 0;
  64.     }
  65.  
  66.     (void)memset((void *)®s, 0, sizeof(regs));
  67.     regs.r[0] = 0;
  68.  
  69.     do {
  70.         /* Get info on the next task */
  71.         regs.r[1] = (int)aiBuffer;
  72.         regs.r[2] = sizeof(aiBuffer);
  73.         e = _kernel_swi(TaskManager_EnumerateTasks, ®s, ®s);
  74.         if (e != NULL) {
  75.             werr(1, "TaskManager_EnumerateTasks error %d: %s",
  76.                 e->errnum, e->errmess);
  77.             exit(EXIT_FAILURE);
  78.         }
  79.         /* Copy the (control character terminated) task name */
  80.         for (iIndex = 0, pcTmp = (const char *)aiBuffer[1];
  81.              iIndex < elementsof(szTmp);
  82.              iIndex++, pcTmp++) {
  83.             if (iscntrl(*pcTmp)) {
  84.                 szTmp[iIndex] = '\0';
  85.                 break;
  86.             }
  87.             szTmp[iIndex] = *pcTmp;
  88.         }
  89.         szTmp[elementsof(szTmp) - 1] = '\0';
  90.         if (bIsMatch(szTmp, szTaskname)) {
  91.             /* Task found */
  92.             return (wimp_t)aiBuffer[0];
  93.         }
  94.     } while (regs.r[0] >= 0);
  95.  
  96.     /* Task not found */
  97.     return 0;
  98. } /* end of tGetTaskHandle */
  99.  
  100. int
  101. main(int argc, char **argv)
  102. {
  103.     wimp_msgstr    tMsg;
  104.     wimp_t    tTaskHandle;
  105.     size_t    tArgLen;
  106.     int    iVersion;
  107.     char    szCommand[512];
  108.  
  109.     iVersion = wimpt_init("StartUp");
  110.  
  111.     if (argc > 1) {
  112.         tArgLen = strlen(argv[1]);
  113.     } else {
  114.         tArgLen = 0;
  115.     }
  116.     if (tArgLen >= sizeof(tMsg.data.dataload.name)) {
  117.         werr(1, "Input filename too long");
  118.         return EXIT_FAILURE;
  119.     }
  120.  
  121.     tTaskHandle = tGetTaskHandle("antiword", iVersion);
  122.  
  123.     if (tTaskHandle == 0) {
  124.         /* Antiword is not active */
  125.         strcpy(szCommand, "chain:<Antiword$Dir>.!Antiword");
  126.         if (argc > 1) {
  127.             strcat(szCommand, " ");
  128.             strcat(szCommand, argv[1]);
  129.         }
  130. #if defined(DEBUG)
  131.         strcat(szCommand, " ");
  132.         strcat(szCommand, "2><Antiword$Dir>.Debug");
  133. #endif /* DEBUG */
  134.         system(szCommand);
  135.         /* If we reach here something has gone wrong */
  136.         return EXIT_FAILURE;
  137.     }
  138.  
  139.     /* Antiword is active */
  140.     if (argc > 1) {
  141.         /* Send the argument to Antiword */
  142.         tMsg.hdr.size = ROUND4(sizeof(tMsg) -
  143.                     sizeof(tMsg.data.dataload.name) +
  144.                     1 + tArgLen);
  145.         tMsg.hdr.your_ref = 0;
  146.         tMsg.hdr.action = wimp_MDATALOAD;
  147.         tMsg.data.dataload.w = -1;
  148.         tMsg.data.dataload.size = 0;
  149.         tMsg.data.dataload.type = FILETYPE_MSWORD;
  150.         strcpy(tMsg.data.dataload.name, argv[1]);
  151.         wimpt_noerr(wimp_sendmessage(wimp_ESEND,
  152.                         &tMsg, tTaskHandle));
  153.         return EXIT_SUCCESS;
  154.     } else {
  155.         /* Give an error message and return */
  156.         werr(1, "Antiword is already running");
  157.         return EXIT_FAILURE;
  158.     }
  159. } /* end of main */
  160.