Imported from antiword-0.34.tar.gz.
[antiword.git] / startup.c
blobc4f9a20d542394a8109c0fabc8c8e689c2001446
1 /*
2 * startup.c
3 * Copyright (C) 1998-2001 A.J. van Os; Released under GPL
5 * Description:
6 * Try to force a single startup of !Antiword
7 */
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"
19 #if !defined(TaskManager_EnumerateTasks)
20 #define TaskManager_EnumerateTasks 0x042681
21 #endif /* TaskManager_EnumerateTasks */
24 * bIsMatch - decide whether the two strings match
26 * like strcmp, but this one ignores case
28 static BOOL
29 bIsMatch(const char *szStr1, const char *szStr2)
31 const char *pcTmp1, *pcTmp2;
33 for (pcTmp1 = szStr1, pcTmp2 = szStr2;
34 *pcTmp1 != '\0';
35 pcTmp1++, pcTmp2++) {
36 if (toupper(*pcTmp1) != toupper(*pcTmp2)) {
37 return FALSE;
40 return *pcTmp2 == '\0';
41 } /* end of bIsMatch */
44 * tGetTaskHandle - get the task handle of the given task
46 * returns the task handle when found, otherwise 0
48 static wimp_t
49 tGetTaskHandle(const char *szTaskname, int iOSVersion)
51 _kernel_swi_regs regs;
52 _kernel_oserror *e;
53 const char *pcTmp;
54 int iIndex;
55 int aiBuffer[4];
56 char szTmp[21];
58 if (iOSVersion < 310) {
60 * SWI TaskManager_EnumerateTasks does not
61 * exist in earlier versions of RISC OS
63 return 0;
66 (void)memset((void *)&regs, 0, sizeof(regs));
67 regs.r[0] = 0;
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, &regs, &regs);
74 if (e != NULL) {
75 werr(1, "TaskManager_EnumerateTasks error %d: %s",
76 e->errnum, e->errmess);
77 exit(EXIT_FAILURE);
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;
87 szTmp[iIndex] = *pcTmp;
89 szTmp[elementsof(szTmp) - 1] = '\0';
90 if (bIsMatch(szTmp, szTaskname)) {
91 /* Task found */
92 return (wimp_t)aiBuffer[0];
94 } while (regs.r[0] >= 0);
96 /* Task not found */
97 return 0;
98 } /* end of tGetTaskHandle */
101 main(int argc, char **argv)
103 wimp_msgstr tMsg;
104 wimp_t tTaskHandle;
105 size_t tArgLen;
106 int iVersion;
107 char szCommand[512];
109 iVersion = wimpt_init("StartUp");
111 if (argc > 1) {
112 tArgLen = strlen(argv[1]);
113 } else {
114 tArgLen = 0;
116 if (tArgLen >= sizeof(tMsg.data.dataload.name)) {
117 werr(1, "Input filename too long");
118 return EXIT_FAILURE;
121 tTaskHandle = tGetTaskHandle("antiword", iVersion);
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]);
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;
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;
159 } /* end of main */