| 1 |
################################################### |
| 2 |
# Trivial Parser Generator |
| 3 |
# Copyright [email protected] 2005-2007 |
| 4 |
# released under the GNU GPL |
| 5 |
|
| 6 |
package Parse::Pidl::Samba4::TDR; |
| 7 |
use Parse::Pidl qw(fatal); |
| 8 |
use Parse::Pidl::Util qw(has_property ParseExpr is_constant); |
| 9 |
use Parse::Pidl::Samba4 qw(is_intree choose_header); |
| 10 |
use Parse::Pidl::Typelist qw(mapTypeName); |
| 11 |
|
| 12 |
use base Parse::Pidl::Base; |
| 13 |
|
| 14 |
use vars qw($VERSION); |
| 15 |
$VERSION = '0.01'; |
| 16 |
|
| 17 |
use strict; |
| 18 |
use warnings; |
| 19 |
|
| 20 |
sub new($) { |
| 21 |
my ($class) = shift; |
| 22 |
my $self = { res => "", res_hdr => "", tabs => "" }; |
| 23 |
bless($self, $class); |
| 24 |
} |
| 25 |
|
| 26 |
sub typearg($) { |
| 27 |
my $t = shift; |
| 28 |
return(", const char *name") if ($t eq "print"); |
| 29 |
return(", TALLOC_CTX *mem_ctx") if ($t eq "pull"); |
| 30 |
return(""); |
| 31 |
} |
| 32 |
|
| 33 |
sub fn_declare($$$) |
| 34 |
{ |
| 35 |
my ($self, $p, $d) = @_; |
| 36 |
if ($p) { |
| 37 |
$self->pidl($d); $self->pidl_hdr("$d;"); |
| 38 |
} else { |
| 39 |
$self->pidl("static $d"); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
sub ContainsArray($) |
| 44 |
{ |
| 45 |
my $e = shift; |
| 46 |
foreach (@{$e->{ELEMENTS}}) { |
| 47 |
next if (has_property($_, "charset") and |
| 48 |
scalar(@{$_->{ARRAY_LEN}}) == 1); |
| 49 |
return 1 if (defined($_->{ARRAY_LEN}) and |
| 50 |
scalar(@{$_->{ARRAY_LEN}}) > 0); |
| 51 |
} |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
sub ParserElement($$$$) |
| 56 |
{ |
| 57 |
my ($self, $e,$t,$env) = @_; |
| 58 |
my $switch = ""; |
| 59 |
my $array = ""; |
| 60 |
my $name = ""; |
| 61 |
my $mem_ctx = "mem_ctx"; |
| 62 |
|
| 63 |
fatal($e,"Pointers not supported in TDR") if ($e->{POINTERS} > 0); |
| 64 |
fatal($e,"size_is() not supported in TDR") if (has_property($e, "size_is")); |
| 65 |
fatal($e,"length_is() not supported in TDR") if (has_property($e, "length_is")); |
| 66 |
|
| 67 |
if ($t eq "print") { |
| 68 |
$name = ", \"$e->{NAME}\"$array"; |
| 69 |
} |
| 70 |
|
| 71 |
if (has_property($e, "flag")) { |
| 72 |
$self->pidl("{"); |
| 73 |
$self->indent; |
| 74 |
$self->pidl("uint32_t saved_flags = tdr->flags;"); |
| 75 |
$self->pidl("tdr->flags |= $e->{PROPERTIES}->{flag};"); |
| 76 |
} |
| 77 |
|
| 78 |
if (has_property($e, "charset")) { |
| 79 |
fatal($e,"charset() on non-array element") unless (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0); |
| 80 |
|
| 81 |
my $len = ParseExpr(@{$e->{ARRAY_LEN}}[0], $env, $e); |
| 82 |
if ($len eq "*") { $len = "-1"; } |
| 83 |
$name = ", mem_ctx" if ($t eq "pull"); |
| 84 |
$self->pidl("TDR_CHECK(tdr_$t\_charset(tdr$name, &v->$e->{NAME}, $len, sizeof($e->{TYPE}_t), CH_$e->{PROPERTIES}->{charset}));"); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if (has_property($e, "switch_is")) { |
| 89 |
$switch = ", " . ParseExpr($e->{PROPERTIES}->{switch_is}, $env, $e); |
| 90 |
} |
| 91 |
|
| 92 |
if (defined($e->{ARRAY_LEN}) and scalar(@{$e->{ARRAY_LEN}}) > 0) { |
| 93 |
my $len = ParseExpr($e->{ARRAY_LEN}[0], $env, $e); |
| 94 |
|
| 95 |
if ($t eq "pull" and not is_constant($len)) { |
| 96 |
$self->pidl("TDR_ALLOC(mem_ctx, v->$e->{NAME}, $len);"); |
| 97 |
$mem_ctx = "v->$e->{NAME}"; |
| 98 |
} |
| 99 |
|
| 100 |
$self->pidl("for (i = 0; i < $len; i++) {"); |
| 101 |
$self->indent; |
| 102 |
$array = "[i]"; |
| 103 |
} |
| 104 |
|
| 105 |
if ($t eq "pull") { |
| 106 |
$name = ", $mem_ctx"; |
| 107 |
} |
| 108 |
|
| 109 |
if (has_property($e, "value") && $t eq "push") { |
| 110 |
$self->pidl("v->$e->{NAME} = ".ParseExpr($e->{PROPERTIES}->{value}, $env, $e).";"); |
| 111 |
} |
| 112 |
|
| 113 |
$self->pidl("TDR_CHECK(tdr_$t\_$e->{TYPE}(tdr$name$switch, &v->$e->{NAME}$array));"); |
| 114 |
|
| 115 |
if ($array) { $self->deindent; $self->pidl("}"); } |
| 116 |
|
| 117 |
if (has_property($e, "flag")) { |
| 118 |
$self->pidl("tdr->flags = saved_flags;"); |
| 119 |
$self->deindent; |
| 120 |
$self->pidl("}"); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
sub ParserStruct($$$$$) |
| 125 |
{ |
| 126 |
my ($self, $e,$t,$p) = @_; |
| 127 |
|
| 128 |
$self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", struct $e->{NAME} *v)"); |
| 129 |
$self->pidl("{"); $self->indent; |
| 130 |
$self->pidl("int i;") if (ContainsArray($e)); |
| 131 |
|
| 132 |
if ($t eq "print") { |
| 133 |
$self->pidl("tdr->print(tdr, \"\%-25s: struct $e->{NAME}\", name);"); |
| 134 |
$self->pidl("tdr->level++;"); |
| 135 |
} |
| 136 |
|
| 137 |
my %env = map { $_->{NAME} => "v->$_->{NAME}" } @{$e->{ELEMENTS}}; |
| 138 |
$env{"this"} = "v"; |
| 139 |
$self->ParserElement($_, $t, \%env) foreach (@{$e->{ELEMENTS}}); |
| 140 |
|
| 141 |
if ($t eq "print") { |
| 142 |
$self->pidl("tdr->level--;"); |
| 143 |
} |
| 144 |
|
| 145 |
$self->pidl("return NT_STATUS_OK;"); |
| 146 |
|
| 147 |
$self->deindent; $self->pidl("}"); |
| 148 |
} |
| 149 |
|
| 150 |
sub ParserUnion($$$$) |
| 151 |
{ |
| 152 |
my ($self, $e,$t,$p) = @_; |
| 153 |
|
| 154 |
$self->fn_declare($p,"NTSTATUS tdr_$t\_$e->{NAME}(struct tdr_$t *tdr".typearg($t).", int level, union $e->{NAME} *v)"); |
| 155 |
$self->pidl("{"); $self->indent; |
| 156 |
$self->pidl("int i;") if (ContainsArray($e)); |
| 157 |
|
| 158 |
if ($t eq "print") { |
| 159 |
$self->pidl("tdr->print(tdr, \"\%-25s: union $e->{NAME}\", name);"); |
| 160 |
$self->pidl("tdr->level++;"); |
| 161 |
} |
| 162 |
|
| 163 |
$self->pidl("switch (level) {"); $self->indent; |
| 164 |
foreach (@{$e->{ELEMENTS}}) { |
| 165 |
if (has_property($_, "case")) { |
| 166 |
$self->pidl("case " . $_->{PROPERTIES}->{case} . ":"); |
| 167 |
} elsif (has_property($_, "default")) { |
| 168 |
$self->pidl("default:"); |
| 169 |
} |
| 170 |
$self->indent; $self->ParserElement($_, $t, {}); $self->deindent; |
| 171 |
$self->pidl("break;"); |
| 172 |
} |
| 173 |
$self->deindent; $self->pidl("}"); |
| 174 |
|
| 175 |
if ($t eq "print") { |
| 176 |
$self->pidl("tdr->level--;"); |
| 177 |
} |
| 178 |
|
| 179 |
$self->pidl("return NT_STATUS_OK;\n"); |
| 180 |
$self->deindent; $self->pidl("}"); |
| 181 |
} |
| 182 |
|
| 183 |
sub ParserBitmap($$$$) |
| 184 |
{ |
| 185 |
my ($self,$e,$t,$p) = @_; |
| 186 |
return if ($p); |
| 187 |
$self->pidl("#define tdr_$t\_$e->{NAME} tdr_$t\_" . Parse::Pidl::Typelist::bitmap_type_fn($e)); |
| 188 |
} |
| 189 |
|
| 190 |
sub ParserEnum($$$$) |
| 191 |
{ |
| 192 |
my ($self,$e,$t,$p) = @_; |
| 193 |
my $bt = Parse::Pidl::Typelist::enum_type_fn($e); |
| 194 |
my $mt = mapTypeName($bt); |
| 195 |
|
| 196 |
$self->fn_declare($p, "NTSTATUS tdr_$t\_$e->{NAME} (struct tdr_$t *tdr".typearg($t).", enum $e->{NAME} *v)"); |
| 197 |
$self->pidl("{"); |
| 198 |
if ($t eq "pull") { |
| 199 |
$self->pidl("\t$mt r;"); |
| 200 |
$self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, mem_ctx, \&r));"); |
| 201 |
$self->pidl("\t*v = r;"); |
| 202 |
} elsif ($t eq "push") { |
| 203 |
$self->pidl("\tTDR_CHECK(tdr_$t\_$bt(tdr, ($mt *)v));"); |
| 204 |
} elsif ($t eq "print") { |
| 205 |
$self->pidl("\t/* FIXME */"); |
| 206 |
} |
| 207 |
$self->pidl("\treturn NT_STATUS_OK;"); |
| 208 |
$self->pidl("}"); |
| 209 |
} |
| 210 |
|
| 211 |
sub ParserTypedef($$$$) |
| 212 |
{ |
| 213 |
my ($self, $e,$t,$p) = @_; |
| 214 |
|
| 215 |
$self->ParserType($e->{DATA},$t); |
| 216 |
} |
| 217 |
|
| 218 |
sub ParserType($$$) |
| 219 |
{ |
| 220 |
my ($self, $e,$t) = @_; |
| 221 |
|
| 222 |
return if (has_property($e, "no$t")); |
| 223 |
|
| 224 |
my $handlers = { |
| 225 |
STRUCT => \&ParserStruct, UNION => \&ParserUnion, |
| 226 |
ENUM => \&ParserEnum, BITMAP => \&ParserBitmap, |
| 227 |
TYPEDEF => \&ParserTypedef |
| 228 |
}; |
| 229 |
|
| 230 |
$handlers->{$e->{TYPE}}->($self, $e, $t, has_property($e, "public")) |
| 231 |
if (defined($handlers->{$e->{TYPE}})); |
| 232 |
|
| 233 |
$self->pidl(""); |
| 234 |
} |
| 235 |
|
| 236 |
sub ParserInterface($$) |
| 237 |
{ |
| 238 |
my ($self,$x) = @_; |
| 239 |
|
| 240 |
$self->pidl_hdr("#ifndef __TDR_$x->{NAME}_HEADER__"); |
| 241 |
$self->pidl_hdr("#define __TDR_$x->{NAME}_HEADER__"); |
| 242 |
|
| 243 |
foreach (@{$x->{DATA}}) { |
| 244 |
$self->ParserType($_, "pull"); |
| 245 |
$self->ParserType($_, "push"); |
| 246 |
$self->ParserType($_, "print"); |
| 247 |
} |
| 248 |
|
| 249 |
$self->pidl_hdr("#endif /* __TDR_$x->{NAME}_HEADER__ */"); |
| 250 |
} |
| 251 |
|
| 252 |
sub Parser($$$$) |
| 253 |
{ |
| 254 |
my ($self,$idl,$hdrname,$baseheader) = @_; |
| 255 |
$self->pidl("/* autogenerated by pidl */"); |
| 256 |
if (is_intree()) { |
| 257 |
$self->pidl("#include \"includes.h\""); |
| 258 |
} else { |
| 259 |
$self->pidl("#include <stdio.h>"); |
| 260 |
$self->pidl("#include <stdbool.h>"); |
| 261 |
$self->pidl("#include <stdlib.h>"); |
| 262 |
$self->pidl("#include <stdint.h>"); |
| 263 |
$self->pidl("#include <stdarg.h>"); |
| 264 |
$self->pidl("#include <string.h>"); |
| 265 |
$self->pidl("#include <core/ntstatus.h>"); |
| 266 |
} |
| 267 |
$self->pidl("#include \"$hdrname\""); |
| 268 |
$self->pidl(""); |
| 269 |
$self->pidl_hdr("/* autogenerated by pidl */"); |
| 270 |
$self->pidl_hdr("#include \"$baseheader\""); |
| 271 |
$self->pidl_hdr(choose_header("lib/tdr/tdr.h", "tdr.h")); |
| 272 |
$self->pidl_hdr(""); |
| 273 |
|
| 274 |
foreach (@$idl) { $self->ParserInterface($_) if ($_->{TYPE} eq "INTERFACE"); } |
| 275 |
return ($self->{res_hdr}, $self->{res}); |
| 276 |
} |
| 277 |
|
| 278 |
1; |