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/* View/edit this file with tab stops set to 4 */
56
57#ifdef HAVE_XORG_CONFIG_H
58#include <xorg-config.h>
59#endif
60
61#include "xf86Parser.h"
62#include "xf86tokens.h"
63#include "Configint.h"
64
65extern LexRec val;
66
67static xf86ConfigSymTabRec VideoPortTab[] =
68{
69	{ENDSUBSECTION, "endsubsection"},
70	{IDENTIFIER, "identifier"},
71	{OPTION, "option"},
72	{-1, ""},
73};
74
75#define CLEANUP xf86freeVideoPortList
76
77static void
78xf86freeVideoPortList (XF86ConfVideoPortPtr ptr)
79{
80	XF86ConfVideoPortPtr prev;
81
82	while (ptr)
83	{
84		TestFree (ptr->vp_identifier);
85		TestFree (ptr->vp_comment);
86		xf86optionListFree (ptr->vp_option_lst);
87		prev = ptr;
88		ptr = ptr->list.next;
89		free (prev);
90	}
91}
92
93static XF86ConfVideoPortPtr
94xf86parseVideoPortSubSection (void)
95{
96	int has_ident = FALSE;
97	int token;
98	parsePrologue (XF86ConfVideoPortPtr, XF86ConfVideoPortRec)
99
100	while ((token = xf86getToken (VideoPortTab)) != ENDSUBSECTION)
101	{
102		switch (token)
103		{
104		case COMMENT:
105			ptr->vp_comment = xf86addComment(ptr->vp_comment, val.str);
106			break;
107		case IDENTIFIER:
108			if (xf86getSubToken (&(ptr->vp_comment)) != STRING)
109				Error (QUOTE_MSG, "Identifier");
110			if (has_ident == TRUE)
111				Error (MULTIPLE_MSG, "Identifier");
112			ptr->vp_identifier = val.str;
113			has_ident = TRUE;
114			break;
115		case OPTION:
116			ptr->vp_option_lst = xf86parseOption(ptr->vp_option_lst);
117			break;
118
119		case EOF_TOKEN:
120			Error (UNEXPECTED_EOF_MSG, NULL);
121			break;
122		default:
123			Error (INVALID_KEYWORD_MSG, xf86tokenString ());
124			break;
125		}
126	}
127
128#ifdef DEBUG
129	printf ("VideoPort subsection parsed\n");
130#endif
131
132	return ptr;
133}
134
135#undef CLEANUP
136
137static xf86ConfigSymTabRec VideoAdaptorTab[] =
138{
139	{ENDSECTION, "endsection"},
140	{IDENTIFIER, "identifier"},
141	{VENDOR, "vendorname"},
142	{BOARD, "boardname"},
143	{BUSID, "busid"},
144	{DRIVER, "driver"},
145	{OPTION, "option"},
146	{SUBSECTION, "subsection"},
147	{-1, ""},
148};
149
150#define CLEANUP xf86freeVideoAdaptorList
151
152XF86ConfVideoAdaptorPtr
153xf86parseVideoAdaptorSection (void)
154{
155	int has_ident = FALSE;
156	int token;
157
158	parsePrologue (XF86ConfVideoAdaptorPtr, XF86ConfVideoAdaptorRec)
159
160	while ((token = xf86getToken (VideoAdaptorTab)) != ENDSECTION)
161	{
162		switch (token)
163		{
164		case COMMENT:
165			ptr->va_comment = xf86addComment(ptr->va_comment, val.str);
166			break;
167		case IDENTIFIER:
168			if (xf86getSubToken (&(ptr->va_comment)) != STRING)
169				Error (QUOTE_MSG, "Identifier");
170			ptr->va_identifier = val.str;
171			if (has_ident == TRUE)
172				Error (MULTIPLE_MSG, "Identifier");
173			has_ident = TRUE;
174			break;
175		case VENDOR:
176			if (xf86getSubToken (&(ptr->va_comment)) != STRING)
177				Error (QUOTE_MSG, "Vendor");
178			ptr->va_vendor = val.str;
179			break;
180		case BOARD:
181			if (xf86getSubToken (&(ptr->va_comment)) != STRING)
182				Error (QUOTE_MSG, "Board");
183			ptr->va_board = val.str;
184			break;
185		case BUSID:
186			if (xf86getSubToken (&(ptr->va_comment)) != STRING)
187				Error (QUOTE_MSG, "BusID");
188			ptr->va_busid = val.str;
189			break;
190		case DRIVER:
191			if (xf86getSubToken (&(ptr->va_comment)) != STRING)
192				Error (QUOTE_MSG, "Driver");
193			ptr->va_driver = val.str;
194			break;
195		case OPTION:
196			ptr->va_option_lst = xf86parseOption(ptr->va_option_lst);
197			break;
198		case SUBSECTION:
199			if (xf86getSubToken (&(ptr->va_comment)) != STRING)
200				Error (QUOTE_MSG, "SubSection");
201			{
202				HANDLE_LIST (va_port_lst, xf86parseVideoPortSubSection,
203							 XF86ConfVideoPortPtr);
204			}
205			break;
206
207		case EOF_TOKEN:
208			Error (UNEXPECTED_EOF_MSG, NULL);
209			break;
210		default:
211			Error (INVALID_KEYWORD_MSG, xf86tokenString ());
212			break;
213		}
214	}
215
216	if (!has_ident)
217		Error (NO_IDENT_MSG, NULL);
218
219#ifdef DEBUG
220	printf ("VideoAdaptor section parsed\n");
221#endif
222
223	return ptr;
224}
225
226void
227xf86printVideoAdaptorSection (FILE * cf, XF86ConfVideoAdaptorPtr ptr)
228{
229	XF86ConfVideoPortPtr pptr;
230
231	while (ptr)
232	{
233		fprintf (cf, "Section \"VideoAdaptor\"\n");
234		if (ptr->va_comment)
235			fprintf (cf, "%s", ptr->va_comment);
236		if (ptr->va_identifier)
237			fprintf (cf, "\tIdentifier  \"%s\"\n", ptr->va_identifier);
238		if (ptr->va_vendor)
239			fprintf (cf, "\tVendorName  \"%s\"\n", ptr->va_vendor);
240		if (ptr->va_board)
241			fprintf (cf, "\tBoardName   \"%s\"\n", ptr->va_board);
242		if (ptr->va_busid)
243			fprintf (cf, "\tBusID       \"%s\"\n", ptr->va_busid);
244		if (ptr->va_driver)
245			fprintf (cf, "\tDriver      \"%s\"\n", ptr->va_driver);
246		xf86printOptionList(cf, ptr->va_option_lst, 1);
247		for (pptr = ptr->va_port_lst; pptr; pptr = pptr->list.next)
248		{
249			fprintf (cf, "\tSubSection \"VideoPort\"\n");
250			if (pptr->vp_comment)
251				fprintf (cf, "%s", pptr->vp_comment);
252			if (pptr->vp_identifier)
253				fprintf (cf, "\t\tIdentifier \"%s\"\n", pptr->vp_identifier);
254			xf86printOptionList(cf, pptr->vp_option_lst, 2);
255			fprintf (cf, "\tEndSubSection\n");
256		}
257		fprintf (cf, "EndSection\n\n");
258		ptr = ptr->list.next;
259	}
260
261}
262
263void
264xf86freeVideoAdaptorList (XF86ConfVideoAdaptorPtr ptr)
265{
266	XF86ConfVideoAdaptorPtr prev;
267
268	while (ptr)
269	{
270		TestFree (ptr->va_identifier);
271		TestFree (ptr->va_vendor);
272		TestFree (ptr->va_board);
273		TestFree (ptr->va_busid);
274		TestFree (ptr->va_driver);
275		TestFree (ptr->va_fwdref);
276		TestFree (ptr->va_comment);
277		xf86freeVideoPortList (ptr->va_port_lst);
278		xf86optionListFree (ptr->va_option_lst);
279		prev = ptr;
280		ptr = ptr->list.next;
281		free (prev);
282	}
283}
284
285XF86ConfVideoAdaptorPtr
286xf86findVideoAdaptor (const char *ident, XF86ConfVideoAdaptorPtr p)
287{
288	while (p)
289	{
290		if (xf86nameCompare (ident, p->va_identifier) == 0)
291			return p;
292
293		p = p->list.next;
294	}
295	return NULL;
296}
297