diff --git a/Makefile b/Makefile index 1350e3c..0fb0bab 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ CC=clang -CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17 +CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c11 LDFLAGS= FILENAME=myshellin -SRC=src/myshellin.c src/loop.c src/console_line.c +SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c OBJ=$(SRC:.c=.o) all: myshellin diff --git a/README.md b/README.md index 8b8290e..7ab650b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,26 @@ # myshellin +myshellin es un proyecto semestral del ramo Sistemas Operativos. El propósito de este proyecto es crear un shell básico. +## Autores +- Christopher Cromer +- Raúl Raúl Hernandez + +## Requisitos software +- glibc +- make +- clang + +## Requisitos informe +- latex +- pdflatex + +## Compilar + +### Software +make myshellin + +### Informe +make informe + +## Licencia +El proyecto es bajo la licencia "[The 3-Clause BSD License](LICENSE)". diff --git a/src/array.c b/src/array.c new file mode 100644 index 0000000..c09ab80 --- /dev/null +++ b/src/array.c @@ -0,0 +1,59 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "array.h" + +void create_string_array(StringArray *string_array) { + string_array->array = NULL; + string_array->size = 0; +} + +void insert_string_array(StringArray *string_array, char *string) { + if (string_array->size == 0) { + string_array->array = malloc(sizeof(char *)); + } + else { + string_array->array = realloc(string_array->array, (string_array->size + 1) * sizeof(char *)); + } + string_array->array[string_array->size] = malloc(sizeof(string)); + strcpy(string_array->array[string_array->size], string); + string_array->size++; +} + +void delete_string_array(StringArray *string_array, int index) { + if (string_array->size > 0 && string_array->size > index) { + for (int i = index; i < string_array->size - 1; i++) { + free(string_array->array[i]); + string_array->array[i] = NULL; + string_array->array[i] = malloc(sizeof(string_array->array[i + 1])); + strcpy(string_array->array[i], string_array->array[i + 1]); + } + free(string_array->array[string_array->size - 1]); + string_array->array[string_array->size - 1] = NULL; + string_array->array = realloc(string_array->array, (string_array->size - 1) * sizeof(char *)); + string_array->size--; + } +} + +void free_string_array(StringArray *string_array) { + for (int i = 0; i < string_array->size; i++) { + free(string_array->array[i]); + string_array->array[i] = NULL; + } + free(string_array->array); + string_array->array = NULL; + string_array->size = 0; +} diff --git a/src/builtins.c b/src/builtins.c new file mode 100644 index 0000000..4648ad9 --- /dev/null +++ b/src/builtins.c @@ -0,0 +1,42 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include "array.h" +#include "builtins.h" + +bool is_builtin(char *command) { + if (strcmp(command, "exit") == 0) { + return true; + } + + if (strcmp(command, "cd") == 0) { + return true; + } + + return false; +} + +void run_builtin(StringArray *string_array) { + if (strcmp(string_array->array[0], "exit") == 0) { + exit_shell(string_array); + } +} + +void exit_shell(StringArray *string_array) { + free_string_array(string_array); + exit(EXIT_SUCCESS); +} diff --git a/src/console_line.c b/src/console_line.c index dab2281..7ea5e34 100644 --- a/src/console_line.c +++ b/src/console_line.c @@ -55,6 +55,5 @@ void print_input_line() { char *name = get_user(); char *cwd = get_working_directory(); printf(BRIGHT_CYAN "%s" MAGENTA "@" RED "localhost" MAGENTA ":" BLUE "%s" MAGENTA "$ " RESET, name, cwd); - free(name); free(cwd); } diff --git a/src/include/array.h b/src/include/array.h new file mode 100644 index 0000000..fef9b3a --- /dev/null +++ b/src/include/array.h @@ -0,0 +1,33 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#ifndef _MYSHELLIN_ARRAY +#define _MYSHELLIN_ARRAY +typedef struct { + char **array; + size_t size; +} StringArray; + +void create_string_array(StringArray *string_array); + +void insert_string_array(StringArray *string_array, char *string); + +void delete_string_array(StringArray *string_array, int index); + +void free_string_array(StringArray *string_array); + +#endif diff --git a/src/include/builtins.h b/src/include/builtins.h new file mode 100644 index 0000000..ccaebbf --- /dev/null +++ b/src/include/builtins.h @@ -0,0 +1,24 @@ +/* + * Copyright 2021 Christopher Cromer + * Copyright 2021 Raúl Hernandez + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "array.h" + +#ifndef _MYSHELLIN_BUILTINS +#define _MYSHELLIN_BUILTINS +bool is_builtin(char *command); +void run_builtin(StringArray *string_array); +void exit_shell(StringArray *string_array); +#endif diff --git a/src/loop.c b/src/loop.c index cd41cfe..cfaf81f 100644 --- a/src/loop.c +++ b/src/loop.c @@ -17,6 +17,8 @@ #include #include #include +#include "array.h" +#include "builtins.h" #include "console_line.h" /** @@ -42,13 +44,15 @@ void loop() { printf("\n"); if (line != NULL) { free(line); + line = NULL; } - break; + exit(EXIT_SUCCESS); } else { perror("getline() error: "); if (line != NULL) { free(line); + line = NULL; } exit(EXIT_FAILURE); } @@ -56,11 +60,29 @@ void loop() { remove_new_line(line); - if (strcmp(line, "quit") == 0) { - if (line != NULL) { - free(line); - } - break; + StringArray string_array; + create_string_array(&string_array); + + char *saveptr = NULL; + char *token = strtok_r(line, " ", &saveptr); + while (token) { + insert_string_array(&string_array, token); + token = strtok_r(NULL, " ", &saveptr); } + if (line != NULL) { + free(line); + line = NULL; + } + + // The user didn't type anything so restart the loop + if (string_array.size == 0) { + continue; + } + + if (is_builtin(string_array.array[0])) { + run_builtin(&string_array); + } + + free_string_array(&string_array); } }