Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/FreeJules/simple_shell

UNIX command line interpreter
https://github.com/FreeJules/simple_shell

Last synced: about 2 months ago
JSON representation

UNIX command line interpreter

Lists

README

        

# Scarjo
Scarjo is a simple shell created as an end of term project for @holbertonschool.

## Table of Contents
* [**Project Details**](#project-details)
* [Description](#description)
* [Function Visualization](#function-visualization)
* [File descriptions](#file-descriptions)
* [Project Requirements](#project-requirements)
* [Project Objectives](#what-students-should-learn-from-this-project)
* [**Project Breakdown**](#project-breakdown)
* [**Getting Started**](#getting-started)
* [Installation](#installation)
* [Screenshots](#screenshots)
* [**Contributing**](#contributing)
* [**Team**](#team)
* [**Resources**](#resources)

---
## Project Details

### Description
ScarJo Shell (hsh) is a simple UNIX command line shell and was built as an end of term project for [Holberton School](https://github.com/holbertonschool/). ScarJo includes many basic features present in the bash shell.

#### Function visualization
![image](https://cloud.githubusercontent.com/assets/23224088/25165641/5b9504f8-248c-11e7-8d0d-c874620cea9a.png)
To interact with image, [click here](https://sketchboard.me/XArlTLxbFDKu).

#### File descriptions

File Functions contained | Description and Return value
------ | :---
**[\_getline.c](/_getline.c)**


  • int _getline(char *input, int size)

  • int exit_shell(char *line)

|

  • Description
    • \_getline() uses _strprint to reads an entire line from standard input, storing the address of the buffer containing the text into *lineptr. The buffer is null-terminated and includes the newline character, if one was found.

  • Return value

    • If *input is NULL, then \_getline() will allocate a buffer for storing the line, which should be freed by the user program. (In this case, the value in \*n is ignored.)
    • On success, \_getline() return the number of characters read, including the delimiter character, but not including the terminating null byte ('\0'). This value can be used to handle embedded null bytes in the line read.
    • Both functions return -1 on failure to read a line (including end-of-file condition). In the event of an error, errno is set to indicate the cause.



**[\_strtok.c](/_strtok.c)**

  • int count_words(char *str, char delim)

  • int _wrdlen(char *s, char delim)

  • char **strtow(char *str, char delim)

|

  • Description


    • This program is our implementation of the strtok() function which extracts tokens from strings.
    • The \_strtok() function breaks a string into a sequence of zero or more nonempty tokens. On the first call to \_strtok() the string to be parsed should be specified in `str`. In each subsequent call that should parse the same string, `str` must be NULL.

    • The delim argument specifies a set of bytes that delimit the tokens in the parsed string. The caller may specify different strings in delim in successive calls that parse the same string.

    • Each call to \_strtok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, \_strtok() returns `NULL`.


  • Return value

    • Returns string tokenized into words
    • The `_strtok()` functions return a pointer to the next token, or `NULL` if there are no more tokens.



**[array_list.c](/array_list.c)**

  • `int arr_size(char **arr)`

  • `list_t *array_to_list(char **array)`

|


  • Description
    • This program builds a linked list of an array of pointers.


  • Return value

    • A pointer of type `list_t` to head


**[lists.c](/lists.c)**

  • `size_t print_list(const list_t *h)`

  • `size_t list_len(const list_t *h)`

  • `list_t *add_node(list_t \*\*head, const char *str)`

  • `list_t *add_node_end(list_t \*\*head, const char *str)`

  • `void free_list(list_t *head)`

|

  • Description


    • `print_list(const list_t *h)` prints all elements of a linked list pointed to by **\*h**

    • `size_t list_len(const list_t *h)` calculates the number of elements in a linked list pointed to by **\*h**

    • `list_t *add_node(list_t **head, const char *str)` adds node to the beginning of a linked list

    • `list_t *add_node_end(list_t **head, const char *str)` adds node to the end of a linked list

    • `void free_list(list_t *head)` frees a linked list of type


  • Return value


    • `print_list()` prints elements of list to stdout

    • `size_t list_len()` length of list

    • `list_t *add_node()` address of the new element, NULL if failed

    • `list_t *add_node_end()` address of the new element, NULL if failed

    • `void free_list()` n/a



**[main.c](/main.c)** | - | - |
**[more_strings.c](/more_strings.c)**

  • _strcmp(char *s1, char *s2)

  • char *_strchr(const char *str, char c)

  • int len_to_char(char *str, char c)

|

  • Description


    • `int _strcmp(char *s1, char *s2)` - compare strings **s1** and **s2**

    • char *_strchr(const char *str, char c) - locate character in a string

    • `int len_to_cha r(char *str, char c)` - calculate length of string **str** up to char **c**
    • <\ul>
    • Return value


      • `int _strcmp(char *s1, char *s2)` - return an integer less than, equal to, or greater than zero if **s1** is found to be less than, to match, or to be greater than **s2**

      • char *_strchr(const char *str, char c) - a pointer to the matched character or NULL if the character is not found.

      • int len_to_char(char *str, char c) - length of string before char, 0 if char not found

      |
      **[prints.c](/prints.c)**

      • int _putchar(char c)

      • void _strprint(char *str)

      • void print_array(char \**array)

      |

      • Description


        • `_putchar` - writes the character **c** to stdout

        • _strprint - prints a string

        • print_array - Prints an array of strings


      • Return value

        • Return: On success 1. On error, -1 is returned, and errno is set appropriately.

      |
      **[shell.h](/shell.h)** |

      • Description
        • header file for **ScarJo shell**

      • Return value

        • n/a

      |
      **[strings.c](/strings.c)**

      • int _strlen(char *s)
      • int _strncmp(char *s1, char *s2, int n)
      • char *_strcpy(char *dest, char *src)
      • char *_strcat(char *dest, char *src)
      • char *_strdup(char *str)

      |

      • Description


        • `_strlen(const char *s)` returns the length of a string

        • int _strncmp(char *s1, const char *s2, int n) - compares two strings for n amount of chars

        • char *_strcpy(char *dest, const char *src) - copies the string with \0 to the buffer

        • char *_strcat(char *dest, const char *src) - appends the src string to the dest string

        • char *_strdup(char *str) - returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter


      • Return value


        • _strlen(const char *s) - Return: 0 on success

        • int _strncmp(char *s1, const char *s2, int n) - Return: 0 if identical

        • char *_strcpy(char *dest, const char *src) - Return: the pointer to dest

        • char *_strcat(char *dest, const char *src) - pointer to resulting string dest

        • char *_strdup(char *str) - pointer to new string or NULL if str = NULL



      **[built_ins.c](/built_ins.c)**

      • int built_ins(char **input, list_t **env_head)

      • int exit_bi(char **line)

      • int print_env(char **line, list_t **env_head)

      • int set_env(char **line, list_t **env_head)

      • int unset_env(char **line, list_t **env_head)

      |

      • Description
        • `built_ins` - checks if command is a built-in @input: tokenized line from the command line @env_head: pointer to environ list

        • exit_bi - exits shell with given status @line: tokenized line from command line @env_head: pointer to environ list

        • print_env - prints current environ @line: tokenized line from command line @env_head: pointer to environ list

        • set_env - initializes a new environment variable, or modify an existing one @line: tokenized line from command line @env_head: pointer to environ list

        • unset_env - removes environment variable @line: tokenized line from command line @env_head: pointer to environ list


      • Return value

        • `built_ins` - 0 on success, 1 on error, -1 if not found

        • exit_bi - 0 on success, 1 on error

        • print_env - 0 on success, 1 on error

        • set_env - 0 on success, 1 on error

        • unset_env - 0 on success, 1 on error


      |
      **[cmd_line_loop.c](/cmd_line_loop.c)** `int cmd_line_loop(char *buffer, char *line, list_t **env_head)` |
      • Description

        • cmd_line_loop - calls getline until user enters exit or EOF(ctrl^D) @buffer: pointer to buffer to store input @line: pointer to a string of line input @env_head: pointer to head of environ list

      • Return value

        • Return: 0 on success or 1 if _realloc failed

      |
      **[new_env.c](/new_env.c)**

      • `char *_getenv(char *name, list_t **env_head)`

      • `int _setenv(char *name, char *value, list_t **env_head)`

      • `int _unsetenv(char *name, list_t **env_head)`

      • `int delete_node(list_t **head, char *string)`

      • `char *var_str(char *name, char *value)`

      |
      • Description


        • _getenv - gets an environment variable. (without using getenv) @name: environment variable name @env_head: pointer to environ list

        • _setenv - changes or adds the variable to the environment with the value, if variable does not already exist. If it does exist in the environment, then its value is changed to value @name: name of the variable @value: value of the variable @env_head: pointer to environ list

        • _unsetenv - deletes the variable name from the environment. If name does not exist in the environment, then the function succeeds, and the environment is unchanged. @name:name of the environment variable @env_head: pointer to environ list

        • delete_node - deletes the node with string of a list_t @head: pointer to head pointer @string: pointer to the node that should be deleted

        • var_str - creates new variable string @name: name of the variable @value: value of the variable


      • Return value


        • _getenv - pointer to a string with that env variable or NULL if not found

        • _setenv - pointer to head of the list or NULL if it failed

        • _unsetenv - Always 0 on success, or 1 on error

        • delete_node - 0 if it succeeded, 1 if it failed

        • var_str - pointer to new string or NULL if failed


      |
      **[run_command.c](/run_command.c)**

      • `char **path_dirs_array(list_t **env_head)`

      • `char *cmd_in_path(char *str, list_t **env_head)`

      • `int run_command(char **line_tok, list_t **env_head, char *buffer)`

      |
        Description

        • `path_dirs_array` - makes array of pointers for all dirs in the PATH

        • `cmd_in_path` - finds command in the PATH @str: pointer to first string in input line (command) @env_head: pointer to environ list

        • `run_command` - runs the command typed into shell prompt @line_tok: tokenized input line @env_head: pointer to environ list

        Return value

        • `path_dirs_array` - Return: Array of pointers

        • `cmd_in_path` - Return: pointer to absolute path of command or NULL if not found

        • `run_command` - Return: Always 0 on success, 1 on error


      |
      **[environment.c](/environment.c)**

      • `char \*_getenv(const char *name)`

      • int _putenv(char *str)

      • int _setenv(const char *name, const char *value, int overwrite)

      • int _unsetenv(const char *name)

      • `int delete_node(list_t \*\*head, char *string)`

      |

      • Description

        • Contains helper functions that interact with environment variables.
        • `*_getenv(const char *name)` searches environment list to find environment variable **name** and returns a pointer to the corresponding **value** string.
        • `int _putenv(char *str)` changes or adds value of an environment variable
        • `int _setenv(const char *name, const char *value, int overwrite)` change or add an environment variable
        • `int _unsetenv(const char *name)` deletes the environment variable name from the environment
        • `int delete_node(list_t \**head, char *string)` deletes the node with string of of a list_t


      • Return value


        • `*\_getenv(const char *name)` returns a pointer to the value in the environment, or NULL if there is no match
        • `\_putenv()` returns zero on success, or nonzero if an error occurs. In the event of an error, errno is set to indicate the cause.
        • `\_setenv()` returns zero on success, or -1 on error, with **errno** set to indicate the cause of the error
        • `\_unsetenv()` returns zero on success, or -1 on error, with **errno** set to indicate the cause of the error
        • `delete_node()` returns 0 on success, or -1 on error


      ### Project Requirements
      - Allowed editors: `vi`, `vim`, `emacs`
      - All your files will be compiled on Ubuntu 14.04 LTS
      - Your programs and functions will be compiled with `gcc 4.8.4` (`C90`) using the flags `-Wall -Werror -Wextra and -pedantic`

      [![image](https://cloud.githubusercontent.com/assets/23224088/24430835/7d83286c-13cd-11e7-9083-aadb330906b8.png)](https://twitter.com/egsy/status/833533513936703489)
      - All your files should end with a new line
      - A `README.md` file, at the root of the folder of the project is mandatory
      - Your code should use the `Betty` style. It will be checked using [`betty-style.pl`](https://github.com/holbertonschool/Betty/blob/master/betty-style.pl) and [`betty-doc.pl`](https://github.com/holbertonschool/Betty/blob/master/betty-doc.pl)
      - No more than 5 functions per file
      - All your header files should be include guarded
      - Unless specified otherwise, your program must have the exact same output as `sh` as long as the exact same error output.

      ### What students should learn from this project

      ## Project Breakdown
      Task # | Type | Short description
      ---: | --- | --- |
      0 | **Mandatory** | Write a README [link](/README.md)
      Write a man for your shell.[link](/man_1_simple_shell)
      You should have an AUTHORS file at the root of your repository, listing all individuals having contributed content to the repository. Format, see Docker [link](/AUTHORS)
      1 | **Mandatory** | Write a beautiful code that passes the Betty checks
      2 | **Mandatory** | Write a UNIX command line interpreter.

      Your Shell should:


      • Display a prompt and wait for the user to type a command. A command line always ends with a new line.

      • The prompt is displayed again each time a command has been executed.

      • The command lines are simple, no semi-columns, no pipes, no redirections or any other advanced features.

      • The command lines are made only of one word. No arguments will be passed to programs.

      • If an executable cannot be found, print an error message and display the prompt again.

      • Handle errors.

      • You have to handle the "end of file" condition (Ctrl+D). Exit with code 0

      You don't have to:

      • use the PATH

      • implement built-ins

      • handle special characters : ", ', `, \, *, &, #

      • be able to move the cursor

      • handle commands with arguments


      3 | ***Advanced***| Simple shell 0.1 +
    • Write your own getline function

    • Use a buffer to read many chars at once and call the least possible the readsystem call

    • You will need to use static variables

    • You are not allowed to use getline
    • You don't have to:
    • be able to move the cursor

    • 4 | **Mandatory** | Simple shell 0.1 +
    • Handle command lines with arguments

    • 5 | ***Advanced***| Simple shell 0.2 +
    • You are not allowed to use strtok

    • 6 | **Mandatory** | Simple shell 0.2 +
    • Handle the PATH

    • 7 | **Mandatory** | Simple shell 0.3 +
    • Implement the exit buit-in, that exits the shell

    • Usage: exit

    • You don't have to handle any argument to the built-in exit

    • 8 | ***Advanced***| Simple shell 0.4 +
    • handle arguments for the built-in exit

    • Usage: exit status, where status is an integer used to exit the shell

    • 9 | ***Advanced***| Simple shell 0.4 +
    • Handle Ctrl+C: your shell should not quit when the user inputs ^C
    • man 2 signal.
      10 | **Mandatory** | Simple shell 4.0 +
    • Implement the env built-in, that prints the current environment

    • 11 | ***Advanced*** | Implement the setenv and unsetenv builtin commands
      **setenv**
    • Initialize a new environment variable, or modify an existing one

    • Command syntax: setenv VARIABLE VALUE
    • **unsetenv**
    • Remove a environment variable

    • Command syntax: unsetenv VARIABLE

    • 12 | ***Advanced*** | Simple shell 1.0 +

      Implement the builtin command cd:
    • Changes the current directory of the process.

    • Command syntax: cd [DIRECTORY]

    • If no argument is given to cd the command must be interpreted like cd $HOME

    • You have to handle the command cd -

    • You have to update the environment variable PWD when you change directory
    • man chdir, man getcwd
      13 | ***Advanced*** | Simple shell 1.0 +
      Handle the commands separator ;
      14 | ***Advanced*** | Simple shell 1.0 +
      Handle the && and || shell logical operators.
      15 | ***Advanced*** | Simple shell 1.0 +
    • Handle aliases

    • 16 | ***Advanced*** | Simple shell 1.0 +
    • Handle variables replacement

    • Handle the $? variable

    • Handle the $$ variable

    • 17 | ***Advanced*** | Simple shell 1.0 +
    • Handle comments (#)

    • 18 | ***Advanced*** | Simple shell 1.0 +
    • Implement the help buit-in

    • Usage: help [BUILTIN]

    • 19 | ***Advanced*** | Simple shell 1.0 +
    • Implement the history built-in, without any argument

    • The history built-in displays the history list, one command by line, preceded with line numbers (starting at 0)

    • On exit, write the entire history, without line numbers, to a file named .simple_shell_history in the directory $HOME

    • When the shell starts, read the file .simple_shell_history in the directory $HOME if it exists, and set the first line number to the total number of lines in the file modulo 4096

    • 20 | ***Advanced*** | Simple shell 0.1 +
    • Usage: simple_shell [filename]

    • Your shell can take a file as a command line argument

    • The file contains all the commands that your shell should run before exiting

    • The file should contain one command per line

    • In this mode, the shell should not print a prompt and should not read from stdin

    • 21 | **Mandatory** | Write a blog post describing step by step what happens when you type ls -l and hit Enter in a shell. Try to explain every step you know of, going in as much details as you can, give examples and draw diagrams when needed. You should merge your previous knowledge of the shell with the specifics of how it works under the hoods (including syscalls).
      22 | ***Advanced*** | Build a test suite for your shell.
    • The test suite should cover every tasks from 0. to 20.

    • The test suite should cover every regular cases (many different examples) and edge cases possible

    • The entire class will work on the same test suite. Use only one repository (don't forget the README.md file)

    • Start adding tests asap and not just before the deadline in order to help everyone from day 0

    • You can take (or fork) inspiration from this example, but it is not to follow this format/way

    • Adopt a style and be consisten. You can for instance follow this style guide. If you chose a style that already exist, add it to the README in a style section. If you write your own, create a wiki page attached to the project and refer to it in the README style section.

    • If you choose to use this code, make sure to update the style accordingly

    • You should have an AUTHORS file, listing all individuals having contributed content to the repository. Format, see Docker


    • This is a task shared by everyone in the class. Everyone will get the same score for this task. Go team!

      ## Getting Started
      Using ScarJo shell is as easy as 1-2-3! Simply clone this repository onto your local machine, compile with the flags listed below and run!

      ### Installation
      1. Clone
      ```sh
      git clone https://github.com/FreeJules/simple_shell.git
      ```
      2. On your machine, navigate (`cd`) to the newly created directory then compile with the following tags.

      ```sh
      cd simple_shell
      gcc -Wall -Werror -Wextra -pedantic *.c -o hsh
      ```
      3. Run shell
      ```sh
      ./hsh
      ```
      4. Run commands and enjoy!
      5. To exit
      ```sh
      exit
      ```

      ![hsh-compilation1](https://cloud.githubusercontent.com/assets/23224088/25164687/638bb300-2487-11e7-9484-8a4a24fde768.gif)

      ### Screenshots
      Once in ScarJo shell you can interact with it in a number of ways.

      ![hsh-usage](https://cloud.githubusercontent.com/assets/23224088/25169794/33528cca-249d-11e7-8c0a-a95fca7f10b8.gif)

      ## Contributing
      This project is a closed and contributions are not accepted at this time.

      ## Team

      [![Julija Lee](https://avatars0.githubusercontent.com/u/6486822?v=3&s=230)](https://github.com/FreeJules/) | [![Elaine Yeung](https://avatars3.githubusercontent.com/u/23224088?v=3&s=230)](https://github.com/yeungegs)
      :---:|:---:
      [Julija Lee](https://github.com/FreeJules) | [Elaine Yeung](https://github.com/yeungegs)

      ## Resources
      * Readme template from GitHub https://guides.github.com/features/wikis/
      * Docker generate-authors script https://github.com/docker/docker/blob/master/hack/generate-authors.sh