core/src/fshell.cpp
changeset 78 b3ffff030d5c
parent 66 2a78c4ff2eab
equal deleted inserted replaced
77:8df58d8c99e8 78:b3ffff030d5c
   115 			{
   115 			{
   116 			FormatError(_L("Unable to run command \"%S\""), &iContext);
   116 			FormatError(_L("Unable to run command \"%S\""), &iContext);
   117 			break;
   117 			break;
   118 			}
   118 			}
   119 		case EFailedToCreatePipeLine:
   119 		case EFailedToCreatePipeLine:
   120 		case EFailedToSetChildErrorVar:
   120 		case EPipelineCompletionError:
   121 		case EFailedToSetScriptLineVar:
       
   122 		case EUnknown:
   121 		case EUnknown:
   123 		default:
   122 		default:
   124 			{
   123 			{
   125 			PrintError();
   124 			PrintError();
   126 			break;
   125 			break;
   198 		{
   197 		{
   199 		CASE_RETURN_LIT(EUnknown);
   198 		CASE_RETURN_LIT(EUnknown);
   200 		CASE_RETURN_LIT(EFailedToCreatePipeLine);
   199 		CASE_RETURN_LIT(EFailedToCreatePipeLine);
   201 		CASE_RETURN_LIT(EFailedToConstructCommand);
   200 		CASE_RETURN_LIT(EFailedToConstructCommand);
   202 		CASE_RETURN_LIT(EFailedToRunCommand);
   201 		CASE_RETURN_LIT(EFailedToRunCommand);
   203 		CASE_RETURN_LIT(EFailedToSetChildErrorVar);
   202 		CASE_RETURN_LIT(EPipelineCompletionError);
   204 		CASE_RETURN_LIT(EFailedToSetScriptLineVar);
       
   205 		CASE_RETURN_LIT(ECommandError);
   203 		CASE_RETURN_LIT(ECommandError);
   206 		DEFAULT_RETURN_LIT("*** REASON UNKNOWN ***");
   204 		DEFAULT_RETURN_LIT("*** REASON UNKNOWN ***");
   207 		}
   205 		}
   208 	}
   206 	}
   209 
   207 
   294 	return self;
   292 	return self;
   295 	}
   293 	}
   296 
   294 
   297 CShell::~CShell()
   295 CShell::~CShell()
   298 	{
   296 	{
       
   297 	iScriptHandle.Close();
   299 	iJobsLock.Close();
   298 	iJobsLock.Close();
   300 	iJobs.ResetAndDestroy();
   299 	iJobs.ResetAndDestroy();
   301 	delete iScriptArgs;
   300 	delete iScriptArgs;
   302 	delete iLineEditor;
   301 	delete iLineEditor;
   303 	delete iLineCompleter;
   302 	delete iLineCompleter;
   304 	delete iConsole;
   303 	delete iConsole;
   305 	delete iCommandFactory;
   304 	delete iCommandFactory;
   306 	delete iScriptData;
       
   307 	delete iOneLiner;
   305 	delete iOneLiner;
   308 	delete iParser;
   306 	delete iParser;
   309 	}
   307 	}
   310 
   308 
   311 CCommandFactory& CShell::CommandFactory()
   309 CCommandFactory& CShell::CommandFactory()
   513 		}
   511 		}
   514 	else if (iScriptName.Length() > 0)
   512 	else if (iScriptName.Length() > 0)
   515 		{
   513 		{
   516 		TIoHandleSet ioHandles(IoSession(), Stdin(), Stdout(), Stderr());
   514 		TIoHandleSet ioHandles(IoSession(), Stdin(), Stdout(), Stderr());
   517 		TBool helpPrinted;
   515 		TBool helpPrinted;
   518 		iScriptData = ReadScriptL(iScriptName, iScriptArgs, Env(), FsL(), ioHandles, helpPrinted);
   516 		iScriptHandle = OpenScriptL(iScriptName, iScriptArgs, Env(), FsL(), ioHandles, helpPrinted);
   519 		if (helpPrinted)
   517 		if (helpPrinted)
   520 			{
   518 			{
   521 			Complete();
   519 			Complete();
   522 			}
   520 			}
   523 		else
   521 		else
   525 			TUint mode = CParser::EExportLineNumbers;
   523 			TUint mode = CParser::EExportLineNumbers;
   526 			if (iKeepGoing)
   524 			if (iKeepGoing)
   527 				{
   525 				{
   528 				mode |= CParser::EKeepGoing;
   526 				mode |= CParser::EKeepGoing;
   529 				}
   527 				}
   530 			iParser = CParser::NewL(mode, *iScriptData, IoSession(), Stdin(), Stdout(), Stderr(), Env(), *iCommandFactory, this);
   528 			iParser = CParser::NewL(mode, iScriptHandle, IoSession(), Stdin(), Stdout(), Stderr(), Env(), *iCommandFactory, this);
   531 			RProcess::Rendezvous(KErrNone);
   529 			RProcess::Rendezvous(KErrNone);
   532 			iParser->Start();
   530 			iParser->Start();
   533 			}
   531 			}
   534 		}
   532 		}
   535 	else
   533 	else
   639 			}
   637 			}
   640 		}
   638 		}
   641 	return KErrNotFound;
   639 	return KErrNotFound;
   642 	}
   640 	}
   643 
   641 
   644 HBufC* CShell::ReadScriptL(const TDesC& aScriptName, const TDesC* aArguments, IoUtils::CEnvironment& aEnv, RFs& aFs, TIoHandleSet& aIoHandles, TBool& aHelpPrinted, RPointerArray<HBufC>* aAdditionalPrefixArguments)
   642 RIoReadHandle CShell::OpenScriptL(const TDesC& aScriptName, const TDesC* aArguments, IoUtils::CEnvironment& aEnv, RFs& aFs, TIoHandleSet& aIoHandles, TBool& aHelpPrinted, RPointerArray<HBufC>* aAdditionalPrefixArguments)
   645 	{
   643 	{
       
   644 	RIoReadHandle readHandle;
       
   645 	readHandle.CreateL(aIoHandles.IoSession());
       
   646 	CleanupClosePushL(readHandle);
       
   647 
   646 	TFileName2 scriptName(aScriptName);
   648 	TFileName2 scriptName(aScriptName);
   647 
   649 
   648 	// Check the scripts dirs in case it wasn't given as an absolute path (although iocli will have made it absolute relative to the pwd)
   650 	// Check the scripts dirs in case it wasn't given as an absolute path (although iocli will have made it absolute relative to the pwd)
   649 	_LIT(KScriptDir, "y:\\system\\console\\scripts\\");
   651 	_LIT(KScriptDir, "y:\\system\\console\\scripts\\");
   650 	_LIT(KScriptSuffix, ".script");
   652 	_LIT(KScriptSuffix, ".script");
   668 			// Put scriptname back the way it was
   670 			// Put scriptname back the way it was
   669 			scriptName.SetLength(scriptNameLen);
   671 			scriptName.SetLength(scriptNameLen);
   670 			} 
   672 			} 
   671 		}
   673 		}
   672 
   674 
   673 	RFile scriptFile;
   675 	RIoFile scriptFile;
   674 	TInt err;
   676 	TInt err;
   675 	TInt retries = 5;
   677 	TInt retries = 5;
   676 	do
   678 	do
   677 		{
   679 		{
   678 		err = scriptFile.Open(aFs, scriptName, EFileRead | EFileShareReadersOnly);
   680 		err = scriptFile.Create(aIoHandles.IoSession(), scriptName, RIoFile::ERead);
   679 		if ((err == KErrNone) || (err != KErrInUse))
   681 		if ((err == KErrNone) || (err != KErrInUse))
   680 			{
   682 			{
   681 			break;
   683 			break;
   682 			}
   684 			}
   683 		User::After(500000);
   685 		User::After(500000);
   684 		--retries;
   686 		--retries;
   685 		}
   687 		}
   686 		while (retries >= 0);
   688 		while (retries >= 0);
   687 	StaticLeaveIfErr(err, _L("Couldn't open script file %S"), &scriptName);
   689 	StaticLeaveIfErr(err, _L("Couldn't open script file %S"), &scriptName);
   688 	CleanupClosePushL(scriptFile);
   690 	CleanupClosePushL(scriptFile);
   689 	TInt scriptFileSize;
   691 	scriptFile.AttachL(readHandle, RIoEndPoint::EForeground);
   690 	User::LeaveIfError(scriptFile.Size(scriptFileSize));
   692 	CleanupStack::PopAndDestroy(&scriptFile);
   691 	HBufC8* scriptData = HBufC8::NewLC(scriptFileSize);
   693 
   692 	TPtr8 scriptDataPtr(scriptData->Des());
       
   693 	User::LeaveIfError(scriptFile.Read(scriptDataPtr));
       
   694 	HBufC* decodedScriptData = LtkUtils::DecodeUtf8L(*scriptData);
       
   695 	CleanupStack::PopAndDestroy(2, &scriptFile);
       
   696 	CleanupStack::PushL(decodedScriptData);
       
   697 	aEnv.SetLocalL(KScriptName);
   694 	aEnv.SetLocalL(KScriptName);
   698 	aEnv.SetLocalL(KScriptPath);
   695 	aEnv.SetLocalL(KScriptPath);
   699 	aEnv.SetLocalL(KScriptLine);
   696 	aEnv.SetLocalL(KScriptLine);
   700 	aEnv.SetLocalL(_L("0"));
   697 	aEnv.SetLocalL(_L("0"));
   701 	aEnv.SetL(KScriptName, scriptName.NameAndExt());
   698 	aEnv.SetL(KScriptName, scriptName.NameAndExt());
   702 	aEnv.SetL(KScriptPath, scriptName.DriveAndPath());
   699 	aEnv.SetL(KScriptPath, scriptName.DriveAndPath());
   703 	aEnv.SetL(_L("0"), scriptName);
   700 	aEnv.SetL(_L("0"), scriptName);
       
   701 
   704 	CScriptCommand* scriptCommand = CScriptCommand::NewLC(scriptName, aIoHandles);
   702 	CScriptCommand* scriptCommand = CScriptCommand::NewLC(scriptName, aIoHandles);
   705 	TRAP(err, scriptCommand->ParseCommandLineArgsL(aArguments ? *aArguments : KNullDesC(), aEnv, aAdditionalPrefixArguments));
   703 	TRAP(err, scriptCommand->ParseCommandLineArgsL(aArguments ? *aArguments : KNullDesC(), aEnv, aAdditionalPrefixArguments));
   706 	if (err == KErrArgument || scriptCommand->ShouldDisplayHelp())
   704 	if (err == KErrArgument || scriptCommand->ShouldDisplayHelp())
   707 		{
   705 		{
   708 		// Need to display help
   706 		// Need to display help
   721 		{
   719 		{
   722 		aHelpPrinted = EFalse;
   720 		aHelpPrinted = EFalse;
   723 		}
   721 		}
   724 	User::LeaveIfError(err); // Propagate error
   722 	User::LeaveIfError(err); // Propagate error
   725 	CleanupStack::PopAndDestroy(scriptCommand);
   723 	CleanupStack::PopAndDestroy(scriptCommand);
   726 	CleanupStack::Pop(decodedScriptData);
   724 	CleanupStack::Pop(&readHandle);
   727 	return decodedScriptData;
   725 	return readHandle;
   728 	}
   726 	}
   729 
   727 
   730 void CShell::SetToForeground()
   728 void CShell::SetToForeground()
   731 	{
   729 	{
   732 	Stdin().SetToForeground();
   730 	Stdin().SetToForeground();