NetBSD/libexec/getNAME/getNAME.c

481 lines
9.6 KiB
C
Raw Normal View History

/* $NetBSD: getNAME.c,v 1.10 1997/12/22 20:02:33 christos Exp $ */
1997-10-07 14:57:08 +04:00
1993-03-21 12:45:37 +03:00
/*-
* Copyright (c) 1997, Christos Zoulas
1997-04-30 00:39:10 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* This product includes software developed by Christos Zoulas.
1993-03-21 12:45:37 +03:00
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-07 14:57:08 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-10-07 14:57:08 +04:00
__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1997-04-30 00:39:10 +04:00
#if 0
static char sccsid[] = "@(#)getNAME.c 8.1 (Berkeley) 6/30/93";
1997-10-07 14:57:08 +04:00
#else
__RCSID("$NetBSD: getNAME.c,v 1.10 1997/12/22 20:02:33 christos Exp $");
1997-04-30 00:39:10 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/*
* Get name sections from manual pages.
* -t for building toc
* -i for building intro entries
* -w for querying type of manual source
1993-03-21 12:45:37 +03:00
* other apropos database
*/
#include <stdio.h>
1997-04-30 00:39:10 +04:00
#include <stdlib.h>
1993-03-21 12:45:37 +03:00
#include <string.h>
#include <err.h>
1993-03-21 12:45:37 +03:00
static int tocrc;
static int intro;
static int typeflag;
#define SLOP 10 /* strlen(" () - ") < 10 */
static char *linebuf = NULL;
static size_t maxlen = 0;
static void doname __P((char *));
static void dorefname __P((char *));
static void getfrom __P((char *));
static void oldman __P((char *, char *));
static void newman __P((char *, char *));
static void remcomma __P((char *, size_t *));
static void remquote __P((char *, size_t *));
static void fixxref __P((char *, size_t *));
static void split __P((char *, char *));
static void usage __P((void));
1993-03-21 12:45:37 +03:00
1997-10-07 14:57:08 +04:00
int main __P((int, char *[]));
1997-04-30 00:39:10 +04:00
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
int argc;
1997-04-30 00:39:10 +04:00
char *argv[];
1993-03-21 12:45:37 +03:00
{
int ch;
while ((ch = getopt(argc, argv, "itw")) != -1)
switch (ch) {
1993-03-21 12:45:37 +03:00
case 'i':
intro = 1;
break;
case 't':
tocrc = 1;
break;
1997-04-30 00:39:10 +04:00
case 'w':
typeflag = 1;
break;
1993-03-21 12:45:37 +03:00
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (!*argv)
usage();
for (; *argv; ++argv)
getfrom(*argv);
exit(0);
}
1997-04-30 00:39:10 +04:00
void
getfrom(pathname)
char *pathname;
1993-03-21 12:45:37 +03:00
{
char *name;
char *line;
size_t len;
1993-03-21 12:45:37 +03:00
1997-04-30 00:39:10 +04:00
if (freopen(pathname, "r", stdin) == 0) {
warn("Cannot open `%s'", pathname);
1993-03-21 12:45:37 +03:00
return;
}
if ((name = strrchr(pathname, '/')) != NULL)
1997-04-30 00:39:10 +04:00
name++;
else
name = pathname;
1993-03-21 12:45:37 +03:00
for (;;) {
if ((line = fgetln(stdin, &len)) == NULL) {
1997-04-30 00:39:10 +04:00
if (typeflag)
printf("%-60s\tUNKNOWN\n", pathname);
1993-03-21 12:45:37 +03:00
return;
1997-04-30 00:39:10 +04:00
}
if (line[0] != '.')
1993-03-21 12:45:37 +03:00
continue;
if ((line[1] == 'T' && line[2] == 'H') ||
(line[1] == 't' && line[2] == 'h'))
return oldman(pathname, name);
if (line[1] == 'D' && line[2] == 't')
return newman(pathname, name);
1997-04-30 00:39:10 +04:00
}
}
static void
oldman(pathname, name)
char *pathname, *name;
{
char *line, *ext, *s;
size_t len, i, extlen;
size_t curlen = 0;
1997-04-30 00:39:10 +04:00
if (typeflag) {
printf("%-60s\tOLD\n", pathname);
1997-04-30 00:39:10 +04:00
return;
1993-03-21 12:45:37 +03:00
}
for (;;) {
if ((line = fgetln(stdin, &len)) == NULL)
1993-03-21 12:45:37 +03:00
return;
if (line[0] != '.')
1993-03-21 12:45:37 +03:00
continue;
if (line[1] == 'S' && line[2] == 'H')
1993-03-21 12:45:37 +03:00
break;
if (line[1] == 's' && line[2] == 'h')
1993-03-21 12:45:37 +03:00
break;
}
1993-03-21 12:45:37 +03:00
if (tocrc)
doname(name);
for (i = 0;; i++) {
if ((line = fgetln(stdin, &len)) == NULL)
1993-03-21 12:45:37 +03:00
break;
if (line[0] == '.') {
if (line[1] == 'S' && line[2] == 'H')
1993-03-21 12:45:37 +03:00
break;
if (line[1] == 's' && line[2] == 'h')
1993-03-21 12:45:37 +03:00
break;
}
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
len--;
}
if ((ext = strrchr(name, '.')) != NULL) {
ext++;
extlen = strlen(ext);
}
else
extlen = 0;
if (maxlen + extlen < curlen + len + SLOP) {
maxlen = 2 * (curlen + len) + SLOP + extlen;
if ((linebuf = realloc(linebuf, maxlen)) == NULL)
err(1, "%s", "");
}
1997-04-30 00:39:10 +04:00
if (i != 0)
linebuf[curlen++] = ' ';
(void)memcpy(&linebuf[curlen], line, len);
curlen += len;
linebuf[curlen] = '\0';
/* change the \- into (N) - */
if ((s = strstr(linebuf, "\\-")) != NULL) {
(void)memmove(s + extlen + 3, s + 1,
curlen - (s + 1 - linebuf));
curlen--;
if (extlen) {
*s++ = '(';
while (*ext)
*s++ = *ext++;
*s++ = ')';
*s++ = ' ';
curlen += extlen + 3;
}
linebuf[curlen] = '\0';
}
1997-04-30 00:39:10 +04:00
}
1997-04-30 00:39:10 +04:00
if (intro)
split(linebuf, name);
1997-04-30 00:39:10 +04:00
else
printf("%s\n", linebuf);
1997-04-30 00:39:10 +04:00
return;
}
static void
newman(pathname, name)
char *pathname, *name;
{
char *line, *ext;
size_t len, i, extlen;
size_t curlen = 0;
1997-04-30 00:39:10 +04:00
if (typeflag) {
printf("%-60s\tNEW\n", pathname);
return;
}
1997-04-30 00:39:10 +04:00
for (;;) {
if ((line = fgetln(stdin, &len)) == NULL)
1997-04-30 00:39:10 +04:00
return;
if (line[0] != '.')
1993-03-21 12:45:37 +03:00
continue;
if (line[1] == 'S' && line[2] == 'h')
1997-04-30 00:39:10 +04:00
break;
}
1997-04-30 00:39:10 +04:00
if (tocrc)
doname(name);
for (i = 0;; i++) {
if ((line = fgetln(stdin, &len)) == NULL)
1997-04-30 00:39:10 +04:00
break;
if (line[0] == '.') {
if (line[1] == 'S' && line[2] == 'h')
1997-04-30 00:39:10 +04:00
break;
1993-03-21 12:45:37 +03:00
}
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
len--;
}
if ((ext = strrchr(name, '.')) != NULL) {
ext++;
extlen = strlen(ext);
}
else
extlen = 0;
if (maxlen + extlen < curlen + len + SLOP) {
maxlen = 2 * (curlen + len) + SLOP + extlen;
if ((linebuf = realloc(linebuf, maxlen)) == NULL)
err(1, "%s", "");
}
1993-03-21 12:45:37 +03:00
if (i != 0)
linebuf[curlen++] = ' ';
remcomma(line, &len);
if (line[0] != '.') {
(void)memcpy(&linebuf[curlen], line, len);
curlen += len;
}
else {
remquote(line, &len);
fixxref(line, &len);
1997-04-30 00:39:10 +04:00
/*
* Put section and dash between names and description.
1997-04-30 00:39:10 +04:00
*/
if (line[1] == 'N' && line[2] == 'd') {
if (extlen) {
linebuf[curlen++] = '(';
while (*ext)
linebuf[curlen++] = *ext++;
linebuf[curlen++] = ')';
linebuf[curlen++] = ' ';
}
linebuf[curlen++] = '-';
linebuf[curlen++] = ' ';
}
1997-04-30 00:39:10 +04:00
/*
* Skip over macro names.
*/
if (len <= 4)
continue;
(void)memcpy(&linebuf[curlen], &line[4], len - 4);
curlen += len - 4;
1997-04-30 00:39:10 +04:00
}
1993-03-21 12:45:37 +03:00
}
linebuf[curlen] = '\0';
1997-04-30 00:39:10 +04:00
if (intro)
split(linebuf, name);
1997-04-30 00:39:10 +04:00
else
printf("%s\n", linebuf);
1993-03-21 12:45:37 +03:00
}
/*
* convert " ," -> " "
*/
static void
remcomma(line, len)
char *line;
size_t *len;
1993-03-21 12:45:37 +03:00
{
char *pline = line, *loc;
size_t plen = *len;
1993-03-21 12:45:37 +03:00
while ((loc = memchr(pline, ' ', plen)) != NULL) {
plen -= loc - pline + 1;
pline = loc;
if (loc[1] == ',') {
(void)memcpy(loc, &loc[1], plen);
(*len)--;
}
else
pline++;
}
1993-03-21 12:45:37 +03:00
}
/*
* Get rid of quotes in macros.
*/
static
void remquote(line, len)
char *line;
size_t *len;
{
char *loc;
char *pline = &line[4];
size_t plen = *len - 4;
if (*len < 4)
return;
while ((loc = memchr(pline, '"', plen)) != NULL) {
plen -= loc - pline + 1;
pline = loc;
(void)memcpy(loc, &loc[1], plen);
(*len)--;
}
}
/*
* Handle cross references
*/
static void
fixxref(line, len)
char *line;
size_t *len;
{
char *loc;
char *pline = &line[4];
size_t plen = *len - 4;
if (*len < 4)
return;
if (line[1] == 'X' && line[2] == 'r') {
if ((loc = memchr(pline, ' ', plen)) != NULL) {
*loc++ = '(';
loc++;
*loc++ = ')';
*len = loc - line;
}
}
}
static void
1993-03-21 12:45:37 +03:00
doname(name)
char *name;
{
char *dp = name, *ep;
1993-03-21 12:45:37 +03:00
again:
while (*dp && *dp != '.')
putchar(*dp++);
if (*dp)
for (ep = dp+1; *ep; ep++)
if (*ep == '.') {
putchar(*dp++);
goto again;
}
putchar('(');
if (*dp)
dp++;
while (*dp)
putchar (*dp++);
putchar(')');
putchar(' ');
}
static void
1993-03-21 12:45:37 +03:00
split(line, name)
char *line, *name;
{
char *cp, *dp;
1993-03-21 12:45:37 +03:00
char *sp, *sep;
1997-04-30 00:39:10 +04:00
cp = strchr(line, '-');
1993-03-21 12:45:37 +03:00
if (cp == 0)
return;
sp = cp + 1;
for (--cp; *cp == ' ' || *cp == '\t' || *cp == '\\'; cp--)
;
*++cp = '\0';
while (*sp && (*sp == ' ' || *sp == '\t'))
sp++;
for (sep = "", dp = line; dp && *dp; dp = cp, sep = "\n") {
1997-04-30 00:39:10 +04:00
cp = strchr(dp, ',');
1993-03-21 12:45:37 +03:00
if (cp) {
char *tp;
1993-03-21 12:45:37 +03:00
for (tp = cp - 1; *tp == ' ' || *tp == '\t'; tp--)
;
*++tp = '\0';
for (++cp; *cp == ' ' || *cp == '\t'; cp++)
;
}
printf("%s%s\t", sep, dp);
dorefname(name);
printf("\t%s", sp);
}
}
static void
1993-03-21 12:45:37 +03:00
dorefname(name)
char *name;
{
char *dp = name, *ep;
1993-03-21 12:45:37 +03:00
again:
while (*dp && *dp != '.')
putchar(*dp++);
if (*dp)
for (ep = dp+1; *ep; ep++)
if (*ep == '.') {
putchar(*dp++);
goto again;
}
putchar('.');
if (*dp)
dp++;
while (*dp)
putchar (*dp++);
}
static void
1993-03-21 12:45:37 +03:00
usage()
{
extern char *__progname;
(void)fprintf(stderr, "Usage: %s [-itw] file ...\n", __progname);
1993-03-21 12:45:37 +03:00
exit(1);
}