2018-03-14 03:39:49 +00:00
|
|
|
/*
|
2020-01-12 16:50:37 +00:00
|
|
|
* Copyright (c) 2020 Calvin Rose
|
2018-03-14 03:39:49 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-31 01:24:40 +00:00
|
|
|
#if !defined(_POSIX_C_SOURCE)
|
|
|
|
#define _POSIX_C_SOURCE 200112L
|
|
|
|
#endif
|
|
|
|
|
2020-01-29 05:38:52 +00:00
|
|
|
#include <janet.h>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <shlwapi.h>
|
|
|
|
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
|
|
|
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
|
|
|
|
#endif
|
2019-10-30 00:18:44 +00:00
|
|
|
#endif
|
2018-03-14 03:39:49 +00:00
|
|
|
|
2020-01-29 05:38:52 +00:00
|
|
|
void janet_line_init();
|
|
|
|
void janet_line_deinit();
|
|
|
|
|
|
|
|
void janet_line_get(const char *p, JanetBuffer *buffer);
|
|
|
|
Janet janet_line_getter(int32_t argc, Janet *argv);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Line Editing
|
|
|
|
*/
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static JANET_THREAD_LOCAL JanetTable *gbl_complete_env;
|
|
|
|
|
2018-03-14 03:39:49 +00:00
|
|
|
/* Common */
|
2019-01-06 01:09:03 +00:00
|
|
|
Janet janet_line_getter(int32_t argc, Janet *argv) {
|
2020-01-18 05:03:50 +00:00
|
|
|
janet_arity(argc, 0, 3);
|
2019-01-31 17:34:22 +00:00
|
|
|
const char *str = (argc >= 1) ? (const char *) janet_getstring(argv, 0) : "";
|
|
|
|
JanetBuffer *buf = (argc >= 2) ? janet_getbuffer(argv, 1) : janet_buffer(10);
|
2020-01-18 06:17:08 +00:00
|
|
|
gbl_complete_env = (argc >= 3) ? janet_gettable(argv, 2) : NULL;
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_line_get(str, buf);
|
2020-01-18 05:03:50 +00:00
|
|
|
gbl_complete_env = NULL;
|
2019-01-31 17:34:22 +00:00
|
|
|
return janet_wrap_buffer(buf);
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static void simpleline(JanetBuffer *buffer) {
|
2019-06-08 19:27:13 +00:00
|
|
|
FILE *in = janet_dynfile("in", stdin);
|
2018-03-14 03:39:49 +00:00
|
|
|
buffer->count = 0;
|
2018-11-18 19:17:50 +00:00
|
|
|
int c;
|
2018-03-14 03:39:49 +00:00
|
|
|
for (;;) {
|
2019-06-08 19:27:13 +00:00
|
|
|
c = fgetc(in);
|
|
|
|
if (feof(in) || c < 0) {
|
2018-03-14 14:49:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, (uint8_t) c);
|
2018-03-14 03:39:49 +00:00
|
|
|
if (c == '\n') break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Windows */
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-03-14 03:39:49 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_line_init() {
|
2018-03-14 03:39:49 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_line_deinit() {
|
2018-03-14 03:39:49 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2019-01-31 17:34:22 +00:00
|
|
|
void janet_line_get(const char *p, JanetBuffer *buffer) {
|
2020-01-18 20:32:25 +00:00
|
|
|
FILE *out = janet_dynfile("err", stderr);
|
2019-06-08 19:27:13 +00:00
|
|
|
fputs(p, out);
|
2019-06-18 04:04:29 +00:00
|
|
|
fflush(out);
|
2018-03-14 03:39:49 +00:00
|
|
|
simpleline(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Posix */
|
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
https://github.com/antirez/linenoise/blob/master/linenoise.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
2018-03-18 13:13:21 +00:00
|
|
|
#include <string.h>
|
2018-06-24 17:17:56 +00:00
|
|
|
#include <signal.h>
|
2018-03-14 03:39:49 +00:00
|
|
|
|
|
|
|
/* static state */
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_LINE_MAX 1024
|
2020-01-18 06:17:08 +00:00
|
|
|
#define JANET_MATCH_MAX 256
|
2018-09-06 02:18:42 +00:00
|
|
|
#define JANET_HISTORY_MAX 100
|
2019-11-27 05:13:53 +00:00
|
|
|
static JANET_THREAD_LOCAL int gbl_israwmode = 0;
|
|
|
|
static JANET_THREAD_LOCAL const char *gbl_prompt = "> ";
|
|
|
|
static JANET_THREAD_LOCAL int gbl_plen = 2;
|
|
|
|
static JANET_THREAD_LOCAL char gbl_buf[JANET_LINE_MAX];
|
|
|
|
static JANET_THREAD_LOCAL int gbl_len = 0;
|
|
|
|
static JANET_THREAD_LOCAL int gbl_pos = 0;
|
|
|
|
static JANET_THREAD_LOCAL int gbl_cols = 80;
|
|
|
|
static JANET_THREAD_LOCAL char *gbl_history[JANET_HISTORY_MAX];
|
|
|
|
static JANET_THREAD_LOCAL int gbl_history_count = 0;
|
|
|
|
static JANET_THREAD_LOCAL int gbl_historyi = 0;
|
|
|
|
static JANET_THREAD_LOCAL int gbl_sigint_flag = 0;
|
|
|
|
static JANET_THREAD_LOCAL struct termios gbl_termios_start;
|
2020-01-18 06:17:08 +00:00
|
|
|
static JANET_THREAD_LOCAL JanetByteView gbl_matches[JANET_MATCH_MAX];
|
|
|
|
static JANET_THREAD_LOCAL int gbl_match_count = 0;
|
2020-01-19 16:37:05 +00:00
|
|
|
static JANET_THREAD_LOCAL int gbl_lines_below = 0;
|
2018-03-14 03:39:49 +00:00
|
|
|
|
2018-03-14 14:49:26 +00:00
|
|
|
/* Unsupported terminal list from linenoise */
|
|
|
|
static const char *badterms[] = {
|
|
|
|
"cons25",
|
|
|
|
"dumb",
|
|
|
|
"emacs",
|
|
|
|
NULL
|
2018-03-14 03:39:49 +00:00
|
|
|
};
|
|
|
|
|
2018-03-18 13:13:21 +00:00
|
|
|
static char *sdup(const char *s) {
|
|
|
|
size_t len = strlen(s) + 1;
|
|
|
|
char *mem = malloc(len);
|
|
|
|
if (!mem) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return memcpy(mem, s, len);
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:39:49 +00:00
|
|
|
/* Ansi terminal raw mode */
|
2020-01-18 05:03:50 +00:00
|
|
|
static int rawmode(void) {
|
2018-03-14 14:49:26 +00:00
|
|
|
struct termios t;
|
2018-03-14 03:39:49 +00:00
|
|
|
if (!isatty(STDIN_FILENO)) goto fatal;
|
2019-10-11 03:59:43 +00:00
|
|
|
if (tcgetattr(STDIN_FILENO, &gbl_termios_start) == -1) goto fatal;
|
|
|
|
t = gbl_termios_start;
|
2018-03-14 14:49:26 +00:00
|
|
|
t.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
|
|
|
t.c_cflag |= (CS8);
|
|
|
|
t.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
|
|
|
t.c_cc[VMIN] = 1;
|
|
|
|
t.c_cc[VTIME] = 0;
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t) < 0) goto fatal;
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_israwmode = 1;
|
2018-03-14 03:39:49 +00:00
|
|
|
return 0;
|
|
|
|
fatal:
|
|
|
|
errno = ENOTTY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable raw mode */
|
2020-01-18 05:03:50 +00:00
|
|
|
static void norawmode(void) {
|
2019-10-11 03:59:43 +00:00
|
|
|
if (gbl_israwmode && tcsetattr(STDIN_FILENO, TCSAFLUSH, &gbl_termios_start) != -1)
|
|
|
|
gbl_israwmode = 0;
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static int curpos(void) {
|
2018-03-14 03:39:49 +00:00
|
|
|
char buf[32];
|
|
|
|
int cols, rows;
|
|
|
|
unsigned int i = 0;
|
|
|
|
if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) return -1;
|
2019-02-20 01:51:34 +00:00
|
|
|
while (i < sizeof(buf) - 1) {
|
|
|
|
if (read(STDIN_FILENO, buf + i, 1) != 1) break;
|
2018-03-14 03:39:49 +00:00
|
|
|
if (buf[i] == 'R') break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
2018-03-14 14:49:26 +00:00
|
|
|
if (buf[0] != 27 || buf[1] != '[') return -1;
|
2018-03-14 03:39:49 +00:00
|
|
|
if (sscanf(buf + 2, "%d;%d", &rows, &cols) != 2) return -1;
|
|
|
|
return cols;
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static int getcols(void) {
|
2018-03-14 03:39:49 +00:00
|
|
|
struct winsize ws;
|
|
|
|
if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
|
|
|
int start, cols;
|
|
|
|
start = curpos();
|
|
|
|
if (start == -1) goto failed;
|
|
|
|
if (write(STDOUT_FILENO, "\x1b[999C", 6) != 6) goto failed;
|
|
|
|
cols = curpos();
|
|
|
|
if (cols == -1) goto failed;
|
|
|
|
if (cols > start) {
|
|
|
|
char seq[32];
|
2019-02-20 01:51:34 +00:00
|
|
|
snprintf(seq, 32, "\x1b[%dD", cols - start);
|
2019-10-11 03:59:43 +00:00
|
|
|
if (write(STDOUT_FILENO, seq, strlen(seq)) == -1) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
return cols;
|
|
|
|
} else {
|
|
|
|
return ws.ws_col;
|
|
|
|
}
|
|
|
|
failed:
|
|
|
|
return 80;
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static void clear(void) {
|
2019-10-11 03:59:43 +00:00
|
|
|
if (write(STDOUT_FILENO, "\x1b[H\x1b[2J", 7) <= 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static void refresh(void) {
|
2018-03-14 03:39:49 +00:00
|
|
|
char seq[64];
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer b;
|
2019-01-06 08:23:03 +00:00
|
|
|
|
2018-03-14 03:39:49 +00:00
|
|
|
/* Keep cursor position on screen */
|
2019-10-11 03:59:43 +00:00
|
|
|
char *_buf = gbl_buf;
|
|
|
|
int _len = gbl_len;
|
|
|
|
int _pos = gbl_pos;
|
|
|
|
while ((gbl_plen + _pos) >= gbl_cols) {
|
2018-03-14 03:39:49 +00:00
|
|
|
_buf++;
|
|
|
|
_len--;
|
|
|
|
_pos--;
|
|
|
|
}
|
2019-10-11 03:59:43 +00:00
|
|
|
while ((gbl_plen + _len) > gbl_cols) {
|
2018-03-14 03:39:49 +00:00
|
|
|
_len--;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_init(&b, 0);
|
2019-10-11 03:59:43 +00:00
|
|
|
/* Cursor to left edge, gbl_prompt and buffer */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(&b, '\r');
|
2019-10-11 03:59:43 +00:00
|
|
|
janet_buffer_push_cstring(&b, gbl_prompt);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_bytes(&b, (uint8_t *) _buf, _len);
|
2018-03-14 03:39:49 +00:00
|
|
|
/* Erase to right */
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_cstring(&b, "\x1b[0K");
|
2018-03-14 03:39:49 +00:00
|
|
|
/* Move cursor to original position. */
|
2019-10-11 03:59:43 +00:00
|
|
|
snprintf(seq, 64, "\r\x1b[%dC", (int)(_pos + gbl_plen));
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_cstring(&b, seq);
|
2019-10-11 03:59:43 +00:00
|
|
|
if (write(STDOUT_FILENO, b.data, b.count) == -1) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_deinit(&b);
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 16:37:05 +00:00
|
|
|
static void clearlines(void) {
|
|
|
|
for (int i = 0; i < gbl_lines_below; i++) {
|
|
|
|
fprintf(stderr, "\x1b[1B\x1b[999D\x1b[K");
|
|
|
|
}
|
|
|
|
if (gbl_lines_below) {
|
|
|
|
fprintf(stderr, "\x1b[%dA\x1b[999D", gbl_lines_below);
|
|
|
|
fflush(stderr);
|
|
|
|
gbl_lines_below = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static int insert(char c, int draw) {
|
2019-10-11 03:59:43 +00:00
|
|
|
if (gbl_len < JANET_LINE_MAX - 1) {
|
|
|
|
if (gbl_len == gbl_pos) {
|
|
|
|
gbl_buf[gbl_pos++] = c;
|
|
|
|
gbl_buf[++gbl_len] = '\0';
|
2020-01-18 05:03:50 +00:00
|
|
|
if (draw) {
|
|
|
|
if (gbl_plen + gbl_len < gbl_cols) {
|
|
|
|
/* Avoid a full update of the line in the
|
|
|
|
* trivial case. */
|
|
|
|
if (write(STDOUT_FILENO, &c, 1) == -1) return -1;
|
|
|
|
} else {
|
|
|
|
refresh();
|
|
|
|
}
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-11 03:59:43 +00:00
|
|
|
memmove(gbl_buf + gbl_pos + 1, gbl_buf + gbl_pos, gbl_len - gbl_pos);
|
|
|
|
gbl_buf[gbl_pos++] = c;
|
|
|
|
gbl_buf[++gbl_len] = '\0';
|
2020-01-18 05:03:50 +00:00
|
|
|
if (draw) refresh();
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void historymove(int delta) {
|
2019-10-11 03:59:43 +00:00
|
|
|
if (gbl_history_count > 1) {
|
|
|
|
free(gbl_history[gbl_historyi]);
|
|
|
|
gbl_history[gbl_historyi] = sdup(gbl_buf);
|
2018-03-14 03:39:49 +00:00
|
|
|
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_historyi += delta;
|
|
|
|
if (gbl_historyi < 0) {
|
|
|
|
gbl_historyi = 0;
|
|
|
|
} else if (gbl_historyi >= gbl_history_count) {
|
|
|
|
gbl_historyi = gbl_history_count - 1;
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
2019-10-11 03:59:43 +00:00
|
|
|
strncpy(gbl_buf, gbl_history[gbl_historyi], JANET_LINE_MAX - 1);
|
|
|
|
gbl_pos = gbl_len = strlen(gbl_buf);
|
|
|
|
gbl_buf[gbl_len] = '\0';
|
2018-03-14 03:39:49 +00:00
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static void addhistory(void) {
|
2018-03-14 03:39:49 +00:00
|
|
|
int i, len;
|
2019-10-11 03:59:43 +00:00
|
|
|
char *newline = sdup(gbl_buf);
|
2018-03-14 03:39:49 +00:00
|
|
|
if (!newline) return;
|
2019-10-11 03:59:43 +00:00
|
|
|
len = gbl_history_count;
|
2018-09-06 02:18:42 +00:00
|
|
|
if (len < JANET_HISTORY_MAX) {
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_history[gbl_history_count++] = newline;
|
2018-03-14 03:39:49 +00:00
|
|
|
len++;
|
2018-05-16 02:03:45 +00:00
|
|
|
} else {
|
2019-10-11 03:59:43 +00:00
|
|
|
free(gbl_history[JANET_HISTORY_MAX - 1]);
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
for (i = len - 1; i > 0; i--) {
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_history[i] = gbl_history[i - 1];
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_history[0] = newline;
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static void replacehistory(void) {
|
2020-01-19 02:56:35 +00:00
|
|
|
/* History count is always > 0 here */
|
|
|
|
if (gbl_len == 0 || (gbl_history_count > 1 && !strcmp(gbl_buf, gbl_history[1]))) {
|
|
|
|
/* Delete history */
|
|
|
|
free(gbl_history[0]);
|
|
|
|
for (int i = 1; i < gbl_history_count; i++) {
|
|
|
|
gbl_history[i - 1] = gbl_history[i];
|
|
|
|
}
|
|
|
|
gbl_history_count--;
|
|
|
|
} else {
|
|
|
|
char *newline = sdup(gbl_buf);
|
|
|
|
if (!newline) return;
|
|
|
|
free(gbl_history[0]);
|
|
|
|
gbl_history[0] = newline;
|
|
|
|
}
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static void kleft(void) {
|
2019-10-11 03:59:43 +00:00
|
|
|
if (gbl_pos > 0) {
|
|
|
|
gbl_pos--;
|
2018-03-14 03:39:49 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 17:16:41 +00:00
|
|
|
static void kleftw(void) {
|
|
|
|
while (gbl_pos > 0 && isspace(gbl_buf[gbl_pos - 1])) {
|
|
|
|
gbl_pos--;
|
|
|
|
}
|
|
|
|
while (gbl_pos > 0 && !isspace(gbl_buf[gbl_pos - 1])) {
|
|
|
|
gbl_pos--;
|
|
|
|
}
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
static void kright(void) {
|
2019-10-11 03:59:43 +00:00
|
|
|
if (gbl_pos != gbl_len) {
|
|
|
|
gbl_pos++;
|
2018-03-14 03:39:49 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 17:16:41 +00:00
|
|
|
static void krightw(void) {
|
|
|
|
while (gbl_pos != gbl_len && !isspace(gbl_buf[gbl_pos])) {
|
|
|
|
gbl_pos++;
|
|
|
|
}
|
|
|
|
while (gbl_pos != gbl_len && isspace(gbl_buf[gbl_pos])) {
|
|
|
|
gbl_pos++;
|
|
|
|
}
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2020-01-19 16:37:05 +00:00
|
|
|
static void kbackspace(int draw) {
|
2019-10-11 03:59:43 +00:00
|
|
|
if (gbl_pos > 0) {
|
|
|
|
memmove(gbl_buf + gbl_pos - 1, gbl_buf + gbl_pos, gbl_len - gbl_pos);
|
|
|
|
gbl_pos--;
|
|
|
|
gbl_buf[--gbl_len] = '\0';
|
2020-01-19 16:37:05 +00:00
|
|
|
if (draw) refresh();
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 17:39:10 +00:00
|
|
|
static void kdelete(int draw) {
|
2019-11-17 00:01:52 +00:00
|
|
|
if (gbl_pos != gbl_len) {
|
|
|
|
memmove(gbl_buf + gbl_pos, gbl_buf + gbl_pos + 1, gbl_len - gbl_pos);
|
|
|
|
gbl_buf[--gbl_len] = '\0';
|
2020-01-19 17:39:10 +00:00
|
|
|
if (draw) refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kbackspacew(void) {
|
2020-01-22 00:45:13 +00:00
|
|
|
while (gbl_pos && isspace(gbl_buf[gbl_pos - 1])) {
|
2020-01-19 17:39:10 +00:00
|
|
|
kbackspace(0);
|
2019-11-17 00:01:52 +00:00
|
|
|
}
|
2020-01-22 00:45:13 +00:00
|
|
|
while (gbl_pos && !isspace(gbl_buf[gbl_pos - 1])) {
|
2020-01-19 17:39:10 +00:00
|
|
|
kbackspace(0);
|
|
|
|
}
|
|
|
|
refresh();
|
2019-11-17 00:01:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 17:39:10 +00:00
|
|
|
static void kdeletew(void) {
|
2020-01-22 00:45:13 +00:00
|
|
|
while (gbl_pos < gbl_len && isspace(gbl_buf[gbl_pos])) {
|
2020-01-19 17:39:10 +00:00
|
|
|
kdelete(0);
|
|
|
|
}
|
2020-01-22 00:45:13 +00:00
|
|
|
while (gbl_pos < gbl_len && !isspace(gbl_buf[gbl_pos])) {
|
2020-01-19 17:39:10 +00:00
|
|
|
kdelete(0);
|
|
|
|
}
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
/* See tools/symchargen.c */
|
|
|
|
static int is_symbol_char_gen(uint8_t c) {
|
|
|
|
if (c & 0x80) return 1;
|
|
|
|
if (c >= 'a' && c <= 'z') return 1;
|
|
|
|
if (c >= 'A' && c <= 'Z') return 1;
|
|
|
|
if (c >= '0' && c <= '9') return 1;
|
|
|
|
return (c == '!' ||
|
|
|
|
c == '$' ||
|
|
|
|
c == '%' ||
|
|
|
|
c == '&' ||
|
|
|
|
c == '*' ||
|
|
|
|
c == '+' ||
|
|
|
|
c == '-' ||
|
|
|
|
c == '.' ||
|
|
|
|
c == '/' ||
|
|
|
|
c == ':' ||
|
|
|
|
c == '<' ||
|
|
|
|
c == '?' ||
|
|
|
|
c == '=' ||
|
|
|
|
c == '>' ||
|
|
|
|
c == '@' ||
|
|
|
|
c == '^' ||
|
|
|
|
c == '_');
|
|
|
|
}
|
|
|
|
|
2020-01-18 06:17:08 +00:00
|
|
|
static JanetByteView get_symprefix(void) {
|
2020-01-18 05:03:50 +00:00
|
|
|
/* Calculate current partial symbol. Maybe we could actually hook up the Janet
|
|
|
|
* parser here...*/
|
|
|
|
int i;
|
2020-01-18 06:17:08 +00:00
|
|
|
JanetByteView ret;
|
|
|
|
ret.len = 0;
|
2020-01-18 05:03:50 +00:00
|
|
|
for (i = gbl_pos - 1; i >= 0; i--) {
|
|
|
|
uint8_t c = (uint8_t) gbl_buf[i];
|
|
|
|
if (!is_symbol_char_gen(c)) break;
|
2020-01-18 06:17:08 +00:00
|
|
|
ret.len++;
|
|
|
|
}
|
|
|
|
/* Will be const for duration of match checking */
|
|
|
|
ret.bytes = (const uint8_t *)(gbl_buf + i + 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compare_bytes(JanetByteView a, JanetByteView b) {
|
|
|
|
int32_t minlen = a.len < b.len ? a.len : b.len;
|
|
|
|
int result = strncmp((const char *) a.bytes, (const char *) b.bytes, minlen);
|
|
|
|
if (result) return result;
|
|
|
|
return a.len < b.len ? -1 : a.len > b.len ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_match(JanetByteView src, const uint8_t *testsym, int32_t testlen) {
|
|
|
|
JanetByteView test;
|
|
|
|
test.bytes = testsym;
|
|
|
|
test.len = testlen;
|
|
|
|
if (src.len > test.len || strncmp((const char *) src.bytes, (const char *) test.bytes, src.len)) return;
|
|
|
|
JanetByteView mm = test;
|
|
|
|
for (int i = 0; i < gbl_match_count; i++) {
|
|
|
|
if (compare_bytes(mm, gbl_matches[i]) < 0) {
|
|
|
|
JanetByteView temp = mm;
|
|
|
|
mm = gbl_matches[i];
|
|
|
|
gbl_matches[i] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gbl_match_count == JANET_MATCH_MAX) return;
|
|
|
|
gbl_matches[gbl_match_count++] = mm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_cmatch(JanetByteView src, const char *cstr) {
|
|
|
|
check_match(src, (const uint8_t *) cstr, (int32_t) strlen(cstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
static JanetByteView longest_common_prefix(void) {
|
|
|
|
JanetByteView bv;
|
|
|
|
if (gbl_match_count == 0) {
|
|
|
|
bv.len = 0;
|
|
|
|
bv.bytes = NULL;
|
|
|
|
} else {
|
|
|
|
bv = gbl_matches[0];
|
|
|
|
for (int i = 0; i < gbl_match_count; i++) {
|
|
|
|
JanetByteView other = gbl_matches[i];
|
|
|
|
int32_t minlen = other.len < bv.len ? other.len : bv.len;
|
|
|
|
for (bv.len = 0; bv.len < minlen; bv.len++) {
|
|
|
|
if (bv.bytes[bv.len] != other.bytes[bv.len]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 05:03:50 +00:00
|
|
|
}
|
2020-01-18 06:17:08 +00:00
|
|
|
return bv;
|
|
|
|
}
|
2020-01-18 05:03:50 +00:00
|
|
|
|
2020-01-18 06:17:08 +00:00
|
|
|
static void check_specials(JanetByteView src) {
|
|
|
|
check_cmatch(src, "break");
|
|
|
|
check_cmatch(src, "def");
|
|
|
|
check_cmatch(src, "do");
|
|
|
|
check_cmatch(src, "fn");
|
|
|
|
check_cmatch(src, "if");
|
|
|
|
check_cmatch(src, "quasiquote");
|
|
|
|
check_cmatch(src, "quote");
|
|
|
|
check_cmatch(src, "set");
|
|
|
|
check_cmatch(src, "splice");
|
|
|
|
check_cmatch(src, "unquote");
|
|
|
|
check_cmatch(src, "var");
|
|
|
|
check_cmatch(src, "while");
|
|
|
|
}
|
2020-01-18 05:03:50 +00:00
|
|
|
|
2020-04-10 16:36:23 +00:00
|
|
|
static void resolve_format(JanetTable *entry) {
|
|
|
|
int is_macro = janet_truthy(janet_table_get(entry, janet_ckeywordv("macro")));
|
|
|
|
Janet refv = janet_table_get(entry, janet_ckeywordv("ref"));
|
|
|
|
int is_ref = janet_checktype(refv, JANET_ARRAY);
|
|
|
|
Janet value = janet_wrap_nil();
|
|
|
|
if (is_ref) {
|
|
|
|
JanetArray *a = janet_unwrap_array(refv);
|
|
|
|
if (a->count) value = a->data[0];
|
|
|
|
} else {
|
|
|
|
value = janet_table_get(entry, janet_ckeywordv("value"));
|
|
|
|
}
|
|
|
|
if (is_macro) {
|
|
|
|
fprintf(stderr, " macro\n");
|
|
|
|
gbl_lines_below++;
|
|
|
|
} else if (is_ref) {
|
|
|
|
janet_eprintf(" var (%t)\n", value);
|
|
|
|
gbl_lines_below++;
|
|
|
|
} else {
|
|
|
|
janet_eprintf(" %t\n", value);
|
|
|
|
gbl_lines_below++;
|
|
|
|
}
|
|
|
|
Janet sm = janet_table_get(entry, janet_ckeywordv("source-map"));
|
|
|
|
Janet path = janet_get(sm, janet_wrap_integer(0));
|
|
|
|
Janet line = janet_get(sm, janet_wrap_integer(1));
|
|
|
|
Janet col = janet_get(sm, janet_wrap_integer(2));
|
|
|
|
if (janet_checktype(path, JANET_STRING) && janet_truthy(line) && janet_truthy(col)) {
|
|
|
|
janet_eprintf(" %S on line %v, column %v\n", janet_unwrap_string(path), line, col);
|
|
|
|
gbl_lines_below++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doc_format(JanetString doc, int32_t width) {
|
|
|
|
int32_t maxcol = width - 8;
|
|
|
|
uint8_t wordbuf[256] = {0};
|
|
|
|
int32_t wordp = 0;
|
|
|
|
int32_t current = 0;
|
|
|
|
if (maxcol > 200) maxcol = 200;
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
for (int32_t i = 0; i < janet_string_length(doc); i++) {
|
|
|
|
uint8_t b = doc[i];
|
|
|
|
switch (b) {
|
|
|
|
default: {
|
|
|
|
if (maxcol <= current + wordp + 1) {
|
|
|
|
if (!current) {
|
|
|
|
fwrite(wordbuf, wordp, 1, stderr);
|
|
|
|
wordp = 0;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n ");
|
|
|
|
gbl_lines_below++;
|
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
wordbuf[wordp++] = b;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '\t': {
|
|
|
|
if (maxcol <= current + wordp + 2) {
|
|
|
|
if (!current) {
|
|
|
|
fwrite(wordbuf, wordp, 1, stderr);
|
|
|
|
wordp = 0;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n ");
|
|
|
|
gbl_lines_below++;
|
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
wordbuf[wordp++] = ' ';
|
|
|
|
wordbuf[wordp++] = ' ';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '\n':
|
|
|
|
case ' ': {
|
|
|
|
if (wordp) {
|
|
|
|
int32_t oldcur = current;
|
|
|
|
int spacer = maxcol > current + wordp + 1;
|
|
|
|
if (spacer) current++;
|
|
|
|
else current = 0;
|
|
|
|
current += wordp;
|
|
|
|
if (oldcur) fprintf(stderr, spacer ? " " : "\n ");
|
|
|
|
if (oldcur && !spacer) gbl_lines_below++;
|
|
|
|
fwrite(wordbuf, wordp, 1, stderr);
|
|
|
|
wordp = 0;
|
|
|
|
}
|
|
|
|
if (b == '\n') {
|
|
|
|
fprintf(stderr, "\n ");
|
|
|
|
gbl_lines_below++;
|
|
|
|
current = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wordp) {
|
|
|
|
int32_t oldcur = current;
|
|
|
|
int spacer = maxcol > current + wordp + 1;
|
|
|
|
if (spacer) current++;
|
|
|
|
else current = 0;
|
|
|
|
current += wordp + 1;
|
|
|
|
if (oldcur) fprintf(stderr, spacer ? " " : "\n ");
|
|
|
|
if (oldcur && !spacer) gbl_lines_below++;
|
|
|
|
fwrite(wordbuf, wordp, 1, stderr);
|
|
|
|
wordp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void find_matches(JanetByteView prefix) {
|
|
|
|
JanetTable *env = gbl_complete_env;
|
|
|
|
gbl_match_count = 0;
|
|
|
|
while (NULL != env) {
|
|
|
|
JanetKV *kvend = env->data + env->capacity;
|
|
|
|
for (JanetKV *kv = env->data; kv < kvend; kv++) {
|
|
|
|
if (!janet_checktype(kv->key, JANET_SYMBOL)) continue;
|
|
|
|
const uint8_t *sym = janet_unwrap_symbol(kv->key);
|
|
|
|
check_match(prefix, sym, janet_string_length(sym));
|
|
|
|
}
|
|
|
|
env = env->proto;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kshowdoc(void) {
|
|
|
|
if (!gbl_complete_env) return;
|
|
|
|
while (is_symbol_char_gen(gbl_buf[gbl_pos])) gbl_pos++;
|
|
|
|
JanetByteView prefix = get_symprefix();
|
|
|
|
Janet symbol = janet_symbolv(prefix.bytes, prefix.len);
|
|
|
|
Janet entry = janet_table_get(gbl_complete_env, symbol);
|
|
|
|
if (!janet_checktype(entry, JANET_TABLE)) return;
|
|
|
|
Janet doc = janet_table_get(janet_unwrap_table(entry), janet_ckeywordv("doc"));
|
|
|
|
if (!janet_checktype(doc, JANET_STRING)) return;
|
|
|
|
JanetString docs = janet_unwrap_string(doc);
|
|
|
|
int num_cols = getcols();
|
|
|
|
clearlines();
|
|
|
|
fprintf(stderr, "\n\n\n");
|
|
|
|
gbl_lines_below += 3;
|
|
|
|
resolve_format(janet_unwrap_table(entry));
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
gbl_lines_below += 1;
|
|
|
|
doc_format(docs, num_cols);
|
|
|
|
fprintf(stderr, "\n\n");
|
|
|
|
gbl_lines_below += 2;
|
|
|
|
/* Go up to original line (zsh-like autocompletion) */
|
|
|
|
fprintf(stderr, "\x1B[%dA", gbl_lines_below);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
2020-01-18 06:17:08 +00:00
|
|
|
static void kshowcomp(void) {
|
|
|
|
JanetTable *env = gbl_complete_env;
|
|
|
|
if (env == NULL) {
|
|
|
|
insert(' ', 0);
|
|
|
|
insert(' ', 0);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-18 05:03:50 +00:00
|
|
|
|
2020-01-18 15:44:59 +00:00
|
|
|
/* Advance while on symbol char */
|
|
|
|
while (is_symbol_char_gen(gbl_buf[gbl_pos]))
|
|
|
|
gbl_pos++;
|
|
|
|
|
2020-01-18 06:17:08 +00:00
|
|
|
JanetByteView prefix = get_symprefix();
|
2020-04-10 16:36:23 +00:00
|
|
|
if (prefix.len == 0) return;
|
2020-01-18 06:17:08 +00:00
|
|
|
|
2020-04-10 16:36:23 +00:00
|
|
|
find_matches(prefix);
|
2020-01-18 05:03:50 +00:00
|
|
|
|
2020-01-18 06:17:08 +00:00
|
|
|
check_specials(prefix);
|
|
|
|
|
|
|
|
JanetByteView lcp = longest_common_prefix();
|
|
|
|
for (int i = prefix.len; i < lcp.len; i++) {
|
|
|
|
insert(lcp.bytes[i], 0);
|
2020-01-18 05:03:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 17:31:42 +00:00
|
|
|
if (!gbl_lines_below && prefix.len != lcp.len) return;
|
2020-01-18 15:44:59 +00:00
|
|
|
|
2020-01-18 06:17:08 +00:00
|
|
|
int32_t maxlen = 0;
|
|
|
|
for (int i = 0; i < gbl_match_count; i++)
|
|
|
|
if (gbl_matches[i].len > maxlen)
|
|
|
|
maxlen = gbl_matches[i].len;
|
|
|
|
|
2020-01-18 05:03:50 +00:00
|
|
|
int num_cols = getcols();
|
2020-01-19 17:31:42 +00:00
|
|
|
clearlines();
|
2020-01-18 06:17:08 +00:00
|
|
|
if (gbl_match_count >= 2) {
|
2020-01-18 05:03:50 +00:00
|
|
|
|
2020-01-18 06:17:08 +00:00
|
|
|
/* Second pass, print */
|
|
|
|
int col_width = maxlen + 4;
|
|
|
|
int cols = num_cols / col_width;
|
|
|
|
if (cols == 0) cols = 1;
|
|
|
|
int current_col = 0;
|
|
|
|
for (int i = 0; i < gbl_match_count; i++) {
|
2020-01-19 16:37:05 +00:00
|
|
|
if (current_col == 0) {
|
|
|
|
putc('\n', stderr);
|
|
|
|
gbl_lines_below++;
|
|
|
|
}
|
2020-01-18 06:27:00 +00:00
|
|
|
JanetByteView s = gbl_matches[i];
|
2020-01-18 20:32:25 +00:00
|
|
|
fprintf(stderr, "%s", (const char *) s.bytes);
|
2020-01-18 06:27:00 +00:00
|
|
|
for (int j = s.len; j < col_width; j++) {
|
2020-01-18 20:32:25 +00:00
|
|
|
putc(' ', stderr);
|
2020-01-18 06:27:00 +00:00
|
|
|
}
|
2020-01-18 05:03:50 +00:00
|
|
|
current_col = (current_col + 1) % cols;
|
|
|
|
}
|
|
|
|
|
2020-01-19 16:37:05 +00:00
|
|
|
/* Go up to original line (zsh-like autocompletion) */
|
|
|
|
fprintf(stderr, "\x1B[%dA", gbl_lines_below);
|
|
|
|
|
|
|
|
fflush(stderr);
|
2020-01-18 06:17:08 +00:00
|
|
|
}
|
2020-01-18 05:03:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 03:39:49 +00:00
|
|
|
static int line() {
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_cols = getcols();
|
|
|
|
gbl_plen = 0;
|
|
|
|
gbl_len = 0;
|
|
|
|
gbl_pos = 0;
|
|
|
|
while (gbl_prompt[gbl_plen]) gbl_plen++;
|
|
|
|
gbl_buf[0] = '\0';
|
2018-03-14 03:39:49 +00:00
|
|
|
|
|
|
|
addhistory();
|
|
|
|
|
2019-10-11 03:59:43 +00:00
|
|
|
if (write(STDOUT_FILENO, gbl_prompt, gbl_plen) == -1) return -1;
|
2018-03-14 03:39:49 +00:00
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
char seq[3];
|
|
|
|
|
2020-01-22 00:45:13 +00:00
|
|
|
if (read(STDIN_FILENO, &c, 1) <= 0) return -1;
|
2018-03-14 03:39:49 +00:00
|
|
|
|
2019-02-20 01:51:34 +00:00
|
|
|
switch (c) {
|
|
|
|
default:
|
2020-01-19 16:37:05 +00:00
|
|
|
if (c < 0x20) break;
|
2020-01-18 05:03:50 +00:00
|
|
|
if (insert(c, 1)) return -1;
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
2020-01-19 16:37:05 +00:00
|
|
|
case 1: /* ctrl-a */
|
|
|
|
gbl_pos = 0;
|
2020-01-18 05:03:50 +00:00
|
|
|
refresh();
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
2020-01-19 16:37:05 +00:00
|
|
|
case 2: /* ctrl-b */
|
|
|
|
kleft();
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case 3: /* ctrl-c */
|
|
|
|
errno = EAGAIN;
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_sigint_flag = 1;
|
2020-01-19 16:37:05 +00:00
|
|
|
clearlines();
|
2019-02-20 01:51:34 +00:00
|
|
|
return -1;
|
|
|
|
case 4: /* ctrl-d, eof */
|
2020-03-04 13:56:04 +00:00
|
|
|
if (gbl_len == 0) { /* quit on empty line */
|
|
|
|
clearlines();
|
|
|
|
return -1;
|
2020-03-09 01:43:06 +00:00
|
|
|
}
|
2020-03-04 13:56:04 +00:00
|
|
|
kdelete(1);
|
|
|
|
break;
|
2020-01-16 04:38:06 +00:00
|
|
|
case 5: /* ctrl-e */
|
|
|
|
gbl_pos = gbl_len;
|
2020-01-17 15:33:30 +00:00
|
|
|
refresh();
|
2020-01-16 04:38:06 +00:00
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case 6: /* ctrl-f */
|
|
|
|
kright();
|
|
|
|
break;
|
2020-04-10 16:36:23 +00:00
|
|
|
case 7: /* ctrl-g */
|
|
|
|
kshowdoc();
|
|
|
|
refresh();
|
|
|
|
break;
|
2020-01-19 16:37:05 +00:00
|
|
|
case 127: /* backspace */
|
|
|
|
case 8: /* ctrl-h */
|
|
|
|
kbackspace(1);
|
|
|
|
break;
|
|
|
|
case 9: /* tab */
|
|
|
|
kshowcomp();
|
|
|
|
refresh();
|
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
case 11: /* ctrl-k */
|
|
|
|
gbl_buf[gbl_pos] = '\0';
|
|
|
|
gbl_len = gbl_pos;
|
|
|
|
refresh();
|
|
|
|
break;
|
2020-01-19 16:37:05 +00:00
|
|
|
case 12: /* ctrl-l */
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
case 13: /* enter */
|
|
|
|
clearlines();
|
|
|
|
return 0;
|
|
|
|
case 14: /* ctrl-n */
|
|
|
|
historymove(-1);
|
|
|
|
break;
|
|
|
|
case 16: /* ctrl-p */
|
|
|
|
historymove(1);
|
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
case 21: { /* ctrl-u */
|
2020-01-22 05:11:00 +00:00
|
|
|
memmove(gbl_buf, gbl_buf + gbl_pos, gbl_len - gbl_pos);
|
|
|
|
gbl_len -= gbl_pos;
|
|
|
|
gbl_buf[gbl_len] = '\0';
|
2020-01-22 00:45:13 +00:00
|
|
|
gbl_pos = 0;
|
2019-02-20 01:51:34 +00:00
|
|
|
refresh();
|
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
}
|
2020-01-19 16:37:05 +00:00
|
|
|
case 23: /* ctrl-w */
|
2020-01-19 17:39:10 +00:00
|
|
|
kbackspacew();
|
2020-01-19 16:37:05 +00:00
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
case 26: /* ctrl-z */
|
|
|
|
norawmode();
|
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
rawmode();
|
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
case 27: /* escape sequence */
|
|
|
|
/* Read the next two bytes representing the escape sequence.
|
|
|
|
* Use two calls to handle slow terminals returning the two
|
|
|
|
* chars at different times. */
|
|
|
|
if (read(STDIN_FILENO, seq, 1) == -1) break;
|
2020-01-22 05:11:00 +00:00
|
|
|
/* Esc[ = Control Sequence Introducer (CSI) */
|
2019-02-20 01:51:34 +00:00
|
|
|
if (seq[0] == '[') {
|
2020-01-19 17:16:41 +00:00
|
|
|
if (read(STDIN_FILENO, seq + 1, 1) == -1) break;
|
2019-02-20 01:51:34 +00:00
|
|
|
if (seq[1] >= '0' && seq[1] <= '9') {
|
|
|
|
/* Extended escape, read additional byte. */
|
|
|
|
if (read(STDIN_FILENO, seq + 2, 1) == -1) break;
|
|
|
|
if (seq[2] == '~') {
|
|
|
|
switch (seq[1]) {
|
2020-01-22 00:45:13 +00:00
|
|
|
case '1': /* Home */
|
|
|
|
gbl_pos = 0;
|
|
|
|
refresh();
|
|
|
|
break;
|
2019-11-17 00:07:15 +00:00
|
|
|
case '3': /* delete */
|
2020-01-19 17:39:10 +00:00
|
|
|
kdelete(1);
|
2019-11-17 00:07:15 +00:00
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
case '4': /* End */
|
|
|
|
gbl_pos = gbl_len;
|
|
|
|
refresh();
|
|
|
|
break;
|
2019-02-20 01:51:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 05:11:00 +00:00
|
|
|
} else if (seq[0] == 'O') {
|
|
|
|
if (read(STDIN_FILENO, seq + 1, 1) == -1) break;
|
|
|
|
switch (seq[1]) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case 'H': /* Home (some keyboards) */
|
|
|
|
gbl_pos = 0;
|
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
case 'F': /* End (some keyboards) */
|
|
|
|
gbl_pos = gbl_len;
|
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
} else {
|
|
|
|
switch (seq[1]) {
|
2020-01-19 16:37:05 +00:00
|
|
|
/* Single escape sequences */
|
2019-02-20 01:51:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
case 'A': /* Up */
|
2019-02-20 01:51:34 +00:00
|
|
|
historymove(1);
|
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
case 'B': /* Down */
|
2019-02-20 01:51:34 +00:00
|
|
|
historymove(-1);
|
|
|
|
break;
|
|
|
|
case 'C': /* Right */
|
|
|
|
kright();
|
|
|
|
break;
|
|
|
|
case 'D': /* Left */
|
|
|
|
kleft();
|
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
case 'H': /* Home */
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_pos = 0;
|
2019-02-20 01:51:34 +00:00
|
|
|
refresh();
|
|
|
|
break;
|
2020-01-22 00:45:13 +00:00
|
|
|
case 'F': /* End */
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_pos = gbl_len;
|
2019-02-20 01:51:34 +00:00
|
|
|
refresh();
|
|
|
|
break;
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 17:16:41 +00:00
|
|
|
} else {
|
|
|
|
/* Check alt-(shift) bindings */
|
|
|
|
switch (seq[0]) {
|
2019-02-20 01:51:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-01-22 05:11:00 +00:00
|
|
|
case 'd': /* Alt-d */
|
2020-01-19 17:39:10 +00:00
|
|
|
kdeletew();
|
|
|
|
break;
|
2020-01-22 05:11:00 +00:00
|
|
|
case 'b': /* Alt-b */
|
2020-01-19 17:16:41 +00:00
|
|
|
kleftw();
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
2020-01-22 05:11:00 +00:00
|
|
|
case 'f': /* Alt-f */
|
2020-01-19 17:16:41 +00:00
|
|
|
krightw();
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
2020-01-22 05:11:00 +00:00
|
|
|
case ',': /* Alt-, */
|
2020-01-19 21:43:47 +00:00
|
|
|
historymove(JANET_HISTORY_MAX);
|
|
|
|
break;
|
2020-01-22 05:11:00 +00:00
|
|
|
case '.': /* Alt-. */
|
2020-01-19 21:43:47 +00:00
|
|
|
historymove(-JANET_HISTORY_MAX);
|
|
|
|
break;
|
2020-03-06 09:53:34 +00:00
|
|
|
case 127: /* Alt-backspace */
|
|
|
|
kbackspacew();
|
|
|
|
break;
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 01:51:34 +00:00
|
|
|
break;
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_line_init() {
|
2018-03-14 03:39:49 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
void janet_line_deinit() {
|
2018-03-14 14:49:26 +00:00
|
|
|
int i;
|
2018-03-14 03:39:49 +00:00
|
|
|
norawmode();
|
2019-10-11 03:59:43 +00:00
|
|
|
for (i = 0; i < gbl_history_count; i++)
|
|
|
|
free(gbl_history[i]);
|
|
|
|
gbl_historyi = 0;
|
2018-03-14 14:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int checktermsupport() {
|
|
|
|
const char *t = getenv("TERM");
|
|
|
|
int i;
|
|
|
|
if (!t) return 1;
|
|
|
|
for (i = 0; badterms[i]; i++)
|
2018-03-18 13:13:21 +00:00
|
|
|
if (!strcmp(t, badterms[i])) return 0;
|
2018-03-14 14:49:26 +00:00
|
|
|
return 1;
|
2018-03-14 03:39:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:34:22 +00:00
|
|
|
void janet_line_get(const char *p, JanetBuffer *buffer) {
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_prompt = p;
|
2018-03-14 03:39:49 +00:00
|
|
|
buffer->count = 0;
|
2019-10-11 03:59:43 +00:00
|
|
|
gbl_historyi = 0;
|
2020-01-18 20:32:25 +00:00
|
|
|
FILE *out = janet_dynfile("err", stderr);
|
2018-03-14 14:49:26 +00:00
|
|
|
if (!isatty(STDIN_FILENO) || !checktermsupport()) {
|
|
|
|
simpleline(buffer);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-14 03:39:49 +00:00
|
|
|
if (rawmode()) {
|
|
|
|
simpleline(buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (line()) {
|
|
|
|
norawmode();
|
2019-10-11 03:59:43 +00:00
|
|
|
if (gbl_sigint_flag) {
|
2019-05-02 21:11:30 +00:00
|
|
|
raise(SIGINT);
|
|
|
|
} else {
|
2019-06-08 19:27:13 +00:00
|
|
|
fputc('\n', out);
|
2019-05-02 21:11:30 +00:00
|
|
|
}
|
2018-03-14 03:39:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-27 05:13:53 +00:00
|
|
|
fflush(stdin);
|
2018-03-14 03:39:49 +00:00
|
|
|
norawmode();
|
2019-06-08 19:27:13 +00:00
|
|
|
fputc('\n', out);
|
2019-10-11 03:59:43 +00:00
|
|
|
janet_buffer_ensure(buffer, gbl_len + 1, 2);
|
|
|
|
memcpy(buffer->data, gbl_buf, gbl_len);
|
|
|
|
buffer->data[gbl_len] = '\n';
|
|
|
|
buffer->count = gbl_len + 1;
|
2018-03-14 03:39:49 +00:00
|
|
|
replacehistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2020-01-29 05:38:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int i, status;
|
|
|
|
JanetArray *args;
|
|
|
|
JanetTable *env;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* Enable color console on windows 10 console and utf8 output. */
|
|
|
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
DWORD dwMode = 0;
|
|
|
|
GetConsoleMode(hOut, &dwMode);
|
|
|
|
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
|
|
SetConsoleMode(hOut, dwMode);
|
|
|
|
SetConsoleOutputCP(65001);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up VM */
|
|
|
|
janet_init();
|
|
|
|
|
|
|
|
/* Replace original getline with new line getter */
|
|
|
|
JanetTable *replacements = janet_table(0);
|
|
|
|
janet_table_put(replacements, janet_csymbolv("getline"), janet_wrap_cfunction(janet_line_getter));
|
|
|
|
janet_line_init();
|
|
|
|
|
|
|
|
/* Get core env */
|
|
|
|
env = janet_core_env(replacements);
|
|
|
|
|
|
|
|
/* Create args tuple */
|
|
|
|
args = janet_array(argc);
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
janet_array_push(args, janet_cstringv(argv[i]));
|
|
|
|
|
|
|
|
/* Save current executable path to (dyn :executable) */
|
|
|
|
janet_table_put(env, janet_ckeywordv("executable"), janet_cstringv(argv[0]));
|
|
|
|
|
|
|
|
/* Run startup script */
|
|
|
|
Janet mainfun, out;
|
|
|
|
janet_resolve(env, janet_csymbol("cli-main"), &mainfun);
|
|
|
|
Janet mainargs[1] = { janet_wrap_array(args) };
|
|
|
|
JanetFiber *fiber = janet_fiber(janet_unwrap_function(mainfun), 64, 1, mainargs);
|
|
|
|
fiber->env = env;
|
|
|
|
status = janet_continue(fiber, janet_wrap_nil(), &out);
|
2020-02-10 01:04:34 +00:00
|
|
|
if (status != JANET_SIGNAL_OK && status < JANET_SIGNAL_USER0) {
|
2020-01-29 05:38:52 +00:00
|
|
|
janet_stacktrace(fiber, out);
|
|
|
|
}
|
|
|
|
|
2020-02-10 01:04:34 +00:00
|
|
|
#ifdef JANET_NET
|
|
|
|
janet_loop();
|
|
|
|
#endif
|
|
|
|
|
2020-01-29 05:38:52 +00:00
|
|
|
/* Deinitialize vm */
|
|
|
|
janet_deinit();
|
|
|
|
janet_line_deinit();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|