Home | History | Annotate | Line # | Download | only in getNAME
getNAME.c revision 1.8
      1 /*	$NetBSD: getNAME.c,v 1.8 1997/11/01 15:03:54 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #if 0
     41 static char sccsid[] = "@(#)getNAME.c	8.1 (Berkeley) 6/30/93";
     42 #else
     43 __RCSID("$NetBSD: getNAME.c,v 1.8 1997/11/01 15:03:54 mrg Exp $");
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * Get name sections from manual pages.
     49  *	-t	for building toc
     50  *	-i	for building intro entries
     51  *	-w	for querying type of manual source
     52  *	other	apropos database
     53  */
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 
     58 int tocrc;
     59 int intro;
     60 int typeflag;
     61 
     62 void doname __P((char *));
     63 void dorefname __P((char *));
     64 void getfrom __P((char *));
     65 void split __P((char *, char *));
     66 void trimln __P((char *));
     67 void usage __P((void));
     68 int main __P((int, char *[]));
     69 
     70 int
     71 main(argc, argv)
     72 	int argc;
     73 	char *argv[];
     74 {
     75 	int ch;
     76 
     77 	while ((ch = getopt(argc, argv, "itw")) != -1)
     78 		switch (ch) {
     79 		case 'i':
     80 			intro = 1;
     81 			break;
     82 		case 't':
     83 			tocrc = 1;
     84 			break;
     85 		case 'w':
     86 			typeflag = 1;
     87 			break;
     88 		case '?':
     89 		default:
     90 			usage();
     91 		}
     92 	argc -= optind;
     93 	argv += optind;
     94 
     95 	if (!*argv)
     96 		usage();
     97 
     98 	for (; *argv; ++argv)
     99 		getfrom(*argv);
    100 	exit(0);
    101 }
    102 
    103 void
    104 getfrom(pathname)
    105 	char *pathname;
    106 {
    107 	int i = 0;
    108 	char *name, *loc, *s, *t;
    109 	char headbuf[BUFSIZ];
    110 	char linbuf[BUFSIZ];
    111 	char savebuf[BUFSIZ];
    112 
    113 	if (freopen(pathname, "r", stdin) == 0) {
    114 		perror(pathname);
    115 		return;
    116 	}
    117 	if ((name = strrchr(pathname, '/')))
    118 		name++;
    119 	else
    120 		name = pathname;
    121 	for (;;) {
    122 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL) {
    123 			if (typeflag)
    124 				printf("%-60s	UNKNOWN\n", pathname);
    125 			return;
    126 		}
    127 		if (headbuf[0] != '.')
    128 			continue;
    129 		if ((headbuf[1] == 'T' && headbuf[2] == 'H') ||
    130 		    (headbuf[1] == 't' && headbuf[2] == 'h'))
    131 			break;
    132 		if (headbuf[1] == 'D' && headbuf[2] == 't') {
    133 			if (typeflag) {
    134 				printf("%-60s	NEW\n", pathname);
    135 				return;
    136 			}
    137 			goto newman;
    138 		}
    139 	}
    140 	if (typeflag) {
    141 		printf("%-60s	OLD\n", pathname);
    142 		return;
    143 	}
    144 	for (;;) {
    145 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
    146 			return;
    147 		if (linbuf[0] != '.')
    148 			continue;
    149 		if (linbuf[1] == 'S' && linbuf[2] == 'H')
    150 			break;
    151 		if (linbuf[1] == 's' && linbuf[2] == 'h')
    152 			break;
    153 	}
    154 	trimln(headbuf);
    155 	if (tocrc)
    156 		doname(name);
    157 	linbuf[0] = '\0';
    158 	for (;;) {
    159 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
    160 			break;
    161 		if (headbuf[0] == '.') {
    162 			if (headbuf[1] == 'S' && headbuf[2] == 'H')
    163 				break;
    164 			if (headbuf[1] == 's' && headbuf[2] == 'h')
    165 				break;
    166 		}
    167 		if (i != 0)
    168 			strcat(linbuf, " ");
    169 		i++;
    170 		trimln(headbuf);
    171 		strcat(linbuf, headbuf);
    172 		/* change the \- into (N) - */
    173 		if ((s = strstr(linbuf, "\\-")) != NULL) {
    174 			strncpy(savebuf, s+1, BUFSIZ);
    175 			if ((t = strchr(name, '.')) != NULL) {
    176 				t++;
    177 				*s++ = '(';
    178 				while (*t)
    179 					*s++ = *t++;
    180 				*s++ = ')';
    181 				*s++ = ' ';
    182 				*s++ = '\0';
    183 			}
    184 			strcat(linbuf, savebuf);
    185 		}
    186 	}
    187 	if (intro)
    188 		split(linbuf, name);
    189 	else
    190 		printf("%s\n", linbuf);
    191 	return;
    192 
    193 newman:
    194 	for (;;) {
    195 		if (fgets(linbuf, sizeof linbuf, stdin) == NULL)
    196 			return;
    197 		if (linbuf[0] != '.')
    198 			continue;
    199 		if (linbuf[1] == 'S' && linbuf[2] == 'h')
    200 			break;
    201 	}
    202 	trimln(headbuf);
    203 	if (tocrc)
    204 		doname(name);
    205 	linbuf[0] = '\0';
    206 	for (;;) {
    207 		if (fgets(headbuf, sizeof headbuf, stdin) == NULL)
    208 			break;
    209 		if (headbuf[0] == '.') {
    210 			if (headbuf[1] == 'S' && headbuf[2] == 'h')
    211 				break;
    212 		}
    213 		if (i != 0)
    214 			strcat(linbuf, " ");
    215 		i++;
    216 		trimln(headbuf);
    217 		for (loc = strchr(headbuf, ' '); loc; loc = strchr(loc, ' '))
    218 			if (loc[1] == ',')
    219 				strcpy(loc, &loc[1]);
    220 			else
    221 				loc++;
    222 		if (headbuf[0] != '.') {
    223 			strcat(linbuf, headbuf);
    224 		} else {
    225 			/*
    226 			 * Get rid of quotes in macros.
    227 			 */
    228 			for (loc = strchr(&headbuf[4], '"'); loc; ) {
    229 				strcpy(loc, &loc[1]);
    230 				loc = strchr(loc, '"');
    231 			}
    232 			/*
    233 			 * Handle cross references
    234 			 */
    235 			if (headbuf[1] == 'X' && headbuf[2] == 'r') {
    236 				for (loc = &headbuf[4]; *loc != ' '; loc++)
    237 					continue;
    238 				loc[0] = '(';
    239 				loc[2] = ')';
    240 				loc[3] = '\0';
    241 			}
    242 
    243 			/*
    244 			 * Put section and dash between names and description.
    245 			 */
    246 			if (headbuf[1] == 'N' && headbuf[2] == 'd') {
    247 				if ((t = strchr(name, '.')) != NULL) {
    248 					strcat(linbuf, "(");
    249 					strcat(linbuf, t+1);
    250 					strcat(linbuf, ") ");
    251 				}
    252 				strcat(linbuf, "- ");
    253 			}
    254 			/*
    255 			 * Skip over macro names.
    256 			 */
    257 			strcat(linbuf, &headbuf[4]);
    258 		}
    259 	}
    260 	if (intro)
    261 		split(linbuf, name);
    262 	else
    263 		printf("%s\n", linbuf);
    264 }
    265 
    266 void
    267 trimln(cp)
    268 	register char *cp;
    269 {
    270 
    271 	while (*cp)
    272 		cp++;
    273 	if (*--cp == '\n')
    274 		*cp = 0;
    275 }
    276 
    277 void
    278 doname(name)
    279 	char *name;
    280 {
    281 	register char *dp = name, *ep;
    282 
    283 again:
    284 	while (*dp && *dp != '.')
    285 		putchar(*dp++);
    286 	if (*dp)
    287 		for (ep = dp+1; *ep; ep++)
    288 			if (*ep == '.') {
    289 				putchar(*dp++);
    290 				goto again;
    291 			}
    292 	putchar('(');
    293 	if (*dp)
    294 		dp++;
    295 	while (*dp)
    296 		putchar (*dp++);
    297 	putchar(')');
    298 	putchar(' ');
    299 }
    300 
    301 void
    302 split(line, name)
    303 	char *line, *name;
    304 {
    305 	register char *cp, *dp;
    306 	char *sp, *sep;
    307 
    308 	cp = strchr(line, '-');
    309 	if (cp == 0)
    310 		return;
    311 	sp = cp + 1;
    312 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
    313 		;
    314 	*++cp = '\0';
    315 	while (*sp && (*sp == ' ' || *sp == '\t'))
    316 		sp++;
    317 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
    318 		cp = strchr(dp, ',');
    319 		if (cp) {
    320 			register char *tp;
    321 
    322 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
    323 				;
    324 			*++tp = '\0';
    325 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
    326 				;
    327 		}
    328 		printf("%s%s\t", sep, dp);
    329 		dorefname(name);
    330 		printf("\t%s", sp);
    331 	}
    332 }
    333 
    334 void
    335 dorefname(name)
    336 	char *name;
    337 {
    338 	register char *dp = name, *ep;
    339 
    340 again:
    341 	while (*dp && *dp != '.')
    342 		putchar(*dp++);
    343 	if (*dp)
    344 		for (ep = dp+1; *ep; ep++)
    345 			if (*ep == '.') {
    346 				putchar(*dp++);
    347 				goto again;
    348 			}
    349 	putchar('.');
    350 	if (*dp)
    351 		dp++;
    352 	while (*dp)
    353 		putchar (*dp++);
    354 }
    355 
    356 void
    357 usage()
    358 {
    359 	(void)fprintf(stderr, "usage: getNAME [-it] file ...\n");
    360 	exit(1);
    361 }
    362