Git Product home page Git Product logo

42sh's Introduction

42sh

Projet de fin de première année du cursus Epitech répliquant le fonctionnement du shell TCSH.

42sh's People

Contributors

neo-diamons avatar thibauthh avatar raphael-mabille avatar gaulaume avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

42sh's Issues

Conditional jump on first call to unknown function

the issue only happens at the first call of an unknow function, i dunno if that's because valgrind hide the ones that follow.
there is also a related memory leak.

[raphael@fedora 42sh]$ valgrind --leak-check=full --show-leak-kinds=all ./42sh 
==13192== Memcheck, a memory error detector
==13192== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==13192== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==13192== Command: ./42sh
==13192== 
$> oui
==13192== Conditional jump or move depends on uninitialised value(s)
==13192==    at 0x40325C: get_bin_path (get_cmd_path.c:59)
==13192==    by 0x40336D: get_cmd_path (get_cmd_path.c:74)
==13192==    by 0x403FB5: run_pipeline (run_pipeline.c:97)
==13192==    by 0x4017EA: mysh (mysh.c:43)
==13192==    by 0x4015D6: main (main.c:58)
==13192== 
oui: Command not found.
$> exit
==13192== 
==13192== HEAP SUMMARY:
==13192==     in use at exit: 20 bytes in 1 blocks
==13192==   total heap usage: 67 allocs, 66 frees, 256,229 bytes allocated
==13192== 
==13192== 20 bytes in 1 blocks are still reachable in loss record 1 of 1
==13192==    at 0x484586F: malloc (vg_replace_malloc.c:381)
==13192==    by 0x4054F6: ice_realloc2 (in /home/raphael/delivery/unix_system_programing/42sh/42sh)
==13192==    by 0x403092: compose_path (get_cmd_path.c:38)
==13192==    by 0x403241: get_bin_path (get_cmd_path.c:60)
==13192==    by 0x40336D: get_cmd_path (get_cmd_path.c:74)
==13192==    by 0x403FB5: run_pipeline (run_pipeline.c:97)
==13192==    by 0x4017EA: mysh (mysh.c:43)
==13192==    by 0x4015D6: main (main.c:58)
==13192== 
==13192== LEAK SUMMARY:
==13192==    definitely lost: 0 bytes in 0 blocks
==13192==    indirectly lost: 0 bytes in 0 blocks
==13192==      possibly lost: 0 bytes in 0 blocks
==13192==    still reachable: 20 bytes in 1 blocks
==13192==         suppressed: 0 bytes in 0 blocks
==13192== 
==13192== Use --track-origins=yes to see where uninitialised values come from
==13192== For lists of detected and suppressed errors, rerun with: -s
==13192== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Scripting builtins

Scripting builtins

Implement if, while and foreach control structures and the special expression syntax.

Control structures

  • if (<expr>) then ... [else ... ]endif
  • while (<expr>) ... end
  • foreach <var> (<words>) ... end

Expressions

  • Mathematical operators (+, -, *, /, %)
  • + and - are right-associative
  • Comparison operators (==, !=, <, <=, >, >=)
  • == and != compare strings; the rest, numbers
  • Pattern matching operators (=~, !~)
  • Regex engine?
  • Logical operators (&&, ||, !)
  • Bit-wise and bitshift operators (~, &, |, ^, <<, >>)
  • Precedence override (())
    Null or missing operands are considered to be 0.

File inquiries (-<letter> <file>)

  • r, w, x: Read, write and execute permissions respectively
  • o: Have ownership
  • e: Is (Existence)
  • z: Is empty (zero size file)
  • f: Is regular file
  • d: Is directory

History

History

Save previous commands to history.

  • Save history to $histfile
  • Access previous and next command in history with Up and Down arrow
  • Format events with clean spaces (cd dir;pwd => cd dir ; pwd)

History substitution

  • !n: Event #n
  • !-n: n-th previous event
  • !!: Last event (equiv. !-1)
  • !#: Current event (maximum 10 levels of recursion are allowed)
  • !<string>: Most recent event beginning with <string>
  • !?<string>?: Most recent event containing <string> (last ? can be a newline)

Word selection !<event specifier>:<word selctor>

  • :0: First word (command)
  • :n: n-th argument
  • :^: First argument (equiv. :1)
  • :$: Last argument
  • :%: Word matched by a ?<string>? search
  • :n-m: Range of words
  • :-n: equiv. 0-n
  • :*: equiv. ^-$ (returns nothing on single word event)
  • :n*: equiv. n-$
  • :n*: equiv. n* but without :$

Word modifiers

Extra :<letter> transformations...

Environment structure is not portable

The env_t struct uses two consecutive arrays in the decleration that could potentially be structure padded or reordered in the wrong way by the compiler.

Aliases

Aliases

Set aliases with the alias builtin.

  • alias builtin

Aliases with special meaning

  • precmd: Run before every displayed prompt
  • cwdcmd: Run after every change of working directory (cd, popd)

Shell variables

Shell variables

Add a second set of variables (same system as environment, but not exported to commands).

  • Manipulation done through set and unset builtins.
  • Read-only variables

Variables wth special meaning

  • term: Terminal type
  • cwd: Absolute path of current working directory
  • owd: Absolute path of previous working directory
  • ignoreeof: Print Use "exit" to leave tcsh. and ignore value minus one consecutive EOFs (Ctrl+D) before exiting, or 25 if set to 0 or to a non-numeric or empty value
  • home: Expansion of ~
  • uid, gid: User real UID and GID
  • user, group: User and main user group names
  • status: Last command's/pipeline's return status

Input line edition

Input line edition

Implement advanced command line editing.

  • Advanced input edition (Arrow keys and modifiers, Delete and backspace and modifiers, etc)
  • Auto completion of file names
  • Advanced auto completion base on context

Signal handling

Signal Handling

Handle signals coming from th e terminal emulator, like SIGINT, SIGSTOP, etc.

  • SIGTSTP handler: stop current job
  • SIGINT handler: interrupt current job or current line input

Sub-shell execution

Sub-shell execution

Execute a sequence of commands inside a sub-shell when encased in pathenses (()).

Example

> pwd
/etc
> ( cd ; pwd ) ; pwd
/home/user
/etc
>

Getline, error handling.

The getline call could go in the loop condition, and it must have error checked, so reset errno before each call and check it's value to differentiate EOF from I/O error.

Originally posted by @ThibautHH in #15 (comment)

Builtins not at the end of a pipeline are executed in the main shell process

Example

cd | pwd
cd isn't executed in a child process and thus change to working directory of the shell.

Expected result

$> _pwd_
/home/user/directory
$> _cd | pwd_
/home/user/directory
$>

Actual result

$> _pwd_
/home/user/directory
$> _cd | pwd_
$> /home/user/directory
_pwd_
/home/user
$>

Text between underscores is user input.

Isatty, error handling.

To be particularly thourough, you could check for EBADF errno return of every isatty call (in a macro for example) to check if the FD is closed (non-tty FDs set errno to ENOTTY),
although getline would fail and tell you anyway in this particular instance.

Originally posted by @ThibautHH in #15 (comment)

Document Minishell

Documentation

Self documenting code is a lie
Document Minishell2 code and maybe use Github Wiki pages to add extra notes.

Job control

Job control

Implement process list (jobs to show), fg/bg builtins, % substitutions

Process list

Each job-controlled command/pipeline must be registered in the job list.
Jobs have a status, they may be Stopped or Running (see #2, signal handling).

  • Job list
  • Job statuses
  • Job completion logging
  • jobs builtin to list current jobs

fg and bg builtins

  • fg: Return specified or current job to the foreground
  • bg: Resume specified stopped or current job in the background

% substitutions

  • %[%|+]: Current (last) job
  • %-: Next-to-last job
  • %n: Job #n
  • %<string>: Job begining with string
  • %?<string>: Job containing string

Character escaping

Character escaping

Use \ to escape any character, and use it literally in the the command line.

Example

> \[space]
 : Command not found.
>

GET_VAR

Refactor code to use identifier composition in GET_VAR macro instead of var_type_t type variable for clarity.

Examples

var_type_t type = VAR_SHELL;
printf("Shell variable 'hello' has value: '%s'\n", GET_VAR("hello"));

will become

printf("Shell variable 'hello' has value: '%s'\n", GET_VAR("hello", SHELL));

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.