Now has the ability to use GNU readline for input editing

git-svn-id: http://picoc.googlecode.com/svn/trunk@461 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2010-07-05 19:53:34 +00:00
parent a365e1e993
commit e977f325ae
6 changed files with 37 additions and 8 deletions

View file

@ -1,6 +1,6 @@
CC=gcc CC=gcc
CFLAGS=-Wall -pedantic -g -DUNIX_HOST CFLAGS=-Wall -pedantic -g -DUNIX_HOST
LIBS=-lm LIBS=-lm -lreadline
TARGET = picoc TARGET = picoc
SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c \ SRCS = picoc.c table.c lex.c parse.c expression.c heap.c type.c \

View file

@ -327,7 +327,7 @@ void LibGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value
char *Result; char *Result;
ReturnValue->Val->Pointer = NULL; ReturnValue->Val->Pointer = NULL;
Result = PlatformGetLine(ReadBuffer, GETS_BUF_MAX); Result = PlatformGetLine(ReadBuffer, GETS_BUF_MAX, NULL);
if (Result == NULL) if (Result == NULL)
return; return;

7
lex.c
View file

@ -556,6 +556,7 @@ enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int I
{ {
enum LexToken Token = TokenNone; enum LexToken Token = TokenNone;
int ValueSize; int ValueSize;
char *Prompt = NULL;
do do
{ {
@ -586,13 +587,13 @@ enum LexToken LexGetToken(struct ParseState *Parser, struct Value **Value, int I
/* get interactive input */ /* get interactive input */
if (LexUseStatementPrompt) if (LexUseStatementPrompt)
{ {
PlatformPrintf(INTERACTIVE_PROMPT_STATEMENT); Prompt = INTERACTIVE_PROMPT_STATEMENT;
LexUseStatementPrompt = FALSE; LexUseStatementPrompt = FALSE;
} }
else else
PlatformPrintf(INTERACTIVE_PROMPT_LINE); Prompt = INTERACTIVE_PROMPT_LINE;
if (PlatformGetLine(&LineBuffer[0], LINEBUFFER_MAX) == NULL) if (PlatformGetLine(&LineBuffer[0], LINEBUFFER_MAX, Prompt) == NULL)
return TokenEOF; return TokenEOF;
/* put the new line at the end of the linked list of interactive lines */ /* put the new line at the end of the linked list of interactive lines */

View file

@ -398,7 +398,7 @@ void AssignFail(struct ParseState *Parser, const char *Format, struct ValueType
void LexFail(struct LexState *Lexer, const char *Message, ...); void LexFail(struct LexState *Lexer, const char *Message, ...);
void PlatformCleanup(); void PlatformCleanup();
void PlatformScanFile(const char *FileName); void PlatformScanFile(const char *FileName);
char *PlatformGetLine(char *Buf, int MaxLen); char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt);
int PlatformGetCharacter(); int PlatformGetCharacter();
void PlatformPutc(unsigned char OutCh, union OutputStreamInfo *); void PlatformPutc(unsigned char OutCh, union OutputStreamInfo *);
void PlatformErrorPrefix(struct ParseState *Parser); void PlatformErrorPrefix(struct ParseState *Parser);

View file

@ -54,7 +54,7 @@
# ifndef NO_FP # ifndef NO_FP
# include <math.h> # include <math.h>
# define PICOC_MATH_LIBRARY # define PICOC_MATH_LIBRARY
/*# define NEED_MATH_LIBRARY*/ # define USE_READLINE
# undef BIG_ENDIAN # undef BIG_ENDIAN
# if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__) # if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
# define BIG_ENDIAN # define BIG_ENDIAN

View file

@ -1,5 +1,10 @@
#include "../picoc.h" #include "../picoc.h"
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
/* a source file we need to clean up */ /* a source file we need to clean up */
static char *CleanupText = NULL; static char *CleanupText = NULL;
@ -11,8 +16,31 @@ void PlatformCleanup()
} }
/* get a line of interactive input */ /* get a line of interactive input */
char *PlatformGetLine(char *Buf, int MaxLen) char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
{ {
#ifdef USE_READLINE
if (Prompt != NULL)
{
/* use GNU readline to read the line */
char *InLine = readline(Prompt);
if (InLine == NULL)
return NULL;
Buf[MaxLen] = '\0';
strncpy(Buf, InLine, MaxLen-1);
strncat(Buf, "\n", MaxLen-1);
if (InLine[0] != '\0')
add_history(InLine);
free(InLine);
return Buf;
}
#endif
if (Prompt != NULL)
printf("%s", Prompt);
fflush(stdout); fflush(stdout);
return fgets(Buf, MaxLen, stdin); return fgets(Buf, MaxLen, stdin);
} }