redirection parsing: handle commands with no redirections

This commit is contained in:
Khaïs COLIN 2025-03-07 11:55:51 +01:00
parent 42bccd0a06
commit 06ebcf132a
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
7 changed files with 116 additions and 3 deletions

View file

@ -50,6 +50,7 @@ srcs = \
src/parser/wordsplit/tokenizing_6_10.c \
src/parser/wordsplit/wordsplit.c \
src/parser/wordsplit/wordsplit_utils.c \
src/postprocess/redirections/redirections.c \
objs = $(srcs:.c=.o)
export objs

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:24:57 by khais #+# #+# */
/* Updated: 2025/02/21 12:48:53 by khais ### ########.fr */
/* Updated: 2025/03/07 12:29:16 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,10 +14,12 @@
# define SIMPLE_CMD_H
# include "../wordlist/wordlist.h"
# include "../../postprocess/redirections/redirections.h"
typedef struct s_simple_cmd
{
t_wordlist *words;
t_wordlist *words;
t_redir_list *redirections;
} t_simple_cmd;
t_simple_cmd *simple_cmd_from_wordlist(t_wordlist *words);

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirections.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 12:30:04 by khais #+# #+# */
/* Updated: 2025/03/07 12:32:02 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "redirections.h"
#include <stdlib.h>
struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
{
return (cmd);
}

View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirections.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 11:59:31 by khais #+# #+# */
/* Updated: 2025/03/07 14:23:48 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef REDIRECTIONS_H
# define REDIRECTIONS_H
# include "../../parser/worddesc/worddesc.h"
/*
** Type of redirection.
*/
typedef enum e_redirection_type
{
// Invalid
REDIR_INVALID,
// <
REDIR_INPUT,
// >
REDIR_OUTPUT,
// <<
REDIR_HERE_DOC,
// >>
REDIR_APPEND,
} t_redir_type;
typedef struct s_redirection
{
// type of this redirection
t_redir_type type;
// either the filepath to the given file, or the delimiter for a here_doc
// redirection
t_worddesc *marker;
} t_redirection;
typedef struct s_redirection_list
{
t_redirection *redirection;
struct s_redirection_list *next;
} t_redir_list;
struct s_simple_cmd;
struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd);
#endif // REDIRECTIONS_H

View file

@ -1,6 +1,7 @@
# make gets confused if a file with the same name exists in the sources, so some
# file are prefixed with test_
rawtests = \
test_redirection_parsing \
quote_removal \
cmdlist_use_after_free \
metacharacters \

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:20:20 by khais #+# #+# */
/* Updated: 2025/02/21 12:49:55 by khais ### ########.fr */
/* Updated: 2025/03/07 12:32:04 by khais ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_redirection_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 11:55:37 by khais #+# #+# */
/* Updated: 2025/03/07 12:00:13 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include <assert.h>
#include "../src/parser/simple_cmd/simple_cmd.h"
#include "../src/parser/wordsplit/wordsplit.h"
#include "../src/postprocess/redirections/redirections.h"
static t_simple_cmd *parse_simple_cmd(char *input)
{
t_wordlist *words = minishell_wordsplit(input);
t_simple_cmd *cmd = simple_cmd_from_wordlist(words);
return (cmd);
}
static void test_redirection_parsing_no_redirections(void)
{
t_simple_cmd *cmd = parse_simple_cmd("echo hello world");
assert(parse_redirections(cmd) != NULL);
assert(cmd->redirections == NULL);
simple_cmd_destroy(cmd);
}
int main(void) {
test_redirection_parsing_no_redirections();
return (0);
}