From 0ca01d6d24b37a1700686f8f7691fe88510ca9b8 Mon Sep 17 00:00:00 2001 From: johanknol Date: Sat, 19 May 2001 10:16:19 +0000 Subject: [PATCH] fixed the octal and hex escape sequences git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@826 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- src/SDCCglue.c | 2 +- src/SDCCval.c | 66 +++++++++++++++++++++++++++++++++--------- support/Util/SDCCerr.c | 4 ++- support/Util/SDCCerr.h | 2 ++ 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/SDCCglue.c b/src/SDCCglue.c index 3a5e4e3a..733c0e6a 100644 --- a/src/SDCCglue.c +++ b/src/SDCCglue.c @@ -400,7 +400,7 @@ printChar (FILE * ofile, char *s, int plen) *p = '\0'; if (p != buf) tfprintf (ofile, "\t!ascii\n", buf); - tfprintf (ofile, "\t!db !constbyte\n", *s); + tfprintf (ofile, "\t!db !constbyte\n", (unsigned char)*s); p = buf; } else diff --git a/src/SDCCval.c b/src/SDCCval.c index e70b7931..8871a531 100644 --- a/src/SDCCval.c +++ b/src/SDCCval.c @@ -334,13 +334,39 @@ constVal (char *s) } +/*------------------------------------------------------------------*/ +/* octalEscape - process an octal constant of max three digits */ +/* return the octal value, throw a warning for illegal octal */ +/* adjust src to point at the last proccesed char */ +/*------------------------------------------------------------------*/ + +char octalEscape (char **str) { + int digits; + unsigned value=0; + + for (digits=0; digits<3; digits++) { + if (**str>='0' && **str<='7') { + value = value*8 + (**str-'0'); + (*str)++; + } else { + break; + } + } + if (digits) { + if (value > 255 /* || (**str>='0' && **str<='7') */ ) { + werror (W_ESC_SEQ_OOR_FOR_CHAR); + } + (*str)--; + } + return value; +} + /*------------------------------------------------------------------*/ /* copyStr - copies src to dest ignoring leading & trailing \"s */ /*------------------------------------------------------------------*/ void copyStr (char *dest, char *src) { - unsigned int x; while (*src) { if (*src == '\"') @@ -371,21 +397,35 @@ copyStr (char *dest, char *src) case 'a': *dest++ = '\a'; break; + case '0': - /* embedded octal or hex constant */ - if (*(src + 1) == 'x' || - *(src + 1) == 'X') - { - x = strtol (src, &src, 16); - *dest++ = x; - } - else - { - /* must be octal */ - x = strtol (src, &src, 8); - *dest++ = x; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + *dest++ = octalEscape(&src); + break; + + case 'x': { + char *s=++src; + unsigned long value=strtol (src, &src, 16); + + if (s==src) { + // no valid hex found + werror(E_INVALID_HEX); + } else { + if (value>255) { + werror(W_ESC_SEQ_OOR_FOR_CHAR); + } else { + *dest++ = value; } + src--; + } break; + } case '\\': *dest++ = '\\'; diff --git a/support/Util/SDCCerr.c b/support/Util/SDCCerr.c index 59aef76a..ed29f22c 100644 --- a/support/Util/SDCCerr.c +++ b/support/Util/SDCCerr.c @@ -186,7 +186,9 @@ struct { { WARNING,"warning *** Model '%s' not supported for %s, ignored.\n"}, { WARNING,"warning *** Both banked and nonbanked attributes used. nonbanked wins.\n"}, { WARNING,"warning *** Both banked and static used. static wins.\n"}, -{ WARNING,"warning *** converting integer type to generic pointer: assuming XDATA\n"} +{ WARNING,"warning *** converting integer type to generic pointer: assuming XDATA\n"}, +{ WARNING,"warning *** escape sequence out of range for char.\n"}, +{ ERROR ,"error *** \\x used with no following hex digits.\n"} }; /* ------------------------------------------------------------------------------- diff --git a/support/Util/SDCCerr.h b/support/Util/SDCCerr.h index ee134615..42090f57 100644 --- a/support/Util/SDCCerr.h +++ b/support/Util/SDCCerr.h @@ -152,6 +152,8 @@ SDCCERR - SDCC Standard error handler #define W_BANKED_WITH_NONBANKED 134 /* banked and nonbanked attributes mixed */ #define W_BANKED_WITH_STATIC 135 /* banked and static mixed */ #define W_INT_TO_GEN_PTR_CAST 136 /* Converting integer type to generic pointer. */ +#define W_ESC_SEQ_OOR_FOR_CHAR 137 /* Escape sequence of of range for char */ +#define E_INVALID_HEX 138 /* \x used with no following hex digits */ /** Describes the maximum error level that will be logged. Any level includes all of the levels listed after it. -- 2.47.2