2010-06-13 05:17:42 -04:00
|
|
|
#include "../picoc.h"
|
2011-02-17 02:11:20 -05:00
|
|
|
#include "../interpreter.h"
|
2009-03-15 03:07:21 -04:00
|
|
|
|
2010-07-05 15:53:34 -04:00
|
|
|
#ifdef USE_READLINE
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
void PlatformCleanup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a line of interactive input */
|
2010-07-05 15:53:34 -04:00
|
|
|
char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
2010-07-05 15:53:34 -04:00
|
|
|
#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);
|
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
fflush(stdout);
|
|
|
|
return fgets(Buf, MaxLen, stdin);
|
|
|
|
}
|
|
|
|
|
2009-03-15 05:57:19 -04:00
|
|
|
/* get a character of interactive input */
|
|
|
|
int PlatformGetCharacter()
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
return getchar();
|
|
|
|
}
|
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
/* write a character to the console */
|
2009-04-27 00:56:58 -04:00
|
|
|
void PlatformPutc(unsigned char OutCh, union OutputStreamInfo *Stream)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
|
|
|
putchar(OutCh);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read a file into memory */
|
|
|
|
char *PlatformReadFile(const char *FileName)
|
|
|
|
{
|
|
|
|
struct stat FileInfo;
|
|
|
|
char *ReadText;
|
|
|
|
FILE *InFile;
|
|
|
|
int BytesRead;
|
|
|
|
|
|
|
|
if (stat(FileName, &FileInfo))
|
|
|
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
|
|
|
|
|
|
|
ReadText = malloc(FileInfo.st_size + 1);
|
|
|
|
if (ReadText == NULL)
|
|
|
|
ProgramFail(NULL, "out of memory\n");
|
|
|
|
|
|
|
|
InFile = fopen(FileName, "r");
|
|
|
|
if (InFile == NULL)
|
|
|
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
|
|
|
|
|
|
|
BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile);
|
|
|
|
if (BytesRead == 0)
|
|
|
|
ProgramFail(NULL, "can't read file %s\n", FileName);
|
|
|
|
|
|
|
|
ReadText[BytesRead] = '\0';
|
|
|
|
fclose(InFile);
|
|
|
|
|
|
|
|
return ReadText;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read and scan a file for definitions */
|
2011-02-17 02:11:20 -05:00
|
|
|
void PicocPlatformScanFile(const char *FileName)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
|
|
|
char *SourceStr = PlatformReadFile(FileName);
|
|
|
|
|
2011-02-17 02:11:20 -05:00
|
|
|
PicocParse(FileName, SourceStr, strlen(SourceStr), TRUE, FALSE, TRUE);
|
2009-03-15 03:07:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mark where to end the program for platforms which require this */
|
2009-03-31 07:08:29 -04:00
|
|
|
jmp_buf ExitBuf;
|
2009-03-15 03:07:21 -04:00
|
|
|
|
2011-02-17 02:11:20 -05:00
|
|
|
/* set the point we'll return to when we exit through an error */
|
|
|
|
int PicocPlatformSetExitPoint()
|
|
|
|
{
|
|
|
|
return setjmp(ExitBuf);
|
|
|
|
}
|
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
/* exit the program */
|
2010-07-27 11:35:25 -04:00
|
|
|
void PlatformExit(int RetVal)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
2011-02-17 02:11:20 -05:00
|
|
|
PicocExitValue = RetVal;
|
2009-03-15 03:07:21 -04:00
|
|
|
longjmp(ExitBuf, 1);
|
|
|
|
}
|
|
|
|
|