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