aboutsummaryrefslogtreecommitdiff
blob: 96dab9b10014d817f91da3c0f7a40096b1e3bc2f (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
   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 inherit_builtin.cpp
/// \brief class that implements the inherit builtin
///

#include "builtins/inherit_builtin.h"

#include <cstdlib>

#include <string>

#include "core/interpreter.h"

inline void inherit_builtin::append_global(const std::string& name)
{
    if(!_walker.is_unset_or_null(name, 0))
      _walker.set_value("E_" + name, _walker.resolve<std::string>("E_" + name) + " " + _walker.resolve<std::string>(name));
}

inline void inherit_builtin::restore_global(const std::string& name, const std::string& value)
{
  if(value != "")
    _walker.set_value(name, value);
  else
    _walker.unset(name);
}

inline void inherit_builtin::backup_global(const std::string& name, std::string& value)
{
  value = _walker.resolve<std::string>(name);
  _walker.unset(name);
}

inline bool inherit_builtin::hasq(const std::string& value, const std::string& name)
{
  const std::string& target = _walker.resolve<std::string>(name);
  return target.find(value) != std::string::npos;
}

// We do not support any QA warning
int inherit_builtin::exec(const std::vector<std::string>& bash_args)
{
  _walker.set_value("ECLASS_DEPTH", _walker.resolve<long>("ECLASS_DEPTH") + 1);

  // find eclass directory
  std::string eclassdir;
  if(getenv("ECLASSDIR"))
    eclassdir = getenv("ECLASSDIR") + std::string("/");
  else
    eclassdir = "/usr/portage/eclass/";

  std::string location;
  std::string export_funcs_var;
  // These variables must be restored before returning
  std::string PECLASS(_walker.resolve<std::string>("ECLASS"));
  std::string prev_export_funcs_var(_walker.resolve<std::string>("__export_funcs_var"));

  std::string B_IUSE;
  std::string B_REQUIRED_USE;
  std::string B_DEPEND;
  std::string B_RDEPEND;
  std::string B_PDEPEND;

  for(auto iter = bash_args.begin(); iter != bash_args.end(); ++iter)
  {
    location = eclassdir + *iter + ".eclass";
    _walker.set_value("ECLASS", *iter);
    _walker.define("__export_funcs_var", "__export_functions_" + _walker.resolve<std::string>("ECLASS_DEPTH"));
    _walker.unset(_walker.resolve<std::string>("__export_funcs_var"));

    // Portage implementation performs actions for overlays here but we don't do it for now

    backup_global("IUSE", B_IUSE);
    backup_global("REQUIRED_USE", B_REQUIRED_USE);
    backup_global("DEPEND", B_DEPEND);
    backup_global("RDEPEND", B_RDEPEND);
    backup_global("PDEPEND", B_PDEPEND);

    _walker.execute_builtin("source", {location});

    append_global("IUSE");
    append_global("REQUIRED_USE");
    append_global("DEPEND");
    append_global("RDEPEND");
    append_global("PDEPEND");

    restore_global("IUSE", B_IUSE);
    restore_global("REQUIRED_USE", B_REQUIRED_USE);
    restore_global("DEPEND", B_DEPEND);
    restore_global("RDEPEND", B_RDEPEND);
    restore_global("PDEPEND", B_PDEPEND);

    if(!_walker.is_unset(_walker.resolve<std::string>("__export_funcs_var")))
    {
      std::stringstream func_names(_walker.resolve<std::string>(_walker.resolve<std::string>("__export_funcs_var")));
      std::string func_name;

      while(func_names >> func_name)
      {
        std::stringstream func;
        func << func_name << "() { " << _walker.resolve<std::string>("ECLASS") << "_" << func_name
          << " \"$@\" ; }";
        _walker.execute_builtin("eval", {func.str()});
      }
    }
    _walker.unset(_walker.resolve<std::string>("__export_funcs_var"));

    if(!hasq(*iter, "INHERITED"))
      _walker.set_value("INHERITED", _walker.resolve<std::string>("INHERITED") + " " + *iter);
  }

  _walker.set_value("ECLASS_DEPTH", _walker.resolve<long>("ECLASS_DEPTH") - 1);
  if(_walker.resolve<long>("ECLASS_DEPTH") > 0)
  {
    _walker.set_value("ECLASS", PECLASS);
  }
  else
  {
    _walker.unset("ECLASS");
    _walker.unset("__export_funcs_var");
  }

  return 0;
}