1cdf0e10cSrcweir: 2cdf0e10cSrcweir eval 'exec perl -S $0 ${1+"$@"}' 3cdf0e10cSrcweir if 0; 4*7e90fac2SAndrew Rist#************************************************************** 5cdf0e10cSrcweir# 6*7e90fac2SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 7*7e90fac2SAndrew Rist# or more contributor license agreements. See the NOTICE file 8*7e90fac2SAndrew Rist# distributed with this work for additional information 9*7e90fac2SAndrew Rist# regarding copyright ownership. The ASF licenses this file 10*7e90fac2SAndrew Rist# to you under the Apache License, Version 2.0 (the 11*7e90fac2SAndrew Rist# "License"); you may not use this file except in compliance 12*7e90fac2SAndrew Rist# with the License. You may obtain a copy of the License at 13cdf0e10cSrcweir# 14*7e90fac2SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 15cdf0e10cSrcweir# 16*7e90fac2SAndrew Rist# Unless required by applicable law or agreed to in writing, 17*7e90fac2SAndrew Rist# software distributed under the License is distributed on an 18*7e90fac2SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19*7e90fac2SAndrew Rist# KIND, either express or implied. See the License for the 20*7e90fac2SAndrew Rist# specific language governing permissions and limitations 21*7e90fac2SAndrew Rist# under the License. 22cdf0e10cSrcweir# 23*7e90fac2SAndrew Rist#************************************************************** 24*7e90fac2SAndrew Rist 25*7e90fac2SAndrew Rist 26cdf0e10cSrcweir 27cdf0e10cSrcweirsub usage() { 28cdf0e10cSrcweir print "Usage: api-to-idl.pl source.api destination_path\n"; 29cdf0e10cSrcweir print; 30cdf0e10cSrcweir print "This tool converts oovbaapi *.api files into *.idl's.\n"; 31cdf0e10cSrcweir exit 1; 32cdf0e10cSrcweir} 33cdf0e10cSrcweir 34cdf0e10cSrcweirmy $src = shift; 35cdf0e10cSrcweirmy $dest = shift; 36cdf0e10cSrcweir 37cdf0e10cSrcweirif ( !defined( $src ) || !defined( $dest ) || $src eq "-h" || $src eq "--help" ) { 38cdf0e10cSrcweir usage(); 39cdf0e10cSrcweir} 40cdf0e10cSrcweir 41cdf0e10cSrcweir# Parsing functions 42cdf0e10cSrcweirmy $state = ""; 43cdf0e10cSrcweirmy $source = ""; 44cdf0e10cSrcweirmy $name = ""; 45cdf0e10cSrcweirmy $value = ""; 46cdf0e10cSrcweir 47cdf0e10cSrcweirmy %result; 48cdf0e10cSrcweir 49cdf0e10cSrcweir# Process element start event 50cdf0e10cSrcweirsub start_element($) { 51cdf0e10cSrcweir my ($el) = @_; 52cdf0e10cSrcweir 53cdf0e10cSrcweir @element_attr = split( /\s+/, $el ); 54cdf0e10cSrcweir my $element = $element_attr[0]; 55cdf0e10cSrcweir 56cdf0e10cSrcweir if ( $element eq "element" ) { 57cdf0e10cSrcweir if ( $element_attr[1] =~ /type="?([^"]*)"?/ && $1 eq "constant" ) { 58cdf0e10cSrcweir $state = "constant"; 59cdf0e10cSrcweir $source = ""; 60cdf0e10cSrcweir $name = ""; 61cdf0e10cSrcweir $value = ""; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir } 64cdf0e10cSrcweir elsif ( $state eq "constant" && $element eq "source" ) { 65cdf0e10cSrcweir $state = "source"; 66cdf0e10cSrcweir if ( $element_attr[1] =~ /id="?([^"]*)"?/ ) { 67cdf0e10cSrcweir chomp( $source = $1 ); 68cdf0e10cSrcweir } 69cdf0e10cSrcweir } 70cdf0e10cSrcweir elsif ( $state eq "source" && $element eq "name" ) { 71cdf0e10cSrcweir $state = "name"; 72cdf0e10cSrcweir } 73cdf0e10cSrcweir elsif ( $state eq "source" && $element eq "value" ) { 74cdf0e10cSrcweir $state = "value"; 75cdf0e10cSrcweir } 76cdf0e10cSrcweir} 77cdf0e10cSrcweir 78cdf0e10cSrcweir# Process element end event 79cdf0e10cSrcweirsub end_element($) { 80cdf0e10cSrcweir my ($element) = @_; 81cdf0e10cSrcweir 82cdf0e10cSrcweir if ( $state eq "name" && $element eq "name" ) { 83cdf0e10cSrcweir $state = "source"; 84cdf0e10cSrcweir } 85cdf0e10cSrcweir elsif ( $state eq "value" && $element eq "value" ) { 86cdf0e10cSrcweir $state = "source"; 87cdf0e10cSrcweir } 88cdf0e10cSrcweir elsif ( $state ne "" && $element eq "element" ) { 89cdf0e10cSrcweir $state = ""; 90cdf0e10cSrcweir 91cdf0e10cSrcweir my @destination = split( /\./, $source ); 92cdf0e10cSrcweir my $module = shift( @destination ); 93cdf0e10cSrcweir my $type = shift( @destination ); 94cdf0e10cSrcweir 95cdf0e10cSrcweir $module =~ tr/[A-Z]/[a-z]/; 96cdf0e10cSrcweir 97cdf0e10cSrcweir $result{$module} = {} unless exists $result{$module}; 98cdf0e10cSrcweir $result{$module}{$type} = [] unless exists $result{$module}{$type}; 99cdf0e10cSrcweir 100cdf0e10cSrcweir push( @{$result{$module}{$type}}, 101cdf0e10cSrcweir { "name" => $name, "value" => $value } ); 102cdf0e10cSrcweir } 103cdf0e10cSrcweir} 104cdf0e10cSrcweir 105cdf0e10cSrcweir# Process characters 106cdf0e10cSrcweirsub characters($) { 107cdf0e10cSrcweir my ($data) = @_; 108cdf0e10cSrcweir 109cdf0e10cSrcweir if ( $state eq "name" ) { 110cdf0e10cSrcweir chomp( $name = $data ); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir elsif ( $state eq "value" ) { 113cdf0e10cSrcweir chomp( $value = $data ); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir} 116cdf0e10cSrcweir 117cdf0e10cSrcweir# Create idls from the parsed data 118cdf0e10cSrcweirsub generate_idls($) { 119cdf0e10cSrcweir my ($path) = @_; 120cdf0e10cSrcweir 121cdf0e10cSrcweir foreach $module ( keys %result ) { 122cdf0e10cSrcweir foreach $type ( keys %{$result{$module}} ) { 123cdf0e10cSrcweir my $fname = $path . "/" . $type . ".idl"; 124cdf0e10cSrcweir open( IDL, ">$fname" ) || die "Cannot write $fname."; 125cdf0e10cSrcweir 126cdf0e10cSrcweir if( $module eq "vba" ) { 127cdf0e10cSrcweir print IDL "module ooo { module $module {\n"; 128cdf0e10cSrcweir } 129cdf0e10cSrcweir else { 130cdf0e10cSrcweir print IDL "module ooo { module vba { module $module {\n"; 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir print IDL " constants $type {\n"; 134cdf0e10cSrcweir foreach $constant ( @{$result{$module}{$type}} ) { 135cdf0e10cSrcweir print IDL " const long $constant->{'name'} = $constant->{'value'};\n"; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir if( $module eq "vba" ) { 138cdf0e10cSrcweir print IDL " };\n}; };\n"; 139cdf0e10cSrcweir } 140cdf0e10cSrcweir else { 141cdf0e10cSrcweir print IDL " };\n}; }; };\n"; 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir close( IDL ); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir } 147cdf0e10cSrcweir} 148cdf0e10cSrcweir 149cdf0e10cSrcweir# Parse the input 150cdf0e10cSrcweiropen( IN, "<$src" ) || die "Cannot open $src."; 151cdf0e10cSrcweir 152cdf0e10cSrcweirmy $in_comment = 0; 153cdf0e10cSrcweirmy $line = ""; 154cdf0e10cSrcweirwhile (<IN>) { 155cdf0e10cSrcweir # ignore comments 156cdf0e10cSrcweir s/<!--[^>]*-->//g; 157cdf0e10cSrcweir if ( /<!--/ ) { 158cdf0e10cSrcweir $in_comment = 1; 159cdf0e10cSrcweir s/<!--.*//; 160cdf0e10cSrcweir } 161cdf0e10cSrcweir elsif ( /-->/ && $in_comment ) { 162cdf0e10cSrcweir $in_comment = 0; 163cdf0e10cSrcweir s/.*-->//; 164cdf0e10cSrcweir } 165cdf0e10cSrcweir elsif ( $in_comment ) { 166cdf0e10cSrcweir next; 167cdf0e10cSrcweir } 168cdf0e10cSrcweir # ignore empty lines 169cdf0e10cSrcweir chomp; 170cdf0e10cSrcweir s/^\s*//; 171cdf0e10cSrcweir s/\s*$//; 172cdf0e10cSrcweir next if ( $_ eq "" ); 173cdf0e10cSrcweir 174cdf0e10cSrcweir # take care of lines where element continues 175cdf0e10cSrcweir if ( $line ne "" ) { 176cdf0e10cSrcweir $line .= " " . $_; 177cdf0e10cSrcweir } 178cdf0e10cSrcweir else { 179cdf0e10cSrcweir $line = $_; 180cdf0e10cSrcweir } 181cdf0e10cSrcweir next if ( !/>$/ ); 182cdf0e10cSrcweir 183cdf0e10cSrcweir # the actual parsing 184cdf0e10cSrcweir my @starts = split( /</, $line ); 185cdf0e10cSrcweir $line = ""; 186cdf0e10cSrcweir foreach $start ( @starts ) { 187cdf0e10cSrcweir next if ( $start eq "" ); 188cdf0e10cSrcweir 189cdf0e10cSrcweir @ends = split( />/, $start ); 190cdf0e10cSrcweir my $element = $ends[0]; 191cdf0e10cSrcweir my $data = $ends[1]; 192cdf0e10cSrcweir 193cdf0e10cSrcweir # start or end element 194cdf0e10cSrcweir if ( $element =~ /^\/(.*)/ ) { 195cdf0e10cSrcweir end_element( $1 ); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir else { 198cdf0e10cSrcweir start_element( $element ); 199cdf0e10cSrcweir } 200cdf0e10cSrcweir 201cdf0e10cSrcweir # the data 202cdf0e10cSrcweir characters( $data ); 203cdf0e10cSrcweir } 204cdf0e10cSrcweir} 205cdf0e10cSrcweirclose( IN ); 206cdf0e10cSrcweir 207cdf0e10cSrcweir# Generate the output 208cdf0e10cSrcweirgenerate_idls($dest); 209