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	{LOGFILEPATH, "logfile"},
74	{XKBDIR, "xkbdir"},
75	/* Obsolete keywords that aren't used but shouldn't cause errors: */
76	{OBSOLETE_TOKEN, "rgbpath"},
77	{OBSOLETE_TOKEN, "inputdevices"},
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 = malloc (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				realloc (ptr->file_fontpath, i);
121			if (j)
122				strcat (ptr->file_fontpath, ",");
123
124			strcat (ptr->file_fontpath, str);
125			free (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 = malloc (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 = realloc (ptr->file_modulepath, k);
148			if (l)
149				strcat (ptr->file_modulepath, ",");
150
151			strcat (ptr->file_modulepath, str);
152			free (val.str);
153			break;
154		case LOGFILEPATH:
155			if (xf86getSubToken (&(ptr->file_comment)) != STRING)
156				Error (QUOTE_MSG, "LogFile");
157			ptr->file_logfile = val.str;
158			break;
159		case XKBDIR:
160			if (xf86getSubToken (&(ptr->file_xkbdir)) != STRING)
161				Error (QUOTE_MSG, "XkbDir");
162			ptr->file_xkbdir = val.str;
163			break;
164		case EOF_TOKEN:
165			Error (UNEXPECTED_EOF_MSG, NULL);
166			break;
167		case OBSOLETE_TOKEN:
168			xf86parseError (OBSOLETE_MSG, xf86tokenString ());
169			xf86getSubToken (&(ptr->file_comment));
170			break;
171		default:
172			Error (INVALID_KEYWORD_MSG, xf86tokenString ());
173			break;
174		}
175	}
176
177#ifdef DEBUG
178	printf ("File section parsed\n");
179#endif
180
181	return ptr;
182}
183
184#undef CLEANUP
185
186void
187xf86printFileSection (FILE * cf, XF86ConfFilesPtr ptr)
188{
189	char *p, *s;
190
191	if (ptr == NULL)
192		return;
193
194	if (ptr->file_comment)
195		fprintf (cf, "%s", ptr->file_comment);
196	if (ptr->file_logfile)
197		fprintf (cf, "\tLogFile      \"%s\"\n", ptr->file_logfile);
198	if (ptr->file_modulepath)
199	{
200		s = ptr->file_modulepath;
201		p = index (s, ',');
202		while (p)
203		{
204			*p = '\000';
205			fprintf (cf, "\tModulePath   \"%s\"\n", s);
206			*p = ',';
207			s = p;
208			s++;
209			p = index (s, ',');
210		}
211		fprintf (cf, "\tModulePath   \"%s\"\n", s);
212	}
213	if (ptr->file_fontpath)
214	{
215		s = ptr->file_fontpath;
216		p = index (s, ',');
217		while (p)
218		{
219			*p = '\000';
220			fprintf (cf, "\tFontPath     \"%s\"\n", s);
221			*p = ',';
222			s = p;
223			s++;
224			p = index (s, ',');
225		}
226		fprintf (cf, "\tFontPath     \"%s\"\n", s);
227	}
228	if (ptr->file_xkbdir)
229		fprintf (cf, "\tXkbDir		\"%s\"\n", ptr->file_xkbdir);
230}
231
232void
233xf86freeFiles (XF86ConfFilesPtr p)
234{
235	if (p == NULL)
236		return;
237
238	TestFree (p->file_logfile);
239	TestFree (p->file_modulepath);
240	TestFree (p->file_fontpath);
241	TestFree (p->file_comment);
242	TestFree (p->file_xkbdir);
243
244	free (p);
245}
246