From 6b91cb54fc7dec367c57b4d9e97f2a086b809ea6 Mon Sep 17 00:00:00 2001 From: "phil@softsynth.com" Date: Fri, 20 Feb 2009 18:38:25 +0000 Subject: [PATCH] Fixed POSIX IO, (ACCEPT) now emits SPACE at end of line. --- build/unix/Makefile | 20 +++++---- csrc/pf_core.c | 4 +- csrc/pf_guts.h | 2 +- csrc/pf_io.c | 4 +- csrc/posix/pf_io_posix.c | 89 ++++++++++++++++++++++++++-------------- fth/history.fth | 4 +- releases.txt | 7 +++- 7 files changed, 84 insertions(+), 46 deletions(-) diff --git a/build/unix/Makefile b/build/unix/Makefile index 5285ad3..2408291 100644 --- a/build/unix/Makefile +++ b/build/unix/Makefile @@ -8,7 +8,6 @@ # Options include: PF_SUPPORT_FP PF_NO_MALLOC PF_NO_INIT PF_DEBUG # See "docs/pf_ref.htm" file for more info. -# Note for Mac OS X, use cc instead of gcc COMPILER = gcc PFORTHDIR := $(shell cd ../../; pwd) @@ -35,7 +34,10 @@ FULL_WARNINGS = \ -Wmissing-prototypes \ -Wmissing-declarations -CCOPTS = -DPF_SUPPORT_FP -DPF_POSIX_IO -O2 $(FULL_WARNINGS) $(EXTRA_CCOPTS) +CCOPTS = -DPF_SUPPORT_FP -O2 $(FULL_WARNINGS) $(EXTRA_CCOPTS) + +IO_SOURCE = ${CSRCDIR}/posix/pf_io_posix.c +#IO_SOURCE = ${CSRCDIR}/stdio/pf_io_stdio.c EMBCCOPTS = -DPF_STATIC_DIC @@ -43,17 +45,17 @@ EMBCCOPTS = -DPF_STATIC_DIC # Build file lists from wildcards. PFITEMP = ${wildcard ${CSRCDIR}/*.h} PFINCLUDES = ${PFITEMP:${CSRCDIR}/pfdicdat.h=} -PFSOURCE = ${wildcard ${CSRCDIR}/*.c} ${CSRCDIR}/posix/pf_io_posix.c +PFSOURCE = ${wildcard ${CSRCDIR}/*.c} ${IO_SOURCE} PFTEMP = ${PFSOURCE:%.c=%.o} PFOBJS = ${PFTEMP:${CSRCDIR}/%=${TEMPOBJECTDIR}/%} PFEMBOBJS = ${PFTEMP:${CSRCDIR}/%=${OBJECTDIR}/%} COMPILE = $(COMPILER) $(CCOPTS) $(CDEFS) -${TEMPOBJECTDIR}/%.o: ${TEMPOBJECTDIR}/posix $(PFINCLUDES) ${CSRCDIR}/%.c +${TEMPOBJECTDIR}/%.o: ${TEMPOBJECTDIR} $(PFINCLUDES) ${CSRCDIR}/%.c $(COMPILE) -O -o ${TEMPOBJECTDIR}/$*.o -c ${CSRCDIR}/$*.c -${OBJECTDIR}/%.o: ${OBJECTDIR}/posix $(PFINCLUDES) ${CSRCDIR}/%.c ${CSRCDIR}/pfdicdat.h +${OBJECTDIR}/%.o: ${OBJECTDIR} $(PFINCLUDES) ${CSRCDIR}/%.c ${CSRCDIR}/pfdicdat.h $(COMPILE) -O -o ${OBJECTDIR}/$*.o -c ${CSRCDIR}/$*.c $(EMBCCOPTS) all: $(PFORTHAPP) @@ -70,11 +72,13 @@ pffiles: @echo "EMBEDDED OBJECT FILES ------------------" @echo ${PFEMBOBJS} -${TEMPOBJECTDIR}/posix: +${TEMPOBJECTDIR}: mkdir -p ${TEMPOBJECTDIR}/posix + mkdir -p ${TEMPOBJECTDIR}/stdio -${OBJECTDIR}/posix: +${OBJECTDIR}: mkdir -p ${OBJECTDIR}/posix + mkdir -p ${OBJECTDIR}/stdio # build pforth by compiling 'C' source $(PFDICAPP): $(PFINCLUDES) $(PFOBJS) @@ -86,7 +90,7 @@ pfdicapp: $(PFDICAPP) $(PFDICDAT): $(PFDICAPP) cd $(FTHDIR); $(PFDICAPP) -i system.fth ; mv pfdicdat.h $(PFDICDAT) -$(PFORTHAPP): ${TEMPOBJECTDIR}/posix $(PFDICDAT) $(PFINCLUDES) $(PFEMBOBJS) +$(PFORTHAPP): ${TEMPOBJECTDIR} $(PFDICDAT) $(PFINCLUDES) $(PFEMBOBJS) $(COMPILER) $(PFEMBOBJS) -lm -o $(PFORTHAPP) @echo "" @echo "Standalone pForth executable written to $(PFORTHAPP)" diff --git a/csrc/pf_core.c b/csrc/pf_core.c index ceab2be..c2fc2c9 100644 --- a/csrc/pf_core.c +++ b/csrc/pf_core.c @@ -455,8 +455,8 @@ int32 pfDoForth( const char *DicName, const char *SourceName, int32 IfInit ) if( !pfQueryQuiet() ) { MSG( "PForth V"PFORTH_VERSION ); - if( IsHostLittleEndian() ) MSG("LE"); - else MSG("BE"); + if( IsHostLittleEndian() ) MSG("-LE"); + else MSG("-BE"); #if PF_BIG_ENDIAN_DIC MSG("/BE"); #elif PF_LITTLE_ENDIAN_DIC diff --git a/csrc/pf_guts.h b/csrc/pf_guts.h index 6c3b032..b3814ad 100644 --- a/csrc/pf_guts.h +++ b/csrc/pf_guts.h @@ -23,7 +23,7 @@ ** PFORTH_VERSION changes when PForth is modified and released. ** See README file for version info. */ -#define PFORTH_VERSION "23" +#define PFORTH_VERSION "24" /* ** PFORTH_FILE_VERSION changes when incompatible changes are made diff --git a/csrc/pf_io.c b/csrc/pf_io.c index 3f57902..2cd3d3d 100644 --- a/csrc/pf_io.c +++ b/csrc/pf_io.c @@ -87,7 +87,8 @@ cell ioKey( void ) ** Receive line from keyboard. ** Return number of characters enterred. */ -#define BACKSPACE (8) +#define SPACE (0x20) +#define BACKSPACE (0x08) #define DELETE (0x7F) cell ioAccept( char *buffer, cell maxChars ) { @@ -135,6 +136,7 @@ DBUGX(("ioAccept(0x%x, 0x%x)\n", buffer, len )); gotline: sdDisableInput(); + sdTerminalEcho( SPACE ); /* NUL terminate line to simplify printing when debugging. */ if( len < maxChars ) p[len] = '\0'; diff --git a/csrc/posix/pf_io_posix.c b/csrc/posix/pf_io_posix.c index bf4a5c3..211c516 100644 --- a/csrc/posix/pf_io_posix.c +++ b/csrc/posix/pf_io_posix.c @@ -16,14 +16,13 @@ ** **************************************************************** ** 941004 PLB Extracted IO calls from pforth_main.c +** 090220 PLB Fixed broken sdQueryTerminal on Mac. It always returned true. ***************************************************************/ #include "../pf_all.h" -#if PF_POSIX_IO /* Configure console so that characters are not buffered. - * This allows KEY to work and also HISTORY.ON - * Thanks to Ralf Baechle and David Feuer for contributing this. + * This allows KEY and ?TERMINAL to work and also HISTORY.ON */ #include @@ -33,22 +32,24 @@ #include #include -#define stdin_fd 1 - static struct termios save_termios; static int stdin_is_tty; +/* poll() is broken in Mac OS X Tiger OS so use select() instead. */ +#define PF_USE_SELECT (1) + /* Default portable terminal I/O. */ int sdTerminalOut( char c ) { return putchar(c); } -/* We don't need to echo because getchar() echos. */ + int sdTerminalEcho( char c ) { - TOUCH(c); + putchar(c); return 0; } + int sdTerminalIn( void ) { return getchar(); @@ -66,44 +67,70 @@ int sdTerminalFlush( void ) /****************************************************/ int sdQueryTerminal( void ) { - struct pollfd pfd; +#if PF_USE_SELECT + fd_set readfds; + struct timeval tv; + FD_ZERO(&readfds); + FD_SET(STDIN_FILENO, &readfds); + /* Set timeout to zero so that we just poll and return. */ + tv.tv_sec = 0; + tv.tv_usec = 0; + int select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv); + if (select_retval < 0) + { + perror("sdTerminalInit: select"); + } + return FD_ISSET(STDIN_FILENO,&readfds) ? FTRUE : FFALSE; + +#else + struct pollfd pfd = { 0 }; sdTerminalFlush(); - pfd.fd = stdin_fd; - pfd.events = stdin_fd; - return poll( &pfd, 1, 0 ); + pfd.fd = STDIN_FILENO; + pfd.events = POLLIN; + int result = poll( &pfd, 1, 0 ); + /* On a Mac it may set revents to POLLNVAL because poll() is broken on Tiger. */ + if( pfd.revents & POLLNVAL ) + { + PRT(("sdQueryTerminal: poll got POLLNVAL, stdin not open\n")); + return FFALSE; + } + else + { + return (pfd.revents & POLLIN) ? FTRUE : FFALSE; + } +#endif } /****************************************************/ void sdTerminalInit(void) { - struct termios term; - - stdin_is_tty = isatty(stdin_fd); - if (!stdin_is_tty) - return; - + struct termios term; + + stdin_is_tty = isatty(STDIN_FILENO); + if (stdin_is_tty) + { /* Get current terminal attributes and save them so we can restore them. */ - tcgetattr(stdin_fd, &term); - save_termios = term; + tcgetattr(STDIN_FILENO, &term); + save_termios = term; /* ICANON says to wait upon read until a character is received, * and then to return it immediately (or soon enough....) * ECHOCTL says not to echo backspaces and other control chars as ^H */ - term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON ); - term.c_cc[VTIME] = 0; - term.c_cc[VMIN] = 1; - tcsetattr(stdin_fd, TCSANOW, &term); + term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON ); + term.c_cc[VTIME] = 0; + term.c_cc[VMIN] = 1; + if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 ) + { + perror("sdTerminalInit: tcsetattr"); + } + } } /****************************************************/ void sdTerminalTerm(void) { - if (!stdin_is_tty) - return; - - tcsetattr(stdin_fd, TCSANOW, &save_termios); + if (stdin_is_tty) + { + tcsetattr(STDIN_FILENO, TCSANOW, &save_termios); + } } - -#undef stdin_fd - -#endif diff --git a/fth/history.fth b/fth/history.fth index dfb7baa..5e5409e 100644 --- a/fth/history.fth +++ b/fth/history.fth @@ -421,9 +421,9 @@ variable KH-INSIDE ( true if we are scrolling inside the history buffer ) kh.inschar REPEAT drop kh-span @ kh-cursor @ - ?dup - IF 1+ tio.forwards ( move to end of line ) - ELSE space + IF tio.forwards ( move to end of line ) THEN + space flushemit ; diff --git a/releases.txt b/releases.txt index 7cfef67..82b8c3d 100644 --- a/releases.txt +++ b/releases.txt @@ -2,6 +2,11 @@ Release History for pForth - a Portable ANS-like Forth written in ANSI 'C' Documentation for pForth at http://www.softsynth.com/pforth/ +V24 2/20/09 + - Fixed Posix IO on Mac. ?TERMINAL was always returning true. + - ACCCEPT now emits a space at end of line before output. + - Fixed RESIZE because it was returning the wrong address. + V23 8/4/2008 - Removed -v option from mkdir in build/unix/Makefile. It was not supported on FreeBSD. Thank you Alexsej Sauchdev for reporting this. @@ -15,7 +20,7 @@ V22 (unreleased) - Modified ACCEPT so that a line at the end of a file that does NOT have a line terminator will now be processed. - Use _getch(), _putch(), and _kbhit() so that KEY, EMIT and ?TERMINAL will work on PC. - - Fixed : foo { -- } 55 ; - was entering local frame but not exiting. + - Fixed : foo { -- } 55 ; - Was entering local frame but not exiting. Now prints error. - Redefined MAKE_ID to protect it from 16 bit ints - John Providenza says "If you split local variables onto 2 lines, PForth crashes." Fixed. Also allow \ - Fixed float evaluation in EVALUATE in "quit.fth". -- 2.20.1