Skip to content

Commit

Permalink
GH-63 Add -h option to set the history size; default 16.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Nov 8, 2024
1 parent 563e94b commit 0e4a992
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ Overview

Post4 is a hosted indirect threaded Forth dialect written in C, based on the ["Forth 200x Draft 19.1, 2019-09-30"](http://www.forth200x.org/documents/forth19-1.pdf). Post4 aims to implement the fewest possible built-in words in C, those that are needed to interact with memory and I/O, leaving the remaining standard words to be implemented in Forth.

usage: post4 [-TV][-b file][-c file][-d size][-f size][-i file][-m size]
[-r size][script [args ...]]

usage: post4 [-TV][-b file][-c file][-d size][-f size][-h size][-i file]
[-m size][-r size][script [args ...]]
-b file open a block file
-c file word definition file; default post4.p4 from $POST4_PATH
-d size data stack size in cells; default 64
-f size float stack size; default 6
-h size history size in lines; default 16
-i file include file; can be repeated; searches $POST4_PATH
-m size data space memory in KB; default 128
-r size return stack size in cells; default 64
-T enable tracing; see TRACE
-V build and version information

If script is "-", read it from standard input.


Expand Down
26 changes: 18 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
*/

#include "post4.h"
#include "aline.h"

/***********************************************************************
*** Main
***********************************************************************/

static const char usage[] =
"usage: post4 [-TV][-b file][-c file][-d size][-f size][-i file][-m size]\r\n"
" [-r size][script [args ...]]\r\n"
"usage: post4 [-TV][-b file][-c file][-d size][-f size][-h size][-i file]\r\n"
" [-m size][-r size][script [args ...]]\r\n"
"\r\n"
"-b file\t\topen a block file\r\n"
"-c file\t\tword definition file; default " P4_CORE_FILE " from $POST4_PATH\r\n"
"-d size\t\tdata stack size in cells; default " QUOTE(P4_DATA_STACK_SIZE) "\r\n"
"-f size\t\tfloat stack size; default " QUOTE(P4_FLOAT_STACK_SIZE) "\r\n"
"-h size\t\thistory size in lines; default " QUOTE(ALINE_HISTORY) "\r\n"
"-i file\t\tinclude file; can be repeated; searches $POST4_PATH\r\n"
"-m size\t\tdata space memory in KB; default " QUOTE(P4_MEM_SIZE) "\r\n"
"-r size\t\treturn stack size in cells; default " QUOTE(P4_RETURN_STACK_SIZE) "\r\n"
Expand All @@ -26,7 +28,7 @@ static const char usage[] =
"If script is \"-\", read it from standard input.\r\n"
;

static char *flags = "b:c:d:f:i:m:r:TV";
static char *flags = "b:c:d:f:h:i:m:r:TV";

static P4_Ctx *ctx_main;

Expand All @@ -35,6 +37,7 @@ static P4_Options options = {
.rs_size = P4_RETURN_STACK_SIZE,
.fs_size = P4_FLOAT_STACK_SIZE,
.mem_size = P4_MEM_SIZE,
.hist_size = ALINE_HISTORY,
.core_file = P4_CORE_FILE,
.block_file = NULL,
};
Expand Down Expand Up @@ -66,6 +69,10 @@ main(int argc, char **argv)
int ch, rc;

while ((ch = getopt(argc, argv, flags)) != -1) {
unsigned val;
if (optarg != NULL) {
val = (unsigned) strtol(optarg, NULL, 10);
}
switch (ch) {
case 'b':
options.block_file = optarg;
Expand All @@ -74,23 +81,26 @@ main(int argc, char **argv)
options.core_file = optarg;
break;
case 'd':
options.ds_size = strtol(optarg, NULL, 10);
options.ds_size = val;
break;
case 'f':
#ifdef HAVE_MATH_H
options.fs_size = strtol(optarg, NULL, 10);
options.fs_size = val;
#else
(void) warnx("float support disabled");
#endif
break;
case 'i':
// Ignore for now.
break;
case 'h':
options.hist_size = val;
break;
case 'm':
options.mem_size = strtol(optarg, NULL, 10);
options.mem_size = val;
break;
case 'r':
options.rs_size = strtol(optarg, NULL, 10);
options.rs_size = val;
break;
case 'T':
options.trace++;
Expand All @@ -116,7 +126,7 @@ main(int argc, char **argv)
options.argc = argc - optind;
options.argv = argv + optind;

p4Init();
p4Init(&options);
if ((rc = SETJMP(sig_break_glass)) != 0) {
THROW_MSG(rc);
(void) fprintf(STDERR, "\r\n");
Expand Down
4 changes: 2 additions & 2 deletions src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ static const char crlf[] = "\r\n";
/**
*/
void
p4Init(void)
p4Init(P4_Options *opts)
{
alineInit(ALINE_HISTORY);
alineInit(opts->hist_size);
}

FILE *
Expand Down
3 changes: 2 additions & 1 deletion src/post4.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ typedef struct {
unsigned rs_size;
unsigned fs_size;
unsigned mem_size;
unsigned hist_size;
const char *core_file;
const char *block_file;
} P4_Options;
Expand Down Expand Up @@ -582,7 +583,7 @@ typedef struct {
/**
* Initialise the global environment.
*/
extern void p4Init(void);
extern void p4Init(P4_Options *opts);

/**
* Create a new interpreter context.
Expand Down

0 comments on commit 0e4a992

Please sign in to comment.