1 /*****************************************************************************/
5 /* Configuration file scanner for the da65 disassembler */
9 /* (C) 2000-2005 Ullrich von Bassewitz */
10 /* Römerstrasse 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;
416 while (C != EOF && C != '\"') {
417 if (GetEncodedChar (InfoSVal, &I, sizeof InfoSVal) < 0) {
419 InfoError ("Unterminated string");
421 InfoError ("Invalid escape char: %c", C);
426 InfoError ("Unterminated string");
429 InfoTok = INFOTOK_STRCON;
434 if (C == EOF || IsControl (C) || C == '\'') {
435 InfoError ("Invalid character constant");
437 if (GetEncodedChar (DecodeBuf, &I, sizeof DecodeBuf) < 0 || I != 1) {
438 InfoError ("Invalid character constant");
440 InfoIVal = DecodeBuf [0];
442 InfoError ("Unterminated character constant");
445 InfoTok = INFOTOK_CHARCON;
449 /* # lineno "sourcefile" or # comment */
450 if (SyncLines && InputCol == 1) {
451 LineMarkerOrComment ();
455 } while (C != EOF && C != '\n');
461 InfoTok = INFOTOK_EOF;
465 /* C++ style comment */
468 InfoError ("Invalid token `/'");
472 } while (C != '\n' && C != EOF);
476 InfoTok = INFOTOK_EOF;
480 InfoTok = INFOTOK_EOF;
484 InfoError ("Invalid character `%c'", C);
491 void InfoConsume (unsigned T, const char* Msg)
492 /* Skip a token, print an error message if not found */
502 void InfoConsumeLCurly (void)
503 /* Consume a left curly brace */
505 InfoConsume (INFOTOK_LCURLY, "`{' expected");
510 void InfoConsumeRCurly (void)
511 /* Consume a right curly brace */
513 InfoConsume (INFOTOK_RCURLY, "`}' expected");
518 void InfoConsumeSemi (void)
519 /* Consume a semicolon */
521 InfoConsume (INFOTOK_SEMI, "`;' expected");
526 void InfoConsumeColon (void)
527 /* Consume a colon */
529 InfoConsume (INFOTOK_COLON, "`:' expected");
534 void InfoOptionalComma (void)
535 /* Consume a comma if there is one */
537 if (InfoTok == INFOTOK_COMMA) {
544 void InfoOptionalAssign (void)
545 /* Consume an equal sign if there is one */
547 if (InfoTok == INFOTOK_EQ) {
554 void InfoAssureInt (void)
555 /* Make sure the next token is an integer */
557 if (InfoTok != INFOTOK_INTCON) {
558 InfoError ("Integer constant expected");
564 void InfoAssureStr (void)
565 /* Make sure the next token is a string constant */
567 if (InfoTok != INFOTOK_STRCON) {
568 InfoError ("String constant expected");
574 void InfoAssureChar (void)
575 /* Make sure the next token is a char constant */
577 if (InfoTok != INFOTOK_STRCON) {
578 InfoError ("Character constant expected");
584 void InfoAssureIdent (void)
585 /* Make sure the next token is an identifier */
587 if (InfoTok != INFOTOK_IDENT) {
588 InfoError ("Identifier expected");
594 void InfoRangeCheck (long Lo, long Hi)
595 /* Check the range of InfoIVal */
597 if (InfoIVal < Lo || InfoIVal > Hi) {
598 InfoError ("Range error");
604 void InfoSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
605 /* Map an identifier to one of the special tokens in the table */
609 /* We need an identifier */
610 if (InfoTok == INFOTOK_IDENT) {
612 /* Make it upper case */
614 while (InfoSVal [I]) {
615 InfoSVal [I] = toupper (InfoSVal [I]);
620 for (I = 0; I < Size; ++I) {
621 if (strcmp (InfoSVal, Table [I].Ident) == 0) {
622 InfoTok = Table [I].Tok;
629 /* Not found or no identifier */
630 InfoError ("%s expected", Name);
635 void InfoBoolToken (void)
636 /* Map an identifier or integer to a boolean token */
638 static const IdentTok Booleans [] = {
639 { "YES", INFOTOK_TRUE },
640 { "NO", INFOTOK_FALSE },
641 { "TRUE", INFOTOK_TRUE },
642 { "FALSE", INFOTOK_FALSE },
643 { "ON", INFOTOK_TRUE },
644 { "OFF", INFOTOK_FALSE },
647 /* If we have an identifier, map it to a boolean token */
648 if (InfoTok == INFOTOK_IDENT) {
649 InfoSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
651 /* We expected an integer here */
652 if (InfoTok != INFOTOK_INTCON) {
653 InfoError ("Boolean value expected");
655 InfoTok = (InfoIVal == 0)? INFOTOK_FALSE : INFOTOK_TRUE;
661 void InfoSetName (const char* Name)
662 /* Set a name for a config file */
666 InputSrcName = xstrdup(Name);
672 /* Return true if we have an info file given */
674 return (InfoFile != 0);
679 void InfoOpenInput (void)
680 /* Open the input file */
683 InputFile = fopen (InfoFile, "r");
684 if (InputFile == 0) {
685 Error ("Cannot open `%s': %s", InfoFile, strerror (errno));
688 /* Initialize variables */
693 /* Start the ball rolling ... */
699 void InfoCloseInput (void)
700 /* Close the input file if we have one */
702 /* Close the input file if we had one */
704 (void) fclose (InputFile);