Debian Codegraph @

generic/bltWinDde.c

raw symbols SCIP
1
2 /*
3 * bltWinDde.c --
4 *
5 * This file provides procedures that implement the "send"
6 * command, allowing commands to be passed from interpreter
7 * to interpreter.
8 *
9 * Copyright (c) 1997 by Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: bltWinDde.c,v 1.1.1.1 2009/05/09 16:27:07 pcmacdon Exp $
15 */
16
17 #include "bltInt.h"
18
19 #ifndef NO_DDE
20
21 #include <ddeml.h>
22
23 /*
24 * The following structure is used to keep track of the interpreters
25 * registered by this process.
26 */
27
28 typedef struct RegisteredInterp {
29 struct RegisteredInterp *nextPtr;
30 /* The next interp this application knows
31 * about. */
32 Tcl_Interp *interp; /* The interpreter attached to this name. */
33 char name[1]; /* Interpreter's name. Malloc-ed as
34 * part of the structure. */
35 } RegisteredInterp;
36
37 /*
38 * Used to keep track of conversations.
39 */
40
41 typedef struct Conversation {
42 struct Conversation *nextPtr;
43 /* The next conversation in the list. */
44 RegisteredInterp *riPtr; /* The info we know about the conversation. */
45 HCONV hConv; /* The DDE handle for this conversation. */
46 Tcl_Obj *returnPackagePtr; /* The result package for this conversation. */
47
48 } Conversation;
49
50 static Conversation *conversations; /* A list of conversations currently
51 * being processed. */
52 static RegisteredInterp *interps; /* List of all interpreters registered
53 * in the current process. */
54 static HSZ globalService;
55 static DWORD instance; /* The application instance handle given
56 * to us by DdeInitialize. */
57 static int isServer;
58
59 #define TCL_DDE_VERSION "1.2"
60 #define TCL_DDE_PACKAGE_NAME "dde"
61 #define TCL_DDE_SERVICE_NAME "TclEval"
62
63 /*
64 * Forward declarations for procedures defined later in this file.
65 */
66
67 static Tcl_Obj *ExecuteRemoteObject _ANSI_ARGS_((Tcl_Interp *interp,
68 Tcl_Obj *objPtr));
69 static int MakeConnection _ANSI_ARGS_((Tcl_Interp *interp, char *name,
70 HCONV *convPtr));
71 static HDDEDATA CALLBACK ServerProc _ANSI_ARGS_((UINT uType, UINT uFmt,
72 HCONV hConv, HSZ topic, HSZ item, HDDEDATA hData, DWORD dwData1,
73 DWORD dwData2));
74
75 static Tcl_ExitProc ExitProc;
76 static Tcl_CmdDeleteProc DeleteProc;
77 static void SetError _ANSI_ARGS_((Tcl_Interp *interp));
78
79 static Tcl_ObjCmdProc DdeObjCmd;
80
81 /*
82 *----------------------------------------------------------------------
83 *
84 * Initialize --
85 *
86 * Initialize the global DDE instance.
87 *
88 * Results:
89 * None.
90 *
91 * Side effects:
92 * Registers the DDE server proc.
93 *
94 *----------------------------------------------------------------------
95 */
96
97 static void
98 Initialize(void)
99 {
100 int nameFound = 0;
101
102 /*
103 * See if the application is already registered; if so, remove its
104 * current name from the registry. The deletion of the command
105 * will take care of disposing of this entry.
106 */
107
108 if (interps != NULL) {
109 nameFound = 1;
110 }
111
112 /*
113 * Make sure that the DDE server is there. This is done only once,
114 * add an exit handler tear it down.
115 */
116
117 if (instance == 0) {
118 if (instance == 0) {
119 unsigned int flags;
120
121 flags = (CBF_SKIP_REGISTRATIONS | CBF_SKIP_UNREGISTRATIONS |
122 CBF_FAIL_POKES);
123 if (DdeInitialize(&instance, ServerProc, flags, 0)
124 != DMLERR_NO_ERROR) {
125 instance = 0;
126 }
127 }
128 }
129 if ((globalService == 0) && (nameFound != 0)) {
130 if ((globalService == 0) && (nameFound != 0)) {
131 isServer = TRUE;
132 Tcl_CreateExitHandler(ExitProc, NULL);
133 globalService = DdeCreateStringHandle(instance,
134 TCL_DDE_SERVICE_NAME, 0);
135 DdeNameService(instance, globalService, 0L, DNS_REGISTER);
136 } else {
137 isServer = FALSE;
138 }
139 }
140 }
141
142 /*
143 *--------------------------------------------------------------
144 *
145 * SetServerName --
146 *
147 * This procedure is called to associate an ASCII name with a Dde
148 * server. If the interpreter has already been named, the
149 * name replaces the old one.
150 *
151 * Results:
152 * The return value is the name actually given to the interp.
153 * This will normally be the same as name, but if name was already
154 * in use for a Dde Server then a name of the form "name #2" will
155 * be chosen, with a high enough number to make the name unique.
156 *
157 * Side effects:
158 * Registration info is saved, thereby allowing the "send" command
159 * to be used later to invoke commands in the application. In
160 * addition, the "send" command is created in the application's
161 * interpreter. The registration will be removed automatically
162 * if the interpreter is deleted or the "send" command is removed.
163 *
164 *--------------------------------------------------------------
165 */
166
167 static char *
168 SetServerName(
169 Tcl_Interp *interp,
170 char *name /* The name that will be used to
171 * refer to the interpreter in later
172 * "send" commands. Must be globally
173 * unique. */
174 )
175 {
176 int suffix, offset;
177 RegisteredInterp *riPtr, *prevPtr;
178 Tcl_DString dString;
179
180 /*
181 * See if the application is already registered; if so, remove its
182 * current name from the registry. The deletion of the command
183 * will take care of disposing of this entry.
184 */
185
186 for (riPtr = interps, prevPtr = NULL; riPtr != NULL;
187 prevPtr = riPtr, riPtr = riPtr->nextPtr) {
188 if (riPtr->interp == interp) {
189 if (name != NULL) {
190 if (prevPtr == NULL) {
191 interps = interps->nextPtr;
192 } else {
193 prevPtr->nextPtr = riPtr->nextPtr;
194 }
195 break;
196 } else {
197 /*
198 * the name was NULL, so the caller is asking for
199 * the name of the current interp.
200 */
201
202 return riPtr->name;
203 }
204 }
205 }
206
207 if (name == NULL) {
208 /*
209 * the name was NULL, so the caller is asking for
210 * the name of the current interp, but it doesn't
211 * have a name.
212 */
213
214 return "";
215 }
216
217 /*
218 * Pick a name to use for the application. Use "name" if it's not
219 * already in use. Otherwise add a suffix such as " #2", trying
220 * larger and larger numbers until we eventually find one that is
221 * unique.
222 */
223
224 suffix = 1;
225 offset = 0;
226 Tcl_DStringInit(&dString);
227
228 /*
229 * We have found a unique name. Now add it to the registry.
230 */
231
232 riPtr = Blt_Malloc(sizeof(RegisteredInterp) + strlen(name));
233 riPtr->interp = interp;
234 riPtr->nextPtr = interps;
235 interps = riPtr;
236 strcpy(riPtr->name, name);
237
238 Tcl_CreateObjCommand(interp, "dde", DdeObjCmd, riPtr, DeleteProc);
239 if (Tcl_IsSafe(interp)) {
240 Tcl_HideCommand(interp, "dde", "dde");
241 }
242 Tcl_DStringFree(&dString);
243
244 /*
245 * re-initialize with the new name
246 */
247 Initialize();
248
249 return riPtr->name;
250 }
251
252 /*
253 *--------------------------------------------------------------
254 *
255 * DeleteProc
256 *
257 * This procedure is called when the command "dde" is destroyed.
258 *
259 * Results:
260 * none
261 *
262 * Side effects:
263 * The interpreter given by riPtr is unregistered.
264 *
265 *--------------------------------------------------------------
266 */
267
268 static void
269 DeleteProc(clientData)
270 ClientData clientData; /* The interp we are deleting passed
271 * as ClientData. */
272 {
273 RegisteredInterp *riPtr = clientData;
274 RegisteredInterp *searchPtr, *prevPtr;
275
276 for (searchPtr = interps, prevPtr = NULL;
277 (searchPtr != NULL) && (searchPtr != riPtr);
278 prevPtr = searchPtr, searchPtr = searchPtr->nextPtr) {
279 /*
280 * Empty loop body.
281 */
282 }
283
284 if (searchPtr != NULL) {
285 if (prevPtr == NULL) {
286 interps = interps->nextPtr;
287 } else {
288 prevPtr->nextPtr = searchPtr->nextPtr;
289 }
290 }
291 Tcl_EventuallyFree(clientData, TCL_DYNAMIC);
292 }
293
294 /*
295 *--------------------------------------------------------------
296 *
297 * ExecuteRemoteObject --
298 *
299 * Takes the package delivered by DDE and executes it in
300 * the server's interpreter.
301 *
302 * Results:
303 * A list Tcl_Obj * that describes what happened. The first
304 * element is the numerical return code (TCL_ERROR, etc.).
305 * The second element is the result of the script. If the
306 * return result was TCL_ERROR, then the third element
307 * will be the value of the global "errorCode", and the
308 * fourth will be the value of the global "errorInfo".
309 * The return result will have a refCount of 0.
310 *
311 * Side effects:
312 * A Tcl script is run, which can cause all kinds of other
313 * things to happen.
314 *
315 *--------------------------------------------------------------
316 */
317
318 static Tcl_Obj *
319 ExecuteRemoteObject(
320 Tcl_Interp *interp, /* Remote interpreter. */
321 Tcl_Obj *objPtr) /* The object to execute. */
322 {
323 Tcl_Obj *listObjPtr;
324 int result;
325
326 result = Tcl_GlobalEval(interp, Tcl_GetString(objPtr));
327 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
328 Tcl_ListObjAppendElement(NULL, listObjPtr, Tcl_NewIntObj(result));
329 Tcl_ListObjAppendElement(NULL, listObjPtr, Tcl_GetObjResult(interp));
330 if (result == TCL_ERROR) {
331 char *value;
332 Tcl_Obj *objPtr;
333
334 value = Tcl_GetVar2(interp, "errorCode", NULL, TCL_GLOBAL_ONLY);
335 objPtr = Tcl_NewStringObj(value, -1);
336 Tcl_ListObjAppendElement(NULL, listObjPtr, objPtr);
337 value = Tcl_GetVar2(interp, "errorInfo", NULL, TCL_GLOBAL_ONLY);
338 objPtr = Tcl_NewStringObj(value, -1);
339 Tcl_ListObjAppendElement(NULL, listObjPtr, objPtr);
340 }
341 return listObjPtr;
342 }
343
344 /*
345 *--------------------------------------------------------------
346 *
347 * ServerProc --
348 *
349 * Handles all transactions for this server. Can handle
350 * execute, request, and connect protocols. Dde will
351 * call this routine when a client attempts to run a dde
352 * command using this server.
353 *
354 * Results:
355 * A DDE Handle with the result of the dde command.
356 *
357 * Side effects:
358 * Depending on which command is executed, arbitrary
359 * Tcl scripts can be run.
360 *
361 *--------------------------------------------------------------
362 */
363
364 static HDDEDATA CALLBACK
365 ServerProc (
366 UINT uType, /* The type of DDE transaction we
367 * are performing. */
368 UINT uFmt, /* The format that data is sent or
369 * received. */
370 HCONV hConv, /* The conversation associated with the
371 * current transaction. */
372 HSZ topic, /* A string handle. Transaction-type
373 * dependent. */
374 HSZ item, /* A string handle. Transaction-type
375 * dependent. */
376 HDDEDATA hData, /* DDE data. Transaction-type dependent. */
377 DWORD dwData1, /* Transaction-dependent data. */
378 DWORD dwData2) /* Transaction-dependent data. */
379 {
380 Tcl_DString dString;
381 char *utilString;
382 Tcl_Obj *objPtr;
383 HDDEDATA code = NULL;
384 RegisteredInterp *riPtr;
385 Conversation *convPtr, *prevConvPtr;
386
387 switch(uType) {
388 case XTYP_CONNECT:
389 {
390 int length;
391
392 /*
393 * Dde is trying to initialize a conversation with us. Check
394 * and make sure we have a valid topic.
395 */
396
397 length = DdeQueryString(instance, topic, NULL, 0, 0);
398 Tcl_DStringInit(&dString);
399 Tcl_DStringSetLength(&dString, length);
400 utilString = Tcl_DStringValue(&dString);
401 DdeQueryString(instance, topic, utilString, length + 1,
402 CP_WINANSI);
403
404 for (riPtr = interps; riPtr != NULL; riPtr = riPtr->nextPtr) {
405 if (strcasecmp(utilString, riPtr->name) == 0) {
406 Tcl_DStringFree(&dString);
407 return (HDDEDATA) TRUE;
408 }
409 }
410
411 Tcl_DStringFree(&dString);
412 return (HDDEDATA) FALSE;
413 }
414 case XTYP_CONNECT_CONFIRM:
415 {
416 DWORD length;
417
418 /*
419 * Dde has decided that we can connect, so it gives us a
420 * conversation handle. We need to keep track of it
421 * so we know which execution result to return in an
422 * XTYP_REQUEST.
423 */
424
425 length = DdeQueryString(instance, topic, NULL, 0, 0);
426 Tcl_DStringInit(&dString);
427 Tcl_DStringSetLength(&dString, length);
428 utilString = Tcl_DStringValue(&dString);
429 DdeQueryString(instance, topic, utilString, length + 1,
430 CP_WINANSI);
431 for (riPtr = interps; riPtr != NULL; riPtr = riPtr->nextPtr) {
432 if (strcasecmp(riPtr->name, utilString) == 0) {
433 convPtr = Blt_Malloc(sizeof(Conversation));
434 convPtr->nextPtr = conversations;
435 convPtr->returnPackagePtr = NULL;
436 convPtr->hConv = hConv;
437 convPtr->riPtr = riPtr;
438 conversations = convPtr;
439 break;
440 }
441 }
442 Tcl_DStringFree(&dString);
443 return (HDDEDATA) TRUE;
444 }
445 case XTYP_DISCONNECT:
446 {
447 /*
448 * The client has disconnected from our server. Forget this
449 * conversation.
450 */
451
452 for (convPtr = conversations, prevConvPtr = NULL;
453 convPtr != NULL;
454 prevConvPtr = convPtr, convPtr = convPtr->nextPtr) {
455 if (hConv == convPtr->hConv) {
456 if (prevConvPtr == NULL) {
457 conversations = convPtr->nextPtr;
458 } else {
459 prevConvPtr->nextPtr = convPtr->nextPtr;
460 }
461 if (convPtr->returnPackagePtr != NULL) {
462 Tcl_DecrRefCount(convPtr->returnPackagePtr);
463 }
464 Blt_Free(convPtr);
465 break;
466 }
467 }
468 return (HDDEDATA) TRUE;
469 }
470 case XTYP_REQUEST:
471 {
472 int length;
473
474 /*
475 * This could be either a request for a value of a Tcl variable,
476 * or it could be the send command requesting the results of the
477 * last execute.
478 */
479
480 if (uFmt != CF_TEXT) {
481 return (HDDEDATA) FALSE;
482 }
483
484 code = (HDDEDATA) FALSE;
485 for (convPtr = conversations; (convPtr != NULL)
486 && (convPtr->hConv != hConv); convPtr = convPtr->nextPtr) {
487 /*
488 * Empty loop body.
489 */
490 }
491
492 if (convPtr != NULL) {
493 length = DdeQueryString(instance, item, NULL, 0, CP_WINANSI);
494 Tcl_DStringInit(&dString);
495 Tcl_DStringSetLength(&dString, length);
496 utilString = Tcl_DStringValue(&dString);
497 DdeQueryString(instance, item, utilString, length + 1,
498 CP_WINANSI);
499 if (strcasecmp(utilString, "$TCLEVAL$EXECUTE$RESULT") == 0) {
500 char *value;
501
502 value = Tcl_GetStringFromObj(convPtr->returnPackagePtr,
503 &length);
504 code = DdeCreateDataHandle(instance, value, length+1, 0,
505 item, CF_TEXT, 0);
506 } else {
507 char *value;
508
509 value = Tcl_GetVar2(convPtr->riPtr->interp, utilString,
510 NULL, TCL_GLOBAL_ONLY);
511 if (value != NULL) {
512 length = strlen(value);
513 code = DdeCreateDataHandle(instance, value, length+1,
514 0, item, CF_TEXT, 0);
515 } else {
516 code = NULL;
517 }
518 }
519 Tcl_DStringFree(&dString);
520 }
521 return code;
522 }
523 case XTYP_EXECUTE:
524 {
525 DWORD length;
526 /*
527 * Execute this script. The results will be saved into
528 * a list object which will be retreived later. See
529 * ExecuteRemoteObject.
530 */
531
532 Tcl_Obj *returnPackagePtr;
533
534 for (convPtr = conversations; (convPtr != NULL)
535 && (convPtr->hConv != hConv); convPtr = convPtr->nextPtr) {
536 /*
537 * Empty loop body.
538 */
539
540 }
541
542 if (convPtr == NULL) {
543 return (HDDEDATA) DDE_FNOTPROCESSED;
544 }
545
546 utilString = (char *) DdeAccessData(hData, &length);
547 objPtr = Tcl_NewStringObj(utilString, -1);
548 Tcl_IncrRefCount(objPtr);
549 DdeUnaccessData(hData);
550 if (convPtr->returnPackagePtr != NULL) {
551 Tcl_DecrRefCount(convPtr->returnPackagePtr);
552 }
553 convPtr->returnPackagePtr = NULL;
554 returnPackagePtr = ExecuteRemoteObject(convPtr->riPtr->interp,
555 objPtr);
556 for (convPtr = conversations; (convPtr != NULL)
557 && (convPtr->hConv != hConv); convPtr = convPtr->nextPtr) {
558 /*
559 * Empty loop body.
560 */
561
562 }
563 if (convPtr != NULL) {
564 Tcl_IncrRefCount(returnPackagePtr);
565 convPtr->returnPackagePtr = returnPackagePtr;
566 }
567 Tcl_DecrRefCount(objPtr);
568 if (returnPackagePtr == NULL) {
569 return (HDDEDATA) DDE_FNOTPROCESSED;
570 } else {
571 return (HDDEDATA) DDE_FACK;
572 }
573 }
574 case XTYP_WILDCONNECT:
575 {
576 DWORD length;
577
578 /*
579 * Dde wants a list of services and topics that we support.
580 */
581
582 HSZPAIR *returnPtr;
583 int i;
584 int numItems;
585
586 for (i = 0, riPtr = interps; riPtr != NULL;
587 i++, riPtr = riPtr->nextPtr) {
588 /*
589 * Empty loop body.
590 */
591
592 }
593
594 numItems = i;
595 code = DdeCreateDataHandle(instance, NULL,
596 (numItems + 1) * sizeof(HSZPAIR), 0, 0, 0, 0);
597 returnPtr = (HSZPAIR *) DdeAccessData(code, &length);
598 for (i = 0, riPtr = interps; i < numItems;
599 i++, riPtr = riPtr->nextPtr) {
600 returnPtr[i].hszSvc = DdeCreateStringHandle(
601 instance, "TclEval", CP_WINANSI);
602 returnPtr[i].hszTopic = DdeCreateStringHandle(
603 instance, riPtr->name, CP_WINANSI);
604 }
605 returnPtr[i].hszSvc = NULL;
606 returnPtr[i].hszTopic = NULL;
607 DdeUnaccessData(code);
608 return code;
609 }
610 }
611 return NULL;
612 }
613
614 /*
615 *--------------------------------------------------------------
616 *
617 * ExitProc --
618 *
619 * Gets rid of our DDE server when we go away.
620 *
621 * Results:
622 * None.
623 *
624 * Side effects:
625 * The DDE server is deleted.
626 *
627 *--------------------------------------------------------------
628 */
629
630 static void
631 ExitProc(
632 ClientData clientData) /* Not used in this handler. */
633 {
634 DdeNameService(instance, NULL, 0, DNS_UNREGISTER);
635 DdeUninitialize(instance);
636 instance = 0;
637 }
638
639 /*
640 *--------------------------------------------------------------
641 *
642 * MakeConnection --
643 *
644 * This procedure is a utility used to connect to a DDE
645 * server when given a server name and a topic name.
646 *
647 * Results:
648 * A standard Tcl result.
649 *
650 *
651 * Side effects:
652 * Passes back a conversation through ddeConvPtr
653 *
654 *--------------------------------------------------------------
655 */
656
657 static int
658 MakeConnection(
659 Tcl_Interp *interp, /* Used to report errors. */
660 char *name, /* The connection to use. */
661 HCONV *convPtr)
662 {
663 HSZ topic, service;
664 HCONV conv;
665
666 service = DdeCreateStringHandle(instance, "TclEval", 0);
667 topic = DdeCreateStringHandle(instance, name, 0);
668
669 conv = DdeConnect(instance, service, topic, NULL);
670 DdeFreeStringHandle(instance, service);
671 DdeFreeStringHandle(instance, topic);
672
673 if (conv == NULL) {
674 if (interp != NULL) {
675 Tcl_AppendResult(interp, "no registered server named \"", name,
676 "\"", (char *) NULL);
677 }
678 return TCL_ERROR;
679 }
680
681 *convPtr = conv;
682 return TCL_OK;
683 }
684
685 /*
686 *--------------------------------------------------------------
687 *
688 * SetError --
689 *
690 * Sets the interp result to a cogent error message
691 * describing the last DDE error.
692 *
693 * Results:
694 * None.
695 *
696 *
697 * Side effects:
698 * The interp's result object is changed.
699 *
700 *--------------------------------------------------------------
701 */
702
703 static void
704 SetError(
705 Tcl_Interp *interp) /* The interp to put the message in.*/
706 {
707 int err;
708 char *mesg;
709
710 err = DdeGetLastError(instance);
711 switch (err) {
712 case DMLERR_DATAACKTIMEOUT:
713 case DMLERR_EXECACKTIMEOUT:
714 case DMLERR_POKEACKTIMEOUT:
715 mesg = "remote interpreter did not respond";
716 break;
717
718 case DMLERR_BUSY:
719 mesg = "remote server is busy";
720 break;
721
722 case DMLERR_NOTPROCESSED:
723 mesg = "remote server cannot handle this command";
724 break;
725
726 default:
727 mesg = "dde command failed";
728 break;
729 }
730 Tcl_SetResult(interp, mesg, TCL_VOLATILE);
731 }
732
733 /*
734 *--------------------------------------------------------------
735 *
736 * DdeObjCmd --
737 *
738 * This procedure is invoked to process the "dde" Tcl command.
739 * See the user documentation for details on what it does.
740 *
741 * Results:
742 * A standard Tcl result.
743 *
744 * Side effects:
745 * See the user documentation.
746 *
747 *--------------------------------------------------------------
748 */
749
750 static int
751 DdeObjCmd(
752 ClientData clientData, /* Used only for deletion */
753 Tcl_Interp *interp, /* The interp we are sending from */
754 int objc, /* Number of arguments */
755 Tcl_Obj *CONST objv[]) /* The arguments */
756 {
757 enum {
758 DDE_SERVERNAME,
759 DDE_EXECUTE,
760 DDE_POKE,
761 DDE_REQUEST,
762 DDE_SERVICES,
763 DDE_EVAL
764 };
765
766 static char *commands[] = {
767 "servername", "execute", "poke", "request", "services", "eval",
768 (char *) NULL
769 };
770 static char *options[] = {
771 "-async", (char *) NULL
772 };
773 int index, argIndex;
774 int async = 0, binary = 0;
775 int result = TCL_OK;
776 HSZ service = NULL;
777 HSZ topic = NULL;
778 HSZ item = NULL;
779 HDDEDATA data = NULL;
780 HDDEDATA itemData = NULL;
781 HCONV hConv = NULL;
782 HSZ cookie = 0;
783 char *serviceName, *topicName, *itemString, *dataString;
784 char *string;
785 int firstArg, length, dataLength;
786 HDDEDATA code;
787 RegisteredInterp *riPtr;
788 Tcl_Interp *sendInterp;
789 Tcl_Obj *objPtr;
790
791 /*
792 * Initialize DDE server/client
793 */
794
795 if (objc < 2) {
796 Tcl_WrongNumArgs(interp, 1, objv,
797 "?-async? serviceName topicName value");
798 return TCL_ERROR;
799 }
800
801 if (Tcl_GetIndexFromObj(interp, objv[1], commands, "command", 0,
802 &index) != TCL_OK) {
803 return TCL_ERROR;
804 }
805
806 serviceName = NULL; /* Suppress compiler warning. */
807 firstArg = 1;
808 switch (index) {
809 case DDE_SERVERNAME:
810 if ((objc != 3) && (objc != 2)) {
811 Tcl_WrongNumArgs(interp, 1, objv, "servername ?serverName?");
812 return TCL_ERROR;
813 }
814 firstArg = (objc - 1);
815 break;
816
817 case DDE_EXECUTE:
818 if ((objc < 5) || (objc > 6)) {
819 Tcl_WrongNumArgs(interp, 1, objv,
820 "execute ?-async? serviceName topicName value");
821 return TCL_ERROR;
822 }
823 if (Tcl_GetIndexFromObj(NULL, objv[2], options, "option", 0,
824 &argIndex) != TCL_OK) {
825 if (objc != 5) {
826 Tcl_WrongNumArgs(interp, 1, objv,
827 "execute ?-async? serviceName topicName value");
828 return TCL_ERROR;
829 }
830 async = 0;
831 firstArg = 2;
832 } else {
833 if (objc != 6) {
834 Tcl_WrongNumArgs(interp, 1, objv,
835 "execute ?-async? serviceName topicName value");
836 return TCL_ERROR;
837 }
838 async = 1;
839 firstArg = 3;
840 }
841 break;
842 case DDE_POKE:
843 if (objc != 6) {
844 Tcl_WrongNumArgs(interp, 1, objv,
845 "poke serviceName topicName item value");
846 return TCL_ERROR;
847 }
848 firstArg = 2;
849 break;
850
851 case DDE_REQUEST:
852 if (objc != 5) {
853 Tcl_WrongNumArgs(interp, 1, objv,
854 "request serviceName topicName value");
855 return TCL_ERROR;
856 }
857 binary = 0;
858 firstArg = 2;
859 break;
860
861 case DDE_SERVICES:
862 if (objc != 4) {
863 Tcl_WrongNumArgs(interp, 1, objv,
864 "services serviceName topicName");
865 return TCL_ERROR;
866 }
867 firstArg = 2;
868 break;
869
870 case DDE_EVAL:
871 if (objc < 4) {
872 Tcl_WrongNumArgs(interp, 1, objv,
873 "eval ?-async? serviceName args");
874 return TCL_ERROR;
875 }
876 if (Tcl_GetIndexFromObj(NULL, objv[2], options, "option", 0,
877 &argIndex) != TCL_OK) {
878 if (objc < 4) {
879 Tcl_WrongNumArgs(interp, 1, objv,
880 "eval ?-async? serviceName args");
881 return TCL_ERROR;
882 }
883 async = 0;
884 firstArg = 2;
885 } else {
886 if (objc < 5) {
887 Tcl_WrongNumArgs(interp, 1, objv,
888 "eval ?-async? serviceName args");
889 return TCL_ERROR;
890 }
891 async = 1;
892 firstArg = 3;
893 }
894 break;
895 }
896
897 Initialize();
898
899 if (firstArg != 1) {
900 serviceName = Tcl_GetStringFromObj(objv[firstArg], &length);
901 } else {
902 length = 0;
903 }
904
905 if (length == 0) {
906 serviceName = NULL;
907 } else if ((index != DDE_SERVERNAME) && (index != DDE_EVAL)) {
908 service = DdeCreateStringHandle(instance, serviceName,
909 CP_WINANSI);
910 }
911
912 if ((index != DDE_SERVERNAME) &&(index != DDE_EVAL)) {
913 topicName = Tcl_GetStringFromObj(objv[firstArg + 1], &length);
914 if (length == 0) {
915 topicName = NULL;
916 } else {
917 topic = DdeCreateStringHandle(instance, topicName, CP_WINANSI);
918 }
919 }
920
921 switch (index) {
922 case DDE_SERVERNAME:
923 serviceName = SetServerName(interp, serviceName);
924 if (serviceName != NULL) {
925 Tcl_SetStringObj(Tcl_GetObjResult(interp),
926 serviceName, -1);
927 } else {
928 Tcl_ResetResult(interp);
929 }
930 break;
931
932 case DDE_EXECUTE:
933 {
934 dataString = Tcl_GetStringFromObj(objv[firstArg + 2], &dataLength);
935 if (dataLength == 0) {
936 Tcl_SetStringObj(Tcl_GetObjResult(interp),
937 "cannot execute null data", -1);
938 result = TCL_ERROR;
939 break;
940 }
941 hConv = DdeConnect(instance, service, topic, NULL);
942 DdeFreeStringHandle(instance, service);
943 DdeFreeStringHandle(instance, topic);
944
945 if (hConv == NULL) {
946 SetError(interp);
947 result = TCL_ERROR;
948 break;
949 }
950
951 data = DdeCreateDataHandle(instance, dataString, dataLength + 1,
952 0, 0, CF_TEXT, 0);
953 if (data != NULL) {
954 if (async) {
955 DWORD status;
956
957 DdeClientTransaction((LPBYTE) data, 0xFFFFFFFF, hConv, 0,
958 CF_TEXT, XTYP_EXECUTE, TIMEOUT_ASYNC, &status);
959 DdeAbandonTransaction(instance, hConv, status);
960 } else {
961 code = DdeClientTransaction((LPBYTE) data, 0xFFFFFFFF,
962 hConv, 0, CF_TEXT, XTYP_EXECUTE, 30000, NULL);
963 if (code == 0) {
964 SetError(interp);
965 result = TCL_ERROR;
966 }
967 }
968 DdeFreeDataHandle(data);
969 } else {
970 SetError(interp);
971 result = TCL_ERROR;
972 }
973 break;
974 }
975 case DDE_REQUEST:
976 {
977 itemString = Tcl_GetStringFromObj(objv[firstArg + 2], &length);
978 if (length == 0) {
979 Tcl_SetStringObj(Tcl_GetObjResult(interp),
980 "cannot request value of null data", -1);
981 return TCL_ERROR;
982 }
983 hConv = DdeConnect(instance, service, topic, NULL);
984 DdeFreeStringHandle(instance, service);
985 DdeFreeStringHandle(instance, topic);
986
987 if (hConv == NULL) {
988 SetError(interp);
989 result = TCL_ERROR;
990 } else {
991 item = DdeCreateStringHandle(instance, itemString, CP_WINANSI);
992 if (item != NULL) {
993 data = DdeClientTransaction(NULL, 0, hConv, item, CF_TEXT,
994 XTYP_REQUEST, 5000, NULL);
995 if (data == NULL) {
996 SetError(interp);
997 result = TCL_ERROR;
998 } else {
999 Tcl_Obj *objPtr;
1000 DWORD dataLength;
1001
1002 dataString = DdeAccessData(data, &dataLength);
1003 objPtr = Tcl_NewStringObj(dataString, -1);
1004 DdeUnaccessData(data);
1005 DdeFreeDataHandle(data);
1006 Tcl_SetObjResult(interp, objPtr);
1007 }
1008 } else {
1009 SetError(interp);
1010 result = TCL_ERROR;
1011 }
1012 }
1013
1014 break;
1015 }
1016 case DDE_POKE:
1017 {
1018 itemString = Tcl_GetStringFromObj(objv[firstArg + 2], &length);
1019 if (length == 0) {
1020 Tcl_SetStringObj(Tcl_GetObjResult(interp),
1021 "cannot have a null item", -1);
1022 return TCL_ERROR;
1023 }
1024 dataString = Tcl_GetStringFromObj(objv[firstArg + 3], &length);
1025
1026 hConv = DdeConnect(instance, service, topic, NULL);
1027 DdeFreeStringHandle(instance, service);
1028 DdeFreeStringHandle(instance, topic);
1029
1030 if (hConv == NULL) {
1031 SetError(interp);
1032 result = TCL_ERROR;
1033 } else {
1034 item = DdeCreateStringHandle(instance, itemString,
1035 CP_WINANSI);
1036 if (item != NULL) {
1037 data = DdeClientTransaction(dataString,length+1,
1038 hConv, item, CF_TEXT, XTYP_POKE, 5000, NULL);
1039 if (data == NULL) {
1040 SetError(interp);
1041 result = TCL_ERROR;
1042 }
1043 } else {
1044 SetError(interp);
1045 result = TCL_ERROR;
1046 }
1047 }
1048 break;
1049 }
1050
1051 case DDE_SERVICES:
1052 {
1053 HCONVLIST hConvList;
1054 CONVINFO convInfo;
1055 Tcl_Obj *convListObjPtr, *elementObjPtr;
1056 Tcl_DString dString;
1057 char *name;
1058
1059 convInfo.cb = sizeof(CONVINFO);
1060 hConvList = DdeConnectList(instance, service,
1061 topic, 0, NULL);
1062 DdeFreeStringHandle(instance, service);
1063 DdeFreeStringHandle(instance, topic);
1064 hConv = 0;
1065 convListObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1066 Tcl_DStringInit(&dString);
1067
1068 while (hConv = DdeQueryNextServer(hConvList, hConv), hConv != 0) {
1069 elementObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1070 DdeQueryConvInfo(hConv, QID_SYNC, &convInfo);
1071 length = DdeQueryString(instance,
1072 convInfo.hszSvcPartner, NULL, 0, CP_WINANSI);
1073 Tcl_DStringSetLength(&dString, length);
1074 name = Tcl_DStringValue(&dString);
1075 DdeQueryString(instance, convInfo.hszSvcPartner,
1076 name, length + 1, CP_WINANSI);
1077 Tcl_ListObjAppendElement(interp, elementObjPtr,
1078 Tcl_NewStringObj(name, length));
1079 length = DdeQueryString(instance, convInfo.hszTopic,
1080 NULL, 0, CP_WINANSI);
1081 Tcl_DStringSetLength(&dString, length);
1082 name = Tcl_DStringValue(&dString);
1083 DdeQueryString(instance, convInfo.hszTopic, name,
1084 length + 1, CP_WINANSI);
1085 Tcl_ListObjAppendElement(interp, elementObjPtr,
1086 Tcl_NewStringObj(name, length));
1087 Tcl_ListObjAppendElement(interp, convListObjPtr,
1088 elementObjPtr);
1089 }
1090 DdeDisconnectList(hConvList);
1091 Tcl_SetObjResult(interp, convListObjPtr);
1092 Tcl_DStringFree(&dString);
1093 break;
1094 }
1095 case DDE_EVAL:
1096 {
1097 objc -= (async + 3);
1098 ((Tcl_Obj **) objv) += (async + 3);
1099
1100 /*
1101 * See if the target interpreter is local. If so, execute
1102 * the command directly without going through the DDE
1103 * server. Don't exchange objects between interps. The
1104 * target interp could compile an object, producing a
1105 * bytecode structure that refers to other objects owned
1106 * by the target interp. If the target interp is then
1107 * deleted, the bytecode structure would be referring to
1108 * deallocated objects.
1109 */
1110
1111 for (riPtr = interps; riPtr != NULL;
1112 riPtr = riPtr->nextPtr) {
1113 if (strcasecmp(serviceName, riPtr->name) == 0) {
1114 break;
1115 }
1116 }
1117
1118 if (riPtr != NULL) {
1119 /*
1120 * This command is to a local interp. No need to go through
1121 * the server.
1122 */
1123
1124 Tcl_Preserve(riPtr);
1125 sendInterp = riPtr->interp;
1126 Tcl_Preserve(sendInterp);
1127
1128 /*
1129 * Don't exchange objects between interps. The target interp
1130 * would compile an object, producing a bytecode structure that
1131 * refers to other objects owned by the target interp. If the
1132 * target interp is then deleted, the bytecode structure would
1133 * be referring to deallocated objects.
1134 */
1135
1136 if (objc == 1) {
1137 result = Tcl_GlobalEval(sendInterp,Tcl_GetString(objv[0]));
1138 } else {
1139 objPtr = Tcl_ConcatObj(objc, objv);
1140 Tcl_IncrRefCount(objPtr);
1141 result = Tcl_GlobalEval(sendInterp, Tcl_GetString(objPtr));
1142 Tcl_DecrRefCount(objPtr);
1143 }
1144 if (interp != sendInterp) {
1145 if (result == TCL_ERROR) {
1146 char *value;
1147 /*
1148 * An error occurred, so transfer error information
1149 * from the destination interpreter back to our
1150 * interpreter.
1151 */
1152
1153 Tcl_ResetResult(interp);
1154 value = Tcl_GetVar2(sendInterp, "errorInfo", NULL,
1155 TCL_GLOBAL_ONLY);
1156 Tcl_AddObjErrorInfo(interp, value, length);
1157
1158 value = Tcl_GetVar2(sendInterp, "errorCode", NULL,
1159 TCL_GLOBAL_ONLY);
1160 Tcl_SetErrorCode(interp, value, (char *)NULL);
1161 }
1162 Tcl_SetObjResult(interp, Tcl_GetObjResult(sendInterp));
1163 }
1164 Tcl_Release(riPtr);
1165 Tcl_Release(sendInterp);
1166 } else {
1167 /*
1168 * This is a non-local request. Send the script to the server
1169 * and poll it for a result.
1170 */
1171
1172 if (MakeConnection(interp, serviceName, &hConv) != TCL_OK) {
1173 goto error;
1174 }
1175
1176 objPtr = Tcl_ConcatObj(objc, objv);
1177 string = Tcl_GetStringFromObj(objPtr, &length);
1178 itemData = DdeCreateDataHandle(instance, string,
1179 length+1, 0, 0, CF_TEXT, 0);
1180
1181 if (async) {
1182 DWORD status;
1183
1184 data = DdeClientTransaction((LPBYTE) itemData, 0xFFFFFFFF,
1185 hConv, 0, CF_TEXT, XTYP_EXECUTE, TIMEOUT_ASYNC,
1186 &status);
1187 DdeAbandonTransaction(instance, hConv, status);
1188 } else {
1189 data = DdeClientTransaction((LPBYTE) itemData,
1190 0xFFFFFFFF, hConv, 0,
1191 CF_TEXT, XTYP_EXECUTE, 30000, NULL);
1192 if (data != 0) {
1193
1194 cookie = DdeCreateStringHandle(instance,
1195 "$TCLEVAL$EXECUTE$RESULT", CP_WINANSI);
1196 data = DdeClientTransaction(NULL, 0, hConv,
1197 cookie, CF_TEXT, XTYP_REQUEST, 30000, NULL);
1198 }
1199 }
1200
1201 Tcl_DecrRefCount(objPtr);
1202
1203 if (data == 0) {
1204 SetError(interp);
1205 goto errorNoResult;
1206 }
1207
1208 if (async == 0) {
1209 Tcl_Obj *resultPtr;
1210
1211 /*
1212 * The return handle has a two or four element list in
1213 * it. The first element is the return code (TCL_OK,
1214 * TCL_ERROR, etc.). The second is the result of the
1215 * script. If the return code is TCL_ERROR, then the third
1216 * element is the value of the variable "errorCode", and
1217 * the fourth is the value of the variable "errorInfo".
1218 */
1219
1220 resultPtr = Tcl_NewObj();
1221 length = DdeGetData(data, NULL, 0, 0);
1222 Tcl_SetObjLength(resultPtr, length);
1223 string = Tcl_GetString(resultPtr);
1224 DdeGetData(data, string, length, 0);
1225 Tcl_SetObjLength(resultPtr, strlen(string));
1226
1227 if (Tcl_ListObjIndex(NULL, resultPtr, 0, &objPtr)
1228 != TCL_OK) {
1229 Tcl_DecrRefCount(resultPtr);
1230 goto error;
1231 }
1232 if (Tcl_GetIntFromObj(NULL, objPtr, &result) != TCL_OK) {
1233 Tcl_DecrRefCount(resultPtr);
1234 goto error;
1235 }
1236 if (result == TCL_ERROR) {
1237 Tcl_ResetResult(interp);
1238
1239 if (Tcl_ListObjIndex(NULL, resultPtr, 3, &objPtr)
1240 != TCL_OK) {
1241 Tcl_DecrRefCount(resultPtr);
1242 goto error;
1243 }
1244 length = -1;
1245 string = Tcl_GetStringFromObj(objPtr, &length);
1246 Tcl_AddObjErrorInfo(interp, string, length);
1247
1248 Tcl_ListObjIndex(NULL, resultPtr, 2, &objPtr);
1249 Tcl_SetObjErrorCode(interp, objPtr);
1250 }
1251 if (Tcl_ListObjIndex(NULL, resultPtr, 1, &objPtr)
1252 != TCL_OK) {
1253 Tcl_DecrRefCount(resultPtr);
1254 goto error;
1255 }
1256 Tcl_SetObjResult(interp, objPtr);
1257 Tcl_DecrRefCount(resultPtr);
1258 }
1259 }
1260 }
1261 }
1262 if (cookie != NULL) {
1263 DdeFreeStringHandle(instance, cookie);
1264 }
1265 if (item != NULL) {
1266 DdeFreeStringHandle(instance, item);
1267 }
1268 if (itemData != NULL) {
1269 DdeFreeDataHandle(itemData);
1270 }
1271 if (data != NULL) {
1272 DdeFreeDataHandle(data);
1273 }
1274 if (hConv != NULL) {
1275 DdeDisconnect(hConv);
1276 }
1277 return result;
1278
1279 error:
1280 Tcl_SetStringObj(Tcl_GetObjResult(interp),
1281 "invalid data returned from server", -1);
1282
1283 errorNoResult:
1284 if (cookie != NULL) {
1285 DdeFreeStringHandle(instance, cookie);
1286 }
1287 if (item != NULL) {
1288 DdeFreeStringHandle(instance, item);
1289 }
1290 if (itemData != NULL) {
1291 DdeFreeDataHandle(itemData);
1292 }
1293 if (data != NULL) {
1294 DdeFreeDataHandle(data);
1295 }
1296 if (hConv != NULL) {
1297 DdeDisconnect(hConv);
1298 }
1299 return TCL_ERROR;
1300 }
1301
1302 /*
1303 *----------------------------------------------------------------------
1304 *
1305 * Blt_DdeInit --
1306 *
1307 * This procedure initializes the dde command.
1308 *
1309 * Results:
1310 * A standard Tcl result.
1311 *
1312 * Side effects:
1313 * None.
1314 *
1315 *----------------------------------------------------------------------
1316 */
1317
1318 int
1319 Blt_DdeInit(interp)
1320 Tcl_Interp *interp;
1321 {
1322 Tcl_CreateObjCommand(interp, "dde", DdeObjCmd, NULL, NULL);
1323 conversations = NULL;
1324 interps = NULL;
1325 Tcl_CreateExitHandler(ExitProc, NULL);
1326 return Tcl_PkgProvide(interp, TCL_DDE_PACKAGE_NAME, TCL_DDE_VERSION);
1327 }
1328
1329 #endif /* NO_DDE */