From 6ce16624751276531a6f5f1e3d3554c7505e9e80 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Sat, 28 Feb 2009 05:43:28 +0000 Subject: [PATCH] Platform-dependent exit() function for Surveyor git-svn-id: http://picoc.googlecode.com/svn/trunk@121 21eae674-98b7-11dd-bd71-f92a316d2d60 --- TODO | 1 + picoc.c | 3 ++ picoc.h | 2 ++ platform.h | 2 ++ platform_support.c | 77 ++++++++++++++++++++++++++++++---------------- 5 files changed, 59 insertions(+), 26 deletions(-) diff --git a/TODO b/TODO index afcf7f0..b1c236b 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,7 @@ TODO * pointers to array elements and struct elements +* setjmp() / longjmp() hooks for error exit * test character array pointers and dereferencing * operator precedence * '->' diff --git a/picoc.c b/picoc.c index 8a3c189..cb72cbf 100644 --- a/picoc.c +++ b/picoc.c @@ -21,6 +21,9 @@ int main(int argc, char **argv) ProgramFail(NULL, "Format: picoc ...\n"); Initialise(); + if (PlatformSetExitPoint()) + return 1; + PlatformScanFile(argv[1]); return 0; diff --git a/picoc.h b/picoc.h index f28d47b..5583966 100644 --- a/picoc.h +++ b/picoc.h @@ -299,5 +299,7 @@ char *PlatformGetLine(char *Buf, int MaxLen); void PlatformPutc(unsigned char OutCh); void PlatformPrintf(const char *Format, ...); void PlatformVPrintf(const char *Format, va_list Args); +int PlatformSetExitPoint(); +void PlatformExit(); #endif /* PICOC_H */ diff --git a/platform.h b/platform.h index c3f8965..40c32b7 100644 --- a/platform.h +++ b/platform.h @@ -45,6 +45,8 @@ # include "../string.h" # include "../print.h" # include "../malloc.h" +# include "../setjmp.h" +# include "../stdarg.h" # define assert(x) # endif # endif diff --git a/platform_support.c b/platform_support.c index 0e7bac8..012616c 100644 --- a/platform_support.c +++ b/platform_support.c @@ -1,32 +1,6 @@ #include "picoc.h" #ifdef UNIX_HOST -/* exit with a message */ -void ProgramFail(struct ParseState *Parser, const char *Message, ...) -{ - va_list Args; - - if (Parser != NULL) - PlatformPrintf("%s:%d: ", Parser->FileName, Parser->Line); - - va_start(Args, Message); - PlatformVPrintf(Message, Args); - PlatformPrintf("\n"); - exit(1); -} - -/* exit lexing with a message */ -void LexFail(struct LexState *Lexer, const char *Message, ...) -{ - va_list Args; - - PlatformPrintf("%s:%d: ", Lexer->FileName, Lexer->Line); - va_start(Args, Message); - PlatformVPrintf(Message, Args); - PlatformPrintf("\n"); - exit(1); -} - /* get a line of interactive input */ char *PlatformGetLine(char *Buf, int MaxLen) { @@ -73,6 +47,18 @@ void PlatformScanFile(const char *FileName) Parse(FileName, SourceStr, strlen(SourceStr), TRUE); free(SourceStr); } + +/* mark where to end the program for platforms which require this */ +int PlatformSetExitPoint() +{ + return 0; +} + +/* exit the program */ +void PlatformExit() +{ + exit(1); +} #endif #ifdef SURVEYOR_HOST @@ -96,8 +82,47 @@ char *PlatformGetLine(char *Buf, int MaxLen) void PlatformPutc(unsigned char OutCh) { } + +/* mark where to end the program for platforms which require this */ +static jmp_buf ExitPoint; +int PlatformSetExitPoint() +{ + return setjmp(ExitPoint); +} + +/* exit the program */ +void PlatformExit() +{ + longjmp(ExitPoint, 1); +} #endif +/* exit with a message */ +void ProgramFail(struct ParseState *Parser, const char *Message, ...) +{ + va_list Args; + + if (Parser != NULL) + PlatformPrintf("%s:%d: ", Parser->FileName, Parser->Line); + + va_start(Args, Message); + PlatformVPrintf(Message, Args); + PlatformPrintf("\n"); + PlatformExit(1); +} + +/* exit lexing with a message */ +void LexFail(struct LexState *Lexer, const char *Message, ...) +{ + va_list Args; + + PlatformPrintf("%s:%d: ", Lexer->FileName, Lexer->Line); + va_start(Args, Message); + PlatformVPrintf(Message, Args); + PlatformPrintf("\n"); + PlatformExit(1); +} + /* printf for compiler error reporting */ void PlatformPrintf(const char *Format, ...) {