2016-04-23 11:03:53 +00:00
|
|
|
/**
|
|
|
|
MIT License
|
|
|
|
|
|
|
|
Copyright (c) 2016 Claudio Maggioni
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-03-16 20:15:24 +00:00
|
|
|
#ifndef SHELL_H
|
|
|
|
#define SHELL_H
|
2016-04-16 17:21:39 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-03-16 20:15:24 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-04-02 14:28:03 +00:00
|
|
|
#include <cstddef>
|
2016-03-16 20:15:24 +00:00
|
|
|
#include <csignal>
|
2016-04-23 11:03:53 +00:00
|
|
|
#include <cerrno>
|
2016-03-16 20:15:24 +00:00
|
|
|
#include "command.h"
|
2016-04-16 17:21:39 +00:00
|
|
|
#include <wordexp.h>
|
2016-03-16 20:15:24 +00:00
|
|
|
#include "commandexecutor.h"
|
2016-04-16 17:21:39 +00:00
|
|
|
#include "stringtoargcargv.h"
|
2016-04-23 11:03:53 +00:00
|
|
|
using namespace StringToArgcArgv;
|
2016-03-16 20:15:24 +00:00
|
|
|
|
2016-03-18 17:48:16 +00:00
|
|
|
namespace mshconsole {
|
|
|
|
|
|
|
|
class Shell : public CommandExecutor
|
2016-03-16 20:15:24 +00:00
|
|
|
{
|
2016-03-18 17:48:16 +00:00
|
|
|
class Commands
|
|
|
|
{
|
|
|
|
Shell* parent;
|
2016-04-23 11:03:53 +00:00
|
|
|
std::vector<Command*> commands; //commands that work the same thread of the shell
|
|
|
|
std::vector<Command*> threadCommands; //commands that work on a different thread
|
2016-03-16 20:15:24 +00:00
|
|
|
|
2016-03-18 17:48:16 +00:00
|
|
|
public:
|
|
|
|
Commands(Shell*);
|
|
|
|
void add(Command*, bool isthread=false);
|
|
|
|
size_t howMany() const;
|
2016-04-16 17:21:39 +00:00
|
|
|
int launch(const struct Params& args, bool launchThread=false);
|
2016-05-21 16:25:20 +00:00
|
|
|
bool found(const std::string& args, bool launchThread=false);
|
2016-03-18 17:48:16 +00:00
|
|
|
};
|
2016-04-02 14:28:03 +00:00
|
|
|
|
2016-04-23 11:03:53 +00:00
|
|
|
static bool SIGINTRaised;
|
2016-03-18 17:48:16 +00:00
|
|
|
Commands cmds;
|
2016-04-23 11:03:53 +00:00
|
|
|
std::string ps;
|
|
|
|
std::string name;
|
|
|
|
std::ostream& errorStream;
|
|
|
|
std::ostream& outputStream;
|
|
|
|
std::istream& inputStream;
|
2016-03-18 17:48:16 +00:00
|
|
|
void (*shellSetup)(Shell *);
|
|
|
|
void (*shellPostSetup)(Shell *);
|
2016-03-16 20:15:24 +00:00
|
|
|
|
2016-04-16 17:21:39 +00:00
|
|
|
int launchCmd(const struct Params& args);
|
2016-04-23 11:03:53 +00:00
|
|
|
static void SIGINTHandler(int,siginfo_t *info, void *);
|
|
|
|
std::string* readLine();
|
2016-05-21 16:25:20 +00:00
|
|
|
std::vector<std::string> splitLine(const std::string* line);
|
|
|
|
int executePipe(std::vector<std::string>& argv);
|
2016-05-01 14:32:41 +00:00
|
|
|
void deleteParams(struct Params p);
|
2016-03-16 20:15:24 +00:00
|
|
|
|
2016-03-18 17:48:16 +00:00
|
|
|
class IsUndoingLineException {};
|
|
|
|
class CommandNotFoundException {};
|
2016-03-16 20:15:24 +00:00
|
|
|
|
2016-03-18 17:48:16 +00:00
|
|
|
public:
|
2016-04-23 11:03:53 +00:00
|
|
|
Shell(std::string n, std::string ps, std::istream& inputStream, std::ostream& outputStream,
|
|
|
|
std::ostream& errorStream, void (*s)(Shell*)=0, void (*pss)(Shell*)=0);
|
|
|
|
const std::string& getPs() const;
|
|
|
|
void setPs(const std::string &value);
|
|
|
|
const std::string& getName() const;
|
|
|
|
void setName(const std::string &value);
|
2016-04-02 14:28:03 +00:00
|
|
|
int launch();
|
2016-03-18 17:48:16 +00:00
|
|
|
void setShellSetup(void (*)(Shell *));
|
2016-04-23 11:03:53 +00:00
|
|
|
void (*getShellSetup())(Shell*) const;
|
2016-03-18 17:48:16 +00:00
|
|
|
void setShellPostSetup(void (*)(Shell *));
|
2016-04-23 11:03:53 +00:00
|
|
|
void (*getShellPostSetup())(Shell*) const;
|
2016-03-16 20:15:24 +00:00
|
|
|
|
2016-03-18 17:48:16 +00:00
|
|
|
//for in-shell commands
|
|
|
|
void addCmd(Command *cmd, bool isThread=false);
|
2016-04-23 11:03:53 +00:00
|
|
|
|
2016-05-21 16:25:20 +00:00
|
|
|
int executeCmd(const struct Params& args, bool isPipe=false);
|
2016-04-23 11:03:53 +00:00
|
|
|
int executeCmd(const std::string& args);
|
|
|
|
size_t howManyCmds() const;
|
2016-03-18 17:48:16 +00:00
|
|
|
};
|
2016-04-23 11:03:53 +00:00
|
|
|
|
|
|
|
extern struct sigaction* setHandler(unsigned signal, void (*funcptr)(int, siginfo_t*, void*));
|
|
|
|
extern void restoreHandler(unsigned signal, struct sigaction* before);
|
2016-03-18 17:48:16 +00:00
|
|
|
}
|
2016-03-16 20:15:24 +00:00
|
|
|
|
|
|
|
#endif // SHELL_H
|