su-filter.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use strict;
  9. my $in_su = 0;
  10. my $indent = 0;
  11. my $out;
  12. my $braces = 0;
  13. my $arrcnt;
  14. my $data;
  15. my $tststr;
  16. my $incomm = 0;
  17. while(<>) {
  18. $tststr = $_;
  19. $incomm++ while $tststr =~ /\/\*/g;
  20. $incomm-- while $tststr =~ /\*\//g;
  21. if($in_su == 1) {
  22. if(/}(.*);/) {
  23. $out .= $_;
  24. do_output($out);
  25. $in_su = 0;
  26. } elsif(/^ *\} [^\s]+(\[\d*\])* = \{/) {
  27. $tststr = $1;
  28. $arrcnt = 0;
  29. $arrcnt++ while $tststr =~ /\[/g;
  30. $in_su++;
  31. $braces = 1;
  32. /^(.* = \{)(.*)$/;
  33. $data = $2;
  34. $out .= $1."\n";
  35. } else {
  36. $out .= $_;
  37. }
  38. } elsif($in_su == 2) {
  39. $data .= $_;
  40. if(/};$/) {
  41. #$data = "\n$data";
  42. $data =~ s/\n */\n/g;
  43. $data =~ s/};\n?//s;
  44. my @strucdata = structureData($data);
  45. $out .= displayData($indent, 0, \@strucdata);
  46. $out .= "\n$indent};\n";
  47. do_output($out);
  48. $in_su = 0;
  49. }
  50. } elsif($incomm <= 0 && /( *)(static )?(const )?(union|struct) ([a-zA-Z_\$][\$0-9a-zA-Z_]+ )?\{/) {
  51. $in_su = 1;
  52. $indent = $1;
  53. $out = $_;
  54. next;
  55. } else {
  56. do_output($_);
  57. }
  58. }
  59. sub structureData {
  60. my $data = $_[0];
  61. my @datalist = split(/(\{|\}|,|"|#|\n|\/\*|\*\/|\(|\))/, $data);
  62. my $item;
  63. my $dataitem = "";
  64. my @struclist = ();
  65. my $substruc;
  66. my $inquote = 0;
  67. my $inbrace = 0;
  68. my $preproc = 0;
  69. my $comment = 0;
  70. my $inparen = 0;
  71. foreach $item (@datalist) {
  72. if($comment) {
  73. if($item eq "*/") {
  74. $comment = 0;
  75. $dataitem .= "*/";
  76. push @struclist, $dataitem;
  77. $dataitem = "";
  78. next;
  79. }
  80. $dataitem .= $item;
  81. next;
  82. }
  83. if($inquote) {
  84. $dataitem .= $item;
  85. if($item eq "\"") {
  86. $inquote--;
  87. }
  88. next;
  89. }
  90. if($preproc) {
  91. if($item eq "\n") {
  92. $preproc = 0;
  93. push @struclist, $dataitem;
  94. $dataitem = "";
  95. next;
  96. }
  97. $dataitem .= $item;
  98. next;
  99. }
  100. if($inbrace) {
  101. if($item eq "}") {
  102. $inbrace --;
  103. if(!$inbrace) {
  104. $substruc = structureData($dataitem);
  105. $dataitem = $substruc;
  106. next;
  107. }
  108. } elsif($item eq "{") {
  109. $inbrace++;
  110. } elsif ($item eq "\"") {
  111. $inquote++;
  112. }
  113. $dataitem .= $item;
  114. next;
  115. }
  116. if($inparen) {
  117. if($item eq ")") {
  118. $inparen--;
  119. }
  120. $dataitem .= $item;
  121. next;
  122. }
  123. if($item eq "\n") {
  124. next;
  125. }
  126. if($item eq "#") {
  127. $preproc = 1;
  128. push @struclist, $dataitem;
  129. $dataitem = "#";
  130. next;
  131. }
  132. if($item eq "/*") {
  133. $comment = 1;
  134. push @struclist, $dataitem;
  135. $dataitem= "/*";
  136. next;
  137. }
  138. if($item eq "\"") {
  139. $dataitem .= $item;
  140. $inquote++;
  141. next;
  142. }
  143. if($item eq "{") {
  144. $inbrace++;
  145. next;
  146. }
  147. if($item eq ",") {
  148. push @struclist, $dataitem;
  149. $dataitem = "";
  150. next;
  151. }
  152. if($item eq "(") {
  153. $dataitem .= $item;
  154. $inparen++;
  155. next;
  156. }
  157. if($item =~ /^\s*$/) {
  158. next;
  159. }
  160. if(ref $dataitem eq 'ARRAY') {
  161. push @struclist, $dataitem;
  162. $dataitem = "";
  163. }
  164. $dataitem .= $item;
  165. }
  166. push @struclist, $dataitem;
  167. return \@struclist;
  168. }
  169. sub displayData {
  170. my $indent = shift;
  171. my $depth = shift;
  172. my $data = shift;
  173. my $item;
  174. my $out = "";
  175. my $currline = "";
  176. my $first = 1;
  177. my $prevpreproc = 0;
  178. my $prevcomment = 0;
  179. foreach $item (@{$data}) {
  180. if($item =~ /^\/\*/) {
  181. #Comment
  182. $item =~ s/\n/\n$indent/g;
  183. if($out =~ /\n\s*$/s) {
  184. $out .= $item."\n".$indent;
  185. } else {
  186. $out .= "\n".$indent.$item."\n".$indent;
  187. }
  188. $currline = $indent;
  189. $prevcomment = 1;
  190. next;
  191. }
  192. $item =~ s/^\s+//;
  193. if($item =~ /^#/) {
  194. #Pre-processor directive
  195. if($out =~ /\n\s*$/s) {
  196. $out =~ s/\n\s*$/\n/;
  197. $out .= $item."\n".$indent;
  198. } else {
  199. $out .= "\n".$item."\n".$indent;
  200. }
  201. $currline = $indent;
  202. $prevpreproc = 1;
  203. next;
  204. }
  205. if($first) {
  206. $first = 0;
  207. if($depth != 0) {
  208. $out .= $indent;
  209. $currline = $indent;
  210. }
  211. } else {
  212. if(!$prevpreproc && !$prevcomment) {
  213. $out .= ", ";
  214. $currline .= ", ";
  215. if($depth == 1) {
  216. $out .= "\n";
  217. $currline = "";
  218. }
  219. if($depth == 1) {
  220. $out .= $indent;
  221. $currline .= $indent;
  222. }
  223. }
  224. }
  225. $prevpreproc = 0;
  226. $prevcomment = 0;
  227. if (ref $item eq 'ARRAY') {
  228. if($depth == 0) {
  229. $out .= displayData("$indent ", $depth+1, $item);
  230. } else {
  231. $out .= "{\n".displayData("$indent ", $depth+1, $item)."\n".$indent."}";
  232. $currline = $indent."}";
  233. }
  234. } else {
  235. if(length $currline.$item > 79) {
  236. $currline = $indent;
  237. $out .= "\n$indent";
  238. }
  239. $out .= $item;
  240. $currline .= $item;
  241. }
  242. }
  243. return $out;
  244. }
  245. sub do_output {
  246. my $out = shift;
  247. # Strip any trailing whitespace
  248. $out =~ s/\s+\n/\n/g;
  249. print $out;
  250. }