OutputClass.c revision 35c4bbdf
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 36xf86ConfigSymTabRec OutputClassTab[] = { 37 {ENDSECTION, "endsection"}, 38 {IDENTIFIER, "identifier"}, 39 {DRIVER, "driver"}, 40 {MATCH_DRIVER, "matchdriver"}, 41 {-1, ""}, 42}; 43 44static void 45xf86freeOutputClassList(XF86ConfOutputClassPtr ptr) 46{ 47 XF86ConfOutputClassPtr prev; 48 49 while (ptr) { 50 xf86MatchGroup *group, *next; 51 char **list; 52 53 TestFree(ptr->identifier); 54 TestFree(ptr->comment); 55 TestFree(ptr->driver); 56 57 xorg_list_for_each_entry_safe(group, next, &ptr->match_driver, entry) { 58 xorg_list_del(&group->entry); 59 for (list = group->values; *list; list++) 60 free(*list); 61 free(group); 62 } 63 64 prev = ptr; 65 ptr = ptr->list.next; 66 free(prev); 67 } 68} 69 70#define CLEANUP xf86freeOutputClassList 71 72#define TOKEN_SEP "|" 73 74static void 75add_group_entry(struct xorg_list *head, char **values) 76{ 77 xf86MatchGroup *group; 78 79 group = malloc(sizeof(*group)); 80 if (group) { 81 group->values = values; 82 xorg_list_add(&group->entry, head); 83 } 84} 85 86XF86ConfOutputClassPtr 87xf86parseOutputClassSection(void) 88{ 89 int has_ident = FALSE; 90 int token; 91 92 parsePrologue(XF86ConfOutputClassPtr, XF86ConfOutputClassRec) 93 94 /* Initialize MatchGroup lists */ 95 xorg_list_init(&ptr->match_driver); 96 97 while ((token = xf86getToken(OutputClassTab)) != ENDSECTION) { 98 switch (token) { 99 case COMMENT: 100 ptr->comment = xf86addComment(ptr->comment, xf86_lex_val.str); 101 break; 102 case IDENTIFIER: 103 if (xf86getSubToken(&(ptr->comment)) != STRING) 104 Error(QUOTE_MSG, "Identifier"); 105 if (has_ident == TRUE) 106 Error(MULTIPLE_MSG, "Identifier"); 107 ptr->identifier = xf86_lex_val.str; 108 has_ident = TRUE; 109 break; 110 case DRIVER: 111 if (xf86getSubToken(&(ptr->comment)) != STRING) 112 Error(QUOTE_MSG, "Driver"); 113 else 114 ptr->driver = xf86_lex_val.str; 115 break; 116 case MATCH_DRIVER: 117 if (xf86getSubToken(&(ptr->comment)) != STRING) 118 Error(QUOTE_MSG, "MatchDriver"); 119 add_group_entry(&ptr->match_driver, 120 xstrtokenize(xf86_lex_val.str, TOKEN_SEP)); 121 free(xf86_lex_val.str); 122 break; 123 case EOF_TOKEN: 124 Error(UNEXPECTED_EOF_MSG); 125 break; 126 default: 127 Error(INVALID_KEYWORD_MSG, xf86tokenString()); 128 break; 129 } 130 } 131 132 if (!has_ident) 133 Error(NO_IDENT_MSG); 134 135#ifdef DEBUG 136 printf("OutputClass section parsed\n"); 137#endif 138 139 return ptr; 140} 141void 142xf86printOutputClassSection(FILE * cf, XF86ConfOutputClassPtr ptr) 143{ 144 const xf86MatchGroup *group; 145 char *const *cur; 146 147 while (ptr) { 148 fprintf(cf, "Section \"OutputClass\"\n"); 149 if (ptr->comment) 150 fprintf(cf, "%s", ptr->comment); 151 if (ptr->identifier) 152 fprintf(cf, "\tIdentifier \"%s\"\n", ptr->identifier); 153 if (ptr->driver) 154 fprintf(cf, "\tDriver \"%s\"\n", ptr->driver); 155 156 xorg_list_for_each_entry(group, &ptr->match_driver, entry) { 157 fprintf(cf, "\tMatchDriver \""); 158 for (cur = group->values; *cur; cur++) 159 fprintf(cf, "%s%s", cur == group->values ? "" : TOKEN_SEP, 160 *cur); 161 fprintf(cf, "\"\n"); 162 } 163 164 fprintf(cf, "EndSection\n\n"); 165 ptr = ptr->list.next; 166 } 167} 168