简体   繁体   中英

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. 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. 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 ?

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 .

$ 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? Thanks!

I decided to just use a function that calls std::exit(0). This may/may not be ideal, but since my program is small I'm hoping it should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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