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