853
|
1 /* DDE client for XEmacs.
|
|
2 Copyright (C) 2002 Alastair J. Houghton
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Not in FSF. */
|
|
22
|
|
23 /* -- Includes -------------------------------------------------------------- */
|
|
24
|
|
25 #ifdef HAVE_CONFIG_H
|
|
26 # include <config.h>
|
|
27 #endif
|
2993
|
28 #include <windows.h>
|
|
29 #include <ddeml.h>
|
853
|
30 #include <stdlib.h>
|
|
31 #include <stdio.h>
|
|
32 #include <ctype.h>
|
|
33 #include <errno.h>
|
|
34
|
|
35 static void error (const char* s1, const char* s2);
|
|
36 static void fatal (const char *s1, const char *s2);
|
|
37 static void * xmalloc (size_t size);
|
|
38 static char * getNextArg (const char **ptr, unsigned *len);
|
|
39
|
|
40 /* -- Post-Include Defines -------------------------------------------------- */
|
|
41
|
|
42 /* Timeouts & delays */
|
|
43 #define CONNECT_DELAY 500 /* ms */
|
|
44 #define TRANSACTION_TIMEOUT 5000 /* ms */
|
|
45 #define MAX_INPUT_IDLE_WAIT INFINITE /* ms */
|
|
46
|
|
47 /* DDE Strings */
|
|
48 #define SERVICE_NAME "XEmacs"
|
|
49 #define TOPIC_NAME "System"
|
|
50 #define COMMAND_FORMAT "[open(\"%s%s\")]"
|
|
51
|
|
52 /* XEmacs program name */
|
|
53 #define PROGRAM_TO_RUN "xemacs.exe"
|
|
54
|
|
55 /* -- Constants ------------------------------------------------------------- */
|
|
56
|
|
57 /* -- Global Variables ------------------------------------------------------ */
|
|
58
|
|
59 HINSTANCE hInstance;
|
|
60 DWORD idInst = 0;
|
|
61
|
|
62 /* -- Function Declarations ------------------------------------------------- */
|
|
63
|
|
64 HDDEDATA CALLBACK ddeCallback (UINT uType, UINT uFmt, HCONV hconv,
|
|
65 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
|
|
66 DWORD dwData1, DWORD dwData2);
|
|
67
|
|
68 int WINAPI WinMain (HINSTANCE hInst,
|
|
69 HINSTANCE hPrev,
|
|
70 LPSTR lpCmdLine,
|
|
71 int nCmdShow);
|
|
72
|
|
73 static HCONV openConversation (void);
|
|
74 static void closeConversation (HCONV hConv);
|
|
75 static int doFile (HCONV hConv, LPSTR lpszFileName1, LPSTR lpszFileName2);
|
|
76 static int parseCommandLine (HCONV hConv, LPSTR lpszCommandLine);
|
|
77
|
|
78 /* -- Function Definitions -------------------------------------------------- */
|
|
79
|
|
80 /*
|
|
81 * Name : ddeCallback
|
|
82 * Function: Gets called by DDEML.
|
|
83 *
|
|
84 */
|
|
85
|
|
86 HDDEDATA CALLBACK
|
|
87 ddeCallback (UINT uType, UINT uFmt, HCONV hconv,
|
|
88 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
|
|
89 DWORD dwData1, DWORD dwData2)
|
|
90 {
|
|
91 return (HDDEDATA) NULL;
|
|
92 }
|
|
93
|
|
94 /*
|
|
95 * Name : WinMain
|
|
96 * Function: The program's entry point function.
|
|
97 *
|
|
98 */
|
|
99
|
|
100 int WINAPI
|
|
101 WinMain (HINSTANCE hInst,
|
|
102 HINSTANCE hPrev,
|
|
103 LPSTR lpCmdLine,
|
|
104 int nCmdShow)
|
|
105 {
|
|
106 HCONV hConv;
|
|
107 int ret = 0;
|
|
108 UINT uiRet;
|
|
109
|
|
110 /* Initialise the DDEML library */
|
|
111 uiRet = DdeInitialize (&idInst,
|
|
112 (PFNCALLBACK) ddeCallback,
|
|
113 APPCMD_CLIENTONLY
|
|
114 |CBF_FAIL_ALLSVRXACTIONS,
|
|
115 0);
|
|
116
|
|
117 if (uiRet != DMLERR_NO_ERROR)
|
|
118 {
|
|
119 MessageBox (NULL, "Could not initialise DDE management library.",
|
|
120 "winclient", MB_ICONEXCLAMATION | MB_OK);
|
|
121
|
|
122 return 1;
|
|
123 }
|
|
124
|
|
125 /* Open a conversation */
|
|
126 hConv = openConversation ();
|
|
127
|
|
128 if (hConv)
|
|
129 {
|
|
130 /* OK. Next, we need to parse the command line. */
|
|
131 ret = parseCommandLine (hConv, lpCmdLine);
|
|
132
|
|
133 /* Close the conversation */
|
|
134 closeConversation (hConv);
|
|
135 }
|
|
136
|
|
137 DdeUninitialize (idInst);
|
|
138
|
|
139 return ret;
|
|
140 }
|
|
141
|
|
142 /*
|
|
143 * Name : openConversation
|
|
144 * Function: Start a conversation.
|
|
145 *
|
|
146 */
|
|
147
|
|
148 static HCONV
|
|
149 openConversation (void)
|
|
150 {
|
|
151 HSZ hszService = NULL, hszTopic = NULL;
|
|
152 HCONV hConv = NULL;
|
|
153
|
|
154 /* Get the application (service) name */
|
|
155 hszService = DdeCreateStringHandle (idInst,
|
|
156 SERVICE_NAME,
|
|
157 CP_WINANSI);
|
|
158
|
|
159 if (!hszService)
|
|
160 {
|
|
161 MessageBox (NULL, "Could not create string handle for service.",
|
|
162 "winclient", MB_ICONEXCLAMATION | MB_OK);
|
|
163
|
|
164 goto error;
|
|
165 }
|
|
166
|
|
167 /* Get the topic name */
|
|
168 hszTopic = DdeCreateStringHandle (idInst,
|
|
169 TOPIC_NAME,
|
|
170 CP_WINANSI);
|
|
171
|
|
172 if (!hszTopic)
|
|
173 {
|
|
174 MessageBox (NULL, "Could not create string handle for topic.",
|
|
175 "winclient", MB_ICONEXCLAMATION | MB_OK);
|
|
176
|
|
177 goto error;
|
|
178 }
|
|
179
|
|
180 /* Try to connect */
|
|
181 hConv = DdeConnect (idInst, hszService, hszTopic, NULL);
|
|
182
|
|
183 if (!hConv)
|
|
184 {
|
|
185 STARTUPINFO sti;
|
|
186 PROCESS_INFORMATION pi;
|
|
187 int n;
|
|
188
|
|
189 /* Try to start the program */
|
|
190 ZeroMemory (&sti, sizeof (sti));
|
|
191 sti.cb = sizeof (sti);
|
|
192 if (!CreateProcess (NULL, PROGRAM_TO_RUN, NULL, NULL, FALSE, 0,
|
|
193 NULL, NULL, &sti, &pi))
|
|
194 {
|
|
195 MessageBox (NULL, "Could not start process.",
|
|
196 "winclient", MB_ICONEXCLAMATION | MB_OK);
|
|
197
|
|
198 goto error;
|
|
199 }
|
|
200
|
|
201 /* Wait for the process to enter an idle state */
|
|
202 WaitForInputIdle (pi.hProcess, MAX_INPUT_IDLE_WAIT);
|
|
203
|
|
204 /* Close the handles */
|
|
205 CloseHandle (pi.hThread);
|
|
206 CloseHandle (pi.hProcess);
|
|
207
|
|
208 /* Try to connect */
|
|
209 for (n = 0; n < 5; n++)
|
|
210 {
|
|
211 Sleep (CONNECT_DELAY);
|
|
212
|
|
213 hConv = DdeConnect (idInst, hszService, hszTopic, NULL);
|
|
214
|
|
215 if (hConv)
|
|
216 break;
|
|
217 }
|
|
218
|
|
219 if (!hConv)
|
|
220 {
|
|
221 /* Still couldn't connect. */
|
|
222 MessageBox (NULL, "Could not connect to DDE server.",
|
|
223 "winclient", MB_ICONEXCLAMATION | MB_OK);
|
|
224
|
|
225 goto error;
|
|
226 }
|
|
227 }
|
|
228
|
|
229 /* Release the string handles */
|
|
230 DdeFreeStringHandle (idInst, hszService);
|
|
231 DdeFreeStringHandle (idInst, hszTopic);
|
|
232
|
|
233 return hConv;
|
|
234
|
|
235 error:
|
|
236 if (hConv)
|
|
237 DdeDisconnect (hConv);
|
|
238 if (hszService)
|
|
239 DdeFreeStringHandle (idInst, hszService);
|
|
240 if (hszTopic)
|
|
241 DdeFreeStringHandle (idInst, hszTopic);
|
|
242
|
|
243 return NULL;
|
|
244 }
|
|
245
|
|
246 /*
|
|
247 * Name : closeConversation
|
|
248 * Function: Close a conversation.
|
|
249 *
|
|
250 */
|
|
251
|
|
252 static void
|
|
253 closeConversation (HCONV hConv)
|
|
254 {
|
|
255 /* Shut down */
|
|
256 DdeDisconnect (hConv);
|
|
257 }
|
|
258
|
|
259 /*
|
|
260 * Name : doFile
|
|
261 * Function: Process a file.
|
|
262 *
|
|
263 */
|
|
264
|
|
265 int
|
|
266 doFile (HCONV hConv, LPSTR lpszFileName1, LPSTR lpszFileName2)
|
|
267 {
|
|
268 char *buf = NULL;
|
|
269 unsigned len;
|
|
270
|
|
271 /* Calculate the buffer length */
|
|
272 len = strlen (lpszFileName1) + strlen (lpszFileName2)
|
|
273 + strlen (COMMAND_FORMAT);
|
|
274
|
|
275 /* Allocate a buffer */
|
|
276 buf = (char *) xmalloc (len);
|
|
277
|
|
278 if (!buf)
|
|
279 {
|
|
280 MessageBox (NULL, "Not enough memory.",
|
|
281 "winclient", MB_ICONEXCLAMATION | MB_OK);
|
|
282
|
|
283 return 1;
|
|
284 }
|
|
285
|
|
286 /* Build the command */
|
|
287 len = wsprintf (buf, COMMAND_FORMAT, lpszFileName1, lpszFileName2);
|
|
288
|
|
289 len++;
|
|
290
|
|
291 /* OK. We're connected. Send the message. */
|
|
292 DdeClientTransaction (buf, len, hConv, NULL,
|
|
293 0, XTYP_EXECUTE, TRANSACTION_TIMEOUT, NULL);
|
|
294
|
|
295 free (buf);
|
|
296
|
|
297 return 0;
|
|
298 }
|
|
299
|
|
300 /*
|
|
301 * Name : getNextArg
|
|
302 * Function: Retrieve the next command line argument.
|
|
303 *
|
|
304 */
|
|
305
|
|
306 static char *
|
|
307 getNextArg (const char **ptr, unsigned *len)
|
|
308 {
|
|
309 int in_quotes = 0, quit = 0, all_in_quotes = 0;
|
|
310 const char *p = *ptr, *start;
|
|
311 char *buf = NULL;
|
|
312 unsigned length = 0;
|
|
313
|
|
314 /* Skip whitespace */
|
|
315 while (*p && isspace (*p))
|
|
316 p++;
|
|
317
|
|
318 /* If this is the end, return NULL */
|
|
319 if (!*p)
|
|
320 return NULL;
|
|
321
|
|
322 /* Remember where we are */
|
|
323 start = p;
|
|
324
|
|
325 /* Find the next whitespace character outside quotes */
|
|
326 if (*p == '"')
|
|
327 all_in_quotes = 1;
|
|
328
|
|
329 while (*p && !quit)
|
|
330 {
|
|
331 switch (*p)
|
|
332 {
|
|
333 case '"':
|
|
334 in_quotes = 1 - in_quotes;
|
|
335 p++;
|
|
336 break;
|
|
337
|
|
338 case '\\':
|
|
339 if (!in_quotes)
|
|
340 all_in_quotes = 0;
|
|
341
|
|
342 p++;
|
|
343
|
|
344 if (!*p)
|
|
345 break;
|
|
346
|
|
347 p++;
|
|
348 break;
|
|
349
|
|
350 default:
|
|
351 if (isspace (*p) && !in_quotes)
|
|
352 quit = 1;
|
|
353 else if (!in_quotes)
|
|
354 all_in_quotes = 0;
|
|
355
|
|
356 if (!quit)
|
|
357 p++;
|
|
358 }
|
|
359 }
|
|
360
|
|
361 /* Work out the length */
|
|
362 length = p - start;
|
|
363
|
|
364 /* Strip quotes if the argument is completely quoted */
|
|
365 if (all_in_quotes)
|
|
366 {
|
|
367 start++;
|
|
368 length -= 2;
|
|
369 }
|
|
370
|
|
371 /* Copy */
|
|
372 buf = (char *) xmalloc (length + 1);
|
|
373
|
|
374 if (!buf)
|
|
375 return NULL;
|
|
376
|
|
377 strncpy (buf, start, length);
|
|
378 buf[length] = '\0';
|
|
379
|
|
380 /* Return the pointer and length */
|
|
381 *ptr = p;
|
|
382 *len = length;
|
|
383
|
|
384 return buf;
|
|
385 }
|
|
386
|
|
387 /*
|
|
388 * Name : parseCommandLine
|
|
389 * Function: Process the command line. This program accepts a list of strings
|
|
390 * : (which may contain wildcards) representing filenames.
|
|
391 *
|
|
392 */
|
|
393
|
|
394 int
|
|
395 parseCommandLine (HCONV hConv, LPSTR lpszCommandLine)
|
|
396 {
|
|
397 char *fullpath, *filepart;
|
|
398 char *arg;
|
|
399 unsigned len, pathlen;
|
|
400 int ret = 0;
|
|
401 HANDLE hFindFile = NULL;
|
|
402 WIN32_FIND_DATA wfd;
|
|
403
|
|
404 /* Retrieve arguments */
|
|
405 while ((arg = getNextArg ((const char**)&lpszCommandLine, &len)) != NULL)
|
|
406 {
|
|
407 /* First find the canonical path name */
|
|
408 fullpath = filepart = NULL;
|
|
409 pathlen = GetFullPathName (arg, 0, fullpath, &filepart);
|
|
410
|
|
411 fullpath = (char *) xmalloc (pathlen);
|
|
412
|
|
413 if (!fullpath)
|
|
414 {
|
|
415 MessageBox (NULL, "Not enough memory.", "winclient",
|
|
416 MB_ICONEXCLAMATION | MB_OK);
|
|
417
|
|
418 ret = 1;
|
|
419 free (arg);
|
|
420
|
|
421 break;
|
|
422 }
|
|
423
|
|
424 GetFullPathName (arg, pathlen, fullpath, &filepart);
|
|
425
|
|
426 /* Find the first matching file */
|
|
427 hFindFile = FindFirstFile (arg, &wfd);
|
|
428
|
|
429 if (hFindFile == INVALID_HANDLE_VALUE)
|
|
430 ret = doFile (hConv, fullpath, "");
|
|
431 else
|
|
432 {
|
|
433 /* Chop off the file part from the full path name */
|
|
434 if (filepart)
|
|
435 *filepart = '\0';
|
|
436
|
|
437 /* For each matching file */
|
|
438 do
|
|
439 {
|
|
440 /* Process it */
|
|
441 ret = doFile (hConv, fullpath, wfd.cFileName);
|
|
442
|
|
443 if (ret)
|
|
444 break;
|
|
445 }
|
|
446 while (FindNextFile (hFindFile, &wfd));
|
|
447
|
|
448 FindClose (hFindFile);
|
|
449 }
|
|
450
|
|
451 /* Release the path name buffers */
|
|
452 free (fullpath);
|
|
453 free (arg);
|
|
454
|
|
455 if (ret)
|
|
456 break;
|
|
457 }
|
|
458
|
|
459 return ret;
|
|
460 }
|
|
461
|
|
462 static void
|
|
463 fatal (const char *s1, const char *s2)
|
|
464 {
|
|
465 error (s1, s2);
|
|
466 exit (1);
|
|
467 }
|
|
468
|
|
469 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
|
|
470 static void
|
|
471 error (const char* s1, const char* s2)
|
|
472 {
|
|
473 fprintf (stderr, "winclient: ");
|
|
474 fprintf (stderr, s1, s2);
|
|
475 fprintf (stderr, "\n");
|
|
476 }
|
|
477
|
|
478 /* Like malloc but get fatal error if memory is exhausted. */
|
|
479
|
|
480 static void *
|
|
481 xmalloc (size_t size)
|
|
482 {
|
|
483 void *result = malloc (size);
|
|
484 if (result == NULL)
|
|
485 fatal ("virtual memory exhausted", (char *) 0);
|
|
486 return result;
|
|
487 }
|