1 /*****************************************************************************/
5 /* Configuration file scanner for the ld65 linker */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
42 #include "../common/xsprintf.h"
50 /*****************************************************************************/
52 /*****************************************************************************/
56 /* Current token and attributes */
58 char CfgSVal [CFG_MAX_IDENT_LEN+1];
59 unsigned long CfgIVal;
62 unsigned CfgErrorLine;
65 /* Input sources for the configuration */
66 static const char* CfgName = 0;
67 static const char* CfgBuf = 0;
69 /* Other input stuff */
71 static unsigned InputLine = 1;
72 static unsigned InputCol = 0;
73 static FILE* InputFile = 0;
77 /*****************************************************************************/
79 /*****************************************************************************/
83 void CfgWarning (const char* Format, ...)
84 /* Print a warning message adding file name and line number of the config file */
89 va_start (ap, Format);
90 xvsprintf (Buf, sizeof (Buf), Format, ap);
93 Warning ("%s(%u): %s", CfgName, CfgErrorLine, Buf);
98 void CfgError (const char* Format, ...)
99 /* Print an error message adding file name and line number of the config file */
104 va_start (ap, Format);
105 xvsprintf (Buf, sizeof (Buf), Format, ap);
108 Error ("%s(%u): %s", CfgName, CfgErrorLine, Buf);
113 /*****************************************************************************/
115 /*****************************************************************************/
119 static void NextChar (void)
120 /* Read the next character from the input file */
123 /* Read from buffer */
124 C = (unsigned char)(*CfgBuf);
131 /* Read from the file */
132 C = getc (InputFile);
149 static unsigned DigitVal (int C)
150 /* Return the value for a numeric digit */
155 return toupper (C) - 'A' + 10;
161 void CfgNextTok (void)
162 /* Read the next token from the input stream */
168 /* Skip whitespace */
169 while (isspace (C)) {
173 /* Remember the current position */
174 CfgErrorLine = InputLine;
175 CfgErrorCol = InputCol;
178 if (C == '_' || isalpha (C)) {
180 /* Read the identifier */
182 while (C == '_' || isalnum (C)) {
183 if (I < CFG_MAX_IDENT_LEN) {
189 CfgTok = CFGTOK_IDENT;
197 Error ("%s(%u): Hex digit expected", CfgName, InputLine);
200 while (isxdigit (C)) {
201 CfgIVal = CfgIVal * 16 + DigitVal (C);
204 CfgTok = CFGTOK_INTCON;
208 /* Decimal number? */
211 while (isdigit (C)) {
212 CfgIVal = CfgIVal * 10 + DigitVal (C);
215 CfgTok = CFGTOK_INTCON;
219 /* Other characters */
224 CfgTok = CFGTOK_LCURLY;
229 CfgTok = CFGTOK_RCURLY;
234 CfgTok = CFGTOK_SEMI;
244 CfgTok = CFGTOK_COMMA;
254 CfgTok = CFGTOK_COLON;
261 if (C == EOF || C == '\n') {
262 Error ("%s(%u): Unterminated string", CfgName, InputLine);
264 if (I < CFG_MAX_IDENT_LEN) {
271 CfgTok = CFGTOK_STRCON;
276 while (C != '\n' && C != EOF) {
292 strncpy (CfgSVal, OutputName, CFG_MAX_IDENT_LEN);
293 CfgSVal [CFG_MAX_IDENT_LEN] = '\0';
297 CfgTok = CFGTOK_STRCON;
303 CfgTok = CFGTOK_INTCON;
307 CfgError ("Invalid format specification");
316 Error ("%s(%u): Invalid character `%c'", CfgName, InputLine, C);
323 void CfgConsume (unsigned T, const char* Msg)
324 /* Skip a token, print an error message if not found */
334 void CfgConsumeSemi (void)
335 /* Consume a semicolon */
337 CfgConsume (CFGTOK_SEMI, "`;' expected");
342 void CfgConsumeColon (void)
343 /* Consume a colon */
345 CfgConsume (CFGTOK_COLON, "`:' expected");
350 void CfgOptionalComma (void)
351 /* Consume a comma if there is one */
353 if (CfgTok == CFGTOK_COMMA) {
360 void CfgOptionalAssign (void)
361 /* Consume an equal sign if there is one */
363 if (CfgTok == CFGTOK_EQ) {
370 void CfgAssureInt (void)
371 /* Make sure the next token is an integer */
373 if (CfgTok != CFGTOK_INTCON) {
374 CfgError ("Integer constant expected");
380 void CfgAssureStr (void)
381 /* Make sure the next token is a string constant */
383 if (CfgTok != CFGTOK_STRCON) {
384 CfgError ("String constant expected");
390 void CfgAssureIdent (void)
391 /* Make sure the next token is an identifier */
393 if (CfgTok != CFGTOK_IDENT) {
394 CfgError ("Identifier expected");
400 void CfgRangeCheck (unsigned long Lo, unsigned long Hi)
401 /* Check the range of CfgIVal */
403 if (CfgIVal < Lo || CfgIVal > Hi) {
404 CfgError ("Range error");
410 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
411 /* Map an identifier to one of the special tokens in the table */
415 /* We need an identifier */
416 if (CfgTok == CFGTOK_IDENT) {
418 /* Make it upper case */
420 while (CfgSVal [I]) {
421 CfgSVal [I] = toupper (CfgSVal [I]);
426 for (I = 0; I < Size; ++I) {
427 if (strcmp (CfgSVal, Table [I].Ident) == 0) {
428 CfgTok = Table [I].Tok;
435 /* Not found or no identifier */
436 Error ("%s(%u): %s expected", CfgName, InputLine, Name);
441 void CfgBoolToken (void)
442 /* Map an identifier or integer to a boolean token */
444 static const IdentTok Booleans [] = {
445 { "YES", CFGTOK_TRUE },
446 { "NO", CFGTOK_FALSE },
447 { "TRUE", CFGTOK_TRUE },
448 { "FALSE", CFGTOK_FALSE },
451 /* If we have an identifier, map it to a boolean token */
452 if (CfgTok == CFGTOK_IDENT) {
453 CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
455 /* We expected an integer here */
456 if (CfgTok != CFGTOK_INTCON) {
457 CfgError ("Boolean value expected");
459 CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
465 void CfgSetName (const char* Name)
466 /* Set a name for a config file */
473 const char* CfgGetName (void)
474 /* Get the name of the config file */
476 return CfgName? CfgName : "";
481 void CfgSetBuf (const char* Buf)
482 /* Set a memory buffer for the config */
490 /* Return true if we have a configuration available */
492 return CfgName != 0 || CfgBuf != 0;
497 void CfgOpenInput (void)
498 /* Open the input file if we have one */
500 /* If we have a config name given, open the file, otherwise we will read
506 InputFile = fopen (CfgName, "r");
507 if (InputFile == 0) {
508 Error ("Cannot open `%s': %s", CfgName, strerror (errno));
513 /* Initialize variables */
518 /* Start the ball rolling ... */
524 void CfgCloseInput (void)
525 /* Close the input file if we have one */
527 /* Close the input file if we had one */
529 (void) fclose (InputFile);