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
view annotate diff log raw
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <ctype.h>
     5 #include <sys/stat.h>
     6 #include <glib/gstrfuncs.h>
     7 #include "tools/gendec.h"
     9 static int yyline;
    11 struct action *new_action() {
    12     struct action *action = malloc( sizeof( struct action ) );
    13     memset( action, 0, sizeof( struct action ) );
    14     return action;
    15 }
    17 int add_action( struct actionset *actions, struct ruleset *rules, char *operation, char *action )
    18 {
    19     char *act = g_strchomp(action);
    21     char opclean[strlen(operation)];
    22     char *p = operation, *q = opclean;
    23     int i;
    25     // Strip c-style comments 
    26     while( *p ) {
    27 	if( *p == '/' && *(p+1) == '*' ) {
    28 	    p+=2;
    29 	    while( *p ) {
    30 		if( *p == '*' && *(p+1) == '/' ) {
    31 		    p+=2;
    32 		    break;
    33 		}
    34 		p++;
    35 	    }
    36 	} else if( *p == '/' && *(p+1) == '/' ) {
    37 	    p+=2;
    38 	    while( *p && *p != '\n' ) {
    39 		p++;
    40 	    }
    41 	} else {
    42 	    *q++ = *p++;
    43 	}
    44     }
    45     *q = '\0';
    46     strcpy( operation, g_strstrip(opclean) );
    48     for( i=0; i<rules->rule_count; i++ ) {
    49 	if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
    50 	    if( actions->actions[i] != NULL ) {
    51 		fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );
    52 		return -1;
    53 	    }
    54 	    actions->actions[i] = act;
    55 	    return 0;
    56 	}
    57     }
    58     fprintf(stderr, "No operation found matching '%s'\n", operation );
    59     return -1;
    60 }
    63 struct actionset *parse_action_file( struct ruleset *rules, FILE *f ) 
    64 {
    65     struct actionset *actions = malloc( sizeof(struct actionset ) );
    66     struct stat st;
    67     char *text;
    68     int i, length;
    70     memset( actions, 0, sizeof( struct actionset ) );
    71     /* Read whole file in (for convenience) */
    72     fstat( fileno(f), &st );
    73     length = st.st_size;
    74     text = malloc( length+1 );
    75     fread( text, length, 1, f );
    76     text[length] = '\0';
    77     yyline = 0;
    78     actions->pretext = text;
    79     for( i=0; i<length; i++ ) {
    80 	if( text[i] == '\n' ) {
    81 	    yyline++;
    82 	    if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
    83 		text[i+1] = '\0';
    84 		i+=3;
    85 		break;
    86 	    }
    87 	}
    88     }
    90     char *operation = &text[i];
    91     for( ; i<length; i++ ) {
    92 	if( text[i] == '\n' ) {
    93 	    yyline++;
    94 	    if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
    95 		i+=3;
    96 		break;
    97 	    }
    98 	}
   100 	if( text[i] == '{' && text[i+1] == ':' ) {
   101 	    text[i] = '\0';
   102 	    i+=2;
   103 	    char *action = &text[i];
   104 	    for( ;i<length; i++ ) {
   105 		if( text[i] == ':' && text[i+1] == '}' ) {
   106 		    text[i] = '\0';
   107 		    i++;
   108 		    if( add_action( actions, rules, operation, action ) != 0 ) {
   109 			free(actions);
   110 			free(text);
   111 			return NULL;
   112 		    }
   113 		    operation = &text[i+1];
   114 		    break;
   115 		}
   116 	    }
   117 	}
   118     }
   120     actions->posttext = &text[i];
   122     return actions;
   123 }
.