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 /*****************************************************************************/
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_LCURLY;
235 CfgTok = CFGTOK_RCURLY;
240 CfgTok = CFGTOK_SEMI;
250 CfgTok = CFGTOK_COMMA;
260 CfgTok = CFGTOK_COLON;
267 if (C == EOF || C == '\n') {
268 CfgError ("Unterminated string");
270 if (I < CFG_MAX_IDENT_LEN) {
277 CfgTok = CFGTOK_STRCON;
282 while (C != '\n' && C != EOF) {
298 strncpy (CfgSVal, OutputName, CFG_MAX_IDENT_LEN);
299 CfgSVal [CFG_MAX_IDENT_LEN] = '\0';
303 CfgTok = CFGTOK_STRCON;
309 CfgTok = CFGTOK_INTCON;
313 CfgError ("Invalid format specification");
322 CfgError ("Invalid character `%c'", C);
329 void CfgConsume (cfgtok_t T, const char* Msg)
330 /* Skip a token, print an error message if not found */
340 void CfgConsumeSemi (void)
341 /* Consume a semicolon */
343 CfgConsume (CFGTOK_SEMI, "`;' expected");
348 void CfgConsumeColon (void)
349 /* Consume a colon */
351 CfgConsume (CFGTOK_COLON, "`:' expected");
356 void CfgOptionalComma (void)
357 /* Consume a comma if there is one */
359 if (CfgTok == CFGTOK_COMMA) {
366 void CfgOptionalAssign (void)
367 /* Consume an equal sign if there is one */
369 if (CfgTok == CFGTOK_EQ) {
376 void CfgAssureInt (void)
377 /* Make sure the next token is an integer */
379 if (CfgTok != CFGTOK_INTCON) {
380 CfgError ("Integer constant expected");
386 void CfgAssureStr (void)
387 /* Make sure the next token is a string constant */
389 if (CfgTok != CFGTOK_STRCON) {
390 CfgError ("String constant expected");
396 void CfgAssureIdent (void)
397 /* Make sure the next token is an identifier */
399 if (CfgTok != CFGTOK_IDENT) {
400 CfgError ("Identifier expected");
406 void CfgRangeCheck (unsigned long Lo, unsigned long Hi)
407 /* Check the range of CfgIVal */
409 if (CfgIVal < Lo || CfgIVal > Hi) {
410 CfgError ("Range error");
416 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
417 /* Map an identifier to one of the special tokens in the table */
421 /* We need an identifier */
422 if (CfgTok == CFGTOK_IDENT) {
424 /* Make it upper case */
426 while (CfgSVal [I]) {
427 CfgSVal [I] = toupper (CfgSVal [I]);
432 for (I = 0; I < Size; ++I) {
433 if (strcmp (CfgSVal, Table [I].Ident) == 0) {
434 CfgTok = Table [I].Tok;
441 /* Not found or no identifier */
442 CfgError ("%s expected", Name);
447 void CfgBoolToken (void)
448 /* Map an identifier or integer to a boolean token */
450 static const IdentTok Booleans [] = {
451 { "YES", CFGTOK_TRUE },
452 { "NO", CFGTOK_FALSE },
453 { "TRUE", CFGTOK_TRUE },
454 { "FALSE", CFGTOK_FALSE },
457 /* If we have an identifier, map it to a boolean token */
458 if (CfgTok == CFGTOK_IDENT) {
459 CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
461 /* We expected an integer here */
462 if (CfgTok != CFGTOK_INTCON) {
463 CfgError ("Boolean value expected");
465 CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
471 void CfgSetName (const char* Name)
472 /* Set a name for a config file */
479 const char* CfgGetName (void)
480 /* Get the name of the config file */
485 return "[builtin config]";
493 void CfgSetBuf (const char* Buf)
494 /* Set a memory buffer for the config */
502 /* Return true if we have a configuration available */
504 return CfgName != 0 || CfgBuf != 0;
509 void CfgOpenInput (void)
510 /* Open the input file if we have one */
512 /* If we have a config name given, open the file, otherwise we will read
518 InputFile = fopen (CfgName, "r");
519 if (InputFile == 0) {
520 Error ("Cannot open `%s': %s", CfgName, strerror (errno));
525 /* Initialize variables */
530 /* Start the ball rolling ... */
536 void CfgCloseInput (void)
537 /* Close the input file if we have one */
539 /* Close the input file if we had one */
541 (void) fclose (InputFile);