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 "os.h"
60 #include "xf86Parser.h"
61 #include "xf86tokens.h"
62 #include "Configint.h"
63 
64 
65 static const xf86ConfigSymTabRec InputTab[] = {
66     {ENDSECTION, "endsection"},
67     {IDENTIFIER, "identifier"},
68     {OPTION, "option"},
69     {DRIVER, "driver"},
70     {-1, ""},
71 };
72 
73 #define CLEANUP xf86freeInputList
74 
75 XF86ConfInputPtr
xf86parseInputSection(void)76 xf86parseInputSection(void)
77 {
78     int has_ident = FALSE;
79     int token;
80 
81     parsePrologue(XF86ConfInputPtr, XF86ConfInputRec)
82 
83         while ((token = xf86getToken(InputTab)) != ENDSECTION) {
84         switch (token) {
85         case COMMENT:
86             ptr->inp_comment = xf86addComment(ptr->inp_comment, xf86_lex_val.str);
87             break;
88         case IDENTIFIER:
89             if (xf86getSubToken(&(ptr->inp_comment)) != STRING)
90                 Error(QUOTE_MSG, "Identifier");
91             if (has_ident == TRUE)
92                 Error(MULTIPLE_MSG, "Identifier");
93             ptr->inp_identifier = xf86_lex_val.str;
94             has_ident = TRUE;
95             break;
96         case DRIVER:
97             if (xf86getSubToken(&(ptr->inp_comment)) != STRING)
98                 Error(QUOTE_MSG, "Driver");
99             if (strcmp(xf86_lex_val.str, "keyboard") == 0) {
100                 ptr->inp_driver = strdup("kbd");
101                 free(xf86_lex_val.str);
102             }
103             else
104                 ptr->inp_driver = xf86_lex_val.str;
105             break;
106         case OPTION:
107             ptr->inp_option_lst = xf86parseOption(ptr->inp_option_lst);
108             break;
109         case EOF_TOKEN:
110             Error(UNEXPECTED_EOF_MSG);
111             break;
112         default:
113             Error(INVALID_KEYWORD_MSG, xf86tokenString());
114             break;
115         }
116     }
117 
118     if (!has_ident)
119         Error(NO_IDENT_MSG);
120 
121 #ifdef DEBUG
122     printf("InputDevice section parsed\n");
123 #endif
124 
125     return ptr;
126 }
127 
128 #undef CLEANUP
129 
130 void
xf86printInputSection(FILE * cf,XF86ConfInputPtr ptr)131 xf86printInputSection(FILE * cf, XF86ConfInputPtr ptr)
132 {
133     while (ptr) {
134         fprintf(cf, "Section \"InputDevice\"\n");
135         if (ptr->inp_comment)
136             fprintf(cf, "%s", ptr->inp_comment);
137         if (ptr->inp_identifier)
138             fprintf(cf, "\tIdentifier  \"%s\"\n", ptr->inp_identifier);
139         if (ptr->inp_driver)
140             fprintf(cf, "\tDriver      \"%s\"\n", ptr->inp_driver);
141         xf86printOptionList(cf, ptr->inp_option_lst, 1);
142         fprintf(cf, "EndSection\n\n");
143         ptr = ptr->list.next;
144     }
145 }
146 
147 void
xf86freeInputList(XF86ConfInputPtr ptr)148 xf86freeInputList(XF86ConfInputPtr ptr)
149 {
150     XF86ConfInputPtr prev;
151 
152     while (ptr) {
153         TestFree(ptr->inp_identifier);
154         TestFree(ptr->inp_driver);
155         TestFree(ptr->inp_comment);
156         xf86optionListFree(ptr->inp_option_lst);
157 
158         prev = ptr;
159         ptr = ptr->list.next;
160         free(prev);
161     }
162 }
163 
164 int
xf86validateInput(XF86ConfigPtr p)165 xf86validateInput(XF86ConfigPtr p)
166 {
167     XF86ConfInputPtr input = p->conf_input_lst;
168 
169     while (input) {
170         if (!input->inp_driver) {
171             xf86validationError(UNDEFINED_INPUTDRIVER_MSG,
172                                 input->inp_identifier);
173             return FALSE;
174         }
175         input = input->list.next;
176     }
177     return TRUE;
178 }
179 
180 XF86ConfInputPtr
xf86findInput(const char * ident,XF86ConfInputPtr p)181 xf86findInput(const char *ident, XF86ConfInputPtr p)
182 {
183     while (p) {
184         if (xf86nameCompare(ident, p->inp_identifier) == 0)
185             return p;
186 
187         p = p->list.next;
188     }
189     return NULL;
190 }
191 
192 XF86ConfInputPtr
xf86findInputByDriver(const char * driver,XF86ConfInputPtr p)193 xf86findInputByDriver(const char *driver, XF86ConfInputPtr p)
194 {
195     while (p) {
196         if (xf86nameCompare(driver, p->inp_driver) == 0)
197             return p;
198 
199         p = p->list.next;
200     }
201     return NULL;
202 }
203