mirror of
https://github.com/zoriya/ash.git
synced 2026-06-07 12:14:47 +00:00
Removing the pty and using a getcuryx at the end of the program
This commit is contained in:
@@ -41,10 +41,8 @@ SRC = src/shell.c \
|
||||
src/key_bindings/basic_typing_functions.c \
|
||||
src/key_bindings/default_bindings.c \
|
||||
src/key_bindings/control_commands.c \
|
||||
src/redirections/pty_pipe.c \
|
||||
src/key_bindings/move_commands.c \
|
||||
src/my_ncurses/my_ncurses.c \
|
||||
src/my_ncurses/clear_utils.c \
|
||||
src/my_ncurses/string_utils.c
|
||||
|
||||
OBJ = $(SRC:%.c=%.o)
|
||||
|
||||
@@ -67,6 +67,7 @@ int newline_command(int key, buffer_t *buffer, env_t *env)
|
||||
ret = eval_raw_cmd(buffer->buffer, env);
|
||||
buffer->buffer[0] = '\0';
|
||||
buffer->pos = 0;
|
||||
my_getcuryx(&env->window->y, &env->window->x);
|
||||
}
|
||||
if (env->window && ret >= 0)
|
||||
prompt_prepare(buffer, env);
|
||||
|
||||
@@ -19,7 +19,6 @@ void on_resize(int sig, siginfo_t *info, void *context)
|
||||
{
|
||||
my_getmaxyx(&stdwin->h, &stdwin->w);
|
||||
my_getcuryx(&stdwin->y, &stdwin->x);
|
||||
my_clrtobot();
|
||||
}
|
||||
|
||||
my_window *my_initwin(void)
|
||||
|
||||
@@ -43,8 +43,6 @@ int prompt_run(char *cmd, redirection *inout[2], env_t *env)
|
||||
argv = globbing(argv);
|
||||
if (!argv)
|
||||
return (0);
|
||||
if (env->window && inout[1] == NULL)
|
||||
inout[1] = new_ncurses_pty();
|
||||
if (**argv == '!' && argv[0][1] && argv[0][1] != ' ')
|
||||
return (run_builtin(&builtins[5], argv, inout, env));
|
||||
for (int i = 0; builtins[i].name; i++)
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
** EPITECH PROJECT, 2020
|
||||
** ash
|
||||
** File description:
|
||||
** pty_pipe
|
||||
*/
|
||||
|
||||
#include "redirections.h"
|
||||
#include "my_ncurses.h"
|
||||
#include <malloc.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <termios.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
#define _XOPEN_SOURCE 600
|
||||
#define __USE_XOPEN_EXTENDED
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
const struct redirection_map pty_type = {
|
||||
.key = NULL,
|
||||
.get_fd = &pyt_get_fd,
|
||||
.run_cmd = &pty_get_output,
|
||||
.type = OUTPUT | PIPE | EX_PIPE | PTY
|
||||
};
|
||||
|
||||
int pty_open(char **slave_name)
|
||||
{
|
||||
int master = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
||||
int my_errno;
|
||||
|
||||
*slave_name = NULL;
|
||||
if (master < 0)
|
||||
return (-1);
|
||||
if (grantpt(master) < 0 || unlockpt(master) < 0
|
||||
|| !(*slave_name = ptsname(master))) {
|
||||
my_errno = errno;
|
||||
close(master);
|
||||
errno = my_errno;
|
||||
return (-1);
|
||||
}
|
||||
return (master);
|
||||
}
|
||||
|
||||
struct redirection *new_ncurses_pty(void)
|
||||
{
|
||||
struct redirection *pty = malloc(sizeof(*pty));
|
||||
char *slave;
|
||||
|
||||
if (!pty)
|
||||
return (NULL);
|
||||
pty->type = &pty_type;
|
||||
pty->fd = -1;
|
||||
pty->extra_data = pty_open(&slave);
|
||||
if (pty->extra_data < 0) {
|
||||
perror(SHELL_NAME);
|
||||
return (pty);
|
||||
}
|
||||
pty->fd = open(slave, O_RDWR);
|
||||
if (pty->fd == -1)
|
||||
perror(SHELL_NAME);
|
||||
return (pty);
|
||||
}
|
||||
|
||||
int pyt_get_fd(redirection *pty)
|
||||
{
|
||||
struct termios termios;
|
||||
struct winsize winsize;
|
||||
|
||||
tcgetattr(0, &termios);
|
||||
tcsetattr(pty->fd, TCSANOW, &termios);
|
||||
|
||||
ioctl(0, TIOCGWINSZ, &winsize);
|
||||
ioctl(pty->fd, TIOCSWINSZ, &winsize);
|
||||
dup2(pty->fd, 2);
|
||||
return (pty->fd);
|
||||
}
|
||||
|
||||
void pty_get_output(redirection *pty, env_t *env)
|
||||
{
|
||||
char *line = NULL;
|
||||
size_t size = 0;
|
||||
FILE *file = fdopen(pty->extra_data, "r");
|
||||
|
||||
close(pty->fd);
|
||||
while (getline(&line, &size, file) > 0)
|
||||
my_addstr(env->window, line);
|
||||
if (line) {
|
||||
if (line[0] && !strchr(line, '\n')) {
|
||||
my_attron(BACKGROUND_COLOR, WHITE);
|
||||
my_attron(COLOR, BLACK);
|
||||
my_addstr(env->window, "%\n");
|
||||
my_attrreset();
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
Reference in New Issue
Block a user