-
Notifications
You must be signed in to change notification settings - Fork 321
/
click-mkelemmap
executable file
·561 lines (510 loc) · 16.9 KB
/
click-mkelemmap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#! /usr/bin/perl -w
# click-mkelemmap -- make map of element name to C++ class and file
# Eddie Kohler
#
# Copyright (c) 1999-2001 Massachusetts Institute of Technology
# Copyright (c) 2004-2006 Regents of the University of California
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, subject to the conditions
# listed in the Click LICENSE file. These conditions include: you must
# preserve this copyright notice, and you cannot mention the copyright
# holders in advertising related to the Software without their permission.
# The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
# notice is a summary of the Click LICENSE file; the license in that file is
# legally binding.
my(%port_count_constants) =
('PORTS_0_0' => '0/0', 'PORTS_0_1' => '0/1', 'PORTS_1_0' => '1/0',
'PORTS_1_1' => '1/1', 'PORTS_1_1X2' => '1/1-2');
my(%processing_constants) =
('AGNOSTIC' => 'a/a', 'PUSH' => 'h/h', 'PULL' => 'l/l',
'PUSH_TO_PULL' => 'h/l', 'PULL_TO_PUSH' => 'l/h',
'PROCESSING_A_AH' => 'a/ah', 'PROCESSING_H_LH' => 'h/lh');
my(%flow_code_constants) =
('COMPLETE_FLOW' => 'x/x');
my(@source_file, @header_file, @click_name, @cxx_name, @doc_name,
@parents, @port_count, @processing, @flow_code, @flags, @requirements,
@element_methods, @element_libs, @provisions, @noexports,
%click_name_to_id, %cxx_name_to_id, $verbose);
my(@includes) = ( );
sub driver_mask ($) {
my($m) = 0;
# XXX "|" syntax?
$m |= 1 if $_[0] =~ /\buserlevel\b/;
$m |= 2 if $_[0] =~ /\blinuxmodule\b/;
$m |= 4 if $_[0] =~ /\bbsdmodule\b/;
$m |= 8 if $_[0] =~ /\bns\b/;
($m ? $m : 15);
}
sub filecontents ($) {
my $text = "";
if (open(IN, $_[0])) {
$text = <IN>;
close IN;
}
$text;
}
sub process_file ($) {
my($filename) = @_;
my($headername, $headerfile, $i, $comment);
if ($filename =~ /^(.*):"(.*)"$/) {
($filename, $headername) = ($1, $2);
$headerfile = $headername;
} elsif ($filename =~ /^(.*):<(.*)>$/) {
($filename, $headername) = ($1, "<$2>");
$headerfile = $2;
} else {
$headername = $filename;
$headername =~ s/\.cc$/.hh/;
$headerfile = $headername;
}
print STDERR "Reading $headerfile\n" if $verbose;
my($text) = "";
if ($headerfile =~ m|^/|) {
$text = filecontents($headerfile);
} else {
for ($i = 0; $i < @includes && $text eq ""; $i++) {
$text = filecontents($includes[$i] . "/" . $headerfile);
}
}
$headername = "" if $text eq "";
my $first;
$first = @cxx_name;
foreach $_ (split(m{^class(?=.*[\n\s]*\{)}m, $text)) {
my($cxx_class) = (/^\s*(\w+)(\s|:\s).*[\n\s]*\{/);
$cxx_class = "" if !defined($cxx_class);
my($click_name) = (/class_name.*return\s*\"([^\"]+)\"/);
next if !$click_name;
push @cxx_name, $cxx_class;
push @source_file, $filename;
push @header_file, $headername;
$cxx_name_to_id{$cxx_class} = @cxx_name - 1 if $cxx_class;
if (/\A\s*\w*\s*:\s*([\w\s,]+)/) {
my $p = $1;
$p =~ s/\bpublic\b//g;
push @parents, [ split(/[\s,]+/, $p) ];
} else {
push @parents, [];
}
if (/class_name.*return\s*\"([^\"]+)\"/) {
push @click_name, $1;
} else {
push @click_name, "";
}
if (/port_count.*return\s*(.*?);/) {
my $p = $1;
$p = $port_count_constants{$p} if exists($port_count_constants{$p});
$p =~ tr/\" \t\r\n//d;
push @port_count, $p;
} else {
push @port_count, "";
}
if (/processing.*return\s*(.*?);/) {
my $p = $1;
$p = $processing_constants{$p} if exists($processing_constants{$p});
$p =~ tr/\" \t\r\n//d;
push @processing, $p;
} else {
push @processing, "";
}
if (/flow_code.*return\s*(.*?);/) {
my $p = $1;
$p = $flow_code_constants{$p} if exists($flow_code_constants{$p});
$p =~ tr/\" \t\r\n//d;
push @flow_code, $p;
} else {
push @flow_code, "";
}
if (/\bflags\(\).*return\s*"(.*?)";/) {
push @flags, $1;
} else {
push @flags, undef;
}
my($methods) = '';
$methods .= "static_initialize " if /\n\s*static\s+void\s+static_initialize/;
$methods .= "static_cleanup " if /\n\s*static\s+void\s+static_cleanup/;
$methods =~ s/\s+\Z//;
push @element_methods, $methods;
}
# process element documentation
my(%doc_titles);
foreach $comment (split(m{(/\*.*?\*/)}s, $text)) {
if ($comment =~ /^\/\*/ && $comment =~ /^[\/*\s]+=/) {
$comment =~ s/^\/\*\s*//g;
$comment =~ s/\s*\*\/$//g;
$comment =~ s/^ ?\* ?//gm;
my($title, $count);
while ($comment =~ m{^=(\w+)( *)(.*)([\0-\377]*?)(?=^=\w|\Z)}mg) {
if ($1 eq 'title') {
$title = $3;
} elsif ($1 eq 'c') {
$_ = $4;
while (/^\s*(\w+)\(/mg) {
$doc_titles{$1} = ($title ? $title : $1);
}
if (!%doc_titles && /^\s*([\w@]+)\s*$/) {
$doc_titles{$1} = ($title ? $title : $1);
}
last;
}
}
}
}
# apply element documentation to element names
for ($i = $first; $i < @processing; $i++) {
push @doc_name, $doc_titles{$click_name[$i]};
}
# check titles
if (@processing == 0 && keys(%doc_titles) > 1) {
print STDERR "$filename: more than 1 documentation class for requirement\n";
}
# process ELEMENT_REQUIRES, ELEMENT_PROVIDES, and EXPORT_ELEMENT
if (!open(IN, $filename)) {
print STDERR "$filename: $!\n";
return;
}
$text = <IN>;
close IN;
my($req, $prov, $exp, $libs) = ('', '', '', '');
$req .= " " . $1 while $text =~ /^ELEMENT_REQUIRES\s*\((.*)\)/mg;
$prov .= " " . $1 while $text =~ /^ELEMENT_PROVIDES\s*\((.*)\)/mg;
$exp .= " " . $1 while $text =~ /^EXPORT_ELEMENT\s*\((.*)\)/mg;
$libs .= " " . $1 while $text =~ /^ELEMENT_LIBS\s*\((.*)\)/mg;
$req =~ s/^\s+//;
$prov =~ s/^\s+//;
$libs =~ s/^\s+//;
# don't change a provision into an export just because a class of that
# name exists
my(%cxx_export);
foreach my $exp1 (split(/\s+/, $exp)) {
my($cxx) = ($exp1 =~ /(\S+)-/ ? $1 : $exp1);
$cxx_export{$cxx} = 1 if $cxx ne "";
}
for ($i = $first; $i < @processing; $i++) {
push @noexports, ($cxx_name[$i] eq "" || !exists($cxx_export{$cxx_name[$i]}));
}
# make copies of classes as required
while ($exp =~ /(\S+)-(\S+)/g) {
my($cxx, $click) = ($1, $2);
for ($i = $first; $i < @processing; $i++) {
if ($cxx_name[$i] eq $cxx) {
push @click_name, $click;
push @cxx_name, $cxx;
push @doc_name, $doc_name[$i];
push @source_file, $source_file[$i];
push @header_file, $header_file[$i];
push @parents, $parents[$i];
push @port_count, $port_count[$i];
push @processing, $processing[$i];
push @flow_code, $flow_code[$i];
push @flags, $flags[$i];
push @element_methods, $element_methods[$i];
push @noexports, 0;
last;
}
}
}
# add a fake class if there were no classes
if (@cxx_name == $first && $prov) {
push @click_name, "";
push @cxx_name, "";
push @doc_name, (%doc_titles ? keys(%doc_titles) : (""));
push @source_file, $filename;
push @header_file, $headername;
push @parents, [];
push @port_count, "";
push @processing, "";
push @flow_code, "";
push @flags, undef;
push @element_methods, "";
push @noexports, 1;
}
# apply requirements and provisions
for ($i = $first; $i < @processing; $i++) {
push @requirements, $req;
push @provisions, $prov;
push @element_libs, $libs;
# check to see if overloading is valid
if ($click_name[$i] && exists($click_name_to_id{$click_name[$i]})) {
my($j) = $click_name_to_id{$click_name[$i]};
my($dm_a) = driver_mask($requirements[$i]);
my($dm_b) = driver_mask($requirements[$j]);
if (($dm_a & $dm_b) == 0) {
# ok
} else {
print STDERR "invalid multiple definition of element class '$click_name[$i]'\n";
print STDERR $header_file[$j], ": first definition here\n";
print STDERR $header_file[$i], ": second definition here\n";
print STDERR "(Two classes may share a name only if they work in disjoint drivers.\nAdd explicit ELEMENT_REQUIRES() statements.)\n";
}
}
$click_name_to_id{$click_name[$i]} = $i;
}
}
sub parents_port_count ($) {
my($classid) = @_;
return undef if !defined $classid;
if (!$port_count[$classid]) {
my($parent);
foreach $parent (@{$parents[$classid]}) {
if ($parent eq 'Element') {
$port_count[$classid] = '0/0';
last;
} elsif ($parent ne '') {
$port_count[$classid] = &parents_port_count($cxx_name_to_id{$parent});
last if $port_count[$classid];
}
}
}
return $port_count[$classid];
}
sub parents_processing ($) {
my($classid) = @_;
return undef if !defined $classid;
if (!$processing[$classid]) {
my($parent);
foreach $parent (@{$parents[$classid]}) {
if ($parent eq 'Element') {
$processing[$classid] = 'a/a';
last;
} elsif ($parent ne '') {
$processing[$classid] = &parents_processing($cxx_name_to_id{$parent});
last if $processing[$classid];
}
}
}
return $processing[$classid];
}
sub parents_flow_code ($) {
my($classid) = @_;
return undef if !defined $classid;
if (!$flow_code[$classid]) {
my($parent);
foreach $parent (@{$parents[$classid]}) {
if ($parent eq 'Element') {
$flow_code[$classid] = 'x/x';
last;
} elsif ($parent ne '') {
$flow_code[$classid] = &parents_flow_code($cxx_name_to_id{$parent});
last if $flow_code[$classid];
}
}
}
return $flow_code[$classid];
}
sub parents_flags ($) {
my($classid) = @_;
return undef if !defined $classid;
if (!defined $flags[$classid]) {
my($parent);
foreach $parent (@{$parents[$classid]}) {
if ($parent eq 'Element') {
last;
} elsif ($parent ne '') {
$flags[$classid] = &parents_flags($cxx_name_to_id{$parent});
last if defined $flags[$classid];
}
}
}
return $flags[$classid];
}
sub xml_quote ($) {
my($x) = @_;
$x =~ s/&/&/g;
$x =~ s/</</g;
$x;
}
# main program: parse options
sub read_files_from ($) {
my($fn) = @_;
if (open(IN, ($fn eq '-' ? "<&STDIN" : $fn))) {
my(@a, @b, $t);
$t = <IN>;
close IN;
# Parse file; click-buildtool gets special treatment
if ($t =~ /\A#.*click-(buildtool findelem|mkmindriver)/) {
$t =~ s/^#.*//mg;
@a = map {
if (/^(\S+)\s+(\S+)/) {
"$1:$2";
} else {
$_;
}
} split(/\n+/, $t);
} else {
$t =~ s/^#.*//mg;
@a = split(/\s+/, $t);
}
foreach $t (@a) {
next if $t eq '';
if ($t =~ /[*?\[]/) {
push @b, glob($t);
} else {
push @b, $t;
}
}
@b;
} else {
print STDERR "$fn: $!\n";
();
}
}
sub long_option_match ($$$) {
my($have, $want, $len) = @_;
$have = $1 if $have =~ /^(--[^=]*)=/;
my($hl) = length($have);
($hl <= length($want) && $hl >= $len && $have eq substr($want, 0, $hl));
}
sub help () {
print STDERR <<"EOD;";
'Click-mkelemmap' creates an elementmap file from a collection of Click
elements.
Usage: click-mkelemmap [OPTIONS] [-f FILE | SRCFILE]...
Each SRCFILE is a Click header file. '-' means standard input. Default is
'-f -' if no '-f' or 'SRCFILE' options are supplied.
Options:
-f, --files FILE Read header filenames, or an 'elements.conf' file,
from FILE.
-r, --provide PROV Record provisions PROV in output.
-t, --drivers DRIVERS Record drivers DRIVERS in output.
--dochref DOCSTR Record documentation URL pattern DOCSTR in output.
-p, --prefix PFX Sources start at PFX. Add PFX to includes and remove
PFX from filenames in output.
-I, --include DIR Look for header files in PFX/DIR.
-s, --sourcedir DIR Record DIR as source directory in output.
-V, --verbose Be more verbose.
-h, --help Print this message and exit.
Report bugs to <click\@pdos.lcs.mit.edu>.
EOD;
exit 0;
}
undef $/;
my(@files, @provides, @drivers, $fn, $prefix, $any_files, $dochref, $sourcedir);
$prefix = "";
while (@ARGV) {
$_ = shift @ARGV;
if (long_option_match($_, '--files', 3) && /^[^=]*=(.*)$/) {
push @files, read_files_from($1);
$any_files = 1;
} elsif (/^-f$/ || long_option_match($_, '--files', 3)) {
die "not enough arguments" if !@ARGV;
push @files, read_files_from(shift @ARGV);
$any_files = 1;
} elsif (long_option_match($_, '--prefix', 5) && /^[^=]*=(.*)$/) {
$prefix = $1;
$prefix .= "/" if $prefix !~ m|/\Z|;
} elsif (/^-p$/ || long_option_match($_, '--prefix', 5)) {
die "not enough arguments" if !@ARGV;
$prefix = shift @ARGV;
$prefix .= "/" if $prefix !~ m|/\Z|;
} elsif (long_option_match($_, '--provide', 5) && /^[^=]*=(.*)$/) {
push @provides, split(/\s+/, $1);
} elsif (/^-r$/ || long_option_match($_, '--provide', 5)) {
die "not enough arguments" if !@ARGV;
push @provides, split(/\s+/, shift @ARGV);
} elsif (long_option_match($_, '--drivers', 4) && /^[^=]*=(.*)$/) {
push @drivers, split(/\s+/, $1);
} elsif (/^-t$/ || long_option_match($_, '--drivers', 4)) {
die "not enough arguments" if !@ARGV;
push @drivers, split(/\s+/, shift @ARGV);
} elsif (long_option_match($_, '--include', 3) && /^[^=]*=(.*)$/) {
push @includes, $1;
} elsif (/^-I$/ || long_option_match($_, '--include', 3)) {
die "not enough arguments" if !@ARGV;
push @includes, shift @ARGV;
} elsif (/^-I(.+)$/) {
push @includes, $1;
} elsif (long_option_match($_, '--sourcedir', 3) && /^[^=]*=(.*)$/) {
$sourcedir = xml_quote($1);
} elsif (/^-s$/ || long_option_match($_, '--sourcedir', 3)) {
die "not enough arguments" if !@ARGV;
$sourcedir = xml_quote(shift @ARGV);
} elsif (/^-s(.+)$/) {
$sourcedir = xml_quote($1);
} elsif ((long_option_match($_, '--dochref', 4) || long_option_match($_, '--webdoc', 3))
&& /^[^=]*=(.*)$/) {
die "repeated --dochref" if defined($dochref);
$dochref = $1;
} elsif (long_option_match($_, '--dochref', 4) || long_option_match($_, '--webdoc', 3)) {
die "not enough arguments" if !@ARGV;
die "repeated --dochref" if defined($dochref);
$dochref = shift @ARGV;
} elsif (/^-h$/ || long_option_match($_, '--help', 3)) {
help();
} elsif (/^-V$/ || long_option_match($_, '--verbose', 3)) {
$verbose = 1;
} elsif (/^-./) {
die "unknown option '$_'\n";
} elsif (/^-$/) {
push @files, "-";
$any_files = 1;
} else {
push @files, glob($_);
$any_files = 1;
}
}
push @files, read_files_from("-") if !$any_files;
if ($prefix eq "" || $prefix eq "." || $prefix eq "./") {
unshift @includes, ".";
} else {
@includes = (".", $prefix, map { m|^/| ? $_ : "$prefix$_" } @includes);
}
print STDERR "Includes: ", join(", ", @includes), "\n" if $verbose;
foreach $fn (@files) {
process_file($fn);
}
umask(022);
open(OUT, ">&STDOUT");
print OUT "<?xml version=\"1.0\" standalone=\"yes\"?>
<?xml-stylesheet type=\"application/xml\" href=\"http://www.lcdf.org/click/xml/elementmap.xsl\"?>\n";
print OUT "<?xml-stylesheet type=\"application/xml\" href=\"file://$sourcedir/etc/elementmap.xsl\"?>\n" if defined($sourcedir) && $sourcedir =~ m|^/|;
print OUT "<elementmap xmlns=\"http://www.lcdf.org/click/xml/\"";
print OUT " sourcedir=\"$sourcedir\"" if defined($sourcedir);
print OUT " src=\"file://$sourcedir\"" if defined($sourcedir) && $sourcedir =~ m|^/|;
print OUT " provides=\"", join(' ', @provides), "\"";
print OUT " drivers=\"", join(' ', @drivers), "\"" if @drivers;
print OUT " dochref=\"$dochref\"" if defined($dochref);
print OUT ">\n";
sub sorter {
my($xa, $xb) = ($click_name[$a], $click_name[$b]);
if ($xa && $xb) {
$xa cmp $xb;
} elsif ($xa || $xb) {
($xa ? -1 : 1);
} else {
$provisions[$a] cmp $provisions[$b];
}
}
foreach $id (sort sorter 0..$#click_name) {
print OUT "<entry";
print OUT " name=\"", $click_name[$id], "\"" if $click_name[$id];
print OUT " cxxclass=\"", $cxx_name[$id], "\"" if $cxx_name[$id];
print OUT " docname=\"", $doc_name[$id], "\"" if $doc_name[$id];
my($hf) = $header_file[$id];
$hf =~ s/^$prefix\/*// if $prefix;
print OUT " headerfile=\"", xml_quote($hf), "\"" if $hf;
my($sf) = $source_file[$id];
$sf =~ s/^$prefix\/*// if $prefix;
print OUT " sourcefile=\"", xml_quote($sf), "\"" if $sf;
my($pc) = $port_count[$id];
$pc = parents_port_count($id) if !$pc;
print OUT " portcount=\"", $pc, "\"" if $pc;
my($p) = $processing[$id];
$p = parents_processing($id) if !$p;
print OUT " processing=\"", $p, "\"" if $p;
my($flow) = $flow_code[$id];
$flow = parents_flow_code($id) if !$flow;
print OUT " flowcode=\"", $flow, "\"" if $flow;
my($flags) = $flags[$id];
$flags = parents_flags($id) if !defined($flags);
print OUT " flags=\"", $flags, "\"" if defined($flags);
print OUT " methods=\"", $element_methods[$id], "\"" if $element_methods[$id];
print OUT " requires=\"", $requirements[$id], "\"" if $requirements[$id];
print OUT " provides=\"", $provisions[$id], "\"" if $provisions[$id];
print OUT " libs=\"", xml_quote($element_libs[$id]), "\"" if $element_libs[$id];
print OUT " noexport=\"noexport\"" if $cxx_name[$id] && $noexports[$id];
print OUT " />\n";
}
print OUT "</elementmap>\n";
close OUT;