1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
static
int get_escape_option(struct tmplpro_state *state, PSTRING OptEscape)
{
/* int escape = HTML_TEMPLATE_OPT_ESCAPE_NO; */
int escape = state->param->default_escape;
if (OptEscape.begin<OptEscape.endnext) {
switch (*OptEscape.begin) {
case '1': case 'H': case 'h': /* HTML*/
escape = HTML_TEMPLATE_OPT_ESCAPE_HTML;
break;
case 'U': case 'u': /* URL */
escape = HTML_TEMPLATE_OPT_ESCAPE_URL;
break;
case 'J': case 'j': /* JS */
escape = HTML_TEMPLATE_OPT_ESCAPE_JS;
break;
case '0': case 'N': case 'n': /* 0 or NONE */
escape = HTML_TEMPLATE_OPT_ESCAPE_NO;
break;
default:
state->param->found_syntax_error=1;
log_state(state,TMPL_LOG_ERROR, " unsupported value of ESCAPE=%.*s\n",(int)(OptEscape.endnext-OptEscape.begin),OptEscape.begin);
}
}
return escape;
}
static
void init_tmpl_var_case_buffers (struct tmplpro_param *param) {
param->lowercase_varname.begin = NULL;
param->lowercase_varname.endnext = NULL;
param->uppercase_varname.begin = NULL;
param->uppercase_varname.endnext = NULL;
}
static
ABSTRACT_VALUE* get_abstract_value (struct tmplpro_param *param, int scope_level, PSTRING name) {
ABSTRACT_VALUE* retval = NULL;
ABSTRACT_MAP* param_HV = getScope(¶m->var_scope_stack, scope_level)->param_HV;
ABSTRACT_DATASTATE* data_state = param->ext_data_state;
get_ABSTRACT_VALUE_functype getval_func = param->GetAbstractValFuncPtr;
int tmpl_var_case = param->tmpl_var_case;
if ((tmpl_var_case & ASK_NAME_MASK) == ASK_NAME_DEFAULT
|| tmpl_var_case & ASK_NAME_AS_IS) {
retval = getval_func(data_state, param_HV, name);
if (retval != NULL) return retval;
}
if (tmpl_var_case & ASK_NAME_LOWERCASE) {
if (param->lowercase_varname.begin == NULL) {
param->lowercase_varname=lowercase_pstring(¶m->lowercase_varname_buffer, name);
}
retval = getval_func(data_state, param_HV, param->lowercase_varname);
if (retval != NULL) return retval;
}
if (tmpl_var_case & ASK_NAME_UPPERCASE) {
if (param->uppercase_varname.begin == NULL) {
param->uppercase_varname=uppercase_pstring(¶m->uppercase_varname_buffer, name);
}
retval = getval_func(data_state, param_HV, param->uppercase_varname);
if (retval != NULL) return retval;
}
return retval;
}
static
ABSTRACT_VALUE* walk_through_nested_loops (struct tmplpro_param *param, PSTRING name) {
int CurLevel;
ABSTRACT_VALUE* valptr;
init_tmpl_var_case_buffers (param);
/* Shigeki Morimoto path_like_variable_scope extension */
if (param->path_like_variable_scope) {
if(*(name.begin) == '/' || strncmp(name.begin, "../", 3) == 0){
PSTRING tmp_name;
int GoalHash;
if(*(name.begin) == '/'){
tmp_name.begin = name.begin+1; // skip '/'
tmp_name.endnext = name.endnext;
GoalHash = 0;
}else{
tmp_name.begin = name.begin;
tmp_name.endnext = name.endnext;
GoalHash = curScopeLevel(¶m->var_scope_stack);
while(strncmp(tmp_name.begin, "../", 3) == 0){
tmp_name.begin = tmp_name.begin + 3; // skip '../'
GoalHash --;
}
}
return get_abstract_value(param, GoalHash, tmp_name);
}
}
/* end Shigeki Morimoto path_like_variable_scope extension */
CurLevel = curScopeLevel(¶m->var_scope_stack);
valptr = get_abstract_value(param, CurLevel, name);
if (valptr) return valptr;
/* optional strict scoping; does it have much sence?
if ((STRICT_SCOPING==param->global_vars)) return NULL;
*/
/* loop-bounded scoping; */
if (0==param->global_vars) {
while (isScopeMap(getScope(¶m->var_scope_stack,CurLevel)) && --CurLevel>=0) {
valptr = get_abstract_value(param, CurLevel, name);
if (valptr!=NULL) return valptr;
}
return NULL;
}
while (--CurLevel>=0) {
valptr = get_abstract_value(param, CurLevel, name);
if (valptr!=NULL) return valptr;
}
return NULL;
}
TMPLPRO_LOCAL
PSTRING _get_variable_value (struct tmplpro_param *param, PSTRING name) {
PSTRING varvalue ={NULL, NULL};
ABSTRACT_VALUE* abstrval;
if (param->loop_context_vars) {
varvalue=get_loop_context_vars_value(param, name);
}
if (varvalue.begin==NULL) {
abstrval=walk_through_nested_loops(param, name);
if (abstrval!=NULL) varvalue=(param->AbstractVal2pstringFuncPtr)(param->ext_data_state, abstrval);
}
if (debuglevel>=TMPL_LOG_DEBUG2) {
if (name.begin!=NULL) {
tmpl_log(TMPL_LOG_DEBUG2,"_get_variable_value: name = %.*s ",(int)(name.endnext-name.begin),name.begin);
} else {
tmpl_log(TMPL_LOG_DEBUG2,"_get_variable_value: name = NULL ");
}
if (varvalue.begin!=NULL) {
tmpl_log(TMPL_LOG_DEBUG2,"value = %.*s\n",(int)(varvalue.endnext-varvalue.begin),varvalue.begin);
} else {
tmpl_log(TMPL_LOG_DEBUG2,"value = UNDEF\n");
}
}
return varvalue;
}
static
PSTRING get_variable_option (struct tmplpro_state *state, const PSTRING* const TagOptVal) {
PSTRING varvalue ={NULL, NULL};
PSTRING defvalue = TagOptVal[TAG_OPT_DEFAULT];
if (TagOptVal[TAG_OPT_EXPR].begin!=NULL) {
varvalue=parse_expr(TagOptVal[TAG_OPT_EXPR], state);
} else {
varvalue=_get_variable_value(state->param, TagOptVal[TAG_OPT_NAME]);
}
if (varvalue.begin==NULL) {
if (defvalue.begin!=defvalue.endnext) {
varvalue=defvalue;
}
}
return varvalue;
}
static
void
tag_handler_var (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
PSTRING varvalue;
int escapeopt;
/*
if (debuglevel>=TMPL_LOG_DEBUG2) {
log_state(state,TMPL_LOG_DEBUG2,"Entered tag_handler_var\n");
}*/
if (! state->is_visible) return;
varvalue = get_variable_option(state, TagOptVal);
escapeopt = get_escape_option(state,TagOptVal[TAG_OPT_ESCAPE]);
if (varvalue.begin==NULL) return;
if (escapeopt!=HTML_TEMPLATE_OPT_ESCAPE_NO) {
varvalue=escape_pstring(&state->param->escape_pstring_buffer, varvalue, escapeopt);
}
(state->param->WriterFuncPtr)(state->param->ext_writer_state,varvalue.begin,varvalue.endnext);
}
static
void
tag_handler_include (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tmplpro_param* param;
char* filename;
int x;
PSTRING varvalue;
PSTRING defvalue;
if (! state->is_visible) return;
param=state->param;
if (param->no_includes) {
log_state(state,TMPL_LOG_ERROR, "HTML::Template::Pro : Illegal attempt to use TMPL_INCLUDE in template file : (no_includes => 1)\n");
return;
}
if (param->max_includes && param->max_includes < param->cur_includes) {
log_state(state,TMPL_LOG_INFO, "HTML::Template::Pro : TMPL_INCLUDE: max_includes exceeded.\n");
return;
}
param->cur_includes++;
varvalue=TagOptVal[TAG_OPT_NAME];
defvalue = TagOptVal[TAG_OPT_DEFAULT];
if (TagOptVal[TAG_OPT_EXPR].begin!=NULL) {
varvalue=parse_expr(TagOptVal[TAG_OPT_EXPR], state);
};
if (varvalue.begin==varvalue.endnext && defvalue.begin!=defvalue.endnext) varvalue=defvalue;
/* pstrdup */
{
const long len = varvalue.endnext-varvalue.begin;
filename =(char*) malloc(len+1);
for (x=0;x<len;x++) {
*(filename+x)=*(varvalue.begin+x);
}
*(filename+len)=0;
}
/* end pstrdup */
tmplpro_exec_tmpl_filename (param,filename);
free (filename);
param->cur_includes--;
return;
}
static
int
is_var_true(struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
register int ifval=-1; /*not yet defined*/
if (TagOptVal[TAG_OPT_EXPR].begin!=NULL) {
/*
if (debuglevel>=TMPL_LOG_DEBUG2) {
tmpl_log(TMPL_LOG_DEBUG2,"is_var_true: expr = %.*s\n",(int)(TagOptVal[TAG_OPT_EXPR].endnext-TagOptVal[TAG_OPT_EXPR].begin),TagOptVal[TAG_OPT_EXPR].begin);
}*/
ifval=is_pstring_true(parse_expr(TagOptVal[TAG_OPT_EXPR], state));
} else
if (state->param->loop_context_vars) {
PSTRING loop_var=get_loop_context_vars_value(state->param, TagOptVal[TAG_OPT_NAME]);
if (loop_var.begin!=NULL) {
ifval=is_pstring_true(loop_var);
}
}
if (ifval==-1) {
is_ABSTRACT_VALUE_true_functype userSuppliedIsTrueFunc;
ABSTRACT_VALUE* abstrval=walk_through_nested_loops(state->param, TagOptVal[TAG_OPT_NAME]);
if (abstrval==NULL) return 0;
userSuppliedIsTrueFunc = state->param->IsAbstractValTrueFuncPtr;
if (userSuppliedIsTrueFunc!=NULL) {
ifval=(userSuppliedIsTrueFunc)(state->param->ext_data_state, abstrval);
} else {
ifval=is_pstring_true((state->param->AbstractVal2pstringFuncPtr)(state->param->ext_data_state, abstrval));
}
}
return ifval;
}
static
void
tag_handler_if (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry iftag;
iftag.tag=HTML_TEMPLATE_TAG_IF;
iftag.vcontext=state->is_visible;
iftag.position=state->cur_pos; /* unused */
/* state->is_visible && means that we do not evaluate variable in shadow */
if (state->is_visible && is_var_true(state,TagOptVal)) {
iftag.value=1;
/* state->is_visible is not touched */
} else {
iftag.value=0;
state->is_visible=0;
}
tagstack_push(&(state->tag_stack), iftag);
if (debuglevel>=TMPL_LOG_DEBUG2) log_state(state,TMPL_LOG_DEBUG2,"tag_handler_if:visible context =%d value=%d ",iftag.vcontext,iftag.value);
}
static
void
tag_handler_unless (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry iftag;
iftag.tag=HTML_TEMPLATE_TAG_UNLESS;
iftag.vcontext=state->is_visible;
iftag.position=state->cur_pos; /* unused */
/* state->is_visible && means that we do not evaluate variable in shadow */
if (state->is_visible && !is_var_true(state,TagOptVal)) {
iftag.value=1;
/* state->is_visible is not touched */
} else {
iftag.value=0;
state->is_visible=0;
}
tagstack_push(&(state->tag_stack), iftag);
if (debuglevel>=TMPL_LOG_DEBUG2) log_state(state,TMPL_LOG_DEBUG2,"tag_handler_unless:visible context =%d value=%d ",iftag.vcontext,iftag.value);
}
static
INLINE
int
test_stack (int tag)
{
// return (tagstack_notempty(&(state->tag_stack)) && (tagstack_top(&(state->tag_stack))->tag==tag));
return 1;
}
static
void
tag_stack_debug (struct tmplpro_state *state, int stack_tag_type)
{
if (stack_tag_type) {
if (tagstack_notempty(&(state->tag_stack))) {
struct tagstack_entry* iftag=tagstack_top(&(state->tag_stack));
if (iftag->tag!=stack_tag_type) {
log_state(state,TMPL_LOG_ERROR, "ERROR: tag mismatch with %s\n",TAGNAME[iftag->tag]);
}
} else {
log_state(state,TMPL_LOG_ERROR, "ERROR: opening tag %s not found\n",TAGNAME[stack_tag_type]);
}
}
}
static
struct tagstack_entry tagstack_smart_pop(struct tmplpro_state *state)
{
int is_underflow=0;
struct tagstack_entry curtag=tagstack_pop(&(state->tag_stack), &is_underflow);
if (is_underflow) {
log_state(state,TMPL_LOG_ERROR,"stack underflow:tag stack is empty. Cased by closing tag w/o matching opening tag.\n");
}
return curtag;
}
static
void
tag_handler_closeif (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry iftag;
if (! test_stack(HTML_TEMPLATE_TAG_IF)) {
tag_stack_debug(state,HTML_TEMPLATE_TAG_IF);
return;
}
iftag=tagstack_smart_pop(state);
if (0==state->is_visible) state->last_processed_pos=state->cur_pos;
state->is_visible=iftag.vcontext;
}
static
void
tag_handler_closeunless (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry iftag;
if (! test_stack(HTML_TEMPLATE_TAG_UNLESS)) {
tag_stack_debug(state,HTML_TEMPLATE_TAG_UNLESS);
return;
}
iftag=tagstack_smart_pop(state);
if (0==state->is_visible) state->last_processed_pos=state->cur_pos;
state->is_visible=iftag.vcontext;
}
static
void
tag_handler_else (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry* iftag;
if (! test_stack(HTML_TEMPLATE_TAG_IF) &&
! test_stack(HTML_TEMPLATE_TAG_UNLESS)) {
tag_stack_debug(state,HTML_TEMPLATE_TAG_ELSE);
return;
}
iftag=tagstack_top(&(state->tag_stack));
if (0==state->is_visible) state->last_processed_pos=state->cur_pos;
if (iftag->value) {
state->is_visible=0;
} else if (1==iftag->vcontext) {
state->is_visible=1;
}
if (debuglevel>=TMPL_LOG_DEBUG2) log_state(state,TMPL_LOG_DEBUG2,"else:(pos " MOD_TD ") visible:context =%d, set to %d ",
TO_PTRDIFF_T(iftag->position - state->top),iftag->vcontext,state->is_visible);
}
static
void
tag_handler_elsif (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry *iftag;
if (! test_stack(HTML_TEMPLATE_TAG_IF) &&
! test_stack(HTML_TEMPLATE_TAG_UNLESS)) {
tag_stack_debug(state,HTML_TEMPLATE_TAG_ELSIF);
return;
}
iftag=tagstack_top(&(state->tag_stack));
if (0==state->is_visible) state->last_processed_pos=state->cur_pos;
if (iftag->value) {
state->is_visible=0;
} else if (1==iftag->vcontext) {
/* test only if vcontext==true; if the whole tag if..endif itself is invisible, skip the is_var_true test */
/*TODO: it is reasonable to skip is_var_true test in if/unless too */
if (is_var_true(state,TagOptVal)) {
iftag->value=1;
state->is_visible=1;
} else {
iftag->value=0;
state->is_visible=0;
}
}
if (debuglevel>=TMPL_LOG_DEBUG2) log_state(state,TMPL_LOG_DEBUG2,"elsif:(pos " MOD_TD ") visible:context =%d, set to %d ",
TO_PTRDIFF_T(iftag->position - state->top), iftag->vcontext, state->is_visible);
}
static
int
next_loop (struct tmplpro_state* state) {
#ifdef DEBUG
log_state(state,TMPL_LOG_DEBUG2,"next_loop:before NextLoopFuncPtr\n");
#endif
struct ProScopeEntry* currentScope = getCurrentScope(&state->param->var_scope_stack);
if (!isScopeLoop(currentScope)) {
log_state(state,TMPL_LOG_ERROR, "next_loop:at scope level %d: internal error - loop is null\n", curScopeLevel(&state->param->var_scope_stack));
return 0;
}
if (++currentScope->loop < currentScope->loop_count || currentScope->loop_count< 0) {
ABSTRACT_MAP* arrayvalptr=(state->param->GetAbstractMapFuncPtr)(state->param->ext_data_state, currentScope->loops_AV,currentScope->loop);
if ((arrayvalptr!=NULL)) {
currentScope->param_HV=arrayvalptr;
return 1;
} else {
/* either undefined loop ended normally or defined loop ended ubnormally */
if (currentScope->loop_count>0) log_state(state,TMPL_LOG_ERROR, "PARAM:LOOP:next_loop(%d): callback returned null scope\n", currentScope->loop);
}
}
if (state->param->ExitLoopScopeFuncPtr) state->param->ExitLoopScopeFuncPtr(state->param->ext_data_state, currentScope->loops_AV);
popScope(&state->param->var_scope_stack);
return 0;
}
static
int init_loop (struct tmplpro_state *state, const PSTRING* const TagOptVal) {
int loop_count;
ABSTRACT_ARRAY* loopptr=(ABSTRACT_ARRAY*) walk_through_nested_loops(state->param,TagOptVal[TAG_OPT_NAME]);
if (loopptr==NULL) {
return 0;
} else {
/* set loop array */
loopptr = (*state->param->AbstractVal2abstractArrayFuncPtr)(state->param->ext_data_state, loopptr);
if (loopptr == NULL)
{
log_state(state,TMPL_LOG_ERROR, "PARAM:LOOP:loop argument:loop was expected but not found.\n");
return 0;
}
loop_count = (*state->param->GetAbstractArrayLengthFuncPtr)(state->param->ext_data_state, loopptr);
/* 0 is an empty array; <0 is an undefined array (iterated until next_loop==NULL */
if (0==loop_count) return 0;
pushScopeLoop(&state->param->var_scope_stack, loop_count, loopptr);
return 1;
}
}
static
void
tag_handler_loop (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry iftag;
iftag.tag=HTML_TEMPLATE_TAG_LOOP;
iftag.vcontext=state->is_visible;
iftag.value=0;
iftag.position=state->cur_pos; /* loop start - to restore in </tmpl_loop> */
#ifdef DEBUG
log_state(state,TMPL_LOG_DEBUG2,"tag_handler_loop:before InitLoopFuncPtr\n");
#endif
if (state->is_visible && init_loop(state,TagOptVal) && next_loop(state)) {
iftag.value=1; /* the loop is non - empty */
} else {
/* empty loop is equal to <if false> ... </if> */
state->is_visible=0;
}
#ifdef DEBUG
log_state(state,TMPL_LOG_DEBUG2,"tag_handler_loop:after InitLoopFuncPtr\n");
#endif
tagstack_push(&(state->tag_stack), iftag);
}
static
void
tag_handler_closeloop (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
struct tagstack_entry* iftag_ptr;
if (! test_stack(HTML_TEMPLATE_TAG_LOOP)) {
tag_stack_debug(state,HTML_TEMPLATE_TAG_LOOP);
return;
}
iftag_ptr=tagstack_top(&(state->tag_stack));
if (iftag_ptr->value==1 && next_loop(state)) {
/* continue loop */
state->cur_pos=iftag_ptr->position;
state->last_processed_pos=iftag_ptr->position;
return;
} else {
/* finish loop */
struct tagstack_entry iftag;
iftag=tagstack_smart_pop(state);
state->is_visible=iftag.vcontext;
state->last_processed_pos=state->cur_pos;
}
}
static
void
tag_handler_unknown (struct tmplpro_state *state, const PSTRING* const TagOptVal)
{
log_state(state,TMPL_LOG_ERROR,"tag_handler_unknown: unknown tag\n");
}
/*
* Local Variables:
* mode: c
* End:
*/
|