aboutsummaryrefslogtreecommitdiff
blob: 2eaafc269e8a34c9c98ad3424027b21df3a167b6 (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
/*
   Please use git log for copyright holder and year information

   This file is part of libbash.

   libbash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   libbash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \file unset_builtin.h
/// \brief implementation for the unset builtin
///
#include "builtins/unset_builtin.h"

#include <functional>

#include "core/interpreter.h"

using namespace boost::xpressive;

int unset_builtin::exec(const std::vector<std::string>& bash_args)
{
  if(bash_args.empty())
    return 0;

  if(bash_args[0] == "-f")
    for_each(bash_args.begin() + 1,
             bash_args.end(),
             std::bind(&interpreter::unset_function, &_walker, std::placeholders::_1));
  else
  /* POSIX says if neither -f nor -v is specified, name refers to a variable;
   * if a variable by that name does not exist, it is unspecified whether a
   * function by that name, if any, shall be unset.
   *
   * >=bash-4.1: without options, unset first tries to unset a variable, and
   * if that fails, tries to unset a function.
   * (We haven't checked bash-4.0)
   *
   * bash-3.2: if no options are supplied, or the -v option is given, each
   * name refers to a shell variable.
   *
   * We addhere to bash-3.2
   * */
    for_each(bash_args.front() == "-v" ? bash_args.begin() + 1 : bash_args.begin(),
             bash_args.end(),
             [&](const std::string& name) {
                static const sregex index_pattern = sregex::compile("^(.*)\\[(\\d*)\\]$");
                smatch match;
                if(regex_match(name, match, index_pattern))
                  _walker.unset(match[1], boost::lexical_cast<unsigned int>(match[2]));
                else
                  _walker.unset(name);
             });

  return 0;
}