some cool things

This commit is contained in:
2026-06-25 18:57:53 +10:00
parent fa2c205f33
commit 9bb083a6f8
19 changed files with 4288 additions and 1 deletions

View File

@@ -439,6 +439,15 @@ struct _Ground {
GroundValue* (*get) (GroundBytecodeHeap* heap, GroundSize idx);
} Heap;
} Bytecode;
struct {
char* (*Arg)(GroundArg* arg);
char* (*Heap)(GroundBytecodeHeap* heap);
char* (*Instruction)(GroundInstruction* instruction);
char* (*Program)(GroundProgram* program);
char* (*String)(GroundString* string);
char* (*Value)(GroundValue* value);
} Stringify;
};
extern struct _Ground Ground;

View File

@@ -98,7 +98,17 @@ sources = files(
'src/State/findLabel.c',
'src/State/findVariable.c',
'src/Struct/addField.c'
'src/Stringify/Arg.c',
'src/Stringify/Heap.c',
'src/Stringify/Instruction.c',
'src/Stringify/Program.c',
'src/Stringify/String.c',
'src/Stringify/Value.c',
'src/Struct/addField.c',
'src/linenoise/linenoise.c'
)
cli_sources = files(

View File

@@ -1,4 +1,5 @@
#include "../../../include/ground.h"
#include "../../linenoise/linenoise.h"
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
@@ -65,6 +66,9 @@ int64_t _GroundBytecodeInstructionExecute(GroundBytecodeInstruction* instruction
return -2;
}
INPUT: {
char* input = linenoise("");
HEAP_SET(heap, instruction->args.at[0], Ground.New.Value.String(Ground.New.String(input)));
free(input);
return -1;
}
PRINT: {

45
src/Stringify/Arg.c Normal file
View File

@@ -0,0 +1,45 @@
#include "../../include/ground.h"
char* _GroundStringifyArg(GroundArg* arg) {
if (arg->type == GroundArg_Value) {
return Ground.Stringify.Value(&arg->as.value);
} else {
char* buf = malloc(strlen(arg->as.ref->string) + 2); // add 2 for sigil + null byte
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Arg");
return NULL;
}
switch (arg->type) {
case GroundArg_ValueRef: {
buf[0] = '$';
break;
}
case GroundArg_DirectRef: {
buf[0] = '&';
break;
}
case GroundArg_LineRef: {
buf[0] = '%';
break;
}
case GroundArg_Label: {
buf[0] = '@';
break;
}
case GroundArg_FunctionRef: {
buf[0] = '!';
break;
}
case GroundArg_TypeRef: {
buf[0] = '-';
break;
}
case GroundArg_Value: {
break;
}
}
memcpy(buf + 1, arg->as.ref->string, strlen(arg->as.ref->string) + 1);
return buf;
}
}

29
src/Stringify/Heap.c Normal file
View File

@@ -0,0 +1,29 @@
#include "../../include/ground.h"
#include "../include/estr.h"
#include <inttypes.h>
char* _GroundStringifyHeap(GroundBytecodeHeap* heap) {
Estr heapString = CREATE_ESTR("");
for (GroundSize i = 0; i < heap->len; i++) {
char* string = Ground.Stringify.Value(&heap->heap[i]);
if (Ground.Flags.error) {
return NULL;
}
if (string == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("Ground.Stringify.Value unexpectedly returned NULL in Ground.Stringify.Heap");
return NULL;
}
char* buf = malloc(snprintf(NULL, 0, "#%" PRIu64 ": %s\n", i, string) + 1);
sprintf(buf, "#%" PRIu64 ": %s\n", i, string);
APPEND_ESTR(heapString, buf);
free(string);
}
return heapString.str;
}

298
src/Stringify/Instruction.c Normal file
View File

@@ -0,0 +1,298 @@
#include "../../include/ground.h"
#include "../include/estr.h"
char* _GroundStringifyInstruction(GroundInstruction* instruction) {
Estr estr;
switch (instruction->type) {
case GroundInstruction_IF: {
estr = CREATE_ESTR("IF ");
break;
}
case GroundInstruction_JUMP: {
estr = CREATE_ESTR("JUMP ");
break;
}
case GroundInstruction_END: {
estr = CREATE_ESTR("END ");
break;
}
case GroundInstruction_INPUT: {
estr = CREATE_ESTR("INPUT ");
break;
}
case GroundInstruction_PRINT: {
estr = CREATE_ESTR("PRINT ");
break;
}
case GroundInstruction_PRINTLN: {
estr = CREATE_ESTR("PRINTLN ");
break;
}
case GroundInstruction_SET: {
estr = CREATE_ESTR("SET ");
break;
}
case GroundInstruction_GETTYPE: {
estr = CREATE_ESTR("GETTYPE ");
break;
}
case GroundInstruction_EXISTS: {
estr = CREATE_ESTR("EXISTS ");
break;
}
case GroundInstruction_SETLIST: {
estr = CREATE_ESTR("SETLIST ");
break;
}
case GroundInstruction_SETLISTAT: {
estr = CREATE_ESTR("SETLISTAT ");
break;
}
case GroundInstruction_GETLISTAT: {
estr = CREATE_ESTR("GETLISTAT ");
break;
}
case GroundInstruction_GETLISTSIZE: {
estr = CREATE_ESTR("GETLISTSIZE ");
break;
}
case GroundInstruction_LISTAPPEND: {
estr = CREATE_ESTR("LISTAPPEND ");
break;
}
case GroundInstruction_GETSTRSIZE: {
estr = CREATE_ESTR("GETSTRSIZE ");
break;
}
case GroundInstruction_GETSTRCHARAT: {
estr = CREATE_ESTR("GETSTRCHARAT ");
break;
}
case GroundInstruction_ADD: {
estr = CREATE_ESTR("ADD ");
break;
}
case GroundInstruction_SUBTRACT: {
estr = CREATE_ESTR("SUBTRACT ");
break;
}
case GroundInstruction_MULTIPLY: {
estr = CREATE_ESTR("MULTIPLY ");
break;
}
case GroundInstruction_DIVIDE: {
estr = CREATE_ESTR("DIVIDE ");
break;
}
case GroundInstruction_EQUAL: {
estr = CREATE_ESTR("EQUAL ");
break;
}
case GroundInstruction_INEQUAL: {
estr = CREATE_ESTR("INEQUAL ");
break;
}
case GroundInstruction_NOT: {
estr = CREATE_ESTR("NOT ");
break;
}
case GroundInstruction_GREATER: {
estr = CREATE_ESTR("GREATER ");
break;
}
case GroundInstruction_LESSER: {
estr = CREATE_ESTR("LESSER ");
break;
}
case GroundInstruction_AND: {
estr = CREATE_ESTR("AND ");
break;
}
case GroundInstruction_OR: {
estr = CREATE_ESTR("OR ");
break;
}
case GroundInstruction_XOR: {
estr = CREATE_ESTR("XOR ");
break;
}
case GroundInstruction_NEG: {
estr = CREATE_ESTR("NEG ");
break;
}
case GroundInstruction_SHIFT: {
estr = CREATE_ESTR("SHIFT ");
break;
}
case GroundInstruction_STOI: {
estr = CREATE_ESTR("STOI ");
break;
}
case GroundInstruction_STOD: {
estr = CREATE_ESTR("STOD ");
break;
}
case GroundInstruction_ITOC: {
estr = CREATE_ESTR("ITOC ");
break;
}
case GroundInstruction_CTOI: {
estr = CREATE_ESTR("CTOI ");
break;
}
case GroundInstruction_TOSTRING: {
estr = CREATE_ESTR("TOSTRING ");
break;
}
case GroundInstruction_FUN: {
estr = CREATE_ESTR("FUN ");
break;
}
case GroundInstruction_RETURN: {
estr = CREATE_ESTR("RETURN ");
break;
}
case GroundInstruction_ENDFUN: {
estr = CREATE_ESTR("ENDFUN ");
break;
}
case GroundInstruction_CALL: {
estr = CREATE_ESTR("CALL ");
break;
}
case GroundInstruction_CALLMETHOD: {
estr = CREATE_ESTR("CALLMETHOD ");
break;
}
case GroundInstruction_STRUCT: {
estr = CREATE_ESTR("STRUCT ");
break;
}
case GroundInstruction_ENDSTRUCT: {
estr = CREATE_ESTR("ENDSTRUCT ");
break;
}
case GroundInstruction_INIT: {
estr = CREATE_ESTR("INIT ");
break;
}
case GroundInstruction_GETFIELD: {
estr = CREATE_ESTR("GETFIELD ");
break;
}
case GroundInstruction_SETFIELD: {
estr = CREATE_ESTR("SETFIELD ");
break;
}
case GroundInstruction_USE: {
estr = CREATE_ESTR("USE ");
break;
}
case GroundInstruction_EXTERN: {
estr = CREATE_ESTR("EXTERN ");
break;
}
case GroundInstruction_CREATELABEL: {
estr = CREATE_ESTR("CREATELABEL ");
break;
}
case GroundInstruction_PAUSE: {
estr = CREATE_ESTR("PAUSE ");
break;
}
case GroundInstruction_DROP: {
estr = CREATE_ESTR("DROP ");
break;
}
case GroundInstruction_LICENSE: {
estr = CREATE_ESTR("LICENSE ");
break;
}
case GroundInstruction_ERROR: {
estr = CREATE_ESTR("ERROR ");
break;
}
case GroundInstruction_THROW: {
estr = CREATE_ESTR("THROW ");
break;
}
case GroundInstruction_CATCH: {
estr = CREATE_ESTR("CATCH ");
break;
}
}
for (GroundSize i = 0; i < instruction->args.len; i++) {
char* arg = Ground.Stringify.Arg(&instruction->args.at[i]);
if (Ground.Flags.error) {
DESTROY_ESTR(estr);
return NULL;
}
if (arg == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("Ground.Stringify.Arg unexpectedly returned NULL in Ground.Stringify.Instruction");
DESTROY_ESTR(estr);
return NULL;
}
APPEND_ESTR(estr, arg);
free(arg);
}
return estr.str;
}

25
src/Stringify/Program.c Normal file
View File

@@ -0,0 +1,25 @@
#include "../../include/ground.h"
#include "../include/estr.h"
char* _GroundStringifyProgram(GroundProgram* program) {
Estr estr = CREATE_ESTR("");
for (size_t i = 0; i < program->len; i++) {
char* arg = Ground.Stringify.Instruction(&program->at[i]);
if (Ground.Flags.error) {
DESTROY_ESTR(estr);
return NULL;
}
if (arg == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("Ground.Stringify.Instruction unexpectedly returned null in Ground.Stringify.Program");
return NULL;
}
APPEND_ESTR(estr, arg);
APPEND_ESTR(estr, " ");
free(arg);
}
return estr.str;
}

12
src/Stringify/String.c Normal file
View File

@@ -0,0 +1,12 @@
#include "../../include/ground.h"
char* _GroundStringifyString(GroundString* string) {
char* out = malloc(string->len + 1);
if (out == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.String");
return NULL;
}
memcpy(out, string->cstr, string->len + 1);
return out;
}

67
src/Stringify/Value.c Normal file
View File

@@ -0,0 +1,67 @@
#include "../../include/ground.h"
#include <inttypes.h>
char* _GroundStringifyValue(GroundValue* value) {
switch (value->type.type) {
case GroundType_Int: {
char* buf = malloc(snprintf(NULL, 0, "%" PRId64, value->as.Int) + 1);
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "%" PRId64, value->as.Int);
return buf;
}
case GroundType_Double: {
char* buf = malloc(snprintf(NULL, 0, "%f", value->as.Double) + 1);
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "%f", value->as.Double);
return buf;
}
case GroundType_Char: {
char* buf = malloc(snprintf(NULL, 0, "%c", value->as.Char) + 1);
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, "%c", value->as.Char);
return buf;
}
case GroundType_Bool: {
char* buf = malloc(6); // max(len(true), len(false)) + 1
if (buf == NULL) {
Ground.Flags.error = true;
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
return NULL;
}
sprintf(buf, value->as.Bool ? "true" : "false");
return buf;
}
case GroundType_String: {
return Ground.Stringify.String(&value->as.String);
}
case GroundType_List: {
// TODO implement list stringification
}
case GroundType_Function: {
// TODO implement function stringification
}
case GroundType_Struct: {
// TODO implement struct stringification
}
case GroundType_Object: {
// TODO implement object stringification
}
}
Ground.Flags.error = true;
Ground.Log.Error("FIXME implement all cases in Ground.Stringify.Value");
return NULL;
}

52
src/include/estr.h Normal file
View File

@@ -0,0 +1,52 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef ESTR_H
#define ESTR_H
/*
estr.h - Easy string manipulation
This library has macros to allow easier manipulation of strings. No longer shall
you have to malloc and realloc away to keep adding to your strings.
Usage:
Estr myString = CREATE_ESTR("my awesome string");
APPEND_ESTR(myString, " is so cool");
printf("%s\n", myString.str);
*/
#define CREATE_ESTR(instr) \
(Estr) { \
.str = instr,\
.size = strlen(instr),\
.shouldBeFreed = 0, \
.destroyed = 0 \
}
#define APPEND_ESTR(estr, instr) { \
estr.size = estr.size + strlen(instr); \
char* tmp_ptr = malloc(estr.size + 1); \
if (tmp_ptr == NULL) printf("WARNING: Could not realloc estr " #estr "\n"); \
else { \
snprintf(tmp_ptr, estr.size + 1, "%s%s", estr.str, instr); \
if (estr.shouldBeFreed > 0) free(estr.str); \
estr.shouldBeFreed = 1; \
estr.str = tmp_ptr; \
} \
}
#define DESTROY_ESTR(estr) if (estr.shouldBeFreed > 0 && estr.destroyed < 1) free(estr.str);
typedef struct Estr {
char* str;
size_t size;
int8_t shouldBeFreed;
int8_t destroyed;
} Estr;
#endif // ESTR_H

View File

@@ -107,6 +107,14 @@ void _GroundBytecodeHeapSet(GroundBytecodeHeap* heap, GroundSize idx, GroundValu
GroundValue* _GroundBytecodeHeapGet(GroundBytecodeHeap* heap, GroundSize idx);
char* _GroundStringifyArg(GroundArg* arg);
char* _GroundStringifyHeap(GroundBytecodeHeap* heap);
char* _GroundStringifyInstruction(GroundInstruction* instruction);
char* _GroundStringifyProgram(GroundProgram* program);
char* _GroundStringifyString(GroundString* string);
char* _GroundStringifyValue(GroundValue* value);
struct _Ground Ground = {
.Flags = {
@@ -240,4 +248,13 @@ struct _Ground Ground = {
.get = _GroundBytecodeHeapGet,
},
},
.Stringify = {
.Arg = _GroundStringifyArg,
.Heap = _GroundStringifyHeap,
.Instruction = _GroundStringifyInstruction,
.Program = _GroundStringifyProgram,
.String = _GroundStringifyString,
.Value = _GroundStringifyValue,
},
};

4
src/linenoise/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
linenoise-example
linenoise-test
*.dSYM
history.txt

25
src/linenoise/LICENSE Normal file
View File

@@ -0,0 +1,25 @@
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 OWNER 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.

13
src/linenoise/Makefile Normal file
View File

@@ -0,0 +1,13 @@
all: linenoise-example linenoise-test
linenoise-example: linenoise.h linenoise.c example.c
$(CC) -Wall -W -Os -g -o linenoise-example linenoise.c example.c
linenoise-test: linenoise-test.c linenoise-example
$(CC) -Wall -W -Os -g -o linenoise-test linenoise-test.c
test: linenoise-test linenoise-example
./linenoise-test
clean:
rm -f linenoise-example linenoise-test

View File

@@ -0,0 +1,380 @@
# Linenoise
A minimal, zero-config, BSD licensed, readline replacement used in Redis,
MongoDB, Android and many other projects.
* Single and multi line editing mode with the usual key bindings implemented.
* History handling.
* Completion.
* Hints (suggestions at the right of the prompt as you type).
* Multiplexing mode, with prompt hiding/restoring for asynchronous output.
* UTF-8 support for multi-byte characters and emoji.
* About ~1100 lines (comments and spaces excluded) of BSD license source code.
* Only uses a subset of VT100 escapes (ANSI.SYS compatible).
## Can a line editing library be 20k lines of code?
Line editing with some support for history is a really important feature for command line utilities. Instead of retyping almost the same stuff again and again it's just much better to hit the up arrow and edit on syntax errors, or in order to try a slightly different command. But apparently code dealing with terminals is some sort of Black Magic: readline is 30k lines of code, libedit 20k. Is it reasonable to link small utilities to huge libraries just to get a minimal support for line editing?
So what usually happens is either:
* Large programs with configure scripts disabling line editing if readline is not present in the system, or not supporting it at all since readline is GPL licensed and libedit (the BSD clone) is not as known and available as readline is (real world example of this problem: Tclsh).
* Smaller programs not using a configure script not supporting line editing at all (A problem we had with `redis-cli`, for instance).
The result is a pollution of binaries without line editing support.
So I spent more or less two hours doing a reality check resulting in this little library: is it *really* needed for a line editing library to be 20k lines of code? Apparently not, it is possibe to get a very small, zero configuration, trivial to embed library, that solves the problem. Smaller programs will just include this, supporting line editing out of the box. Larger programs may use this little library or just checking with configure if readline/libedit is available and resorting to Linenoise if not.
## Terminals, in 2010.
Apparently almost every terminal you can happen to use today has some kind of support for basic VT100 escape sequences. So I tried to write a lib using just very basic VT100 features. The resulting library appears to work everywhere I tried to use it, and now can work even on ANSI.SYS compatible terminals, since no
VT220 specific sequences are used anymore.
The library is currently about 850 lines of code. In order to use it in your project just look at the *example.c* file in the source distribution, it is pretty straightforward. The library supports both a blocking mode and a multiplexing mode, see the API documentation later in this file for more information.
Linenoise is BSD-licensed code, so you can use both in free software and commercial software.
## Tested with...
* Linux text only console ($TERM = linux)
* Linux KDE terminal application ($TERM = xterm)
* Linux xterm ($TERM = xterm)
* Linux Buildroot ($TERM = vt100)
* Mac OS X iTerm ($TERM = xterm)
* Mac OS X default Terminal.app ($TERM = xterm)
* OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen)
* IBM AIX 6.1
* FreeBSD xterm ($TERM = xterm)
* ANSI.SYS
* Emacs comint mode ($TERM = dumb)
Please test it everywhere you can and report back!
## Let's push this forward!
Patches should be provided in the respect of Linenoise sensibility for small
easy to understand code.
Send feedbacks to antirez at gmail
# The API
Linenoise is very easy to use, and reading the example shipped with the
library should get you up to speed ASAP. Here is a list of API calls
and how to use them. Let's start with the simple blocking mode:
char *linenoise(const char *prompt);
This is the main Linenoise call: it shows the user a prompt with line editing
and history capabilities. The prompt you specify is used as a prompt, that is,
it will be printed to the left of the cursor. The library returns a buffer
with the line composed by the user, or NULL on end of file or when there
is an out of memory condition.
When a tty is detected (the user is actually typing into a terminal session)
the maximum editable line length is `LINENOISE_MAX_LINE`. When instead the
standard input is not a tty, which happens every time you redirect a file
to a program, or use it in an Unix pipeline, there are no limits to the
length of the line that can be returned.
The returned line should be freed with the `free()` standard system call.
However sometimes it could happen that your program uses a different dynamic
allocation library, so you may also used `linenoiseFree` to make sure the
line is freed with the same allocator it was created.
The canonical loop used by a program using Linenoise will be something like
this:
while((line = linenoise("hello> ")) != NULL) {
printf("You wrote: %s\n", line);
linenoiseFree(line); /* Or just free(line) if you use libc malloc. */
}
## Single line VS multi line editing
By default, Linenoise uses single line editing, that is, a single row on the
screen will be used, and as the user types more, the text will scroll towards
left to make room. This works if your program is one where the user is
unlikely to write a lot of text, otherwise multi line editing, where multiple
screens rows are used, can be a lot more comfortable.
In order to enable multi line editing use the following API call:
linenoiseSetMultiLine(1);
You can disable it using `0` as argument.
## History
Linenoise supporst history, so that the user does not have to retype
again and again the same things, but can use the down and up arrows in order
to search and re-edit already inserted lines of text.
The followings are the history API calls:
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
Use `linenoiseHistoryAdd` every time you want to add a new element
to the top of the history (it will be the first the user will see when
using the up arrow).
Note that for history to work, you have to set a length for the history
(which is zero by default, so history will be disabled if you don't set
a proper one). This is accomplished using the `linenoiseHistorySetMaxLen`
function.
Linenoise has direct support for persisting the history into an history
file. The functions `linenoiseHistorySave` and `linenoiseHistoryLoad` do
just that. Both functions return -1 on error and 0 on success.
## Mask mode
Sometimes it is useful to allow the user to type passwords or other
secrets that should not be displayed. For such situations linenoise supports
a "mask mode" that will just replace the characters the user is typing
with `*` characters, like in the following example:
$ ./linenoise_example
hello> get mykey
echo: 'get mykey'
hello> /mask
hello> *********
You can enable and disable mask mode using the following two functions:
void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(void);
## Completion
Linenoise supports completion, which is the ability to complete the user
input when she or he presses the `<TAB>` key.
In order to use completion, you need to register a completion callback, which
is called every time the user presses `<TAB>`. Your callback will return a
list of items that are completions for the current string.
The following is an example of registering a completion callback:
linenoiseSetCompletionCallback(completion);
The completion must be a function returning `void` and getting as input
a `const char` pointer, which is the line the user has typed so far, and
a `linenoiseCompletions` object pointer, which is used as argument of
`linenoiseAddCompletion` in order to add completions inside the callback.
An example will make it more clear:
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
}
}
Basically in your completion callback, you inspect the input, and return
a list of items that are good completions by using `linenoiseAddCompletion`.
If you want to test the completion feature, compile the example program
with `make`, run it, type `h` and press `<TAB>`.
## Hints
Linenoise has a feature called *hints* which is very useful when you
use Linenoise in order to implement a REPL (Read Eval Print Loop) for
a program that accepts commands and arguments, but may also be useful in
other conditions.
The feature shows, on the right of the cursor, as the user types, hints that
may be useful. The hints can be displayed using a different color compared
to the color the user is typing, and can also be bold.
For example as the user starts to type `"git remote add"`, with hints it's
possible to show on the right of the prompt a string `<name> <url>`.
The feature works similarly to the history feature, using a callback.
To register the callback we use:
linenoiseSetHintsCallback(hints);
The callback itself is implemented like this:
char *hints(const char *buf, int *color, int *bold) {
if (!strcasecmp(buf,"git remote add")) {
*color = 35;
*bold = 0;
return " <name> <url>";
}
return NULL;
}
The callback function returns the string that should be displayed or NULL
if no hint is available for the text the user currently typed. The returned
string will be trimmed as needed depending on the number of columns available
on the screen.
It is possible to return a string allocated in dynamic way, by also registering
a function to deallocate the hint string once used:
void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
The free hint callback will just receive the pointer and free the string
as needed (depending on how the hits callback allocated it).
As you can see in the example above, a `color` (in xterm color terminal codes)
can be provided together with a `bold` attribute. If no color is set, the
current terminal foreground color is used. If no bold attribute is set,
non-bold text is printed.
Color codes are:
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
white = 37;
## Screen handling
Sometimes you may want to clear the screen as a result of something the
user typed. You can do this by calling the following function:
void linenoiseClearScreen(void);
## Asyncrhronous API
Sometimes you want to read from the keyboard but also from sockets or other
external events, and at the same time there could be input to display to the
user *while* the user is typing something. Let's call this the "IRC problem",
since if you want to write an IRC client with linenoise, without using
some fully featured libcurses approach, you will surely end having such an
issue.
Fortunately now a multiplexing friendly API exists, and it is just what the
blocking calls internally use. To start, we need to initialize a linenoise
context like this:
struct linenoiseState ls;
char buf[1024];
linenoiseEditStart(&ls,-1,-1,buf,sizeof(buf),"some prompt> ");
The two -1 and -1 arguments are the stdin/out descriptors. If they are
set to -1, linenoise will just use the default stdin/out file descriptors.
Now as soon as we have data from stdin (and we know it via select(2) or
some other way), we can ask linenoise to read the next character with:
linenoiseEditFeed(&ls);
The function returns a `char` pointer: if the user didn't yet press enter
to provide a line to the program, it will return `linenoiseEditMore`, that
means we need to call `linenoiseEditFeed()` again when more data is
available. If the function returns non NULL, then this is a heap allocated
data (to be freed with `linenoiseFree()`) representing the user input.
When the function returns NULL, than the user pressed CTRL-C or CTRL-D
with an empty line, to quit the program, or there was some I/O error.
After each line is received (or if you want to quit the program, and exit raw mode), the following function needs to be called:
linenoiseEditStop(&ls);
To start reading the next line, a new linenoiseEditStart() must
be called, in order to reset the state, and so forth, so a typical event
handler called when the standard input is readable, will work similarly
to the example below:
``` c
void stdinHasSomeData(void) {
char *line = linenoiseEditFeed(&LineNoiseState);
if (line == linenoiseEditMore) return;
linenoiseEditStop(&LineNoiseState);
if (line == NULL) exit(0);
printf("line: %s\n", line);
linenoiseFree(line);
linenoiseEditStart(&LineNoiseState,-1,-1,LineNoiseBuffer,sizeof(LineNoiseBuffer),"serial> ");
}
```
Now that we have a way to avoid blocking in the user input, we can use
two calls to hide/show the edited line, so that it is possible to also
show some input that we received (from socekts, bluetooth, whatever) on
screen:
linenoiseHide(&ls);
printf("some data...\n");
linenoiseShow(&ls);
To the API calls, the linenoise example C file implements a multiplexing
example using select(2) and the asynchronous API:
```c
struct linenoiseState ls;
char buf[1024];
linenoiseEditStart(&ls,-1,-1,buf,sizeof(buf),"hello> ");
while(1) {
// Select(2) setup code removed...
retval = select(ls.ifd+1, &readfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
exit(1);
} else if (retval) {
line = linenoiseEditFeed(&ls);
/* A NULL return means: line editing is continuing.
* Otherwise the user hit enter or stopped editing
* (CTRL+C/D). */
if (line != linenoiseEditMore) break;
} else {
// Timeout occurred
static int counter = 0;
linenoiseHide(&ls);
printf("Async output %d.\n", counter++);
linenoiseShow(&ls);
}
}
linenoiseEditStop(&ls);
if (line == NULL) exit(0); /* Ctrl+D/C. */
```
You can test the example by running the example program with the `--async` option.
## Running the tests
To run the test suite:
make test
The tests will display a virtual terminal showing linenoise output in real-time, making it easy to see what's being tested and debug any failures.
### What the tests cover
The test suite verifies:
* Basic typing and cursor movement (left, right, home, end)
* Backspace and delete operations
* UTF-8 multi-byte characters (accented letters, CJK)
* Emoji and grapheme clusters (skin tones, ZWJ sequences like flags)
* Horizontal scrolling for long lines
* Multiline mode editing and navigation
* History navigation in multiline mode
* Word and line deletion (Ctrl-W, Ctrl-U)
### How the test harness works
The test program (`linenoise-test.c`) implements a VT100 terminal emulator that captures and verifies linenoise output:
1. **Fork and pipes**: The test harness forks `linenoise-example`, connecting to it via pipes. The child process sees `LINENOISE_ASSUME_TTY=1` to enable terminal mode despite not having a real TTY.
2. **VT100 emulator**: A minimal VT100 emulator parses escape sequences (cursor movement, screen clearing, etc.) and maintains a virtual screen buffer. Each cell stores a complete UTF-8 grapheme cluster and its display width.
3. **Visual rendering**: After each operation, the virtual screen is rendered to your real terminal with a border, so you can watch the test execute and see exactly what linenoise is displaying.
4. **Assertions**: Tests verify screen contents and cursor position against expected values.
This approach tests linenoise as users actually experience it, catching rendering bugs that unit tests would miss.
## Related projects
* [Linenoise NG](https://github.com/arangodb/linenoise-ng) is a fork of Linenoise that aims to add more advanced features like Windows support and other features. Uses C++ instead of C as development language.
* [Linenoise-swift](https://github.com/andybest/linenoise-swift) is a reimplementation of Linenoise written in Swift.

124
src/linenoise/example.c Normal file
View File

@@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include "linenoise.h"
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
}
}
char *hints(const char *buf, int *color, int *bold) {
if (!strcasecmp(buf,"hello")) {
*color = 35;
*bold = 0;
return " World";
}
return NULL;
}
int main(int argc, char **argv) {
char *line;
char *prgname = argv[0];
int async = 0;
/* Parse options, with --multiline we enable multi line editing. */
while(argc > 1) {
argc--;
argv++;
if (!strcmp(*argv,"--multiline")) {
linenoiseSetMultiLine(1);
printf("Multi-line mode enabled.\n");
} else if (!strcmp(*argv,"--keycodes")) {
linenoisePrintKeyCodes();
exit(0);
} else if (!strcmp(*argv,"--async")) {
async = 1;
} else {
fprintf(stderr, "Usage: %s [--multiline] [--keycodes] [--async]\n", prgname);
exit(1);
}
}
/* Set the completion callback. This will be called every time the
* user uses the <tab> key. */
linenoiseSetCompletionCallback(completion);
linenoiseSetHintsCallback(hints);
/* Load history from file. The history file is just a plain text file
* where entries are separated by newlines. */
linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
/* Now this is the main loop of the typical linenoise-based application.
* The call to linenoise() will block as long as the user types something
* and presses enter.
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
while(1) {
if (!async) {
line = linenoise("hello> ");
if (line == NULL) break;
} else {
/* Asynchronous mode using the multiplexing API: wait for
* data on stdin, and simulate async data coming from some source
* using the select(2) timeout. */
struct linenoiseState ls;
char buf[1024];
linenoiseEditStart(&ls,-1,-1,buf,sizeof(buf),"hello> ");
while(1) {
fd_set readfds;
struct timeval tv;
int retval;
FD_ZERO(&readfds);
FD_SET(ls.ifd, &readfds);
tv.tv_sec = 1; // 1 sec timeout
tv.tv_usec = 0;
retval = select(ls.ifd+1, &readfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
exit(1);
} else if (retval) {
line = linenoiseEditFeed(&ls);
/* A NULL return means: line editing is continuing.
* Otherwise the user hit enter or stopped editing
* (CTRL+C/D). */
if (line != linenoiseEditMore) break;
} else {
// Timeout occurred
static int counter = 0;
linenoiseHide(&ls);
printf("Async output %d.\n", counter++);
linenoiseShow(&ls);
}
}
linenoiseEditStop(&ls);
if (line == NULL) exit(0); /* Ctrl+D/C. */
}
/* Do something with the string. */
if (line[0] != '\0' && line[0] != '/') {
printf("echo: '%s'\n", line);
linenoiseHistoryAdd(line); /* Add to the history. */
linenoiseHistorySave("history.txt"); /* Save the history on disk. */
} else if (!strncmp(line,"/historylen",11)) {
/* The "/historylen" command will change the history len. */
int len = atoi(line+11);
linenoiseHistorySetMaxLen(len);
} else if (!strncmp(line, "/mask", 5)) {
linenoiseMaskModeEnable();
} else if (!strncmp(line, "/unmask", 7)) {
linenoiseMaskModeDisable();
} else if (line[0] == '/') {
printf("Unreconized command: %s\n", line);
}
free(line);
}
return 0;
}

File diff suppressed because it is too large Load Diff

1762
src/linenoise/linenoise.c Normal file

File diff suppressed because it is too large Load Diff

114
src/linenoise/linenoise.h Normal file
View File

@@ -0,0 +1,114 @@
/* 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-2023, 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
#include <stddef.h> /* For size_t. */
extern char *linenoiseEditMore;
/* The linenoiseState structure represents the state during line editing.
* We pass this state to functions implementing specific editing
* functionalities. */
struct linenoiseState {
int in_completion; /* The user pressed TAB and we are now in completion
* mode, so input is handled by completeLine(). */
size_t completion_idx; /* Index of next completion to propose. */
int ifd; /* Terminal stdin file descriptor. */
int ofd; /* Terminal stdout file descriptor. */
char *buf; /* Edited line buffer. */
size_t buflen; /* Edited line buffer size. */
const char *prompt; /* Prompt to display. */
size_t plen; /* Prompt length. */
size_t pos; /* Current cursor position. */
size_t oldpos; /* Previous refresh cursor position. */
size_t len; /* Current edited line length. */
size_t cols; /* Number of columns in terminal. */
size_t oldrows; /* Rows used by last refrehsed line (multiline mode) */
int oldrpos; /* Cursor row from last refresh (for multiline clearing). */
int history_index; /* The history index we are currently editing. */
};
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
/* Non blocking API. */
int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);
char *linenoiseEditFeed(struct linenoiseState *l);
void linenoiseEditStop(struct linenoiseState *l);
void linenoiseHide(struct linenoiseState *l);
void linenoiseShow(struct linenoiseState *l);
/* Blocking API. */
char *linenoise(const char *prompt);
void linenoiseFree(void *ptr);
/* Completion API. */
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 *);
/* History API. */
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
/* Other utilities. */
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
void linenoiseMaskModeEnable(void);
void linenoiseMaskModeDisable(void);
#ifdef __cplusplus
}
#endif
#endif /* __LINENOISE_H */