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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Current token and attributes */
60 char CfgSVal [CFG_MAX_IDENT_LEN+1];
61 unsigned long CfgIVal;
64 unsigned CfgErrorLine;
67 /* Input sources for the configuration */
68 static const char* CfgName = 0;
69 static const char* CfgBuf = 0;
71 /* Other input stuff */
73 static unsigned InputLine = 1;
74 static unsigned InputCol = 0;
75 static FILE* InputFile = 0;
79 /*****************************************************************************/
81 /*****************************************************************************/
85 void CfgWarning (const char* Format, ...)
86 /* Print a warning message adding file name and line number of the config file */
91 va_start (ap, Format);
92 xvsprintf (Buf, sizeof (Buf), Format, ap);
95 Warning ("%s(%u): %s", CfgName, CfgErrorLine, Buf);
100 void CfgError (const char* Format, ...)
101 /* Print an error message adding file name and line number of the config file */
106 va_start (ap, Format);
107 xvsprintf (Buf, sizeof (Buf), Format, ap);
110 Error ("%s(%u): %s", CfgName, CfgErrorLine, Buf);
115 /*****************************************************************************/
117 /*****************************************************************************/
121 static void NextChar (void)
122 /* Read the next character from the input file */
125 /* Read from buffer */
126 C = (unsigned char)(*CfgBuf);
133 /* Read from the file */
134 C = getc (InputFile);
151 static unsigned DigitVal (int C)
152 /* Return the value for a numeric digit */
157 return toupper (C) - 'A' + 10;
163 void CfgNextTok (void)
164 /* Read the next token from the input stream */
170 /* Skip whitespace */
171 while (isspace (C)) {
175 /* Remember the current position */
176 CfgErrorLine = InputLine;
177 CfgErrorCol = InputCol;
180 if (C == '_' || isalpha (C)) {
182 /* Read the identifier */
184 while (C == '_' || isalnum (C)) {
185 if (I < CFG_MAX_IDENT_LEN) {
191 CfgTok = CFGTOK_IDENT;
199 Error ("%s(%u): Hex digit expected", CfgName, InputLine);
202 while (isxdigit (C)) {
203 CfgIVal = CfgIVal * 16 + DigitVal (C);
206 CfgTok = CFGTOK_INTCON;
210 /* Decimal number? */
213 while (isdigit (C)) {
214 CfgIVal = CfgIVal * 10 + DigitVal (C);
217 CfgTok = CFGTOK_INTCON;
221 /* Other characters */
226 CfgTok = CFGTOK_LCURLY;
231 CfgTok = CFGTOK_RCURLY;
236 CfgTok = CFGTOK_SEMI;
246 CfgTok = CFGTOK_COMMA;
256 CfgTok = CFGTOK_COLON;
263 if (C == EOF || C == '\n') {
264 Error ("%s(%u): Unterminated string", CfgName, InputLine);
266 if (I < CFG_MAX_IDENT_LEN) {
273 CfgTok = CFGTOK_STRCON;
278 while (C != '\n' && C != EOF) {
294 strncpy (CfgSVal, OutputName, CFG_MAX_IDENT_LEN);
295 CfgSVal [CFG_MAX_IDENT_LEN] = '\0';
299 CfgTok = CFGTOK_STRCON;
305 CfgTok = CFGTOK_INTCON;
309 CfgError ("Invalid format specification");
318 Error ("%s(%u): Invalid character `%c'", CfgName, InputLine, C);
325 void CfgConsume (cfgtok_t T, const char* Msg)
326 /* Skip a token, print an error message if not found */
336 void CfgConsumeSemi (void)
337 /* Consume a semicolon */
339 CfgConsume (CFGTOK_SEMI, "`;' expected");
344 void CfgConsumeColon (void)
345 /* Consume a colon */
347 CfgConsume (CFGTOK_COLON, "`:' expected");
352 void CfgOptionalComma (void)
353 /* Consume a comma if there is one */
355 if (CfgTok == CFGTOK_COMMA) {
362 void CfgOptionalAssign (void)
363 /* Consume an equal sign if there is one */
365 if (CfgTok == CFGTOK_EQ) {
372 void CfgAssureInt (void)
373 /* Make sure the next token is an integer */
375 if (CfgTok != CFGTOK_INTCON) {
376 CfgError ("Integer constant expected");
382 void CfgAssureStr (void)
383 /* Make sure the next token is a string constant */
385 if (CfgTok != CFGTOK_STRCON) {
386 CfgError ("String constant expected");
392 void CfgAssureIdent (void)
393 /* Make sure the next token is an identifier */
395 if (CfgTok != CFGTOK_IDENT) {
396 CfgError ("Identifier expected");
402 void CfgRangeCheck (unsigned long Lo, unsigned long Hi)
403 /* Check the range of CfgIVal */
405 if (CfgIVal < Lo || CfgIVal > Hi) {
406 CfgError ("Range error");
412 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
413 /* Map an identifier to one of the special tokens in the table */
417 /* We need an identifier */
418 if (CfgTok == CFGTOK_IDENT) {
420 /* Make it upper case */
422 while (CfgSVal [I]) {
423 CfgSVal [I] = toupper (CfgSVal [I]);
428 for (I = 0; I < Size; ++I) {
429 if (strcmp (CfgSVal, Table [I].Ident) == 0) {
430 CfgTok = Table [I].Tok;
437 /* Not found or no identifier */
438 Error ("%s(%u): %s expected", CfgName, InputLine, Name);
443 void CfgBoolToken (void)
444 /* Map an identifier or integer to a boolean token */
446 static const IdentTok Booleans [] = {
447 { "YES", CFGTOK_TRUE },
448 { "NO", CFGTOK_FALSE },
449 { "TRUE", CFGTOK_TRUE },
450 { "FALSE", CFGTOK_FALSE },
453 /* If we have an identifier, map it to a boolean token */
454 if (CfgTok == CFGTOK_IDENT) {
455 CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
457 /* We expected an integer here */
458 if (CfgTok != CFGTOK_INTCON) {
459 CfgError ("Boolean value expected");
461 CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
467 void CfgSetName (const char* Name)
468 /* Set a name for a config file */
475 const char* CfgGetName (void)
476 /* Get the name of the config file */
478 return CfgName? CfgName : "";
483 void CfgSetBuf (const char* Buf)
484 /* Set a memory buffer for the config */
492 /* Return true if we have a configuration available */
494 return CfgName != 0 || CfgBuf != 0;
499 void CfgOpenInput (void)
500 /* Open the input file if we have one */
502 /* If we have a config name given, open the file, otherwise we will read
508 InputFile = fopen (CfgName, "r");
509 if (InputFile == 0) {
510 Error ("Cannot open `%s': %s", CfgName, strerror (errno));
515 /* Initialize variables */
520 /* Start the ball rolling ... */
526 void CfgCloseInput (void)
527 /* Close the input file if we have one */
529 /* Close the input file if we had one */
531 (void) fclose (InputFile);