home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Util / helpfiles next >
Encoding:
Text File  |  1996-08-23  |  3.6 KB  |  166 lines

  1. #!/usr/local/bin/perl -- -*-perl-*-
  2.  
  3. # helpfiles:  make help files for Z-shell builtins from the manual entries.
  4. # Author:  Peter Stephenson <pws@ifh.de>
  5.  
  6. # Create help files for zsh commands in the current directory;
  7. # assumes no other files are present.
  8. # No overwriting check;  `.' becomes `dot', `:' becomes `colon'.
  9.  
  10. # Any command claiming to be `same as <foo>' or `equivalent to <foo>'
  11. # has its help file appended to the end of <foo>'s and replaced by a
  12. # link to <foo>.  (Arguably the help file should be put at the start
  13. # instead.)
  14.  
  15. # Takes one filename argument, or stdin: the zsh manual page as a plain
  16. # ascii file: `man zshbuiltins | colcrt -' (remember the -) should do the
  17. # trick.
  18.  
  19. # If you don't have colcrt, try 'col -bx'.  The x is necessary so that
  20. # spaces don't turn into tabs, which messes up the calculations of
  21. # indentation on machines which randomly wrap lines round to the
  22. # previous line (so you see what we're up against).
  23.  
  24. # Example usage:
  25. #    cd ~/zsh-3.0                # or wherever
  26. #    mkdir Help
  27. #    cd Help
  28. #    man zshbuiltins | colcrt - | helpfiles
  29. #    run-help() {
  30. #      typeset zhelp=~/zsh-3.0/Help        # or wherever
  31. #      [[ $1 == . ]] && 1=dot
  32. #      [[ $1 == : ]] && 1=colon
  33. #      if [[ -f $zhelp/$1 ]]; then
  34. #          ${=PAGER:-more} $zhelp/$1
  35. #      else
  36. #          man $1
  37. #      fi
  38. #    }
  39. # now <Esc>-h works for shell builtins.
  40.  
  41. while (<>) {
  42.     last if /^SHELL BUILTIN COMMANDS/;
  43.     /zshbuiltins/ && $zb++;
  44.     last if ($zb && /^\s*DESCRIPTIONS/);
  45. }
  46.  
  47. $print = 0;
  48.  
  49. sub namesub {
  50.     local($cmd) = shift;
  51.     if ($cmd =~ /^\w+$/) {
  52.     $cmd;
  53.     } elsif ($cmd eq '.') {
  54.     'dot';
  55.     } elsif ($cmd eq ':') {
  56.     'colon';
  57.     } else {
  58.     undef;
  59.     }
  60. }
  61.  
  62. sub getsame {
  63.     local($_) = shift;
  64.     if (/same\s*as\s*(\S+)/i || /equivalent\s*to\s*(\S+)/i) {
  65.     local($name) = $1;
  66.     ($name =~ /[.,]$/) && chop($name);
  67.     return $name;
  68.     } else {
  69.     return undef;
  70.     }
  71. }
  72.  
  73. sub newcmd {
  74.     local($_) = shift;
  75.     local($cmd);
  76.     # new command
  77.     if (defined($cmd = &namesub($_))) {
  78.     # in case there's something nasty here like a link..
  79.     unlink $cmd;
  80.     open (OUT, ">$cmd");
  81.     select OUT;
  82.     $print = 1;
  83.     } else {
  84.     $print = 0;
  85.     }
  86. }
  87.  
  88. while (<>) { last unless /^\s*$/; }
  89.  
  90. /^(\s+)(\S+)/;
  91. $indent = length($1);
  92. &newcmd($2);
  93. print if $print;
  94.  
  95. BUILTINS: while (<>) {
  96.     next if /^\w/;
  97.  
  98.     undef($undented);
  99.     if (/^\s*$/ || ($undented = (/^(\s*)/  && length($1) < $indent))) {
  100.     $undented && print;
  101.     while (defined($_ = <>) && /(^\w)|(^\s*$)/) { 
  102.         last BUILTINS if /^STARTUP\/SHUTDOWN FILES/;
  103.     }
  104.         if (/^\s*Page/) {
  105.         do {
  106.         $_ = <>;
  107.         } while (defined($_) && /^\s*$/);
  108.         if (/^\s*ZSHBUILTINS/) {
  109.         do {
  110.             $_ = <>;
  111.         } while (defined($_) && /^\s*$/);
  112.         }
  113.     }
  114.     if (/^(\s*)/ && length($1) < $indent) {
  115.         # This may be just a bug on the SGI, or maybe something
  116.         # more sinister (don\'t laugh, this is nroff).
  117.         s/^\s*/ /;
  118.         $defer = $_;
  119.         do {
  120.         $_ = <>;
  121.         } while (defined($_) && /^\s*$/);
  122.     }
  123.     if (/^(\s+)(\S+)/ && length($1) == $indent) {
  124.         &newcmd($2);
  125.     } else {
  126.         print "\n";
  127.     }
  128.         if ($print) {
  129.         if (defined($defer)) {
  130.         chop;
  131.         print "$_$defer";
  132.         undef($defer);
  133.         } else {
  134.         print;
  135.         }
  136.     }
  137.     } else {
  138.     print if $print;
  139.     }
  140. }
  141.  
  142. select STDOUT;
  143. close OUT;
  144.  
  145. foreach $file (<*>) {
  146.     open (IN, $file);
  147.     if ($sameas = (&getsame($_ = <IN>) || &getsame($_ = <IN>))) {
  148.     defined($sameas = &namesub($sameas)) || next;
  149. #    print "$file is the same as $sameas\n";
  150.     seek (IN, 0, 0);
  151.  
  152.     # Copy this to base builtin.
  153.     open (OUT, ">>$sameas");
  154.     select OUT;
  155.     print "\n";
  156.     while (<IN>) { print; }
  157.     close IN;
  158.     select STDOUT;
  159.     close OUT;
  160.  
  161.     # Make this a link to that.
  162.     unlink $file;
  163.     symlink ($sameas, $file);
  164.     }
  165. }
  166.