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
|
|
|
|
|
2015-06-07 22:12:20 -04:00
|
|
|
#ifdef DEBUGGER
|
2015-06-10 15:24:53 -04:00
|
|
|
static int gEnableDebugger = true;
|
2015-06-07 22:12:20 -04:00
|
|
|
#else
|
2015-06-10 15:24:53 -04:00
|
|
|
static int gEnableDebugger = false;
|
2015-06-07 20:37:21 -04:00
|
|
|
#endif
|
|
|
|
|
2011-02-17 21:16:51 -05:00
|
|
|
/* mark where to end the program for platforms which require this */
|
|
|
|
jmp_buf PicocExitBuf;
|
|
|
|
|
2015-06-07 22:12:20 -04:00
|
|
|
#ifdef DEBUGGER
|
2011-05-01 02:50:00 -04:00
|
|
|
#include <signal.h>
|
|
|
|
|
2012-09-22 01:11:44 -04:00
|
|
|
Picoc *break_pc = NULL;
|
|
|
|
|
2011-05-01 02:50:00 -04:00
|
|
|
static void BreakHandler(int Signal)
|
|
|
|
{
|
2015-06-10 15:24:53 -04:00
|
|
|
break_pc->DebugManualBreak = true;
|
2011-05-01 02:50:00 -04:00
|
|
|
}
|
|
|
|
|
2012-09-22 01:11:44 -04:00
|
|
|
void PlatformInit(Picoc *pc)
|
2011-05-01 02:50:00 -04:00
|
|
|
{
|
|
|
|
/* capture the break signal and pass it to the debugger */
|
2012-09-22 01:11:44 -04:00
|
|
|
break_pc = pc;
|
2011-05-01 02:50:00 -04:00
|
|
|
signal(SIGINT, BreakHandler);
|
|
|
|
}
|
|
|
|
#else
|
2015-06-07 22:12:20 -04:00
|
|
|
void PlatformInit(Picoc *pc) { }
|
2011-05-01 02:50:00 -04:00
|
|
|
#endif
|
|
|
|
|
2015-06-07 22:12:20 -04:00
|
|
|
void PlatformCleanup(Picoc *pc) { }
|
2009-03-15 03:07:21 -04:00
|
|
|
|
|
|
|
/* 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
|
2015-06-07 00:51:02 -04:00
|
|
|
if (Prompt != NULL) {
|
2010-07-05 15:53:34 -04:00
|
|
|
/* use GNU readline to read the line */
|
|
|
|
char *InLine = readline(Prompt);
|
|
|
|
if (InLine == NULL)
|
|
|
|
return NULL;
|
2015-06-07 00:51:02 -04:00
|
|
|
|
2013-10-14 19:38:00 -04:00
|
|
|
Buf[MaxLen-1] = '\0';
|
|
|
|
strncpy(Buf, InLine, MaxLen-2);
|
|
|
|
strncat(Buf, "\n", MaxLen-2);
|
2015-06-07 00:51:02 -04:00
|
|
|
|
2010-07-05 15:53:34 -04:00
|
|
|
if (InLine[0] != '\0')
|
|
|
|
add_history(InLine);
|
2015-06-07 00:51:02 -04:00
|
|
|
|
2010-07-05 15:53:34 -04:00
|
|
|
free(InLine);
|
|
|
|
return Buf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (Prompt != NULL)
|
|
|
|
printf("%s", Prompt);
|
2015-06-07 00:51:02 -04:00
|
|
|
|
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 */
|
2012-09-22 01:11:44 -04:00
|
|
|
char *PlatformReadFile(Picoc *pc, const char *FileName)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
|
|
|
struct stat FileInfo;
|
|
|
|
char *ReadText;
|
|
|
|
FILE *InFile;
|
|
|
|
int BytesRead;
|
2012-05-31 01:59:34 -04:00
|
|
|
char *p;
|
2015-06-07 00:51:02 -04:00
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
if (stat(FileName, &FileInfo))
|
2012-09-22 01:11:44 -04:00
|
|
|
ProgramFailNoParser(pc, "can't read file %s\n", FileName);
|
2015-06-07 00:51:02 -04:00
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
ReadText = malloc(FileInfo.st_size + 1);
|
|
|
|
if (ReadText == NULL)
|
2012-09-22 01:11:44 -04:00
|
|
|
ProgramFailNoParser(pc, "out of memory\n");
|
2015-06-07 00:51:02 -04:00
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
InFile = fopen(FileName, "r");
|
|
|
|
if (InFile == NULL)
|
2012-09-22 01:11:44 -04:00
|
|
|
ProgramFailNoParser(pc, "can't read file %s\n", FileName);
|
2015-06-07 00:51:02 -04:00
|
|
|
|
2009-03-15 03:07:21 -04:00
|
|
|
BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile);
|
|
|
|
if (BytesRead == 0)
|
2012-09-22 01:11:44 -04:00
|
|
|
ProgramFailNoParser(pc, "can't read file %s\n", FileName);
|
2009-03-15 03:07:21 -04:00
|
|
|
|
|
|
|
ReadText[BytesRead] = '\0';
|
|
|
|
fclose(InFile);
|
2015-06-07 00:51:02 -04:00
|
|
|
|
|
|
|
if ((ReadText[0] == '#') && (ReadText[1] == '!')) {
|
2015-06-13 16:49:34 -04:00
|
|
|
for (p = ReadText; (*p != '\0') && (*p != '\r') && (*p != '\n'); ++p) {
|
2012-05-31 01:59:34 -04:00
|
|
|
*p = ' ';
|
|
|
|
}
|
|
|
|
}
|
2015-06-07 00:51:02 -04:00
|
|
|
|
|
|
|
return ReadText;
|
2009-03-15 03:07:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read and scan a file for definitions */
|
2012-09-22 01:11:44 -04:00
|
|
|
void PicocPlatformScanFile(Picoc *pc, const char *FileName)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
2012-09-22 01:11:44 -04:00
|
|
|
char *SourceStr = PlatformReadFile(pc, FileName);
|
2009-03-15 03:07:21 -04:00
|
|
|
|
2012-08-28 06:26:12 -04:00
|
|
|
/* ignore "#!/path/to/picoc" .. by replacing the "#!" with "//" */
|
2015-06-07 00:51:02 -04:00
|
|
|
if (SourceStr != NULL && SourceStr[0] == '#' && SourceStr[1] == '!') {
|
|
|
|
SourceStr[0] = '/';
|
|
|
|
SourceStr[1] = '/';
|
2012-08-28 06:26:12 -04:00
|
|
|
}
|
|
|
|
|
2015-06-10 19:49:09 -04:00
|
|
|
PicocParse(pc, FileName, SourceStr, strlen(SourceStr), true, false, true,
|
|
|
|
gEnableDebugger);
|
2009-03-15 03:07:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* exit the program */
|
2012-09-22 01:11:44 -04:00
|
|
|
void PlatformExit(Picoc *pc, int RetVal)
|
2009-03-15 03:07:21 -04:00
|
|
|
{
|
2012-09-22 01:11:44 -04:00
|
|
|
pc->PicocExitValue = RetVal;
|
|
|
|
longjmp(pc->PicocExitBuf, 1);
|
2009-03-15 03:07:21 -04:00
|
|
|
}
|
|
|
|
|