owned this note
owned this note
Published
Linked with GitHub
---
tags: epita, piscine
---
# 42sh Wiki
# SCL structure
- **==Shell introduction==**
- **==Quoting==**
- Escape Character (Backslash) `\`
- Single-Quotes `'`
- Double-Quotes `"`
- **==Token Recognition==**
- Alias Substitution
- **==Reserved Words==**
- **==Parameters and Variables==**
- Positional Parameters
- Special Parameters
- Shell Variables
- **==Word Expansions==**
- Tilde Expansion
- Parameter Expansion
- Command Substitution
- Arithmetic Expansion
- Field Splitting
- Pathname Expansion
- Quote Removal
- **==Redirection==**
- Redirecting Input
- Redirecting Output
- Appending Redirected Output
- Here-Document
- Duplicating an Output File Descriptor
- Open File Descriptors for Reading and Writing
- **==Exit Status and Errors==**
- Consequences of Shell Errors
- Exit Status for Commands
- **==Shell Commands==**
- Simple Commands
- Pipelines
- Lists
- Compound Commands
- Function Definition Command
- **==Shell Grammar==**
- Shell Grammar Lexical Conventions
- Shell Grammar Rules
- **==Signals and Error Handling==**
- **==Shell Execution Environment==**
- **==Pattern Matching Notation==**
- Patterns Matching a Single Character
- Patterns Matching Multiple Characters
- Patterns Used for Filename Expansion
- **==Special Built-In Utilities==**
# Tips
## 1) Utilities
- `tree -I 'venv|build'`: display all directories execpt `venv` and `build`.
- `alias ccg="gcc -Wall -Wextra -Werror -pedantic -std=c99 -g -fsanitize=address -o out"`
## 2) Shell
### <ins>A) To check if command is POSIX-compliant</ins>
- run `bash --posix` in terminal.
- run your `command`
## 3) Cmake
### <ins>A) General</ins>
- Add macro to target (ex: `_XOPEN_SOURCE >= 500`)
- [Conférences 42sh 2019 - Build systems => Timeline 2:16:56](https://youtu.be/jp1l4mg3NN0?t=8216)
- Include directories to target (ex: `gcc -Isrc`)
- [Conférences 42sh 2019 - Build systems => Timeline 2:17:39](https://youtu.be/jp1l4mg3NN0?t=8259)
### <ins>B) Run Pytest</ins>
- First time:
- Download `conftest.py` (Renamed if needed) and move it to test folder. (not needed as pierre did it for us)
- Run `python -m venv venv` to create env. ( in root of dir)
- To activate env, run `source venv/bin/activate`
- Run `pip install pytest pyyaml`
- Run `pip install cmake-format` ==(`cmake-format -i CMakeLists.txt`)==
- To run tests, type `pytest`
- Tests are written in a `.yaml` file (see `conftest.py` for syntax).
- To deactivate env, run `deactivate`
- Other:
- To activate env, run `source venv/bin/activate`
- To run tests, type `pytest`
- To deactivate env, run `deactivate`
### <ins>C) Run criterion tests</ins>
> To run criterion testsuite: `ctest --verbose` or `make check` (if implemented).
- Suppose the following architecture:
```
├── CMakeLists.txt (1)
├── src
│ ├── CMakeLists.txt (2)
│ ├── lexer
│ │ ├── CMakeLists.txt (3)
│ │ ├── lexer.c
│ │ ├── lexer.h
│ │ ├── token.c
│ │ └── token.h
└── tests
└── utests
├── CMakeLists.txt (4)
├── lexer
│ └── lexer_1.c
```
- [x] In `CMakeLists.txt (1)`, make sure to have:
- `add_subdirectory(src/lexer)`
- [x] In `CMakeLists.txt (2)`, make sure to have:
- `target_link_libraries(42sh PRIVATE ${READLINE_LIB} common_options lexer)`
- [x] In `CMakeLists.txt (3)`, make sure to have:
```
add_library(lexer STATIC <--- THIS
lexer.c
lexer.h
token.c
token.h
)
target_include_directories(lexer PUBLIC .) <--- THIS
target_link_libraries(lexer PRIVATE common_options) <--- THIS
```
- [x] In `CMakeLists.txt (4)`, make sure to have:
```
find_library(CRITERION_LIB criterion)
add_executable(test_criterion utests_1.c lexer/lexer_1.c)
^
`--- THIS
target_link_libraries(test_criterion PRIVATE
${CRITERION_LIB}
common_options
lexer <-- THIS
)
```
- [x] In `lexer/lexer_1.c`, only need to include `#include "token.h"`
## 4) Git
### A) <ins>`.git/config`</ins>
```c=
[core]
pager = less -S
[alias]
tree = log --graph --decorate --pretty=oneline --abbrev-commit
```
- `git tree`: view commit in tree-like view.
- `git config --global pull.rebase true`: Enable pull rebase.
### B) branching
- `git branch`: Display all existing branches.
- `git checkout <branch>`: switch branch
- `git checkout -b <branch>`: create branch + switch branch
- `git push --set-upstream origin <branch>`: Tells git to push to `<branch>` and not master.
### C) Merge
- `git checkout master`
- `git pull`
- `git merge <name_branch>`
## 5) Debugging
- In your file test, replace `Test(....)` by `int main(void)`
- Run `make debug`, an executable named `utest_criterion` will be created.
- Run `gdb utest_criterion`.
- ==**ALWAYS `make clean` + `rm utest_criterion`**==
- because the `utest_criterion` at the root is a copy from the one in test. Thus `make clean` won't see it.
### Valgrind
- At root, `mkdir build/`
- `cd build`
- `cmake .. -DCMAKE_BUILD_TYPE=Release` (This should run version without fsanitize=address)
### fsanitize=address
- At root, `mkdir debug/`
- `cd debug`
- `cmake .. -DCMAKE_BUILD_TYPE=Debug` (This should run version with fsanitize=address)
## 6) Static library linking
- Use `-Wl,--start-group` and `-Wl,--end-group` to stop worrying about libraries ordering.
```C
target_link_libraries(
utest_criterion
PRIVATE ${CRITERION_LIB}
common_options
"-Wl,--start-group"
ast
execution
option_parser
parser
lexer
data-structure
utils
"-Wl,--end-group")
```
## 7) Doxygen (manpage)
- if you want to have acces to manpage from root, use the command : `groff -man -Tascii doc/42sh.man | less`
- Updates can and MUST be done, it can be manipulated like a .txt document in vim/emacs.
# Miscellaneous
- [Shell architecture (made by ex-ACU)](https://shell.multun.net/index.html)
- [Check if script is posix compliant (use !/bin/sh)](https://www.shellcheck.net/)
- [Example to clarify interpretation and syntax of shell grammar](https://lists.gnu.org/archive/html/help-bash/2015-07/msg00022.html)
- [Layman explication of `Token Recogntion` && `Shell Grammar` sections](https://docs.google.com/document/d/14JO-CZBlcv3WQFh1998gGwD_XOz5kyhyAiqKGSJlFJI/edit)
- [Variables en shell Bash](http://www.epons.org/shell-bash-variables.php)
- [bash-cheatsheet](https://devhints.io/bash)
- [bash: all about redirections](https://catonmat.net/bash-one-liners-explained-part-three)
# Conferences
- [Shell Grammar conference](https://web.microsoftstream.com/video/aa398132-009f-4bdf-a5a6-8afbb4a71b55)
- [Tests UFOs did it: learning about the test pyramid (python too)](https://web.microsoftstream.com/video/77e9f560-9058-40a4-8b16-37ede92fc386)
- [Python & Testsuite fonctionnelle](https://www.youtube.com/watch?v=1CoFy6eM7_I)
- [Gaby conf achitecture shell](https://www.youtube.com/watch?v=oIFRiwFRSRY)