mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
minishell: only parse simple commands
This commit is contained in:
parent
f07a80c762
commit
d8bc528c5a
4 changed files with 25 additions and 211 deletions
|
|
@ -6,13 +6,14 @@
|
||||||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
|
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/03/20 17:36:58 by khais ### ########.fr */
|
/* Updated: 2025/03/27 15:58:27 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "buffer/buffer.h"
|
#include "buffer/buffer.h"
|
||||||
#include "get_command.h"
|
#include "get_command.h"
|
||||||
#include "parser/cmdgroup/cmdgroup.h"
|
#include "parser/cmdgroup/cmdgroup.h"
|
||||||
|
#include "parser/simple_cmd/simple_cmd.h"
|
||||||
#include "parser/wordlist/wordlist.h"
|
#include "parser/wordlist/wordlist.h"
|
||||||
#include "parser/wordsplit/wordsplit.h"
|
#include "parser/wordsplit/wordsplit.h"
|
||||||
#include "postprocess/redirections/redirection_parsing.h"
|
#include "postprocess/redirections/redirection_parsing.h"
|
||||||
|
|
@ -24,29 +25,30 @@
|
||||||
**
|
**
|
||||||
** Frees line before exiting
|
** Frees line before exiting
|
||||||
*/
|
*/
|
||||||
static t_cmdgroup *parse_command(char *line)
|
static t_simple_cmd *parse_command(char *line)
|
||||||
{
|
{
|
||||||
t_wordlist *words;
|
t_wordlist *words;
|
||||||
t_cmdgroup *cmd;
|
t_simple_cmd *cmd;
|
||||||
|
|
||||||
words = minishell_wordsplit(line);
|
words = minishell_wordsplit(line);
|
||||||
free(line);
|
free(line);
|
||||||
cmd = cmdgroup_from_wordlist(words);
|
if (words == NULL)
|
||||||
wordlist_destroy(words);
|
return (NULL);
|
||||||
|
cmd = simple_cmd_from_wordlist(words);
|
||||||
return (cmd);
|
return (cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** debug-print a cmdgroup
|
** debug-print a cmdgroup
|
||||||
*/
|
*/
|
||||||
static void debug_command(t_cmdgroup *cmd)
|
static void debug_command(t_simple_cmd *cmd)
|
||||||
{
|
{
|
||||||
t_buffer *indent;
|
t_buffer *indent;
|
||||||
|
|
||||||
indent = ft_buffer_new();
|
indent = ft_buffer_new();
|
||||||
cmdgroup_debug(cmd, indent, true);
|
simple_cmd_debug(cmd, indent, true);
|
||||||
ft_buffer_free(indent);
|
ft_buffer_free(indent);
|
||||||
cmdgroup_destroy(cmd);
|
simple_cmd_destroy(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -56,19 +58,19 @@ static void debug_command(t_cmdgroup *cmd)
|
||||||
** 1. redirection parsing
|
** 1. redirection parsing
|
||||||
** 2. quote removal
|
** 2. quote removal
|
||||||
*/
|
*/
|
||||||
static t_cmdgroup *post_process_command(t_cmdgroup *cmd)
|
static t_simple_cmd *post_process_command(t_simple_cmd *cmd)
|
||||||
{
|
{
|
||||||
if (cmdgroup_parse_redirections(cmd) == NULL)
|
if (parse_redirections(cmd) == NULL)
|
||||||
return (cmdgroup_destroy(cmd), NULL);
|
return (simple_cmd_destroy(cmd), NULL);
|
||||||
if (cmdgroup_remove_quotes(cmd) == NULL)
|
if (simple_cmd_remove_quotes(cmd) == NULL)
|
||||||
return (cmdgroup_destroy(cmd), NULL);
|
return (simple_cmd_destroy(cmd), NULL);
|
||||||
return (cmd);
|
return (cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[], char **envp)
|
int main(int argc, char *argv[], char **envp)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
t_cmdgroup *cmd;
|
t_simple_cmd *cmd;
|
||||||
|
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/20 17:36:20 by khais #+# #+# */
|
/* Created: 2025/03/20 17:36:20 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/21 18:33:43 by khais ### ########.fr */
|
/* Updated: 2025/03/27 14:27:14 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
/*
|
/*
|
||||||
** do quote removal on all words of this simple cmd
|
** do quote removal on all words of this simple cmd
|
||||||
*/
|
*/
|
||||||
static t_simple_cmd *simple_cmd_remove_quotes(t_simple_cmd *cmd)
|
t_simple_cmd *simple_cmd_remove_quotes(t_simple_cmd *cmd)
|
||||||
{
|
{
|
||||||
t_wordlist *new;
|
t_wordlist *new;
|
||||||
t_worddesc *result;
|
t_worddesc *result;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/28 13:51:03 by khais #+# #+# */
|
/* Created: 2025/02/28 13:51:03 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/20 17:36:11 by khais ### ########.fr */
|
/* Updated: 2025/03/27 14:27:27 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -16,7 +16,8 @@
|
||||||
# include "../worddesc/worddesc.h"
|
# include "../worddesc/worddesc.h"
|
||||||
# include "../cmdgroup/cmdgroup.h"
|
# include "../cmdgroup/cmdgroup.h"
|
||||||
|
|
||||||
t_worddesc *remove_quotes(t_worddesc *word);
|
t_worddesc *remove_quotes(t_worddesc *word);
|
||||||
t_cmdgroup *cmdgroup_remove_quotes(t_cmdgroup *cmd);
|
t_cmdgroup *cmdgroup_remove_quotes(t_cmdgroup *cmd);
|
||||||
|
t_simple_cmd *simple_cmd_remove_quotes(t_simple_cmd *cmd);
|
||||||
|
|
||||||
#endif // REMOVE_QUOTES_H
|
#endif // REMOVE_QUOTES_H
|
||||||
|
|
|
||||||
193
test.sh
193
test.sh
|
|
@ -116,197 +116,8 @@ when_run <<EOF "simple command is parsed"
|
||||||
echo hello
|
echo hello
|
||||||
EOF
|
EOF
|
||||||
expecting <<EOF
|
expecting <<EOF
|
||||||
╰─ t_cmdgroup
|
╰─ t_simple_cmd
|
||||||
├─ t_cmdlist
|
├─ words = [echo][hello]
|
||||||
│ ├─ num_cmds = 1
|
|
||||||
│ ╰─ cmd[0]
|
|
||||||
│ ├─ t_cmdlist_item
|
|
||||||
│ │ ╰─ t_pipeline
|
|
||||||
│ │ ├─ num_cmd = 1
|
|
||||||
│ │ ╰─ cmd[0]
|
|
||||||
│ │ ╰─ t_simple_cmd
|
|
||||||
│ │ ├─ words = [echo][hello]
|
|
||||||
│ │ ╰─ (no redirections)
|
|
||||||
│ ╰─ t_operator = END
|
|
||||||
╰─ (no redirections)
|
|
||||||
EOF
|
|
||||||
|
|
||||||
when_run <<EOF "single redirection is parsed"
|
|
||||||
echo hello>outfile
|
|
||||||
EOF
|
|
||||||
expecting <<EOF
|
|
||||||
╰─ t_cmdgroup
|
|
||||||
├─ t_cmdlist
|
|
||||||
│ ├─ num_cmds = 1
|
|
||||||
│ ╰─ cmd[0]
|
|
||||||
│ ├─ t_cmdlist_item
|
|
||||||
│ │ ╰─ t_pipeline
|
|
||||||
│ │ ├─ num_cmd = 1
|
|
||||||
│ │ ╰─ cmd[0]
|
|
||||||
│ │ ╰─ t_simple_cmd
|
|
||||||
│ │ ├─ words = [echo][hello]
|
|
||||||
│ │ ╰─ t_redir_list
|
|
||||||
│ │ ╰─ redirection[0]
|
|
||||||
│ │ ╰─ t_redirection
|
|
||||||
│ │ ├─ t_redir_type = REDIR_OUTPUT
|
|
||||||
│ │ ╰─ marker = [outfile]
|
|
||||||
│ ╰─ t_operator = END
|
|
||||||
╰─ (no redirections)
|
|
||||||
EOF
|
|
||||||
|
|
||||||
when_run <<EOF "redirections are parsed"
|
|
||||||
echo hello < in hi > out
|
|
||||||
EOF
|
|
||||||
expecting <<EOF
|
|
||||||
╰─ t_cmdgroup
|
|
||||||
├─ t_cmdlist
|
|
||||||
│ ├─ num_cmds = 1
|
|
||||||
│ ╰─ cmd[0]
|
|
||||||
│ ├─ t_cmdlist_item
|
|
||||||
│ │ ╰─ t_pipeline
|
|
||||||
│ │ ├─ num_cmd = 1
|
|
||||||
│ │ ╰─ cmd[0]
|
|
||||||
│ │ ╰─ t_simple_cmd
|
|
||||||
│ │ ├─ words = [echo][hello][hi]
|
|
||||||
│ │ ╰─ t_redir_list
|
|
||||||
│ │ ├─ redirection[0]
|
|
||||||
│ │ │ ╰─ t_redirection
|
|
||||||
│ │ │ ├─ t_redir_type = REDIR_INPUT
|
|
||||||
│ │ │ ╰─ marker = [in]
|
|
||||||
│ │ ╰─ redirection[1]
|
|
||||||
│ │ ╰─ t_redirection
|
|
||||||
│ │ ├─ t_redir_type = REDIR_OUTPUT
|
|
||||||
│ │ ╰─ marker = [out]
|
|
||||||
│ ╰─ t_operator = END
|
|
||||||
╰─ (no redirections)
|
|
||||||
EOF
|
|
||||||
|
|
||||||
when_run <<EOF "multiple redirections are parsed"
|
|
||||||
echo << HERE_DOC hello>outfile there < infile < infile2 >> append
|
|
||||||
EOF
|
|
||||||
expecting <<EOF
|
|
||||||
╰─ t_cmdgroup
|
|
||||||
├─ t_cmdlist
|
|
||||||
│ ├─ num_cmds = 1
|
|
||||||
│ ╰─ cmd[0]
|
|
||||||
│ ├─ t_cmdlist_item
|
|
||||||
│ │ ╰─ t_pipeline
|
|
||||||
│ │ ├─ num_cmd = 1
|
|
||||||
│ │ ╰─ cmd[0]
|
|
||||||
│ │ ╰─ t_simple_cmd
|
|
||||||
│ │ ├─ words = [echo][hello][there]
|
|
||||||
│ │ ╰─ t_redir_list
|
|
||||||
│ │ ├─ redirection[0]
|
|
||||||
│ │ │ ╰─ t_redirection
|
|
||||||
│ │ │ ├─ t_redir_type = REDIR_HERE_DOC
|
|
||||||
│ │ │ ╰─ marker = [HERE_DOC]
|
|
||||||
│ │ ├─ redirection[1]
|
|
||||||
│ │ │ ╰─ t_redirection
|
|
||||||
│ │ │ ├─ t_redir_type = REDIR_OUTPUT
|
|
||||||
│ │ │ ╰─ marker = [outfile]
|
|
||||||
│ │ ├─ redirection[2]
|
|
||||||
│ │ │ ╰─ t_redirection
|
|
||||||
│ │ │ ├─ t_redir_type = REDIR_INPUT
|
|
||||||
│ │ │ ╰─ marker = [infile]
|
|
||||||
│ │ ├─ redirection[3]
|
|
||||||
│ │ │ ╰─ t_redirection
|
|
||||||
│ │ │ ├─ t_redir_type = REDIR_INPUT
|
|
||||||
│ │ │ ╰─ marker = [infile2]
|
|
||||||
│ │ ╰─ redirection[4]
|
|
||||||
│ │ ╰─ t_redirection
|
|
||||||
│ │ ├─ t_redir_type = REDIR_APPEND
|
|
||||||
│ │ ╰─ marker = [append]
|
|
||||||
│ ╰─ t_operator = END
|
|
||||||
╰─ (no redirections)
|
|
||||||
EOF
|
|
||||||
|
|
||||||
when_run <<EOF "a single command in parentheses is has the parenthesis implicitly removed"
|
|
||||||
(echo hi)
|
|
||||||
EOF
|
|
||||||
# this is how it is handled in bash:
|
|
||||||
# doing a `ps uf` on bash running `sleep 10`
|
|
||||||
# \_ bash --norc
|
|
||||||
# \_ sleep 10
|
|
||||||
# and `(sleep 10)`
|
|
||||||
# \_ bash --norc
|
|
||||||
# \_ sleep 10
|
|
||||||
# this is exactly the same process graph
|
|
||||||
expecting <<EOF
|
|
||||||
╰─ t_cmdgroup
|
|
||||||
├─ t_cmdlist
|
|
||||||
│ ├─ num_cmds = 1
|
|
||||||
│ ╰─ cmd[0]
|
|
||||||
│ ├─ t_cmdlist_item
|
|
||||||
│ │ ╰─ t_pipeline
|
|
||||||
│ │ ├─ num_cmd = 1
|
|
||||||
│ │ ╰─ cmd[0]
|
|
||||||
│ │ ╰─ t_simple_cmd
|
|
||||||
│ │ ├─ words = [echo][hi]
|
|
||||||
│ │ ╰─ (no redirections)
|
|
||||||
│ ╰─ t_operator = END
|
|
||||||
╰─ (no redirections)
|
|
||||||
EOF
|
|
||||||
|
|
||||||
when_run <<EOF "a single cmdlist in parenthesis has the parenthesis implicitly removed"
|
|
||||||
(echo hi >> append | echo < infile bye && echo hello > outfile)
|
|
||||||
EOF
|
|
||||||
expecting <<EOF
|
|
||||||
╰─ t_cmdgroup
|
|
||||||
├─ t_cmdlist
|
|
||||||
│ ├─ num_cmds = 2
|
|
||||||
│ ├─ cmd[0]
|
|
||||||
│ │ ├─ t_cmdlist_item
|
|
||||||
│ │ │ ╰─ t_pipeline
|
|
||||||
│ │ │ ├─ num_cmd = 2
|
|
||||||
│ │ │ ├─ cmd[0]
|
|
||||||
│ │ │ │ ╰─ t_simple_cmd
|
|
||||||
│ │ │ │ ├─ words = [echo][hi]
|
|
||||||
│ │ │ │ ╰─ t_redir_list
|
|
||||||
│ │ │ │ ╰─ redirection[0]
|
|
||||||
│ │ │ │ ╰─ t_redirection
|
|
||||||
│ │ │ │ ├─ t_redir_type = REDIR_APPEND
|
|
||||||
│ │ │ │ ╰─ marker = [append]
|
|
||||||
│ │ │ ╰─ cmd[1]
|
|
||||||
│ │ │ ╰─ t_simple_cmd
|
|
||||||
│ │ │ ├─ words = [echo][bye]
|
|
||||||
│ │ │ ╰─ t_redir_list
|
|
||||||
│ │ │ ╰─ redirection[0]
|
|
||||||
│ │ │ ╰─ t_redirection
|
|
||||||
│ │ │ ├─ t_redir_type = REDIR_INPUT
|
|
||||||
│ │ │ ╰─ marker = [infile]
|
|
||||||
│ │ ╰─ t_operator = AND
|
|
||||||
│ ╰─ cmd[1]
|
|
||||||
│ ├─ t_cmdlist_item
|
|
||||||
│ │ ╰─ t_pipeline
|
|
||||||
│ │ ├─ num_cmd = 1
|
|
||||||
│ │ ╰─ cmd[0]
|
|
||||||
│ │ ╰─ t_simple_cmd
|
|
||||||
│ │ ├─ words = [echo][hello]
|
|
||||||
│ │ ╰─ t_redir_list
|
|
||||||
│ │ ╰─ redirection[0]
|
|
||||||
│ │ ╰─ t_redirection
|
|
||||||
│ │ ├─ t_redir_type = REDIR_OUTPUT
|
|
||||||
│ │ ╰─ marker = [outfile]
|
|
||||||
│ ╰─ t_operator = END
|
|
||||||
╰─ (no redirections)
|
|
||||||
EOF
|
|
||||||
|
|
||||||
when_run <<EOF "quotes are removed"
|
|
||||||
echo "this contains quotes"
|
|
||||||
EOF
|
|
||||||
expecting <<EOF
|
|
||||||
╰─ t_cmdgroup
|
|
||||||
├─ t_cmdlist
|
|
||||||
│ ├─ num_cmds = 1
|
|
||||||
│ ╰─ cmd[0]
|
|
||||||
│ ├─ t_cmdlist_item
|
|
||||||
│ │ ╰─ t_pipeline
|
|
||||||
│ │ ├─ num_cmd = 1
|
|
||||||
│ │ ╰─ cmd[0]
|
|
||||||
│ │ ╰─ t_simple_cmd
|
|
||||||
│ │ ├─ words = [echo][this contains quotes]
|
|
||||||
│ │ ╰─ (no redirections)
|
|
||||||
│ ╰─ t_operator = END
|
|
||||||
╰─ (no redirections)
|
╰─ (no redirections)
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue