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