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 int add_action( struct actionset *actions, struct ruleset *rules, char *operation, char *action )
37 char *act = g_strchomp(action);
39 char opclean[strlen(operation)];
40 char *p = operation, *q = opclean;
43 // Strip c-style comments
45 if( *p == '/' && *(p+1) == '*' ) {
48 if( *p == '*' && *(p+1) == '/' ) {
54 } else if( *p == '/' && *(p+1) == '/' ) {
56 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->actions[i] != NULL ) {
69 fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );
72 actions->actions[i] = act;
76 fprintf(stderr, "No operation found matching '%s'\n", operation );
81 struct actionset *parse_action_file( struct ruleset *rules, FILE *f )
83 struct actionset *actions = malloc( sizeof(struct actionset ) );
88 memset( actions, 0, sizeof( struct actionset ) );
89 /* Read whole file in (for convenience) */
90 fstat( fileno(f), &st );
92 text = malloc( length+1 );
93 fread( text, length, 1, f );
96 actions->pretext = text;
97 for( i=0; i<length; i++ ) {
98 if( text[i] == '\n' ) {
100 if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
108 char *operation = &text[i];
109 for( ; i<length; i++ ) {
110 if( text[i] == '\n' ) {
112 if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
118 if( text[i] == '{' && text[i+1] == ':' ) {
121 char *action = &text[i];
122 for( ;i<length; i++ ) {
123 if( text[i] == ':' && text[i+1] == '}' ) {
126 if( add_action( actions, rules, operation, action ) != 0 ) {
131 operation = &text[i+1];
138 actions->posttext = &text[i];
.