| 1 | #!/usr/bin/perl -w
|
| 2 | use strict;
|
| 3 |
|
| 4 | if ( $#ARGV != 3 ) {
|
| 5 | die "Usage: $0 <infile> <configetc> <config.h> <config.c>\n";
|
| 6 | }
|
| 7 |
|
| 8 | my $infile = $ARGV[0];
|
| 9 | my $conffile = $ARGV[1];
|
| 10 | my $hfile = $ARGV[2];
|
| 11 | my $cfile = $ARGV[3];
|
| 12 |
|
| 13 | if ( ! -f $infile ) {
|
| 14 | die "Cant stat $infile $!\n";
|
| 15 | }
|
| 16 |
|
| 17 | my $realconf = ""; # the final etc/config file
|
| 18 | my $configh = ""; # the aconfig.h config + entry structs
|
| 19 | my $configh2 = ""; # the aconfig.c holding defaults
|
| 20 | my $configh3 = ""; # all config entries structs for the c parser
|
| 21 |
|
| 22 | $configh .= sprintf("/* Auto generated by make config in $0 %s */\n",scalar localtime(time()) );
|
| 23 | $configh2 .= sprintf("/* Auto generated by make config in $0 %s */\n",scalar localtime(time()) );
|
| 24 | $realconf .= sprintf("# Auto generated by make config in $0 %s\n",scalar localtime(time()) );
|
| 25 | $configh .= "enum cftype {\n\ttype_str, type_int\n};\n";
|
| 26 | $configh .= "struct cfentry {\n\tchar *name;\n\tvoid *addr;\n\tenum cftype type;\n};\n#define CONFIG struct config\nstruct config {\n";
|
| 27 | $configh2 .= "#include <stdio.h>\n#include \"aconfig.h\"\nstruct config cfg = {\n";
|
| 28 | $configh3 = "struct cfentry confentry[] = {\n";
|
| 29 |
|
| 30 | open(F,$infile) || die $!;
|
| 31 | while(<F>){
|
| 32 | chop;
|
| 33 | if ( m/^$/g || m/^#/g ) {
|
| 34 | $realconf .= "$_\n";
|
| 35 | next;
|
| 36 | }
|
| 37 |
|
| 38 | my ($type, $key, $val) = split;
|
| 39 | if ( $type eq "string" || $type eq "char" ) {
|
| 40 | $val = "NULL" if ! $val;
|
| 41 | $configh .= sprintf("\tchar *%s;\n", $key);
|
| 42 | $configh2 .= sprintf("\t\"%s\",\n", $val);
|
| 43 | $realconf .= sprintf("%s %s\n",$key, $val);
|
| 44 | $configh3 .= sprintf("\t{ \"%s\", (void*)&cfg.%s, type_str } ,\n", $key, $key);
|
| 45 | }elsif ( $type eq "int" ) {
|
| 46 | $val = "0" if ! $val;
|
| 47 | $configh .= sprintf("\tint %s;\n", $key);
|
| 48 | $configh2 .= sprintf("\t%d,\n", $val);
|
| 49 | $realconf .= sprintf("%s %s\n",$key, $val);
|
| 50 | $configh3 .= sprintf("\t{ \"%s\", (void*)&cfg.%s, type_int } ,\n", $key, $key);
|
| 51 | }else{
|
| 52 | print "Unhandled config entry: $key $val\n";
|
| 53 | }
|
| 54 | }
|
| 55 | close F;
|
| 56 |
|
| 57 | $configh .= "\tchar *null;\n};\nextern struct config cfg;\nextern struct cfentry confentry[];\n";
|
| 58 | $configh2 .= "\tNULL\n};\n$configh3};\n";
|
| 59 |
|
| 60 | open(F,">$conffile") || die "$conffile $!\n";
|
| 61 | print F $realconf;
|
| 62 | close F;
|
| 63 |
|
| 64 | open(F,">$hfile") || die "$cfile $!\n";
|
| 65 | print F $configh;
|
| 66 | close F;
|
| 67 |
|
| 68 | open(F,">$cfile") || die "$cfile $!\n";
|
| 69 | print F $configh2;
|
| 70 | close F;
|
| 71 |
|