From eff1eede66c5d9a8e65744364031563bffc93a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 7 Feb 2025 16:02:50 +0100 Subject: [PATCH 01/16] notes: add redirection section MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Khaïs COLIN --- NOTES.md | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/NOTES.md b/NOTES.md index d52c450..1ad7b15 100644 --- a/NOTES.md +++ b/NOTES.md @@ -19,6 +19,11 @@ Parses the tokens into simple and compound commands (see *Shell Commands*) Performs the various _Shell Expansions_, breaking the expanded tokens into lists of filenames and commands and arguments. +Performs any necessary _redirections_ and removes the **redirection operators** +and their operands from the argument list. + +TODO: add missing operations + ### Quoting Rules cf. 3.1.2 Quoting @@ -252,7 +257,7 @@ by a slash in the pattern. cf. 3.5.8.1 Pattern Matching Any character that appears in a pattern, other than the special pattern -characters described below, matches itself. The NUL character may not occur in +characters described below, matches itself. The NUL character may not occur in a pattern. The special pattern characters have the following meanings: @@ -273,9 +278,83 @@ bash-5.1$ ls *'*'there #### Quote Removal cf. 3.5.9 Quote Removal -After the preceding expansions, all unquoted occurrences of the -characters ''' and '"' that did not result from one of the above -expansions are removed. +After the preceding expansions, all unquoted occurrences of the characters ''' +and '"' that did not result from one of the above expansions are removed. + +### Redirection +cf. 3.6 Redirections + +Before a command is executed, its input and output may be "redirected" using a +special notation interpreted by the shell. "Redirection" allows commands' file +handles to be made to refer to different files, and can change the files the +command reads from and writes to. + +The redirection operators may precede or appear anywhere within a simple command +or may follow a command. + +e.g. this is the correct behaviour +```shell +bash-5.1$ ls > hello.txt *here +bash-5.1$ cat hello.txt +hello*there +hi*there +noonethere +``` + +Redirections are processed in the order they appear, from left to right. + +e.g. this is the correct behaviour +```shell +bash-5.1$ ls > hello.txt share > here.txt *.txt +bash-5.1$ ls -l hello.txt here.txt +-rw-r--r-- 1 kcolin 2024_le-havre 0 Feb 7 15:54 hello.txt +-rw-r--r-- 1 kcolin 2024_le-havre 68 Feb 7 15:54 here.txt +bash-5.1$ cat here.txt +hello.txt +here.txt +log.txt +newlog-strict.txt +newlog.txt + +share: +man +``` + +'<' refers to the standard input (fd 0, STDIN\_FILENO) + +'>' refers to the standard output (fd 1, STDOUT\_FILENO) + +TODO: check unless otherwise noted +The word following the redirection operator, unless otherwise noted, is +subjected to parameter expansion, filename expansion, word splitting, and +quote removal. + +If it expands to more than one word, Bash reports an error, except when in posix +mode. +TODO: decide if we follow posix default or bash default + +Note: This behaviour change is, as far as I can tell, not documented in the Bash +Reference Manual. + +```shell +# bash behaviour +bash-5.1$ var="file1 file2" +bash-5.1$ echo "hello world" > $var +bash: $var: ambiguous redirect +# posix behaviour +bash-5.1$ var="file1 file2" +bash-5.1$ echo "hello world" > $var +bash-5.1$ cat "$var" +hello world +``` + +In bash mode and in posix mode, if the variable is not defined, bash prints the +following error: + +```shell +bash-5.1$ echo "hello world" > $nonexist +bash: $nonexist: ambiguous redirect +``` ## Subshell From f3c875c34ec1189034fb817d65a1c514956faf44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 16:05:10 +0100 Subject: [PATCH 02/16] gitignore: do not commit bash reference manual --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a40b9d1..6638e88 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ tests/* infile outfile mallocfail_hashes +bash.txt From 9c81ac832a1ece8dedec12a57b97e9fd3cd3ebe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 10 Feb 2025 17:30:12 +0100 Subject: [PATCH 03/16] notes: add an interesting case of shell variables vs environment variables --- NOTES.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/NOTES.md b/NOTES.md index 1ad7b15..e02a0b6 100644 --- a/NOTES.md +++ b/NOTES.md @@ -213,6 +213,17 @@ The form is $VAR, where VAR may only contain the following characters: * _ * 0-9 (not in the first character) +Just noticed an interesting case: +```shell +bash-5.2$ VAR=hello # we set a shell variable (NOT environment variable) +bash-5.2$ VAR=hi env | grep VAR=; env | grep VAR= # here VAR is an environment variable, which is only valid for the next command (the second env returns nothing, confirming that it is not valid for that command) +VAR=hi +bash-5.2$ env | grep VAR= # var is not an environment variable +bash-5.2$ echo $VAR # but it is a shell variable +hello +``` +Luckily for us, we don't have to handle shell variables, nor do we have to handle `VAR=value` or `VAR=value cmd`. + #### Word Splitting cf. 3.5.7 Word Splitting From 845c0adb6a71af4df1f947380c75c10a17fd0b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 10 Feb 2025 17:38:08 +0100 Subject: [PATCH 04/16] fix(notes): we actually don't have to handle ';' --- NOTES.md | 7 ------- 1 file changed, 7 deletions(-) diff --git a/NOTES.md b/NOTES.md index e02a0b6..f632c57 100644 --- a/NOTES.md +++ b/NOTES.md @@ -402,13 +402,6 @@ here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’. -## Signal handling - -cf. 6.12 Shell Compatibility Mode => compat32 - -interrupting a command list such as "a; b; c" causes the execution of the entire -list to be aborted. - ## Definitions cf. [Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Definitions) cf. 2 Definitions From a8ff6481390e2b43c8af520c4dcbe99743182ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 10 Feb 2025 17:30:12 +0100 Subject: [PATCH 05/16] notes: add executing commands section --- NOTES.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/NOTES.md b/NOTES.md index f632c57..d6aef6e 100644 --- a/NOTES.md +++ b/NOTES.md @@ -22,6 +22,8 @@ of filenames and commands and arguments. Performs any necessary _redirections_ and removes the **redirection operators** and their operands from the argument list. +Executes the command (see _Command Execution_); + TODO: add missing operations ### Quoting Rules @@ -367,8 +369,61 @@ bash-5.1$ echo "hello world" > $nonexist bash: $nonexist: ambiguous redirect ``` -## Subshell +### Executing Commands +cf. 3.7 Executing Commands +#### Simple Command Execution +cf. 3.7.1 Simple Command Expansion + +When a simple command is executed, the shell performs the following +expansions, assignments, and redirections, from left to right, in the +following order. + + 1. The words that the parser has marked as redirections are saved for later + processing. + + 2. The words that are not redirections are expanded (see _Shell Expansions_). + If any words remain after expansion, the first word is taken to be the name + of the command and the remaining words are the arguments. + + 3. Redirections are performed as described above (see _Redirections_). + +If no command name results, redirections are performed, but do not affect the +current shell environment. A redirection error causes the command to exit with +a non-zero status. + +If there is a command name left after expansion, execution proceeds as described +below. Otherwise, the command exits with a status of zero. + +##### Command Search and Execution +cf. 3.7.2 Command Search and Execution + +After a command has been split into words, if it results in a simple command and +an optional list of arguments, the following actions are taken. + + 1. The shell searches for it in the list of shell builtins. If a match is + found, that builtin is invoked. + + 2. If the name is not a builtin, and contains no slashes, Bash searches each + element of '$PATH' for a directory containing an executable file by that + name. If the search is unsuccessful, the shell prints an error message and + returns an exit status of 127. + + 3. If the search is successful, or if the command name contains one or more + slashes, the shell executes the named program in a separate execution + environment. Argument 0 is set to the name given, and the remaining + arguments to the command are set to the arguments supplied, if any. + + 4. If this execution fails because the file is not in executable format, and + the file is not a directory, it is assumed to be a "shell script" and the + shell executes it as described in _Shell Scripts_. + TODO: check if we need to implement the _Shell Scripts_ behaviour + + 5. The shell waits for the command to complete and collects its exit status. + +TODO remove $@ $* mentions as we don't have to implement that + +#### Subshell cf. 3.7.3 Command Execution Environment The shell has an execution environment, which consists of the following: From b10f2a2ec8dd99a6a4cebaad1dae444faafb853b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 10 Feb 2025 17:30:12 +0100 Subject: [PATCH 06/16] notes: add environment section --- NOTES.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/NOTES.md b/NOTES.md index d6aef6e..534ecf1 100644 --- a/NOTES.md +++ b/NOTES.md @@ -439,6 +439,23 @@ execution environment. A subshell is a copy of the shell process. +#### Environment +cf. 3.7.4 Environment + +When a program is invoked it is given an array of strings called the +"environment". This is a list of name-value pairs, of the form 'name=value'. + +Bash provides several ways to manipulate the environment. On invocation, the +shell scans its own environment and creates a parameter for each name found, +automatically marking it for 'export' to child processes. Executed commands +inherit the environment. The 'export' and 'unset' builtins allow parameters to +be added to and deleted from the environment. If the value of a parameter in +the environment is modified using the 'export' builtin, the new value becomes +part of the environment, replacing the old. The environment inherited by any +executed command consists of the shell's initial environment, whose values may +be modified in the shell, less any pairs removed by the 'unset' builtin, plus +any additions via the 'export' command. + ## Here Documents cf. Bash Reference Manual 3.6.6 Here Documents From e6e108a98d3189810f9de6d51a12c1d81947a1ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 13:18:12 +0100 Subject: [PATCH 07/16] notes: remove $* and $@ mention, since we do not have to implement them --- NOTES.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/NOTES.md b/NOTES.md index 534ecf1..f75c725 100644 --- a/NOTES.md +++ b/NOTES.md @@ -50,9 +50,6 @@ cf. 3.1.2.3 Double Quotes Preserves the literal value of all characters within the quotes, with the exception of '$'. -TODO: The special parameters ‘*’ and ‘@’ have special meaning when in double quotes (see Shell Parameter Expansion). -See if we have to handle this - Per the subject: minishell should not interpret unclosed quotes ### Shell Commands @@ -269,7 +266,7 @@ by a slash in the pattern. ##### Pattern Matching cf. 3.5.8.1 Pattern Matching -Any character that appears in a pattern, other than the special pattern + * [ ] Any character that appears in a pattern, other than the special pattern characters described below, matches itself. The NUL character may not occur in a pattern. @@ -421,8 +418,6 @@ an optional list of arguments, the following actions are taken. 5. The shell waits for the command to complete and collects its exit status. -TODO remove $@ $* mentions as we don't have to implement that - #### Subshell cf. 3.7.3 Command Execution Environment From 806e98ac971696a017b7e16830f7dad91c33e1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 13:22:47 +0100 Subject: [PATCH 08/16] notes: clarified note about parameter expansion in here doc Still need to see if we have to implement that.. --- NOTES.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/NOTES.md b/NOTES.md index f75c725..22e5d3a 100644 --- a/NOTES.md +++ b/NOTES.md @@ -459,15 +459,56 @@ source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command. -TODO: The following paragraph may not apply fully to our project, check it again! - No parameter and variable expansion, command substitution, arithmetic expansion, -or filename expansion is performed on word. If any part of word is quoted, the -delimiter is the result of quote removal on word, and the lines in the -here-document are not expanded. If word is unquoted, all lines of the -here-document are subjected to parameter expansion, command substitution, and -arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must -be used to quote the characters ‘\’, ‘$’, and ‘`’. +or filename expansion is performed on word. + +If any part of word is quoted, the delimiter is the result of quote removal on +word, and the lines in the here-document are not expanded. If word is unquoted, +all lines of the here-document are subjected to parameter expansion, and the +character sequence \\newline is ignored. + +This is the correct behaviour for quoting and parameter expansion: +```shell +bash-5.2$ cat << EOF +> hello +> $$ +> EOF +hello +1491742 +bash-5.2$ cat << "EOF" +> hello +> $$ +> EOF +hello +$$ +bash-5.2$ cat << 'E'OF +> hello +> $$ +> EOF +hello +$$ +bash-5.2$ cat << $USER +> hello +> khais +> $USER +hello +khais +bash-5.2$ echo $USER +khais +bash-5.2$ cat << "$USER" +> $USER +``` + +TODO: do we need to ignore \\newline? subject says \\ is not required.. + +If we have to implement it, this is the correct behaviour: +```shell +bash-5.2$ cat << EOF +> hello \ +world +> EOF +hello world +``` ## Definitions cf. [Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Definitions) From 78298fbb1d3f9226a689e3917af143d283528cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 13:57:56 +0100 Subject: [PATCH 09/16] notes: decide to follow regular bash behaviour, not POSIX --- NOTES.md | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/NOTES.md b/NOTES.md index 22e5d3a..3b654b8 100644 --- a/NOTES.md +++ b/NOTES.md @@ -2,7 +2,9 @@ cf. [Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html) -Comparative testing with bash should be done with bash --norc --posix. +Comparative testing with bash should be done with bash --norc. + +In case of difference between regular bash and posix bash, we decide to follow regular bash. ## Ideas for testing * use prysk or shellspec with shell=./minishell @@ -339,27 +341,16 @@ The word following the redirection operator, unless otherwise noted, is subjected to parameter expansion, filename expansion, word splitting, and quote removal. -If it expands to more than one word, Bash reports an error, except when in posix -mode. -TODO: decide if we follow posix default or bash default - -Note: This behaviour change is, as far as I can tell, not documented in the Bash -Reference Manual. +If it expands to more than one word, Bash reports an error. +This is the correct behaviour: ```shell -# bash behaviour bash-5.1$ var="file1 file2" bash-5.1$ echo "hello world" > $var bash: $var: ambiguous redirect -# posix behaviour -bash-5.1$ var="file1 file2" -bash-5.1$ echo "hello world" > $var -bash-5.1$ cat "$var" -hello world ``` -In bash mode and in posix mode, if the variable is not defined, bash prints the -following error: +If the variable is not defined, bash prints the following error: ```shell bash-5.1$ echo "hello world" > $nonexist From 0637a637b9c6a1224feddab414bb3211afcaf083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 14:14:00 +0100 Subject: [PATCH 10/16] notes: clarify here doc section and move it to the correct place --- NOTES.md | 125 +++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/NOTES.md b/NOTES.md index 3b654b8..be8fa39 100644 --- a/NOTES.md +++ b/NOTES.md @@ -336,10 +336,9 @@ man '>' refers to the standard output (fd 1, STDOUT\_FILENO) -TODO: check unless otherwise noted -The word following the redirection operator, unless otherwise noted, is -subjected to parameter expansion, filename expansion, word splitting, and -quote removal. +The word following the redirection operator, unless the redirection operator is +'<<', is subjected to parameter expansion, filename expansion, word splitting, +and quote removal. If it expands to more than one word, Bash reports an error. @@ -357,6 +356,65 @@ bash-5.1$ echo "hello world" > $nonexist bash: $nonexist: ambiguous redirect ``` +#### Here Documents +cf. Bash Reference Manual 3.6.6 Here Documents + +This type of redirection instructs the shell to read input from the current +source until a line containing only word (with no trailing blanks) is seen. All +of the lines read up to that point are then used as the standard input for a +command. + +No parameter and variable expansion, command substitution, arithmetic expansion, +or filename expansion is performed on word. + +If any part of word is quoted, the delimiter is the result of quote removal on +word, and the lines in the here-document are not expanded. If word is unquoted, +all lines of the here-document are subjected to parameter expansion, and the +character sequence \\newline is ignored. + +This is the correct behaviour for quoting and parameter expansion: +```shell +bash-5.2$ cat << EOF +> hello +> $$ +> EOF +hello +1491742 +bash-5.2$ cat << "EOF" +> hello +> $$ +> EOF +hello +$$ +bash-5.2$ cat << 'E'OF +> hello +> $$ +> EOF +hello +$$ +bash-5.2$ cat << $USER +> hello +> khais +> $USER +hello +khais +bash-5.2$ echo $USER +khais +bash-5.2$ cat << "$USER" +> $USER +``` + +TODO: do we need to ignore \\newline? subject says \\ is not required.. + +If we have to implement it, this is the correct behaviour: +```shell +bash-5.2$ cat << EOF +> hello \ +world +> EOF +hello world +``` + ### Executing Commands cf. 3.7 Executing Commands @@ -442,65 +500,6 @@ executed command consists of the shell's initial environment, whose values may be modified in the shell, less any pairs removed by the 'unset' builtin, plus any additions via the 'export' command. -## Here Documents -cf. Bash Reference Manual 3.6.6 Here Documents - -This type of redirection instructs the shell to read input from the current -source until a line containing only word (with no trailing blanks) is seen. All -of the lines read up to that point are then used as the standard input for a -command. - -No parameter and variable expansion, command substitution, arithmetic expansion, -or filename expansion is performed on word. - -If any part of word is quoted, the delimiter is the result of quote removal on -word, and the lines in the here-document are not expanded. If word is unquoted, -all lines of the here-document are subjected to parameter expansion, and the -character sequence \\newline is ignored. - -This is the correct behaviour for quoting and parameter expansion: -```shell -bash-5.2$ cat << EOF -> hello -> $$ -> EOF -hello -1491742 -bash-5.2$ cat << "EOF" -> hello -> $$ -> EOF -hello -$$ -bash-5.2$ cat << 'E'OF -> hello -> $$ -> EOF -hello -$$ -bash-5.2$ cat << $USER -> hello -> khais -> $USER -hello -khais -bash-5.2$ echo $USER -khais -bash-5.2$ cat << "$USER" -> $USER -``` - -TODO: do we need to ignore \\newline? subject says \\ is not required.. - -If we have to implement it, this is the correct behaviour: -```shell -bash-5.2$ cat << EOF -> hello \ -world -> EOF -hello world -``` - ## Definitions cf. [Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Definitions) cf. 2 Definitions From d72613c29f336684eba4f2bf7543aa6bfd6d87ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 14:19:19 +0100 Subject: [PATCH 11/16] decision: in heredoc, we will not ignore \newline --- NOTES.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/NOTES.md b/NOTES.md index be8fa39..3dd6fa3 100644 --- a/NOTES.md +++ b/NOTES.md @@ -369,8 +369,7 @@ or filename expansion is performed on word. If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, -all lines of the here-document are subjected to parameter expansion, and the -character sequence \\newline is ignored. +all lines of the here-document are subjected to parameter expansion. This is the correct behaviour for quoting and parameter expansion: ```shell @@ -404,9 +403,7 @@ bash-5.2$ cat << "$USER" > $USER ``` -TODO: do we need to ignore \\newline? subject says \\ is not required.. - -If we have to implement it, this is the correct behaviour: +Subject says \\ is not required, so this behaviour we will not implement: ```shell bash-5.2$ cat << EOF > hello \ From 6672aec007498ffd378cf9e50baf10fef9768596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 14:26:32 +0100 Subject: [PATCH 12/16] decision: not sure if we will implement shell script execution, it is not subject-required --- NOTES.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NOTES.md b/NOTES.md index 3dd6fa3..2c8298f 100644 --- a/NOTES.md +++ b/NOTES.md @@ -460,7 +460,9 @@ an optional list of arguments, the following actions are taken. 4. If this execution fails because the file is not in executable format, and the file is not a directory, it is assumed to be a "shell script" and the shell executes it as described in _Shell Scripts_. - TODO: check if we need to implement the _Shell Scripts_ behaviour + + NOTE: we will _maybe_ implement this, we will see. It does not seem to be + required. 5. The shell waits for the command to complete and collects its exit status. From b4502207207fb73d6edb363878bef29f174945c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 15:38:23 +0100 Subject: [PATCH 13/16] notes: add exit status section --- NOTES.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/NOTES.md b/NOTES.md index 2c8298f..cf56809 100644 --- a/NOTES.md +++ b/NOTES.md @@ -438,7 +438,7 @@ a non-zero status. If there is a command name left after expansion, execution proceeds as described below. Otherwise, the command exits with a status of zero. -##### Command Search and Execution +#### Command Search and Execution cf. 3.7.2 Command Search and Execution After a command has been split into words, if it results in a simple command and @@ -499,6 +499,37 @@ executed command consists of the shell's initial environment, whose values may be modified in the shell, less any pairs removed by the 'unset' builtin, plus any additions via the 'export' command. +#### Exit Status +cf. 3.7.5 Exit Status + +The exit status of an executed command is the value returned by the 'waitpid' +system call or equivalent function. Exit statuses fall between 0 and 255, +though, as explained below, the shell may use values above 125 specially. Exit +statuses from shell builtins and compound commands are also limited to this +range. Under certain circumstances, the shell will use special values to +indicate specific failure modes. + +For the shell's purposes, a command which exits with a zero exit status has +succeeded. A non-zero exit status indicates failure. This seemingly +counter-intuitive scheme is used so there is one well-defined way to indicate +success and a variety of ways to indicate various failure modes. When a command +terminates on a fatal signal whose number is N, Bash uses the value 128+N as the +exit status. + +If a command is not found, the child process created to execute it returns a +status of 127. If a command is found but is not executable, the return status +is 126. + +If a command fails because of an error during expansion or redirection, the exit +status is greater than zero. + +All of the Bash builtins return an exit status of zero if they succeed and a +non-zero status on failure, so they may be used by the conditional and list +constructs. All builtins return an exit status of 2 to indicate incorrect +usage, generally invalid options or missing arguments. + +The exit status of the last command is available in the special parameter $?. + ## Definitions cf. [Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Definitions) cf. 2 Definitions From 0db1867a55aee4c6bd2b838584997e177572712a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 15:53:38 +0100 Subject: [PATCH 14/16] notes: add signals section (WIP - needs to be investigated further) --- NOTES.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/NOTES.md b/NOTES.md index cf56809..9914ae1 100644 --- a/NOTES.md +++ b/NOTES.md @@ -530,6 +530,19 @@ usage, generally invalid options or missing arguments. The exit status of the last command is available in the special parameter $?. +#### Signals +cf. 3.7.6 Signals + +When Bash is interactive, it ignores 'SIGTERM' (so that 'kill 0' does not kill +an interactive shell), and 'SIGINT' is caught and handled. When Bash receives a +'SIGINT', it breaks out of any executing loops. In all cases, Bash ignores +'SIGQUIT'. Bash ignores 'SIGTTIN', 'SIGTTOU', and 'SIGTSTP'. + +NOTE: The behaviour on when ^C is printed seems strange, investigate further +once we implement this + +TODO: investigate this further, this seems very complicated + ## Definitions cf. [Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Definitions) cf. 2 Definitions From ac905d0674cfc26cd8460ec1f2448f472188b6bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Feb 2025 15:57:12 +0100 Subject: [PATCH 15/16] notes: finish last operation --- NOTES.md | 2 +- compile_flags.txt | 284 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 compile_flags.txt diff --git a/NOTES.md b/NOTES.md index 9914ae1..7091cee 100644 --- a/NOTES.md +++ b/NOTES.md @@ -26,7 +26,7 @@ and their operands from the argument list. Executes the command (see _Command Execution_); -TODO: add missing operations +Waits for the command to complete and collects its exit status (see _Exit Status_). ### Quoting Rules diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..2099b5d --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,284 @@ +-fPIC + -Wformat + -Wformat-security + -Werror=format-security + -fzero-call-used-regs=used-gpr + -fstack-protector-strong + --param + ssp-buffer-size=4 + -O2 + -U_FORTIFY_SOURCE + -fwrapv + -target + x86_64-unknown-linux-gnu +original flags to /nix/store/n266hs15lrsvb3h2nyiasj0gyxzxgxrc-clang-18.1.8/bin/clang: + -c + -Wall + -Wextra + -Werror + -g + -o + src/minishell.o + src/minishell.c +extra flags after to /nix/store/n266hs15lrsvb3h2nyiasj0gyxzxgxrc-clang-18.1.8/bin/clang: + -U_FORTIFY_SOURCE + -D_FORTIFY_SOURCE=2 + -B/nix/store/nqb2ns2d1lahnd5ncwmn6k84qfd7vx2k-glibc-2.40-36/lib/ + -idirafter + /nix/store/lcxvgkg659vbvdq86mhxa599wn48f35c-glibc-2.40-36-dev/include + -B/nix/store/62qjb50708fdhb4f2y7zxyqr1afir4fk-gcc-13.3.0/lib/gcc/x86_64-unknown-linux-gnu/13.3.0 + --gcc-toolchain=/nix/store/62qjb50708fdhb4f2y7zxyqr1afir4fk-gcc-13.3.0 + -B/nix/store/flqqrbm3pv9mcxf7xg1j6ssmfwxwrsdx-clang-18.1.8-lib/lib + -resource-dir=/nix/store/z99smrlncwz340g79b3lils97qps9690-clang-wrapper-18.1.8/resource-root + -B/nix/store/wd1dlav3z5vwwv6yqj69xkzhldk5hpvb-binutils-wrapper-2.43.1/bin/ + -frandom-seed=arjng27r1l + -isystem + /nix/store/jd82fkkcyi48pna0q2hjf0x929lsp6gb-llvm-18.1.8-dev/include + -isystem + /nix/store/jd82fkkcyi48pna0q2hjf0x929lsp6gb-llvm-18.1.8-dev/include + -isystem + /nix/store/69v6c64pn1ay26207b0cmzi40qpr9q0w-ncurses-6.4.20221231-dev/include + -isystem + /nix/store/69v6c64pn1ay26207b0cmzi40qpr9q0w-ncurses-6.4.20221231-dev/include + -isystem + /nix/store/06q0p7bhn2ffxxya20rrfqdib3h32csn-zlib-1.3.1-dev/include + -isystem + /nix/store/06q0p7bhn2ffxxya20rrfqdib3h32csn-zlib-1.3.1-dev/include + -isystem + /nix/store/zps3l0mc26r7bvjqd8x0y1lbc6gmbdvn-gnumake-4.4.1/include + -isystem + /nix/store/zps3l0mc26r7bvjqd8x0y1lbc6gmbdvn-gnumake-4.4.1/include + -isystem + /nix/store/kjgslpdqchx1sm7a5h9xibi5rrqcqfnl-python3-3.12.8/include + -isystem + /nix/store/kjgslpdqchx1sm7a5h9xibi5rrqcqfnl-python3-3.12.8/include + -isystem + /nix/store/jii2qmkiwlynzkxyq3ly0cqzv4pj46ng-gdb-15.2/include + -isystem + /nix/store/jii2qmkiwlynzkxyq3ly0cqzv4pj46ng-gdb-15.2/include + -isystem + /nix/store/baa844xjkr00jf4py0i9z5w9arf6bq7r-valgrind-3.23.0-dev/include + -isystem + /nix/store/baa844xjkr00jf4py0i9z5w9arf6bq7r-valgrind-3.23.0-dev/include + -isystem + /nix/store/v5x90j46fchzh3b891hcc1h3pajbcsc7-python3-3.10.16/include + -isystem + /nix/store/v5x90j46fchzh3b891hcc1h3pajbcsc7-python3-3.10.16/include + -isystem + /nix/store/nws87hfnn2sifdwa4r17ixr30zh8c739-compiler-rt-libc-18.1.8-dev/include + -isystem + /nix/store/nws87hfnn2sifdwa4r17ixr30zh8c739-compiler-rt-libc-18.1.8-dev/include + -isystem + /nix/store/p6ccnh6ahw1k9sjrly45l7nx12a2k5aj-compiler-rt-libc-12.0.1-dev/include + -isystem + /nix/store/p6ccnh6ahw1k9sjrly45l7nx12a2k5aj-compiler-rt-libc-12.0.1-dev/include + -isystem + /nix/store/13fw0scdl9ciz5lpabi9nkxwrnn8555g-libglvnd-1.7.0-dev/include + -isystem + /nix/store/13fw0scdl9ciz5lpabi9nkxwrnn8555g-libglvnd-1.7.0-dev/include + -isystem + /nix/store/ax75iyi15z98ygig0qmkwfyvj3c5h4jy-wayland-1.23.1-dev/include + -isystem + /nix/store/ax75iyi15z98ygig0qmkwfyvj3c5h4jy-wayland-1.23.1-dev/include + -isystem + /nix/store/9fyv534c1xp8rvx51zz3jv49cnxalydc-libXrandr-1.5.4-dev/include + -isystem + /nix/store/9fyv534c1xp8rvx51zz3jv49cnxalydc-libXrandr-1.5.4-dev/include + -isystem + /nix/store/xw159a07hd1z48pdkzv65l60j474z9p1-libXrender-0.9.11-dev/include + -isystem + /nix/store/xw159a07hd1z48pdkzv65l60j474z9p1-libXrender-0.9.11-dev/include + -isystem + /nix/store/x79ks3lk3a6d4z6bsma21fx04vrp0nqd-xorgproto-2024.1/include + -isystem + /nix/store/x79ks3lk3a6d4z6bsma21fx04vrp0nqd-xorgproto-2024.1/include + -isystem + /nix/store/2nq19mwkgxmb9npqb4vb7gzs3inpvs3x-libX11-1.8.10-dev/include + -isystem + /nix/store/2nq19mwkgxmb9npqb4vb7gzs3inpvs3x-libX11-1.8.10-dev/include + -isystem + /nix/store/94pix4pzamq3svx4jxjj2sgrvafn29b8-libxcb-1.17.0-dev/include + -isystem + /nix/store/94pix4pzamq3svx4jxjj2sgrvafn29b8-libxcb-1.17.0-dev/include + -isystem + /nix/store/k53lmh232zhcm5mf1gg717cj8aqmc65a-libXinerama-1.1.5-dev/include + -isystem + /nix/store/k53lmh232zhcm5mf1gg717cj8aqmc65a-libXinerama-1.1.5-dev/include + -isystem + /nix/store/4nib5gpv4ckmy9w2drj4g99bx5v0xxz5-libXcursor-1.2.2-dev/include + -isystem + /nix/store/4nib5gpv4ckmy9w2drj4g99bx5v0xxz5-libXcursor-1.2.2-dev/include + -isystem + /nix/store/npvyfw89qr7lyv03ng79bmrdc0bz2wly-libXi-1.8.2-dev/include + -isystem + /nix/store/npvyfw89qr7lyv03ng79bmrdc0bz2wly-libXi-1.8.2-dev/include + -isystem + /nix/store/gmnr6a33dbr978p4dzc8ck589z4ddwm2-libXfixes-6.0.1-dev/include + -isystem + /nix/store/gmnr6a33dbr978p4dzc8ck589z4ddwm2-libXfixes-6.0.1-dev/include + -isystem + /nix/store/sj3jfnpd35rv18z9b1gcjqzjgvff5hq6-libXext-1.3.6-dev/include + -isystem + /nix/store/sj3jfnpd35rv18z9b1gcjqzjgvff5hq6-libXext-1.3.6-dev/include + -isystem + /nix/store/y5yr09v8mph0lq610sla6c0z2j438ppi-libXau-1.0.11-dev/include + -isystem + /nix/store/y5yr09v8mph0lq610sla6c0z2j438ppi-libXau-1.0.11-dev/include + -isystem + /nix/store/3wb7n9majhmwmznvkbsxihxzsnmx9csb-glfw-3.4/include + -isystem + /nix/store/3wb7n9majhmwmznvkbsxihxzsnmx9csb-glfw-3.4/include + -isystem + /nix/store/rgc9szrr28wgx3h0wglhb9ci5qvf2b1h-readline-8.2p13-dev/include + -isystem + /nix/store/rgc9szrr28wgx3h0wglhb9ci5qvf2b1h-readline-8.2p13-dev/include + -isystem + /nix/store/jd82fkkcyi48pna0q2hjf0x929lsp6gb-llvm-18.1.8-dev/include + -isystem + /nix/store/jd82fkkcyi48pna0q2hjf0x929lsp6gb-llvm-18.1.8-dev/include + -isystem + /nix/store/69v6c64pn1ay26207b0cmzi40qpr9q0w-ncurses-6.4.20221231-dev/include + -isystem + /nix/store/69v6c64pn1ay26207b0cmzi40qpr9q0w-ncurses-6.4.20221231-dev/include + -isystem + /nix/store/06q0p7bhn2ffxxya20rrfqdib3h32csn-zlib-1.3.1-dev/include + -isystem + /nix/store/06q0p7bhn2ffxxya20rrfqdib3h32csn-zlib-1.3.1-dev/include + -isystem + /nix/store/zps3l0mc26r7bvjqd8x0y1lbc6gmbdvn-gnumake-4.4.1/include + -isystem + /nix/store/zps3l0mc26r7bvjqd8x0y1lbc6gmbdvn-gnumake-4.4.1/include + -isystem + /nix/store/kjgslpdqchx1sm7a5h9xibi5rrqcqfnl-python3-3.12.8/include + -isystem + /nix/store/kjgslpdqchx1sm7a5h9xibi5rrqcqfnl-python3-3.12.8/include + -isystem + /nix/store/jii2qmkiwlynzkxyq3ly0cqzv4pj46ng-gdb-15.2/include + -isystem + /nix/store/jii2qmkiwlynzkxyq3ly0cqzv4pj46ng-gdb-15.2/include + -isystem + /nix/store/baa844xjkr00jf4py0i9z5w9arf6bq7r-valgrind-3.23.0-dev/include + -isystem + /nix/store/baa844xjkr00jf4py0i9z5w9arf6bq7r-valgrind-3.23.0-dev/include + -isystem + /nix/store/v5x90j46fchzh3b891hcc1h3pajbcsc7-python3-3.10.16/include + -isystem + /nix/store/v5x90j46fchzh3b891hcc1h3pajbcsc7-python3-3.10.16/include + -isystem + /nix/store/nws87hfnn2sifdwa4r17ixr30zh8c739-compiler-rt-libc-18.1.8-dev/include + -isystem + /nix/store/nws87hfnn2sifdwa4r17ixr30zh8c739-compiler-rt-libc-18.1.8-dev/include + -isystem + /nix/store/p6ccnh6ahw1k9sjrly45l7nx12a2k5aj-compiler-rt-libc-12.0.1-dev/include + -isystem + /nix/store/p6ccnh6ahw1k9sjrly45l7nx12a2k5aj-compiler-rt-libc-12.0.1-dev/include + -isystem + /nix/store/13fw0scdl9ciz5lpabi9nkxwrnn8555g-libglvnd-1.7.0-dev/include + -isystem + /nix/store/13fw0scdl9ciz5lpabi9nkxwrnn8555g-libglvnd-1.7.0-dev/include + -isystem + /nix/store/ax75iyi15z98ygig0qmkwfyvj3c5h4jy-wayland-1.23.1-dev/include + -isystem + /nix/store/ax75iyi15z98ygig0qmkwfyvj3c5h4jy-wayland-1.23.1-dev/include + -isystem + /nix/store/9fyv534c1xp8rvx51zz3jv49cnxalydc-libXrandr-1.5.4-dev/include + -isystem + /nix/store/9fyv534c1xp8rvx51zz3jv49cnxalydc-libXrandr-1.5.4-dev/include + -isystem + /nix/store/xw159a07hd1z48pdkzv65l60j474z9p1-libXrender-0.9.11-dev/include + -isystem + /nix/store/xw159a07hd1z48pdkzv65l60j474z9p1-libXrender-0.9.11-dev/include + -isystem + /nix/store/x79ks3lk3a6d4z6bsma21fx04vrp0nqd-xorgproto-2024.1/include + -isystem + /nix/store/x79ks3lk3a6d4z6bsma21fx04vrp0nqd-xorgproto-2024.1/include + -isystem + /nix/store/2nq19mwkgxmb9npqb4vb7gzs3inpvs3x-libX11-1.8.10-dev/include + -isystem + /nix/store/2nq19mwkgxmb9npqb4vb7gzs3inpvs3x-libX11-1.8.10-dev/include + -isystem + /nix/store/94pix4pzamq3svx4jxjj2sgrvafn29b8-libxcb-1.17.0-dev/include + -isystem + /nix/store/94pix4pzamq3svx4jxjj2sgrvafn29b8-libxcb-1.17.0-dev/include + -isystem + /nix/store/k53lmh232zhcm5mf1gg717cj8aqmc65a-libXinerama-1.1.5-dev/include + -isystem + /nix/store/k53lmh232zhcm5mf1gg717cj8aqmc65a-libXinerama-1.1.5-dev/include + -isystem + /nix/store/4nib5gpv4ckmy9w2drj4g99bx5v0xxz5-libXcursor-1.2.2-dev/include + -isystem + /nix/store/4nib5gpv4ckmy9w2drj4g99bx5v0xxz5-libXcursor-1.2.2-dev/include + -isystem + /nix/store/npvyfw89qr7lyv03ng79bmrdc0bz2wly-libXi-1.8.2-dev/include + -isystem + /nix/store/npvyfw89qr7lyv03ng79bmrdc0bz2wly-libXi-1.8.2-dev/include + -isystem + /nix/store/gmnr6a33dbr978p4dzc8ck589z4ddwm2-libXfixes-6.0.1-dev/include + -isystem + /nix/store/gmnr6a33dbr978p4dzc8ck589z4ddwm2-libXfixes-6.0.1-dev/include + -isystem + /nix/store/sj3jfnpd35rv18z9b1gcjqzjgvff5hq6-libXext-1.3.6-dev/include + -isystem + /nix/store/sj3jfnpd35rv18z9b1gcjqzjgvff5hq6-libXext-1.3.6-dev/include + -isystem + /nix/store/y5yr09v8mph0lq610sla6c0z2j438ppi-libXau-1.0.11-dev/include + -isystem + /nix/store/y5yr09v8mph0lq610sla6c0z2j438ppi-libXau-1.0.11-dev/include + -isystem + /nix/store/3wb7n9majhmwmznvkbsxihxzsnmx9csb-glfw-3.4/include + -isystem + /nix/store/3wb7n9majhmwmznvkbsxihxzsnmx9csb-glfw-3.4/include + -isystem + /nix/store/rgc9szrr28wgx3h0wglhb9ci5qvf2b1h-readline-8.2p13-dev/include + -isystem + /nix/store/rgc9szrr28wgx3h0wglhb9ci5qvf2b1h-readline-8.2p13-dev/include + -isystem + /nix/store/jd82fkkcyi48pna0q2hjf0x929lsp6gb-llvm-18.1.8-dev/include + -isystem + /nix/store/69v6c64pn1ay26207b0cmzi40qpr9q0w-ncurses-6.4.20221231-dev/include + -isystem + /nix/store/06q0p7bhn2ffxxya20rrfqdib3h32csn-zlib-1.3.1-dev/include + -isystem + /nix/store/zps3l0mc26r7bvjqd8x0y1lbc6gmbdvn-gnumake-4.4.1/include + -isystem + /nix/store/kjgslpdqchx1sm7a5h9xibi5rrqcqfnl-python3-3.12.8/include + -isystem + /nix/store/jii2qmkiwlynzkxyq3ly0cqzv4pj46ng-gdb-15.2/include + -isystem + /nix/store/baa844xjkr00jf4py0i9z5w9arf6bq7r-valgrind-3.23.0-dev/include + -isystem + /nix/store/v5x90j46fchzh3b891hcc1h3pajbcsc7-python3-3.10.16/include + -isystem + /nix/store/nws87hfnn2sifdwa4r17ixr30zh8c739-compiler-rt-libc-18.1.8-dev/include + -isystem + /nix/store/p6ccnh6ahw1k9sjrly45l7nx12a2k5aj-compiler-rt-libc-12.0.1-dev/include + -isystem + /nix/store/13fw0scdl9ciz5lpabi9nkxwrnn8555g-libglvnd-1.7.0-dev/include + -isystem + /nix/store/ax75iyi15z98ygig0qmkwfyvj3c5h4jy-wayland-1.23.1-dev/include + -isystem + /nix/store/9fyv534c1xp8rvx51zz3jv49cnxalydc-libXrandr-1.5.4-dev/include + -isystem + /nix/store/xw159a07hd1z48pdkzv65l60j474z9p1-libXrender-0.9.11-dev/include + -isystem + /nix/store/x79ks3lk3a6d4z6bsma21fx04vrp0nqd-xorgproto-2024.1/include + -isystem + /nix/store/2nq19mwkgxmb9npqb4vb7gzs3inpvs3x-libX11-1.8.10-dev/include + -isystem + /nix/store/94pix4pzamq3svx4jxjj2sgrvafn29b8-libxcb-1.17.0-dev/include + -isystem + /nix/store/k53lmh232zhcm5mf1gg717cj8aqmc65a-libXinerama-1.1.5-dev/include + -isystem + /nix/store/4nib5gpv4ckmy9w2drj4g99bx5v0xxz5-libXcursor-1.2.2-dev/include + -isystem + /nix/store/npvyfw89qr7lyv03ng79bmrdc0bz2wly-libXi-1.8.2-dev/include + -isystem + /nix/store/gmnr6a33dbr978p4dzc8ck589z4ddwm2-libXfixes-6.0.1-dev/include + -isystem + /nix/store/sj3jfnpd35rv18z9b1gcjqzjgvff5hq6-libXext-1.3.6-dev/include + -isystem + /nix/store/y5yr09v8mph0lq610sla6c0z2j438ppi-libXau-1.0.11-dev/include + -isystem + /nix/store/3wb7n9majhmwmznvkbsxihxzsnmx9csb-glfw-3.4/include + -isystem + /nix/store/rgc9szrr28wgx3h0wglhb9ci5qvf2b1h-readline-8.2p13-dev/include From c027eda58cb1accfb590a7d1f0b002300c525a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 12 Feb 2025 17:17:49 +0100 Subject: [PATCH 16/16] notes: add some usefull resources --- NOTES.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/NOTES.md b/NOTES.md index 7091cee..a6e77b0 100644 --- a/NOTES.md +++ b/NOTES.md @@ -9,6 +9,22 @@ In case of difference between regular bash and posix bash, we decide to follow r ## Ideas for testing * use prysk or shellspec with shell=./minishell +## Usefull resources + +[Bash Reference Manual](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html) + +[Internal parsing & flow](https://mail.gnu.org/archive/html/help-bash/2014-01/msg00000.html) + +[Python parser for bash](https://github.com/idank/bashlex) + +[The Stages of Word Expansion](https://www.gnu.org/software/libc/manual/html_node/Expansion-Stages.html) + +[Diagram of bash parse flow](https://web.archive.org/web/20160308045823/https://stuff.lhunath.com/parser.png) + +[The Bash Parser](http://mywiki.wooledge.org/BashParser) + +[The Architecture of Open Source Applications (Volume 1) The Bourne-Again Shell](https://aosabook.org/en/v1/bash.html) + ## Shell Operation cf. 3.1.1 Shell Operation