Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.8 KiB

  1. open( EXPRESSION, $ARGV[0] );
  2. $expression = <EXPRESSION>;
  3. close( EXPRESSION );
  4. $subExprIndex=0;
  5. &prettyPrint( $expression, $subExprIndex );
  6. sub prettyPrint
  7. {
  8. local(@calcLparens);
  9. local(@calcRparens);
  10. local(@ops);
  11. local(@addresses);
  12. local($index);
  13. local($indentLevel) = -1;
  14. local($outputThusFar); # Used to determine when to do a newline
  15. local($lparenCnt) = 0; # Keeps count of left parantheses
  16. local($rparenCnt) = 0; # Keeps count of right parantheses
  17. local($subexpr);
  18. # Used to keep track if there are cycles in the call graph
  19. local(%nodeVisits);
  20. @ops = split(/@/, $_[0] );
  21. for ($_ = 1; $_ <= $#ops; $_++ )
  22. {
  23. $ops[$_] =~ /<([0-9]*)>/;
  24. if ( $nodeVisits{ $1 } == undef )
  25. {
  26. $nodeVisits{ $1 } = 1;
  27. }
  28. else
  29. {
  30. $nodeVisits{ $1 } += 1;
  31. }
  32. }
  33. @addresses = keys( %nodeVisits );
  34. for ( $index = 0; $index <= $#addresses; $index++ )
  35. {
  36. if( $nodeVisits{ $addresses[$index] } > 1 )
  37. {
  38. $_ = $_[0];
  39. $subexpr = sprintf( "s%d", ++$subExprIndex );
  40. s/(<$addresses[$index]>.*?<\/$addresses[$index]>)/$subexpr/g;
  41. $_[0] = $_;
  42. if ( length($1) != 0 )
  43. {
  44. &prettyPrint( $1, $subExprIndex );
  45. }
  46. }
  47. }
  48. @ops = split(/@/, $_[0] );
  49. printf( "\ns%d=\n", $_[1] );
  50. for ($index = 1; $index <= $#ops; $index++ )
  51. {
  52. # Add newlines at the end of the string so that the split operator
  53. # will catch parentheses at the end of the string yielding another
  54. # "split" and therefore telling us the number of parantheses.
  55. @calcLparens = split(/\(/, $ops[$index]."\n" );
  56. @calcRparens = split(/\)/, $ops[$index]."\n" );
  57. $lparenCnt += $#calcLparens;
  58. $rparenCnt += $#calcRparens;
  59. # If there are parantheses and the operation is a function
  60. # call, i.e. begins with a capital letter, increment
  61. # our indentation level.
  62. if ( $ops[$index] =~ /\(/ && $ops[$index] =~ /[A-Z]/)
  63. {
  64. $indentLevel += 1;
  65. }
  66. # if we're starting a function print the indentation or
  67. # if the previous line was a function call print the
  68. # indentation.
  69. if ( $ops[$index] =~ /[A-Z]/ || $ops[$index-1] =~ /[A-Z]/)
  70. {
  71. $indentation = &calcIndentation($indentLevel);
  72. $outputThusFar = $indentation;
  73. printf( "%s", $indentation );
  74. }
  75. # Print the operation
  76. $_ = $ops[$index];
  77. s/<[0-9]*>|<\/[0-9]*>//g;
  78. printf( "%s", $_);
  79. # Keep concatenating what we've outputtted thus far in order to
  80. # determine when we should print a newline.
  81. $outputThusFar .= $_;
  82. # If the operation we've outputted is a function call or the next
  83. # operation is a function call, spit out a newline.
  84. if ( $ops[$index] =~ /[A-Z]/ || $ops[$index+1] =~ /[A-Z]/ )
  85. {
  86. printf( "\n" );
  87. }
  88. else
  89. {
  90. if ( length($outputThusFar) >= 60 )
  91. {
  92. printf( "\n%s", $indentation );
  93. # Reset output thus far
  94. $outputThusFar = $indentation;
  95. }
  96. }
  97. if ( $rparenCnt+$indentLevel >= $lparenCnt )
  98. {
  99. $indentLevel = $lparenCnt - $rparenCnt - 1;
  100. }
  101. }
  102. printf( "\n" );
  103. }
  104. sub calcIndentation
  105. {
  106. local($index);
  107. local($calcIndent);
  108. $calcIndent = "";
  109. for ( $index = 0; $index < $_[0]; $index++ )
  110. {
  111. $calcIndent .= " ";
  112. }
  113. $calcIndent;
  114. }