Search
lxdream.org :: lxdream/src/tools/actparse.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/actparse.c
changeset 948:545c85cc56f1
prev736:a02d1475ccfd
next961:3541b280e0f1
author nkeynes
date Tue Jan 13 11:56:28 2009 +0000 (15 years ago)
permissions -rw-r--r--
last change Merge lxdream-mem branch back to trunk
file annotate diff log raw
1.1 --- a/src/tools/actparse.c Mon Jul 14 07:44:42 2008 +0000
1.2 +++ b/src/tools/actparse.c Tue Jan 13 11:56:28 2009 +0000
1.3 @@ -32,7 +32,7 @@
1.4 return action;
1.5 }
1.6
1.7 -int add_action( struct actionset *actions, struct ruleset *rules, char *operation, char *action )
1.8 +int add_action( char **actions, struct ruleset *rules, char *operation, char *action )
1.9 {
1.10 char *act = g_strchomp(action);
1.11
1.12 @@ -65,11 +65,11 @@
1.13
1.14 for( i=0; i<rules->rule_count; i++ ) {
1.15 if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
1.16 - if( actions->actions[i] != NULL ) {
1.17 + if( actions[i] != NULL ) {
1.18 fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );
1.19 return -1;
1.20 }
1.21 - actions->actions[i] = act;
1.22 + actions[i] = act;
1.23 return 0;
1.24 }
1.25 }
1.26 @@ -77,65 +77,105 @@
1.27 return -1;
1.28 }
1.29
1.30 +struct actionfile {
1.31 + FILE *f;
1.32 + char *text;
1.33 + int length;
1.34 + int yyposn;
1.35 + int yyline;
1.36 + struct ruleset *rules;
1.37 + struct actiontoken token;
1.38 +};
1.39
1.40 -struct actionset *parse_action_file( struct ruleset *rules, FILE *f )
1.41 +actionfile_t action_file_open( const char *filename, struct ruleset *rules )
1.42 {
1.43 - struct actionset *actions = malloc( sizeof(struct actionset ) );
1.44 struct stat st;
1.45 - char *text;
1.46 - int i, length;
1.47 + FILE *f = fopen( filename, "ro" );
1.48 + if( f == NULL )
1.49 + return NULL;
1.50 + fstat( fileno(f), &st );
1.51 +
1.52 + actionfile_t af = malloc( sizeof(struct actionfile) );
1.53 + af->f = f;
1.54 + af->length = st.st_size+1;
1.55 + af->text = malloc( st.st_size+1 );
1.56 + fread( af->text, st.st_size, 1, f );
1.57 + af->text[st.st_size] = '\0';
1.58 + af->yyline = 0;
1.59 + af->yyposn = 0;
1.60 + af->rules = rules;
1.61 + af->token.symbol = NONE;
1.62 +
1.63 + return af;
1.64 +}
1.65
1.66 - memset( actions, 0, sizeof( struct actionset ) );
1.67 - /* Read whole file in (for convenience) */
1.68 - fstat( fileno(f), &st );
1.69 - length = st.st_size;
1.70 - text = malloc( length+1 );
1.71 - fread( text, length, 1, f );
1.72 - text[length] = '\0';
1.73 - yyline = 0;
1.74 - actions->pretext = text;
1.75 - for( i=0; i<length; i++ ) {
1.76 - if( text[i] == '\n' ) {
1.77 - yyline++;
1.78 - if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
1.79 - text[i+1] = '\0';
1.80 - i+=3;
1.81 - break;
1.82 +actiontoken_t action_file_next( actionfile_t af )
1.83 +{
1.84 + if( af->token.symbol == ACTIONS ) {
1.85 + /* Destroy previous actions */
1.86 + memset( af->token.actions, 0, sizeof(af->token.actions) );
1.87 + }
1.88 +
1.89 + if( af->yyposn == af->length ) {
1.90 + af->token.symbol = END;
1.91 + } else if( af->token.symbol == TEXT || /* ACTIONS must follow TEXT */
1.92 + (af->token.symbol == NONE && af->text[af->yyposn] == '\%' && af->text[af->yyposn+1] == '%') ) {
1.93 + /* Begin action block */
1.94 + af->token.symbol = ACTIONS;
1.95 +
1.96 + char *operation = &af->text[af->yyposn];
1.97 + while( af->yyposn < af->length ) {
1.98 + if( af->text[af->yyposn] == '\n' ) {
1.99 + yyline++;
1.100 + if( af->text[af->yyposn+1] == '%' && af->text[af->yyposn+2] == '%' ) {
1.101 + af->yyposn += 3;
1.102 + break;
1.103 + }
1.104 }
1.105 +
1.106 + if( af->text[af->yyposn] == '{' && af->text[af->yyposn+1] == ':' ) {
1.107 + af->text[af->yyposn] = '\0';
1.108 + af->yyposn+=2;
1.109 + char *action = &af->text[af->yyposn];
1.110 + while( af->yyposn < af->length ) {
1.111 + if( af->text[af->yyposn] == ':' && af->text[af->yyposn+1] == '}' ) {
1.112 + af->text[af->yyposn] = '\0';
1.113 + af->yyposn++;
1.114 + if( add_action( af->token.actions, af->rules, operation, action ) != 0 ) {
1.115 + af->token.symbol = ERROR;
1.116 + return &af->token;
1.117 + }
1.118 + operation = &af->text[af->yyposn+1];
1.119 + break;
1.120 + }
1.121 + af->yyposn++;
1.122 + }
1.123 + }
1.124 + af->yyposn++;
1.125 }
1.126 - }
1.127 -
1.128 - char *operation = &text[i];
1.129 - for( ; i<length; i++ ) {
1.130 - if( text[i] == '\n' ) {
1.131 - yyline++;
1.132 - if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
1.133 - i+=3;
1.134 - break;
1.135 - }
1.136 - }
1.137 -
1.138 - if( text[i] == '{' && text[i+1] == ':' ) {
1.139 - text[i] = '\0';
1.140 - i+=2;
1.141 - char *action = &text[i];
1.142 - for( ;i<length; i++ ) {
1.143 - if( text[i] == ':' && text[i+1] == '}' ) {
1.144 - text[i] = '\0';
1.145 - i++;
1.146 - if( add_action( actions, rules, operation, action ) != 0 ) {
1.147 - free(actions);
1.148 - free(text);
1.149 - return NULL;
1.150 - }
1.151 - operation = &text[i+1];
1.152 + } else {
1.153 + /* Text block */
1.154 + af->token.symbol = TEXT;
1.155 + af->token.text = &af->text[af->yyposn];
1.156 + while( af->yyposn < af->length ) {
1.157 + af->yyposn++;
1.158 + if( af->text[af->yyposn-1] == '\n' ) {
1.159 + af->yyline++;
1.160 + if( af->text[af->yyposn] == '%' && af->text[af->yyposn+1] == '%' ) {
1.161 + af->text[af->yyposn] = '\0';
1.162 + af->yyposn += 2;
1.163 break;
1.164 }
1.165 }
1.166 }
1.167 }
1.168 + return &af->token;
1.169 +}
1.170
1.171 - actions->posttext = &text[i];
1.172 +void action_file_close( actionfile_t af )
1.173 +{
1.174 + free( af->text );
1.175 + fclose( af->f );
1.176 + free( af );
1.177 +}
1.178
1.179 - return actions;
1.180 -}
.