commands/uidinfo/uidinfo.cpp
changeset 0 7f656887cf89
child 69 849a0b46c767
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // uidinfo.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 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 #include <fshell/ioutils.h>
       
    14 #include <apgcli.h>
       
    15 #include <fshell/ltkutils.h>
       
    16 #include <fshell/qr3dll.h>
       
    17 
       
    18 using namespace IoUtils;
       
    19 
       
    20 class CCmdUidInfo : public CCommandBase
       
    21 	{
       
    22 public:
       
    23 	static CCommandBase* NewLC();
       
    24 	~CCmdUidInfo();
       
    25 private:
       
    26 	CCmdUidInfo();
       
    27 	TInt PrintUid(TUint aUid);
       
    28 private: // From CCommandBase.
       
    29 	virtual const TDesC& Name() const;
       
    30 	virtual void DoRunL();
       
    31 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    32 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    33 private:
       
    34 	TUint iUid;
       
    35 	TBool iStdin;
       
    36 	RBuf iBuf;
       
    37 	};
       
    38 
       
    39 CCommandBase* CCmdUidInfo::NewLC()
       
    40 	{
       
    41 	CCmdUidInfo* self = new(ELeave) CCmdUidInfo();
       
    42 	CleanupStack::PushL(self);
       
    43 	self->BaseConstructL();
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 CCmdUidInfo::~CCmdUidInfo()
       
    48 	{
       
    49 	iBuf.Close();
       
    50 	}
       
    51 
       
    52 CCmdUidInfo::CCmdUidInfo()
       
    53 	{
       
    54 	}
       
    55 
       
    56 const TDesC& CCmdUidInfo::Name() const
       
    57 	{
       
    58 	_LIT(KName, "uidinfo");	
       
    59 	return KName;
       
    60 	}
       
    61 
       
    62 void CCmdUidInfo::ArgumentsL(RCommandArgumentList& aArguments)
       
    63 	{
       
    64 	aArguments.AppendUintL(iUid, _L("uid"));
       
    65 	}
       
    66 
       
    67 void CCmdUidInfo::OptionsL(RCommandOptionList& aOptions)
       
    68 	{
       
    69 	aOptions.AppendBoolL(iStdin, _L("stdin"));
       
    70 	}
       
    71 
       
    72 
       
    73 EXE_BOILER_PLATE(CCmdUidInfo)
       
    74 
       
    75 void CCmdUidInfo::DoRunL()
       
    76 	{
       
    77 	if (iStdin)
       
    78 		{
       
    79 		Stdin().SetReadModeL(RIoReadHandle::ELine);
       
    80 		TBuf<512> lineBuf; // Note, the code below doesn't properly handle the situation where a given line of input in longer than this buffer.
       
    81 		while (ETrue)
       
    82 			{
       
    83 			TInt err = Stdin().Read(lineBuf);
       
    84 			if (err == KErrEof)
       
    85 				{
       
    86 				break;
       
    87 				}
       
    88 			else if (err)
       
    89 				{
       
    90 				User::Leave(err);
       
    91 				}
       
    92 			if (lineBuf.Length() < 2) continue;
       
    93 			// In this case we assume hex even without an 0x
       
    94 			if (lineBuf.Left(2).CompareF(_L("0x")) != 0) lineBuf.Insert(0, _L("0x"));
       
    95 			TLex lex(lineBuf);
       
    96 			TUint uid = 0;
       
    97 			TRAP(err, uid = LtkUtils::HexLexL(lex));
       
    98 			if (err)
       
    99 				{
       
   100 				PrintWarning(_L("Failed to find a uid on line '%S'"), &lineBuf);
       
   101 				continue;
       
   102 				}
       
   103 			err = PrintUid(uid);
       
   104 			if (err)
       
   105 				{
       
   106 				PrintWarning(_L("Couldn't find exe for uid 0x%08x, err = %d\r\n"), uid, err);
       
   107 				}
       
   108 			}
       
   109 		}
       
   110 	else if (!iArguments.IsPresent(&iUid))
       
   111 		{
       
   112 		LeaveIfErr(KErrArgument, _L("You must specify either a uid or the --stdin option."));
       
   113 		}
       
   114 	else
       
   115 		{
       
   116 		LeaveIfErr(PrintUid(iUid), _L("Couldn't find exe for uid 0x%08x"), iUid);
       
   117 		}
       
   118 	}
       
   119 
       
   120 TInt CCmdUidInfo::PrintUid(TUint aUid)
       
   121 	{
       
   122 	TInt err = ExeNameForSid(aUid, iBuf);
       
   123 	if (err) return err;
       
   124 	Printf(_L("0x%x %S\r\n"), aUid, &iBuf);
       
   125 	return KErrNone;
       
   126 	}