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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
// -*- C++ -*-
/* Copyright (C) 2000, 2001, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
Written by Gaius Mulley (gaius@glam.ac.uk).
This file is part of groff.
groff 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 3 of the License, or
(at your option) any later version.
groff 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, see <http://www.gnu.org/licenses/>. */
#include "lib.h"
#include <signal.h>
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include "errarg.h"
#include "error.h"
#include "stringclass.h"
#include "posix.h"
#include "nonposix.h"
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "pushback.h"
#include "pre-html.h"
#if !defined(TRUE)
# define TRUE (1==1)
#endif
#if !defined(FALSE)
# define FALSE (1==0)
#endif
# define ERROR(X) (void)(fprintf(stderr, "%s:%d error %s\n", __FILE__, __LINE__, X) && \
(fflush(stderr)) && localexit(1))
#define MAXPUSHBACKSTACK 4096 /* maximum number of character that can be pushed back */
/*
* constructor for pushBackBuffer
*/
pushBackBuffer::pushBackBuffer (char *filename)
{
charStack = (char *)malloc(MAXPUSHBACKSTACK);
if (charStack == 0) {
sys_fatal("malloc");
}
stackPtr = 0; /* index to push back stack */
debug = 0;
verbose = 0;
eofFound = FALSE;
lineNo = 1;
if (strcmp(filename, "") != 0) {
stdIn = dup(0);
if (stdIn < 0) {
sys_fatal("dup stdin");
}
close(0);
if (open(filename, O_RDONLY) != 0) {
sys_fatal("when trying to open file");
} else {
fileName = filename;
}
}
}
pushBackBuffer::~pushBackBuffer ()
{
if (charStack != 0) {
free(charStack);
}
close(0);
/* restore stdin in file descriptor 0 */
if (dup(stdIn) < 0) {
sys_fatal("restore stdin");
}
close(stdIn);
}
/*
* localexit - wraps exit with a return code to aid the ERROR macro.
*/
int localexit (int i)
{
exit(i);
return( 1 );
}
/*
* getPB - returns a character, possibly a pushed back character.
*/
char pushBackBuffer::getPB (void)
{
if (stackPtr>0) {
stackPtr--;
return( charStack[stackPtr] );
} else {
char ch;
if (read(0, &ch, 1) == 1) {
if (verbose) {
printf("%c", ch);
}
if (ch == '\n') {
lineNo++;
}
return( ch );
} else {
eofFound = TRUE;
return( eof );
}
}
}
/*
* putPB - pushes a character onto the push back stack.
* The same character is returned.
*/
char pushBackBuffer::putPB (char ch)
{
if (stackPtr<MAXPUSHBACKSTACK) {
charStack[stackPtr] = ch ;
stackPtr++;
} else {
ERROR("max push back stack exceeded, increase MAXPUSHBACKSTACK constant");
}
return( ch );
}
/*
* isWhite - returns TRUE if a white character is found. This character is NOT consumed.
*/
static int isWhite (char ch)
{
return( (ch==' ') || (ch == '\t') || (ch == '\n') );
}
/*
* skipToNewline - skips characters until a newline is seen.
*/
void pushBackBuffer::skipToNewline (void)
{
while ((putPB(getPB()) != '\n') && (! eofFound)) {
getPB();
}
}
/*
* skipUntilToken - skips until a token is seen
*/
void pushBackBuffer::skipUntilToken (void)
{
char ch;
while ((isWhite(putPB(getPB())) || (putPB(getPB()) == '#')) && (! eofFound)) {
ch = getPB();
if (ch == '#') {
skipToNewline();
}
}
}
/*
* isString - returns TRUE if the string, s, matches the pushed back string.
* if TRUE is returned then this string is consumed, otherwise it is
* left alone.
*/
int pushBackBuffer::isString (const char *s)
{
int length=strlen(s);
int i=0;
while ((i<length) && (putPB(getPB())==s[i])) {
if (getPB() != s[i]) {
ERROR("assert failed");
}
i++;
}
if (i==length) {
return( TRUE );
} else {
i--;
while (i>=0) {
if (putPB(s[i]) != s[i]) {
ERROR("assert failed");
}
i--;
}
}
return( FALSE );
}
/*
* isDigit - returns TRUE if the character, ch, is a digit.
*/
static int isDigit (char ch)
{
return( ((ch>='0') && (ch<='9')) );
}
/*
* isHexDigit - returns TRUE if the character, ch, is a hex digit.
*/
#if 0
static int isHexDigit (char ch)
{
return( (isDigit(ch)) || ((ch>='a') && (ch<='f')) );
}
#endif
/*
* readInt - returns an integer from the input stream.
*/
int pushBackBuffer::readInt (void)
{
int c =0;
int i =0;
int s =1;
char ch=getPB();
while (isWhite(ch)) {
ch=getPB();
}
// now read integer
if (ch == '-') {
s = -1;
ch = getPB();
}
while (isDigit(ch)) {
i *= 10;
if ((ch>='0') && (ch<='9')) {
i += (int)(ch-'0');
}
ch = getPB();
c++;
}
if (ch != putPB(ch)) {
ERROR("assert failed");
}
return( i*s );
}
/*
* convertToFloat - converts integers, a and b into a.b
*/
static double convertToFloat (int a, int b)
{
int c=10;
double f;
while (b>c) {
c *= 10;
}
f = ((double)a) + (((double)b)/((double)c));
return( f );
}
/*
* readNumber - returns a float representing the word just read.
*/
double pushBackBuffer::readNumber (void)
{
int i;
char ch;
i = readInt();
if ((ch = getPB()) == '.') {
return convertToFloat(i, readInt());
}
putPB(ch);
return (double)i;
}
/*
* readString - reads a string terminated by white space
* and returns a malloced area of memory containing
* a copy of the characters.
*/
char *pushBackBuffer::readString (void)
{
char buffer[MAXPUSHBACKSTACK];
char *str = 0;
int i=0;
char ch=getPB();
while (isWhite(ch)) {
ch=getPB();
}
while ((i < MAXPUSHBACKSTACK) && (! isWhite(ch)) && (! eofFound)) {
buffer[i] = ch;
i++;
ch = getPB();
}
if (i < MAXPUSHBACKSTACK) {
buffer[i] = (char)0;
str = (char *)malloc(strlen(buffer)+1);
strcpy(str, buffer);
}
return( str );
}
|