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 SubModuleTab[] = {
65    {ENDSUBSECTION, "endsubsection"},
66    {OPTION, "option"},
67    {-1, ""},
68};
69
70static const xf86ConfigSymTabRec ModuleTab[] = {
71    {ENDSECTION, "endsection"},
72    {LOAD, "load"},
73    {DISABLE, "disable"},
74    {LOAD_DRIVER, "loaddriver"},
75    {SUBSECTION, "subsection"},
76    {-1, ""},
77};
78
79#define CLEANUP xf86freeModules
80
81static XF86LoadPtr
82xf86parseModuleSubSection(XF86LoadPtr head, char *name)
83{
84    int token;
85
86    parsePrologue(XF86LoadPtr, XF86LoadRec)
87
88        ptr->load_name = name;
89    ptr->load_type = XF86_LOAD_MODULE;
90    ptr->ignore = 0;
91    ptr->load_opt = NULL;
92    ptr->list.next = NULL;
93
94    while ((token = xf86getToken(SubModuleTab)) != ENDSUBSECTION) {
95        switch (token) {
96        case COMMENT:
97            ptr->load_comment = xf86addComment(ptr->load_comment, xf86_lex_val.str);
98            free(xf86_lex_val.str);
99            xf86_lex_val.str = NULL;
100            break;
101        case OPTION:
102            ptr->load_opt = xf86parseOption(ptr->load_opt);
103            break;
104        case EOF_TOKEN:
105            xf86parseError(UNEXPECTED_EOF_MSG);
106            free(ptr);
107            return NULL;
108        default:
109            xf86parseError(INVALID_KEYWORD_MSG, xf86tokenString());
110            free(ptr);
111            return NULL;
112            break;
113        }
114
115    }
116
117    return ((XF86LoadPtr) xf86addListItem((glp) head, (glp) ptr));
118}
119
120XF86ConfModulePtr
121xf86parseModuleSection(void)
122{
123    int token;
124
125    parsePrologue(XF86ConfModulePtr, XF86ConfModuleRec)
126
127        while ((token = xf86getToken(ModuleTab)) != ENDSECTION) {
128        switch (token) {
129        case COMMENT:
130            ptr->mod_comment = xf86addComment(ptr->mod_comment, xf86_lex_val.str);
131            free(xf86_lex_val.str);
132            xf86_lex_val.str = NULL;
133            break;
134        case LOAD:
135            if (xf86getSubToken(&(ptr->mod_comment)) != STRING)
136                Error(QUOTE_MSG, "Load");
137            ptr->mod_load_lst =
138                xf86addNewLoadDirective(ptr->mod_load_lst, xf86_lex_val.str,
139                                        XF86_LOAD_MODULE, NULL);
140            break;
141        case DISABLE:
142            if (xf86getSubToken(&(ptr->mod_comment)) != STRING)
143                Error(QUOTE_MSG, "Disable");
144            ptr->mod_disable_lst =
145                xf86addNewLoadDirective(ptr->mod_disable_lst, xf86_lex_val.str,
146                                        XF86_DISABLE_MODULE, NULL);
147            break;
148        case LOAD_DRIVER:
149            if (xf86getSubToken(&(ptr->mod_comment)) != STRING)
150                Error(QUOTE_MSG, "LoadDriver");
151            ptr->mod_load_lst =
152                xf86addNewLoadDirective(ptr->mod_load_lst, xf86_lex_val.str,
153                                        XF86_LOAD_DRIVER, NULL);
154            break;
155        case SUBSECTION:
156            if (xf86getSubToken(&(ptr->mod_comment)) != STRING)
157                Error(QUOTE_MSG, "SubSection");
158            ptr->mod_load_lst =
159                xf86parseModuleSubSection(ptr->mod_load_lst, xf86_lex_val.str);
160            break;
161        case EOF_TOKEN:
162            Error(UNEXPECTED_EOF_MSG);
163            break;
164        default:
165            Error(INVALID_KEYWORD_MSG, xf86tokenString());
166            break;
167        }
168    }
169
170#ifdef DEBUG
171    printf("Module section parsed\n");
172#endif
173
174    return ptr;
175}
176
177#undef CLEANUP
178
179void
180xf86printModuleSection(FILE * cf, XF86ConfModulePtr ptr)
181{
182    XF86LoadPtr lptr;
183
184    if (ptr == NULL)
185        return;
186
187    if (ptr->mod_comment)
188        fprintf(cf, "%s", ptr->mod_comment);
189    for (lptr = ptr->mod_load_lst; lptr; lptr = lptr->list.next) {
190        switch (lptr->load_type) {
191        case XF86_LOAD_MODULE:
192            if (lptr->load_opt == NULL) {
193                fprintf(cf, "\tLoad  \"%s\"", lptr->load_name);
194                if (lptr->load_comment)
195                    fprintf(cf, "%s", lptr->load_comment);
196                else
197                    fputc('\n', cf);
198            }
199            else {
200                fprintf(cf, "\tSubSection \"%s\"\n", lptr->load_name);
201                if (lptr->load_comment)
202                    fprintf(cf, "%s", lptr->load_comment);
203                xf86printOptionList(cf, lptr->load_opt, 2);
204                fprintf(cf, "\tEndSubSection\n");
205            }
206            break;
207        case XF86_LOAD_DRIVER:
208            fprintf(cf, "\tLoadDriver  \"%s\"", lptr->load_name);
209            if (lptr->load_comment)
210                fprintf(cf, "%s", lptr->load_comment);
211            else
212                fputc('\n', cf);
213            break;
214#if 0
215        default:
216            fprintf(cf, "#\tUnknown type  \"%s\"\n", lptr->load_name);
217            break;
218#endif
219        }
220    }
221}
222
223XF86LoadPtr
224xf86addNewLoadDirective(XF86LoadPtr head, const char *name, int type,
225                        XF86OptionPtr opts)
226{
227    XF86LoadPtr new;
228    int token;
229
230    new = calloc(1, sizeof(XF86LoadRec));
231    new->load_name = name;
232    new->load_type = type;
233    new->load_opt = opts;
234    new->ignore = 0;
235    new->list.next = NULL;
236
237    if ((token = xf86getToken(NULL)) == COMMENT) {
238        new->load_comment = xf86addComment(new->load_comment, xf86_lex_val.str);
239        free(xf86_lex_val.str);
240        xf86_lex_val.str = NULL;
241    } else {
242        xf86unGetToken(token);
243    }
244
245    return ((XF86LoadPtr) xf86addListItem((glp) head, (glp) new));
246}
247
248void
249xf86freeModules(XF86ConfModulePtr ptr)
250{
251    XF86LoadPtr lptr;
252    XF86LoadPtr prev;
253
254    if (ptr == NULL)
255        return;
256    lptr = ptr->mod_load_lst;
257    while (lptr) {
258        TestFree(lptr->load_name);
259        TestFree(lptr->load_comment);
260        prev = lptr;
261        lptr = lptr->list.next;
262        free(prev);
263    }
264    lptr = ptr->mod_disable_lst;
265    while (lptr) {
266        TestFree(lptr->load_name);
267        TestFree(lptr->load_comment);
268        prev = lptr;
269        lptr = lptr->list.next;
270        free(prev);
271    }
272    TestFree(ptr->mod_comment);
273    free(ptr);
274}
275