Home | History | Annotate | Line # | Download | only in getNAME
getNAME.c revision 1.13
      1  1.13      fair /*	$NetBSD: getNAME.c,v 1.13 1998/04/10 22:32:45 fair Exp $	*/
      2   1.4       mrg 
      3   1.1       cgd /*-
      4  1.10  christos  * Copyright (c) 1997, Christos Zoulas
      5   1.3       tls  * Copyright (c) 1980, 1993
      6   1.3       tls  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1       cgd  *    must display the following acknowledgement:
     18   1.1       cgd  *	This product includes software developed by the University of
     19   1.1       cgd  *	California, Berkeley and its contributors.
     20  1.10  christos  *	This product includes software developed by Christos Zoulas.
     21   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     22   1.1       cgd  *    may be used to endorse or promote products derived from this software
     23   1.1       cgd  *    without specific prior written permission.
     24   1.1       cgd  *
     25   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1       cgd  * SUCH DAMAGE.
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.4       mrg #include <sys/cdefs.h>
     39   1.1       cgd #ifndef lint
     40   1.4       mrg __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
     41   1.4       mrg 	The Regents of the University of California.  All rights reserved.\n");
     42   1.3       tls #if 0
     43   1.3       tls static char sccsid[] = "@(#)getNAME.c	8.1 (Berkeley) 6/30/93";
     44   1.4       mrg #else
     45  1.13      fair __RCSID("$NetBSD: getNAME.c,v 1.13 1998/04/10 22:32:45 fair Exp $");
     46   1.3       tls #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd /*
     50   1.1       cgd  * Get name sections from manual pages.
     51   1.1       cgd  *	-t	for building toc
     52   1.1       cgd  *	-i	for building intro entries
     53   1.8       mrg  *	-w	for querying type of manual source
     54   1.1       cgd  *	other	apropos database
     55   1.1       cgd  */
     56  1.11     perry #include <err.h>
     57   1.1       cgd #include <stdio.h>
     58   1.3       tls #include <stdlib.h>
     59   1.1       cgd #include <string.h>
     60  1.11     perry #include <unistd.h>
     61  1.10  christos 
     62  1.10  christos static int tocrc;
     63  1.10  christos static int intro;
     64  1.10  christos static int typeflag;
     65  1.10  christos 
     66  1.10  christos #define SLOP 10	/* strlen(" () - ") < 10 */
     67  1.10  christos 
     68  1.10  christos static char *linebuf = NULL;
     69  1.10  christos static size_t maxlen = 0;
     70  1.10  christos 
     71  1.10  christos 
     72  1.10  christos static void doname __P((char *));
     73  1.10  christos static void dorefname __P((char *));
     74  1.10  christos static void getfrom __P((char *));
     75  1.10  christos static void oldman __P((char *, char *));
     76  1.10  christos static void newman __P((char *, char *));
     77  1.10  christos static void remcomma __P((char *, size_t *));
     78  1.10  christos static void remquote __P((char *, size_t *));
     79  1.10  christos static void fixxref __P((char *, size_t *));
     80  1.10  christos static void split __P((char *, char *));
     81  1.10  christos static void usage __P((void));
     82   1.1       cgd 
     83   1.4       mrg int main __P((int, char *[]));
     84   1.3       tls 
     85   1.3       tls int
     86   1.1       cgd main(argc, argv)
     87   1.1       cgd 	int argc;
     88   1.3       tls 	char *argv[];
     89   1.1       cgd {
     90   1.1       cgd 	int ch;
     91   1.1       cgd 
     92   1.5     enami 	while ((ch = getopt(argc, argv, "itw")) != -1)
     93   1.6     enami 		switch (ch) {
     94   1.1       cgd 		case 'i':
     95   1.1       cgd 			intro = 1;
     96   1.1       cgd 			break;
     97   1.1       cgd 		case 't':
     98   1.1       cgd 			tocrc = 1;
     99   1.1       cgd 			break;
    100   1.3       tls 		case 'w':
    101   1.3       tls 			typeflag = 1;
    102   1.3       tls 			break;
    103   1.1       cgd 		case '?':
    104   1.1       cgd 		default:
    105   1.1       cgd 			usage();
    106   1.1       cgd 		}
    107   1.1       cgd 	argc -= optind;
    108   1.1       cgd 	argv += optind;
    109   1.1       cgd 
    110   1.1       cgd 	if (!*argv)
    111   1.1       cgd 		usage();
    112   1.1       cgd 
    113   1.1       cgd 	for (; *argv; ++argv)
    114   1.1       cgd 		getfrom(*argv);
    115   1.1       cgd 	exit(0);
    116   1.1       cgd }
    117   1.1       cgd 
    118   1.3       tls void
    119   1.3       tls getfrom(pathname)
    120   1.3       tls 	char *pathname;
    121   1.1       cgd {
    122  1.10  christos 	char *name;
    123  1.10  christos 	char *line;
    124  1.10  christos 	size_t len;
    125   1.1       cgd 
    126   1.3       tls 	if (freopen(pathname, "r", stdin) == 0) {
    127  1.10  christos 		warn("Cannot open `%s'", pathname);
    128   1.1       cgd 		return;
    129   1.1       cgd 	}
    130  1.10  christos 	if ((name = strrchr(pathname, '/')) != NULL)
    131   1.3       tls 		name++;
    132   1.3       tls 	else
    133   1.3       tls 		name = pathname;
    134   1.1       cgd 	for (;;) {
    135  1.10  christos 		if ((line = fgetln(stdin, &len)) == NULL) {
    136   1.3       tls 			if (typeflag)
    137  1.10  christos 				printf("%-60s\tUNKNOWN\n", pathname);
    138   1.1       cgd 			return;
    139   1.3       tls 		}
    140  1.10  christos 		if (line[0] != '.')
    141   1.1       cgd 			continue;
    142  1.10  christos 		if ((line[1] == 'T' && line[2] == 'H') ||
    143  1.10  christos 		    (line[1] == 't' && line[2] == 'h'))
    144  1.10  christos 			return oldman(pathname, name);
    145  1.10  christos 		if (line[1] == 'D' && line[2] == 't')
    146  1.10  christos 			return newman(pathname, name);
    147   1.3       tls 	}
    148  1.10  christos }
    149  1.10  christos 
    150  1.10  christos static void
    151  1.10  christos oldman(pathname, name)
    152  1.10  christos 	char *pathname, *name;
    153  1.10  christos {
    154  1.10  christos 	char *line, *ext, *s;
    155  1.10  christos 	size_t len, i, extlen;
    156  1.10  christos 	size_t curlen = 0;
    157  1.10  christos 
    158   1.3       tls 	if (typeflag) {
    159  1.10  christos 		printf("%-60s\tOLD\n", pathname);
    160   1.3       tls 		return;
    161   1.1       cgd 	}
    162   1.1       cgd 	for (;;) {
    163  1.10  christos 		if ((line = fgetln(stdin, &len)) == NULL)
    164   1.1       cgd 			return;
    165  1.10  christos 		if (line[0] != '.')
    166   1.1       cgd 			continue;
    167  1.10  christos 		if (line[1] == 'S' && line[2] == 'H')
    168   1.1       cgd 			break;
    169  1.10  christos 		if (line[1] == 's' && line[2] == 'h')
    170   1.1       cgd 			break;
    171   1.1       cgd 	}
    172  1.10  christos 
    173   1.1       cgd 	if (tocrc)
    174   1.1       cgd 		doname(name);
    175  1.10  christos 
    176  1.10  christos 	for (i = 0;; i++) {
    177  1.10  christos 		if ((line = fgetln(stdin, &len)) == NULL)
    178   1.1       cgd 			break;
    179  1.10  christos 		if (line[0] == '.') {
    180  1.13      fair 			if (line[1] == '\\' && line[2] == '"')
    181  1.13      fair 				continue;	/* [nt]roff comment */
    182  1.10  christos 			if (line[1] == 'S' && line[2] == 'H')
    183   1.1       cgd 				break;
    184  1.10  christos 			if (line[1] == 's' && line[2] == 'h')
    185  1.12       mrg 				break;
    186  1.12       mrg 			if (line[1] == 'P' && line[2] == 'P')
    187   1.1       cgd 				break;
    188   1.1       cgd 		}
    189  1.10  christos 		if (line[len - 1] == '\n') {
    190  1.10  christos 			line[len - 1] = '\0';
    191  1.10  christos 			len--;
    192  1.10  christos 		}
    193  1.10  christos 		if ((ext = strrchr(name, '.')) != NULL) {
    194  1.10  christos 			ext++;
    195  1.10  christos 			extlen = strlen(ext);
    196  1.10  christos 		}
    197  1.10  christos 		else
    198  1.10  christos 			extlen = 0;
    199  1.10  christos 
    200  1.10  christos 		if (maxlen + extlen < curlen + len + SLOP) {
    201  1.10  christos 			maxlen = 2 * (curlen + len) + SLOP + extlen;
    202  1.10  christos 			if ((linebuf = realloc(linebuf, maxlen)) == NULL)
    203  1.10  christos 				err(1, "%s", "");
    204  1.10  christos 		}
    205   1.3       tls 		if (i != 0)
    206  1.10  christos 			linebuf[curlen++] = ' ';
    207  1.10  christos 		(void)memcpy(&linebuf[curlen], line, len);
    208  1.10  christos 		curlen += len;
    209  1.10  christos 		linebuf[curlen] = '\0';
    210  1.10  christos 
    211   1.8       mrg 		/* change the \- into (N) - */
    212  1.10  christos 		if ((s = strstr(linebuf, "\\-")) != NULL) {
    213  1.10  christos 			(void)memmove(s + extlen + 3, s + 1,
    214  1.10  christos 			    curlen - (s + 1 - linebuf));
    215  1.10  christos 			curlen--;
    216  1.10  christos 			if (extlen) {
    217   1.8       mrg 				*s++ = '(';
    218  1.10  christos 				while (*ext)
    219  1.10  christos 					*s++ = *ext++;
    220   1.8       mrg 				*s++ = ')';
    221   1.8       mrg 				*s++ = ' ';
    222  1.10  christos 				curlen += extlen + 3;
    223   1.8       mrg 			}
    224  1.10  christos 			linebuf[curlen] = '\0';
    225   1.8       mrg 		}
    226   1.3       tls 	}
    227  1.10  christos 
    228   1.3       tls 	if (intro)
    229  1.10  christos 		split(linebuf, name);
    230   1.3       tls 	else
    231  1.10  christos 		printf("%s\n", linebuf);
    232   1.3       tls 	return;
    233  1.10  christos }
    234  1.10  christos 
    235  1.10  christos static void
    236  1.10  christos newman(pathname, name)
    237  1.10  christos 	char *pathname, *name;
    238  1.10  christos {
    239  1.10  christos 	char *line, *ext;
    240  1.10  christos 	size_t len, i, extlen;
    241  1.10  christos 	size_t curlen = 0;
    242   1.3       tls 
    243   1.9     lukem 	if (typeflag) {
    244  1.10  christos 		printf("%-60s\tNEW\n", pathname);
    245   1.9     lukem 		return;
    246   1.9     lukem 	}
    247   1.3       tls 	for (;;) {
    248  1.10  christos 		if ((line = fgetln(stdin, &len)) == NULL)
    249   1.3       tls 			return;
    250  1.10  christos 		if (line[0] != '.')
    251   1.1       cgd 			continue;
    252  1.10  christos 		if (line[1] == 'S' && line[2] == 'h')
    253   1.3       tls 			break;
    254   1.3       tls 	}
    255  1.10  christos 
    256   1.3       tls 	if (tocrc)
    257   1.3       tls 		doname(name);
    258  1.10  christos 
    259  1.10  christos 	for (i = 0;; i++) {
    260  1.10  christos 		if ((line = fgetln(stdin, &len)) == NULL)
    261   1.3       tls 			break;
    262  1.10  christos 
    263  1.10  christos 		if (line[0] == '.') {
    264  1.13      fair 			if (line[1] == '\\' && line[2] == '"')
    265  1.13      fair 				continue;	/* [nt]roff comment */
    266  1.10  christos 			if (line[1] == 'S' && line[2] == 'h')
    267   1.3       tls 				break;
    268   1.1       cgd 		}
    269  1.10  christos 
    270  1.10  christos 		if (line[len - 1] == '\n') {
    271  1.10  christos 			line[len - 1] = '\0';
    272  1.10  christos 			len--;
    273  1.10  christos 		}
    274  1.10  christos 
    275  1.10  christos 		if ((ext = strrchr(name, '.')) != NULL) {
    276  1.10  christos 			ext++;
    277  1.10  christos 			extlen = strlen(ext);
    278  1.10  christos 		}
    279  1.10  christos 		else
    280  1.10  christos 			extlen = 0;
    281  1.10  christos 
    282  1.10  christos 		if (maxlen + extlen < curlen + len + SLOP) {
    283  1.10  christos 			maxlen = 2 * (curlen + len) + SLOP + extlen;
    284  1.10  christos 			if ((linebuf = realloc(linebuf, maxlen)) == NULL)
    285  1.10  christos 				err(1, "%s", "");
    286  1.10  christos 		}
    287  1.10  christos 
    288   1.1       cgd 		if (i != 0)
    289  1.10  christos 			linebuf[curlen++] = ' ';
    290  1.10  christos 
    291  1.10  christos 		remcomma(line, &len);
    292  1.10  christos 
    293  1.10  christos 		if (line[0] != '.') {
    294  1.10  christos 			(void)memcpy(&linebuf[curlen], line, len);
    295  1.10  christos 			curlen += len;
    296  1.10  christos 		}
    297  1.10  christos 		else {
    298  1.10  christos 			remquote(line, &len);
    299  1.10  christos 			fixxref(line, &len);
    300   1.8       mrg 
    301   1.3       tls 			/*
    302   1.8       mrg 			 * Put section and dash between names and description.
    303   1.3       tls 			 */
    304  1.10  christos 			if (line[1] == 'N' && line[2] == 'd') {
    305  1.10  christos 				if (extlen) {
    306  1.10  christos 					linebuf[curlen++] = '(';
    307  1.10  christos 					while (*ext)
    308  1.10  christos 						linebuf[curlen++] = *ext++;
    309  1.10  christos 					linebuf[curlen++] = ')';
    310  1.10  christos 					linebuf[curlen++] = ' ';
    311   1.8       mrg 				}
    312  1.10  christos 				linebuf[curlen++] = '-';
    313  1.10  christos 				linebuf[curlen++] = ' ';
    314   1.8       mrg 			}
    315   1.3       tls 			/*
    316   1.3       tls 			 * Skip over macro names.
    317   1.3       tls 			 */
    318  1.10  christos 			if (len <= 4)
    319  1.10  christos 				continue;
    320  1.10  christos 			(void)memcpy(&linebuf[curlen], &line[4], len - 4);
    321  1.10  christos 			curlen += len - 4;
    322   1.3       tls 		}
    323   1.1       cgd 	}
    324  1.10  christos 	linebuf[curlen] = '\0';
    325   1.3       tls 	if (intro)
    326  1.10  christos 		split(linebuf, name);
    327   1.3       tls 	else
    328  1.10  christos 		printf("%s\n", linebuf);
    329   1.1       cgd }
    330   1.1       cgd 
    331  1.10  christos /*
    332  1.10  christos  * convert " ," -> " "
    333  1.10  christos  */
    334  1.10  christos static void
    335  1.10  christos remcomma(line, len)
    336  1.10  christos 	char *line;
    337  1.10  christos 	size_t *len;
    338  1.10  christos {
    339  1.10  christos 	char *pline = line, *loc;
    340  1.10  christos 	size_t plen = *len;
    341  1.10  christos 
    342  1.10  christos 	while ((loc = memchr(pline, ' ', plen)) != NULL) {
    343  1.10  christos 		plen -= loc - pline + 1;
    344  1.10  christos 		pline = loc;
    345  1.10  christos 		if (loc[1] == ',') {
    346  1.10  christos 			(void)memcpy(loc, &loc[1], plen);
    347  1.10  christos 			(*len)--;
    348  1.10  christos 		}
    349  1.10  christos 		else
    350  1.10  christos 			pline++;
    351  1.10  christos 	}
    352  1.10  christos }
    353  1.10  christos 
    354  1.10  christos /*
    355  1.10  christos  * Get rid of quotes in macros.
    356  1.10  christos  */
    357  1.10  christos static
    358  1.10  christos void remquote(line, len)
    359  1.10  christos 	char *line;
    360  1.10  christos 	size_t *len;
    361  1.10  christos {
    362  1.10  christos 	char *loc;
    363  1.10  christos 	char *pline = &line[4];
    364  1.10  christos 	size_t plen = *len - 4;
    365  1.10  christos 
    366  1.10  christos 	if (*len < 4)
    367  1.10  christos 		return;
    368  1.10  christos 
    369  1.10  christos 	while ((loc = memchr(pline, '"', plen)) != NULL) {
    370  1.10  christos 		plen -= loc - pline + 1;
    371  1.10  christos 		pline = loc;
    372  1.10  christos 		(void)memcpy(loc, &loc[1], plen);
    373  1.10  christos 		(*len)--;
    374  1.10  christos 	}
    375  1.10  christos }
    376  1.10  christos 
    377  1.10  christos /*
    378  1.10  christos  * Handle cross references
    379  1.10  christos  */
    380  1.10  christos static void
    381  1.10  christos fixxref(line, len)
    382  1.10  christos 	char *line;
    383  1.10  christos 	size_t *len;
    384   1.1       cgd {
    385  1.10  christos 	char *loc;
    386  1.10  christos 	char *pline = &line[4];
    387  1.10  christos 	size_t plen = *len - 4;
    388   1.1       cgd 
    389  1.10  christos 	if (*len < 4)
    390  1.10  christos 		return;
    391  1.10  christos 
    392  1.10  christos 	if (line[1] == 'X' && line[2] == 'r') {
    393  1.10  christos 		if ((loc = memchr(pline, ' ', plen)) != NULL) {
    394  1.10  christos 			*loc++ = '(';
    395  1.10  christos 			loc++;
    396  1.10  christos 			*loc++ = ')';
    397  1.10  christos 			*len = loc - line;
    398  1.10  christos 		}
    399  1.10  christos 	}
    400   1.1       cgd }
    401   1.1       cgd 
    402  1.10  christos static void
    403   1.1       cgd doname(name)
    404   1.1       cgd 	char *name;
    405   1.1       cgd {
    406   1.9     lukem 	char *dp = name, *ep;
    407   1.1       cgd 
    408   1.1       cgd again:
    409   1.1       cgd 	while (*dp && *dp != '.')
    410   1.1       cgd 		putchar(*dp++);
    411   1.1       cgd 	if (*dp)
    412   1.1       cgd 		for (ep = dp+1; *ep; ep++)
    413   1.1       cgd 			if (*ep == '.') {
    414   1.1       cgd 				putchar(*dp++);
    415   1.1       cgd 				goto again;
    416   1.1       cgd 			}
    417   1.1       cgd 	putchar('(');
    418   1.1       cgd 	if (*dp)
    419   1.1       cgd 		dp++;
    420   1.1       cgd 	while (*dp)
    421   1.1       cgd 		putchar (*dp++);
    422   1.1       cgd 	putchar(')');
    423   1.1       cgd 	putchar(' ');
    424   1.1       cgd }
    425   1.1       cgd 
    426  1.10  christos static void
    427   1.1       cgd split(line, name)
    428   1.1       cgd 	char *line, *name;
    429   1.1       cgd {
    430   1.9     lukem 	char *cp, *dp;
    431   1.1       cgd 	char *sp, *sep;
    432   1.1       cgd 
    433   1.3       tls 	cp = strchr(line, '-');
    434   1.1       cgd 	if (cp == 0)
    435   1.1       cgd 		return;
    436   1.1       cgd 	sp = cp + 1;
    437   1.1       cgd 	for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
    438   1.1       cgd 		;
    439   1.1       cgd 	*++cp = '\0';
    440   1.1       cgd 	while (*sp && (*sp == ' ' || *sp == '\t'))
    441   1.1       cgd 		sp++;
    442   1.1       cgd 	for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
    443   1.3       tls 		cp = strchr(dp, ',');
    444   1.1       cgd 		if (cp) {
    445   1.9     lukem 			char *tp;
    446   1.1       cgd 
    447   1.1       cgd 			for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
    448   1.1       cgd 				;
    449   1.1       cgd 			*++tp = '\0';
    450   1.1       cgd 			for (++cp; *cp == ' ' || *cp == '\t'; cp++)
    451   1.1       cgd 				;
    452   1.1       cgd 		}
    453   1.1       cgd 		printf("%s%s\t", sep, dp);
    454   1.1       cgd 		dorefname(name);
    455   1.1       cgd 		printf("\t%s", sp);
    456   1.1       cgd 	}
    457   1.1       cgd }
    458   1.1       cgd 
    459  1.10  christos static void
    460   1.1       cgd dorefname(name)
    461   1.1       cgd 	char *name;
    462   1.1       cgd {
    463   1.9     lukem 	char *dp = name, *ep;
    464   1.1       cgd 
    465   1.1       cgd again:
    466   1.1       cgd 	while (*dp && *dp != '.')
    467   1.1       cgd 		putchar(*dp++);
    468   1.1       cgd 	if (*dp)
    469   1.1       cgd 		for (ep = dp+1; *ep; ep++)
    470   1.1       cgd 			if (*ep == '.') {
    471   1.1       cgd 				putchar(*dp++);
    472   1.1       cgd 				goto again;
    473   1.1       cgd 			}
    474   1.1       cgd 	putchar('.');
    475   1.1       cgd 	if (*dp)
    476   1.1       cgd 		dp++;
    477   1.1       cgd 	while (*dp)
    478   1.1       cgd 		putchar (*dp++);
    479   1.1       cgd }
    480   1.1       cgd 
    481  1.10  christos static void
    482   1.1       cgd usage()
    483   1.1       cgd {
    484  1.10  christos 	extern char *__progname;
    485  1.10  christos 	(void)fprintf(stderr, "Usage: %s [-itw] file ...\n", __progname);
    486   1.1       cgd 	exit(1);
    487   1.1       cgd }
    488