#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "kconf2h.h"
#define TYPE_YM 1
#define TYPE_HEX 2
#define TYPE_STR 3
#define TYPE_DEC 4
char *new_string( const char *start, const char *end )
{
int str_s = end-start+1;
char *new_str=malloc( str_s+1 );
memcpy( new_str, start, str_s );
if ( new_str != NULL )
new_str[str_s]=0x0;
return new_str;
}
int o_head_def( FILE *f, int type, const char *s_define, const char *e_define,
const char *s_value, const char *e_value )
{
if ( f != NULL )
{
if ( (s_define != NULL) && (e_define != NULL) && (s_define <= e_define) &&
(s_value != NULL) && ( e_value != NULL ) && ( s_value <= e_value ))
{
char *def = new_string( s_define, e_define );
char *val = NULL;
//printf( "%s\n", def );
fprintf( f, "#define %s", def );
free( def );
if ( type != TYPE_YM )
{
val = new_string( s_value, e_value );
fprintf( f, " %s", val );
free( val );
}
/*
switch ( type )
{
case TYPE_HEX:
fprintf( f, " %s", val );
break;
case TYPE_STR:
fprintf( f, " %s", val );
break;
case TYPE_DEC:
fprintf( f, " %s", val );
break;
default:
printf("Unknown Kconfig type %d\n", type);
}
*/
fprintf( f, "\n" );
return 1;
}
}
return 0;
}
%%{
machine k2h;
strings = ( '"' (print+)? '"' );
hex = ( '0x' ([a-fA-F0-9])+ );
comments = ( ('#' (print+)? )>{} );
command_name = (( 'CONFIG_' ([A-Z0-9_])+ )>{token_s = p;}%{token_e = p-1;});
command = (( command_name '=' (
('y'|'m')%{token_type=TYPE_YM;} |
('-'? digit+)%{token_type=TYPE_DEC;} |
strings%{token_type=TYPE_STR;} |
hex%{token_type=TYPE_HEX;} )>{value_s = p;}%{value_e = p-1;}
)>{}%{ o_head_def( outf, token_type, token_s, token_e, value_s, value_e ); } );
eol = ( ( '\n' )>{} );
expr = ((command | comments )? eol)+;
main := expr;
write data;
}%%
int parse_kconf2h( const char *str, FILE *outf )
{
static uint8_t cs;
const int stacksize = 10;
int res=0, *top=0, *stack=NULL;
stack = malloc( sizeof(stack)*stacksize );
char *p=(char *)str, *pe = (char *)str + strlen( str ), *eof=NULL;
/*
variables used in state machine
*/
char *token_s=NULL, *token_e=NULL;
char *value_s=NULL, *value_e=NULL;
int token_type=0;
fprintf( outf, "#ifndef __CONFIG_H\n" );
fprintf( outf, "#define __CONFIG_H\n" );
%%write init;
%%write exec;
if ( cs == k2h_error )
{
printf("ERR state [%d] pos[%d]:[%s]\n", res, p-str, p);
res = -1;
}
fprintf( outf, "#endif\n" );
return res;
}