Giter VIP home page Giter VIP logo

minishell's Introduction

Minishell

Design

There are two main components on a shell:

  • Analyzer
  • Evaluator
---
title: Analyzer
---
flowchart LR
    input["INPUT: Raw CMD"]
    analyzer["ANALYZER"]
    output["OUTPUT: Command AST"]
    input --> analyzer
    analyzer --> output
Loading
---
title: Evaluator
---
flowchart LR
    input["INPUT: Command AST"]
    evaluator["EVALUATOR"]
    output["OUTPUT: CMD execution"]
    input --> evaluator
    evaluator --> output
Loading
---
title: Command Abstract Syntax Tree (AST)
---
%%{ init: { 'flowchart': { 'curve': 'stepAfter' } } }%%
graph TD
    A((Pipe)):::pipe --> B((Redir)):::redir
    A --> C((Pipe)):::pipe
    B --> D((Filename)):::arg
    B --> E((Redir)):::redir
    E --> EL((Filename)):::arg
    E --> ER((Redir)):::redir
    ER --> EER((Filename)):::arg
    ER --> EEL((CMD)):::cmd
    C --> F((Redir)):::redir
    F --> FL((Filename)):::arg
    F --> FR((Redir)):::redir
    FR --> FFR((Filename)):::arg
    FR --> FFL(... Redirs ...):::redir
    FFL --> FFFL((Redir)):::redir
    FFFL --> FFFR((Filename)):::arg
    FFFL --> FFFFL((CMD)):::cmd
    C --> G(... Pipes ...):::pipe
    G --> H((Pipe)):::pipe
    H --> HL((Redir)):::redir
    HL --> HR((Filename)):::arg
    H --> I((Redir)):::redir
    I --> IR((Filename)):::arg
    I --> IL(... Redirs ...):::redir
    IL --> IIL((CMD)):::cmd
    HL --> HHL(... Redirs ...):::redir
    HHL --> HHHL((CMD)):::cmd
    classDef pipe stroke:#f00
    classDef arg stroke:#0f0
    classDef cmd stroke:#ff0
    classDef redir stroke:#0ff
Loading
---
title: Simplified Abstract Syntax Tree (AST)
---
%%{ init: { 'flowchart': { 'curve': 'stepAfter' } } }%%
graph TD
    A((Pipe)):::pipe --> B((Redir\nFilename)):::redir
    A --> C((Pipe)):::pipe
    B --> E((Redir\nFilename)):::redir
    E --> ER((Redir\nFilename)):::redir
    ER --> EEL((CMD)):::cmd
    C --> F((Redir\nFilename)):::redir
    F --> FR((Redir\nFilename)):::redir
    FR --> FFL(... Redirs ...):::redir
    FFL --> FFFL((Redir\nFilename)):::redir
    FFFL --> FFFFL((CMD)):::cmd
    C --> G(... Pipes ...):::pipe
    G --> H((Pipe)):::pipe
    H --> HL((Redir\nFilename)):::redir
    H --> I((Redir\nFilename)):::redir
    I --> IL(... Redirs ...):::redir
    IL --> IIL((CMD)):::cmd
    HL --> HHL(... Redirs ...):::redir
    HHL --> HHHL((CMD)):::cmd
    classDef pipe stroke:#f00
    classDef arg stroke:#0f0
    classDef cmd stroke:#ff0
    classDef redir stroke:#0ff
Loading

We can reduce the AST into a linked list if we include the redirections' linked list as an attribute of a command

---
title: Reduced AST
---
%%{ init: { 'flowchart': { 'curve': 'stepAfter' } } }%%
graph LR
    a((CMD)):::cmd --> b((CMD)):::cmd --> c((...)):::cmd --> d((CMD)):::cmd
    classDef cmd stroke:#ff0
Loading
%%{ init: { 'flowchart': { 'curve': 'stepAfter' } } }%%
graph TB
    subgraph CMD node
    direction TB
    aa((CMD)):::cmd --> bb((Redir)):::redir -->bbb((Filename)):::arg
    cc-->cccc((...)):::arg
    bb-->cc((Redir)):::redir --> ccc((...)):::redir
    aa-->aaa((arg)):::arg-->arg2((arg)):::arg-->arg3((...)):::arg
    aa-->next((next)):::cmd-->next2((next)):::cmd-->next3((...)):::cmd

    
    classDef arg stroke:#0f0
    classDef cmd stroke:#ff0
    classDef redir stroke:#0ff
    end
Loading

Node structure

Command

typedef struct s_ast
{
	char		*bin;
	int		ac;
	char		**av;
	t_redir		*redir;
	struct s_ast	*next;
}	t_ast;

Redirections

typedef struct s_redir
{
	t_byte	id;
	char	*file;
}	t_redir;

Interface

In this minishell, the interface is very small and is mostly a tool for the Analyzer since it doesn't handle any pointer interaction. However it does handle:

  • A prompt to input a command
  • History
  • Handles user signals:
    Key Combination Signal Name
    ctrl-C SIGINT
    ctrl-D EOF (End of File)
    ctrl-\ SIGQUIT

Analyzer

The analyzer takes the command, analyze the lexic, syntax and semantic, and process the data into an Command AST, that the Evaluator will take and use it to evaluate the command, redirection, pipe, etc. It handles:

  • Lexic Analysis
  • Syntax Analysis
  • Semantic Analysis
  • Quotes
    Quotes Behavior
    Single quotes Prevent interpreting anything inside.
    Double quotes Prevent interpreting anything except $ inside.
    • Not interpret unclosed quotes
  • Translations
    • meta-characters
    • Relative path from command to absolute path
    • Env variables
    • $? which the evaluator has to save

Evaluator

The evaluator takes the Command AST, launch the appropriate executable on a child process, handling pipes, signal inheritance, clean process management, etc.

  • Search and launch executables
    • Based on the PATH env variable.
    • Based on an aboslute path
    • Based on builtins commands
  • Interpret redirections and pipes
    Redirection Symbol Description
    < Redirects input.
    > Redirects output.
    << Reads input until a line containing the delimiter is seen.
    >> Redirects output in append mode.
  • Builtins
    Command Option
    echo -n
    cd Only a relative or absolute path
    pwd No options
    export No options
    unset No options
    env No options or arguments
    exit No options

image

Entity Relationship Diagram

---
title: Shell Command Processing Flow
---
erDiagram
    Shell_Command }|--|{ Analyzer : "Analyze"
    Shell_Command }|--|{ Command_AST : "Creates"
    Analyzer }o--o{ Command_AST : "Creates"
    Command_AST }o--|| Evaluator : "Evaluate"
Loading

minishell's People

Contributors

3lsy avatar andreiglesias avatar

Stargazers

 avatar Clément Kuzniewycz avatar

Watchers

 avatar

minishell's Issues

on exit

==29399== 2 bytes in 1 blocks are definitely lost in loss record 1 of 23
==29399== at 0x8961899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==29399== by 0x110021: ft_strdup (in /home//42/minishell/minishell)
==29399== by 0x110950: ft_itoa (in /home//42/minishell/minishell)
==29399== by 0x10B9B1: expand_line (in /home//42/minishell/minishell)
==29399== by 0x10A871: ft_lexer (in /home//42/minishell/minishell)
==29399== by 0x10A7DA: ft_analyzer (in /home//42/minishell/minishell)
==29399== by 0x10A439: main (in /home//42/minishell/minishell)

Correct ft_split_args()

Quotation has to be expanded/translated.
Correct ft_split_args();

Example

ech''o hello world

Expected translation

echo hello world

Actual behavior

ech''o hello world

cat ctrl+c

when you enter cat and try to exit with ctrl+c it wont exit

leaks with double point -> : : command not found

expected -> \n no error message

LEAKS at 0x895F899: malloc
==28498== by 0x10EAA5: ft_split
==28498== by 0x10AC4F: ft_which
==28498== by 0x10AB96: ft_execute
==28498== by 0x10AAFC: ft_evaluator
==28498== by 0x1093ED: main

Ctrl-D cat

Try ctrl-D after running a blocking command like cat without arguments or grep “something“.

running bash

running bash inside minishell makes the cursor invisible.

parent_exec -> Invalid read of size

ex ls | ls
Invalid read of size 4
==27074== at 0x10A91A: parent_exec
==27074== by 0x10AB0C: ft_evaluator
==27074== by 0x1093FD: main
==27074== Address 0x10c1d088 is 0 bytes after a block of size 8 alloc'd
==27074== at 0x895F899: malloc
==27074== by 0x10E45C: ft_calloc
==27074== by 0x10AABA: ft_evaluator

export HOME=something

==13137== 12 bytes in 1 blocks are definitely lost in loss record 3 of 23
==13137== at 0x8961899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==13137== by 0x10FFD1: ft_strdup (in /home//42/minishell/minishell)
==13137== by 0x10E190: init_env (in /home//42/minishell/minishell)
==13137== by 0x10A335: main (in /home//42/minishell/minishell)

<< test SIGSEGV

<< test

test
Stack overflow in thread #1: can't grow stack to 0x1ffe801000
Process terminating with default action of signal 11 (SIGSEGV)
==27596== Access not within mapped region at address 0x1FFE801FD1
==27596== Stack overflow in thread #1: can't grow stack to 0x1ffe801000
==27596== at 0x8AFBFB2: tcsetattr (tcsetattr.c:80)

frzz

echo < /etc/h0sts | echo hello | cat

echo $envar

if envar doesn't exists:

==13056== Invalid read of size 1
==13056==    at 0x10B926: expand_line (in /home//42/minishell/minishell)
==13056==    by 0x10A861: ft_lexer (in /home//42/minishell/minishell)
==13056==    by 0x10A7CA: ft_analyzer (in /home//42/minishell/minishell)
==13056==    by 0x10A429: main (in /home//42/minishell/minishell)
==13056==  Address 0x10d48ff6 is 0 bytes after a block of size 6 alloc'd
==13056==    at 0x8961899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==13056==    by 0x110341: ft_strjoin (in /home//42/minishell/minishell)
==13056==    by 0x10B87E: expand_env_var (in /home//42/minishell/minishell)
==13056==    by 0x10BA31: expand_line (in /home//42/minishell/minishell)
==13056==    by 0x10A861: ft_lexer (in /home//42/minishell/minishell)
==13056==    by 0x10A7CA: ft_analyzer (in /home//42/minishell/minishell)
==13056==    by 0x10A429: main (in /home//42/minishell/minishell)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.