Ues linenoise in Lua, Python, and SQLite

This commit is contained in:
Justine Tunney 2021-08-14 11:26:23 -07:00
parent fe29710e4e
commit 1e5bd4d23e
14 changed files with 230 additions and 303 deletions

View file

@ -136,6 +136,7 @@ include third_party/regex/regex.mk #─┘
include third_party/third_party.mk include third_party/third_party.mk
include libc/testlib/testlib.mk include libc/testlib/testlib.mk
include tool/viz/lib/vizlib.mk include tool/viz/lib/vizlib.mk
include third_party/linenoise/linenoise.mk
include third_party/lua/lua.mk include third_party/lua/lua.mk
include third_party/sqlite3/sqlite3.mk include third_party/sqlite3/sqlite3.mk
include third_party/mbedtls/test/test.mk include third_party/mbedtls/test/test.mk

View file

@ -5,8 +5,8 @@ DESCRIPTION
ORIGIN ORIGIN
https://github.com/antirez/linenoise https://github.com/antirez/linenoise
97d2850af13c339369093b78abe5265845d78220 97d2850af13c339369093b78abe5265845d78220
Author: antirez <antirez@gmail.com> Author: antirez <antirez@gmail.com>
Date: Thu Mar 12 15:51:45 2020 +0100 Date: Thu Mar 12 15:51:45 2020 +0100
Use unsigned int instead of uint like rest of code base. Use unsigned int instead of uint like rest of code base.

View file

@ -1,121 +1,122 @@
/* linenoise.c -- guerrilla line editing library against the idea that a /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-│
* line editing lib needs to be 20,000 lines of C code. vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi
*
* You can find the latest source code at: linenoise.c -- guerrilla line editing library against the idea that a
* line editing lib needs to be 20,000 lines of C code.
* http://github.com/antirez/linenoise
* You can find the latest source code at:
* Does a number of crazy assumptions that happen to be true in 99.9999% of
* the 2010 UNIX computers around. http://github.com/antirez/linenoise │
*
* ------------------------------------------------------------------------ Does a number of crazy assumptions that happen to be true in 99.9999% of
* the 2010 UNIX computers around.
* Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved. Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are All rights reserved.
* met:
* Redistribution and use in source and binary forms, with or without
* * Redistributions of source code must retain the above copyright modification, are permitted provided that the following conditions are
* notice, this list of conditions and the following disclaimer. met:
*
* * Redistributions in binary form must reproduce the above copyright * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer.
* documentation and/or other materials provided with the distribution.
* * Redistributions in binary form must reproduce the above copyright
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS notice, this list of conditions and the following disclaimer in the
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT documentation and/or other materials provided with the distribution.
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* ------------------------------------------------------------------------ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* References:
* - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
* - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
* References:
* Todo list: - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html │
* - Filter bogus Ctrl+<char> combinations. - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html │
* - Win32 support
* Todo list:
* Bloat: - Filter bogus Ctrl+<char> combinations.
* - History search like Ctrl+r in readline? - Win32 support
*
* List of escape sequences used by this program, we do everything just Bloat:
* with three sequences. In order to be so cheap we may have some - History search like Ctrl+r in readline?
* flickering effect with some slow terminal, but the lesser sequences
* the more compatible. List of escape sequences used by this program, we do everything just
* with three sequences. In order to be so cheap we may have some
* EL (Erase Line) flickering effect with some slow terminal, but the lesser sequences
* Sequence: ESC [ n K the more compatible.
* Effect: if n is 0 or missing, clear from cursor to end of line
* Effect: if n is 1, clear from beginning of line to cursor EL (Erase Line)
* Effect: if n is 2, clear entire line Sequence: ESC [ n K
* Effect: if n is 0 or missing, clear from cursor to end of line
* CUF (CUrsor Forward) Effect: if n is 1, clear from beginning of line to cursor
* Sequence: ESC [ n C Effect: if n is 2, clear entire line
* Effect: moves cursor forward n chars
* CUF (CUrsor Forward)
* CUB (CUrsor Backward) Sequence: ESC [ n C
* Sequence: ESC [ n D Effect: moves cursor forward n chars
* Effect: moves cursor backward n chars
* CUB (CUrsor Backward)
* The following is used to get the terminal width if getting Sequence: ESC [ n D
* the width with the TIOCGWINSZ ioctl fails Effect: moves cursor backward n chars
*
* DSR (Device Status Report) The following is used to get the terminal width if getting
* Sequence: ESC [ 6 n the width with the TIOCGWINSZ ioctl fails
* Effect: reports the current cusor position as ESC [ n ; m R
* where n is the row and m is the column DSR (Device Status Report)
* Sequence: ESC [ 6 n
* When multi line mode is enabled, we also use an additional escape Effect: reports the current cusor position as ESC [ n ; m R
* sequence. However multi line editing is disabled by default. where n is the row and m is the column
*
* CUU (Cursor Up) When multi line mode is enabled, we also use an additional escape
* Sequence: ESC [ n A sequence. However multi line editing is disabled by default.
* Effect: moves cursor up of n chars.
* CUU (Cursor Up)
* CUD (Cursor Down) Sequence: ESC [ n A
* Sequence: ESC [ n B Effect: moves cursor up of n chars.
* Effect: moves cursor down of n chars.
* CUD (Cursor Down)
* When linenoiseClearScreen() is called, two additional escape sequences Sequence: ESC [ n B
* are used in order to clear the screen and position the cursor at home Effect: moves cursor down of n chars.
* position.
* When linenoiseClearScreen() is called, two additional escape sequences
* CUP (Cursor position) are used in order to clear the screen and position the cursor at home
* Sequence: ESC [ H position.
* Effect: moves the cursor to upper left corner
* CUP (Cursor position)
* ED (Erase display) Sequence: ESC [ H
* Sequence: ESC [ 2 J Effect: moves the cursor to upper left corner
* Effect: clear the whole screen
* ED (Erase display)
*/ Sequence: ESC [ 2 J
Effect: clear the whole screen
#include <termios.h> */
#include <unistd.h> #include "libc/calls/calls.h"
#include <stdlib.h> #include "libc/calls/termios.h"
#include <stdio.h> #include "libc/calls/weirdtypes.h"
#include <errno.h> #include "libc/errno.h"
#include <string.h> #include "libc/fmt/fmt.h"
#include <stdlib.h> #include "libc/runtime/gc.internal.h"
#include <ctype.h> #include "libc/runtime/runtime.h"
#include <sys/stat.h> #include "libc/stdio/stdio.h"
#include <sys/types.h> #include "libc/str/str.h"
#include <sys/ioctl.h> #include "libc/sysv/consts/fileno.h"
#include <unistd.h> #include "libc/x/x.h"
#include "linenoise.h" #include "third_party/linenoise/linenoise.h"
/* clang-format off */
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
#define LINENOISE_MAX_LINE 4096 #define LINENOISE_MAX_LINE 4096
@ -174,8 +175,7 @@ enum KEY_ACTION{
}; };
static void linenoiseAtExit(void); static void linenoiseAtExit(void);
int linenoiseHistoryAdd(const char *line); static void refreshLine(struct linenoiseState *);
static void refreshLine(struct linenoiseState *l);
/* Debugging macro. */ /* Debugging macro. */
#if 0 #if 0
@ -219,26 +219,26 @@ void linenoiseSetMultiLine(int ml) {
/* Return true if the terminal name is in the list of terminals we know are /* Return true if the terminal name is in the list of terminals we know are
* not able to understand basic escape sequences. */ * not able to understand basic escape sequences. */
static int isUnsupportedTerm(void) { static int isUnsupportedTerm(void) {
char *term = getenv("TERM");
int j; int j;
char *term = getenv("TERM");
if (term == NULL) return 0; if (term == NULL) return 0;
for (j = 0; unsupported_term[j]; j++) for (j = 0; unsupported_term[j]; j++) {
if (!strcasecmp(term,unsupported_term[j])) return 1; if (!strcasecmp(term,unsupported_term[j])) {
return 1;
}
}
return 0; return 0;
} }
/* Raw mode: 1960 magic shit. */ /* Raw mode: 1960's magic. */
static int enableRawMode(int fd) { static int enableRawMode(int fd) {
struct termios raw; struct termios raw;
if (!isatty(STDIN_FILENO)) goto fatal; if (!isatty(STDIN_FILENO)) goto fatal;
if (!atexit_registered) { if (!atexit_registered) {
atexit(linenoiseAtExit); atexit(linenoiseAtExit);
atexit_registered = 1; atexit_registered = 1;
} }
if (tcgetattr(fd,&orig_termios) == -1) goto fatal; if (tcgetattr(fd,&orig_termios) == -1) goto fatal;
raw = orig_termios; /* modify the original mode */ raw = orig_termios; /* modify the original mode */
/* input modes: no break, no CR to NL, no parity check, no strip char, /* input modes: no break, no CR to NL, no parity check, no strip char,
* no start/stop output control. */ * no start/stop output control. */
@ -249,22 +249,20 @@ static int enableRawMode(int fd) {
raw.c_cflag |= (CS8); raw.c_cflag |= (CS8);
/* local modes - choing off, canonical off, no extended functions, /* local modes - choing off, canonical off, no extended functions,
* no signal chars (^Z,^C) */ * no signal chars (^Z,^C) */
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
/* control chars - set return condition: min number of bytes and timer. /* control chars - set return condition: min number of bytes and timer.
* We want read to return every single byte, without timeout. */ * We want read to return every single byte, without timeout. */
raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */ raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
/* put terminal in raw mode */
/* put terminal in raw mode after flushing */ if (tcsetattr(fd,TCSANOW,&raw) < 0) goto fatal;
if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
rawmode = 1; rawmode = 1;
return 0; return 0;
fatal: fatal:
errno = ENOTTY; errno = ENOTTY;
return -1; return -1;
} }
static void disableRawMode(int fd) { void linenoiseDisableRawMode(int fd) {
/* Don't even check the return value as it's too late. */ /* Don't even check the return value as it's too late. */
if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1) if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
rawmode = 0; rawmode = 0;
@ -277,10 +275,8 @@ static int getCursorPosition(int ifd, int ofd) {
char buf[32]; char buf[32];
int cols, rows; int cols, rows;
unsigned int i = 0; unsigned int i = 0;
/* Report cursor location */ /* Report cursor location */
if (write(ofd, "\x1b[6n", 4) != 4) return -1; if (write(ofd, "\x1b[6n", 4) != 4) return -1;
/* Read the response: ESC [ rows ; cols R */ /* Read the response: ESC [ rows ; cols R */
while (i < sizeof(buf)-1) { while (i < sizeof(buf)-1) {
if (read(ifd,buf+i,1) != 1) break; if (read(ifd,buf+i,1) != 1) break;
@ -288,7 +284,6 @@ static int getCursorPosition(int ifd, int ofd) {
i++; i++;
} }
buf[i] = '\0'; buf[i] = '\0';
/* Parse it. */ /* Parse it. */
if (buf[0] != ESC || buf[1] != '[') return -1; if (buf[0] != ESC || buf[1] != '[') return -1;
if (sscanf(buf+2,"%d;%d",&rows,&cols) != 2) return -1; if (sscanf(buf+2,"%d;%d",&rows,&cols) != 2) return -1;
@ -299,20 +294,16 @@ static int getCursorPosition(int ifd, int ofd) {
* if it fails. */ * if it fails. */
static int getColumns(int ifd, int ofd) { static int getColumns(int ifd, int ofd) {
struct winsize ws; struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
/* ioctl() failed. Try to query the terminal itself. */ /* ioctl() failed. Try to query the terminal itself. */
int start, cols; int start, cols;
/* Get the initial position so we can restore it later. */ /* Get the initial position so we can restore it later. */
start = getCursorPosition(ifd,ofd); start = getCursorPosition(ifd,ofd);
if (start == -1) goto failed; if (start == -1) goto failed;
/* Go to right margin and get position. */ /* Go to right margin and get position. */
if (write(ofd,"\x1b[999C",6) != 6) goto failed; if (write(ofd,"\x1b[999C",6) != 6) goto failed;
cols = getCursorPosition(ifd,ofd); cols = getCursorPosition(ifd,ofd);
if (cols == -1) goto failed; if (cols == -1) goto failed;
/* Restore position. */ /* Restore position. */
if (cols > start) { if (cols > start) {
char seq[32]; char seq[32];
@ -325,7 +316,6 @@ static int getColumns(int ifd, int ofd) {
} else { } else {
return ws.ws_col; return ws.ws_col;
} }
failed: failed:
return 80; return 80;
} }
@ -365,18 +355,15 @@ static int completeLine(struct linenoiseState *ls) {
linenoiseCompletions lc = { 0, NULL }; linenoiseCompletions lc = { 0, NULL };
int nread, nwritten; int nread, nwritten;
char c = 0; char c = 0;
completionCallback(ls->buf,&lc); completionCallback(ls->buf,&lc);
if (lc.len == 0) { if (lc.len == 0) {
linenoiseBeep(); linenoiseBeep();
} else { } else {
size_t stop = 0, i = 0; size_t stop = 0, i = 0;
while(!stop) { while(!stop) {
/* Show completion or original buffer */ /* Show completion or original buffer */
if (i < lc.len) { if (i < lc.len) {
struct linenoiseState saved = *ls; struct linenoiseState saved = *ls;
ls->len = ls->pos = strlen(lc.cvec[i]); ls->len = ls->pos = strlen(lc.cvec[i]);
ls->buf = lc.cvec[i]; ls->buf = lc.cvec[i];
refreshLine(ls); refreshLine(ls);
@ -386,13 +373,11 @@ static int completeLine(struct linenoiseState *ls) {
} else { } else {
refreshLine(ls); refreshLine(ls);
} }
nread = read(ls->ifd,&c,1); nread = read(ls->ifd,&c,1);
if (nread <= 0) { if (nread <= 0) {
freeCompletions(&lc); freeCompletions(&lc);
return -1; return -1;
} }
switch(c) { switch(c) {
case 9: /* tab */ case 9: /* tab */
i = (i+1) % (lc.len+1); i = (i+1) % (lc.len+1);
@ -414,7 +399,6 @@ static int completeLine(struct linenoiseState *ls) {
} }
} }
} }
freeCompletions(&lc); freeCompletions(&lc);
return c; /* Return last read character */ return c; /* Return last read character */
} }
@ -443,7 +427,6 @@ void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *fn) {
void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) { void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
size_t len = strlen(str); size_t len = strlen(str);
char *copy, **cvec; char *copy, **cvec;
copy = malloc(len+1); copy = malloc(len+1);
if (copy == NULL) return; if (copy == NULL) return;
memcpy(copy,str,len+1); memcpy(copy,str,len+1);
@ -462,7 +445,7 @@ void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
* allocated string where we can append to. This is useful in order to * allocated string where we can append to. This is useful in order to
* write all the escape sequences in a buffer and flush them to the standard * write all the escape sequences in a buffer and flush them to the standard
* output in a single call, to avoid flickering effects. */ * output in a single call, to avoid flickering effects. */
struct abuf { struct abuf{
char *b; char *b;
int len; int len;
}; };
@ -474,7 +457,6 @@ static void abInit(struct abuf *ab) {
static void abAppend(struct abuf *ab, const char *s, int len) { static void abAppend(struct abuf *ab, const char *s, int len) {
char *new = realloc(ab->b,ab->len+len); char *new = realloc(ab->b,ab->len+len);
if (new == NULL) return; if (new == NULL) return;
memcpy(new+ab->len,s,len); memcpy(new+ab->len,s,len);
ab->b = new; ab->b = new;
@ -523,7 +505,6 @@ static void refreshSingleLine(struct linenoiseState *l) {
size_t len = l->len; size_t len = l->len;
size_t pos = l->pos; size_t pos = l->pos;
struct abuf ab; struct abuf ab;
while((plen+pos) >= l->cols) { while((plen+pos) >= l->cols) {
buf++; buf++;
len--; len--;
@ -532,7 +513,6 @@ static void refreshSingleLine(struct linenoiseState *l) {
while (plen+len > l->cols) { while (plen+len > l->cols) {
len--; len--;
} }
abInit(&ab); abInit(&ab);
/* Cursor to left edge */ /* Cursor to left edge */
snprintf(seq,64,"\r"); snprintf(seq,64,"\r");
@ -570,10 +550,8 @@ static void refreshMultiLine(struct linenoiseState *l) {
int old_rows = l->maxrows; int old_rows = l->maxrows;
int fd = l->ofd, j; int fd = l->ofd, j;
struct abuf ab; struct abuf ab;
/* Update maxrows if needed. */ /* Update maxrows if needed. */
if (rows > (int)l->maxrows) l->maxrows = rows; if (rows > (int)l->maxrows) l->maxrows = rows;
/* First step: clear all the lines used before. To do so start by /* First step: clear all the lines used before. To do so start by
* going to the last row. */ * going to the last row. */
abInit(&ab); abInit(&ab);
@ -582,19 +560,16 @@ static void refreshMultiLine(struct linenoiseState *l) {
snprintf(seq,64,"\x1b[%dB", old_rows-rpos); snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
/* Now for every row clear it, go up. */ /* Now for every row clear it, go up. */
for (j = 0; j < old_rows-1; j++) { for (j = 0; j < old_rows-1; j++) {
lndebug("clear+up"); lndebug("clear+up");
snprintf(seq,64,"\r\x1b[0K\x1b[1A"); snprintf(seq,64,"\r\x1b[0K\x1b[1A");
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
/* Clean the top line. */ /* Clean the top line. */
lndebug("clear"); lndebug("clear");
snprintf(seq,64,"\r\x1b[0K"); snprintf(seq,64,"\r\x1b[0K");
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
/* Write the prompt and the current buffer content */ /* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt)); abAppend(&ab,l->prompt,strlen(l->prompt));
if (maskmode == 1) { if (maskmode == 1) {
@ -603,10 +578,8 @@ static void refreshMultiLine(struct linenoiseState *l) {
} else { } else {
abAppend(&ab,l->buf,l->len); abAppend(&ab,l->buf,l->len);
} }
/* Show hits if any. */ /* Show hits if any. */
refreshShowHints(&ab,l,plen); refreshShowHints(&ab,l,plen);
/* If we are at the very end of the screen with our prompt, we need to /* If we are at the very end of the screen with our prompt, we need to
* emit a newline and move the prompt to the first column. */ * emit a newline and move the prompt to the first column. */
if (l->pos && if (l->pos &&
@ -620,18 +593,15 @@ static void refreshMultiLine(struct linenoiseState *l) {
rows++; rows++;
if (rows > (int)l->maxrows) l->maxrows = rows; if (rows > (int)l->maxrows) l->maxrows = rows;
} }
/* Move cursor to right position. */ /* Move cursor to right position. */
rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */ rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
lndebug("rpos2 %d", rpos2); lndebug("rpos2 %d", rpos2);
/* Go up till we reach the expected positon. */ /* Go up till we reach the expected positon. */
if (rows-rpos2 > 0) { if (rows-rpos2 > 0) {
lndebug("go-up %d", rows-rpos2); lndebug("go-up %d", rows-rpos2);
snprintf(seq,64,"\x1b[%dA", rows-rpos2); snprintf(seq,64,"\x1b[%dA", rows-rpos2);
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
} }
/* Set column. */ /* Set column. */
col = (plen+(int)l->pos) % (int)l->cols; col = (plen+(int)l->pos) % (int)l->cols;
lndebug("set col %d", 1+col); lndebug("set col %d", 1+col);
@ -640,10 +610,8 @@ static void refreshMultiLine(struct linenoiseState *l) {
else else
snprintf(seq,64,"\r"); snprintf(seq,64,"\r");
abAppend(&ab,seq,strlen(seq)); abAppend(&ab,seq,strlen(seq));
lndebug("\n"); lndebug("\n");
l->oldpos = l->pos; l->oldpos = l->pos;
if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */ if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
abFree(&ab); abFree(&ab);
} }
@ -772,7 +740,6 @@ void linenoiseEditBackspace(struct linenoiseState *l) {
void linenoiseEditDeletePrevWord(struct linenoiseState *l) { void linenoiseEditDeletePrevWord(struct linenoiseState *l) {
size_t old_pos = l->pos; size_t old_pos = l->pos;
size_t diff; size_t diff;
while (l->pos > 0 && l->buf[l->pos-1] == ' ') while (l->pos > 0 && l->buf[l->pos-1] == ' ')
l->pos--; l->pos--;
while (l->pos > 0 && l->buf[l->pos-1] != ' ') while (l->pos > 0 && l->buf[l->pos-1] != ' ')
@ -791,10 +758,8 @@ void linenoiseEditDeletePrevWord(struct linenoiseState *l) {
* when ctrl+d is typed. * when ctrl+d is typed.
* *
* The function returns the length of the current buffer. */ * The function returns the length of the current buffer. */
static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) {
{
struct linenoiseState l; struct linenoiseState l;
/* Populate the linenoise state that we pass to functions implementing /* Populate the linenoise state that we pass to functions implementing
* specific editing functionalities. */ * specific editing functionalities. */
l.ifd = stdin_fd; l.ifd = stdin_fd;
@ -808,24 +773,19 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
l.cols = getColumns(stdin_fd, stdout_fd); l.cols = getColumns(stdin_fd, stdout_fd);
l.maxrows = 0; l.maxrows = 0;
l.history_index = 0; l.history_index = 0;
/* Buffer starts empty. */ /* Buffer starts empty. */
l.buf[0] = '\0'; l.buf[0] = '\0';
l.buflen--; /* Make sure there is always space for the nulterm */ l.buflen--; /* Make sure there is always space for the nulterm */
/* The latest history entry is always our current buffer, that /* The latest history entry is always our current buffer, that
* initially is just an empty string. */ * initially is just an empty string. */
linenoiseHistoryAdd(""); linenoiseHistoryAdd("");
if (write(l.ofd,prompt,l.plen) == -1) return -1; if (write(l.ofd,prompt,l.plen) == -1) return -1;
while(1) { while(1) {
char c; char c;
int nread; int nread;
char seq[3]; char seq[3];
nread = read(l.ifd,&c,1); nread = read(l.ifd,&c,1);
if (nread <= 0) return l.len; if (nread <= 0) return l.len;
/* Only autocomplete when the callback is set. It returns < 0 when /* Only autocomplete when the callback is set. It returns < 0 when
* there was an error reading from fd. Otherwise it will return the * there was an error reading from fd. Otherwise it will return the
* character that should be handled next. */ * character that should be handled next. */
@ -836,7 +796,6 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
/* Read next character when 0 */ /* Read next character when 0 */
if (c == 0) continue; if (c == 0) continue;
} }
switch(c) { switch(c) {
case ENTER: /* enter */ case ENTER: /* enter */
history_len--; history_len--;
@ -895,7 +854,6 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
* chars at different times. */ * chars at different times. */
if (read(l.ifd,seq,1) == -1) break; if (read(l.ifd,seq,1) == -1) break;
if (read(l.ifd,seq+1,1) == -1) break; if (read(l.ifd,seq+1,1) == -1) break;
/* ESC [ sequences. */ /* ESC [ sequences. */
if (seq[0] == '[') { if (seq[0] == '[') {
if (seq[1] >= '0' && seq[1] <= '9') { if (seq[1] >= '0' && seq[1] <= '9') {
@ -931,7 +889,6 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
} }
} }
} }
/* ESC O sequences. */ /* ESC O sequences. */
else if (seq[0] == 'O') { else if (seq[0] == 'O') {
switch(seq[1]) { switch(seq[1]) {
@ -980,7 +937,6 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
* by the linenoise_example program using the --keycodes option. */ * by the linenoise_example program using the --keycodes option. */
void linenoisePrintKeyCodes(void) { void linenoisePrintKeyCodes(void) {
char quit[4]; char quit[4];
printf("Linenoise key codes debugging mode.\n" printf("Linenoise key codes debugging mode.\n"
"Press keys to see scan codes. Type 'quit' at any time to exit.\n"); "Press keys to see scan codes. Type 'quit' at any time to exit.\n");
if (enableRawMode(STDIN_FILENO) == -1) return; if (enableRawMode(STDIN_FILENO) == -1) return;
@ -988,34 +944,30 @@ void linenoisePrintKeyCodes(void) {
while(1) { while(1) {
char c; char c;
int nread; int nread;
nread = read(STDIN_FILENO,&c,1); nread = read(STDIN_FILENO,&c,1);
if (nread <= 0) continue; if (nread <= 0) continue;
memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */ memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */
quit[sizeof(quit)-1] = c; /* Insert current char on the right. */ quit[sizeof(quit)-1] = c; /* Insert current char on the right. */
if (memcmp(quit,"quit",sizeof(quit)) == 0) break; if (memcmp(quit,"quit",sizeof(quit)) == 0) break;
printf("'%c' %02x (%d) (type quit to exit)\n", printf("'%c' %02x (%d) (type quit to exit)\n",
isprint(c) ? c : '?', (int)c, (int)c); isprint(c) ? c : '?', (int)c, (int)c);
printf("\r"); /* Go left edge manually, we are in raw mode. */ printf("\r"); /* Go left edge manually, we are in raw mode. */
fflush(stdout); fflush(stdout);
} }
disableRawMode(STDIN_FILENO); linenoiseDisableRawMode(STDIN_FILENO);
} }
/* This function calls the line editing function linenoiseEdit() using /* This function calls the line editing function linenoiseEdit() using
* the STDIN file descriptor set in raw mode. */ * the STDIN file descriptor set in raw mode. */
static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) { static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
int count; int count;
if (buflen == 0) { if (buflen == 0) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
if (enableRawMode(STDIN_FILENO) == -1) return -1; if (enableRawMode(STDIN_FILENO) == -1) return -1;
count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt); count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt);
disableRawMode(STDIN_FILENO); linenoiseDisableRawMode(STDIN_FILENO);
printf("\n"); printf("\n");
return count; return count;
} }
@ -1028,7 +980,6 @@ static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
static char *linenoiseNoTTY(void) { static char *linenoiseNoTTY(void) {
char *line = NULL; char *line = NULL;
size_t len = 0, maxlen = 0; size_t len = 0, maxlen = 0;
while(1) { while(1) {
if (len == maxlen) { if (len == maxlen) {
if (maxlen == 0) maxlen = 16; if (maxlen == 0) maxlen = 16;
@ -1062,26 +1013,18 @@ static char *linenoiseNoTTY(void) {
* editing function or uses dummy fgets() so that you will be able to type * editing function or uses dummy fgets() so that you will be able to type
* something even in the most desperate of the conditions. */ * something even in the most desperate of the conditions. */
char *linenoise(const char *prompt) { char *linenoise(const char *prompt) {
char buf[LINENOISE_MAX_LINE];
int count; int count;
char *buf;
if (!isatty(STDIN_FILENO)) { if (!isatty(STDIN_FILENO)) {
/* Not a tty: read from file / pipe. In this mode we don't want any /* Not a tty: read from file / pipe. In this mode we don't want any
* limit to the line size, so we call a function to handle that. */ * limit to the line size, so we call a function to handle that. */
return linenoiseNoTTY(); return linenoiseNoTTY();
} else if (isUnsupportedTerm()) { } else if (isUnsupportedTerm()) {
size_t len;
printf("%s",prompt); printf("%s",prompt);
fflush(stdout); fflush(stdout);
if (fgets(buf,LINENOISE_MAX_LINE,stdin) == NULL) return NULL; return chomp(xgetline(stdin));
len = strlen(buf);
while(len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
len--;
buf[len] = '\0';
}
return strdup(buf);
} else { } else {
buf = malloc(LINENOISE_MAX_LINE);
count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt); count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt);
if (count == -1) return NULL; if (count == -1) return NULL;
return strdup(buf); return strdup(buf);
@ -1103,7 +1046,6 @@ void linenoiseFree(void *ptr) {
static void freeHistory(void) { static void freeHistory(void) {
if (history) { if (history) {
int j; int j;
for (j = 0; j < history_len; j++) for (j = 0; j < history_len; j++)
free(history[j]); free(history[j]);
free(history); free(history);
@ -1112,7 +1054,7 @@ static void freeHistory(void) {
/* At exit we'll try to fix the terminal to the initial conditions. */ /* At exit we'll try to fix the terminal to the initial conditions. */
static void linenoiseAtExit(void) { static void linenoiseAtExit(void) {
disableRawMode(STDIN_FILENO); linenoiseDisableRawMode(STDIN_FILENO);
freeHistory(); freeHistory();
} }
@ -1125,19 +1067,15 @@ static void linenoiseAtExit(void) {
* Using a circular buffer is smarter, but a bit more complex to handle. */ * Using a circular buffer is smarter, but a bit more complex to handle. */
int linenoiseHistoryAdd(const char *line) { int linenoiseHistoryAdd(const char *line) {
char *linecopy; char *linecopy;
if (history_max_len == 0) return 0; if (history_max_len == 0) return 0;
/* Initialization on first call. */ /* Initialization on first call. */
if (history == NULL) { if (history == NULL) {
history = malloc(sizeof(char*)*history_max_len); history = malloc(sizeof(char*)*history_max_len);
if (history == NULL) return 0; if (history == NULL) return 0;
memset(history,0,(sizeof(char*)*history_max_len)); memset(history,0,(sizeof(char*)*history_max_len));
} }
/* Don't add duplicated lines. */ /* Don't add duplicated lines. */
if (history_len && !strcmp(history[history_len-1], line)) return 0; if (history_len && !strcmp(history[history_len-1], line)) return 0;
/* Add an heap allocated copy of the line in the history. /* Add an heap allocated copy of the line in the history.
* If we reached the max length, remove the older line. */ * If we reached the max length, remove the older line. */
linecopy = strdup(line); linecopy = strdup(line);
@ -1158,18 +1096,14 @@ int linenoiseHistoryAdd(const char *line) {
* than the amount of items already inside the history. */ * than the amount of items already inside the history. */
int linenoiseHistorySetMaxLen(int len) { int linenoiseHistorySetMaxLen(int len) {
char **new; char **new;
if (len < 1) return 0; if (len < 1) return 0;
if (history) { if (history) {
int tocopy = history_len; int tocopy = history_len;
new = malloc(sizeof(char*)*len); new = malloc(sizeof(char*)*len);
if (new == NULL) return 0; if (new == NULL) return 0;
/* If we can't copy everything, free the elements we'll not use. */ /* If we can't copy everything, free the elements we'll not use. */
if (len < tocopy) { if (len < tocopy) {
int j; int j;
for (j = 0; j < tocopy-len; j++) free(history[j]); for (j = 0; j < tocopy-len; j++) free(history[j]);
tocopy = len; tocopy = len;
} }
@ -1190,7 +1124,6 @@ int linenoiseHistorySave(const char *filename) {
mode_t old_umask = umask(S_IXUSR|S_IRWXG|S_IRWXO); mode_t old_umask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
FILE *fp; FILE *fp;
int j; int j;
fp = fopen(filename,"w"); fp = fopen(filename,"w");
umask(old_umask); umask(old_umask);
if (fp == NULL) return -1; if (fp == NULL) return -1;
@ -1209,12 +1142,9 @@ int linenoiseHistorySave(const char *filename) {
int linenoiseHistoryLoad(const char *filename) { int linenoiseHistoryLoad(const char *filename) {
FILE *fp = fopen(filename,"r"); FILE *fp = fopen(filename,"r");
char buf[LINENOISE_MAX_LINE]; char buf[LINENOISE_MAX_LINE];
if (fp == NULL) return -1; if (fp == NULL) return -1;
while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) { while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) {
char *p; char *p;
p = strchr(buf,'\r'); p = strchr(buf,'\r');
if (!p) p = strchr(buf,'\n'); if (!p) p = strchr(buf,'\n');
if (p) *p = '\0'; if (p) *p = '\0';

View file

@ -1,47 +1,7 @@
/* linenoise.h -- VERSION 1.0
*
* Guerrilla line editing library against the idea that a line editing lib
* needs to be 20,000 lines of C code.
*
* See linenoise.c for more information.
*
* ------------------------------------------------------------------------
*
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __LINENOISE_H #ifndef __LINENOISE_H
#define __LINENOISE_H #define __LINENOISE_H
COSMOPOLITAN_C_START_
#ifdef __cplusplus /* clang-format off */
extern "C" {
#endif
typedef struct linenoiseCompletions { typedef struct linenoiseCompletions {
size_t len; size_t len;
@ -49,27 +9,26 @@ typedef struct linenoiseCompletions {
} linenoiseCompletions; } linenoiseCompletions;
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *); typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold); typedef char*(linenoiseHintsCallback)(const char *, int *, int *);
typedef void(linenoiseFreeHintsCallback)(void *); typedef void(linenoiseFreeHintsCallback)(void *);
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *); void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
void linenoiseSetHintsCallback(linenoiseHintsCallback *); void linenoiseSetHintsCallback(linenoiseHintsCallback *);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *); void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *); void linenoiseAddCompletion(linenoiseCompletions *, const char *);
char *linenoise(const char *prompt); char *linenoise(const char *);
void linenoiseFree(void *ptr); void linenoiseFree(void *);
int linenoiseHistoryAdd(const char *line); int linenoiseHistoryAdd(const char *);
int linenoiseHistorySetMaxLen(int len); int linenoiseHistorySetMaxLen(int);
int linenoiseHistorySave(const char *filename); int linenoiseHistorySave(const char *);
int linenoiseHistoryLoad(const char *filename); int linenoiseHistoryLoad(const char *);
void linenoiseClearScreen(void); void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml); void linenoiseSetMultiLine(int );
void linenoisePrintKeyCodes(void); void linenoisePrintKeyCodes(void);
void linenoiseMaskModeEnable(void); void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(void); void linenoiseMaskModeDisable(void);
void linenoiseDisableRawMode(int);
#ifdef __cplusplus COSMOPOLITAN_C_END_
}
#endif
#endif /* __LINENOISE_H */ #endif /* __LINENOISE_H */

View file

@ -11,7 +11,7 @@ THIRD_PARTY_LINENOISE_A_HDRS = $(filter %.h,$(THIRD_PARTY_LINENOISE_A_FILES))
THIRD_PARTY_LINENOISE_A_SRCS = $(filter %.c,$(THIRD_PARTY_LINENOISE_A_FILES)) THIRD_PARTY_LINENOISE_A_SRCS = $(filter %.c,$(THIRD_PARTY_LINENOISE_A_FILES))
THIRD_PARTY_LINENOISE_A_OBJS = $(THIRD_PARTY_LINENOISE_A_SRCS:%.c=o/$(MODE)/%.o) THIRD_PARTY_LINENOISE_A_OBJS = $(THIRD_PARTY_LINENOISE_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_LINENOISE_A_CHECKS = \ THIRD_PARTY_LINENOISE_A_CHECKS = \
$(THIRD_PARTY_LINENOISE_A).pkg \ $(THIRD_PARTY_LINENOISE_A).pkg \
$(THIRD_PARTY_LINENOISE_A_HDRS:%=o/$(MODE)/%.ok) $(THIRD_PARTY_LINENOISE_A_HDRS:%=o/$(MODE)/%.ok)
@ -19,9 +19,13 @@ THIRD_PARTY_LINENOISE_A_DIRECTDEPS = \
LIBC_CALLS \ LIBC_CALLS \
LIBC_FMT \ LIBC_FMT \
LIBC_INTRIN \ LIBC_INTRIN \
LIBC_LOG \
LIBC_NEXGEN32E \ LIBC_NEXGEN32E \
LIBC_MEM \
LIBC_SYSV \
LIBC_STDIO \ LIBC_STDIO \
LIBC_RUNTIME \
LIBC_SYSV_CALLS \
LIBC_X \
LIBC_STR \ LIBC_STR \
LIBC_UNICODE \ LIBC_UNICODE \
LIBC_STUBS LIBC_STUBS

View file

@ -412,6 +412,14 @@ static int handle_luainit (lua_State *L) {
#define lua_saveline(L,line) ((void)L, add_history(line)) #define lua_saveline(L,line) ((void)L, add_history(line))
#define lua_freeline(L,b) ((void)L, free(b)) #define lua_freeline(L,b) ((void)L, free(b))
#elif defined(LUA_USE_LINENOISE)
#include "third_party/linenoise/linenoise.h"
#define lua_initreadline(L) ((void)L)
#define lua_readline(L,b,p) ((void)L, ((b)=linenoise(p)) != NULL)
#define lua_saveline(L,line) ((void)L, linenoiseHistoryAdd(line))
#define lua_freeline(L,b) ((void)L, free(b))
#else /* }{ */ #else /* }{ */
#define lua_initreadline(L) ((void)L) #define lua_initreadline(L) ((void)L)

View file

@ -34,6 +34,7 @@ THIRD_PARTY_LUA_DIRECTDEPS = \
LIBC_TIME \ LIBC_TIME \
LIBC_TINYMATH \ LIBC_TINYMATH \
LIBC_UNICODE \ LIBC_UNICODE \
THIRD_PARTY_LINENOISE \
THIRD_PARTY_GDTOA THIRD_PARTY_GDTOA
THIRD_PARTY_LUA_DEPS := \ THIRD_PARTY_LUA_DEPS := \

View file

@ -7,6 +7,7 @@
#include "third_party/gdtoa/gdtoa.h" #include "third_party/gdtoa/gdtoa.h"
#define LUA_USE_POSIX #define LUA_USE_POSIX
#define LUA_USE_LINENOISE
/* clang-format off */ /* clang-format off */

View file

@ -7,6 +7,7 @@
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/limits.h" #include "libc/limits.h"
#include "libc/log/log.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "third_party/python/Include/ceval.h" #include "third_party/python/Include/ceval.h"
#include "third_party/python/Include/intrcheck.h" #include "third_party/python/Include/intrcheck.h"
@ -40,9 +41,6 @@ int (*PyOS_InputHook)(void) = NULL;
static int static int
my_fgets(char *buf, int len, FILE *fp) my_fgets(char *buf, int len, FILE *fp)
{ {
#ifdef MS_WINDOWS
HANDLE hInterruptEvent;
#endif
char *p; char *p;
int err; int err;
while (1) { while (1) {
@ -54,31 +52,6 @@ my_fgets(char *buf, int len, FILE *fp)
if (p != NULL) if (p != NULL)
return 0; /* No error */ return 0; /* No error */
err = errno; err = errno;
#ifdef MS_WINDOWS
/* Ctrl-C anywhere on the line or Ctrl-Z if the only character
on a line will set ERROR_OPERATION_ABORTED. Under normal
circumstances Ctrl-C will also have caused the SIGINT handler
to fire which will have set the event object returned by
_PyOS_SigintEvent. This signal fires in another thread and
is not guaranteed to have occurred before this point in the
code.
Therefore: check whether the event is set with a small timeout.
If it is, assume this is a Ctrl-C and reset the event. If it
isn't set assume that this is a Ctrl-Z on its own and drop
through to check for EOF.
*/
if (GetLastError()==ERROR_OPERATION_ABORTED) {
hInterruptEvent = _PyOS_SigintEvent();
switch (WaitForSingleObjectEx(hInterruptEvent, 10, FALSE)) {
case WAIT_OBJECT_0:
ResetEvent(hInterruptEvent);
return 1; /* Interrupt */
case WAIT_FAILED:
return -2; /* Error */
}
}
#endif /* MS_WINDOWS */
if (feof(fp)) { if (feof(fp)) {
clearerr(fp); clearerr(fp);
return -1; /* EOF */ return -1; /* EOF */

View file

@ -4,15 +4,58 @@
Python 3 Python 3
https://docs.python.org/3/license.html │ https://docs.python.org/3/license.html │
*/ */
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/log/check.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/sig.h"
#include "libc/unicode/locale.h" #include "libc/unicode/locale.h"
#include "third_party/linenoise/linenoise.h"
#include "third_party/python/Include/fileutils.h" #include "third_party/python/Include/fileutils.h"
#include "third_party/python/Include/pylifecycle.h" #include "third_party/python/Include/pylifecycle.h"
#include "third_party/python/Include/pymem.h" #include "third_party/python/Include/pymem.h"
#include "third_party/python/Include/pyport.h" #include "third_party/python/Include/pyport.h"
#include "third_party/python/Include/pythonrun.h"
/* clang-format off */ /* clang-format off */
/* Minimal main program -- everything is loaded from the library */ static jmp_buf jbuf;
static void
OnKeyboardInterrupt(int sig)
{
longjmp(jbuf, 1);
}
char *
TerminalReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
{
size_t n;
char *p, *q;
PyOS_sighandler_t saint;
saint = PyOS_setsig(SIGINT, OnKeyboardInterrupt);
if (setjmp(jbuf)) {
linenoiseDisableRawMode(STDIN_FILENO);
PyOS_setsig(SIGINT, saint);
return NULL;
}
p = linenoise(prompt);
PyOS_setsig(SIGINT, saint);
if (p) {
if (*p) linenoiseHistoryAdd(p);
n = strlen(p);
q = PyMem_RawMalloc(n + 2);
strcpy(mempcpy(q, p, n), "\n");
free(p);
clearerr(sys_stdin);
return q;
} else {
q = PyMem_RawMalloc(1);
if (q) *q = 0;
return q;
}
}
int int
main(int argc, char **argv) main(int argc, char **argv)
@ -23,6 +66,8 @@ main(int argc, char **argv)
int i, res; int i, res;
char *oldloc; char *oldloc;
PyOS_ReadlineFunctionPointer = TerminalReadline;
/* Force malloc() allocator to bootstrap Python */ /* Force malloc() allocator to bootstrap Python */
(void)_PyMem_SetupAllocators("malloc"); (void)_PyMem_SetupAllocators("malloc");

View file

@ -418,6 +418,7 @@ THIRD_PARTY_PYTHON_A_DIRECTDEPS0 = \
LIBC_ZIPOS \ LIBC_ZIPOS \
THIRD_PARTY_GDTOA \ THIRD_PARTY_GDTOA \
THIRD_PARTY_GETOPT \ THIRD_PARTY_GETOPT \
THIRD_PARTY_LINENOISE \
THIRD_PARTY_MUSL \ THIRD_PARTY_MUSL \
THIRD_PARTY_ZLIB THIRD_PARTY_ZLIB

View file

@ -140,6 +140,7 @@ typedef unsigned char u8;
# define shell_readline(X) readline(X) # define shell_readline(X) readline(X)
#elif HAVE_LINENOISE #elif HAVE_LINENOISE
#include "third_party/linenoise/linenoise.h"
#define shell_add_history(X) linenoiseHistoryAdd(X) #define shell_add_history(X) linenoiseHistoryAdd(X)
#define shell_read_history(X) linenoiseHistoryLoad(X) #define shell_read_history(X) linenoiseHistoryLoad(X)

View file

@ -59,6 +59,7 @@ THIRD_PARTY_SQLITE3_A_DIRECTDEPS = \
LIBC_TINYMATH \ LIBC_TINYMATH \
LIBC_UNICODE \ LIBC_UNICODE \
THIRD_PARTY_GDTOA \ THIRD_PARTY_GDTOA \
THIRD_PARTY_LINENOISE \
THIRD_PARTY_MUSL \ THIRD_PARTY_MUSL \
THIRD_PARTY_ZLIB THIRD_PARTY_ZLIB
@ -150,7 +151,8 @@ $(THIRD_PARTY_SQLITE3_SHELL_OBJS): \
-DSQLITE_ENABLE_FTS5 \ -DSQLITE_ENABLE_FTS5 \
-DSQLITE_ENABLE_RTREE \ -DSQLITE_ENABLE_RTREE \
-DSQLITE_ENABLE_GEOPOLY \ -DSQLITE_ENABLE_GEOPOLY \
-DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_JSON1 \
-DHAVE_LINENOISE
o/$(MODE)/third_party/sqlite3/shell.shell.o: \ o/$(MODE)/third_party/sqlite3/shell.shell.o: \
OVERRIDE_CFLAGS += \ OVERRIDE_CFLAGS += \

View file

@ -8,15 +8,16 @@ o/$(MODE)/third_party: \
o/$(MODE)/third_party/dlmalloc \ o/$(MODE)/third_party/dlmalloc \
o/$(MODE)/third_party/gdtoa \ o/$(MODE)/third_party/gdtoa \
o/$(MODE)/third_party/getopt \ o/$(MODE)/third_party/getopt \
o/$(MODE)/third_party/infozip \
o/$(MODE)/third_party/linenoise \
o/$(MODE)/third_party/lua \ o/$(MODE)/third_party/lua \
o/$(MODE)/third_party/lz4cli \ o/$(MODE)/third_party/lz4cli \
o/$(MODE)/third_party/mbedtls \ o/$(MODE)/third_party/mbedtls \
o/$(MODE)/third_party/musl \ o/$(MODE)/third_party/musl \
o/$(MODE)/third_party/python \
o/$(MODE)/third_party/quickjs \ o/$(MODE)/third_party/quickjs \
o/$(MODE)/third_party/regex \ o/$(MODE)/third_party/regex \
o/$(MODE)/third_party/stb \
o/$(MODE)/third_party/sqlite3 \ o/$(MODE)/third_party/sqlite3 \
o/$(MODE)/third_party/stb \
o/$(MODE)/third_party/xed \ o/$(MODE)/third_party/xed \
o/$(MODE)/third_party/infozip \
o/$(MODE)/third_party/python \
o/$(MODE)/third_party/zlib o/$(MODE)/third_party/zlib