core/extra/shebang/sbapp.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // sbapp.cpp
       
     2 // 
       
     3 // Copyright (c) 2007 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 // Description:
       
    14 // Launches the executable named in a "shebang" line (e.g "#!fshell") at the beginning of a script.
       
    15 
       
    16 #include <e32std.h>
       
    17 #include <e32base.h>
       
    18 #include <f32file.h>
       
    19 #include <apacmdln.h>
       
    20 
       
    21 void RunL()
       
    22 	{
       
    23 	CApaCommandLine* commandLine = NULL;
       
    24 	User::LeaveIfError(CApaCommandLine::GetCommandLineFromProcessEnvironment(commandLine));
       
    25 	CleanupStack::PushL(commandLine);
       
    26 	TFileName scriptFileName(commandLine->DocumentName());
       
    27 	RFs fs;
       
    28 	User::LeaveIfError(fs.Connect());
       
    29 	CleanupClosePushL(fs);
       
    30 	RFile file;
       
    31 	commandLine->GetFileByHandleL(file);
       
    32 	if (!file.SubSessionHandle())
       
    33 		{
       
    34 		User::LeaveIfError(file.Open(fs, scriptFileName, EFileRead));
       
    35 		}
       
    36 	RThread().Rendezvous(KErrNone);
       
    37 	CleanupClosePushL(file);
       
    38 	TBuf8<64> buf;
       
    39 	User::LeaveIfError(file.Read(buf));
       
    40 	CleanupStack::PopAndDestroy(3, commandLine);
       
    41 	if (buf.Left(2) == _L8("#!"))
       
    42 		{
       
    43 		TLex8 lex(buf.Mid(2));
       
    44 		lex.Mark();
       
    45 		while (!lex.Eos())
       
    46 			{
       
    47 			TChar ch(lex.Get());
       
    48 			if ((ch == TChar('\r')) || (ch == TChar('\n')))
       
    49 				{
       
    50 				lex.UnGet();
       
    51 				break;
       
    52 				}
       
    53 			}
       
    54 		TPtrC8 narrowExeName(lex.MarkedToken());
       
    55 		if (narrowExeName.Length() > 0)
       
    56 			{
       
    57 			TBuf<64> exeName;
       
    58 			exeName.Copy(narrowExeName);
       
    59 			_LIT(KSingleQuote, "'");
       
    60 			scriptFileName.Insert(0, KSingleQuote);
       
    61 			scriptFileName.Append(KSingleQuote);
       
    62 			RProcess process;
       
    63 			User::LeaveIfError(process.Create(exeName, scriptFileName));
       
    64 			TRequestStatus status;
       
    65 			process.Logon(status);
       
    66 			process.Resume();
       
    67 			User::WaitForRequest(status);
       
    68 			User::LeaveIfError(status.Int());
       
    69 			process.Close();
       
    70 			}
       
    71 		}
       
    72 	}
       
    73 
       
    74 GLDEF_C TInt E32Main()
       
    75 	{
       
    76 	__UHEAP_MARK;
       
    77 	TInt err = KErrNoMemory;
       
    78 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    79 	if (cleanup)
       
    80 		{
       
    81 		TRAP(err, RunL());
       
    82 		delete cleanup;
       
    83 		}
       
    84 	__UHEAP_MARKEND;
       
    85 	return err;
       
    86 	}
       
    87