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 "xf86Config.h"
60#include "xf86Parser.h"
61#include "xf86tokens.h"
62#include "Configint.h"
63
64
65static const 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 = xf86allocateConfig()) == 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, xf86_lex_val.str);
103            free(xf86_lex_val.str);
104            xf86_lex_val.str = NULL;
105            break;
106        case SECTION:
107            if (xf86getSubToken(&(ptr->conf_comment)) != STRING) {
108                xf86parseError(QUOTE_MSG, "Section");
109                CLEANUP(ptr);
110                return NULL;
111            }
112            xf86setSection(xf86_lex_val.str);
113            if (xf86nameCompare(xf86_lex_val.str, "files") == 0) {
114                free(xf86_lex_val.str);
115                xf86_lex_val.str = NULL;
116                HANDLE_RETURN(conf_files, xf86parseFilesSection());
117            }
118            else if (xf86nameCompare(xf86_lex_val.str, "serverflags") == 0) {
119                free(xf86_lex_val.str);
120                xf86_lex_val.str = NULL;
121                HANDLE_RETURN(conf_flags, xf86parseFlagsSection());
122            }
123            else if (xf86nameCompare(xf86_lex_val.str, "pointer") == 0) {
124                free(xf86_lex_val.str);
125                xf86_lex_val.str = NULL;
126                HANDLE_LIST(conf_input_lst, xf86parsePointerSection,
127                            XF86ConfInputPtr);
128            }
129            else if (xf86nameCompare(xf86_lex_val.str, "videoadaptor") == 0) {
130                free(xf86_lex_val.str);
131                xf86_lex_val.str = NULL;
132                HANDLE_LIST(conf_videoadaptor_lst, xf86parseVideoAdaptorSection,
133                            XF86ConfVideoAdaptorPtr);
134            }
135            else if (xf86nameCompare(xf86_lex_val.str, "device") == 0) {
136                free(xf86_lex_val.str);
137                xf86_lex_val.str = NULL;
138                HANDLE_LIST(conf_device_lst, xf86parseDeviceSection,
139                            XF86ConfDevicePtr);
140            }
141            else if (xf86nameCompare(xf86_lex_val.str, "monitor") == 0) {
142                free(xf86_lex_val.str);
143                xf86_lex_val.str = NULL;
144                HANDLE_LIST(conf_monitor_lst, xf86parseMonitorSection,
145                            XF86ConfMonitorPtr);
146            }
147            else if (xf86nameCompare(xf86_lex_val.str, "modes") == 0) {
148                free(xf86_lex_val.str);
149                xf86_lex_val.str = NULL;
150                HANDLE_LIST(conf_modes_lst, xf86parseModesSection,
151                            XF86ConfModesPtr);
152            }
153            else if (xf86nameCompare(xf86_lex_val.str, "screen") == 0) {
154                free(xf86_lex_val.str);
155                xf86_lex_val.str = NULL;
156                HANDLE_LIST(conf_screen_lst, xf86parseScreenSection,
157                            XF86ConfScreenPtr);
158            }
159            else if (xf86nameCompare(xf86_lex_val.str, "inputdevice") == 0) {
160                free(xf86_lex_val.str);
161                xf86_lex_val.str = NULL;
162                HANDLE_LIST(conf_input_lst, xf86parseInputSection,
163                            XF86ConfInputPtr);
164            }
165            else if (xf86nameCompare(xf86_lex_val.str, "inputclass") == 0) {
166                free(xf86_lex_val.str);
167                xf86_lex_val.str = NULL;
168                HANDLE_LIST(conf_inputclass_lst,
169                            xf86parseInputClassSection, XF86ConfInputClassPtr);
170            }
171            else if (xf86nameCompare(xf86_lex_val.str, "outputclass") == 0) {
172                free(xf86_lex_val.str);
173                xf86_lex_val.str = NULL;
174                HANDLE_LIST(conf_outputclass_lst, xf86parseOutputClassSection,
175                            XF86ConfOutputClassPtr);
176            }
177            else if (xf86nameCompare(xf86_lex_val.str, "module") == 0) {
178                free(xf86_lex_val.str);
179                xf86_lex_val.str = NULL;
180                HANDLE_RETURN(conf_modules, xf86parseModuleSection());
181            }
182            else if (xf86nameCompare(xf86_lex_val.str, "serverlayout") == 0) {
183                free(xf86_lex_val.str);
184                xf86_lex_val.str = NULL;
185                HANDLE_LIST(conf_layout_lst, xf86parseLayoutSection,
186                            XF86ConfLayoutPtr);
187            }
188            else if (xf86nameCompare(xf86_lex_val.str, "vendor") == 0) {
189                free(xf86_lex_val.str);
190                xf86_lex_val.str = NULL;
191                HANDLE_LIST(conf_vendor_lst, xf86parseVendorSection,
192                            XF86ConfVendorPtr);
193            }
194            else if (xf86nameCompare(xf86_lex_val.str, "dri") == 0) {
195                free(xf86_lex_val.str);
196                xf86_lex_val.str = NULL;
197                HANDLE_RETURN(conf_dri, xf86parseDRISection());
198            }
199            else if (xf86nameCompare(xf86_lex_val.str, "extensions") == 0) {
200                free(xf86_lex_val.str);
201                xf86_lex_val.str = NULL;
202                HANDLE_RETURN(conf_extensions, xf86parseExtensionsSection());
203            }
204            else {
205                free(xf86_lex_val.str);
206                xf86_lex_val.str = NULL;
207                Error(INVALID_SECTION_MSG, xf86tokenString());
208            }
209            break;
210        default:
211            free(xf86_lex_val.str);
212            xf86_lex_val.str = NULL;
213            Error(INVALID_KEYWORD_MSG, xf86tokenString());
214        }
215    }
216
217    if (xf86validateConfig(ptr))
218        return ptr;
219    else {
220        CLEANUP(ptr);
221        return NULL;
222    }
223}
224
225#undef CLEANUP
226
227/*
228 * adds an item to the end of the linked list. Any record whose first field
229 * is a GenericListRec can be cast to this type and used with this function.
230 * A pointer to the head of the list is returned to handle the addition of
231 * the first item.
232 */
233GenericListPtr
234xf86addListItem(GenericListPtr head, GenericListPtr new)
235{
236    GenericListPtr p = head;
237    GenericListPtr last = NULL;
238
239    while (p) {
240        last = p;
241        p = p->next;
242    }
243
244    if (last) {
245        last->next = new;
246        return head;
247    }
248    else
249        return new;
250}
251
252/*
253 * Test if one chained list contains the other.
254 * In this case both list have the same endpoint (provided they don't loop)
255 */
256int
257xf86itemNotSublist(GenericListPtr list_1, GenericListPtr list_2)
258{
259    GenericListPtr p = list_1;
260    GenericListPtr last_1 = NULL, last_2 = NULL;
261
262    while (p) {
263        last_1 = p;
264        p = p->next;
265    }
266
267    p = list_2;
268    while (p) {
269        last_2 = p;
270        p = p->next;
271    }
272
273    return (!(last_1 == last_2));
274}
275
276/*
277 * Conditionally allocate config struct, but only allocate it
278 * if it's not already there.  In either event, return the pointer
279 * to the global config struct.
280 */
281XF86ConfigPtr xf86allocateConfig(void)
282{
283    if (!xf86configptr) {
284        xf86configptr = calloc(1, sizeof(XF86ConfigRec));
285    }
286    return xf86configptr;
287}
288
289void
290xf86freeConfig(XF86ConfigPtr p)
291{
292    if (p == NULL)
293        return;
294
295    xf86freeFiles(p->conf_files);
296    xf86freeModules(p->conf_modules);
297    xf86freeFlags(p->conf_flags);
298    xf86freeMonitorList(p->conf_monitor_lst);
299    xf86freeModesList(p->conf_modes_lst);
300    xf86freeVideoAdaptorList(p->conf_videoadaptor_lst);
301    xf86freeDeviceList(p->conf_device_lst);
302    xf86freeScreenList(p->conf_screen_lst);
303    xf86freeLayoutList(p->conf_layout_lst);
304    xf86freeInputList(p->conf_input_lst);
305    xf86freeVendorList(p->conf_vendor_lst);
306    xf86freeDRI(p->conf_dri);
307    xf86freeExtensions(p->conf_extensions);
308    TestFree(p->conf_comment);
309
310    free(p);
311}
312