Browse Source

support for alias builtin

master
alistair 5 years ago
parent
commit
77ad54bb8e
  1. 69
      main.c
  2. 2
      main.h
  3. 34
      util.c
  4. 4
      util.h

69
main.c

@ -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;

2
main.h

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
#define INITIAL_WORD_SIZE 80
#define INITIAL_NUM_ALIASES 50
#define NUM_BUILTINS 2
#define NUM_BUILTINS 3
struct Alias {
char *name;

34
util.c

@ -35,7 +35,7 @@ int readline(FILE* in, char **out) { @@ -35,7 +35,7 @@ int readline(FILE* in, char **out) {
}
/* Join an array of strings together using the delim string as the joiner. */
int join_stirng(char **joined, int arr_len, char *sep_stirng[], char *delim) {
int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) {
int length = 0;
int delim_len = strlen(delim);
@ -68,6 +68,38 @@ int join_stirng(char **joined, int arr_len, char *sep_stirng[], char *delim) { @@ -68,6 +68,38 @@ int join_stirng(char **joined, int arr_len, char *sep_stirng[], char *delim) {
return ER_FAILURE;
}
int strip_char(char *string, char del) {
int len = strlen(string);
bool move = false;
for (int i = 0; i < len; i++) {
if (move && i != len-1) {
string[i] = string[i + 1];
} else if (move) {
string[i] ='\0';
}
if (string[i] == del) {
move = true;
len -= 1;
if (move && i != len-1) {
string[i] = string[i + 1];
} else if (move) {
string[i] ='\0';
}
}
}
if (!move) {
return ER_SUCCESS;
} else {
strip_char(string, del);
}
return ER_FAILURE;
}
/* Take a string and split it into an array of strings, separate by the delim
* character.
*/

4
util.h

@ -8,6 +8,8 @@ int readline(FILE* in, char **out); @@ -8,6 +8,8 @@ int readline(FILE* in, char **out);
int split_string(char **sep_string[], int *num, char *string, char delim);
int join_stirng(char **joined, int arr_len, char *sep_stirng[], char *delim);
int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim);
int strip_char(char *string, char del);
#endif

Loading…
Cancel
Save