Giter Site home page Giter Site logo

minishell's Introduction

minishell_banner

Allowed functions and usage examples: Functions.md

Installation

To install and use minishell, you need:

  1. Clone this git repository

    git clone https://github.com/tesla33io/minishell.git
  2. Navigate into minishell and build the executable

    cd minishell
    make
  3. Now you can use minishell executable to run minishell on your machine.

  4. Additionally you can create a symlink for minishell and move it to the directory on the $PATH to use minishell from any part of the file system

    ln -s minishell /usr/bin/minishell

Builtins

A struct that consists of two elements, the name of the command (builtin) and a pointer to the appropriate function. (?)

  1. echo with -n support (maybe some other arguments)
  2. cd (only relative or absolute path)
  3. pwd
  4. export
  5. unset
  6. env
  7. exit

Useful resources

Chapter5-WritingYourOwnShell

> file vs >& file


Allowed functions and usage examples: Functions.md

minishell's People

Contributors

tesla33io avatar ltreser avatar

Stargazers

Dima Dem avatar  avatar

Watchers

 avatar

minishell's Issues

Redirection Before Command

When a redirection occurs before a command, the output is still sent to STDOUT instead of the specified file. The file is created but remains empty.

Example:

$ ./minishell
(/home/artem/projects/minishell) $> > f2 echo "not in the file =("
"not in the file =("
$ cat f2 | cat -e

$

In this case, the expected behavior is that the content "not in the file =(" should be written to f2 instead of being printed to STDOUT.

Quotes Not Being Removed Correctly

Minishell currently fails to correctly remove quotes from the input. Here’s an example demonstrating the problem:

Example Command and Output:

$ ./minishell
(/home/artem/projects/minishell) $> echo "some  " extra "  spaces"
"some  " extra "  spaces"

The expected output should not contain the " characters:

some   extra   spaces

The same issue occurs with redirections:

When redirecting output to a file, quotes are not removed correctly:

$ ./minishell
(/home/artem/projects/minishell) $> echo "This text goes inside a file =D" > file1

$ cat file1
"This text goes inside a file =D"

Expected Behavior:
The content in file1 should be:

This text goes inside a file =D

Problems with Input Redirection `<`

Problem 1:

When attempting to set a file as an input for a command (for example, cat), the program segfaults.
The source of segfault is parser.c:74.

Problem 2:

Input redirection does not work at all. For example, running the following command does not produce the expected behavior:

$ ./minishell
(/home/artem/projects/minishell) $> < inputfile command

Multiple spaces instead of command

MS crashes when trying to input multiple spaces without any command.
Valgrind trace:

<EOL>
==119722== Invalid write of size 8
==119722==    at 0x10D0A9: take_out_trash (src/lexer/lexer_utils.c:48)
==119722==    by 0x10D505: remove_spaces (src/lexer/merge.c:91)
==119722==    by 0x10D536: merge_tokens (src/lexer/merge.c:99)
==119722==    by 0x10CEC4: lexer (src/lexer/lexer.c:147)
==119722==    by 0x10C4FA: main (src/main.c:40)
==119722==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==119722==
==119722==
==119722== Process terminating with default action of signal 11 (SIGSEGV)
==119722==  Access not within mapped region at address 0x18
==119722==    at 0x10D0A9: take_out_trash (src/lexer/lexer_utils.c:48)
==119722==    by 0x10D505: remove_spaces (src/lexer/merge.c:91)
==119722==    by 0x10D536: merge_tokens (src/lexer/merge.c:99)
==119722==    by 0x10CEC4: lexer (src/lexer/lexer.c:147)
==119722==    by 0x10C4FA: main (src/main.c:40)
==119722==  If you believe this happened as a result of a stack
==119722==  overflow in your program's main thread (unlikely but
==119722==  possible), you can try to increase the size of the
==119722==  main thread stack using the --main-stacksize= flag.
==119722==  The main thread stack size used in this run was 8388608.
==119722==
==119722== FILE DESCRIPTORS: 5 open (3 std) at exit.
==119722== Open file descriptor 4: /dev/pts/2
==119722==    at 0x49D993B: dup (syscall-template.S:120)
==119722==    by 0x10C4CA: main (src/main.c:34)
==119722==
==119722== Open file descriptor 3: /dev/pts/2
==119722==    at 0x49D993B: dup (syscall-template.S:120)
==119722==    by 0x10C4BA: main (src/main.c:33)
==119722==
==119722==
==119722== HEAP SUMMARY:
==119722==     in use at exit: 253,363 bytes in 595 blocks
==119722==   total heap usage: 1,276 allocs, 681 frees, 309,805 bytes allocated
==119722==
==119722== LEAK SUMMARY:
==119722==    definitely lost: 0 bytes in 0 blocks
==119722==    indirectly lost: 0 bytes in 0 blocks
==119722==      possibly lost: 0 bytes in 0 blocks
==119722==    still reachable: 0 bytes in 0 blocks
==119722==         suppressed: 253,363 bytes in 595 blocks
==119722==
==119722== For lists of detected and suppressed errors, rerun with: -s
==119722== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 1 from 1)

Fix `gcc` errors

For now I compile minishell using clang because gcc throws some errors. This needs to be fixed.

bug with prompt

When running minishell if type in d (for example) and then delete this character, the prompt should disappear, then press Enter.
minishell should receive a SIGSEGV afterwards.

Here is an output of Valgrind related to this issue:

==7201== Invalid read of size 8
==7201==    at 0x10C204: group_tokens (merge.c:10)
==7201==    by 0x10C36B: merge_tokens (merge.c:71)
==7201==    by 0x10BF65: lexer (lexer.c:136)
==7201==    by 0x10B560: main (main.c:26)
==7201==  Address 0x18 is not stack'd, malloc'd or (recently) free'd
==7201==
==7201==
==7201== Process terminating with default action of signal 11 (SIGSEGV)
==7201==  Access not within mapped region at address 0x18
==7201==    at 0x10C204: group_tokens (merge.c:10)
==7201==    by 0x10C36B: merge_tokens (merge.c:71)
==7201==    by 0x10BF65: lexer (lexer.c:136)
==7201==    by 0x10B560: main (main.c:26)
==7201==  If you believe this happened as a result of a stack
==7201==  overflow in your program's main thread (unlikely but
==7201==  possible), you can try to increase the size of the
==7201==  main thread stack using the --main-stacksize= flag.
==7201==  The main thread stack size used in this run was 8388608.

More problems to fix 😊🫠

Summary

  1. String Operations:

    • Issue: The current code uses ft_strjoin for appending "=" to var.
    • Improvement: Review and ensure the correctness of the string concatenation process.
    • Code Reference:
      var = ft_strjoin(var, "=");
  2. Directory Check:

    • Issue: The directory check should utilize open(tmp_file_name, O_DIRECTORY) to verify if a directory exists.
    • Compliance: Ensure compatibility with _POSIX_C_SOURCE 200809L.
    • Code Reference:
      if (open(tmp_file_name, O_DIRECTORY) < 0) { 
         // Handle the error 
      }
  3. Exit Code 126 for Permission Denied:

    • Issue: Ensure that the system correctly returns exit code 126 when permission is denied for executing a command.
    • Context: This is particularly important for ensuring compliance with common shell behaviors.
  4. Handling Empty Redirections/Heredoc/Append:

    • Issue: Properly handle scenarios with empty redirections, heredoc, and append operations.
    • Examples:
      >>
      <<
      <>>
      >><
  5. Handling Empty Pipes:

    • Issue: Properly handle cases where there are empty pipes in the command input.
    • Example:
      ||
  6. Signal Handling in Child Processes:

    • Issue: Ensure that SIGQUIT is correctly remapped back to its default behavior (SIG_DFL) in child processes.
  7. Variable Expansion in Quotes:

    • Issue: The variable expansion within double quotes at the end of a string should be handled correctly.
    • Example: Review and fix the case described in this example.
  8. Handling Invalid Identifiers in export:

    • Issue: Handle cases where invalid identifiers (e.g., -=) are used with the export command.
    • Expected Behavior: The system should handle and report these invalid identifiers appropriately.
  9. Exit Codes:

    • Issue: Ensure that exit codes are handled correctly, including:
      • Correct sign (positive/negative) for the exit code. (only in exit)
      • Consistent use of the last command’s exit code in sequential commands.
    • Context: This is critical for maintaining shell behavior consistency.
  10. Immediate Failure on File Access Issues:

    • Issue: The shell should fail immediately if it cannot open a file, regardless of the direction (input/output).
    • Expected Behavior: Failure should be prompt, with appropriate error messaging.
  11. Handling Empty Variables:

    • Issue: The shell should discard the variable and just return the prompt.
    • Example:
      $ $NONEXISTING
      $

MS crashing when trying to execute here-doc

Trying to run here-doc in minshell causes it to crash. The issue is in parsing, namely - when trying to print error with data from token_stream which is null at that point (after multiple recursive calls).

Logs from core file and gdb:

#0  0x0000572872e87e65 in ft_parse (shell_data=0x57287477c2a0, production=0x5728747bc7e0 "", parent=0x5728747bc5a0, token_stream=0x0) at src/parser/parser.c:83
#1  0x0000572872e87efd in ft_parse (shell_data=0x57287477c2a0, production=0x5728747bb8c0 "", parent=0x5728747bc5a0, token_stream=0x5728747a2970) at src/parser/parser.c:94
#2  0x0000572872e87efd in ft_parse (shell_data=0x57287477c2a0, production=0x5728747bb550 "", parent=0x57287477cf90, token_stream=0x5728747a2970) at src/parser/parser.c:94
#3  0x0000572872e87efd in ft_parse (shell_data=0x57287477c2a0, production=0x5728747a28a0 "", parent=0x57287477cf90, token_stream=0x5728747a2970) at src/parser/parser.c:94
#4  0x0000572872e87efd in ft_parse (shell_data=0x57287477c2a0, production=0x5728747babe0 "", parent=0x57287477cf90, token_stream=0x5728747a2970) at src/parser/parser.c:94
#5  0x0000572872e86b91 in main (ac=1, av=0x7ffe312811f8, envp=0x7ffe31281208) at src/main.c:31
(gdb) info locals
alternative = 0x0
symbol = 0x5728747bc7e0 ""

Tried to change check for null in this function:

src/parser/parser.c:71
-if (!token_stream && !production && !shell_data->lexer->unmatched)
+if (!token_stream || !production || !shell_data->lexer->unmatched)

It didn't fixed the problem, but prevented MS from crashing.

Doesn't Execute Local Binaries

Minishell should handle the execution of programs specified by a path to the binary. Currently, this functionality is not working:

Example:

$ ./minishell
(/home/artem/projects/minishell) $> ./minishell
[REPLACE] ./minishell: program not found
execve: Bad address
(/home/artem/projects/minishell) $> /usr/bin/ls
[REPLACE] /usr/bin/ls: program not found
execve: Bad address

In these cases, Minishell should be able to execute both the local binary ./minishell and the system binary /usr/bin/ls correctly.

Multiple redirection

MS doesn't yet handle multiple redirection (out/in) and also multiple heredoc/append.

Syntax error when prompt is empty

MS gives a syntax error, whenever the prompt is an empty string (not a command). Correct behavior is to just display prompt again.

$ ./minishell | cat -e
€ echo 123$
123$
€ $
Syntax Error$

fd-management

MS doesn't properly close all FD's in cases:

  • multiple redirection

Invalid read of size 1 when `echo` unseted env var

Problem seems to happen only once during execution. When I unset some env variable (as the very first command), for example PWD, and then try to print it using echo, Valgrind outputs an error invalid read of size 1. The problem is inside the expander.

Valgrind traces:

₳ unset PWD
₳ echo $PWD
==123987== Invalid read of size 1
==123987==    at 0x10EEEB: var_expand (src/expander/expander.c:61)
==123987==    by 0x10C57F: extract_args (src/adapter/single_command.c:131)
==123987==    by 0x10C210: extract_command (src/adapter/single_command.c:50)
==123987==    by 0x10BEF6: handle_command (src/adapter/adapter.c:59)
==123987==    by 0x10BDCD: adapt (src/adapter/adapter.c:30)
==123987==    by 0x10CA0A: main (src/main.c:43)
==123987==  Address 0x4b5e851 is 0 bytes after a block of size 1 alloc'd
==123987==    at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==123987==    by 0x10F284: gc_malloc (memory/ft_malloc.c:73)
==123987==    by 0x10F9AB: ft_strjoin (string/ft_strjoin.c:22)
==123987==    by 0x10EFCB: var_expand (src/expander/expander.c:71)
==123987==    by 0x10C57F: extract_args (src/adapter/single_command.c:131)
==123987==    by 0x10C210: extract_command (src/adapter/single_command.c:50)
==123987==    by 0x10BEF6: handle_command (src/adapter/adapter.c:59)
==123987==    by 0x10BDCD: adapt (src/adapter/adapter.c:30)
==123987==    by 0x10CA0A: main (src/main.c:43)
==123987==

Worth noticing, that this issues happens only after first try to print unsetted var, afterwards no other errors.

env starts with number

MS breaks when trying to echo or export (fixed) an env var that start with numbers:

export 123=qweqwe
export: `123=qweqwe': not a valid identifier
₳ echo $123
^C    <<<<<< shell is broken at this point
₳ zsh: terminated  ./minishell

MS doesn't interpret more than one heredoc

For some reason (maybe during parsing) heredoc could not be interpreted more than one per command.
Example:

$ ./minishell
€ cat << EOF1 << EOF2 << EOF3
Syntax Error
> weird
> EOF1
cat: EOF2: No such file or directory
cat: EOF3: No such file or directory

P.S.: weird Syntax error, shouldn't be there

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.