1
// CommandParser.cc for Fluxbox - an X11 Window manager
2
// Copyright (c) 2003 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
3
// and Simon Bowden (rathnor at users.sourceforge.net)
5
// Permission is hereby granted, free of charge, to any person obtaining a
6
// copy of this software and associated documentation files (the "Software"),
7
// to deal in the Software without restriction, including without limitation
8
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
// and/or sell copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following conditions:
12
// The above copyright notice and this permission notice shall be included in
13
// all copies or substantial portions of the Software.
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
// DEALINGS IN THE SOFTWARE.
23
// $Id: CommandParser.cc 3910 2005-03-10 16:51:24Z mathias $
25
#include "CommandParser.hh"
26
#include "FbTk/StringUtil.hh"
32
using FbTk::StringUtil::removeFirstWhitespace;
33
using FbTk::StringUtil::toLower;
35
CommandFactory::CommandFactory() {
39
CommandFactory::~CommandFactory() {
40
// remove all associations with this factory
41
CommandParser::instance().removeAssociation(*this);
44
void CommandFactory::addCommand(const string &command_name) {
45
CommandParser::instance().associateCommand(command_name, *this);
48
CommandParser &CommandParser::instance() {
49
static CommandParser singleton;
53
FbTk::Command *CommandParser::parseLine(const string &line) {
55
// parse arguments and command
56
string command = line;
58
string::size_type first_pos = removeFirstWhitespace(command);
59
FbTk::StringUtil::removeTrailingWhitespace(command);
60
string::size_type second_pos = command.find_first_of(" \t", first_pos);
61
if (second_pos != string::npos) {
62
// ok we have arguments, parsing them here
63
arguments = command.substr(second_pos);
64
removeFirstWhitespace(arguments);
65
command.erase(second_pos); // remove argument from command
68
// now we have parsed command and arguments
69
command = toLower(command);
71
// we didn't find any matching command in default commands,
72
// so we search in the command creators modules for a
73
// matching command string
74
return toCommand(command, arguments);
78
FbTk::Command *CommandParser::toCommand(const std::string &command_str, const std::string &arguments) {
79
if (m_commandfactorys[command_str] != 0)
80
return m_commandfactorys[command_str]->stringToCommand(command_str, arguments);
85
void CommandParser::associateCommand(const std::string &command, CommandFactory &factory) {
86
// we shouldnt override other commands
87
if (m_commandfactorys[command] != 0)
90
m_commandfactorys[command] = &factory;
93
void CommandParser::removeAssociation(CommandFactory &factory) {
94
// commands that are associated with the factory
95
vector<string> commands;
97
CommandFactoryMap::iterator factory_it = m_commandfactorys.begin();
98
const CommandFactoryMap::iterator factory_it_end = m_commandfactorys.end();
99
for (; factory_it != factory_it_end; ++factory_it) {
100
if ((*factory_it).second == &factory)
101
commands.push_back((*factory_it).first);
103
// remove all associations
104
while (!commands.empty()) {
105
m_commandfactorys.erase(commands.back());