]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/aux/antiword/startup.c
exec(2): fix prototypes
[plan9front.git] / sys / src / cmd / aux / antiword / startup.c
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 "DeskLib:Error.h"
13 #include "DeskLib:Event.h"
14 #include "DeskLib:SWI.h"
15 #include "antiword.h"
16
17
18 #if !defined(TaskManager_EnumerateTasks)
19 #define TaskManager_EnumerateTasks      0x042681
20 #endif /* TaskManager_EnumerateTasks */
21
22 /*
23  * bIsMatch - decide whether the two strings match
24  *
25  * like strcmp, but this one ignores case
26  */
27 static BOOL
28 bIsMatch(const char *szStr1, const char *szStr2)
29 {
30         const char      *pcTmp1, *pcTmp2;
31
32         for (pcTmp1 = szStr1, pcTmp2 = szStr2;
33              *pcTmp1 != '\0';
34              pcTmp1++, pcTmp2++) {
35                 if (toupper(*pcTmp1) != toupper(*pcTmp2)) {
36                         return FALSE;
37                 }
38         }
39         return *pcTmp2 == '\0';
40 } /* end of bIsMatch */
41
42 /*
43  * tGetTaskHandle - get the task handle of the given task
44  *
45  * returns the task handle when found, otherwise 0
46  */
47 static task_handle
48 tGetTaskHandle(const char *szTaskname)
49 {
50         const char      *pcTmp;
51         int     iReg0, iIndex;
52         int     aiBuffer[4];
53         char    szTmp[21];
54
55         iReg0 = 0;
56         do {
57                 /* Get info on the next task */
58                 Error_CheckFatal(SWI(3, 1, TaskManager_EnumerateTasks | XOS_Bit,
59                         iReg0, aiBuffer, sizeof(aiBuffer), &iReg0));
60                 /* Copy the (control character terminated) task name */
61                 for (iIndex = 0, pcTmp = (const char *)aiBuffer[1];
62                      iIndex < elementsof(szTmp);
63                      iIndex++, pcTmp++) {
64                         if (iscntrl(*pcTmp)) {
65                                 szTmp[iIndex] = '\0';
66                                 break;
67                         }
68                         szTmp[iIndex] = *pcTmp;
69                 }
70                 szTmp[elementsof(szTmp) - 1] = '\0';
71                 if (bIsMatch(szTmp, szTaskname)) {
72                         /* Task found */
73                         return (task_handle)aiBuffer[0];
74                 }
75         } while (iReg0 >= 0);
76
77         /* Task not found */
78         return 0;
79 } /* end of tGetTaskHandle */
80
81 int
82 main(int argc, char **argv)
83 {
84         message_block   tMsg;
85         task_handle     tTaskHandle;
86         size_t  tArgLen;
87         int     aiMessages[] = {0};
88         char    szCommand[512];
89
90         Event_Initialise3("StartUp", 310, aiMessages);
91
92         if (argc > 1) {
93                 tArgLen = strlen(argv[1]);
94         } else {
95                 tArgLen = 0;
96         }
97         if (tArgLen >= sizeof(tMsg.data.dataload.filename)) {
98                 werr(1, "Input filename too long");
99                 return EXIT_FAILURE;
100         }
101
102         tTaskHandle = tGetTaskHandle("antiword");
103
104         if (tTaskHandle == 0) {
105                 /* Antiword is not active */
106                 strcpy(szCommand, "chain:<Antiword$Dir>.!Antiword");
107                 if (argc > 1) {
108                         strcat(szCommand, " ");
109                         strcat(szCommand, argv[1]);
110                 }
111 #if defined(DEBUG)
112                 strcat(szCommand, " ");
113                 strcat(szCommand, "2><Antiword$Dir>.Debug");
114 #endif /* DEBUG */
115                 system(szCommand);
116                 /* If we reach here something has gone wrong */
117                 return EXIT_FAILURE;
118         }
119
120         /* Antiword is active */
121         if (argc > 1) {
122                 /*
123                  * Send the argument to Antiword by imitating a
124                  * drag-and-drop to Antiword's iconbar icon
125                  */
126                 memset(&tMsg, 0, sizeof(tMsg));
127                 tMsg.header.size = ROUND4(offsetof(message_block, data) +
128                                         offsetof(message_dataload, filename) +
129                                         1 + tArgLen);
130                 tMsg.header.yourref = 0;
131                 tMsg.header.action = message_DATALOAD;
132                 tMsg.data.dataload.window = window_ICONBAR;
133                 tMsg.data.dataload.icon = -1;
134                 tMsg.data.dataload.size = 0;
135                 tMsg.data.dataload.filetype = FILETYPE_MSWORD;
136                 strcpy(tMsg.data.dataload.filename, argv[1]);
137                 Error_CheckFatal(Wimp_SendMessage(event_SEND,
138                                                 &tMsg, tTaskHandle, 0));
139                 return EXIT_SUCCESS;
140         } else {
141                 /* Give an error message and return */
142                 werr(1, "Antiword is already running");
143                 return EXIT_FAILURE;
144         }
145 } /* end of main */