2 * Copyright (c) 2013 David Chisnall
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "input_buffer.hh"
50 input_buffer::skip_spaces()
52 if (cursor >= size) { return; }
53 if (cursor < 0) { return; }
54 char c = buffer[cursor];
55 while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\f')
56 || (c == '\v') || (c == '\r'))
71 input_buffer::buffer_from_offset(int offset, int s)
79 return input_buffer();
81 if (s > (size-offset))
83 return input_buffer();
85 return input_buffer(&buffer[offset], s);
89 input_buffer::consume(const char *str)
91 int len = strlen(str);
92 if (len > size - cursor)
98 for (int i=0 ; i<len ; ++i)
100 if (str[i] != buffer[cursor + i])
112 input_buffer::consume_integer(long long &outInt)
114 // The first character must be a digit. Hex and octal strings
115 // are prefixed by 0 and 0x, respectively.
116 if (!isdigit((*this)[0]))
121 outInt = strtoll(&buffer[cursor], &end, 0);
122 if (end == &buffer[cursor])
126 cursor = end - buffer;
131 input_buffer::consume_hex_byte(uint8_t &outByte)
133 if (!ishexdigit((*this)[0]) && !ishexdigit((*this)[1]))
137 outByte = (digittoint((*this)[0]) << 4) | digittoint((*this)[1]);
143 input_buffer::next_token()
150 if (((*this)[0] == '/') && ((*this)[1] == '*'))
152 // eat the start of the comment
156 // Find the ending * of */
157 while ((**this != '\0') && (**this != '*'))
163 } while ((**this != '\0') && (**this != '/'));
168 if (((*this)[0] == '/') && ((*this)[1] == '/'))
170 // eat the start of the comment
173 // Find the ending * of */
174 while (**this != '\n')
181 } while (start != cursor);
186 input_buffer::parse_error(const char *msg)
190 int line_end = cursor;
191 for (int i=cursor ; i>0 ; --i)
193 if (buffer[i] == '\n')
202 for (int i=cursor+1 ; i<size ; ++i)
204 if (buffer[i] == '\n')
210 fprintf(stderr, "Error on line %d: %s\n", line_count, msg);
211 fwrite(&buffer[line_start], line_end-line_start, 1, stderr);
213 for (int i=0 ; i<(cursor-line_start) ; ++i)
223 fprintf(stderr, "Current cursor: %d\n", cursor);
224 fwrite(&buffer[cursor], size-cursor, 1, stderr);
227 mmap_input_buffer::mmap_input_buffer(int fd) : input_buffer(0, 0)
232 perror("Failed to stat file");
235 buffer = (const char*)mmap(0, size, PROT_READ,
236 MAP_PREFAULT_READ, fd, 0);
239 perror("Failed to mmap file");
243 mmap_input_buffer::~mmap_input_buffer()
247 munmap((void*)buffer, size);
251 stream_input_buffer::stream_input_buffer() : input_buffer(0, 0)
254 while ((c = fgetc(stdin)) != EOF)