mpw/bin/loadtrap.rl

220 lines
4.2 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>
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
2013-08-20 02:12:55 +00:00
length = getline(&lineBuffer, &lineSize, fp);
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