Home | History | Annotate | Line # | Download | only in getNAME
getNAME.c revision 1.2
      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[] = "from: @(#)getNAME.c	5.4 (Berkeley) 1/20/91";*/
     42 static char rcsid[] = "$Id: getNAME.c,v 1.2 1993/08/01 18:30:33 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * Get name sections from manual pages.
     47  *	-t	for building toc
     48  *	-i	for building intro entries
     49  *	other	apropos database
     50  */
     51 #include <stdio.h>
     52 #include <string.h>
     53 
     54 int tocrc;
     55 int intro;
     56 
     57 main(argc, argv)
     58 	int argc;
     59 	char **argv;
     60 {
     61 	extern int optind;
     62 	int ch;
     63 
     64 	while ((ch = getopt(argc, argv, "it")) != EOF)
     65 		switch(ch) {
     66 		case 'i':
     67 			intro = 1;
     68 			break;
     69 		case 't':
     70 			tocrc = 1;
     71 			break;
     72 		case '?':
     73 		default:
     74 			usage();
     75 		}
     76 	argc -= optind;
     77 	argv += optind;
     78 
     79 	if (!*argv)
     80 		usage();
     81 
     82 	for (; *argv; ++argv)
     83 		getfrom(*argv);
     84 	exit(0);
     85 }
     86 
     87 getfrom(name)
     88 	char *name;
     89 {
     90 	int i = 0;
     91 	char headbuf[BUFSIZ];
     92 	char linbuf[BUFSIZ];
     93 
     94 	if (freopen(name, "r", stdin) == 0) {
     95 		perror(name);
     96 		return;
     97 	}
     98 	for (;;) {
     99 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
    100 			return;
    101 		if (headbuf[0] != '.')
    102 			continue;
    103 		if (headbuf[1] == 'T' && headbuf[2] == 'H')
    104 			break;
    105 		if (headbuf[1] == 't' && headbuf[2] == 'h')
    106 			break;
    107 	}
    108 	for (;;) {
    109 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
    110 			return;
    111 		if (linbuf[0] != '.')
    112 			continue;
    113 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
    114 			break;
    115 		if (linbuf[1] == 's' && linbuf[2] == 'h')
    116 			break;
    117 	}
    118 	trimln(headbuf);
    119 	if (tocrc)
    120 		doname(name);
    121 	if (!intro)
    122 		printf("%s\t", headbuf);
    123 	for (;;) {
    124 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
    125 			break;
    126 		if (linbuf[0] == '.') {
    127 			if (linbuf[1] == 'S' && linbuf[2] == 'H')
    128 				break;
    129 			if (linbuf[1] == 's' && linbuf[2] == 'h')
    130 				break;
    131 		}
    132 		trimln(linbuf);
    133 		if (intro) {
    134 			split(linbuf, name);
    135 			continue;
    136 		}
    137 		if (i != 0)
    138 			printf(" ");
    139 		i++;
    140 		printf("%s", linbuf);
    141 	}
    142 	printf("\n");
    143 }
    144 
    145 trimln(cp)
    146 	register char *cp;
    147 {
    148 
    149 	while (*cp)
    150 		cp++;
    151 	if (*--cp == '\n')
    152 		*cp = 0;
    153 }
    154 
    155 doname(name)
    156 	char *name;
    157 {
    158 	register char *dp = name, *ep;
    159 
    160 again:
    161 	while (*dp && *dp != '.')
    162 		putchar(*dp++);
    163 	if (*dp)
    164 		for (ep = dp+1; *ep; ep++)
    165 			if (*ep == '.') {
    166 				putchar(*dp++);
    167 				goto again;
    168 			}
    169 	putchar('(');
    170 	if (*dp)
    171 		dp++;
    172 	while (*dp)
    173 		putchar (*dp++);
    174 	putchar(')');
    175 	putchar(' ');
    176 }
    177 
    178 split(line, name)
    179 	char *line, *name;
    180 {
    181 	register char *cp, *dp;
    182 	char *sp, *sep;
    183 
    184 	cp = index(line, '-');
    185 	if (cp == 0)
    186 		return;
    187 	sp = cp + 1;
    188 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
    189 		;
    190 	*++cp = '\0';
    191 	while (*sp && (*sp == ' ' || *sp == '\t'))
    192 		sp++;
    193 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
    194 		cp = index(dp, ',');
    195 		if (cp) {
    196 			register char *tp;
    197 
    198 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
    199 				;
    200 			*++tp = '\0';
    201 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
    202 				;
    203 		}
    204 		printf("%s%s\t", sep, dp);
    205 		dorefname(name);
    206 		printf("\t%s", sp);
    207 	}
    208 }
    209 
    210 dorefname(name)
    211 	char *name;
    212 {
    213 	register char *dp = name, *ep;
    214 
    215 again:
    216 	while (*dp && *dp != '.')
    217 		putchar(*dp++);
    218 	if (*dp)
    219 		for (ep = dp+1; *ep; ep++)
    220 			if (*ep == '.') {
    221 				putchar(*dp++);
    222 				goto again;
    223 			}
    224 	putchar('.');
    225 	if (*dp)
    226 		dp++;
    227 	while (*dp)
    228 		putchar (*dp++);
    229 }
    230 
    231 usage()
    232 {
    233 	(void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
    234 	exit(1);
    235 }
    236