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 @@ @@ -1,5 +1,5 @@
GCC=gcc -g
GCC=gcc -g -lreadline
.PHONY default: build

29
main.c

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

7
main.h

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

7
run.c

@ -1,5 +1,12 @@ @@ -1,5 +1,12 @@
#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
* 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) { @@ -10,7 +10,7 @@ void *dualloc(void *thing, size_t size) {
}
/* 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;
char *buffer = calloc(size_of_buffer, sizeof(char));
char character;
@ -159,14 +159,14 @@ int split_string(char **sep_string[], int *num, char *string, char delim) { @@ -159,14 +159,14 @@ int split_string(char **sep_string[], int *num, char *string, char delim) {
int print_chicken(State *state) {
char *chicken[] = {
" _\n",
" _\n",
" (o)\n",
" / |\n",
" / |==========\n",
" \\ CHSH /\n",
" \\______/\n",
" |\n",
" _|}\n"};
" _|\n"};
for (int i = 0; i < 8; i ++) {

2
util.h

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
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);

Loading…
Cancel
Save