aboutsummaryrefslogtreecommitdiff
blob: fd9e5813fbff1b006965fe6d5eba5ad8ec054902 (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
/*
   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 shopt_builtin.cpp
/// \brief implementation for the shopt builtin
///

#include "builtins/shopt_builtin.h"

#include "core/interpreter.h"
#include "cppbash_builtin.h"
#include "exceptions.h"

void shopt_builtin::set_opt(const std::vector<std::string>& bash_args, bool value)
{
  for(auto iter = bash_args.begin() + 1; iter != bash_args.end(); ++iter)
      _walker.set_additional_option(*iter, value);
}

void shopt_builtin::print_opts() const
{
  for(auto iter = _walker.additional_options_begin(); iter != _walker.additional_options_end(); ++iter)
      *_out_stream << "shopt " << (iter->second ? "-s " : "-u ") << iter->first << std::endl;
}

int shopt_builtin::exec(const std::vector<std::string>& bash_args)
{
  if(bash_args.empty())
    throw libbash::illegal_argument_exception("Arguments required for shopt");
  else if(bash_args[0].size() != 2)
    throw libbash::unsupported_exception("Multiple arguments are not supported");

  switch(bash_args[0][1])
  {
    case 'u':
      set_opt(bash_args, false);
      break;
    case 's':
      set_opt(bash_args, true);
      break;
    case 'p':
      print_opts();
      break;
    case 'q':
    case 'o':
      throw libbash::unsupported_exception("shopt " + bash_args[0] + " is not supported yet");
    default:
      throw libbash::illegal_argument_exception("Unrecognized option for shopt: " + bash_args[0]);
  }

  return 0;
}