|
|
|
@ -16,8 +16,11 @@ int get_alias(Alias *alias, State *state, char *name) {
@@ -16,8 +16,11 @@ int get_alias(Alias *alias, State *state, char *name) {
|
|
|
|
|
|
|
|
|
|
int add_alias(State* state, char *name, char *substitution) { |
|
|
|
|
Alias alias; |
|
|
|
|
alias.name = name; |
|
|
|
|
alias.substitution = substitution; |
|
|
|
|
strip_char(name, ' '); |
|
|
|
|
alias.name = calloc(strlen(name) + 1, sizeof(char)); |
|
|
|
|
alias.substitution = calloc(strlen(substitution) + 1, sizeof(char)); |
|
|
|
|
strcpy(alias.name, name); |
|
|
|
|
strcpy(alias.substitution, substitution); |
|
|
|
|
Alias throw; |
|
|
|
|
|
|
|
|
|
if (get_alias(&throw, state, name)) { |
|
|
|
@ -54,7 +57,6 @@ int del_alias(State *state, char *name) {
@@ -54,7 +57,6 @@ int del_alias(State *state, char *name) {
|
|
|
|
|
} else { |
|
|
|
|
return ER_FAILURE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int list_aliases(State *state) { |
|
|
|
@ -68,7 +70,8 @@ int list_aliases(State *state) {
@@ -68,7 +70,8 @@ int list_aliases(State *state) {
|
|
|
|
|
int check_builtins(State *state, int num_args, char *args[]) { |
|
|
|
|
char *builtins[NUM_BUILTINS] = { |
|
|
|
|
"cd", |
|
|
|
|
"alias" |
|
|
|
|
"alias", |
|
|
|
|
"unalias" |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if (!strcmp(args[0], "cd")) { |
|
|
|
@ -93,15 +96,16 @@ int check_builtins(State *state, int num_args, char *args[]) {
@@ -93,15 +96,16 @@ int check_builtins(State *state, int num_args, char *args[]) {
|
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
// add new alias
|
|
|
|
|
if (num_args == 2) { |
|
|
|
|
if (!get_alias(&alias, state, args[1])) { |
|
|
|
|
printf("alias %s=%s\n", alias.name, alias.substitution); |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
}
|
|
|
|
|
if (num_args > 1) { |
|
|
|
|
if (num_args >= 2) { |
|
|
|
|
if (num_args == 2) { |
|
|
|
|
if (!get_alias(&alias, state, args[1])) { |
|
|
|
|
printf("alias %s=%s\n", alias.name, alias.substitution); |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
}
|
|
|
|
|
} else { |
|
|
|
|
char *substitution; |
|
|
|
|
char **new_split; |
|
|
|
|
int err = join_stirng(&substitution, num_args - 1, args + 1, " "); |
|
|
|
|
int err = join_string(&substitution, num_args - 1, args + 1, " "); |
|
|
|
|
if (err) { |
|
|
|
|
return err; |
|
|
|
|
} |
|
|
|
@ -120,9 +124,28 @@ int check_builtins(State *state, int num_args, char *args[]) {
@@ -120,9 +124,28 @@ int check_builtins(State *state, int num_args, char *args[]) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!strcmp(args[0], "unalias")) { |
|
|
|
|
Alias alias; |
|
|
|
|
if (num_args == 2) { |
|
|
|
|
if (!del_alias(state, args[1])) { |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ER_FAILURE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int check_aliases(State *state, char **substitution, char *name) { |
|
|
|
|
Alias alias; |
|
|
|
|
for (int i = 0; i < state->num_aliases; i++) { |
|
|
|
|
if (!get_alias(&alias, state, name)) { |
|
|
|
|
*substitution = alias.substitution; |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return ER_FAILURE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int run_command(State *state, int argc, char *argv[]) { |
|
|
|
|
pid_t child; |
|
|
|
@ -130,6 +153,28 @@ int run_command(State *state, int argc, char *argv[]) {
@@ -130,6 +153,28 @@ int run_command(State *state, int argc, char *argv[]) {
|
|
|
|
|
if (argc == 0) { |
|
|
|
|
return ER_FAILURE; |
|
|
|
|
} |
|
|
|
|
char *substitution; |
|
|
|
|
|
|
|
|
|
if (!check_aliases(state, &substitution, argv[0])) { |
|
|
|
|
strip_char(argv[0], ' '); |
|
|
|
|
argv[0] = reallocarray(argv[0], strlen(substitution) + 1, sizeof(char)); |
|
|
|
|
strcpy(argv[0], substitution); |
|
|
|
|
char *joined; |
|
|
|
|
char **new_argv; |
|
|
|
|
int len; |
|
|
|
|
join_string(&joined, argc, argv, " "); |
|
|
|
|
split_string(&new_argv, &len, joined, ' '); |
|
|
|
|
|
|
|
|
|
//run_command(state, len, new_argv);
|
|
|
|
|
execute(stdin, stdout, new_argv, &child); |
|
|
|
|
waitpid(child, NULL, 0); |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
|
|
|
|
|
for (int i = 0 ; i < len; i ++) { |
|
|
|
|
free(new_argv[i]); |
|
|
|
|
} |
|
|
|
|
free(new_argv); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (check_builtins(state, argc, argv)) { |
|
|
|
|
execute(stdin, stdout, argv, &child); |
|
|
|
@ -176,7 +221,7 @@ int main(int argc, char** argv) {
@@ -176,7 +221,7 @@ int main(int argc, char** argv) {
|
|
|
|
|
" |\n", |
|
|
|
|
" _|}\n"}; |
|
|
|
|
|
|
|
|
|
State *state = calloc(1, sizeof(state)); |
|
|
|
|
State *state = calloc(1, sizeof(State)); |
|
|
|
|
state->aliases = calloc(INITIAL_NUM_ALIASES, sizeof(struct Alias)); |
|
|
|
|
state->num_aliases = 0; |
|
|
|
|
|
|
|
|
|