1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
* deref.c
*
* Make all texinfo references into the one argument form.
*
* Arnold Robbins
* arnold@gnu.org
* Written: December, 1991
* Released: November, 1998
*
* 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
*/
/*
* 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 */
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);
extern char *strerror(int errno);
extern char *strchr(char *cp, int ch);
extern int strncmp(const char *s1, const char *s2, int count);
extern int errno;
void process(FILE *fp);
void repair(char *line, char *ref, int toffset);
int Errs = 0;
char *Name = "stdin";
int Line = 0;
char *Me;
/* main --- handle arguments, global vars for errors */
int
main(int argc, char **argv)
{
FILE *fp;
Me = argv[0];
if (argc == 1)
process(stdin);
else
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;
}
/* isref --- decide if we've seen a texinfo cross reference */
int
isref(char *cp)
{
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;
}
/* process --- read files, look for references, fix them up */
void
process(FILE *fp)
{
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;
}
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: ;
}
}
/* repair --- turn all texinfo cross references into the one argument form */
void
repair(char *line, char *ref, int toffset)
{
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:
;
}
putchar('}');
if (*cp == '}')
cp++;
/* now the rest of the line */
for (; *cp; cp++)
putchar(*cp);
return;
}
/* strerror --- return error string, delete if in your library */
char *
strerror(int errno)
{
static char buf[100];
extern int sys_nerr;
extern char *sys_errlist[];
if (errno < sys_nerr && errno >= 0)
return sys_errlist[errno];
sprintf(buf, "unknown error %d", errno);
return buf;
}
|