cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.cpp
changeset 31 c0e7917aa107
parent 11 9d767430696e
parent 29 ece3df019add
--- a/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.cpp	Sat Dec 05 21:34:52 2009 +0000
+++ b/cryptoservices/filebasedcertificateandkeystores/test/certtool/certtool.cpp	Sat Dec 05 21:41:51 2009 +0000
@@ -36,6 +36,7 @@
 _LIT(KDone, "Press any key to continue... \n");
 
 // CertTool command line parameters
+
 _LIT(KList, "-list");
 _LIT(KListShort, "-l");
 
@@ -85,9 +86,21 @@
 
 _LIT(KUids, "-uids");
 
+_LIT(KDeletable, "-deletable");
+_LIT(KDeletableShort, "-del");
+
 const TInt KMaxArgs = 10;
 
 /**
+ * Certtool can operate in the following modes.
+**/
+enum OperationMode {
+Interactive,
+NonInteractive
+};
+
+
+/**
  * Displays tool name and copy-right informations.
  */
 LOCAL_D void BoilerPlateL(CConsoleBase* console) 
@@ -145,276 +158,333 @@
 	}
 	
 /**
+ * Returns the mode in which the tool would operate. If the command is invoked 
+ * with 2 parameters(certool inputFile outputFile), the tool works in non-interactive
+ * mode else the interactive mode is chosen.
+ */
+
+LOCAL_D OperationMode ModeOfOperationL(const CCommandLineArguments& aCmdArgs, RFs& aFs, RFile& aFile)
+    {
+    OperationMode mode = Interactive;
+    if (KeyToolUtils::DoesFileExistsL(aFs,aCmdArgs.Arg(1)))
+        {
+		mode = NonInteractive;
+        TInt error = aFile.Open(aFs, aCmdArgs.Arg(1), EFileRead|EFileShareAny);
+        aFile.Close();
+        
+        TInt error1 = aFile.Replace(aFs, aCmdArgs.Arg(2), EFileWrite|EFileShareExclusive);
+        // If the input file doesn't exist or not able to create outputfile
+        // switch to Interactive mode
+        if (error != KErrNone || error1 != KErrNone)
+            {
+            CleanupStack::PopAndDestroy(&aFile);
+            mode = Interactive;
+            }   
+                    
+         }
+    return mode;
+    }
+
+/**
+ * The main parsing logic. Same for interactive and non-interactive modes.
+ */
+LOCAL_D void ParseAndHandleCommandL(CArrayFixFlat<TPtrC>& aArgs, CCertToolController& aController)
+    {
+    CKeyToolParameters* params = CKeyToolParameters::NewLC();
+            
+    TInt command = -1; 
+    TInt i = -1;
+           
+    TInt argsCount = aArgs.Count();
+    while (i < (argsCount-1))
+        {
+        i++;
+        if ((aArgs.At(i).CompareF(KDetails)==0)|| (aArgs.At(i).Compare(KDetailsShort)==0))
+                    {
+                    params->iIsDetailed = ETrue;
+                    continue;
+                    }
+        
+                if (aArgs.At(i).CompareF(KPageWise)==0 || (aArgs.At(i).Compare(KPageWiseShort)==0))
+                    {
+                    i++;
+                    params->iPageWise = ETrue;
+                    continue;
+                    }
+
+                if (aArgs.At(i).Compare(KRemoveKeyAlso)==0)
+                    {
+                    params->iRemoveKey = ETrue;
+                    continue;
+                    }
+
+                if (aArgs.At(i).CompareF(KApps)==0)
+                    {
+                    i++;
+                    RArray<TUid> apps;
+                    TInt k = 0;
+                    for (k = i; k < argsCount; k++)
+                        {               
+                        if (aArgs.At(k).Find(_L("-")) == KErrNotFound)
+                            {
+                            TUint uid;
+                            if (aArgs.At(k).CompareF(KSWInstall)==0)
+                                {
+                                uid = swinstalluid;
+                                }
+                            else
+                                {
+                                if (aArgs.At(k).CompareF(KSWInstallOCSP)==0)
+                                    {
+                                    uid = swinstallocspuid;
+                                    }
+                                else
+                                    {
+                                    if (aArgs.At(k).CompareF(KMidletInstall)==0)
+                                        {
+                                        uid = midletinstalluid;
+                                        }
+                                    else
+                                        {
+                                        if (aArgs.At(k).CompareF(KTls)==0)
+                                            {
+                                            uid = tlsuid;
+                                            }
+                                        else
+                                            {
+                                            // no more valid apps, break cycle
+                                            break;
+                                            }
+                                        }
+                                    }
+                                }
+                            apps.Append(TUid::Uid(uid));                        
+                            }
+                        else 
+                            {
+                            // We parsed all UIDs, break the cycle and go on!
+                            break;
+                            }
+                        }
+                    i = k-1;
+                    params->iUIDs = apps; // We pass on ownership
+                    params->iIsDetailed = ETrue;
+                    continue;           
+                    }
+            
+                if (aArgs.At(i).CompareF(KUids)==0)
+                    {
+                    i++;
+                    RArray<TUid> uids;
+                    TInt k = 0;
+                    for (k = i; k < argsCount; k++)
+                        {               
+                        if (aArgs.At(k).Left(2) == _L("0x"))
+                            {
+                            TLex lex(aArgs.At(k).Mid(2));       
+                            TUint uid =0;
+                            TInt err = lex.Val(uid, EHex);
+                            if (err == KErrNone)
+                                {
+                                params->iUIDs.Append(TUid::Uid(uid));                       
+                                }            
+                            }
+                        else 
+                            {
+                            // We parsed all UIDs, break the cycle and go on!
+                            break;
+                            }
+                        }
+                    i = k-1;
+                    params->iIsDetailed = ETrue;
+                    continue;           
+                    }
+                
+                TDesC& cmd = aArgs.At(i);
+                if (cmd.CompareF(KLabel) == 0 || 
+                    cmd.CompareF(KPrivate) == 0 || cmd.CompareF(KStore) == 0 ||
+                    cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0 ||
+                    cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 ||
+                    cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0)
+                    {
+                    i++;
+                    if (i >= argsCount || aArgs.At(i)[0] == '-')
+                        {
+                        i = argsCount;
+                        command = CertToolDefController::KUsageCommand;
+                        }
+                    else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
+                        {
+                        params->iDefault = aArgs.At(i).AllocL();
+                        i = argsCount;  
+                        }
+                    else if (cmd.CompareF(KLabel) == 0)
+                        {
+                        params->iLabel = aArgs.At(i).AllocL();
+                        }
+                    else if (cmd.CompareF(KPrivate) == 0)
+                        {
+                        params->iPrivate = aArgs.At(i).AllocL();
+                        }
+                    else if (cmd.CompareF(KStore) == 0)
+                        {
+                        TLex parser(aArgs.At(i));
+                        TInt err = parser.Val(params->iCertstoreIndex);
+                        params->iIsDetailed = ETrue;
+                        }
+                    else if (cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0)
+                        {
+                        params->iIsDetailed = ETrue;
+                        params->iOwnerType = aArgs.At(i).AllocL();
+                        }
+                    else if (cmd.CompareF(KDeletable) == 0 || cmd.CompareF(KDeletableShort) == 0)
+                        {
+                        params->iIsDetailed = ETrue;
+                        params->iIsDeletable = aArgs.At(i).AllocL();
+                        }
+                    continue;
+                    }
+        
+                if (VerifyCommand(aArgs.At(i), command, i))
+                    {
+                    continue;
+                    }
+
+                    
+                if (i!=0) 
+                    {
+                    if (aArgs.At(i)[0] == '-')
+                        {
+                        i = argsCount;
+                        command = CertToolDefController::KUsageCommand;
+                        continue;
+                        }
+                    delete params->iDefault;
+                    params->iDefault = NULL;
+                    params->iDefault = aArgs.At(i).AllocL();
+                    params->iIsDetailed = ETrue;            
+                    }
+                } 
+             
+             
+            if (command != -1)
+                {
+                TRAP_IGNORE(aController.HandleCommandL(command, params));
+                }
+            else 
+                {
+                aController.HandleCommandL(CertToolDefController::KUsageCommand, params);       
+                }
+            CleanupStack::PopAndDestroy(params);
+            }
+
+
+/**
+ * Parsing the command for non-interactive mode.
+ */
+
+LOCAL_D void ParseCommandInNonInteractiveModeL(RFile& aFile, const CCommandLineArguments& aCmdArgs)
+    {
+  
+    KeyToolUtils::SetFile(&aFile);
+    
+    CKeytoolFileView* view(0); 
+    view = CKeytoolFileView::NewLC(aCmdArgs.Arg(1));
+    TInt cmdCount = view->SplitFileInputToArrayL();
+    
+    //For every command, parse and handle.
+    for (TInt j = 0; j < cmdCount; j++)
+        {
+        CCertToolController* controller = CCertToolController::NewLC(*view);
+        
+        CArrayFixFlat<TPtrC>* args = view->ReadArrayArgumentsLC(j);
+        ParseAndHandleCommandL(*args, *controller);
+        
+        CleanupStack::PopAndDestroy(2, controller);
+        }
+        
+    
+    CleanupStack::PopAndDestroy(view);
+    
+    }
+
+
+/**
+ * Parsing the command for interactive mode.
+ */
+
+
+LOCAL_D void ParseCommandInInteractiveModeL(CConsoleBase& aConsole, const CCommandLineArguments& aCmdArgs)
+    {
+    CArrayFixFlat<TPtrC>* args = new (ELeave) CArrayFixFlat<TPtrC> (10);
+    CleanupStack::PushL(args);
+    CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(aConsole);
+    CCertToolController* controller = CCertToolController::NewLC(*view);
+
+    TInt cmdArgsCount = aCmdArgs.Count();
+    
+    KeyToolUtils::SetConsole(&aConsole);
+    BoilerPlateL(&aConsole);
+    
+    for (TInt i = 0; i < cmdArgsCount; i++)
+        {
+        args->AppendL(aCmdArgs.Arg(i));
+        }
+    
+    //Interactive mode can handle only one command at a time.
+    ParseAndHandleCommandL(*args, *controller); 
+    
+    // We are done!
+    aConsole.Printf(KNewLine);
+    aConsole.Printf(KDone);
+    aConsole.Getch(); 
+    
+    CleanupStack::PopAndDestroy(3, args); // controller, view, args
+    
+    }
+
+/**
  * Parses the command line and given control to the handler to deal with the request.
  */
 LOCAL_D void DoMainL() 
 	{
-	TBool interactiveMode = ETrue;
-	
+
 	RFs fs;
 	User::LeaveIfError(fs.Connect());
 	CleanupClosePushL(fs);
+	RFile file;
 	
 	CConsoleBase* console = Console::NewL(KShortName, TSize(KConsFullScreen, KConsFullScreen));
 	CleanupStack::PushL(console);
 	CCommandLineArguments* cmdArgs = CCommandLineArguments::NewLC();
 	TInt cmdArgsCount = cmdArgs->Count();
 	
-	RFile file;
-	// command: certtool inputfile outputfile
+	
+	OperationMode currentMode = Interactive; //Interactive by default.
+	
+	// Determine the mode of operation as either interactive or non-interactive.
 	if (cmdArgsCount == 3)
 		{
-		if (KeyToolUtils::DoesFileExistsL(fs,cmdArgs->Arg(1)))
-		  	{
-		  	interactiveMode = EFalse;
-		  	TInt error = file.Open(fs, cmdArgs->Arg(1), EFileRead|EFileShareAny);
-		  	file.Close();
-		  	TInt error1 = file.Replace(fs, cmdArgs->Arg(2), EFileWrite|EFileShareExclusive);
-		  	CleanupClosePushL(file);
-		  	// If the input file doesn't exist or not able to create outputfile
-		  	// switch to Interactive mode
-		  	if (error != KErrNone || error1 != KErrNone)
-		   		{
-			   	CleanupStack::PopAndDestroy(&file);
-			   	interactiveMode = ETrue;
-			  	}	
-				
-			}
+		currentMode = ModeOfOperationL(*cmdArgs, fs, file);
 		}
 		
-	CKeytoolConsoleView* view = CKeytoolConsoleView::NewLC(*console);
-	CCertToolController* controller = CCertToolController::NewLC(*view);
-	CArrayFixFlat<TPtrC>* args = new (ELeave) CArrayFixFlat<TPtrC> (10);
-	CleanupStack::PushL(args);
-	CKeytoolFileView* view1 = NULL;
-
-	TInt cmdCount = 0;
-	if (interactiveMode)
-		{
-		KeyToolUtils::SetConsole(console);
-		BoilerPlateL(console);
-		for (TInt i = 0; i < cmdArgsCount; i++)
-			{
-			args->AppendL(cmdArgs->Arg(i));
-			}
-		// In Interactive mode by default we can execute 1 command only.
-		cmdCount = 1;
-		}
-	else
-		{
-		CleanupStack::PopAndDestroy(3, view); // args, controller, view
-		KeyToolUtils::SetFile(&file);
-		view1 = CKeytoolFileView::NewLC(cmdArgs->Arg(1));
-		cmdCount = view1->SplitFileInputToArrayL();
-		}	
-	
-	for (TInt j = 0; j < cmdCount; j++)
-		{
-		if (!interactiveMode)
-			{
-			controller = CCertToolController::NewLC(*view1);
-			args = view1->ReadArrayArgumentsLC(j);
-			}
-
-		CKeyToolParameters* params = CKeyToolParameters::NewLC();
-		
-		TInt command = -1; 
-		TInt i = -1;
-		
-		TInt argsCount = args->Count();
-		while (i < (argsCount-1))
-		 	{
-		 	i++;
-			if ((args->At(i).CompareF(KDetails)==0)|| (args->At(i).Compare(KDetailsShort)==0))
-				{
-				params->iIsDetailed = ETrue;
-				continue;
-				}
-	
-			if (args->At(i).CompareF(KPageWise)==0 || (args->At(i).Compare(KPageWiseShort)==0))
-				{
-				i++;
-				params->iPageWise = ETrue;
-				continue;
-				}
-
-			if (args->At(i).Compare(KRemoveKeyAlso)==0)
-				{
-				params->iRemoveKey = ETrue;
-				continue;
-				}
+	switch(currentMode)
+	    {
+	    case Interactive:
+	        ParseCommandInInteractiveModeL(*console, *cmdArgs);
+	        break;
+	        
+	    case NonInteractive:
+	        //file refers to the output file name.
+	        CleanupClosePushL(file);
+	        ParseCommandInNonInteractiveModeL(file, *cmdArgs);
+	        CleanupStack::PopAndDestroy(&file);
+	        break;
+	    }
 
-			if (args->At(i).CompareF(KApps)==0)
-				{
-				i++;
-				RArray<TUid> apps;
-				TInt k = 0;
-				for (k = i; k < argsCount; k++)
-					{				
-					if (args->At(k).Find(_L("-")) == KErrNotFound)
-						{
-						TUint uid;
-						if (args->At(k).CompareF(KSWInstall)==0)
-							{
-							uid = swinstalluid;
-							}
-						else
-							{
-							if (args->At(k).CompareF(KSWInstallOCSP)==0)
-								{
-								uid = swinstallocspuid;
-								}
-							else
-								{
-								if (args->At(k).CompareF(KMidletInstall)==0)
-									{
-									uid = midletinstalluid;
-									}
-								else
-									{
-									if (args->At(k).CompareF(KTls)==0)
-										{
-										uid = tlsuid;
-										}
-									else
-										{
-										// no more valid apps, break cycle
-										break;
-										}
-									}
-								}
-							}
-						apps.Append(TUid::Uid(uid));						
-						}
-					else 
-						{
-						// We parsed all UIDs, break the cycle and go on!
-						break;
-						}
-					}
-				i = k-1;
-				params->iUIDs = apps; // We pass on ownership
-				params->iIsDetailed = ETrue;
-				continue;			
-				}
-		
-			if (args->At(i).CompareF(KUids)==0)
-				{
-				i++;
-				RArray<TUid> uids;
-				TInt k = 0;
-				for (k = i; k < argsCount; k++)
-					{				
-					if (args->At(k).Left(2) == _L("0x"))
-						{
-						TLex lex(args->At(k).Mid(2));		
-						TUint uid =0;
-						TInt err = lex.Val(uid, EHex);
-						if (err == KErrNone)
-							{
-							params->iUIDs.Append(TUid::Uid(uid));						
-							}			 
-						}
-					else 
-						{
-						// We parsed all UIDs, break the cycle and go on!
-						break;
-						}
-					}
-				i = k-1;
-				params->iIsDetailed = ETrue;
-				continue;			
-				}
-			
-			TDesC& cmd = args->At(i);
-			if (cmd.CompareF(KLabel) == 0 || 
-			    cmd.CompareF(KPrivate) == 0 || cmd.CompareF(KStore) == 0 ||
-			    cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0 ||
-			    cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0 )
-				{
-				i++;
-				if (i >= argsCount || args->At(i)[0] == '-')
-					{
-					i = argsCount;
-					command = CertToolDefController::KUsageCommand;
-					}
-				else if (cmd.CompareF(KHelp) == 0 || cmd.Compare(KHelpShort) == 0)
-					{
-					params->iDefault = args->At(i).AllocL();
-					i = argsCount;	
-					}
-				else if (cmd.CompareF(KLabel) == 0)
-					{
-					params->iLabel = args->At(i).AllocL();
-					}
-				else if (cmd.CompareF(KPrivate) == 0)
-					{
-					params->iPrivate = args->At(i).AllocL();
-					}
-				else if (cmd.CompareF(KStore) == 0)
-					{
-					TLex parser(args->At(i));
-					TInt err = parser.Val(params->iCertstoreIndex);
-					params->iIsDetailed = ETrue;
-					}
-				else if (cmd.CompareF(KOwnerType) == 0 || cmd.Compare(KOwnerTypeShort) == 0)
-					{
-					params->iIsDetailed = ETrue;
-					params->iOwnerType = args->At(i).AllocL();
-					}
-				else
-					{
-					// no action required	
-					}
-				continue;
-				}
-	
-			if (VerifyCommand(args->At(i), command, i))
-				{
-				continue;
-				}
+	CleanupStack::PopAndDestroy(3, &fs);
+	}
 
-				
-			if (i!=0) 
-				{
-				if (args->At(i)[0] == '-')
-					{
-					i = argsCount;
-					command = CertToolDefController::KUsageCommand;
-					continue;
-					}
-				delete params->iDefault;
-				params->iDefault = NULL;
-				params->iDefault = args->At(i).AllocL();
-				params->iIsDetailed = ETrue;			
-				}
-		 	} 
-		 
-		 
-		if (command != -1)
-			{
-			TRAP_IGNORE(controller->HandleCommandL(command, params));
-			}
-		else 
-			{
-			controller->HandleCommandL(CertToolDefController::KUsageCommand, params);		
-			}
-		CleanupStack::PopAndDestroy(3, controller); // params, args, controller
-		}
-	if (interactiveMode)
-		{
-		CleanupStack::PopAndDestroy(view);
-		// We are done!
-		console->Printf(KNewLine);
-		console->Printf(KDone);
-		console->Getch();	
-		}
-	else
-		{
-		CleanupStack::PopAndDestroy(2, &file); //view1 and file
-		}
-	CleanupStack::PopAndDestroy(3, &fs); //cmdArgs, console, fs
-	}
 	
 
 GLDEF_C TInt E32Main()         // main function called by E32