cid#1640468 Dereference after null check
[LibreOffice.git] / basic / source / sbx / sbxexec.cxx
blob9d9b339ef7159d09fef097fbcc1f6ba4cfb2de9d
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 .
20 #include <sal/config.h>
22 #include <basic/sbx.hxx>
23 #include <basic/sberrors.hxx>
24 #include <rtl/character.hxx>
25 #include <rtl/ustrbuf.hxx>
27 #include <basiccharclass.hxx>
29 static SbxVariableRef Element
30 ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf,
31 SbxClassType, bool bCompatible );
33 static const sal_Unicode* SkipWhitespace( const sal_Unicode* p )
35 while( BasicCharClass::isWhitespace(*p) )
36 p++;
37 return p;
40 // Scanning of a symbol. The symbol were inserted in rSym, the return value
41 // is the new scan position. The symbol is at errors empty.
43 static const sal_Unicode* Symbol( const sal_Unicode* p, OUString& rSym, bool bCompatible )
45 sal_uInt16 nLen = 0;
46 // Did we have a nonstandard symbol?
47 if( *p == '[' )
49 rSym = ++p;
50 while( *p && *p != ']' )
52 p++;
53 nLen++;
55 p++;
57 else
59 // A symbol had to begin with an alphabetic character or an underline
60 if( !BasicCharClass::isAlpha( *p, bCompatible ) && *p != '_' )
62 SbxBase::SetError( ERRCODE_BASIC_SYNTAX );
64 else
66 rSym = p;
67 // The it can contain alphabetic characters, numbers or underlines
68 while( *p && (BasicCharClass::isAlphaNumeric( *p, bCompatible ) || *p == '_') )
70 p++;
71 nLen++;
73 // Ignore standard BASIC suffixes
74 if( *p && (*p == '%' || *p == '&' || *p == '!' || *p == '#' || *p == '$' ) )
76 p++;
80 rSym = rSym.copy( 0, nLen );
81 return p;
84 // Qualified name. Element.Element...
86 static SbxVariableRef QualifiedName
87 ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf, SbxClassType t, bool bCompatible )
90 SbxVariableRef refVar;
91 const sal_Unicode* p = SkipWhitespace( *ppBuf );
92 if( BasicCharClass::isAlpha( *p, bCompatible ) || *p == '_' || *p == '[' )
94 // Read in the element
95 refVar = Element( pObj, pGbl, &p, t, bCompatible );
96 while( refVar.is() && (*p == '.' || *p == '!') )
98 // It follows still an objectelement. The current element
99 // had to be a SBX-Object or had to deliver such an object!
100 pObj = dynamic_cast<SbxObject*>( refVar.get() );
101 if( !pObj )
102 // Then it had to deliver an object
103 pObj = dynamic_cast<SbxObject*>( refVar->GetObject() );
104 refVar.clear();
105 if( !pObj )
106 break;
107 p++;
108 // And the next element please
109 refVar = Element( pObj, pGbl, &p, t, bCompatible );
112 else
113 SbxBase::SetError( ERRCODE_BASIC_SYNTAX );
114 *ppBuf = p;
115 return refVar;
118 // Read in of an operand. This could be a number, a string or
119 // a function (with optional parameters).
121 static SbxVariableRef Operand
122 ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf, bool bVar, bool bCompatible )
124 SbxVariableRef refVar( new SbxVariable );
125 const sal_Unicode* p = SkipWhitespace( *ppBuf );
126 if( !bVar && ( rtl::isAsciiDigit( *p )
127 || ( *p == '.' && rtl::isAsciiDigit( *( p+1 ) ) )
128 || *p == '-'
129 || *p == '&' ) )
131 // A number could be scanned in directly!
132 sal_Int32 nLen;
133 if (!refVar->Scan(p, &nLen))
135 refVar.clear();
137 else
139 p += nLen;
142 else if( !bVar && *p == '"' )
144 // A string
145 OUStringBuffer aString;
146 p++;
147 for( ;; )
149 // This is perhaps an error
150 if( !*p )
152 return nullptr;
154 // Double quotes are OK
155 if( *p == '"' && (*++p) != '"' )
157 break;
159 aString.append(*p++);
161 refVar->PutString( aString.makeStringAndClear() );
163 else
165 refVar = QualifiedName( pObj, pGbl, &p, SbxClassType::DontCare, bCompatible );
167 *ppBuf = p;
168 return refVar;
171 // Read in of a simple term. The operands +, -, * and /
172 // are supported.
174 static SbxVariableRef MulDiv( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf, bool bCompatible )
176 const sal_Unicode* p = *ppBuf;
177 SbxVariableRef refVar( Operand( pObj, pGbl, &p, false, bCompatible ) );
178 p = SkipWhitespace( p );
179 while( refVar.is() && ( *p == '*' || *p == '/' ) )
181 sal_Unicode cOp = *p++;
182 SbxVariableRef refVar2( Operand( pObj, pGbl, &p, false, bCompatible ) );
183 if( refVar2.is() )
185 // temporary variable!
186 SbxVariable* pVar = refVar.get();
187 pVar = new SbxVariable( *pVar );
188 refVar = pVar;
189 if( cOp == '*' )
190 *refVar *= *refVar2;
191 else
192 *refVar /= *refVar2;
194 else
196 refVar.clear();
197 break;
200 *ppBuf = p;
201 return refVar;
204 static SbxVariableRef PlusMinus( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf, bool bCompatible )
206 const sal_Unicode* p = *ppBuf;
207 SbxVariableRef refVar( MulDiv( pObj, pGbl, &p, bCompatible ) );
208 p = SkipWhitespace( p );
209 while( refVar.is() && ( *p == '+' || *p == '-' ) )
211 sal_Unicode cOp = *p++;
212 SbxVariableRef refVar2( MulDiv( pObj, pGbl, &p, bCompatible ) );
213 if( refVar2.is() )
215 // temporary Variable!
216 SbxVariable* pVar = refVar.get();
217 pVar = new SbxVariable( *pVar );
218 refVar = pVar;
219 if( cOp == '+' )
220 *refVar += *refVar2;
221 else
222 *refVar -= *refVar2;
224 else
226 refVar.clear();
227 break;
230 *ppBuf = p;
231 return refVar;
234 static SbxVariableRef Assign( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf, bool bCompatible )
236 const sal_Unicode* p = *ppBuf;
237 SbxVariableRef refVar( Operand( pObj, pGbl, &p, true, bCompatible ) );
238 p = SkipWhitespace( p );
239 if( refVar.is() )
241 if( *p == '=' )
243 // Assign only onto properties!
244 if( refVar->GetClass() != SbxClassType::Property )
246 SbxBase::SetError( ERRCODE_BASIC_BAD_ACTION );
247 refVar.clear();
249 else
251 p++;
252 SbxVariableRef refVar2( PlusMinus( pObj, pGbl, &p, bCompatible ) );
253 if( refVar2.is() )
255 SbxVariable* pVar = refVar.get();
256 SbxVariable* pVar2 = refVar2.get();
257 *pVar = *pVar2;
258 pVar->SetParameters( nullptr );
262 else
263 // Simple call: once activating
264 refVar->Broadcast( SfxHintId::BasicDataWanted );
266 *ppBuf = p;
267 return refVar;
270 // Read in of an element. This is a symbol, optional followed
271 // by a parameter list. The symbol will be searched in the
272 // specified object and the parameter list will be attached if necessary.
274 static SbxVariableRef Element
275 ( SbxObject* pObj, SbxObject* pGbl, const sal_Unicode** ppBuf,
276 SbxClassType t, bool bCompatible )
278 OUString aSym;
279 const sal_Unicode* p = Symbol( *ppBuf, aSym, bCompatible );
280 SbxVariableRef refVar;
281 if( !aSym.isEmpty() )
283 SbxFlagBits nOld = pObj->GetFlags();
284 if( pObj == pGbl )
286 pObj->SetFlag( SbxFlagBits::GlobalSearch );
288 refVar = pObj->Find( aSym, t );
289 pObj->SetFlags( nOld );
290 if( refVar.is() )
292 refVar->SetParameters( nullptr );
293 // Follow still parameter?
294 p = SkipWhitespace( p );
295 if( *p == '(' )
297 p++;
298 auto refPar = tools::make_ref<SbxArray>();
299 sal_uInt32 nArg = 0;
300 // We are once relaxed and accept as well
301 // the line- or command end as delimiter
302 // Search parameter always global!
303 while( *p && *p != ')' && *p != ']' )
305 SbxVariableRef refArg = PlusMinus( pGbl, pGbl, &p, bCompatible );
306 if( !refArg.is() )
308 // Error during the parsing
309 refVar.clear(); break;
311 else
313 // One copies the parameter, so that
314 // one have the current status (triggers also
315 // the call per access)
316 refPar->Put(new SbxVariable(*refArg), ++nArg);
318 p = SkipWhitespace( p );
319 if( *p == ',' )
320 p++;
322 if( *p == ')' )
323 p++;
324 if( refVar.is() )
325 refVar->SetParameters( refPar.get() );
328 else
329 SbxBase::SetError( ERRCODE_BASIC_NO_METHOD, aSym );
331 *ppBuf = p;
332 return refVar;
335 // Mainroutine
337 SbxVariable* SbxObject::Execute( const OUString& rTxt )
339 SbxVariableRef pVar;
340 const sal_Unicode* p = rTxt.getStr();
341 for( ;; )
343 p = SkipWhitespace( p );
344 if( !*p )
346 break;
348 if( *p++ != '[' )
350 SetError( ERRCODE_BASIC_SYNTAX ); break;
352 pVar = Assign( this, this, &p, IsOptionCompatible() );
353 if( !pVar.is() )
355 break;
357 p = SkipWhitespace( p );
358 if( *p++ != ']' )
360 SetError( ERRCODE_BASIC_SYNTAX ); break;
363 return pVar.get();
366 SbxVariable* SbxObject::FindQualified( const OUString& rName, SbxClassType t )
368 SbxVariableRef pVar;
369 const sal_Unicode* p = rName.getStr();
370 p = SkipWhitespace( p );
371 if( !*p )
373 return nullptr;
375 pVar = QualifiedName( this, this, &p, t, IsOptionCompatible() );
376 p = SkipWhitespace( p );
377 if( *p )
379 SetError( ERRCODE_BASIC_SYNTAX );
381 return pVar.get();
384 bool SbxObject::IsOptionCompatible() const
386 if (const SbxObject* pObj = GetParent())
387 return pObj->IsOptionCompatible();
388 return false;
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */