gpt4 book ai didi

c++ - cin.getline() 开发 shell 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:05 25 4
gpt4 key购买 nike

我正在为自己开发一个受限版本的 shell。这是一个C++程序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
using namespace std;
const int MAX = 256;
const int CMD_MAX = 10;
char *valid_cmds = " ls ps df ";
int main()
{
char line_input[MAX], the_cmd[CMD_MAX];
char *new_args[CMD_MAX], *cp;
int i;
while (1) {
cout << "cmd> ";
if (cin.getline(line_input, MAX, '\n') != NULL) {
cp = line_input;
i = 0;
if ((new_args[i] = strtok(cp, " ")) != NULL) {
strcpy(the_cmd, new_args[i]);
strcat(the_cmd, " ");
if ((strstr(valid_cmds, the_cmd) -valid_cmds)% 4 == 1) {
do {
cp = NULL;
new_args[++i] = strtok(cp, " ");
} while (i < CMD_MAX
&& new_args[i] != NULL);
new_args[i] = NULL;
switch (fork()) {
case 0:
execvp(new_args[0], new_args);
perror("exec failure");
exit(1);
case -1:
perror("fork failure");
exit(2);
default:
// In the parent we should be waiting for
// the child to finis h
;
}
} else
cout << "huh?" << endl;
}
}
}
}

我无法理解的是,当我编译这个程序时,我在行中遇到了错误

 (cin.getline(line_input, MAX, '\n') != NULL) 

错误是在我做了一个$ g++ p3.7.cpp

p3.7.cpp:10:20: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
char *valid_cmds = " ls ps df ";
^~~~~~~~~~~~
p3.7.cpp: In function ‘int main()’:
p3.7.cpp:18:42: error: no match for ‘operator!=’ (operand types are ‘std::basic_istream<char>’ and ‘long int’)
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
p3.7.cpp:18:42: note: candidate: operator!=(int, long int) <built-in>
p3.7.cpp:18:42: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘int’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iosfwd:40:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/postypes.h:221:5: note: candidate: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/postypes.h:221:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::fpos<_StateT>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:64:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_pair.h:456:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_pair.h:456:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::pair<_T1, _T2>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:311:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator!=(const reverse_iterator<_Iterator>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:311:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:349:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator!=(const reverse_iterator<_IteratorL>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:349:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1130:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
operator!=(const move_iterator<_IteratorL>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1130:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_algobase.h:67:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/char_traits.h:39,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1136:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
operator!=(const move_iterator<_Iterator>& __x,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/stl_iterator.h:1136:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:158:5: note: candidate: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator!=(const allocator<_T1>&, const allocator<_T2>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:158:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::allocator<_CharT>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:41:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:164:5: note: candidate: template<class _Tp> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/allocator.h:164:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::allocator<_CharT>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6044:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6044:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6057:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const _CharT* __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6057:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: mismatched types ‘const _CharT*’ and ‘std::basic_istream<char>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/string:52:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6069:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_string.h:6069:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
if (cin.getline(line_input, MAX, '\n') != NULL) {
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/ios_base.h:46:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:42,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:319:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_code&)
operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:319:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:323:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_condition&)
operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:323:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_code&’
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:327:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_code&)
operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:327:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_condition&’
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:331:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_condition&)
operator!=(const error_condition& __lhs,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/system_error:331:3: note: no known conversion for argument 1 from ‘std::basic_istream<char>’ to ‘const std::error_condition&’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/locale_facets.h:48:0,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/basic_ios.h:37,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ios:44,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/ostream:38,
from /usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream:39,
from p3.7.cpp:2:
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/streambuf_iterator.h:210:5: note: candidate: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/bits/streambuf_iterator.h:210:5: note: template argument deduction/substitution failed:
In file included from /usr/include/sys/unistd.h:14:0,
from /usr/include/unistd.h:4,
from p3.7.cpp:6:
p3.7.cpp:18:45: note: ‘std::basic_istream<char>’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
if (cin.getline(line_input, MAX, '\n') != NULL) {

我上面的错误是什么。我是否需要包含一些我忘记的头文件。

最佳答案

更多的是风格评论。我倾向于将您的主要功能分解为单独的功能,每个功能处理不同的问题。

在这个例子中,我使用了 C++ 数据结构,而不是 C 风格的数据结构。

我还使用了 boost::optionalboost::tokenizer

boost 库套件对任何 c++ 开发人员来说都是无价的,因为它提供了 c++ 标准库无法提供的许多基本应用程序编程工具。

#include <boost/optional.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>

#include <iostream>

#include <unistd.h>
#include <sys/wait.h>

using boost::optional;
using std::string;
using std::vector;

void prompt(std::istream& is)
{
if (std::addressof(is) == std::addressof(std::cin))
{
std::cout << "cmd> ";
}
}

optional<string> next_line(std::istream& is)
{
auto result = optional<string>();
std::string line;

while (1)
{
prompt(is);
if (!getline(is, line)) break;
if (!line.empty())
{
result = line;
break;
}
}
return result;
}

vector<string> tokenise(std::string const& source)
{
using char_function = boost::char_separator<char>;

auto tokens = boost::tokenizer<char_function>(source, char_function(" "));

return vector<string>(tokens.begin(), tokens.end());
}

auto to_char_data_vector(vector<string>::iterator first, vector<string>::iterator last) -> vector<char *>
{
auto to_char_data = [](std::string& str) { return str.data(); };
auto result = vector<char *>();
std::transform(first, last,
back_inserter(result),
to_char_data);
return result;
};

auto append_null(vector<char*> v) -> vector<char *>
{
v.push_back(nullptr);
return v;
}

int sub_command(string const& cmd, vector<string>& args) // note - not const.
{
auto do_error = [](const char *message)
{
auto retcode = errno;
std::cerr << message << std::endl;
return retcode;
};

auto pid = fork();
if (pid == -1)
{
return do_error("fork failure");
}
else if (pid == 0)
{
auto result = execv(cmd.data(),
append_null(to_char_data_vector(begin(args),
std::end(args)))
.data());
std::exit(do_error("execv failure"));
}
else
{
int stat = 0;
auto waitresult = waitpid(pid, &stat, 0);
if (waitresult == -1)
return do_error("failed to wait");
return WEXITSTATUS(stat);
}
}

/// @pre tokens.length() >= 1
int interpret(vector<string> const& tokens)
{
auto args = vector<string>(next(begin(tokens)), std::end(tokens));
auto& cmd = tokens[0];

if (boost::iequals(cmd, "exit"))
{
std::exit(args.empty() ? 0 : std::stoi(args[0]));
}
else
{
return sub_command(cmd, args);
}

}

int main()
{
while (auto opt_line = next_line(std::cin))
{
auto tokens = tokenise(*opt_line);
if (tokens.empty()) continue;

auto return_code = interpret(tokens);
std::cout << "command returned: " << return_code << std::endl;
}
}

运行示例:

cmd> /bin/ls .
CMakeCache.txt TryThings.cbp trythings
CMakeFiles _3rdParty
Makefile cmake_install.cmake
command returned: 0
cmd> exit 20

Process finished with exit code 20

请注意,我必须指定 ls 命令的确切路径。 evecv 对搜索路径一无所知。

关于c++ - cin.getline() 开发 shell 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com