1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
libpsyc/test/test_parser.c

122 lines
3.1 KiB
C
Raw Normal View History

2013-07-16 14:53:51 +00:00
/*
This file is part of libpsyc.
Copyright (C) 2011,2012 Carlo v. Loesch, Gabor X Toth, Mathias L. Baumann,
and other contributing authors.
libpsyc is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version. As a special exception, libpsyc is distributed with additional
permissions to link libpsyc libraries with non-AGPL works.
libpsyc is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License and
the linking exception along with libpsyc in a COPYING file.
*/
2010-02-20 16:41:16 +00:00
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <psyc.h>
2011-05-09 07:02:15 +00:00
#include <psyc/parse.h>
2011-11-11 21:18:24 +00:00
int
main (int argc, char **argv)
2010-02-20 16:41:16 +00:00
{
2011-11-11 21:18:24 +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;
char buffer[2048], oper;
PsycString name, value, type;
2011-11-11 21:18:24 +00:00
PsycParseState state;
PsycParseListState listState;
2010-02-20 16:41:16 +00:00
2011-11-11 21:18:24 +00:00
if (argc < 2)
return -1;
int file = open(argv[1],O_RDONLY);
if (file < 0)
return -1;
idx = read(file,(void*)buffer,sizeof(buffer));
2010-02-20 16:41:16 +00:00
2011-11-11 21:18:24 +00:00
if (verbose) {
printf(">> INPUT\n");
printf("%.*s\n", (int)idx, buffer);
printf(">> PARSE\n");
}
2011-11-11 21:18:24 +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-11-11 21:18:24 +00:00
// try parsing that now
do {
oper = 0;
name.length = 0;
value.length = 0;
2011-04-30 14:42:03 +00:00
2011-11-11 21:18:24 +00:00
ret = psyc_parse(&state, &oper, &name, &value);
if (verbose)
printf(">> ret = %d\n", ret);
2011-11-11 21:18:24 +00:00
switch (ret) {
case PSYC_PARSE_ROUTING:
case PSYC_PARSE_ENTITY:
if (verbose)
printf("%c", oper);
case PSYC_PARSE_BODY:
// printf("the string is '%.*s'\n", name);
if (verbose)
printf("%.*s = %.*s\n",
(int)name.length, name.data,
(int)value.length, value.data);
2011-11-11 21:18:24 +00:00
if (psyc_var_is_list(PSYC_S2ARG(name))) {
if (verbose)
printf(">> LIST START\n");
2011-11-11 21:18:24 +00:00
psyc_parse_list_state_init(&listState);
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
while ((ret = psyc_parse_list(&listState, &type, &value))) {
2011-11-11 21:18:24 +00:00
switch (ret) {
case PSYC_PARSE_LIST_END:
case PSYC_PARSE_LIST_ELEM:
if (verbose)
printf("|%.*s %.*s\n", (int)type.length, type.data,
(int)value.length, value.data);
2011-11-11 21:18:24 +00:00
break;
default:
printf("Error while parsing list: %i\n", ret);
return 1;
}
2011-04-19 17:41:25 +00:00
2011-11-11 21:18:24 +00:00
if (ret == PSYC_PARSE_LIST_END) {
if (verbose)
printf(">> LIST END\n");
break;
}
}
2011-11-11 21:18:24 +00:00
}
break;
case PSYC_PARSE_COMPLETE:
// printf("Done parsing.\n");
ret = 0;
continue;
case PSYC_PARSE_INSUFFICIENT:
printf("Insufficient data.\n");
return 1;
default:
printf("Error while parsing: %i\n", ret);
return 1;
}
2011-11-11 21:18:24 +00:00
}
while (ret);
2011-11-11 21:18:24 +00:00
return 0;
2010-02-20 16:41:16 +00:00
}