builtins #6

Merged
cromer merged 6 commits from builtins into master 2021-06-26 21:12:27 -04:00
8 changed files with 212 additions and 9 deletions

View File

@ -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

View File

@ -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)".

59
src/array.c Normal file
View File

@ -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 <string.h>
#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;
}

42
src/builtins.c Normal file
View File

@ -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 <stdbool.h>
#include <string.h>
#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);
}

View File

@ -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);
}

33
src/include/array.h Normal file
View File

@ -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 <stdlib.h>
#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

24
src/include/builtins.h Normal file
View File

@ -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 <stdbool.h>
#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

View File

@ -17,6 +17,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}
}