First public contribution.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
24 #include "uloggercommands.h"
25 #include "uloggershared.h"
26 using namespace Ulogger;
28 CCommand::CCommand(MCommandImpl* aCommandImpl)
29 : iCommandImpl(aCommandImpl)
33 EXPORT_C CCommand::~CCommand()
35 iCommandsLookup.Close();
38 EXPORT_C CCommand* CCommand::NewL(MCommandImpl* aCommandImpl)
40 CCommand* obj = CCommand::NewLC(aCommandImpl);
41 CleanupStack::Pop( obj );
45 EXPORT_C CCommand* CCommand::NewLC(MCommandImpl* aCommandImpl)
47 CCommand* obj = new (ELeave) CCommand(aCommandImpl);
48 CleanupStack::PushL( obj );
53 EXPORT_C void CCommand::ConstructL()
57 //fill the lookup table
59 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdPrimaryFilter, ESetPrimaryFilter) ); // -ef
60 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdSecondaryFilter, ESetSecondaryFilter) ); // -es
61 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdSecondaryFilterToggle, EEnableSecondaryFiltering) ); // -eS
62 iCommandsLookup.Append( TCommandLookup(&KCmdDisable, &KCmdSecondaryFilterToggle, EDisableSecondaryFiltering) ); // -dS
63 iCommandsLookup.Append( TCommandLookup(&KCmdDisable, &KCmdPluginConfigurations, ERemovePluginSettings) ); // -dc
64 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdOutputPlugin, EGetInstalledPlugins) ); // -lo
65 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdPluginConfigurations, EGetPluginSettings) ); // -lc
66 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdPrimaryFilter, EGetPrimaryFilters) ); // -lf
67 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdSecondaryFilter, EGetSecondaryFilters) ); // -ls
68 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdSecondaryFilterToggle, EGetSecondaryFiltering) ); // -lS
69 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdBuffer, EResizeTraceBuffer) ); // -eb
70 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdBuffer, EGetTraceBufferSize) ); // -lb
71 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdBufferMode, ESetBufferMode) ); // -em
72 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdBufferMode, EGetBufferMode) ); // -lm
73 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdNotification, ESetDataNotificationSize) );// -en
74 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdNotification, EGetDataNotificationSize) ); // -ln
75 iCommandsLookup.Append( TCommandLookup(&KCmdDisable, &KCmdPrimaryFilter, ERemovePrimaryFilter) ); // -df
76 iCommandsLookup.Append( TCommandLookup(&KCmdDisable, &KCmdSecondaryFilter, ERemoveSecondaryFilter) ); // -ds
77 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdPluginConfigurations, ESetPluginSettings) ); // -ec
78 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdOutputPlugin, ESetActivePlugin) ); // -eo
79 iCommandsLookup.Append( TCommandLookup(&KCmdEnable, &KCmdInputPlugin, ESetActiveInputPlugin) ); // -ei
80 iCommandsLookup.Append( TCommandLookup(&KCmdDisable, &KCmdInputPlugin, EDeactivateInputPlugin) ); // -di
81 iCommandsLookup.Append( TCommandLookup(&KCmdList, &KCmdInputPlugin, EGetInputPlugins) ); // -li
82 //only main command matters
83 iCommandsLookup.Append( TCommandLookup(&KCmdStart, &KNullDesC, EStart) ); // -r
84 iCommandsLookup.Append( TCommandLookup(&KCmdStop, &KNullDesC, EStop) ); // -q
85 iCommandsLookup.Append( TCommandLookup(&KCmdRestart, &KNullDesC, ERestart) ); // -t
86 iCommandsLookup.Append( TCommandLookup(&KCmdOutputPlugin, &KNullDesC, EGetActivePlugin) ); // -o
87 iCommandsLookup.Append( TCommandLookup(&KCmdInputPlugin, &KNullDesC, EGetActiveInputPlugin) ); // -i
88 iCommandsLookup.Append( TCommandLookup(&KCmdHelp, &KNullDesC, EHelp) ); // -h
89 iCommandsLookup.Append( TCommandLookup(&KCmdMan, &KNullDesC, EManPage) ); // -H
90 iCommandsLookup.Append( TCommandLookup(&KCmdVersion, &KNullDesC, EVersion) ); // -version
95 This function parses commands given as a parameter and generates TCommand value.
96 The function is responsible also for triggering event for MCommandImpl
98 @param aArgs array of arguments to parse
99 @return KErrNone if commands were parsed successfully and event was triggered, system wide error code otherwise
101 EXPORT_C TInt CCommand::HandleCommandL(const RArray<TPtrC>& aArgs)
103 TBool optionsConsumed = EFalse;
104 RArray<TPtrC> commands;
105 CleanupClosePushL( commands );
107 CleanupClosePushL(command);
109 for(TInt k=0; k<aArgs.Count(); ++k) //calculate max command length
110 maxSize += aArgs[k].Length();
111 User::LeaveIfError(command.Create(maxSize));
113 for(TInt i=0; i<aArgs.Count(); ++i)
115 TPtrC argPtr(aArgs[i]);
117 //the 'version' command is the special case
118 if(!argPtr.Mid(1).Compare(KCmdVersion))
120 TPtrC ptr(argPtr.Mid(1));
121 commands.AppendL(ptr);
125 //check if we have to parse combined commands (like -ca)
126 //other arguments are added to new array for further processing
128 cmdChecker.Copy( argPtr.Mid(0,1) );
129 if(!cmdChecker.Compare(KCmdIndicator) && !optionsConsumed)
131 //if there is option 'v' (verbose), this option shoul be removed
133 //command data format: 'command param1 param2 paramN'
135 //command: ia string with commands letters, for example: 'lf', 'esv'
136 //paramN: is parameter string, for example 'uloggerserialplugin'
137 //array example: 'ec uloggerserialplugin output_port 3'
139 for(TInt k=1; k<argPtr.Length(); ++k)
141 TPtrC ptr(argPtr.Mid(k,1));
142 if(!ptr.Compare(KCmdVerboseMode))
145 command.AppendFormat(_L("%S"), &ptr);
147 //put command as a first element in array
148 if(commands.Count() > 0)
151 User::LeaveIfError(commands.Insert(command, 0));
154 commands.AppendL(command);
157 optionsConsumed = ETrue;
160 commands.AppendL(argPtr);
163 TInt errCode = PrepareCommandL( commands );
164 CleanupStack::PopAndDestroy(2); //command, commands
169 /** This method uses lookup table to translate user readable commands into TCommand value.
170 @param aArgs Array of arguments.
171 @return KErrNone if command was preapared correctly and system wide error code otherwise
173 TInt CCommand::PrepareCommandL(RArray<TPtrC>& aArgs)
176 TInt lookupArraySize = iCommandsLookup.Count();
178 if(aArgs.Count() == 0)
179 return iCommandImpl->DoCommandL(EUnknown, aArgs);
181 //first elements contains command letters
183 if(cmd.Length() <= KMaxNumberOfLettersInCommand) //we want precise command
185 //check two arguments - 1st pass
186 for(i=0; i<lookupArraySize; ++i)
188 if(cmd.Length() == 2)
189 if(!iCommandsLookup[i].iMainCommand->Compare(cmd.Mid(0,1)))
190 if(!iCommandsLookup[i].iSubCommand->Compare(cmd.Mid(1,1)))
193 return iCommandImpl->DoCommandL(iCommandsLookup[i].iCommand, aArgs);
197 //check two arguments - 2nd pass
198 for(i=0; i<lookupArraySize; ++i)
201 if(cmd.Length() == 1)
202 if(!iCommandsLookup[i].iMainCommand->Compare(cmd.Mid(0,1)))
204 //make sure that none of the 'two letters commands' will not be called
205 if(iCommandsLookup[i].iSubCommand->Compare(KNullDesC))
208 return iCommandImpl->DoCommandL(iCommandsLookup[i].iCommand, aArgs);
213 return iCommandImpl->DoCommandL(EUnknown, aArgs);