Module.c (8623B)
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 SubModuleTab[] = { 65 {ENDSUBSECTION, "endsubsection"}, 66 {OPTION, "option"}, 67 {-1, ""}, 68 }; 69 70 static const xf86ConfigSymTabRec ModuleTab[] = { 71 {ENDSECTION, "endsection"}, 72 {LOAD, "load"}, 73 {DISABLE, "disable"}, 74 {LOAD_DRIVER, "loaddriver"}, 75 {SUBSECTION, "subsection"}, 76 {-1, ""}, 77 }; 78 79 #define CLEANUP xf86freeModules 80 81 static XF86LoadPtr 82 xf86parseModuleSubSection(XF86LoadPtr head, char *name) 83 { 84 int token; 85 86 parsePrologue(XF86LoadPtr, XF86LoadRec) 87 88 ptr->load_name = name; 89 ptr->load_type = XF86_LOAD_MODULE; 90 ptr->ignore = 0; 91 ptr->load_opt = NULL; 92 ptr->list.next = NULL; 93 94 while ((token = xf86getToken(SubModuleTab)) != ENDSUBSECTION) { 95 switch (token) { 96 case COMMENT: 97 ptr->load_comment = xf86addComment(ptr->load_comment, xf86_lex_val.str); 98 break; 99 case OPTION: 100 ptr->load_opt = xf86parseOption(ptr->load_opt); 101 break; 102 case EOF_TOKEN: 103 xf86parseError(UNEXPECTED_EOF_MSG); 104 free(ptr); 105 return NULL; 106 default: 107 xf86parseError(INVALID_KEYWORD_MSG, xf86tokenString()); 108 free(ptr); 109 return NULL; 110 break; 111 } 112 113 } 114 115 return ((XF86LoadPtr) xf86addListItem((glp) head, (glp) ptr)); 116 } 117 118 XF86ConfModulePtr 119 xf86parseModuleSection(void) 120 { 121 int token; 122 123 parsePrologue(XF86ConfModulePtr, XF86ConfModuleRec) 124 125 while ((token = xf86getToken(ModuleTab)) != ENDSECTION) { 126 switch (token) { 127 case COMMENT: 128 ptr->mod_comment = xf86addComment(ptr->mod_comment, xf86_lex_val.str); 129 break; 130 case LOAD: 131 if (xf86getSubToken(&(ptr->mod_comment)) != STRING) 132 Error(QUOTE_MSG, "Load"); 133 ptr->mod_load_lst = 134 xf86addNewLoadDirective(ptr->mod_load_lst, xf86_lex_val.str, 135 XF86_LOAD_MODULE, NULL); 136 break; 137 case DISABLE: 138 if (xf86getSubToken(&(ptr->mod_comment)) != STRING) 139 Error(QUOTE_MSG, "Disable"); 140 ptr->mod_disable_lst = 141 xf86addNewLoadDirective(ptr->mod_disable_lst, xf86_lex_val.str, 142 XF86_DISABLE_MODULE, NULL); 143 break; 144 case LOAD_DRIVER: 145 if (xf86getSubToken(&(ptr->mod_comment)) != STRING) 146 Error(QUOTE_MSG, "LoadDriver"); 147 ptr->mod_load_lst = 148 xf86addNewLoadDirective(ptr->mod_load_lst, xf86_lex_val.str, 149 XF86_LOAD_DRIVER, NULL); 150 break; 151 case SUBSECTION: 152 if (xf86getSubToken(&(ptr->mod_comment)) != STRING) 153 Error(QUOTE_MSG, "SubSection"); 154 ptr->mod_load_lst = 155 xf86parseModuleSubSection(ptr->mod_load_lst, xf86_lex_val.str); 156 break; 157 case EOF_TOKEN: 158 Error(UNEXPECTED_EOF_MSG); 159 break; 160 default: 161 Error(INVALID_KEYWORD_MSG, xf86tokenString()); 162 break; 163 } 164 } 165 166 #ifdef DEBUG 167 printf("Module section parsed\n"); 168 #endif 169 170 return ptr; 171 } 172 173 #undef CLEANUP 174 175 void 176 xf86printModuleSection(FILE * cf, XF86ConfModulePtr ptr) 177 { 178 XF86LoadPtr lptr; 179 180 if (ptr == NULL) 181 return; 182 183 if (ptr->mod_comment) 184 fprintf(cf, "%s", ptr->mod_comment); 185 for (lptr = ptr->mod_load_lst; lptr; lptr = lptr->list.next) { 186 switch (lptr->load_type) { 187 case XF86_LOAD_MODULE: 188 if (lptr->load_opt == NULL) { 189 fprintf(cf, "\tLoad \"%s\"", lptr->load_name); 190 if (lptr->load_comment) 191 fprintf(cf, "%s", lptr->load_comment); 192 else 193 fputc('\n', cf); 194 } 195 else { 196 fprintf(cf, "\tSubSection \"%s\"\n", lptr->load_name); 197 if (lptr->load_comment) 198 fprintf(cf, "%s", lptr->load_comment); 199 xf86printOptionList(cf, lptr->load_opt, 2); 200 fprintf(cf, "\tEndSubSection\n"); 201 } 202 break; 203 case XF86_LOAD_DRIVER: 204 fprintf(cf, "\tLoadDriver \"%s\"", lptr->load_name); 205 if (lptr->load_comment) 206 fprintf(cf, "%s", lptr->load_comment); 207 else 208 fputc('\n', cf); 209 break; 210 #if 0 211 default: 212 fprintf(cf, "#\tUnknown type \"%s\"\n", lptr->load_name); 213 break; 214 #endif 215 } 216 } 217 } 218 219 XF86LoadPtr 220 xf86addNewLoadDirective(XF86LoadPtr head, const char *name, int type, 221 XF86OptionPtr opts) 222 { 223 XF86LoadPtr new; 224 int token; 225 226 new = calloc(1, sizeof(XF86LoadRec)); 227 new->load_name = name; 228 new->load_type = type; 229 new->load_opt = opts; 230 new->ignore = 0; 231 new->list.next = NULL; 232 233 if ((token = xf86getToken(NULL)) == COMMENT) 234 new->load_comment = xf86addComment(new->load_comment, xf86_lex_val.str); 235 else 236 xf86unGetToken(token); 237 238 return ((XF86LoadPtr) xf86addListItem((glp) head, (glp) new)); 239 } 240 241 void 242 xf86freeModules(XF86ConfModulePtr ptr) 243 { 244 XF86LoadPtr lptr; 245 XF86LoadPtr prev; 246 247 if (ptr == NULL) 248 return; 249 lptr = ptr->mod_load_lst; 250 while (lptr) { 251 TestFree(lptr->load_name); 252 TestFree(lptr->load_comment); 253 prev = lptr; 254 lptr = lptr->list.next; 255 free(prev); 256 } 257 lptr = ptr->mod_disable_lst; 258 while (lptr) { 259 TestFree(lptr->load_name); 260 TestFree(lptr->load_comment); 261 prev = lptr; 262 lptr = lptr->list.next; 263 free(prev); 264 } 265 TestFree(ptr->mod_comment); 266 free(ptr); 267 }