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 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 /* Current token and attributes */
61 char CfgSVal [CFG_MAX_IDENT_LEN+1];
62 unsigned long CfgIVal;
65 unsigned CfgErrorLine;
68 /* Input sources for the configuration */
69 static const char* CfgName = 0;
70 static const char* CfgBuf = 0;
72 /* Other input stuff */
74 static unsigned InputLine = 1;
75 static unsigned InputCol = 0;
76 static FILE* InputFile = 0;
80 /*****************************************************************************/
82 /*****************************************************************************/
86 void CfgWarning (const char* Format, ...)
87 /* Print a warning message adding file name and line number of the config file */
92 va_start (ap, Format);
93 xvsprintf (Buf, sizeof (Buf), Format, ap);
96 Warning ("%s(%u): %s", CfgGetName(), CfgErrorLine, Buf);
101 void CfgError (const char* Format, ...)
102 /* Print an error message adding file name and line number of the config file */
107 va_start (ap, Format);
108 xvsprintf (Buf, sizeof (Buf), Format, ap);
111 Error ("%s(%u): %s", CfgGetName(), CfgErrorLine, Buf);
116 /*****************************************************************************/
118 /*****************************************************************************/
122 static void NextChar (void)
123 /* Read the next character from the input file */
126 /* Read from buffer */
127 C = (unsigned char)(*CfgBuf);
134 /* Read from the file */
135 C = getc (InputFile);
152 static unsigned DigitVal (int C)
153 /* Return the value for a numeric digit */
158 return toupper (C) - 'A' + 10;
164 void CfgNextTok (void)
165 /* Read the next token from the input stream */
171 /* Skip whitespace */
172 while (isspace (C)) {
176 /* Remember the current position */
177 CfgErrorLine = InputLine;
178 CfgErrorCol = InputCol;
181 if (C == '_' || IsAlpha (C)) {
183 /* Read the identifier */
185 while (C == '_' || IsAlNum (C)) {
186 if (I < CFG_MAX_IDENT_LEN) {
192 CfgTok = CFGTOK_IDENT;
200 Error ("%s(%u): Hex digit expected", CfgName, InputLine);
203 while (isxdigit (C)) {
204 CfgIVal = CfgIVal * 16 + DigitVal (C);
207 CfgTok = CFGTOK_INTCON;
211 /* Decimal number? */
214 while (isdigit (C)) {
215 CfgIVal = CfgIVal * 10 + DigitVal (C);
218 CfgTok = CFGTOK_INTCON;
222 /* Other characters */
227 CfgTok = CFGTOK_LCURLY;
232 CfgTok = CFGTOK_RCURLY;
237 CfgTok = CFGTOK_SEMI;
247 CfgTok = CFGTOK_COMMA;
257 CfgTok = CFGTOK_COLON;
264 if (C == EOF || C == '\n') {
265 Error ("%s(%u): Unterminated string", CfgName, InputLine);
267 if (I < CFG_MAX_IDENT_LEN) {
274 CfgTok = CFGTOK_STRCON;
279 while (C != '\n' && C != EOF) {
295 strncpy (CfgSVal, OutputName, CFG_MAX_IDENT_LEN);
296 CfgSVal [CFG_MAX_IDENT_LEN] = '\0';
300 CfgTok = CFGTOK_STRCON;
306 CfgTok = CFGTOK_INTCON;
310 CfgError ("Invalid format specification");
319 Error ("%s(%u): Invalid character `%c'", CfgName, InputLine, C);
326 void CfgConsume (cfgtok_t T, const char* Msg)
327 /* Skip a token, print an error message if not found */
337 void CfgConsumeSemi (void)
338 /* Consume a semicolon */
340 CfgConsume (CFGTOK_SEMI, "`;' expected");
345 void CfgConsumeColon (void)
346 /* Consume a colon */
348 CfgConsume (CFGTOK_COLON, "`:' expected");
353 void CfgOptionalComma (void)
354 /* Consume a comma if there is one */
356 if (CfgTok == CFGTOK_COMMA) {
363 void CfgOptionalAssign (void)
364 /* Consume an equal sign if there is one */
366 if (CfgTok == CFGTOK_EQ) {
373 void CfgAssureInt (void)
374 /* Make sure the next token is an integer */
376 if (CfgTok != CFGTOK_INTCON) {
377 CfgError ("Integer constant expected");
383 void CfgAssureStr (void)
384 /* Make sure the next token is a string constant */
386 if (CfgTok != CFGTOK_STRCON) {
387 CfgError ("String constant expected");
393 void CfgAssureIdent (void)
394 /* Make sure the next token is an identifier */
396 if (CfgTok != CFGTOK_IDENT) {
397 CfgError ("Identifier expected");
403 void CfgRangeCheck (unsigned long Lo, unsigned long Hi)
404 /* Check the range of CfgIVal */
406 if (CfgIVal < Lo || CfgIVal > Hi) {
407 CfgError ("Range error");
413 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
414 /* Map an identifier to one of the special tokens in the table */
418 /* We need an identifier */
419 if (CfgTok == CFGTOK_IDENT) {
421 /* Make it upper case */
423 while (CfgSVal [I]) {
424 CfgSVal [I] = toupper (CfgSVal [I]);
429 for (I = 0; I < Size; ++I) {
430 if (strcmp (CfgSVal, Table [I].Ident) == 0) {
431 CfgTok = Table [I].Tok;
438 /* Not found or no identifier */
439 Error ("%s(%u): %s expected", CfgName, InputLine, Name);
444 void CfgBoolToken (void)
445 /* Map an identifier or integer to a boolean token */
447 static const IdentTok Booleans [] = {
448 { "YES", CFGTOK_TRUE },
449 { "NO", CFGTOK_FALSE },
450 { "TRUE", CFGTOK_TRUE },
451 { "FALSE", CFGTOK_FALSE },
454 /* If we have an identifier, map it to a boolean token */
455 if (CfgTok == CFGTOK_IDENT) {
456 CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
458 /* We expected an integer here */
459 if (CfgTok != CFGTOK_INTCON) {
460 CfgError ("Boolean value expected");
462 CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
468 void CfgSetName (const char* Name)
469 /* Set a name for a config file */
476 const char* CfgGetName (void)
477 /* Get the name of the config file */
482 return "[builtin config]";
490 void CfgSetBuf (const char* Buf)
491 /* Set a memory buffer for the config */
499 /* Return true if we have a configuration available */
501 return CfgName != 0 || CfgBuf != 0;
506 void CfgOpenInput (void)
507 /* Open the input file if we have one */
509 /* If we have a config name given, open the file, otherwise we will read
515 InputFile = fopen (CfgName, "r");
516 if (InputFile == 0) {
517 Error ("Cannot open `%s': %s", CfgName, strerror (errno));
522 /* Initialize variables */
527 /* Start the ball rolling ... */
533 void CfgCloseInput (void)
534 /* Close the input file if we have one */
536 /* Close the input file if we had one */
538 (void) fclose (InputFile);