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
56/* View/edit this file with tab stops set to 4 */
57
58#ifdef HAVE_XORG_CONFIG_H
59#include <xorg-config.h>
60#endif
61
62#include "xf86Parser.h"
63#include "xf86tokens.h"
64#include "Configint.h"
65
66extern LexRec val;
67
68static xf86ConfigSymTabRec TopLevelTab[] =
69{
70	{SECTION, "section"},
71	{-1, ""},
72};
73
74#define CLEANUP xf86freeConfig
75
76/*
77 * This function resolves name references and reports errors if the named
78 * objects cannot be found.
79 */
80static int
81xf86validateConfig (XF86ConfigPtr p)
82{
83	if (!xf86validateScreen (p))
84		return FALSE;
85	if (!xf86validateInput (p))
86		return FALSE;
87	if (!xf86validateLayout (p))
88		return FALSE;
89
90	return TRUE;
91}
92
93XF86ConfigPtr
94xf86readConfigFile (void)
95{
96	int token;
97	XF86ConfigPtr ptr = NULL;
98
99	if ((ptr = calloc (1, sizeof (XF86ConfigRec))) == NULL)
100	{
101		return NULL;
102	}
103
104	while ((token = xf86getToken (TopLevelTab)) != EOF_TOKEN)
105	{
106		switch (token)
107		{
108		case COMMENT:
109			ptr->conf_comment = xf86addComment(ptr->conf_comment, val.str);
110			break;
111		case SECTION:
112			if (xf86getSubToken (&(ptr->conf_comment)) != STRING)
113			{
114				xf86parseError (QUOTE_MSG, "Section");
115				CLEANUP (ptr);
116				return NULL;
117			}
118			xf86setSection (val.str);
119			if (xf86nameCompare (val.str, "files") == 0)
120			{
121				free(val.str);
122				val.str = NULL;
123				HANDLE_RETURN (conf_files, xf86parseFilesSection ());
124			}
125			else if (xf86nameCompare (val.str, "serverflags") == 0)
126			{
127				free(val.str);
128				val.str = NULL;
129				HANDLE_RETURN (conf_flags, xf86parseFlagsSection ());
130			}
131			else if (xf86nameCompare (val.str, "pointer") == 0)
132			{
133				free(val.str);
134				val.str = NULL;
135				HANDLE_LIST (conf_input_lst, xf86parsePointerSection,
136							 XF86ConfInputPtr);
137			}
138			else if (xf86nameCompare (val.str, "videoadaptor") == 0)
139			{
140				free(val.str);
141				val.str = NULL;
142				HANDLE_LIST (conf_videoadaptor_lst, xf86parseVideoAdaptorSection,
143							 XF86ConfVideoAdaptorPtr);
144			}
145			else if (xf86nameCompare (val.str, "device") == 0)
146			{
147				free(val.str);
148				val.str = NULL;
149				HANDLE_LIST (conf_device_lst, xf86parseDeviceSection,
150							 XF86ConfDevicePtr);
151			}
152			else if (xf86nameCompare (val.str, "monitor") == 0)
153			{
154				free(val.str);
155				val.str = NULL;
156				HANDLE_LIST (conf_monitor_lst, xf86parseMonitorSection,
157							 XF86ConfMonitorPtr);
158			}
159			else if (xf86nameCompare (val.str, "modes") == 0)
160			{
161				free(val.str);
162				val.str = NULL;
163				HANDLE_LIST (conf_modes_lst, xf86parseModesSection,
164							 XF86ConfModesPtr);
165			}
166			else if (xf86nameCompare (val.str, "screen") == 0)
167			{
168				free(val.str);
169				val.str = NULL;
170				HANDLE_LIST (conf_screen_lst, xf86parseScreenSection,
171							 XF86ConfScreenPtr);
172			}
173			else if (xf86nameCompare(val.str, "inputdevice") == 0)
174			{
175				free(val.str);
176				val.str = NULL;
177				HANDLE_LIST (conf_input_lst, xf86parseInputSection,
178							 XF86ConfInputPtr);
179			}
180			else if (xf86nameCompare(val.str, "inputclass") == 0)
181			{
182				free(val.str);
183				val.str = NULL;
184				HANDLE_LIST (conf_inputclass_lst,
185						xf86parseInputClassSection,
186						XF86ConfInputClassPtr);
187			}
188			else if (xf86nameCompare (val.str, "module") == 0)
189			{
190				free(val.str);
191				val.str = NULL;
192				HANDLE_RETURN (conf_modules, xf86parseModuleSection ());
193			}
194			else if (xf86nameCompare (val.str, "serverlayout") == 0)
195			{
196				free(val.str);
197				val.str = NULL;
198				HANDLE_LIST (conf_layout_lst, xf86parseLayoutSection,
199							 XF86ConfLayoutPtr);
200			}
201			else if (xf86nameCompare (val.str, "vendor") == 0)
202			{
203				free(val.str);
204				val.str = NULL;
205				HANDLE_LIST (conf_vendor_lst, xf86parseVendorSection,
206							 XF86ConfVendorPtr);
207			}
208			else if (xf86nameCompare (val.str, "dri") == 0)
209			{
210				free(val.str);
211				val.str = NULL;
212				HANDLE_RETURN (conf_dri, xf86parseDRISection ());
213			}
214			else if (xf86nameCompare (val.str, "extensions") == 0)
215			{
216				free(val.str);
217				val.str = NULL;
218				HANDLE_RETURN (conf_extensions, xf86parseExtensionsSection ());
219			}
220			else
221			{
222				free(val.str);
223				val.str = NULL;
224				Error (INVALID_SECTION_MSG, xf86tokenString ());
225			}
226			break;
227		default:
228			free(val.str);
229			val.str = NULL;
230			Error (INVALID_KEYWORD_MSG, xf86tokenString ());
231		}
232	}
233
234	if (xf86validateConfig (ptr))
235		return ptr;
236	else
237	{
238		CLEANUP (ptr);
239		return NULL;
240	}
241}
242
243#undef CLEANUP
244
245/*
246 * adds an item to the end of the linked list. Any record whose first field
247 * is a GenericListRec can be cast to this type and used with this function.
248 * A pointer to the head of the list is returned to handle the addition of
249 * the first item.
250 */
251GenericListPtr
252xf86addListItem (GenericListPtr head, GenericListPtr new)
253{
254	GenericListPtr p = head;
255	GenericListPtr last = NULL;
256
257	while (p)
258	{
259		last = p;
260		p = p->next;
261	}
262
263	if (last)
264	{
265		last->next = new;
266		return head;
267	}
268	else
269		return new;
270}
271
272/*
273 * Test if one chained list contains the other.
274 * In this case both list have the same endpoint (provided they don't loop)
275 */
276int
277xf86itemNotSublist(GenericListPtr list_1, GenericListPtr list_2)
278{
279	GenericListPtr p = list_1;
280	GenericListPtr last_1 = NULL, last_2 = NULL;
281
282	while (p) {
283		last_1 = p;
284		p = p->next;
285	}
286
287	p = list_2;
288	while (p) {
289		last_2 = p;
290		p = p->next;
291	}
292
293	return (!(last_1 == last_2));
294}
295
296void
297xf86freeConfig (XF86ConfigPtr p)
298{
299	if (p == NULL)
300		return;
301
302	xf86freeFiles (p->conf_files);
303	xf86freeModules (p->conf_modules);
304	xf86freeFlags (p->conf_flags);
305	xf86freeMonitorList (p->conf_monitor_lst);
306	xf86freeModesList (p->conf_modes_lst);
307	xf86freeVideoAdaptorList (p->conf_videoadaptor_lst);
308	xf86freeDeviceList (p->conf_device_lst);
309	xf86freeScreenList (p->conf_screen_lst);
310	xf86freeLayoutList (p->conf_layout_lst);
311	xf86freeInputList (p->conf_input_lst);
312	xf86freeVendorList (p->conf_vendor_lst);
313	xf86freeDRI (p->conf_dri);
314	xf86freeExtensions (p->conf_extensions);
315	TestFree(p->conf_comment);
316
317	free (p);
318}
319