简体   繁体   English

如何安全地关闭外壳的 C++ 模拟

[英]How to safely close a C++ mimic of a shell

I'm trying to code a basic shell in C++ that displays a prompt ( $ ) and waits for user input.我正在尝试用 C++ 编写一个基本的 shell,它显示一个提示 ( $ ) 并等待用户输入。 I can get that down, but the problem is the program ends after the first execution of the user's input.我可以解决这个问题,但问题是程序在第一次执行用户输入后结束。

I have classes for the prompt and commands.我有提示和命令的课程。 The prompt class displays the prompt, gets user input, parses it, and calls the appropriate command->execute() functions. prompt 类显示提示,获取用户输入,解析它,并调用适当的 command->execute() 函数。 After the first command execution, how do I get the program to return to displaying the command prompt and asking for input?第一个命令执行后,如何让程序返回显示命令提示符并要求输入? And how do I safely exit the entire program when the user types exit ?当用户输入exit时,我如何安全地退出整个程序?

Below is a snippet of what happens when I run.下面是我跑步时发生的事情的片段。 What I want is that for the $ to display again (and wait for input), after the hello .我想要的是$hello之后再次显示(并等待输入)。

$ ls -a && echo hello
.   a.log  a.out   command.h  or.h      separate.h  tmp.log
..  and.h  base.h  main.cpp   Prompt.h  tmp.cpp     tst
hello

I guess what I'm wondering is, how do I create my own exit function that causes main to return?我想我想知道的是,如何创建自己的退出函数来导致 main 返回? Thanks!谢谢!

I decided to just use a function that calls std::exit(0).我决定只使用一个调用 std::exit(0) 的函数。 This may/may not be ideal, but since my program is small I'm hoping it should be fine.这可能/可能不理想,但由于我的程序很小,我希望它应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM