redirect #17
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,5 +6,6 @@ myshellin
|
||||
*.gz
|
||||
*.aux
|
||||
*.out
|
||||
compile_commands.json
|
||||
.clangd
|
||||
docs
|
||||
refman.pdf
|
||||
|
1
LICENSE
1
LICENSE
@ -1,5 +1,4 @@
|
||||
Copyright (c) 2021 Christopher Cromer
|
||||
Copyright (c) 2021 Raúl Hernandez
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
|
45
Makefile
45
Makefile
@ -1,32 +1,43 @@
|
||||
CC=clang
|
||||
CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c11
|
||||
CFLAGS=-Wall -Isrc/include -g -std=c11
|
||||
LDFLAGS=
|
||||
FILENAME=myshellin
|
||||
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c src/utils.c
|
||||
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c src/utils.c src/redirect.c
|
||||
OBJ=$(SRC:.c=.o)
|
||||
|
||||
all: myshellin
|
||||
all: shell doc report
|
||||
|
||||
myshellin: $(OBJ)
|
||||
shell: $(OBJ)
|
||||
$(CC) $(CFLAGS) -o $(FILENAME) $^ $(LDFLAGS)
|
||||
|
||||
informe:
|
||||
# if pdflatex is installed create the informe
|
||||
ifneq (, $(shell which pdflatex))
|
||||
make -C doc
|
||||
mv doc/Informe.pdf Informe.pdf
|
||||
else ifneq (, $(shell which pdftex))
|
||||
make -C doc
|
||||
mv doc/Informe.pdf Informe.pdf
|
||||
doc:
|
||||
# if doxygen is install create the code documentation
|
||||
ifneq (, $(shell which doxygen))
|
||||
doxygen doxygen.conf
|
||||
make -C docs/latex
|
||||
mv docs/latex/refman.pdf refman.pdf
|
||||
endif
|
||||
|
||||
clean: cleanmyshellin
|
||||
report:
|
||||
# if pdflatex is installed create the report
|
||||
ifneq (, $(shell which pdflatex))
|
||||
make -C report
|
||||
mv report/Informe.pdf Informe.pdf
|
||||
else ifneq (, $(shell which pdftex))
|
||||
make -C report
|
||||
mv report/Informe.pdf Informe.pdf
|
||||
endif
|
||||
|
||||
cleanmyshellin:
|
||||
clean: cleanshell cleandoc cleanreport
|
||||
|
||||
cleanshell:
|
||||
rm -f src/*.o $(FILENAME)
|
||||
|
||||
cleaninforme:
|
||||
make -C doc clean
|
||||
cleandoc:
|
||||
rm -rf refman.pdf docs
|
||||
|
||||
cleanreport:
|
||||
make -C report clean
|
||||
rm -f Informe.pdf
|
||||
|
||||
.PHONY: all myshellin informe clean cleanmyshellin cleaninforme
|
||||
.PHONY: all shell doc report clean cleanshell cleandoc cleanreport
|
||||
|
11
README.md
11
README.md
@ -3,7 +3,6 @@ myshellin es un proyecto semestral del ramo Sistemas Operativos. El propósito d
|
||||
|
||||
## Autores
|
||||
- Christopher Cromer
|
||||
- Raúl Raúl Hernandez
|
||||
|
||||
## Requisitos software
|
||||
- glibc
|
||||
@ -14,13 +13,19 @@ myshellin es un proyecto semestral del ramo Sistemas Operativos. El propósito d
|
||||
- latex
|
||||
- pdflatex
|
||||
|
||||
## Requisitos documentacón
|
||||
- doxygen
|
||||
|
||||
## Compilar
|
||||
|
||||
### Software
|
||||
make myshellin
|
||||
make shell
|
||||
|
||||
### Informe
|
||||
make informe
|
||||
make report
|
||||
|
||||
### Documentación
|
||||
make doc
|
||||
|
||||
## Licencia
|
||||
El proyecto es bajo la licencia "[The 3-Clause BSD License](LICENSE)".
|
||||
|
122
compile_commands.json
Normal file
122
compile_commands.json
Normal file
@ -0,0 +1,122 @@
|
||||
[
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/utils.o",
|
||||
"src/utils.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/utils.c"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/loop.o",
|
||||
"src/loop.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/loop.c"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/array.o",
|
||||
"src/array.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/array.c"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/builtins.o",
|
||||
"src/builtins.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/builtins.c"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/launch.o",
|
||||
"src/launch.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/launch.c"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/myshellin.o",
|
||||
"src/myshellin.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/myshellin.c"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/console_line.o",
|
||||
"src/console_line.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/console_line.c"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"clang",
|
||||
"-c",
|
||||
"-Wall",
|
||||
"-Isrc/include",
|
||||
"-g",
|
||||
"-std=c11",
|
||||
"-o",
|
||||
"src/redirect.o",
|
||||
"src/redirect.c"
|
||||
],
|
||||
"directory": "/mnt/data/projects/myshellin",
|
||||
"file": "src/redirect.c"
|
||||
}
|
||||
]
|
@ -1,9 +0,0 @@
|
||||
informe:
|
||||
# we do this twice to make sure the toc is updated
|
||||
pdflatex -synctex=1 -interaction=nonstopmode "Informe.tex"
|
||||
pdflatex -synctex=1 -interaction=nonstopmode "Informe.tex"
|
||||
|
||||
clean:
|
||||
rm -f *.log *.toc *.gz *.aux sections/*.aux *.out Informe.pdf
|
||||
|
||||
.PHONY: informe clean
|
@ -1,4 +0,0 @@
|
||||
\section{Código}
|
||||
\input{sections/codigo/ciclo}
|
||||
\input{sections/codigo/builtins}
|
||||
\input{sections/codigo/launch}
|
@ -1,6 +0,0 @@
|
||||
\newpage
|
||||
\subsection{Launch}
|
||||
Launch(lanzar) es la parte responsable por permutar un programa que está en el sistema operativo o en PATH. Para hacer eso\
|
||||
primer hacemos un fork donde el hijo permuta la operación y el padre espera que su hijo termina. Luego pasamos un nombre\
|
||||
de programa a lanzar y sus argumentos al execvp para permutar en el hijo.
|
||||
\lstinputlisting{code/launch.txt}
|
2579
doxygen.conf
Normal file
2579
doxygen.conf
Normal file
File diff suppressed because it is too large
Load Diff
@ -13,6 +13,12 @@
|
||||
\usepackage{amsmath}
|
||||
\usepackage{listings}
|
||||
\usepackage{xcolor}
|
||||
\usepackage[T1]{fontenc}
|
||||
\usepackage{lmodern}
|
||||
|
||||
\usepackage{mathptmx}
|
||||
|
||||
\newcommand{\Quad}{\hspace*{1em}}
|
||||
|
||||
\lstdefinestyle{freefempp}{
|
||||
language=C,
|
||||
@ -29,7 +35,7 @@
|
||||
}
|
||||
|
||||
\lstset{
|
||||
basicstyle=\footnotesize\ttfamily,
|
||||
basicstyle=\scriptsize\ttfamily,
|
||||
columns=flexible,
|
||||
breaklines=true,
|
||||
inputencoding=utf8,
|
||||
@ -59,8 +65,7 @@
|
||||
|
||||
\begin{flushright}
|
||||
\textbf{Integrantes:}\\
|
||||
Christopher Cromer\\
|
||||
Raúl Hernandez
|
||||
Christopher Cromer
|
||||
\end{flushright}
|
||||
|
||||
\begin{flushright}
|
||||
@ -80,9 +85,10 @@ Carlos Faúndez
|
||||
|
||||
\include{sections/introduccion}
|
||||
|
||||
\include{sections/funcionamiento}
|
||||
|
||||
\include{sections/codigo}
|
||||
|
||||
\include{sections/conclusiones}
|
||||
|
||||
\end{document}
|
||||
|
16
report/Makefile
Normal file
16
report/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
all: report
|
||||
|
||||
report:
|
||||
# we do this twice to make sure the toc is updated
|
||||
pdflatex -synctex=1 -interaction=nonstopmode "Informe.tex"
|
||||
pdflatex -synctex=1 -interaction=nonstopmode "Informe.tex"
|
||||
|
||||
clean:
|
||||
rm -f Informe.pdf
|
||||
find . -type f -name '*.log' -exec rm {} +
|
||||
find . -type f -name '*.toc' -exec rm {} +
|
||||
find . -type f -name '*.gz' -exec rm {} +
|
||||
find . -type f -name '*.aux' -exec rm {} +
|
||||
find . -type f -name '*.out' -exec rm {} +
|
||||
|
||||
.PHONY: all report clean
|
33
report/code/environ.txt
Normal file
33
report/code/environ.txt
Normal file
@ -0,0 +1,33 @@
|
||||
typedef struct {
|
||||
StringArray *keys;
|
||||
StringArray *values;
|
||||
size_t size;
|
||||
} ArrayList;
|
||||
|
||||
void set_array_list(ArrayList *array_list, char *key, char *value) {
|
||||
if (array_list->keys == NULL) {
|
||||
array_list->keys = create_string_array();
|
||||
array_list->values = create_string_array();
|
||||
}
|
||||
for (size_t i = 0; i < array_list->size; i++) {
|
||||
if (strcmp(array_list->keys->array[i], key) == 0) {
|
||||
array_list->values->array[i] = realloc(array_list->values->array[i], strlen(value) * sizeof(char *));
|
||||
strcpy(array_list->values->array[i], value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
insert_string_array(array_list->keys, key);
|
||||
insert_string_array(array_list->values, value);
|
||||
array_list->size++;
|
||||
}
|
||||
|
||||
char *get_array_list(ArrayList *array_list, char *key) {
|
||||
if (array_list->keys != NULL) {
|
||||
for (size_t i = 0; i < array_list->size; i++) {
|
||||
if (strcmp(array_list->keys->array[i], key) == 0) {
|
||||
return array_list->values->array[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
44
report/code/redirection.txt
Normal file
44
report/code/redirection.txt
Normal file
@ -0,0 +1,44 @@
|
||||
typedef struct {
|
||||
int fd;
|
||||
int fd_new;
|
||||
int fd_copy;
|
||||
char *filename;
|
||||
int flags;
|
||||
} Redirect;
|
||||
|
||||
Redirect in = {.fd = STDIN_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_RDONLY};
|
||||
Redirect out = {.fd = STDOUT_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
|
||||
Redirect err = {.fd = STDERR_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
|
||||
|
||||
void open_redirect(Redirect *redirect) {
|
||||
redirect->fd_copy = dup(redirect->fd);
|
||||
if (redirect->fd_copy == -1) {
|
||||
perror("dup");
|
||||
}
|
||||
else {
|
||||
redirect->fd_new = open(redirect->filename, redirect->flags, 0664);
|
||||
if (redirect->fd_new == -1) {
|
||||
fprintf(stderr, "open: Could not open file %s: \"%s\"\n", redirect->filename, strerror(errno));
|
||||
}
|
||||
else {
|
||||
if (close(redirect->fd) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
if (dup(redirect->fd_new) == -1) {
|
||||
perror("dup");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void close_redirect(Redirect *redirect) {
|
||||
if (close(redirect->fd_new) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
if (dup2(redirect->fd_copy, redirect->fd) == -1) {
|
||||
perror("dup2");
|
||||
}
|
||||
if (close(redirect->fd_copy) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
}
|
6
report/sections/codigo.tex
Normal file
6
report/sections/codigo.tex
Normal file
@ -0,0 +1,6 @@
|
||||
\section{Código}
|
||||
\input{sections/codigo/ciclo}
|
||||
\input{sections/codigo/builtins}
|
||||
\input{sections/codigo/permutar}
|
||||
\input{sections/codigo/redireccion}
|
||||
\input{sections/codigo/environ}
|
8
report/sections/codigo/environ.tex
Normal file
8
report/sections/codigo/environ.tex
Normal file
@ -0,0 +1,8 @@
|
||||
\newpage
|
||||
\subsection{Entorno}
|
||||
El entorno está controlado por un struct de array de strings de llaves y valores. Se inserta y busca adentro de estos array de llaves\
|
||||
para encontrar y usar el variable de entorno. En el shell el struct es usado como un variable global para que se puede accederlo\
|
||||
desde cualquier parte del código que se necesita los variables de entorno. Lo mas importante son 2 funciones, set\_array\_list y\
|
||||
get\_array\_list. Se usa set\_array\_list para insertar o sobre escribir un variable de entorno. Y el get\_array\_list retorno un\
|
||||
valor si se encuentre en el array list basado en la llava pasado a la función como parámetro.
|
||||
\lstinputlisting{code/environ.txt}
|
6
report/sections/codigo/permutar.tex
Normal file
6
report/sections/codigo/permutar.tex
Normal file
@ -0,0 +1,6 @@
|
||||
\newpage
|
||||
\subsection{Permutar}
|
||||
Permutar es la parte responsable por correr un programa que está en el sistema operativo o en PATH. Para hacer eso\
|
||||
primer hacemos un fork donde el hijo permuta la operación y el padre espera que su hijo termina. Luego pasamos un nombre\
|
||||
de programa a permutar y sus argumentos al execvp.
|
||||
\lstinputlisting{code/launch.txt}
|
12
report/sections/codigo/redireccion.tex
Normal file
12
report/sections/codigo/redireccion.tex
Normal file
@ -0,0 +1,12 @@
|
||||
\newpage
|
||||
\subsection{Redireccionamiento}
|
||||
Para redireccionar las entradas y salidas se usa 4 comandos principales, dup, dup2, open y close. El primer paso a tomar\
|
||||
es duplicar la entrada o salida(stdin, stdout o stderr) con dup. Eso es importante para restaurarlo después. Luego se abre el archivo\
|
||||
de salida o entrada usando open y los flags correspondiente a la operación que quiere\
|
||||
hacer(O\_RDONLY, O\_WRONLY, O\_CREATE, O\_APPEND o O\_TRUNC). Después, cerra stdin, stdout o stderr. Eso va a dejar su fd disponible para\
|
||||
usar. Finalmente hacer una duplicación con dup del fd de archivo de entrad o salida. Eso va a dejar el archivo usando el fd que
|
||||
cerramos anteriormente. Ahora todos las cosas mandados al stdin, stdout o stderr van a ser redirigidas al archivo. Para terminar\
|
||||
cerramos el archivo usado. Luego usamos dup2 para duplicar la copia original en su fd original. I finalmente cerramos la copia.\
|
||||
Para que funciona eso, debe abrir el redireccionamiento antes de llamar los builtins o permutar y cerrarlo después que termina\
|
||||
correr los builtins o el proceso hijo.
|
||||
\lstinputlisting{code/redirection.txt}
|
@ -1,5 +1,5 @@
|
||||
\section{Conclusiones}
|
||||
Lo cosa mas importante que se puede aprender del proyecto es que el shell es un puente entre el usuario y el sistema operativo.\
|
||||
El shell es responsable para lanzar programas, interpretar comandos y un shells mas avanzados interpretar un lenguaje de\
|
||||
El shell es responsable para lanzar programas, interpretar comandos y en shells mas avanzados interpretar un lenguaje de\
|
||||
scripting para poder facilitar tareas o automatizarlas. Por lo tanto el shell es una parte integral para el sistema operativo\
|
||||
especialmente en sistemas como GNU/Linux o BSD.
|
38
report/sections/funcionamiento.tex
Normal file
38
report/sections/funcionamiento.tex
Normal file
@ -0,0 +1,38 @@
|
||||
\section{Funcionamiento}
|
||||
El myshellin consiste de 3 partes importantes, los comandos "builtins", permutar y redireccionamiento.
|
||||
|
||||
\subsection{Builtins}
|
||||
Los builtins son comandos que son proporcionados por el shell en vez de un programa que provee el sistema operativo. Los comandos\
|
||||
que proporciona el myshellin son:
|
||||
\begin{itemize}
|
||||
\item help \newline \Quad mostrar una mensaje de ayuda de como usar el shell.
|
||||
\item environ \newline \Quad imprimir todos los variables del entorno.
|
||||
\item set \newline \Quad crear o sobre escribir un variable de entorno.
|
||||
\item echo \newline \Quad imprimir un comentario o variable.
|
||||
\item cd \newline \Quad cambiar el directorio donde está trabajando el shell.
|
||||
\item exit \newline \Quad salir del shell.
|
||||
\end{itemize}
|
||||
|
||||
Cuando el comando que ingreso el usuario no es un builtin el siguiente paso es tratar de permutar un software que proporciona el\
|
||||
sistema operativo.
|
||||
|
||||
\subsection{Permutar}
|
||||
Para permutar un programa de sistema, el proceso de myshellin hace una llamada al fork para crear un proceso hijo. Luego el\
|
||||
proceso hijo intenta permutar con el comando execvp. Lo que hace eso es reemplazar el proceso hijo con el proceso del programa\
|
||||
que proporciona el sistema operativo como ls, rm, df, du, etc. Mientras que corre el programa el proceso padre espera que\
|
||||
termina el proceso hijo antes que puede seguir pidiendo que el usuario ingresa otros comandos.
|
||||
|
||||
\newpage
|
||||
\subsection{Redireccionamiento}
|
||||
Redireccionamiento es el proceso de redirigir los contenidos de una entrada o salida al stdin, stdout o stderr. Por ejemplo\
|
||||
se puede redirigir todo el contenidos que se debe imprimir en stdout a un archivo usando el símbolo ``>`` seguido por un nombre\
|
||||
de archivo. En el shell existen los siguientes tipos de redireccionamiento:
|
||||
\begin{itemize}
|
||||
\item > \newline \Quad redirigir el stdout a un archivo y borrar el contenido anterior si existe el archivo.
|
||||
\item >\null> \newline \Quad redirigir el stdout a un archivo y agregue el el contenido nuevo al final del archivo si existe.
|
||||
\item 1> \newline \Quad redirigir el stdout a un archivo y borrar el contenido anterior si existe el archivo.
|
||||
\item 1>\null> \newline \Quad redirigir el stdout a un archivo y agregue el el contenido nuevo al final del archivo si existe.
|
||||
\item 2> \newline \Quad redirigir el stderr a un archivo y borrar el contenido anterior si existe el archivo.
|
||||
\item 2>\null> \newline \Quad redirigir el stderr a un archivo y agregue el el contenido nuevo al final del archivo si existe.
|
||||
\item < \newline \Quad redirigir el contenido de un archivo al stdin.
|
||||
\end{itemize}
|
Before Width: | Height: | Size: 185 KiB After Width: | Height: | Size: 185 KiB |
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -14,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "array.h"
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -14,8 +13,8 @@
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "array.h"
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -13,8 +12,6 @@
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef _MYSHELLIN_ARRAY
|
||||
#define _MYSHELLIN_ARRAY
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -13,12 +12,12 @@
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "array.h"
|
||||
|
||||
#ifndef _MYSHELLIN_BUILTINS
|
||||
#define _MYSHELLIN_BUILTINS
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "array.h"
|
||||
|
||||
/**
|
||||
* Check if the command is a builtin or not.
|
||||
* @param command String with the command name to check.
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -13,11 +12,11 @@
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#ifndef _MYSHELLIN_LAUNCH
|
||||
#define _MYSHELLIN_LAUNCH
|
||||
|
||||
#include "array.h"
|
||||
|
||||
/**
|
||||
* Launch programs from the OS.
|
||||
* @param args The arguments to launch.
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
|
68
src/include/redirect.h
Normal file
68
src/include/redirect.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _MYSHELLIN_REDIRECT
|
||||
#define _MYSHELLIN_REDIRECT
|
||||
|
||||
/**
|
||||
* This structure helps for handling redirection to new fds.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* The fd to work with. Should be either 1 for stdout or 2 for stderr.
|
||||
* Once set it should not be changed.
|
||||
*/
|
||||
int fd;
|
||||
|
||||
/**
|
||||
* The new fd for the file.
|
||||
*/
|
||||
int fd_new;
|
||||
|
||||
/**
|
||||
* The copy of the original fd.
|
||||
*/
|
||||
int fd_copy;
|
||||
|
||||
/**
|
||||
* The name of the file to write to.
|
||||
*/
|
||||
char *filename;
|
||||
|
||||
/**
|
||||
* Whether or not to truncate or append the file.
|
||||
*/
|
||||
int flags;
|
||||
} Redirect;
|
||||
|
||||
/**
|
||||
* Open an fd to handle redirect.
|
||||
* @param redirect The redirect struct that houses the new fd and the old fd.
|
||||
*/
|
||||
void open_redirect(Redirect *redirect);
|
||||
|
||||
/**
|
||||
* Close an fd and restore the original.
|
||||
* @param redirect The redirect struct that houses the new fd and the old fd.
|
||||
*/
|
||||
void close_redirect(Redirect *redirect);
|
||||
|
||||
/**
|
||||
* Set the filename in a redirect object.
|
||||
* @param redirect The redirect struct to set the filename to.
|
||||
* @param filename The new filename to set.
|
||||
*/
|
||||
void set_filename(Redirect *redirect, char *filename);
|
||||
|
||||
#endif
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -15,6 +14,7 @@
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
71
src/loop.c
71
src/loop.c
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -14,13 +13,16 @@
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "array.h"
|
||||
#include "builtins.h"
|
||||
#include "console_line.h"
|
||||
#include "launch.h"
|
||||
#include "redirect.h"
|
||||
#include "utils.h"
|
||||
|
||||
void add_to_cleanup(void *data) {
|
||||
@ -64,10 +66,67 @@ void loop() {
|
||||
|
||||
StringArray *args = create_string_array();
|
||||
|
||||
int get_file_name = -1;
|
||||
Redirect in = {.fd = STDIN_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_RDONLY};
|
||||
Redirect out = {.fd = STDOUT_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
|
||||
Redirect err = {.fd = STDERR_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
|
||||
|
||||
char *saveptr = NULL;
|
||||
char *token = strtok_r(line, " ", &saveptr);
|
||||
while (token) {
|
||||
insert_string_array(args, token);
|
||||
if (get_file_name == STDIN_FILENO) {
|
||||
// get file name for stdin redirect
|
||||
set_filename(&in, token);
|
||||
get_file_name = -1;
|
||||
}
|
||||
else if (get_file_name == STDOUT_FILENO) {
|
||||
// get file name for stdout redirect
|
||||
set_filename(&out, token);
|
||||
get_file_name = -1;
|
||||
}
|
||||
else if (get_file_name == STDERR_FILENO) {
|
||||
// get file name for stderr redirect
|
||||
set_filename(&err, token);
|
||||
get_file_name = -1;
|
||||
}
|
||||
else if (strcmp(token, ">") == 0 || strcmp(token, "1>") == 0) {
|
||||
// redirect stdout and overwrite
|
||||
get_file_name = STDOUT_FILENO;
|
||||
if ((out.flags & O_APPEND) == O_APPEND) {
|
||||
out.flags &= ~O_APPEND;
|
||||
out.flags |= O_TRUNC;
|
||||
}
|
||||
}
|
||||
else if (strcmp(token, ">>") == 0 || strcmp(token, "1>>") == 0) {
|
||||
// redirect stdout and append
|
||||
get_file_name = STDOUT_FILENO;
|
||||
if ((out.flags & O_TRUNC) == O_TRUNC) {
|
||||
out.flags &= ~O_TRUNC;
|
||||
out.flags |= O_APPEND;
|
||||
}
|
||||
}
|
||||
else if (strcmp(token, "2>") == 0) {
|
||||
// redirect stderr and overwrite
|
||||
get_file_name = STDERR_FILENO;
|
||||
if ((err.flags & O_APPEND) == O_APPEND) {
|
||||
err.flags &= ~O_APPEND;
|
||||
err.flags |= O_TRUNC;
|
||||
}
|
||||
}
|
||||
else if (strcmp(token, "2>>") == 0) {
|
||||
// redirect stderr and append
|
||||
get_file_name = STDERR_FILENO;
|
||||
if ((err.flags & O_TRUNC) == O_TRUNC) {
|
||||
err.flags &= ~O_TRUNC;
|
||||
err.flags |= O_APPEND;
|
||||
}
|
||||
}
|
||||
else if (strcmp(token, "<") == 0) {
|
||||
get_file_name = STDIN_FILENO;
|
||||
}
|
||||
else {
|
||||
insert_string_array(args, token);
|
||||
}
|
||||
token = strtok_r(NULL, " ", &saveptr);
|
||||
}
|
||||
if (line != NULL) {
|
||||
@ -81,6 +140,10 @@ void loop() {
|
||||
continue;
|
||||
}
|
||||
|
||||
open_redirect(&err);
|
||||
open_redirect(&out);
|
||||
open_redirect(&in);
|
||||
|
||||
if (is_builtin(args->array[0])) {
|
||||
run_builtin(args);
|
||||
}
|
||||
@ -88,6 +151,10 @@ void loop() {
|
||||
launch_program(args);
|
||||
}
|
||||
|
||||
close_redirect(&in);
|
||||
close_redirect(&out);
|
||||
close_redirect(&err);
|
||||
|
||||
free_string_array(args);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
|
86
src/redirect.c
Normal file
86
src/redirect.c
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "redirect.h"
|
||||
|
||||
void open_redirect(Redirect *redirect) {
|
||||
if (redirect->filename != NULL) {
|
||||
redirect->fd_copy = dup(redirect->fd);
|
||||
if (redirect->fd_copy == -1) {
|
||||
perror("dup");
|
||||
}
|
||||
else {
|
||||
redirect->fd_new = open(redirect->filename, redirect->flags, 0664);
|
||||
if (redirect->fd_new == -1) {
|
||||
fprintf(stderr, "open: Could not open file %s: \"%s\"\n", redirect->filename, strerror(errno));
|
||||
free(redirect->filename);
|
||||
redirect->filename = NULL;
|
||||
}
|
||||
else {
|
||||
if (close(redirect->fd) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
if (dup(redirect->fd_new) == -1) {
|
||||
perror("dup");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void close_redirect(Redirect *redirect) {
|
||||
if (redirect->filename != NULL) {
|
||||
if (close(redirect->fd_new) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
if (dup2(redirect->fd_copy, redirect->fd) == -1) {
|
||||
perror("dup2");
|
||||
}
|
||||
if (close(redirect->fd_copy) == -1) {
|
||||
perror("close");
|
||||
}
|
||||
free(redirect->filename);
|
||||
redirect->filename = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void set_filename(Redirect *redirect, char *filename) {
|
||||
if (filename == NULL) {
|
||||
redirect->filename = NULL;
|
||||
}
|
||||
else {
|
||||
if (redirect->filename != NULL) {
|
||||
redirect->filename = realloc(redirect->filename, (strlen(filename) + 1) * sizeof(char *));
|
||||
if (redirect->filename == NULL) {
|
||||
perror("remalloc");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else {
|
||||
redirect->filename = malloc((strlen(filename) + 1) * sizeof(char *));
|
||||
if (redirect->filename == NULL) {
|
||||
perror("malloc");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
memset(redirect->filename, 0, strlen(filename) + 1);
|
||||
strcpy(redirect->filename, filename);
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright 2021 Christopher Cromer
|
||||
* Copyright 2021 Raúl Hernandez
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user