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"
29 struct action *new_action() {
30 struct action *action = malloc( sizeof( struct action ) );
31 memset( action, 0, sizeof( struct action ) );
35 static int add_action( char **actions, struct ruleset *rules, char *operation, char *action )
37 char *act = g_strchomp(action);
38 char opclean[strlen(operation)+1];
39 char *p = operation, *q = opclean;
42 // Strip c-style comments
44 if( *p == '/' && *(p+1) == '*' ) {
47 if( *p == '*' && *(p+1) == '/' ) {
53 } else if( *p == '/' && *(p+1) == '/' ) {
55 while( *p && *p != '\n' ) {
64 strcpy( operation, g_strstrip(opclean) );
66 for( i=0; i<rules->rule_count; i++ ) {
67 if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
68 if( actions[i] != NULL ) {
69 fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );
76 fprintf(stderr, "No operation found matching '%s'\n", operation );
86 struct ruleset *rules;
87 struct actiontoken token;
90 actionfile_t action_file_open( const char *filename, struct ruleset *rules )
93 FILE *f = fopen( filename, "ro" );
96 fstat( fileno(f), &st );
98 actionfile_t af = malloc( sizeof(struct actionfile) );
100 af->length = st.st_size+1;
101 af->text = malloc( st.st_size+1 );
102 fread( af->text, st.st_size, 1, f );
103 af->text[st.st_size] = '\0';
107 af->token.symbol = NONE;
112 actiontoken_t action_file_next( actionfile_t af )
114 if( af->token.symbol == ACTIONS ) {
115 /* Destroy previous actions */
116 memset( af->token.actions, 0, sizeof(af->token.actions) );
119 if( af->yyposn == af->length ) {
120 af->token.symbol = END;
121 } else if( af->token.symbol == TEXT || /* ACTIONS must follow TEXT */
122 (af->token.symbol == NONE && af->text[af->yyposn] == '%' && af->text[af->yyposn+1] == '%') ) {
123 /* Begin action block */
124 af->token.symbol = ACTIONS;
126 char *operation = &af->text[af->yyposn];
127 while( af->yyposn < af->length ) {
128 if( af->text[af->yyposn] == '\n' ) {
130 if( af->text[af->yyposn+1] == '%' && af->text[af->yyposn+2] == '%' ) {
136 if( af->text[af->yyposn] == '{' && af->text[af->yyposn+1] == ':' ) {
137 af->text[af->yyposn] = '\0';
139 char *action = &af->text[af->yyposn];
140 while( af->yyposn < af->length ) {
141 if( af->text[af->yyposn] == ':' && af->text[af->yyposn+1] == '}' ) {
142 af->text[af->yyposn] = '\0';
144 if( add_action( af->token.actions, af->rules, operation, action ) != 0 ) {
145 af->token.symbol = ERROR;
148 operation = &af->text[af->yyposn+1];
158 af->token.symbol = TEXT;
159 af->token.text = &af->text[af->yyposn];
160 while( af->yyposn < af->length ) {
162 if( af->text[af->yyposn-1] == '\n' ) {
164 if( af->text[af->yyposn] == '%' && af->text[af->yyposn+1] == '%' ) {
165 af->text[af->yyposn] = '\0';
175 void action_file_close( actionfile_t af )
.