home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Apps.iso / Rhapsody / Programming / PBExtensions-1.0-PI / PBExtensions.bundle / Resources / format_comment next >
Encoding:
Text File  |  1997-10-17  |  5.8 KB  |  270 lines

  1. #!/usr/local/bin/perl
  2. require 5.001;
  3.  
  4. # format_comment
  5. #
  6. #     written by bill bumgarner in response to lo's hacquage.  send
  7. # bugs, suggestions, etc. to <bbum@friday.com>
  8. #
  9. # Attempts to intelligently format a c-style comment based on whatever
  10. #     it finds on stdin.  NOTE:  This is NOT for use as a code
  11. #     commentor... it will do evil things to code.
  12. #
  13. #   Features:
  14. #
  15. # - auto wraps to $lineLength characters per line.
  16. # - indents all comments based on indention of first line
  17. # - can re-format existing c comments
  18. # - if the first line contains /*, the characters following the '/*' but
  19. #     not including any random text will be used to determine the format
  20. #     of the comment
  21. # - /*- will create a comment with a row of ----'s at the beginning/end
  22. # - /*= same, but with =====
  23. # - /*+ same, but with +++++
  24. # - /*" surround comment with /*" and "*/ for use with autodoc
  25. # - "" same as above
  26. # - /** create double bullets down left side
  27. # - /*** triple bullets
  28. # - // line left side w/'//'
  29. # - /***= triple bullets w/ =========== at begin/end.
  30. #
  31. #   BUGS:
  32. #
  33. # Not intelligent about the end of the input stream-- if one makes
  34. # a selection in C land, and repeatedly runs this script on the
  35. # selection, the length of the comment will grow by one line each
  36. # time.
  37.  
  38. BEGIN {
  39.     use Getopt::Long;
  40.  
  41.     ##  version & authorship info
  42.     $file_version      = '$Revision: 1.6 $';
  43.     $file_id           = '$Id: format_comment,v 1.6 1995/07/17 23:00:47 bbum Exp $';
  44.     $file_copyright    = 
  45.     'Friday Software & Consulting, 1995 All Rights Reserved';
  46.     $file_author       = '<bbum@friday.com>';
  47.      push (@file_contributors,
  48.        'K. Lo Shih <lo@scissor.com>');
  49.  
  50.     ## post process id and version
  51.     $file_id      =~ s!(\$\w+: |\$)!!g;
  52.     $file_version =~ s!(\$\w+: |\$)!!g;
  53.  
  54.     $show_usage = "unrecognized or invalid argument"
  55.     unless &GetOptions('lib=s@',
  56.                'tab=i',
  57.                'version',
  58.                'help'
  59.                );
  60.  
  61.     ## bogus stuff to shut up -w
  62.     $opt_help     = $opt_help if $opt_help;
  63.     $opt_version = $opt_version if $opt_version;
  64.     $opt_tab     = $opt_tab if $opt_tab;
  65.  
  66.     unshift (@INC, @opt_lib)
  67.     if @opt_lib;
  68. }
  69.  
  70. &usage ($show_usage)
  71.     if $show_usage;
  72.  
  73. &usage
  74.     if $opt_help;
  75.  
  76. &show_version
  77.     if $opt_version;
  78.  
  79. $lineLength = 75;
  80.  
  81. if ($opt_tab) {
  82.   $tabSize    = $opt_tab;
  83. } else {
  84.   $tabSize = 8;
  85. }
  86.  
  87. $tabs        = " " x $tabSize;
  88.  
  89. $: = "\n\t ";
  90.  
  91. # goto first non-blank line
  92. while (<STDIN>) {
  93.     s!\t!$tabs!g;
  94.     last
  95.     if m!\S!;
  96. }
  97.  
  98. # find indention and prefix
  99. $process = $_;
  100. if ($process =~ m!^(\s*)(.*)!) {
  101.     $space   = $1;
  102.     $process = $2;
  103. } else {
  104.     $space = '';
  105. }
  106.  
  107.  
  108. # set prefix and mode (and retrieve end-o-first line)
  109. if ($process =~ m!^\s*""(.*)!) {
  110.     $prefix = '*';
  111.     $mode   = '"';
  112.     $out    = $1;
  113. } elsif ($process =~ m!^\s*/([/*]+)(.*)!) { 
  114.     $prefix = $1;
  115.     $process = $2;
  116.     if ( $process =~ m!(["-=+]*)\s*(.*)!) { ## "
  117.     $mode   = substr $1, 0, 1;
  118.     $out    = $2;
  119.     } else {
  120.     if ($process =~ m!(\s*)(.*)!) {
  121.         $space  .= $1;
  122.         $out     = $2;
  123.     } else {
  124.         $out = '';
  125.     }
  126.     }    
  127. } else {
  128.     $prefix = '*';
  129.     $mode   = '';
  130.     $out    = $process;
  131. }
  132.  
  133. if ($prefix =~ m!^/!) {
  134.     $pLength = length $prefix;
  135.     $prefix = '/' x (($pLength > 1) ? $pLength : 2);
  136.     $doSurround = 0;
  137. } else {
  138.     $prefix = '*' x (length $prefix);
  139.     $doSurround = 1;
  140. }
  141.  
  142. if ((length $prefix) > 1) {
  143.     $hPrefix = '*' x ((length $prefix) - 1);
  144. } else {
  145.     $hPrefix = $prefix;
  146. }
  147.  
  148. $indentLen = (length $space) + 1;
  149. if ($indentLen) {
  150.     $hIndent = " " x ($indentLen - 1);
  151.     # adjust indent according to width of prefix.
  152.     if( (length $prefix) > 1) {
  153.     $indent = $hIndent;
  154.     } else {
  155.     $indent = " " x ($indentLen);
  156.     }
  157. } else {
  158.     $indent = ((length $prefix) > 1) ? "" : " ";
  159.     $hIndent = "";
  160. }
  161.  
  162. ## build line header
  163. $lineHead = "$indent$prefix ";
  164. $outLength = ($lineLength - (length $lineHead));
  165.  
  166. # set up mode
  167. if($mode eq '"') { ## "
  168.    $autodoc = 1;
  169.    $mode = '';
  170. } else {
  171.     $mode = $mode x $outLength;
  172. }
  173.  
  174. # spew opener and mode line
  175. if ($doSurround) {
  176.     if ($autodoc) {
  177.     # autodoc only deals w/a single * between the / and the " ## "
  178.     print "$hIndent/*\"\n"; ## "
  179.     } else {
  180.     if ($mode) {
  181.         print "$hIndent/$hPrefix $mode\n";
  182.     } else {
  183.         print "$hIndent/$hPrefix\n";
  184.     }
  185.     }
  186. }
  187.  
  188. format CODE_TOP =
  189. .
  190.  
  191. # dynamically build format
  192. $format  = "format CODE_OUT = \n$lineHead^" .
  193.     '<' x  ($outLength - 1) . "~~\n" .
  194.     '$out' . "\n.\n";
  195. eval $format;
  196. die $@ if $@;
  197.  
  198. # set page length to [near] infinite (and set up formats)
  199. $- = 100000000;
  200. $~ = CODE_OUT;
  201. $^ = CODE_TOP;
  202.  
  203. $out =~ s!(\S+)\s*$!$1 !;
  204.  
  205. # grab stuff from first line.
  206. $blankLineCount = 0;
  207. while(<STDIN>) {
  208.     # remove whitespace and any '*'s from beginning of line
  209.     s!^\s*"?[*/]*\s*!!; ## "
  210.     s!\t!$tabs!g;
  211.  
  212.     unless(length) {
  213.     # empty line -- flush $out
  214.     write;
  215.     $out = '';
  216.     $blankLineCount++;
  217.     } else {
  218.     # line w/stuff -- concatenate to last line.
  219.     for(;$blankLineCount != 0;$blankLineCount--) {
  220.         print "$lineHead\n";
  221.     }
  222.     $out .= $_;
  223.     }
  224. }
  225.  
  226. write
  227.     if ($out =~ m!\S+!);
  228.  
  229. # spew close
  230. if ($doSurround) {
  231.     if ($autodoc) {
  232.         print "$hIndent\"*/\n";
  233.     } else {
  234.         print "$indent$prefix $mode\n"
  235.         if defined($mode);
  236.         print "$indent$hPrefix/\n";
  237.     }
  238. }
  239.  
  240. sub usage {
  241.     $error_fmt = shift;
  242.     
  243.     $0 = substr $0, (rindex $0, '/')+1;
  244.     
  245.     select(STDERR);
  246.     
  247.     printf "$0: $error_fmt\n", @_
  248.     if $error_fmt;
  249.  
  250.     print <<_ENDOFUSAGE_;
  251. Usage: $0 [-version] [-help]
  252.  
  253.    -version         Display version/copyright and development information.
  254.    -help             Show this help and exit.       
  255. _ENDOFUSAGE_
  256.     exit(1);
  257. }
  258.  
  259. sub show_version
  260. {
  261.     $0 = substr $0, (rindex $0, '/')+1;
  262.     print "This is $0 v$file_version\n";
  263.     print "Copyright $file_copyright\n";
  264.     print "Please send bugs and suggestions to:";
  265.     print "$file_author\n";
  266.     print "Improvements by:\n\t";
  267.     print join("\n\t", @file_contributors), "\n";
  268.     exit(0);
  269. }
  270.