aboutsummaryrefslogtreecommitdiff
blob: 0e21707f0be9a004b31aded229212d69d632f3e7 (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
/*
   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 echo_builtin.cpp
/// \author Nathan Eloe
/// \brief class that implements the echo builtin
///

#include "echo_builtin.h"
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>


namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;
namespace phoenix = boost::phoenix;

class suppress_output
{
};

int echo_builtin::exec(const std::vector<std::string>& bash_args)
{
  bool suppress_nl = false;
  bool enable_escapes = false;
  bool options_parsed = false;

  if(bash_args.empty())
  {
    this->out_buffer() << std::endl;
    return 0;
  }

  for(auto i = bash_args.begin(); i != bash_args.end(); i++)
  {
    const std::string& str = *i;

    if(!options_parsed)
    {
      options_parsed = determine_options(str, suppress_nl, enable_escapes);
    }

    if(options_parsed)
    {
      if(enable_escapes)
      {
        for(; i != bash_args.end(); i++)
        {
          try
          {
            transform_escapes(*i);
          }
          catch(suppress_output)
          {
            return 0;
          }
        }
      }
      else
      {
        this->out_buffer() << karma::format(karma::string % ' ', std::vector<std::string>(i, bash_args.end()));
      }

      if(!suppress_nl)
        this->out_buffer() << std::endl;

      return 0;
    }
  }

  return 0;
}

bool echo_builtin::determine_options(const std::string &string, bool &suppress_nl, bool &enable_escapes)
{
  using phoenix::ref;
  using qi::char_;

  bool n_matched = false, e_matched = false, E_matched = false;

  auto options = '-' >
    +(
      char_('n')[ref(n_matched) = true] |
      char_('e')[ref(e_matched) = true, ref(E_matched) = false] |
      char_('E')[ref(E_matched) = true, ref(e_matched) = false]
    );

  auto first = string.begin();
  qi::parse(first, string.end(), options);

  if(first != string.end()) {
    return true;
  }
  else
  {
    if(n_matched)
      suppress_nl = true;

    if(e_matched)
      enable_escapes = true;

    if(E_matched)
      enable_escapes = false;

    return false;
  }
}

void echo_builtin::transform_escapes(const std::string &string)
{
  using phoenix::val;
  using qi::lit;

  auto escape_parser =
  +(
    lit('\\') >>
    (
     lit('a')[this->out_buffer() << val("\a")] |
     lit('b')[this->out_buffer() << val("\b")] |
     // \e is a GNU extension
     lit('e')[this->out_buffer() << val("\033")] |
     lit('f')[this->out_buffer() << val("\f")] |
     lit('n')[this->out_buffer() << val("\n")] |
     lit('r')[this->out_buffer() << val("\r")] |
     lit('t')[this->out_buffer() << val("\t")] |
     lit('v')[this->out_buffer() << val("\v")] |
     lit('c')[phoenix::throw_(suppress_output())] |
     lit('\\')[this->out_buffer() << val('\\')] |
     lit("0") >> qi::uint_parser<unsigned, 8, 1, 3>()[ this->out_buffer() << phoenix::static_cast_<char>(qi::_1)] |
     lit("x") >> qi::uint_parser<unsigned, 16, 1, 2>()[ this->out_buffer() << phoenix::static_cast_<char>(qi::_1)]

    ) |
    qi::char_[this->out_buffer() << qi::_1]
  );

  auto begin = string.begin();
  qi::parse(begin, string.end(), escape_parser);
}