Mercurial > hg > Members > tobaru > CbC_xv6
changeset 97:70eae4b230f2
add update_context.pl
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 14 Nov 2019 10:14:15 +0900 |
parents | d5c4016c65b8 |
children | d0aea70fe1b0 |
files | src/gearsTools/update_context.pl |
diffstat | 1 files changed, 112 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gearsTools/update_context.pl Thu Nov 14 10:14:15 2019 +0900 @@ -0,0 +1,112 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Getopt::Std; + +my $interface_file = shift or die "require itnerface file"; +my $h2context = parse_interface($interface_file); +my $context = dump_h2context($h2context); +my ($first,$last) = slup_context_h($h2context->{name}); + +my %opt; +getopts("w" => \%opt); + +if ($opt{w}) { + context_write(@{$first},$context,@{$last}); +} else { + context_dump(@{$first},$context,@{$last}); +} + + +sub slup_context_h { + open my $fh, '<', 'context.h'; + + my $data_gear_name = shift; + + my @first_context_headers = (); + my @last_context_headers = (); + + while (my $line = <$fh>) { + if ( $line =~ /this is necessary for context generator/) { + push(@last_context_headers, $line); + push(@last_context_headers, <$fh>); + last; + } + if ( $line =~ /struct $data_gear_name/) { + print "WARN! $data_gear_name struct already exists\n"; + exit 1; + } + push(@first_context_headers, $line); + } + + close $fh; + + print "@first_context_headers\n"; + print "@last_context_headers\n"; + return (\@first_context_headers,\@last_context_headers); +} + +sub parse_interface { + my $file_name = shift; + + open my $fh, '<', $file_name; + + my $h2context = {}; + + while (my $line = <$fh>) { + if ($line =~ /typedef struct (\w+)\s?<.*/) { + die "invalied struct name $1" unless $1; + $h2context->{name} = $1; + next; + } + + if ($line =~ m[/\*|//|}]) { + next; + } + + if ($line =~ /__code (\w+)\(.*/) { + push(@{$h2context->{codes}},$1); + next; + } + + $line =~ s/\s*([\w\s\*]+);\s*/$1/; + push(@{$h2context->{data}},$1); + } + + close $fh; + return $h2context; +} + + +sub dump_h2context { + my $h2context = shift; + my $context = ''; + my $space = ' '; + #print "${space}struct $h2context->{name} {\n"; + $context = "${space}struct $h2context->{name} {\n"; + for my $datum (@{$h2context->{data}}) { + #print "${space}${space}$datum; \n"; + $context .= "${space}${space}$datum; \n"; + } + for my $code (@{$h2context->{codes}}) { + #print "${space}${space}enum Code $code;\n"; + $context .= "${space}${space}enum Code $code;\n"; + } + #print "${space}} $h2context->{name};\n"; + $context .= "${space}} $h2context->{name};\n"; + return $context; +} + +sub context_dump { + for my $line (@_) { + print "$line"; + } +} + +sub context_write { + open my $fh, '>', "context.h"; + for my $line (@_) { + print $fh "$line"; + } + close $fh; +}