2 * Copyright (c) 2014 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
4 * Copyright (C) 2000, 2001, 2003 - 2005 Free Software Foundation, Inc.
5 * Written by Gaius Mulley (gaius@glam.ac.uk).
7 * This is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2, or (at your option) any later
12 * This is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * You should have received a copy of the GNU General Public License along
18 * with groff; see the file COPYING. If not, write to the Free Software
19 * Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "html-config.h"
25 #include <sys/types.h>
38 #include "stringclass.h"
44 (void)(fprintf(stderr, "%s:%d error %s\n", __FILE__, __LINE__, X) &&\
45 (fflush(stderr)) && localexit(1))
47 pushBackBuffer::pushBackBuffer (char *filename
)
49 charStack
= (char *)malloc(MAXPUSHBACKSTACK
);
53 stackPtr
= 0; /* index to push back stack */
58 if (strcmp(filename
, "") != 0) {
59 if ((stdIn
= dup(0)) == -1)
60 sys_fatal("cannot dup(2)licate standard input");
62 if (open(filename
, O_RDONLY
) != 0) {
63 sys_fatal("when trying to open file");
70 pushBackBuffer::~pushBackBuffer ()
76 /* restore stdin in file descriptor 0 */
77 if (dup(stdIn
) == -1) /* FIXME use dup2() if possible, not close(0)+dup(2)! */
78 sys_fatal("cannot restore dup(2)licated standard input");
83 * localexit - wraps exit with a return code to aid the ERROR macro.
93 * getPB - returns a character, possibly a pushed back character.
96 char pushBackBuffer::getPB (void)
100 return( charStack
[stackPtr
] );
104 if (read(0, &ch
, 1) == 1) {
120 * putPB - pushes a character onto the push back stack.
121 * The same character is returned.
124 char pushBackBuffer::putPB (char ch
)
126 if (stackPtr
<MAXPUSHBACKSTACK
) {
127 charStack
[stackPtr
] = ch
;
130 ERROR("max push back stack exceeded, increase MAXPUSHBACKSTACK constant");
136 * isWhite - returns true if a white character is found. This character is NOT consumed.
139 static int isWhite (char ch
)
141 return( (ch
==' ') || (ch
== '\t') || (ch
== '\n') );
145 * skipToNewline - skips characters until a newline is seen.
148 void pushBackBuffer::skipToNewline (void)
150 while ((putPB(getPB()) != '\n') && (! eofFound
)) {
156 * skipUntilToken - skips until a token is seen
159 void pushBackBuffer::skipUntilToken (void)
163 while ((isWhite(putPB(getPB())) || (putPB(getPB()) == '#')) && (! eofFound
)) {
172 * isString - returns true if the string, s, matches the pushed back string.
173 * if true is returned then this string is consumed, otherwise it is
177 int pushBackBuffer::isString (const char *s
)
179 int length
=strlen(s
);
182 while ((i
<length
) && (putPB(getPB())==s
[i
])) {
183 if (getPB() != s
[i
]) {
184 ERROR("assert failed");
193 if (putPB(s
[i
]) != s
[i
]) {
194 ERROR("assert failed");
203 * isDigit - returns true if the character, ch, is a digit.
206 static int isDigit (char ch
)
208 return( ((ch
>='0') && (ch
<='9')) );
212 * isHexDigit - returns true if the character, ch, is a hex digit.
216 static int isHexDigit (char ch
)
218 return( (isDigit(ch
)) || ((ch
>='a') && (ch
<='f')) );
223 * readInt - returns an integer from the input stream.
226 int pushBackBuffer::readInt (void)
233 while (isWhite(ch
)) {
242 while (isDigit(ch
)) {
244 if ((ch
>='0') && (ch
<='9')) {
250 if (ch
!= putPB(ch
)) {
251 ERROR("assert failed");
257 * convertToFloat - converts integers, a and b into a.b
260 static double convertToFloat (int a
, int b
)
268 f
= ((double)a
) + (((double)b
)/((double)c
));
273 * readNumber - returns a float representing the word just read.
276 double pushBackBuffer::readNumber (void)
282 if ((ch
= getPB()) == '.') {
283 return convertToFloat(i
, readInt());
290 * readString - reads a string terminated by white space
291 * and returns a malloced area of memory containing
292 * a copy of the characters.
295 char *pushBackBuffer::readString (void)
297 char buffer
[MAXPUSHBACKSTACK
];
302 while (isWhite(ch
)) {
305 while ((i
< MAXPUSHBACKSTACK
) && (! isWhite(ch
)) && (! eofFound
)) {
310 if (i
< MAXPUSHBACKSTACK
) {
312 str
= (char *)malloc(strlen(buffer
)+1);