home *** CD-ROM | disk | FTP | other *** search
/ Dream 48 / Amiga_Dream_48.iso / LinuxPpc / contrib / makewhatis < prev    next >
Text File  |  1998-01-19  |  8KB  |  298 lines

  1. #!/bin/sh
  2. # makewhatis: create the whatis database
  3. # Created: Sun Jun 14 10:49:37 1992
  4. # Revised: Sat Jan  8 14:12:37 1994 by faith@cs.unc.edu
  5. # Revised: Sat Mar 23 17:56:18 1996 by micheal@actrix.gen.nz
  6. # Copyright 1992, 1993, 1994 Rickard E. Faith (faith@cs.unc.edu)
  7. # May be freely distributed and modified as long as copyright is retained.
  8. #
  9. # Wed Dec 23 13:27:50 1992: Rik Faith (faith@cs.unc.edu) applied changes
  10. # based on Mitchum DSouza (mitchum.dsouza@mrc-apu.cam.ac.uk) cat patches.
  11. # Also, cleaned up code and make it work with NET-2 doc pages.
  12. #
  13. # makewhatis-1.4: aeb 940802, 941007, 950417
  14. # Fixed so that the -c option works correctly for the cat pages
  15. # on my machine. Fix for -u by Nan Zou (nan@ksu.ksu.edu).
  16. # Many minor changes.
  17. # The -s option is undocumented, and may well disappear again.
  18. #
  19. # Sat Mar 23 1996: Michael Hamilton (michael@actrix.gen.nz).
  20. # I changed the script to invoke gawk only once for each directory tree.
  21. # This speeds things up considerably (from 30 minutes down to 1.5 minutes
  22. # on my 486DX66).
  23. # 960401 - aeb: slight adaptation to work correctly with cat pages.
  24. # 960510 - added fixes by brennan@raven.ca.boeing.com, author of mawk.
  25. #
  26. # Note for Slackware users: "makewhatis -v -w -c" will work.
  27.  
  28. PATH=/usr/bin:/bin
  29.  
  30. DEFMANPATH=/usr/man
  31. DEFCATPATH=/usr/man/preformat:/usr/man
  32.  
  33. TMPFILE=/root/whatis$$
  34.  
  35. topath=manpath
  36.  
  37. defmanpath=$DEFMANPATH
  38. defcatpath=
  39.  
  40. sections="1 2 3 4 5 6 7 8 9 n l"
  41.  
  42. for name in $*
  43. do
  44. if [ -n "$setsections" ]; then
  45.     setsections=
  46.     sections=$name
  47.     continue
  48. fi
  49. case $name in
  50.     -c) topath=catpath
  51.     defmanpath=
  52.     defcatpath=$DEFCATPATH
  53.         continue;;
  54.     -s) setsections=1
  55.         continue;;
  56.     -u) findarg="-ctime 0"
  57.         update=1
  58.         continue;;
  59.     -v) verbose=1
  60.     continue;;
  61.     -w) manpath=`man --path`
  62.     continue;;
  63.     -*) echo "Usage: makewhatis [-u] [-v] [-w] [manpath] [-c [catpath]]"
  64.     echo "       This will build the whatis database for the man pages"
  65.     echo "       found in manpath and the cat pages found in catpath."
  66.         echo "       -u: update database with new pages"
  67.     echo "       -v: verbose"
  68.     echo "       -w: use manpath obtained from \`man --path\`"
  69.         echo "       [manpath]: man directories (default: $DEFMANPATH)"
  70.     echo "       [catpath]: cat directories (default: the first existing"
  71.     echo "           directory in $DEFCATPATH)"
  72.         exit;;
  73.      *) if [ -d $name ]
  74.         then
  75.             eval $topath=$"$topath":$name
  76.         else
  77.             echo "No such directory $name"
  78.             exit
  79.         fi;;
  80. esac
  81. done
  82.  
  83. manpath=`echo ${manpath-$defmanpath} | tr : ' '`
  84. if [ -z "$catpath" ]; then
  85.    for d in `echo $defcatpath | tr : ' '`
  86.    do
  87.       if [ -d $d ]; then catpath=$d; break; fi
  88.    done
  89. fi
  90. catpath=`echo ${catpath} | tr : ' '`
  91.  
  92. # first remove all the whatis files that will be created new,
  93. # then only update - we might visit the same directory twice
  94. if [ -z $update ]; then
  95.    for pages in man cat
  96.    do
  97.       eval path=$"$pages"path
  98.       for mandir in $path
  99.       do
  100.          rm -f $mandir/whatis
  101.       done
  102.    done
  103. fi
  104.  
  105. for pages in man cat
  106. do
  107.    export pages
  108.    eval path=$"$pages"path
  109.    for mandir in $path
  110.    do
  111.      if [ x$verbose != x ]; then
  112.         echo "about to enter $mandir" > /dev/tty
  113.      fi
  114.      if [ -f ${mandir}/whatis -a $pages = man ]; then
  115.         if [ x$verbose != x ]; then
  116.            echo skipping $mandir - we did it already > /dev/tty
  117.         fi
  118.      else      
  119.        here=`pwd`
  120.        cd $mandir
  121.        for i in $sections
  122.        do
  123.          if [ -d ${pages}$i ]
  124.          then
  125.             cd ${pages}$i
  126.             section=$i
  127.             export section verbose
  128.         find . -name '*' $findarg -print | /usr/bin/gawk '
  129.         function readline() {
  130.           if (use_zcat) {
  131.         result = (pipe_cmd | getline);
  132.         if (result < 0) {
  133.           print "Pipe error: " pipe_cmd " " ERRNO > "/dev/stderr";
  134.         }
  135.           } else {
  136.         result = (getline < filename);
  137.         if (result < 0) {
  138.           print "Read file error: " filename " " ERRNO > "/dev/stderr";
  139.         }
  140.           }
  141.           return result;
  142.         }
  143.         
  144.         function closeline() {
  145.           if (use_zcat) {
  146.         return close(pipe_cmd);
  147.           } else {
  148.         return close(filename);
  149.           }
  150.         }
  151.         
  152.         function do_one() {
  153.           after = 0; insh = 0; thisjoin = 1; charct = 0;
  154.  
  155.           if (verbose) {
  156.         print "adding " filename > "/dev/tty"
  157.           }
  158.           
  159.           use_zcat = (filename ~ /\.Z$/ || filename ~ /\.z$/ ||
  160.               filename ~ /\.gz$/);
  161.           match(filename, "/[^/]+$");
  162.           progname = substr(filename, RSTART + 1, RLENGTH);
  163.           sub(/\..*/, "", progname);
  164.           if (use_zcat) {
  165.         pipe_cmd = "zcat " filename;
  166.           }
  167.         
  168.           while (readline() > 0) {
  169.         gsub(/.\b/, "");
  170.         if (($1 ~ /^\.[Ss][Hh]/ && $2 ~ /[Nn][Aa][Mm][Ee]/) ||
  171.           (pages == "cat" && $1 ~ /^NAME/)) {
  172.             if (!insh)
  173.               insh = 1;
  174.             else {
  175.               printf "\n";
  176.               closeline();
  177.                       return;
  178.             }
  179.         } else if (insh) {
  180.           if ($1 ~ /^\.[Ss][HhYS]/ ||
  181.             (pages == "cat" &&
  182.             ($1 ~ /^S[yYeE]/ || $1 ~ /^DESCRIPTION/ ||
  183.             $1 ~ /^COMMAND/ || $1 ~ /^OVERVIEW/ ||
  184.             $1 ~ /^STRUCTURES/ || $1 ~ /^INTRODUCTION/))) {
  185.               # end insh for Synopsis, Syntax, but also for
  186.               # DESCRIPTION (e.g., XFree86.1x),
  187.               # COMMAND (e.g., xspread.1)
  188.               # OVERVIEW (e.g., TclCommandWriting.3)
  189.               # STRUCTURES (e.g., XEvent.3x)
  190.               # INTRODUCTION (e.g., TclX.n)
  191.               printf "\n";
  192.               closeline();
  193.                       return; 
  194.           } else { # derived from Tom Christiansen perl script
  195.               if (!after && $0 ~ progname"-") {  # Fix old cat pages
  196.               sub(progname"-", progname" - ");
  197.               }
  198.               gsub(/    /, " ");        # Translate tabs to spaces
  199.               gsub(/  +/, " ");         # Collapse spaces
  200.               gsub(/ *, */, ", ");      # Fix comma spacings
  201.               sub(/^ /, "");            # Kill initial spaces
  202.               sub(/ $/, "");            # Kill trailing spaces
  203.               sub(/__+/, "_");          # Collapse underscores
  204.               if ($0 ~ /[^ ]-$/) {
  205.             sub(/-$/, "");          # Handle Hyphenations
  206.             nextjoin = 1;
  207.               } else
  208.             nextjoin = 0;
  209.             sub(/^.[IB] /, "");       # Kill bold and italics
  210.             sub(/^.Nm /, "");         # Kill bold
  211.             sub(/^.Tn /, "");         # Kill normal
  212.             sub(/^.Li /, "");         # Kill .Li
  213.             sub(/^.Dq /, "");         # Kill .Dq
  214.             sub(/^.Nd */, "- ");      # Convert .Nd to dash
  215.             gsub(/\\f[PRIB0123]/, "");   # Kill font changes
  216.             gsub(/\\s[-+0-9]*/, "");     # Kill size changes
  217.             gsub(/\\&/, "");             # Kill \&
  218.             gsub(/\\\((ru|ul)/, "_");    # Translate
  219.             gsub(/\\\((mi|hy|em)/, "-"); # Translate
  220.             gsub(/\\\*\(../, "");        # Kill troff strings
  221.             sub(/^\.\\\".*/, "");        # Kill comments
  222.             gsub(/\\/, "");              # Kill all backslashes
  223.             if ($1 ~ /^\.../ || $1 == "") {
  224.               if (after && !needmore) {
  225.                 printf "\n";
  226.                 thisjoin = 1;
  227.                 charct = 0;
  228.                 after = 0;
  229.               }
  230.             } else {
  231.               if ($0 ~ /^- /) {
  232.                 sub("- ", " - ");
  233.               } else if (!thisjoin && $0 !~ /^- /) {
  234.                 printf " ";
  235.                 charct += 1;
  236.               }
  237.               thisjoin = nextjoin;
  238.               if ($0 !~ / - / && $0 !~ / -$/ && $0 !~ /^- /) {
  239.                 printf "%s", $0;
  240.                 charct += length();
  241.                 needmore = 0;
  242.               } else {
  243.                 after = 1
  244.                 if ($0 ~ / - /) {
  245.                   where = match( $0 , / - /);
  246.                 } else if ($0 ~ / -$/) {
  247.                   where = match( $0, / -$/);
  248.                 } else {
  249.                   where = 1;
  250.                 }
  251.                 if ((width = 20-charct) < 0) width=0
  252.                 printf "%-*s", width, sprintf( "%s (%s)",
  253.                 substr( $0, 1, where-1 ), section );
  254.                 printf "%s", substr( $0, where )
  255.                 if ($0 ~ /- *$/) {
  256.                   needmore = 1;
  257.                 } else {
  258.                   needmore = 0;
  259.                 }
  260.               }
  261.             }
  262.               }
  263.            }
  264.         }
  265.           closeline();
  266.         }
  267.  
  268.             {            # Main action - process each filename read in.
  269.           filename = $0;
  270.           do_one();
  271.         }
  272.         ' pages=$pages section=$section verbose=$verbose
  273.             cd ..
  274.          fi
  275.        done > $TMPFILE
  276.  
  277.        cd $here
  278.  
  279.        # kludge for Slackware's /usr/man/preformat
  280.        if [ $mandir = /usr/man/preformat ]
  281.        then
  282.          mandir1=/usr/man
  283.        else
  284.          mandir1=$mandir
  285.        fi
  286.  
  287.        if [ -f ${mandir1}/whatis ]
  288.        then
  289.          cat ${mandir1}/whatis >> $TMPFILE
  290.        fi
  291.        sed '/^$/d' < $TMPFILE | sort | uniq > ${mandir1}/whatis
  292.  
  293.        chmod 644 ${mandir1}/whatis
  294.        rm $TMPFILE
  295.      fi
  296.    done
  297. done
  298.