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