aboutsummaryrefslogtreecommitdiff
blob: 2644bb7857faa7c44d12bc2be248dd607727a93f (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
/*
   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 declare_builtin.cpp
/// \brief class that implements the declare builtin
///
#include "builtins/declare_builtin.h"

#include <algorithm>
#include <iostream>

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

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

  if(bash_args[0][0] != '-' && bash_args[0][0] != '+')
  {
    throw libbash::illegal_argument_exception("Invalid option for declare builtin");
    return 1;
  }

  int result = 0;
  switch(bash_args[0][1])
  {
    case 'F':
      if(bash_args[0][0] == '+')
        return 0;
      if(bash_args.size() > 1)
      {
        for(auto iter = bash_args.begin() + 1; iter != bash_args.end(); ++iter)
        {
          if(_walker.has_function(*iter))
            *_out_stream << *iter << std::endl;
          else
            result = 1;
        }
      }
      else
      {
        std::vector<std::string> functions;

        _walker.get_all_function_names(functions);
        sort(functions.begin(), functions.end());

        for(auto iter = functions.begin(); iter != functions.end(); ++iter)
          *_out_stream << "declare -f " << *iter << std::endl;
      }
      return result;
    case 'p':
      if(bash_args.size() > 1)
      {
        for(auto iter = bash_args.begin() + 1; iter != bash_args.end(); ++iter)
        {
          // We do not print the type of the variable for now
          if(!_walker.is_unset(*iter))
          {
            *_out_stream << "declare -- " << *iter << "=\"" << _walker.resolve<std::string>(*iter) << "\"" << std::endl;
          }
          else
          {
            *_out_stream << "-bash: declare: " << *iter << ": not found" << std::endl;
            result = 1;
          }
        }
      }
      else
      {
        throw libbash::unsupported_exception("We do not support declare -p without arguments for now");
      }
      return result;
    case 'a':
    case 'A':
    case 'f':
    case 'i':
    case 'l':
    case 'r':
    case 't':
    case 'u':
    case 'x':
      throw libbash::unsupported_exception("declare " + bash_args[0] + " is not supported yet");
      return 1;
    default:
      throw libbash::illegal_argument_exception("Unrecognized option for declare: " + bash_args[0]);
      return 1;
  }
}