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.
25 #include <glib/gstrfuncs.h>
26 #include "tools/gendec.h"
28 static int add_action( struct action *actions, struct ruleset *rules, char *operation, const char *file, int line, char *action )
30 char *act = g_strchomp(action);
31 char opclean[strlen(operation)+1];
32 char *p = operation, *q = opclean;
35 // Strip c-style comments
37 if( *p == '/' && *(p+1) == '*' ) {
40 if( *p == '*' && *(p+1) == '/' ) {
46 } else if( *p == '/' && *(p+1) == '/' ) {
48 while( *p && *p != '\n' ) {
57 /* Drop any leading blank lines from the start of the action (and add them
58 * to the line number ) */
59 for( p=act; isspace(*p); p++ ) {
66 strcpy( operation, g_strstrip(opclean) );
68 for( i=0; i<rules->rule_count; i++ ) {
69 if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
70 if( actions[i].text != NULL ) {
71 fprintf( stderr, "gendec:%d: Duplicate actions for operation '%s' (previous action on line %d)\n", line, operation,
75 actions[i].filename = file;
76 actions[i].lineno = line;
77 actions[i].text = act;
81 fprintf(stderr, "gendec:%d: No operation found matching '%s'\n", line, operation );
92 struct ruleset *rules;
93 struct actiontoken token;
96 actionfile_t action_file_open( const char *filename, struct ruleset *rules )
99 FILE *f = fopen( filename, "ro" );
102 fstat( fileno(f), &st );
104 actionfile_t af = malloc( sizeof(struct actionfile) );
106 af->filename = filename;
107 af->length = st.st_size+1;
108 af->text = malloc( st.st_size+1 );
109 if( fread( af->text, st.st_size, 1, f ) != 1 ) {
110 fprintf( stderr, "Error: unable to read from file '%s' (%s)\n", filename, strerror(errno));
115 af->text[st.st_size] = '\0';
119 af->token.symbol = NONE;
120 af->token.lineno = 1;
121 af->token.filename = filename;
125 actiontoken_t action_file_next( actionfile_t af )
127 af->token.lineno = af->yyline;
128 if( af->yyposn == af->length ) {
129 af->token.symbol = END;
130 } else if( af->token.symbol == TEXT || /* ACTIONS must follow TEXT */
131 (af->token.symbol == NONE && af->text[af->yyposn] == '%' && af->text[af->yyposn+1] == '%') ) {
132 /* Begin action block */
133 af->token.symbol = ACTIONS;
134 memset( af->token.actions, 0, sizeof(af->token.actions) );
136 char *operation = &af->text[af->yyposn];
137 while( af->yyposn < af->length ) {
138 if( af->text[af->yyposn] == '\n' ) {
140 if( af->text[af->yyposn+1] == '%' && af->text[af->yyposn+2] == '%' ) {
146 if( af->text[af->yyposn] == '{' && af->text[af->yyposn+1] == ':' ) {
147 af->text[af->yyposn] = '\0';
148 int line = af->yyline;
150 char *action = &af->text[af->yyposn];
151 while( af->yyposn < af->length ) {
152 if( af->text[af->yyposn] == ':' && af->text[af->yyposn+1] == '}' ) {
153 af->text[af->yyposn] = '\0';
155 if( add_action( af->token.actions, af->rules, operation, af->filename, line, action ) != 0 ) {
156 af->token.symbol = ERROR;
159 operation = &af->text[af->yyposn+1];
161 } else if( af->text[af->yyposn] == '\n' ) {
171 af->token.symbol = TEXT;
172 af->token.text = &af->text[af->yyposn];
173 while( af->yyposn < af->length ) {
175 if( af->text[af->yyposn-1] == '\n' ) {
177 if( af->text[af->yyposn] == '%' && af->text[af->yyposn+1] == '%' ) {
178 af->text[af->yyposn] = '\0';
188 void action_file_close( actionfile_t af )
.