ispell is working

This commit is contained in:
~d6 2023-12-03 22:07:14 -05:00
parent 931f3652e8
commit da202786ec
4 changed files with 74 additions and 89 deletions

View File

@ -32,8 +32,6 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
#define CONSOLE_DEBUG 1
UxnSubprocess children[CONSOLE_MAX_CHILDREN];
static char *fork_args[4] = {"/bin/sh", "-c", "", NULL};
static int to_child_fd[2], from_child_fd[2];
@ -173,9 +171,12 @@ std_put(Uint8 c, FILE *fd)
static void
proc_put(Uxn *u, Uint8 *d)
{
Uint8 c = d[0x2];
Uint8 c = d[0xa];
int n = d[0xe] & 0x3;
if (CONSOLE_DEBUG)
fprintf(stderr, " fd %d: write %02x\n", children[n].fd_in, c);
write(children[n].fd_in, &c, 1);
fdatasync(children[n].fd_in);
}
static void
@ -304,6 +305,7 @@ int
console_input(Uxn *u, int port, char c, int type)
{
Uint8 *d = &u->dev[0x10];
d[0x2] = d[0x4] = d[0x5] = 0;
d[port] = c;
d[0x7] = type;
return uxn_eval(u, PEEK2(d));

View File

@ -11,6 +11,8 @@ WITH REGARD TO THIS SOFTWARE.
#define CONSOLE_VERSION 1
#define CONSOLE_DEBUG 0
#define CONSOLE_NONE 0x00
#define CONSOLE_STDIN 0x01
#define CONSOLE_ARG 0x02

View File

@ -50,12 +50,18 @@ handle_input(Uxn *u, int fd, int port, int argdata, int argend)
{
char buf[32];
int n = read(fd, &buf, 32);
if(n == 0)
if(n == 0) {
if(CONSOLE_DEBUG)
fprintf(stderr, " fd %d: closed\n", fd);
console_input(u, port, 0x00, argend);
else
for(int i = 0; i < n; i++)
} else {
for(int i = 0; i < n; i++) {
if(CONSOLE_DEBUG)
fprintf(stderr, " fd %d: read %02x\n", fd, buf[i]);
console_input(u, port, buf[i], argdata);
}
}
}
static int
init_rfds(fd_set *rfds)
@ -68,11 +74,8 @@ init_rfds(fd_set *rfds)
UxnSubprocess *child = get_child(i);
int fd = child->fd_out;
if (fd >= 0) {
/* printf("ok: %d/%d (%d, %d)\n", i, child->id, fd, child->pid); */
FD_SET(fd, rfds);
if (fd >= nfds) nfds = fd + 1;
/* } else { */
/* printf("nope: %d/%d (%d, %d)\n", i, child->id, fd, child->pid); */
}
}
return nfds;
@ -87,7 +90,7 @@ emu_run(Uxn *u)
while(!u->dev[0x0f]) {
int nfds = init_rfds(&rfds);
/* tv.tv_sec = 0; */
tv.tv_sec = 1;
tv.tv_sec = 0;
tv.tv_usec = 1000;
int retval = select(nfds, &rfds, NULL, NULL, &tv);
if(retval > 0) {

View File

@ -1,107 +1,85 @@
( ispell.tal )
( )
( note that this is not a production spell-checker and is currently )
( kind of brittle in the same ways as `ispell -a` on raw input )
|10 @Console [
&vector $2 &stdin $1 &pad1 $1 &proc-get $1 &host-get $1 &pad2 $1 &type $1
&stdout $1 &stderr $1 &proc-put $1 &pad3 $1 &param $2 &opts $1 &host-put $1
]
|0000
@word-buf $100
@word-pos $2
@spelled-ok $1 ( ; was the last word spelled ok? )
@resume $2 ( ; if set should have effect ` -- BRK` )
@ready $1 ( ; are we ready? or still reading ispell startup banner? )
@correct $1 ( ; was the last word spelled ok? )
|0100 ( -> BRK )
;on-console-init .Console/vector DEO2 ( ; set up console vector callback )
init-ispell ( ; set up ispell child process )
word-reset BRK
@init-ispell ( -- )
LIT "! print
#31 .Console/opts DEO ( ; child 1: write to stdin, read from stdout )
;on-console-read .Console/vector DEO2 ( ; set up console vector callback )
word-reset ( ; initialize word buffer )
#31 .Console/opts DEO ( ; child 1: write child.in, read child.out )
;ispell-cmd .Console/param DEO2 ( ; set up `ispell -a` to run )
#01 .Console/host-put DEO JMP2r ( ; run the command now and return )
@on-console-init ( -- BRK )
( LIT "? print )
.Console/type DEI #21 EQU ?{ LIT "u print BRK } ( ; 0x21 signals input from child 1 )
( LIT ". print )
( .Console/proc-get DEI emit/byte )
.Console/proc-get DEI #0a EQU ?{ BRK } ( ; skip ispell's one line banner )
LIT "> print
;on-console-read .Console/vector DEO2 ( ; finished banner, checker is ready )
BRK
#01 .Console/host-put DEO BRK ( ; run the command now and return )
@word-reset ( -> )
;word-buf ;word-pos STA2 ( ; reset word-pos pointer )
#00 ;word-buf STA JMP2r ( ; write zero into word buffer )
@on-console-read
LIT ", print
( .Console/type emit/byte )
.Console/type DEI #21 EQU ?on-ispell-input
.Console/type DEI #01 EQU ?on-stdin
BRK
@on-stdin
.Console/stdin DEI DUP #0a NEQ ?word-append
POP ;word-buf ;finish !check-spelling
@on-ispell-input ( -> BRK )
LIT "i print
.Console/proc-get DEI emit/byte
.Console/proc-get DEI LIT "* EQU ( ; line starting with "*" means ok )
.spelled-ok STZ ( ; store spelling result )
LIT "- print LIT "0 .spelled-ok LDZ ADD print
#00 ,on-ispell-drain/done STR ( ; drain two lines )
;on-ispell-drain .Console/vector DEO2 ( ; ignore rest of line )
BRK
@word-append ( c^ -> BRK )
LIT "% print
DUP print
;word-pos LDA2 STA ( ; pos<-c )
;word-pos LDA2 INC2 ;word-pos STA2 ( ; pos<-pos+1 )
#00 ;word-pos LDA2 STA BRK ( ; pos<-00 )
@finish ( -> BRK )
LIT "f print
LIT "0 .spelled-ok LDZ ADD println
LIT "> print
word-reset BRK
@on-console-read
.Console/type DEI ( ; what kind of input is this? )
DUP #21 NEQ ?{ POP !on-ispell-input } ( ; input from ispell process )
DUP #01 NEQ ?{ POP !on-stdin } ( ; input from stdin, i.e. user )
POP BRK ( ; otherwise skip )
@ispell-cmd "ispell 20 "-a 00 ( ; "ispell -a" )
@on-stdin
.Console/stdin DEI #0a EQU ?check ( ; if newline, check spelling )
.Console/stdin DEI DUP !word-append ( ; if not newline, append to buffer )
@on-ispell-drain ( -- BRK )
LIT "d print
@on-ispell-input ( -> BRK )
.ready LDZ ?{ !on-console-init } ( ; if not ready, parse ispell banner )
.Console/proc-get DEI LIT "* EQU ( ; line starting with "*" means ok )
.Console/proc-get DEI LIT "+ EQU ORA ( ; line starting with "+" means ok too )
.correct STZ ( ; store spelling result )
#20 print ( ; print a space )
LIT "0 .correct LDZ ADD println ( ; print 1 if ok, 0 otherwise )
#00 ;on-ispell-drain/done STA ( ; drain two lines )
;on-ispell-drain .Console/vector DEO2 ( ; ignore rest of line )
BRK
@on-console-init ( -- BRK )
.Console/proc-get DEI #0a EQU ?{ BRK } ( ; skip ispell's one line banner )
#01 .ready STZ ( ; after newline we are ready )
LIT "> print BRK ( ; print simple prompt and return )
@on-ispell-drain ( -> BRK )
.Console/type DEI #21 EQU ?{ BRK } ( ; 0x21 signals input from child 1 )
.Console/proc-get DEI #0a EQU ?{ BRK } ( ; skip ispell's outupt )
LIT [ &done $1 ] ?{ ( ; is this the second newline? )
#01 ,on-ispell-drain/done STR BRK ( ; no, but next one will be. )
}
LIT &done $1 ?{ #01 ,&done STR BRK } ( ; only exit if second newline )
;on-console-read .Console/vector DEO2 ( ; drain finished, checker is ready )
;resume LDA2 JMP2 ( ; resume whatever we wanted to do after checking )
word-reset LIT "> print BRK ( ; reset buffer, print prompt, return )
@check-spelling ( word* continue* -- BRK )
LIT "$ print
;resume STA2 ( ; write continuation to resume )
#01 .Console/opts DEO ( ; act on child 1 )
LIT "< print
&loop LIT ": print LDAk ( #00 NEQ ) ?{
POP2
"> print
#0a .Console/proc-put DEO BRK
} ( ; when we read \0 we are done )
LDAk print
LDAk .Console/proc-put DEO INC2 !&loop ( ; send byte to child 1 and loop )
@check ( word* -> BRK )
.ready LDZ ?{ !check-not-ready }
;word-buf #01 .Console/opts DEO ( ; prepare buffer, act on child 1 )
&loop LDAk ?{ POP2 #0a send BRK } ( ; when we read \0 we are done )
LDAk DUP print send INC2 !&loop ( ; send byte to child 1 and loop )
@print
.Console/stdout DEO JMP2r
@check-not-ready
#20 print LIT "? println ( ; print " ?\n" )
word-reset LIT "> print BRK ( ; reset the buffer, print prompt, return )
@println
.Console/stdout DEO #0a .Console/stdout DEO JMP2r
@send ( c^ -> )
.Console/proc-put DEO JMP2r ( ; send character to ispell process )
@emit
&long SWP2 ,&short JSR
&short SWP ,&byte JSR
&byte DUP #04 SFT ,&char JSR
&char #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
JMP2r
@print ( c^ -> )
.Console/stdout DEO JMP2r ( ; send character to stdout )
@println ( c^ -> )
print #0a .Console/stdout DEO JMP2r ( ; send character and newline to stdout )
@ispell-cmd "ispell 20 "-a 00 ( ; "ispell -a" )
@word-pos $2 ( ; position in word-buf )
@word-buf $100 ( ; buffer for input characters )