add builtins
This commit is contained in:
parent
79e2b6697b
commit
5ce2096b2c
2
Makefile
2
Makefile
@ -2,7 +2,7 @@ CC=clang
|
|||||||
CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17
|
CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17
|
||||||
LDFLAGS=
|
LDFLAGS=
|
||||||
FILENAME=myshellin
|
FILENAME=myshellin
|
||||||
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c
|
SRC=src/myshellin.c src/loop.c src/console_line.c src/array.c src/builtins.c
|
||||||
OBJ=$(SRC:.c=.o)
|
OBJ=$(SRC:.c=.o)
|
||||||
|
|
||||||
all: myshellin
|
all: myshellin
|
||||||
|
41
src/builtins.c
Normal file
41
src/builtins.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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"
|
||||||
|
|
||||||
|
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) {
|
||||||
|
free_string_array(string_array);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void exit_shell() {
|
||||||
|
|
||||||
|
}
|
24
src/include/builtins.h
Normal file
24
src/include/builtins.h
Normal 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();
|
||||||
|
#endif
|
30
src/loop.c
30
src/loop.c
@ -17,6 +17,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "array.h"
|
||||||
|
#include "builtins.h"
|
||||||
#include "console_line.h"
|
#include "console_line.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,13 +44,15 @@ void loop() {
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
if (line != NULL) {
|
if (line != NULL) {
|
||||||
free(line);
|
free(line);
|
||||||
|
line = NULL;
|
||||||
}
|
}
|
||||||
break;
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
perror("getline() error: ");
|
perror("getline() error: ");
|
||||||
if (line != NULL) {
|
if (line != NULL) {
|
||||||
free(line);
|
free(line);
|
||||||
|
line = NULL;
|
||||||
}
|
}
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -56,11 +60,29 @@ void loop() {
|
|||||||
|
|
||||||
remove_new_line(line);
|
remove_new_line(line);
|
||||||
|
|
||||||
if (strcmp(line, "quit") == 0) {
|
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) {
|
if (line != NULL) {
|
||||||
free(line);
|
free(line);
|
||||||
|
line = NULL;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user