Search
lxdream.org :: lxdream/src/tools/actparse.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/actparse.c
changeset 420:e6f43dec3cf0
prev359:c588dce7ebde
next561:533f6b478071
author nkeynes
date Sat Oct 06 08:48:47 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix compile warnings
file annotate diff log raw
nkeynes@359
     1
#include <stdlib.h>
nkeynes@359
     2
#include <stdio.h>
nkeynes@359
     3
#include <string.h>
nkeynes@359
     4
#include <ctype.h>
nkeynes@359
     5
#include <sys/stat.h>
nkeynes@359
     6
#include <glib/gstrfuncs.h>
nkeynes@359
     7
#include "tools/gendec.h"
nkeynes@359
     8
nkeynes@359
     9
static int yyline;
nkeynes@359
    10
nkeynes@420
    11
struct action *new_action() {
nkeynes@359
    12
    struct action *action = malloc( sizeof( struct action ) );
nkeynes@359
    13
    memset( action, 0, sizeof( struct action ) );
nkeynes@359
    14
    return action;
nkeynes@359
    15
}
nkeynes@359
    16
nkeynes@359
    17
int add_action( struct actionset *actions, struct ruleset *rules, char *operation, char *action )
nkeynes@359
    18
{
nkeynes@359
    19
    char *act = g_strchomp(action);
nkeynes@359
    20
nkeynes@359
    21
    char opclean[strlen(operation)];
nkeynes@359
    22
    char *p = operation, *q = opclean;
nkeynes@359
    23
    int i;
nkeynes@359
    24
nkeynes@359
    25
    // Strip c-style comments 
nkeynes@359
    26
    while( *p ) {
nkeynes@359
    27
	if( *p == '/' && *(p+1) == '*' ) {
nkeynes@359
    28
	    p+=2;
nkeynes@359
    29
	    while( *p ) {
nkeynes@359
    30
		if( *p == '*' && *(p+1) == '/' ) {
nkeynes@359
    31
		    p+=2;
nkeynes@359
    32
		    break;
nkeynes@359
    33
		}
nkeynes@359
    34
		p++;
nkeynes@359
    35
	    }
nkeynes@359
    36
	} else if( *p == '/' && *(p+1) == '/' ) {
nkeynes@359
    37
	    p+=2;
nkeynes@359
    38
	    while( *p && *p != '\n' ) {
nkeynes@359
    39
		p++;
nkeynes@359
    40
	    }
nkeynes@359
    41
	} else {
nkeynes@359
    42
	    *q++ = *p++;
nkeynes@359
    43
	}
nkeynes@359
    44
    }
nkeynes@359
    45
    *q = '\0';
nkeynes@359
    46
    strcpy( operation, g_strstrip(opclean) );
nkeynes@359
    47
nkeynes@359
    48
    for( i=0; i<rules->rule_count; i++ ) {
nkeynes@359
    49
	if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
nkeynes@359
    50
	    if( actions->actions[i] != NULL ) {
nkeynes@359
    51
		fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );
nkeynes@359
    52
		return -1;
nkeynes@359
    53
	    }
nkeynes@359
    54
	    actions->actions[i] = act;
nkeynes@359
    55
	    return 0;
nkeynes@359
    56
	}
nkeynes@359
    57
    }
nkeynes@359
    58
    fprintf(stderr, "No operation found matching '%s'\n", operation );
nkeynes@359
    59
    return -1;
nkeynes@359
    60
}
nkeynes@359
    61
	
nkeynes@359
    62
nkeynes@359
    63
struct actionset *parse_action_file( struct ruleset *rules, FILE *f ) 
nkeynes@359
    64
{
nkeynes@359
    65
    struct actionset *actions = malloc( sizeof(struct actionset ) );
nkeynes@359
    66
    struct stat st;
nkeynes@359
    67
    char *text;
nkeynes@359
    68
    int i, length;
nkeynes@359
    69
    
nkeynes@359
    70
    memset( actions, 0, sizeof( struct actionset ) );
nkeynes@359
    71
    /* Read whole file in (for convenience) */
nkeynes@359
    72
    fstat( fileno(f), &st );
nkeynes@359
    73
    length = st.st_size;
nkeynes@359
    74
    text = malloc( length+1 );
nkeynes@359
    75
    fread( text, length, 1, f );
nkeynes@359
    76
    text[length] = '\0';
nkeynes@359
    77
    yyline = 0;
nkeynes@359
    78
    actions->pretext = text;
nkeynes@359
    79
    for( i=0; i<length; i++ ) {
nkeynes@359
    80
	if( text[i] == '\n' ) {
nkeynes@359
    81
	    yyline++;
nkeynes@359
    82
	    if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
nkeynes@359
    83
		text[i+1] = '\0';
nkeynes@359
    84
		i+=3;
nkeynes@359
    85
		break;
nkeynes@359
    86
	    }
nkeynes@359
    87
	}
nkeynes@359
    88
    }
nkeynes@359
    89
nkeynes@359
    90
    char *operation = &text[i];
nkeynes@359
    91
    for( ; i<length; i++ ) {
nkeynes@359
    92
	if( text[i] == '\n' ) {
nkeynes@359
    93
	    yyline++;
nkeynes@359
    94
	    if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
nkeynes@359
    95
		i+=3;
nkeynes@359
    96
		break;
nkeynes@359
    97
	    }
nkeynes@359
    98
	}
nkeynes@359
    99
	
nkeynes@359
   100
	if( text[i] == '{' && text[i+1] == ':' ) {
nkeynes@359
   101
	    text[i] = '\0';
nkeynes@359
   102
	    i+=2;
nkeynes@359
   103
	    char *action = &text[i];
nkeynes@359
   104
	    for( ;i<length; i++ ) {
nkeynes@359
   105
		if( text[i] == ':' && text[i+1] == '}' ) {
nkeynes@359
   106
		    text[i] = '\0';
nkeynes@359
   107
		    i++;
nkeynes@359
   108
		    if( add_action( actions, rules, operation, action ) != 0 ) {
nkeynes@359
   109
			free(actions);
nkeynes@359
   110
			free(text);
nkeynes@359
   111
			return NULL;
nkeynes@359
   112
		    }
nkeynes@359
   113
		    operation = &text[i+1];
nkeynes@359
   114
		    break;
nkeynes@359
   115
		}
nkeynes@359
   116
	    }
nkeynes@359
   117
	}
nkeynes@359
   118
    }
nkeynes@359
   119
nkeynes@359
   120
    actions->posttext = &text[i];
nkeynes@359
   121
	
nkeynes@359
   122
    return actions;
nkeynes@359
   123
}
.