PageRenderTime 24ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/cmpt471/a2/wireshark/docbook/make-wsluarm.pl

http://cwoodruf-sfu-cmpt.googlecode.com/
Perl | 491 lines | 347 code | 76 blank | 68 comment | 38 complexity | 5182a65dcf260668a97beaaa304e8d62 MD5 | raw file
  1. #!/usr/bin/perl
  2. #
  3. # make-doc.pl
  4. # WSLUA's Reference Manual Generator
  5. #
  6. # (c) 2006, Luis E. Garcia Onatnon <luis@ontanon.org>
  7. #
  8. # $Id: make-wsluarm.pl 35731 2011-01-31 21:16:20Z jake $
  9. #
  10. # Wireshark - Network traffic analyzer
  11. # By Gerald Combs <gerald@wireshark.org>
  12. # Copyright 1998 Gerald Combs
  13. #
  14. # This program is free software; you can redistribute it and/or
  15. # modify it under the terms of the GNU General Public License
  16. # as published by the Free Software Foundation; either version 2
  17. # of the License, or (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with this program; if not, write to the Free Software
  26. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. #
  28. # (-: I don't even think writing this in Lua :-)
  29. use strict;
  30. #use V2P;
  31. sub deb {
  32. # warn $_[0];
  33. }
  34. sub gorolla {
  35. # a gorilla stays to a chimp like gorolla stays to chomp
  36. # but this one returns the shrugged string.
  37. my $s = shift;
  38. $s =~ s/^([\n]|\s)*//ms;
  39. $s =~ s/([\n]|\s)*$//ms;
  40. $s =~ s/\</&lt;/ms;
  41. $s =~ s/\>/&gt;/ms;
  42. $s;
  43. }
  44. my %module = ();
  45. my %modules = ();
  46. my $class;
  47. my %classes;
  48. my $function;
  49. my @functions;
  50. my $docbook_template = {
  51. module_header => "<section id='lua_module_%s'>\n",
  52. module_desc => "\t<title>%s</title>\n",
  53. module_footer => "</section>\n",
  54. class_header => "\t<section id='lua_class_%s'><title>%s</title>\n",
  55. class_desc => "\t\t<para>%s</para>\n",
  56. class_footer => "\t</section> <!-- class_footer: %s -->\n",
  57. # class_constructors_header => "\t\t<section id='lua_class_constructors_%s'>\n\t\t\t<title>%s Constructors</title>\n",
  58. # class_constructors_footer => "\t\t</section> <!-- class_constructors_footer -->\n",
  59. # class_methods_header => "\t\t<section id='lua_class_methods_%s'>\n\t\t\t<title>%s Methods</title>\n",
  60. # class_methods_footer => "\t\t</section> <!-- class_methods_footer: %s -->\n",
  61. class_attr_header => "\t\t<section id='lua_class_attrib_%s'>\n\t\t\t<title>%s</title>\n",
  62. class_attr_footer => "\t\t</section> <!-- class_attr_footer: %s -->\n",
  63. class_attr_descr => "\t\t\t<para>%s</para>\n",
  64. function_header => "\t\t\t<section id='lua_fn_%s'>\n\t\t\t\t<title>%s</title>\n",
  65. function_descr => "\t\t\t\t<para>%s</para>\n",
  66. function_footer => "\t\t\t</section> <!-- function_footer: %s -->\n",
  67. function_args_header => "\t\t\t\t\t<section><title>Arguments</title>\t\t\t\t<variablelist>\n",
  68. function_args_footer => "\t\t\t\t</variablelist></section>\n",
  69. function_arg_header => "\t\t\t\t<varlistentry><term>%s</term>\n",
  70. function_arg_descr => "\t\t\t\t\t<listitem><para>%s</para></listitem>\n",
  71. function_arg_footer => "\t\t\t\t</varlistentry> <!-- function_arg_footer: %s -->\n",
  72. function_argerror_header => "", #"\t\t\t\t\t<section><title>Errors</title>\n\t\t\t\t\t\t<itemizedlist>\n",
  73. function_argerror => "", #"\t\t\t\t\t\t\t<listitem><para>%s</para></listitem>\n",
  74. function_argerror_footer => "", #"\t\t\t\t\t\t</itemizedlist></section> <!-- function_argerror_footer: %s -->\n",
  75. function_returns_header => "\t\t\t\t<section><title>Returns</title>\n",
  76. function_returns_footer => "\t\t\t\t</section> <!-- function_returns_footer: %s -->\n",
  77. function_returns => "\t\t\t\t\t<para>%s</para>\n",
  78. function_errors_header => "\t\t\t\t<section><title>Errors</title><itemizedlist>\n",
  79. function_errors => "\t\t\t\t\t\t<listitem><para>%s</para></listitem>\n",
  80. function_errors_footer => "\t\t\t\t\t</itemizedlist></section> <!-- function_error_footer: %s -->\n",
  81. non_method_functions_header => "\t\t<section id='non_method_functions_%s'><title>Non Method Functions</title>\n",
  82. non_method_functions_footer => "\t\t</section> <!-- Non method -->\n",
  83. };
  84. my $template_ref = $docbook_template;
  85. my $out_extension = "xml";
  86. # It's said that only perl can parse perl... my editor isn't perl...
  87. # if unencoded this causes my editor's autoindent to bail out so I encoded in octal
  88. # XXX: support \" within ""
  89. my $QUOTED_RE = "\042\050\133^\042\135*\051\042";
  90. my $TRAILING_COMMENT_RE = '((\s*|[\n\r]*)/\*(.*?)\*/)?';
  91. my @control =
  92. (
  93. # This will be scanned in order trying to match the re if it matches
  94. # the body will be executed immediatelly after.
  95. [ 'WSLUA_MODULE\s*([A-Z][a-zA-Z]+)([^\*]*)',
  96. sub {
  97. $module{name} = $1;
  98. $module{descr} = $2
  99. } ],
  100. [ 'WSLUA_CLASS_DEFINE\050\s*([A-Z][a-zA-Z]+).*?\051;' . $TRAILING_COMMENT_RE,
  101. sub {
  102. deb ">c=$1=$2=$3=$4=$5=$6=$7=\n";
  103. $class = {
  104. name => $1,
  105. descr=> gorolla($4),
  106. constructors => [],
  107. methods => [],
  108. attributes => []
  109. };
  110. $classes{$1} = $class;
  111. } ],
  112. [ 'WSLUA_FUNCTION\s+wslua_([a-z_]+)[^\173]*\173' . $TRAILING_COMMENT_RE,
  113. sub {
  114. deb ">f=$1=$2=$3=$4=$5=$6=$7=\n";
  115. $function = {
  116. returns => [],
  117. arglist => [],
  118. args => {},
  119. name => $1,
  120. descr => gorolla($4),
  121. type => 'standalone'
  122. };
  123. push @functions, $function;
  124. } ],
  125. [ 'WSLUA_CONSTRUCTOR\s+([A-Za-z0-9]+)_([a-z0-9_]+).*?\173' . $TRAILING_COMMENT_RE,
  126. sub {
  127. deb ">cc=$1=$2=$3=$4=$5=$6=$7=\n";
  128. $function = {
  129. returns => [],
  130. arglist => [],
  131. args => {},
  132. name => "$1.$2",
  133. descr => gorolla($5),
  134. type => 'constructor'
  135. };
  136. push @{${$class}{constructors}}, $function;
  137. } ],
  138. [ '_WSLUA_CONSTRUCTOR_\s+([A-Za-z0-9]+)_([a-z0-9_]+)\s*(.*?)\052\057',
  139. sub {
  140. deb ">cc=$1=$2=$3=$4=$5=$6=$7=\n";
  141. $function = {
  142. returns => [],
  143. arglist => [],
  144. args => {},
  145. name => "$1.$2",
  146. descr => gorolla($3),
  147. type => 'constructor'
  148. };
  149. push @{${$class}{constructors}}, $function;
  150. } ],
  151. [ 'WSLUA_METHOD\s+([A-Za-z]+)_([a-z0-9_]+)[^\173]*\173' . $TRAILING_COMMENT_RE,
  152. sub {
  153. deb ">cm=$1=$2=$3=$4=$5=$6=$7=\n";
  154. my $name = "$1";
  155. $name =~ tr/A-Z/a-z/;
  156. $name .= ":$2";
  157. $function = {
  158. returns => [],
  159. arglist => [],
  160. args => {},
  161. name => $name,
  162. descr => gorolla($5),
  163. type => 'method'
  164. };
  165. push @{${$class}{methods}}, $function;
  166. } ],
  167. [ 'WSLUA_METAMETHOD\s+([A-Za-z]+)(__[a-z0-9]+)[^\173]*\173' . $TRAILING_COMMENT_RE,
  168. sub {
  169. deb ">cm=$1=$2=$3=$4=$5=$6=$7=\n";
  170. my $name = "$1";
  171. $name =~ tr/A-Z/a-z/;
  172. $name .= ":$2";
  173. my ($c,$d) = ($1,$5);
  174. $function = {
  175. returns => [],
  176. arglist => [],
  177. args => {},
  178. name => $name,
  179. descr => gorolla($5),
  180. type => 'metamethod'
  181. };
  182. push @{${$class}{methods}}, $function;
  183. } ],
  184. [ '#define WSLUA_(OPT)?ARG_([a-z0-9_]+)_([A-Z0-9]+)\s+\d+' . $TRAILING_COMMENT_RE,
  185. sub {
  186. deb ">a=$1=$2=$3=$4=$5=$6=$7=\n";
  187. my $name = $1 eq 'OPT' ? "[$3]" : $3;
  188. push @{${$function}{arglist}} , $name;
  189. ${${$function}{args}}{$name} = {descr=>$6,}
  190. } ],
  191. [ '\057\052\s*WSLUA_(OPT)?ARG_([A-Za-z0-9_]+)_([A-Z0-9]+)\s*(.*?)\052\057',
  192. sub {
  193. deb ">a=$1=$2=$3=$4=$5=$6=$7=\n";
  194. my $name = $1 eq 'OPT' ? "[$3]" : $3;
  195. push @{${$function}{arglist}} , $name;
  196. ${${$function}{args}}{$name} = {descr=>$4,}
  197. } ],
  198. [ '#define WSLUA_(OPT)?ARG_([A-Za-z]+)_([a-z_]+)_([A-Z0-9]+)\s+\d+' . $TRAILING_COMMENT_RE,
  199. sub {
  200. deb ">ca=$1=$2=$3=$4=$5=$6=$7=\n";
  201. my $name = $1 eq 'OPT' ? "[$4]" : $4;
  202. push @{${$function}{arglist}} , $name;
  203. ${${$function}{args}}{$name} = {descr=>$7,optional => $1 eq '' ? 1 : 0 }
  204. } ],
  205. [ '/\052\s+WSLUA_ATTRIBUTE\s+([A-Za-z]+)_([a-z_]+)\s+([A-Z]*)\s*(.*?)\052/',
  206. sub {
  207. deb ">at=$1=$2=$3=$4=$5=$6=$7=\n";
  208. my $name = "$1";
  209. $name =~ tr/A-Z/a-z/;
  210. $name .= ".$2";
  211. push @{${$class}{attributes}}, { name => $name, descr => gorolla($4), mode=>$3 };
  212. } ],
  213. [ 'WSLUA_ATTR_GET\s+([A-Za-z]+)_([a-z_]+).*?' . $TRAILING_COMMENT_RE,
  214. sub {
  215. deb ">at=$1=$2=$3=$4=$5=$6=$7=\n";
  216. my $name = "$1";
  217. $name =~ tr/A-Z/a-z/;
  218. $name .= ".$2";
  219. push @{${$class}{attributes}}, { name => $name, descr => gorolla($4), mode=>$3 };
  220. } ],
  221. [ '/\052\s+WSLUA_MOREARGS\s+([A-Za-z_]+)\s+(.*?)\052/',
  222. sub {
  223. deb ">ma=$1=$2=$3=$4=$5=$6=$7=\n";
  224. push @{${$function}{arglist}} , "...";
  225. ${${$function}{args}}{"..."} = {descr=>gorolla($2)}
  226. } ],
  227. [ 'WSLUA_(FINAL_)?RETURN\050\s*.*?\s*\051\s*;' . $TRAILING_COMMENT_RE,
  228. sub {
  229. deb ">fr=$1=$2=$3=$4=$5=$6=$7=\n";
  230. push @{${$function}{returns}} , gorolla($4) if $4 ne '';
  231. } ],
  232. [ '\057\052\s*_WSLUA_RETURNS_\s*(.*?)\052\057',
  233. sub {
  234. deb ">fr2=$1=$2=$3=$4=$5=$6=$7=\n";
  235. push @{${$function}{returns}} , gorolla($1) if $1 ne '';
  236. } ],
  237. [ 'WSLUA_ERROR\s*\050\s*(([A-Z][A-Za-z]+)_)?([a-z_]+),' . $QUOTED_RE ,
  238. sub {
  239. deb ">e=$1=$2=$3=$4=$5=$6=$7=\n";
  240. my $errors;
  241. unless (exists ${$function}{errors}) {
  242. $errors = ${$function}{errors} = [];
  243. } else {
  244. $errors = ${$function}{errors};
  245. }
  246. push @{$errors}, gorolla($4);
  247. } ],
  248. [ 'WSLUA_(OPT)?ARG_ERROR\s*\050\s*(([A-Z][A-Za-z]+)_)?([a-z_]+)\s*,\s*([A-Z0-9]+)\s*,\s*' . $QUOTED_RE,
  249. sub {
  250. deb ">ae=$1=$2=$3=$4=$5=$6=$7=\n";
  251. my $errors;
  252. unless (exists ${${${$function}{args}}{$5}}{errors}) {
  253. $errors = ${${${$function}{args}}{$5}}{errors} = [];
  254. } else {
  255. $errors = ${${${$function}{args}}{$5}}{errors};
  256. }
  257. push @{$errors}, gorolla($6);
  258. } ],
  259. );
  260. my $anymatch = '(^ThIsWiLlNeVeRmAtCh$';
  261. for (@control) {
  262. $anymatch .= "|${$_}[0]";
  263. }
  264. $anymatch .= ')';
  265. # for each file given in the command line args
  266. my $file;
  267. while ( $file = shift) {
  268. next unless -f $file;
  269. %module = ();
  270. my $docfile = $file;
  271. $docfile =~ s#.*/##;
  272. $docfile =~ s/\.c$/.$out_extension/;
  273. open C, "< $file" or die "Can't open input file $file: $!";
  274. open D, "> wsluarm_src/$docfile" or die "Can't open output file wsluarm_src/$docfile: $!";
  275. my $b = '';
  276. $b .= $_ while (<C>);
  277. while ($b =~ /$anymatch/ms ) {
  278. my $match = $1;
  279. # print "\n-----\n$match\n-----\n";
  280. for (@control) {
  281. my ($re,$f) = @{$_};
  282. if ( $match =~ /$re/ms) {
  283. &{$f}();
  284. $b =~ s/.*?$re//ms;
  285. last;
  286. }
  287. }
  288. }
  289. $modules{$module{name}} = $docfile;
  290. printf D ${$template_ref}{module_header}, $module{name}, $module{name};
  291. if ( exists ${$template_ref}{module_desc} ) {
  292. printf D ${$template_ref}{module_desc}, $module{descr}, $module{descr};
  293. }
  294. for my $cname (sort keys %classes) {
  295. my $cl = $classes{$cname};
  296. printf D ${$template_ref}{class_header}, $cname, $cname;
  297. if ( ${$cl}{descr} ) {
  298. printf D ${$template_ref}{class_desc} , ${$cl}{descr};
  299. }
  300. if ( $#{${$cl}{constructors}} >= 0) {
  301. # printf D ${$template_ref}{class_constructors_header}, $cname, $cname;
  302. for my $c (@{${$cl}{constructors}}) {
  303. function_descr($c);
  304. }
  305. # printf D ${$template_ref}{class_constructors_footer}, $cname, $cname;
  306. }
  307. if ( $#{${$cl}{methods}} >= 0) {
  308. # printf D ${$template_ref}{class_methods_header}, $cname, $cname;
  309. for my $m (@{${$cl}{methods}}) {
  310. function_descr($m);
  311. }
  312. # printf D ${$template_ref}{class_methods_footer}, $cname, $cname;
  313. }
  314. if ( $#{${$cl}{attributes}} >= 0) {
  315. for my $a (@{${$cl}{attributes}}) {
  316. my $a_id = ${$a}{name};
  317. $a_id =~ s/[^a-zA-Z0-9]/_/g;
  318. printf D ${$template_ref}{class_attr_header}, $a_id, ${$a}{name};
  319. printf D ${$template_ref}{class_attr_descr}, ${$a}{descr}, ${$a}{descr} if ${$a}{descr};
  320. printf D ${$template_ref}{class_attr_footer}, ${$a}{name}, ${$a}{name};
  321. }
  322. }
  323. if (exists ${$template_ref}{class_footer}) {
  324. printf D ${$template_ref}{class_footer}, $cname, $cname;
  325. }
  326. }
  327. if ($#functions >= 0) {
  328. printf D ${$template_ref}{non_method_functions_header}, $module{name};
  329. for my $f (@functions) {
  330. function_descr($f);
  331. }
  332. print D ${$template_ref}{non_method_functions_footer};
  333. }
  334. %classes = ();
  335. $class = undef;
  336. $function = undef;
  337. @functions = ();
  338. close C;
  339. printf D ${$template_ref}{module_footer}, $module{name};
  340. close D;
  341. }
  342. #my $wsluarm = '';
  343. #open B, "< template-wsluarm.xml";
  344. #$wsluarm .= $_ while(<B>);
  345. #close B;
  346. #
  347. #my $ents = '';
  348. #my $txt = '';
  349. #
  350. #for my $module_name (sort keys %modules) {
  351. # $ents .= <<"_ENT";
  352. # <!ENTITY $module_name SYSTEM "wsluarm_src/$modules{$module_name}">
  353. #_ENT
  354. # $txt .= "&$module_name;\n";
  355. #}
  356. #
  357. #$wsluarm =~ s/<!-- WSLUA_MODULE_ENTITIES -->/$ents/;
  358. #$wsluarm =~ s/<!-- WSLUA_MODULE_TEXT -->/$txt/;
  359. #
  360. #open X, "> wsluarm.xml";
  361. #print X $wsluarm;
  362. #close X;
  363. sub function_descr {
  364. my $f = $_[0];
  365. my $label = $_[1];
  366. if (defined $label ) {
  367. $label =~ s/>/&gt;/;
  368. $label =~ s/</&lt;/;
  369. my $section_name = ${$f}{section_name};
  370. $section_name =~ s/[^a-zA-Z0-9]/_/g;
  371. printf D ${$template_ref}{function_header}, $section_name, $label;
  372. } else {
  373. my $arglist = '';
  374. for (@{ ${$f}{arglist} }) {
  375. my $a = $_;
  376. $a =~ tr/A-Z/a-z/;
  377. $arglist .= "$a, ";
  378. }
  379. $arglist =~ s/, $//;
  380. my $section_name = "${$f}{name}($arglist)";
  381. $section_name =~ s/[^a-zA-Z0-9]/_/g;
  382. printf D ${$template_ref}{function_header}, $section_name , "${$f}{name}($arglist)";
  383. }
  384. printf D ${$template_ref}{function_descr}, ${$f}{descr} if ${$f}{descr};
  385. print D ${$template_ref}{function_args_header} if $#{${$f}{arglist}} >= 0;
  386. for my $argname (@{${$f}{arglist}}) {
  387. my $arg = ${${$f}{args}}{$argname};
  388. $argname =~ tr/A-Z/a-z/;
  389. $argname =~ s/\[(.*)\]/$1 (optional)/;
  390. printf D ${$template_ref}{function_arg_header}, $argname, $argname;
  391. printf D ${$template_ref}{function_arg_descr}, ${$arg}{descr} , ${$arg}{descr} if ${$arg}{descr};
  392. if ( $#{${$arg}{errors}} >= 0) {
  393. printf D ${$template_ref}{function_argerror_header}, $argname, $argname;
  394. printf D ${$template_ref}{function_argerror}, $_, $_ for @{${$arg}{errors}};
  395. printf D ${$template_ref}{function_argerror_footer}, $argname, $argname;
  396. }
  397. printf D ${$template_ref}{function_arg_footer}, $argname, $argname;
  398. }
  399. print D ${$template_ref}{function_args_footer} if $#{${$f}{arglist}} >= 0;
  400. if ( $#{${$f}{returns}} >= 0) {
  401. printf D ${$template_ref}{function_returns_header}, ${$f}{name};
  402. printf D ${$template_ref}{function_returns}, $_ for @{${$f}{returns}};
  403. printf D ${$template_ref}{function_returns_footer}, ${$f}{name};
  404. }
  405. if ( $#{${$f}{errors}} >= 0) {
  406. my $sname = exists ${$f}{section_name} ? ${$f}{section_name} : ${$f}{name};
  407. printf D ${$template_ref}{function_errors_header}, $sname;
  408. printf D ${$template_ref}{function_errors}, $_ for @{${$f}{errors}};
  409. printf D ${$template_ref}{function_errors_footer}, ${$f}{name};
  410. }
  411. if (not defined $label ) {
  412. $label = '';
  413. }
  414. printf D ${$template_ref}{function_footer}, $label, $label;
  415. }