summaryrefslogtreecommitdiffhomepage
path: root/windows/winfw/src/extras/cli/cli.cpp
blob: a1758df8eea53e2cffbc1465ec1de93494ba8301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// cli.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "util.h"
#include "filterengineprovider.h"
#include "modules/imodule.h"
#include "modules/list.h"
#include "modules/monitor.h"
#include "modules/winfw.h"
#include "libcommon/string.h"
#include <iostream>
#include <conio.h>

// Should set key comparison operator to compare lowercase strings but meh.
std::map<std::wstring, std::unique_ptr<modules::IModule> > g_modules;

void OutputConsole(const std::wstring &str)
{
	std::wcout << str.c_str() << std::endl;
}

void InitializeFilterEngine()
{
	FilterEngineProvider::Instance().set(std::move(wfp::FilterEngine::DynamicSession()));
}

void InitializeModules()
{
	auto list = std::make_unique<modules::List>(OutputConsole);
	g_modules.insert(std::make_pair(common::string::Lower(list->name()), std::move(list)));

	auto monitor = std::make_unique<modules::Monitor>(OutputConsole);
	g_modules.insert(std::make_pair(common::string::Lower(monitor->name()), std::move(monitor)));

	auto winfw = std::make_unique<modules::WinFw>(OutputConsole);
	g_modules.insert(std::make_pair(common::string::Lower(winfw->name()), std::move(winfw)));
}

void ProcessHelp(const std::wstring &request)
{
	auto tokens = common::string::Tokenize(request, L" ");

	if (tokens.empty())
	{
		std::wcout << L"Unable to interpret request." << std::endl;
		return;
	}

	if (tokens.size() == 1)
	{
		PropertyList list;
		
		for (auto &module : g_modules)
		{
			list.add(common::string::Lower(module.second->name()), module.second->description());
		}

		list.add(L"help", L"List available modules.");
		list.add(L"help /module/", L"Show module specific help.");
		list.add(L"reset", L"Reset the Filter Engine session.");
		list.add(L"quit", L"Exit the application.");

		PrettyPrintOptions options;

		options.indent = 0;
		options.useSeparator = false;

		PrettyPrintProperties(OutputConsole, options, list);

		return;
	}

	if (tokens.size() != 2)
	{
		std::wcout << L"Unable to interpret request." << std::endl;
		return;
	}

	auto wanted = common::string::Lower(tokens[1]);
	auto found = g_modules.find(wanted);

	if (found == g_modules.end())
	{
		std::wcout << L"No such module: " << wanted << L"." << std::endl;
		return;
	}

	auto moduleCommands = found->second->commands();

	PrettyPrintOptions options;

	options.indent = 0;
	options.useSeparator = false;

	PrettyPrintProperties(OutputConsole, options, moduleCommands);
}

void ProcessRequest(const std::wstring &request)
{
	auto tokens = common::string::Tokenize(request, L" ");

	if (tokens.empty())
	{
		std::wcout << L"Unable to interpret request." << std::endl;
		return;
	}

	auto wanted = common::string::Lower(tokens[0]);
	auto found = g_modules.find(wanted);

	if (found == g_modules.end())
	{
		std::wcout << L"No such module: " << wanted << L"." << std::endl;
		return;
	}

	tokens.erase(tokens.begin());

	found->second->handleRequest(tokens);
}

int main(int, wchar_t **)
{
	InitializeFilterEngine();
	InitializeModules();

	for (;;)
	{
		std::wcout << L"wfp> ";

		std::wstring request;
		std::getline(std::wcin, request);

		if (0 == _wcsicmp(request.c_str(), L"quit"))
		{
			break;
		}

		if (0 == _wcsicmp(request.c_str(), L"reset"))
		{
			InitializeFilterEngine();
			std::wcout << std::endl;

			continue;
		}

		if (0 == _wcsnicmp(request.c_str(), L"help", 4))
		{
			ProcessHelp(request);
			std::wcout << std::endl;

			continue;
		}

		try
		{
			ProcessRequest(request);
		}
		catch (std::exception &error)
		{
			std::cout << "Error: " << error.what() << std::endl;
		}
		catch (...)
		{
			std::cout << "Unknown error, caught exception!" << std::endl;
		}

		std::wcout << std::endl;
	}

	return 0;
}