Tomato 1.28
[tomato.git] / release / src / router / httpd / parser.c
blob9dc4e94d565b96797058f50e6294b160eca51463
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
11 #define DEBUG 1
14 <% ident(arg, "arg", 'arg'); %>
16 Syntax checking is very relaxed and all arguments are considered a
17 string. Example, the following are the same:
19 <% ident(foo); %>
20 <% ident('foo'); %>
23 int parse_asp(const char *path)
25 char *buffer;
26 char *cp;
27 char *a, *b, *c;
28 char x;
29 int argc;
30 char *argv[32];
31 char *ident;
32 const aspapi_t *api;
34 #if TOMATO_N
35 // temp!!!
36 char npath[256];
37 int n;
39 if (!nvram_match("debug_npages", "0")) {
40 if (((a = strrchr(path, '.')) != NULL) && ((n = a - path) > 3) && (strncmp(a - 2, "-n", 2) != 0)) {
41 memcpy(npath, path, n);
42 memcpy(npath + n, "-n", 2);
43 strcpy(npath + n + 2, a);
44 if (f_exists(npath)) {
45 path = npath;
49 #endif
51 if (f_read_alloc_string(path, &buffer, 128 * 1024) < 0) {
52 free(buffer);
53 if (!header_sent) send_error(500, NULL, "Read error");
54 return 0;
57 if (!header_sent) send_header(200, NULL, mime_html, 0);
59 // <% id(arg, arg); %>
60 cp = buffer;
61 while (*cp) {
62 if ((b = strstr(cp, "%>")) == NULL) {
63 web_puts(cp);
64 break;
66 *b = 0;
68 //xx <% <% %>
69 //xx %>
71 a = cp;
72 while ((c = strstr(a, "<%")) != NULL) {
73 a = c + 2;
76 if (a == cp) {
77 *b = '%';
78 b += 2;
79 web_write(cp, b - cp);
80 cp = b;
81 continue;
84 web_write(cp, (a - cp) - 2);
86 cp = b + 2;
88 while (*a == ' ') ++a;
89 ident = a;
90 while (((*a >= 'a') && (*a <= 'z')) || ((*a >= 'A') && (*a <= 'Z')) || ((*a >= '0') && (*a <= '9')) || (*a == '_')) {
91 ++a;
93 if (ident == a) {
94 #ifdef DEBUG
95 syslog(LOG_WARNING, "Identifier not found in %s @%u", path, a - buffer);
96 #endif
97 continue;
99 b = a;
100 while (*a == ' ') ++a;
101 if (*a++ != '(') {
102 #ifdef DEBUG
103 syslog(LOG_WARNING, "Expecting ( in %s @%u", path, a - buffer);
104 #endif
105 continue;
107 *b = 0;
109 // <% foo(123, "arg"); %>
110 // a -----^ ^--- null
112 // printf("\n[[['%s'\n", ident);
114 argc = 0;
115 while (*a) {
116 while (*a == ' ') ++a;
117 if (*a == ')') {
118 FINAL:
119 ++a;
120 while ((*a == ' ') || (*a == ';')) ++a;
121 if (*a != 0) break;
123 for (api = aspapi; api->name; ++api) {
124 if (strcmp(api->name, ident) == 0) {
125 api->exec(argc, argv);
126 break;
130 a = NULL;
132 int z;
133 for (z = 0; z < argc; ++z) {
134 printf(" %d '%s'\n", z, argv[z]);
137 break;
140 if (argc >= 32) {
141 #ifdef DEBUG
142 syslog(LOG_WARNING, "Error while parsing arguments in %s @%u", path, a - buffer);
143 #endif
144 break;
147 if ((*a == '"') || (*a == '\'')) {
148 x = *a;
149 argv[argc++] = a + 1;
150 while ((*++a != x) && (*a != 0)) {
151 if (*a == '\\') {
152 if (*++a == 0) break;
153 *(a - 1) = *a;
156 if (*a == 0) break;
157 *a++ = 0;
159 else {
160 argv[argc++] = a;
161 while ((*a != ',') && (*a != ')') && (*a != ' ') && (*a != 0)) ++a;
163 while (*a == ' ') ++a;
164 if (*a == ')') {
165 *a = 0;
166 goto FINAL;
168 if (*a != ',') break;
169 *a++ = 0;
172 #ifdef DEBUG
173 if (a != NULL) syslog(LOG_WARNING, "Error while parsing arguments in %s @%u", path, a - buffer);
174 #endif
176 // printf("argc=%d]]]\n", argc);
180 free(buffer);
181 return 1;
184 void wo_asp(char *path)
186 parse_asp(path);