mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-24 14:22:28 +00:00
Ues linenoise in Lua, Python, and SQLite
This commit is contained in:
parent
fe29710e4e
commit
1e5bd4d23e
14 changed files with 230 additions and 303 deletions
29
third_party/python/Parser/myreadline.c
vendored
29
third_party/python/Parser/myreadline.c
vendored
|
@ -7,6 +7,7 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "third_party/python/Include/ceval.h"
|
||||
#include "third_party/python/Include/intrcheck.h"
|
||||
|
@ -40,9 +41,6 @@ int (*PyOS_InputHook)(void) = NULL;
|
|||
static int
|
||||
my_fgets(char *buf, int len, FILE *fp)
|
||||
{
|
||||
#ifdef MS_WINDOWS
|
||||
HANDLE hInterruptEvent;
|
||||
#endif
|
||||
char *p;
|
||||
int err;
|
||||
while (1) {
|
||||
|
@ -54,31 +52,6 @@ my_fgets(char *buf, int len, FILE *fp)
|
|||
if (p != NULL)
|
||||
return 0; /* No error */
|
||||
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)) {
|
||||
clearerr(fp);
|
||||
return -1; /* EOF */
|
||||
|
|
47
third_party/python/Programs/python.c
vendored
47
third_party/python/Programs/python.c
vendored
|
@ -4,15 +4,58 @@
|
|||
│ Python 3 │
|
||||
│ 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/sysv/consts/fileno.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/unicode/locale.h"
|
||||
#include "third_party/linenoise/linenoise.h"
|
||||
#include "third_party/python/Include/fileutils.h"
|
||||
#include "third_party/python/Include/pylifecycle.h"
|
||||
#include "third_party/python/Include/pymem.h"
|
||||
#include "third_party/python/Include/pyport.h"
|
||||
#include "third_party/python/Include/pythonrun.h"
|
||||
/* 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
|
||||
main(int argc, char **argv)
|
||||
|
@ -23,6 +66,8 @@ main(int argc, char **argv)
|
|||
int i, res;
|
||||
char *oldloc;
|
||||
|
||||
PyOS_ReadlineFunctionPointer = TerminalReadline;
|
||||
|
||||
/* Force malloc() allocator to bootstrap Python */
|
||||
(void)_PyMem_SetupAllocators("malloc");
|
||||
|
||||
|
|
1
third_party/python/python.mk
vendored
1
third_party/python/python.mk
vendored
|
@ -418,6 +418,7 @@ THIRD_PARTY_PYTHON_A_DIRECTDEPS0 = \
|
|||
LIBC_ZIPOS \
|
||||
THIRD_PARTY_GDTOA \
|
||||
THIRD_PARTY_GETOPT \
|
||||
THIRD_PARTY_LINENOISE \
|
||||
THIRD_PARTY_MUSL \
|
||||
THIRD_PARTY_ZLIB
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue