bump product version to 4.1.6.2
[LibreOffice.git] / extensions / workben / pythontest.cxx
blobc1c6bdb4cf959914158c1351cce105a877a6e5eb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <stdio.h>
22 #include <stardiv/uno/repos/implementationregistration.hxx>
23 #include <stardiv/uno/script/script.hxx>
24 #include <stardiv/uno/beans/exactname.hxx>
26 #include <rtl/ustring.hxx>
27 #include <osl/diagnose.h>
28 #include <usr/services.hxx>
29 #include <vcl/svapp.hxx>
30 #include <usr/ustring.hxx>
31 #include <usr/weak.hxx>
32 #include <tools/string.hxx>
33 #include <osl/conditn.hxx>
35 using namespace usr;
37 using ::rtl::StringToOUString;
40 #define PCHAR_TO_USTRING(x) StringToOUString(String(x),CHARSET_SYSTEM)
44 class NullEngineListenerRef : public XEngineListenerRef
46 virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) ) {}
47 virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) ) {}
48 virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) ) {}
51 #define USTRING_TO_PCHAR(x) OUStringToString(x , CHARSET_DONTKNOW ).GetCharStr()
53 class CmdDebugger :
54 public XEngineListener,
55 public OWeakObject
57 public:
59 CmdDebugger()
61 m_pDebuggingRef = 0;
62 m_pEngineRef = 0;
63 m_bIsTerminating = FALSE;
64 m_bIsRunning = FALSE;
68 CmdDebugger( XDebuggingRef *p, XEngineRef *pEngine , XInvokationRef *pInvokation)
70 attach( p , pEngine , pInvokation );
73 ~CmdDebugger()
75 if( m_pDebuggingRef ) {
76 detach();
80 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
81 void acquire() { OWeakObject::acquire(); }
82 void release() { OWeakObject::release(); }
83 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
86 void attach( XDebuggingRef *p , XEngineRef *pEngine , XInvokationRef *pInvokation )
88 m_pDebuggingRef = p;
89 m_pEngineRef = pEngine;
90 m_pInvokationRef = pInvokation;
91 m_bIsRunning = FALSE;
92 m_bIsTerminating = FALSE;
95 void detach( );
98 virtual void disposing( const EventObject &o )
100 if( m_pDebuggingRef ) {
101 detach();
104 virtual void interrupt(const InterruptEngineEvent& Evt) THROWS( (UsrSystemException) )
106 if( m_pDebuggingRef && ! m_bIsTerminating ) {
107 (*m_pDebuggingRef)->stop();
108 fprintf( stderr, "%s\n" , USTRING_TO_PCHAR(Evt.ErrorMessage ) );
109 fprintf( stderr, "%s.%s (%d)\n", USTRING_TO_PCHAR(Evt.SourceCode),
110 USTRING_TO_PCHAR(Evt.Name ),
111 Evt.StartLine );
112 m_aDebugCondition.set();
113 m_bIsRunning = TRUE;
117 virtual void running(const EventObject& Evt) THROWS( (UsrSystemException) )
119 if( m_pDebuggingRef && ! m_bIsTerminating ) {
120 (*m_pDebuggingRef)->stop();
122 m_aDebugCondition.set();
123 m_bIsRunning = TRUE;
124 fprintf( stderr, "%s\n" , "Script starts\n" );
128 virtual void finished(const FinishEngineEvent& Evt) THROWS( (UsrSystemException) )
130 if( m_pDebuggingRef && ! m_bIsTerminating ) {
131 m_aDebugCondition.set();
132 m_bIsRunning = FALSE;
133 fprintf( stderr , "%s\n", USTRING_TO_PCHAR( Evt.ErrorMessage ) );
137 void dumpIntrospectionToStream( const XIntrospectionAccessRef &, FILE *f );
138 void dumpVarToStream( const char *pcName, const UsrAny &any, FILE *f );
141 void cmdLine();
142 protected:
144 osl::Condition m_aDebugCondition;
145 XDebuggingRef *m_pDebuggingRef;
146 XEngineRef *m_pEngineRef;
147 XInvokationRef *m_pInvokationRef;
148 int m_bIsRunning;
149 int m_bIsTerminating; // The listeners ignore everything when set
154 void CmdDebugger::cmdLine()
156 char pcLine[80];
157 fprintf( stderr, "entering debugger\n" );
158 while( TRUE ) {
160 m_aDebugCondition.wait();
162 fprintf( stderr , "(debug %d) : " , m_bIsRunning );
163 fflush( stderr);
164 fgets( pcLine , 79 , stdin );
166 if( strlen( pcLine) ) pcLine[strlen(pcLine)-1] =0;
167 String sLine( pcLine );
169 if( ! strcmp( pcLine , "g" ) ) {
170 if( m_bIsRunning ) {
171 m_aDebugCondition.reset();
172 (*m_pDebuggingRef)->doContinue();
174 else fprintf( stderr,"no script running !\n" );
176 else if( ! strcmp( pcLine , "s" ) ) {
177 if( m_bIsRunning ) {
178 m_aDebugCondition.reset();
179 (*m_pDebuggingRef)->stepOver();
181 else fprintf(stderr, "no script running !\n" );
183 else if( ! strcmp( pcLine , "so" ) ) {
184 if( m_bIsRunning ) {
185 m_aDebugCondition.reset();
186 (*m_pDebuggingRef)->stepOut();
188 else fprintf(stderr, "no script running !\n" );
190 else if( ! strcmp( pcLine , "si" ) ) {
191 if( m_bIsRunning ) {
192 m_aDebugCondition.reset();
193 (*m_pDebuggingRef)->stepIn();
195 else fprintf(stderr, "no script running !\n" );
197 else if( ! strncmp( pcLine , "sbp" , 3 ) ){
198 if( m_bIsRunning ) {
199 (*m_pDebuggingRef)->setBreakPoint( UString( L"<string>" ),
200 atoi(&pcLine[3]) , TRUE );
203 else if( ! strncmp( pcLine , "rbp" , 3 ) ){
204 if( m_bIsRunning ) {
205 (*m_pDebuggingRef)->setBreakPoint( UString( L"<string>" ),
206 atoi(&pcLine[3]) , FALSE );
209 else if( ! strncmp( pcLine , "dv " , 3 ) ) {
210 if( m_bIsRunning ) {
211 int nCallstack = 0;
212 if( sLine.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
213 nCallstack = atoi( sLine.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
216 UString str = (*m_pDebuggingRef)->dumpVariable(
217 PCHAR_TO_USTRING( &pcLine[3]),nCallstack);
218 fprintf( stderr, "%s\n" , USTRING_TO_PCHAR( str ) );
221 else if( ! strncmp( pcLine , "sv " , 3 ) ) {
222 int nCallstack = 0;
223 if( sLine.GetQuotedTokenCount( String("''"),' ' ) == 3 ) {
224 nCallstack = atoi( sLine.GetQuotedToken( 3 , String("''"), ' ' ).GetCharStr() );
226 (*m_pDebuggingRef)->setVariable(
227 StringToOUString( sLine.GetQuotedToken( 1 , String("''"), ' ' ), CHARSET_SYSTEM ),
228 StringToOUString( sLine.GetQuotedToken( 2 , String("''"), ' ' ), CHARSET_SYSTEM ),
229 nCallstack );
232 else if( ! strncmp( pcLine , "ci" ,2 ) ) {
233 if( m_bIsRunning ) {
234 UString *aUString ;
235 ContextInformation ci = (*m_pDebuggingRef)->getContextInformation(atoi(&pcLine[2]));
236 int i,iMax;
238 fprintf( stderr, "File %s (%d)\n", USTRING_TO_PCHAR(ci.Name),
239 ci.StartLine );
240 fprintf( stderr, "Available variables : \n" );
241 aUString = ci.LocalVariableNames.getArray();
242 iMax = ci.LocalVariableNames.getLen();
244 for( i = 0 ; i < iMax ; i++ ) {
245 fprintf( stderr, " %s\n" , USTRING_TO_PCHAR( aUString[i]) );
249 else if ( !strcmp( pcLine , "d" ) ) {
250 if( m_bIsRunning ) {
251 UString * aUString ;
252 Sequence<UString> seq = (*m_pDebuggingRef)->getStackTrace();
254 aUString = seq.getArray();
255 int iMax = seq.getLen();
256 for( int i = 0; i < iMax ; i++ ) {
257 fprintf( stderr , "%s\n" , USTRING_TO_PCHAR( aUString[i] ) );
261 else if( !strcmp( pcLine , "c" ) ) {
262 if( m_bIsRunning ) {
263 (*m_pEngineRef)->cancel();
264 m_aDebugCondition.reset();
266 else fprintf( stderr,"no script running !\n" );
268 else if( !strcmp( pcLine , "q" ) ) {
269 if( m_bIsRunning ) {
270 m_aDebugCondition.reset();
271 (*m_pEngineRef)->cancel();
273 else {
274 m_bIsTerminating = TRUE;
275 fprintf(stderr, "Debugger terminates\n" );
276 break;
279 else if( ! strcmp( pcLine , "id" ) ) {
281 XIntrospectionAccessRef ref = (*m_pInvokationRef)->getIntrospection();
283 dumpIntrospectionToStream( ref , stderr );
287 else if( ! strncmp( pcLine , "idv" , 3) ) {
288 try {
289 UsrAny any = (*m_pInvokationRef)->getValue( PCHAR_TO_USTRING( &(pcLine[4]) ) );
290 dumpVarToStream( &(pcLine[4]) , any , stderr );
292 catch(UnknownPropertyException& e ) {
293 fprintf( stderr, "UnknownPropertyException\n" );
295 catch(IllegalArgumentException& e ) {
296 fprintf( stderr, "IllegalArgumentException\n" );
299 else if( !strcmp( pcLine , "t" ) ) {
301 else if( !strcmp( pcLine , "h" ) ) {
302 fprintf( stderr , "\nvalid commands :\n"
303 "Go : g\n"
304 "StepOver : s\n"
305 "StepIn : si\n"
306 "StepOut : so\n"
307 "Set BreakPoint : sbp Line [ModuleName]\n"
308 "Remove BreakPoint : rbp [Line] [ModuleName]\n"
309 "via XDebugging Interface :\n"
310 " dump Variable : dv varname [CallStack]\n"
311 " set Variable : sv varname value [CallStack]\n"
312 "globals via XInvokation Interface :\n"
313 " dump Global vars : id\n"
314 " dump Variable : idv varname\n"
315 " set Variable : isv varname value\n"
316 "ContextInformation : ci\n"
317 "Dump callstack : d\n"
318 "Cancel : c (stops actual script)\n"
319 "Quit : q (exits debugger)\n"
322 else if( ! strlen( pcLine ) ) {
324 else {
325 fprintf( stderr , "unknown command %s\n" , pcLine );
330 void CmdDebugger::dumpIntrospectionToStream( const XIntrospectionAccessRef &ref, FILE *f )
332 int i,iMax;
333 fprintf( stderr, "Callable Attributes (Methods) :\n" );
334 Sequence<XIdlMethodRef> seq = ref->getMethods( 0 );
335 iMax = seq.getLen();
336 XIdlMethodRef *aRef = seq.getArray();
337 for( i = 0; i < iMax ; i++ ) {
338 fprintf( f, " %s\n" , USTRING_TO_PCHAR( aRef[i]->getName( ) ) );
341 fprintf( stderr, "Other attributes\n" );
342 Sequence<Property> seqProp = ref->getProperties( 0 );
343 iMax = seqProp.getLen();
344 Property *aProp = seqProp.getArray();
345 for( i = 0; i < iMax ; i ++ ) {
346 fprintf( f, " %s %s\n" , USTRING_TO_PCHAR( aProp[i].Type->getName() ),
347 USTRING_TO_PCHAR( aProp[i].Name ) );
352 void CmdDebugger::dumpVarToStream( const char *pc , const UsrAny &aValue, FILE *f )
354 TypeClass type = aValue.getReflection()->getTypeClass();
356 if( TypeClass_INT == type ) {
357 fprintf( f, "INT32 %s : %d\n" , pc , aValue.getINT32() );
359 else if( TypeClass_ENUM == type ) {
360 fprintf( f, "ENUM %s : %d\n", pc , aValue.getEnumAsINT32() );
362 else if( TypeClass_STRING == type ) {
363 fprintf( f, "STRING %s : %s\n" , pc , USTRING_TO_PCHAR( aValue.getString()) );
365 else if( TypeClass_BOOLEAN == type ) {
366 fprintf( f, "BOOL %s : %d\n", pc , aValue.getBOOL() );
368 else if( TypeClass_CHAR == type ) {
369 fprintf( f, "char %s : %d\n", pc , ( INT32) aValue.getChar() );
371 else if( TypeClass_SHORT == type ) {
372 fprintf( f, "INT16 %s : %d\n", pc , (INT32) aValue.getINT16());
374 else if( TypeClass_LONG == type ) {
375 fprintf( f, "LONG %s : %d\n", pc , (INT32) aValue.getINT32());
377 else if( TypeClass_UNSIGNED_SHORT == type ) {
378 fprintf( f, "UINT16 %s : %d\n", pc , (INT32) aValue.getUINT16() );
380 else if( TypeClass_UNSIGNED_BYTE == type ) {
381 fprintf( f, "Byte %s : %d\n", pc , (INT32) aValue.getBYTE() );
383 else if( TypeClass_UNSIGNED_INT == type ) {
384 fprintf( f, "UINT32 %s : %d\n", pc , aValue.getUINT32() );
386 else if( TypeClass_FLOAT == type ) {
387 fprintf( f, "float %s : %f\n" , pc , aValue.getFloat() );
389 else if( TypeClass_DOUBLE == type ) {
390 fprintf( f, "double %s : %f\n" , pc , aValue.getDouble() );
392 else if( TypeClass_VOID == type ) {
393 fprintf( f, "void %s :\n" , pc );
395 else if( TypeClass_INTERFACE == type ) {
396 // Check, what has been put in
397 if( aValue.getReflection() == XPropertySet_getReflection() ) {
398 // XPropertySet !
399 XPropertySetRef *pRef = ( XPropertySetRef * ) aValue.get();
400 XPropertySetInfoRef refInfo = (*pRef)->getPropertySetInfo();
401 Sequence< Property > seq = refInfo->getProperties();
402 int i,iMax = seq.getLen();
404 Property *pArray;
405 pArray = seq.getArray();
406 fprintf( stderr, "Property List :\n" );
407 for( i = 0; i < iMax ; i ++ ) {
408 fprintf( f, "%s\t %s\n" , USTRING_TO_PCHAR(pArray[i].Type->getName()),
409 USTRING_TO_PCHAR( pArray[i].Name ) );
412 else if( aValue.getReflection() == XInvokation_getReflection() ) {
413 XInvokationRef *pRef = ( XInvokationRef * ) aValue.get();
414 XIntrospectionAccessRef refIntro = (*pRef)->getIntrospection();
416 dumpIntrospectionToStream( refIntro, stderr );
419 else if( TypeClass_SEQUENCE == type ) {
420 fprintf( f , "%s Sequence \n" , pc );
421 String s( " " );
422 s += pc;
423 SequenceReflection *pSeqRefl = ( SequenceReflection * ) aValue.getReflection();
425 int i,iMax = pSeqRefl->getLen( aValue );
427 for( i = 0 ; i < iMax ; i ++ ) {
428 dumpVarToStream( s.GetCharStr() , pSeqRefl->get( aValue , i ) , stderr );
431 else {
432 fprintf( f, "%s : unknown %d\n" , pc , type );
437 void CmdDebugger::detach()
439 OSL_ASSERT( m_pDebuggingRef );
441 m_bIsRunning = FALSE;
442 m_pDebuggingRef = 0;
443 m_pEngineRef = 0;
444 m_pInvokationRef = 0;
447 // Methoden von XInterface
448 BOOL CmdDebugger::queryInterface( Uik aUik, XInterfaceRef & rOut )
450 if( aUik == XEngineListener::getSmartUik() )
451 rOut = (XEngineListener*)this;
452 else
453 return OWeakObject::queryInterface( aUik, rOut );
454 return TRUE;
463 * main.
465 int SAL_CALL main (int argc, char **argv)
467 XMultiServiceFactoryRef xSMgr = createRegistryServiceManager();
468 registerUsrServices( xSMgr );
469 setProcessServiceManager( xSMgr );
471 XInterfaceRef x = xSMgr->createInstance( L"stardiv.uno.repos.ImplementationRegistration" );
472 XImplementationRegistrationRef xReg( x, USR_QUERY );
473 sal_Char szBuf[1024];
475 ORealDynamicLoader::computeModuleName( "pythonengine", szBuf, 1024 );
476 UString aDllName( StringToOUString( szBuf, CHARSET_SYSTEM ) );
477 xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
479 ORealDynamicLoader::computeModuleName( "aps", szBuf, 1024 );
480 aDllName = UString( StringToOUString( szBuf, CHARSET_SYSTEM ) );
481 xReg->registerImplementation( L"stardiv.loader.SharedLibrary", aDllName, XSimpleRegistryRef() );
483 XInterfaceRef y = xSMgr->createInstance( L"stardiv.script.Python" );
484 XEngineRef yEngine( y, USR_QUERY );
486 x = xSMgr->createInstance( L"stardiv.script.Python" );
487 XEngineRef xEngine( x, USR_QUERY );
490 UString Script;
492 Sequence<UsrAny> args(3);
493 UsrAny *pArray = args.getArray();
494 pArray[0].setString( L"Arg_0" );
495 pArray[1].setString( L"Arg_1" );
496 pArray[2].setString( L"Arg_2" );
498 if( argc > 2) {
499 Script = StringToOUString( String( argv[2] ) , CHARSET_DONTKNOW );
502 XInvokationRef xInvokation( x , USR_QUERY );
503 XDebuggingRef xDebug( x , USR_QUERY );
505 CmdDebugger *pDbg = new CmdDebugger( &xDebug , &xEngine , &xInvokation );
507 XEngineListenerRef xDebugRef( (XEngineListener *) pDbg , USR_QUERY);
508 xEngine->addEngineListener( xDebugRef );
511 if( argc >1 && ! strcmp( argv[1] , "1" ) ) {
512 fprintf( stderr, "one thread only\n" );
513 Script = UString( L"print 'Hello World'\n" );
514 xEngine->runAsync( Script , XInterfaceRef(), args , XEngineListenerRef() );
516 else if( argc >1 && ! strcmp( argv[1] , "2" ) ) {
518 xEngine->runAsync( UString( L"x=1\nprint 1\n") , XInterfaceRef(), args , XEngineListenerRef() );
519 xEngine->runAsync( UString( L"x=x+1\nprint 2\n") , XInterfaceRef(), args , XEngineListenerRef() );
520 xEngine->runAsync( UString( L"x=x+1\nprint 3\n") , XInterfaceRef(), args , XEngineListenerRef());
521 xEngine->runAsync( UString( L"x=x+1\nprint 4\n") , XInterfaceRef(), args , XEngineListenerRef() );
526 else if( argc >1 && ! strcmp( argv[1] , "3" ) ) {
528 fprintf( stderr , "1st thread in engine y, next 5 threads in engine x\n" );
529 yEngine->runAsync( UString( L"print 1\n") , XInterfaceRef(), args , XEngineListenerRef() );
530 xEngine->runAsync( UString( L"print 2\n") , XInterfaceRef(), args , XEngineListenerRef() );
531 xEngine->runAsync( UString( L"print 3\n") , XInterfaceRef(), args , XEngineListenerRef() );
532 xEngine->runAsync( UString( L"print 4\n") , XInterfaceRef(), args , XEngineListenerRef());
533 xEngine->runAsync( UString( L"print 5\n") , XInterfaceRef(), args , XEngineListenerRef());
534 xEngine->runAsync( UString( L"print 6\n") , XInterfaceRef(), args , XEngineListenerRef());
538 pDbg->cmdLine();
540 xEngine->removeEngineListener( xDebugRef );
542 xReg->revokeImplementation( aDllName, XSimpleRegistryRef() );
544 fprintf( stderr, "main terminates\n" );
545 return 0;
548 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */