| 1 |
################################################### |
| 2 |
# create C header files for an IDL structure |
| 3 |
# Copyright [email protected] 2000 |
| 4 |
# Copyright [email protected] 2005 |
| 5 |
# released under the GNU GPL |
| 6 |
|
| 7 |
package Parse::Pidl::Samba4::Header; |
| 8 |
require Exporter; |
| 9 |
|
| 10 |
@ISA = qw(Exporter); |
| 11 |
@EXPORT_OK = qw(GenerateFunctionInEnv GenerateFunctionOutEnv EnvSubstituteValue GenerateStructEnv); |
| 12 |
|
| 13 |
use strict; |
| 14 |
use warnings; |
| 15 |
use Parse::Pidl qw(fatal); |
| 16 |
use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference scalarTypeUsed); |
| 17 |
use Parse::Pidl::Util qw(has_property is_constant unmake_str ParseExpr); |
| 18 |
use Parse::Pidl::Samba4 qw(is_intree ElementStars ArrayBrackets choose_header); |
| 19 |
|
| 20 |
use vars qw($VERSION); |
| 21 |
$VERSION = '0.01'; |
| 22 |
|
| 23 |
my($res); |
| 24 |
my($tab_depth); |
| 25 |
|
| 26 |
sub pidl($) { $res .= shift; } |
| 27 |
|
| 28 |
sub tabs() |
| 29 |
{ |
| 30 |
my $res = ""; |
| 31 |
$res .="\t" foreach (1..$tab_depth); |
| 32 |
return $res; |
| 33 |
} |
| 34 |
|
| 35 |
##################################################################### |
| 36 |
# parse a properties list |
| 37 |
sub HeaderProperties($$) |
| 38 |
{ |
| 39 |
my($props,$ignores) = @_; |
| 40 |
my $ret = ""; |
| 41 |
|
| 42 |
foreach my $d (sort(keys %{$props})) { |
| 43 |
next if (grep(/^$d$/, @$ignores)); |
| 44 |
if($props->{$d} ne "1") { |
| 45 |
$ret.= "$d($props->{$d}),"; |
| 46 |
} else { |
| 47 |
$ret.="$d,"; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ($ret) { |
| 52 |
pidl "/* [" . substr($ret, 0, -1) . "] */"; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
##################################################################### |
| 57 |
# parse a structure element |
| 58 |
sub HeaderElement($) |
| 59 |
{ |
| 60 |
my($element) = shift; |
| 61 |
|
| 62 |
pidl tabs(); |
| 63 |
if (has_property($element, "represent_as")) { |
| 64 |
pidl mapTypeName($element->{PROPERTIES}->{represent_as})." "; |
| 65 |
} else { |
| 66 |
if (ref($element->{TYPE}) eq "HASH") { |
| 67 |
HeaderType($element, $element->{TYPE}, $element->{TYPE}->{NAME}); |
| 68 |
} else { |
| 69 |
HeaderType($element, $element->{TYPE}, ""); |
| 70 |
} |
| 71 |
pidl " ".ElementStars($element); |
| 72 |
} |
| 73 |
pidl $element->{NAME}; |
| 74 |
pidl ArrayBrackets($element); |
| 75 |
|
| 76 |
pidl ";"; |
| 77 |
if (defined $element->{PROPERTIES}) { |
| 78 |
HeaderProperties($element->{PROPERTIES}, ["in", "out"]); |
| 79 |
} |
| 80 |
pidl "\n"; |
| 81 |
} |
| 82 |
|
| 83 |
##################################################################### |
| 84 |
# parse a struct |
| 85 |
sub HeaderStruct($$;$) |
| 86 |
{ |
| 87 |
my($struct,$name,$tail) = @_; |
| 88 |
pidl "struct $name"; |
| 89 |
pidl $tail if defined($tail) and not defined($struct->{ELEMENTS}); |
| 90 |
return if (not defined($struct->{ELEMENTS})); |
| 91 |
pidl " {\n"; |
| 92 |
$tab_depth++; |
| 93 |
my $el_count=0; |
| 94 |
foreach (@{$struct->{ELEMENTS}}) { |
| 95 |
HeaderElement($_); |
| 96 |
$el_count++; |
| 97 |
} |
| 98 |
if ($el_count == 0) { |
| 99 |
# some compilers can't handle empty structures |
| 100 |
pidl tabs()."char _empty_;\n"; |
| 101 |
} |
| 102 |
$tab_depth--; |
| 103 |
pidl tabs()."}"; |
| 104 |
if (defined $struct->{PROPERTIES}) { |
| 105 |
HeaderProperties($struct->{PROPERTIES}, []); |
| 106 |
} |
| 107 |
pidl $tail if defined($tail); |
| 108 |
} |
| 109 |
|
| 110 |
##################################################################### |
| 111 |
# parse a enum |
| 112 |
sub HeaderEnum($$;$) |
| 113 |
{ |
| 114 |
my($enum,$name,$tail) = @_; |
| 115 |
my $first = 1; |
| 116 |
|
| 117 |
pidl "enum $name"; |
| 118 |
if (defined($enum->{ELEMENTS})) { |
| 119 |
pidl "\n#ifndef USE_UINT_ENUMS\n"; |
| 120 |
pidl " {\n"; |
| 121 |
$tab_depth++; |
| 122 |
foreach my $e (@{$enum->{ELEMENTS}}) { |
| 123 |
my @enum_els = (); |
| 124 |
unless ($first) { pidl ",\n"; } |
| 125 |
$first = 0; |
| 126 |
pidl tabs(); |
| 127 |
@enum_els = split(/=/, $e); |
| 128 |
if (@enum_els == 2) { |
| 129 |
pidl $enum_els[0]; |
| 130 |
pidl "=(int)"; |
| 131 |
pidl "("; |
| 132 |
pidl $enum_els[1]; |
| 133 |
pidl ")"; |
| 134 |
} else { |
| 135 |
pidl $e; |
| 136 |
} |
| 137 |
} |
| 138 |
pidl "\n"; |
| 139 |
$tab_depth--; |
| 140 |
pidl "}"; |
| 141 |
pidl "\n"; |
| 142 |
pidl "#else\n"; |
| 143 |
my $count = 0; |
| 144 |
my $with_val = 0; |
| 145 |
my $without_val = 0; |
| 146 |
pidl " { __do_not_use_enum_$name=INT_MAX}\n"; |
| 147 |
foreach my $e (@{$enum->{ELEMENTS}}) { |
| 148 |
my $t = "$e"; |
| 149 |
my $name; |
| 150 |
my $value; |
| 151 |
if ($t =~ /(.*)=(.*)/) { |
| 152 |
$name = $1; |
| 153 |
$value = $2; |
| 154 |
$with_val = 1; |
| 155 |
fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!") |
| 156 |
unless ($without_val == 0); |
| 157 |
} else { |
| 158 |
$name = $t; |
| 159 |
$value = $count++; |
| 160 |
$without_val = 1; |
| 161 |
fatal($e->{ORIGINAL}, "you can't mix enum member with values and without values!") |
| 162 |
unless ($with_val == 0); |
| 163 |
} |
| 164 |
pidl "#define $name ( $value )\n"; |
| 165 |
} |
| 166 |
pidl "#endif\n"; |
| 167 |
} |
| 168 |
pidl $tail if defined($tail); |
| 169 |
} |
| 170 |
|
| 171 |
##################################################################### |
| 172 |
# parse a bitmap |
| 173 |
sub HeaderBitmap($$) |
| 174 |
{ |
| 175 |
my($bitmap,$name) = @_; |
| 176 |
|
| 177 |
return unless defined($bitmap->{ELEMENTS}); |
| 178 |
|
| 179 |
pidl "/* bitmap $name */\n"; |
| 180 |
pidl "#define $_\n" foreach (@{$bitmap->{ELEMENTS}}); |
| 181 |
pidl "\n"; |
| 182 |
} |
| 183 |
|
| 184 |
##################################################################### |
| 185 |
# parse a union |
| 186 |
sub HeaderUnion($$;$) |
| 187 |
{ |
| 188 |
my($union,$name,$tail) = @_; |
| 189 |
my %done = (); |
| 190 |
|
| 191 |
pidl "union $name"; |
| 192 |
pidl $tail if defined($tail) and not defined($union->{ELEMENTS}); |
| 193 |
return if (not defined($union->{ELEMENTS})); |
| 194 |
pidl " {\n"; |
| 195 |
$tab_depth++; |
| 196 |
my $needed = 0; |
| 197 |
foreach my $e (@{$union->{ELEMENTS}}) { |
| 198 |
if ($e->{TYPE} ne "EMPTY") { |
| 199 |
if (! defined $done{$e->{NAME}}) { |
| 200 |
HeaderElement($e); |
| 201 |
} |
| 202 |
$done{$e->{NAME}} = 1; |
| 203 |
$needed++; |
| 204 |
} |
| 205 |
} |
| 206 |
if (!$needed) { |
| 207 |
# sigh - some compilers don't like empty structures |
| 208 |
pidl tabs()."int _dummy_element;\n"; |
| 209 |
} |
| 210 |
$tab_depth--; |
| 211 |
pidl "}"; |
| 212 |
|
| 213 |
if (defined $union->{PROPERTIES}) { |
| 214 |
HeaderProperties($union->{PROPERTIES}, []); |
| 215 |
} |
| 216 |
pidl $tail if defined($tail); |
| 217 |
} |
| 218 |
|
| 219 |
##################################################################### |
| 220 |
# parse a pipe |
| 221 |
sub HeaderPipe($$;$) |
| 222 |
{ |
| 223 |
my($pipe,$name,$tail) = @_; |
| 224 |
|
| 225 |
my $struct = $pipe->{DATA}; |
| 226 |
my $e = $struct->{ELEMENTS}[1]; |
| 227 |
|
| 228 |
pidl "struct $name;\n"; |
| 229 |
pidl "struct $struct->{NAME} {\n"; |
| 230 |
$tab_depth++; |
| 231 |
pidl tabs()."uint32_t count;\n"; |
| 232 |
pidl tabs().mapTypeName($e->{TYPE})." *array;\n"; |
| 233 |
$tab_depth--; |
| 234 |
pidl "}"; |
| 235 |
|
| 236 |
if (defined $struct->{PROPERTIES}) { |
| 237 |
HeaderProperties($struct->{PROPERTIES}, []); |
| 238 |
} |
| 239 |
|
| 240 |
pidl $tail if defined($tail); |
| 241 |
} |
| 242 |
|
| 243 |
##################################################################### |
| 244 |
# parse a type |
| 245 |
sub HeaderType($$$;$) |
| 246 |
{ |
| 247 |
my($e,$data,$name,$tail) = @_; |
| 248 |
if (ref($data) eq "HASH") { |
| 249 |
($data->{TYPE} eq "ENUM") && HeaderEnum($data, $name, $tail); |
| 250 |
($data->{TYPE} eq "BITMAP") && HeaderBitmap($data, $name); |
| 251 |
($data->{TYPE} eq "STRUCT") && HeaderStruct($data, $name, $tail); |
| 252 |
($data->{TYPE} eq "UNION") && HeaderUnion($data, $name, $tail); |
| 253 |
($data->{TYPE} eq "PIPE") && HeaderPipe($data, $name, $tail); |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
if (has_property($e, "charset")) { |
| 258 |
pidl "const char"; |
| 259 |
} else { |
| 260 |
pidl mapTypeName($e->{TYPE}); |
| 261 |
} |
| 262 |
pidl $tail if defined($tail); |
| 263 |
} |
| 264 |
|
| 265 |
##################################################################### |
| 266 |
# parse a typedef |
| 267 |
sub HeaderTypedef($;$) |
| 268 |
{ |
| 269 |
my($typedef,$tail) = @_; |
| 270 |
# Don't print empty "enum foo;", since some compilers don't like it. |
| 271 |
return if ($typedef->{DATA}->{TYPE} eq "ENUM" and not defined($typedef->{DATA}->{ELEMENTS})); |
| 272 |
HeaderType($typedef, $typedef->{DATA}, $typedef->{NAME}, $tail) if defined ($typedef->{DATA}); |
| 273 |
} |
| 274 |
|
| 275 |
##################################################################### |
| 276 |
# parse a const |
| 277 |
sub HeaderConst($) |
| 278 |
{ |
| 279 |
my($const) = shift; |
| 280 |
if (!defined($const->{ARRAY_LEN}[0])) { |
| 281 |
pidl "#define $const->{NAME}\t( $const->{VALUE} )\n"; |
| 282 |
} else { |
| 283 |
pidl "#define $const->{NAME}\t $const->{VALUE}\n"; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
sub ElementDirection($) |
| 288 |
{ |
| 289 |
my ($e) = @_; |
| 290 |
|
| 291 |
return "inout" if (has_property($e, "in") and has_property($e, "out")); |
| 292 |
return "in" if (has_property($e, "in")); |
| 293 |
return "out" if (has_property($e, "out")); |
| 294 |
return "inout"; |
| 295 |
} |
| 296 |
|
| 297 |
##################################################################### |
| 298 |
# parse a function |
| 299 |
sub HeaderFunctionInOut($$) |
| 300 |
{ |
| 301 |
my($fn,$prop) = @_; |
| 302 |
|
| 303 |
return unless defined($fn->{ELEMENTS}); |
| 304 |
|
| 305 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 306 |
HeaderElement($e) if (ElementDirection($e) eq $prop); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
##################################################################### |
| 311 |
# determine if we need an "in" or "out" section |
| 312 |
sub HeaderFunctionInOut_needed($$) |
| 313 |
{ |
| 314 |
my($fn,$prop) = @_; |
| 315 |
|
| 316 |
return 1 if ($prop eq "out" && defined($fn->{RETURN_TYPE})); |
| 317 |
|
| 318 |
return undef unless defined($fn->{ELEMENTS}); |
| 319 |
|
| 320 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 321 |
return 1 if (ElementDirection($e) eq $prop); |
| 322 |
} |
| 323 |
|
| 324 |
return undef; |
| 325 |
} |
| 326 |
|
| 327 |
my %headerstructs; |
| 328 |
|
| 329 |
##################################################################### |
| 330 |
# parse a function |
| 331 |
sub HeaderFunction($) |
| 332 |
{ |
| 333 |
my($fn) = shift; |
| 334 |
|
| 335 |
return if ($headerstructs{$fn->{NAME}}); |
| 336 |
|
| 337 |
$headerstructs{$fn->{NAME}} = 1; |
| 338 |
|
| 339 |
pidl "\nstruct $fn->{NAME} {\n"; |
| 340 |
$tab_depth++; |
| 341 |
my $needed = 0; |
| 342 |
|
| 343 |
if (HeaderFunctionInOut_needed($fn, "in") or |
| 344 |
HeaderFunctionInOut_needed($fn, "inout")) { |
| 345 |
pidl tabs()."struct {\n"; |
| 346 |
$tab_depth++; |
| 347 |
HeaderFunctionInOut($fn, "in"); |
| 348 |
HeaderFunctionInOut($fn, "inout"); |
| 349 |
$tab_depth--; |
| 350 |
pidl tabs()."} in;\n\n"; |
| 351 |
$needed++; |
| 352 |
} |
| 353 |
|
| 354 |
if (HeaderFunctionInOut_needed($fn, "out") or |
| 355 |
HeaderFunctionInOut_needed($fn, "inout")) { |
| 356 |
pidl tabs()."struct {\n"; |
| 357 |
$tab_depth++; |
| 358 |
HeaderFunctionInOut($fn, "out"); |
| 359 |
HeaderFunctionInOut($fn, "inout"); |
| 360 |
if (defined($fn->{RETURN_TYPE})) { |
| 361 |
pidl tabs().mapTypeName($fn->{RETURN_TYPE}) . " result;\n"; |
| 362 |
} |
| 363 |
$tab_depth--; |
| 364 |
pidl tabs()."} out;\n\n"; |
| 365 |
$needed++; |
| 366 |
} |
| 367 |
|
| 368 |
if (!$needed) { |
| 369 |
# sigh - some compilers don't like empty structures |
| 370 |
pidl tabs()."int _dummy_element;\n"; |
| 371 |
} |
| 372 |
|
| 373 |
$tab_depth--; |
| 374 |
pidl "};\n\n"; |
| 375 |
} |
| 376 |
|
| 377 |
sub HeaderImport |
| 378 |
{ |
| 379 |
my @imports = @_; |
| 380 |
foreach my $import (@imports) { |
| 381 |
$import = unmake_str($import); |
| 382 |
$import =~ s/\.idl$//; |
| 383 |
pidl choose_header("librpc/gen_ndr/$import\.h", "gen_ndr/$import.h") . "\n"; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
sub HeaderInclude |
| 388 |
{ |
| 389 |
my @includes = @_; |
| 390 |
foreach (@includes) { |
| 391 |
pidl "#include $_\n"; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
##################################################################### |
| 396 |
# parse the interface definitions |
| 397 |
sub HeaderInterface($) |
| 398 |
{ |
| 399 |
my($interface) = shift; |
| 400 |
|
| 401 |
pidl "#ifndef _HEADER_$interface->{NAME}\n"; |
| 402 |
pidl "#define _HEADER_$interface->{NAME}\n\n"; |
| 403 |
|
| 404 |
if (has_property($interface, "headerhelper")) { |
| 405 |
foreach my $h (split /,/, $interface->{PROPERTIES}->{headerhelper}) { |
| 406 |
pidl("#include $h\n"); |
| 407 |
} |
| 408 |
pidl("\n"); |
| 409 |
} |
| 410 |
|
| 411 |
foreach my $c (@{$interface->{CONSTS}}) { |
| 412 |
HeaderConst($c); |
| 413 |
} |
| 414 |
|
| 415 |
foreach my $t (@{$interface->{TYPES}}) { |
| 416 |
HeaderTypedef($t, ";\n\n") if ($t->{TYPE} eq "TYPEDEF"); |
| 417 |
HeaderStruct($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "STRUCT"); |
| 418 |
HeaderUnion($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "UNION"); |
| 419 |
HeaderEnum($t, $t->{NAME}, ";\n\n") if ($t->{TYPE} eq "ENUM"); |
| 420 |
HeaderBitmap($t, $t->{NAME}) if ($t->{TYPE} eq "BITMAP"); |
| 421 |
HeaderPipe($t, $t->{NAME}, "\n\n") if ($t->{TYPE} eq "PIPE"); |
| 422 |
} |
| 423 |
|
| 424 |
foreach my $fn (@{$interface->{FUNCTIONS}}) { |
| 425 |
HeaderFunction($fn); |
| 426 |
} |
| 427 |
|
| 428 |
pidl "#endif /* _HEADER_$interface->{NAME} */\n"; |
| 429 |
} |
| 430 |
|
| 431 |
sub HeaderQuote($) |
| 432 |
{ |
| 433 |
my($quote) = shift; |
| 434 |
|
| 435 |
pidl unmake_str($quote->{DATA}) . "\n"; |
| 436 |
} |
| 437 |
|
| 438 |
##################################################################### |
| 439 |
# parse a parsed IDL into a C header |
| 440 |
sub Parse($) |
| 441 |
{ |
| 442 |
my($ndr) = shift; |
| 443 |
$tab_depth = 0; |
| 444 |
|
| 445 |
$res = ""; |
| 446 |
%headerstructs = (); |
| 447 |
|
| 448 |
my $ifacename = ""; |
| 449 |
|
| 450 |
# work out a unique interface name |
| 451 |
foreach (@{$ndr}) { |
| 452 |
if ($_->{TYPE} eq "INTERFACE") { |
| 453 |
$ifacename = $_->{NAME}; |
| 454 |
last; |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
foreach (@{$ndr}) { |
| 459 |
($_->{TYPE} eq "CPP_QUOTE") && HeaderQuote($_); |
| 460 |
($_->{TYPE} eq "INTERFACE") && HeaderInterface($_); |
| 461 |
($_->{TYPE} eq "IMPORT") && HeaderImport(@{$_->{PATHS}}); |
| 462 |
($_->{TYPE} eq "INCLUDE") && HeaderInclude(@{$_->{PATHS}}); |
| 463 |
} |
| 464 |
|
| 465 |
my $res2 = $res; |
| 466 |
$res = ""; |
| 467 |
pidl "/* header auto-generated by pidl */\n\n"; |
| 468 |
|
| 469 |
pidl "#ifndef _PIDL_HEADER_$ifacename\n"; |
| 470 |
pidl "#define _PIDL_HEADER_$ifacename\n\n"; |
| 471 |
|
| 472 |
if (!is_intree()) { |
| 473 |
pidl "#include <util/data_blob.h>\n"; |
| 474 |
} |
| 475 |
pidl "#include <stdint.h>\n"; |
| 476 |
pidl "\n"; |
| 477 |
if (scalarTypeUsed("NTSTATUS")) { |
| 478 |
pidl choose_header("libcli/util/ntstatus.h", "core/ntstatus.h") . "\n"; |
| 479 |
} |
| 480 |
pidl "\n"; |
| 481 |
|
| 482 |
$res .= $res2; |
| 483 |
|
| 484 |
pidl "#endif /* _PIDL_HEADER_$ifacename */\n"; |
| 485 |
|
| 486 |
return $res; |
| 487 |
} |
| 488 |
|
| 489 |
sub GenerateStructEnv($$) |
| 490 |
{ |
| 491 |
my ($x, $v) = @_; |
| 492 |
my %env; |
| 493 |
|
| 494 |
foreach my $e (@{$x->{ELEMENTS}}) { |
| 495 |
$env{$e->{NAME}} = "$v->$e->{NAME}"; |
| 496 |
} |
| 497 |
|
| 498 |
$env{"this"} = $v; |
| 499 |
|
| 500 |
return \%env; |
| 501 |
} |
| 502 |
|
| 503 |
sub EnvSubstituteValue($$) |
| 504 |
{ |
| 505 |
my ($env,$s) = @_; |
| 506 |
|
| 507 |
# Substitute the value() values in the env |
| 508 |
foreach my $e (@{$s->{ELEMENTS}}) { |
| 509 |
next unless (defined(my $v = has_property($e, "value"))); |
| 510 |
|
| 511 |
$env->{$e->{NAME}} = ParseExpr($v, $env, $e); |
| 512 |
} |
| 513 |
|
| 514 |
return $env; |
| 515 |
} |
| 516 |
|
| 517 |
sub GenerateFunctionInEnv($;$) |
| 518 |
{ |
| 519 |
my ($fn, $base) = @_; |
| 520 |
my %env; |
| 521 |
|
| 522 |
$base = "r->" unless defined($base); |
| 523 |
|
| 524 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 525 |
if (grep (/in/, @{$e->{DIRECTION}})) { |
| 526 |
$env{$e->{NAME}} = $base."in.$e->{NAME}"; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
return \%env; |
| 531 |
} |
| 532 |
|
| 533 |
sub GenerateFunctionOutEnv($;$) |
| 534 |
{ |
| 535 |
my ($fn, $base) = @_; |
| 536 |
my %env; |
| 537 |
|
| 538 |
$base = "r->" unless defined($base); |
| 539 |
|
| 540 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 541 |
if (grep (/out/, @{$e->{DIRECTION}})) { |
| 542 |
$env{$e->{NAME}} = $base."out.$e->{NAME}"; |
| 543 |
} elsif (grep (/in/, @{$e->{DIRECTION}})) { |
| 544 |
$env{$e->{NAME}} = $base."in.$e->{NAME}"; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
return \%env; |
| 549 |
} |
| 550 |
|
| 551 |
1; |