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
35static 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
45static void
46xf86freeOutputClassList(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
78static void
79add_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
90XF86ConfOutputClassPtr
91xf86parseOutputClassSection(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            free(xf86_lex_val.str);
106            xf86_lex_val.str = NULL;
107            break;
108        case IDENTIFIER:
109            if (xf86getSubToken(&(ptr->comment)) != STRING)
110                Error(QUOTE_MSG, "Identifier");
111            if (has_ident == TRUE)
112                Error(MULTIPLE_MSG, "Identifier");
113            ptr->identifier = xf86_lex_val.str;
114            has_ident = TRUE;
115            break;
116        case DRIVER:
117            if (xf86getSubToken(&(ptr->comment)) != STRING)
118                Error(QUOTE_MSG, "Driver");
119            else
120                ptr->driver = xf86_lex_val.str;
121            break;
122        case MODULEPATH:
123            if (xf86getSubToken(&(ptr->comment)) != STRING)
124                Error(QUOTE_MSG, "ModulePath");
125            if (ptr->modulepath) {
126                char *path;
127                XNFasprintf(&path, "%s,%s", ptr->modulepath, xf86_lex_val.str);
128                free(xf86_lex_val.str);
129                free(ptr->modulepath);
130                ptr->modulepath = path;
131            } else {
132                ptr->modulepath = xf86_lex_val.str;
133            }
134            break;
135        case OPTION:
136            ptr->option_lst = xf86parseOption(ptr->option_lst);
137            break;
138        case MATCH_DRIVER:
139            if (xf86getSubToken(&(ptr->comment)) != STRING)
140                Error(QUOTE_MSG, "MatchDriver");
141            add_group_entry(&ptr->match_driver,
142                            xstrtokenize(xf86_lex_val.str, TOKEN_SEP));
143            free(xf86_lex_val.str);
144            break;
145        case EOF_TOKEN:
146            Error(UNEXPECTED_EOF_MSG);
147            break;
148        default:
149            Error(INVALID_KEYWORD_MSG, xf86tokenString());
150            break;
151        }
152    }
153
154    if (!has_ident)
155        Error(NO_IDENT_MSG);
156
157#ifdef DEBUG
158    printf("OutputClass section parsed\n");
159#endif
160
161    return ptr;
162}
163void
164xf86printOutputClassSection(FILE * cf, XF86ConfOutputClassPtr ptr)
165{
166    const xf86MatchGroup *group;
167    char *const *cur;
168
169    while (ptr) {
170        fprintf(cf, "Section \"OutputClass\"\n");
171        if (ptr->comment)
172            fprintf(cf, "%s", ptr->comment);
173        if (ptr->identifier)
174            fprintf(cf, "\tIdentifier      \"%s\"\n", ptr->identifier);
175        if (ptr->driver)
176            fprintf(cf, "\tDriver          \"%s\"\n", ptr->driver);
177
178        xorg_list_for_each_entry(group, &ptr->match_driver, entry) {
179            fprintf(cf, "\tMatchDriver     \"");
180            for (cur = group->values; *cur; cur++)
181                fprintf(cf, "%s%s", cur == group->values ? "" : TOKEN_SEP,
182                        *cur);
183            fprintf(cf, "\"\n");
184        }
185
186        fprintf(cf, "EndSection\n\n");
187        ptr = ptr->list.next;
188    }
189}
190