NetBSD/gnu/dist/texinfo/util/deref.c

212 lines
4.3 KiB
C
Raw Normal View History

2003-01-17 17:54:14 +03:00
/* $NetBSD: deref.c,v 1.1.1.3 2003/01/17 14:54:37 wiz Exp $ */
1999-02-11 06:57:13 +03:00
/*
* deref.c
*
* Make all texinfo references into the one argument form.
2001-07-25 20:20:30 +04:00
*
1999-02-11 06:57:13 +03:00
* Arnold Robbins
2001-07-25 20:20:30 +04:00
* arnold@gnu.org
* Written: December, 1991
* Released: November, 1998
1999-02-11 06:57:13 +03:00
*
2001-07-25 20:20:30 +04:00
* Copyright, 1991, 1998 Arnold David Robbins
*
* DEREF is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* DEREF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
1999-02-11 06:57:13 +03:00
*/
/*
* LIMITATIONS:
* One texinfo cross reference per line.
* Cross references may not cross newlines.
* Use of fgets for input (to be fixed).
*/
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
/* for gcc on the 3B1, delete if this gives you grief */
2001-07-25 20:20:30 +04:00
extern int fclose(FILE *fp);
extern int fprintf(FILE *fp, const char *str, ...);
/* extern int sprintf(char *str, const char *fmt, ...); */
extern int fputs(char *buf, FILE *fp);
1999-02-11 06:57:13 +03:00
2001-07-25 20:20:30 +04:00
extern char *strerror(int errno);
extern char *strchr(char *cp, int ch);
extern int strncmp(const char *s1, const char *s2, int count);
1999-02-11 06:57:13 +03:00
extern int errno;
2001-07-25 20:20:30 +04:00
void process(FILE *fp);
void repair(char *line, char *ref, int toffset);
1999-02-11 06:57:13 +03:00
int Errs = 0;
char *Name = "stdin";
int Line = 0;
char *Me;
/* main --- handle arguments, global vars for errors */
int
2001-07-25 20:20:30 +04:00
main(int argc, char **argv)
1999-02-11 06:57:13 +03:00
{
2001-07-25 20:20:30 +04:00
FILE *fp;
Me = argv[0];
if (argc == 1)
process(stdin);
1999-02-11 06:57:13 +03:00
else
2001-07-25 20:20:30 +04:00
for (argc--, argv++; *argv != NULL; argc--, argv++) {
if (argv[0][0] == '-' && argv[0][1] == '\0') {
Name = "stdin";
Line = 0;
process(stdin);
} else if ((fp = fopen(*argv, "r")) != NULL) {
Name = *argv;
Line = 0;
process(fp);
fclose(fp);
} else {
fprintf(stderr, "%s: can not open: %s\n",
*argv, strerror(errno));
Errs++;
}
}
return Errs != 0;
1999-02-11 06:57:13 +03:00
}
/* isref --- decide if we've seen a texinfo cross reference */
int
2001-07-25 20:20:30 +04:00
isref(char *cp)
1999-02-11 06:57:13 +03:00
{
2001-07-25 20:20:30 +04:00
if (strncmp(cp, "@ref{", 5) == 0)
return 5;
if (strncmp(cp, "@xref{", 6) == 0)
return 6;
if (strncmp(cp, "@pxref{", 7) == 0)
return 7;
return 0;
1999-02-11 06:57:13 +03:00
}
/* process --- read files, look for references, fix them up */
void
2001-07-25 20:20:30 +04:00
process(FILE *fp)
1999-02-11 06:57:13 +03:00
{
2001-07-25 20:20:30 +04:00
char buf[BUFSIZ];
char *cp;
int count;
while (fgets(buf, sizeof buf, fp) != NULL) {
Line++;
cp = strchr(buf, '@');
if (cp == NULL) {
fputs(buf, stdout);
continue;
1999-02-11 06:57:13 +03:00
}
2001-07-25 20:20:30 +04:00
do {
count = isref(cp);
if (count == 0) {
cp++;
cp = strchr(cp, '@');
if (cp == NULL) {
fputs(buf, stdout);
goto next;
}
continue;
}
/* got one */
repair(buf, cp, count);
break;
} while (cp != NULL);
next: ;
1999-02-11 06:57:13 +03:00
}
}
/* repair --- turn all texinfo cross references into the one argument form */
void
2001-07-25 20:20:30 +04:00
repair(char *line, char *ref, int toffset)
1999-02-11 06:57:13 +03:00
{
2001-07-25 20:20:30 +04:00
int braces = 1; /* have seen first left brace */
char *cp;
ref += toffset;
/* output line up to and including left brace in reference */
for (cp = line; cp <= ref; cp++)
putchar(*cp);
/* output node name */
for (; *cp && *cp != '}' && *cp != ',' && *cp != '\n'; cp++)
putchar(*cp);
if (*cp != '}') { /* could have been one arg xref */
/* skip to matching right brace */
for (; braces > 0; cp++) {
switch (*cp) {
case '@':
cp++; /* blindly skip next character */
break;
case '{':
braces++;
break;
case '}':
braces--;
break;
case '\n':
case '\0':
Errs++;
fprintf(stderr,
"%s: %s: %d: mismatched braces\n",
Me, Name, Line);
goto out;
default:
break;
}
}
out:
;
1999-02-11 06:57:13 +03:00
}
2001-07-25 20:20:30 +04:00
putchar('}');
if (*cp == '}')
cp++;
/* now the rest of the line */
for (; *cp; cp++)
putchar(*cp);
return;
1999-02-11 06:57:13 +03:00
}
/* strerror --- return error string, delete if in your library */
char *
2001-07-25 20:20:30 +04:00
strerror(int errno)
1999-02-11 06:57:13 +03:00
{
2001-07-25 20:20:30 +04:00
static char buf[100];
extern int sys_nerr;
extern char *sys_errlist[];
1999-02-11 06:57:13 +03:00
2001-07-25 20:20:30 +04:00
if (errno < sys_nerr && errno >= 0)
return sys_errlist[errno];
1999-02-11 06:57:13 +03:00
2001-07-25 20:20:30 +04:00
sprintf(buf, "unknown error %d", errno);
return buf;
1999-02-11 06:57:13 +03:00
}