gsplus/src/readline.c

200 lines
3.7 KiB
C
Raw Normal View History

2019-02-11 02:40:22 +00:00
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
2019-02-11 23:11:45 +00:00
#define HISTORY_SIZE 64
/* remove trailing whitespace and terminators */
static void cleanup_buffer(char *buffer, int size) {
while (size > 0) {
unsigned c = buffer[size-1];
if (c == ' ' || c == '\r' || c == '\n') {
--size;
continue;
}
break;
}
buffer[size] = 0;
}
2019-02-11 02:40:22 +00:00
#if defined(USE_LIBEDIT)
#include <histedit.h>
/* BSD libedit support */
EditLine *el = NULL;
History *hist = NULL;
HistEvent ev;
static const char *el_prompt = NULL;
static char *prompt_fn(EditLine *el) {
return el_prompt ? (char *)el_prompt : "";
}
char *x_readline(const char *prompt) {
static char buffer[1024];
2019-02-11 23:11:45 +00:00
int count = 0;
2019-02-11 02:40:22 +00:00
const char *cp;
if (!el) {
hist = history_init();
2019-02-11 23:11:45 +00:00
history(hist, &ev, H_SETSIZE, HISTORY_SIZE);
2019-02-11 02:40:22 +00:00
history(hist, &ev, H_SETUNIQUE, 1);
el = el_init("GS+", stdin, stdout, stderr);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_BIND, "-e", NULL, NULL, NULL);
el_set(el, EL_HIST, history, hist);
el_set(el, EL_PROMPT, prompt_fn);
el_set(el, EL_SIGNAL, 1);
el_source(el, NULL);
}
el_prompt = prompt;
2019-02-11 23:11:45 +00:00
cp = el_gets(el, &count);
2019-02-11 02:40:22 +00:00
el_prompt = NULL;
2019-02-11 23:11:45 +00:00
if (count <= 0) return NULL;
if (count > sizeof(buffer) - 1) return "";
2019-02-11 02:40:22 +00:00
2019-02-11 23:11:45 +00:00
memcpy(buffer, cp, count);
cleanup_buffer(buffer, count);
2019-02-11 02:40:22 +00:00
if (*buffer)
history(hist, &ev, H_ENTER, buffer);
return buffer;
}
void x_readline_end(void) {
if (el) {
el_end(el);
el = NULL;
}
if (hist) {
history_end(hist);
hist = NULL;
}
}
#elif defined(USE_READLINE)
/* GNU Readline support */
#include <readline/readline.h>
#include <readline/history.h>
static int readline_init = 0;
/* suppress tab completion, which defaults to filenames */
static char **rl_acf(const char* text, int start, int end) {
return NULL;
}
char *x_readline(const char *prompt) {
static char buffer[1024];
char *cp;
2019-02-11 23:11:45 +00:00
int count;
2019-02-11 02:40:22 +00:00
if (!readline_init) {
rl_readline_name = "GS+";
rl_attempted_completion_function = rl_acf;
using_history();
2019-02-11 23:11:45 +00:00
stifle_history(HISTORY_SIZE);
2019-02-11 02:40:22 +00:00
readline_init = 1;
}
2019-02-11 03:45:42 +00:00
cp = readline(prompt);
2019-02-11 02:40:22 +00:00
if (!cp) return NULL;
2019-02-11 23:11:45 +00:00
count = strlen(cp);
if (count > sizeof(buffer) - 1) {
2019-02-11 02:40:22 +00:00
free(cp);
return "";
}
2019-02-11 23:11:45 +00:00
memcpy(buffer, cp, count);
cleanup_buffer(buffer, count);
2019-02-11 02:40:22 +00:00
free(cp);
/* append to history, but only if unique from prev. entry */
if (*buffer) {
HIST_ENTRY *h = history_get(history_length);
2019-02-11 02:40:22 +00:00
if (h == NULL || strcmp(buffer, h->line))
add_history(buffer);
}
return buffer;
}
void x_readline_end(void) {
}
2019-02-11 23:11:45 +00:00
#elif defined(WIN32)
#include <windows.h>
static int readline_init = 0;
#ifndef HISTORY_NO_DUP_FLAG
#define HISTORY_NO_DUP_FLAG 0x01
#endif
2019-02-11 23:11:45 +00:00
char *x_readline(const char *prompt) {
static char buffer[1024];
DWORD count = 0;
BOOL ok;
2019-02-18 04:34:10 +00:00
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
2019-02-11 23:11:45 +00:00
if (!readline_init) {
CONSOLE_HISTORY_INFO chi;
DWORD mode;
memset(&chi, 0, sizeof(chi));
chi.cbSize = sizeof(CONSOLE_HISTORY_INFO);
chi.HistoryBufferSize = HISTORY_SIZE;
chi.NumberOfHistoryBuffers = 1; /* ???? */
chi.dwFlags = HISTORY_NO_DUP_FLAG;
SetConsoleHistoryInfo(&chi);
mode = ENABLE_ECHO_INPUT | ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(h, mode);
readline_init = 1;
}
ok = ReadConsole(h, buffer, sizeof(buffer), &count, NULL);
if (!ok) return NULL;
cleanup_buffer(buffer, count);
return buffer;
}
2019-02-11 02:40:22 +00:00
#else
2019-02-11 23:11:45 +00:00
#include <unistd.h>
2019-02-11 02:40:22 +00:00
char *x_readline(const char *prompt) {
static char buffer[1024];
fputs(prompt, stdout);
fflush(stdout);
for(;;) {
int ok = read(STDIN_FILENO, buffer, sizeof(buffer)-1);
if (ok < 0) {
if (ok == EINTR) continue;
return NULL;
}
if (ok == 0) return NULL;
2019-02-11 23:11:45 +00:00
cleanup_buffer(buffer, ok);
2019-02-11 02:40:22 +00:00
return buffer;
}
}
void x_readline_end(void) {
}
#endif