2010-02-20 16:41:16 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
#include <psyc.h>
|
2011-05-09 07:02:15 +00:00
|
|
|
#include <psyc/parse.h>
|
2011-04-26 20:44:48 +00:00
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
int main (int argc, char **argv)
|
2010-02-20 16:41:16 +00:00
|
|
|
{
|
2011-05-04 13:41:35 +00:00
|
|
|
uint8_t routing_only = argc > 2 && memchr(argv[2], (int)'r', strlen(argv[2]));
|
|
|
|
uint8_t verbose = argc > 2 && memchr(argv[2], (int)'v', strlen(argv[2]));
|
|
|
|
int idx, ret;
|
2011-04-22 20:59:15 +00:00
|
|
|
char buffer[2048], oper;
|
2011-10-31 19:26:47 +00:00
|
|
|
PsycString name, value, elem;
|
|
|
|
PsycParseState state;
|
|
|
|
PsycParseListState listState;
|
2010-02-20 16:41:16 +00:00
|
|
|
|
2011-05-04 13:41:35 +00:00
|
|
|
if (argc < 2)
|
|
|
|
return -1;
|
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) {
|
2011-05-03 23:00:35 +00:00
|
|
|
printf(">> INPUT\n");
|
|
|
|
printf("%.*s\n", (int)idx, buffer);
|
|
|
|
printf(">> PARSE\n");
|
2011-04-26 01:43:49 +00:00
|
|
|
}
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
psyc_parse_state_init(&state, routing_only ?
|
|
|
|
PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
|
|
|
|
psyc_parse_buffer_set(&state, buffer, idx);
|
2010-02-20 16:41:16 +00:00
|
|
|
|
2011-04-19 07:21:00 +00:00
|
|
|
// try parsing that now
|
2011-05-03 23:00:35 +00:00
|
|
|
do
|
|
|
|
{
|
2011-04-30 14:42:03 +00:00
|
|
|
oper = 0;
|
|
|
|
name.length = 0;
|
|
|
|
value.length = 0;
|
|
|
|
|
2011-04-25 22:10:02 +00:00
|
|
|
ret = psyc_parse(&state, &oper, &name, &value);
|
2011-04-30 14:42:03 +00:00
|
|
|
if (verbose)
|
|
|
|
printf(">> ret = %d\n", ret);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
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)
|
2011-05-03 23:00:35 +00:00
|
|
|
printf("%c", oper);
|
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-05-03 23:00:35 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("%.*s = %.*s\n",
|
2011-11-01 11:06:58 +00:00
|
|
|
(int)name.length, name.data,
|
|
|
|
(int)value.length, value.data);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
if (psyc_var_is_list(PSYC_S2ARG(name)))
|
2011-04-19 17:41:25 +00:00
|
|
|
{
|
2011-04-26 01:43:49 +00:00
|
|
|
if (verbose)
|
2011-05-03 23:00:35 +00:00
|
|
|
printf(">> LIST START\n");
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
psyc_parse_list_state_init(&listState);
|
|
|
|
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
while ((ret = psyc_parse_list(&listState, &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-05-03 23:00:35 +00:00
|
|
|
if (verbose)
|
2011-11-01 11:06:58 +00:00
|
|
|
printf("|%.*s\n", (int)elem.length, elem.data);
|
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)
|
2011-05-03 23:00:35 +00:00
|
|
|
printf(">> LIST END\n");
|
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-29 21:00:03 +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-05-03 23:00:35 +00:00
|
|
|
}
|
|
|
|
while (ret);
|
2011-04-25 22:10:02 +00:00
|
|
|
|
2010-02-20 16:41:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|