Search
lxdream.org :: lxdream/src/tools/actparse.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/actparse.c
changeset 561:533f6b478071
prev420:e6f43dec3cf0
next736:a02d1475ccfd
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@561
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@561
     3
 *
nkeynes@561
     4
 * gendec action file parser. 
nkeynes@561
     5
 *
nkeynes@561
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@561
     7
 *
nkeynes@561
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@561
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@561
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@561
    11
 * (at your option) any later version.
nkeynes@561
    12
 *
nkeynes@561
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@561
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@561
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@561
    16
 * GNU General Public License for more details.
nkeynes@561
    17
 */
nkeynes@561
    18
nkeynes@359
    19
#include <stdlib.h>
nkeynes@359
    20
#include <stdio.h>
nkeynes@359
    21
#include <string.h>
nkeynes@359
    22
#include <ctype.h>
nkeynes@359
    23
#include <sys/stat.h>
nkeynes@359
    24
#include <glib/gstrfuncs.h>
nkeynes@359
    25
#include "tools/gendec.h"
nkeynes@359
    26
nkeynes@359
    27
static int yyline;
nkeynes@359
    28
nkeynes@420
    29
struct action *new_action() {
nkeynes@359
    30
    struct action *action = malloc( sizeof( struct action ) );
nkeynes@359
    31
    memset( action, 0, sizeof( struct action ) );
nkeynes@359
    32
    return action;
nkeynes@359
    33
}
nkeynes@359
    34
nkeynes@359
    35
int add_action( struct actionset *actions, struct ruleset *rules, char *operation, char *action )
nkeynes@359
    36
{
nkeynes@359
    37
    char *act = g_strchomp(action);
nkeynes@359
    38
nkeynes@359
    39
    char opclean[strlen(operation)];
nkeynes@359
    40
    char *p = operation, *q = opclean;
nkeynes@359
    41
    int i;
nkeynes@359
    42
nkeynes@359
    43
    // Strip c-style comments 
nkeynes@359
    44
    while( *p ) {
nkeynes@359
    45
	if( *p == '/' && *(p+1) == '*' ) {
nkeynes@359
    46
	    p+=2;
nkeynes@359
    47
	    while( *p ) {
nkeynes@359
    48
		if( *p == '*' && *(p+1) == '/' ) {
nkeynes@359
    49
		    p+=2;
nkeynes@359
    50
		    break;
nkeynes@359
    51
		}
nkeynes@359
    52
		p++;
nkeynes@359
    53
	    }
nkeynes@359
    54
	} else if( *p == '/' && *(p+1) == '/' ) {
nkeynes@359
    55
	    p+=2;
nkeynes@359
    56
	    while( *p && *p != '\n' ) {
nkeynes@359
    57
		p++;
nkeynes@359
    58
	    }
nkeynes@359
    59
	} else {
nkeynes@359
    60
	    *q++ = *p++;
nkeynes@359
    61
	}
nkeynes@359
    62
    }
nkeynes@359
    63
    *q = '\0';
nkeynes@359
    64
    strcpy( operation, g_strstrip(opclean) );
nkeynes@359
    65
nkeynes@359
    66
    for( i=0; i<rules->rule_count; i++ ) {
nkeynes@359
    67
	if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
nkeynes@359
    68
	    if( actions->actions[i] != NULL ) {
nkeynes@359
    69
		fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );
nkeynes@359
    70
		return -1;
nkeynes@359
    71
	    }
nkeynes@359
    72
	    actions->actions[i] = act;
nkeynes@359
    73
	    return 0;
nkeynes@359
    74
	}
nkeynes@359
    75
    }
nkeynes@359
    76
    fprintf(stderr, "No operation found matching '%s'\n", operation );
nkeynes@359
    77
    return -1;
nkeynes@359
    78
}
nkeynes@359
    79
	
nkeynes@359
    80
nkeynes@359
    81
struct actionset *parse_action_file( struct ruleset *rules, FILE *f ) 
nkeynes@359
    82
{
nkeynes@359
    83
    struct actionset *actions = malloc( sizeof(struct actionset ) );
nkeynes@359
    84
    struct stat st;
nkeynes@359
    85
    char *text;
nkeynes@359
    86
    int i, length;
nkeynes@359
    87
    
nkeynes@359
    88
    memset( actions, 0, sizeof( struct actionset ) );
nkeynes@359
    89
    /* Read whole file in (for convenience) */
nkeynes@359
    90
    fstat( fileno(f), &st );
nkeynes@359
    91
    length = st.st_size;
nkeynes@359
    92
    text = malloc( length+1 );
nkeynes@359
    93
    fread( text, length, 1, f );
nkeynes@359
    94
    text[length] = '\0';
nkeynes@359
    95
    yyline = 0;
nkeynes@359
    96
    actions->pretext = text;
nkeynes@359
    97
    for( i=0; i<length; i++ ) {
nkeynes@359
    98
	if( text[i] == '\n' ) {
nkeynes@359
    99
	    yyline++;
nkeynes@359
   100
	    if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
nkeynes@359
   101
		text[i+1] = '\0';
nkeynes@359
   102
		i+=3;
nkeynes@359
   103
		break;
nkeynes@359
   104
	    }
nkeynes@359
   105
	}
nkeynes@359
   106
    }
nkeynes@359
   107
nkeynes@359
   108
    char *operation = &text[i];
nkeynes@359
   109
    for( ; i<length; i++ ) {
nkeynes@359
   110
	if( text[i] == '\n' ) {
nkeynes@359
   111
	    yyline++;
nkeynes@359
   112
	    if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
nkeynes@359
   113
		i+=3;
nkeynes@359
   114
		break;
nkeynes@359
   115
	    }
nkeynes@359
   116
	}
nkeynes@359
   117
	
nkeynes@359
   118
	if( text[i] == '{' && text[i+1] == ':' ) {
nkeynes@359
   119
	    text[i] = '\0';
nkeynes@359
   120
	    i+=2;
nkeynes@359
   121
	    char *action = &text[i];
nkeynes@359
   122
	    for( ;i<length; i++ ) {
nkeynes@359
   123
		if( text[i] == ':' && text[i+1] == '}' ) {
nkeynes@359
   124
		    text[i] = '\0';
nkeynes@359
   125
		    i++;
nkeynes@359
   126
		    if( add_action( actions, rules, operation, action ) != 0 ) {
nkeynes@359
   127
			free(actions);
nkeynes@359
   128
			free(text);
nkeynes@359
   129
			return NULL;
nkeynes@359
   130
		    }
nkeynes@359
   131
		    operation = &text[i+1];
nkeynes@359
   132
		    break;
nkeynes@359
   133
		}
nkeynes@359
   134
	    }
nkeynes@359
   135
	}
nkeynes@359
   136
    }
nkeynes@359
   137
nkeynes@359
   138
    actions->posttext = &text[i];
nkeynes@359
   139
	
nkeynes@359
   140
    return actions;
nkeynes@359
   141
}
.