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 #ifdef HAVE_XORG_CONFIG_H
55 #include <xorg-config.h>
56 #endif
57 
58 #include <X11/Xos.h>
59 #include "xf86Parser.h"
60 #include "xf86tokens.h"
61 #include "Configint.h"
62 
63 
64 static const xf86ConfigSymTabRec FilesTab[] = {
65     {ENDSECTION, "endsection"},
66     {FONTPATH, "fontpath"},
67     {MODULEPATH, "modulepath"},
68     {LOGFILEPATH, "logfile"},
69     {XKBDIR, "xkbdir"},
70     /* Obsolete keywords that aren't used but shouldn't cause errors: */
71     {OBSOLETE_TOKEN, "rgbpath"},
72     {OBSOLETE_TOKEN, "inputdevices"},
73     {-1, ""},
74 };
75 
76 #define CLEANUP xf86freeFiles
77 
78 XF86ConfFilesPtr
xf86parseFilesSection(void)79 xf86parseFilesSection(void)
80 {
81     int i, j;
82     int k, l;
83     char *str;
84     int token;
85 
86     parsePrologue(XF86ConfFilesPtr, XF86ConfFilesRec)
87 
88         while ((token = xf86getToken(FilesTab)) != ENDSECTION) {
89         switch (token) {
90         case COMMENT:
91             ptr->file_comment = xf86addComment(ptr->file_comment, xf86_lex_val.str);
92             break;
93         case FONTPATH:
94             if (xf86getSubToken(&(ptr->file_comment)) != STRING)
95                 Error(QUOTE_MSG, "FontPath");
96             j = FALSE;
97             str = xf86_lex_val.str;
98             if (ptr->file_fontpath == NULL) {
99                 ptr->file_fontpath = calloc(1, 1);
100                 i = strlen(str) + 1;
101             }
102             else {
103                 i = strlen(ptr->file_fontpath) + strlen(str) + 1;
104                 if (ptr->file_fontpath[strlen(ptr->file_fontpath) - 1] != ',') {
105                     i++;
106                     j = TRUE;
107                 }
108             }
109             ptr->file_fontpath = realloc(ptr->file_fontpath, i);
110             if (j)
111                 strcat(ptr->file_fontpath, ",");
112 
113             strcat(ptr->file_fontpath, str);
114             free(xf86_lex_val.str);
115             break;
116         case MODULEPATH:
117             if (xf86getSubToken(&(ptr->file_comment)) != STRING)
118                 Error(QUOTE_MSG, "ModulePath");
119             l = FALSE;
120             str = xf86_lex_val.str;
121             if (ptr->file_modulepath == NULL) {
122                 ptr->file_modulepath = malloc(1);
123                 ptr->file_modulepath[0] = '\0';
124                 k = strlen(str) + 1;
125             }
126             else {
127                 k = strlen(ptr->file_modulepath) + strlen(str) + 1;
128                 if (ptr->file_modulepath[strlen(ptr->file_modulepath) - 1] !=
129                     ',') {
130                     k++;
131                     l = TRUE;
132                 }
133             }
134             ptr->file_modulepath = realloc(ptr->file_modulepath, k);
135             if (l)
136                 strcat(ptr->file_modulepath, ",");
137 
138             strcat(ptr->file_modulepath, str);
139             free(xf86_lex_val.str);
140             break;
141         case LOGFILEPATH:
142             if (xf86getSubToken(&(ptr->file_comment)) != STRING)
143                 Error(QUOTE_MSG, "LogFile");
144             ptr->file_logfile = xf86_lex_val.str;
145             break;
146         case XKBDIR:
147             if (xf86getSubToken(&(ptr->file_xkbdir)) != STRING)
148                 Error(QUOTE_MSG, "XkbDir");
149             ptr->file_xkbdir = xf86_lex_val.str;
150             break;
151         case EOF_TOKEN:
152             Error(UNEXPECTED_EOF_MSG);
153             break;
154         case OBSOLETE_TOKEN:
155             xf86parseError(OBSOLETE_MSG, xf86tokenString());
156             xf86getSubToken(&(ptr->file_comment));
157             break;
158         default:
159             Error(INVALID_KEYWORD_MSG, xf86tokenString());
160             break;
161         }
162     }
163 
164 #ifdef DEBUG
165     printf("File section parsed\n");
166 #endif
167 
168     return ptr;
169 }
170 
171 #undef CLEANUP
172 
173 void
xf86printFileSection(FILE * cf,XF86ConfFilesPtr ptr)174 xf86printFileSection(FILE * cf, XF86ConfFilesPtr ptr)
175 {
176     char *p, *s;
177 
178     if (ptr == NULL)
179         return;
180 
181     if (ptr->file_comment)
182         fprintf(cf, "%s", ptr->file_comment);
183     if (ptr->file_logfile)
184         fprintf(cf, "\tLogFile      \"%s\"\n", ptr->file_logfile);
185     if (ptr->file_modulepath) {
186         s = ptr->file_modulepath;
187         p = index(s, ',');
188         while (p) {
189             *p = '\000';
190             fprintf(cf, "\tModulePath   \"%s\"\n", s);
191             *p = ',';
192             s = p;
193             s++;
194             p = index(s, ',');
195         }
196         fprintf(cf, "\tModulePath   \"%s\"\n", s);
197     }
198     if (ptr->file_fontpath) {
199         s = ptr->file_fontpath;
200         p = index(s, ',');
201         while (p) {
202             *p = '\000';
203             fprintf(cf, "\tFontPath     \"%s\"\n", s);
204             *p = ',';
205             s = p;
206             s++;
207             p = index(s, ',');
208         }
209         fprintf(cf, "\tFontPath     \"%s\"\n", s);
210     }
211     if (ptr->file_xkbdir)
212         fprintf(cf, "\tXkbDir		\"%s\"\n", ptr->file_xkbdir);
213 }
214 
215 void
xf86freeFiles(XF86ConfFilesPtr p)216 xf86freeFiles(XF86ConfFilesPtr p)
217 {
218     if (p == NULL)
219         return;
220 
221     TestFree(p->file_logfile);
222     TestFree(p->file_modulepath);
223     TestFree(p->file_fontpath);
224     TestFree(p->file_comment);
225     TestFree(p->file_xkbdir);
226 
227     free(p);
228 }
229