]> git.sur5r.net Git - cc65/blob - src/da65/config.c
7d284d24240aaf53023c7afb74434803cc938730
[cc65] / src / da65 / config.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 config.c                                  */
4 /*                                                                           */
5 /*                 Disassembler configuration file handling                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 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 #if defined(_MSC_VER)
37 /* Microsoft compiler */
38 #  include <io.h>
39 #else
40 /* Anyone else */
41 #  include <unistd.h>
42 #endif
43
44 /* common */
45 #include "xmalloc.h"
46
47 /* da65 */
48 #include "global.h"
49 #include "scanner.h"
50 #include "config.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Code                                    */
56 /*****************************************************************************/
57
58
59
60 static void GlobalSection (void)
61 /* Parse a global section */
62 {
63     static const IdentTok GlobalDefs[] = {
64         {   "INPUTNAME",        CFGTOK_INPUTNAME        },
65         {   "OUTPUTNAME",       CFGTOK_OUTPUTNAME       },
66         {   "PAGELENGTH",       CFGTOK_PAGELENGTH       },
67     };
68
69     /* Skip the token */
70     CfgNextTok ();
71
72     /* Expect the opening curly brace */
73     CfgConsumeLCurly ();
74
75     /* Look for section tokens */
76     while (CfgTok != CFGTOK_RCURLY) {
77
78         /* Convert to special token */
79         CfgSpecialToken (GlobalDefs, ENTRY_COUNT (GlobalDefs), "Global directive");
80
81         /* Look at the token */
82         switch (CfgTok) {
83
84             case CFGTOK_INPUTNAME:
85                 CfgNextTok ();
86                 CfgAssureStr ();
87                 if (InFile) {
88                     CfgError ("Input file name already given");
89                 }
90                 InFile = xstrdup (CfgSVal);
91                 CfgNextTok ();
92                 break;
93
94             case CFGTOK_OUTPUTNAME:
95                 CfgNextTok ();
96                 CfgAssureStr ();
97                 if (OutFile) {
98                     CfgError ("Output file name already given");
99                 }
100                 OutFile = xstrdup (CfgSVal);
101                 CfgNextTok ();
102                 break;
103
104             case CFGTOK_PAGELENGTH:
105                 CfgNextTok ();
106                 CfgAssureInt ();
107                 if (CfgIVal != -1) {
108                     CfgRangeCheck (MIN_PAGE_LEN, MAX_PAGE_LEN);
109                 }
110                 PageLength = CfgIVal;
111                 CfgNextTok ();
112                 break;
113         }
114
115         /* Directive is followed by a semicolon */
116         CfgConsumeSemi ();
117
118     }
119
120     /* Consume the closing brace */
121     CfgConsumeRCurly ();
122 }
123
124
125
126 static void RangeSection (void)
127 /* Parse a range section */
128 {
129     static const IdentTok RangeDefs[] = {
130         {   "START",            CFGTOK_START    },
131         {   "END",              CFGTOK_END      },
132         {   "TYPE",             CFGTOK_TYPE     },
133     };
134
135     /* Skip the token */
136     CfgNextTok ();
137
138     /* Expect the opening curly brace */
139     CfgConsumeLCurly ();
140
141     /* Look for section tokens */
142     while (CfgTok != CFGTOK_RCURLY) {
143
144
145     }
146
147     /* Consume the closing brace */
148     CfgConsumeRCurly ();
149 }
150
151
152
153 static void LabelSection (void)
154 /* Parse a label section */
155 {
156     static const IdentTok Globals[] = {
157         {   "INPUTNAMEL",       CFGTOK_INPUTNAME        },
158         {   "OUTPUTNAME",       CFGTOK_OUTPUTNAME       },
159         {   "PAGELENGTH",       CFGTOK_PAGELENGTH       },
160     };
161
162     /* Skip the token */
163     CfgNextTok ();
164
165     /* Expect the opening curly brace */
166     CfgConsumeLCurly ();
167
168     /* Look for section tokens */
169     while (CfgTok != CFGTOK_RCURLY) {
170
171
172     }
173
174     /* Consume the closing brace */
175     CfgConsumeRCurly ();
176 }
177
178
179
180 static void CfgParse (void)
181 /* Parse the config file */
182 {
183     static const IdentTok Globals[] = {
184         {   "GLOBAL",   CFGTOK_GLOBAL   },
185         {   "RANGE",    CFGTOK_RANGE    },
186         {   "LABEL",    CFGTOK_LABEL    },
187     };
188
189     while (CfgTok != CFGTOK_EOF) {
190
191         /* Convert an identifier into a token */
192         CfgSpecialToken (Globals, ENTRY_COUNT (Globals), "Config directive");
193
194         /* Check the token */
195         switch (CfgTok) {
196
197             case CFGTOK_GLOBAL:
198                 GlobalSection ();
199                 break;
200
201             case CFGTOK_RANGE:
202                 RangeSection ();
203                 break;
204
205             case CFGTOK_LABEL:
206                 LabelSection ();
207                 break;
208
209         }
210
211         /* Semicolon expected */
212         CfgConsumeSemi ();
213     }
214 }
215
216
217
218 void CfgRead (void)
219 /* Read the configuration if a configuration file exists */
220 {
221     /* Check if we have a config file given */
222     if (!CfgAvail() || access (CfgGetName(), 0) != 0) {
223         /* No name given or file not found */
224         return;
225     }
226
227     /* Open the config file */
228     CfgOpenInput ();
229
230     /* Parse the config file */
231     CfgParse ();
232
233     /* Close the file */
234     CfgCloseInput ();
235 }
236
237
238
239
240
241