Skip to content

Commit

Permalink
Redirect program output to separate window
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Nahnybida committed Aug 31, 2023
1 parent 2b048cf commit 48f8daf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gf2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern "C" {
#define MSG_RECEIVED_DATA ((UIMessage) (UI_MSG_USER + 1))
#define MSG_RECEIVED_CONTROL ((UIMessage) (UI_MSG_USER + 2))
#define MSG_RECEIVED_LOG ((UIMessage) (UI_MSG_USER + 3))
#define MSG_RECEIVED_STDOUT ((UIMessage) (UI_MSG_USER + 4))

// Data structures:

Expand Down Expand Up @@ -142,6 +143,9 @@ FILE *commandLog;
char emptyString;
bool programRunning = true;
const char *vimServerName = "GVIM";

char setPttyCMD[PATH_MAX];
int pseudoTerminalMasterFD = -1;
const char *logPipePath;
const char *controlPipePath;
Array<INIState> presetCommands;
Expand Down Expand Up @@ -558,6 +562,8 @@ void *DebuggerThread(void *) {
const char *setPrompt = "set prompt (gdb) \n";
write(pipeToGDB, setPrompt, strlen(setPrompt));

write(pipeToGDB, setPttyCMD, strlen(setPttyCMD));

char *catBuffer = NULL;
size_t catBufferUsed = 0;
size_t catBufferAllocated = 0;
Expand Down Expand Up @@ -1310,6 +1316,7 @@ void InterfaceAddBuiltinWindowsAndCommands() {
interfaceWindows.Add({ "Files", FilesWindowCreate, nullptr });
interfaceWindows.Add({ "Console", ConsoleWindowCreate, nullptr });
interfaceWindows.Add({ "Log", LogWindowCreate, nullptr });
interfaceWindows.Add({ "STDOUT", STDOUTWindowCreate, nullptr });
interfaceWindows.Add({ "Thread", ThreadWindowCreate, ThreadWindowUpdate });
interfaceWindows.Add({ "Exe", ExecutableWindowCreate, nullptr });

Expand Down Expand Up @@ -1482,6 +1489,8 @@ int WindowMessage(UIElement *, UIMessage message, int di, void *dp) {
free(input);
} else if (message == MSG_RECEIVED_LOG) {
LogReceived(dp);
} else if (message == MSG_RECEIVED_STDOUT) {
STDOUTReceived(dp);
} else if (message == UI_MSG_WINDOW_ACTIVATE) {
DisplaySetPosition(currentFileFull, currentLine, false);
}
Expand Down
72 changes: 72 additions & 0 deletions windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,78 @@ UIElement *CommandsWindowCreate(UIElement *parent) {
return &panel->e;
}

//////////////////////////////////////////////////////
// STDOUT window:
//////////////////////////////////////////////////////

void *STDOUTWindowThread(void *context) {
if (pseudoTerminalMasterFD < 0) {
fprintf(stderr, "Warning: ptty fd invalid\n");
return nullptr;
}

struct pollfd p = { .fd = pseudoTerminalMasterFD, .events = POLLIN };

while (true) {
poll(&p, 1, 10000);

if (p.revents & POLLHUP) {
struct timespec t = { .tv_nsec = 10000000 };
nanosleep(&t, 0);
}

while (true) {
char input[16384];
int length = read(pseudoTerminalMasterFD, input, sizeof(input) - 1);
if (length <= 0) break;
input[length] = 0;
void *buffer = malloc(strlen(input) + sizeof(context) + 1);
memcpy(buffer, &context, sizeof(context));
strcpy((char *) buffer + sizeof(context), input);
UIWindowPostMessage(windowMain, MSG_RECEIVED_STDOUT, buffer);
}
}
}

void STDOUTReceived(void *buffer) {
UICodeInsertContent(*(UICode **) buffer, (char *) buffer + sizeof(void *), -1, false);
UIElementRefresh(*(UIElement **) buffer);
free(buffer);
}

UIElement *STDOUTWindowCreate(UIElement *parent) {
UICode *code = UICodeCreate(parent, 0);

char *pseudoTerminalPath;
int masterFd = posix_openpt(O_RDWR|O_NOCTTY);

if (masterFd < 0) {
UICodeInsertContent(code, "Warning: cannot open pseudo tty", -1, false);
return &code->e;
}
if (grantpt(masterFd) < 0) {
UICodeInsertContent(code, "Warning: failed to change the mode and ownership of the slave pseudo-terminal", -1, false);
return &code->e;
}
if (unlockpt(masterFd) < 0) {
UICodeInsertContent(code, "Warning: failed to unlock slave pseudo-terminal", -1, false);
return &code->e;
}

pseudoTerminalPath = ptsname(masterFd);
if (!pseudoTerminalPath) {
UICodeInsertContent(code, "Warning: failed to change the mode and ownership of the slave pseudo-terminal", -1, false);
return &code->e;
}

StringFormat(setPttyCMD, sizeof(setPttyCMD), "tty %s\n", pseudoTerminalPath);
pseudoTerminalMasterFD = masterFd;;

pthread_t thread;
pthread_create(&thread, nullptr, STDOUTWindowThread, code);
return &code->e;
}

//////////////////////////////////////////////////////
// Log window:
//////////////////////////////////////////////////////
Expand Down

0 comments on commit 48f8daf

Please sign in to comment.