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