1 /*****************************************************************************/
5 /* Configuration file scanner for the da65 disassembler */
9 /* (C) 2000-2005 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* Current token and attributes */
62 char InfoSVal [CFG_MAX_IDENT_LEN+1];
66 unsigned InfoErrorLine;
67 unsigned InfoErrorCol;
69 /* Input sources for the configuration */
70 static const char* InfoFile = 0;
72 /* Other input stuff */
74 static unsigned InputLine = 1;
75 static unsigned InputCol = 0;
76 static FILE* InputFile = 0;
77 static char* InputSrcName = 0;
81 /*****************************************************************************/
83 /*****************************************************************************/
87 void InfoWarning (const char* Format, ...)
88 /* Print a warning message adding file name and line number of the config file */
93 va_start (ap, Format);
94 xvsprintf (Buf, sizeof (Buf), Format, ap);
97 fprintf (stderr, "%s(%u): Warning: %s\n",
98 InputSrcName, InfoErrorLine, Buf);
103 void InfoError (const char* Format, ...)
104 /* Print an error message adding file name and line number of the config file */
109 va_start (ap, Format);
110 xvsprintf (Buf, sizeof (Buf), Format, ap);
113 fprintf (stderr, "%s(%u): Error: %s\n",
114 InputSrcName, InfoErrorLine, Buf);
121 /*****************************************************************************/
123 /*****************************************************************************/
127 static void NextChar (void)
128 /* Read the next character from the input file */
130 /* Read from the file */
131 C = getc (InputFile);
147 static unsigned DigitVal (int C)
148 /* Return the value for a numeric digit */
153 return toupper (C) - 'A' + 10;
159 static void SkipBlanks (int SingleLine)
161 while (C != EOF && (!SingleLine || C != '\n') && IsSpace (C)) {
168 static long GetDecimalToken (void)
172 while (C != EOF && IsDigit (C)) {
173 Value = Value * 10 + DigitVal (C);
181 static int GetEncodedChar (char* Buf, unsigned* IPtr, unsigned Size)
188 } else if (C != '\\') {
193 NextChar (); /* consume '\\' */
196 } else if (IsODigit (C)) {
199 Decoded = Decoded * 8 + DigitVal (C);
202 } while (Count > 0 && C != EOF && IsODigit (C));
203 } else if (C == 'x') {
204 NextChar (); /* consume 'x' */
206 while (Count > 0 && C != EOF && IsXDigit (C)) {
207 Decoded = Decoded * 16 + DigitVal (C);
213 case '"': case '\'': case '\\':
215 case 't': Decoded = '\t'; break;
216 case 'r': Decoded = '\r'; break;
217 case 'n': Decoded = '\n'; break;
223 if (*IPtr < Size - 1) {
224 Buf [(*IPtr)++] = Decoded;
232 static void LineMarkerOrComment ()
233 /* Handle a line beginning with '#'. Possible interpretations are:
234 ** - #line <lineno> ["<filename>"] (C preprocessor input)
235 ** - # <lineno> "<filename>" [<flag>]... (gcc preprocessor output)
239 unsigned long LineNo = 0;
240 int LineDirective = 0;
241 StrBuf SrcNameBuf = AUTO_STRBUF_INITIALIZER;
243 /* Skip the first "# " */
251 for (I = 0; I < sizeof MaybeLine - 1 && C != EOF && IsAlNum (C); ++I) {
256 if (strcmp (MaybeLine, "line") != 0) {
263 /* Get line number */
264 if (C == EOF || !IsDigit (C)) {
267 LineNo = GetDecimalToken ();
270 /* Get the source file name */
272 /* The source file name is missing */
273 if (LineDirective && C == '\n') {
274 /* got #line <lineno> */
283 while (C != EOF && C != '\n' && C != '\"') {
286 if (GetEncodedChar (DecodeBuf, &I, sizeof DecodeBuf) < 0) {
289 SB_AppendBuf (&SrcNameBuf, DecodeBuf, I);
296 /* Ignore until the end of line */
297 while (C != EOF && C != '\n') {
301 /* Accepted a line marker */
302 SB_Terminate (&SrcNameBuf);
303 xfree (InputSrcName);
304 InputSrcName = SB_GetBuf (&SrcNameBuf);
305 SB_Init (&SrcNameBuf);
306 InputLine = (unsigned)LineNo;
311 InfoWarning ("Bad line marker");
313 while (C != EOF && C != '\n') {
318 SB_Done (&SrcNameBuf);
323 void InfoNextTok (void)
324 /* Read the next token from the input stream */
330 /* Skip whitespace */
333 /* Remember the current position */
334 InfoErrorLine = InputLine;
335 InfoErrorCol = InputCol;
338 if (C == '_' || IsAlpha (C)) {
340 /* Read the identifier */
342 while (C == '_' || IsAlNum (C)) {
343 if (I < CFG_MAX_IDENT_LEN) {
349 InfoTok = INFOTOK_IDENT;
357 InfoError ("Hex digit expected");
360 while (IsXDigit (C)) {
361 InfoIVal = InfoIVal * 16 + DigitVal (C);
364 InfoTok = INFOTOK_INTCON;
368 /* Decimal number? */
370 InfoIVal = GetDecimalToken ();
371 InfoTok = INFOTOK_INTCON;
375 /* Other characters */
380 InfoTok = INFOTOK_LCURLY;
385 InfoTok = INFOTOK_RCURLY;
390 InfoTok = INFOTOK_SEMI;
395 InfoTok = INFOTOK_DOT;
400 InfoTok = INFOTOK_COMMA;
405 InfoTok = INFOTOK_EQ;
410 InfoTok = INFOTOK_COLON;
417 while (C != EOF && C != '\"') {
418 if (GetEncodedChar (InfoSVal, &I, sizeof InfoSVal) < 0) {
420 InfoError ("Unterminated string");
422 InfoError ("Invalid escape char: %c", C);
427 InfoError ("Unterminated string");
430 InfoTok = INFOTOK_STRCON;
435 if (C == EOF || IsControl (C) || C == '\'') {
436 InfoError ("Invalid character constant");
438 if (GetEncodedChar (DecodeBuf, &I, sizeof DecodeBuf) < 0 || I != 1) {
439 InfoError ("Invalid character constant");
441 InfoIVal = DecodeBuf [0];
443 InfoError ("Unterminated character constant");
446 InfoTok = INFOTOK_CHARCON;
450 /* # lineno "sourcefile" or # comment */
451 if (SyncLines && InputCol == 1) {
452 LineMarkerOrComment ();
456 } while (C != EOF && C != '\n');
462 InfoTok = INFOTOK_EOF;
466 /* C++ style comment */
469 InfoError ("Invalid token '/'");
473 } while (C != '\n' && C != EOF);
477 InfoTok = INFOTOK_EOF;
481 InfoTok = INFOTOK_EOF;
485 InfoError ("Invalid character '%c'", C);
492 void InfoConsume (unsigned T, const char* Msg)
493 /* Skip a token, print an error message if not found */
503 void InfoConsumeLCurly (void)
504 /* Consume a left curly brace */
506 InfoConsume (INFOTOK_LCURLY, "'{' expected");
511 void InfoConsumeRCurly (void)
512 /* Consume a right curly brace */
514 InfoConsume (INFOTOK_RCURLY, "'}' expected");
519 void InfoConsumeSemi (void)
520 /* Consume a semicolon */
522 InfoConsume (INFOTOK_SEMI, "';' expected");
527 void InfoConsumeColon (void)
528 /* Consume a colon */
530 InfoConsume (INFOTOK_COLON, "':' expected");
535 void InfoOptionalComma (void)
536 /* Consume a comma if there is one */
538 if (InfoTok == INFOTOK_COMMA) {
545 void InfoOptionalAssign (void)
546 /* Consume an equal sign if there is one */
548 if (InfoTok == INFOTOK_EQ) {
555 void InfoAssureInt (void)
556 /* Make sure the next token is an integer */
558 if (InfoTok != INFOTOK_INTCON) {
559 InfoError ("Integer constant expected");
565 void InfoAssureStr (void)
566 /* Make sure the next token is a string constant */
568 if (InfoTok != INFOTOK_STRCON) {
569 InfoError ("String constant expected");
575 void InfoAssureChar (void)
576 /* Make sure the next token is a char constant */
578 if (InfoTok != INFOTOK_STRCON) {
579 InfoError ("Character constant expected");
585 void InfoAssureIdent (void)
586 /* Make sure the next token is an identifier */
588 if (InfoTok != INFOTOK_IDENT) {
589 InfoError ("Identifier expected");
595 void InfoRangeCheck (long Lo, long Hi)
596 /* Check the range of InfoIVal */
598 if (InfoIVal < Lo || InfoIVal > Hi) {
599 InfoError ("Range error");
605 void InfoSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
606 /* Map an identifier to one of the special tokens in the table */
610 /* We need an identifier */
611 if (InfoTok == INFOTOK_IDENT) {
613 /* Make it upper case */
615 while (InfoSVal [I]) {
616 InfoSVal [I] = toupper (InfoSVal [I]);
621 for (I = 0; I < Size; ++I) {
622 if (strcmp (InfoSVal, Table [I].Ident) == 0) {
623 InfoTok = Table [I].Tok;
630 /* Not found or no identifier */
631 InfoError ("%s expected", Name);
636 void InfoBoolToken (void)
637 /* Map an identifier or integer to a boolean token */
639 static const IdentTok Booleans [] = {
640 { "YES", INFOTOK_TRUE },
641 { "NO", INFOTOK_FALSE },
642 { "TRUE", INFOTOK_TRUE },
643 { "FALSE", INFOTOK_FALSE },
644 { "ON", INFOTOK_TRUE },
645 { "OFF", INFOTOK_FALSE },
648 /* If we have an identifier, map it to a boolean token */
649 if (InfoTok == INFOTOK_IDENT) {
650 InfoSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
652 /* We expected an integer here */
653 if (InfoTok != INFOTOK_INTCON) {
654 InfoError ("Boolean value expected");
656 InfoTok = (InfoIVal == 0)? INFOTOK_FALSE : INFOTOK_TRUE;
662 void InfoSetName (const char* Name)
663 /* Set a name for a config file */
667 InputSrcName = xstrdup(Name);
673 /* Return true if we have an info file given */
675 return (InfoFile != 0);
680 void InfoOpenInput (void)
681 /* Open the input file */
684 InputFile = fopen (InfoFile, "r");
685 if (InputFile == 0) {
686 Error ("Cannot open '%s': %s", InfoFile, strerror (errno));
689 /* Initialize variables */
694 /* Start the ball rolling ... */
700 void InfoCloseInput (void)
701 /* Close the input file if we have one */
703 /* Close the input file if we had one */
705 (void) fclose (InputFile);