home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Examples / AutoDoc / etc / format_comment
Encoding:
Text File  |  1995-11-02  |  6.6 KB  |  299 lines

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