Skip to content

Commit

Permalink
Terminal+:Sh: Ctrl+C to send SIGINT to current command
Browse files Browse the repository at this point in the history
  • Loading branch information
fido2020 committed Aug 2, 2021
1 parent 2477b09 commit afe0e3e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
41 changes: 33 additions & 8 deletions Applications/LSh/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <string>
#include <sys/wait.h>
#include <termios.h>
Expand All @@ -15,6 +16,7 @@ termios execAttributes; // Set before executing
termios readAttributes; // Set on ReadLine

std::string ln;
int lnPos = 0;

typedef void (*builtin_call_t)(int, char**);

Expand Down Expand Up @@ -68,13 +70,22 @@ builtin_t builtinExport = {.name = "export", .func = LShBuiltin_Export};
builtin_t builtinClear = {.name = "clear", .func = LShBuiltin_Clear};
builtin_t builtinExit = {.name = "exit", .func = LShBuiltin_Exit};

pid_t job = -1;

void InterruptSignalHandler(int sig){
if(job > 0){
// If we have a current job, send SIGINT to child.
kill(job, SIGINT);
}
}

void ReadLine() {
tcsetattr(STDOUT_FILENO, TCSANOW, &readAttributes);

bool esc = false; // Escape sequence
bool csi = false; // Control sequence indicator

int lnPos = 0; // Line cursor position
lnPos = 0; // Line cursor position
int historyIndex = -1; // History index
ln.clear();

Expand Down Expand Up @@ -206,10 +217,12 @@ void ParseLine() {
errno = 0;

if (strchr(argv[0], '/')) {
pid_t pid = lemon_spawn(argv[0], argc, argv.data(), 1);
if (pid > 0) {
job = lemon_spawn(argv[0], argc, argv.data(), 1);
if (job > 0) {
int status = 0;
waitpid(pid, &status, 0);
int ret = 0;
while((ret = waitpid(job, &status, 0)) == 0 || (ret < 0 && errno == SIGINT))
;
} else if (errno == ENOENT) {
printf("\nNo such file or directory: %s\n", argv[0]);
} else {
Expand All @@ -222,11 +235,12 @@ void ParseLine() {
return;
} else
for (std::string path : path) {
pid_t pid = lemon_spawn((path + "/" + argv[0]).c_str(), argc, argv.data(), 1);
if (pid > 0) {
job = lemon_spawn((path + "/" + argv[0]).c_str(), argc, argv.data(), 1);
if (job > 0) {
int status = 0;
waitpid(pid, &status, 0);

int ret = 0;
while((ret = waitpid(job, &status, 0)) == 0 || (ret < 0 && errno == SIGINT))
;
return;
} else if (errno == ENOENT) {
continue;
Expand Down Expand Up @@ -256,6 +270,17 @@ int main() {
readAttributes = execAttributes;
readAttributes.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echo when reading user input

struct sigaction action = {
.sa_handler = InterruptSignalHandler,
.sa_flags = 0,
};
sigemptyset(&action.sa_mask);

if(sigaction(SIGINT, &action, nullptr)){
perror("sigaction");
return 99;
}

builtins.push_back(builtinPwd);
builtins.push_back(builtinCd);
builtins.push_back(builtinExport);
Expand Down
8 changes: 6 additions & 2 deletions Applications/Terminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <stdio.h>
#include <ctype.h>
#include <termios.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/wait.h>

Expand Down Expand Up @@ -522,7 +523,7 @@ void PrintChar(char ch){

char buf[512];

while(waitpid(lsh, nullptr, WNOHANG) <= 0){
while(waitpid(lsh, nullptr, WNOHANG) == 0){
poll(fds.data(), fds.size(), 500000); // Wake up every 500ms to check if LSh has exited

bool needsPaint = false;
Expand Down Expand Up @@ -564,6 +565,7 @@ int main(int argc, char** argv){

syscall(SYS_GRANT_PTY, (uintptr_t)&masterPTYFd, 0, 0, 0, 0);
setenv("TERM", "xterm-256color", 1); // the Lemon OS terminal is (fairly) xterm compatible (256 colour, etc.)
chdir("/system");

char* const _argv[] = {const_cast<char*>("/system/bin/lsh.lef")};
lsh = lemon_spawn(_argv[0], 1, _argv, 1);
Expand Down Expand Up @@ -596,7 +598,9 @@ int main(int argc, char** argv){
Lemon::LemonEvent ev;
while(window->PollEvent(ev)){
if(ev.event == Lemon::EventKeyPressed){
if(ev.key == KEY_ARROW_UP){
if(ev.keyModifiers & KeyModifier_Control && tolower(ev.key) == 'c'){
kill(lsh, SIGINT); // Send SIGINT to child
} else if(ev.key == KEY_ARROW_UP){
const char* esc = "\e[A";
write(masterPTYFd, esc, strlen(esc));
} else if(ev.key == KEY_ARROW_DOWN){
Expand Down

0 comments on commit afe0e3e

Please sign in to comment.