lt

simple terminal emulator
Log | Files | Refs | git clone https://git.ne02ptzero.me/git/lt

commit 562aa0b3d211de8ed9d088ddb3e67f0923cc3786
parent 9f8aa270084a0d7466a78e48c6c5b29bdedc3a90
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu,  7 Jun 2018 19:31:59 +0200

[TTY] NEW: Now creating TTY and executing a shell

Signed-off-by: Ne02ptzero <louis@ne02ptzero.me>

Diffstat:
Mconfig.h | 1+
Alt.h | 14++++++++++++++
Mmain.c | 21+++++++++++----------
Mrun.c | 5++---
Mrun.h | 5++---
Atty.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atty.h | 15+++++++++++++++
7 files changed, 133 insertions(+), 16 deletions(-)

diff --git a/config.h b/config.h @@ -16,5 +16,6 @@ */ // CF: linux/Documentation/CodingStyle #define TABSPACES 8 +#define SHELL "/bin/sh" #endif /* CONFIG_H */ diff --git a/lt.h b/lt.h @@ -0,0 +1,14 @@ +#ifndef LT_H +#define LT_H + +#include "x.h" +#include "term.h" +#include "tty.h" + +typedef struct { + x_window_t x; + term_t t; + tty_t tty; +} lt_t; + +#endif /* LT_H */ diff --git a/main.c b/main.c @@ -1,8 +1,6 @@ #include <stdio.h> #include <locale.h> -#include "x.h" -#include "term.h" #include "run.h" void lt_setenv(x_window_t *x) @@ -15,18 +13,21 @@ void lt_setenv(x_window_t *x) int main(void) { + lt_t lt = { 0 }; + setlocale(LC_CTYPE, ""); XSetLocaleModifiers(""); - x_window_t x = { 0 }; - term_t t = { 0 }; - x_window_init(&x, 80, 25); - term_init(&t, 80, 25); - lt_setenv(&x); + x_window_init(&lt.x, 80, 25); + term_init(&lt.t, 80, 25); + tty_init(&lt.tty); + lt_setenv(&lt.x); + + run(&lt); - run(&x, &t); + x_window_dtr(&lt.x); + term_dtr(&lt.t); + tty_dtr(&lt.tty); - x_window_dtr(&x); - term_dtr(&t); return 0; } diff --git a/run.c b/run.c @@ -1,7 +1,6 @@ #include "run.h" -void run(x_window_t *x, term_t *term) +void run(lt_t *lt) { - (void)x; - (void)term; + (void)lt; } diff --git a/run.h b/run.h @@ -1,9 +1,8 @@ #ifndef RUN_H #define RUN_H -#include "x.h" -#include "term.h" +#include "lt.h" -void run(x_window_t *x, term_t *term); +void run(lt_t *lt); #endif /* RUN_H */ diff --git a/tty.c b/tty.c @@ -0,0 +1,88 @@ +#define _XOPEN_SOURCE 600 +#include <stdlib.h> + +#include "tty.h" +#include "utils.h" +#include "config.h" + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <unistd.h> +#include <fcntl.h> + +#define DUP_ALL(fd) \ + do { \ + dup2(fd, 0); \ + dup2(fd, 1); \ + dup2(fd, 2); \ + } while (0); + +static bool tty_run(tty_t *tty) +{ + char *env[] = { NULL }; /* XXX: Set TERM var */ + pid_t p = fork(); + + if (p == -1) + { + ERROR("Could not fork"); + return false; + } + else if (p == 0) + { + close(tty->master); + setsid(); + if (ioctl(tty->slave, TIOCSCTTY, NULL) == -1) + { + ERROR("ioctl to slave tty failed"); + return false; + } + DUP_ALL(tty->slave); + close(tty->slave); + execle(SHELL, "-" SHELL, NULL, env); + } + else if (p > 0) + { + close(tty->slave); + } + + return true; +} + +bool tty_init(tty_t *tty) +{ + char *slave_name; + + tty->master = posix_openpt(O_RDWR | O_NOCTTY); + if (tty->master == -1) + { + ERROR("Could not open master TTY"); + return false; + } + + grantpt(tty->master); + unlockpt(tty->master); + + slave_name = ptsname(tty->master); + tty->slave = open(slave_name, O_RDWR | O_NOCTTY); + if (tty->slave == -1) + { + close(tty->master); + ERROR("Could not open slave TTY"); + return false; + } + + if (!tty_run(tty)) + { + tty_dtr(tty); + return false; + } + + return true; +} + +void tty_dtr(tty_t *tty) +{ + close(tty->slave); + close(tty->master); +} diff --git a/tty.h b/tty.h @@ -0,0 +1,15 @@ +#ifndef TTY_H +#define TTY_H + +#include <stdbool.h> + +typedef struct { + int master; + int slave; +} tty_t; + +bool tty_init(tty_t *tty); +void tty_dtr(tty_t *tty); +void tty_resize(tty_t *tty, int cols, int rows); + +#endif /* TTY_H */