Skip to content

Commit

Permalink
GH-86 Sidebar: Move SH and SOURCE from C to Forth.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Dec 10, 2024
1 parent 2664ce4 commit 38f194f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
31 changes: 6 additions & 25 deletions src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,17 @@
#include "post4.h"

#ifdef HAVE_HOOKS

# ifdef HOOK_SHELL
/*
* SH ( `remaining input line` -- )
*/
static void
p4System(P4_Ctx *ctx)
{
P4_Input *input = ctx->input;
// Assumes input buffer is writeable.
input->buffer[input->length] = '\0';
(void) system(input->buffer + input->offset);
input->offset = input->length;
}

/*
* SHELL ( caddr u -- n )
*/
static void
p4SystemString(P4_Ctx *ctx)
p4Shell(P4_Ctx *ctx)
{
P4_DROP(ctx->ds, 1); /* Ignore u */
char *s = P4_TOP(ctx->ds).s;
// Assumes caddr NUL terminated.
P4_TOP(ctx->ds).n = system(s);
size_t len = P4_POP(ctx->ds).z;
char *cmd = strndup(P4_TOP(ctx->ds).s, len);
P4_TOP(ctx->ds).n = system(cmd);
free(cmd);
}
# endif

# ifdef HOOK_PRIMATIVES
/* Examples of how some words, in particular those calling libc
Expand Down Expand Up @@ -121,13 +105,10 @@ p4SystemPath(P4_Ctx *ctx)
}

P4_Hook p4_hooks[] = {
# ifdef HOOK_SHELL
P4_HOOK(0x00, "SH", p4System),
P4_HOOK(0x21, "SHELL", p4SystemString),
# endif
# ifdef HOOK_PRIMATIVES
P4_HOOK(0x30, "MOVE", p4Move),
# endif
P4_HOOK(0x21, "shell", p4Shell),
P4_HOOK(0x02, "getcwd", p4GetCwd),
P4_HOOK(0x22, "getenv", p4GetEnv),
P4_HOOK(0x02, "system-path", p4SystemPath),
Expand Down
7 changes: 0 additions & 7 deletions src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,6 @@ p4Repl(P4_Ctx *ctx, volatile int thrown)
P4_WORD("MS", &&_ms, 0, 0x10),
P4_WORD("_parse", &&_parse, 0, 0x22), // p4
P4_WORD("PARSE-NAME", &&_parse_name, 0, 0x02),
P4_WORD("SOURCE", &&_source, 0, 0x02),
P4_WORD("SOURCE-ID", &&_source_id, 0, 0x01),

P4_WORD(NULL, NULL, 0, 0),
Expand Down Expand Up @@ -2003,12 +2002,6 @@ _input_offset: p4AllocStack(ctx, &ctx->ds, 1);
P4_PUSH(ctx->ds, (P4_Cell *) &ctx->input->offset);
NEXT;

// ( -- caddr u )
_source: p4AllocStack(ctx, &ctx->ds, 2);
P4_PUSH(ctx->ds, ctx->input->buffer);
P4_PUSH(ctx->ds, ctx->input->length);
NEXT;

// ( -- -1 | 0 | fp )
// Alias FILE *stdin to NULL.
_source_id: p4AllocStack(ctx, &ctx->ds, 1);
Expand Down
4 changes: 0 additions & 4 deletions src/post4.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ extern "C" {
#define HAVE_HOOKS 1
#endif

#ifdef HAVE_HOOKS
#define HOOK_SHELL 1
#endif

/***********************************************************************
*** No configuration below this point.
***********************************************************************/
Expand Down
8 changes: 8 additions & 0 deletions src/post4.p4
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,9 @@ MAX-U MAX-N 2CONSTANT MAX-D $02 _pp!
THEN
; IMMEDIATE $10 _pp!

\ (S: -- caddr u )
: SOURCE _ctx ctx.input @ DUP in.buffer @ SWAP in.length @ ; $20 _pp!

\ (S: -- u )
: source-offset >IN @ ; $01 _pp!

Expand Down Expand Up @@ -1980,6 +1983,11 @@ VARIABLE SCR
\
: PAGE 0 0 AT-XY S\" \e[0J" TYPE ;

[DEFINED] shell [IF]
\ (S: `remaining input line` -- n )
: sh source-remaining SOURCE NIP set-source-offset shell ;
[THEN]

\ (S: i*x fd -- j*x )
\ *** An uncaught exception within the include file will leak the file
\ *** handle and some memory.
Expand Down

0 comments on commit 38f194f

Please sign in to comment.