secureswitools/swisistools/source/dumpsis/options.cpp
changeset 4 32704c33136d
equal deleted inserted replaced
-1:000000000000 4:32704c33136d
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <iostream>
       
    20 #include "options.h"
       
    21 #include "utility.h"
       
    22 #include "symbiantypes.h"
       
    23 #include "utility_interface.h"
       
    24 
       
    25 void Options::DisplayError (CmdLineException err)
       
    26 	{
       
    27 	const char* msg;
       
    28 	switch (err)
       
    29 		{
       
    30 		case ECmdLineUnknownOption:
       
    31 			msg = "unknown option specified";
       
    32 			break;
       
    33 		case ECmdLineMissingSIS:
       
    34 			msg = "no SIS file specified";
       
    35 			break;
       
    36 		case ECmdLineMultipleSIS:
       
    37 			msg = "too many SIS files specified";
       
    38 			break;
       
    39 		case ECmdLineNoDirArgument:
       
    40 			msg = "No argument supplied with the -d flag";
       
    41 			break;
       
    42 		case ECmdLineDirIsFile:
       
    43 			msg = "Directory specified with the -d flag is a file";
       
    44 			break;
       
    45 		case ECmdLineNoPermission:
       
    46 			msg = "Permission was denied when using directory specified with -d";
       
    47 			break;
       
    48 		case ECmdLineOtherDirFailure:
       
    49 			msg = "Operation failed when accessing directory specified with -d";
       
    50 			break;
       
    51 		case ECmdLineInvalidDir:
       
    52 			msg = "Directory name specified with -d could not be created, invalid name";
       
    53 			break;
       
    54 		default:
       
    55 			msg = "unknown error";
       
    56 			break;
       
    57 		}
       
    58 	std::cerr << CommandName () << ": Error in command: " << msg << std::endl;
       
    59 	DisplayUsage ();
       
    60 	}
       
    61 
       
    62 void Options::DisplayUsage ()
       
    63 	{
       
    64 	std::cout << "Usage: " << CommandName () << " [-v] [-h] [-x] [-l[-y]][-d directory] filename.sis" << std::endl;
       
    65 	std::cout << "Where:\t-i\tdisplays verbose output" << std::endl;
       
    66 	std::cout << "\t-v\tdisplays version" << std::endl;
       
    67 	std::cout << "\t-h\tdisplays this message" << std::endl;
       
    68 	std::cout << "\t-x\textracts the files" << std::endl;
       
    69 	std::cout << "\t-d\tspecifies where you wish to extract the files to" << std::endl; 
       
    70 	std::cout << "\t-p\tpauses when finishing" << std::endl;
       
    71 	std::cout << "\t-l\tlist the executable against their capablities. " << std::endl;
       
    72 	std::cout << "\t\tAlso verifies capablities with executable header" << std::endl;
       
    73 	std::cout << "\t-y\tlist the executable against their capablities, " << std::endl;
       
    74 	std::cout << "\t\tin the format supported by DumpInstallFileStatus" << std::endl;
       
    75 	std::cout << "\t\tThis option has to be given along with -l option" << std::endl;
       
    76 	
       
    77 	}
       
    78 
       
    79 /**
       
    80 *This is a new function added to provide version information
       
    81 */
       
    82 
       
    83 void Options::DisplayVersion()
       
    84 {
       
    85 	std::cout << "\nDUMPSIS  Version  " << MajorVersion << '.' << MinorVersion << std::endl;
       
    86 	std::cout << "A utility for decompiling Software Installation (SIS) files" << std::endl;
       
    87 	std::cout <<"Copyright (c) Symbian Software Limited 2004-2008. All rights reserved.\n\n" << std::flush;
       
    88 }
       
    89 
       
    90 Options::Options (int argc, wchar_t** argv)
       
    91 	: iVerboseFlag (false),
       
    92 	iVersion (false),
       
    93 	iHelpFlag (false),
       
    94 	iExtractFilesFlag (false),
       
    95 	iPauseOnExit (false),
       
    96 	iList(false),
       
    97 	iCreateECI(false)
       
    98 	{
       
    99 	while (--argc)
       
   100 		{
       
   101 		argv++;
       
   102 		if ((**argv == '-') || (**argv == '/'))
       
   103 			{
       
   104 			const wchar_t* optPtr = *argv;
       
   105 
       
   106 			while (*++optPtr)
       
   107 				{
       
   108 				switch (*optPtr)
       
   109 					{
       
   110 					case 'd':
       
   111 					case 'D':
       
   112 						if (argc > 1)
       
   113 							{
       
   114 							argc--;
       
   115 							iExtractDirectory = *(++argv);
       
   116 							}
       
   117 						else
       
   118 							{
       
   119 							
       
   120 							throw ECmdLineNoDirArgument;
       
   121 
       
   122 							}
       
   123 						break;
       
   124 
       
   125 					case 'h':
       
   126 					case 'H':
       
   127 					case '?':
       
   128 						iHelpFlag = true;
       
   129 						break;
       
   130 					case 'v':
       
   131 					case 'V':
       
   132 						iVersion = true;
       
   133 						break;
       
   134 
       
   135 					case 'p':
       
   136 					case 'P':
       
   137 						iPauseOnExit = true;
       
   138 						break;
       
   139 					case 'i':
       
   140 					case 'I':
       
   141 						iVerboseFlag = true;
       
   142 						break;
       
   143 					case 'x':
       
   144 					case 'X':
       
   145 						iExtractFilesFlag = true;
       
   146 						break;
       
   147 
       
   148 					case 'l':
       
   149 					case 'L':
       
   150 						iList = true;
       
   151 						break;
       
   152 					case 'y':
       
   153 					case 'Y':
       
   154 						iCreateECI = true;
       
   155 						break;
       
   156 
       
   157 			
       
   158 					default:
       
   159 						throw ECmdLineUnknownOption;
       
   160 					}
       
   161 				}
       
   162 			}
       
   163 		else
       
   164 			{
       
   165 			if (iSISFileName != L"")
       
   166 				{
       
   167 				throw ECmdLineMultipleSIS;
       
   168 				}
       
   169 			else
       
   170 				{
       
   171 				iSISFileName = *argv;
       
   172 				}
       
   173 			}
       
   174 		}
       
   175 		
       
   176 	if (iHelpFlag)
       
   177 		{
       
   178 		DisplayUsage ();
       
   179 		if (iSISFileName == L"")	// If we were asked for help then we shouldn't
       
   180 			{				// complain that there's no SIS file specified,
       
   181 			exit (0);		// just exit quietly.
       
   182 			}
       
   183 		}
       
   184 
       
   185 	if (iVersion)
       
   186 		{
       
   187 		DisplayVersion ();
       
   188 		if (iSISFileName == L"")
       
   189 			{
       
   190 			exit (0);		
       
   191 			}
       
   192 		}
       
   193 
       
   194 	if (iSISFileName == L"")
       
   195 		{
       
   196 		throw ECmdLineMissingSIS;	
       
   197 		}
       
   198 	
       
   199 	// Sanity check the directory argument
       
   200 	// If directory name is not provided then
       
   201 	// generate a directory name.
       
   202 	GetExtractDir();
       
   203 	}
       
   204 
       
   205 void Options::GetExtractDir()
       
   206 	{
       
   207 	iExtractDirectory = FixPathDelimiters(iExtractDirectory);
       
   208 
       
   209 	if(iExtractDirectory != L"")
       
   210 		{
       
   211 		return;
       
   212 		}
       
   213 
       
   214 	int pos = iSISFileName.rfind(L".sis");
       
   215 	if(std::wstring::npos == pos)
       
   216 		{
       
   217 		pos = iSISFileName.rfind(L".ctl");
       
   218 		}
       
   219 	
       
   220 	if(std::wstring::npos == pos)
       
   221 		{
       
   222 		iExtractDirectory = L".";
       
   223 		}
       
   224 	else
       
   225 		{
       
   226 		iExtractDirectory = FixPathDelimiters(iSISFileName);
       
   227 		pos = iExtractDirectory.find_last_of(L"/");
       
   228 		if (std::wstring::npos == pos)
       
   229 			{
       
   230 			pos = 0;
       
   231 			}
       
   232 		else
       
   233 			{
       
   234 			pos += 1; // Discard "/"
       
   235 			}
       
   236 		
       
   237 		// Discard the extension
       
   238 		int dirLength = iExtractDirectory.length() - 4 - pos;
       
   239 		iExtractDirectory = iExtractDirectory.substr(pos, dirLength);
       
   240 		}
       
   241 	}
       
   242