2010-05-25 15:19:52 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Robert Bieber
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "skin_parser.h"
|
|
|
|
#include "skin_debug.h"
|
|
|
|
#include "tag_table.h"
|
|
|
|
#include "symbols.h"
|
|
|
|
#include "skin_scan.h"
|
|
|
|
|
|
|
|
/* Declaration of parse tree buffer */
|
|
|
|
char skin_parse_tree[SKIN_MAX_MEMORY];
|
2010-05-25 22:24:08 +00:00
|
|
|
int skin_current_block = 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
/* Global variables for the parser */
|
|
|
|
int skin_line = 0;
|
|
|
|
|
|
|
|
/* Auxiliary parsing functions (not visible at global scope) */
|
2010-06-01 07:11:23 +00:00
|
|
|
struct skin_element* skin_parse_viewport(char** document);
|
2010-05-25 15:19:52 +00:00
|
|
|
struct skin_element* skin_parse_line(char** document);
|
|
|
|
struct skin_element* skin_parse_line_optional(char** document, int conditional);
|
|
|
|
struct skin_element* skin_parse_sublines(char** document);
|
|
|
|
struct skin_element* skin_parse_sublines_optional(char** document,
|
|
|
|
int conditional);
|
|
|
|
|
|
|
|
int skin_parse_tag(struct skin_element* element, char** document);
|
|
|
|
int skin_parse_text(struct skin_element* element, char** document,
|
|
|
|
int conditional);
|
|
|
|
int skin_parse_conditional(struct skin_element* element, char** document);
|
|
|
|
int skin_parse_comment(struct skin_element* element, char** document);
|
|
|
|
struct skin_element* skin_parse_code_as_arg(char** document);
|
|
|
|
|
2010-06-01 21:25:02 +00:00
|
|
|
struct skin_element* skin_parse(const char* document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-06-01 07:11:23 +00:00
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
struct skin_element* root = NULL;
|
|
|
|
struct skin_element* last = NULL;
|
|
|
|
|
|
|
|
struct skin_element** to_write = 0;
|
|
|
|
|
2010-06-01 21:25:02 +00:00
|
|
|
char* cursor = (char*)document; /*Keeps track of location in the document*/
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
skin_line = 1;
|
|
|
|
|
2010-06-07 21:09:13 +00:00
|
|
|
skin_clear_errors();
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
while(*cursor != '\0')
|
2010-06-01 07:11:23 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if(!root)
|
|
|
|
to_write = &root;
|
|
|
|
else
|
|
|
|
to_write = &(last->next);
|
|
|
|
|
|
|
|
|
|
|
|
*to_write = skin_parse_viewport(&cursor);
|
|
|
|
last = *to_write;
|
|
|
|
if(!last)
|
2010-06-08 19:34:27 +00:00
|
|
|
{
|
|
|
|
skin_free_tree(root); /* Clearing any memory already used */
|
2010-06-01 07:11:23 +00:00
|
|
|
return NULL;
|
2010-06-08 19:34:27 +00:00
|
|
|
}
|
2010-06-01 07:11:23 +00:00
|
|
|
|
|
|
|
/* Making sure last is at the end */
|
|
|
|
while(last->next)
|
|
|
|
last = last->next;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct skin_element* skin_parse_viewport(char** document)
|
|
|
|
{
|
|
|
|
|
|
|
|
struct skin_element* root = NULL;
|
|
|
|
struct skin_element* last = NULL;
|
|
|
|
struct skin_element* retval = NULL;
|
|
|
|
|
|
|
|
retval = skin_alloc_element();
|
|
|
|
retval->type = VIEWPORT;
|
|
|
|
retval->children_count = 1;
|
|
|
|
retval->line = skin_line;
|
|
|
|
|
|
|
|
struct skin_element** to_write = 0;
|
|
|
|
|
|
|
|
char* cursor = *document; /* Keeps track of location in the document */
|
|
|
|
char* bookmark; /* Used when we need to look ahead */
|
|
|
|
|
|
|
|
int sublines = 0; /* Flag for parsing sublines */
|
|
|
|
|
2010-06-01 18:31:58 +00:00
|
|
|
/* Parsing out the viewport tag if there is one */
|
|
|
|
if(check_viewport(cursor))
|
|
|
|
{
|
|
|
|
retval->children_count = 2;
|
|
|
|
retval->children = skin_alloc_children(2);
|
|
|
|
retval->children[0] = skin_alloc_element();
|
|
|
|
skin_parse_tag(retval->children[0], &cursor);
|
2010-06-02 07:04:33 +00:00
|
|
|
if(*cursor == '\n')
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
skin_line++;
|
|
|
|
}
|
2010-06-01 18:31:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval->children_count = 1;
|
|
|
|
retval->children = skin_alloc_children(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-01 07:11:23 +00:00
|
|
|
while(*cursor != '\0' && !(check_viewport(cursor) && cursor != *document))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* First, we check to see if this line will contain sublines */
|
|
|
|
bookmark = cursor;
|
|
|
|
sublines = 0;
|
2010-06-01 07:11:23 +00:00
|
|
|
while(*cursor != '\n' && *cursor != '\0'
|
|
|
|
&& !(check_viewport(cursor) && cursor != *document))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(*cursor == MULTILINESYM)
|
|
|
|
{
|
|
|
|
sublines = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
/* A ';' directly after a '%' doesn't count */
|
|
|
|
cursor ++;
|
|
|
|
|
|
|
|
if(*cursor == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-05-30 01:56:50 +00:00
|
|
|
else if(*cursor == COMMENTSYM)
|
|
|
|
{
|
|
|
|
skip_comment(&cursor);
|
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ARGLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
|
|
|
else if(*cursor == ENUMLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Advancing the cursor as normal */
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cursor = bookmark;
|
|
|
|
|
|
|
|
if(!root)
|
|
|
|
to_write = &root;
|
|
|
|
else
|
|
|
|
to_write = &(last->next);
|
|
|
|
|
2010-06-02 06:41:41 +00:00
|
|
|
if(sublines)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
*to_write = skin_parse_sublines(&cursor);
|
|
|
|
last = *to_write;
|
|
|
|
if(!last)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
*to_write = skin_parse_line(&cursor);
|
|
|
|
last = *to_write;
|
|
|
|
if(!last)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Making sure last is at the end */
|
|
|
|
while(last->next)
|
|
|
|
last = last->next;
|
|
|
|
|
2010-06-02 06:41:41 +00:00
|
|
|
if(*cursor == '\n')
|
2010-06-02 07:04:33 +00:00
|
|
|
{
|
2010-06-02 06:41:41 +00:00
|
|
|
cursor++;
|
2010-06-02 07:04:33 +00:00
|
|
|
skin_line++;
|
|
|
|
}
|
2010-06-01 07:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*document = cursor;
|
|
|
|
|
2010-06-01 18:31:58 +00:00
|
|
|
retval->children[retval->children_count - 1] = root;
|
2010-06-01 07:11:23 +00:00
|
|
|
return retval;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Auxiliary Parsing Functions */
|
|
|
|
|
|
|
|
struct skin_element* skin_parse_line(char**document)
|
|
|
|
{
|
|
|
|
|
|
|
|
return skin_parse_line_optional(document, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If conditional is set to true, then this will break upon encountering
|
|
|
|
* SEPERATESYM. This should only be used when parsing a line inside a
|
|
|
|
* conditional, otherwise just use the wrapper function skin_parse_line()
|
|
|
|
*/
|
|
|
|
struct skin_element* skin_parse_line_optional(char** document, int conditional)
|
|
|
|
{
|
|
|
|
char* cursor = *document;
|
|
|
|
|
|
|
|
struct skin_element* root = NULL;
|
|
|
|
struct skin_element* current = NULL;
|
2010-05-27 19:57:15 +00:00
|
|
|
struct skin_element* retval = NULL;
|
|
|
|
|
|
|
|
/* A wrapper for the line */
|
|
|
|
retval = skin_alloc_element();
|
|
|
|
retval->type = LINE;
|
|
|
|
retval->line = skin_line;
|
|
|
|
retval->children_count = 1;
|
|
|
|
retval->children = skin_alloc_children(1);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
while(*cursor != '\n' && *cursor != '\0' && *cursor != MULTILINESYM
|
2010-05-25 17:22:39 +00:00
|
|
|
&& !((*cursor == ARGLISTSEPERATESYM
|
|
|
|
|| *cursor == ARGLISTCLOSESYM
|
|
|
|
|| *cursor == ENUMLISTSEPERATESYM
|
|
|
|
|| *cursor == ENUMLISTCLOSESYM)
|
2010-06-01 07:11:23 +00:00
|
|
|
&& conditional)
|
|
|
|
&& !(check_viewport(cursor) && cursor != *document))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
/* Allocating memory if necessary */
|
|
|
|
if(root)
|
|
|
|
{
|
|
|
|
current->next = skin_alloc_element();
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current = skin_alloc_element();
|
|
|
|
root = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parsing the current element */
|
|
|
|
if(*cursor == TAGSYM && cursor[1] == CONDITIONSYM)
|
|
|
|
{
|
|
|
|
if(!skin_parse_conditional(current, &cursor))
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-02 05:45:34 +00:00
|
|
|
else if(*cursor == TAGSYM && !find_escape_character(cursor[1]))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(!skin_parse_tag(current, &cursor))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if(*cursor == COMMENTSYM)
|
|
|
|
{
|
|
|
|
if(!skin_parse_comment(current, &cursor))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!skin_parse_text(current, &cursor, conditional))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Moving up the calling function's pointer */
|
|
|
|
*document = cursor;
|
|
|
|
|
2010-05-27 19:57:15 +00:00
|
|
|
retval->children[0] = root;
|
|
|
|
return retval;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct skin_element* skin_parse_sublines(char** document)
|
|
|
|
{
|
|
|
|
return skin_parse_sublines_optional(document, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct skin_element* skin_parse_sublines_optional(char** document,
|
|
|
|
int conditional)
|
|
|
|
{
|
|
|
|
struct skin_element* retval;
|
|
|
|
char* cursor = *document;
|
|
|
|
int sublines = 1;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
retval = skin_alloc_element();
|
|
|
|
retval->type = SUBLINES;
|
|
|
|
retval->next = NULL;
|
2010-05-27 19:57:15 +00:00
|
|
|
retval->line = skin_line;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
/* First we count the sublines */
|
|
|
|
while(*cursor != '\0' && *cursor != '\n'
|
2010-05-25 17:22:39 +00:00
|
|
|
&& !((*cursor == ARGLISTSEPERATESYM
|
|
|
|
|| *cursor == ARGLISTCLOSESYM
|
|
|
|
|| *cursor == ENUMLISTSEPERATESYM
|
|
|
|
|| *cursor == ENUMLISTCLOSESYM)
|
2010-06-01 07:11:23 +00:00
|
|
|
&& conditional)
|
|
|
|
&& !(check_viewport(cursor) && cursor != *document))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(*cursor == COMMENTSYM)
|
2010-06-02 07:48:48 +00:00
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
skip_comment(&cursor);
|
2010-06-02 07:48:48 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ENUMLISTOPENSYM)
|
2010-06-02 07:48:48 +00:00
|
|
|
{
|
2010-06-07 23:49:06 +00:00
|
|
|
skip_enumlist(&cursor);
|
2010-06-02 07:48:48 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ARGLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
|
|
|
else if(*cursor == TAGSYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
if(*cursor == '\0' || *cursor == '\n')
|
|
|
|
break;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == MULTILINESYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
sublines++;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ...and then we parse them */
|
|
|
|
retval->children_count = sublines;
|
|
|
|
retval->children = skin_alloc_children(sublines);
|
|
|
|
|
|
|
|
cursor = *document;
|
|
|
|
for(i = 0; i < sublines; i++)
|
|
|
|
{
|
|
|
|
retval->children[i] = skin_parse_line_optional(&cursor, conditional);
|
|
|
|
skip_whitespace(&cursor);
|
|
|
|
|
|
|
|
if(*cursor != MULTILINESYM && i != sublines - 1)
|
|
|
|
{
|
|
|
|
skin_error(MULTILINE_EXPECTED);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-27 19:57:15 +00:00
|
|
|
else if(i != sublines - 1)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*document = cursor;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int skin_parse_tag(struct skin_element* element, char** document)
|
|
|
|
{
|
|
|
|
|
|
|
|
char* cursor = *document + 1;
|
|
|
|
char* bookmark;
|
|
|
|
|
|
|
|
char tag_name[3];
|
|
|
|
char* tag_args;
|
2010-06-01 16:44:52 +00:00
|
|
|
struct tag_info *tag;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
int num_args = 1;
|
|
|
|
int i;
|
2010-05-25 17:22:39 +00:00
|
|
|
int star = 0; /* Flag for the all-or-none option */
|
|
|
|
int req_args; /* To mark when we enter optional arguments */
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
int optional = 0;
|
|
|
|
|
|
|
|
/* Checking the tag name */
|
|
|
|
tag_name[0] = cursor[0];
|
|
|
|
tag_name[1] = cursor[1];
|
|
|
|
tag_name[2] = '\0';
|
|
|
|
|
|
|
|
/* First we check the two characters after the '%', then a single char */
|
2010-06-01 16:44:52 +00:00
|
|
|
tag = find_tag(tag_name);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-01 16:44:52 +00:00
|
|
|
if(!tag)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
tag_name[1] = '\0';
|
2010-06-01 16:44:52 +00:00
|
|
|
tag = find_tag(tag_name);
|
2010-05-25 15:19:52 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor += 2;
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:44:52 +00:00
|
|
|
if(!tag)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
skin_error(ILLEGAL_TAG);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copying basic tag info */
|
|
|
|
element->type = TAG;
|
2010-06-01 16:44:52 +00:00
|
|
|
element->tag = tag;
|
|
|
|
tag_args = tag->params;
|
2010-05-25 15:19:52 +00:00
|
|
|
element->line = skin_line;
|
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
/* Checking for the * flag */
|
|
|
|
if(tag_args[0] == '*')
|
|
|
|
{
|
|
|
|
star = 1;
|
|
|
|
tag_args++;
|
|
|
|
}
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
/* If this tag has no arguments, we can bail out now */
|
2010-05-25 17:22:39 +00:00
|
|
|
if(strlen(tag_args) == 0
|
2010-06-02 05:27:37 +00:00
|
|
|
|| (tag_args[0] == '|' && *cursor != ARGLISTOPENSYM)
|
|
|
|
|| (star && *cursor != ARGLISTOPENSYM))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
*document = cursor;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checking the number of arguments and allocating args */
|
|
|
|
if(*cursor != ARGLISTOPENSYM && tag_args[0] != '|')
|
|
|
|
{
|
|
|
|
skin_error(ARGLIST_EXPECTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
|
2010-06-07 23:49:06 +00:00
|
|
|
bookmark = cursor;
|
|
|
|
while(*cursor != '\n' && *cursor != '\0' && *cursor != ARGLISTCLOSESYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
/* Skipping over escaped characters */
|
|
|
|
if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
if(*cursor == '\0')
|
|
|
|
break;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == COMMENTSYM)
|
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
skip_comment(&cursor);
|
2010-06-07 23:49:06 +00:00
|
|
|
}
|
|
|
|
else if(*cursor == ARGLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
|
|
|
else if(*cursor == ARGLISTSEPERATESYM)
|
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
num_args++;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
cursor = bookmark; /* Restoring the cursor */
|
|
|
|
element->params_count = num_args;
|
|
|
|
element->params = skin_alloc_params(num_args);
|
|
|
|
|
|
|
|
/* Now we have to actually parse each argument */
|
2010-05-25 17:22:39 +00:00
|
|
|
for(i = 0; i < num_args; i++)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
/* Making sure we haven't run out of arguments */
|
|
|
|
if(*tag_args == '\0')
|
|
|
|
{
|
|
|
|
skin_error(TOO_MANY_ARGS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checking for the optional bar */
|
|
|
|
if(*tag_args == '|')
|
|
|
|
{
|
|
|
|
optional = 1;
|
2010-06-01 07:11:23 +00:00
|
|
|
req_args = i;
|
2010-05-25 15:19:52 +00:00
|
|
|
tag_args++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scanning the arguments */
|
2010-05-25 17:22:39 +00:00
|
|
|
skip_whitespace(&cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
/* Checking for comments */
|
|
|
|
if(*cursor == COMMENTSYM)
|
|
|
|
skip_comment(&cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-01 19:55:20 +00:00
|
|
|
/* Storing the type code */
|
|
|
|
element->params[i].type_code = *tag_args;
|
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
/* Checking a nullable argument for null */
|
2010-06-02 06:52:17 +00:00
|
|
|
if(*cursor == DEFAULTSYM && !isdigit(cursor[1]))
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
|
|
|
if(islower(*tag_args))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-05-25 17:22:39 +00:00
|
|
|
element->params[i].type = DEFAULT;
|
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-05-25 17:22:39 +00:00
|
|
|
else
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-05-25 17:22:39 +00:00
|
|
|
skin_error(DEFAULT_NOT_ALLOWED);
|
|
|
|
return 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-05-25 17:22:39 +00:00
|
|
|
}
|
|
|
|
else if(tolower(*tag_args) == 'i')
|
|
|
|
{
|
|
|
|
/* Scanning an int argument */
|
2010-06-02 06:52:17 +00:00
|
|
|
if(!isdigit(*cursor) && *cursor != '-')
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-05-25 17:22:39 +00:00
|
|
|
skin_error(INT_EXPECTED);
|
|
|
|
return 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
element->params[i].type = NUMERIC;
|
|
|
|
element->params[i].data.numeric = scan_int(&cursor);
|
|
|
|
}
|
|
|
|
else if(tolower(*tag_args) == 's' || tolower(*tag_args) == 'f')
|
|
|
|
{
|
|
|
|
/* Scanning a string argument */
|
|
|
|
element->params[i].type = STRING;
|
|
|
|
element->params[i].data.text = scan_string(&cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
}
|
|
|
|
else if(tolower(*tag_args) == 'c')
|
|
|
|
{
|
|
|
|
/* Recursively parsing a code argument */
|
|
|
|
element->params[i].type = CODE;
|
|
|
|
element->params[i].data.code = skin_parse_code_as_arg(&cursor);
|
|
|
|
if(!element->params[i].data.code)
|
2010-05-25 15:19:52 +00:00
|
|
|
return 0;
|
2010-05-25 17:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
skip_whitespace(&cursor);
|
|
|
|
|
|
|
|
if(*cursor != ARGLISTSEPERATESYM && i < num_args - 1)
|
|
|
|
{
|
|
|
|
skin_error(SEPERATOR_EXPECTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(*cursor != ARGLISTCLOSESYM && i == num_args - 1)
|
|
|
|
{
|
|
|
|
skin_error(CLOSE_EXPECTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tag_args++;
|
|
|
|
|
2010-06-01 07:11:23 +00:00
|
|
|
/* Checking for the optional bar */
|
|
|
|
if(*tag_args == '|')
|
|
|
|
{
|
|
|
|
optional = 1;
|
|
|
|
req_args = i + 1;
|
|
|
|
tag_args++;
|
|
|
|
}
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
/* Checking for a premature end */
|
2010-06-02 05:45:34 +00:00
|
|
|
if(*tag_args != '\0' && !optional)
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
|
|
|
skin_error(INSUFFICIENT_ARGS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
*document = cursor;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the conditional flag is set true, then parsing text will stop at an
|
|
|
|
* ARGLISTSEPERATESYM. Only set that flag when parsing within a conditional
|
|
|
|
*/
|
|
|
|
int skin_parse_text(struct skin_element* element, char** document,
|
|
|
|
int conditional)
|
|
|
|
{
|
|
|
|
char* cursor = *document;
|
|
|
|
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
int dest;
|
|
|
|
|
|
|
|
/* First figure out how much text we're copying */
|
|
|
|
while(*cursor != '\0' && *cursor != '\n' && *cursor != MULTILINESYM
|
|
|
|
&& *cursor != COMMENTSYM
|
2010-05-25 17:22:39 +00:00
|
|
|
&& !((*cursor == ARGLISTSEPERATESYM
|
|
|
|
|| *cursor == ARGLISTCLOSESYM
|
|
|
|
|| *cursor == ENUMLISTSEPERATESYM
|
|
|
|
|| *cursor == ENUMLISTCLOSESYM)
|
|
|
|
&& conditional))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
/* Dealing with possibility of escaped characters */
|
|
|
|
if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
if(find_escape_character(cursor[1]))
|
|
|
|
cursor++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
length++;
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = *document;
|
|
|
|
|
|
|
|
/* Copying the text into the element struct */
|
|
|
|
element->type = TEXT;
|
|
|
|
element->line = skin_line;
|
|
|
|
element->next = NULL;
|
|
|
|
element->text = skin_alloc_string(length);
|
|
|
|
|
|
|
|
for(dest = 0; dest < length; dest++)
|
|
|
|
{
|
|
|
|
/* Advancing cursor if we've encountered an escaped character */
|
|
|
|
if(*cursor == TAGSYM)
|
|
|
|
cursor++;
|
|
|
|
|
|
|
|
element->text[dest] = *cursor;
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
element->text[length] = '\0';
|
|
|
|
|
|
|
|
*document = cursor;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int skin_parse_conditional(struct skin_element* element, char** document)
|
|
|
|
{
|
|
|
|
|
|
|
|
char* cursor = *document + 1; /* Starting past the "%" */
|
|
|
|
char* bookmark;
|
|
|
|
struct skin_element* tag = skin_alloc_element(); /* The tag to evaluate */
|
|
|
|
int children = 1;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
element->type = CONDITIONAL;
|
|
|
|
element->line = skin_line;
|
|
|
|
|
|
|
|
/* Parsing the tag first */
|
|
|
|
if(!skin_parse_tag(tag, &cursor))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Counting the children */
|
2010-05-25 17:22:39 +00:00
|
|
|
if(*(cursor++) != ENUMLISTOPENSYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
skin_error(ARGLIST_EXPECTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
bookmark = cursor;
|
2010-05-25 17:22:39 +00:00
|
|
|
while(*cursor != ENUMLISTCLOSESYM && *cursor != '\n' && *cursor != '\0')
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(*cursor == COMMENTSYM)
|
|
|
|
{
|
|
|
|
skip_comment(&cursor);
|
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ENUMLISTOPENSYM)
|
2010-06-02 07:48:48 +00:00
|
|
|
{
|
2010-06-07 23:49:06 +00:00
|
|
|
skip_enumlist(&cursor);
|
2010-06-02 07:48:48 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == TAGSYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
if(*cursor == '\0' || *cursor == '\n')
|
|
|
|
break;
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ENUMLISTSEPERATESYM)
|
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
children++;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
cursor = bookmark;
|
|
|
|
|
|
|
|
/* Parsing the children */
|
|
|
|
element->children_count = children + 1; /* Make sure to include the tag */
|
|
|
|
element->children = skin_alloc_children(children + 1);
|
|
|
|
element->children[0] = tag;
|
|
|
|
|
|
|
|
for(i = 1; i < children + 1; i++)
|
|
|
|
{
|
|
|
|
element->children[i] = skin_parse_code_as_arg(&cursor);
|
|
|
|
skip_whitespace(&cursor);
|
|
|
|
|
|
|
|
if(i < children && *cursor != ENUMLISTSEPERATESYM)
|
|
|
|
{
|
|
|
|
skin_error(SEPERATOR_EXPECTED);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-25 17:22:39 +00:00
|
|
|
else if(i == children && *cursor != ENUMLISTCLOSESYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
skin_error(CLOSE_EXPECTED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*document = cursor;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int skin_parse_comment(struct skin_element* element, char** document)
|
|
|
|
{
|
|
|
|
char* cursor = *document;
|
|
|
|
|
|
|
|
int length;
|
|
|
|
/*
|
|
|
|
* Finding the index of the ending newline or null-terminator
|
|
|
|
* The length of the string of interest doesn't include the leading #, the
|
|
|
|
* length we need to reserve is the same as the index of the last character
|
|
|
|
*/
|
|
|
|
for(length = 0; cursor[length] != '\n' && cursor[length] != '\0'; length++);
|
|
|
|
|
|
|
|
element->type = COMMENT;
|
|
|
|
element->line = skin_line;
|
|
|
|
element->text = skin_alloc_string(length);
|
|
|
|
/* We copy from one char past cursor to leave out the # */
|
2010-06-01 21:25:02 +00:00
|
|
|
memcpy((void*)(element->text), (void*)(cursor + 1),
|
|
|
|
sizeof(char) * (length-1));
|
|
|
|
element->text[length - 1] = '\0';
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-01 21:25:02 +00:00
|
|
|
if(cursor[length] == '\n')
|
2010-05-25 15:19:52 +00:00
|
|
|
skin_line++;
|
|
|
|
|
2010-06-01 21:25:02 +00:00
|
|
|
*document += (length); /* Move cursor up past # and all text */
|
|
|
|
if(**document == '\n')
|
|
|
|
(*document)++;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct skin_element* skin_parse_code_as_arg(char** document)
|
|
|
|
{
|
|
|
|
|
|
|
|
int sublines = 0;
|
|
|
|
char* cursor = *document;
|
|
|
|
|
|
|
|
/* Checking for sublines */
|
|
|
|
while(*cursor != '\n' && *cursor != '\0')
|
|
|
|
{
|
|
|
|
if(*cursor == MULTILINESYM)
|
|
|
|
{
|
|
|
|
sublines = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
/* A ';' directly after a '%' doesn't count */
|
|
|
|
cursor ++;
|
|
|
|
|
|
|
|
if(*cursor == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Advancing the cursor as normal */
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sublines)
|
|
|
|
return skin_parse_sublines_optional(document, 1);
|
|
|
|
else
|
|
|
|
return skin_parse_line_optional(document, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Memory management */
|
|
|
|
struct skin_element* skin_alloc_element()
|
|
|
|
{
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
char* retval = &skin_parse_tree[skin_current_block * 4];
|
|
|
|
|
|
|
|
int delta = sizeof(struct skin_element) / (sizeof(char) * 4);
|
|
|
|
|
|
|
|
/* If one block is partially filled, make sure to advance to the
|
|
|
|
* next one for the next allocation
|
|
|
|
*/
|
|
|
|
if(sizeof(struct skin_element) % (sizeof(char) * 4) != 0)
|
|
|
|
delta++;
|
|
|
|
|
|
|
|
skin_current_block += delta;
|
|
|
|
|
|
|
|
return (struct skin_element*)retval;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct skin_element* retval = (struct skin_element*)
|
|
|
|
malloc(sizeof(struct skin_element));
|
|
|
|
retval->next = NULL;
|
|
|
|
retval->params_count = 0;
|
|
|
|
retval->children_count = 0;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct skin_tag_parameter* skin_alloc_params(int count)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
char* retval = &skin_parse_tree[skin_current_block * 4];
|
|
|
|
|
|
|
|
int delta = sizeof(struct skin_tag_parameter) / (sizeof(char) * 4);
|
|
|
|
delta *= count;
|
|
|
|
|
|
|
|
/* Correcting uneven alignment */
|
|
|
|
if(count * sizeof(struct skin_tag_parameter) % (sizeof(char) * 4) != 0)
|
|
|
|
delta++;
|
|
|
|
|
|
|
|
skin_current_block += delta;
|
|
|
|
|
|
|
|
return (struct skin_tag_parameter*) retval;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (struct skin_tag_parameter*)malloc(sizeof(struct skin_tag_parameter)
|
|
|
|
* count);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char* skin_alloc_string(int length)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
char* retval = &skin_parse_tree[skin_current_block * 4];
|
|
|
|
|
|
|
|
/* Checking alignment */
|
|
|
|
length++; /* Accounting for the null terminator */
|
|
|
|
int delta = length / 4;
|
|
|
|
if(length % 4 != 0)
|
|
|
|
delta++;
|
|
|
|
|
|
|
|
skin_current_block += delta;
|
|
|
|
|
|
|
|
if(skin_current_block >= SKIN_MAX_MEMORY / 4)
|
|
|
|
skin_error(MEMORY_LIMIT_EXCEEDED);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (char*)malloc(sizeof(char) * (length + 1));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct skin_element** skin_alloc_children(int count)
|
|
|
|
{
|
|
|
|
return (struct skin_element**) malloc(sizeof(struct skin_element*) * count);
|
|
|
|
}
|
2010-05-25 22:24:08 +00:00
|
|
|
|
|
|
|
void skin_free_tree(struct skin_element* root)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* First make the recursive call */
|
|
|
|
if(!root)
|
|
|
|
return;
|
|
|
|
skin_free_tree(root->next);
|
|
|
|
|
|
|
|
/* Free any text */
|
|
|
|
if(root->type == TEXT)
|
|
|
|
free(root->text);
|
|
|
|
|
|
|
|
/* Then recursively free any children, before freeing their pointers */
|
|
|
|
for(i = 0; i < root->children_count; i++)
|
|
|
|
skin_free_tree(root->children[i]);
|
|
|
|
if(root->children_count > 0)
|
|
|
|
free(root->children);
|
|
|
|
|
|
|
|
/* Free any parameters, making sure to deallocate strings */
|
|
|
|
for(i = 0; i < root->params_count; i++)
|
|
|
|
if(root->params[i].type == STRING)
|
|
|
|
free(root->params[i].data.text);
|
|
|
|
if(root->params_count > 0)
|
|
|
|
free(root->params);
|
|
|
|
|
|
|
|
/* Finally, delete root's memory */
|
|
|
|
free(root);
|
|
|
|
}
|