]> git.sur5r.net Git - cc65/blob - src/ld65/scanner.c
8767c368c04076d60818e55d481ce5f6e283f19b
[cc65] / src / ld65 / scanner.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 scanner.c                                 */
4 /*                                                                           */
5 /*              Configuration file scanner for the ld65 linker               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2000 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <ctype.h>
41
42 /* common */
43 #include "chartype.h"
44 #include "strbuf.h"
45 #include "xsprintf.h"
46
47 /* ld65 */
48 #include "global.h"
49 #include "error.h"
50 #include "scanner.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 /* Current token and attributes */
61 cfgtok_t        CfgTok;
62 char            CfgSVal [CFG_MAX_IDENT_LEN+1];
63 unsigned long   CfgIVal;
64
65 /* Error location */
66 unsigned                CfgErrorLine;
67 unsigned                CfgErrorCol;
68
69 /* Input sources for the configuration */
70 static const char*      CfgName         = 0;
71 static const char*      CfgBuf          = 0;
72
73 /* Other input stuff */
74 static int              C               = ' ';
75 static unsigned         InputLine       = 1;
76 static unsigned         InputCol        = 0;
77 static FILE*            InputFile       = 0;
78
79
80
81 /*****************************************************************************/
82 /*                              Error handling                               */
83 /*****************************************************************************/
84
85
86
87 void CfgWarning (const char* Format, ...)
88 /* Print a warning message adding file name and line number of the config file */
89 {
90     StrBuf Buf = STATIC_STRBUF_INITIALIZER;
91     va_list ap;
92
93     va_start (ap, Format);
94     SB_VPrintf (&Buf, Format, ap);
95     va_end (ap);
96
97     Warning ("%s(%u): %s", CfgGetName(), CfgErrorLine, SB_GetConstBuf (&Buf));
98     DoneStrBuf (&Buf);
99 }
100
101
102
103 void CfgError (const char* Format, ...)
104 /* Print an error message adding file name and line number of the config file */
105 {
106     StrBuf Buf = STATIC_STRBUF_INITIALIZER;
107     va_list ap;
108
109     va_start (ap, Format);
110     SB_VPrintf (&Buf, Format, ap);
111     va_end (ap);
112
113     Error ("%s(%u): %s", CfgGetName(), CfgErrorLine, SB_GetConstBuf (&Buf));
114     DoneStrBuf (&Buf);
115 }
116
117
118
119 /*****************************************************************************/
120 /*                                   Code                                    */
121 /*****************************************************************************/
122
123
124
125 static void NextChar (void)
126 /* Read the next character from the input file */
127 {
128     if (CfgBuf) {
129         /* Read from buffer */
130         C = (unsigned char)(*CfgBuf);
131         if (C == 0) {
132             C = EOF;
133         } else {
134             ++CfgBuf;
135         }
136     } else {
137         /* Read from the file */
138         C = getc (InputFile);
139     }
140
141     /* Count columns */
142     if (C != EOF) {
143         ++InputCol;
144     }
145
146     /* Count lines */
147     if (C == '\n') {
148         ++InputLine;
149         InputCol = 0;
150     }
151 }
152
153
154
155 static unsigned DigitVal (int C)
156 /* Return the value for a numeric digit */
157 {
158     if (isdigit (C)) {
159         return C - '0';
160     } else {
161         return toupper (C) - 'A' + 10;
162     }
163 }
164
165
166
167 void CfgNextTok (void)
168 /* Read the next token from the input stream */
169 {
170     unsigned I;
171
172
173 Again:
174     /* Skip whitespace */
175     while (isspace (C)) {
176         NextChar ();
177     }
178
179     /* Remember the current position */
180     CfgErrorLine = InputLine;
181     CfgErrorCol  = InputCol;
182
183     /* Identifier? */
184     if (C == '_' || IsAlpha (C)) {
185
186         /* Read the identifier */
187         I = 0;
188         while (C == '_' || IsAlNum (C)) {
189             if (I < CFG_MAX_IDENT_LEN) {
190                 CfgSVal [I++] = C;
191             }
192             NextChar ();
193         }
194         CfgSVal [I] = '\0';
195         CfgTok = CFGTOK_IDENT;
196         return;
197     }
198
199     /* Hex number? */
200     if (C == '$') {
201         NextChar ();
202         if (!isxdigit (C)) {
203             CfgError ("Hex digit expected");
204         }
205         CfgIVal = 0;
206         while (isxdigit (C)) {
207             CfgIVal = CfgIVal * 16 + DigitVal (C);
208             NextChar ();
209         }
210         CfgTok = CFGTOK_INTCON;
211         return;
212     }
213
214     /* Decimal number? */
215     if (isdigit (C)) {
216         CfgIVal = 0;
217         while (isdigit (C)) {
218             CfgIVal = CfgIVal * 10 + DigitVal (C);
219             NextChar ();
220         }
221         CfgTok = CFGTOK_INTCON;
222         return;
223     }
224
225     /* Other characters */
226     switch (C) {
227
228         case '{':
229             NextChar ();
230             CfgTok = CFGTOK_LCURLY;
231             break;
232
233         case '}':
234             NextChar ();
235             CfgTok = CFGTOK_RCURLY;
236             break;
237
238         case ';':
239             NextChar ();
240             CfgTok = CFGTOK_SEMI;
241             break;
242
243         case '.':
244             NextChar ();
245             CfgTok = CFGTOK_DOT;
246             break;
247
248         case ',':
249             NextChar ();
250             CfgTok = CFGTOK_COMMA;
251             break;
252
253         case '=':
254             NextChar ();
255             CfgTok = CFGTOK_EQ;
256             break;
257
258         case ':':
259             NextChar ();
260             CfgTok = CFGTOK_COLON;
261             break;
262
263         case '\"':
264             NextChar ();
265             I = 0;
266             while (C != '\"') {
267                 if (C == EOF || C == '\n') {
268                     CfgError ("Unterminated string");
269                 }
270                 if (I < CFG_MAX_IDENT_LEN) {
271                     CfgSVal [I++] = C;
272                 }
273                 NextChar ();
274             }
275             NextChar ();
276             CfgSVal [I] = '\0';
277             CfgTok = CFGTOK_STRCON;
278             break;
279
280         case '#':
281             /* Comment */
282             while (C != '\n' && C != EOF) {
283                 NextChar ();
284             }
285             if (C != EOF) {
286                 goto Again;
287             }
288             CfgTok = CFGTOK_EOF;
289             break;
290
291         case '%':
292             NextChar ();
293             switch (C) {
294
295                 case 'O':
296                     NextChar ();
297                     if (OutputName) {
298                         strncpy (CfgSVal, OutputName, CFG_MAX_IDENT_LEN);
299                         CfgSVal [CFG_MAX_IDENT_LEN] = '\0';
300                     } else {
301                         CfgSVal [0] = '\0';
302                     }
303                     CfgTok = CFGTOK_STRCON;
304                     break;
305
306                 case 'S':
307                     NextChar ();
308                     CfgIVal = StartAddr;
309                     CfgTok = CFGTOK_INTCON;
310                     break;
311
312                 default:
313                     CfgError ("Invalid format specification");
314             }
315             break;
316
317         case EOF:
318             CfgTok = CFGTOK_EOF;
319             break;
320
321         default:
322             CfgError ("Invalid character `%c'", C);
323
324     }
325 }
326
327
328
329 void CfgConsume (cfgtok_t T, const char* Msg)
330 /* Skip a token, print an error message if not found */
331 {
332     if (CfgTok != T) {
333         CfgError (Msg);
334     }
335     CfgNextTok ();
336 }
337
338
339
340 void CfgConsumeSemi (void)
341 /* Consume a semicolon */
342 {
343     CfgConsume (CFGTOK_SEMI, "`;' expected");
344 }
345
346
347
348 void CfgConsumeColon (void)
349 /* Consume a colon */
350 {
351     CfgConsume (CFGTOK_COLON, "`:' expected");
352 }
353
354
355
356 void CfgOptionalComma (void)
357 /* Consume a comma if there is one */
358 {
359     if (CfgTok == CFGTOK_COMMA) {
360         CfgNextTok ();
361     }
362 }
363
364
365
366 void CfgOptionalAssign (void)
367 /* Consume an equal sign if there is one */
368 {
369     if (CfgTok == CFGTOK_EQ) {
370         CfgNextTok ();
371     }
372 }
373
374
375
376 void CfgAssureInt (void)
377 /* Make sure the next token is an integer */
378 {
379     if (CfgTok != CFGTOK_INTCON) {
380         CfgError ("Integer constant expected");
381     }
382 }
383
384
385
386 void CfgAssureStr (void)
387 /* Make sure the next token is a string constant */
388 {
389     if (CfgTok != CFGTOK_STRCON) {
390         CfgError ("String constant expected");
391     }
392 }
393
394
395
396 void CfgAssureIdent (void)
397 /* Make sure the next token is an identifier */
398 {
399     if (CfgTok != CFGTOK_IDENT) {
400         CfgError ("Identifier expected");
401     }
402 }
403
404
405
406 void CfgRangeCheck (unsigned long Lo, unsigned long Hi)
407 /* Check the range of CfgIVal */
408 {
409     if (CfgIVal < Lo || CfgIVal > Hi) {
410         CfgError ("Range error");
411     }
412 }
413
414
415
416 void CfgSpecialToken (const IdentTok* Table, unsigned Size, const char* Name)
417 /* Map an identifier to one of the special tokens in the table */
418 {
419     unsigned I;
420
421     /* We need an identifier */
422     if (CfgTok == CFGTOK_IDENT) {
423
424         /* Make it upper case */
425         I = 0;
426         while (CfgSVal [I]) {
427             CfgSVal [I] = toupper (CfgSVal [I]);
428             ++I;
429         }
430
431         /* Linear search */
432         for (I = 0; I < Size; ++I) {
433             if (strcmp (CfgSVal, Table [I].Ident) == 0) {
434                 CfgTok = Table [I].Tok;
435                 return;
436             }
437         }
438
439     }
440
441     /* Not found or no identifier */
442     CfgError ("%s expected", Name);
443 }
444
445
446
447 void CfgBoolToken (void)
448 /* Map an identifier or integer to a boolean token */
449 {
450     static const IdentTok Booleans [] = {
451         {   "YES",      CFGTOK_TRUE     },
452         {   "NO",       CFGTOK_FALSE    },
453         {   "TRUE",     CFGTOK_TRUE     },
454         {   "FALSE",    CFGTOK_FALSE    },
455     };
456
457     /* If we have an identifier, map it to a boolean token */
458     if (CfgTok == CFGTOK_IDENT) {
459         CfgSpecialToken (Booleans, ENTRY_COUNT (Booleans), "Boolean");
460     } else {
461         /* We expected an integer here */
462         if (CfgTok != CFGTOK_INTCON) {
463             CfgError ("Boolean value expected");
464         }
465         CfgTok = (CfgIVal == 0)? CFGTOK_FALSE : CFGTOK_TRUE;
466     }
467 }
468
469
470
471 void CfgSetName (const char* Name)
472 /* Set a name for a config file */
473 {
474     CfgName = Name;
475 }
476
477
478
479 const char* CfgGetName (void)
480 /* Get the name of the config file */
481 {
482     if (CfgName) {
483         return CfgName;
484     } else if (CfgBuf) {
485         return "[builtin config]";
486     } else {
487         return "";
488     }
489 }
490
491
492
493 void CfgSetBuf (const char* Buf)
494 /* Set a memory buffer for the config */
495 {
496     CfgBuf = Buf;
497 }
498
499
500
501 int CfgAvail (void)
502 /* Return true if we have a configuration available */
503 {
504     return CfgName != 0 || CfgBuf != 0;
505 }
506
507
508
509 void CfgOpenInput (void)
510 /* Open the input file if we have one */
511 {
512     /* If we have a config name given, open the file, otherwise we will read
513      * from a buffer.
514      */
515     if (!CfgBuf) {
516
517         /* Open the file */
518         InputFile = fopen (CfgName, "r");
519         if (InputFile == 0) {
520             Error ("Cannot open `%s': %s", CfgName, strerror (errno));
521         }
522
523     }
524
525     /* Initialize variables */
526     C         = ' ';
527     InputLine = 1;
528     InputCol  = 0;
529
530     /* Start the ball rolling ... */
531     CfgNextTok ();
532 }
533
534
535
536 void CfgCloseInput (void)
537 /* Close the input file if we have one */
538 {
539     /* Close the input file if we had one */
540     if (InputFile) {
541         (void) fclose (InputFile);
542         InputFile = 0;
543     }
544 }
545
546
547
548