home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / libexec / getNAME / getNAME.c next >
Encoding:
C/C++ Source or Header  |  1991-04-12  |  4.9 KB  |  235 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)getNAME.c    5.4 (Berkeley) 1/20/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * Get name sections from manual pages.
  46.  *    -t    for building toc
  47.  *    -i    for building intro entries
  48.  *    other    apropos database
  49.  */
  50. #include <stdio.h>
  51. #include <string.h>
  52.  
  53. int tocrc;
  54. int intro;
  55.  
  56. main(argc, argv)
  57.     int argc;
  58.     char **argv;
  59. {
  60.     extern int optind;
  61.     int ch;
  62.  
  63.     while ((ch = getopt(argc, argv, "it")) != EOF)
  64.         switch(ch) {
  65.         case 'i':
  66.             intro = 1;
  67.             break;
  68.         case 't':
  69.             tocrc = 1;
  70.             break;
  71.         case '?':
  72.         default:
  73.             usage();
  74.         }
  75.     argc -= optind;
  76.     argv += optind;
  77.  
  78.     if (!*argv)
  79.         usage();
  80.  
  81.     for (; *argv; ++argv)
  82.         getfrom(*argv);
  83.     exit(0);
  84. }
  85.  
  86. getfrom(name)
  87.     char *name;
  88. {
  89.     int i = 0;
  90.     char headbuf[BUFSIZ];
  91.     char linbuf[BUFSIZ];
  92.  
  93.     if (freopen(name, "r", stdin) == 0) {
  94.         perror(name);
  95.         return;
  96.     }
  97.     for (;;) {
  98.         if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
  99.             return;
  100.         if (headbuf[0] != '.')
  101.             continue;
  102.         if (headbuf[1] == 'T' && headbuf[2] == 'H')
  103.             break;
  104.         if (headbuf[1] == 't' && headbuf[2] == 'h')
  105.             break;
  106.     }
  107.     for (;;) {
  108.         if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
  109.             return;
  110.         if (linbuf[0] != '.')
  111.             continue;
  112.         if (linbuf[1] == 'S' && linbuf[2] == 'H')
  113.             break;
  114.         if (linbuf[1] == 's' && linbuf[2] == 'h')
  115.             break;
  116.     }
  117.     trimln(headbuf);
  118.     if (tocrc)
  119.         doname(name);
  120.     if (!intro)
  121.         printf("%s\t", headbuf);
  122.     for (;;) {
  123.         if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
  124.             break;
  125.         if (linbuf[0] == '.') {
  126.             if (linbuf[1] == 'S' && linbuf[2] == 'H')
  127.                 break;
  128.             if (linbuf[1] == 's' && linbuf[2] == 'h')
  129.                 break;
  130.         }
  131.         trimln(linbuf);
  132.         if (intro) {
  133.             split(linbuf, name);
  134.             continue;
  135.         }
  136.         if (i != 0)
  137.             printf(" ");
  138.         i++;
  139.         printf("%s", linbuf);
  140.     }
  141.     printf("\n");
  142. }
  143.  
  144. trimln(cp)
  145.     register char *cp;
  146. {
  147.  
  148.     while (*cp)
  149.         cp++;
  150.     if (*--cp == '\n')
  151.         *cp = 0;
  152. }
  153.  
  154. doname(name)
  155.     char *name;
  156. {
  157.     register char *dp = name, *ep;
  158.  
  159. again:
  160.     while (*dp && *dp != '.')
  161.         putchar(*dp++);
  162.     if (*dp)
  163.         for (ep = dp+1; *ep; ep++)
  164.             if (*ep == '.') {
  165.                 putchar(*dp++);
  166.                 goto again;
  167.             }
  168.     putchar('(');
  169.     if (*dp)
  170.         dp++;
  171.     while (*dp)
  172.         putchar (*dp++);
  173.     putchar(')');
  174.     putchar(' ');
  175. }
  176.  
  177. split(line, name)
  178.     char *line, *name;
  179. {
  180.     register char *cp, *dp;
  181.     char *sp, *sep;
  182.  
  183.     cp = index(line, '-');
  184.     if (cp == 0)
  185.         return;
  186.     sp = cp + 1;
  187.     for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
  188.         ;
  189.     *++cp = '\0';
  190.     while (*sp && (*sp == ' ' || *sp == '\t'))
  191.         sp++;
  192.     for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
  193.         cp = index(dp, ',');
  194.         if (cp) {
  195.             register char *tp;
  196.  
  197.             for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
  198.                 ;
  199.             *++tp = '\0';
  200.             for (++cp; *cp == ' ' || *cp == '\t'; cp++)
  201.                 ;
  202.         }
  203.         printf("%s%s\t", sep, dp);
  204.         dorefname(name);
  205.         printf("\t%s", sp);
  206.     }
  207. }
  208.  
  209. dorefname(name)
  210.     char *name;
  211. {
  212.     register char *dp = name, *ep;
  213.  
  214. again:
  215.     while (*dp && *dp != '.')
  216.         putchar(*dp++);
  217.     if (*dp)
  218.         for (ep = dp+1; *ep; ep++)
  219.             if (*ep == '.') {
  220.                 putchar(*dp++);
  221.                 goto again;
  222.             }
  223.     putchar('.');
  224.     if (*dp)
  225.         dp++;
  226.     while (*dp)
  227.         putchar (*dp++);
  228. }
  229.  
  230. usage()
  231. {
  232.     (void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
  233.     exit(1);
  234. }
  235.