Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More raw pointer removals #15

Merged
merged 15 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ test/err2
"test/p\253\324\001"
test/shell-in
build
Debug
Release
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ before_install:
- sudo apt-get update -qq
install:
# C++17
- sudo apt-get install -qq g++-6
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-6 90
- sudo apt-get install -qq g++-7
- sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 90
script:
- mkdir build
- cd build
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.8.2)
project(yash)
project(yash VERSION 0.0.8)

# find flex and bison
find_package(BISON)
Expand Down
8 changes: 4 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
BUILT_SOURCES = shell_bison.hh

if DEBUG
AM_CFLAGS = -ggdb3 -fsanitize=address -O0 -lpthread -std=c++17
AM_CXXFLAGS = -ggdb3 -O0 -fsanitize=address -lpthread -std=c++17
AM_CFLAGS = -ggdb3 -fsanitize=address -Wall -Wextra -O0 -lpthread -std=c++17
AM_CXXFLAGS = -ggdb3 -O0 -fsanitize=address -Wall -Wextra -lpthread -std=c++17
else
AM_CFLAGS = -O2 -lpthread
AM_CXXFLAGS = -O2 -lpthread -std=c++17
AM_CFLAGS = -O2 -Wall -Wextra -lpthread
AM_CXXFLAGS = -O2 -Wall -Wextra -lpthread -std=c++17
endif

AM_YFLAGS = -d
Expand Down
22 changes: 11 additions & 11 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ inline int eval_to_buffer(char * const * cmd, char * outBuff, size_t buffSize)
} else {
/** Parent Process: read from the pipe **/
close(fdpipe[1]); // close unused write end
for (memset(outBuff, 0, buffSize); x = read(fdpipe[0], outBuff, buffSize); ) {
for (memset(outBuff, 0, buffSize); (x = read(fdpipe[0], outBuff, buffSize)); ) {
}
if (x == buffSize) {
close(fdpipe[0]);
Expand Down Expand Up @@ -139,12 +139,16 @@ void Command::insertSimpleCommand(std::shared_ptr<SimpleCommand> simpleCommand)

void Command::clear()
{
if (m_stdin != 0) {close(m_stdin);}
if (m_stdout != 1) {close(m_stdout);}
if (m_stderr != 2) {close(m_stderr);}

if (m_stdin != 0) {
close(m_stdin);
}
if (m_stdout != 1) {
close(m_stdout);
}
if (m_stderr != 2) {
close(m_stderr);
}
m_stdin = 0, m_stdout = 1, m_stderr = 2;

simpleCommands.clear(),
background = append = false,
numOfSimpleCommands = 0, m_pgid = 0,
Expand Down Expand Up @@ -188,7 +192,6 @@ void Command::execute()

time_t rs, us, ss;
int rsf, usf, ssf;
int cpu;

struct rusage selfb, selfa;
struct rusage kidsb, kidsa;
Expand Down Expand Up @@ -235,9 +238,7 @@ void Command::execute()
for (int x = 0; x < numOfSimpleCommands; ++x) {
/* manage commands */
std::vector<char *> curr = simpleCommands.at(x).get()->arguments;
char ** d_args;
curr.push_back(nullptr);
d_args = curr.data();

/* add nullptr to the end of the simple command (for exec) */
simpleCommands.at(x).get()->arguments.push_back(nullptr);
Expand Down Expand Up @@ -367,7 +368,7 @@ void Command::prompt()
if (!gethostname(buffer, 100)) {_host = std::string(buffer);} else {
_host = std::string("localhost");
}
char * _wd = nullptr, * _hme = nullptr;
char* _hme = nullptr;
auto _pwd = std::shared_ptr<char>(
new char[4], [](auto s) {delete[] s;});
snprintf(_pwd.get(), sizeof(_pwd.get()), "pwd");
Expand Down Expand Up @@ -489,7 +490,6 @@ void Command::send_to_foreground(
bool & fg,
termios & _oldtermios)
{
pid_t current = m_shell_pgid;
if (m_p_jobs->size()) {
/* did they pass an argument? */
job_num = (job_num < 0) ? m_p_jobs->size() - 1 : job_num;
Expand Down
71 changes: 42 additions & 29 deletions src/shell-readline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool read_line::write_with_error(int _fd, char & c)
*/
bool read_line::write_with_error(int _fd, const char * s)
{
if (write(_fd, s, strlen(s)) != strlen(s)) {
if (static_cast<size_t>(write(_fd, s, strlen(s))) != strlen(s)) {
perror("write");
return false;
}
Expand All @@ -101,7 +101,7 @@ bool read_line::write_with_error(int _fd, const char * s)
*/
bool read_line::write_with_error(int _fd, const char * s, const size_t & len)
{
if (write(_fd, s, len) != len) {
if (static_cast<size_t>(write(_fd, s, len)) != len) {
perror("write");
return false;
}
Expand All @@ -118,10 +118,10 @@ bool read_line::write_with_error(int _fd, const char * s, const size_t & len)
*
* @return True upon successful read.
*/
bool read_line::read_with_error(int _fd, char & c)
bool read_line::read_with_error(const int & _fd, char & c)
{
char d; /* temp, for reading */
if (!read(0, &d, 1)) {
if (!read(_fd, &d, 1)) {
perror("read");
return false;
} else {
Expand Down Expand Up @@ -206,8 +206,13 @@ bool read_line::handle_ctrl_a(std::string & _line)
} else if (!write_with_error(1, _line.c_str(), term_width - 3)) {return false;}

for (size_t k = 0; k < term_width - 2; ++k) {
if (!write_with_error(1, "\b", 1)) {return true;}}
} else if (!write_with_error(1, "\b", 1)) {return false;}
if (!write_with_error(1, "\b", 1)) {
return true;
}
}
} else if (!write_with_error(1, "\b", 1)) {
return false;
}
}
return false;
}
Expand Down Expand Up @@ -279,7 +284,6 @@ bool read_line::handle_ctrl_d(std::string & _line)
return false;
}
}
char b = ' ';
if (!(write_with_error(1, " ", 1) && write_with_error(1, "\b", 1))) {
return false;
}
Expand Down Expand Up @@ -314,28 +318,29 @@ bool read_line::handle_ctrl_d(std::string & _line)
*/
bool read_line::handle_tab(std::string & _line)
{
Command::currentSimpleCommand = std::unique_ptr<SimpleCommand>(new SimpleCommand());
Command::currentSimpleCommand = std::make_unique<SimpleCommand>();

/* Part 1: add a '*' to the end of the stream. */
std::string _temp;

std::vector<std::string> _split;
if (_line.size()) {
_split = string_split(_line, ' ');
_temp = tilde_expand(_split.back()) + "*";
} else {_temp = "*";}

} else {
_temp = "*";
}
auto _complete_me = std::shared_ptr<char>(strndup(_temp.c_str(), _temp.size()), free);

/* Part 2: Invoke wildcard expand */
wildcard_expand(_complete_me);

/**
* Part 3: If wc_collector.size() <= 1, then complete the tab.
* otherwise, run "echo <text>*"
*/
std::string * array = Command::currentCommand.wc_collector.data();
std::sort(array, array + Command::currentCommand.wc_collector.size());

std::sort(
Command::currentCommand.wc_collector.begin(), Command::currentCommand.wc_collector.end());
/* TODO(allenh1): for some reason, wc_collector has no elements upon upstart tab */
if (Command::currentCommand.wc_collector.size() == 1) {
/* First check if the line has any spaces! */
/* If so, we will wrap in quotes! */
Expand All @@ -349,21 +354,30 @@ bool read_line::handle_tab(std::string & _line)
char ch[_line.size() + 1]; char sp[_line.size() + 1];
ch[_line.size()] = '\0'; sp[_line.size()] = '\0';
memset(ch, '\b', _line.size()); memset(sp, ' ', _line.size());
if (!write_with_error(1, ch, _line.size())) {return false;}
if (!write_with_error(1, sp, _line.size())) {return false;}
if (!write_with_error(1, ch, _line.size())) {return false;}
if (!write_with_error(1, ch, _line.size())) {
return false;
}
if (!write_with_error(1, sp, _line.size())) {
return false;
}
if (!write_with_error(1, ch, _line.size())) {
return false;
}
_line = "";
for (size_t x = 0; x < _split.size() - 1; _line += _split[x++] + " ") {
for (size_t x = 0; x < _split.size() - 1; ++x) {
_line += _split[x] + " ";
}
_line += Command::currentCommand.wc_collector[0];
if (quote_wrap) {_line = _line + "\"";}
if (quote_wrap) {
_line = _line + "\"";
}
m_current_line_copy = _line;
Command::currentCommand.wc_collector.clear();
Command::currentCommand.wc_collector.shrink_to_fit();
} else if (Command::currentCommand.wc_collector.size() == 0) {
/* now we check the binaries! */
char * _path = getenv("PATH");
if (!_path) {
const char * _path = getenv("PATH");
if (nullptr == _path) {
/* if path isn't set, continue */
return false;
}
Expand Down Expand Up @@ -514,7 +528,7 @@ bool read_line::handle_ctrl_arrow(std::string & _line)
*
* @return False upon error.
*/
bool read_line::handle_ctrl_k(std::string & _line)
bool read_line::handle_ctrl_k()
{
if (!m_buff.size()) {return false;}
size_t count = m_buff.size() + 1;
Expand Down Expand Up @@ -605,7 +619,7 @@ bool read_line::handle_backspace(std::string & _line)
return false;
}

bool read_line::handle_ctrl_del(std::string & _line)
bool read_line::handle_ctrl_del()
{
char ch4, ch5;
/* read the 53 and the 126 char */
Expand Down Expand Up @@ -681,7 +695,6 @@ bool read_line::handle_delete(std::string & _line)
d = temp.top(); temp.pop();
if (!write_with_error(1, d)) {return false;}
}
char b = ' ';
if (!write_with_error(1, " ", 1)) {return false;} else if (!write_with_error(1, "\b", 1)) {
return false;
}
Expand Down Expand Up @@ -787,7 +800,7 @@ bool read_line::handle_up_arrow(std::string & _line)
m_rev_search.shrink_to_fit();
hist = &m_rev_search;
search_str = _line;
auto it = std::copy_if(
std::copy_if(
m_history->begin(), m_history->end(),
std::back_inserter(*hist),
[_line](const auto & s) {
Expand Down Expand Up @@ -870,7 +883,7 @@ bool read_line::handle_down_arrow(std::string & _line)
m_rev_search.shrink_to_fit();
hist = &m_rev_search;
search_str = _line;
auto it = std::copy_if(
std::copy_if(
m_history->begin(), m_history->end(),
std::back_inserter(*hist),
[_line](const auto & s) {
Expand All @@ -882,7 +895,7 @@ bool read_line::handle_down_arrow(std::string & _line)
index = &search_index;
}

if (*index == hist->size()) {return false;}
if (static_cast<size_t>(*index) == hist->size()) {return false;}


/* clear input so far */
Expand All @@ -902,7 +915,7 @@ bool read_line::handle_down_arrow(std::string & _line)
} else if (!write_with_error(1, ch, _line.size())) {return false;}

/* don't increment past the last command */
if (((*index)) != hist->size() - 1) {
if (static_cast<size_t>((*index)) != hist->size() - 1) {
(*index)++;
_line = hist->at(*index);
_line.pop_back();
Expand Down Expand Up @@ -1013,7 +1026,7 @@ void read_line::load_history()
{
/* load history from ~/.cache/yash-history */
std::ifstream history_file(tilde_expand("~/.cache/yash_history"));
std::string _line; int x = 0;
std::string _line;
for (; std::getline(history_file, _line); history_index++, m_history->push_back(_line + "\n")) {
}
}
10 changes: 5 additions & 5 deletions src/shell-readline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class read_line
bool write_with_error(int _fd, const char * s);
bool write_with_error(int _fd, const char * s, const size_t & len);

bool read_with_error(int _fd, char & c);
bool read_with_error(const int & _fd, char & c);
size_t get_term_width();

bool handle_enter(std::string & _line, char & input);
Expand All @@ -72,8 +72,8 @@ class read_line
bool handle_ctrl_a(std::string & _line);
bool handle_ctrl_d(std::string & _line);
bool handle_ctrl_e(std::string & _line);
bool handle_ctrl_k(std::string & _line);
bool handle_ctrl_del(std::string & _line);
bool handle_ctrl_k();
bool handle_ctrl_del();
bool handle_ctrl_arrow(std::string & _line);

bool handle_up_arrow(std::string & _line);
Expand Down Expand Up @@ -133,7 +133,7 @@ class read_line
} else if (input == 5 && !handle_ctrl_e(_line)) {
continue;
} else if (input == 4 && !handle_ctrl_d(_line)) {continue;} else if (input == 11) {
if (!handle_ctrl_k(_line)) {continue;} else {break;}
if (!handle_ctrl_k()) {continue;} else {break;}
} else if ((input == 8 || input == 127) && !handle_backspace(_line)) {
continue;
} else if (input == 9 && !handle_tab(_line)) {continue;} else if (input == 27) {
Expand All @@ -147,7 +147,7 @@ class read_line
continue;
} else if (ch1 == 91 && ch2 == 51 && ch3 == 126 &&
!handle_delete(_line)) {continue;} else if (ch1 == 91 && ch2 == 51 && ch3 == 59 &&
!handle_ctrl_del(_line)) {continue;}
!handle_ctrl_del()) {continue;}
if (ch1 == 91 && ch2 == 65 && !handle_up_arrow(_line)) {continue;}
if (ch1 == 91 && ch2 == 66 && !handle_down_arrow(_line)) {continue;}
if (ch1 == 91 && ch2 == 67 && !handle_right_arrow(_line)) {continue;}
Expand Down
Loading