Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
624a838f1a | |||
46bc7347a7 | |||
c9356571e8 | |||
fb9be77a50 | |||
025e95c020 | |||
40f2b2ee6b | |||
dd2e2ad50b |
19 changed files with 366 additions and 115 deletions
56
README.md
56
README.md
|
@ -1,17 +1,41 @@
|
||||||
# Msh-console
|
<!-- vim: set et ts=2 sw=2 tw=80 : -->
|
||||||
* included flag handling implemented with popt.h;
|
|
||||||
Library that provides a bash-like command line for C++ programs. Features included:
|
# msh-console
|
||||||
* creation of personalized commands in the same thread of the shell or in other threads;
|
|
||||||
* basic shell functionalities, such as execution of other programs in the system. This is implemented with execvp().
|
Library that provides a bash-like command line interface for C++ programs.
|
||||||
What is not actually included:
|
|
||||||
|
## Abandoned
|
||||||
|
|
||||||
|
This was my first OOP project, so pardon the style and the conventions taken.
|
||||||
|
This project is currently unmantained.
|
||||||
|
|
||||||
|
## Features include:
|
||||||
|
|
||||||
|
* flag handling implemented with `popt.h`;
|
||||||
|
* creation of personalized commands in the same thread of the shell or in other
|
||||||
|
threads;
|
||||||
|
* basic shell functionalities, such as execution of other programs in the
|
||||||
|
system. This is implemented with `execvp()`.
|
||||||
|
* piping, implemented with `<unistd.h>` `pipe()` function. Code based on
|
||||||
|
https://stackoverflow.com/a/5207730.
|
||||||
|
|
||||||
|
## What is not actually included:
|
||||||
|
|
||||||
* advanced shell features, like:
|
* advanced shell features, like:
|
||||||
* piping,
|
* redirection,
|
||||||
* redirection,
|
* scripting,
|
||||||
* scripting,
|
* command history.
|
||||||
* command history.
|
|
||||||
### Credits
|
## Credits
|
||||||
Code based on "Write a Shell in C" tutorial by Stephen Brennan (http://brennan.io/2015/01/16/write-a-shell-in-c/).
|
|
||||||
The code has been slightly modified.
|
Code based on
|
||||||
The code contains stringtoargcargv.cpp, a set of functions written by Bernhard Eder (http://web.archive.org/web/20121030075237/http://bbgen.net/blog/2011/06/string-to-argc-argv) for parsing a string into argc and argv.
|
["Write a Shell in C"](http://brennan.io/2015/01/16/write-a-shell-in-c/)
|
||||||
### Compiling
|
tutorial by Stephen Brennan. The code contains `stringtoargcargv.cpp`, a set of
|
||||||
The library can be compiled as shared library with the CMakeLists.txt file already in msh-console-library/.
|
functions written by Bernhard Eder
|
||||||
|
[here](http://web.archive.org/web/20121030075237/http://bbgen.net/blog/2011/06/string-to-argc-argv)
|
||||||
|
for parsing a string into `argc` and `argv`.
|
||||||
|
|
||||||
|
## Compiling
|
||||||
|
|
||||||
|
The library can be compiled as shared library with the CMakeLists.txt file
|
||||||
|
already in `msh-console-library/`.
|
||||||
|
|
9
aur/.gitignore
vendored
Normal file
9
aur/.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# src folder
|
||||||
|
src/
|
||||||
|
|
||||||
|
# pkg folder
|
||||||
|
pkg/
|
||||||
|
|
||||||
|
# created package
|
||||||
|
*.pkg.tar.xz
|
||||||
|
*.zip
|
46
aur/PKGBUILD
Normal file
46
aur/PKGBUILD
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
# Maintainer : praticamentetilde
|
||||||
|
_name=libmshconsole
|
||||||
|
_branch=master
|
||||||
|
|
||||||
|
pkgname=libmshconsole
|
||||||
|
pkgver=0.40f2b2e
|
||||||
|
pkgrel=1
|
||||||
|
|
||||||
|
pkgdesc='Library that provides a bash-like interface for CLI C++ programs'
|
||||||
|
url='https://github.com/praticamentetilde/msh-console.git'
|
||||||
|
arch=('any')
|
||||||
|
license=('custom:unlicense')
|
||||||
|
|
||||||
|
depends=()
|
||||||
|
makedepends=('git' 'gcc' 'cmake')
|
||||||
|
optdepends=()
|
||||||
|
|
||||||
|
headers=('commandexecutor.h' 'command.h' 'shell.h' 'stringtoargcargv.h')
|
||||||
|
provides=()
|
||||||
|
conflicts=()
|
||||||
|
|
||||||
|
pkgver() {
|
||||||
|
DATE='0'
|
||||||
|
HASH=$(git ls-remote -h $url $_branch | cut -c1-7)
|
||||||
|
echo $DATE.$HASH
|
||||||
|
}
|
||||||
|
|
||||||
|
source=("$pkgname-$(pkgver).zip::https://github.com/praticamentetilde/msh-console/archive/$_branch.zip")
|
||||||
|
sha512sums=(SKIP)
|
||||||
|
|
||||||
|
#prepare() {}
|
||||||
|
|
||||||
|
package() {
|
||||||
|
cd msh-console-$_branch/library
|
||||||
|
cmake CMakeLists.txt
|
||||||
|
make
|
||||||
|
mkdir -p "$pkgdir"/usr/include/mshconsole
|
||||||
|
chmod 0755 "$pkgdir"/usr/include/mshconsole
|
||||||
|
mv libmshconsole.so libmshconsole.so.1
|
||||||
|
install -D -m644 libmshconsole.so.1 "$pkgdir"/usr/lib/libmshconsole.so.1
|
||||||
|
ln -s "$pkgdir"/usr/lib/libmshconsole.so.1 "$pkgdir"/usr/lib/libmshconsole.so
|
||||||
|
for i in "${headers[@]}"
|
||||||
|
do
|
||||||
|
install -D -m644 $i "$pkgdir"/usr/include/mshconsole/"$i"
|
||||||
|
done
|
||||||
|
}
|
2
aur/README.md
Normal file
2
aur/README.md
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
# msh-console-aur
|
||||||
|
AUR package for msh-console
|
|
@ -25,14 +25,15 @@ cmake_minimum_required(VERSION 2.8)
|
||||||
|
|
||||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpopt")
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpopt")
|
||||||
list(APPEND SRC_LIST "command.cpp" "commandexecutor.h" "commands.cpp" "datas.cpp" "shell.cpp" "stringtoargcargv.cpp"
|
list(APPEND SRC_LIST "command.cpp" "commandexecutor.h" "commands.cpp" "datas.cpp" "shell.cpp" "stringtoargcargv.cpp"
|
||||||
"commandexecutor.cpp" "command.h" "data.cpp" "main.cpp" "shell.h" "stringtoargcargv.h")
|
"commandexecutor.cpp" "command.h" "data.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/../snippet/main.cpp" "shell.h" "stringtoargcargv.h")
|
||||||
|
|
||||||
set(lib ON) #off=debug demo with main.cpp, on=library
|
set(lib Off) #off=debug demo with main.cpp, on=library
|
||||||
if(${lib})
|
if(${lib})
|
||||||
MESSAGE(STATUS "Building .so.1")
|
MESSAGE(STATUS "Building .so.1")
|
||||||
list(REMOVE_ITEM SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) #remove demo file
|
list(REMOVE_ITEM SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/../msh-console-script/main.cpp) #remove snippet file
|
||||||
add_library (mshconsole SHARED ${SRC_LIST})
|
add_library (mshconsole SHARED ${SRC_LIST})
|
||||||
else(${lib})
|
else(${lib})
|
||||||
MESSAGE(STATUS "Building exec (debug only)")
|
MESSAGE(STATUS "Building msh-console-snippet (debug only)")
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
|
||||||
add_executable(${PROJECT_NAME} ${SRC_LIST})
|
add_executable(${PROJECT_NAME} ${SRC_LIST})
|
||||||
endif(${lib})
|
endif(${lib})
|
|
@ -43,8 +43,6 @@ namespace mshconsole {
|
||||||
int Command::execute(const struct Params& p, CommandExecutor* c, std::ostream& errorStream){
|
int Command::execute(const struct Params& p, CommandExecutor* c, std::ostream& errorStream){
|
||||||
if(p.argc<=0) return -1;
|
if(p.argc<=0) return -1;
|
||||||
|
|
||||||
// options a, b, c take integer arguments
|
|
||||||
// options f and g take no arguments
|
|
||||||
poptContext pc;
|
poptContext pc;
|
||||||
opts.push_back(POPT_TERMINATOR);
|
opts.push_back(POPT_TERMINATOR);
|
||||||
|
|
||||||
|
@ -92,6 +90,8 @@ namespace mshconsole {
|
||||||
while (poptPeekArg(pc) != NULL)
|
while (poptPeekArg(pc) != NULL)
|
||||||
nonOptionArgs.push_back(const_cast<const char*>(poptGetArg(pc)));
|
nonOptionArgs.push_back(const_cast<const char*>(poptGetArg(pc)));
|
||||||
|
|
||||||
|
poptFreeContext(pc);
|
||||||
|
|
||||||
return (*funcCommand)(c,datas,nonOptionArgs);
|
return (*funcCommand)(c,datas,nonOptionArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,5 +110,10 @@ namespace mshconsole {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Command::~Command(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
const struct poptOption Command::POPT_TERMINATOR = {NULL,'\0',POPT_ARG_NONE,NULL,-1,NULL,NULL};
|
const struct poptOption Command::POPT_TERMINATOR = {NULL,'\0',POPT_ARG_NONE,NULL,-1,NULL,NULL};
|
||||||
}
|
}
|
|
@ -41,7 +41,7 @@ namespace mshconsole{
|
||||||
ExitException(int c=0) : code(c){}
|
ExitException(int c=0) : code(c){}
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
virtual int executeCmd(const struct Params& args) = 0;
|
virtual int executeCmd(const struct Params& args, bool isPipe=false) = 0;
|
||||||
virtual int executeCmd(const std::string& args) = 0;
|
virtual int executeCmd(const std::string& args) = 0;
|
||||||
virtual size_t howManyCmds() const = 0;
|
virtual size_t howManyCmds() const = 0;
|
||||||
void exit(int code=0);
|
void exit(int code=0);
|
|
@ -46,6 +46,14 @@ namespace mshconsole {
|
||||||
}
|
}
|
||||||
throw CommandNotFoundException();
|
throw CommandNotFoundException();
|
||||||
}
|
}
|
||||||
|
bool Shell::Commands::found(const std::string& s, bool launchThread){
|
||||||
|
for(unsigned int i=0; i<(launchThread ? threadCommands.size() : commands.size()); i++){
|
||||||
|
if((launchThread ? threadCommands[i]->getName() : commands[i]->getName())==s){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,11 +60,10 @@ namespace mshconsole {
|
||||||
|
|
||||||
//Run the shell loop.
|
//Run the shell loop.
|
||||||
int exitCode;
|
int exitCode;
|
||||||
|
std::string* line=nullptr;
|
||||||
|
std::vector<std::string> p;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
std::string* line=nullptr;
|
|
||||||
struct Params p;
|
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
delete line;
|
delete line;
|
||||||
outputStream << ps << " "; //Print the prompt.
|
outputStream << ps << " "; //Print the prompt.
|
||||||
|
@ -89,10 +88,11 @@ namespace mshconsole {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Execute the command.
|
//Execute the pipe.
|
||||||
executeCmd(p);
|
executePipe(p);
|
||||||
};
|
};
|
||||||
} catch(CommandExecutor::ExitException c) {
|
} catch(CommandExecutor::ExitException c) {
|
||||||
|
delete line;
|
||||||
exitCode=c.getCode();
|
exitCode=c.getCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +103,103 @@ namespace mshconsole {
|
||||||
return exitCode;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Shell::executePipe(std::vector<std::string>& argv)
|
||||||
|
{
|
||||||
|
int newPipe[2], oldPipe[2], status;
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
|
std::vector<std::vector<std::string>> argvs;
|
||||||
|
|
||||||
|
size_t c=0, i=0;
|
||||||
|
for(; i<argv.size(); i++){
|
||||||
|
if(argv[i]=="|"){ //Pipe reached
|
||||||
|
if(c!=i){
|
||||||
|
//Write the command in the pipe std::vector
|
||||||
|
argvs.push_back(std::vector<std::string>(argv.begin()+c, argv.begin()+i));
|
||||||
|
}
|
||||||
|
c=i+1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(c!=i){
|
||||||
|
//Write the last command in the pipe std::vector
|
||||||
|
argvs.push_back(std::vector<std::string>(argv.begin()+c, argv.end()));
|
||||||
|
}
|
||||||
|
if(argvs.size()==1){
|
||||||
|
struct Params p;
|
||||||
|
vectorToArgcArgv(argvs[0], &p.argc, &p.argv);
|
||||||
|
return executeCmd(p,false);
|
||||||
|
}
|
||||||
|
for(size_t i=0; i<argvs.size(); i++){
|
||||||
|
if(cmds.found(std::string(argvs[i][0]),false)){
|
||||||
|
errorStream << name << ": non-thread command " << argv[0] << " can't be launched from a pipe" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(size_t i=0; i<argvs.size(); i++) /* For each command */
|
||||||
|
{
|
||||||
|
struct Params p;
|
||||||
|
vectorToArgcArgv(argvs[i], &p.argc, &p.argv);
|
||||||
|
|
||||||
|
/* If there still are commands to be executed */
|
||||||
|
if(i < argvs.size()-1)
|
||||||
|
{
|
||||||
|
pipe(newPipe); /* just create a pipe */
|
||||||
|
}
|
||||||
|
|
||||||
|
pid = fork();
|
||||||
|
|
||||||
|
if(pid == 0) /* Child */
|
||||||
|
{
|
||||||
|
/* If there is a previous command */
|
||||||
|
if(i > 0)
|
||||||
|
{
|
||||||
|
close(oldPipe[1]);
|
||||||
|
dup2(oldPipe[0], 0); //redirect stdin to input of the pipe
|
||||||
|
close(oldPipe[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If there still are commands to be executed */
|
||||||
|
if(i < argvs.size()-1)
|
||||||
|
{
|
||||||
|
close(newPipe[0]);
|
||||||
|
dup2(newPipe[1], 1); //redirect stdout to output of the pipe
|
||||||
|
close(newPipe[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Execute
|
||||||
|
exit(executeCmd(p,true));
|
||||||
|
}
|
||||||
|
else if (pid < 0) {
|
||||||
|
// Error forking
|
||||||
|
errorStream << name <<": error in process forking.\n";
|
||||||
|
}
|
||||||
|
else /* Father */
|
||||||
|
{
|
||||||
|
/* If there is a previous command */
|
||||||
|
if(i > 0)
|
||||||
|
{
|
||||||
|
close(oldPipe[0]);
|
||||||
|
close(oldPipe[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* do we have a next command? */
|
||||||
|
if(i < argvs.size()-1)
|
||||||
|
{
|
||||||
|
oldPipe[0] = newPipe[0];
|
||||||
|
oldPipe[1] = newPipe[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* wait for last command process? */
|
||||||
|
if(i == argvs.size()-1)
|
||||||
|
{
|
||||||
|
waitpid(pid, &status, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deleteParams(p);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int Shell::launchCmd(const struct Params& p){
|
int Shell::launchCmd(const struct Params& p){
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
|
@ -120,7 +217,9 @@ namespace mshconsole {
|
||||||
catch (CommandNotFoundException){}
|
catch (CommandNotFoundException){}
|
||||||
|
|
||||||
//execute bash command or program
|
//execute bash command or program
|
||||||
if((a=execvp(p.argv[0], p.argv)) == -1) {
|
a=execvp(p.argv[0], p.argv);
|
||||||
|
|
||||||
|
if(a == -1) {
|
||||||
outputStream << name << ": execution failed. Errno is set to: "<<strerror(errno)<<"\n";
|
outputStream << name << ": execution failed. Errno is set to: "<<strerror(errno)<<"\n";
|
||||||
std::exit(EXIT_FAILURE);
|
std::exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
@ -140,21 +239,29 @@ namespace mshconsole {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Shell::executeCmd(const struct Params& p){
|
int Shell::executeCmd(const struct Params& p, bool isPipe){
|
||||||
if (!p.argc) return 1; //Return if the line is empty.
|
if (!p.argc) return 1; //Return if the line is empty.
|
||||||
|
|
||||||
try {
|
if(isPipe){
|
||||||
//Search in non-thread commands.
|
if(cmds.found(p.argv[0],false)){
|
||||||
return cmds.launch(p, false);
|
errorStream << name << ": non-thread command " << p.argv[0] << " can't be launched from a pipe" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
try {
|
||||||
|
//Search in non-thread commands.
|
||||||
|
return cmds.launch(p, false);
|
||||||
|
}
|
||||||
|
catch(CommandNotFoundException) {}
|
||||||
}
|
}
|
||||||
catch(CommandNotFoundException) {}
|
|
||||||
|
|
||||||
//Execute the threadCommand or the executable.
|
//Execute the threadCommand or the executable.
|
||||||
return launchCmd(p);
|
return launchCmd(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Shell::executeCmd(const std::string &args){
|
inline int Shell::executeCmd(const std::string &args){
|
||||||
return executeCmd(splitLine(&args));
|
std::vector<std::string>a =splitLine(&args);
|
||||||
|
return executePipe(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shell::SIGINTHandler(int,siginfo_t*, void *){
|
void Shell::SIGINTHandler(int,siginfo_t*, void *){
|
||||||
|
@ -181,9 +288,14 @@ namespace mshconsole {
|
||||||
struct sigaction* setHandler(unsigned signal, void (*funcptr)(int, siginfo_t*, void*)){
|
struct sigaction* setHandler(unsigned signal, void (*funcptr)(int, siginfo_t*, void*)){
|
||||||
struct sigaction* before = new struct sigaction();
|
struct sigaction* before = new struct sigaction();
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
|
sigset_t mask;
|
||||||
|
sigemptyset(&mask);
|
||||||
|
sigaddset(&mask, SIGTERM);
|
||||||
|
sa.sa_mask=mask;
|
||||||
sa.sa_sigaction = funcptr;
|
sa.sa_sigaction = funcptr;
|
||||||
sa.sa_flags = 0;
|
sa.sa_flags = SA_RESTART;
|
||||||
sigaction(signal, &sa, before);
|
sigaction(signal, &sa, before);
|
||||||
|
return before;
|
||||||
}
|
}
|
||||||
|
|
||||||
void restoreHandler(unsigned signal, struct sigaction* before){
|
void restoreHandler(unsigned signal, struct sigaction* before){
|
||||||
|
@ -206,10 +318,12 @@ namespace mshconsole {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Params Shell::splitLine(const std::string* line){
|
std::vector<std::string> Shell::splitLine(const std::string* line){
|
||||||
struct Params p;
|
return parseStringToVector(*line);
|
||||||
stringToArgcArgv(*line, &(p.argc), &(p.argv));
|
}
|
||||||
return p;
|
|
||||||
|
void Shell::deleteParams(struct Params p){
|
||||||
|
freeString(p.argc,p.argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Shell::SIGINTRaised = false;
|
bool Shell::SIGINTRaised = false;
|
|
@ -55,6 +55,7 @@ namespace mshconsole {
|
||||||
void add(Command*, bool isthread=false);
|
void add(Command*, bool isthread=false);
|
||||||
size_t howMany() const;
|
size_t howMany() const;
|
||||||
int launch(const struct Params& args, bool launchThread=false);
|
int launch(const struct Params& args, bool launchThread=false);
|
||||||
|
bool found(const std::string& args, bool launchThread=false);
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool SIGINTRaised;
|
static bool SIGINTRaised;
|
||||||
|
@ -70,7 +71,9 @@ namespace mshconsole {
|
||||||
int launchCmd(const struct Params& args);
|
int launchCmd(const struct Params& args);
|
||||||
static void SIGINTHandler(int,siginfo_t *info, void *);
|
static void SIGINTHandler(int,siginfo_t *info, void *);
|
||||||
std::string* readLine();
|
std::string* readLine();
|
||||||
struct Params splitLine(const std::string* line);
|
std::vector<std::string> splitLine(const std::string* line);
|
||||||
|
int executePipe(std::vector<std::string>& argv);
|
||||||
|
void deleteParams(struct Params p);
|
||||||
|
|
||||||
class IsUndoingLineException {};
|
class IsUndoingLineException {};
|
||||||
class CommandNotFoundException {};
|
class CommandNotFoundException {};
|
||||||
|
@ -91,7 +94,7 @@ namespace mshconsole {
|
||||||
//for in-shell commands
|
//for in-shell commands
|
||||||
void addCmd(Command *cmd, bool isThread=false);
|
void addCmd(Command *cmd, bool isThread=false);
|
||||||
|
|
||||||
int executeCmd(const struct Params& args);
|
int executeCmd(const struct Params& args, bool isPipe=false);
|
||||||
int executeCmd(const std::string& args);
|
int executeCmd(const std::string& args);
|
||||||
size_t howManyCmds() const;
|
size_t howManyCmds() const;
|
||||||
};
|
};
|
|
@ -27,33 +27,38 @@
|
||||||
#include "stringtoargcargv.h"
|
#include "stringtoargcargv.h"
|
||||||
|
|
||||||
namespace StringToArgcArgv{
|
namespace StringToArgcArgv{
|
||||||
/*
|
|
||||||
* Usage:
|
|
||||||
* int argc;
|
|
||||||
* char** argv;
|
|
||||||
* stringToArgcArgv("foo bar", &argc, &argv);
|
|
||||||
*/
|
|
||||||
void stringToArgcArgv(const std::string& str, int* argc, char*** argv)
|
|
||||||
{
|
|
||||||
std::vector<std::string> args = parse(str);
|
|
||||||
|
|
||||||
*argv = (char**)std::malloc((args.size()+1) * sizeof(char*));
|
static inline bool _isQuote(char c);
|
||||||
|
static inline bool _isEscape(char c);
|
||||||
|
static inline bool _isPipe(char c);
|
||||||
|
static inline bool _isWhitespace(char c);
|
||||||
|
|
||||||
int i=0;
|
void vectorToArgcArgv(const std::vector<std::string>& args, int* argc, char*** argv){
|
||||||
for(std::vector<std::string>::iterator it = args.begin();
|
*argv = new char*[args.size()+1]();
|
||||||
|
int i=0;
|
||||||
|
for(std::vector<std::string>::const_iterator it = args.begin();
|
||||||
it != args.end();
|
it != args.end();
|
||||||
++it, ++i)
|
++it, ++i){
|
||||||
{
|
std::string arg = *it;
|
||||||
std::string arg = *it;
|
(*argv)[i] = new char[arg.length()+1]();
|
||||||
(*argv)[i] = (char*)std::malloc((arg.length()+1) * sizeof(char));
|
std::strcpy((*argv)[i], arg.c_str());
|
||||||
std::strcpy((*argv)[i], arg.c_str());
|
}
|
||||||
}
|
(*argv)[i]=NULL;
|
||||||
(*argv)[i]=NULL;
|
*argc = args.size();
|
||||||
|
|
||||||
*argc = args.size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> parse(const std::string& args)
|
void stringToArgcArgv(const std::string& str, int* argc, char*** argv){
|
||||||
|
return vectorToArgcArgv(parseStringToVector(str),argc,argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeString(int& argc, char**& argv){
|
||||||
|
for(int i=0; i<argc; i++){
|
||||||
|
delete[] argv[i];
|
||||||
|
}
|
||||||
|
delete[] argv;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> parseStringToVector(const std::string& args)
|
||||||
{
|
{
|
||||||
std::stringstream ain(args); // used to iterate over input string
|
std::stringstream ain(args); // used to iterate over input string
|
||||||
ain >> std::noskipws; // do not skip white spaces
|
ain >> std::noskipws; // do not skip white spaces
|
||||||
|
@ -94,19 +99,21 @@ namespace StringToArgcArgv{
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(_isWhitespace(c)) {
|
else if(_isWhitespace(c) || _isPipe(c)) {
|
||||||
switch(currentState) {
|
switch(currentState) {
|
||||||
case InArg:
|
case InArg:
|
||||||
oargs.push_back(currentArg.str());
|
oargs.push_back(currentArg.str());
|
||||||
currentState = OutOfArg;
|
if(_isPipe(c)) oargs.push_back("|");
|
||||||
break;
|
currentState = OutOfArg;
|
||||||
|
break;
|
||||||
case InArgQuote:
|
case InArgQuote:
|
||||||
currentArg << c;
|
currentArg << c;
|
||||||
break;
|
break;
|
||||||
case OutOfArg:
|
case OutOfArg:
|
||||||
// nothing
|
if(_isPipe(c)) oargs.push_back("|");
|
||||||
break;
|
// nothing
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if(_isEscape(c)) {
|
else if(_isEscape(c)) {
|
||||||
switch(currentState) {
|
switch(currentState) {
|
||||||
|
@ -167,8 +174,7 @@ namespace StringToArgcArgv{
|
||||||
return oargs;
|
return oargs;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _isQuote(char c)
|
static inline bool _isQuote(char c){
|
||||||
{
|
|
||||||
if(c == '\"')
|
if(c == '\"')
|
||||||
return true;
|
return true;
|
||||||
else if(c == '\'')
|
else if(c == '\'')
|
||||||
|
@ -177,16 +183,14 @@ namespace StringToArgcArgv{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _isEscape(char c)
|
static inline bool _isEscape(char c){
|
||||||
{
|
|
||||||
if(c == '\\')
|
if(c == '\\')
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _isWhitespace(char c)
|
static inline bool _isWhitespace(char c){
|
||||||
{
|
|
||||||
if(c == ' ')
|
if(c == ' ')
|
||||||
return true;
|
return true;
|
||||||
else if(c == '\t')
|
else if(c == '\t')
|
||||||
|
@ -194,4 +198,9 @@ namespace StringToArgcArgv{
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline bool _isPipe(char c){
|
||||||
|
if(c == '|') return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -36,11 +36,9 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
namespace StringToArgcArgv{
|
namespace StringToArgcArgv{
|
||||||
bool _isQuote(char c);
|
void vectorToArgcArgv(const std::vector<std::string>& args, int* argc, char*** argv);
|
||||||
bool _isEscape(char c);
|
std::vector<std::string> parseStringToVector(const std::string& args);
|
||||||
bool _isEscape(char c);
|
|
||||||
bool _isWhitespace(char c);
|
|
||||||
std::vector<std::string> parse(const std::string& args);
|
|
||||||
void stringToArgcArgv(const std::string& str, int* argc, char*** argv);
|
void stringToArgcArgv(const std::string& str, int* argc, char*** argv);
|
||||||
|
void freeString(int& argc, char**& argv);
|
||||||
}
|
}
|
||||||
#endif //STRINGTOARGCARGV_H
|
#endif //STRINGTOARGCARGV_H
|
25
license.md
25
license.md
|
@ -1,24 +1,7 @@
|
||||||
This is free and unencumbered software released into the public domain.
|
2017 Claudio Maggioni (praticamentetilde)
|
||||||
|
|
||||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
distribute this software, either in source code form or as a compiled
|
|
||||||
binary, for any purpose, commercial or non-commercial, and by any
|
|
||||||
means.
|
|
||||||
|
|
||||||
In jurisdictions that recognize copyright laws, the author or authors
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
of this software dedicate any and all copyright interest in the
|
|
||||||
software to the public domain. We make this dedication for the benefit
|
|
||||||
of the public at large and to the detriment of our heirs and
|
|
||||||
successors. We intend this dedication to be an overt act of
|
|
||||||
relinquishment in perpetuity of all present and future rights to this
|
|
||||||
software under copyright law.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
||||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
||||||
OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
For more information, please refer to <http://unlicense.org>
|
|
||||||
|
|
45
snippet/.gitignore
vendored
Normal file
45
snippet/.gitignore
vendored
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#kate directory files
|
||||||
|
*.directory
|
||||||
|
|
||||||
|
#Qt creator .pro.user
|
||||||
|
*.pro.user
|
||||||
|
*.cbp
|
||||||
|
*.txt.user
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
|
||||||
|
# cmake
|
||||||
|
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
|
@ -1,8 +1,8 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../msh-console-library/shell.h"
|
#include "../library/shell.h"
|
||||||
#include "../msh-console-library/command.h"
|
#include "../library/command.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace mshconsole;
|
using namespace mshconsole;
|
||||||
|
|
||||||
|
@ -55,17 +55,21 @@ int main(int argc, char **argv)
|
||||||
string c = "msh-console-test";
|
string c = "msh-console-test";
|
||||||
string ps = "[msh-console-test]:";
|
string ps = "[msh-console-test]:";
|
||||||
Shell mshConsoleTest(c, ps, cin,cout,cerr, &setup);
|
Shell mshConsoleTest(c, ps, cin,cout,cerr, &setup);
|
||||||
Command* help = new Command("help", &helpExecute);
|
Command help("help", &helpExecute);
|
||||||
struct poptOption h = {"help",'h',POPT_ARG_NONE,NULL,1000,"advance","advaced options"};
|
struct poptOption h = {"help",'h',POPT_ARG_NONE,NULL,1000,"advance","advaced options"};
|
||||||
struct poptOption p = {"prova",'p',POPT_ARG_STRING,NULL,1000,"cose","cosecose"};
|
struct poptOption p = {"prova",'p',POPT_ARG_STRING,NULL,1000,"cose","cosecose"};
|
||||||
help->addOption(h);
|
help.addOption(h);
|
||||||
help->addOption(p);
|
help.addOption(p);
|
||||||
|
|
||||||
|
Command exit("exit", &exitExecute);
|
||||||
|
Command cd("cd", &cdExecute);
|
||||||
|
|
||||||
//add builtin commands
|
//add builtin commands
|
||||||
mshConsoleTest.addCmd(help);
|
mshConsoleTest.addCmd(&help, true);
|
||||||
mshConsoleTest.addCmd(new Command("exit", &exitExecute));
|
mshConsoleTest.addCmd(&cd);
|
||||||
mshConsoleTest.addCmd(new Command("cd", &cdExecute));
|
mshConsoleTest.addCmd(&exit);
|
||||||
mshConsoleTest.launch();
|
mshConsoleTest.launch();
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue