2010-02-20 16:41:16 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-04-26 20:44:48 +00:00
|
|
|
#include <psyc/lib.h>
|
|
|
|
#include <psyc/parser.h>
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2010-02-20 16:41:16 +00:00
|
|
|
{
|
2011-04-26 20:44:48 +00:00
|
|
|
int idx, ret, verbose = argc > 2;
|
2011-04-22 20:59:15 +00:00
|
|
|
char buffer[2048], oper;
|
2011-04-25 21:40:38 +00:00
|
|
|
psycString name, value, elem;
|
|
|
|
psycParseState state;
|
|
|
|
psycParseListState listState;
|
2010-02-20 16:41:16 +00:00
|
|
|
|
|
|
|
int file = open(argv[1],O_RDONLY);
|
2011-04-26 20:44:48 +00:00
|
|
|
if (file < 0)
|
2011-04-19 07:21:00 +00:00
|
|
|
return -1;
|
2011-04-26 20:44:48 +00:00
|
|
|
idx = read(file,(void*)buffer,sizeof(buffer));
|
2010-02-20 16:41:16 +00:00
|
|
|
|
2011-04-26 01:43:49 +00:00
|
|
|
if (verbose) {
|
|
|
|
write(1, ">> INPUT\n", 9);
|
2011-04-26 20:44:48 +00:00
|
|
|
write(1, buffer, idx);
|
2011-04-26 01:43:49 +00:00
|
|
|
write(1, ">> PARSE\n", 9);
|
|
|
|
}
|
2011-04-25 21:40:38 +00:00
|
|
|
psyc_initParseState(&state);
|
2011-04-26 20:44:48 +00:00
|
|
|
psyc_nextParseBuffer(&state, psyc_newString(buffer, idx));
|
2010-02-20 16:41:16 +00:00
|
|
|
|
2011-04-19 07:21:00 +00:00
|
|
|
// try parsing that now
|
2011-04-25 22:10:02 +00:00
|
|
|
// while ((ret = psyc_parse(&state, &oper, &name, &value)))
|
|
|
|
// {
|
|
|
|
do {
|
|
|
|
ret = psyc_parse(&state, &oper, &name, &value);
|
2011-04-17 11:25:06 +00:00
|
|
|
switch (ret)
|
2010-02-20 21:29:09 +00:00
|
|
|
{
|
2011-04-22 15:09:32 +00:00
|
|
|
case PSYC_PARSE_ROUTING:
|
|
|
|
case PSYC_PARSE_ENTITY:
|
2011-04-26 01:43:49 +00:00
|
|
|
if (verbose)
|
|
|
|
write(1, &oper, 1);
|
2011-04-22 15:09:32 +00:00
|
|
|
case PSYC_PARSE_BODY:
|
2011-04-20 16:59:27 +00:00
|
|
|
// printf("the string is '%.*s'\n", name);
|
2011-04-26 01:43:49 +00:00
|
|
|
if (verbose) {
|
|
|
|
write(1, name.ptr, name.length);
|
|
|
|
write(1, " = ", 3);
|
|
|
|
write(1, value.ptr, value.length);
|
|
|
|
write(1, "\n", 1);
|
|
|
|
}
|
2011-04-19 17:41:25 +00:00
|
|
|
if (memcmp(name.ptr, "_list", 5) == 0)
|
|
|
|
{
|
2011-04-26 01:43:49 +00:00
|
|
|
if (verbose)
|
|
|
|
write(1, ">>> LIST START\n", 15);
|
2011-04-25 21:40:38 +00:00
|
|
|
psyc_initParseListState(&listState);
|
|
|
|
psyc_nextParseListBuffer(&listState, value);
|
|
|
|
while ((ret = psyc_parseList(&listState, &name, &value, &elem)))
|
2011-04-19 17:41:25 +00:00
|
|
|
{
|
|
|
|
switch (ret)
|
|
|
|
{
|
2011-04-22 15:09:32 +00:00
|
|
|
case PSYC_PARSE_LIST_END:
|
|
|
|
case PSYC_PARSE_LIST_ELEM:
|
2011-04-26 01:43:49 +00:00
|
|
|
if (verbose) {
|
|
|
|
write(1, "|", 1);
|
|
|
|
write(1, elem.ptr, elem.length);
|
|
|
|
write(1, "\n", 1);
|
|
|
|
}
|
2011-04-19 17:41:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("Error while parsing list: %i\n", ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-22 15:09:32 +00:00
|
|
|
if (ret == PSYC_PARSE_LIST_END)
|
2011-04-19 17:41:25 +00:00
|
|
|
{
|
2011-04-26 01:43:49 +00:00
|
|
|
if (verbose)
|
|
|
|
write(1, ">>> LIST END\n", 13);
|
2011-04-19 17:41:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-17 11:25:06 +00:00
|
|
|
break;
|
2011-04-22 15:09:32 +00:00
|
|
|
case PSYC_PARSE_COMPLETE:
|
2011-04-25 22:10:02 +00:00
|
|
|
// printf("Done parsing.\n");
|
|
|
|
ret = 0;
|
2011-04-17 11:25:06 +00:00
|
|
|
continue;
|
2011-04-22 15:09:32 +00:00
|
|
|
case PSYC_PARSE_INSUFFICIENT:
|
2011-04-19 07:21:00 +00:00
|
|
|
printf("Insufficient data.\n");
|
2011-04-25 22:10:02 +00:00
|
|
|
return -1;
|
2011-04-17 11:25:06 +00:00
|
|
|
default:
|
|
|
|
printf("Error while parsing: %i\n", ret);
|
2011-04-19 07:21:00 +00:00
|
|
|
return 1;
|
2010-02-20 21:29:09 +00:00
|
|
|
}
|
2011-04-25 22:10:02 +00:00
|
|
|
} while (ret);
|
|
|
|
|
2010-02-20 16:41:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|