picoc/platform/platform_unix.c

109 lines
2.3 KiB
C
Raw Normal View History

#include "../picoc.h"
#include "../interpreter.h"
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
void PlatformCleanup()
{
}
/* get a line of interactive input */
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);
return fgets(Buf, MaxLen, stdin);
}
/* get a character of interactive input */
int PlatformGetCharacter()
{
fflush(stdout);
return getchar();
}
/* write a character to the console */
void PlatformPutc(unsigned char OutCh, union OutputStreamInfo *Stream)
{
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 */
void PicocPlatformScanFile(const char *FileName)
{
char *SourceStr = PlatformReadFile(FileName);
PicocParse(FileName, SourceStr, strlen(SourceStr), TRUE, FALSE, TRUE);
}
/* mark where to end the program for platforms which require this */
jmp_buf ExitBuf;
/* set the point we'll return to when we exit through an error */
int PicocPlatformSetExitPoint()
{
return setjmp(ExitBuf);
}
/* exit the program */
void PlatformExit(int RetVal)
{
PicocExitValue = RetVal;
longjmp(ExitBuf, 1);
}