Terminal: Add -e (execute) command line option

This allows a user to specify a command to run after opening the terminal
program. By default it will still spawn an interactive shell.
This commit is contained in:
Conrad Pankoff 2019-09-02 15:28:52 +10:00 committed by Andreas Kling
parent 17cf3c143a
commit 1f56612c8a

View file

@ -1,5 +1,6 @@
#include "TerminalWidget.h" #include "TerminalWidget.h"
#include <Kernel/KeyCode.h> #include <Kernel/KeyCode.h>
#include <LibCore/CArgsParser.h>
#include <LibCore/CUserInfo.h> #include <LibCore/CUserInfo.h>
#include <LibDraw/PNGLoader.h> #include <LibDraw/PNGLoader.h>
#include <LibGUI/GAction.h> #include <LibGUI/GAction.h>
@ -23,7 +24,7 @@
#include <sys/select.h> #include <sys/select.h>
#include <unistd.h> #include <unistd.h>
static void make_shell(int ptm_fd) static void run_command(int ptm_fd, String command)
{ {
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
@ -71,7 +72,11 @@ static void make_shell(int ptm_fd)
perror("ioctl(TIOCSCTTY)"); perror("ioctl(TIOCSCTTY)");
exit(1); exit(1);
} }
const char* args[] = { "/bin/Shell", nullptr }; const char* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr };
if (!command.is_empty()) {
args[1] = "-c";
args[2] = command.characters();
}
const char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr }; const char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr };
rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs)); rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs));
if (rc < 0) { if (rc < 0) {
@ -133,6 +138,12 @@ int main(int argc, char** argv)
{ {
GApplication app(argc, argv); GApplication app(argc, argv);
CArgsParser args_parser("Terminal");
args_parser.add_arg("e", "execute", "Execute this command inside the terminal.");
CArgsParserResult args = args_parser.parse(argc, argv);
if (chdir(get_current_user_home_path().characters()) < 0) if (chdir(get_current_user_home_path().characters()) < 0)
perror("chdir"); perror("chdir");
@ -142,7 +153,7 @@ int main(int argc, char** argv)
return 1; return 1;
} }
make_shell(ptm_fd); run_command(ptm_fd, args.get("e"));
auto* window = new GWindow; auto* window = new GWindow;
window->set_title("Terminal"); window->set_title("Terminal");