Skip to content

Latest commit

 

History

History
201 lines (168 loc) · 8.87 KB

README.md

File metadata and controls

201 lines (168 loc) · 8.87 KB

42sh

A 42 school project. A bash style shell with some features from Oh my Zsh. Made with 4 persons. A bash style shell with some features from Oh my Zsh. The project follow the C Norme of 42 school. if you dont see any 'for' or 'case', or have to witness some awful code, dont be surprised, blame the Norme ! 42 - Norme

DISCLAIMER

The following list of features is not accurate, some have been removed from the project because unstable, while some do not figure in the list. The features presented in the following document should be here and enable relatively experienced user to use this shell without too much discomfort. 42 - 42sh Subject

How to

Requirements

Termcap being as old as it is, it can be complicated to get the library setup on some systems.

Installing

git clone https:/ade-sede/42sh.git 42sh
cd 42sh && make

Running

./42sh

The line editing does not work anymore and seems to lock / misbehave all the time. This is because we were forced to use the TERMCAP library as part of the school subject. Termcap was already obsolete 15 years ago ... In order to work again we would need to replace Termcap with Ncurses .

You can still test execution of commands and scripts using the following command.

./42sh -c "<command>"

Syntax coloring

set -o syncolor

Line editing

Keys Functions
Opt+v
Opt+c
Opt+p
Enter visual mode
Copy
Paste
Opt+
Opt+
Move per words.
Opt+
Opt+
Move per line.

History

Keys Functions

Browse the history.
Ctrl+R Back Search.
!! Retype the last command.
!n Retype the n(numerical value) command from the begin of history.
!-n Retype the -n(numerical value) command from the last command.
!name Search for a command beginning with name.
!?name Search for a command which contain name.

Autocompletion

Autocompletion works with binary, path and env variable. Output is colored upon type. Using arrows to navigate is supported. Autorefreshing with a new input from the user: modification of the list of possibility.

Commands Functions
$> l[tab] Search for binary.
$> ls s[tab] Search for path/files.
$> $[tab] Search for variables in env.
$> [a-b]{1,2}*[tab] Search file matching pattern.

Globbing

Posix globbing see Pattern Matching Notation

Pattern Behavior
* Everything.
? Single char.
[a-z] Match range from a to z.
[!a-z] [^a-z] Exclude range from a to z.

Job Control

Commands Functions
jobs List all the current running jobs.
fg Bring the most recent process to foreground.
fg n Bring the specified jobs to foreground where n is the numerical value of the process found in jobs.
Ctrl+C Terminate/Kill current foreground process.
Ctrl+Z Suspend/Stop current foreground process.

Variable Expansion

During execution, the shell perform various expansion as specified by POSIX. Our Shell do in order:

commands Exemple Functions
• Brace Expansion: a{d,c,b}e a{d,c,b}e Expansion of expressions within braces.
• Tilde Expansion: ~ Expansion of the ~ character.
• Shell Parameter Expansion $PATH, ${PATH}, ${#PATH} expands local and environement variable.
• Command Substitution $(ls), `echo a` Using the output of a command as an argument.
• Word Splitting $> IFS=a; var=baba echo $var => b b the result of the three expansions above are splitted into token using IFS variable.
• Filename Expansion *, [a-z], [!a-z] match file with shell regex.
• Quote Removal $>echo "lol" => lol remove quotes and backslash as needed.

Grammar

Tokens

Bellow the tokens of the POSIX Grammar which can take many litteral forms. In the exemple le token is in ' '.

Tokens Exemple
WORD ls './srcs'
ASSIGNMENT_WORD 'a=b' ;
NAME for 'foo' in bar; do ls; done
NEWLINE ls '\n' ls
IO_NUMBER ls ./srcs '2'&>1

Rules

Bellow the Backus Naur form of the POSIX Grammar which we parse and execute.

Symbol Components
complete_commands complete_commands newline_list complete_command
complete_command
complete_command list separator_op
list
list list separator_op and_or
and_or
and_or pipeline
and_or '&&' linebreak pipeline
and_or '
pipeline pipe_sequence
'!' pipe_sequence
pipe_sequence command
pipe_sequence '
' linebreak command
command simple_command
compound_command
compound_command redirect_list
function_definition
compound_command brace_group
subshell
for_clause
case_clause
if_clause
while_clause
until_clause
subshell '(' compound_list ')'
compound_list linebreak term
linebreak term separator
term term separator and_or
and_or
for_clause 'for' name do_group
'for' name sequential_sep do_group
'for' name linebreak in sequential_sep do_group
'for' name linebreak in wordlist sequential_sep do_group
name NAME
in 'in'
wordlist wordlist WORD
WORD
case_clause 'case' WORD linebreak in linebreak case_list 'esac'
'case' WORD linebreak in linebreak case_list_ns 'esac'
'case' WORD linebreak in linebreak 'esac'
case_list_ns case_list case_item_ns
case_item_ns
case_list case_list case_item
case_item
case_item_ns pattern ')' linebreak
pattern ')' compound_list
'(' pattern ')' linebreak
'(' pattern ')' compound_list
case_item pattern ')' linebreak ';;' linebreak
pattern ')' compound_list ';;' linebreak
'(' pattern ')' linebreak ';;' linebreak
'(' pattern ')' compound_list ';;' linebreak
pattern WORD
if_clause 'if' compound_list 'then' compound_list else_part 'fi'
'if' compound_list 'then' compound_list 'fi'
else_part 'elif' compound_list 'then' compound_list
'elif' compound_list 'then' compound_list else_part
'else' compound_list
while_clause 'while' compound_list do_group
until_clause 'until' compound_list do_group
function_definition fname '(' ')' linebreak function_body
function_body compound_command
fname NAME
brace_group '{' compound_list '}'
do_group 'do' compound_list 'do'ne
simple_command cmd_prefix cmd_word cmd_suffix
cmd_prefix cmd_word
cmd_prefix
cmd_name cmd_suffix
cmd_name
cmd_name WORD
cmd_word WORD
cmd_prefix io_redirect
cmd_prefix io_redirect
ASSIGNMENT_WORD
cmd_prefix ASSIGNMENT_WORD
cmd_suffix io_redirect
cmd_suffix io_redirect
WORD
cmd_suffix WORD
redirect_list io_redirect
redirect_list io_redirect
io_redirect io_file
IO_NUMBER io_file
io_here
IO_NUMBER io_here
io_file '<' filename
'<&' filename
'>' filename
'>&' filename
'>>' filename
'<>' filename
'>
filename WORD
io_here '<<' here_end
'<<-' here_end
here_end WORD
newline_list NEWLINE
newline_list NEWLINE
linebreak newline_list
separator_op '&'
separator separator_op linebreak
newline_list
newline_list

Exemples

This code of the factorial function works fine in our shell.

factorial()
{
  if test "$1" -gt "1" ; then
    i=$(($1 - 1))
    j=$(factorial $i)
    k=$(($1 * $j))
    echo $k
  else
    echo 1
  fi
}   

Authors