Search
lxdream.org :: lxdream/src/tools/actparse.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/actparse.c
changeset 736:a02d1475ccfd
prev561:533f6b478071
next948:545c85cc56f1
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
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@736
    45
        if( *p == '/' && *(p+1) == '*' ) {
nkeynes@736
    46
            p+=2;
nkeynes@736
    47
            while( *p ) {
nkeynes@736
    48
                if( *p == '*' && *(p+1) == '/' ) {
nkeynes@736
    49
                    p+=2;
nkeynes@736
    50
                    break;
nkeynes@736
    51
                }
nkeynes@736
    52
                p++;
nkeynes@736
    53
            }
nkeynes@736
    54
        } else if( *p == '/' && *(p+1) == '/' ) {
nkeynes@736
    55
            p+=2;
nkeynes@736
    56
            while( *p && *p != '\n' ) {
nkeynes@736
    57
                p++;
nkeynes@736
    58
            }
nkeynes@736
    59
        } else {
nkeynes@736
    60
            *q++ = *p++;
nkeynes@736
    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@736
    67
        if( strcasecmp(rules->rules[i]->format, operation) == 0 ) {
nkeynes@736
    68
            if( actions->actions[i] != NULL ) {
nkeynes@736
    69
                fprintf( stderr, "Duplicate actions for operation '%s'\n", operation );
nkeynes@736
    70
                return -1;
nkeynes@736
    71
            }
nkeynes@736
    72
            actions->actions[i] = act;
nkeynes@736
    73
            return 0;
nkeynes@736
    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@736
    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@736
    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@736
    98
        if( text[i] == '\n' ) {
nkeynes@736
    99
            yyline++;
nkeynes@736
   100
            if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
nkeynes@736
   101
                text[i+1] = '\0';
nkeynes@736
   102
                i+=3;
nkeynes@736
   103
                break;
nkeynes@736
   104
            }
nkeynes@736
   105
        }
nkeynes@359
   106
    }
nkeynes@359
   107
nkeynes@359
   108
    char *operation = &text[i];
nkeynes@359
   109
    for( ; i<length; i++ ) {
nkeynes@736
   110
        if( text[i] == '\n' ) {
nkeynes@736
   111
            yyline++;
nkeynes@736
   112
            if( i+3 < length && text[i+1] == '%' && text[i+2] == '%' ) {
nkeynes@736
   113
                i+=3;
nkeynes@736
   114
                break;
nkeynes@736
   115
            }
nkeynes@736
   116
        }
nkeynes@736
   117
nkeynes@736
   118
        if( text[i] == '{' && text[i+1] == ':' ) {
nkeynes@736
   119
            text[i] = '\0';
nkeynes@736
   120
            i+=2;
nkeynes@736
   121
            char *action = &text[i];
nkeynes@736
   122
            for( ;i<length; i++ ) {
nkeynes@736
   123
                if( text[i] == ':' && text[i+1] == '}' ) {
nkeynes@736
   124
                    text[i] = '\0';
nkeynes@736
   125
                    i++;
nkeynes@736
   126
                    if( add_action( actions, rules, operation, action ) != 0 ) {
nkeynes@736
   127
                        free(actions);
nkeynes@736
   128
                        free(text);
nkeynes@736
   129
                        return NULL;
nkeynes@736
   130
                    }
nkeynes@736
   131
                    operation = &text[i+1];
nkeynes@736
   132
                    break;
nkeynes@736
   133
                }
nkeynes@736
   134
            }
nkeynes@736
   135
        }
nkeynes@359
   136
    }
nkeynes@359
   137
nkeynes@359
   138
    actions->posttext = &text[i];
nkeynes@736
   139
nkeynes@359
   140
    return actions;
nkeynes@359
   141
}
.