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 
64 static const xf86ConfigSymTabRec VendorSubTab[] = {
65     {ENDSUBSECTION, "endsubsection"},
66     {IDENTIFIER, "identifier"},
67     {OPTION, "option"},
68     {-1, ""},
69 };
70 
71 static void
xf86freeVendorSubList(XF86ConfVendSubPtr ptr)72 xf86freeVendorSubList(XF86ConfVendSubPtr ptr)
73 {
74     XF86ConfVendSubPtr prev;
75 
76     while (ptr) {
77         TestFree(ptr->vs_identifier);
78         TestFree(ptr->vs_name);
79         TestFree(ptr->vs_comment);
80         xf86optionListFree(ptr->vs_option_lst);
81         prev = ptr;
82         ptr = ptr->list.next;
83         free(prev);
84     }
85 }
86 
87 #define CLEANUP xf86freeVendorSubList
88 
89 static XF86ConfVendSubPtr
xf86parseVendorSubSection(void)90 xf86parseVendorSubSection(void)
91 {
92     int has_ident = FALSE;
93     int token;
94 
95     parsePrologue(XF86ConfVendSubPtr, XF86ConfVendSubRec)
96 
97         while ((token = xf86getToken(VendorSubTab)) != ENDSUBSECTION) {
98         switch (token) {
99         case COMMENT:
100             ptr->vs_comment = xf86addComment(ptr->vs_comment, xf86_lex_val.str);
101             break;
102         case IDENTIFIER:
103             if (xf86getSubToken(&(ptr->vs_comment)))
104                 Error(QUOTE_MSG, "Identifier");
105             if (has_ident == TRUE)
106                 Error(MULTIPLE_MSG, "Identifier");
107             ptr->vs_identifier = xf86_lex_val.str;
108             has_ident = TRUE;
109             break;
110         case OPTION:
111             ptr->vs_option_lst = xf86parseOption(ptr->vs_option_lst);
112             break;
113 
114         case EOF_TOKEN:
115             Error(UNEXPECTED_EOF_MSG);
116             break;
117         default:
118             Error(INVALID_KEYWORD_MSG, xf86tokenString());
119             break;
120         }
121     }
122 
123 #ifdef DEBUG
124     printf("Vendor subsection parsed\n");
125 #endif
126 
127     return ptr;
128 }
129 
130 #undef CLEANUP
131 
132 static const xf86ConfigSymTabRec VendorTab[] = {
133     {ENDSECTION, "endsection"},
134     {IDENTIFIER, "identifier"},
135     {OPTION, "option"},
136     {SUBSECTION, "subsection"},
137     {-1, ""},
138 };
139 
140 #define CLEANUP xf86freeVendorList
141 
142 XF86ConfVendorPtr
xf86parseVendorSection(void)143 xf86parseVendorSection(void)
144 {
145     int has_ident = FALSE;
146     int token;
147 
148     parsePrologue(XF86ConfVendorPtr, XF86ConfVendorRec)
149 
150         while ((token = xf86getToken(VendorTab)) != ENDSECTION) {
151         switch (token) {
152         case COMMENT:
153             ptr->vnd_comment = xf86addComment(ptr->vnd_comment, xf86_lex_val.str);
154             break;
155         case IDENTIFIER:
156             if (xf86getSubToken(&(ptr->vnd_comment)) != STRING)
157                 Error(QUOTE_MSG, "Identifier");
158             if (has_ident == TRUE)
159                 Error(MULTIPLE_MSG, "Identifier");
160             ptr->vnd_identifier = xf86_lex_val.str;
161             has_ident = TRUE;
162             break;
163         case OPTION:
164             ptr->vnd_option_lst = xf86parseOption(ptr->vnd_option_lst);
165             break;
166         case SUBSECTION:
167             if (xf86getSubToken(&(ptr->vnd_comment)) != STRING)
168                 Error(QUOTE_MSG, "SubSection");
169             {
170                 HANDLE_LIST(vnd_sub_lst, xf86parseVendorSubSection,
171                             XF86ConfVendSubPtr);
172             }
173             break;
174         case EOF_TOKEN:
175             Error(UNEXPECTED_EOF_MSG);
176             break;
177         default:
178             Error(INVALID_KEYWORD_MSG, xf86tokenString());
179             break;
180         }
181 
182     }
183 
184     if (!has_ident)
185         Error(NO_IDENT_MSG);
186 
187 #ifdef DEBUG
188     printf("Vendor section parsed\n");
189 #endif
190 
191     return ptr;
192 }
193 
194 #undef CLEANUP
195 
196 void
xf86printVendorSection(FILE * cf,XF86ConfVendorPtr ptr)197 xf86printVendorSection(FILE * cf, XF86ConfVendorPtr ptr)
198 {
199     XF86ConfVendSubPtr pptr;
200 
201     while (ptr) {
202         fprintf(cf, "Section \"Vendor\"\n");
203         if (ptr->vnd_comment)
204             fprintf(cf, "%s", ptr->vnd_comment);
205         if (ptr->vnd_identifier)
206             fprintf(cf, "\tIdentifier     \"%s\"\n", ptr->vnd_identifier);
207 
208         xf86printOptionList(cf, ptr->vnd_option_lst, 1);
209         for (pptr = ptr->vnd_sub_lst; pptr; pptr = pptr->list.next) {
210             fprintf(cf, "\tSubSection \"Vendor\"\n");
211             if (pptr->vs_comment)
212                 fprintf(cf, "%s", pptr->vs_comment);
213             if (pptr->vs_identifier)
214                 fprintf(cf, "\t\tIdentifier \"%s\"\n", pptr->vs_identifier);
215             xf86printOptionList(cf, pptr->vs_option_lst, 2);
216             fprintf(cf, "\tEndSubSection\n");
217         }
218         fprintf(cf, "EndSection\n\n");
219         ptr = ptr->list.next;
220     }
221 }
222 
223 void
xf86freeVendorList(XF86ConfVendorPtr p)224 xf86freeVendorList(XF86ConfVendorPtr p)
225 {
226     if (p == NULL)
227         return;
228     xf86freeVendorSubList(p->vnd_sub_lst);
229     TestFree(p->vnd_identifier);
230     TestFree(p->vnd_comment);
231     xf86optionListFree(p->vnd_option_lst);
232     free(p);
233 }
234