1 /*****************************************************************************/
5 /* Configuration file scanner for the ld65 linker */
9 /* (C) 1998-2008, 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 CfgSVal [CFG_MAX_IDENT_LEN+1];
63 unsigned long CfgIVal;
66 unsigned CfgErrorLine;
69 /* Input sources for the configuration */
70 static const char* CfgName = 0;
71 static const char* CfgBuf = 0;
73 /* Other input stuff */
75 static unsigned InputLine = 1;
76 static unsigned InputCol = 0;
77 static FILE* InputFile = 0;
81 /*****************************************************************************/
83 /*****************************************************************************/
87 void CfgWarning (const char* Format, ...)
88 /* Print a warning message adding file name and line number of the config file */
90 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
93 va_start (ap, Format);
94 SB_VPrintf (&Buf, Format, ap);
97 Warning ("%s(%u): %s", CfgGetName(), CfgErrorLine, SB_GetConstBuf (&Buf));
103 void CfgError (const char* Format, ...)
104 /* Print an error message adding file name and line number of the config file */
106 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
109 va_start (ap, Format);
110 SB_VPrintf (&Buf, Format, ap);
113 Error ("%s(%u): %s", CfgGetName(), CfgErrorLine, SB_GetConstBuf (&Buf));
119 /*****************************************************************************/
121 /*****************************************************************************/
125 static void NextChar (void)
126 /* Read the next character from the input file */
129 /* Read from buffer */
130 C = (unsigned char)(*CfgBuf);
137 /* Read from the file */
138 C = getc (InputFile);
155 static unsigned DigitVal (int C)
156 /* Return the value for a numeric digit */
161 return toupper (C) - 'A' + 10;
167 void CfgNextTok (void)
168 /* Read the next token from the input stream */
174 /* Skip whitespace */
175 while (isspace (C)) {
179 /* Remember the current position */
180 CfgErrorLine = InputLine;
181 CfgErrorCol = InputCol;
184 if (C == '_' || IsAlpha (C)) {
186 /* Read the identifier */
188 while (C == '_' || IsAlNum (C)) {
189 if (I < CFG_MAX_IDENT_LEN) {
195 CfgTok = CFGTOK_IDENT;
203 CfgError ("Hex digit expected");
206 while (isxdigit (C)) {
207 CfgIVal = CfgIVal * 16 + DigitVal (C);
210 CfgTok = CFGTOK_INTCON;
214 /* Decimal number? */
217 while (isdigit (C)) {
218 CfgIVal = CfgIVal * 10 + DigitVal (C);
221 CfgTok = CFGTOK_INTCON;
225 /* Other characters */
230 CfgTok = CFGTOK_MINUS;
235 CfgTok = CFGTOK_PLUS;
250 CfgTok = CFGTOK_LPAR;
255 CfgTok = CFGTOK_RPAR;
260 CfgTok = CFGTOK_LCURLY;
265 CfgTok = CFGTOK_RCURLY;
270 CfgTok = CFGTOK_SEMI;
280 CfgTok = CFGTOK_COMMA;
290 CfgTok = CFGTOK_COLON;
297 if (C == EOF || C == '\n') {
298 CfgError ("Unterminated string");
300 if (I < CFG_MAX_IDENT_LEN) {
307 CfgTok = CFGTOK_STRCON;
312 while (C != '\n' && C != EOF) {
328 strncpy (CfgSVal, OutputName, CFG_MAX_IDENT_LEN);
329 CfgSVal [CFG_MAX_IDENT_LEN] = '\0';
333 CfgTok = CFGTOK_STRCON;
339 CfgTok = CFGTOK_INTCON;
343 CfgError ("Invalid format specification");
352 CfgError ("Invalid character `%c'", C);
359 void CfgConsume (cfgtok_t T, const char* Msg)
360 /* Skip a token, print an error message if not found */
370 void CfgConsumeSemi (void)
371 /* Consume a semicolon */
373 CfgConsume (CFGTOK_SEMI, "`;' expected");
378 void CfgConsumeColon (void)
379 /* Consume a colon */
381 CfgConsume (CFGTOK_COLON, "`:' expected");
386 void CfgOptionalComma (void)
387 /* Consume a comma if there is one */
389 if (CfgTok == CFGTOK_COMMA) {
396 void CfgOptionalAssign (void)
397 /* Consume an equal sign if there is one */
399 if (CfgTok == CFGTOK_EQ) {
406 void CfgAssureInt (void)
407 /* Make sure the next token is an integer */
409 if (CfgTok != CFGTOK_INTCON) {
410 CfgError ("Integer constant expected");
416 void CfgAssureStr (void)
417 /* Make sure the next token is a string constant */
419 if (CfgTok != CFGTOK_STRCON) {
420 CfgError ("String constant expected");
426 void CfgAssureIdent (void)
427 /* Make sure the next token is an identifier */
429 if (CfgTok != CFGTOK_IDENT) {
430 CfgError ("Identifier expected");
436 void CfgRangeCheck (unsigned long Lo, unsigned long Hi)
437 /* Check the range of CfgIVal */
439 if (CfgIVal < Lo || CfgIVal > Hi) {
440 CfgError ("Range error");
446 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
447 /* Map an identifier to one of the special tokens in the table */
451 /* We need an identifier */
452 if (CfgTok == CFGTOK_IDENT) {
454 /* Make it upper case */
456 while (CfgSVal [I]) {
457 CfgSVal [I] = toupper (CfgSVal [I]);
462 for (I = 0; I < Size; ++I) {
463 if (strcmp (CfgSVal, Table [I].Ident) == 0) {
464 CfgTok = Table [I].Tok;
471 /* Not found or no identifier */
472 CfgError ("%s expected", Name);
477 void CfgBoolToken (void)
478 /* Map an identifier or integer to a boolean token */
480 static const IdentTok Booleans [] = {
481 { "YES", CFGTOK_TRUE },
482 { "NO", CFGTOK_FALSE },
483 { "TRUE", CFGTOK_TRUE },
484 { "FALSE", CFGTOK_FALSE },
487 /* If we have an identifier, map it to a boolean token */
488 if (CfgTok == CFGTOK_IDENT) {
489 CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
491 /* We expected an integer here */
492 if (CfgTok != CFGTOK_INTCON) {
493 CfgError ("Boolean value expected");
495 CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
501 void CfgSetName (const char* Name)
502 /* Set a name for a config file */
509 const char* CfgGetName (void)
510 /* Get the name of the config file */
515 return "[builtin config]";
523 void CfgSetBuf (const char* Buf)
524 /* Set a memory buffer for the config */
532 /* Return true if we have a configuration available */
534 return CfgName != 0 || CfgBuf != 0;
539 void CfgOpenInput (void)
540 /* Open the input file if we have one */
542 /* If we have a config name given, open the file, otherwise we will read
548 InputFile = fopen (CfgName, "r");
549 if (InputFile == 0) {
550 Error ("Cannot open `%s': %s", CfgName, strerror (errno));
555 /* Initialize variables */
560 /* Start the ball rolling ... */
566 void CfgCloseInput (void)
567 /* Close the input file if we have one */
569 /* Close the input file if we had one */
571 (void) fclose (InputFile);