add stdin redirection and refactor
This commit is contained in:
parent
5f9b4bf81d
commit
83c0aa17b1
@ -12,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 <stdbool.h>
|
||||
|
||||
#ifndef _MYSHELLIN_REDIRECT
|
||||
#define _MYSHELLIN_REDIRECT
|
||||
|
||||
@ -45,7 +43,7 @@ typedef struct {
|
||||
/**
|
||||
* Whether or not to truncate or append the file.
|
||||
*/
|
||||
bool append;
|
||||
int flags;
|
||||
} Redirect;
|
||||
|
||||
/**
|
||||
|
59
src/loop.c
59
src/loop.c
@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -65,43 +66,63 @@ void loop() {
|
||||
|
||||
StringArray *args = create_string_array();
|
||||
|
||||
enum FileType {FileType_none, FileType_stdout, FileType_stderr};
|
||||
enum FileType get_file_name = FileType_none;
|
||||
Redirect out = {.fd = 1, .fd_new = -1, .fd_copy = -1, .filename = NULL, .append = false};
|
||||
Redirect err = {.fd = 2, .fd_new = -1, .fd_copy = -1, .filename = NULL, .append = false};
|
||||
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) {
|
||||
if (get_file_name == FileType_stdout) {
|
||||
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 = FileType_none;
|
||||
get_file_name = -1;
|
||||
}
|
||||
else if (get_file_name == 2) {
|
||||
else if (get_file_name == STDERR_FILENO) {
|
||||
// get file name for stderr redirect
|
||||
set_filename(&err, token);
|
||||
get_file_name = FileType_none;
|
||||
get_file_name = -1;
|
||||
}
|
||||
else if (strcmp(token, ">") == 0 || strcmp(token, "1>") == 0) {
|
||||
// redirect stdout and overwrite
|
||||
get_file_name = FileType_stdout;
|
||||
out.append = false;
|
||||
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 = FileType_stdout;
|
||||
out.append = true;
|
||||
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 stdout and overwrite
|
||||
get_file_name = FileType_stderr;
|
||||
err.append = false;
|
||||
// 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 stdout and append
|
||||
get_file_name = FileType_stderr;
|
||||
err.append = true;
|
||||
// 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);
|
||||
@ -121,6 +142,7 @@ void loop() {
|
||||
|
||||
open_redirect(&err);
|
||||
open_redirect(&out);
|
||||
open_redirect(&in);
|
||||
|
||||
if (is_builtin(args->array[0])) {
|
||||
run_builtin(args);
|
||||
@ -129,6 +151,7 @@ void loop() {
|
||||
launch_program(args);
|
||||
}
|
||||
|
||||
close_redirect(&in);
|
||||
close_redirect(&out);
|
||||
close_redirect(&err);
|
||||
|
||||
|
@ -27,12 +27,7 @@ void open_redirect(Redirect *redirect) {
|
||||
perror("dup");
|
||||
}
|
||||
else {
|
||||
if (redirect->append) {
|
||||
redirect->fd_new = open(redirect->filename, O_WRONLY | O_CREAT | O_APPEND, 0664);
|
||||
}
|
||||
else {
|
||||
redirect->fd_new = open(redirect->filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
|
||||
}
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user