Merge pull request 'environ' (#15) from environ into master

Reviewed-on: #15
This commit is contained in:
Chris Cromer 2021-07-17 22:15:23 -04:00
commit 57dbb56c4c
18 changed files with 687 additions and 138 deletions

2
.gitignore vendored
View File

@ -6,3 +6,5 @@ myshellin
*.gz
*.aux
*.out
compile_commands.json
.clangd

View File

@ -2,7 +2,7 @@ CC=clang
CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c11
LDFLAGS=
FILENAME=myshellin
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c src/launch.c src/utils.c
OBJ=$(SRC:.c=.o)
all: myshellin

20
doc/code/launch.txt Normal file
View File

@ -0,0 +1,20 @@
void launch_program(StringArray *args) {
pid_t child = 0;
child = fork();
if (child == 0) {
if (execvp(args->array[0], args->array) == -1) {
fprintf(stderr, "%s: command not found\n", args->array[0]);
free_string_array(args);
exit(EXIT_FAILURE);
}
}
else if (child < 0) {
perror("fork");
}
else {
int child_status;
waitpid(child, &child_status, 0);
}
}

View File

@ -1,3 +1,4 @@
\section{Código}
\input{sections/codigo/ciclo}
\input{sections/codigo/builtins}
\input{sections/codigo/builtins}
\input{sections/codigo/launch}

View File

@ -1,3 +1,4 @@
\newpage
\subsection{Builtins}
Los builtins son comandos que son parte del shell, por lo tanto el sistema operativo no los proporciona. Un ejemplo es el comando\
''exit''. Este comando no existe en el sistema operativo, por lo tanto el shell debe actuar cuando el usuario lo escribe en vez\

View File

@ -0,0 +1,6 @@
\newpage
\subsection{Launch}
Launch(lanzar) es la parte responsable por permutar un programa que está en el sistema operativo o en PATH. Para hacer eso\
primer hacemos un fork donde el hijo permuta la operación y el padre espera que su hijo termina. Luego pasamos un nombre\
de programa a lanzar y sus argumentos al execvp para permutar en el hijo.
\lstinputlisting{code/launch.txt}

View File

@ -17,55 +17,52 @@
#include <string.h>
#include "array.h"
/**
* Create a String Array by initializing its structure.
* @param string_array The String Array to create.
*/
void create_string_array(StringArray *string_array) {
StringArray *create_string_array() {
StringArray *string_array = malloc(sizeof(StringArray));
if (string_array == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
string_array->array = NULL;
string_array->size = 0;
return string_array;
}
/**
* Insert a string into the String Array.
* @param string_array The String Array to insert into.
* @param string The string to insert into the String Array.
*/
void insert_string_array(StringArray *string_array, char *string) {
if (string_array->size == 0) {
string_array->array = malloc(sizeof(char *));
if (string_array->array == NULL) {
string_array->array = malloc(2 * sizeof(char *));
if (string_array->array == NULL) {
perror("malloc");
free_string_array(string_array);
exit(EXIT_FAILURE);
}
}
else {
string_array->array = realloc(string_array->array, (string_array->size + 1) * sizeof(char *));
string_array->array = realloc(string_array->array, (string_array->size + 2) * sizeof(char *));
if (string_array->array == NULL) {
perror("realloc");
free_string_array(string_array);
exit(EXIT_FAILURE);
}
}
string_array->array[string_array->size] = malloc(sizeof(string));
string_array->array[string_array->size] = malloc(strlen(string) * sizeof(char *));
if (string_array->array == NULL) {
perror("malloc");
free_string_array(string_array);
exit(EXIT_FAILURE);
}
strcpy(string_array->array[string_array->size], string);
// A NULL terminated array
string_array->array[string_array->size + 1] = NULL;
string_array->size++;
}
/**
* Delete a string from the String Array.
* @param string_array The String Array to delete from.
* @param index The index in the String Array to delete.
*/
void delete_string_array(StringArray *string_array, int index) {
if (string_array->array != NULL && string_array->size > 0 && string_array->size > index) {
for (int i = index; i < string_array->size - 1; i++) {
for (size_t 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]));
string_array->array[i] = malloc(strlen(string_array->array[i + 1]) * sizeof(char *));
if (string_array->array[i] == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
@ -74,9 +71,10 @@ void delete_string_array(StringArray *string_array, int index) {
}
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->array = realloc(string_array->array, (string_array->size) * sizeof(char *));
if (string_array->array == NULL) {
perror("realloc");
free_string_array(string_array);
exit(EXIT_FAILURE);
}
string_array->size--;
@ -88,27 +86,86 @@ void delete_string_array(StringArray *string_array, int index) {
}
}
/**
* Free the String Array and all of its strings.
* @param string_array The String Array to free.
*/
void free_string_array(StringArray *string_array) {
if (string_array->array == NULL) {
fprintf(stderr, "StringArray is not freeable!\n");
free_string_array(string_array);
exit(EXIT_FAILURE);
for (size_t i = 0; i < string_array->size; i++) {
if (string_array->array[i] != NULL) {
free(string_array->array[i]);
string_array->array[i] = NULL;
}
}
else {
for (int i = 0; i < string_array->size; i++) {
if (string_array->array[i] != NULL) {
free(string_array->array[i]);
string_array->array[i] = NULL;
}
}
if (string_array->array != NULL) {
free(string_array->array);
string_array->array = NULL;
}
string_array->size = 0;
if (string_array->array != NULL) {
free(string_array->array);
string_array->array = NULL;
}
string_array->size = 0;
if (string_array != NULL) {
free(string_array);
string_array = NULL;
}
}
ArrayList *create_array_list() {
ArrayList *array_list = malloc(sizeof(ArrayList));
if (array_list == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
array_list->keys = NULL;
array_list->values = NULL;
array_list->size = 0;
return array_list;
}
void set_array_list(ArrayList *array_list, char *key, char *value) {
if (array_list->keys == NULL) {
array_list->keys = create_string_array();
array_list->values = create_string_array();
}
for (size_t i = 0; i < array_list->size; i++) {
if (strcmp(array_list->keys->array[i], key) == 0) {
array_list->values->array[i] = realloc(array_list->values->array[i], strlen(value) * sizeof(char *));
strcpy(array_list->values->array[i], value);
if (array_list->values->array[i] == NULL) {
perror("realloc");
exit(EXIT_FAILURE);
}
return;
}
}
insert_string_array(array_list->keys, key);
insert_string_array(array_list->values, value);
array_list->size++;
}
char *get_array_list(ArrayList *array_list, char *key) {
if (array_list->keys != NULL) {
for (size_t i = 0; i < array_list->size; i++) {
if (strcmp(array_list->keys->array[i], key) == 0) {
return array_list->values->array[i];
}
}
}
return NULL;
}
void unset_array_list(ArrayList *array_list, char *key) {
if (array_list->keys != NULL) {
for (size_t i = 0; i < array_list->size; i++) {
if (strcmp(array_list->keys->array[i], key) == 0) {
delete_string_array(array_list->keys, i);
delete_string_array(array_list->values, i);
array_list->size--;
return;
}
}
}
}
void free_array_list(ArrayList *array_list) {
if (array_list != NULL) {
free_string_array(array_list->keys);
free_string_array(array_list->values);
free(array_list);
array_list = NULL;
}
}

View File

@ -13,19 +13,15 @@
* 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 <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "array.h"
#include "builtins.h"
#include "console_line.h"
#include "utils.h"
/**
* Check if the command is a builtin or not.
* @param command String with the command name to check.
* @return Returns true if it's a builtin or false otherwise.
*/
bool is_builtin(char *command) {
if (strcmp(command, "exit") == 0) {
return true;
@ -35,13 +31,21 @@ bool is_builtin(char *command) {
return true;
}
if (strcmp(command, "environ") == 0) {
return true;
}
if (strcmp(command, "set") == 0) {
return true;
}
if (strcmp(command, "echo") == 0) {
return true;
}
return false;
}
/**
* Run the builtin command.
* @param args An array of strings containing the arguments to run.
*/
void run_builtin(StringArray *args) {
if (strcmp(args->array[0], "exit") == 0) {
exit_shell(args);
@ -49,40 +53,216 @@ void run_builtin(StringArray *args) {
else if (strcmp(args->array[0], "cd") == 0) {
change_directory(args);
}
else if (strcmp(args->array[0], "environ") == 0) {
print_environ(args);
}
else if (strcmp(args->array[0], "set") == 0) {
set_variable(args);
}
else if (strcmp(args->array[0], "echo") == 0) {
echo(args);
}
else {
fprintf(stderr, "Builtin %s does not exist!\n", args->array[0]);
}
}
/**
* Exit the shell.
* @param args The arguments that were used to call exit. This is used to free the memory before exit.
*/
void exit_shell(StringArray *args) {
free_string_array(args);
exit(EXIT_SUCCESS);
}
/**
* Change the directory to what the user inputs.
* @param args The arguments the user input.
*/
void change_directory(StringArray *args) {
if (args->size > 2) {
fprintf(stderr, "Too many arguments!\n");
return;
}
else if (args->size == 1) {
char *cwd = get_working_directory();
fprintf(stdout, "%s\n", cwd);
if (cwd != NULL) {
free(cwd);
cwd = NULL;
}
print_current_directory();
}
else {
if (chdir(args->array[1]) != 0) {
perror("cd");
char *value = NULL;
if (args->array[1][0] == '$') {
char *variable = remove_variable_symbol(args->array[1]);
char *array_value = get_array_list(variables, variable);
if (array_value == NULL) {
if (variable != NULL) {
free(variable);
variable = NULL;
}
print_current_directory();
return;
}
value = malloc((strlen(array_value) + 1) * sizeof(char *));
if (value == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(value, 0, strlen(array_value) + 1);
strcpy(value, array_value);
if (variable != NULL) {
free(variable);
variable = NULL;
}
}
else {
value = malloc((strlen(args->array[1]) + 1) * sizeof(char *));
if (value == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(value, 0, strlen(args->array[1]) + 1);
strcpy(value, args->array[1]);
}
if (chdir(value) != 0) {
fprintf(stderr, "cd: %s \"%s\"\n", strerror(errno), value);
}
else {
char *cwd = get_working_directory();
set_array_list(variables, "PWD", cwd);
if (cwd != NULL) {
free(cwd);
cwd = NULL;
}
}
if (value != NULL) {
free(value);
value = NULL;
}
}
}
void print_current_directory() {
char *cwd = get_working_directory();
fprintf(stdout, "%s\n", cwd);
if (cwd != NULL) {
free(cwd);
cwd = NULL;
}
}
void print_environ(StringArray *args) {
if (args->size > 1) {
fprintf(stderr, "Too many arguments!\n");
return;
}
for (size_t i = 0; i < variables->size; i++) {
fprintf(stdout, "%s=%s\n", variables->keys->array[i], variables->values->array[i]);
}
}
void set_variable(StringArray *args) {
if (args->size < 4) {
fprintf(stderr, "Too few arguments!\n");
return;
}
if (strcmp(args->array[2], "=") != 0) {
fprintf(stderr, "Invalid arguments!\n");
return;
}
if (args->array[1][0] != '$') {
fprintf(stderr, "Invalid variable argument!\n");
return;
}
char *variable = remove_variable_symbol(args->array[1]);
// Check variable name for invalid characters
for (size_t i = 0; i < strlen(variable); i++) {
if (variable[i] == 0) {
break;
}
if (!((variable[i] >= 48 && variable[i] <= 57) ||
(variable[i] >= 65 && variable[i] <= 90) ||
(variable[i] >= 97 && variable[i] <= 122))) {
fprintf(stderr, "Invalid character \"%d\" in variable name!\n", variable[i]);
return;
}
}
size_t value_length = 1;
for (size_t i = 3; i < args->size; i++) {
if (i != 3) {
value_length++;
}
value_length += strlen(args->array[i]);
}
char *value = malloc(value_length * sizeof(char *));
if (value == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(value, 0, value_length);
for (size_t i = 3; i < args->size; i++) {
if (i != 3) {
strcat(value, " ");
}
strcat(value, args->array[i]);
}
if (strcmp(variable, "PWD") == 0) {
StringArray *chdir_args = create_string_array();
insert_string_array(chdir_args, "cd");
insert_string_array(chdir_args, args->array[3]);
change_directory(chdir_args);
free_string_array(chdir_args);
free(variable);
free(value);
// Return without setting the variable because change directory will set it on success
return;
}
set_array_list(variables, variable, value);
free(variable);
free(value);
}
void echo(StringArray *args) {
StringArray *no_variables = create_string_array();
for (size_t i = 1; i < args->size; i++) {
if (args->array[i][0] == '$') {
char *variable = remove_variable_symbol(args->array[i]);
char *value = get_array_list(variables, variable);
if (value == NULL) {
insert_string_array(no_variables, variable);
}
else {
if (i != 1) {
fprintf(stdout, " ");
}
fprintf(stdout, "%s", value);
}
if (variable != NULL) {
free(variable);
variable = NULL;
}
}
else {
if (i != 1) {
fprintf(stdout, " ");
}
fprintf(stdout, "%s", args->array[i]);
}
}
if (args->size > 0) {
fprintf(stdout, "\n");
}
for (size_t i = 0; i < no_variables->size; i++) {
fprintf(stderr, "The variable %s doesn't exist!\n", no_variables->array[i]);
}
free_string_array(no_variables);
}

View File

@ -21,23 +21,13 @@
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "array.h"
#include "color.h"
#include "console_line.h"
#include "utils.h"
/**
* Remove new line from the end of a string.
* @param line The string to remove the new line from.
*/
void remove_new_line(char* line) {
line[strcspn(line, "\n")] = 0;
}
/**
* Get the logged in user's username.
* @return Returns the logged in user's username.
*/
char *get_username() {
struct passwd *pass;
struct passwd *pass = NULL;
pass = getpwuid(getuid());
if (pass == NULL) {
perror("getpwuid");
@ -46,10 +36,6 @@ char *get_username() {
return pass->pw_name;
}
/**
* Get the hostname of the machine.
* @return Returns the hostname.
*/
char *get_hostname() {
char hostname[HOST_NAME_MAX + 1];
gethostname(hostname, HOST_NAME_MAX + 1);
@ -62,23 +48,6 @@ char *get_hostname() {
return result;
}
/**
* Get the current working directory of the shell.
* @return Returns the current working directory.
*/
char *get_working_directory() {
char *cwd = NULL;
cwd = getcwd(NULL, PATH_MAX);
if (cwd == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
}
return cwd;
}
/**
* Print the console line before the user input.
*/
void print_input_line() {
char *username = get_username();
char *hostname = get_hostname();
@ -94,10 +63,6 @@ void print_input_line() {
}
}
/**
* Get input from the console.
* @return Returns a string input by the user.
*/
char *get_console_input() {
size_t buffer_size = 0;
char *line = NULL;

View File

@ -17,16 +17,126 @@
#ifndef _MYSHELLIN_ARRAY
#define _MYSHELLIN_ARRAY
/**
* This struct houses a dynamically sized string array.
*/
typedef struct {
/**
* The array.
*/
char **array;
/**
* The amount of elements in the array.
*/
size_t size;
} StringArray;
void create_string_array(StringArray *string_array);
/**
* This struct is used to clean up memory on exit.
*/
typedef struct {
/**
* The array of memory to clean.
*/
void **array;
/**
* The amount of elements in the array.
*/
size_t size;
} CleanArray;
/**
* This struct has an array list with key value pairs based on strings.
*/
typedef struct {
/**
* The keys array.
*/
StringArray *keys;
/**
* The values array.
*/
StringArray *values;
/**
* The amount of elements in the array list.
*/
size_t size;
} ArrayList;
/**
* A global clean array to use on exit.
*/
CleanArray clean;
/**
* A global variables array used for environment variables and values.
*/
ArrayList *variables;
/**
* Create a String Array by initializing its structure.
* @return Returns a new String Array.
*/
StringArray *create_string_array();
/**
* Insert a string into the String Array.
* @param string_array The String Array to insert into.
* @param string The string to insert into the String Array.
*/
void insert_string_array(StringArray *string_array, char *string);
/**
* Delete a string from the String Array.
* @param string_array The String Array to delete from.
* @param index The index in the String Array to delete.
*/
void delete_string_array(StringArray *string_array, int index);
/**
* Free the String Array and all of its strings.
* @param string_array The String Array to free.
*/
void free_string_array(StringArray *string_array);
/**
* Create a new Array List.
* @return Returns the newly created Array List.
*/
ArrayList *create_array_list();
/**
* Set a key inside the Array List.
* @param array_list The Array List to work on.
* @param key The key to insert/update.
* @param value The value to insert/update.
*/
void set_array_list(ArrayList *array_list, char *key, char *value);
/**
* Get a value based on a key from an Array List.
* @param array_list The Array List to work on.
* @param key The key to search for.
* @return Returns the value if the key is found in the Array List otherwise it returns NULL.
*/
char *get_array_list(ArrayList *array_list, char *key);
/**
* Remove a key from the Array List.
* @param array_list The Array List to work on.
* @param key The key to remove.
*/
void unset_array_list(ArrayList *array_list, char *key);
/**
* Free all the memory used in the Array List.
* @param array_list The Array List to free.
*/
void free_array_list(ArrayList *array_list);
#endif

View File

@ -18,11 +18,53 @@
#ifndef _MYSHELLIN_BUILTINS
#define _MYSHELLIN_BUILTINS
/**
* Check if the command is a builtin or not.
* @param command String with the command name to check.
* @return Returns true if it's a builtin or false otherwise.
*/
bool is_builtin(char *command);
/**
* Run the builtin command.
* @param args An array of strings containing the arguments to run.
*/
void run_builtin(StringArray *args);
/**
* Exit the shell.
* @param args The arguments that were used to call exit. This is used to free the memory before exit.
*/
void exit_shell(StringArray *args);
/**
* Change the directory to what the user inputs.
* @param args The arguments the user input.
*/
void change_directory(StringArray *args);
/**
* Print the current working directory.
*/
void print_current_directory();
/**
* Print all of the environment variables.
* @param args The arguments the user input.
*/
void print_environ(StringArray *args);
/**
* Set an environment variable.
* @param args The arguments passed to set.
*/
void set_variable(StringArray *args);
/**
* Print a message or variable.
* @param args The arguments passed to echo.
*/
void echo(StringArray *args);
#endif

View File

@ -15,17 +15,28 @@
#ifndef _MYSHELLIN_CONSOLE_LINE
#define _MYSHELLIN_CONSOLE_LINE
#define CONSOLE_BUFFER_SIZE 1024
void remove_new_line(char *line);
/**
* Get the logged in user's username.
* @return Returns the logged in user's username.
*/
char *get_username();
/**
* Get the hostname of the machine.
* @return Returns the hostname.
*/
char *get_hostname();
char *get_working_directory();
/**
* Print the console line before the user input.
*/
void print_input_line();
/**
* Get input from the console.
* @return Returns a string input by the user.
*/
char *get_console_input();
#endif

View File

@ -17,5 +17,11 @@
#ifndef _MYSHELLIN_LAUNCH
#define _MYSHELLIN_LAUNCH
/**
* Launch programs from the OS.
* @param args The arguments to launch.
*/
void launch_program(StringArray *args);
#endif

View File

@ -15,5 +15,21 @@
#ifndef _MYSHELLIN_LOOP
#define _MYSHELLIN_LOOP
/**
* Add memory address to array to be cleaned up later.
* @param data The data to be cleaned up on exit.
*/
void add_to_cleanup(void *data);
/**
* Cleanup memory when exiting.
*/
void exit_cleanup();
/**
* This is the loop that checks for user input and acts on it.
*/
void loop();
#endif

38
src/include/utils.h Normal file
View File

@ -0,0 +1,38 @@
/*
* 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.
*/
#ifndef _MYSHELLIN_UTILS
#define _MYSHELLIN_UTILS
/**
* Remove new line from the end of a string.
* @param line The string to remove the new line from.
*/
void remove_new_line(char *line);
/**
* Get the current working directory of the shell.
* @return Returns the current working directory.
*/
char *get_working_directory();
/**
* Remove the $ symbol from a variable name.
* @param original_variable The original variable name.
* @return Returns the string without the $ symbool.
*/
char *remove_variable_symbol(char *original_variable);
#endif

View File

@ -19,27 +19,41 @@
#include <sys/wait.h>
#include <unistd.h>
#include "array.h"
#include "utils.h"
/**
* Launch programs from the OS.
* @param args The arguments to launch.
*/
void launch_program(StringArray *args) {
pid_t child = 0;
child = fork();
if (child == 0) {
// Copy the array and add a NULL to the end of it
char *argv[args->size + 1];
for (int i = 0; i < args->size; i++) {
argv[i] = args->array[i];
}
argv[args->size] = NULL;
StringArray *new_args = create_string_array();
insert_string_array(new_args, args->array[0]);
for (size_t i = 1; i < args->size; i++) {
if (args->array[i][0] == '$') {
char *variable = remove_variable_symbol(args->array[i]);
execvp(args->array[0], argv);
fprintf(stderr, "%s: command not found\n", args->array[0]);
exit(EXIT_FAILURE);
char *value = get_array_list(variables, variable);
if (value != NULL) {
insert_string_array(new_args, value);
}
if (variable != NULL) {
free(variable);
variable = NULL;
}
}
else {
insert_string_array(new_args, args->array[i]);
}
}
if (execvp(new_args->array[0], new_args->array) == -1) {
fprintf(stderr, "%s: command not found\n", new_args->array[0]);
free_string_array(args);
free_string_array(new_args);
exit(EXIT_FAILURE);
}
}
else if (child < 0) {
perror("fork");

View File

@ -21,23 +21,53 @@
#include "builtins.h"
#include "console_line.h"
#include "launch.h"
#include "utils.h"
void add_to_cleanup(void *data) {
clean.array = realloc(clean.array, (clean.size + 1) * sizeof(void *));
if (clean.array == NULL) {
perror("realloc");
exit(EXIT_FAILURE);
}
clean.array[clean.size++] = data;
}
void exit_cleanup() {
for (size_t i = 0; i < clean.size; i++) {
if (clean.array[i] != NULL) {
free(clean.array[i]);
clean.array[i] = NULL;
}
}
if (clean.array != NULL) {
free(clean.array);
clean.array = NULL;
}
free_array_list(variables);
}
/**
* This is the loop that checks for user input and acts on it.
*/
void loop() {
clean.size = 0;
variables = create_array_list();
atexit(exit_cleanup);
char *cwd = get_working_directory();
set_array_list(variables, "PWD", cwd);
if (cwd != NULL) {
free(cwd);
cwd = NULL;
}
while (1) {
print_input_line();
char *line = get_console_input();
StringArray args;
create_string_array(&args);
StringArray *args = create_string_array();
char *saveptr = NULL;
char *token = strtok_r(line, " ", &saveptr);
while (token) {
insert_string_array(&args, token);
insert_string_array(args, token);
token = strtok_r(NULL, " ", &saveptr);
}
if (line != NULL) {
@ -46,17 +76,18 @@ void loop() {
}
// The user didn't type anything so restart the loop
if (args.size == 0) {
if (args->size == 0) {
free_string_array(args);
continue;
}
if (is_builtin(args.array[0])) {
run_builtin(&args);
if (is_builtin(args->array[0])) {
run_builtin(args);
}
else {
launch_program(&args);
launch_program(args);
}
free_string_array(&args);
free_string_array(args);
}
}

49
src/utils.c Normal file
View File

@ -0,0 +1,49 @@
/*
* 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.
*/
#define _GNU_SOURCE
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void remove_new_line(char* line) {
line[strcspn(line, "\n")] = 0;
}
char *get_working_directory() {
char *cwd = malloc(PATH_MAX * sizeof(char *));
getcwd(cwd, PATH_MAX);
if (cwd == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
}
return cwd;
}
char *remove_variable_symbol(char *original_variable) {
char *variable = malloc((strlen(original_variable)) * sizeof(char *));
if (variable == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memset(variable, 0, strlen(original_variable));
for (size_t i = 0; i < strlen(original_variable); i++) {
variable[i] = original_variable[i + 1];
}
return variable;
}