Line editing with linenoise.

https://github.com/antirez/linenoise
https://todo.sr.ht/~sforman/thun-der/33
This commit is contained in:
Simon Forman 2023-02-20 10:20:40 -08:00
parent d8263e0527
commit b32a3f2496
5 changed files with 1309 additions and 21 deletions

Binary file not shown.

View File

@ -1,4 +1,4 @@
OBJS=joy.o keywords.o definitions.o
OBJS=joy.o keywords.o definitions.o linenoise.o
LDFLAGS=-L/usr/local/lib -lgc -lgmp
@ -42,3 +42,6 @@ keywords.c: KEYWORDS.txt
clean:
rm -vf *.o joy KEYWORDS.txt
linenoise.o: linenoise.c linenoise.h
cc -c linenoise.c -o linenoise.o

View File

@ -37,6 +37,7 @@ along with Thun. If not see <http://www.gnu.org/licenses/>.
#include "joy.h"
#include "definitions.h"
#include "uthash.h"
#include "linenoise.h"
static jmp_buf jbuf;
@ -924,7 +925,6 @@ int
main(int argc, char *argv[])
{
char *line;
char *status;
JoyList stack = EMPTY_LIST;
JoyList expression = EMPTY_LIST;
JoyList s;
@ -939,29 +939,13 @@ main(int argc, char *argv[])
quiet = ((2 == argc) && (!strcmp("-q", argv[1])));
line = (char *)GC_malloc(1025);
while (1) {
SHH("joy? ")
status = gets_s(line, 1025);
if (NULL == status) {
/*
From the man page:
> Upon successful completion, fgets(), gets_s(), and gets() return a
pointer to the string. If end-of-file occurs before any characters are
read, they return NULL and the buffer contents remain unchanged. If an
error occurs, they return NULL and the buffer contents are indeterminate.
The fgets(), gets_s(), and gets() functions do not distinguish between
end-of-file and error, and callers must use feof(3) and ferror(3) to
determine which occurred.
TODO: "use feof(3) and ferror(3)"...
*/
line = linenoise(quiet? "" : "joy? ");
if (NULL == line) {
SHH("\n")
break;
}
linenoiseHistoryAdd(line);
s = stack;
if (!setjmp(jbuf)) {
expression = text_to_expression(line);
@ -970,6 +954,7 @@ main(int argc, char *argv[])
/* err */
stack = s;
}
free(line);
print_stack(stack);
printf("\n");
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,75 @@
/* 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
#define __LINENOISE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
typedef void(linenoiseFreeHintsCallback)(void *);
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
void linenoiseSetHintsCallback(linenoiseHintsCallback *);
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
char *linenoise(const char *prompt);
void linenoiseFree(void *ptr);
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(void);
#ifdef __cplusplus
}
#endif
#endif /* __LINENOISE_H */