1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <stardiv/uno/repos/implementationregistration.hxx>
33*cdf0e10cSrcweir #include <stardiv/uno/script/script.hxx>
34*cdf0e10cSrcweir #include <stardiv/uno/beans/exactname.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <rtl/ustring.hxx>
37*cdf0e10cSrcweir #include <vos/dynload.hxx>
38*cdf0e10cSrcweir #include <vos/diagnose.hxx>
39*cdf0e10cSrcweir #include <usr/services.hxx>
40*cdf0e10cSrcweir #include <vcl/svapp.hxx>
41*cdf0e10cSrcweir #include <usr/ustring.hxx>
42*cdf0e10cSrcweir #include <usr/weak.hxx>
43*cdf0e10cSrcweir #include <tools/string.hxx>
44*cdf0e10cSrcweir #include <vos/conditn.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace rtl;
47*cdf0e10cSrcweir using namespace vos;
48*cdf0e10cSrcweir using namespace usr;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #define PCHAR_TO_USTRING(x) StringToOUString(String(x),CHARSET_SYSTEM)
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir class NullEngineListenerRef : public XEngineListenerRef
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) ) {}
57*cdf0e10cSrcweir     virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) ) {}
58*cdf0e10cSrcweir     virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) ) {}
59*cdf0e10cSrcweir };
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #define USTRING_TO_PCHAR(x) OUStringToString(x , CHARSET_DONTKNOW ).GetCharStr()
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir class CmdDebugger :
64*cdf0e10cSrcweir 	public XEngineListener,
65*cdf0e10cSrcweir 	public OWeakObject
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir public:
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	CmdDebugger()
70*cdf0e10cSrcweir 	{
71*cdf0e10cSrcweir 		m_pDebuggingRef = 0;
72*cdf0e10cSrcweir 		m_pEngineRef = 0;
73*cdf0e10cSrcweir 		m_bIsTerminating = FALSE;
74*cdf0e10cSrcweir 		m_bIsRunning = FALSE;
75*cdf0e10cSrcweir 	}
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	CmdDebugger( XDebuggingRef *p, XEngineRef *pEngine , XInvokationRef *pInvokation)
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		attach( p , pEngine , pInvokation );
81*cdf0e10cSrcweir 	}
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	~CmdDebugger()
84*cdf0e10cSrcweir 	{
85*cdf0e10cSrcweir 		if( m_pDebuggingRef ) {
86*cdf0e10cSrcweir 			detach();
87*cdf0e10cSrcweir 		}
88*cdf0e10cSrcweir 	}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	BOOL				queryInterface( Uik aUik, XInterfaceRef & rOut );
91*cdf0e10cSrcweir 	void 				acquire() 						 { OWeakObject::acquire(); }
92*cdf0e10cSrcweir 	void 				release() 						 { OWeakObject::release(); }
93*cdf0e10cSrcweir 	void* 				getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	void attach( XDebuggingRef *p , XEngineRef *pEngine , XInvokationRef *pInvokation )
97*cdf0e10cSrcweir 	{
98*cdf0e10cSrcweir 		m_pDebuggingRef = p;
99*cdf0e10cSrcweir 		m_pEngineRef = pEngine;
100*cdf0e10cSrcweir 		m_pInvokationRef = pInvokation;
101*cdf0e10cSrcweir 		m_bIsRunning = FALSE;
102*cdf0e10cSrcweir 		m_bIsTerminating = FALSE;
103*cdf0e10cSrcweir 	}
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	void detach( );
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	virtual void disposing( const EventObject &o )
109*cdf0e10cSrcweir 	{
110*cdf0e10cSrcweir 		if( m_pDebuggingRef ) {
111*cdf0e10cSrcweir 			detach();
112*cdf0e10cSrcweir 		}
113*cdf0e10cSrcweir 	}
114*cdf0e10cSrcweir     virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) )
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir 		if( m_pDebuggingRef && ! m_bIsTerminating ) {
117*cdf0e10cSrcweir    			(*m_pDebuggingRef)->stop();
118*cdf0e10cSrcweir    			fprintf( stderr, "%s\n" , USTRING_TO_PCHAR(Evt.ErrorMessage ) );
119*cdf0e10cSrcweir 			fprintf( stderr, "%s.%s (%d)\n",  USTRING_TO_PCHAR(Evt.SourceCode),
120*cdf0e10cSrcweir 											  USTRING_TO_PCHAR(Evt.Name ),
121*cdf0e10cSrcweir 											  Evt.StartLine );
122*cdf0e10cSrcweir 	   		m_aDebugCondition.set();
123*cdf0e10cSrcweir     		m_bIsRunning = TRUE;
124*cdf0e10cSrcweir 	   	}
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) )
128*cdf0e10cSrcweir    	{
129*cdf0e10cSrcweir 		if( m_pDebuggingRef && ! m_bIsTerminating ) {
130*cdf0e10cSrcweir    			(*m_pDebuggingRef)->stop();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     		m_aDebugCondition.set();
133*cdf0e10cSrcweir     		m_bIsRunning = TRUE;
134*cdf0e10cSrcweir     		fprintf( stderr, "%s\n" , "Script starts\n" );
135*cdf0e10cSrcweir     	}
136*cdf0e10cSrcweir     }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) )
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir 		if( m_pDebuggingRef && ! m_bIsTerminating  ) {
141*cdf0e10cSrcweir 			m_aDebugCondition.set();
142*cdf0e10cSrcweir     		m_bIsRunning = FALSE;
143*cdf0e10cSrcweir     		fprintf( stderr , "%s\n", USTRING_TO_PCHAR( Evt.ErrorMessage ) );
144*cdf0e10cSrcweir     	}
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	void dumpIntrospectionToStream( const XIntrospectionAccessRef &, FILE *f );
148*cdf0e10cSrcweir 	void dumpVarToStream( const char *pcName, const UsrAny &any, FILE *f );
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	void cmdLine();
152*cdf0e10cSrcweir protected:
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	OCondition m_aDebugCondition;
155*cdf0e10cSrcweir 	XDebuggingRef *m_pDebuggingRef;
156*cdf0e10cSrcweir 	XEngineRef	*m_pEngineRef;
157*cdf0e10cSrcweir 	XInvokationRef *m_pInvokationRef;
158*cdf0e10cSrcweir 	int m_bIsRunning;
159*cdf0e10cSrcweir 	int m_bIsTerminating;		// The listeners ignore everything when set
160*cdf0e10cSrcweir };
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir void CmdDebugger::cmdLine()
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir 		char pcLine[80];
167*cdf0e10cSrcweir 		fprintf( stderr, "entering debugger\n" );
168*cdf0e10cSrcweir 		while( TRUE ) {
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 			m_aDebugCondition.wait();
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 			fprintf( stderr , "(debug %d) : " , m_bIsRunning );
173*cdf0e10cSrcweir 			fflush( stderr);
174*cdf0e10cSrcweir 			fgets( pcLine ,  79 , stdin );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 			if( strlen( pcLine) ) pcLine[strlen(pcLine)-1] =0;
177*cdf0e10cSrcweir 			String sLine( pcLine );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 			if( ! strcmp( pcLine , "g" ) ) {
180*cdf0e10cSrcweir 				if( m_bIsRunning ) {
181*cdf0e10cSrcweir 					m_aDebugCondition.reset();
182*cdf0e10cSrcweir 					(*m_pDebuggingRef)->doContinue();
183*cdf0e10cSrcweir 				}
184*cdf0e10cSrcweir 				else fprintf( stderr,"no script running !\n" );
185*cdf0e10cSrcweir 			}
186*cdf0e10cSrcweir 			else if( ! strcmp( pcLine , "s" ) ) {
187*cdf0e10cSrcweir 				if( m_bIsRunning ) {
188*cdf0e10cSrcweir 					m_aDebugCondition.reset();
189*cdf0e10cSrcweir 					(*m_pDebuggingRef)->stepOver();
190*cdf0e10cSrcweir 				}
191*cdf0e10cSrcweir 				else fprintf(stderr, "no script running !\n" );
192*cdf0e10cSrcweir 			}
193*cdf0e10cSrcweir 			else if( ! strcmp( pcLine , "so" ) ) {
194*cdf0e10cSrcweir 				if( m_bIsRunning ) {
195*cdf0e10cSrcweir 					m_aDebugCondition.reset();
196*cdf0e10cSrcweir 					(*m_pDebuggingRef)->stepOut();
197*cdf0e10cSrcweir 				}
198*cdf0e10cSrcweir 				else fprintf(stderr, "no script running !\n" );
199*cdf0e10cSrcweir 			}
200*cdf0e10cSrcweir 			else if( ! strcmp( pcLine , "si" ) ) {
201*cdf0e10cSrcweir 				if( m_bIsRunning ) {
202*cdf0e10cSrcweir 					m_aDebugCondition.reset();
203*cdf0e10cSrcweir 					(*m_pDebuggingRef)->stepIn();
204*cdf0e10cSrcweir 				}
205*cdf0e10cSrcweir 				else fprintf(stderr, "no script running !\n" );
206*cdf0e10cSrcweir 			}
207*cdf0e10cSrcweir 			else if( ! strncmp( pcLine , "sbp" , 3 ) ){
208*cdf0e10cSrcweir 				if( m_bIsRunning ) {
209*cdf0e10cSrcweir 					(*m_pDebuggingRef)->setBreakPoint( 	UString( L"<string>" ),
210*cdf0e10cSrcweir 														atoi(&pcLine[3]) , TRUE );
211*cdf0e10cSrcweir 				}
212*cdf0e10cSrcweir 			}
213*cdf0e10cSrcweir 			else if( ! strncmp( pcLine , "rbp" , 3 ) ){
214*cdf0e10cSrcweir 				if( m_bIsRunning ) {
215*cdf0e10cSrcweir 					(*m_pDebuggingRef)->setBreakPoint( 	UString( L"<string>" ),
216*cdf0e10cSrcweir 														atoi(&pcLine[3]) , FALSE );
217*cdf0e10cSrcweir 				}
218*cdf0e10cSrcweir 			}
219*cdf0e10cSrcweir 			else if( ! strncmp( pcLine , "dv " , 3 ) ) {
220*cdf0e10cSrcweir 				if( m_bIsRunning ) {
221*cdf0e10cSrcweir 					int nCallstack = 0;
222*cdf0e10cSrcweir 					if( sLine.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
223*cdf0e10cSrcweir 						nCallstack = atoi( sLine.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
224*cdf0e10cSrcweir 					}
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 					UString str = (*m_pDebuggingRef)->dumpVariable(
227*cdf0e10cSrcweir 													PCHAR_TO_USTRING( &pcLine[3]),nCallstack);
228*cdf0e10cSrcweir 					fprintf( stderr, "%s\n" , USTRING_TO_PCHAR( str ) );
229*cdf0e10cSrcweir 				}
230*cdf0e10cSrcweir 			}
231*cdf0e10cSrcweir 			else if( ! strncmp( pcLine , "sv " , 3 ) ) {
232*cdf0e10cSrcweir 				int nCallstack = 0;
233*cdf0e10cSrcweir 				if( sLine.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
234*cdf0e10cSrcweir 					nCallstack = atoi( sLine.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
235*cdf0e10cSrcweir 				}
236*cdf0e10cSrcweir 				(*m_pDebuggingRef)->setVariable(
237*cdf0e10cSrcweir 						StringToOUString( sLine.GetQuotedToken( 1 , String("''"), ' ' ), CHARSET_SYSTEM ),
238*cdf0e10cSrcweir 						StringToOUString( sLine.GetQuotedToken( 2 , String("''"), ' ' ), CHARSET_SYSTEM ),
239*cdf0e10cSrcweir 						nCallstack );
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 			}
242*cdf0e10cSrcweir 			else if( ! strncmp( pcLine , "ci" ,2 ) ) {
243*cdf0e10cSrcweir 				if( m_bIsRunning ) {
244*cdf0e10cSrcweir 					UString *aUString ;
245*cdf0e10cSrcweir 					ContextInformation ci = (*m_pDebuggingRef)->getContextInformation(atoi(&pcLine[2]));
246*cdf0e10cSrcweir 					int i,iMax;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 					fprintf( stderr, "File %s (%d)\n", USTRING_TO_PCHAR(ci.Name),
249*cdf0e10cSrcweir 													   ci.StartLine );
250*cdf0e10cSrcweir 					fprintf( stderr, "Available variables : \n" );
251*cdf0e10cSrcweir 					aUString = ci.LocalVariableNames.getArray();
252*cdf0e10cSrcweir 					iMax = ci.LocalVariableNames.getLen();
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 					for( i = 0 ; i < iMax ; i++ ) {
255*cdf0e10cSrcweir 						fprintf( stderr, "      %s\n" , USTRING_TO_PCHAR( aUString[i]) );
256*cdf0e10cSrcweir 					}
257*cdf0e10cSrcweir 				}
258*cdf0e10cSrcweir 			}
259*cdf0e10cSrcweir 			else if ( !strcmp( pcLine , "d" ) ) {
260*cdf0e10cSrcweir 				if( m_bIsRunning ) {
261*cdf0e10cSrcweir 					UString * aUString ;
262*cdf0e10cSrcweir 					Sequence<UString> seq =  (*m_pDebuggingRef)->getStackTrace();
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 					aUString = seq.getArray();
265*cdf0e10cSrcweir 					int iMax = seq.getLen();
266*cdf0e10cSrcweir 					for( int i = 0; i < iMax ; i++ ) {
267*cdf0e10cSrcweir 						fprintf( stderr , "%s\n" , USTRING_TO_PCHAR( aUString[i] ) );
268*cdf0e10cSrcweir 					}
269*cdf0e10cSrcweir 				}
270*cdf0e10cSrcweir 			}
271*cdf0e10cSrcweir 			else if( !strcmp( pcLine , "c" ) ) {
272*cdf0e10cSrcweir 				if( m_bIsRunning ) {
273*cdf0e10cSrcweir 					(*m_pEngineRef)->cancel();
274*cdf0e10cSrcweir 					m_aDebugCondition.reset();
275*cdf0e10cSrcweir 				}
276*cdf0e10cSrcweir 				else fprintf( stderr,"no script running !\n" );
277*cdf0e10cSrcweir 			}
278*cdf0e10cSrcweir 			else if( !strcmp( pcLine , "q" ) ) {
279*cdf0e10cSrcweir 				if( m_bIsRunning ) {
280*cdf0e10cSrcweir 					m_aDebugCondition.reset();
281*cdf0e10cSrcweir 					(*m_pEngineRef)->cancel();
282*cdf0e10cSrcweir 				}
283*cdf0e10cSrcweir 				else {
284*cdf0e10cSrcweir 					m_bIsTerminating = TRUE;
285*cdf0e10cSrcweir 					fprintf(stderr,  "Debugger terminates\n" );
286*cdf0e10cSrcweir 					break;
287*cdf0e10cSrcweir 				}
288*cdf0e10cSrcweir 			}
289*cdf0e10cSrcweir 			else if( ! strcmp( pcLine , "id" ) ) {
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 				XIntrospectionAccessRef ref = (*m_pInvokationRef)->getIntrospection();
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir 				dumpIntrospectionToStream( ref , stderr );
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir 			}
297*cdf0e10cSrcweir 			else if( ! strncmp( pcLine , "idv" , 3) ) {
298*cdf0e10cSrcweir 				try {
299*cdf0e10cSrcweir 					UsrAny any = (*m_pInvokationRef)->getValue( PCHAR_TO_USTRING( &(pcLine[4]) ) );
300*cdf0e10cSrcweir 					dumpVarToStream( &(pcLine[4]) , any , stderr );
301*cdf0e10cSrcweir 				}
302*cdf0e10cSrcweir 				catch(UnknownPropertyException& e ) {
303*cdf0e10cSrcweir 					fprintf( stderr, "UnknownPropertyException\n" );
304*cdf0e10cSrcweir 				}
305*cdf0e10cSrcweir 				catch(IllegalArgumentException& e ) {
306*cdf0e10cSrcweir 					fprintf( stderr, "IllegalArgumentException\n" );
307*cdf0e10cSrcweir 				}
308*cdf0e10cSrcweir 			}
309*cdf0e10cSrcweir 			else if( !strcmp( pcLine , "t" ) ) {
310*cdf0e10cSrcweir 			}
311*cdf0e10cSrcweir 			else if( !strcmp( pcLine , "h" ) ) {
312*cdf0e10cSrcweir 				fprintf( stderr ,  "\nvalid commands :\n"
313*cdf0e10cSrcweir 								   "Go                 : g\n"
314*cdf0e10cSrcweir 								   "StepOver           : s\n"
315*cdf0e10cSrcweir 								   "StepIn             : si\n"
316*cdf0e10cSrcweir 								   "StepOut            : so\n"
317*cdf0e10cSrcweir 								   "Set BreakPoint     : sbp Line [ModuleName]\n"
318*cdf0e10cSrcweir 								   "Remove BreakPoint  : rbp [Line] [ModuleName]\n"
319*cdf0e10cSrcweir 								   "via XDebugging Interface :\n"
320*cdf0e10cSrcweir 								   "    dump Variable      : dv varname [CallStack]\n"
321*cdf0e10cSrcweir 								   "    set Variable       : sv varname value [CallStack]\n"
322*cdf0e10cSrcweir 								   "globals via XInvokation Interface :\n"
323*cdf0e10cSrcweir 								   "	dump Global vars   : id\n"
324*cdf0e10cSrcweir 								   "    dump Variable      : idv varname\n"
325*cdf0e10cSrcweir 								   "    set Variable       : isv varname value\n"
326*cdf0e10cSrcweir 								   "ContextInformation : ci\n"
327*cdf0e10cSrcweir 								   "Dump callstack     : d\n"
328*cdf0e10cSrcweir 								   "Cancel             : c (stops actual script)\n"
329*cdf0e10cSrcweir 								   "Quit               : q (exits debugger)\n"
330*cdf0e10cSrcweir 								   );
331*cdf0e10cSrcweir 			}
332*cdf0e10cSrcweir 			else if( ! strlen( pcLine ) ) {
333*cdf0e10cSrcweir 			}
334*cdf0e10cSrcweir 			else {
335*cdf0e10cSrcweir 				fprintf( stderr , "unknown command %s\n" , pcLine );
336*cdf0e10cSrcweir 			}
337*cdf0e10cSrcweir 		}
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir void CmdDebugger::dumpIntrospectionToStream( const XIntrospectionAccessRef &ref, FILE *f )
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir 	int i,iMax;
343*cdf0e10cSrcweir 	fprintf( stderr, "Callable Attributes (Methods) :\n" );
344*cdf0e10cSrcweir 	Sequence<XIdlMethodRef> seq = ref->getMethods( 0 );
345*cdf0e10cSrcweir 	iMax = seq.getLen();
346*cdf0e10cSrcweir 	XIdlMethodRef *aRef = seq.getArray();
347*cdf0e10cSrcweir 	for( i = 0; i < iMax ; i++ ) {
348*cdf0e10cSrcweir 		fprintf( f, "  %s\n" , USTRING_TO_PCHAR( aRef[i]->getName( ) ) );
349*cdf0e10cSrcweir 	}
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir 	fprintf( stderr, "Other attributes\n"  );
352*cdf0e10cSrcweir 	Sequence<Property> seqProp = ref->getProperties( 0 );
353*cdf0e10cSrcweir 	iMax = seqProp.getLen();
354*cdf0e10cSrcweir 	Property *aProp = seqProp.getArray();
355*cdf0e10cSrcweir 	for( i = 0; i < iMax ; i ++ ) {
356*cdf0e10cSrcweir 		fprintf( f, "  %s %s\n" , 	USTRING_TO_PCHAR( aProp[i].Type->getName() ),
357*cdf0e10cSrcweir 										USTRING_TO_PCHAR( aProp[i].Name	) );
358*cdf0e10cSrcweir 	}
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir void CmdDebugger::dumpVarToStream( const char *pc , const UsrAny &aValue, FILE *f )
363*cdf0e10cSrcweir {
364*cdf0e10cSrcweir 	TypeClass type = aValue.getReflection()->getTypeClass();
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir 	if( TypeClass_INT == type ) {
367*cdf0e10cSrcweir 		fprintf( f, "INT32 %s : %d\n" , pc , aValue.getINT32() );
368*cdf0e10cSrcweir 	}
369*cdf0e10cSrcweir 	else if( TypeClass_ENUM == type ) {
370*cdf0e10cSrcweir 		fprintf( f, "ENUM %s : %d\n", pc ,  aValue.getEnumAsINT32() );
371*cdf0e10cSrcweir 	}
372*cdf0e10cSrcweir 	else if( TypeClass_STRING == type ) {
373*cdf0e10cSrcweir 		fprintf( f, "STRING %s : %s\n" , pc ,  USTRING_TO_PCHAR( aValue.getString())	 );
374*cdf0e10cSrcweir 	}
375*cdf0e10cSrcweir 	else if( TypeClass_BOOLEAN == type ) {
376*cdf0e10cSrcweir 		fprintf( f, "BOOL %s : %d\n", pc , aValue.getBOOL() );
377*cdf0e10cSrcweir 	}
378*cdf0e10cSrcweir 	else if( TypeClass_CHAR == type  ) {
379*cdf0e10cSrcweir 		fprintf( f, "char %s : %d\n", pc , ( INT32) aValue.getChar() );
380*cdf0e10cSrcweir 	}
381*cdf0e10cSrcweir 	else if( TypeClass_SHORT == type ) {
382*cdf0e10cSrcweir 		fprintf( f, "INT16 %s : %d\n", pc , (INT32) aValue.getINT16());
383*cdf0e10cSrcweir 	}
384*cdf0e10cSrcweir 	else if( TypeClass_LONG == type ) {
385*cdf0e10cSrcweir 		fprintf( f, "LONG %s : %d\n", pc , (INT32) aValue.getINT32());
386*cdf0e10cSrcweir 	}
387*cdf0e10cSrcweir 	else if( TypeClass_UNSIGNED_SHORT == type ) {
388*cdf0e10cSrcweir 		fprintf( f, "UINT16 %s : %d\n", pc , (INT32) aValue.getUINT16() );
389*cdf0e10cSrcweir 	}
390*cdf0e10cSrcweir 	else if( TypeClass_UNSIGNED_BYTE == type ) {
391*cdf0e10cSrcweir 		fprintf( f, "Byte %s : %d\n", pc ,	(INT32) aValue.getBYTE() );
392*cdf0e10cSrcweir 	}
393*cdf0e10cSrcweir 	else if( TypeClass_UNSIGNED_INT == type ) {
394*cdf0e10cSrcweir 		fprintf( f, "UINT32 %s : %d\n", pc , aValue.getUINT32() );
395*cdf0e10cSrcweir 	}
396*cdf0e10cSrcweir 	else if( TypeClass_FLOAT == type ) {
397*cdf0e10cSrcweir 		fprintf( f, "float %s : %f\n" , pc , aValue.getFloat() );
398*cdf0e10cSrcweir 	}
399*cdf0e10cSrcweir 	else if( TypeClass_DOUBLE == type ) {
400*cdf0e10cSrcweir 		fprintf( f, "double %s : %f\n" , pc , aValue.getDouble() );
401*cdf0e10cSrcweir 	}
402*cdf0e10cSrcweir 	else if( TypeClass_VOID == type ) {
403*cdf0e10cSrcweir 		fprintf( f, "void %s :\n" , pc );
404*cdf0e10cSrcweir 	}
405*cdf0e10cSrcweir 	else if( TypeClass_INTERFACE == type ) {
406*cdf0e10cSrcweir 		// Check, what has been put in
407*cdf0e10cSrcweir 		if( aValue.getReflection() == XPropertySet_getReflection() ) {
408*cdf0e10cSrcweir 			// XPropertySet !
409*cdf0e10cSrcweir 			XPropertySetRef *pRef = ( XPropertySetRef * ) aValue.get();
410*cdf0e10cSrcweir 			XPropertySetInfoRef refInfo = (*pRef)->getPropertySetInfo();
411*cdf0e10cSrcweir 			Sequence< Property > seq = refInfo->getProperties();
412*cdf0e10cSrcweir 			int i,iMax = seq.getLen();
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 			Property *pArray;
415*cdf0e10cSrcweir 			pArray = seq.getArray();
416*cdf0e10cSrcweir 			fprintf( stderr, "Property List :\n" );
417*cdf0e10cSrcweir 			for( i = 0; i < iMax ; i ++ ) {
418*cdf0e10cSrcweir 				fprintf( f, "%s\t %s\n" , USTRING_TO_PCHAR(pArray[i].Type->getName()),
419*cdf0e10cSrcweir 				      					       USTRING_TO_PCHAR( pArray[i].Name ) );
420*cdf0e10cSrcweir 			}
421*cdf0e10cSrcweir 		}
422*cdf0e10cSrcweir 		else if( aValue.getReflection() == XInvokation_getReflection() ) {
423*cdf0e10cSrcweir 			XInvokationRef *pRef = ( XInvokationRef * ) aValue.get();
424*cdf0e10cSrcweir 			XIntrospectionAccessRef refIntro = (*pRef)->getIntrospection();
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir 			dumpIntrospectionToStream( refIntro, stderr );
427*cdf0e10cSrcweir 		}
428*cdf0e10cSrcweir 	}
429*cdf0e10cSrcweir 	else if( TypeClass_SEQUENCE == type ) {
430*cdf0e10cSrcweir 		fprintf( f , "%s Sequence \n" , pc  );
431*cdf0e10cSrcweir 		String s( "   " );
432*cdf0e10cSrcweir 		s += pc;
433*cdf0e10cSrcweir 		SequenceReflection *pSeqRefl = ( SequenceReflection * ) aValue.getReflection();
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir 		int i,iMax = pSeqRefl->getLen( aValue );
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir 		for( i = 0 ; i < iMax ; i ++ ) {
438*cdf0e10cSrcweir 			dumpVarToStream( s.GetCharStr() , pSeqRefl->get( aValue , i ) , stderr );
439*cdf0e10cSrcweir 		}
440*cdf0e10cSrcweir 	}
441*cdf0e10cSrcweir 	else {
442*cdf0e10cSrcweir 		fprintf( f, "%s : unknown %d\n" , pc , type  );
443*cdf0e10cSrcweir 	}
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir }
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir void CmdDebugger::detach()
448*cdf0e10cSrcweir {
449*cdf0e10cSrcweir 	OSL_ASSERT( m_pDebuggingRef );
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir    	m_bIsRunning = FALSE;
452*cdf0e10cSrcweir    	m_pDebuggingRef = 0;
453*cdf0e10cSrcweir 	m_pEngineRef = 0;
454*cdf0e10cSrcweir 	m_pInvokationRef = 0;
455*cdf0e10cSrcweir }
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir // Methoden von XInterface
458*cdf0e10cSrcweir BOOL CmdDebugger::queryInterface( Uik aUik, XInterfaceRef & rOut )
459*cdf0e10cSrcweir {
460*cdf0e10cSrcweir 	if( aUik == XEngineListener::getSmartUik() )
461*cdf0e10cSrcweir 		rOut = (XEngineListener*)this;
462*cdf0e10cSrcweir 	else
463*cdf0e10cSrcweir 		return OWeakObject::queryInterface( aUik, rOut );
464*cdf0e10cSrcweir 	return TRUE;
465*cdf0e10cSrcweir }
466*cdf0e10cSrcweir 
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir /*
473*cdf0e10cSrcweir  * main.
474*cdf0e10cSrcweir  */
475*cdf0e10cSrcweir int __LOADONCALLAPI main (int argc, char **argv)
476*cdf0e10cSrcweir {
477*cdf0e10cSrcweir 	XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
478*cdf0e10cSrcweir 	registerUsrServices( xSMgr );
479*cdf0e10cSrcweir 	setProcessServiceManager( xSMgr );
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir 	XInterfaceRef x = xSMgr->createInstance( L"stardiv.uno.repos.ImplementationRegistration" );
482*cdf0e10cSrcweir 	XImplementationRegistrationRef xReg( x, USR_QUERY );
483*cdf0e10cSrcweir 	sal_Char szBuf[1024];
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir 	ORealDynamicLoader::computeModuleName( "pythonengine", szBuf, 1024 );
486*cdf0e10cSrcweir 	UString aDllName( StringToOUString( szBuf, CHARSET_SYSTEM ) );
487*cdf0e10cSrcweir 	xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir 	ORealDynamicLoader::computeModuleName( "aps", szBuf, 1024 );
490*cdf0e10cSrcweir 	aDllName = UString( StringToOUString( szBuf, CHARSET_SYSTEM ) );
491*cdf0e10cSrcweir 	xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
492*cdf0e10cSrcweir 
493*cdf0e10cSrcweir 	XInterfaceRef y = xSMgr->createInstance( L"stardiv.script.Python" );
494*cdf0e10cSrcweir 	XEngineRef yEngine( y, USR_QUERY );
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir 	x = xSMgr->createInstance( L"stardiv.script.Python" );
497*cdf0e10cSrcweir 	XEngineRef xEngine( x, USR_QUERY );
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir 	UString Script;
501*cdf0e10cSrcweir 
502*cdf0e10cSrcweir 	Sequence<UsrAny> args(3);
503*cdf0e10cSrcweir 	UsrAny *pArray = args.getArray();
504*cdf0e10cSrcweir 	pArray[0].setString( L"Arg_0" );
505*cdf0e10cSrcweir 	pArray[1].setString( L"Arg_1" );
506*cdf0e10cSrcweir 	pArray[2].setString( L"Arg_2" );
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir 	if( argc > 2) {
509*cdf0e10cSrcweir 		Script = StringToOUString( String( argv[2] ) ,  CHARSET_DONTKNOW );
510*cdf0e10cSrcweir 	}
511*cdf0e10cSrcweir 
512*cdf0e10cSrcweir 	XInvokationRef xInvokation( x , USR_QUERY );
513*cdf0e10cSrcweir 	XDebuggingRef xDebug( x , USR_QUERY );
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir 	CmdDebugger *pDbg = new CmdDebugger( &xDebug , &xEngine , &xInvokation );
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir 	XEngineListenerRef xDebugRef( (XEngineListener *) pDbg , USR_QUERY);
518*cdf0e10cSrcweir 	xEngine->addEngineListener( xDebugRef );
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir 	if( argc >1 && ! strcmp( argv[1] , "1" ) ) {
522*cdf0e10cSrcweir 		fprintf( stderr, "one thread only\n" );
523*cdf0e10cSrcweir 		Script = UString( 	L"print 'Hello World'\n" );
524*cdf0e10cSrcweir 		xEngine->runAsync( Script ,  XInterfaceRef(), args , XEngineListenerRef() );
525*cdf0e10cSrcweir 	}
526*cdf0e10cSrcweir 	else if( argc >1 && ! strcmp( argv[1] , "2" ) )  {
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"x=1\nprint 1\n") ,  XInterfaceRef(), args , XEngineListenerRef() );
529*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"x=x+1\nprint 2\n") ,  XInterfaceRef(), args , XEngineListenerRef() );
530*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"x=x+1\nprint 3\n") ,  XInterfaceRef(), args , XEngineListenerRef());
531*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"x=x+1\nprint 4\n") ,  XInterfaceRef(), args , XEngineListenerRef() );
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir 	}
536*cdf0e10cSrcweir 	else if( argc >1 && ! strcmp( argv[1] , "3" ) ) {
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir 		fprintf( stderr , "1st thread in engine y, next 5 threads in engine x\n" );
539*cdf0e10cSrcweir 		yEngine->runAsync( UString( L"print 1\n") ,  XInterfaceRef(), args , XEngineListenerRef() );
540*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"print 2\n") ,  XInterfaceRef(), args , XEngineListenerRef() );
541*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"print 3\n") ,  XInterfaceRef(), args , XEngineListenerRef() );
542*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"print 4\n") ,  XInterfaceRef(), args , XEngineListenerRef());
543*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"print 5\n") ,  XInterfaceRef(), args , XEngineListenerRef());
544*cdf0e10cSrcweir 		xEngine->runAsync( UString( L"print 6\n") ,  XInterfaceRef(), args , XEngineListenerRef());
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir 	}
548*cdf0e10cSrcweir 	pDbg->cmdLine();
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir 	xEngine->removeEngineListener( xDebugRef );
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir 	xReg->revokeImplementation( aDllName, XSimpleRegistryRef() );
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir 	fprintf( stderr, "main terminates\n" );
555*cdf0e10cSrcweir 	return 0;
556*cdf0e10cSrcweir }
557*cdf0e10cSrcweir 
558