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