Browse Source

Script mode and loading .chickenrc file from home and XDG_CONFIG_HOME

master
alistair 5 years ago
parent
commit
7ba5309991
  1. 82
      main.c
  2. 4
      main.h
  3. 20
      util.c
  4. 1
      util.h

82
main.c

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

4
main.h

@ -24,6 +24,10 @@ struct Alias { @@ -24,6 +24,10 @@ struct Alias {
struct State {
struct Alias* aliases;
int num_aliases;
FILE* code;
FILE *input;
FILE *output;
bool repl;
};
typedef struct State State;

20
util.c

@ -143,3 +143,23 @@ int split_string(char **sep_string[], int *num, char *string, char delim) { @@ -143,3 +143,23 @@ int split_string(char **sep_string[], int *num, char *string, char delim) {
return ER_FAILURE;
}
int print_chicken(State *state) {
char *chicken[] = {
" _\n",
" (o)\n",
" / |\n",
" / |==========\n",
" \\ CHSH /\n",
" \\______/\n",
" |\n",
" _|}\n"};
for (int i = 0; i < 8; i ++) {
fprintf(state->output, "%s", chicken[i]);
}
return ER_SUCCESS;
}

1
util.h

@ -12,4 +12,5 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim); @@ -12,4 +12,5 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim);
int strip_char(char *string, char del);
int print_chicken(State *state);
#endif

Loading…
Cancel
Save