mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-09-04 19:47:18 +02:00

- Implemented simple word substitution aliases. This is not the ultimate form which will involve the ability to insert arbitrary constructs. Later.
122 lines
3.8 KiB
C++
122 lines
3.8 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright 2006 - 2014, Paul Beckingham, Federico Hernandez.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included
|
|
// in all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
//
|
|
// http://www.opensource.org/licenses/mit-license.php
|
|
//
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <cmake.h>
|
|
#include <iostream> // TODO Remove
|
|
#include <vector>
|
|
#include <Alias.h>
|
|
#include <Context.h>
|
|
#include <Lexer.h>
|
|
#include <Tree.h>
|
|
#include <text.h>
|
|
|
|
extern Context context;
|
|
|
|
// Alias expansion limit. Any more indicates some kind of error.
|
|
const int safetyValveDefault = 10;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
Alias::Alias ()
|
|
{
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
Alias::~Alias ()
|
|
{
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void Alias::load ()
|
|
{
|
|
_aliases.clear ();
|
|
|
|
std::vector <std::string> vars;
|
|
context.config.all (vars);
|
|
|
|
std::vector <std::string>::iterator var;
|
|
for (var = vars.begin (); var != vars.end (); ++var)
|
|
if (var->substr (0, 6) == "alias.")
|
|
_aliases[var->substr (6)] = context.config.get (*var);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// An alias must be a distinct word on the command line.
|
|
//
|
|
// TODO This is straight word substitution. What this really needs is node
|
|
// insertion, for more advanced aliases.
|
|
void Alias::resolve (Tree* tree)
|
|
{
|
|
bool something;
|
|
int safety_valve = safetyValveDefault;
|
|
|
|
do
|
|
{
|
|
something = false;
|
|
|
|
std::string command;
|
|
std::vector <Tree*>::iterator i;
|
|
for (i = tree->_branches.begin (); i != tree->_branches.end (); ++i)
|
|
{
|
|
// Parser override operator.
|
|
if ((*i)->attribute ("raw") == "--")
|
|
break;
|
|
|
|
// Skip known args.
|
|
if (! (*i)->hasTag ("?"))
|
|
continue;
|
|
|
|
std::map <std::string, std::string>::iterator a;
|
|
if ((*i)->_branches.size ())
|
|
{
|
|
std::vector <Tree*>::iterator b;
|
|
for (b = (*i)->_branches.begin (); b != (*i)->_branches.end (); ++b)
|
|
{
|
|
a = _aliases.find ((*b)->attribute ("raw"));
|
|
if (a != _aliases.end ())
|
|
{
|
|
(*b)->attribute ("raw", a->second);
|
|
something = true;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
a = _aliases.find ((*i)->attribute ("raw"));
|
|
if (a != _aliases.end ())
|
|
{
|
|
(*i)->attribute ("raw", a->second);
|
|
something = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
while (something && --safety_valve > 0);
|
|
|
|
if (safety_valve <= 0)
|
|
context.debug (format ("Nested alias limit of {1} reached.", safetyValveDefault));
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|