Files.c revision 4642e01f
1/*
2 * Copyright (c) 1997  Metro Link Incorporated
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 * Except as contained in this notice, the name of the Metro Link shall not be
23 * used in advertising or otherwise to promote the sale, use or other dealings
24 * in this Software without prior written authorization from Metro Link.
25 *
26 */
27/*
28 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
29 *
30 * Permission is hereby granted, free of charge, to any person obtaining a
31 * copy of this software and associated documentation files (the "Software"),
32 * to deal in the Software without restriction, including without limitation
33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34 * and/or sell copies of the Software, and to permit persons to whom the
35 * Software is furnished to do so, subject to the following conditions:
36 *
37 * The above copyright notice and this permission notice shall be included in
38 * all copies or substantial portions of the Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
43 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
44 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
45 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
46 * OTHER DEALINGS IN THE SOFTWARE.
47 *
48 * Except as contained in this notice, the name of the copyright holder(s)
49 * and author(s) shall not be used in advertising or otherwise to promote
50 * the sale, use or other dealings in this Software without prior written
51 * authorization from the copyright holder(s) and author(s).
52 */
53
54
55/* View/edit this file with tab stops set to 4 */
56
57#ifdef HAVE_XORG_CONFIG_H
58#include <xorg-config.h>
59#endif
60
61#include <X11/Xos.h>
62#include "xf86Parser.h"
63#include "xf86tokens.h"
64#include "Configint.h"
65
66extern LexRec val;
67
68static xf86ConfigSymTabRec FilesTab[] =
69{
70	{ENDSECTION, "endsection"},
71	{FONTPATH, "fontpath"},
72	{MODULEPATH, "modulepath"},
73	{INPUTDEVICES, "inputdevices"},
74	{LOGFILEPATH, "logfile"},
75	{XKBDIR, "xkbdir"},
76	/* Obsolete keywords that aren't used but shouldn't cause errors: */
77	{OBSOLETE_TOKEN, "rgbpath"},
78	{-1, ""},
79};
80
81#define CLEANUP xf86freeFiles
82
83XF86ConfFilesPtr
84xf86parseFilesSection (void)
85{
86	int i, j;
87	int k, l;
88	char *str;
89	int token;
90	parsePrologue (XF86ConfFilesPtr, XF86ConfFilesRec)
91
92	while ((token = xf86getToken (FilesTab)) != ENDSECTION)
93	{
94		switch (token)
95		{
96		case COMMENT:
97			ptr->file_comment = xf86addComment(ptr->file_comment, val.str);
98			break;
99		case FONTPATH:
100			if (xf86getSubToken (&(ptr->file_comment)) != STRING)
101				Error (QUOTE_MSG, "FontPath");
102			j = FALSE;
103			str = val.str;
104			if (ptr->file_fontpath == NULL)
105			{
106				ptr->file_fontpath = xf86confmalloc (1);
107				ptr->file_fontpath[0] = '\0';
108				i = strlen (str) + 1;
109			}
110			else
111			{
112				i = strlen (ptr->file_fontpath) + strlen (str) + 1;
113				if (ptr->file_fontpath[strlen (ptr->file_fontpath) - 1] != ',')
114				{
115					i++;
116					j = TRUE;
117				}
118			}
119			ptr->file_fontpath =
120				xf86confrealloc (ptr->file_fontpath, i);
121			if (j)
122				strcat (ptr->file_fontpath, ",");
123
124			strcat (ptr->file_fontpath, str);
125			xf86conffree (val.str);
126			break;
127		case MODULEPATH:
128			if (xf86getSubToken (&(ptr->file_comment)) != STRING)
129				Error (QUOTE_MSG, "ModulePath");
130			l = FALSE;
131			str = val.str;
132			if (ptr->file_modulepath == NULL)
133			{
134				ptr->file_modulepath = xf86confmalloc (1);
135				ptr->file_modulepath[0] = '\0';
136				k = strlen (str) + 1;
137			}
138			else
139			{
140				k = strlen (ptr->file_modulepath) + strlen (str) + 1;
141				if (ptr->file_modulepath[strlen (ptr->file_modulepath) - 1] != ',')
142				{
143					k++;
144					l = TRUE;
145				}
146			}
147			ptr->file_modulepath = xf86confrealloc (ptr->file_modulepath, k);
148			if (l)
149				strcat (ptr->file_modulepath, ",");
150
151			strcat (ptr->file_modulepath, str);
152			xf86conffree (val.str);
153			break;
154		case INPUTDEVICES:
155			if (xf86getSubToken (&(ptr->file_comment)) != STRING)
156				Error (QUOTE_MSG, "InputDevices");
157			l = FALSE;
158			str = val.str;
159			if (ptr->file_inputdevs == NULL)
160			{
161				ptr->file_inputdevs = xf86confmalloc (1);
162				ptr->file_inputdevs[0] = '\0';
163				k = strlen (str) + 1;
164			}
165			else
166			{
167				k = strlen (ptr->file_inputdevs) + strlen (str) + 1;
168				if (ptr->file_inputdevs[strlen (ptr->file_inputdevs) - 1] != ',')
169				{
170					k++;
171					l = TRUE;
172				}
173			}
174			ptr->file_inputdevs = xf86confrealloc (ptr->file_inputdevs, k);
175			if (l)
176				strcat (ptr->file_inputdevs, ",");
177
178			strcat (ptr->file_inputdevs, str);
179			xf86conffree (val.str);
180			break;
181		case LOGFILEPATH:
182			if (xf86getSubToken (&(ptr->file_comment)) != STRING)
183				Error (QUOTE_MSG, "LogFile");
184			ptr->file_logfile = val.str;
185			break;
186		case XKBDIR:
187			if (xf86getSubToken (&(ptr->file_xkbdir)) != STRING)
188				Error (QUOTE_MSG, "XkbDir");
189			ptr->file_xkbdir = val.str;
190			break;
191		case EOF_TOKEN:
192			Error (UNEXPECTED_EOF_MSG, NULL);
193			break;
194		case OBSOLETE_TOKEN:
195			xf86parseError (OBSOLETE_MSG, xf86tokenString ());
196			xf86getSubToken (&(ptr->file_comment));
197			break;
198		default:
199			Error (INVALID_KEYWORD_MSG, xf86tokenString ());
200			break;
201		}
202	}
203
204#ifdef DEBUG
205	printf ("File section parsed\n");
206#endif
207
208	return ptr;
209}
210
211#undef CLEANUP
212
213void
214xf86printFileSection (FILE * cf, XF86ConfFilesPtr ptr)
215{
216	char *p, *s;
217
218	if (ptr == NULL)
219		return;
220
221	if (ptr->file_comment)
222		fprintf (cf, "%s", ptr->file_comment);
223	if (ptr->file_logfile)
224		fprintf (cf, "\tLogFile      \"%s\"\n", ptr->file_logfile);
225	if (ptr->file_modulepath)
226	{
227		s = ptr->file_modulepath;
228		p = index (s, ',');
229		while (p)
230		{
231			*p = '\000';
232			fprintf (cf, "\tModulePath   \"%s\"\n", s);
233			*p = ',';
234			s = p;
235			s++;
236			p = index (s, ',');
237		}
238		fprintf (cf, "\tModulePath   \"%s\"\n", s);
239	}
240	if (ptr->file_inputdevs)
241	{
242		s = ptr->file_inputdevs;
243		p = index (s, ',');
244		while (p)
245		{
246			*p = '\000';
247			fprintf (cf, "\tInputDevices   \"%s\"\n", s);
248			*p = ',';
249			s = p;
250			s++;
251			p = index (s, ',');
252		}
253		fprintf (cf, "\tInputDevices   \"%s\"\n", s);
254	}
255	if (ptr->file_fontpath)
256	{
257		s = ptr->file_fontpath;
258		p = index (s, ',');
259		while (p)
260		{
261			*p = '\000';
262			fprintf (cf, "\tFontPath     \"%s\"\n", s);
263			*p = ',';
264			s = p;
265			s++;
266			p = index (s, ',');
267		}
268		fprintf (cf, "\tFontPath     \"%s\"\n", s);
269	}
270	if (ptr->file_xkbdir)
271		fprintf (cf, "\tXkbDir		\"%s\"\n", ptr->file_xkbdir);
272}
273
274void
275xf86freeFiles (XF86ConfFilesPtr p)
276{
277	if (p == NULL)
278		return;
279
280	TestFree (p->file_logfile);
281	TestFree (p->file_modulepath);
282	TestFree (p->file_inputdevs);
283	TestFree (p->file_fontpath);
284	TestFree (p->file_comment);
285	TestFree (p->file_xkbdir);
286
287	xf86conffree (p);
288}
289