2012-03-18 14:14:17 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
gbc_form_webpage.c
|
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2012-03-18 14:14:17 +01:00
|
|
|
|
|
|
|
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, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBC_FORM_WEBPAGE_C
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gb_str.h"
|
|
|
|
#include "gbc_compile.h"
|
|
|
|
#include "gbc_chown.h"
|
|
|
|
#include "gbc_form.h"
|
|
|
|
|
|
|
|
/*#define DEBUG*/
|
|
|
|
|
2012-09-20 04:29:37 +02:00
|
|
|
enum { TYPE_CODE, TYPE_HTML, TYPE_COMMENT, TYPE_MARKUP, TYPE_ARG, TYPE_ROOT };
|
2012-03-23 22:35:24 +01:00
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
static const char *_start;
|
|
|
|
|
|
|
|
static void print_quoted_string(const char *str, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char buf[8];
|
2012-08-29 02:43:48 +02:00
|
|
|
uchar c;
|
2012-03-18 14:14:17 +01:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FORM_print_char('"');
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2012-08-29 02:43:48 +02:00
|
|
|
c = (uchar)str[i];
|
2012-03-18 14:14:17 +01:00
|
|
|
//if (c >= ' ' && c <= 126 && c != '\\' && c != '"')
|
|
|
|
if (c >= ' ' && c != '\\' && c != '"')
|
|
|
|
FORM_print_char(c);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FORM_print_char('\\');
|
|
|
|
if (c == '\n')
|
|
|
|
c = 'n';
|
|
|
|
else if (c == '\r')
|
|
|
|
c = 'r';
|
|
|
|
else if (c == '\t')
|
|
|
|
c = 't';
|
|
|
|
else if (!(c == '"' || c == '\\'))
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "x%02X", c);
|
|
|
|
FORM_print_len(buf, 3);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
FORM_print_char(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FORM_print_char('"');
|
|
|
|
}
|
|
|
|
|
2012-03-24 13:25:59 +01:00
|
|
|
static void print_lower_len(const char *str, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
FORM_print_char(tolower(str[i]));
|
|
|
|
}
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
static void flush_html(const char *end)
|
|
|
|
{
|
|
|
|
if (_start == end)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FORM_print_len("Print ", 6);
|
|
|
|
if (end[-1] == '\n')
|
|
|
|
{
|
|
|
|
print_quoted_string(_start, end - _start - 1);
|
|
|
|
FORM_print_char('\n');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_quoted_string(_start, end - _start);
|
|
|
|
FORM_print_len(";\n", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
_start = end;
|
|
|
|
}
|
|
|
|
|
2012-09-20 04:29:37 +02:00
|
|
|
static bool check_contents(const char *str, int len)
|
|
|
|
{
|
|
|
|
if (len < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (str[0] == '-' && str[1] == '-' && str[len - 1] == '-' && str[len - 2] == '-')
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-04-29 23:27:11 +02:00
|
|
|
static int print_code(const char *str, int len)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
const char *start = str;
|
|
|
|
|
|
|
|
//fprintf(stderr, "'%.*s'\n", len, str);
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
c = *str++;
|
|
|
|
len--;
|
|
|
|
|
|
|
|
if (c == '"')
|
|
|
|
{
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
c = *str++;
|
|
|
|
len--;
|
|
|
|
if (c == '"')
|
|
|
|
break;
|
|
|
|
if (c == '\\' && *str)
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (c == '%' && *str == '>')
|
|
|
|
{
|
|
|
|
len = str - start - 1;
|
|
|
|
FORM_print_len(start, len);
|
|
|
|
return len + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
THROW("&1: syntax error in form file", JOB->form);
|
|
|
|
}
|
|
|
|
|
2012-03-24 13:25:59 +01:00
|
|
|
static void print_markup(const char *str, int len)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
char c;
|
|
|
|
const char *p;
|
|
|
|
bool comma;
|
|
|
|
bool quote;
|
2012-09-20 04:29:37 +02:00
|
|
|
bool end;
|
2012-03-24 13:25:59 +01:00
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
c = *str++;
|
|
|
|
len--;
|
|
|
|
if (c == ' ')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = str - p - (c == ' ');
|
2012-09-20 04:29:37 +02:00
|
|
|
|
|
|
|
if (*p == '/')
|
|
|
|
{
|
|
|
|
end = TRUE;
|
|
|
|
p++;
|
|
|
|
l--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
end = FALSE;
|
|
|
|
|
2012-03-24 13:25:59 +01:00
|
|
|
if (l <= 0)
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
|
|
|
|
FORM_print_len(p, l);
|
2012-09-20 04:29:37 +02:00
|
|
|
|
|
|
|
if (end)
|
|
|
|
{
|
2013-04-29 23:27:11 +02:00
|
|
|
FORM_print(".__RenderEnd()\n");
|
2012-09-20 04:29:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-29 23:27:11 +02:00
|
|
|
FORM_print(".__Render(");
|
2012-03-24 13:25:59 +01:00
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
FORM_print_char('[');
|
|
|
|
|
|
|
|
comma = FALSE;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
while (len > 0 && *str == ' ')
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
2013-04-29 23:27:11 +02:00
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
2012-03-24 13:25:59 +01:00
|
|
|
p = str;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
c = *str++;
|
|
|
|
len--;
|
|
|
|
if (c == '=' || c == ' ')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comma)
|
|
|
|
FORM_print(", ");
|
|
|
|
|
|
|
|
FORM_print_char('"');
|
|
|
|
l = str - p - (c == '=');
|
|
|
|
print_lower_len(p, l);
|
|
|
|
FORM_print("\": ");
|
|
|
|
|
|
|
|
comma = TRUE;
|
|
|
|
|
|
|
|
if (len <= 0 || c == ' ')
|
|
|
|
{
|
|
|
|
FORM_print("True");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
|
2013-04-29 23:27:11 +02:00
|
|
|
// attr=<% ... %>
|
|
|
|
if (len >= 3 && *str == '<' && str[1] == '%' && str[2] == '=')
|
|
|
|
{
|
|
|
|
str += 3;
|
|
|
|
len -= 3;
|
|
|
|
FORM_print_char('(');
|
|
|
|
l = print_code(str, len);
|
|
|
|
FORM_print_char(')');
|
|
|
|
str += l;
|
|
|
|
len -= l;
|
|
|
|
//fprintf(stderr, "-> '%.*s'\n", len, str);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (*str == '"')
|
2012-03-24 13:25:59 +01:00
|
|
|
{
|
|
|
|
str++;
|
|
|
|
len--;
|
|
|
|
quote = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quote = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
c = *str++;
|
|
|
|
len--;
|
2013-04-29 23:27:11 +02:00
|
|
|
if (quote)
|
|
|
|
{
|
|
|
|
if (len > 0 && c == '\\')
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
else if (c == '"')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c == ' ')
|
|
|
|
break;
|
|
|
|
}
|
2012-03-24 13:25:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!quote && str == p)
|
|
|
|
FORM_print("True");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!quote)
|
|
|
|
FORM_print_char('"');
|
|
|
|
FORM_print_len(p, str - p);
|
|
|
|
if (!quote)
|
|
|
|
FORM_print_char('"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FORM_print_char(']');
|
|
|
|
}
|
|
|
|
|
|
|
|
FORM_print(")\n");
|
|
|
|
}
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
void FORM_webpage(char *source)
|
|
|
|
{
|
2012-03-23 22:35:24 +01:00
|
|
|
char type = TYPE_CODE;
|
2012-03-18 14:14:17 +01:00
|
|
|
char c;
|
|
|
|
const char *p;
|
|
|
|
int line;
|
|
|
|
char buf[8];
|
2012-09-20 04:29:37 +02:00
|
|
|
bool has_contents = FALSE;
|
2012-03-18 14:14:17 +01:00
|
|
|
|
|
|
|
line = FORM_FIRST_LINE;
|
|
|
|
|
|
|
|
FORM_print("Inherits WebPage\n\n");
|
2012-09-05 01:11:11 +02:00
|
|
|
FORM_print("Public Sub _Render(Optional (_Arg) As Collection = New Collection)\n\n");
|
2012-03-18 14:14:17 +01:00
|
|
|
|
|
|
|
p = source;
|
|
|
|
|
|
|
|
__PRINT:
|
|
|
|
|
|
|
|
_start = p;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
c = *p++;
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
flush_html(p - 1);
|
|
|
|
goto __END;
|
|
|
|
}
|
|
|
|
|
2012-09-20 04:29:37 +02:00
|
|
|
if (c == '<')
|
2012-03-18 14:14:17 +01:00
|
|
|
{
|
2012-09-20 04:29:37 +02:00
|
|
|
if (*p == '%')
|
|
|
|
{
|
|
|
|
flush_html(p - 1);
|
|
|
|
|
|
|
|
if (p[1] == '/' && p[2] == '%' && p[3] == '>')
|
|
|
|
{
|
|
|
|
p += 4;
|
|
|
|
type = TYPE_ROOT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
type = TYPE_CODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto __CODE;
|
|
|
|
}
|
|
|
|
else if (*p == '<')
|
|
|
|
{
|
|
|
|
flush_html(p - 1);
|
|
|
|
p++;
|
|
|
|
type = TYPE_MARKUP;
|
|
|
|
goto __CODE;
|
|
|
|
}
|
2012-03-18 14:14:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
if ((p - _start) >= 256)
|
|
|
|
flush_html(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__CODE:
|
|
|
|
|
|
|
|
FORM_print("#Line ");
|
|
|
|
sprintf(buf, "%d", line);
|
|
|
|
FORM_print(buf);
|
|
|
|
FORM_print_char('\n');
|
2012-09-20 04:29:37 +02:00
|
|
|
|
|
|
|
if (type == TYPE_CODE)
|
2012-03-24 13:25:59 +01:00
|
|
|
{
|
2012-09-20 04:29:37 +02:00
|
|
|
if (*p == '=')
|
|
|
|
{
|
|
|
|
type = TYPE_HTML;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else if (*p == '!')
|
|
|
|
{
|
|
|
|
type = TYPE_ARG;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else if (*p == '-' && p[1] == '-')
|
|
|
|
{
|
|
|
|
type = TYPE_COMMENT;
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
type = TYPE_CODE;
|
2012-03-24 13:25:59 +01:00
|
|
|
}
|
2012-09-20 04:29:37 +02:00
|
|
|
else if (type == TYPE_ROOT)
|
2012-03-23 22:35:24 +01:00
|
|
|
{
|
2012-09-20 04:29:37 +02:00
|
|
|
FORM_print("Print Html$(Application.Root);\n");
|
|
|
|
goto __PRINT;
|
2012-03-23 22:35:24 +01:00
|
|
|
}
|
2012-03-18 14:14:17 +01:00
|
|
|
|
|
|
|
_start = p;
|
|
|
|
|
|
|
|
__END_STRING:
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
c = *p++;
|
|
|
|
if (!c)
|
|
|
|
goto __ERROR;
|
|
|
|
if (c == '"')
|
|
|
|
goto __STRING;
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-20 04:29:37 +02:00
|
|
|
if (c == '%' && *p == '>' && type != TYPE_MARKUP)
|
2012-03-18 14:14:17 +01:00
|
|
|
{
|
2012-03-23 22:35:24 +01:00
|
|
|
switch (type)
|
2012-03-18 14:14:17 +01:00
|
|
|
{
|
2012-03-23 22:35:24 +01:00
|
|
|
case TYPE_CODE:
|
|
|
|
FORM_print_len(_start, p - _start - 1);
|
|
|
|
FORM_print_char('\n');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_HTML:
|
|
|
|
|
2012-09-20 04:29:37 +02:00
|
|
|
if ((p - _start) == 1)
|
|
|
|
{
|
|
|
|
FORM_print("Print Html$(Application.Root);\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FORM_print("Print Html$(");
|
|
|
|
FORM_print_len(_start, p - _start - 1);
|
|
|
|
FORM_print(");\n");
|
|
|
|
}
|
2012-03-24 13:25:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TYPE_ARG:
|
|
|
|
FORM_print("Print _Arg!");
|
|
|
|
print_lower_len(_start, p - _start - 1);
|
|
|
|
FORM_print(";\n");
|
|
|
|
break;
|
|
|
|
|
2012-03-23 22:35:24 +01:00
|
|
|
case TYPE_COMMENT:
|
|
|
|
default:
|
|
|
|
break;
|
2012-03-18 14:14:17 +01:00
|
|
|
}
|
2012-03-23 22:35:24 +01:00
|
|
|
|
2012-09-20 04:29:37 +02:00
|
|
|
p++;
|
|
|
|
if (*p == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
goto __PRINT;
|
|
|
|
}
|
|
|
|
else if (c == '>' && *p == '>' && type == TYPE_MARKUP)
|
|
|
|
{
|
|
|
|
if (check_contents(_start, p - _start - 1))
|
|
|
|
{
|
|
|
|
if (has_contents)
|
|
|
|
THROW("Contents already declared");
|
|
|
|
has_contents = TRUE;
|
|
|
|
|
|
|
|
FORM_print("\nEnd\n\n");
|
|
|
|
FORM_print("Public Sub _RenderEnd()\n\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_markup(_start, p - _start - 1);
|
|
|
|
FORM_print_char('\n');
|
|
|
|
}
|
|
|
|
|
2012-03-18 14:14:17 +01:00
|
|
|
p++;
|
|
|
|
if (*p == '\n')
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
goto __PRINT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__STRING:
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
c = *p++;
|
|
|
|
if (!c)
|
|
|
|
goto __ERROR;
|
|
|
|
if (c == '"')
|
|
|
|
goto __END_STRING;
|
|
|
|
if (c == '\\' && *p)
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
__END:
|
|
|
|
|
|
|
|
FORM_print("\nEnd\n\n");
|
|
|
|
return;
|
|
|
|
|
|
|
|
__ERROR:
|
|
|
|
|
|
|
|
THROW("&1: syntax error in form file", JOB->form);
|
|
|
|
}
|
|
|
|
|