1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <basic/sberrors.hxx>
23 #include <basic/sbmeth.hxx>
24 #include <basic/sbstar.hxx>
25 #include <basic/sbx.hxx>
27 #include <rtl/ustring.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <osl/file.hxx>
30 #include <i18nlangtag/lang.h>
36 #include <com/sun/star/lang/XComponent.hpp>
37 #include <com/sun/star/container/XEnumeration.hpp>
38 #include <com/sun/star/container/XIndexAccess.hpp>
39 #include <unotools/localedatawrapper.hxx>
40 #include <o3tl/deleter.hxx>
41 #include <o3tl/typed_flags_set.hxx>
42 #include <tools/wldcrd.hxx>
44 class SbiInstance
; // active StarBASIC process
45 class SbiRuntime
; // active StarBASIC procedure instance
47 struct SbiArgv
; // Argv stack element
48 struct SbiGosub
; // GOSUB stack element
49 class SbiImage
; // Code-Image
53 class SvNumberFormatter
; // time/date functions
54 enum class SbiImageFlags
;
65 struct SbiForStack
{ // for/next stack:
66 SbiForStack
* pNext
; // Chain
67 SbxVariableRef refVar
; // loop variable
68 SbxVariableRef refEnd
; // end expression / for each: Array/BasicCollection object
69 SbxVariableRef refInc
; // increment expression
73 sal_Int32 nCurCollectionIndex
;
74 std::unique_ptr
<sal_Int32
[]>
76 std::unique_ptr
<sal_Int32
[]>
78 std::unique_ptr
<sal_Int32
[]>
80 css::uno::Reference
< css::container::XEnumeration
> xEnumeration
;
81 css::uno::Reference
<css::container::XIndexAccess
> xIndexAccess
;
85 , eForType(ForType::To
)
86 , nCurCollectionIndex(0)
90 #define MAXRECURSION 500 //to prevent dead-recursions
92 // Related to: Dir, GetAttr, SetAttr
93 namespace SbAttributes
95 constexpr sal_Int16 NORMAL
= 0x0000;
96 constexpr sal_Int16 READONLY
= 0x0001;
97 constexpr sal_Int16 HIDDEN
= 0x0002;
98 constexpr sal_Int16 SYSTEM
= 0x0004;
99 constexpr sal_Int16 VOLUME
= 0x0008;
100 constexpr sal_Int16 DIRECTORY
= 0x0010;
101 constexpr sal_Int16 ARCHIVE
= 0x0020;
104 // Related to: MsgBox (Buttons argument)
107 constexpr sal_Int16 OK
= 0;
108 constexpr sal_Int16 OKCANCEL
= 1;
109 constexpr sal_Int16 ABORTRETRYIGNORE
= 2;
110 constexpr sal_Int16 YESNOCANCEL
= 3;
111 constexpr sal_Int16 YESNO
= 4;
112 constexpr sal_Int16 RETRYCANCEL
= 5;
113 constexpr sal_Int16 ICONSTOP
= 16;
114 constexpr sal_Int16 ICONQUESTION
= 32;
115 constexpr sal_Int16 ICONEXCLAMATION
= 48;
116 constexpr sal_Int16 ICONINFORMATION
= 64;
117 constexpr sal_Int16 DEFBUTTON1
= 0;
118 constexpr sal_Int16 DEFBUTTON2
= 256;
119 constexpr sal_Int16 DEFBUTTON3
= 512;
120 constexpr sal_Int16 APPLMODAL
= 0;
121 constexpr sal_Int16 SYSTEMMODAL
= 4096;
123 // Related to: MsgBox (return value)
126 constexpr sal_Int16 OK
= 1;
127 constexpr sal_Int16 CANCEL
= 2;
128 constexpr sal_Int16 ABORT
= 3;
129 constexpr sal_Int16 RETRY
= 4;
130 constexpr sal_Int16 IGNORE
= 5;
131 constexpr sal_Int16 YES
= 6;
132 constexpr sal_Int16 NO
= 7;
136 // Related to: SwFieldTypesEnum in sw/inc/fldbas.hxx
139 constexpr sal_Int16 DATE
= 0;
140 constexpr sal_Int16 TIME
= 1;
141 constexpr sal_Int16 FILENAME
= 2;
142 constexpr sal_Int16 DATABASENAME
= 3;
143 constexpr sal_Int16 CHAPTER
= 4;
144 constexpr sal_Int16 PAGENUMBER
= 5;
145 constexpr sal_Int16 DOCUMENTSTATISTICS
= 6;
146 constexpr sal_Int16 AUTHOR
= 7;
147 constexpr sal_Int16 SET
= 8;
148 constexpr sal_Int16 GET
= 9;
149 constexpr sal_Int16 FORMEL
= 10;
150 constexpr sal_Int16 HIDDENTEXT
= 11;
151 constexpr sal_Int16 SETREF
= 12;
152 constexpr sal_Int16 GETREF
= 13;
153 constexpr sal_Int16 DDE
= 14;
154 constexpr sal_Int16 MACRO
= 15;
155 constexpr sal_Int16 INPUT
= 16;
156 constexpr sal_Int16 HIDDENPARAGRAPH
= 17;
157 constexpr sal_Int16 DOCUMENTINFO
= 18;
158 constexpr sal_Int16 DATABASE
= 19;
159 constexpr sal_Int16 USER
= 20;
160 constexpr sal_Int16 POSTIT
= 21;
161 constexpr sal_Int16 TEMPLATENAME
= 22;
162 constexpr sal_Int16 SEQUENCE
= 23;
163 constexpr sal_Int16 DATABASENEXTSET
= 24;
164 constexpr sal_Int16 DATABASENUMBERSET
= 25;
165 constexpr sal_Int16 DATABASESETNUMBER
= 26;
166 constexpr sal_Int16 CONDITIONALTEXT
= 27;
167 constexpr sal_Int16 NEXTPAGE
= 28;
168 constexpr sal_Int16 PREVIOUSPAGE
= 29;
169 constexpr sal_Int16 EXTENDEDUSER
= 30;
170 constexpr sal_Int16 FIXEDDATE
= 31;
171 constexpr sal_Int16 FIXEDTIME
= 32;
172 constexpr sal_Int16 SETINPUT
= 33;
173 constexpr sal_Int16 USERINPUT
= 34;
174 constexpr sal_Int16 SETREFPAGE
= 35;
175 constexpr sal_Int16 GETREFPAGE
= 36;
176 constexpr sal_Int16 INTERNET
= 37;
177 constexpr sal_Int16 JUMPEDIT
= 38;
184 std::unique_ptr
<osl::Directory
> pDir
;
188 OUString sFullNameToBeChecked
;
189 std::optional
<WildCard
> moWildCard
;
191 css::uno::Sequence
< OUString
> aDirSeq
;
197 // The instance matches a running StarBASIC. Many basics running at the same
198 // time are managed by chained instances. There is all the data that only lives
199 // when the BASIC is living too, like the I/O-system.
201 typedef std::vector
< css::uno::Reference
< css::lang::XComponent
> > ComponentVector_t
;
206 friend class SbiRuntime
;
211 std::unique_ptr
<SbiIoSystem
, o3tl::default_delete
<SbiIoSystem
>> pIosys
;
213 std::unique_ptr
<SbiDdeControl
> pDdeCtrl
;
214 // DLL-Calls (DECLARE)
215 std::unique_ptr
<SbiDllMgr
> pDllMgr
;
216 std::shared_ptr
<SvNumberFormatter
> pNumberFormatter
;
218 LanguageType meFormatterLangType
;
219 DateOrder meFormatterDateOrder
;
220 sal_uInt32 nStdDateIdx
, nStdTimeIdx
, nStdDateTimeIdx
;
223 OUString aErrorMsg
; // last error message for $ARG
224 sal_Int32 nErl
; // current error line
225 bool bReschedule
; // Flag: sal_True = Reschedule in main loop
226 bool bCompatibility
; // Flag: sal_True = VBA runtime compatibility mode
228 ComponentVector_t ComponentVector
;
230 SbiRuntime
* pRun
; // Call-Stack
232 // #31460 new concept for StepInto/Over/Out,
233 // explanation see runtime.cxx at SbiInstance::CalcBreakCallLevel()
235 sal_uInt16 nBreakCallLvl
;
236 void CalcBreakCallLevel( BasicDebugFlags nFlags
);
238 SbiInstance( StarBASIC
* );
241 void Error( ErrCode
); // trappable Error
242 void Error( ErrCode
, const OUString
& rMsg
); // trappable Error with message
243 void ErrorVB( sal_Int32 nVBNumber
, const OUString
& rMsg
);
244 void setErrorVB( sal_Int32 nVBNumber
);
245 void FatalError( ErrCode
); // non-trappable Error
246 void FatalError( ErrCode
, const OUString
& ); // non-trappable Error
247 void Abort(); // with current error code
250 ErrCode
const & GetErr() const { return nErr
; }
251 const OUString
& GetErrorMsg() const { return aErrorMsg
; }
252 sal_Int32
GetErl() const { return nErl
; }
253 void EnableReschedule( bool bEnable
) { bReschedule
= bEnable
; }
254 bool IsReschedule() const { return bReschedule
; }
255 void EnableCompatibility( bool bEnable
) { bCompatibility
= bEnable
; }
256 bool IsCompatibility() const { return bCompatibility
; }
258 ComponentVector_t
& getComponentVector() { return ComponentVector
; }
260 SbMethod
* GetCaller( sal_uInt16
);
261 SbModule
* GetActiveModule();
263 SbiIoSystem
* GetIoSystem() { return pIosys
.get(); }
264 SbiDdeControl
* GetDdeControl() { return pDdeCtrl
.get(); }
265 StarBASIC
* GetBasic() { return pBasic
; }
266 SbiDllMgr
* GetDllMgr();
267 SbiRTLData
& GetRTLData() { return aRTLData
; }
269 std::shared_ptr
<SvNumberFormatter
> const & GetNumberFormatter();
270 sal_uInt32
GetStdDateIdx() const { return nStdDateIdx
; }
271 sal_uInt32
GetStdTimeIdx() const { return nStdTimeIdx
; }
272 sal_uInt32
GetStdDateTimeIdx() const { return nStdDateTimeIdx
; }
274 // offer NumberFormatter also static
275 static std::shared_ptr
<SvNumberFormatter
> PrepareNumberFormatter( sal_uInt32
&rnStdDateIdx
,
276 sal_uInt32
&rnStdTimeIdx
, sal_uInt32
&rnStdDateTimeIdx
,
277 LanguageType
const * peFormatterLangType
=nullptr, DateOrder
const * peFormatterDateOrder
=nullptr );
280 // There's one instance of this class for every executed sub-program.
281 // This instance is the heart of the BASIC-machine and contains only local data.
285 friend void SbRtl_CallByName( StarBASIC
* pBasic
, SbxArray
& rPar
, bool bWrite
);
287 typedef void( SbiRuntime::*pStep0
)();
288 typedef void( SbiRuntime::*pStep1
)( sal_uInt32 nOp1
);
289 typedef void( SbiRuntime::*pStep2
)( sal_uInt32 nOp1
, sal_uInt32 nOp2
);
290 static const pStep0 aStep0
[]; // opcode-table group 0
291 static const pStep1 aStep1
[];
292 static const pStep2 aStep2
[];
294 StarBASIC
& rBasic
; // StarBASIC instance
295 SbiInstance
* pInst
; // current thread
296 SbModule
* pMod
; // current module
297 SbMethod
* pMeth
; // method instance
298 SbiIoSystem
* pIosys
; // I/O-System
299 const SbiImage
* pImg
; // Code-Image
300 SbxArrayRef refExprStk
; // expression stack
301 SbxArrayRef refCaseStk
; // CASE expression stack
302 SbxArrayRef refRedimpArray
; // Array saved to use for REDIM PRESERVE
303 SbxVariableRef refRedim
; // Array saved to use for REDIM
304 SbxVariableRef xDummyVar
; // substitute for variables that weren't found
305 SbxVariable
* mpExtCaller
= nullptr; // Caller ( external - e.g. button name, shape, range object etc. - only in vba mode )
306 SbiForStack
* pForStk
= nullptr; // FOR/NEXT-Stack
307 sal_uInt16 nExprLvl
= 0; // depth of the expr-stack
308 sal_uInt16 nForLvl
= 0; // #118235: Maintain for level
309 const sal_uInt8
* pCode
; // current Code-Pointer
310 const sal_uInt8
* pStmnt
; // beginning of the last statement
311 const sal_uInt8
* pError
= nullptr; // address of the current error handler
312 const sal_uInt8
* pRestart
= nullptr; // restart-address
313 const sal_uInt8
* pErrCode
= nullptr; // restart-address RESUME NEXT
314 const sal_uInt8
* pErrStmnt
= nullptr; // restart-address RESUME 0
315 OUString aLibName
; // Lib-name for declare-call
316 SbxArrayRef refParams
; // current procedure parameters
317 SbxArrayRef refLocals
; // local variable
319 // #74254, one refSaveObj is not enough! new: pRefSaveList (see above)
322 bool bError
= true; // true: handle errors
323 bool bInError
= false; // true: in an error handler
324 bool bBlocked
= false; // true: blocked by next call level, #i48868
326 BasicDebugFlags nFlags
; // Debugging-Flags
327 ErrCode nError
= ERRCODE_NONE
;
328 sal_uInt16 nOps
= 0; // opcode counter
330 std::vector
<SbxVariableRef
> aRefSaved
; // #74254 save temporary references
331 std::vector
<SbiGosub
> pGosubStk
; // GOSUB stack
332 std::vector
<SbiArgv
> pArgvStk
; // ARGV-Stack
335 SbxVariable
* FindElement
336 ( SbxObject
* pObj
, sal_uInt32 nOp1
, sal_uInt32 nOp2
, ErrCode
, bool bLocal
, bool bStatic
= false );
337 void SetupArgs( SbxVariable
*, sal_uInt32
);
338 SbxVariable
* CheckArray( SbxVariable
* );
340 void PushVar( SbxVariable
* );
341 SbxVariableRef
PopVar();
342 SbxVariable
* GetTOS();
344 void ClearExprStack();
346 void PushGosub( const sal_uInt8
* );
351 void ClearArgvStack();
356 void ClearForStack();
358 void StepArith( SbxOperator
);
359 void StepUnary( SbxOperator
);
360 void StepCompare( SbxOperator
);
362 void SetParameters( SbxArray
* );
364 // HAS TO BE IMPLEMENTED SOME TIME
365 void DllCall( std::u16string_view
, std::u16string_view
, SbxArray
*, SbxDataType
, bool );
367 // #56204 swap out DIM-functionality into help method (step0.cxx)
368 void DimImpl(const SbxVariableRef
& refVar
);
369 bool EvaluateTopOfStackAsBool();
371 static bool implIsClass( SbxObject
const * pObj
, const OUString
& aClass
);
373 void StepSETCLASS_impl( sal_uInt32 nOp1
, bool bHandleDflt
);
375 // the following routines are called by the single
376 // stepper and implement the single opcodes
377 void StepNOP(), StepEXP(), StepMUL(), StepDIV();
378 void StepMOD(), StepPLUS(), StepMINUS(), StepNEG();
379 void StepEQ(), StepNE(), StepLT(), StepGT();
380 void StepLE(), StepGE(), StepIDIV(), StepAND();
381 void StepOR(), StepXOR(), StepEQV(), StepIMP();
382 void StepNOT(), StepCAT(), StepLIKE(), StepIS();
384 void StepARGV(), StepINPUT(), StepLINPUT(), StepSTOP();
385 void StepGET(), StepSET(), StepVBASET(), StepPUT(), StepPUTC();
386 void StepSET_Impl( SbxVariableRef
& refVal
, SbxVariableRef
& refVar
, bool bDefaultHandling
= false );
387 void StepDIM(), StepREDIM(), StepREDIMP(), StepERASE();
388 void StepINITFOR(), StepNEXT(), StepERROR(), StepINITFOREACH();
389 void StepCASE(), StepENDCASE(), StepSTDERROR();
390 void StepNOERROR(), StepCHANNEL(), StepCHANNEL0(), StepPRINT();
391 void StepPRINTF(), StepWRITE(), StepRENAME(), StepPROMPT();
392 void StepRESTART(), StepEMPTY(), StepLEAVE();
393 void StepLSET(), StepRSET(), StepREDIMP_ERASE(), StepERASE_CLEAR();
394 void StepARRAYACCESS(), StepBYVAL();
395 // all opcodes with one operand
396 void StepLOADNC( sal_uInt32
), StepLOADSC( sal_uInt32
), StepLOADI( sal_uInt32
);
397 void StepARGN( sal_uInt32
), StepBASED( sal_uInt32
), StepPAD( sal_uInt32
);
398 void StepJUMP( sal_uInt32
), StepJUMPT( sal_uInt32
);
399 void StepJUMPF( sal_uInt32
), StepONJUMP( sal_uInt32
);
400 void StepGOSUB( sal_uInt32
), StepRETURN( sal_uInt32
);
401 void StepTESTFOR( sal_uInt32
), StepCASETO( sal_uInt32
), StepERRHDL( sal_uInt32
);
402 void StepRESUME( sal_uInt32
), StepSETCLASS( sal_uInt32
), StepVBASETCLASS( sal_uInt32
), StepTESTCLASS( sal_uInt32
), StepLIB( sal_uInt32
);
403 bool checkClass_Impl( const SbxVariableRef
& refVal
, const OUString
& aClass
, bool bRaiseErrors
, bool bDefault
);
404 void StepCLOSE( sal_uInt32
), StepPRCHAR( sal_uInt32
), StepARGTYP( sal_uInt32
);
405 // all opcodes with two operands
406 void StepRTL( sal_uInt32
, sal_uInt32
), StepPUBLIC( sal_uInt32
, sal_uInt32
), StepPUBLIC_P( sal_uInt32
, sal_uInt32
);
407 void StepPUBLIC_Impl( sal_uInt32
, sal_uInt32
, bool bUsedForClassModule
);
408 void StepFIND_Impl( SbxObject
* pObj
, sal_uInt32 nOp1
, sal_uInt32 nOp2
, ErrCode
, bool bStatic
= false );
409 void StepFIND( sal_uInt32
, sal_uInt32
), StepELEM( sal_uInt32
, sal_uInt32
);
410 void StepGLOBAL( sal_uInt32
, sal_uInt32
), StepLOCAL( sal_uInt32
, sal_uInt32
);
411 void StepPARAM( sal_uInt32
, sal_uInt32
), StepCREATE( sal_uInt32
, sal_uInt32
);
412 void StepCALL( sal_uInt32
, sal_uInt32
), StepCALLC( sal_uInt32
, sal_uInt32
);
413 void StepCASEIS( sal_uInt32
, sal_uInt32
), StepSTMNT( sal_uInt32
, sal_uInt32
);
414 SbxVariable
* StepSTATIC_Impl(
415 OUString
const & aName
, SbxDataType t
, sal_uInt32 nOp2
);
416 void StepOPEN( sal_uInt32
, sal_uInt32
), StepSTATIC( sal_uInt32
, sal_uInt32
);
417 void StepTCREATE(sal_uInt32
,sal_uInt32
), StepDCREATE(sal_uInt32
,sal_uInt32
);
418 void StepGLOBAL_P( sal_uInt32
, sal_uInt32
),StepFIND_G( sal_uInt32
, sal_uInt32
);
419 void StepDCREATE_REDIMP(sal_uInt32
,sal_uInt32
), StepDCREATE_IMPL(sal_uInt32
,sal_uInt32
);
420 void StepFIND_CM( sal_uInt32
, sal_uInt32
);
421 void StepFIND_STATIC( sal_uInt32
, sal_uInt32
);
422 static void implHandleSbxFlags( SbxVariable
* pVar
, SbxDataType t
, sal_uInt32 nOp2
);
424 void SetVBAEnabled( bool bEnabled
);
425 bool IsImageFlag( SbiImageFlags n
) const;
426 sal_uInt16
GetBase() const;
427 sal_Int32 nLine
= 0, nCol1
= 0, nCol2
= 0;
428 SbiRuntime
* pNext
= nullptr; // Stack-Chain
430 // tdf#79426, tdf#125180 - adds the information about a missing parameter
431 static void SetIsMissing( SbxVariable
* );
432 // tdf#79426, tdf#125180 - checks if a variable contains the information about a missing parameter
433 static bool IsMissing( SbxVariable
*, sal_uInt16
);
435 SbiRuntime( SbModule
*, SbMethod
*, sal_uInt32
);
437 void Error( ErrCode
, bool bVBATranslationAlreadyDone
= false ); // set error if != 0
438 void Error( ErrCode
, const OUString
& ); // set error if != 0
439 void FatalError( ErrCode
); // error handling = standard, set error
440 void FatalError( ErrCode
, const OUString
& ); // error handling = standard, set error
441 static sal_Int32
translateErrorToVba( ErrCode nError
, OUString
& rMsg
);
442 bool Step(); // single step (one opcode)
443 void Stop() { bRun
= false; }
444 void block() { bBlocked
= true; }
445 void unblock() { bBlocked
= false; }
446 SbModule
* GetModule() { return pMod
; }
447 BasicDebugFlags
GetDebugFlags() const { return nFlags
; }
448 void SetDebugFlags( BasicDebugFlags nFl
) { nFlags
= nFl
; }
449 SbMethod
* GetCaller() { return pMeth
;}
450 SbxVariable
* GetExternalCaller(){ return mpExtCaller
; }
452 SbiForStack
* FindForStackItemForCollection( class BasicCollection
const * pCollection
);
454 SbxBase
* FindElementExtern( const OUString
& rName
);
455 static bool isVBAEnabled();
459 inline void checkArithmeticOverflow( double d
)
461 if( !std::isfinite( d
) )
462 StarBASIC::Error( ERRCODE_BASIC_MATH_OVERFLOW
);
465 inline void checkArithmeticOverflow( SbxVariable
const * pVar
)
467 if( pVar
->GetType() == SbxDOUBLE
)
469 double d
= pVar
->GetDouble();
470 checkArithmeticOverflow( d
);
475 StarBASIC
* GetCurrentBasic( StarBASIC
* pRTBasic
);
477 // Returns true if UNO is available, otherwise the old
478 // file system implementation has to be used
479 // (Implemented in iosys.cxx)
482 // Converts possibly relative paths to absolute paths
483 // according to the setting done by ChDir/ChDrive
484 // (Implemented in methods.cxx)
485 OUString
getFullPath( const OUString
& aRelPath
);
487 // Implementation of StepRENAME with UCB
488 // (Implemented in methods.cxx, so step0.cxx
489 // has not to be infected with UNO)
490 void implStepRenameUCB( const OUString
& aSource
, const OUString
& aDest
);
492 void implStepRenameOSL( const OUString
& aSource
, const OUString
& aDest
);
493 bool IsBaseIndexOne();
495 void removeDimAsNewRecoverItem( SbxVariable
* pVar
);
497 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */