Skip to content

Commit

Permalink
GH-92 Add ability to disable line editing & history with -h 0.
Browse files Browse the repository at this point in the history
Fix handling of newlines to use Unix '\n' by default unless NL
C macro is overriden.
  • Loading branch information
SirWumpus committed Dec 20, 2024
1 parent a452808 commit 73243aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/aline.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ alineInit(int hist_size)
}

/* Size as next highest power of 2. */
for (histsize = 2, hist_size--; hist_size >>= 1; ) {
for (histsize = 0 < hist_size, hist_size -= histsize; histsize < hist_size; ) {
histsize = histsize << 1;
}

Expand Down Expand Up @@ -179,7 +179,7 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
int ch, pcol, pos[2], prevline = lastline;

(void) fflush(stdout);
if (!isatty(fileno(fp))) {
if (histsize == 0 || !isatty(fileno(fp))) {
*buf = '\0';
clearerr(fp); errno = 0;
(void) alineSetMode(ALINE_CANONICAL);
Expand Down
42 changes: 21 additions & 21 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
***********************************************************************/

static const char usage[] =
"usage: post4 [-TV][-b file][-c file][-h size][-i file][-m size]\r\n"
" [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"
"-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"
"-T\t\tenable tracing; see TRACE\r\n"
"-V\t\tbuild and version information\r\n\r\n"
"If script is \"-\", read it from standard input.\r\n"
"usage: post4 [-TV][-b file][-c file][-h size][-i file][-m size]" NL
" [script [args ...]]" NL
"" NL
"-b file\t\topen a block file" NL
"-c file\t\tword definition file; default " P4_CORE_FILE " from $POST4_PATH" NL
"-h size\t\thistory size in lines; default " QUOTE(ALINE_HISTORY) "" NL
"-i file\t\tinclude file; can be repeated; searches $POST4_PATH" NL
"-m size\t\tdata space memory in KB; default " QUOTE(P4_MEM_SIZE) "" NL
"-T\t\tenable tracing; see TRACE" NL
"-V\t\tbuild and version information\r\n" NL
"If script is \"-\", read it from standard input." NL
;

static char *flags = "b:c:d:f:h:i:m:r:TV";
Expand All @@ -37,13 +37,13 @@ static P4_Options options = {
};

static const char p4_build_info[] =
P4_NAME "/" P4_VERSION " " P4_COPYRIGHT "\r\n\r\n"
"BUILT=\"" P4_BUILT "\"\r\n"
"COMMIT=\"" P4_COMMIT "\"\r\n"
"CFLAGS=\"" P4_CFLAGS "\"\r\n"
"LDFLAGS=\"" P4_LDFLAGS "\"\r\n"
"LIBS=\"" P4_LIBS "\"\r\n"
"POST4_PATH=\"" P4_CORE_PATH "\"\r\n"
P4_NAME "/" P4_VERSION " " P4_COPYRIGHT "" NL
"BUILT=\"" P4_BUILT "\"" NL
"COMMIT=\"" P4_COMMIT "\"" NL
"CFLAGS=\"" P4_CFLAGS "\"" NL
"LDFLAGS=\"" P4_LDFLAGS "\"" NL
"LIBS=\"" P4_LIBS "\"" NL
"POST4_PATH=\"" P4_CORE_PATH "\"" NL
;

static void
Expand All @@ -66,7 +66,7 @@ main(int argc, char **argv)
while ((ch = getopt(argc, argv, flags)) != -1) {
unsigned val = 0;
if (optarg != NULL) {
val = (unsigned) strtol(optarg, NULL, 10);
val = strtoul(optarg, NULL, 10);
}
switch (ch) {
case 'b':
Expand All @@ -91,7 +91,7 @@ main(int argc, char **argv)
(void) printf(
"%s\r\nsizeof char=%zu short=%zu int=%zu long=%zu size_t=%zu "
"intptr_t=%zu float=%zu double=%zu\r\nvoid *=%zu long long=%zu "
"long double=%zu JMP_BUF=%zu\r\n",
"long double=%zu JMP_BUF=%zu" NL,
p4_build_info,
sizeof (char), sizeof (short), sizeof (int), sizeof (long),
sizeof (size_t), sizeof (intptr_t), sizeof (float), sizeof (double),
Expand All @@ -113,7 +113,7 @@ main(int argc, char **argv)
(void) atexit(cleanup);
if ((rc = SETJMP(sig_break_glass)) != 0) {
THROW_MSG(rc);
(void) fprintf(stderr, "\r\n");
(void) fprintf(stderr, "" NL);
return P4_EXIT_STATUS(rc);
}
if ((ctx_main = p4Create(&options)) == NULL) {
Expand Down
38 changes: 22 additions & 16 deletions src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const char *p4_exceptions[] = {
};
#endif

static const char crlf[] = "\r\n";
const char newline[] = NL;

/***********************************************************************
*** Context
Expand Down Expand Up @@ -327,7 +327,7 @@ p4StackDump(FILE *fp, P4_Cell *base, int length)
unsigned count;

if (length < 0 || 1024 <= length) {
(void) fprintf(fp, "stack under or over flow, depth=%d\r\n", length);
(void) fprintf(fp, "stack under or over flow, depth=%d" NL, length);
return;
}
for (count = 0, cell = base; 0 < length--; cell++) {
Expand All @@ -336,11 +336,11 @@ p4StackDump(FILE *fp, P4_Cell *base, int length)
}
(void) fprintf(fp, P4_H0X_FMT" ", cell->u);
if ((++count & 3) == 0) {
(void) fprintf(fp, crlf);
(void) fprintf(fp, newline);
}
}
if ((count & 3) != 0) {
(void) fprintf(fp, crlf);
(void) fprintf(fp, newline);
}
}

Expand Down Expand Up @@ -946,7 +946,7 @@ p4Create(P4_Options *opts)
if (opts->block_file != NULL && *opts->block_file != '\0' /* Block file name? */
&& (ctx->block_fd = fopen(opts->block_file, "rb+")) == NULL /* File exists? */
&& (ctx->block_fd = fopen(opts->block_file, "wb+")) == NULL) { /* Else create file. */
(void) fprintf(STDERR, "post4: %s: %s\r\n", opts->block_file, strerror(errno));
(void) fprintf(STDERR, "post4: %s: %s" NL, opts->block_file, strerror(errno));
}
ctx->norder = 1;
ctx->order[0] = 1;
Expand All @@ -967,7 +967,7 @@ p4Bp(P4_Ctx *ctx)
for (unsigned i = 0; i < input->length-has_nl; i++) {
(void) fputc(input->buffer[i] == '\t' ? ' ' : input->buffer[i], STDERR);
}
(void) fprintf(STDERR, "\r\n>> %*c\r\n", (int)input->offset, '^' );
(void) fprintf(STDERR, "\r\n>> %*c" NL, (int)input->offset, '^' );
}

#pragma GCC diagnostic push
Expand Down Expand Up @@ -1015,7 +1015,7 @@ p4Trace(P4_Ctx *ctx, P4_Xt xt, P4_Cell *ip)
#endif
p4TraceStack(ctx, &ctx->rs, P4_RS_CAN_POP(xt), " ; ");
}
(void) fputs(crlf, STDERR);
(void) fputs(newline, STDERR);
}
}
#else
Expand Down Expand Up @@ -1190,11 +1190,12 @@ p4Repl(P4_Ctx *ctx, volatile int thrown)
/* Constants. */
P4_VAL("R/O", 0),
P4_VAL("R/W", 1),
P4_VAL("/pad", P4_PAD_SIZE), // p4
P4_VAL("address-unit-bits", P4_CHAR_BIT), // p4
P4_VAL("/pad", P4_PAD_SIZE), // p4
P4_VAL("address-unit-bits", P4_CHAR_BIT), // p4
P4_VAL("WORDLISTS", P4_WORDLISTS),
P4_WORD("post4-path", &&_post4_path, 0, 0x02), // p4
P4_WORD("post4-commit", &&_post4_commit,0, 0x02), // p4
P4_WORD("post4-path", &&_post4_path, 0, 0x02), // p4
P4_WORD("post4-commit", &&_post4_commit,0, 0x02), // p4
P4_WORD("newline", &&_newline, 0, 0x02), // p4

/* Internal support. */
P4_WORD("_bp", &&_bp, 0, 0x00), // p4
Expand Down Expand Up @@ -1372,18 +1373,18 @@ p4Repl(P4_Ctx *ctx, volatile int thrown)
p4WordFree(w.nt);
} else {
/* Cannot rely on ip pointing to the xt after the error. */
(void) fprintf(STDERR, crlf);
(void) fprintf(STDERR, newline);
for (x.p = ctx->rs.top; ctx->rs.base <= x.p; x.p--) {
w = (*x.p).p[-1];
y.s = p4IsNt(ctx, w.nt) ? w.nt->name : "";
(void) fprintf(STDERR, P4_H0X_FMT" %s\r\n", (long) w.nt, y.s);
(void) fprintf(STDERR, P4_H0X_FMT" %s" NL, (long) w.nt, y.s);
}
}
/*@fallthrough@*/
case P4_THROW_SIGINT:
case P4_THROW_ABORT_MSG:
/* Ensure ABORT" and other messages print newline.*/
(void) fprintf(STDERR, crlf);
(void) fprintf(STDERR, newline);
/*@fallthrough@*/
case P4_THROW_ABORT:
/* Historically no message, simply return to REPL. */
Expand Down Expand Up @@ -1598,7 +1599,7 @@ _colon: str = p4ParseName(ctx->input);
// Save the current lengths so we can check for imbalance.
_do_colon: ctx->state = P4_STATE_COMPILE;
if (ctx->trace) {
(void) printf("%*s%.*s\r\n", 19+2*(int)ctx->level, "", (int)str.length, str.string);
(void) printf("%*s%.*s" NL, 19+2*(int)ctx->level, "", (int)str.length, str.string);
}
x.nt = p4WordCreate(ctx, str.string, str.length, &&_enter);
p4AllocStack(ctx, &ctx->ds, 1+(x.nt->length == 0));
Expand Down Expand Up @@ -2108,6 +2109,11 @@ _stack_dump: P4_DROP(ctx->ds, 1);
p4StackDump(stdout, w.p, x.n);
NEXT;

// ( -- caddr u )
_newline: P4_PUSH(ctx->ds, (char *) newline);
P4_PUSH(ctx->ds, STRLEN(NL));
NEXT;

// ( -- caddr u )
_post4_path: if ((w.s = getenv("POST4_PATH")) == NULL) {
w.s = P4_CORE_PATH;
Expand Down Expand Up @@ -2638,7 +2644,7 @@ sig_int(int signum)
static void
sig_exit(int signum)
{
(void) fputs(crlf, stdout);
(void) fputs(newline, stdout);
exit(P4_EXIT_SIGNAL(signum));
}

Expand Down
6 changes: 6 additions & 0 deletions src/post4.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ extern "C" {
#define HAVE_HOOKS 1
#endif

#ifndef NL
#define NL "\n"
#endif

/***********************************************************************
*** No configuration below this point.
***********************************************************************/
Expand Down Expand Up @@ -582,6 +586,8 @@ typedef struct {
*** API
***********************************************************************/

extern const char newline[];

extern const char p4_commit[];

/**
Expand Down
19 changes: 8 additions & 11 deletions src/post4.p4
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,18 @@ END-STRUCTURE
: stack-cells _dstk stk.size @ ; $01 _pp!
: return-stack-cells _rstk stk.size @ ; $01 _pp!

\ (S: -- )
: CR newline TYPE ;

\ (S: char -- )
\ Assumes little-endian.
: EMIT dsp@ 1 TYPE DROP ; $10 _pp!

\ ( -- )
: .S 'd' EMIT 's' EMIT '\r' EMIT '\n' EMIT _ds DROP _stack_dump ;
: .S 'd' EMIT 's' EMIT CR _ds DROP _stack_dump ;

\ ( -- )
: .RS 'r' EMIT 's' EMIT '\r' EMIT '\n' EMIT _rs DROP 1 - _stack_dump ;
: .RS 'r' EMIT 's' EMIT CR _rs DROP 1 - _stack_dump ;

\ ( u "<spaces>name" -- addr )
: BUFFER: CREATE ALLOT ; $11 _pp!
Expand Down Expand Up @@ -527,9 +530,6 @@ MAX-U MAX-N 2CONSTANT MAX-D $02 _pp!
\ (S: <spaces>name.new <spaces>name.old -- )
: SYNONYM >IN @ PARSE-NAME 2DROP ' >IN @ SPIN >IN ! alias >IN ! ;

\ (S: -- )
: CR '\r' EMIT '\n' EMIT ;

\ (S: -- )
: SPACE BL EMIT ;

Expand Down Expand Up @@ -1751,10 +1751,7 @@ VARIABLE _str_buf_curr

[DEFINED] WRITE-FILE [IF]
\ ( caddr u fid -- ior )
: WRITE-LINE
DUP >R WRITE-FILE DROP
S\" \r\n" R> WRITE-FILE
;
: WRITE-LINE DUP >R WRITE-FILE DROP newline R> WRITE-FILE ;
[THEN]

\ (S: ctx -- aaddr )
Expand Down Expand Up @@ -2312,7 +2309,7 @@ VARIABLE _do_sys_stk
: FALIGNED ALIGNED ;
: FFIELD: FIELD: ;

: .fs S\" fs\r\n" TYPE _fs DROP _stack_dump ;
: .fs S\" fs" TYPE CR _fs DROP _stack_dump ;

\ (S: -- f ; F: f -- )
\ Move f between stacks _without_ conversion; F>S and S>F convert formats.
Expand Down Expand Up @@ -2821,7 +2818,7 @@ FORTH-WORDLIST SET-CURRENT
: ALSO GET-ORDER OVER SWAP 1+ SET-ORDER ;
: DEFINITIONS GET-ORDER OVER SET-CURRENT SET-ORDER ;

: show_wid ( wid -- ) S\" \e[36m[ " TYPE #. S\" ]\e[0m\r\n" TYPE ; $10 _pp!
: show_wid ( wid -- ) S\" \e[36m[ " TYPE #. S\" ]\e[0m" TYPE CR ; $10 _pp!

: ORDER ( -- )
." Search: " GET-ORDER 0 DO . SPACE LOOP CR
Expand Down

0 comments on commit 73243aa

Please sign in to comment.