mpw/bin/loadtrap.rl

299 lines
6.5 KiB
Plaintext
Raw Normal View History

2013-07-30 05:06:19 +00:00
/*
* Copyright (c) 2013, Kelvin W Sherlock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
#include <map>
2013-07-08 03:39:53 +00:00
#include <string>
#include <cstdio>
2015-02-17 00:23:12 +00:00
#include <sys/types.h>
#include <limits.h>
namespace _loadtrap_rl {
2016-08-03 01:02:34 +00:00
#if __APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1050
2015-02-17 00:23:12 +00:00
#define _GETDELIM_GROWBY 128 /* amount to grow line buffer by */
#define _GETDELIM_MINLEN 4 /* minimum line buffer size */
ssize_t getdelim(char ** lineptr, size_t * n, int delimiter, FILE * stream) {
char *buf, *pos;
int c;
ssize_t bytes;
if (lineptr == NULL || n == NULL) {
errno = EINVAL;
return -1;
}
if (stream == NULL) {
errno = EBADF;
return -1;
}
/* resize (or allocate) the line buffer if necessary */
buf = *lineptr;
if (buf == NULL || *n < _GETDELIM_MINLEN) {
buf = (char*)realloc(*lineptr, _GETDELIM_GROWBY);
if (buf == NULL) {
/* ENOMEM */
return -1;
}
*n = _GETDELIM_GROWBY;
*lineptr = buf;
}
/* read characters until delimiter is found, end of file is reached, or an
error occurs. */
bytes = 0;
pos = buf;
while ((c = getc(stream)) != EOF) {
if (bytes + 1 >= SSIZE_MAX) {
errno = EOVERFLOW;
return -1;
}
bytes++;
if (bytes >= *n - 1) {
buf = (char*)realloc(*lineptr, *n + _GETDELIM_GROWBY);
if (buf == NULL) {
/* ENOMEM */
return -1;
}
*n += _GETDELIM_GROWBY;
pos = buf + bytes - 1;
*lineptr = buf;
}
*pos++ = (char) c;
if (c == delimiter) {
break;
}
}
if (ferror(stream) || (feof(stream) && (bytes == 0))) {
/* EOF, or an error from getc(). */
return -1;
}
*pos = '\0';
return bytes;
}
#endif
ssize_t getline(char ** lineptr, size_t * n, FILE * stream) {
2016-08-03 01:02:34 +00:00
#if __APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1050
2015-02-17 00:23:12 +00:00
return getdelim(lineptr, n, '\n', stream);
#else
return ::getline(lineptr, n, stream);
#endif
}
}
2013-08-20 02:12:55 +00:00
namespace {
// private...
%%{
machine lexer;
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
action addx {
value = (value << 4) + digittoint(fc);
}
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
ws = [ \t];
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
value =
'0x' xdigit+ @addx
|
'$' xdigit+ @addx
|
'-'? @{ negative = true; }
digit+ @{value = value * 10 + (fc - '0'); }
|
2013-08-20 02:12:55 +00:00
[A-Za-z_] @{ stringValue.push_back(fc); }
[A-Za-z0-9_]+ @{ stringValue.push_back(fc); }
;
2013-07-14 18:34:16 +00:00
2013-07-14 19:17:59 +00:00
2013-08-20 02:12:55 +00:00
name =
[A-Za-z0-9_]+ @{
name.push_back(fc);
2013-07-14 19:17:59 +00:00
}
2013-08-20 02:12:55 +00:00
;
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
main :=
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
name
ws*
'='
ws*
value
;
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
write data;
}%%
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
bool ParseLine(const char *p, const char *pe, std::map<std::string, uint16_t> &map)
{
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
// trap = number
// or trap = [previously defined trap]
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
std::string name;
std::string stringValue;
uint32_t value;
bool negative = false;
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
value = 0;
const char *eof = pe;
int cs;
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
%%write init;
%%write exec;
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
if (cs < %%{ write first_final; }%% )
{
return false;
}
if (negative) value = (-value) & 0xffff;
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
// name lookup
if (!stringValue.empty())
{
auto iter = map.find(stringValue);
if (iter == map.end())
{
fprintf(stderr, "Undefined trap: %s\n", stringValue.c_str());
return false;
}
value = iter->second;
}
2013-07-08 03:39:53 +00:00
#if 0
2013-08-20 02:12:55 +00:00
// if loading globals, wouldn't need to do this...
if (value > 0xafff || value < 0xa000)
{
fprintf(stderr, "Invalid trap number: $%04x\n", value);
return false;
}
#endif
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
map.emplace(name, (uint16_t)value);
return true;
2013-07-08 03:39:53 +00:00
}
2013-07-14 18:34:16 +00:00
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
}
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
namespace Debug {
void LoadTrapFile(const std::string &path, std::map<std::string, uint16_t> &map)
{
FILE *fp;
fp = fopen(path.c_str(), "r");
if (!fp)
2013-07-08 03:39:53 +00:00
{
2013-08-20 02:12:55 +00:00
fprintf(stderr, "Unable to open trap file %s\n", path.c_str());
return;
2013-07-08 03:39:53 +00:00
}
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
/*
* getline(3) is 2008 posix. it allocates (and resizes as appropriate)
* the buffer.
*
*/
char *lineBuffer = NULL;
size_t lineSize = 0;
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
for(;;)
{
char *line;
ssize_t length;
2013-07-08 03:39:53 +00:00
2015-02-17 00:23:12 +00:00
length = _loadtrap_rl::getline(&lineBuffer, &lineSize, fp);
2013-08-20 02:12:55 +00:00
if (!length) continue; //?
if (length < 0) break; // eof or error.
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
line = lineBuffer;
// skip any leading space.
while (length && isspace(*line))
{
++line;
--length;
}
if (!length) continue;
2013-07-08 03:39:53 +00:00
2013-08-20 02:12:55 +00:00
// comments
if (*line == '#') continue;
2013-07-14 18:34:16 +00:00
2013-08-20 02:12:55 +00:00
// strip any trailing space.
// (will be \n terminated unless there was no \n)
while (length && isspace(line[length - 1]))
{
line[--length] = 0;
}
if (!length) continue;
if (!ParseLine(line, line + length, map))
{
fprintf(stderr, "Error in trap definition: %s\n", line);
}
2013-07-14 18:34:16 +00:00
}
2013-08-20 02:12:55 +00:00
fclose(fp);
2013-07-08 03:39:53 +00:00
}
2013-08-20 02:12:55 +00:00
2013-07-08 03:39:53 +00:00
}
2013-07-14 18:34:16 +00:00
#ifdef TEST
int main(int argc, char **argv)
{
for (int i = 1; i < argc; ++i)
{
std::string f(argv[i]);
2013-08-20 02:12:55 +00:00
std::map<std::string, uint16_t> map;
Debug::LoadTrapFile(f, map);
2013-07-14 18:34:16 +00:00
for(const auto kv : map)
{
printf("%s -> %04x\n", kv.first.c_str(), kv.second);
}
}
return 0;
}
#endif