1 /*
2  * regexpl - Console Registry Explorer
3  *
4  * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 // ShellCommandHelp.cpp: implementation of the CShellCommandHelp class.
23 //
24 //////////////////////////////////////////////////////////////////////
25 
26 #include "ph.h"
27 #include "ShellCommandHelp.h"
28 
29 #define HELP_CMD			_T("HELP")
30 #define HELP_CMD_SHORT_DESC	HELP_CMD _T(" command provides help information about Registry Explorer commands.\n")
31 
32 //////////////////////////////////////////////////////////////////////
33 // Construction/Destruction
34 //////////////////////////////////////////////////////////////////////
35 
36 CShellCommandHelp::CShellCommandHelp(CShellCommandsLinkedList& rCommandsLinkedList):m_rCommandsLinkedList(rCommandsLinkedList)
37 {
38 }
39 
40 CShellCommandHelp::~CShellCommandHelp()
41 {
42 
43 }
44 
45 BOOL CShellCommandHelp::Match(const TCHAR *pchCommand)
46 {
47 	return _tcsicmp(pchCommand,HELP_CMD) == 0;
48 }
49 
50 int CShellCommandHelp::Execute(CConsole &rConsole, CArgumentParser& rArguments)
51 {
52 	const TCHAR *pchArg = rArguments.GetNextArgument();
53 	CShellCommand *pCommand;
54 	if (pchArg == NULL)
55 	{
56 		POSITION pos = m_rCommandsLinkedList.GetFirstCommandPosition();
57 		while(pos)
58 		{
59 			pCommand = m_rCommandsLinkedList.GetNextCommand(pos);
60 			rConsole.Write(pCommand->GetHelpShortDescriptionString());
61 		}
62 
63 		return 0;
64 	}
65 
66 	if (_tcsicmp(pchArg,_T("GPL")) == 0)
67 	{
68 		HRSRC hrcGPL;
69 		HGLOBAL hGPL;
70 		char *pchGPL;
71 		DWORD dwSize;
72 		if ((hrcGPL = FindResource(NULL, _T("GPL"), _T("LICENSE")))&&
73 			(hGPL = LoadResource(NULL,hrcGPL))&&
74 			(pchGPL = (char *)LockResource(hGPL))&&
75 			(dwSize = SizeofResource(NULL,hrcGPL)))
76 		{
77 			// save last char
78 			char pchSaved[2];
79 			pchSaved[0] = pchGPL[dwSize-1];
80 			pchSaved[1] = 0;
81 
82 			// make null-terminated string
83 			pchGPL[dwSize-1] = 0;
84 
85 			// replace all non-printable chars except CR, LF and HTAB with spaces
86 			for (DWORD i = 0; i < dwSize ; i++)
87 				if ((!isprint(pchGPL[i]))&&(pchGPL[i] != '\r')&&(pchGPL[i] != '\n')&&(pchGPL[i] != '\t'))
88 					pchGPL[i] = ' ';
89 
90 			rConsole.Write(pchGPL);
91 			rConsole.Write(pchSaved);
92 		}
93 		else
94 		{
95 			rConsole.Write(_T("Internal error cannot load GPL.\n"));
96 		}
97 		return 0;
98 	}
99 
100 	while (pchArg)
101 	{
102 		pCommand = m_rCommandsLinkedList.Match(pchArg);
103 		if ((!pCommand)&&((_tcsicmp(pchArg,_T("/?")) == 0)||(_tcsicmp(pchArg,_T("-?")) == 0)))
104 			pCommand = this;
105 
106 		if (pCommand)
107 		{
108 			rConsole.Write(pCommand->GetHelpString());
109 		}
110 		else
111 		{
112 			rConsole.Write(_T("HELP: Unknown command \""));
113 			rConsole.Write(pchArg);
114 			rConsole.Write(_T("\"\n"));
115 		}
116 
117 		pchArg = rArguments.GetNextArgument();
118 	}
119 	return 0;
120 }
121 
122 const TCHAR * CShellCommandHelp::GetHelpString()
123 {
124 	return HELP_CMD_SHORT_DESC
125 			_T("Syntax: ") HELP_CMD _T(" [<COMMAND>] [/?]\n")
126 			_T("    COMMAND    - Command for which help will be displayed.\n")
127 			_T("    /?    - This help.\n\n")
128 			_T("Without parameters, command lists available commands.\n");
129 }
130 
131 const TCHAR * CShellCommandHelp::GetHelpShortDescriptionString()
132 {
133 	return HELP_CMD_SHORT_DESC;
134 }
135