4 * gendec action file parser.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
24 #include <glib/gstrfuncs.h>
25 #include "tools/gendec.h"
27 static int add_action( struct action *actions, struct ruleset *rules, char *operation, const char *file, int line, char *action )
29 char *act = g_strchomp(action);
30 char opclean[strlen(operation)+1];
31 char *p = operation, *q = opclean;
34 // Strip c-style comments
36 if( *p == '/' && *(p+1) == '*' ) {
39 if( *p == '*' && *(p+1) == '/' ) {
45 } else if( *p == '/' && *(p+1) == '/' ) {
47 while( *p && *p != '\n' ) {
56 /* Drop any leading blank lines from the start of the action (and add them
57 * to the line number ) */
58 for( p=act; isspace(*p); p++ ) {
65 strcpy( operation, g_strstrip(opclean) );
67 for( i=0; i<rules->rule_count; i++ ) {
68 if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
69 if( actions[i].text != NULL ) {
70 fprintf( stderr, "gendec:%d: Duplicate actions for operation '%s' (previous action on line %d)\n", line, operation,
74 actions[i].filename = file;
75 actions[i].lineno = line;
76 actions[i].text = act;
80 fprintf(stderr, "gendec:%d: No operation found matching '%s'\n", line, operation );
91 struct ruleset *rules;
92 struct actiontoken token;
95 actionfile_t action_file_open( const char *filename, struct ruleset *rules )
98 FILE *f = fopen( filename, "ro" );
101 fstat( fileno(f), &st );
103 actionfile_t af = malloc( sizeof(struct actionfile) );
105 af->filename = filename;
106 af->length = st.st_size+1;
107 af->text = malloc( st.st_size+1 );
108 fread( af->text, st.st_size, 1, f );
109 af->text[st.st_size] = '\0';
113 af->token.symbol = NONE;
114 af->token.lineno = 1;
115 af->token.filename = filename;
120 actiontoken_t action_file_next( actionfile_t af )
122 af->token.lineno = af->yyline;
123 if( af->yyposn == af->length ) {
124 af->token.symbol = END;
125 } else if( af->token.symbol == TEXT || /* ACTIONS must follow TEXT */
126 (af->token.symbol == NONE && af->text[af->yyposn] == '%' && af->text[af->yyposn+1] == '%') ) {
127 /* Begin action block */
128 af->token.symbol = ACTIONS;
129 memset( af->token.actions, 0, sizeof(af->token.actions) );
131 char *operation = &af->text[af->yyposn];
132 while( af->yyposn < af->length ) {
133 if( af->text[af->yyposn] == '\n' ) {
135 if( af->text[af->yyposn+1] == '%' && af->text[af->yyposn+2] == '%' ) {
141 if( af->text[af->yyposn] == '{' && af->text[af->yyposn+1] == ':' ) {
142 af->text[af->yyposn] = '\0';
143 int line = af->yyline;
145 char *action = &af->text[af->yyposn];
146 while( af->yyposn < af->length ) {
147 if( af->text[af->yyposn] == ':' && af->text[af->yyposn+1] == '}' ) {
148 af->text[af->yyposn] = '\0';
150 if( add_action( af->token.actions, af->rules, operation, af->filename, line, action ) != 0 ) {
151 af->token.symbol = ERROR;
154 operation = &af->text[af->yyposn+1];
156 } else if( af->text[af->yyposn] == '\n' ) {
166 af->token.symbol = TEXT;
167 af->token.text = &af->text[af->yyposn];
168 while( af->yyposn < af->length ) {
170 if( af->text[af->yyposn-1] == '\n' ) {
172 if( af->text[af->yyposn] == '%' && af->text[af->yyposn+1] == '%' ) {
173 af->text[af->yyposn] = '\0';
183 void action_file_close( actionfile_t af )
.