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
CFLAGS= -O2 -Wall -Wextra -lreadline
CFLAGS= -O2 -Wall -Wextra
LIBS= -lreadline
LFLAGS=
SRCFILES= main.c
@ -10,5 +11,5 @@ OBJFILES= orsh
all: 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 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;
position = 0;
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
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
if (status >= 256)
if (status >= 256)
status = 1;
return status;
}
int parse_command(char **command, int status) {
int parse_command(char **command, int status) {
int variable = 0;
int where_var[position - 1];
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;
} else {
command[i] = getenv(command[i]);
if (command[i] == NULL)
if (command[i] == NULL)
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
command[0][strlen(command[0])-1] = '\0';
} 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;
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");
} else {
chdir(command[1]);
getcwd(pwd, sizeof(pwd));
getcwd(pwd, sizeof(pwd));
}
} else if (!strcmp(command[0], "pwd")) {
printf("%s\n", pwd);
printf("%s\n", pwd);
} else if (!strcmp(command[0], "!")) {
if (status != 0)
status = 0;
else if (status == 0)
status = 1;
} else {
status = execute(command, status);
status = execute(command, status);
}
return status;
}
@ -163,9 +163,9 @@ void signal_handler(int sig) {
printf("\n");
print_ps();
} else if (shell_pid == getpid()) { // Do nothing because readline prints the prompt
} else
} else
raise(sig);
}
int main(int argc, char *argv[]) {
@ -176,7 +176,7 @@ int main(int argc, char *argv[]) {
char** args;
int status = 0; // FIXME
int command_status = 0; // FIXME
signal(SIGINT, signal_handler);
signal(SIGINT, signal_handler);
if (argc >= 2) {
FILE *script = fopen(argv[1], "r");
if (script == NULL) {
@ -184,10 +184,10 @@ int main(int argc, char *argv[]) {
return 1;
}
while (fgets(script_line, 100, script) != NULL) {
args = split_line(script_line, " ");
args = split_line(script_line, " ");
command_status = parse_command(args, command_status);
}
} else {
while(1) {
line = readline("orsh> "); // FIXME: add prompt
@ -195,11 +195,11 @@ int main(int argc, char *argv[]) {
printf("exit\n");
break;
}
commands = split_line(line, ";");
commands = split_line(line, ";");
int after_parse_position = position; // To know where we in ; list FIXME
for (int i = 0; i <= after_parse_position - 1; i++) {
args = split_line(commands[i], " ");
command_status = parse_command(args, command_status);
}
}