aboutsummaryrefslogtreecommitdiff
blob: 98f20b6f5e4670bcdde176deed7d64a86e64f2db (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
/*
   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 libbash.cpp
/// \brief implementation for libbash interface
///

#include "libbash.h"

#include <fstream>

#include <boost/numeric/conversion/cast.hpp>

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

namespace internal
{
  int interpret(interpreter& walker,
                const std::string& path,
                std::unordered_map<std::string, std::vector<std::string>>& variables,
                std::vector<std::string>& functions)
  {
    // Initialize bash environment
    for(auto iter = variables.begin(); iter != variables.end(); ++iter)
      walker.define(iter->first, (iter->second)[0]);
    walker.define("0", path, true);
    variables.clear();

    bash_ast ast(path);
    ast.interpret_with(walker);

    for(auto iter = walker.begin(); iter != walker.end(); ++iter)
      iter->second->get_all_values<std::string>(variables[iter->first]);
    walker.get_all_function_names(functions);

    return walker.get_status();
  }
}

namespace libbash
{
  int interpret(const std::string& target_path,
                std::unordered_map<std::string, std::vector<std::string>>& variables,
                std::vector<std::string>& functions)
  {
    interpreter walker;
    return internal::interpret(walker, target_path, variables, functions);
  }

  int interpret(const std::string& target_path,
                const std::string& preload_path,
                std::unordered_map<std::string, std::vector<std::string>>& variables,
                std::vector<std::string>& functions)
  {
    interpreter walker;

    // Preloading
    bash_ast preload_ast(preload_path);
    preload_ast.interpret_with(walker);

    return internal::interpret(walker, target_path, variables, functions);
  }
}