Giter Club home page Giter Club logo

42-minishell's Introduction

42-minishell's People

Contributors

sandoramix avatar martybb01 avatar

Stargazers

 avatar

Watchers

 avatar

42-minishell's Issues

[CMD] export

Implementare il commando export

Descrizione:

Questo commando mostra tutte le variabili d'ambiente e ha la possibilita' di aggiungerli/modificarli.

  • Se usato senza argomenti: viene stampata la lista di tutte le variabili d'ambiente con il prefisso "declare -x ".
    • Es:
      declare -x SGOINFRE="/nfs/homes/odudniak/sgoinfre"
      declare -x SHELL="/bin/zsh"
      declare -x SHLVL="2"
      
  • Se usato con: export <name>: viene creata una variabile chiamata <name> con il valore uguale a "" (stringa vuota)
    • Es:
      export test
      • Env. variable: test=''
  • Se usato con: export <name>=<value> viene creata una variabile chiamata <name> con il valore uguale a <value>
    • Es:
      export test=foobar
      • Env. variable: test=foobar

Return value

0


Possibile soluzione:

Se si utilizza la gestione delle variabili d'ambiente tramite le liste, basterebbe implementare la ricerca del nodo in base alla chiave (<name>) e modificare o aggiungere il nodo con quel valore <value>


ChatGPT Suggestions:

[CMD] echo

Implementare il commando echo

  • Alla fine del messaggio di echo viene inserito \n
  • Gestire anche l'opzione -n
    • Non inserire \n alla fine dell'output in automatico
      • Es 1
        echo -n ciao | cat -e
        Risultato:
        ciao%
        
      • Es 2
        echo -n "ciao\n" | cat -e
        Risultato:
        ciao$
        

Bisogna stare attenti a:

  • Variabili d'ambiente (espansione con " o senza quotes)
  • Se vengono passati N argomenti (separati da X spazi) bisogna stampare ogni argomento separato dallo spazio
    • Es:
      echo ciao     mondo
      Risultato:
      ciao mondo
      

[CMD] exit

Implementare il commando exit

Descrizione:

  • Uscita dalla shell con uno statuscode

    • Range "valido": 0-255 (inclusi)
  • All'esecuzione del comando viene stampato "exit\n"

  • Se nessun argomento e' stato specificato, lo status code e' 0 se nessun commando e' stato eseguito, altrimenti e' lo statuscode dell'ultimo commando eseguito.

  • Se viene specificato un argomento n la shell viene chiusa con lo status code n specificato.

  • Se ci sono piu' argomenti, viene considerato il primo argomento come lo statuscode, oltre ad "exit\n" viene stampato un messaggio di errore ("bash: exit: too many arguments"), e la shell non si chiude.

Return/exit value

   The exit status shall be n, if specified. Otherwise, the value shall be
   the exit value of the last command executed, or zero if no command  was
   executed.  When  exit is executed in a trap action, the last command is
   considered to be the command that executed  immediately  preceding  the
   trap action.

Possibile soluzione:


ChatGPT Suggestions:

  • exit: This function terminates the calling process. It's typically used to exit from a program.

[CMD] env

Implementare il commando env

Descrizione:

  • Il commando non deve gestire ne le opzioni ne argomenti
  • Deve stampare tutte le variabili d'ambiente nel seguente formato: <name>=<value>
  • Es:
    USER=odudniak
    DISPLAY=:0
    SHLVL=2
    NVM_CD_FLAGS=-q
    PAGER=less
    

Return/exit value

0


Possibile soluzione:

Se si utilizza la gestione delle variabili d'ambiente tramite le liste, basterebbe implementare la stampa di tutti i nodi nel formato corretto.


ChatGPT Suggestions:

Parsing flow

Proposta del Flow

  • lettura delle righe dal stdin con readline
  • Viene effettuato il controllo che l'input sia veritiero. Se non e' valido allora si stampa un messaggio di errore, deallochiamo la linea e si riparte in ascolto di una nuova linea.
    • Se ci sono quotes, devono avere una chiusura.
    • Se ci sono token [<, >, <<], devono avere con un commando/argomento accanto a loro (forse?).
  • La linea ottenuta non viene alterata ma salvata in una struttura (lista) a parte [#1]
  • Si fa lo "split" custom degli argomenti, facendo il "collapse" delle quotes, cioe' attaccare le stringhe all'interno di quotes ad un un'altro argomento, SENZA togliere le quotes (es: e'cho' ''ciao'mondo' > ["e'cho'", "''ciao'mondo'", NULL])
  • Questi argomenti splittati saranno messi invece di un char **, un array/lista (da decidere, per un uso piu' semplice conviene usare gli array, per una prestazione migliore [meno allocazioni, ecc...] convengono le liste) di strutture, che avranno la stringa dell'argomento e un tipo con il valore:
    • DEFAULT (0) - Una semplice stringa/argomento
    • TOKEN (1) - Un token speciale (< > << ecc...).
  • Viene effettuata l'espansione delle variabili all'interno di ogni argomento, tenendo in conto se sta dentro le quotes e che non siano quotes singole. Dopodiche' le quotes vengono tolte
  • Suppongo ci sia il bisogno di fare una divisione della lista/array finale se ci sono pipeline

Refactor dei commandi

Refactor dei commandi

Descrizione:

Quando si finisce il parsing dell'input (facendo vari check di validita', splittando, espandendo le variabili) nella lista di argomenti, tutti i commandi fatti finora dovranno essere modificati per adattarsi alla lista nuova creata in t_list * al posto di char **

[CMD] pwd

Implementare il commando pwd

All'esecuzione vengono ignorati tutti gli argomenti e viene stampato il path assoluto della cartella corrente.

Le opzioni non si devono gestire.

Possibili soluzioni:

  • Ho notato che da bash che ci sono due variabili d'ambiente chiamate PWD e OLDPWD che:
    • Si aggiornano quando si usa cd (OLDPWD inizialmente non esiste ma viene aggiunto dopo il primo cd diverso da PWD).
    • Tuttavia se viene usato unset su PWD, pwd funziona ancora.
    • Quindi ci serve una variabile nella t_var per tenere al "sicuro" la directory corrente, che verra' inizializzata con $PWD e ad ogni cambio di cartella verra' aggiornata.

ChatGPT Suggestions:

[FEAT] Pipe

Implementare pipe | operator

Descrizione:

The output of each command in the pipeline is connected to the input of the next command via a pipe.

Return/exit value

The exit status of a pipeline is the exit status of the last command in the pipeline


Possibile soluzione:


ChatGPT Suggestions:

  1. Parse the cmd line args
  2. Create a pipe
  3. Fork processes
  4. Redirect I/O (1st cmd stdout to pipe[write-end], 2nd cmd reads from pipe and writes to stdout)
  5. Execute cmds --> execve
  6. Close pipes
  7. Wait for child processes

[~CMD] history

Implementare il commando history

  • Deve mostrare tutte le linee inviate al terminale. Ogni linea e' identificata dal suo numero (ordine) di esecuzione.
  • Es:
    • history
    • Risultato:
         1  2024-04-20 15:04  cat nohup.out
         2  2024-04-20 15:04  history
         3  2024-04-20 15:04  ls
         4  2024-04-20 15:04  ls -l
         5  2024-04-20 15:04  ls -l | grep 4060
         6  2024-04-20 15:04  ls -l | grep 4096
         7  2024-04-20 15:04  echo "test"
         8  2024-04-20 15:04  echo $TEST
         9  2024-04-20 15:04  echo $USER
        10  2024-04-20 15:05  history
        11  2024-04-20 15:05  nano test
        12  2024-04-20 15:05  cat test
        13  2024-04-20 15:05  nano test
        14  2024-04-20 15:05  history
        15  2024-04-20 15:05  ls
        16  2024-04-20 15:05  sl
      
  • Non deve salvare il heredoc (<<)
    • Es:
        17  2024-04-20 15:18  << EOF\nCIAO\nEOF
      

Forse si puo' semplificare togliendo le colonne della data e l'ora.

    1  cat nohup.out
    2  history
    3  ls
    4  ls -l
    5  ls -l | grep 4060
    6  ls -l | grep 4096
    7  echo "test"
    8  echo $TEST
    9  echo $USER
   10  history
   11  nano test
   12  cat test
   13  nano test
   14  history
   15  ls
   16  sl

[CMD] unset

Implementare il commando unset

Descrizione:

  • gestisce la rimozione di 1 o piu' variabili d'ambiente.
  • E' obbligatorio avere almeno 1 argomento
  • Es:
    unset PWD nonexistent

Return/exit value

    0    All name operands were successfully unset.

Possibile soluzione:

Se si utilizza la gestione delle variabili d'ambiente tramite le liste, basterebbe implementare la ricerca e l'eliminazione di un nodo.


ChatGPT Suggestions:

Implementare il prompt

Implementare il prompt (readline)

Descrizione:

Bisogna implementare un while loop che ascolta l'input dell'utente per poi successivamente parsare ed eseguire i commandi.

Return/exit value

N/A


Possibile soluzione:

Si potrebbe usare la readline che gestisce anche la "history" degli ultimi commandi, usando gli directional arrows.


ChatGPT Suggestions:

N/A

readline + builtin usage

Integrare readline con i builtin creati finora.

Descrizione:

Visto che e' gia' possibile "parsare" (parzialmente) una stringa, possiamo integrare l'uso dei builtin con la funzione readline.

La procedura dovrebbe essere circa questa:

  • In un while loop si usa readline
  • Il contenuto preso va aggiunto (con lst_addnew_tail) in una lista nella struttura t_var mshell che serve per la history (#1) (che si dealloca solo in cleanup). La chiave del nodo puo' rimanere NULL
  • Lo stesso contenuto viene parsato da cmd_parse_new + expand_and_clear (vedi esempio qui) per ottenere la lista dei commandi/argomenti (per adesso si gestisce solo un commando).
  • Data la lista dei commandi/argomenti si cerca di eseguire il builtin (str_cmp sul primo nodo). Se non e' un commando conosciuto si stampa <minishell prompt>: command not found: <valore del nodo> sullo STD_ERR.
  • Si dealloca la lista degli argomenti e si riparte dalla readline

P.s. il #if DEBUG == false dentro main.c va tolto.

[CMD] cd

Implementare il commando cd

Possibile soluzione

  • Si suppone di utilizzare questa funzione:
  • Bisogna Gestire i path relativi e quelli assoluti.

EXIT STATUS

   The following exit values shall be returned:

    0    The directory was successfully changed.

   >0    An error occurred

ChatGPT suggestions:

  • getcwd: Get the current working directory.
  • access: Check whether a directory exists and is accessible.
  • strerror: Get a string describing the last error encountered during a call to a system or library function.

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.