Line 0
Link Here
|
|
|
1 |
/* |
2 |
Please use git log for copyright holder and year information |
3 |
|
4 |
This file is part of libbash. |
5 |
|
6 |
libbash is free software: you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation, either version 2 of the License, or |
9 |
(at your option) any later version. |
10 |
|
11 |
libbash is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with libbash. If not, see <http://www.gnu.org/licenses/>. |
18 |
*/ |
19 |
/// |
20 |
/// \file read_builtin.cpp |
21 |
/// \brief class that implements the read builtin |
22 |
/// |
23 |
|
24 |
#include "builtins/read_builtin.h" |
25 |
|
26 |
#include <string.h> |
27 |
|
28 |
#include "core/interpreter.h" |
29 |
#include "builtins/builtin_exceptions.h" |
30 |
|
31 |
int read_builtin::exec(const std::vector<std::string>& bash_args) |
32 |
{ |
33 |
std::string input; |
34 |
std::stringstream formated_input; |
35 |
|
36 |
getline(this->input_buffer(), input); |
37 |
|
38 |
while(input[input.length()-1] == '\\') { |
39 |
input.erase(input.end()-1); |
40 |
std::string input_line; |
41 |
getline(this->input_buffer(), input_line); |
42 |
input += input_line; |
43 |
} |
44 |
|
45 |
cppbash_builtin::transform_escapes(input, formated_input, false); |
46 |
|
47 |
if(bash_args.empty()) |
48 |
{ |
49 |
_walker.set_value("REPLY", formated_input.str()); |
50 |
return 0; |
51 |
} |
52 |
|
53 |
char *cstr, *token; |
54 |
cstr = new char[formated_input.str().size() + 1]; |
55 |
strcpy(cstr, formated_input.str().c_str()); |
56 |
|
57 |
for(auto i = bash_args.begin(); i != bash_args.end(); i++) |
58 |
{ |
59 |
if(i != bash_args.end() - 1) |
60 |
{ |
61 |
if(i == bash_args.begin()) |
62 |
token = strtok(cstr, " "); |
63 |
else |
64 |
token = strtok(NULL, " "); |
65 |
|
66 |
if(token == NULL) |
67 |
return 0; |
68 |
} |
69 |
else |
70 |
{ |
71 |
if(i == bash_args.begin()) |
72 |
token = cstr; |
73 |
else |
74 |
token += strlen(token) + 1; |
75 |
} |
76 |
|
77 |
_walker.set_value(*i, token); |
78 |
} |
79 |
|
80 |
free(cstr); |
81 |
|
82 |
return 0; |
83 |
} |