Skip to content

Commit f9f85af

Browse files
committed
up and down arrow support + stdlib system
1 parent c63efce commit f9f85af

File tree

14 files changed

+1735
-1612
lines changed

14 files changed

+1735
-1612
lines changed

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ RIC_SOURCES = \
3737
interpret \
3838
print \
3939
prioqueue \
40+
stdin \
4041
$(LIB_DIR)/libio \
4142
$(LIB_DIR)/libmath \
4243
$(LIB_DIR)/libstd \

src/ast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ typedef struct libFunction {
586586
intptr_t p; \
587587
*sb = calloc(sz + 1, sizeof(stackval_t)); \
588588
assert(*sb != NULL); \
589-
p = ((intptr_t) * sb) % sizeof(stackval_t); \
589+
p = ((intptr_t)*sb) % sizeof(stackval_t); \
590590
if (p != 0) { \
591591
p = (sizeof(stackval_t) - (p % sizeof(stackval_t))); \
592592
} \
@@ -600,7 +600,7 @@ typedef struct libFunction {
600600
heapval_t hpbv; \
601601
*hb = calloc(hz + 2, sizeof(heapval_t)); \
602602
assert(*hb != NULL); \
603-
p = ((intptr_t) * hb) % sizeof(heapval_t); \
603+
p = ((intptr_t)*hb) % sizeof(heapval_t); \
604604
p = (sizeof(heapval_t) - (p % sizeof(heapval_t))); \
605605
hpbv.sv.type = INT32TYPE; \
606606
hpbv.sv.i = (int32_t)hz; \

src/gram.y

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,7 @@ void runInteractive(int argc, char *argv[], interactiveInterpreterFunc func, int
940940
941941
PRINT_INTERACTIVE_BANNER();
942942
943-
printf("%s", prompt);
944-
while ( fgets(lineBuffer, sizeof(lineBuffer), stdin) != NULL ) {
943+
while ( readCommand(lineBuffer, sizeof(lineBuffer), prompt) != NULL ) {
945944
YY_BUFFER_STATE buffer;
946945
947946
/* Check if the user wants to quit */
@@ -959,7 +958,6 @@ void runInteractive(int argc, char *argv[], interactiveInterpreterFunc func, int
959958
func(argc, argv, root, 0, stacksize, heapsize);
960959
}
961960
962-
printf("%s", prompt);
963961
memset(lineBuffer, 0, sizeof(lineBuffer));
964962
}
965963
}

src/hooks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ void runInteractive(int argc, char *argv[], interactiveInterpreterFunc func, int
2020
int heapsize, const char *prompt);
2121
void runCommand(int argc, char *argv[], interactiveInterpreterFunc func, char *command,
2222
int stacksize, int heapsize);
23+
char *readCommand(char *lineBuffer, size_t size, const char *prompt);
2324

2425
#endif

src/lex.yy.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#line 2 "lex.yy.c"
2+
#line 3 "lex.yy.c"
33

44
#define YY_INT_ALIGNED short int
55

@@ -46,7 +46,6 @@ typedef int16_t flex_int16_t;
4646
typedef uint16_t flex_uint16_t;
4747
typedef int32_t flex_int32_t;
4848
typedef uint32_t flex_uint32_t;
49-
typedef uint64_t flex_uint64_t;
5049
#else
5150
typedef signed char flex_int8_t;
5251
typedef short int flex_int16_t;
@@ -155,7 +154,7 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE;
155154
typedef size_t yy_size_t;
156155
#endif
157156

158-
extern yy_size_t yyleng;
157+
extern int yyleng;
159158

160159
extern FILE *yyin, *yyout;
161160

@@ -198,7 +197,7 @@ struct yy_buffer_state
198197
/* Number of characters read into yy_ch_buf, not including EOB
199198
* characters.
200199
*/
201-
yy_size_t yy_n_chars;
200+
int yy_n_chars;
202201

203202
/* Whether we "own" the buffer - i.e., we know we created it,
204203
* and can realloc() it to grow it, and should free() it to
@@ -267,8 +266,8 @@ static YY_BUFFER_STATE * yy_buffer_stack = NULL; /**< Stack as an array. */
267266

268267
/* yy_hold_char holds the character lost when yytext is formed. */
269268
static char yy_hold_char;
270-
static yy_size_t yy_n_chars; /* number of characters read into yy_ch_buf */
271-
yy_size_t yyleng;
269+
static int yy_n_chars; /* number of characters read into yy_ch_buf */
270+
int yyleng;
272271

273272
/* Points to current character in buffer. */
274273
static char *yy_c_buf_p = NULL;
@@ -295,7 +294,7 @@ static void yy_init_buffer ( YY_BUFFER_STATE b, FILE *file );
295294

296295
YY_BUFFER_STATE yy_scan_buffer ( char *base, yy_size_t size );
297296
YY_BUFFER_STATE yy_scan_string ( const char *yy_str );
298-
YY_BUFFER_STATE yy_scan_bytes ( const char *bytes, yy_size_t len );
297+
YY_BUFFER_STATE yy_scan_bytes ( const char *bytes, int len );
299298

300299
void *yyalloc ( yy_size_t );
301300
void *yyrealloc ( void *, yy_size_t );
@@ -351,7 +350,7 @@ static void yynoreturn yy_fatal_error ( const char* msg );
351350
*/
352351
#define YY_DO_BEFORE_ACTION \
353352
(yytext_ptr) = yy_bp; \
354-
yyleng = (yy_size_t) (yy_cp - yy_bp); \
353+
yyleng = (int) (yy_cp - yy_bp); \
355354
(yy_hold_char) = *yy_cp; \
356355
*yy_cp = '\0'; \
357356
(yy_c_buf_p) = yy_cp;
@@ -510,7 +509,7 @@ FILE *yyget_out ( void );
510509

511510
void yyset_out ( FILE * _out_str );
512511

513-
yy_size_t yyget_leng ( void );
512+
int yyget_leng ( void );
514513

515514
char *yyget_text ( void );
516515

@@ -579,7 +578,7 @@ static int input ( void );
579578
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
580579
{ \
581580
int c = '*'; \
582-
yy_size_t n; \
581+
int n; \
583582
for ( n = 0; n < max_size && \
584583
(c = getc( yyin )) != EOF && c != '\n'; ++n ) \
585584
buf[n] = (char) c; \
@@ -1051,7 +1050,7 @@ static int yy_get_next_buffer (void)
10511050

10521051
else
10531052
{
1054-
yy_size_t num_to_read =
1053+
int num_to_read =
10551054
YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1;
10561055

10571056
while ( num_to_read <= 0 )
@@ -1065,7 +1064,7 @@ static int yy_get_next_buffer (void)
10651064

10661065
if ( b->yy_is_our_buffer )
10671066
{
1068-
yy_size_t new_size = b->yy_buf_size * 2;
1067+
int new_size = b->yy_buf_size * 2;
10691068

10701069
if ( new_size <= 0 )
10711070
b->yy_buf_size += b->yy_buf_size / 8;
@@ -1123,7 +1122,7 @@ static int yy_get_next_buffer (void)
11231122

11241123
if (((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
11251124
/* Extend the array by 50%, plus the number we really need. */
1126-
yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1);
1125+
int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1);
11271126
YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yyrealloc(
11281127
(void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf, (yy_size_t) new_size );
11291128
if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
@@ -1212,7 +1211,7 @@ static int yy_get_next_buffer (void)
12121211
if ( yy_cp < YY_CURRENT_BUFFER_LVALUE->yy_ch_buf + 2 )
12131212
{ /* need to shift things up to make room */
12141213
/* +2 for EOB chars. */
1215-
yy_size_t number_to_move = (yy_n_chars) + 2;
1214+
int number_to_move = (yy_n_chars) + 2;
12161215
char *dest = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[
12171216
YY_CURRENT_BUFFER_LVALUE->yy_buf_size + 2];
12181217
char *source =
@@ -1263,7 +1262,7 @@ static int yy_get_next_buffer (void)
12631262

12641263
else
12651264
{ /* need more input */
1266-
yy_size_t offset = (yy_c_buf_p) - (yytext_ptr);
1265+
int offset = (int) ((yy_c_buf_p) - (yytext_ptr));
12671266
++(yy_c_buf_p);
12681267

12691268
switch ( yy_get_next_buffer( ) )
@@ -1632,12 +1631,12 @@ YY_BUFFER_STATE yy_scan_string (const char * yystr )
16321631
*
16331632
* @return the newly allocated buffer state object.
16341633
*/
1635-
YY_BUFFER_STATE yy_scan_bytes (const char * yybytes, yy_size_t _yybytes_len )
1634+
YY_BUFFER_STATE yy_scan_bytes (const char * yybytes, int _yybytes_len )
16361635
{
16371636
YY_BUFFER_STATE b;
16381637
char *buf;
16391638
yy_size_t n;
1640-
yy_size_t i;
1639+
int i;
16411640

16421641
/* Get memory for full buffer, including space for trailing EOB's. */
16431642
n = (yy_size_t) (_yybytes_len + 2);
@@ -1679,7 +1678,7 @@ static void yynoreturn yy_fatal_error (const char* msg )
16791678
do \
16801679
{ \
16811680
/* Undo effects of setting up yytext. */ \
1682-
yy_size_t yyless_macro_arg = (n); \
1681+
int yyless_macro_arg = (n); \
16831682
YY_LESS_LINENO(yyless_macro_arg);\
16841683
yytext[yyleng] = (yy_hold_char); \
16851684
(yy_c_buf_p) = yytext + yyless_macro_arg; \
@@ -1719,7 +1718,7 @@ FILE *yyget_out (void)
17191718
/** Get the length of the current token.
17201719
*
17211720
*/
1722-
yy_size_t yyget_leng (void)
1721+
int yyget_leng (void)
17231722
{
17241723
return yyleng;
17251724
}

src/lib.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ libFunction_t ric_library[] = {
4141
DECLARE_LIB_FUNCTION("sort", 1, ric_sort),
4242
DECLARE_LIB_FUNCTION("join", 2, ric_join),
4343
DECLARE_LIB_FUNCTION("cachepot", 0, ric_cachepot),
44+
DECLARE_LIB_FUNCTION("system", 1, ric_system),
4445
// libjson
4546
DECLARE_LIB_FUNCTION("jsonLoad", 1, ric_json_load),
4647
DECLARE_LIB_FUNCTION("jsonConvert", 1, ric_json_convert),

src/library/libstd.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,3 +1713,32 @@ int ric_get_env(LIBRARY_PARAMS()) {
17131713

17141714
return 0;
17151715
}
1716+
1717+
int ric_system(LIBRARY_PARAMS()) {
1718+
stackval_t stv;
1719+
char *argText;
1720+
int result = 0;
1721+
void *sp = PROVIDE_CONTEXT()->sp;
1722+
size_t *sc = PROVIDE_CONTEXT()->sc;
1723+
1724+
// pop arg 1 - directory to search in
1725+
POP_VAL(&stv, sp, sc);
1726+
1727+
switch (stv.type) {
1728+
case TEXT:
1729+
argText = stv.t;
1730+
break;
1731+
default: {
1732+
fprintf(stderr,
1733+
"error: function call '%s' got unexpected data type as argument, string expected.\n",
1734+
LIBRARY_FUNC_NAME());
1735+
exit(1);
1736+
} break;
1737+
}
1738+
1739+
result = system(argText);
1740+
1741+
/* Pushing the result */
1742+
PUSH_INT(result, sp, sc);
1743+
return 0;
1744+
}

src/library/libstd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ int ric_sort(LIBRARY_PARAMS());
4141
int ric_join(LIBRARY_PARAMS());
4242
int ric_cachepot(LIBRARY_PARAMS());
4343
int ric_get_env(LIBRARY_PARAMS());
44+
int ric_system(LIBRARY_PARAMS());
4445

4546
#endif

src/meson.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ if ( target_os == 'windows' )
4444
deps += [dep]
4545

4646
# Adding windows specific translation units
47-
sources += ['sync_nt.c', 'dl_nt.c']
47+
sources += ['stdin_nt.c', 'sync_nt.c', 'dl_nt.c']
4848
else
4949
# Math as depencendy, not needed in MSVC but needed elsewhere
5050
mathlib = cc.find_library('m', required: true)
@@ -58,7 +58,7 @@ else
5858
ric_c_args += ['-Wno-unused-parameter', '-Wno-unused-function', '-Wno-sign-compare']
5959

6060
# Adding POSIX translation units (for darwin, linux etc.)
61-
sources += ['sync.c', 'dl.c']
61+
sources += ['stdin.c', 'sync.c', 'dl.c']
6262
endif
6363

6464
subdir('external')

src/sdk_lib/meson.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ if ( target_os == 'windows' )
164164
includes += [pcre_inc]
165165

166166
# Adding windows specific translation units
167-
sources += [join_paths('..','sync_nt.c'), join_paths('..','dl_nt.c')]
167+
sources += [join_paths('..','stdin_nt.c'), join_paths('..','sync_nt.c'), join_paths('..','dl_nt.c')]
168168
else
169169
# Math as depencendy, not needed in MSVC but needed elsewhere
170170
mathlib = cc.find_library('m', required: true)
@@ -178,7 +178,7 @@ else
178178
ric_c_args += ['-Wno-unused-parameter', '-Wno-unused-function']
179179

180180
# Adding POSIX translation units (for darwin, linux etc.)
181-
sources += [join_paths('..','sync.c'), join_paths('..','dl.c')]
181+
sources += [join_paths('..','stdin.c'), join_paths('..','sync.c'), join_paths('..','dl.c')]
182182
endif
183183

184184
if ( target_os == 'windows' )

src/stdin.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <unistd.h>
4+
#include <termios.h>
5+
6+
#define MAX_LINE 128
7+
#define HISTORY_SIZE 10
8+
9+
static char history[HISTORY_SIZE][MAX_LINE];
10+
static int history_count = 0;
11+
static int history_index = -1;
12+
13+
void enable_raw_mode(struct termios *orig_termios) {
14+
struct termios raw;
15+
tcgetattr(STDIN_FILENO, orig_termios);
16+
raw = *orig_termios;
17+
18+
raw.c_lflag &= ~(ICANON | ECHO); // disable canonical mode & echo
19+
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
20+
}
21+
22+
void disable_raw_mode(struct termios *orig_termios) {
23+
tcsetattr(STDIN_FILENO, TCSAFLUSH, orig_termios);
24+
}
25+
26+
char *readCommand(char *lineBuffer, size_t size, const char *prompt) {
27+
struct termios orig_termios;
28+
enable_raw_mode(&orig_termios);
29+
30+
int pos = 0;
31+
int c;
32+
lineBuffer[0] = '\0';
33+
printf("%s", prompt);
34+
fflush(stdout);
35+
36+
while (1) {
37+
c = getchar();
38+
39+
if (c == 27) { // ESC
40+
if (getchar() == '[') {
41+
c = getchar();
42+
if (c == 'A') { // UP
43+
if (history_count > 0 && history_index < history_count - 1) {
44+
history_index++;
45+
strcpy(lineBuffer, history[history_count - 1 - history_index]);
46+
printf("\r%s%s\033[K", prompt, lineBuffer);
47+
pos = strlen(lineBuffer);
48+
fflush(stdout);
49+
}
50+
} else if (c == 'B') { // DOWN
51+
if (history_index > 0) {
52+
history_index--;
53+
strcpy(lineBuffer, history[history_count - 1 - history_index]);
54+
printf("\r%s%s\033[K", prompt, lineBuffer);
55+
pos = strlen(lineBuffer);
56+
fflush(stdout);
57+
} else if (history_index == 0) {
58+
history_index = -1;
59+
lineBuffer[0] = '\0';
60+
printf("\r%s\033[K", prompt);
61+
pos = 0;
62+
fflush(stdout);
63+
}
64+
}
65+
}
66+
} else if (c == '\n') {
67+
break;
68+
} else if (c == 127 || c == '\b') { // Backspace
69+
if (pos > 0) {
70+
pos--;
71+
lineBuffer[pos] = '\0';
72+
printf("\r%s%s \b\033[K", prompt, lineBuffer);
73+
fflush(stdout);
74+
}
75+
} else if (pos < size - 1) {
76+
lineBuffer[pos++] = c;
77+
lineBuffer[pos] = '\0';
78+
putchar(c);
79+
fflush(stdout);
80+
}
81+
}
82+
83+
printf("\n");
84+
disable_raw_mode(&orig_termios);
85+
86+
// Save to history
87+
if (lineBuffer[0] != '\0') {
88+
if (history_count < HISTORY_SIZE) {
89+
strcpy(history[history_count++], lineBuffer);
90+
}
91+
}
92+
history_index = -1;
93+
94+
return lineBuffer;
95+
}

0 commit comments

Comments
 (0)