2006-03-16 23:02:31 +03:00
###################################################
# Common Samba4 functions
# Copyright jelmer@samba.org 2006
# released under the GNU GPL
package Parse::Pidl::Samba4 ;
require Exporter ;
@ ISA = qw( Exporter ) ;
2008-12-12 13:16:36 +03:00
@ EXPORT = qw( is_intree choose_header NumStars ElementStars ArrayBrackets DeclLong ArrayDynamicallyAllocated ) ;
2006-03-16 23:02:31 +03:00
2006-05-14 02:03:44 +04:00
use Parse::Pidl::Util qw( has_property is_constant ) ;
2007-08-17 17:01:51 +04:00
use Parse::Pidl::NDR qw( GetNextLevel ) ;
2007-02-18 21:44:56 +03:00
use Parse::Pidl::Typelist qw( mapTypeName scalar_is_reference ) ;
2008-12-16 20:02:14 +03:00
use Parse::Pidl qw( fatal error ) ;
2006-03-16 23:02:31 +03:00
use strict ;
use vars qw( $VERSION ) ;
$ VERSION = '0.01' ;
2011-02-03 05:05:18 +03:00
# return true if we are using pidl within the samba source tree. This changes
# the names of include files, as some include files (such as ntstatus.h) have
# different paths when installed to the patch in the source tree
2006-03-16 23:02:31 +03:00
sub is_intree ()
{
2007-04-02 21:06:14 +04:00
my $ srcdir = $ ENV { srcdir } ;
$ srcdir = $ srcdir ? "$srcdir/" : "" ;
2011-02-03 05:05:18 +03:00
return 1 if ( - f "${srcdir}kdc/kdc.c" ) ;
return 1 if ( - d "${srcdir}source4" ) ;
return 1 if ( - f "${srcdir}include/smb.h" ) ;
2006-09-08 00:01:17 +04:00
return 0 ;
2006-03-16 23:02:31 +03:00
}
# Return an #include line depending on whether this build is an in-tree
# build or not.
sub choose_header ($$)
{
my ( $ in , $ out ) = @ _ ;
return "#include \"$in\"" if ( is_intree ( ) ) ;
return "#include <$out>" ;
}
2008-12-12 13:16:36 +03:00
sub ArrayDynamicallyAllocated ($$)
{
my ( $ e , $ l ) = @ _ ;
die ( "Not an array" ) unless ( $ l - > { TYPE } eq "ARRAY" ) ;
return 0 if ( $ l - > { IS_FIXED } and not has_property ( $ e , "charset" ) ) ;
return 1 ;
}
2007-08-17 17:01:51 +04:00
sub NumStars ( $ ; $ )
{
my ( $ e , $ d ) = @ _ ;
$ d = 0 unless defined ( $ d ) ;
my $ n = 0 ;
foreach my $ l ( @ { $ e - > { LEVELS } } ) {
next unless ( $ l - > { TYPE } eq "POINTER" ) ;
my $ nl = GetNextLevel ( $ e , $ l ) ;
next if ( defined ( $ nl ) and $ nl - > { TYPE } eq "ARRAY" ) ;
$ n + + ;
}
if ( $ n >= 1 ) {
$ n - - if ( scalar_is_reference ( $ e - > { TYPE } ) ) ;
}
foreach my $ l ( @ { $ e - > { LEVELS } } ) {
next unless ( $ l - > { TYPE } eq "ARRAY" ) ;
2008-12-12 13:16:36 +03:00
next unless ( ArrayDynamicallyAllocated ( $ e , $ l ) ) ;
2007-08-17 17:01:51 +04:00
$ n + + ;
}
2008-12-16 20:02:14 +03:00
error ( $ e - > { ORIGINAL } , "Too few pointers $n < $d" ) if ( $ n < $ d ) ;
2007-08-17 17:01:51 +04:00
$ n -= $ d ;
return $ n ;
}
sub ElementStars ( $ ; $ )
{
my ( $ e , $ d ) = @ _ ;
my $ res = "" ;
my $ n = 0 ;
$ n = NumStars ( $ e , $ d ) ;
$ res . = "*" foreach ( 1 .. $ n ) ;
return $ res ;
}
sub ArrayBrackets ($)
{
my ( $ e ) = @ _ ;
my $ res = "" ;
foreach my $ l ( @ { $ e - > { LEVELS } } ) {
next unless ( $ l - > { TYPE } eq "ARRAY" ) ;
2008-12-12 13:16:36 +03:00
next if ArrayDynamicallyAllocated ( $ e , $ l ) ;
2007-08-17 17:01:51 +04:00
$ res . = "[$l->{SIZE_IS}]" ;
}
return $ res ;
}
2009-07-31 10:58:36 +04:00
sub DeclLong ( $ ; $ )
2006-11-19 20:56:35 +03:00
{
2009-07-31 10:58:36 +04:00
my ( $ e , $ p ) = @ _ ;
2007-08-17 17:01:51 +04:00
my $ res = "" ;
2009-07-31 10:58:36 +04:00
$ p = "" unless defined ( $ p ) ;
2006-05-14 02:03:44 +04:00
2007-08-17 17:01:51 +04:00
if ( has_property ( $ e , "represent_as" ) ) {
$ res . = mapTypeName ( $ e - > { PROPERTIES } - > { represent_as } ) . " " ;
2006-05-14 02:03:44 +04:00
} else {
2007-08-17 17:01:51 +04:00
if ( has_property ( $ e , "charset" ) ) {
$ res . = "const char " ;
2006-05-14 02:03:44 +04:00
} else {
2007-08-17 17:01:51 +04:00
$ res . = mapTypeName ( $ e - > { TYPE } ) . " " ;
2006-05-14 02:03:44 +04:00
}
2007-08-17 17:01:51 +04:00
$ res . = ElementStars ( $ e ) ;
2006-05-14 02:03:44 +04:00
}
2009-07-31 10:58:36 +04:00
$ res . = $ p . $ e - > { NAME } ;
2007-08-17 17:01:51 +04:00
$ res . = ArrayBrackets ( $ e ) ;
2006-05-14 02:03:44 +04:00
2007-08-17 17:01:51 +04:00
return $ res ;
2006-05-14 02:03:44 +04:00
}
2006-03-16 23:02:31 +03:00
1 ;