Compare commits

...

3 Commits

Author SHA1 Message Date
g1n
6eae072b19 Merge branch 'hedy-main' 2021-09-28 08:29:43 +03:00
Hedy Li
7522f3a602
Remove trailing whitespaces 2021-09-28 09:22:52 +08:00
Hedy Li
8f5e931d6a
Put -lreadline into separate variable in Makefile
A separate variable is needed as the libs needs to be specified at the
end of the command.
2021-09-28 09:19:14 +08:00
2 changed files with 21 additions and 20 deletions

View File

@ -1,5 +1,6 @@
CC = gcc CC = gcc
CFLAGS= -O2 -Wall -Wextra -lreadline CFLAGS= -O2 -Wall -Wextra
LIBS= -lreadline
LFLAGS= LFLAGS=
SRCFILES= main.c SRCFILES= main.c
@ -10,5 +11,5 @@ OBJFILES= orsh
all: main all: main
main: main:
$(CC) $(CFLAGS) $(SRCFILES) -o $(OBJFILES) $(CC) $(CFLAGS) $(SRCFILES) -o $(OBJFILES) $(LIBS)

View File

@ -18,7 +18,7 @@ char *homedir; // HOME directory of current user
pid_t shell_pid; // pid of current shell (gets only on start) pid_t shell_pid; // pid of current shell (gets only on start)
pid_t pid; // FIXME: this shouldn't be global i think pid_t pid; // FIXME: this shouldn't be global i think
char **split_line(char *line, char *delim) { char **split_line(char *line, char *delim) {
int bufsize = 64; int bufsize = 64;
position = 0; position = 0;
char **tokens = malloc(bufsize * sizeof(char*)); char **tokens = malloc(bufsize * sizeof(char*));
@ -61,13 +61,13 @@ int execute(char **command, int status) {
wpid = waitpid(pid, &status, WUNTRACED); // FIXME: warning unused variable wpid = waitpid(pid, &status, WUNTRACED); // FIXME: warning unused variable
} while (!WIFEXITED(status) && !WIFSIGNALED(status)); } while (!WIFEXITED(status) && !WIFSIGNALED(status));
} }
if (status >= 256) if (status >= 256)
status = 1; status = 1;
return status; return status;
} }
int parse_command(char **command, int status) { int parse_command(char **command, int status) {
int variable = 0; int variable = 0;
int where_var[position - 1]; int where_var[position - 1];
for (int i = 0; i <= position - 1; i++) { // Loop for checking if line has variable for (int i = 0; i <= position - 1; i++) { // Loop for checking if line has variable
@ -88,7 +88,7 @@ int parse_command(char **command, int status) {
command[i] = status_string; command[i] = status_string;
} else { } else {
command[i] = getenv(command[i]); command[i] = getenv(command[i]);
if (command[i] == NULL) if (command[i] == NULL)
command[i] = ""; command[i] = "";
} }
} }
@ -98,10 +98,10 @@ int parse_command(char **command, int status) {
if (command[0][strlen(command[0]) - 1] == ';'){ // FIXME: parse all word not only first if (command[0][strlen(command[0]) - 1] == ';'){ // FIXME: parse all word not only first
command[0][strlen(command[0])-1] = '\0'; command[0][strlen(command[0])-1] = '\0';
} else if (command[0][0] == ';'){ // Check if command is ; or first char of command is ; } else if (command[0][0] == ';'){ // Check if command is ; or first char of command is ;
command[0][0] = '\0'; command[0][0] = '\0';
} }
if (status >= 256) if (status >= 256)
status = 1; status = 1;
if (!strcmp(command[0], "exit")) { // FIXME: add switch statement (if possible) if (!strcmp(command[0], "exit")) { // FIXME: add switch statement (if possible)
@ -120,17 +120,17 @@ int parse_command(char **command, int status) {
printf("orsh: cd: too many arguments\n"); printf("orsh: cd: too many arguments\n");
} else { } else {
chdir(command[1]); chdir(command[1]);
getcwd(pwd, sizeof(pwd)); getcwd(pwd, sizeof(pwd));
} }
} else if (!strcmp(command[0], "pwd")) { } else if (!strcmp(command[0], "pwd")) {
printf("%s\n", pwd); printf("%s\n", pwd);
} else if (!strcmp(command[0], "!")) { } else if (!strcmp(command[0], "!")) {
if (status != 0) if (status != 0)
status = 0; status = 0;
else if (status == 0) else if (status == 0)
status = 1; status = 1;
} else { } else {
status = execute(command, status); status = execute(command, status);
} }
return status; return status;
} }
@ -163,9 +163,9 @@ void signal_handler(int sig) {
printf("\n"); printf("\n");
print_ps(); print_ps();
} else if (shell_pid == getpid()) { // Do nothing because readline prints the prompt } else if (shell_pid == getpid()) { // Do nothing because readline prints the prompt
} else } else
raise(sig); raise(sig);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@ -176,7 +176,7 @@ int main(int argc, char *argv[]) {
char** args; char** args;
int status = 0; // FIXME int status = 0; // FIXME
int command_status = 0; // FIXME int command_status = 0; // FIXME
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
if (argc >= 2) { if (argc >= 2) {
FILE *script = fopen(argv[1], "r"); FILE *script = fopen(argv[1], "r");
if (script == NULL) { if (script == NULL) {
@ -184,10 +184,10 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
while (fgets(script_line, 100, script) != NULL) { while (fgets(script_line, 100, script) != NULL) {
args = split_line(script_line, " "); args = split_line(script_line, " ");
command_status = parse_command(args, command_status); command_status = parse_command(args, command_status);
} }
} else { } else {
while(1) { while(1) {
line = readline("orsh> "); // FIXME: add prompt line = readline("orsh> "); // FIXME: add prompt
@ -195,11 +195,11 @@ int main(int argc, char *argv[]) {
printf("exit\n"); printf("exit\n");
break; break;
} }
commands = split_line(line, ";"); commands = split_line(line, ";");
int after_parse_position = position; // To know where we in ; list FIXME int after_parse_position = position; // To know where we in ; list FIXME
for (int i = 0; i <= after_parse_position - 1; i++) { for (int i = 0; i <= after_parse_position - 1; i++) {
args = split_line(commands[i], " "); args = split_line(commands[i], " ");
command_status = parse_command(args, command_status); command_status = parse_command(args, command_status);
} }
} }