Skip to content

Commit

Permalink
src: move pty code to protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Feb 3, 2021
1 parent 62c340c commit bfb2d0c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 63 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ else()
set(CMAKE_C_STANDARD 99)
endif()

set(SOURCE_FILES src/server.c src/http.c src/protocol.c src/terminal.c src/utils.c)
set(SOURCE_FILES src/server.c src/http.c src/protocol.c src/utils.c)

include(FindPackageHandleStandardArgs)

Expand Down
48 changes: 47 additions & 1 deletion src/protocol.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
#include <errno.h>
#include <fcntl.h>
#include <json.h>
#include <libwebsockets.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <unistd.h>

#if defined(__OpenBSD__) || defined(__APPLE__)
#include <util.h>
#elif defined(__FreeBSD__)
#include <libutil.h>
#else
#include <pty.h>
#endif

#include "server.h"
#include "terminal.h"
#include "utils.h"

// initial message list
Expand Down Expand Up @@ -87,6 +97,42 @@ static bool check_host_origin(struct lws *wsi) {
return len > 0 && strcasecmp(buf, host_buf) == 0;
}

pid_t pty_fork(int *pty, const char *file, char *const argv[], const char *term) {
pid_t pid = forkpty(pty, NULL, NULL, NULL);

if (pid < 0) {
return pid;
} else if (pid == 0) {
setenv("TERM", term, true);
int ret = execvp(file, argv);
if (ret < 0) {
perror("execvp failed\n");
_exit(-errno);
}
}

// set the file descriptor non blocking
int flags = fcntl(*pty, F_GETFL);
if (flags != -1) {
fcntl(*pty, F_SETFD, flags | O_NONBLOCK);
}
// set the file descriptor close-on-exec
fd_set_cloexec(*pty);

return pid;
}

int pty_resize(int pty, int cols, int rows) {
struct winsize size;

size.ws_col = (unsigned short)cols;
size.ws_row = (unsigned short)rows;
size.ws_xpixel = 0;
size.ws_ypixel = 0;

return ioctl(pty, TIOCSWINSZ, &size);
}

static void close_cb(uv_handle_t *handle) {
struct pty_proc *proc = container_of((uv_pipe_t *)handle, struct pty_proc, pipe);
free(proc);
Expand Down
53 changes: 0 additions & 53 deletions src/terminal.c

This file was deleted.

8 changes: 0 additions & 8 deletions src/terminal.h

This file was deleted.

0 comments on commit bfb2d0c

Please sign in to comment.