Compare commits

...

10 Commits

Author SHA1 Message Date
wash 422416e99b frontend: replace builtin line-ed with bshell-line-editor 2026-06-20 15:18:51 +01:00
wash 9e6c3bb431 core: foreach-object: update variable definition api usage 2026-06-20 15:17:15 +01:00
wash ad8b284869 runtime: implement an extensible line editor
the line editor supports custom keybinds and data storage, allowing for
a wide range of functionality to be implemented via plugins.

included plugins include:
* core: basic input and cursor movement.
* history: input history storage and retrieval (unfinished)
* highlight: support for colour-highlighting the line buffer (unfinished)
* syntax: parses the line buffer and uses `highlight` to apply syntax highlighting (unfinished).
* shell-integration:
  - allows a `prompt` function to be defined, which will be called to get the text to
    be used for the prompt
  - exposes key properties of the line editor to the shell environment, allowing line editor
    functionality to be extended via shell scripts.
2026-06-20 15:13:34 +01:00
wash f622c2eba3 runtime: adjust formatting 2026-06-20 15:13:17 +01:00
wash f62713872f runtime: cmdcall: resolve() can now re-resolve an already-resolved cmdcall 2026-06-20 15:13:03 +01:00
wash 8b18421f63 runtime: parse: fix parsing of linefeed after last statement in block 2026-06-20 15:11:02 +01:00
wash 4a567a1bb9 runtime: format: add truncation and single-line support to value_writer 2026-06-20 15:10:15 +01:00
wash c262b53eab runtime: move variable definition from runtime to individual scopes 2026-06-20 15:09:10 +01:00
wash c2d3e05430 runtime: command: handle commants that don't have a source assembly 2026-06-20 15:07:20 +01:00
wash 4e6493da58 runtime: lex: fix incorrect state-transition when scanning func keyword 2026-06-20 15:06:04 +01:00
60 changed files with 2178 additions and 1368 deletions
+4 -1
View File
@@ -2,6 +2,7 @@
#include <bshell/command/command.h> #include <bshell/command/command.h>
#include <bshell/runtime/pipeline.h> #include <bshell/runtime/pipeline.h>
#include <bshell/runtime/runtime.h> #include <bshell/runtime/runtime.h>
#include <bshell/runtime/scope.h>
#include <bshell/runtime/script-block.h> #include <bshell/runtime/script-block.h>
#include <bshell/status.h> #include <bshell/status.h>
#include <fx/reflection/function.h> #include <fx/reflection/function.h>
@@ -51,7 +52,9 @@ static enum bshell_status process_record(
} }
bshell_runtime_push_scope(rt, p->f_block); bshell_runtime_push_scope(rt, p->f_block);
bshell_variable *item = bshell_runtime_define_var(rt, "_"); bshell_variable *item = bshell_runtime_scope_define_variable(
bshell_runtime_get_current_scope(rt),
"_");
bshell_variable_set_value(item, in); bshell_variable_set_value(item, in);
fx_value out = bshell_runtime_eval(rt); fx_value out = bshell_runtime_eval(rt);
bshell_runtime_pop_scope(rt); bshell_runtime_pop_scope(rt);
+1 -1
View File
@@ -1,6 +1,6 @@
set(source_dirs set(source_dirs
ast compile parse parse/lex parse/syntax ast compile parse parse/lex parse/syntax
format runtime command) format runtime command line-editor)
file(GLOB sources file(GLOB sources
*.c *.c
+10 -1
View File
@@ -112,6 +112,10 @@ static fx_status get_version(
fx_value_get_object(cmd_v, &cmd); fx_value_get_object(cmd_v, &cmd);
const fx_type *ty = fx_type_get_by_id(fx_object_query_type(cmd)); const fx_type *ty = fx_type_get_by_id(fx_object_query_type(cmd));
const fx_assembly *assembly = fx_type_get_assembly(ty); const fx_assembly *assembly = fx_type_get_assembly(ty);
if (!assembly) {
*out = FX_VALUE_EMPTY;
return FX_SUCCESS;
}
long major = 0, minor = 0, build = 0, revision = 0; long major = 0, minor = 0, build = 0, revision = 0;
fx_assembly_get_version(assembly, &major, &minor, &build, &revision); fx_assembly_get_version(assembly, &major, &minor, &build, &revision);
@@ -148,7 +152,12 @@ static fx_status get_source(
const fx_type *ty = fx_type_get_by_id(fx_object_query_type(cmd)); const fx_type *ty = fx_type_get_by_id(fx_object_query_type(cmd));
const fx_assembly *assembly = fx_type_get_assembly(ty); const fx_assembly *assembly = fx_type_get_assembly(ty);
*out = FX_CSTR(fx_assembly_get_name(assembly)); if (assembly) {
*out = FX_CSTR(fx_assembly_get_name(assembly));
} else {
*out = FX_VALUE_EMPTY;
}
return FX_SUCCESS; return FX_SUCCESS;
} }
+3 -2
View File
@@ -2,6 +2,7 @@
#include <bshell/command/function.h> #include <bshell/command/function.h>
#include <bshell/runtime/pipeline.h> #include <bshell/runtime/pipeline.h>
#include <bshell/runtime/runtime.h> #include <bshell/runtime/runtime.h>
#include <bshell/runtime/scope.h>
#include <bshell/runtime/script-block.h> #include <bshell/runtime/script-block.h>
#include <bshell/status.h> #include <bshell/status.h>
#include <fx/collections/array.h> #include <fx/collections/array.h>
@@ -72,8 +73,8 @@ static enum bshell_status process_record(
break; break;
} }
bshell_variable *var = bshell_runtime_define_var( bshell_variable *var = bshell_runtime_scope_define_variable(
rt, bshell_runtime_get_current_scope(rt),
fx_string_get_cstr(param)); fx_string_get_cstr(param));
bshell_variable_set_value(var, *arg); bshell_variable_set_value(var, *arg);
} }
+5 -1
View File
@@ -125,7 +125,11 @@ static enum bshell_status process_record(
fx_stream *in_stream = fx_process_get_stdin(cmd_p->cmd_proc); fx_stream *in_stream = fx_process_get_stdin(cmd_p->cmd_proc);
if (!cmd_p->cmd_writer_init) { if (!cmd_p->cmd_writer_init) {
bshell_value_writer_init(&cmd_p->cmd_writer, in_stream); bshell_value_writer_init(
&cmd_p->cmd_writer,
0,
0,
in_stream);
cmd_p->cmd_writer_init = true; cmd_p->cmd_writer_init = true;
} }
+1 -1
View File
@@ -22,7 +22,7 @@ static void format_value_fallback(const fx_value *value, fx_stream *dest)
enum bshell_status format_value_default(const fx_value *value, fx_stream *dest) enum bshell_status format_value_default(const fx_value *value, fx_stream *dest)
{ {
struct bshell_value_writer writer; struct bshell_value_writer writer;
bshell_value_writer_init(&writer, dest); bshell_value_writer_init(&writer, 0, 0, dest);
bshell_value_writer_write(&writer, value); bshell_value_writer_write(&writer, value);
bshell_value_writer_cleanup(&writer); bshell_value_writer_cleanup(&writer);
+29 -24
View File
@@ -167,7 +167,7 @@ enum bshell_status bshell_table_ctx_prepare_columns_from_format(
if (column_defs[i].col_width <= tty_width) { if (column_defs[i].col_width <= tty_width) {
columns[i].col_width = column_defs[i].col_width; columns[i].col_width = column_defs[i].col_width;
tty_width -= columns[i].col_width; tty_width -= (columns[i].col_width + 1);
continue; continue;
} }
@@ -289,6 +289,10 @@ enum bshell_status bshell_table_ctx_print_headers(struct bshell_table_ctx *ctx)
fx_stream_write_char(ctx->ctx_out, ' '); fx_stream_write_char(ctx->ctx_out, ' ');
} }
if (i < ctx->ctx_columns_count - 1) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
if (to_write != title_chars_remaining) { if (to_write != title_chars_remaining) {
repeat = true; repeat = true;
} }
@@ -316,6 +320,10 @@ enum bshell_status bshell_table_ctx_print_headers(struct bshell_table_ctx *ctx)
for (; x < c->col_width; x++) { for (; x < c->col_width; x++) {
fx_stream_write_char(ctx->ctx_out, ' '); fx_stream_write_char(ctx->ctx_out, ' ');
} }
if (i < ctx->ctx_columns_count - 1) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
} }
reset_tty_colour(); reset_tty_colour();
@@ -327,14 +335,22 @@ enum bshell_status bshell_table_ctx_print_record(
struct bshell_table_ctx *ctx, struct bshell_table_ctx *ctx,
const fx_value *record) const fx_value *record)
{ {
fx_stringstream *strm = fx_stringstream_create(); struct bshell_value_writer writer;
size_t written = 0;
for (size_t i = 0; i < ctx->ctx_columns_count; i++) { for (size_t i = 0; i < ctx->ctx_columns_count; i++) {
struct bshell_table_ctx_column *c = &ctx->ctx_columns[i]; struct bshell_table_ctx_column *c = &ctx->ctx_columns[i];
if (c->col_width == 0) { if (c->col_width == 0) {
continue; continue;
} }
fx_stringstream_reset(strm); bshell_value_writer_init(
&writer,
BSHELL_VALUE_WRITER_NO_NEWLINE
| BSHELL_VALUE_WRITER_ONELINE
| BSHELL_VALUE_WRITER_TRUNCATE,
c->col_width,
ctx->ctx_out);
fx_value column_value = FX_VALUE_EMPTY; fx_value column_value = FX_VALUE_EMPTY;
if (c->col_property) { if (c->col_property) {
fx_status status = fx_property_get_value( fx_status status = fx_property_get_value(
@@ -342,34 +358,23 @@ enum bshell_status bshell_table_ctx_print_record(
record, record,
&column_value); &column_value);
if (FX_OK(status)) { if (FX_OK(status)) {
fx_value_to_string(&column_value, strm, NULL); written = bshell_value_writer_write(
&writer,
&column_value);
} }
} }
size_t to_write = fx_stringstream_get_length(strm); for (size_t x = written; x < c->col_width; x++) {
const char *s = fx_stringstream_get_cstr(strm);
bool overflow = false;
if (to_write > c->col_width) {
to_write = c->col_width - 2;
overflow = true;
}
size_t x;
for (x = 0; x < to_write; x++) {
fx_stream_write_char(ctx->ctx_out, s[x]);
}
if (overflow) {
fx_stream_write_char(ctx->ctx_out, L'');
x++;
}
for (; x < c->col_width; x++) {
fx_stream_write_char(ctx->ctx_out, ' '); fx_stream_write_char(ctx->ctx_out, ' ');
} }
if (i < ctx->ctx_columns_count - 1) {
fx_stream_write_char(ctx->ctx_out, ' ');
}
bshell_value_writer_cleanup(&writer);
} }
fx_stream_write_char(ctx->ctx_out, '\n'); fx_stream_write_char(ctx->ctx_out, '\n');
fx_stringstream_unref(strm);
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
} }
+106 -20
View File
@@ -15,17 +15,22 @@ static void initialise_table(
w->w_table_initialised = true; w->w_table_initialised = true;
} }
static void write_iterable(struct bshell_value_writer *w, const fx_value *value) static size_t write_iterable(
struct bshell_value_writer *w,
const fx_value *value)
{ {
size_t written = 0;
fx_array *array = NULL; fx_array *array = NULL;
fx_value_get_object(value, &array); fx_value_get_object(value, &array);
const fx_iterator *it = fx_iterator_begin(array); const fx_iterator *it = fx_iterator_begin(array);
fx_foreach(v, it) fx_foreach(v, it)
{ {
bshell_value_writer_write(w, v); written += bshell_value_writer_write(w, v);
} }
fx_iterator_unref(it); fx_iterator_unref(it);
return written;
} }
static bool string_has_trailing_newline(const char *s) static bool string_has_trailing_newline(const char *s)
@@ -38,34 +43,105 @@ static bool string_has_trailing_newline(const char *s)
return s[len - 1] == '\n'; return s[len - 1] == '\n';
} }
static void write_string(struct bshell_value_writer *w, const fx_value *value) static size_t write_control_char(struct bshell_value_writer *w, fx_wchar c)
{ {
const char *str = NULL; bool oneline = (w->w_flags & BSHELL_VALUE_WRITER_ONELINE) != 0;
fx_value_get_cstr(value, &str); size_t written = 1;
fx_stream_write_cstr(w->w_dest, str, NULL);
if (!string_has_trailing_newline(str)) { switch (c) {
fx_stream_write_char(w->w_dest, '\n'); case '\n':
if (oneline) {
fx_stream_write_cstr(w->w_dest, "\\n", &written);
} else {
fx_stream_write_char(w->w_dest, '\n');
}
break;
case '\r':
fx_stream_write_cstr(w->w_dest, "\\r", NULL);
break;
case '\t':
fx_stream_write_cstr(w->w_dest, "\\t", NULL);
break;
case '\b':
fx_stream_write_cstr(w->w_dest, "\\b", NULL);
break;
default:
written = 0;
break;
} }
return written;
} }
static void write_value(struct bshell_value_writer *w, const fx_value *value) static size_t write_string(struct bshell_value_writer *w, const fx_value *value)
{
const char *s = NULL;
size_t written = 0;
fx_value_get_cstr(value, &s);
bool trailing_newline = string_has_trailing_newline(s);
bool inhibit_newline
= (w->w_flags & BSHELL_VALUE_WRITER_NO_NEWLINE) != 0;
bool truncate = (w->w_flags & BSHELL_VALUE_WRITER_TRUNCATE) != 0;
for (written = 0; *s;) {
if (truncate && written == w->w_max_width - 1) {
fx_stream_write_char(w->w_dest, L'');
written++;
break;
}
fx_wchar c = fx_wchar_utf8_codepoint_decode(s);
unsigned int stride = fx_wchar_utf8_codepoint_size(c);
if (stride == 0) {
break;
}
if (!fx_wchar_is_graph(c)) {
written += write_control_char(w, c);
goto next;
}
char cs[5] = {0};
fx_wchar_utf8_codepoint_encode(c, cs);
size_t n = 0;
fx_stream_write_cstr(w->w_dest, cs, &n);
written += n;
next:
s += stride;
}
if (!trailing_newline && !inhibit_newline) {
fx_stream_write_char(w->w_dest, '\n');
written++;
}
return written;
}
static size_t write_value(struct bshell_value_writer *w, const fx_value *value)
{ {
const fx_type *ty = fx_type_get_by_id(value->v_type); const fx_type *ty = fx_type_get_by_id(value->v_type);
if (fx_value_is_type(value, FX_TYPE_STRING)) { if (fx_value_is_type(value, FX_TYPE_STRING)
write_string(w, value); || fx_value_is_type(value, FX_TYPE_CSTR)) {
return; return write_string(w, value);
} }
if (fx_type_get_interface(ty, FX_TYPE_ITERABLE)) { if (fx_type_get_interface(ty, FX_TYPE_ITERABLE)) {
write_iterable(w, value); return write_iterable(w, value);
return;
} }
if (fx_type_is_value_type(value->v_type)) { if (fx_type_is_value_type(value->v_type)) {
fx_value_to_string(value, w->w_dest, NULL); fx_stringstream_reset(w->w_tmp);
fx_stream_write_char(w->w_dest, '\n'); fx_value_to_string(value, w->w_tmp, NULL);
return; fx_stream_write_char(w->w_tmp, '\n');
size_t result = fx_stringstream_get_length(w->w_tmp);
fx_stream_write_cstr(
w->w_dest,
fx_stringstream_get_cstr(w->w_tmp),
NULL);
return result;
} }
if (!w->w_table_initialised) { if (!w->w_table_initialised) {
@@ -73,19 +149,27 @@ static void write_value(struct bshell_value_writer *w, const fx_value *value)
} }
bshell_table_ctx_print_record(&w->w_table_ctx, value); bshell_table_ctx_print_record(&w->w_table_ctx, value);
return 0;
} }
void bshell_value_writer_init(struct bshell_value_writer *w, fx_stream *dest) void bshell_value_writer_init(
struct bshell_value_writer *w,
enum bshell_value_writer_flags flags,
size_t max_width,
fx_stream *dest)
{ {
memset(w, 0x00, sizeof *w); memset(w, 0x00, sizeof *w);
w->w_dest = dest; w->w_dest = dest;
w->w_flags = flags;
w->w_max_width = max_width;
w->w_tmp = fx_stringstream_create();
} }
void bshell_value_writer_write( size_t bshell_value_writer_write(
struct bshell_value_writer *w, struct bshell_value_writer *w,
const fx_value *value) const fx_value *value)
{ {
write_value(w, value); return write_value(w, value);
} }
void bshell_value_writer_cleanup(struct bshell_value_writer *w) void bshell_value_writer_cleanup(struct bshell_value_writer *w)
@@ -94,5 +178,7 @@ void bshell_value_writer_cleanup(struct bshell_value_writer *w)
bshell_table_ctx_cleanup(&w->w_table_ctx); bshell_table_ctx_cleanup(&w->w_table_ctx);
} }
fx_stringstream_unref(w->w_tmp);
w->w_table_initialised = false; w->w_table_initialised = false;
} }
+5 -5
View File
@@ -1,14 +1,14 @@
#include <bshell/format.h> #include <bshell/format.h>
static struct bshell_table_column verb_table_columns[] = { static struct bshell_table_column verb_table_columns[] = {
TABLE_COLUMN("verb", "verb", 13), TABLE_COLUMN("verb", "verb", 12),
TABLE_COLUMN("alias_prefix", "alias_prefix", 13), TABLE_COLUMN("alias_prefix", "alias_prefix", 12),
TABLE_COLUMN("group", "group", 13), TABLE_COLUMN("group", "group", 12),
TABLE_COLUMN("description", "description", COL_WIDTH_INFINITE), TABLE_COLUMN("description", "description", COL_WIDTH_INFINITE),
}; };
struct bshell_table verb_bshell_table = { struct bshell_table verb_bshell_table = {
.fmt_columns = verb_table_columns, .fmt_columns = verb_table_columns,
.fmt_column_count = sizeof verb_table_columns .fmt_column_count
/ sizeof verb_table_columns[0], = sizeof verb_table_columns / sizeof verb_table_columns[0],
}; };
+13 -1
View File
@@ -8,6 +8,7 @@
#include <fx/reflection/property.h> #include <fx/reflection/property.h>
#include <fx/reflection/type.h> #include <fx/reflection/type.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/stringstream.h>
#include <fx/value.h> #include <fx/value.h>
#define TABLE_COLUMN(display_name, property_name, width) \ #define TABLE_COLUMN(display_name, property_name, width) \
@@ -18,6 +19,12 @@
} }
#define COL_WIDTH_INFINITE ((size_t)-1) #define COL_WIDTH_INFINITE ((size_t)-1)
enum bshell_value_writer_flags {
BSHELL_VALUE_WRITER_ONELINE = 0x01u,
BSHELL_VALUE_WRITER_TRUNCATE = 0x02u,
BSHELL_VALUE_WRITER_NO_NEWLINE = 0x04u,
};
struct bshell_table_column { struct bshell_table_column {
const char *col_display_name; const char *col_display_name;
const char *col_property_name; const char *col_property_name;
@@ -49,7 +56,10 @@ struct bshell_table_ctx {
struct bshell_value_writer { struct bshell_value_writer {
struct bshell_table_ctx w_table_ctx; struct bshell_table_ctx w_table_ctx;
enum bshell_value_writer_flags w_flags;
size_t w_max_width;
bool w_table_initialised; bool w_table_initialised;
fx_stringstream *w_tmp;
fx_stream *w_dest; fx_stream *w_dest;
}; };
@@ -82,8 +92,10 @@ extern enum bshell_status bshell_table_ctx_print_record(
extern void bshell_value_writer_init( extern void bshell_value_writer_init(
struct bshell_value_writer *w, struct bshell_value_writer *w,
enum bshell_value_writer_flags flags,
size_t max_width,
fx_stream *dest); fx_stream *dest);
extern void bshell_value_writer_write( extern size_t bshell_value_writer_write(
struct bshell_value_writer *w, struct bshell_value_writer *w,
const fx_value *value); const fx_value *value);
extern void bshell_value_writer_cleanup(struct bshell_value_writer *w); extern void bshell_value_writer_cleanup(struct bshell_value_writer *w);
@@ -0,0 +1,21 @@
#ifndef BSHELL_LINE_EDITOR_CORE_H_
#define BSHELL_LINE_EDITOR_CORE_H_
#include <fx/macros.h>
FX_DECLS_BEGIN;
#define BSHELL_TYPE_LINE_EDITOR_CORE (bshell_line_ed_core_get_type())
FX_DECLARE_TYPE(bshell_line_ed_core);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed_core)
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed_core)
extern fx_type_id bshell_line_ed_core_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(bshell_line_ed_core, BSHELL_TYPE_LINE_EDITOR_CORE);
FX_DECLS_END;
#endif
@@ -0,0 +1,23 @@
#ifndef BSHELL_LINE_EDITOR_HIGHLIGHT_H_
#define BSHELL_LINE_EDITOR_HIGHLIGHT_H_
#include <fx/macros.h>
FX_DECLS_BEGIN;
#define BSHELL_TYPE_LINE_EDITOR_HIGHLIGHT (bshell_line_ed_highlight_get_type())
FX_DECLARE_TYPE(bshell_line_ed_highlight);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed_highlight)
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed_highlight)
extern fx_type_id bshell_line_ed_highlight_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(
bshell_line_ed_highlight,
BSHELL_TYPE_LINE_EDITOR_HIGHLIGHT);
FX_DECLS_END;
#endif
@@ -0,0 +1,23 @@
#ifndef BSHELL_LINE_EDITOR_HISTORY_H_
#define BSHELL_LINE_EDITOR_HISTORY_H_
#include <fx/macros.h>
FX_DECLS_BEGIN;
#define BSHELL_TYPE_LINE_EDITOR_HISTORY (bshell_line_ed_history_get_type())
FX_DECLARE_TYPE(bshell_line_ed_history);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed_history)
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed_history)
extern fx_type_id bshell_line_ed_history_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(
bshell_line_ed_history,
BSHELL_TYPE_LINE_EDITOR_HISTORY);
FX_DECLS_END;
#endif
@@ -0,0 +1,44 @@
#ifndef BSHELL_LINE_EDITOR_KEYBIND_H_
#define BSHELL_LINE_EDITOR_KEYBIND_H_
#include <bshell/status.h>
#include <fx/macros.h>
#include <fx/term/tty.h>
FX_DECLS_BEGIN;
#define BSHELL_LINE_EDITOR_KEYBIND_RESULT(status, action) \
((struct bshell_line_ed_keybind_result) { \
.r_status = (status), \
.r_action = (action), \
})
typedef FX_TYPE_FWDREF(bshell_line_ed) bshell_line_ed;
struct bshell_line_ed_keybind_result {
enum bshell_status r_status;
enum {
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE = 0,
BSHELL_LINE_EDITOR_KEYBIND_STOP,
} r_action;
};
typedef struct bshell_line_ed_keybind_result (
*bshell_line_ed_keybind_callback_t)(bshell_line_ed *, fx_keycode);
#define BSHELL_TYPE_LINE_EDITOR_KEYBIND (bshell_line_ed_keybind_get_type())
FX_DECLARE_TYPE(bshell_line_ed_keybind);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed_keybind)
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed_keybind)
extern fx_type_id bshell_line_ed_keybind_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(
bshell_line_ed_keybind,
BSHELL_TYPE_LINE_EDITOR_KEYBIND);
FX_DECLS_END;
#endif
@@ -0,0 +1,68 @@
#ifndef BSHELL_LINE_EDITOR_H_
#define BSHELL_LINE_EDITOR_H_
#include <bshell/line-editor/keybind.h>
#include <bshell/line-editor/plugin.h>
#include <fx/macros.h>
#include <fx/string.h>
#include <fx/term/tty.h>
FX_DECLS_BEGIN;
#define BSHELL_TYPE_LINE_EDITOR (bshell_line_ed_get_type())
FX_DECLARE_TYPE(bshell_line_ed);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed)
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed)
extern fx_type_id bshell_line_ed_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(bshell_line_ed, BSHELL_TYPE_LINE_EDITOR);
extern enum bshell_status bshell_line_ed_readline(
bshell_line_ed *ed,
fx_string *out);
extern struct bshell_line_source *bshell_line_ed_get_line_source(
bshell_line_ed *ed);
extern bshell_line_ed_keybind *bshell_line_ed_bind_key(
bshell_line_ed *ed,
const char *key,
bshell_line_ed_keybind_callback_t callback,
unsigned long priority);
extern enum bshell_status bshell_line_ed_unbind_key(
bshell_line_ed *ed,
bshell_line_ed_keybind *bind);
extern unsigned long bshell_line_ed_get_cursor(const bshell_line_ed *ed);
extern void bshell_line_ed_set_cursor(bshell_line_ed *ed, unsigned long cursor);
extern unsigned long bshell_line_ed_move_cursor(
bshell_line_ed *ed,
long move_amount);
extern enum bshell_status bshell_line_ed_set_done(bshell_line_ed *ed);
extern enum bshell_status bshell_line_ed_set_eof(bshell_line_ed *ed);
extern bool bshell_line_ed_is_done(const bshell_line_ed *ed);
extern bool bshell_line_ed_is_eof(const bshell_line_ed *ed);
extern fx_string *bshell_line_ed_get_prompt(bshell_line_ed *ed);
extern fx_string *bshell_line_ed_get_buffer(bshell_line_ed *ed);
extern fx_tty *bshell_line_ed_get_tty(bshell_line_ed *ed);
extern fx_value bshell_line_ed_get_data(
const bshell_line_ed *ed,
const char *key);
extern void bshell_line_ed_put_data(
bshell_line_ed *ed,
const char *key,
fx_value value);
extern void bshell_line_ed_buffer_changed(bshell_line_ed *ed);
extern enum bshell_status bshell_line_ed_add_plugin(
bshell_line_ed *ed,
bshell_line_ed_plugin *plugin);
FX_DECLS_END;
#endif
@@ -0,0 +1,42 @@
#ifndef BSHELL_LINE_EDITOR_PLUGIN_H_
#define BSHELL_LINE_EDITOR_PLUGIN_H_
#include <bshell/status.h>
#include <fx/macros.h>
#include <fx/value.h>
FX_DECLS_BEGIN;
typedef FX_TYPE_FWDREF(bshell_line_ed) bshell_line_ed;
#define BSHELL_TYPE_LINE_EDITOR_PLUGIN (bshell_line_ed_plugin_get_type())
FX_DECLARE_TYPE(bshell_line_ed_plugin);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed_plugin)
enum bshell_status (*p_load)(bshell_line_ed_plugin *, bshell_line_ed *);
enum bshell_status (
*p_unload)(bshell_line_ed_plugin *, bshell_line_ed *);
enum bshell_status (
*p_render_prompt)(bshell_line_ed_plugin *, bshell_line_ed *);
enum bshell_status (
*p_render_buffer)(bshell_line_ed_plugin *, bshell_line_ed *);
enum bshell_status (
*p_buffer_changed)(bshell_line_ed_plugin *, bshell_line_ed *);
enum bshell_status (*p_get_data)(
bshell_line_ed_plugin *,
bshell_line_ed *,
const char *,
fx_value *);
enum bshell_status (*p_put_data)(
bshell_line_ed_plugin *,
bshell_line_ed *,
const char *,
const fx_value *);
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed_plugin)
extern fx_type_id bshell_line_ed_plugin_get_type(void);
FX_DECLS_END;
#endif
@@ -0,0 +1,25 @@
#ifndef BSHELL_LINE_EDITOR_SHELL_INTEGRATION_H_
#define BSHELL_LINE_EDITOR_SHELL_INTEGRATION_H_
#include <fx/macros.h>
FX_DECLS_BEGIN;
struct bshell_runtime;
#define BSHELL_TYPE_LINE_EDITOR_SHELL_INTEGRATION \
(bshell_line_ed_shell_integration_get_type())
FX_DECLARE_TYPE(bshell_line_ed_shell_integration);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed_shell_integration)
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed_shell_integration)
extern fx_type_id bshell_line_ed_shell_integration_get_type(void);
extern bshell_line_ed_shell_integration *bshell_line_ed_shell_integration_create(
struct bshell_runtime *rt);
FX_DECLS_END;
#endif
@@ -0,0 +1,23 @@
#ifndef BSHELL_LINE_EDITOR_SYNTAX_H_
#define BSHELL_LINE_EDITOR_SYNTAX_H_
#include <fx/macros.h>
FX_DECLS_BEGIN;
#define BSHELL_TYPE_LINE_EDITOR_SYNTAX (bshell_line_ed_syntax_get_type())
FX_DECLARE_TYPE(bshell_line_ed_syntax);
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_line_ed_syntax)
FX_TYPE_CLASS_DECLARATION_END(bshell_line_ed_syntax)
extern fx_type_id bshell_line_ed_syntax_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(
bshell_line_ed_syntax,
BSHELL_TYPE_LINE_EDITOR_SYNTAX);
FX_DECLS_END;
#endif
@@ -32,9 +32,6 @@ extern fx_value bshell_runtime_eval_script(
extern bshell_variable *bshell_runtime_find_var( extern bshell_variable *bshell_runtime_find_var(
struct bshell_runtime *rt, struct bshell_runtime *rt,
const char *name); const char *name);
extern bshell_variable *bshell_runtime_define_var(
struct bshell_runtime *rt,
const char *name);
extern enum bshell_status bshell_runtime_define_function( extern enum bshell_status bshell_runtime_define_function(
struct bshell_runtime *rt, struct bshell_runtime *rt,
bshell_function *func); bshell_function *func);
@@ -49,6 +46,8 @@ extern enum bshell_status bshell_runtime_push_scope(
extern enum bshell_status bshell_runtime_pop_scope(struct bshell_runtime *rt); extern enum bshell_status bshell_runtime_pop_scope(struct bshell_runtime *rt);
extern fx_value bshell_runtime_eval(struct bshell_runtime *rt); extern fx_value bshell_runtime_eval(struct bshell_runtime *rt);
extern struct bshell_runtime_scope *bshell_runtime_get_global_scope(
struct bshell_runtime *rt);
extern struct bshell_runtime_scope *bshell_runtime_get_current_scope( extern struct bshell_runtime_scope *bshell_runtime_get_current_scope(
struct bshell_runtime *rt); struct bshell_runtime *rt);
extern struct bshell_runtime_scope *bshell_runtime_get_parent_scope( extern struct bshell_runtime_scope *bshell_runtime_get_parent_scope(
@@ -1,11 +1,15 @@
#ifndef BSHELL_RUNTIME_SCOPE_H_ #ifndef BSHELL_RUNTIME_SCOPE_H_
#define BSHELL_RUNTIME_SCOPE_H_ #define BSHELL_RUNTIME_SCOPE_H_
#include <bshell/runtime/var.h>
#include <fx/collections/hashtable.h> #include <fx/collections/hashtable.h>
struct bshell_runtime_scope; struct bshell_runtime_scope;
extern fx_hashtable *bshell_runtime_scope_get_functions( extern fx_hashtable *bshell_runtime_scope_get_functions(
struct bshell_runtime_scope *scope); struct bshell_runtime_scope *scope);
extern bshell_variable *bshell_runtime_scope_define_variable(
struct bshell_runtime_scope *scope,
const char *name);
#endif #endif
+229
View File
@@ -0,0 +1,229 @@
#include <bshell/line-editor/core.h>
#include <bshell/line-editor/line-editor.h>
#include <bshell/line-editor/plugin.h>
#include <fx/collections/hashtable.h>
#include <fx/term/tty.h>
struct bshell_line_ed_core_p {
bshell_line_ed_keybind *c_key_cursor_left;
bshell_line_ed_keybind *c_key_cursor_right;
bshell_line_ed_keybind *c_key_return;
bshell_line_ed_keybind *c_key_eof;
bshell_line_ed_keybind *c_key_generic;
bshell_line_ed_keybind *c_key_backspace;
fx_hashtable *c_data;
};
static void init(fx_object *obj, void *priv)
{
struct bshell_line_ed_core_p *core = priv;
core->c_data = fx_hashtable_create();
}
static void fini(fx_object *obj, void *priv)
{
struct bshell_line_ed_core_p *core = priv;
fx_hashtable_unref(core->c_data);
}
static struct bshell_line_ed_keybind_result delete_backward(
bshell_line_ed *ed,
fx_keycode key)
{
return BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
}
static struct bshell_line_ed_keybind_result cursor_left(
bshell_line_ed *ed,
fx_keycode key)
{
bshell_line_ed_move_cursor(ed, -1);
return BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
}
static struct bshell_line_ed_keybind_result cursor_right(
bshell_line_ed *ed,
fx_keycode key)
{
bshell_line_ed_move_cursor(ed, 1);
return BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
}
static struct bshell_line_ed_keybind_result eof_key(
bshell_line_ed *ed,
fx_keycode key)
{
bshell_line_ed_set_eof(ed);
return BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
}
static struct bshell_line_ed_keybind_result return_key(
bshell_line_ed *ed,
fx_keycode key)
{
bshell_line_ed_set_done(ed);
fx_string *buf = bshell_line_ed_get_buffer(ed);
fx_string_append_c(buf, '\n');
return BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
}
static struct bshell_line_ed_keybind_result backspace(
bshell_line_ed *ed,
fx_keycode key)
{
fx_string *buf = bshell_line_ed_get_buffer(ed);
unsigned long cursor = bshell_line_ed_get_cursor(ed);
if (cursor > 0) {
fx_string_remove(buf, cursor - 1, 1);
bshell_line_ed_move_cursor(ed, -1);
bshell_line_ed_buffer_changed(ed);
}
return BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
}
static struct bshell_line_ed_keybind_result generic_key(
bshell_line_ed *ed,
fx_keycode key)
{
fx_wchar c = key;
bool valid = fx_wchar_utf8_is_valid_scalar(c) && fx_wchar_is_graph(c);
if (valid) {
fx_string *buf = bshell_line_ed_get_buffer(ed);
unsigned long cursor = bshell_line_ed_get_cursor(ed);
char cstr[5] = {0};
fx_string_insert_wc(buf, c, cursor);
bshell_line_ed_move_cursor(ed, 1);
bshell_line_ed_buffer_changed(ed);
}
return BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
}
static enum bshell_status load(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed)
{
struct bshell_line_ed_core_p *core
= fx_object_get_private(plugin, BSHELL_TYPE_LINE_EDITOR_CORE);
core->c_key_eof = bshell_line_ed_bind_key(ed, "^d", eof_key, 100);
core->c_key_cursor_left
= bshell_line_ed_bind_key(ed, "left", cursor_left, 100);
core->c_key_cursor_right
= bshell_line_ed_bind_key(ed, "right", cursor_right, 100);
core->c_key_cursor_right
= bshell_line_ed_bind_key(ed, "return", return_key, 100);
core->c_key_generic
= bshell_line_ed_bind_key(ed, "*", generic_key, 100);
core->c_key_backspace
= bshell_line_ed_bind_key(ed, "backspace", backspace, 100);
return BSHELL_SUCCESS;
}
static enum bshell_status render_prompt(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed)
{
fx_string *prompt = bshell_line_ed_get_prompt(ed);
fx_tty *tty = bshell_line_ed_get_tty(ed);
const char *s = fx_string_get_cstr(prompt);
fx_tty_puts(tty, 0, s);
return BSHELL_SUCCESS;
}
static enum bshell_status render_buffer(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed)
{
fx_string *buf = bshell_line_ed_get_buffer(ed);
fx_tty *tty = bshell_line_ed_get_tty(ed);
const char *s = fx_string_get_cstr(buf);
for (unsigned long i = 0; *s; i++) {
fx_wchar c = fx_wchar_utf8_codepoint_decode(s);
unsigned int stride = fx_wchar_utf8_codepoint_size(c);
if (stride == 0) {
break;
}
char cs[5] = {0};
fx_wchar_utf8_codepoint_encode(c, cs);
fx_tty_puts(tty, 0, cs);
s += stride;
}
return BSHELL_SUCCESS;
}
static enum bshell_status get_data(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed,
const char *key,
fx_value *out)
{
struct bshell_line_ed_core_p *core
= fx_object_get_private(plugin, BSHELL_TYPE_LINE_EDITOR_CORE);
const fx_value *v = fx_hashtable_get(core->c_data, &FX_CSTR(key));
if (v) {
fx_value_copy(out, v);
} else {
*out = FX_VALUE_EMPTY;
}
return BSHELL_SUCCESS;
}
static enum bshell_status put_data(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed,
const char *key,
const fx_value *value)
{
struct bshell_line_ed_core_p *core
= fx_object_get_private(plugin, BSHELL_TYPE_LINE_EDITOR_CORE);
fx_hashtable_put(core->c_data, &FX_CSTR(key), (fx_value *)value);
return BSHELL_SUCCESS;
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed_core)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(
bshell_line_ed_plugin,
BSHELL_TYPE_LINE_EDITOR_PLUGIN)
FX_INTERFACE_ENTRY(p_load) = load;
FX_INTERFACE_ENTRY(p_render_prompt) = render_prompt;
FX_INTERFACE_ENTRY(p_render_buffer) = render_buffer;
FX_INTERFACE_ENTRY(p_get_data) = get_data;
FX_INTERFACE_ENTRY(p_put_data) = put_data;
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
FX_TYPE_CLASS_END(bshell_line_ed_core)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed_core)
FX_TYPE_ID(0xf5174a63, 0x8fe4, 0x4720, 0xa861, 0x4ebb0d170dd3);
FX_TYPE_NAME("bshell.runtime.line_editor.plugins.core");
FX_TYPE_EXTENDS(BSHELL_TYPE_LINE_EDITOR_PLUGIN);
FX_TYPE_CLASS(bshell_line_ed_core_class);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_core_p);
FX_TYPE_DEFINITION_END(bshell_line_ed_core)
+105
View File
@@ -0,0 +1,105 @@
#include "line-editor.h"
#include <fx/encoding.h>
static void convert_offset_to_xy(
struct bshell_line_ed_p *ed,
unsigned long offset,
unsigned long *out_x,
unsigned long *out_y)
{
unsigned long x = 0, y = 0;
unsigned int w = 0, h = 0;
fx_tty_get_dimensions(ed->ed_tty, &w, &h);
if (!w || !h) {
w = 80;
h = 25;
}
fx_string *prompt = line_ed_get_prompt(ed);
fx_string *buffer = line_ed_get_buffer(ed);
const char *s = fx_string_get_cstr(prompt);
size_t len = fx_string_get_size(prompt, FX_STRLEN_CODEPOINTS);
fx_wchar prev = FX_WCHAR_INVALID;
for (unsigned long i = 0; i < len; i++) {
fx_wchar c = fx_wchar_utf8_codepoint_decode(s);
if (prev != FX_WCHAR_INVALID) {
if (prev == '\n' || x + 1 >= w) {
x = 0;
y++;
} else {
x += line_ed_get_char_width(c);
}
}
unsigned int stride = fx_wchar_utf8_codepoint_size(c);
if (stride == 0) {
break;
}
s += stride;
prev = c;
}
s = fx_string_get_cstr(buffer);
len = fx_string_get_size(buffer, FX_STRLEN_CODEPOINTS);
for (unsigned long i = 0; i < offset && i < len; i++) {
fx_wchar c = fx_wchar_utf8_codepoint_decode(s);
if (prev != FX_WCHAR_INVALID) {
if (prev == '\n' || x + 1 >= w) {
x = 0;
y++;
} else {
x += line_ed_get_char_width(c);
}
}
unsigned int stride = fx_wchar_utf8_codepoint_size(c);
if (stride == 0) {
break;
}
s += stride;
prev = c;
}
if (offset > len) {
if (x + 1 >= w) {
x = 0;
y++;
} else {
x++;
}
}
if (out_x) {
*out_x = x;
}
if (out_y) {
*out_y = y;
}
}
void line_ed_get_points(struct bshell_line_ed_p *ed, struct cursor_points *out)
{
unsigned long cursor = line_ed_get_cursor(ed);
fx_string *buffer = line_ed_get_buffer(ed);
convert_offset_to_xy(
ed,
cursor + 1,
&out->c_cursor_x,
&out->c_cursor_y);
convert_offset_to_xy(
ed,
fx_string_get_size(buffer, FX_STRLEN_CODEPOINTS),
&out->c_render_limit_x,
&out->c_render_limit_y);
convert_offset_to_xy(
ed,
fx_string_get_size(buffer, FX_STRLEN_CODEPOINTS) + 1,
&out->c_cursor_limit_x,
&out->c_cursor_limit_y);
}
+98
View File
@@ -0,0 +1,98 @@
#include "line-editor.h"
void line_ed_hide(struct bshell_line_ed_p *ed)
{
if (!ed->ed_render.r_vlines) {
return;
}
#if 0
fprintf(stderr, "--- hide ---\n");
fprintf(stderr,
"vlines=%lu, cursor=%lu->(%ld,%ld)\n",
ed->ed_render.r_vlines,
ed->ed_cursor,
ed->ed_render.ed_points.c_cursor_x,
ed->ed_render.ed_points.c_cursor_y);
#endif
if (ed->ed_render.ed_points.c_cursor_y > 0) {
fx_tty_move_cursor_y(
ed->ed_tty,
FX_TTY_POS_CURSOR,
-ed->ed_render.ed_points.c_cursor_y);
}
fx_tty_move_cursor_x(ed->ed_tty, FX_TTY_POS_START, 0);
fx_tty_clear(
ed->ed_tty,
FX_TTY_CLEAR_SCREEN | FX_TTY_CLEAR_FROM_CURSOR);
memset(&ed->ed_render, 0x0, sizeof ed->ed_render);
}
static void render_prompt(struct bshell_line_ed_p *ed)
{
if (ed->ed_render_prompt) {
bshell_line_ed_plugin_class *renderer = fx_object_get_interface(
ed->ed_render_prompt,
BSHELL_TYPE_LINE_EDITOR_PLUGIN);
renderer->p_render_prompt(ed->ed_render_prompt, ed->ed_self);
}
}
static void render_buffer(struct bshell_line_ed_p *ed)
{
if (ed->ed_render_buffer) {
bshell_line_ed_plugin_class *renderer = fx_object_get_interface(
ed->ed_render_buffer,
BSHELL_TYPE_LINE_EDITOR_PLUGIN);
renderer->p_render_buffer(ed->ed_render_buffer, ed->ed_self);
}
}
void line_ed_show(struct bshell_line_ed_p *ed)
{
render_prompt(ed);
render_buffer(ed);
line_ed_get_points(ed, &ed->ed_render.ed_points);
bool cursor_limit_newline
= ed->ed_render.ed_points.c_cursor_limit_x == 0
&& ed->ed_render.ed_points.c_cursor_limit_y > 0;
if (cursor_limit_newline) {
fx_tty_putc(ed->ed_tty, 0, '\r');
fx_tty_putc(ed->ed_tty, 0, '\n');
}
long shift_x = 0, shift_y = 0;
shift_x = (long)ed->ed_render.ed_points.c_cursor_x
- (long)ed->ed_render.ed_points.c_cursor_limit_x;
shift_y = (long)ed->ed_render.ed_points.c_cursor_y
- (long)ed->ed_render.ed_points.c_cursor_limit_y;
fx_tty_move_cursor_x(ed->ed_tty, FX_TTY_POS_CURSOR, shift_x);
fx_tty_move_cursor_y(ed->ed_tty, FX_TTY_POS_CURSOR, shift_y);
#if 0
fprintf(stderr, "--- show ---\n");
fprintf(stderr,
"cursor=%lu->(%lu,%lu)\n",
ed->ed_cursor,
ed->ed_render.ed_points.c_cursor_x,
ed->ed_render.ed_points.c_cursor_y);
fprintf(stderr,
"cursor_limit=(%lu,%lu)\n",
ed->ed_render.ed_points.c_cursor_limit_x,
ed->ed_render.ed_points.c_cursor_limit_y);
fprintf(stderr,
"render_limit=(%lu,%lu)\n",
ed->ed_render.ed_points.c_render_limit_x,
ed->ed_render.ed_points.c_render_limit_y);
#endif
ed->ed_render.r_vlines = ed->ed_render.ed_points.c_render_limit_y + 1;
}
int line_ed_get_char_width(fx_wchar c)
{
/* TODO support double-width and zero-width characters */
return fx_wchar_is_graph(c) ? 1 : 0;
}
+61
View File
@@ -0,0 +1,61 @@
#include <bshell/line-editor/highlight.h>
#include <bshell/line-editor/line-editor.h>
#include <bshell/line-editor/plugin.h>
#include <fx/term/tty.h>
struct bshell_line_ed_highlight_p {
};
static void init(fx_object *obj, void *priv)
{
}
static void fini(fx_object *obj, void *priv)
{
}
static enum bshell_status render_buffer(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed)
{
fx_string *buf = bshell_line_ed_get_buffer(ed);
fx_tty *tty = bshell_line_ed_get_tty(ed);
const char *s = fx_string_get_cstr(buf);
for (unsigned long i = 0; *s; i++) {
fx_wchar c = fx_wchar_utf8_codepoint_decode(s);
unsigned int stride = fx_wchar_utf8_codepoint_size(c);
if (stride == 0) {
break;
}
char cs[5] = {0};
fx_wchar_utf8_codepoint_encode(c, cs);
fx_tty_puts(tty, 0, cs);
s += stride;
}
fx_tty_puts(tty, 0, "[reset]");
return BSHELL_SUCCESS;
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed_highlight)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(
bshell_line_ed_plugin,
BSHELL_TYPE_LINE_EDITOR_PLUGIN)
FX_INTERFACE_ENTRY(p_render_buffer) = render_buffer;
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
FX_TYPE_CLASS_END(bshell_line_ed_highlight)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed_highlight)
FX_TYPE_ID(0x162823ef, 0x4893, 0x4089, 0xa4f6, 0xf015a1976881);
FX_TYPE_NAME("bshell.runtime.line_editor.plugins.highlight");
FX_TYPE_EXTENDS(BSHELL_TYPE_LINE_EDITOR_PLUGIN);
FX_TYPE_CLASS(bshell_line_ed_highlight_class);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_highlight_p);
FX_TYPE_DEFINITION_END(bshell_line_ed_highlight)
+34
View File
@@ -0,0 +1,34 @@
#include <bshell/line-editor/history.h>
#include <bshell/line-editor/plugin.h>
struct bshell_line_ed_history_p {
};
static void init(fx_object *obj, void *priv)
{
}
static void fini(fx_object *obj, void *priv)
{
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed_history)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(
bshell_line_ed_plugin,
BSHELL_TYPE_LINE_EDITOR_PLUGIN)
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
FX_TYPE_CLASS_END(bshell_line_ed_history)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed_history)
FX_TYPE_ID(0xa08c98d5, 0x9512, 0x4a65, 0xaf57, 0x0daa8a943e52);
FX_TYPE_NAME("bshell.runtime.line_editor.plugins.history");
FX_TYPE_EXTENDS(BSHELL_TYPE_LINE_EDITOR_PLUGIN);
FX_TYPE_CLASS(bshell_line_ed_history_class);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_history_p);
FX_TYPE_DEFINITION_END(bshell_line_ed_history)
+223
View File
@@ -0,0 +1,223 @@
#include "keybind.h"
#include "line-editor.h"
#include <fx/term/tty.h>
#define KEYCODE_WILDCARD FX_KEY_RESERVED_START
static FX_BST_DEFINE_SIMPLE_INSERT(
struct keymap_entry,
e_node,
e_code,
put_keymap_entry);
static FX_BST_DEFINE_SIMPLE_GET(
struct keymap_entry,
fx_keycode,
e_node,
e_code,
get_keymap_entry);
static FX_BST_DEFINE_SIMPLE_INSERT(
struct keybind,
b_node,
b_priority,
put_keybind);
struct bshell_line_ed_keybind_p {
bshell_line_ed_keybind *kb_self;
struct keybind kb_bind;
};
static void keymap_entry_destroy(struct keymap_entry *entry)
{
fx_bst_node *cur_bind = fx_bst_first(&entry->e_binds);
while (cur_bind) {
fx_bst_node *next_bind = fx_bst_next(cur_bind);
fx_bst_delete(&entry->e_binds, cur_bind);
struct keybind *keybind
= fx_unbox(struct keybind, cur_bind, b_node);
struct bshell_line_ed_keybind_p *keybind_object_p = fx_unbox(
struct bshell_line_ed_keybind_p,
keybind,
kb_bind);
bshell_line_ed_keybind_unref(keybind_object_p->kb_self);
cur_bind = next_bind;
}
free(entry);
}
void line_ed_cleanup_keymap(fx_bst *keymap)
{
fx_bst_node *cur_entry = fx_bst_first(keymap);
while (cur_entry) {
fx_bst_node *next_entry = fx_bst_next(cur_entry);
fx_bst_delete(keymap, cur_entry);
struct keymap_entry *keymap_entry
= fx_unbox(struct keymap_entry, cur_entry, e_node);
keymap_entry_destroy(keymap_entry);
cur_entry = next_entry;
}
}
static fx_keycode key_string_to_code(const char *key)
{
const char *tmp = key;
fx_keycode result = 0;
if (*key == '^') {
result |= FX_MOD_CTRL;
key++;
}
if (!strcmp(key, "*")) {
result |= KEYCODE_WILDCARD;
} else if (!strcmp(key, "left")) {
result |= FX_KEY_ARROW_LEFT;
} else if (!strcmp(key, "right")) {
result |= FX_KEY_ARROW_RIGHT;
} else if (!strcmp(key, "up")) {
result |= FX_KEY_ARROW_UP;
} else if (!strcmp(key, "down")) {
result |= FX_KEY_ARROW_DOWN;
} else if (!strcmp(key, "backspace")) {
result |= FX_KEY_BACKSPACE;
} else if (!strcmp(key, "return")) {
result |= FX_KEY_RETURN;
} else {
result |= fx_wchar_utf8_codepoint_decode(key);
}
return result;
}
static bshell_line_ed_keybind *line_ed_bind_key(
struct bshell_line_ed_p *ed,
const char *key,
bshell_line_ed_keybind_callback_t callback,
unsigned long priority)
{
fx_keycode keycode = key_string_to_code(key);
if (!keycode) {
return NULL;
}
bshell_line_ed_keybind *bind = NULL;
bind = bshell_line_ed_keybind_create();
if (!bind) {
return NULL;
}
struct bshell_line_ed_keybind_p *bind_p
= fx_object_get_private(bind, BSHELL_TYPE_LINE_EDITOR_KEYBIND);
struct keymap_entry *keymap_entry
= get_keymap_entry(&ed->ed_keymap, keycode);
if (!keymap_entry) {
keymap_entry = calloc(1, sizeof *keymap_entry);
if (!keymap_entry) {
bshell_line_ed_keybind_unref(bind);
return NULL;
}
keymap_entry->e_code = keycode;
put_keymap_entry(&ed->ed_keymap, keymap_entry);
}
bind_p->kb_self = bind;
bind_p->kb_bind.b_priority = priority;
bind_p->kb_bind.b_callback = callback;
put_keybind(&keymap_entry->e_binds, &bind_p->kb_bind);
return bind;
}
bshell_line_ed_keybind *bshell_line_ed_bind_key(
bshell_line_ed *ed,
const char *key,
bshell_line_ed_keybind_callback_t callback,
unsigned long priority)
{
FX_CLASS_DISPATCH_STATIC(
BSHELL_TYPE_LINE_EDITOR,
line_ed_bind_key,
ed,
key,
callback,
priority);
}
static struct bshell_line_ed_keybind_result do_handle_key(
struct bshell_line_ed_p *ed,
fx_keycode match_code,
fx_keycode real_code)
{
struct bshell_line_ed_keybind_result result
= BSHELL_LINE_EDITOR_KEYBIND_RESULT(
BSHELL_SUCCESS,
BSHELL_LINE_EDITOR_KEYBIND_CONTINUE);
struct keymap_entry *keymap_entry
= get_keymap_entry(&ed->ed_keymap, match_code);
if (!keymap_entry) {
return result;
}
fx_bst_node *cur = fx_bst_last(&keymap_entry->e_binds);
while (cur) {
struct keybind *bind = fx_unbox(struct keybind, cur, b_node);
result = bind->b_callback(ed->ed_self, real_code);
if (result.r_status != BSHELL_SUCCESS
|| result.r_action != BSHELL_LINE_EDITOR_KEYBIND_CONTINUE) {
break;
}
cur = fx_bst_prev(cur);
}
return result;
}
void line_ed_handle_key(struct bshell_line_ed_p *ed, fx_keycode code)
{
struct bshell_line_ed_keybind_result result
= do_handle_key(ed, code, code);
if (result.r_status != BSHELL_SUCCESS
|| result.r_action != BSHELL_LINE_EDITOR_KEYBIND_CONTINUE) {
return;
}
if (code & FX_MOD_CTRL) {
result = do_handle_key(
ed,
FX_MOD_CTRL | KEYCODE_WILDCARD,
code);
} else {
result = do_handle_key(ed, KEYCODE_WILDCARD, code);
}
}
static void fini(fx_object *obj, void *priv)
{
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed_keybind)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(bshell_line_ed_keybind)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed_keybind)
FX_TYPE_ID(0x5d842371, 0x40a3, 0x4683, 0xaff2, 0x05daa53d36b7);
FX_TYPE_NAME("bshell.line_editor.keybind");
FX_TYPE_CLASS(bshell_line_ed_keybind_class);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_keybind_p);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_DEFINITION_END(bshell_line_ed_keybind)
+22
View File
@@ -0,0 +1,22 @@
#ifndef _KEYBIND_H_
#define _KEYBIND_H_
#include <bshell/line-editor/keybind.h>
#include <fx/bst.h>
#include <fx/term/tty.h>
struct keybind {
fx_bst_node b_node;
unsigned long b_priority;
bshell_line_ed_keybind_callback_t b_callback;
};
struct keymap_entry {
fx_bst_node e_node;
fx_keycode e_code;
fx_bst e_binds;
};
extern void line_ed_cleanup_keymap(fx_bst *keymap);
#endif
+507
View File
@@ -0,0 +1,507 @@
#include "line-editor.h"
#include "keybind.h"
/*** PRIVATE FUNCTIONS ********************************************************/
static void line_ed_set_cursor(
struct bshell_line_ed_p *ed,
unsigned long cursor);
static fx_value line_ed_get_data(
const struct bshell_line_ed_p *ed,
const char *key);
static enum bshell_status line_ed_put_data(
struct bshell_line_ed_p *ed,
const char *key,
fx_value value);
static void clear_cache(struct bshell_line_ed_p *ed)
{
fx_string_unref(ed->ed_cache.c_prompt);
fx_string_unref(ed->ed_cache.c_buffer);
memset(&ed->ed_cache, 0x0, sizeof ed->ed_cache);
}
static enum bshell_status line_ed_readline(
struct bshell_line_ed_p *ed,
fx_string *out)
{
ed->ed_flags &= ~(LINE_ED_DONE | LINE_ED_EOF);
fx_tty_set_mode(ed->ed_tty, FX_TTY_RAW);
clear_cache(ed);
fx_string *prompt = line_ed_get_prompt(ed);
fx_string *buffer = line_ed_get_buffer(ed);
fx_string_clear(buffer);
line_ed_set_cursor(ed, 0);
#define SHOULD_CONTINUE(ed) (!(ed->ed_flags & (LINE_ED_DONE | LINE_ED_EOF)))
while (SHOULD_CONTINUE(ed)) {
line_ed_hide(ed);
line_ed_show(ed);
fx_tty_flush(ed->ed_tty);
fx_keycode key = fx_tty_read_key(ed->ed_tty);
line_ed_handle_key(ed, key);
}
#undef SHOULD_CONTINUE
long shift_y = (long)ed->ed_render.ed_points.c_cursor_limit_y
- (long)ed->ed_render.ed_points.c_cursor_y + 1;
#if 0
fprintf(stderr,
"cursor_y=%lu, cursor_limit_y=%lu, shift_y=%ld\n",
ed->ed_render.ed_points.c_cursor_limit_y,
ed->ed_render.ed_points.c_cursor_y,
shift_y);
#endif
fx_tty_putc(ed->ed_tty, 0, '\r');
for (long i = 0; i < shift_y; i++) {
fx_tty_putc(ed->ed_tty, 0, '\n');
}
fx_tty_flush(ed->ed_tty);
fx_tty_set_mode(ed->ed_tty, FX_TTY_CANONICAL);
buffer = line_ed_get_buffer(ed);
if (out) {
fx_string_append_s(out, buffer);
}
return BSHELL_SUCCESS;
}
static struct bshell_line_source *line_ed_get_line_source(
struct bshell_line_ed_p *ed)
{
return &ed->ed_linesrc;
}
unsigned long line_ed_get_cursor(const struct bshell_line_ed_p *ed)
{
enum bshell_status status = BSHELL_SUCCESS;
fx_value v = line_ed_get_data(ed, "cursor");
if (!fx_value_is_set(&v)) {
status = line_ed_put_data(
(struct bshell_line_ed_p *)ed,
"cursor",
FX_ULONG(0));
return 0;
}
unsigned long cursor = 0;
fx_value_get_ulong(&v, &cursor);
fx_value_unset(&v);
return cursor;
}
static void line_ed_set_cursor(
struct bshell_line_ed_p *ed,
unsigned long cursor)
{
line_ed_put_data(ed, "cursor", FX_ULONG(cursor));
}
static unsigned long line_ed_move_cursor(
struct bshell_line_ed_p *ed,
long move_amount)
{
unsigned long cursor = line_ed_get_cursor(ed);
fx_string *buffer = line_ed_get_buffer(ed);
if (move_amount < 0) {
if (labs(move_amount) > cursor) {
cursor = 0;
} else {
cursor += move_amount;
}
} else {
unsigned long max
= fx_string_get_size(buffer, FX_STRLEN_CODEPOINTS);
if (cursor + move_amount > max) {
cursor = max;
} else {
cursor += move_amount;
}
}
line_ed_put_data(ed, "cursor", FX_ULONG(cursor));
return cursor;
}
static enum bshell_status line_ed_set_done(struct bshell_line_ed_p *ed)
{
ed->ed_flags |= LINE_ED_DONE;
return BSHELL_SUCCESS;
}
static enum bshell_status line_ed_set_eof(struct bshell_line_ed_p *ed)
{
ed->ed_flags |= LINE_ED_EOF;
return BSHELL_SUCCESS;
}
static bool line_ed_is_done(const struct bshell_line_ed_p *ed)
{
return (ed->ed_flags & LINE_ED_DONE) == LINE_ED_DONE;
}
static bool line_ed_is_eof(const struct bshell_line_ed_p *ed)
{
return (ed->ed_flags & LINE_ED_EOF) == LINE_ED_EOF;
}
static fx_value line_ed_get_data(
const struct bshell_line_ed_p *ed,
const char *key)
{
if (!ed->ed_data) {
return FX_VALUE_EMPTY;
}
bshell_line_ed_plugin_class *ops = fx_object_get_interface(
ed->ed_data,
BSHELL_TYPE_LINE_EDITOR_PLUGIN);
if (!ops || !ops->p_get_data) {
return FX_VALUE_EMPTY;
}
fx_value result = FX_VALUE_EMPTY;
enum bshell_status status
= ops->p_get_data(ed->ed_data, ed->ed_self, key, &result);
if (status != BSHELL_SUCCESS) {
return FX_VALUE_EMPTY;
}
return result;
}
static enum bshell_status line_ed_put_data(
struct bshell_line_ed_p *ed,
const char *key,
fx_value value)
{
if (!ed->ed_data) {
return BSHELL_ERR_NOT_SUPPORTED;
}
bshell_line_ed_plugin_class *ops = fx_object_get_interface(
ed->ed_data,
BSHELL_TYPE_LINE_EDITOR_PLUGIN);
if (!ops || !ops->p_put_data) {
return BSHELL_ERR_NOT_SUPPORTED;
}
return ops->p_put_data(ed->ed_data, ed->ed_self, key, &value);
}
fx_string *line_ed_get_prompt(struct bshell_line_ed_p *ed)
{
if (ed->ed_cache.c_prompt) {
return ed->ed_cache.c_prompt;
}
enum bshell_status status = BSHELL_SUCCESS;
fx_value v = line_ed_get_data(ed, "prompt");
if (!fx_value_is_set(&v)) {
fx_string *prompt = fx_string_create();
status = line_ed_put_data(
ed,
"prompt",
FX_VALUE_OBJECT(prompt));
if (status != BSHELL_SUCCESS) {
fx_string_unref(prompt);
return NULL;
}
ed->ed_cache.c_prompt = prompt;
return prompt;
}
fx_value_get_object(&v, &ed->ed_cache.c_prompt);
return ed->ed_cache.c_prompt;
}
fx_string *line_ed_get_buffer(struct bshell_line_ed_p *ed)
{
if (ed->ed_cache.c_buffer) {
return ed->ed_cache.c_buffer;
}
enum bshell_status status = BSHELL_SUCCESS;
fx_value v = line_ed_get_data(ed, "buffer");
if (!fx_value_is_set(&v)) {
fx_string *buffer = fx_string_create();
status = line_ed_put_data(
ed,
"buffer",
FX_VALUE_OBJECT(buffer));
if (status != BSHELL_SUCCESS) {
fx_string_unref(buffer);
return NULL;
}
ed->ed_cache.c_buffer = buffer;
return buffer;
}
fx_value_get_object(&v, &ed->ed_cache.c_buffer);
return ed->ed_cache.c_buffer;
}
static fx_tty *line_ed_get_tty(struct bshell_line_ed_p *ed)
{
return ed->ed_tty;
}
static enum bshell_status line_ed_add_plugin(
struct bshell_line_ed_p *ed,
bshell_line_ed_plugin *plugin)
{
bshell_line_ed_plugin_class *ops = fx_object_get_interface(
plugin,
BSHELL_TYPE_LINE_EDITOR_PLUGIN);
if (!ops) {
return BSHELL_ERR_INVALID_ARGUMENT;
}
if (ops->p_load) {
ops->p_load(plugin, ed->ed_self);
}
fx_array_push_back(ed->ed_plugins, FX_VALUE_OBJECT(plugin));
if (ops->p_render_buffer) {
ed->ed_render_buffer = plugin;
}
if (ops->p_render_prompt) {
ed->ed_render_prompt = plugin;
}
if (ops->p_get_data && ops->p_put_data) {
ed->ed_data = plugin;
}
return BSHELL_SUCCESS;
}
static void line_ed_buffer_changed(struct bshell_line_ed_p *ed)
{
const fx_iterator *it = fx_iterator_begin(ed->ed_plugins);
fx_foreach(v, it)
{
bshell_line_ed_plugin *plugin = NULL;
fx_value_get_object(v, &plugin);
bshell_line_ed_plugin_class *ops = fx_object_get_interface(
plugin,
BSHELL_TYPE_LINE_EDITOR_PLUGIN);
if (ops && ops->p_buffer_changed) {
ops->p_buffer_changed(plugin, ed->ed_self);
}
}
fx_iterator_unref(it);
}
static enum bshell_status readline(
struct bshell_line_source *src,
fx_stringstream *out)
{
struct bshell_line_ed_p *ed
= fx_unbox(struct bshell_line_ed_p, src, ed_linesrc);
long r = line_ed_readline(ed, NULL);
if (line_ed_is_eof(ed)) {
return BSHELL_ERR_EOF;
}
fx_string *buf = line_ed_get_buffer(ed);
if (buf) {
fx_stream_write_cstr(out, fx_string_get_cstr(buf), NULL);
}
return BSHELL_SUCCESS;
}
/*** PUBLIC FUNCTIONS
* *********************************************************/
enum bshell_status bshell_line_ed_readline(bshell_line_ed *ed, fx_string *out)
{
FX_CLASS_DISPATCH_STATIC(
BSHELL_TYPE_LINE_EDITOR,
line_ed_readline,
ed,
out);
}
struct bshell_line_source *bshell_line_ed_get_line_source(bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_get_line_source,
ed);
}
unsigned long bshell_line_ed_get_cursor(const bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_get_cursor,
ed);
}
void bshell_line_ed_set_cursor(bshell_line_ed *ed, unsigned long cursor)
{
FX_CLASS_DISPATCH_STATIC(
BSHELL_TYPE_LINE_EDITOR,
line_ed_set_cursor,
ed,
cursor);
}
unsigned long bshell_line_ed_move_cursor(bshell_line_ed *ed, long move_amount)
{
FX_CLASS_DISPATCH_STATIC(
BSHELL_TYPE_LINE_EDITOR,
line_ed_move_cursor,
ed,
move_amount);
}
enum bshell_status bshell_line_ed_set_done(bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_set_done,
ed);
}
enum bshell_status bshell_line_ed_set_eof(bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_set_eof,
ed);
}
bool bshell_line_ed_is_done(const bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_is_done,
ed);
}
bool bshell_line_ed_is_eof(const bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(BSHELL_TYPE_LINE_EDITOR, line_ed_is_eof, ed);
}
fx_string *bshell_line_ed_get_prompt(bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_get_prompt,
ed);
}
fx_string *bshell_line_ed_get_buffer(bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_get_buffer,
ed);
}
fx_tty *bshell_line_ed_get_tty(bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_get_tty,
ed);
}
fx_value bshell_line_ed_get_data(const bshell_line_ed *ed, const char *key)
{
FX_CLASS_DISPATCH_STATIC(
BSHELL_TYPE_LINE_EDITOR,
line_ed_get_data,
ed,
key);
}
void bshell_line_ed_put_data(
bshell_line_ed *ed,
const char *key,
fx_value value)
{
FX_CLASS_DISPATCH_STATIC_V(
BSHELL_TYPE_LINE_EDITOR,
line_ed_put_data,
ed,
key,
value);
}
enum bshell_status bshell_line_ed_add_plugin(
bshell_line_ed *ed,
bshell_line_ed_plugin *plugin)
{
FX_CLASS_DISPATCH_STATIC(
BSHELL_TYPE_LINE_EDITOR,
line_ed_add_plugin,
ed,
plugin);
}
void bshell_line_ed_buffer_changed(bshell_line_ed *ed)
{
FX_CLASS_DISPATCH_STATIC_V0(
BSHELL_TYPE_LINE_EDITOR,
line_ed_buffer_changed,
ed);
}
/*** VIRTUAL FUNCTIONS
* ********************************************************/
static void init(fx_object *obj, void *priv)
{
struct bshell_line_ed_p *ed = priv;
ed->ed_self = obj;
ed->ed_tty = fx_stdtty;
ed->ed_plugins = fx_array_create();
ed->ed_linesrc.s_readline = readline;
}
static void fini(fx_object *obj, void *priv)
{
struct bshell_line_ed_p *ed = priv;
clear_cache(ed);
line_ed_cleanup_keymap(&ed->ed_keymap);
fx_array_unref(ed->ed_plugins);
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(bshell_line_ed)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed)
FX_TYPE_ID(0xc59de843, 0xea6a, 0x46d9, 0xb7fd, 0xa3777a40c4fa);
FX_TYPE_NAME("bshell.line_editor");
FX_TYPE_CLASS(bshell_line_ed_class);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_p);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_DEFINITION_END(bshell_line_ed)
+76
View File
@@ -0,0 +1,76 @@
#ifndef _LINE_EDITOR_H_
#define _LINE_EDITOR_H_
#include <bshell/line-editor/line-editor.h>
#include <bshell/line-source.h>
#include <fx/collections/array.h>
#include <fx/collections/hashtable.h>
#include <fx/term/tty.h>
enum line_ed_flags {
LINE_ED_DONE = 0x01u,
LINE_ED_EOF = 0x02u,
};
struct cursor_points {
/* visual co-ordinates of the current cursor position */
unsigned long c_cursor_x, c_cursor_y;
/* visual co-ordinates of the last character in the rendered prompt
* string */
unsigned long c_render_limit_x, c_render_limit_y;
/* visual co-ordinates of the last cell that could be occupied by the
* cursor in the rendered prompt string */
unsigned long c_cursor_limit_x, c_cursor_limit_y;
};
struct line_render {
/* how many visual terminal rows are being used to display the current
* buffer. may be greater than the number of lines in ed_buffer if one
* or more of the lines exceeds the width of the terminal. will be
* less than or equal to the terminal height. */
unsigned long r_vlines;
/* the logical offset that the cursor was at when the prompt was
* rendered */
unsigned long r_cursor;
/* points of interest */
struct cursor_points ed_points;
};
struct data_cache {
fx_string *c_prompt;
fx_string *c_buffer;
};
struct bshell_line_ed_p {
bshell_line_ed *ed_self;
enum line_ed_flags ed_flags;
struct bshell_line_source ed_linesrc;
fx_bst ed_keymap;
fx_array *ed_plugins;
fx_tty *ed_tty;
struct data_cache ed_cache;
bshell_line_ed_plugin *ed_render_prompt;
bshell_line_ed_plugin *ed_render_buffer;
bshell_line_ed_plugin *ed_data;
struct line_render ed_render;
};
extern void line_ed_handle_key(struct bshell_line_ed_p *ed, fx_keycode code);
extern void line_ed_get_points(
struct bshell_line_ed_p *ed,
struct cursor_points *out);
extern void line_ed_hide(struct bshell_line_ed_p *ed);
extern void line_ed_show(struct bshell_line_ed_p *ed);
extern fx_string *line_ed_get_prompt(struct bshell_line_ed_p *ed);
extern fx_string *line_ed_get_buffer(struct bshell_line_ed_p *ed);
extern unsigned long line_ed_get_cursor(const struct bshell_line_ed_p *ed);
extern int line_ed_get_char_width(fx_wchar c);
#endif
+27
View File
@@ -0,0 +1,27 @@
#include <bshell/line-editor/plugin.h>
struct bshell_line_ed_plugin_p {
};
static void init(fx_object *obj, void *priv)
{
}
static void fini(fx_object *obj, void *priv)
{
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed_plugin)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(bshell_line_ed_plugin)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed_plugin)
FX_TYPE_ID(0x6311ff4e, 0x5eb7, 0x48f0, 0x982b, 0xf8ffbda74869);
FX_TYPE_NAME("bshell.runtime.line_editor.plugin");
FX_TYPE_CLASS(bshell_line_ed_plugin_class);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_plugin_p);
FX_TYPE_DEFINITION_END(bshell_line_ed_plugin)
@@ -0,0 +1,160 @@
#include <bshell/line-editor/line-editor.h>
#include <bshell/line-editor/plugin.h>
#include <bshell/line-editor/shell-integration.h>
#include <bshell/runtime/cmdcall.h>
#include <bshell/runtime/pipeline.h>
#include <bshell/runtime/runtime.h>
#include <bshell/runtime/scope.h>
#include <fx/term/tty.h>
#define DEFAULT_PROMPT "Bsh> "
struct bshell_line_ed_shell_integration_p {
struct bshell_runtime *si_rt;
fx_hashtable *si_data;
fx_string *si_default_prompt;
bshell_pipeline *si_pipeline;
bshell_cmdcall *si_prompt;
};
static void init(fx_object *obj, void *priv)
{
struct bshell_line_ed_shell_integration_p *p = priv;
p->si_default_prompt = fx_string_create_from_cstr(DEFAULT_PROMPT);
p->si_pipeline = bshell_pipeline_create();
p->si_prompt = bshell_cmdcall_create();
bshell_cmdcall_push_arg(p->si_prompt, FX_CSTR("prompt"));
bshell_pipeline_add_cmdcall(p->si_pipeline, p->si_prompt);
}
static void fini(fx_object *obj, void *priv)
{
struct bshell_line_ed_shell_integration_p *p = priv;
bshell_pipeline_unref(p->si_pipeline);
bshell_cmdcall_unref(p->si_prompt);
fx_string_unref(p->si_default_prompt);
}
bshell_line_ed_shell_integration *bshell_line_ed_shell_integration_create(
struct bshell_runtime *rt)
{
bshell_line_ed_shell_integration *out
= fx_object_create(BSHELL_TYPE_LINE_EDITOR_SHELL_INTEGRATION);
if (!out) {
return NULL;
}
struct bshell_line_ed_shell_integration_p *p = fx_object_get_private(
out,
BSHELL_TYPE_LINE_EDITOR_SHELL_INTEGRATION);
p->si_rt = rt;
return out;
}
static enum bshell_status load(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed)
{
struct bshell_line_ed_shell_integration_p *shell
= fx_object_get_private(
plugin,
BSHELL_TYPE_LINE_EDITOR_SHELL_INTEGRATION);
shell->si_data = fx_hashtable_create();
struct bshell_runtime_scope *global
= bshell_runtime_get_global_scope(shell->si_rt);
bshell_variable *item
= bshell_runtime_scope_define_variable(global, "line_editor");
bshell_variable_set_value(item, FX_VALUE_OBJECT(shell->si_data));
printf("shell integration loaded\n");
return BSHELL_SUCCESS;
}
static enum bshell_status get_prompt(
struct bshell_line_ed_shell_integration_p *shell,
bshell_line_ed *ed,
fx_value *out)
{
enum bshell_status status
= bshell_cmdcall_resolve(shell->si_prompt, shell->si_rt);
if (status != BSHELL_SUCCESS) {
*out = fx_value_copy_return(
FX_VALUE_OBJECT(shell->si_default_prompt));
return BSHELL_SUCCESS;
}
int eof = 0;
status = bshell_pipeline_pump_record(
shell->si_pipeline,
shell->si_rt,
out,
&eof);
return status;
}
static enum bshell_status get_data(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed,
const char *key,
fx_value *out)
{
struct bshell_line_ed_shell_integration_p *shell
= fx_object_get_private(
plugin,
BSHELL_TYPE_LINE_EDITOR_SHELL_INTEGRATION);
if (!strcmp(key, "prompt")) {
return get_prompt(shell, ed, out);
}
const fx_value *v = fx_hashtable_get(shell->si_data, &FX_CSTR(key));
if (v) {
fx_value_copy(out, v);
} else {
*out = FX_VALUE_EMPTY;
}
return BSHELL_SUCCESS;
}
static enum bshell_status put_data(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed,
const char *key,
const fx_value *value)
{
if (!strcmp(key, "prompt")) {
return BSHELL_SUCCESS;
}
struct bshell_line_ed_shell_integration_p *shell
= fx_object_get_private(
plugin,
BSHELL_TYPE_LINE_EDITOR_SHELL_INTEGRATION);
fx_hashtable_put(shell->si_data, &FX_CSTR(key), (fx_value *)value);
return BSHELL_SUCCESS;
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed_shell_integration)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(
bshell_line_ed_plugin,
BSHELL_TYPE_LINE_EDITOR_PLUGIN)
FX_INTERFACE_ENTRY(p_load) = load;
FX_INTERFACE_ENTRY(p_get_data) = get_data;
FX_INTERFACE_ENTRY(p_put_data) = put_data;
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
FX_TYPE_CLASS_END(bshell_line_ed_shell_integration)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed_shell_integration)
FX_TYPE_ID(0x8e89c8d0, 0xbc82, 0x4153, 0x9607, 0xa7a710486596);
FX_TYPE_NAME("bshell.runtime.line_editor.plugins.shell_integration");
FX_TYPE_EXTENDS(BSHELL_TYPE_LINE_EDITOR_PLUGIN);
FX_TYPE_CLASS(bshell_line_ed_shell_integration_class);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_shell_integration_p);
FX_TYPE_DEFINITION_END(bshell_line_ed_shell_integration)
+45
View File
@@ -0,0 +1,45 @@
#include <bshell/line-editor/line-editor.h>
#include <bshell/line-editor/plugin.h>
#include <bshell/line-editor/syntax.h>
#include <fx/term/tty.h>
struct bshell_line_ed_syntax_p {
};
static void init(fx_object *obj, void *priv)
{
}
static void fini(fx_object *obj, void *priv)
{
}
static enum bshell_status buffer_changed(
bshell_line_ed_plugin *plugin,
bshell_line_ed *ed)
{
// fprintf(stderr, "buffer changed!\n");
return BSHELL_SUCCESS;
}
FX_TYPE_CLASS_BEGIN(bshell_line_ed_syntax)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(
bshell_line_ed_plugin,
BSHELL_TYPE_LINE_EDITOR_PLUGIN)
FX_INTERFACE_ENTRY(p_buffer_changed) = buffer_changed;
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
FX_TYPE_CLASS_END(bshell_line_ed_syntax)
FX_TYPE_DEFINITION_BEGIN(bshell_line_ed_syntax)
FX_TYPE_ID(0x4d9dea6c, 0x2ed8, 0x40e0, 0x943d, 0xfd70a7a9f54a);
FX_TYPE_NAME("bshell.runtime.line_editor.plugins.syntax");
FX_TYPE_EXTENDS(BSHELL_TYPE_LINE_EDITOR_PLUGIN);
FX_TYPE_CLASS(bshell_line_ed_syntax_class);
FX_TYPE_INSTANCE_INIT(init);
FX_TYPE_INSTANCE_FINI(fini);
FX_TYPE_INSTANCE_PRIVATE(struct bshell_line_ed_syntax_p);
FX_TYPE_DEFINITION_END(bshell_line_ed_syntax)
+3 -2
View File
@@ -188,10 +188,11 @@ enum bshell_status command_pump_token(struct bshell_lex_ctx *ctx)
const struct bshell_lex_state_link links[] = { const struct bshell_lex_state_link links[] = {
LINK_PUSH(BSHELL_SYM_DQUOTE, BSHELL_LEX_STATE_STRING, 0), LINK_PUSH(BSHELL_SYM_DQUOTE, BSHELL_LEX_STATE_STRING, 0),
LINK_PUSH( LINK_PUSH_WITH_TERM(
BSHELL_SYM_LEFT_PAREN, BSHELL_SYM_LEFT_PAREN,
BSHELL_LEX_STATE_STATEMENT, BSHELL_LEX_STATE_STATEMENT,
STATEMENT_F_DISABLE_KEYWORDS), STATEMENT_F_DISABLE_KEYWORDS,
BSHELL_SYM_RIGHT_PAREN),
LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0), LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0),
LINK_POP(BSHELL_SYM_RIGHT_PAREN), LINK_POP(BSHELL_SYM_RIGHT_PAREN),
LINK_POP(BSHELL_SYM_RIGHT_BRACE), LINK_POP(BSHELL_SYM_RIGHT_BRACE),
+7 -2
View File
@@ -142,7 +142,8 @@ static enum bshell_status statement_pump_token(struct bshell_lex_ctx *ctx)
} }
if (newline) { if (newline) {
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_LINEFEED); struct bshell_lex_token *tok
= bshell_lex_token_create(BSHELL_TOK_LINEFEED);
enqueue_token(ctx, tok); enqueue_token(ctx, tok);
handle_lex_state_transition(ctx, BSHELL_TOK_LINEFEED); handle_lex_state_transition(ctx, BSHELL_TOK_LINEFEED);
return BSHELL_SUCCESS; return BSHELL_SUCCESS;
@@ -203,7 +204,11 @@ static const struct bshell_lex_state_link links[] = {
BSHELL_SYM_RIGHT_PAREN), BSHELL_SYM_RIGHT_PAREN),
/* command tokens */ /* command tokens */
LINK_CHANGE(BSHELL_KW_FUNC, BSHELL_LEX_STATE_COMMAND), LINK_PUSH_WITH_TERM(
BSHELL_KW_FUNC,
BSHELL_LEX_STATE_COMMAND,
0,
BSHELL_SYM_RIGHT_BRACE),
LINK_CHANGE(BSHELL_SYM_AMPERSAND, BSHELL_LEX_STATE_COMMAND), LINK_CHANGE(BSHELL_SYM_AMPERSAND, BSHELL_LEX_STATE_COMMAND),
LINK_CHANGE(BSHELL_TOK_WORD, BSHELL_LEX_STATE_COMMAND), LINK_CHANGE(BSHELL_TOK_WORD, BSHELL_LEX_STATE_COMMAND),
LINK_END, LINK_END,
+2
View File
@@ -28,6 +28,8 @@ bool parse_block(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
fx_queue_push_back(&block->n_statements, &stmt->n_entry); fx_queue_push_back(&block->n_statements, &stmt->n_entry);
parse_linefeed(ctx);
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_BRACE)) { if (parse_symbol(ctx, BSHELL_SYM_RIGHT_BRACE)) {
break; break;
} }
-4
View File
@@ -193,10 +193,6 @@ static enum bshell_status resolve_call_target(
struct bshell_cmdcall_p *cmdcall, struct bshell_cmdcall_p *cmdcall,
struct bshell_runtime *rt) struct bshell_runtime *rt)
{ {
if (cmdcall->cmd_type != CMDCALL_NONE) {
return BSHELL_SUCCESS;
}
fx_value *cmd_name_v = fx_array_get_ref(cmdcall->cmd_args_processed, 0); fx_value *cmd_name_v = fx_array_get_ref(cmdcall->cmd_args_processed, 0);
if (!cmd_name_v) { if (!cmd_name_v) {
return BSHELL_ERR_BAD_STATE; return BSHELL_ERR_BAD_STATE;
+7 -3
View File
@@ -4,6 +4,7 @@
#include <bshell/runtime/cmdcall.h> #include <bshell/runtime/cmdcall.h>
#include <bshell/runtime/pipeline.h> #include <bshell/runtime/pipeline.h>
#include <bshell/runtime/runtime.h> #include <bshell/runtime/runtime.h>
#include <bshell/runtime/scope.h>
#include <bshell/status.h> #include <bshell/status.h>
#include <fx/collections/array.h> #include <fx/collections/array.h>
#include <fx/collections/hashtable.h> #include <fx/collections/hashtable.h>
@@ -219,8 +220,9 @@ static enum bshell_status eval_instruction(
fx_value_to_string(&y, strm, NULL); fx_value_to_string(&y, strm, NULL);
fx_value_unset(&y); fx_value_unset(&y);
const fx_property *prop const fx_property *prop = fx_type_get_property(
= fx_type_get_property(ty, fx_stringstream_get_cstr(strm)); ty,
fx_stringstream_get_cstr(strm));
fx_stringstream_unref(strm); fx_stringstream_unref(strm);
if (!prop) { if (!prop) {
@@ -250,7 +252,9 @@ static enum bshell_status eval_instruction(
fx_value_get_cstr(pool_value, &s); fx_value_get_cstr(pool_value, &s);
var = bshell_runtime_find_var(rt, s); var = bshell_runtime_find_var(rt, s);
if (!var) { if (!var) {
var = bshell_runtime_define_var(rt, s); var = bshell_runtime_scope_define_variable(
bshell_runtime_get_current_scope(rt),
s);
} }
bshell_variable_set_value(var, x); bshell_variable_set_value(var, x);
+1 -1
View File
@@ -4,7 +4,7 @@
void bshell_runtime_begin_output(struct bshell_runtime *rt) void bshell_runtime_begin_output(struct bshell_runtime *rt)
{ {
bshell_value_writer_init(&rt->rt_writer, fx_stdout); bshell_value_writer_init(&rt->rt_writer, 0, 0, fx_stdout);
} }
void bshell_runtime_output_value( void bshell_runtime_output_value(
+6 -18
View File
@@ -68,24 +68,6 @@ bshell_variable *bshell_runtime_find_var(
return var; return var;
} }
bshell_variable *bshell_runtime_define_var(
struct bshell_runtime *rt,
const char *name)
{
struct bshell_runtime_scope *scope = runtime_get_scope(rt);
if (!scope) {
return NULL;
}
bshell_variable *var = bshell_variable_create(name);
if (!var) {
return NULL;
}
var_map_put(&scope->s_vars, var);
return var;
}
enum bshell_status bshell_runtime_define_function( enum bshell_status bshell_runtime_define_function(
struct bshell_runtime *rt, struct bshell_runtime *rt,
bshell_function *func) bshell_function *func)
@@ -149,6 +131,12 @@ bshell_command *bshell_runtime_find_command(
return NULL; return NULL;
} }
struct bshell_runtime_scope *bshell_runtime_get_global_scope(
struct bshell_runtime *rt)
{
return rt->rt_global;
}
struct bshell_runtime_scope *bshell_runtime_get_current_scope( struct bshell_runtime_scope *bshell_runtime_get_current_scope(
struct bshell_runtime *rt) struct bshell_runtime *rt)
{ {
+17
View File
@@ -116,3 +116,20 @@ fx_hashtable *bshell_runtime_scope_get_functions(
{ {
return scope->s_functions; return scope->s_functions;
} }
bshell_variable *bshell_runtime_scope_define_variable(
struct bshell_runtime_scope *scope,
const char *name)
{
if (!scope) {
return NULL;
}
bshell_variable *var = bshell_variable_create(name);
if (!var) {
return NULL;
}
var_map_put(&scope->s_vars, var);
return var;
}
+6 -4
View File
@@ -1,5 +1,5 @@
#ifndef BSHELL_RUNTIME_SCOPE_H_ #ifndef RUNTIME_SCOPE_H_
#define BSHELL_RUNTIME_SCOPE_H_ #define RUNTIME_SCOPE_H_
#include "var-map.h" #include "var-map.h"
@@ -37,12 +37,14 @@ extern struct bshell_runtime_scope *runtime_push_scope(
enum bshell_runtime_scope_type type, enum bshell_runtime_scope_type type,
bshell_scriptblock *block); bshell_scriptblock *block);
extern void runtime_pop_scope(struct bshell_runtime *rt); extern void runtime_pop_scope(struct bshell_runtime *rt);
extern struct bshell_runtime_scope *runtime_get_scope(struct bshell_runtime *rt); extern struct bshell_runtime_scope *runtime_get_scope(
struct bshell_runtime *rt);
extern void bshell_runtime_scope_push_value( extern void bshell_runtime_scope_push_value(
struct bshell_runtime_scope *scope, struct bshell_runtime_scope *scope,
const fx_value *value); const fx_value *value);
extern fx_value bshell_runtime_scope_pop_value(struct bshell_runtime_scope *scope); extern fx_value bshell_runtime_scope_pop_value(
struct bshell_runtime_scope *scope);
extern void bshell_runtime_scope_set_block( extern void bshell_runtime_scope_set_block(
struct bshell_runtime_scope *scope, struct bshell_runtime_scope *scope,
+1 -3
View File
@@ -1,6 +1,4 @@
set(source_dirs set(source_dirs)
ast compile parse parse/lex parse/syntax
runtime format command cmdlets aliases)
if (bshell_interactive EQUAL 1) if (bshell_interactive EQUAL 1)
message(STATUS "Interactive support: Enabled") message(STATUS "Interactive support: Enabled")
+23 -4
View File
@@ -10,7 +10,12 @@
#include <fx/reflection/assembly.h> #include <fx/reflection/assembly.h>
#if BSHELL_INTERACTIVE == 1 #if BSHELL_INTERACTIVE == 1
#include "line-ed/line-ed.h" #include <bshell/line-editor/core.h>
#include <bshell/line-editor/highlight.h>
#include <bshell/line-editor/history.h>
#include <bshell/line-editor/line-editor.h>
#include <bshell/line-editor/shell-integration.h>
#include <bshell/line-editor/syntax.h>
#endif #endif
#define FRONTEND_FLAG_SET(f, flag) \ #define FRONTEND_FLAG_SET(f, flag) \
@@ -230,13 +235,27 @@ enum bshell_status frontend_eval_file(
enum bshell_status frontend_repl(struct frontend_ctx *ctx) enum bshell_status frontend_repl(struct frontend_ctx *ctx)
{ {
#if BSHELL_INTERACTIVE #if BSHELL_INTERACTIVE
struct line_ed *ed = line_ed_create(); bshell_line_ed_core *ed_core = bshell_line_ed_core_create();
bshell_line_ed_highlight *ed_highlight
= bshell_line_ed_highlight_create();
bshell_line_ed_syntax *ed_syntax = bshell_line_ed_syntax_create();
bshell_line_ed_shell_integration *ed_shell
= bshell_line_ed_shell_integration_create(ctx->ctx_rt);
bshell_line_ed *ed = bshell_line_ed_create();
bshell_line_ed_add_plugin(ed, ed_core);
bshell_line_ed_add_plugin(ed, ed_highlight);
bshell_line_ed_add_plugin(ed, ed_syntax);
bshell_line_ed_add_plugin(ed, ed_shell);
struct bshell_line_source *linesrc = line_ed_to_line_source(ed); struct bshell_line_source *linesrc = bshell_line_ed_get_line_source(ed);
ctx->ctx_status = eval(ctx, linesrc, EVAL_PRINT_RESULT); ctx->ctx_status = eval(ctx, linesrc, EVAL_PRINT_RESULT);
line_ed_destroy(ed); bshell_line_ed_unref(ed);
bshell_line_ed_plugin_unref(ed_shell);
bshell_line_ed_plugin_unref(ed_syntax);
bshell_line_ed_plugin_unref(ed_highlight);
bshell_line_ed_plugin_unref(ed_core);
return ctx->ctx_status; return ctx->ctx_status;
#else #else
-43
View File
@@ -1,43 +0,0 @@
#include "buffer.h"
#include "line-ed.h"
#include <fx/wstr.h>
const fx_wchar *line_start(struct line_ed *ed, size_t y)
{
const fx_wchar *line = ed->l_buf;
for (size_t i = 0; i < y; i++) {
line += fx_wstrcspn(line, L"\n");
if (*line == '\n') {
line++;
}
}
return line;
}
size_t line_length(struct line_ed *ed, size_t y)
{
const fx_wchar *line = ed->l_buf;
for (size_t i = 0; i < y; i++) {
line += fx_wstrcspn(line, L"\n");
if (*line == '\n') {
line++;
}
}
if (*line == '\0') {
return 0;
}
size_t len = fx_wstrcspn(line, L"\n");
if (line[len] == '\n') {
len++;
}
return len;
}
-17
View File
@@ -1,17 +0,0 @@
#ifndef LINE_ED_BUFFER_H_
#define LINE_ED_BUFFER_H_
#include <fx/encoding.h>
#include <stddef.h>
struct line_ed;
/* returns a pointer to the start of the line based on the given `y`
* coordinate */
extern const fx_wchar *line_start(struct line_ed *ed, size_t y);
/* returns the length of the line based on the given `y` coordinate.
* for any line other than the last line in the buffer, this length
* INCLUDES the trailing linefeed. */
extern size_t line_length(struct line_ed *ed, size_t y);
#endif
-26
View File
@@ -1,26 +0,0 @@
#include "line-ed.h"
#include "cursor.h"
#include "prompt.h"
void line_ed_coords_to_physical_coords(
struct line_ed *ed, size_t x, size_t y, size_t *out_x, size_t *out_y)
{
size_t prompt_len = 0;
if (ed->l_cursor_y == 0) {
prompt_len = prompt_length(ed, PROMPT_MAIN);
} else if (ed->l_cursor_y <= ed->l_continuations) {
prompt_len = prompt_length(ed, PROMPT_CONT);
}
if (y == 0) {
x += prompt_len;
}
if (out_x) {
*out_x = x;
}
if (out_y) {
*out_y = y;
}
}
-9
View File
@@ -1,9 +0,0 @@
#ifndef LINE_ED_CURSOR_H_
#define LINE_ED_CURSOR_H_
struct line_ed;
extern void line_ed_coords_to_physical_coords(
struct line_ed *ed, size_t x, size_t y, size_t *out_x, size_t *out_y);
#endif
-87
View File
@@ -1,87 +0,0 @@
#include "../misc.h"
#include "line-ed.h"
#include <fx/collections/array.h>
#include <fx/string.h>
void alloc_empty_history_entry(struct line_ed *ed)
{
fx_value *str_v = fx_array_get_ref(
ed->l_history,
fx_array_get_size(ed->l_history) - 1);
fx_string *str = NULL;
fx_value_get_object(str_v, &str);
if (!str || fx_string_get_size(str, FX_STRLEN_NORMAL) > 0) {
str = fx_string_create();
fx_array_push_back(ed->l_history, FX_VALUE_OBJECT(str));
fx_string_unref(str);
}
ed->l_history_pos = fx_array_get_size(ed->l_history) - 1;
}
void save_buf_to_history(struct line_ed *ed)
{
fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos);
fx_string *cur = NULL;
fx_value_get_object(cur_v, &cur);
fx_string_clear(cur);
fx_string_append_wstr(cur, ed->l_buf);
}
void append_buf_to_history(struct line_ed *ed)
{
fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos);
fx_string *cur = NULL;
fx_value_get_object(cur_v, &cur);
char s[] = {'\n', 0};
fx_string_append_cstr(cur, s);
fx_string_append_wstr(cur, ed->l_buf);
}
void load_buf_from_history(struct line_ed *ed)
{
fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos);
fx_string *cur = NULL;
fx_value_get_object(cur_v, &cur);
size_t len = MIN(
(size_t)(ed->l_buf_end - ed->l_buf - 1),
fx_string_get_size(cur, FX_STRLEN_CODEPOINTS));
const char *src = fx_string_get_cstr(cur);
for (size_t i = 0; i < len; i++) {
fx_wchar ch_src = fx_wchar_utf8_codepoint_decode(src);
ed->l_buf[i] = ch_src;
src += fx_wchar_utf8_codepoint_stride(src);
}
ed->l_buf[len] = '\0';
unsigned int x = 0, y = 0;
for (size_t i = 0; ed->l_buf[i]; i++) {
if (ed->l_buf[i] == '\n') {
x = 0;
y++;
} else {
x++;
}
}
ed->l_buf_ptr = ed->l_buf + len;
ed->l_line_end = ed->l_buf_ptr;
ed->l_cursor_x = x;
ed->l_cursor_y = y;
}
const char *last_history_line(struct line_ed *ed)
{
size_t nlines = fx_array_get_size(ed->l_history);
if (nlines < 2) {
return NULL;
}
fx_value *last_v = fx_array_get_ref(ed->l_history, nlines - 2);
fx_string *last = NULL;
fx_value_get_object(last_v, &last);
return fx_string_get_cstr(last);
}
-12
View File
@@ -1,12 +0,0 @@
#ifndef LINE_ED_HISTORY_H_
#define LINE_ED_HISTORY_H_
struct line_ed;
extern void alloc_empty_history_entry(struct line_ed *ed);
extern void save_buf_to_history(struct line_ed *ed);
extern void append_buf_to_history(struct line_ed *ed);
extern void load_buf_from_history(struct line_ed *ed);
extern const char *last_history_line(struct line_ed *ed);
#endif
-41
View File
@@ -1,41 +0,0 @@
#include "hook.h"
#include "line-ed.h"
void line_ed_add_hook(struct line_ed *ed, struct line_ed_hook *hook)
{
fx_queue_push_back(&ed->l_hooks, &hook->hook_entry);
}
void line_ed_remove_hook(struct line_ed *ed, struct line_ed_hook *hook)
{
fx_queue_delete(&ed->l_hooks, &hook->hook_entry);
}
void hook_keypress(struct line_ed *ed, fx_keycode key)
{
fx_queue_entry *entry = fx_queue_first(&ed->l_hooks);
while (entry) {
struct line_ed_hook *hook
= fx_unbox(struct line_ed_hook, entry, hook_entry);
if (hook->hook_keypress) {
hook->hook_keypress(ed, hook, key);
}
entry = fx_queue_next(entry);
}
}
void hook_buffer_modified(struct line_ed *ed)
{
fx_queue_entry *entry = fx_queue_first(&ed->l_hooks);
while (entry) {
struct line_ed_hook *hook
= fx_unbox(struct line_ed_hook, entry, hook_entry);
if (hook->hook_buffer_modified) {
hook->hook_buffer_modified(ed, hook);
}
entry = fx_queue_next(entry);
}
}
-16
View File
@@ -1,16 +0,0 @@
#ifndef LINE_ED_HOOK_H_
#define LINE_ED_HOOK_H_
#include <fx/term/tty.h>
enum hook_id {
HOOK_KEYPRESS,
HOOK_BEFORE_PAINT,
};
struct line_ed;
extern void hook_keypress(struct line_ed *ed, fx_keycode key);
extern void hook_buffer_modified(struct line_ed *ed);
#endif
-232
View File
@@ -1,232 +0,0 @@
#include "buffer.h"
#include "cursor.h"
#include "history.h"
#include "hook.h"
#include "line-ed.h"
#include "prompt.h"
#include "refresh.h"
#include <stdio.h>
void put_char(struct line_ed *ed, fx_wchar c)
{
if (ed->l_buf_ptr > ed->l_line_end + 1) {
return;
}
struct refresh_state state = {
.r_prev_cursor_x = ed->l_cursor_x,
.r_prev_cursor_y = ed->l_cursor_y,
};
size_t prev_cursor = ed->l_buf_ptr - ed->l_buf;
fx_wchar *dest = ed->l_buf_ptr;
size_t len = (ed->l_line_end - ed->l_buf_ptr + 1) * sizeof *dest;
if (dest < ed->l_line_end) {
memmove(dest + 1, dest, len);
}
ed->l_cursor_x++;
ed->l_line_end++;
ed->l_buf_ptr++;
*dest = c;
if (ed->l_buf_ptr == ed->l_line_end) {
*ed->l_buf_ptr = '\0';
}
hook_buffer_modified(ed);
put_refresh(ed, &state);
}
static void backspace_simple(struct line_ed *ed)
{
if (ed->l_buf_ptr == ed->l_buf) {
return;
}
struct refresh_state state = {
.r_prev_cursor_x = ed->l_cursor_x,
.r_prev_cursor_y = ed->l_cursor_y,
};
size_t prev_cursor = ed->l_buf_ptr - ed->l_buf;
fx_wchar *dest = ed->l_buf_ptr;
size_t len = (ed->l_line_end - ed->l_buf_ptr + 1) * sizeof *dest;
memmove(dest - 1, dest, len);
ed->l_cursor_x--;
ed->l_line_end--;
ed->l_buf_ptr--;
hook_buffer_modified(ed);
backspace_simple_refresh(ed, &state);
}
static void backspace_nl(struct line_ed *ed)
{
size_t prev_line_len = line_length(ed, ed->l_cursor_y - 1);
struct refresh_state state = {
.r_prev_cursor_x = ed->l_cursor_x,
.r_prev_cursor_y = ed->l_cursor_y,
.r_prev_line_len = prev_line_len,
};
fx_wchar *dest = ed->l_buf_ptr;
size_t len = (ed->l_line_end - ed->l_buf_ptr + 1) * sizeof *dest;
memmove(dest - 1, dest, len);
ed->l_cursor_x = prev_line_len - 1;
ed->l_cursor_y--;
ed->l_buf_ptr--;
ed->l_line_end--;
hook_buffer_modified(ed);
backspace_nl_refresh(ed, &state);
}
void backspace(struct line_ed *ed)
{
if (ed->l_buf_ptr == ed->l_buf) {
return;
}
if (ed->l_cursor_x == 0 && ed->l_cursor_y <= ed->l_continuations) {
return;
}
if (ed->l_cursor_x == 0 && ed->l_cursor_y > 0) {
backspace_nl(ed);
} else {
backspace_simple(ed);
}
}
void cursor_left(struct line_ed *ed)
{
if (ed->l_cursor_x != 0) {
// fputs("\010", stdout);
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_CURSOR, -1);
fflush(stdout);
ed->l_cursor_x--;
ed->l_buf_ptr--;
return;
}
if (ed->l_cursor_y <= ed->l_continuations
|| ed->l_buf_ptr <= ed->l_buf) {
return;
}
ed->l_cursor_y--;
ed->l_buf_ptr--;
size_t prompt_len = 0;
if (ed->l_cursor_y == 0) {
prompt_len = prompt_length(ed, PROMPT_MAIN);
}
size_t len = line_length(ed, ed->l_cursor_y);
ed->l_cursor_x = len - 1;
// printf("\033[A\033[%dG", len + prompt_len);
fx_tty_move_cursor_y(ed->l_tty, FX_TTY_POS_CURSOR, -1);
fx_tty_move_cursor_x(
ed->l_tty,
FX_TTY_POS_START,
(int)(len + prompt_len));
fflush(stdout);
}
void cursor_right(struct line_ed *ed)
{
if (ed->l_buf_ptr >= ed->l_line_end) {
return;
}
if (*ed->l_buf_ptr != '\n') {
ed->l_cursor_x++;
ed->l_buf_ptr++;
// fputs("\033[C", stdout);
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_CURSOR, 1);
fflush(stdout);
return;
}
if (ed->l_buf_ptr >= ed->l_line_end) {
return;
}
ed->l_cursor_y++;
ed->l_cursor_x = 0;
ed->l_buf_ptr++;
// printf("\033[B\033[G");
fx_tty_move_cursor_y(ed->l_tty, FX_TTY_POS_CURSOR, 1);
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_START, 0);
fflush(stdout);
}
void arrow_up(struct line_ed *ed)
{
if (ed->l_history_pos == 0) {
return;
}
if (ed->l_cursor_y > 0) {
// printf("\033[%uA", ed->l_cursor_y);
fx_tty_move_cursor_y(
ed->l_tty,
FX_TTY_POS_CURSOR,
(long long)ed->l_cursor_y);
}
// printf("\033[%zuG\033[J", prompt_length(ed, PROMPT_MAIN) + 1);
fx_tty_move_cursor_x(
ed->l_tty,
FX_TTY_POS_START,
(long long)prompt_length(ed, PROMPT_MAIN));
fx_tty_clear(ed->l_tty, FX_TTY_CLEAR_SCREEN | FX_TTY_CLEAR_FROM_CURSOR);
save_buf_to_history(ed);
ed->l_history_pos--;
load_buf_from_history(ed);
print_buffer(ed);
fflush(stdout);
}
void arrow_down(struct line_ed *ed)
{
if (ed->l_history_pos == fx_array_get_size(ed->l_history) - 1) {
return;
}
if (ed->l_cursor_y > 0) {
// printf("\033[%uA", ed->l_cursor_y);
fx_tty_move_cursor_y(
ed->l_tty,
FX_TTY_POS_CURSOR,
(int)ed->l_cursor_y);
}
// printf("\033[%zuG\033[J", prompt_length(ed, PROMPT_MAIN) + 1);
fx_tty_move_cursor_x(
ed->l_tty,
FX_TTY_POS_START,
prompt_length(ed, PROMPT_MAIN) + 1);
save_buf_to_history(ed);
ed->l_history_pos++;
load_buf_from_history(ed);
print_buffer(ed);
fflush(stdout);
}
-15
View File
@@ -1,15 +0,0 @@
#ifndef LINE_ED_INPUT_H_
#define LINE_ED_INPUT_H_
#include <fx/encoding.h>
struct line_ed;
extern void put_char(struct line_ed *ed, fx_wchar c);
extern void backspace(struct line_ed *ed);
extern void cursor_left(struct line_ed *ed);
extern void cursor_right(struct line_ed *ed);
extern void arrow_up(struct line_ed *ed);
extern void arrow_down(struct line_ed *ed);
#endif
-302
View File
@@ -1,302 +0,0 @@
#include "line-ed.h"
#include "history.h"
#include "hook.h"
#include "input.h"
#include "prompt.h"
#include <ctype.h>
#include <fx/term/tty.h>
#include <fx/wstr.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#define LINE_ED_FROM_LEX_SOURCE(p) \
((struct line_ed *)((char *)p \
- offsetof(struct line_ed, l_line_source)))
static enum bshell_status readline(
struct bshell_line_source *src,
fx_stringstream *out)
{
struct line_ed *ed = LINE_ED_FROM_LEX_SOURCE(src);
long r = line_ed_readline(ed, out);
line_ed_set_scope_type(ed, NULL);
if (r < 0) {
return BSHELL_ERR_EOF;
}
return BSHELL_SUCCESS;
}
struct line_ed *line_ed_create(void)
{
struct line_ed *out = malloc(sizeof *out);
if (!out) {
return NULL;
}
memset(out, 0x0, sizeof *out);
out->l_buf = malloc(LINE_MAX);
if (!out->l_buf) {
free(out);
return NULL;
}
out->l_history = fx_array_create();
if (!out->l_history) {
free(out->l_buf);
free(out);
return NULL;
}
out->l_tty = fx_stdtty;
out->l_buf_end = out->l_buf + LINE_MAX;
out->l_buf_ptr = out->l_buf;
out->l_line_end = out->l_buf;
out->l_prompt[PROMPT_MAIN] = ">>> ";
out->l_prompt[PROMPT_CONT] = "> ";
out->l_line_source.s_readline = readline;
return out;
}
void line_ed_destroy(struct line_ed *ed)
{
fx_array_unref(ed->l_history);
free(ed->l_buf);
free(ed);
}
void line_ed_set_flags(struct line_ed *ed, enum line_ed_flags flags)
{
ed->l_flags |= flags;
}
void line_ed_set_scope_type(struct line_ed *ed, const char *scope_type)
{
ed->l_scope_type = scope_type;
}
static void clear_buffer(struct line_ed *ed)
{
memset(ed->l_buf, 0x0, ed->l_buf_end - ed->l_buf);
ed->l_buf_ptr = ed->l_buf;
ed->l_line_end = ed->l_buf;
ed->l_cursor_x = 0;
ed->l_cursor_y = 0;
ed->l_continuations = 0;
}
static void convert_continuation_feeds(fx_wchar *s, size_t max)
{
fx_wchar *end = s + max;
size_t len = fx_wstrlen(s);
while (1) {
size_t r = fx_wstrcspn(s, L"\\");
if (s + r > end) {
break;
}
s += r;
len -= r;
if (*s == '\0') {
break;
}
if (*(s + 1) != '\n') {
s++;
continue;
}
memmove(s, s + 1, len);
s += 1;
}
}
static void remove_continuation_feeds(fx_wchar *s, size_t max)
{
fx_wchar *end = s + max;
size_t len = fx_wstrlen(s);
while (1) {
size_t r = fx_wstrcspn(s, L"\\");
if (s + r > end) {
break;
}
s += r;
len -= r;
if (*s == '\0') {
break;
}
if (*(s + 1) != '\n') {
s++;
continue;
}
memmove(s, s + 2, len);
s += 2;
}
}
static bool input_is_empty(struct line_ed *ed)
{
const fx_wchar *p = ed->l_buf;
while (p < ed->l_line_end) {
if (!fx_wchar_is_space(*p)) {
return false;
}
}
return true;
}
size_t line_ed_readline(struct line_ed *ed, fx_stringstream *out)
{
clear_buffer(ed);
bool append_history = false;
size_t append_to_index = (size_t)-1;
if (!ed->l_scope_type) {
alloc_empty_history_entry(ed);
} else {
append_history = true;
append_to_index = ed->l_history_pos;
}
fx_tty *tty = ed->l_tty;
fx_tty_set_mode(tty, FX_TTY_RAW);
show_prompt(ed);
for (int i = 0; ed->l_buf[i]; i++) {
if (ed->l_buf[i] == '\n') {
fputc('\r', stdout);
}
fx_stream_write_char(fx_stdout, ed->l_buf[i]);
}
fflush(stdout);
bool end = false;
bool eof = false;
while (!end) {
fx_keycode key = fx_tty_read_key(tty);
hook_keypress(ed, key);
if (key == FX_TTY_CTRL_KEY('d')) {
if (!input_is_empty(ed)) {
continue;
}
eof = true;
break;
}
if (key & FX_MOD_CTRL) {
continue;
}
switch (key) {
case FX_KEY_RETURN:
fx_tty_reset_vmode(tty);
if (ed->l_line_end > ed->l_buf
&& *(ed->l_line_end - 1) != '\\') {
end = true;
break;
}
if (input_is_empty(ed)) {
fputc('\r', stdout);
fputc('\n', stdout);
clear_buffer(ed);
show_prompt(ed);
break;
}
*ed->l_line_end = '\n';
ed->l_line_end++;
ed->l_buf_ptr = ed->l_line_end;
ed->l_cursor_x = 0;
ed->l_cursor_y++;
ed->l_continuations++;
fputc('\r', stdout);
fputc('\n', stdout);
// fputs("\033[G\n", stdout);
show_prompt(ed);
break;
case FX_KEY_BACKSPACE:
backspace(ed);
break;
case FX_KEY_ARROW_LEFT:
cursor_left(ed);
break;
case FX_KEY_ARROW_RIGHT:
cursor_right(ed);
break;
case FX_KEY_ARROW_UP:
arrow_up(ed);
break;
case FX_KEY_ARROW_DOWN:
arrow_down(ed);
break;
default:
if (fx_wchar_is_graph(key) || key == ' ') {
put_char(ed, key);
}
break;
}
}
fx_tty_set_mode(tty, FX_TTY_CANONICAL);
fputc('\n', stdout);
if (*ed->l_buf == '\0') {
return eof ? -1 : 0;
}
if (append_history) {
ed->l_history_pos = append_to_index;
append_buf_to_history(ed);
} else {
ed->l_history_pos = fx_array_get_size(ed->l_history) - 1;
const char *last = last_history_line(ed);
if (!last || fx_wastrcmp(last, ed->l_buf) != 0) {
save_buf_to_history(ed);
}
}
size_t sz = ed->l_line_end - ed->l_buf + 1;
if ((ed->l_flags & LINE_ED_REMOVE_CONTINUATIONS)
== LINE_ED_REMOVE_CONTINUATIONS) {
remove_continuation_feeds(ed->l_buf, sz);
} else if (
(ed->l_flags & LINE_ED_CONVERT_CONTINUATIONS)
== LINE_ED_CONVERT_CONTINUATIONS) {
convert_continuation_feeds(ed->l_buf, sz);
}
fx_stream_write_wstr(out, ed->l_buf, NULL);
fx_stream_write_char(out, '\n');
return sz;
}
-107
View File
@@ -1,107 +0,0 @@
#ifndef LINE_ED_H_
#define LINE_ED_H_
#define LINE_MAX 4096
#include <bshell/line-source.h>
#include <fx/collections/array.h>
#include <fx/queue.h>
#include <fx/term/tty.h>
#include <stddef.h>
struct s_tty;
struct fx_tty_vmode;
struct line_ed;
struct line_ed_hook {
void (*hook_keypress)(
struct line_ed *,
struct line_ed_hook *,
fx_keycode);
void (*hook_buffer_modified)(struct line_ed *, struct line_ed_hook *);
fx_queue_entry hook_entry;
};
enum line_ed_flags {
/* always reprint an entire line when a character is added/deleted.
* without this flag, only the modified character any subsequent
* characters are reprinted. */
LINE_ED_FULL_REPRINT = 0x01u,
/* keep line continuation (backslash-newline) tokens in the output
* buffer (default behaviour) */
LINE_ED_KEEP_CONTINUATIONS = 0x00u,
/* convert line continuation tokens in the output buffer to simple
* linefeeds. */
LINE_ED_CONVERT_CONTINUATIONS = 0x02u,
/* remove line continuation tokens from the output buffer, so that all
* chars are on a single line */
LINE_ED_REMOVE_CONTINUATIONS = 0x06u,
};
struct line_ed {
enum line_ed_flags l_flags;
/* array of basic prompt strings */
const char *l_prompt[2];
/* input buffer, pointer to the buffer cell that corresponds to
* the current cursor position, and pointer to the byte AFTER the last
* usable byte in the buffer */
fx_wchar *l_buf, *l_buf_ptr, *l_buf_end;
/* pointer to the byte AFTER the last byte of the user's input */
fx_wchar *l_line_end;
/* 2-dimensional coordinates of the current cursor position.
* this does NOT include any prompts that are visible on the terminal */
size_t l_cursor_x, l_cursor_y;
/* the number of line continuations that have been inputted */
unsigned int l_continuations;
struct bshell_line_source l_line_source;
/* pointer to tty interface */
fx_tty *l_tty;
/* the lexical scope that we are currently in.
* this is provided by components further up the input pipeline,
* for example, when we are inside a string or if-statement. */
const char *l_scope_type;
/* array of previously entered commands */
fx_array *l_history;
/* index of the currently selected history entry */
size_t l_history_pos;
/* list of defined highlight ranges */
fx_queue l_hl_ranges;
/* list of installed hooks */
fx_queue l_hooks;
};
extern struct line_ed *line_ed_create(void);
extern void line_ed_destroy(struct line_ed *ed);
extern void line_ed_set_flags(struct line_ed *ed, enum line_ed_flags flags);
extern void line_ed_set_scope_type(struct line_ed *ed, const char *scope_type);
extern void line_ed_put_highlight(
struct line_ed *ed,
unsigned long start_x,
unsigned long start_y,
unsigned long end_x,
unsigned long end_y,
const struct fx_tty_vmode *vmode);
extern void line_ed_clear_highlights(struct line_ed *ed);
extern void line_ed_print_highlights(struct line_ed *ed);
extern void line_ed_add_hook(struct line_ed *ed, struct line_ed_hook *hook);
extern void line_ed_remove_hook(struct line_ed *ed, struct line_ed_hook *hook);
extern size_t line_ed_readline(struct line_ed *ed, fx_stringstream *out);
static inline struct bshell_line_source *line_ed_to_line_source(
struct line_ed *ed)
{
return &ed->l_line_source;
}
#endif
-27
View File
@@ -1,27 +0,0 @@
#include <stdio.h>
#include "line-ed.h"
#include "prompt.h"
void show_prompt(struct line_ed *ed)
{
int type = PROMPT_MAIN;
if (ed->l_scope_type) {
type = PROMPT_CONT;
/* this is a temporary solution to show the current
* scope type, until prompts are implemented properly. */
fputs(ed->l_scope_type, stdout);
}
if (ed->l_continuations > 0) {
type = PROMPT_CONT;
}
fputs(ed->l_prompt[type], stdout);
fflush(stdout);
}
size_t prompt_length(struct line_ed *ed, int prompt_id)
{
return strlen(ed->l_prompt[prompt_id]);
}
-12
View File
@@ -1,12 +0,0 @@
#ifndef LINE_ED_PROMPT_H_
#define LINE_ED_PROMPT_H_
#define PROMPT_MAIN 0
#define PROMPT_CONT 1
struct line_ed;
extern void show_prompt(struct line_ed *ed);
extern size_t prompt_length(struct line_ed *ed, int prompt_id);
#endif
-266
View File
@@ -1,266 +0,0 @@
#include "refresh.h"
#include "buffer.h"
#include "cursor.h"
#include "line-ed.h"
#include <fx/term/tty.h>
#include <fx/wstr.h>
#include <stdio.h>
#include <stdlib.h>
/* prints the provided string to the terminal, applying any relevant highlight
* ranges. this function prints all characters in `s` until it encounters a null
* char (\0) or linefeed (\n).
*
* the (x, y) coordinates provided should be the data coordinates of the
* first character in `s`.
*/
void print_text(struct line_ed *ed, size_t x, size_t y, const fx_wchar *s)
{
fx_tty *tty = ed->l_tty;
for (size_t i = 0; s[i] != '\n' && s[i] != '\0'; i++) {
fx_stream_write_char(fx_stdout, s[i]);
}
}
void print_buffer(struct line_ed *ed)
{
const fx_wchar *line_buf = ed->l_buf;
size_t line_len = fx_wstrcspn(line_buf, L"\n");
unsigned int y = 0;
while (1) {
print_text(ed, 0, y, line_buf);
line_buf += line_len;
if (*line_buf == '\n') {
line_buf++;
}
if (*line_buf == '\0') {
break;
}
y++;
line_len = fx_wstrcspn(line_buf, L"\n");
fputc('\r', stdout);
fputc('\n', stdout);
}
}
/* this function is called after a character is inserted into the line_ed
*buffer. the function performs the following steps:
* 1. get a pointer to the start of the line that was modified.
* 2. determine the first character in the line that needs to be redrawn.
* this may result in an append, a partial reprint, or a full reprint.
* 3. re-print the relevant portion of the buffer:
* for an append (a character added to the end of the line):
* * write the inserted char to the terminal.
* * done.
* for a partial reprint:
* * clear all printed chars from the logical cursor position to
*the end of the line.
* * print the portion of the line corresponding to the cleared
*section.
* * move the physical (terminal) cursor backwards until its
*position matches the logical (line_ed) cursor. for a full reprint:
* * same as a partial reprint except that, rather than reprinting
* from the logical cursor position, the entire line is
*reprinted.
*/
void put_refresh(struct line_ed *ed, struct refresh_state *state)
{
/* get the data for the line that is being refreshed */
const fx_wchar *line_buf = line_start(ed, ed->l_cursor_y);
size_t line_len = fx_wstrcspn(line_buf, L"\n");
/* the index of the first char in line_buf that needs to be reprinted */
size_t start_x = state->r_prev_cursor_x;
/* the distance between the first char to be reprinted and the end
* of the line.
* the physical cursor will be moved back by this amount after the
* line is reprinted. */
long cursor_rdelta = (long)(line_len - start_x);
if (ed->l_flags & LINE_ED_FULL_REPRINT) {
if (start_x) {
// fprintf(stdout, "\033[%uD", start_x);
fx_tty_move_cursor_x(
ed->l_tty,
FX_TTY_POS_CURSOR,
-(long long)start_x);
}
start_x = 0;
}
print_text(ed, start_x, ed->l_cursor_y, line_buf + start_x);
/* decrement the rdelta (move the cursor back one fewer cells),
* so that the physical cursor will be placed AFTER the character that
* was just inserted. */
cursor_rdelta--;
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_CURSOR, -cursor_rdelta);
#if 0
for (unsigned int i = 0; i < cursor_rdelta; i++) {
fputs("\010", stdout);
}
#endif
fflush(stdout);
}
/* this function is called after a character is removed from the line_ed buffer.
* IF the character was a linefeed.
*
* this is separate from backspace_simple_refresh because, in this situation,
* the cursor position depends on the length of the previous line before
* the linefeed was deleted, and we have to reprint every line following the
* two that were combined.
*/
void backspace_nl_refresh(struct line_ed *ed, struct refresh_state *state)
{
/* get the data for the line that is being refreshed.
* relative to where the cursor was before the linefeed was deleted,
* this is the PREVIOUS line. */
const fx_wchar *line_buf = line_start(ed, ed->l_cursor_y);
size_t line_len = fx_wstrcspn(line_buf, L"\n");
/* the index of the first char in line_buf that needs to be reprinted.
* subtract one to account for the linefeed that has since been deleted.
*/
size_t start_x = state->r_prev_line_len - 1;
/* the column to move the physical cursor to after it has been moved
* to the previous line.
* NOTE that this number includes the length of the prompt!
* we add 1 to start_x to ensure that the cursor is moved to the cell
* AFTER the last char of the line. */
size_t new_x;
line_ed_coords_to_physical_coords(
ed,
start_x + 1,
ed->l_cursor_y,
&new_x,
NULL);
/* the physical cursor is currently at the beginning of the line that
* has just been moved up. from here, clear this line and the rest
* from the screen. */
// fputs("\033[J", stdout);
fx_tty_clear(ed->l_tty, FX_TTY_CLEAR_SCREEN | FX_TTY_CLEAR_FROM_CURSOR);
if (ed->l_flags & LINE_ED_FULL_REPRINT) {
/* next, move the physical cursor up and to the beginning of the
* previous line */
size_t tmp_x;
line_ed_coords_to_physical_coords(
ed,
0,
ed->l_cursor_y,
&tmp_x,
NULL);
fx_tty_move_cursor_y(ed->l_tty, FX_TTY_POS_CURSOR, -1);
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_START, tmp_x);
// fprintf(stdout, "\033[A\033[%uG", tmp_x + 1);
start_x = 0;
} else {
/* next, move the physical cursor up and to the end of the
* previous line */
// fprintf(stdout, "\033[A\033[%uG", new_x);
fx_tty_move_cursor_y(ed->l_tty, FX_TTY_POS_CURSOR, -1);
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_START, new_x);
}
/* now reprint all of the buffer lines, starting with the first of the
* two lines that were concatenated. */
size_t ydiff = 0;
while (1) {
print_text(
ed,
start_x,
ed->l_cursor_y + ydiff,
line_buf + start_x);
line_buf += line_len + 1;
line_len = fx_wstrcspn(line_buf, L"\n");
start_x = 0;
if (*line_buf == '\0') {
break;
}
fputc('\r', stdout);
fputc('\n', stdout);
ydiff++;
}
/* finally, move the cursor BACK to the point where the two lines
* were concatenated. */
if (ydiff) {
// fprintf(stdout, "\033[%uA", ydiff);
fx_tty_move_cursor_y(ed->l_tty, FX_TTY_POS_CURSOR, ydiff);
}
// fprintf(stdout, "\033[%uG", new_x);
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_START, new_x);
fflush(stdout);
}
/* this function is called after a character is removed from the line_ed buffer.
* IF the character was not a linefeed.
*/
void backspace_simple_refresh(struct line_ed *ed, struct refresh_state *state)
{
/* get the data for the line that is being refreshed */
const fx_wchar *line_buf = line_start(ed, ed->l_cursor_y);
size_t line_len = fx_wstrcspn(line_buf, L"\n");
/* the index of the first char in line_buf that needs to be reprinted */
size_t start_x = ed->l_cursor_x;
// get_data_cursor_position(ed, &start_x, NULL);
/* the distance between the first char to be reprinted and the end
* of the line.
* the physical cursor will be moved back by this amount after the
* line is reprinted. */
long long cursor_rdelta = (long long)(line_len - start_x);
if (ed->l_flags & LINE_ED_FULL_REPRINT) {
if (start_x) {
// fprintf(stdout, "\033[%uD", start_x);
fx_tty_move_cursor_x(
ed->l_tty,
FX_TTY_POS_CURSOR,
-(long long)start_x);
}
start_x = 0;
}
// fputc('\010', stdout);
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_CURSOR, -1);
print_text(ed, start_x, ed->l_cursor_y, line_buf + start_x);
fputc(' ', stdout);
/* increment the rdelta (move the cursor back one more cell), so
* that the cursor will appear to move back one cell (to accord with
* the fact that backspace was just pressed) */
cursor_rdelta++;
fx_tty_move_cursor_x(ed->l_tty, FX_TTY_POS_CURSOR, -cursor_rdelta);
#if 0
for (unsigned int i = 0; i < cursor_rdelta; i++) {
fputs("\010", stdout);
}
#endif
fflush(stdout);
}
-33
View File
@@ -1,33 +0,0 @@
#ifndef LINE_ED_REFRESH_H_
#define LINE_ED_REFRESH_H_
#include <fx/encoding.h>
#include <stddef.h>
struct line_ed;
struct refresh_state {
/* cursor position before the update was performed (excluding the
* prompt) */
size_t r_prev_cursor_x, r_prev_cursor_y;
/* when a backspace results in two separate lines being combined,
* this property contains the length of the first of the two combined
* lines BEFORE the concotenation was performed */
size_t r_prev_line_len;
};
extern void print_text(
struct line_ed *ed,
size_t x,
size_t y,
const fx_wchar *s);
extern void print_buffer(struct line_ed *ed);
extern void put_refresh(struct line_ed *ed, struct refresh_state *state);
extern void backspace_nl_refresh(
struct line_ed *ed,
struct refresh_state *state);
extern void backspace_simple_refresh(
struct line_ed *ed,
struct refresh_state *state);
#endif
+66 -22
View File
@@ -4,6 +4,10 @@
#include <bshell/compile.h> #include <bshell/compile.h>
#include <bshell/file.h> #include <bshell/file.h>
#include <bshell/format.h> #include <bshell/format.h>
#include <bshell/line-editor/core.h>
#include <bshell/line-editor/highlight.h>
#include <bshell/line-editor/line-editor.h>
#include <bshell/line-editor/syntax.h>
#include <bshell/parse/lex.h> #include <bshell/parse/lex.h>
#include <bshell/parse/parse.h> #include <bshell/parse/parse.h>
#include <bshell/parse/token.h> #include <bshell/parse/token.h>
@@ -20,6 +24,7 @@ enum {
OPT_PRINT_LEX, OPT_PRINT_LEX,
OPT_PRINT_AST, OPT_PRINT_AST,
OPT_PRINT_BYTECODE, OPT_PRINT_BYTECODE,
OPT_NO_PROFILE,
ARG_SCRIPT_FILE, ARG_SCRIPT_FILE,
}; };
@@ -70,6 +75,57 @@ static enum frontend_flags collect_frontend_flags(const fx_arglist *opt)
return result; return result;
} }
static void new_line_ed(void)
{
bshell_line_ed_core *ed_core = bshell_line_ed_core_create();
bshell_line_ed_highlight *ed_highlight
= bshell_line_ed_highlight_create();
bshell_line_ed_syntax *ed_syntax = bshell_line_ed_syntax_create();
bshell_line_ed *ed = bshell_line_ed_create();
bshell_line_ed_add_plugin(ed, ed_core);
bshell_line_ed_add_plugin(ed, ed_highlight);
bshell_line_ed_add_plugin(ed, ed_syntax);
fx_string *line = fx_string_create();
while (1) {
fx_string_clear(line);
enum bshell_status status = bshell_line_ed_readline(ed, line);
printf("read line: '%s'\n", fx_string_get_cstr(line));
}
fx_string_unref(line);
bshell_line_ed_unref(ed);
}
static enum bshell_status load_profile(struct frontend_ctx *ctx)
{
enum bshell_status status = BSHELL_SUCCESS;
fx_path *config_base_dir = fx_path_get_system(FX_PATH_CONFIG);
fx_path *profile_path = fx_path_join_list(
3,
&FX_VALUE_OBJECT(config_base_dir),
&FX_CSTR("bshell"),
&FX_CSTR("profile.bshell"));
fx_path_unref(config_base_dir);
if (fx_path_is_file(profile_path)) {
status = frontend_eval_file(
ctx,
NULL,
fx_path_get_cstr(profile_path));
if (status != BSHELL_SUCCESS) {
fprintf(stderr,
"%s: cannot evaluate '%s': %s\n",
exec_name,
fx_path_get_cstr(profile_path),
bshell_status_get_description(status));
}
}
fx_path_unref(profile_path);
return status;
}
static int bshell( static int bshell(
const fx_command *self, const fx_command *self,
const fx_arglist *opt, const fx_arglist *opt,
@@ -84,29 +140,11 @@ static int bshell(
frontend_init(&ctx, flags); frontend_init(&ctx, flags);
enum bshell_status status = BSHELL_SUCCESS; enum bshell_status status = BSHELL_SUCCESS;
fx_path *config_base_dir = fx_path_get_system(FX_PATH_CONFIG); #if 1
fx_path *profile_path = fx_path_join_list( if (!fx_arglist_get_count(opt, OPT_NO_PROFILE, FX_COMMAND_INVALID_ID)) {
3, status = load_profile(&ctx);
&FX_VALUE_OBJECT(config_base_dir),
&FX_CSTR("bshell"),
&FX_CSTR("profile.bshell"));
fx_path_unref(config_base_dir);
if (fx_path_is_file(profile_path)) {
status = frontend_eval_file(
&ctx,
NULL,
fx_path_get_cstr(profile_path));
if (status != BSHELL_SUCCESS) {
fprintf(stderr,
"%s: cannot evaluate '%s': %s\n",
exec_name,
fx_path_get_cstr(profile_path),
bshell_status_get_description(status));
}
} }
fx_path_unref(profile_path); #endif
size_t nr_input_files = fx_arglist_get_count( size_t nr_input_files = fx_arglist_get_count(
opt, opt,
@@ -198,6 +236,12 @@ FX_COMMAND(CMD_ROOT, FX_COMMAND_INVALID_ID)
FX_OPTION_DESC("Print bytecode generated from shell input."); FX_OPTION_DESC("Print bytecode generated from shell input.");
} }
FX_COMMAND_OPTION(OPT_NO_PROFILE)
{
FX_OPTION_LONG_NAME("no-profile");
FX_OPTION_DESC("Don't load the current user's profile script.");
}
FX_COMMAND_ARG(ARG_SCRIPT_FILE) FX_COMMAND_ARG(ARG_SCRIPT_FILE)
{ {
FX_ARG_DESC("Script file to evaluate."); FX_ARG_DESC("Script file to evaluate.");