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