1 /*****************************************************************************/
5 /* Configuration file scanner for the sim65 6502 simulator */
9 /* (C) 1998-2002 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 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Current token and attributes */
59 char CfgSVal [CFG_MAX_IDENT_LEN+1];
60 unsigned long CfgIVal;
63 unsigned CfgErrorLine;
66 /* Input sources for the configuration */
67 static const char* CfgName = 0;
68 static const char* CfgBuf = 0;
70 /* Other input stuff */
72 static unsigned InputLine = 1;
73 static unsigned InputCol = 0;
74 static FILE* InputFile = 0;
78 /*****************************************************************************/
80 /*****************************************************************************/
84 void CfgWarning (const char* Format, ...)
85 /* Print a warning message adding file name and line number of the config file */
90 va_start (ap, Format);
91 xvsprintf (Buf, sizeof (Buf), Format, ap);
94 Warning ("%s(%u): %s", CfgGetName(), CfgErrorLine, Buf);
99 void CfgError (const char* Format, ...)
100 /* Print an error message adding file name and line number of the config file */
105 va_start (ap, Format);
106 xvsprintf (Buf, sizeof (Buf), Format, ap);
109 Error ("%s(%u): %s", CfgGetName(), CfgErrorLine, Buf);
114 /*****************************************************************************/
116 /*****************************************************************************/
120 static void NextChar (void)
121 /* Read the next character from the input file */
124 /* Read from buffer */
125 C = (unsigned char)(*CfgBuf);
132 /* Read from the file */
133 C = getc (InputFile);
150 static unsigned DigitVal (int C)
151 /* Return the value for a numeric digit */
156 return toupper (C) - 'A' + 10;
162 void CfgNextTok (void)
163 /* Read the next token from the input stream */
169 /* Skip whitespace */
170 while (isspace (C)) {
174 /* Remember the current position */
175 CfgErrorLine = InputLine;
176 CfgErrorCol = InputCol;
179 if (C == '_' || isalpha (C)) {
181 /* Read the identifier */
183 while (C == '_' || isalnum (C)) {
184 if (I < CFG_MAX_IDENT_LEN) {
190 CfgTok = CFGTOK_IDENT;
198 Error ("%s(%u): Hex digit expected", CfgName, InputLine);
201 while (isxdigit (C)) {
202 CfgIVal = CfgIVal * 16 + DigitVal (C);
205 CfgTok = CFGTOK_INTCON;
209 /* Decimal number? */
212 while (isdigit (C)) {
213 CfgIVal = CfgIVal * 10 + DigitVal (C);
216 CfgTok = CFGTOK_INTCON;
220 /* Other characters */
225 CfgTok = CFGTOK_LCURLY;
230 CfgTok = CFGTOK_RCURLY;
235 CfgTok = CFGTOK_SEMI;
245 CfgTok = CFGTOK_COMMA;
255 CfgTok = CFGTOK_COLON;
262 if (C == EOF || C == '\n') {
263 Error ("%s(%u): Unterminated string", CfgName, InputLine);
265 if (I < CFG_MAX_IDENT_LEN) {
272 CfgTok = CFGTOK_STRCON;
277 while (C != '\n' && C != EOF) {
291 Error ("%s(%u): Invalid character `%c'", CfgName, InputLine, C);
298 void CfgConsume (cfgtok_t T, const char* Msg)
299 /* Skip a token, print an error message if not found */
309 void CfgConsumeSemi (void)
310 /* Consume a semicolon */
312 CfgConsume (CFGTOK_SEMI, "`;' expected");
317 void CfgConsumeColon (void)
318 /* Consume a colon */
320 CfgConsume (CFGTOK_COLON, "`:' expected");
325 void CfgOptionalComma (void)
326 /* Consume a comma if there is one */
328 if (CfgTok == CFGTOK_COMMA) {
335 void CfgOptionalAssign (void)
336 /* Consume an equal sign if there is one */
338 if (CfgTok == CFGTOK_EQ) {
345 void CfgAssureInt (void)
346 /* Make sure the next token is an integer */
348 if (CfgTok != CFGTOK_INTCON) {
349 CfgError ("Integer constant expected");
355 void CfgAssureStr (void)
356 /* Make sure the next token is a string constant */
358 if (CfgTok != CFGTOK_STRCON) {
359 CfgError ("String constant expected");
365 void CfgAssureIdent (void)
366 /* Make sure the next token is an identifier */
368 if (CfgTok != CFGTOK_IDENT) {
369 CfgError ("Identifier expected");
375 void CfgRangeCheck (unsigned long Lo, unsigned long Hi)
376 /* Check the range of CfgIVal */
378 if (CfgIVal < Lo || CfgIVal > Hi) {
379 CfgError ("Range error");
385 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
386 /* Map an identifier to one of the special tokens in the table */
390 /* We need an identifier */
391 if (CfgTok == CFGTOK_IDENT) {
393 /* Make it upper case */
395 while (CfgSVal [I]) {
396 CfgSVal [I] = toupper (CfgSVal [I]);
401 for (I = 0; I < Size; ++I) {
402 if (strcmp (CfgSVal, Table [I].Ident) == 0) {
403 CfgTok = Table [I].Tok;
410 /* Not found or no identifier */
411 Error ("%s(%u): %s expected", CfgName, InputLine, Name);
416 void CfgBoolToken (void)
417 /* Map an identifier or integer to a boolean token */
419 static const IdentTok Booleans [] = {
420 { "YES", CFGTOK_TRUE },
421 { "NO", CFGTOK_FALSE },
422 { "TRUE", CFGTOK_TRUE },
423 { "FALSE", CFGTOK_FALSE },
426 /* If we have an identifier, map it to a boolean token */
427 if (CfgTok == CFGTOK_IDENT) {
428 CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
430 /* We expected an integer here */
431 if (CfgTok != CFGTOK_INTCON) {
432 CfgError ("Boolean value expected");
434 CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
440 void CfgSetName (const char* Name)
441 /* Set a name for a config file */
448 const char* CfgGetName (void)
449 /* Get the name of the config file */
454 return "[builtin config]";
462 void CfgSetBuf (const char* Buf)
463 /* Set a memory buffer for the config */
471 /* Return true if we have a configuration available */
473 return CfgName != 0 || CfgBuf != 0;
478 void CfgOpenInput (void)
479 /* Open the input file if we have one */
481 /* If we have a config name given, open the file, otherwise we will read
487 InputFile = fopen (CfgName, "r");
488 if (InputFile == 0) {
489 Error ("Cannot open `%s': %s", CfgName, strerror (errno));
494 /* Initialize variables */
499 /* Start the ball rolling ... */
505 void CfgCloseInput (void)
506 /* Close the input file if we have one */
508 /* Close the input file if we had one */
510 (void) fclose (InputFile);