Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / xfree86 / parser / read.c
CommitLineData
a09e091a
JB
1/*
2 *
3 * Copyright (c) 1997 Metro Link Incorporated
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Except as contained in this notice, the name of the Metro Link shall not be
24 * used in advertising or otherwise to promote the sale, use or other dealings
25 * in this Software without prior written authorization from Metro Link.
26 *
27 */
28/*
29 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
30 *
31 * Permission is hereby granted, free of charge, to any person obtaining a
32 * copy of this software and associated documentation files (the "Software"),
33 * to deal in the Software without restriction, including without limitation
34 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
35 * and/or sell copies of the Software, and to permit persons to whom the
36 * Software is furnished to do so, subject to the following conditions:
37 *
38 * The above copyright notice and this permission notice shall be included in
39 * all copies or substantial portions of the Software.
40 *
41 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
44 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
45 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
46 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
47 * OTHER DEALINGS IN THE SOFTWARE.
48 *
49 * Except as contained in this notice, the name of the copyright holder(s)
50 * and author(s) shall not be used in advertising or otherwise to promote
51 * the sale, use or other dealings in this Software without prior written
52 * authorization from the copyright holder(s) and author(s).
53 */
54
55#ifdef HAVE_XORG_CONFIG_H
56#include <xorg-config.h>
57#endif
58
59#include "xf86Parser.h"
60#include "xf86tokens.h"
61#include "Configint.h"
62
63extern LexRec val;
64
65static xf86ConfigSymTabRec TopLevelTab[] = {
66 {SECTION, "section"},
67 {-1, ""},
68};
69
70#define CLEANUP xf86freeConfig
71
72/*
73 * This function resolves name references and reports errors if the named
74 * objects cannot be found.
75 */
76static int
77xf86validateConfig(XF86ConfigPtr p)
78{
79 if (!xf86validateScreen(p))
80 return FALSE;
81 if (!xf86validateInput(p))
82 return FALSE;
83 if (!xf86validateLayout(p))
84 return FALSE;
85
86 return TRUE;
87}
88
89XF86ConfigPtr
90xf86readConfigFile(void)
91{
92 int token;
93 XF86ConfigPtr ptr = NULL;
94
95 if ((ptr = calloc(1, sizeof(XF86ConfigRec))) == NULL) {
96 return NULL;
97 }
98
99 while ((token = xf86getToken(TopLevelTab)) != EOF_TOKEN) {
100 switch (token) {
101 case COMMENT:
102 ptr->conf_comment = xf86addComment(ptr->conf_comment, val.str);
103 break;
104 case SECTION:
105 if (xf86getSubToken(&(ptr->conf_comment)) != STRING) {
106 xf86parseError(QUOTE_MSG, "Section");
107 CLEANUP(ptr);
108 return NULL;
109 }
110 xf86setSection(val.str);
111 if (xf86nameCompare(val.str, "files") == 0) {
112 free(val.str);
113 val.str = NULL;
114 HANDLE_RETURN(conf_files, xf86parseFilesSection());
115 }
116 else if (xf86nameCompare(val.str, "serverflags") == 0) {
117 free(val.str);
118 val.str = NULL;
119 HANDLE_RETURN(conf_flags, xf86parseFlagsSection());
120 }
121 else if (xf86nameCompare(val.str, "pointer") == 0) {
122 free(val.str);
123 val.str = NULL;
124 HANDLE_LIST(conf_input_lst, xf86parsePointerSection,
125 XF86ConfInputPtr);
126 }
127 else if (xf86nameCompare(val.str, "videoadaptor") == 0) {
128 free(val.str);
129 val.str = NULL;
130 HANDLE_LIST(conf_videoadaptor_lst, xf86parseVideoAdaptorSection,
131 XF86ConfVideoAdaptorPtr);
132 }
133 else if (xf86nameCompare(val.str, "device") == 0) {
134 free(val.str);
135 val.str = NULL;
136 HANDLE_LIST(conf_device_lst, xf86parseDeviceSection,
137 XF86ConfDevicePtr);
138 }
139 else if (xf86nameCompare(val.str, "monitor") == 0) {
140 free(val.str);
141 val.str = NULL;
142 HANDLE_LIST(conf_monitor_lst, xf86parseMonitorSection,
143 XF86ConfMonitorPtr);
144 }
145 else if (xf86nameCompare(val.str, "modes") == 0) {
146 free(val.str);
147 val.str = NULL;
148 HANDLE_LIST(conf_modes_lst, xf86parseModesSection,
149 XF86ConfModesPtr);
150 }
151 else if (xf86nameCompare(val.str, "screen") == 0) {
152 free(val.str);
153 val.str = NULL;
154 HANDLE_LIST(conf_screen_lst, xf86parseScreenSection,
155 XF86ConfScreenPtr);
156 }
157 else if (xf86nameCompare(val.str, "inputdevice") == 0) {
158 free(val.str);
159 val.str = NULL;
160 HANDLE_LIST(conf_input_lst, xf86parseInputSection,
161 XF86ConfInputPtr);
162 }
163 else if (xf86nameCompare(val.str, "inputclass") == 0) {
164 free(val.str);
165 val.str = NULL;
166 HANDLE_LIST(conf_inputclass_lst,
167 xf86parseInputClassSection, XF86ConfInputClassPtr);
168 }
169 else if (xf86nameCompare(val.str, "module") == 0) {
170 free(val.str);
171 val.str = NULL;
172 HANDLE_RETURN(conf_modules, xf86parseModuleSection());
173 }
174 else if (xf86nameCompare(val.str, "serverlayout") == 0) {
175 free(val.str);
176 val.str = NULL;
177 HANDLE_LIST(conf_layout_lst, xf86parseLayoutSection,
178 XF86ConfLayoutPtr);
179 }
180 else if (xf86nameCompare(val.str, "vendor") == 0) {
181 free(val.str);
182 val.str = NULL;
183 HANDLE_LIST(conf_vendor_lst, xf86parseVendorSection,
184 XF86ConfVendorPtr);
185 }
186 else if (xf86nameCompare(val.str, "dri") == 0) {
187 free(val.str);
188 val.str = NULL;
189 HANDLE_RETURN(conf_dri, xf86parseDRISection());
190 }
191 else if (xf86nameCompare(val.str, "extensions") == 0) {
192 free(val.str);
193 val.str = NULL;
194 HANDLE_RETURN(conf_extensions, xf86parseExtensionsSection());
195 }
196 else {
197 free(val.str);
198 val.str = NULL;
199 Error(INVALID_SECTION_MSG, xf86tokenString());
200 }
201 break;
202 default:
203 free(val.str);
204 val.str = NULL;
205 Error(INVALID_KEYWORD_MSG, xf86tokenString());
206 }
207 }
208
209 if (xf86validateConfig(ptr))
210 return ptr;
211 else {
212 CLEANUP(ptr);
213 return NULL;
214 }
215}
216
217#undef CLEANUP
218
219/*
220 * adds an item to the end of the linked list. Any record whose first field
221 * is a GenericListRec can be cast to this type and used with this function.
222 * A pointer to the head of the list is returned to handle the addition of
223 * the first item.
224 */
225GenericListPtr
226xf86addListItem(GenericListPtr head, GenericListPtr new)
227{
228 GenericListPtr p = head;
229 GenericListPtr last = NULL;
230
231 while (p) {
232 last = p;
233 p = p->next;
234 }
235
236 if (last) {
237 last->next = new;
238 return head;
239 }
240 else
241 return new;
242}
243
244/*
245 * Test if one chained list contains the other.
246 * In this case both list have the same endpoint (provided they don't loop)
247 */
248int
249xf86itemNotSublist(GenericListPtr list_1, GenericListPtr list_2)
250{
251 GenericListPtr p = list_1;
252 GenericListPtr last_1 = NULL, last_2 = NULL;
253
254 while (p) {
255 last_1 = p;
256 p = p->next;
257 }
258
259 p = list_2;
260 while (p) {
261 last_2 = p;
262 p = p->next;
263 }
264
265 return (!(last_1 == last_2));
266}
267
268void
269xf86freeConfig(XF86ConfigPtr p)
270{
271 if (p == NULL)
272 return;
273
274 xf86freeFiles(p->conf_files);
275 xf86freeModules(p->conf_modules);
276 xf86freeFlags(p->conf_flags);
277 xf86freeMonitorList(p->conf_monitor_lst);
278 xf86freeModesList(p->conf_modes_lst);
279 xf86freeVideoAdaptorList(p->conf_videoadaptor_lst);
280 xf86freeDeviceList(p->conf_device_lst);
281 xf86freeScreenList(p->conf_screen_lst);
282 xf86freeLayoutList(p->conf_layout_lst);
283 xf86freeInputList(p->conf_input_lst);
284 xf86freeVendorList(p->conf_vendor_lst);
285 xf86freeDRI(p->conf_dri);
286 xf86freeExtensions(p->conf_extensions);
287 TestFree(p->conf_comment);
288
289 free(p);
290}