1 /*
2  * Copyright (c) 2014 NVIDIA Corporation. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use,
8  * copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following
11  * conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifdef HAVE_XORG_CONFIG_H
27 #include <xorg-config.h>
28 #endif
29 
30 #include "os.h"
31 #include "xf86Parser.h"
32 #include "xf86tokens.h"
33 #include "Configint.h"
34 
35 static const xf86ConfigSymTabRec OutputClassTab[] = {
36     {ENDSECTION, "endsection"},
37     {IDENTIFIER, "identifier"},
38     {DRIVER, "driver"},
39     {MODULEPATH, "modulepath"},
40     {OPTION, "option"},
41     {MATCH_DRIVER, "matchdriver"},
42     {-1, ""},
43 };
44 
45 static void
xf86freeOutputClassList(XF86ConfOutputClassPtr ptr)46 xf86freeOutputClassList(XF86ConfOutputClassPtr ptr)
47 {
48     XF86ConfOutputClassPtr prev;
49 
50     while (ptr) {
51         xf86MatchGroup *group, *next;
52         char **list;
53 
54         TestFree(ptr->identifier);
55         TestFree(ptr->comment);
56         TestFree(ptr->driver);
57         TestFree(ptr->modulepath);
58 
59         xorg_list_for_each_entry_safe(group, next, &ptr->match_driver, entry) {
60             xorg_list_del(&group->entry);
61             for (list = group->values; *list; list++)
62                 free(*list);
63             free(group);
64         }
65 
66         xf86optionListFree(ptr->option_lst);
67 
68         prev = ptr;
69         ptr = ptr->list.next;
70         free(prev);
71     }
72 }
73 
74 #define CLEANUP xf86freeOutputClassList
75 
76 #define TOKEN_SEP "|"
77 
78 static void
add_group_entry(struct xorg_list * head,char ** values)79 add_group_entry(struct xorg_list *head, char **values)
80 {
81     xf86MatchGroup *group;
82 
83     group = malloc(sizeof(*group));
84     if (group) {
85         group->values = values;
86         xorg_list_add(&group->entry, head);
87     }
88 }
89 
90 XF86ConfOutputClassPtr
xf86parseOutputClassSection(void)91 xf86parseOutputClassSection(void)
92 {
93     int has_ident = FALSE;
94     int token;
95 
96     parsePrologue(XF86ConfOutputClassPtr, XF86ConfOutputClassRec)
97 
98     /* Initialize MatchGroup lists */
99     xorg_list_init(&ptr->match_driver);
100 
101     while ((token = xf86getToken(OutputClassTab)) != ENDSECTION) {
102         switch (token) {
103         case COMMENT:
104             ptr->comment = xf86addComment(ptr->comment, xf86_lex_val.str);
105             break;
106         case IDENTIFIER:
107             if (xf86getSubToken(&(ptr->comment)) != STRING)
108                 Error(QUOTE_MSG, "Identifier");
109             if (has_ident == TRUE)
110                 Error(MULTIPLE_MSG, "Identifier");
111             ptr->identifier = xf86_lex_val.str;
112             has_ident = TRUE;
113             break;
114         case DRIVER:
115             if (xf86getSubToken(&(ptr->comment)) != STRING)
116                 Error(QUOTE_MSG, "Driver");
117             else
118                 ptr->driver = xf86_lex_val.str;
119             break;
120         case MODULEPATH:
121             if (xf86getSubToken(&(ptr->comment)) != STRING)
122                 Error(QUOTE_MSG, "ModulePath");
123             if (ptr->modulepath) {
124                 char *path;
125                 XNFasprintf(&path, "%s,%s", ptr->modulepath, xf86_lex_val.str);
126                 free(xf86_lex_val.str);
127                 free(ptr->modulepath);
128                 ptr->modulepath = path;
129             } else {
130                 ptr->modulepath = xf86_lex_val.str;
131             }
132             break;
133         case OPTION:
134             ptr->option_lst = xf86parseOption(ptr->option_lst);
135             break;
136         case MATCH_DRIVER:
137             if (xf86getSubToken(&(ptr->comment)) != STRING)
138                 Error(QUOTE_MSG, "MatchDriver");
139             add_group_entry(&ptr->match_driver,
140                             xstrtokenize(xf86_lex_val.str, TOKEN_SEP));
141             free(xf86_lex_val.str);
142             break;
143         case EOF_TOKEN:
144             Error(UNEXPECTED_EOF_MSG);
145             break;
146         default:
147             Error(INVALID_KEYWORD_MSG, xf86tokenString());
148             break;
149         }
150     }
151 
152     if (!has_ident)
153         Error(NO_IDENT_MSG);
154 
155 #ifdef DEBUG
156     printf("OutputClass section parsed\n");
157 #endif
158 
159     return ptr;
160 }
161 void
xf86printOutputClassSection(FILE * cf,XF86ConfOutputClassPtr ptr)162 xf86printOutputClassSection(FILE * cf, XF86ConfOutputClassPtr ptr)
163 {
164     const xf86MatchGroup *group;
165     char *const *cur;
166 
167     while (ptr) {
168         fprintf(cf, "Section \"OutputClass\"\n");
169         if (ptr->comment)
170             fprintf(cf, "%s", ptr->comment);
171         if (ptr->identifier)
172             fprintf(cf, "\tIdentifier      \"%s\"\n", ptr->identifier);
173         if (ptr->driver)
174             fprintf(cf, "\tDriver          \"%s\"\n", ptr->driver);
175 
176         xorg_list_for_each_entry(group, &ptr->match_driver, entry) {
177             fprintf(cf, "\tMatchDriver     \"");
178             for (cur = group->values; *cur; cur++)
179                 fprintf(cf, "%s%s", cur == group->values ? "" : TOKEN_SEP,
180                         *cur);
181             fprintf(cf, "\"\n");
182         }
183 
184         fprintf(cf, "EndSection\n\n");
185         ptr = ptr->list.next;
186     }
187 }
188