aboutsummaryrefslogtreecommitdiff
blob: b04835353ebc6c00dad212a3749a1077c713d6af (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
   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 interpreter.cpp
/// \brief implementations for bash interpreter (visitor pattern).
///
#include "core/interpreter.h"

#include <cctype>

#include <functional>
#include <limits>

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/foreach.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>

#include "core/bash_ast.h"

namespace
{
  std::string get_options(std::map<char, bool>& options)
  {
    std::string result;
    boost::copy(options | boost::adaptors::map_keys
                        | boost::adaptors::filtered(
                          [&](char option) -> bool {
                            return options[option];
                          }
                        ),
                back_inserter(result));
    return result;
  }
}

interpreter::interpreter(): _out(&std::cout), _err(&std::cerr), _in(&std::cin), additional_options(
    {
      {"autocd", false},
      {"cdable_vars", false},
      {"cdspell", false},
      {"checkhash", false},
      {"checkjobs", false},
      {"checkwinsize", false},
      {"cmdhist", false},
      {"compat31", false},
      {"compat32", false},
      {"compat40", false},
      {"dirspell", false},
      {"dotglob", false},
      {"execfail", false},
      {"expand_aliases", false},
      {"extdebug", false},
      {"extglob", false},
      {"extquote", true},
      {"failglob", false},
      {"force_fignore", false},
      {"globstar", false},
      {"gnu_errfmt", false},
      {"histappend", false},
      {"histreedit", false},
      {"histverify", false},
      {"hostcomplete", false},
      {"huponexit", false},
      {"interactive_comments", false},
      {"lithist", false},
      {"login_shell", false},
      {"mailwarn", false},
      {"no_empty_cmd_completion", false},
      {"nocaseglob", false},
      {"nocasematch", false},
      {"nullglob", false},
      {"progcomp", false},
      {"promptvars", false},
      {"restricted_shell", false},
      {"shift_verbose", false},
      {"sourcepath", false},
      {"xpg_echo", false},
    }
    ), options(
    {
      {'a', false},
      {'b', false},
      {'e', false},
      {'f', false},
      {'h', true},
      {'k', false},
      {'m', false},
      {'n', false},
      {'p', false},
      {'t', false},
      {'u', false},
      {'v', false},
      {'x', false},
      {'B', true},
      {'C', false},
      {'E', false},
      {'H', false},
      {'P', false},
      {'T', false},
    }
    ), status(0)
{
  define("IFS", " \t\n");
  // We do not support the options set by the shell itself (such as the -i option)
  define("-", get_options(options));
}

std::shared_ptr<variable> interpreter::resolve_variable(const std::string& name) const
{
  if(name.empty())
    return std::shared_ptr<variable>();

  BOOST_REVERSE_FOREACH(auto& frame, local_members)
  {
    auto iter_local = frame.find(name);
    if(iter_local != frame.end())
      return iter_local->second;
  }

  auto iter_global = members.find(name);
  if(iter_global == members.end())
    return std::shared_ptr<variable>();
  return iter_global->second;
}

bool interpreter::is_unset_or_null(const std::string& name,
                                   const unsigned index) const
{
  auto var = resolve_variable(name);
  if(!var)
    return true;
  return var->is_null(index);
}

std::string interpreter::get_substring(const std::string& name,
                                       long long offset,
                                       unsigned length,
                                       const unsigned index) const
{
  std::string value = resolve<std::string>(name, index);
  if(!get_real_offset(offset, boost::numeric_cast<unsigned>(value.size())))
    return "";
  // After get_real_offset, we know offset can be cast to unsigned.
  return value.substr(boost::numeric_cast<std::string::size_type>(offset), length);
}

const std::string interpreter::do_substring_expansion(const std::string& name,
                                                      long long offset,
                                                      const unsigned index) const
{
  return get_substring(name, offset, std::numeric_limits<unsigned>::max(), index);
}

const std::string interpreter::do_substring_expansion(const std::string& name,
                                                      long long offset,
                                                      int length,
                                                      const unsigned index) const
{
  if(length < 0)
    throw libbash::illegal_argument_exception("length of substring expression should be greater or equal to zero");

  return get_substring(name, offset, boost::numeric_cast<unsigned>(length), index);
}

std::string interpreter::get_subarray(const std::string& name,
                                      long long offset,
                                      unsigned length) const
{
  std::vector<std::string> array;
  if(name == "*" || name == "@")
  {
    // ${*:1} has the same content as ${*}, ${*:0} contains current script name as the first element
    if(offset > 0)
      offset--;
    else if(offset == 0)
      array.push_back(resolve<std::string>("0"));
  }
  // We do not support arrays that have size bigger than numeric_limits<unsigned>::max()
  if(resolve_array(name, array) && get_real_offset(offset, boost::numeric_cast<unsigned>(array.size())))
  {
    // We do not support arrays that have size bigger than numeric_limits<unsigned>::max()
    // After get_real_offset, we know offset can be cast to unsigned.
    unsigned max_length = boost::numeric_cast<unsigned>(array.size()) - boost::numeric_cast<unsigned>(offset);
    if(length > max_length)
      length = max_length;

    auto start = array.begin() + boost::numeric_cast<std::vector<std::string>::difference_type>(offset);
    auto end = array.begin() + boost::numeric_cast<std::vector<std::string>::difference_type>(offset + length);
    return boost::algorithm::join(std::vector<std::string>(start, end), resolve<std::string>("IFS").substr(0, 1));
  }
  else
  {
    return "";
  }
}

const std::string interpreter::do_subarray_expansion(const std::string& name,
                                                     long long offset) const
{
  return get_subarray(name, offset, std::numeric_limits<unsigned>::max());
}

const std::string interpreter::do_subarray_expansion(const std::string& name,
                                                     long long offset,
                                                     int length) const
{
  if(length < 0)
    throw libbash::illegal_argument_exception("length of substring expression should be greater or equal to zero");

  return get_subarray(name, offset, boost::numeric_cast<unsigned>(length));
}

std::string interpreter::do_replace_expansion(const std::string& name,
                                              std::function<void(std::string&)> replacer,
                                              const unsigned index) const
{
  std::string value = resolve<std::string>(name, index);
  replacer(value);
  return value;
}

std::string::size_type interpreter::get_length(const std::string& name,
                                 const unsigned index) const
{
  auto var = resolve_variable(name);
  if(!var)
    return 0;
  return var->get_length(index);
}

variable::size_type interpreter::get_array_length(const std::string& name) const
{
  auto var = resolve_variable(name);
  if(!var)
    return 0;
  return var->get_array_length();
}

void interpreter::get_all_elements_joined(const std::string& name,
                                          const std::string& delim,
                                          std::string& result) const
{
  std::vector<std::string> array;

  if(resolve_array(name, array))
    result = boost::algorithm::join(array, delim);
  else
    result = "";
}

void interpreter::get_all_elements(const std::string& name,
                                   std::string& result) const
{
  get_all_elements_joined(name, " ", result);
}

void interpreter::get_all_elements_IFS_joined(const std::string& name,
                                              std::string& result) const
{
  get_all_elements_joined(name,
                          resolve<std::string>("IFS").substr(0, 1),
                          result);
}

void interpreter::split_word(const std::string& word, std::vector<std::string>& output) const
{
  const std::string& delimeter = resolve<std::string>("IFS");
  std::string trimmed(word);
  boost::trim_if(trimmed, boost::is_any_of(delimeter));

  if(trimmed == "")
    return;

  std::vector<std::string> splitted_values;
  boost::split(splitted_values, trimmed, boost::is_any_of(delimeter), boost::token_compress_on);
  output.insert(output.end(), splitted_values.begin(), splitted_values.end());
}

void interpreter::define_function_arguments(scope& current_stack,
                                            const std::vector<std::string>& arguments)
{
  std::map<unsigned, std::string> positional_args;

  for(auto i = 1u; i <= arguments.size(); ++i)
    positional_args[i] = arguments[i - 1];

  current_stack["*"].reset(new variable("*", positional_args));
}

namespace
{
  bool check_function_name(const std::string& name)
  {
    using namespace boost::xpressive;
    sregex bash_name_pattern =
      !digit >>
      ~(set[range('0', '9') | (set= '$', '\'', '"', '(', ')', ' ', '\n', '\r')]) >>
      *(~(set= '$', '\'', '"', '(', ')', ' ', '\n', '\r'));
    return regex_match(name, bash_name_pattern);
  }
}

void interpreter::define_function(const std::string& name,
                                  ANTLR3_MARKER body_index)
{
  if(!check_function_name(name))
    throw libbash::parse_exception("illegal function name: " + name);
  functions.insert(make_pair(name, function(*ast_stack.top(), body_index)));
}

void interpreter::call(const std::string& name,
                       const std::vector<std::string>& arguments)
{
  // Prepare arguments
  define_function_arguments(local_members.back(), arguments);

  auto iter = functions.find(name);
  if(iter != functions.end())
    iter->second.call(*this);
  else
    throw libbash::runtime_exception(name + " is not defined.");
}

void interpreter::replace_all(std::string& value,
                              const boost::xpressive::sregex& pattern,
                              const std::string& replacement)
{
  value = boost::xpressive::regex_replace(value,
                                          pattern,
                                          replacement,
                                          boost::xpressive::regex_constants::format_literal);
}

void interpreter::lazy_remove_at_end(std::string& value,
                              const boost::xpressive::sregex& pattern)
{
  boost::xpressive::smatch what;
  if(boost::xpressive::regex_match(value,
                                   what,
                                   pattern))
    value = what[1];
}

void interpreter::replace_first(std::string& value,
                                const boost::xpressive::sregex& pattern,
                                const std::string& replacement)
{
  value = boost::xpressive::regex_replace(value,
                                          pattern,
                                          replacement,
                                          boost::xpressive::regex_constants::format_literal | boost::xpressive::regex_constants::format_first_only);
}

void interpreter::trim_trailing_eols(std::string& value)
{
  boost::trim_right_if(value, boost::is_any_of("\n"));
}

void interpreter::get_all_function_names(std::vector<std::string>& function_names) const
{
  boost::copy(functions | boost::adaptors::map_keys, back_inserter(function_names));
}

namespace
{
  void check_unset_positional(const std::string& name)
  {
    // Unsetting positional parameters is not allowed
    if(isdigit(name[0]))
      throw libbash::runtime_exception("unset: not a valid identifier");
  }
}

void interpreter::unset(const std::string& name)
{
  check_unset_positional(name);

  auto unsetter = [&](scope& frame) -> bool {
    auto iter_local = frame.find(name);
    if(iter_local != frame.end())
    {
      if(iter_local->second->is_readonly())
        throw libbash::readonly_exception("unset a readonly variable");
      frame.erase(iter_local);
      return true;
    }
    return false;
  };

  if(std::none_of(local_members.rbegin(), local_members.rend(), unsetter))
    unsetter(members);
}

// We need to return false when unsetting readonly functions in future
void interpreter::unset_function(const std::string& name)
{
  auto function = functions.find(name);
  if(function != functions.end())
    functions.erase(name);
}

void interpreter::unset(const std::string& name,
                        const unsigned index)
{
  check_unset_positional(name);

  auto var = resolve_variable(name);
  if(var)
  {
    if(var->is_readonly())
      throw libbash::readonly_exception("unset a readonly variable");
    var->unset_value(index);
  }
}

bool interpreter::get_additional_option(const std::string& name) const
{
  auto iter = additional_options.find(name);
  if(iter == additional_options.end())
    throw libbash::illegal_argument_exception("Invalid bash option");

  return iter->second;
}

void interpreter::set_additional_option(const std::string& name, bool value)
{
  auto iter = additional_options.find(name);
  if(iter == additional_options.end())
    throw libbash::illegal_argument_exception(name + " is not a valid bash option");

  iter->second = value;
}

long interpreter::eval_arithmetic(const std::string& expression)
{
  bash_ast ast(std::stringstream(expression), &bash_ast::parser_arithmetics);
  return ast.interpret_with(*this, &bash_ast::walker_arithmetics);
}

int interpreter::shift(int shift_number)
{
  auto parameters = resolve_variable("*");
  if(shift_number < 0)
    return 1;

  return parameters->shift(static_cast<unsigned>(shift_number));
}