Browse Source

Lines in interactive mode use libreadline

gnu libreadline is used to fetch lines now, and also handles the prompt.

It has been a very long time since the last commit and I have tweaked many
other files but I dont know what I changed, everything still works tho so oh
well.
master
alistair 4 years ago
parent
commit
34f287420a
  1. 2
      Makefile
  2. 29
      main.c
  3. 7
      main.h
  4. 7
      run.c
  5. 6
      util.c
  6. 2
      util.h

2
Makefile

@ -1,5 +1,5 @@
GCC=gcc -g GCC=gcc -g -lreadline
.PHONY default: build .PHONY default: build

29
main.c

@ -5,6 +5,9 @@
#include <dirent.h> #include <dirent.h>
#include <readline/readline.h>
#include <readline/history.h>
int get_alias(Alias *alias, State *state, char *name); int get_alias(Alias *alias, State *state, char *name);
int add_alias(State* state, char *name, char *substitution); int add_alias(State* state, char *name, char *substitution);
int del_alias(State *state, char *name); int del_alias(State *state, char *name);
@ -113,7 +116,6 @@ int del_array_element(GrowingArray* array, char* key) {
} else { } else {
array->inner[i] = array->inner[i+1]; array->inner[i] = array->inner[i+1];
} }
} }
} }
} }
@ -203,7 +205,6 @@ void shutdown(State *state) {
} }
int check_pipes(State* state, int num_args, char* args[]) { int check_pipes(State* state, int num_args, char* args[]) {
int left = 0; int left = 0;
int num_commands = 0; int num_commands = 0;
int space = 10; int space = 10;
@ -256,13 +257,8 @@ int check_pipes(State* state, int num_args, char* args[]) {
for (int i = 0; i < num_commands; i++) { for (int i = 0; i < num_commands; i++) {
execute(in, out, pipes[i], &child); execute(in, out, pipes[i], &child);
if (i != num_commands - 1) { if (i != num_commands - 1) {
if (in == pipein[0]) { in = pipeout[0];
in = pipeout[0]; out = pipein[1];
out = pipein[1];
} else {
in = pipein[0];
out = pipeout[1];
}
} else { } else {
out = 1; out = 1;
in = out - 1; in = out - 1;
@ -324,7 +320,6 @@ int check_builtins(State *state, int num_args, char *args[]) {
} }
} }
return ER_FAILURE; return ER_FAILURE;
} }
if (!strcmp(args[0], "alias")) { if (!strcmp(args[0], "alias")) {
@ -459,11 +454,12 @@ int repl(State* state, FILE *input) {
char *line; char *line;
if (state->interactive) { if (state->interactive) {
fprintf(state->output, "%s", state->ps1); line = readline(state->ps1);
} add_history(line);
} else {
if (readline(input, &line)) { if (get_line(input, &line)) {
return ER_SUCCESS; return ER_SUCCESS;
}
} }
char **sep_string; char **sep_string;
@ -472,6 +468,7 @@ int repl(State* state, FILE *input) {
split_string(&sep_string, &num_words, line, ' '); split_string(&sep_string, &num_words, line, ' ');
int err = check_pipes(state, num_words, sep_string); int err = check_pipes(state, num_words, sep_string);
if (err == ER_NOT_EXISTS) { if (err == ER_NOT_EXISTS) {
expand_command(state, num_words, sep_string); expand_command(state, num_words, sep_string);
} }
@ -517,8 +514,8 @@ int startup(State *state, int argc, char **argv) {
perror("Config file error"); perror("Config file error");
return err; return err;
} }
fclose(rc);
} }
fclose(rc);
if (argc == 2) { if (argc == 2) {
FILE *script_text = fopen(argv[1], "r"); FILE *script_text = fopen(argv[1], "r");

7
main.h

@ -6,7 +6,6 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#ifndef MAINH #ifndef MAINH
#define MAINH #define MAINH
@ -46,8 +45,10 @@ struct State {
struct GrowingArray variables; struct GrowingArray variables;
int num_aliases; int num_aliases;
FILE* code; FILE* code;
FILE *input;
FILE *output; FILE* input;
FILE* output;
bool interactive; bool interactive;
char *ps1; char *ps1;
}; };

7
run.c

@ -1,5 +1,12 @@
#include "run.h" #include "run.h"
struct Job {
bool done;
int ret_val;
char **argv;
int argc;
};
/* Fork and exec a command given in args taking input from in and sending /* Fork and exec a command given in args taking input from in and sending
* output to out. The arguments array must end in a NULL. * output to out. The arguments array must end in a NULL.
* *

6
util.c

@ -10,7 +10,7 @@ void *dualloc(void *thing, size_t size) {
} }
/* Read a line into a newly created buffer */ /* Read a line into a newly created buffer */
int readline(FILE* in, char **out) { int get_line(FILE* in, char **out) {
int size_of_buffer = INITIAL_LINE_BUFFER; int size_of_buffer = INITIAL_LINE_BUFFER;
char *buffer = calloc(size_of_buffer, sizeof(char)); char *buffer = calloc(size_of_buffer, sizeof(char));
char character; char character;
@ -159,14 +159,14 @@ int split_string(char **sep_string[], int *num, char *string, char delim) {
int print_chicken(State *state) { int print_chicken(State *state) {
char *chicken[] = { char *chicken[] = {
" _\n", " _\n",
" (o)\n", " (o)\n",
" / |\n", " / |\n",
" / |==========\n", " / |==========\n",
" \\ CHSH /\n", " \\ CHSH /\n",
" \\______/\n", " \\______/\n",
" |\n", " |\n",
" _|}\n"}; " _|\n"};
for (int i = 0; i < 8; i ++) { for (int i = 0; i < 8; i ++) {

2
util.h

@ -6,7 +6,7 @@
void *dualloc(void *thing, size_t size); void *dualloc(void *thing, size_t size);
int readline(FILE* in, char **out); int get_line(FILE* in, char **out);
int split_string(char **sep_string[], int *num, char *string, char delim); int split_string(char **sep_string[], int *num, char *string, char delim);

Loading…
Cancel
Save