|
|
|
@ -3,7 +3,6 @@
@@ -3,7 +3,6 @@
|
|
|
|
|
#include "main.h" |
|
|
|
|
#include "util.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int get_alias(Alias *alias, State *state, char *name) { |
|
|
|
|
for (int i = 0; i < state->num_aliases; i++) { |
|
|
|
|
if (!strcmp(name, state->aliases[i].name)) { |
|
|
|
@ -62,7 +61,7 @@ int del_alias(State *state, char *name) {
@@ -62,7 +61,7 @@ int del_alias(State *state, char *name) {
|
|
|
|
|
int list_aliases(State *state) { |
|
|
|
|
for (int i = 0; i < state->num_aliases; i++) { |
|
|
|
|
Alias alias = state->aliases[i]; |
|
|
|
|
printf("alias %s=%s\n", alias.name, alias.substitution); |
|
|
|
|
fprintf(state->output, "alias %s=%s\n", alias.name, alias.substitution); |
|
|
|
|
} |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
@ -99,10 +98,11 @@ int check_builtins(State *state, int num_args, char *args[]) {
@@ -99,10 +98,11 @@ int check_builtins(State *state, int num_args, char *args[]) {
|
|
|
|
|
if (num_args >= 2) { |
|
|
|
|
if (num_args == 2) { |
|
|
|
|
if (!get_alias(&alias, state, args[1])) { |
|
|
|
|
printf("alias %s=%s\n", alias.name, alias.substitution); |
|
|
|
|
fprintf(state->output, "alias %s=%s\n", alias.name, alias.substitution); |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
}
|
|
|
|
|
} else { |
|
|
|
|
}
|
|
|
|
|
if (num_args >= 2) { |
|
|
|
|
char *substitution; |
|
|
|
|
char **new_split; |
|
|
|
|
int err = join_string(&substitution, num_args - 1, args + 1, " "); |
|
|
|
@ -184,12 +184,15 @@ int run_command(State *state, int argc, char *argv[]) {
@@ -184,12 +184,15 @@ int run_command(State *state, int argc, char *argv[]) {
|
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int repl(State* state) { |
|
|
|
|
int repl(State* state, FILE *input) { |
|
|
|
|
while (true) { |
|
|
|
|
char *line; |
|
|
|
|
printf("$ "); |
|
|
|
|
|
|
|
|
|
if (readline(stdin, &line)) { |
|
|
|
|
if (state->repl) { |
|
|
|
|
fprintf(state->output, "$ "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (readline(input, &line)) { |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -209,27 +212,66 @@ int repl(State* state) {
@@ -209,27 +212,66 @@ int repl(State* state) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
int startup(State *state, int argc, char **argv) { |
|
|
|
|
state->output = stdout; |
|
|
|
|
|
|
|
|
|
char *config_home; |
|
|
|
|
char *config = calloc(500, sizeof(char)); |
|
|
|
|
if ((config_home = getenv("XDG_CONFIG_HOME"))) { |
|
|
|
|
strcpy(config, config_home); |
|
|
|
|
int len = strlen(config_home); |
|
|
|
|
strcpy(config + len, "/chicken/chickenrc"); |
|
|
|
|
} else { |
|
|
|
|
config_home = getenv("HOME"); |
|
|
|
|
strcpy(config, config_home); |
|
|
|
|
int len = strlen(config_home); |
|
|
|
|
strcpy(config + len, "/.chickenrc"); |
|
|
|
|
} |
|
|
|
|
printf("%s\n", config); |
|
|
|
|
|
|
|
|
|
int err; |
|
|
|
|
FILE* rc = fopen(config, "r"); |
|
|
|
|
if (rc) { |
|
|
|
|
state->repl = false; |
|
|
|
|
state->input = rc; |
|
|
|
|
if ((err = repl(state, rc))) { |
|
|
|
|
perror("Config file error"); |
|
|
|
|
return err; |
|
|
|
|
} |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (argc == 2) { |
|
|
|
|
FILE *script_text = fopen(argv[1], "r"); |
|
|
|
|
if (script_text) { |
|
|
|
|
state->code = script_text; |
|
|
|
|
} else { |
|
|
|
|
perror("Failed to load script"); |
|
|
|
|
return ER_FAILURE; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char *chicken[] = { |
|
|
|
|
" _\n", |
|
|
|
|
" (o)\n", |
|
|
|
|
" / |\n", |
|
|
|
|
" / |==========\n", |
|
|
|
|
" \\ CHSH /\n", |
|
|
|
|
" \\______/\n", |
|
|
|
|
" |\n", |
|
|
|
|
" _|}\n"}; |
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
|
|
|
|
|
State *state = calloc(1, sizeof(State)); |
|
|
|
|
state->aliases = calloc(INITIAL_NUM_ALIASES, sizeof(struct Alias)); |
|
|
|
|
state->num_aliases = 0; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i ++) { |
|
|
|
|
printf("%s", chicken[i]); |
|
|
|
|
int err = startup(state, argc, argv); |
|
|
|
|
if (err) { |
|
|
|
|
return err; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
repl(state); |
|
|
|
|
if (state->code) { |
|
|
|
|
state->repl = false; |
|
|
|
|
repl(state, state->code); |
|
|
|
|
} else { |
|
|
|
|
state->repl = true; |
|
|
|
|
print_chicken(state); |
|
|
|
|
repl(state, stdin); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ER_SUCCESS; |
|
|
|
|
} |
|
|
|
|