Skip to content

Commit

Permalink
shell: Switch from linenoise to bestline. Bestline has history search…
Browse files Browse the repository at this point in the history
… and other improvements. Replace `PICOL_SHELL_LINENOISE` with generic `PICOL_SHELL_LINE_EDIT` in case of future changes. Rename the binary to `picol-line-edit`.
  • Loading branch information
dbohdan committed Aug 8, 2024
1 parent 3f63734 commit 9d224db
Show file tree
Hide file tree
Showing 8 changed files with 3,745 additions and 1,352 deletions.
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
CFLAGS ?= -Wall -Wextra
PREFIX ?= /usr/local

all: picolsh picolsh-big-stack picolsh-linenoise
all: picolsh picolsh-big-stack picolsh-line-edit

picolsh: shell.c picol.h
$(CC) shell.c -o $@ $(CFLAGS) -DPICOL_SHELL_LINENOISE=0
$(CC) shell.c -o $@ $(CFLAGS) -DPICOL_SHELL_LINE_EDIT=0
picolsh-big-stack: shell.c picol.h
$(CC) shell.c -o $@ $(CFLAGS) -DPICOL_SHELL_LINENOISE=0 -DPICOL_SMALL_STACK=0
picolsh-linenoise: shell.c picol.h vendor/linenoise.o
$(CC) vendor/linenoise.o shell.c -o $@ $(CFLAGS)
$(CC) shell.c -o $@ $(CFLAGS) -DPICOL_SHELL_LINE_EDIT=0 -DPICOL_SMALL_STACK=0
picolsh-line-edit: shell.c picol.h vendor/bestline.o
$(CC) vendor/bestline.o shell.c -o $@ $(CFLAGS)

test: picolsh picolsh-big-stack
./picolsh test.pcl
./picolsh-big-stack test.pcl

vendor/linenoise.o: vendor/linenoise.h vendor/linenoise.c
$(CC) -c vendor/linenoise.c -o $@ $(CFLAGS)
vendor/bestline.o: vendor/bestline.h vendor/bestline.c
$(CC) -c vendor/bestline.c -o $@ $(CFLAGS)

examples: examples/command examples/hello examples/privdata examples/regexp-ext
examples/command: examples/command.c picol.h
Expand All @@ -34,20 +34,20 @@ examples-test: examples
./examples/regexp-ext

clean:
-rm -f picolsh picolsh-big-stack picolsh-linenoise picolsh.exe shell.obj
-rm -f picolsh picolsh-big-stack picolsh-line-edit picolsh.exe shell.obj
-rm -f examples/command examples/command.exe command.obj
-rm -f examples/hello examples/hello.exe hello.obj
-rm -f examples/privdata examples/privdata.exe privdata.obj
-rm -f examples/regexp-ext examples/regexp-ext.exe regexp-ext.obj
-rm -f vendor/linenoise.o
-rm -f vendor/bestline.o

install: install-bin install-include
install-bin: picolsh-linenoise
install-bin: picolsh-line-edit
install $< $(DESTDIR)$(PREFIX)/bin/picolsh
install-include: picol.h
install -m 0644 $< $(DESTDIR)$(PREFIX)/include

upx: all
upx -9 picolsh picolsh-big-stack picolsh-linenoise
upx -9 picolsh picolsh-big-stack picolsh-line-edit

.PHONY: all clean examples examples-test install install-bin install-include test upx
29 changes: 13 additions & 16 deletions shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
#include "extensions/regexp-wrapper.h"

#if PICOL_TCL_PLATFORM_PLATFORM == PICOL_TCL_PLATFORM_UNIX
#ifndef PICOL_SHELL_LINENOISE
#define PICOL_SHELL_LINENOISE 1
#include "vendor/linenoise.h"
#ifndef PICOL_SHELL_LINE_EDIT
#define PICOL_SHELL_LINE_EDIT 1
#include "vendor/bestline.h"
#endif
#else
#define PICOL_SHELL_LINENOISE 0
#define PICOL_SHELL_LINE_EDIT 0
#endif

/* --- Configuration --- */

/* History is currently only available on *nix. */
#define PICOL_SHELL_HISTORY_FILE ".picolsh_history"
#define PICOL_SHELL_HISTORY_LEN 1000

#define PICOL_SHELL_INIT_FILE_UNIX ".picolshrc"
#define PICOL_SHELL_INIT_FILE_WINDOWS "picolshrc.pcl"
Expand Down Expand Up @@ -82,7 +81,7 @@ int main(int argc, char** argv) {
values. */
picolEval(interp, "array set env {}");
if (argc == 1) { /* No arguments - interactive mode. */
#if PICOL_SHELL_LINENOISE
#if PICOL_SHELL_LINE_EDIT
char history_file_path[PICOL_MAX_STR] = "";
#endif

Expand All @@ -100,28 +99,26 @@ int main(int argc, char** argv) {
interp->current = NULL; /* Prevent a misleading error traceback. */
}

#if PICOL_SHELL_LINENOISE
linenoiseSetMultiLine(1);
linenoiseHistorySetMaxLen(PICOL_SHELL_HISTORY_LEN);
#if PICOL_SHELL_LINE_EDIT
rc = home_dir_path(
history_file_path,
sizeof(history_file_path),
"/" PICOL_SHELL_HISTORY_FILE
);
if (rc == PICOL_OK) {
linenoiseHistoryLoad(history_file_path);
bestlineHistoryLoad(history_file_path);
}
#endif

while (1) {
#if PICOL_SHELL_LINENOISE
char* line = linenoise(PICOL_SHELL_PROMPT);
#if PICOL_SHELL_LINE_EDIT
char* line = bestline(PICOL_SHELL_PROMPT);
if (line == NULL) {
break;
}
strncpy(buf, line, sizeof(buf));
linenoiseHistoryAdd(buf);
linenoiseFree(line);
bestlineHistoryAdd(buf);
bestlineFree(line);
#else
printf(PICOL_SHELL_PROMPT);
fflush(stdout);
Expand All @@ -136,8 +133,8 @@ int main(int argc, char** argv) {
}
}

#if PICOL_SHELL_LINENOISE
linenoiseHistorySave(history_file_path);
#if PICOL_SHELL_LINE_EDIT
bestlineHistorySave(history_file_path);
#endif
} else if (argc == 3 && PICOL_EQ(argv[1], "-e")) { /* A script in argv[2]. */
set_interp_argv(interp, 1, argc, argv);
Expand Down
Loading

0 comments on commit 9d224db

Please sign in to comment.