|
1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // $Header$ |
|
15 // The CTEngine is the class that manages the test infrastructure |
|
16 // It knows how to process scripts and where to find command knowledge |
|
17 // The plan is: |
|
18 // Process console command line or command line supplied script |
|
19 // read in each line (or command) and process that (if appropriate) |
|
20 // Include Files |
|
21 // |
|
22 // |
|
23 |
|
24 #include <e32std.h> |
|
25 #include <s32file.h> |
|
26 #include <f32file.h> |
|
27 #include <e32hal.h> |
|
28 #include <fbs.h> |
|
29 |
|
30 //----------------------------------------------------------------------------- |
|
31 |
|
32 const TInt KMaxLogEntrySize = 512; |
|
33 const TInt KMaxUserEntrySize = 128; |
|
34 |
|
35 //----------------------------------------------------------------------------- |
|
36 |
|
37 // result define names |
|
38 _LIT(KLastResultCode, "$LastResult$"); |
|
39 _LIT(KLastErrorCode, "$LastError$"); |
|
40 |
|
41 //----------------------------------------------------------------------------- |
|
42 |
|
43 #include "TEngine.h" |
|
44 |
|
45 //----------------------------------------------------------------------------- |
|
46 // This is used by the CallCmdFile, CloseCmdFile and CloseCmdFiles member |
|
47 // functions to alter the command file context. This function defines the |
|
48 // call argument tags %0%, %1%, ... %k% to those of the present cmd file. |
|
49 // If there is no command file, then the argument tags (all) are removed. |
|
50 |
|
51 #if !defined(TEMPLATED_CATALOGUE) |
|
52 |
|
53 LOCAL_C void SetContextL(CCatalogue *aCatalog, CCmdFile* aCmdFile ) |
|
54 |
|
55 #else |
|
56 |
|
57 template <class T> |
|
58 LOCAL_C void SetContextL(CCatalogue<T> *aCatalog, CCmdFile* aCmdFile ) |
|
59 |
|
60 #endif |
|
61 |
|
62 { |
|
63 if ( aCmdFile == NULL ) |
|
64 { |
|
65 // No command file = console context: parameter markers are not |
|
66 // recognised at all i.e. remove them all. |
|
67 TBuf<6> buf; |
|
68 TInt i; |
|
69 for ( i = 0; i <= TFR_MAX_CALL_ARGUMENTS; ++i ) |
|
70 { |
|
71 buf.Format( _L("%%%d%%"), i ); |
|
72 aCatalog->Delete(buf); |
|
73 } |
|
74 } |
|
75 else |
|
76 { |
|
77 // Command file context: parameters markers will be mapped to the |
|
78 // file name, to the call arguments and to "". |
|
79 TBuf<6> buf; |
|
80 TPtrC arg; |
|
81 TInt i; |
|
82 TInt iArgs = aCmdFile->Argc(); |
|
83 |
|
84 for ( i = 0; ( (i < TFR_MAX_CALL_ARGUMENTS) && (i < iArgs) ); ++i ) |
|
85 { |
|
86 buf.Format(_L("%%%d%%"), i); |
|
87 arg.Set(aCmdFile->Argv(i)); |
|
88 aCatalog->Delete(buf); |
|
89 aCatalog->AddL(buf,arg); |
|
90 } |
|
91 |
|
92 for ( ; i < 10; ++i ) |
|
93 { |
|
94 buf.Format(_L("%%%d%%"), i); |
|
95 aCatalog->Delete(buf); |
|
96 aCatalog->AddL(buf, _L("")); |
|
97 } |
|
98 } |
|
99 |
|
100 } |
|
101 |
|
102 //----------------------------------------------------------------------------- |
|
103 // Overflow handler. |
|
104 |
|
105 void TTestMachineOverflow::Overflow( TDes16& aDes) |
|
106 { |
|
107 _LIT(KErrOverflowMsg, "..."); |
|
108 if( aDes.MaxLength() >= KErrOverflowMsg().Length() + aDes.Length() ) |
|
109 aDes.Append(KErrOverflowMsg); |
|
110 } |
|
111 |
|
112 //----------------------------------------------------------------------------- |
|
113 // NewL for Console interface |
|
114 |
|
115 CTEngine* CTEngine::NewL( CConsoleBase* aConsole ) |
|
116 { |
|
117 CTEngine* self = NewLC(aConsole); |
|
118 CleanupStack::Pop(self); |
|
119 return self; |
|
120 } |
|
121 |
|
122 //----------------------------------------------------------------------------- |
|
123 // NewLC for Console Interface |
|
124 |
|
125 CTEngine* CTEngine::NewLC( CConsoleBase* aConsole ) |
|
126 { |
|
127 CTEngine* self = new (ELeave) CTEngine(aConsole); |
|
128 CleanupStack::PushL(self); |
|
129 self->ConstructL(); |
|
130 return self; |
|
131 } |
|
132 |
|
133 //----------------------------------------------------------------------------- |
|
134 // Destructor |
|
135 |
|
136 CTEngine::~CTEngine() |
|
137 { |
|
138 Cancel(); |
|
139 |
|
140 iTimer.Close(); |
|
141 iFileServer.Close(); |
|
142 delete iCmdFile; |
|
143 delete iLogFile; |
|
144 |
|
145 delete iConsoleReader; |
|
146 delete iDomain; |
|
147 delete iDefines; |
|
148 delete iIFPile; |
|
149 delete iLogPile; |
|
150 delete iCmdPile; |
|
151 } |
|
152 |
|
153 //----------------------------------------------------------------------------- |
|
154 |
|
155 void CTEngine::ConstructL() |
|
156 { |
|
157 |
|
158 iConsoleEnabled = ( iConsole != NULL ); |
|
159 |
|
160 iCmdPile = CPile<CCmdFile>::NewL(); |
|
161 iLogPile = CPile<CLogFile>::NewL(); |
|
162 iIFPile = CPile<CIFControl>::NewL(); |
|
163 |
|
164 |
|
165 #if !defined(TEMPLATED_CATALOGUE) |
|
166 iDefines = CCatalogue::NewL(); |
|
167 #else |
|
168 iDefines = CCatalogue<CLabeledText>::NewL(); |
|
169 #endif |
|
170 |
|
171 |
|
172 // domain specific catalogue of items. This provides |
|
173 // a mechanism by which we can disassociate the framework |
|
174 // from the domain specific commands/data requirements |
|
175 iDomain = CObjCatalogue::NewL(); |
|
176 |
|
177 iConsoleReader = CConsoleReader::NewL(*iConsole); |
|
178 TInt r = iTimer.CreateLocal(); |
|
179 |
|
180 if (r!=KErrNone) |
|
181 User::Leave(r); |
|
182 |
|
183 // Provide access to the fileserver |
|
184 User::LeaveIfError(iFileServer.Connect()); |
|
185 |
|
186 // This is normally true (to enable replication of printed |
|
187 // output to log (if enabled) |
|
188 iLogActivity = ETrue; |
|
189 |
|
190 // show the unprocessed command lines on screen and log |
|
191 SetShowCmdLine(ETrue); |
|
192 } |
|
193 |
|
194 //----------------------------------------------------------------------------- |
|
195 |
|
196 CTEngine::CTEngine(CConsoleBase* aConsole) |
|
197 : |
|
198 CActive(EPriorityStandard), |
|
199 iStopped(ETrue), |
|
200 iEchoMode(ETrue), |
|
201 iConsole(aConsole) |
|
202 { |
|
203 iConsoleEnabled = ( iConsole != NULL ); |
|
204 |
|
205 iCmdPath.Zero(); |
|
206 iLogPath.Zero(); |
|
207 iCmdline.Zero(); |
|
208 iCommand.Zero(); |
|
209 iPrompt.Copy(THA_CommandPrompt); |
|
210 |
|
211 CActiveScheduler::Add(this); |
|
212 } |
|
213 |
|
214 //----------------------------------------------------------------------------- |
|
215 |
|
216 void CTEngine::SetCmdFamily(CCmdFamily* aCmdFamily) |
|
217 { |
|
218 iCmdFamily = aCmdFamily; |
|
219 if (aCmdFamily != NULL) |
|
220 aCmdFamily->SetMachine( this ); |
|
221 } |
|
222 |
|
223 //----------------------------------------------------------------------------- |
|
224 |
|
225 TBool CTEngine::ConsoleEnabled( ) |
|
226 { |
|
227 return ( iConsoleEnabled ); |
|
228 } |
|
229 |
|
230 //----------------------------------------------------------------------------- |
|
231 |
|
232 void CTEngine::DisableConsole() |
|
233 { |
|
234 iConsoleEnabled = EFalse; |
|
235 } |
|
236 |
|
237 //----------------------------------------------------------------------------- |
|
238 |
|
239 void CTEngine::EnableConsole() |
|
240 { |
|
241 iConsoleEnabled = (iConsole != NULL); |
|
242 } |
|
243 |
|
244 //----------------------------------------------------------------------------- |
|
245 |
|
246 CConsoleBase* CTEngine::Console() const |
|
247 { |
|
248 return (iConsole); |
|
249 } |
|
250 |
|
251 //----------------------------------------------------------------------------- |
|
252 |
|
253 void CTEngine::Write(const TDesC& aText) |
|
254 { |
|
255 if (ConsoleEnabled()) |
|
256 iConsole->Write(aText); |
|
257 } |
|
258 |
|
259 //----------------------------------------------------------------------------- |
|
260 |
|
261 void CTEngine::Writeln( const TDesC& aText ) |
|
262 { |
|
263 if (ConsoleEnabled()) |
|
264 { |
|
265 iConsole->Write(aText); |
|
266 iConsole->Write(TFR_KTxtNewLine); |
|
267 } |
|
268 } |
|
269 |
|
270 //----------------------------------------------------------------------------- |
|
271 |
|
272 void CTEngine::Writeln() |
|
273 { |
|
274 if (ConsoleEnabled()) |
|
275 iConsole->Write(TFR_KTxtNewLine); |
|
276 } |
|
277 |
|
278 //----------------------------------------------------------------------------- |
|
279 |
|
280 void CTEngine::Printf( TRefByValue<const TDesC> aFmt, ... ) |
|
281 { |
|
282 VA_LIST list; |
|
283 VA_START( list, aFmt ); |
|
284 Printf( aFmt, list ); |
|
285 } |
|
286 |
|
287 //----------------------------------------------------------------------------- |
|
288 |
|
289 void CTEngine::Printf( TRefByValue<const TDesC> aFmt, VA_LIST& aList ) |
|
290 { |
|
291 if ( ConsoleEnabled() ) |
|
292 { |
|
293 iPrintBuf.Zero(); |
|
294 TRAPD(error,iPrintBuf.AppendFormatList( aFmt, aList, &iOverflow)); |
|
295 if ( error == KErrNone ) |
|
296 { |
|
297 iConsole->Write( iPrintBuf ); |
|
298 } |
|
299 else |
|
300 { |
|
301 iConsole->Write( TFR_KTxtErrPrintOflo ); |
|
302 iConsole->Write( TFR_KTxtNewLine ); |
|
303 } |
|
304 } |
|
305 } |
|
306 |
|
307 //----------------------------------------------------------------------------- |
|
308 |
|
309 void CTEngine::Printfln( TRefByValue<const TDesC> aFmt, ... ) |
|
310 { |
|
311 VA_LIST list; |
|
312 VA_START( list, aFmt ); |
|
313 Printfln( aFmt, list ); |
|
314 } |
|
315 |
|
316 //----------------------------------------------------------------------------- |
|
317 |
|
318 void CTEngine::Printfln( TRefByValue<const TDesC> aFmt, VA_LIST& aList ) |
|
319 { |
|
320 if ( ConsoleEnabled( ) ) |
|
321 { |
|
322 iPrintBuf.Zero(); |
|
323 TInt error = KErrNone; |
|
324 TRAP(error,iPrintBuf.AppendFormatList( aFmt, aList, &iOverflow)); |
|
325 if ( error != KErrNone ) |
|
326 return; |
|
327 TRAP(error,iPrintBuf.AppendFormat( _L("%S"), &iOverflow, &TFR_KTxtNewLine )); |
|
328 if (error == KErrNone ) |
|
329 { |
|
330 iConsole->Write( iPrintBuf ); |
|
331 } |
|
332 else |
|
333 { |
|
334 iConsole->Write( TFR_KTxtErrPrintOflo ); |
|
335 iConsole->Write( TFR_KTxtNewLine ); |
|
336 } |
|
337 } |
|
338 } |
|
339 |
|
340 //----------------------------------------------------------------------------- |
|
341 |
|
342 void CTEngine::Pause(const TDesC& aPrompt) |
|
343 { |
|
344 if (ConsoleEnabled()) |
|
345 { |
|
346 iCmdline.Zero(); |
|
347 iConsole->Write(aPrompt); |
|
348 if (!IsActive()) |
|
349 SetActive(); |
|
350 iConsoleReader->ReadChar(iCmdline, iStatus); |
|
351 } |
|
352 else |
|
353 CompleteRequest(); |
|
354 } |
|
355 |
|
356 //----------------------------------------------------------------------------- |
|
357 |
|
358 void CTEngine::Hold(TTimeIntervalMicroSeconds32 aInterval) |
|
359 { |
|
360 iTimer.After(iStatus, aInterval); |
|
361 SetActive(); |
|
362 } |
|
363 |
|
364 //----------------------------------------------------------------------------- |
|
365 |
|
366 TInt CTEngine::SetCmdPath( const TDesC& aPath ) |
|
367 { |
|
368 if (aPath.Length() == 0) |
|
369 { |
|
370 Printf(_L("Path is set to: %S\r\n"), &iCmdPath); |
|
371 return KErrNone; |
|
372 } |
|
373 |
|
374 if (iParse.SetNoWild( aPath, NULL, NULL ) != KErrNone) |
|
375 return KErrArgument; |
|
376 |
|
377 if (!iParse.PathPresent() || iParse.NameOrExtPresent()) |
|
378 return KErrArgument; |
|
379 |
|
380 // set it |
|
381 iCmdPath.Copy(aPath); |
|
382 |
|
383 // echo it |
|
384 Printf(_L("Path is set to: %S\r\n"), &iCmdPath); |
|
385 |
|
386 return KErrNone; |
|
387 } |
|
388 |
|
389 //----------------------------------------------------------------------------- |
|
390 |
|
391 TPtrC CTEngine::CmdPath() const |
|
392 { |
|
393 TPtrC path(iCmdPath); |
|
394 return path; |
|
395 } |
|
396 |
|
397 //----------------------------------------------------------------------------- |
|
398 |
|
399 TPtrC CTEngine::FileName() const |
|
400 { |
|
401 TPtrC fileName(iFileName); |
|
402 return fileName; |
|
403 } |
|
404 |
|
405 //----------------------------------------------------------------------------- |
|
406 // parameterless version |
|
407 |
|
408 TInt CTEngine::CallCmdFile(const TDesC& aFile) |
|
409 { |
|
410 return CallCmdFile(aFile, _L("")); |
|
411 } |
|
412 |
|
413 //----------------------------------------------------------------------------- |
|
414 |
|
415 TInt CTEngine::CallCmdFile(const TDesC &aFile, const TDesC &aArgs) |
|
416 { |
|
417 // Make CCmdFile object. |
|
418 CCmdFile *file = NULL; |
|
419 TRAPD(error, file = CCmdFile::NewL()); |
|
420 if ( error != KErrNone ) |
|
421 return error; |
|
422 |
|
423 error = file->Open(iCmdPath, aFile); |
|
424 if ( error != KErrNone ) |
|
425 { |
|
426 delete file; |
|
427 |
|
428 // print file x not found echo it |
|
429 Printf(_L("Error: file [%S] not found\r\n"), &aFile); |
|
430 Printf(_L(" Check file path <%S>\r\n"), &iCmdPath); |
|
431 return error; |
|
432 } |
|
433 |
|
434 // Save file name and arguments. |
|
435 TRAP(error, file->AddArgsL(aFile)); |
|
436 if (error == KErrNone) |
|
437 TRAP(error, file->AddArgsL(aArgs)); |
|
438 |
|
439 if ( error != KErrNone ) |
|
440 { |
|
441 delete file; |
|
442 return error; |
|
443 } |
|
444 |
|
445 // Check that not too many call arguments. |
|
446 if ( file->Argc() > (TFR_MAX_CALL_ARGUMENTS + 1)) |
|
447 { |
|
448 delete file; |
|
449 return error = KErrArgument; |
|
450 } |
|
451 |
|
452 // Push present command file to cmdfile pile. |
|
453 TRAP(error, iCmdPile->PushL( iCmdFile )); |
|
454 if ( error != KErrNone ) |
|
455 { |
|
456 delete file; |
|
457 return error; |
|
458 } |
|
459 |
|
460 // Switch context to the called command file. |
|
461 iCmdFile = file; |
|
462 file = NULL; |
|
463 TRAP(error, SetContextL(iDefines, iCmdFile)); |
|
464 return error; |
|
465 } |
|
466 |
|
467 //----------------------------------------------------------------------------- |
|
468 |
|
469 CCmdFile* CTEngine::CmdFile() |
|
470 { |
|
471 return iCmdFile; |
|
472 } |
|
473 |
|
474 //----------------------------------------------------------------------------- |
|
475 |
|
476 TInt CTEngine::CloseCmdFile() |
|
477 { |
|
478 // Get rid of the present command file (NULL iCmdFile is Ok). |
|
479 delete iCmdFile; |
|
480 iCmdFile = NULL; |
|
481 |
|
482 // Pop the previous one from the pile (returns NULL if none). |
|
483 iCmdFile = iCmdPile->Pop(); |
|
484 |
|
485 // Switch context to that file (NULL iCmdFile does no harm). |
|
486 TRAPD(error,SetContextL(iDefines,iCmdFile)); |
|
487 return error; |
|
488 } |
|
489 |
|
490 //----------------------------------------------------------------------------- |
|
491 |
|
492 void CTEngine::CloseCmdFiles() |
|
493 { |
|
494 // Pop and delete all previous command files from the file pile. |
|
495 iCmdPile->Empty(); |
|
496 |
|
497 // Get rid of the present file as well (NULL iCmdFile is Ok). |
|
498 delete iCmdFile; |
|
499 iCmdFile = NULL; |
|
500 |
|
501 // Switch context to "no command file" (since iCmdFile = NULL). |
|
502 TRAP_IGNORE(SetContextL(iDefines,iCmdFile)); // doesn't LEAVE, see yourself |
|
503 } |
|
504 |
|
505 //----------------------------------------------------------------------------- |
|
506 |
|
507 TInt CTEngine::SetLogPath( const TDesC& aPath ) |
|
508 { |
|
509 if (aPath.Length() == 0) |
|
510 { |
|
511 iLogPath.Zero(); |
|
512 return KErrNone; |
|
513 } |
|
514 |
|
515 if (iParse.SetNoWild( aPath, NULL, NULL ) != KErrNone) |
|
516 return KErrArgument; |
|
517 |
|
518 if (!iParse.PathPresent() || iParse.NameOrExtPresent()) |
|
519 return KErrArgument; |
|
520 |
|
521 TInt error; |
|
522 RFs fs; |
|
523 if (error = fs.Connect(), error == KErrNone) |
|
524 { |
|
525 error = fs.MkDirAll( iParse.DriveAndPath()); |
|
526 if (error == KErrAlreadyExists) |
|
527 error = KErrNone; |
|
528 |
|
529 fs.Close(); |
|
530 if (error == KErrNone) |
|
531 iLogPath.Copy(aPath); |
|
532 } |
|
533 return error; |
|
534 } |
|
535 |
|
536 //----------------------------------------------------------------------------- |
|
537 |
|
538 TPtrC CTEngine::LogPath() const |
|
539 { |
|
540 TPtrC logpath(iLogPath); |
|
541 return logpath; |
|
542 } |
|
543 |
|
544 //----------------------------------------------------------------------------- |
|
545 |
|
546 TInt CTEngine::OpenLogFile(const TDesC& aFile) |
|
547 { |
|
548 // Construct a full log file. |
|
549 TInt error = iParse.Set(aFile, NULL, &iLogPath); |
|
550 if (error != KErrNone) |
|
551 return error; |
|
552 |
|
553 // Construct and open the new log file. |
|
554 CLogFile* file = NULL; |
|
555 TRAP(error, file = CLogFile::NewL()); |
|
556 if ( error != KErrNone ) |
|
557 return error; |
|
558 |
|
559 error = file->Open( iParse.FullName() ); |
|
560 if ( error == KErrNone && iLogFile != NULL ) |
|
561 { |
|
562 // Close and save the present log file. |
|
563 iLogFile->Close(); |
|
564 TRAP(error, iLogPile->PushL(iLogFile)); |
|
565 if ( error == KErrNone ) |
|
566 iLogFile = NULL; |
|
567 } |
|
568 |
|
569 if (error == KErrNone) |
|
570 { |
|
571 // Switch the log file. |
|
572 file->PrintHeading(); |
|
573 iLogFile = file; |
|
574 file = NULL; |
|
575 } |
|
576 |
|
577 delete file; // is NULL if no errors! |
|
578 return error; |
|
579 } |
|
580 |
|
581 //----------------------------------------------------------------------------- |
|
582 |
|
583 CLogFile *CTEngine::LogFile() |
|
584 { |
|
585 return iLogFile; |
|
586 } |
|
587 |
|
588 //----------------------------------------------------------------------------- |
|
589 |
|
590 TInt CTEngine::CloseLogFile( ) |
|
591 { |
|
592 delete iLogFile; |
|
593 iLogFile = NULL; |
|
594 iLogFile = iLogPile->Pop(); |
|
595 TInt error = KErrNone; |
|
596 if ( iLogFile != NULL ) |
|
597 error = iLogFile->Open(); |
|
598 return error; |
|
599 } |
|
600 |
|
601 //----------------------------------------------------------------------------- |
|
602 |
|
603 void CTEngine::CloseLogFiles() |
|
604 { |
|
605 TInt count = iLogPile->Count(); |
|
606 while (count > 0 ) |
|
607 { |
|
608 CLogFile* file = iLogPile->Pop(); |
|
609 delete file; // NULL does no harm! |
|
610 } |
|
611 |
|
612 delete iLogFile; |
|
613 iLogFile = NULL; |
|
614 } |
|
615 |
|
616 //----------------------------------------------------------------------------- |
|
617 |
|
618 void CTEngine::LogWrite( const TDesC& aText ) |
|
619 { |
|
620 if ( iLogFile != NULL ) |
|
621 iLogFile->Write( aText ); |
|
622 } |
|
623 |
|
624 //----------------------------------------------------------------------------- |
|
625 |
|
626 void CTEngine::LogWriteln( const TDesC& aText ) |
|
627 { |
|
628 if ( iLogFile != NULL ) |
|
629 iLogFile->Writeln( aText ); |
|
630 } |
|
631 |
|
632 //----------------------------------------------------------------------------- |
|
633 |
|
634 void CTEngine::LogWriteln() |
|
635 { |
|
636 if ( iLogFile != NULL ) |
|
637 iLogFile->Writeln(); |
|
638 } |
|
639 |
|
640 //----------------------------------------------------------------------------- |
|
641 |
|
642 void CTEngine::LogPrintf( TRefByValue<const TDesC> aFmt, ... ) |
|
643 { |
|
644 VA_LIST list; |
|
645 VA_START(list, aFmt); |
|
646 LogPrintf(aFmt, list); |
|
647 } |
|
648 |
|
649 //----------------------------------------------------------------------------- |
|
650 |
|
651 void CTEngine::LogPrintf( TRefByValue<const TDesC> aFmt, VA_LIST& aList ) |
|
652 { |
|
653 if (iLogFile != NULL) |
|
654 iLogFile->Printf(aFmt, aList); |
|
655 } |
|
656 |
|
657 //----------------------------------------------------------------------------- |
|
658 |
|
659 void CTEngine::LogPrintfln( TRefByValue<const TDesC> aFmt, ... ) |
|
660 { |
|
661 VA_LIST list; |
|
662 VA_START(list, aFmt); |
|
663 LogPrintfln(aFmt, list); |
|
664 } |
|
665 |
|
666 //----------------------------------------------------------------------------- |
|
667 |
|
668 void CTEngine::LogPrintfln( TRefByValue<const TDesC> aFmt, VA_LIST& aList ) |
|
669 { |
|
670 if (iLogFile != NULL) |
|
671 { |
|
672 iLogFile->Printf(aFmt, aList); |
|
673 iLogFile->Writeln(); |
|
674 } |
|
675 } |
|
676 |
|
677 //----------------------------------------------------------------------------- |
|
678 // Standard message output: |
|
679 // - CTEngine::MsgWrite |
|
680 // - CTEngine::MsgWriteln |
|
681 // - CTEngine::MsgWriteln |
|
682 // - CTEngine::MsgPrintf |
|
683 // - CTEngine::MsgPrintf |
|
684 // - CTEngine::MsgPrintfln |
|
685 // - CTEngine::MsgPrintfln |
|
686 // |
|
687 // A message goes to the log if there is one open. The message is printed |
|
688 // to the display as well, except when having echo mode off and running a |
|
689 // command file. |
|
690 // |
|
691 //----------------------------------------------------------------------------- |
|
692 |
|
693 void CTEngine::MsgWrite(const TDesC& aText) |
|
694 { |
|
695 LogWrite(aText); |
|
696 if (EchoMode() || (iCmdFile == NULL)) |
|
697 Write(aText); |
|
698 } |
|
699 |
|
700 //----------------------------------------------------------------------------- |
|
701 |
|
702 void CTEngine::MsgWriteln(const TDesC& aText) |
|
703 { |
|
704 LogWriteln(aText); |
|
705 if (EchoMode() || (iCmdFile == NULL)) |
|
706 Writeln(aText); |
|
707 } |
|
708 |
|
709 //----------------------------------------------------------------------------- |
|
710 |
|
711 void CTEngine::MsgWriteln() |
|
712 { |
|
713 LogWriteln(); |
|
714 if (EchoMode() || (iCmdFile == NULL)) |
|
715 Writeln(); |
|
716 } |
|
717 |
|
718 //----------------------------------------------------------------------------- |
|
719 |
|
720 void CTEngine::MsgPrintf( TRefByValue<const TDesC> aFmt, ... ) |
|
721 { |
|
722 VA_LIST list; |
|
723 VA_START( list, aFmt ); |
|
724 MsgPrintf( aFmt, list ); |
|
725 } |
|
726 |
|
727 //----------------------------------------------------------------------------- |
|
728 |
|
729 void CTEngine::MsgPrintf( TRefByValue<const TDesC> aFmt, VA_LIST& aList ) |
|
730 { |
|
731 if ( EchoMode() || (iCmdFile == NULL) ) |
|
732 { |
|
733 VA_LIST clone; |
|
734 Mem::Move( &clone, &aList, sizeof(VA_LIST) ); |
|
735 LogPrintf( aFmt, aList ); |
|
736 Printf( aFmt, clone ); |
|
737 } |
|
738 else |
|
739 { |
|
740 LogPrintf( aFmt, aList ); |
|
741 } |
|
742 } |
|
743 |
|
744 //----------------------------------------------------------------------------- |
|
745 |
|
746 void CTEngine::MsgPrintfln( TRefByValue<const TDesC> aFmt, ... ) |
|
747 { |
|
748 VA_LIST list; |
|
749 VA_START( list, aFmt ); |
|
750 MsgPrintfln( aFmt, list ); |
|
751 } |
|
752 |
|
753 //----------------------------------------------------------------------------- |
|
754 |
|
755 void CTEngine::MsgPrintfln( TRefByValue<const TDesC> aFmt, VA_LIST& aList ) |
|
756 { |
|
757 if ( EchoMode() || (iCmdFile == NULL) ) |
|
758 { |
|
759 VA_LIST clone; |
|
760 Mem::Move( &clone, &aList, sizeof(VA_LIST) ); |
|
761 LogPrintfln( aFmt, aList ); |
|
762 Printfln( aFmt, clone ); |
|
763 } |
|
764 else |
|
765 LogPrintfln(aFmt, aList); |
|
766 } |
|
767 |
|
768 //----------------------------------------------------------------------------- |
|
769 |
|
770 TInt CTEngine::SetPrompt(const TDesC& aPrompt) |
|
771 { |
|
772 // Return error in case a too long prompt was proposed. |
|
773 if (aPrompt.Length() > iPrompt.MaxLength()) |
|
774 return KErrTooBig; |
|
775 |
|
776 // Otherwise alter trhe prompt and return Ok. |
|
777 iPrompt.Copy(aPrompt); |
|
778 return KErrNone; |
|
779 } |
|
780 |
|
781 //----------------------------------------------------------------------------- |
|
782 |
|
783 TPtrC CTEngine::Prompt() const |
|
784 { |
|
785 TPtrC prompt(iPrompt); |
|
786 return prompt; |
|
787 } |
|
788 |
|
789 //----------------------------------------------------------------------------- |
|
790 // Run |
|
791 // Start things off! We have a variety of options: |
|
792 // 1. interactive. (no params) => Call Run() |
|
793 // 2. semi-interactive. (some params, no script) => Call Run(Args) |
|
794 // 3. automatic. (some params & script) => Run(Script, Args) |
|
795 |
|
796 void CTEngine::Run() |
|
797 { |
|
798 if (!IsActive()) |
|
799 { |
|
800 // Start the engine and set the state to get the next cmd |
|
801 iStopped = EFalse; |
|
802 iState = EGetNextCmd; |
|
803 |
|
804 // Complete request |
|
805 CompleteRequest(); |
|
806 } |
|
807 } |
|
808 |
|
809 //----------------------------------------------------------------------------- |
|
810 |
|
811 TInt CTEngine::Run(const TDesC& aFile) |
|
812 { |
|
813 return Run(aFile, _L("")); |
|
814 } |
|
815 |
|
816 //----------------------------------------------------------------------------- |
|
817 |
|
818 TInt CTEngine::Run(const TDesC& aFile, const TDesC& aArgs) |
|
819 { |
|
820 iStopped = EFalse; |
|
821 |
|
822 // Open (call) the command file. Stop test engine if fails. |
|
823 TInt error = CallCmdFile(aFile, aArgs); |
|
824 if (error != KErrNone) |
|
825 StopEngine(); |
|
826 |
|
827 // Keep going until stopped or reading the commands fails. |
|
828 while ((!iStopped) && (error == KErrNone)) |
|
829 { |
|
830 error = ReadCmdline(EFalse); |
|
831 if (error == KErrNone) |
|
832 { |
|
833 if (MakeCommand() == KErrNone) |
|
834 { |
|
835 TRAP(error,(void) ObeyCommandL()); |
|
836 } |
|
837 } |
|
838 } |
|
839 |
|
840 // Make sure that command files and log files get closed and return. |
|
841 CloseCmdFiles(); |
|
842 CloseLogFiles(); |
|
843 return error; |
|
844 } |
|
845 |
|
846 //----------------------------------------------------------------------------- |
|
847 // CTEngine::Terminate |
|
848 // |
|
849 // Terminate processing of command files. If not inside a command file, |
|
850 // then does not do anything - doesn't even close the opened log files. |
|
851 |
|
852 void CTEngine::Terminate() |
|
853 { |
|
854 // Inside command file? |
|
855 if (iCmdFile != NULL) |
|
856 { |
|
857 |
|
858 // Close all command files. |
|
859 CloseCmdFiles(); |
|
860 |
|
861 // Close all log files. Write the "run aborted" message to the end |
|
862 // of every log file. |
|
863 while (iLogPile->Count() > 0) |
|
864 { |
|
865 // There is yet at least one log file in pile: write the abort |
|
866 // message and close => previous file in pile will be opened. |
|
867 LogWriteln( TFR_KTxtMsgRunAborted ); |
|
868 (void)CloseLogFile(); |
|
869 } |
|
870 |
|
871 // Close the very last log file too. Having iLogFile = NULL is not |
|
872 // checkd in here because that doesn't do any harm. |
|
873 LogWriteln( TFR_KTxtMsgRunAborted ); |
|
874 delete iLogFile; |
|
875 iLogFile = NULL; |
|
876 |
|
877 // Print abort message to console too. |
|
878 Writeln( TFR_KTxtMsgRunAborted ); |
|
879 } |
|
880 } |
|
881 |
|
882 //----------------------------------------------------------------------------- |
|
883 |
|
884 void CTEngine::StopEngine( ) |
|
885 { |
|
886 iStopped = ETrue; |
|
887 CloseCmdFiles(); |
|
888 |
|
889 // Print message to console |
|
890 Writeln(TFR_KTxtMsgRunStopped); |
|
891 } |
|
892 |
|
893 //----------------------------------------------------------------------------- |
|
894 |
|
895 TBool CTEngine::Stopped() |
|
896 { |
|
897 return iStopped; |
|
898 } |
|
899 |
|
900 //----------------------------------------------------------------------------- |
|
901 |
|
902 void CTEngine::SetEchoMode(const TBool &aBoolean) |
|
903 { |
|
904 iEchoMode = aBoolean; |
|
905 } |
|
906 |
|
907 //----------------------------------------------------------------------------- |
|
908 |
|
909 TBool CTEngine::EchoMode() |
|
910 { |
|
911 return iEchoMode; |
|
912 } |
|
913 |
|
914 //----------------------------------------------------------------------------- |
|
915 |
|
916 #if !defined(TEMPLATED_CATALOGUE) |
|
917 CCatalogue *CTEngine::Defines() |
|
918 #else |
|
919 CCatalogue<CLabeledText> *CTEngine::Defines() |
|
920 #endif |
|
921 { |
|
922 return iDefines; |
|
923 } |
|
924 |
|
925 //----------------------------------------------------------------------------- |
|
926 |
|
927 TInt CTEngine::ReadCmdline(TBool aConsole) |
|
928 { |
|
929 |
|
930 // Reset cmd. |
|
931 iCmdline.Zero(); |
|
932 |
|
933 TInt error = KErrNone; |
|
934 if (iCmdFile != NULL) |
|
935 error = ReadCmdFile(); |
|
936 |
|
937 else |
|
938 // Set error to imply that the end of the cmd file has been reached |
|
939 error = KErrEof; |
|
940 |
|
941 if ((error == KErrEof) && aConsole ) |
|
942 // Get the next cmd line from the console |
|
943 error = ReadConsole(); |
|
944 |
|
945 return error; |
|
946 } |
|
947 |
|
948 //----------------------------------------------------------------------------- |
|
949 // Read command line (iCmdline) from command files. |
|
950 |
|
951 TInt CTEngine::ReadCmdFile() |
|
952 { |
|
953 |
|
954 // Reset cmd and read from the command files. Close files if at end. |
|
955 iCmdline.Zero(); |
|
956 TInt error = KErrEof; |
|
957 while ((iCmdFile != NULL) && (error == KErrEof)) |
|
958 { |
|
959 error = iCmdFile->Read(iCmdline); |
|
960 if (error == KErrEof) |
|
961 { |
|
962 error = CloseCmdFile(); |
|
963 if (error == KErrNone) |
|
964 error = KErrEof; |
|
965 } |
|
966 else |
|
967 CompleteRequest(); |
|
968 |
|
969 } |
|
970 |
|
971 return error; |
|
972 } |
|
973 |
|
974 //----------------------------------------------------------------------------- |
|
975 // Read command line (iCmdline) from console. |
|
976 |
|
977 TInt CTEngine::ReadConsole() |
|
978 { |
|
979 // Get cmd from console - first write prompt. |
|
980 iConsole->Write(iPrompt); |
|
981 iConsoleReader->ReadLine(iCmdline, iStatus); |
|
982 SetActive(); |
|
983 |
|
984 return KErrNone; |
|
985 } |
|
986 |
|
987 //----------------------------------------------------------------------------- |
|
988 |
|
989 TPtrC CTEngine::GetDefine(const TDesC &aTag) const |
|
990 { |
|
991 TPtrC iVal; |
|
992 TInt len = aTag.Length(); |
|
993 |
|
994 for (TInt i = 0; i < iDefines->Count(); ++i) |
|
995 { |
|
996 CLabeledText *tag = iDefines->At(i); |
|
997 TInt pos = tag->Label().Find(aTag); |
|
998 //make sure that 'xyz' does not match with '$xyz_abcd$' |
|
999 if ( (pos == 1) && (tag->Label().Length() == len+2) ) |
|
1000 { |
|
1001 iVal.Set(tag->Value()); |
|
1002 break; |
|
1003 } |
|
1004 } |
|
1005 |
|
1006 return iVal; |
|
1007 } |
|
1008 |
|
1009 //----------------------------------------------------------------------------- |
|
1010 // Make the command (iCommand) to obey. Copies the command line (iCmdline) |
|
1011 // to the iCommand buffer and expands the tags to their defined values. If |
|
1012 // fails, prints out the original command line and an error message. |
|
1013 |
|
1014 TInt CTEngine::MakeCommand() |
|
1015 { |
|
1016 TInt error = KErrNone; |
|
1017 |
|
1018 // Copy command line from iCmdline buffer to iCommand buffer. |
|
1019 iCommand.Copy(iCmdline); |
|
1020 |
|
1021 // Expand tags = replace tag markers with their defined values. |
|
1022 TInt counter = 0; |
|
1023 TBool proceed = ETrue; |
|
1024 while (proceed && (error == KErrNone)) |
|
1025 { |
|
1026 // One round = replace all tag labels with their values and |
|
1027 // alter the proceed to ETrue in replaced one or more tags. |
|
1028 proceed = EFalse; |
|
1029 for (TInt i = 0; i < iDefines->Count(); ++i) |
|
1030 { |
|
1031 CLabeledText *tag = iDefines->At(i); |
|
1032 TInt pos = iCommand.Find(tag->Label()); |
|
1033 if (pos >= 0) |
|
1034 { |
|
1035 |
|
1036 // More than a hundred replacements as the whole is |
|
1037 // propably due to an infinite loop ( recursive tag |
|
1038 // definitions ): |
|
1039 if (counter > 99) |
|
1040 { |
|
1041 // Propably recursive tag definitions. |
|
1042 MsgWriteln(TfrLex::Trim(iCmdline)); |
|
1043 MsgWriteln(TFR_KTxtErrRecursiveTags); |
|
1044 error = KErrOverflow; |
|
1045 break; |
|
1046 } |
|
1047 |
|
1048 // Check that this very tag replacement won't cause |
|
1049 // overflow in command buffer. |
|
1050 TInt newlength = iCommand.Length() - tag->Label().Length(); |
|
1051 newlength += tag->Value().Length(); |
|
1052 if (newlength > iCommand.MaxLength()) |
|
1053 { |
|
1054 // Command has grown too big in size. |
|
1055 MsgWriteln(TfrLex::Trim( iCmdline)); |
|
1056 MsgWriteln(TFR_KTxtErrTooLongCmd); |
|
1057 error = KErrOverflow; |
|
1058 break; |
|
1059 } |
|
1060 |
|
1061 // Fine, replace the tag with its defined value and |
|
1062 // mark that there shall be at least one more round. |
|
1063 proceed = ETrue; |
|
1064 iCommand.Replace(pos, tag->Label().Length(), tag->Value()); |
|
1065 ++counter; // keep up total count of replacements. |
|
1066 } |
|
1067 } // end-for |
|
1068 } // end-while |
|
1069 |
|
1070 // Return if everything went fine. |
|
1071 if ( error == KErrNone ) |
|
1072 return error; |
|
1073 |
|
1074 // Otherwise terminate command files, unless when in check mode. |
|
1075 if ( (iCmdFamily == NULL) || !iCmdFamily->Switch(CCmdFamily::EParseOnly) ) |
|
1076 Terminate(); |
|
1077 |
|
1078 return error; |
|
1079 } |
|
1080 |
|
1081 //----------------------------------------------------------------------------- |
|
1082 |
|
1083 TInt CTEngine::ObeyCommandL() |
|
1084 { |
|
1085 TInt error = KErrNone; |
|
1086 |
|
1087 // The command is handled without the leading/trailing spaces. |
|
1088 TPtrC cmd = TfrLex::Trim(iCommand); |
|
1089 |
|
1090 // The command shall be echoed if echo mode is On and command |
|
1091 // was read from a command file. |
|
1092 TBool doEcho = (iEchoMode && (iCmdFile != NULL)); |
|
1093 |
|
1094 // Check if command begins with @ character and remove if yes. |
|
1095 // That kind of commands aren't echoed afterall. |
|
1096 if (cmd.Match(_L("@*")) == 0) |
|
1097 { |
|
1098 // Command is not echoed afterall. |
|
1099 doEcho = EFalse; |
|
1100 |
|
1101 // Remove @ and them trim once more. |
|
1102 cmd.Set(cmd.Ptr()+1, cmd.Length()-1); |
|
1103 cmd.Set(TfrLex::TrimLeft(cmd)); |
|
1104 } |
|
1105 |
|
1106 // Look up from Command Family if there's a registered command to |
|
1107 // handle this very command string. If there is no Command Family |
|
1108 // then there is no handler either. |
|
1109 // bear in mind that we have to possibly skip a valid command if |
|
1110 // our if mode directs us... |
|
1111 CCmdBase *handler = NULL; |
|
1112 if (iCmdFamily != NULL) |
|
1113 handler = iCmdFamily->Recognize(cmd); |
|
1114 |
|
1115 if (handler != NULL) |
|
1116 { |
|
1117 TBool stepover = DetermineStepover(handler); |
|
1118 |
|
1119 if (!stepover) |
|
1120 { |
|
1121 // This one shall be processed. |
|
1122 // ---------------------------- |
|
1123 // Echo the original input command to the display. |
|
1124 |
|
1125 // log command and display the command on the screen if appropriate |
|
1126 if (iShowCmdLine) |
|
1127 MsgWriteln(iCmdline); |
|
1128 else if (doEcho) |
|
1129 Writeln(TfrLex::TrimRight(iCommand)); |
|
1130 |
|
1131 // Write into the log unless that has been prevented. |
|
1132 // and then process the command. |
|
1133 error = handler->ProcessL(cmd); |
|
1134 } |
|
1135 else |
|
1136 // This one shall not be processed. |
|
1137 // -------------------------------- |
|
1138 // Command it is not written to log nor echoed either. |
|
1139 { |
|
1140 error = handler->ProcessStepOverL(); |
|
1141 } |
|
1142 |
|
1143 } |
|
1144 else |
|
1145 { |
|
1146 // There was no handler for this command i.e. it's an unknown |
|
1147 // command and we don't known what to do with it. However, if |
|
1148 // the processing is in step over mode odds commands are here |
|
1149 // silently ignored (not echoed either). |
|
1150 |
|
1151 if (iCmdFamily->StepOver()) |
|
1152 // Step over mode: ignore odd command silently. |
|
1153 error = KErrNone; |
|
1154 |
|
1155 else |
|
1156 { |
|
1157 // Otherwise, echo the original input command to display |
|
1158 // and log an error message. |
|
1159 if (doEcho) |
|
1160 Writeln(TfrLex::TrimRight(iCommand)); |
|
1161 LogWriteln(cmd); |
|
1162 MsgWriteln(TFR_KTxtErrUnknownCmd); |
|
1163 error = KErrNotFound; |
|
1164 } |
|
1165 } |
|
1166 |
|
1167 // if the last command was a 'domain' command store the result (deano requirement) |
|
1168 // unless the last command was IF! |
|
1169 if (handler == NULL || (handler->CommandId() < THA_KCmdMaxStandard)) |
|
1170 ;// do nothing |
|
1171 else |
|
1172 SetErrorDefine(error); |
|
1173 |
|
1174 // As we are recording the value of the return, it is no longer necessary |
|
1175 // to abort here - let the 'script' worry about subsequent action if required |
|
1176 |
|
1177 // Return if successful. |
|
1178 //if (error == KErrNone) |
|
1179 // return error; |
|
1180 // Error/failure: terminate command files, unless when check mode. |
|
1181 //if ((iCmdFamily == NULL) || !iCmdFamily->Switch(CCmdFamily::EParseOnly)) |
|
1182 //Terminate(); |
|
1183 |
|
1184 return error; |
|
1185 } |
|
1186 |
|
1187 |
|
1188 //----------------------------------------------------------------------------- |
|
1189 // Assign the passed value to the last result define |
|
1190 |
|
1191 TBool CTEngine::DetermineStepover(CCmdBase *aHandler) |
|
1192 { |
|
1193 // Handler was found: check if the command is to be processed |
|
1194 // and if the command is among those to write into the log. |
|
1195 // dont forget to check the if, else, endif situation |
|
1196 // first - incase the if mode needs changing |
|
1197 TBool iSOver = aHandler->StepOver(); |
|
1198 |
|
1199 // we must be aware of the problem of nested loops |
|
1200 // we must 'count' and 'match' the intervening if/else/endif's |
|
1201 // until we get back to our current level |
|
1202 // |
|
1203 // to do this, we need to know what command is being executed |
|
1204 // and use that and the current state to work out where we are. |
|
1205 // At this point we have not set the ifstate so we can predetermine |
|
1206 // the next move. |
|
1207 // |
|
1208 // NOTE: If we encounter if's we must process them (if they are part |
|
1209 // of the correct branch! |
|
1210 |
|
1211 CIFControl::TIFMode eIFMode = GetIfMode(); |
|
1212 CIFControl::TIFProcessing eIFState = GetIfState(); |
|
1213 |
|
1214 // iBranch merely tells us if the current clause return true/false |
|
1215 // not which branch is being executed (thats from eIFState) |
|
1216 TBool iBranch = (iIFPile->Peek()) ? (iIFPile->Peek())->GetIFResult() : EFalse; |
|
1217 TInt iCmd = aHandler->CommandId(); |
|
1218 TBool iInSubClause = EFalse; |
|
1219 switch (iCmd) |
|
1220 { |
|
1221 case THA_KCmdIf : |
|
1222 |
|
1223 // we've encountered an IF statement, which should be processed ONLY |
|
1224 // if we're in the correct branch of a current IF |
|
1225 // if the if is FALSE we still need to count the internal IFs/ELSE/ENDIFs as normal |
|
1226 switch (eIFState) |
|
1227 { |
|
1228 // we are in the middle of an if,else,endif block note: the logic here is tortuous! |
|
1229 // iIfState is true if the IF was true |
|
1230 case CIFControl::EInIF : |
|
1231 |
|
1232 // process the IF if we're in the correct (true) clause this will start |
|
1233 // another IF object (unless it's false) |
|
1234 iSOver = (eIFMode == CIFControl::EELSETrue); |
|
1235 if (iSOver) |
|
1236 // we now have to count the else's and endifs as we're not processing the IF! |
|
1237 { |
|
1238 if (iIFPile->Peek()) |
|
1239 (iIFPile->Peek())->If(); |
|
1240 } |
|
1241 break; |
|
1242 |
|
1243 case CIFControl::EInELSE : |
|
1244 |
|
1245 iSOver = (eIFMode == CIFControl::EIFTrue); |
|
1246 if (iSOver) |
|
1247 { |
|
1248 if (iIFPile->Peek()) |
|
1249 (iIFPile->Peek())->Else(); |
|
1250 } |
|
1251 break; |
|
1252 |
|
1253 // must be a 'virgin' IF so ensure we call it! |
|
1254 default : iSOver = EFalse; break; |
|
1255 } |
|
1256 break; |
|
1257 |
|
1258 case THA_KCmdElse : |
|
1259 |
|
1260 // we're already in an IF or ELSE so merely continue as 'normal' |
|
1261 switch (eIFState) |
|
1262 { |
|
1263 // We should execute the ELSE as this sets up the state var but not the subsequent |
|
1264 // commands as necessary. We are in the middle of an if,else,endif block |
|
1265 // note: the logic here is tortuous! iIfState is true if the IF was true (i.e. |
|
1266 case CIFControl::EInIF : |
|
1267 if (iIFPile->Peek()) |
|
1268 // we need to ensure we're accounting for outer IF's etc |
|
1269 // we need to ensure we process it correctly |
|
1270 iInSubClause = ((iIFPile->Peek())->GetIfCount() > 0) ? ETrue : EFalse; |
|
1271 |
|
1272 if (iInSubClause) |
|
1273 iSOver = iInSubClause; |
|
1274 else |
|
1275 // if the subclause is subject to fail then we should ignore this completely |
|
1276 //iSOver = (eIFMode == CIFControl::EELSETrue) ? ETrue : EFalse; |
|
1277 // if the subclause is subject to fail then we should ignore this completely |
|
1278 // unless we are supposed to execute it... |
|
1279 if (iBranch) |
|
1280 iSOver = (eIFMode == CIFControl::EELSETrue) ? ETrue : EFalse; |
|
1281 else |
|
1282 iSOver = (eIFMode == CIFControl::EELSETrue) ? EFalse : ETrue; |
|
1283 |
|
1284 if (iSOver) |
|
1285 { |
|
1286 if (iIFPile->Peek()) |
|
1287 (iIFPile->Peek())->Else(); |
|
1288 } |
|
1289 break; |
|
1290 |
|
1291 case CIFControl::EInELSE : |
|
1292 if (iIFPile->Peek()) |
|
1293 // we need to ensure we're accounting for outer IF's etc |
|
1294 // we need to ensure we process it correctly |
|
1295 iInSubClause = ((iIFPile->Peek())->GetIfCount() > 0) ? ETrue : EFalse; |
|
1296 |
|
1297 if (iInSubClause) |
|
1298 iSOver = iInSubClause; |
|
1299 else |
|
1300 // if the subclause is subject to fail then we should ignore this completely |
|
1301 // unless we are supposed to execute it... |
|
1302 if (iBranch) |
|
1303 iSOver = (eIFMode == CIFControl::EELSETrue) ? ETrue : EFalse; |
|
1304 else |
|
1305 iSOver = (eIFMode == CIFControl::EELSETrue) ? EFalse : ETrue; |
|
1306 |
|
1307 if (iSOver) |
|
1308 { |
|
1309 if (iIFPile->Peek()) |
|
1310 (iIFPile->Peek())->Else(); |
|
1311 } |
|
1312 break; |
|
1313 |
|
1314 default : |
|
1315 // accept current iSOver state but if we're in the non-executing |
|
1316 // branch of current clause, increment the else count and ensure we |
|
1317 // skip the command |
|
1318 if (!iBranch) |
|
1319 { |
|
1320 if (iIFPile->Peek()) |
|
1321 (iIFPile->Peek())->Else(); |
|
1322 iSOver = true; |
|
1323 } |
|
1324 break; |
|
1325 } |
|
1326 break; |
|
1327 |
|
1328 // ok, here's where the real 'meat' of the situation |
|
1329 case THA_KCmdEndIf : |
|
1330 { |
|
1331 |
|
1332 // we MUST be in an IF sequence, but we also need to be aware |
|
1333 // of the state of any outer IF clause |
|
1334 if (iIFPile->Peek()) |
|
1335 { |
|
1336 // before we decrement our counters, we need to see the state of play |
|
1337 // if we're in an IF which is part of an untrue IF clause *see if test9/10* |
|
1338 // we need to ensure we process it correctly |
|
1339 TInt iIfs = (iIFPile->Peek())->GetIfCount(); |
|
1340 //TInt iElses = (iIFPile->Peek())->GetElseCount(); |
|
1341 iInSubClause = (iIfs == 0) ? EFalse : ETrue; |
|
1342 //iInSubClause = ( (iIfs == 0) && (iElses == 0) ) ? EFalse : ETrue; |
|
1343 |
|
1344 // decrement our counters. If we are NOT in a SubClause then these will |
|
1345 // be zero. If so we SHOULD execute the ENDIF |
|
1346 (iIFPile->Peek())->EndIf(); |
|
1347 } |
|
1348 |
|
1349 switch (eIFState) |
|
1350 { |
|
1351 // we are in the middle of an if,else,endif block |
|
1352 // note: the logic here is tortuous! |
|
1353 // iIfState is true if the IF was true (i.e. |
|
1354 //iSOver = iIfState; break; |
|
1355 case CIFControl::EInIF : |
|
1356 iSOver = (iSOver || (eIFMode == CIFControl::EELSETrue)) && iInSubClause; |
|
1357 break; |
|
1358 case CIFControl::EInELSE : |
|
1359 iSOver = (iSOver || (eIFMode == CIFControl::EIFTrue)) && iInSubClause; |
|
1360 break; |
|
1361 default : |
|
1362 if (iInSubClause) |
|
1363 iSOver = ETrue; |
|
1364 break; |
|
1365 } |
|
1366 break; |
|
1367 } |
|
1368 |
|
1369 // other commands must be checked against our if state! |
|
1370 default : |
|
1371 switch (eIFState) |
|
1372 { |
|
1373 case CIFControl::EInIF : |
|
1374 switch (eIFMode) |
|
1375 { |
|
1376 case CIFControl::EIFTrue : iSOver = EFalse; break; |
|
1377 case CIFControl::EELSETrue : iSOver = ETrue; break; |
|
1378 default : iSOver = ETrue; break; // ????? |
|
1379 } |
|
1380 break; |
|
1381 |
|
1382 case CIFControl::EInELSE : |
|
1383 switch (eIFMode) |
|
1384 { |
|
1385 case CIFControl::EIFTrue : iSOver = ETrue; break; |
|
1386 case CIFControl::EELSETrue : iSOver = EFalse; break; |
|
1387 default : iSOver = ETrue; break; // ????? |
|
1388 //case CIFControl::EENDIF : iSOver = !iBranch; break; |
|
1389 } |
|
1390 break; |
|
1391 |
|
1392 default : |
|
1393 switch (eIFMode) |
|
1394 { |
|
1395 case CIFControl::EIFTrue : iSOver = ETrue; break; |
|
1396 case CIFControl::EELSETrue : iSOver = EFalse; break; |
|
1397 default : iSOver = EFalse; break; // ????? |
|
1398 //case CIFControl::EENDIF : iSOver = ETrue; break; |
|
1399 } |
|
1400 break; |
|
1401 } |
|
1402 break; |
|
1403 } |
|
1404 |
|
1405 return iSOver; |
|
1406 } |
|
1407 |
|
1408 //----------------------------------------------------------------------------- |
|
1409 // Assign the passed value to the last result define |
|
1410 |
|
1411 void CTEngine::SetResultDefine(TInt aValue) |
|
1412 { |
|
1413 TInt error; |
|
1414 |
|
1415 // so that we can process it if required - code stolen from CmdDefine... |
|
1416 TBuf<16+2> taglabel; |
|
1417 taglabel.Format(KLastResultCode); |
|
1418 |
|
1419 // Set tag value to be the last error code received |
|
1420 TBuf<12> tagvalue; |
|
1421 tagvalue.Format(_L("%d"), aValue); |
|
1422 CLabeledText *tag = Defines()->Text(taglabel); |
|
1423 if (tag != NULL) |
|
1424 { |
|
1425 // alter tag |
|
1426 TRAP(error, tag->SetL(tagvalue)); |
|
1427 } |
|
1428 else |
|
1429 { |
|
1430 // Add a tag. |
|
1431 TRAP(error, Defines()->AddL(taglabel, tagvalue)); |
|
1432 } |
|
1433 } |
|
1434 |
|
1435 void CTEngine::SetResultDefine (const TDesC& aDefineTxt, TInt aValue) |
|
1436 { |
|
1437 TInt error; |
|
1438 |
|
1439 TBuf<32+2> taglabel; |
|
1440 |
|
1441 taglabel.Format(_L("$%SHttpStatus$"), &aDefineTxt); |
|
1442 |
|
1443 // Set tag value to be the last error code received |
|
1444 TBuf<16> tagvalue; |
|
1445 tagvalue.Format(_L("%d"), aValue); |
|
1446 CLabeledText *tag = Defines()->Text(taglabel); |
|
1447 |
|
1448 if (tag != NULL) |
|
1449 { |
|
1450 TRAP(error, tag->SetL(tagvalue)); |
|
1451 } |
|
1452 else |
|
1453 { |
|
1454 // Add a tag. |
|
1455 TRAP(error, Defines()->AddL(taglabel, tagvalue)); |
|
1456 } |
|
1457 } |
|
1458 |
|
1459 void CTEngine::DeleteResultDefine (const TDesC& aDefineTxt) |
|
1460 { |
|
1461 TBuf<32+2> taglabel; |
|
1462 |
|
1463 taglabel.Format(_L("$%SHttpStatus$"), &aDefineTxt); |
|
1464 |
|
1465 Defines()->Delete(taglabel); |
|
1466 } |
|
1467 |
|
1468 //----------------------------------------------------------------------------- |
|
1469 // Assign the passed value to the last result define |
|
1470 |
|
1471 void CTEngine::SetErrorDefine(TInt aValue) |
|
1472 { |
|
1473 TInt error; |
|
1474 |
|
1475 // so that we can process it if required - code stolen from CmdDefine... |
|
1476 TBuf<16+2> taglabel; |
|
1477 taglabel.Format(KLastErrorCode); |
|
1478 |
|
1479 // Set tag value to be the last error code received |
|
1480 TBuf<12> tagvalue; |
|
1481 tagvalue.Format(_L("%d"), aValue); |
|
1482 CLabeledText *tag = Defines()->Text(taglabel); |
|
1483 if (tag != NULL) |
|
1484 { |
|
1485 // alter tag |
|
1486 TRAP(error, tag->SetL(tagvalue)); |
|
1487 } |
|
1488 else |
|
1489 { |
|
1490 // Add a tag. |
|
1491 TRAP(error, Defines()->AddL(taglabel, tagvalue)); |
|
1492 } |
|
1493 } |
|
1494 |
|
1495 void CTEngine::SetErrorDefine (const TDesC& aDefineTxt, TInt aValue) |
|
1496 { |
|
1497 TInt error; |
|
1498 |
|
1499 TBuf<32+2> taglabel; |
|
1500 |
|
1501 taglabel.Format(_L("$%SLastEvent$"), &aDefineTxt); |
|
1502 |
|
1503 // Set tag value to be the last error code received |
|
1504 TBuf<16> tagvalue; |
|
1505 tagvalue.Format(_L("%d"), aValue); |
|
1506 CLabeledText *tag = Defines()->Text(taglabel); |
|
1507 |
|
1508 if (tag != NULL) |
|
1509 { |
|
1510 TRAP(error, tag->SetL(tagvalue)); |
|
1511 } |
|
1512 else |
|
1513 { |
|
1514 // Add a tag. |
|
1515 TRAP(error, Defines()->AddL(taglabel, tagvalue)); |
|
1516 } |
|
1517 } |
|
1518 |
|
1519 void CTEngine::DeleteErrorDefine (const TDesC& aDefineTxt) |
|
1520 { |
|
1521 TBuf<32+2> taglabel; |
|
1522 |
|
1523 taglabel.Format(_L("$%SLastEvent$"), &aDefineTxt); |
|
1524 |
|
1525 Defines()->Delete(taglabel); |
|
1526 } |
|
1527 |
|
1528 //----------------------------------------------------------------------------- |
|
1529 // Called by the scheduler evert time request completed. |
|
1530 |
|
1531 void CTEngine::RunL() |
|
1532 { |
|
1533 switch (iState) |
|
1534 { |
|
1535 case EIdle: |
|
1536 if (iStopped) |
|
1537 // We've finished - close resources... |
|
1538 { |
|
1539 CloseCmdFiles(); |
|
1540 CloseLogFiles(); |
|
1541 |
|
1542 // Cancel... |
|
1543 Cancel(); |
|
1544 |
|
1545 // Stop the active scheduler only if we're driven via commandline script. |
|
1546 // if via interactive then has no meaning! |
|
1547 //if (Console != NULL) |
|
1548 CActiveScheduler::Stop(); |
|
1549 } |
|
1550 break; |
|
1551 |
|
1552 case EStopping: |
|
1553 { |
|
1554 // Close any open sessions and their transactions |
|
1555 if ( iCmdFamily != NULL ) |
|
1556 { |
|
1557 // Perform any operational close/shutdown operations here |
|
1558 //CCmdBase *handler = NULL; |
|
1559 //handler = iCmdFamily->Command(EDoClose); |
|
1560 //STATIC_CAST(CCmdDoClose*, handler)->CloseAll(); |
|
1561 } |
|
1562 iState = EIdle; |
|
1563 } |
|
1564 CompleteRequest(); |
|
1565 break; |
|
1566 |
|
1567 case EGetNextCmd: |
|
1568 // Get the next cmd |
|
1569 if (ReadCmdline(iConsoleEnabled) != KErrNone) |
|
1570 { |
|
1571 // Something went wrong, so stop |
|
1572 iStopped = ETrue; |
|
1573 iState = EIdle; |
|
1574 |
|
1575 // Complete self |
|
1576 CompleteRequest(); |
|
1577 } |
|
1578 else |
|
1579 // Parse the next cmd once we get it |
|
1580 iState = EParseCurrentCmd; |
|
1581 break; |
|
1582 |
|
1583 case EParseCurrentCmd: |
|
1584 // Run the parsed cmd or Could not parse the cmd line so get the next cmd |
|
1585 iState = (MakeCommand() == KErrNone) ? ERunCurrentCmd : EGetNextCmd; |
|
1586 |
|
1587 // Complete self and set active |
|
1588 CompleteRequest(); |
|
1589 break; |
|
1590 |
|
1591 case ERunCurrentCmd: |
|
1592 { |
|
1593 TInt iErr = ObeyCommandL(); |
|
1594 |
|
1595 // Run the cmd - the cmd will complete the request |
|
1596 if (iErr != KErrNone) |
|
1597 { |
|
1598 // if major problem then complete the request |
|
1599 if (!IsActive()) |
|
1600 CompleteRequest(); |
|
1601 else // command requested produced subsequent operational error (i.e. what we're looking for!) |
|
1602 { |
|
1603 // assign the response to an accessible 'variable' |
|
1604 } |
|
1605 } |
|
1606 |
|
1607 // Set state... |
|
1608 iState = (!iStopped) ? EGetNextCmd : EStopping; |
|
1609 } |
|
1610 |
|
1611 break; |
|
1612 |
|
1613 default : break; |
|
1614 } |
|
1615 } |
|
1616 |
|
1617 //----------------------------------------------------------------------------- |
|
1618 |
|
1619 void CTEngine::DoCancel() |
|
1620 { |
|
1621 iTimer.Cancel(); |
|
1622 iConsoleReader->ReadCancel(); |
|
1623 } |
|
1624 |
|
1625 //----------------------------------------------------------------------------- |
|
1626 // Standard Issue... |
|
1627 |
|
1628 void CTEngine::CompleteRequest() |
|
1629 { |
|
1630 if (IsActive()) |
|
1631 return ; |
|
1632 |
|
1633 TRequestStatus* pStat = &iStatus; |
|
1634 |
|
1635 User::RequestComplete(pStat, KErrNone); |
|
1636 SetActive(); |
|
1637 } |
|
1638 |
|
1639 //----------------------------------------------------------------------------- |
|
1640 // was 'GetAnEntry' but that is slightly inaccurate! |
|
1641 |
|
1642 void CTEngine::GetUserInput(const TDesC& ourPrompt, TDes& currentstring) |
|
1643 { |
|
1644 TBuf16<KMaxUserEntrySize> ourLine; |
|
1645 TBuf<KMaxUserEntrySize> tempstring; //tempstring is a unicode descriptor |
|
1646 |
|
1647 // create a temporary buffer where the unicode strings are stored in order to |
|
1648 // be displayed |
|
1649 ourLine.Zero(); |
|
1650 tempstring.Copy(currentstring); //Copy current string to Unicode buffer |
|
1651 TKeyCode key = EKeyNull; //current string buffer is 8 bits wide. |
|
1652 |
|
1653 // Unicode string bufffer (tempstring) is 16 bits wide. |
|
1654 FOREVER |
|
1655 { |
|
1656 if (ourLine.Length () == 0) |
|
1657 { |
|
1658 |
|
1659 iConsole->SetPos (0, iConsole->WhereY ()); |
|
1660 iConsole->Printf (_L ("%S"), &ourPrompt); |
|
1661 if (tempstring.Length () != 0) //get tempstring's number of items |
|
1662 iConsole->Printf (_L (" = %S"), &tempstring); //if not zero print them to iTest.Console() |
|
1663 iConsole->Printf (_L (" : ")); |
|
1664 iConsole->ClearToEndOfLine (); |
|
1665 } |
|
1666 |
|
1667 key = iConsole->Getch(); |
|
1668 |
|
1669 if (key == EKeyBackspace) |
|
1670 { |
|
1671 if (ourLine.Length() !=0) |
|
1672 { |
|
1673 ourLine.SetLength(ourLine.Length()-1); |
|
1674 iConsole->Printf(_L ("%c"), key); |
|
1675 iConsole->SetPos(iConsole->WhereX(), iConsole->WhereY()); |
|
1676 iConsole->ClearToEndOfLine(); |
|
1677 } // end if (ourLine.Length() !=0) |
|
1678 } // end if (key == KeyBackSpace) |
|
1679 |
|
1680 |
|
1681 if (key == EKeyDelete) |
|
1682 { |
|
1683 ourLine.Zero(); |
|
1684 iConsole->SetPos(0, iConsole->WhereY()); |
|
1685 iConsole->ClearToEndOfLine(); |
|
1686 tempstring.Copy(ourLine); |
|
1687 break; |
|
1688 } |
|
1689 |
|
1690 if (key == EKeyEnter) |
|
1691 break; |
|
1692 |
|
1693 if (key < ' ') // ascii code thats not a printable character |
|
1694 continue; |
|
1695 |
|
1696 ourLine.Append(key); |
|
1697 iConsole->Printf(_L ("%c"), key); |
|
1698 iConsole->SetPos(iConsole->WhereX(), iConsole->WhereY()); |
|
1699 iConsole->ClearToEndOfLine(); |
|
1700 if (ourLine.Length () == ourLine.MaxLength ()) |
|
1701 break; |
|
1702 } // end of for statement |
|
1703 |
|
1704 if ((key == EKeyEnter) && (ourLine.Length () == 0)) |
|
1705 tempstring.Copy (currentstring); //copy contents of 8 bit "ourLine" descriptor |
|
1706 |
|
1707 iConsole->SetPos(0, iConsole->WhereY()); |
|
1708 iConsole->ClearToEndOfLine(); |
|
1709 |
|
1710 if ((key == EKeyEnter) && (ourLine.Length() !=0)) |
|
1711 tempstring.Copy(ourLine); |
|
1712 |
|
1713 if (tempstring.Length () != 0) //if temstring length is not zero |
|
1714 { |
|
1715 iConsole->Printf(_L(" Entered = %S\n"), &tempstring); //print the contents to iTest.Console() |
|
1716 LogPrintf(_L("%S = %S\n"), &ourPrompt, &tempstring); |
|
1717 } |
|
1718 |
|
1719 iConsole->Printf (_L("\n")); |
|
1720 currentstring.Copy(tempstring); //copy 16 bit tempstring descriptor back |
|
1721 } |
|
1722 |
|
1723 //----------------------------------------------------------------------------- |
|
1724 // File Management and Support |
|
1725 // note: this takes the current 'path' as defined by PATH as the root file path |
|
1726 // if defined. |
|
1727 |
|
1728 TInt CTEngine::SetFileName(const TDesC &aName, RFile &aReqFile) |
|
1729 { |
|
1730 TBuf<256> ourFile(iCmdPath); |
|
1731 ourFile.Append(aName); |
|
1732 |
|
1733 TParse ParsedFileName; |
|
1734 TInt iError = ParsedFileName.Set(ourFile, NULL, NULL); |
|
1735 |
|
1736 if (iError == KErrNone) |
|
1737 {// check is a valid file and open a handle |
|
1738 |
|
1739 // assign the filename to our reference |
|
1740 TFileName fileName; |
|
1741 fileName.Copy(ParsedFileName.FullName()); |
|
1742 |
|
1743 if (iFileServer.IsValidName(fileName)) |
|
1744 { |
|
1745 iError = aReqFile.Open(iFileServer, ParsedFileName.FullName(), EFileRead); |
|
1746 } |
|
1747 } |
|
1748 |
|
1749 return iError; |
|
1750 } |
|
1751 |
|
1752 //----------------------------------------------------------------------------- |
|
1753 |
|
1754 //----------------------------------------------------------------------------- |
|
1755 // Do a formatted dump of binary data, optionally logging it |
|
1756 // Iterate the supplied block of data in blocks of 16 bytes |
|
1757 |
|
1758 void CTEngine::DumpData(const TDesC8& aData, const TBool &logIt) |
|
1759 { |
|
1760 TInt pos = 0; |
|
1761 TBuf<KMaxLogEntrySize> logLine; |
|
1762 TBuf<KMaxLogEntrySize> anEntry; |
|
1763 |
|
1764 while (pos < aData.Length()) |
|
1765 { |
|
1766 anEntry.Format(TRefByValue<const TDesC>_L("%04x : "), pos); |
|
1767 logLine.Append(anEntry); |
|
1768 |
|
1769 // Hex output |
|
1770 TInt offset; |
|
1771 for (offset = 0; offset < 16; ++offset) |
|
1772 { |
|
1773 if (pos + offset < 10)//aData.Length()) |
|
1774 { |
|
1775 TInt nextByte = aData[pos + offset]; |
|
1776 anEntry.Format(TRefByValue<const TDesC>_L("%02x "), nextByte); |
|
1777 logLine.Append(anEntry); |
|
1778 } |
|
1779 else |
|
1780 { |
|
1781 anEntry.Format(TRefByValue<const TDesC>_L(" ")); |
|
1782 logLine.Append(anEntry); |
|
1783 } |
|
1784 } |
|
1785 |
|
1786 anEntry.Format(TRefByValue<const TDesC>_L(": ")); |
|
1787 logLine.Append(anEntry); |
|
1788 |
|
1789 // Char output |
|
1790 for (offset = 0; offset < 16; ++offset) |
|
1791 { |
|
1792 if (pos + offset < 10)//aData.Length()) |
|
1793 { |
|
1794 TInt nextByte = aData[pos + offset]; |
|
1795 if ((nextByte >= 32) && (nextByte <= 127)) |
|
1796 { |
|
1797 anEntry.Format(TRefByValue<const TDesC>_L("%c"), nextByte); |
|
1798 logLine.Append(anEntry); |
|
1799 } |
|
1800 else |
|
1801 { |
|
1802 anEntry.Format(TRefByValue<const TDesC>_L(".")); |
|
1803 logLine.Append(anEntry); |
|
1804 } |
|
1805 } |
|
1806 else |
|
1807 { |
|
1808 anEntry.Format(TRefByValue<const TDesC>_L(" ")); |
|
1809 logLine.Append(anEntry); |
|
1810 } |
|
1811 } |
|
1812 |
|
1813 if (logIt) |
|
1814 LogPrintf(TRefByValue<const TDesC>_L("%S"), &logLine); |
|
1815 |
|
1816 // Advance to next 16 byte segment |
|
1817 pos += 16; |
|
1818 } |
|
1819 } |
|
1820 |
|
1821 //----------------------------------------------------------------------------- |
|
1822 |
|
1823 CIFControl::TIFMode CTEngine::GetIfMode() |
|
1824 { |
|
1825 return (iIFPile->Peek()) ? (iIFPile->Peek())->GetIFMode() : CIFControl::ENotIf; |
|
1826 } |
|
1827 |
|
1828 //----------------------------------------------------------------------------- |
|
1829 |
|
1830 void CTEngine::SetIfMode(const CIFControl::TIFMode &eMode) |
|
1831 { |
|
1832 if (iIFPile->Peek()) |
|
1833 (iIFPile->Peek())->SetIFMode(eMode); |
|
1834 } |
|
1835 |
|
1836 //----------------------------------------------------------------------------- |
|
1837 |
|
1838 CIFControl::TIFProcessing CTEngine::GetIfState() |
|
1839 { |
|
1840 return (iIFPile->Peek()) ? (iIFPile->Peek())->GetIFState() : CIFControl::EIgnoreIF; |
|
1841 } |
|
1842 |
|
1843 //----------------------------------------------------------------------------- |
|
1844 |
|
1845 void CTEngine::SetIfState(const CIFControl::TIFProcessing &eProc) |
|
1846 { |
|
1847 if (iIFPile->Peek()) |
|
1848 (iIFPile->Peek())->SetIFState(eProc); |
|
1849 } |
|
1850 |
|
1851 //----------------------------------------------------------------------------- |
|
1852 // Create new IF object to manage the current processing |
|
1853 // Use the EndIf to Pop the current object when ENDIF is processed |
|
1854 |
|
1855 void CTEngine::IfL(const CIFControl::TIFMode &eProc, |
|
1856 const TBool &aResult, |
|
1857 const CIFControl::TIFProcessing &aProcess) |
|
1858 { |
|
1859 CIFControl *cif = CIFControl::NewLC(eProc, aResult, aProcess); |
|
1860 |
|
1861 iIFPile->PushL(cif); |
|
1862 // CleanupStack::PopAndDestroy(); |
|
1863 CleanupStack::Pop(); |
|
1864 } |
|
1865 |
|
1866 //----------------------------------------------------------------------------- |
|
1867 |
|
1868 void CTEngine::EndIf() |
|
1869 { |
|
1870 if (iIFPile->Peek()) |
|
1871 iIFPile->Skim(); |
|
1872 } |
|
1873 |
|
1874 //----------------------------------------------------------------------------- |
|
1875 // End of File |
|
1876 //----------------------------------------------------------------------------- |