Exemple #1
0
ZEND_API void zend_strip(void)
{
	zval token;
	int token_type;
	int prev_space = 0;

	ZVAL_UNDEF(&token);
	while ((token_type=lex_scan(&token))) {
		switch (token_type) {
			case T_WHITESPACE:
				if (!prev_space) {
					zend_write(" ", sizeof(" ") - 1);
					prev_space = 1;
				}
						/* lack of break; is intentional */
			case T_COMMENT:
			case T_DOC_COMMENT:
				ZVAL_UNDEF(&token);
				continue;

			case T_END_HEREDOC:
				zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
				/* read the following character, either newline or ; */
				if (lex_scan(&token) != T_WHITESPACE) {
					zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
				}
				zend_write("\n", sizeof("\n") - 1);
				prev_space = 1;
				ZVAL_UNDEF(&token);
				continue;

			default:
				zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
				break;
		}

		if (Z_TYPE(token) == IS_STRING) {
			switch (token_type) {
				case T_OPEN_TAG:
				case T_OPEN_TAG_WITH_ECHO:
				case T_CLOSE_TAG:
				case T_WHITESPACE:
				case T_COMMENT:
				case T_DOC_COMMENT:
					break;

				default:
					zend_string_release(Z_STR(token));
					break;
			}
		}
		prev_space = 0;
		ZVAL_UNDEF(&token);
	}

	/* Discard parse errors thrown during tokenization */
	zend_clear_exception();
}
Exemple #2
0
/**
 *  Called when the internal buffer should be synchronized
 *  @return int
 */
int StreamBuf::sync()
{
    // current buffer size
    size_t size = pptr() - pbase();
    
    // is this the error stream or the regular output stream?
    if (_error)
    {
        // write to error (the zend_error() method is a varargs function, 
        // which means that we have to include a printf() like format as first
        // parameter. We can not specify pbase() directly, because (1) it is
        // not null terminated and (2) it could contain % signs and allow all
        // sorts of buffer overflows.
        zend_error(_error, "%.*s", (int)size, pbase());
        
    }
    else
    {
        // write to zend
        zend_write(pbase(), size);
    }
    
    // reset the buffer
    pbump(-size);
    
    // done
    return 0;
}
Exemple #3
0
static void handle_whitespace(int *emit_whitespace)
{
	unsigned char c;
	int i;

	for (c=0; c<128; c++) {
		if (emit_whitespace[c]>0) {
			for (i=0; i<emit_whitespace[c]; i++) {
				zend_write((char *) &c, 1);
			}
		}
	}
	memset(emit_whitespace, 0, sizeof(int)*256);
}
Exemple #4
0
ZEND_API void zend_indent()
{
	zval token;
	int token_type;
	int in_string=0;
	int nest_level=0;
	int emit_whitespace[256];
	int i;
	TSRMLS_FETCH();

	memset(emit_whitespace, 0, sizeof(int)*256);

	/* highlight stuff coming back from zendlex() */
	token.type = 0;
	while ((token_type=lex_scan(&token TSRMLS_CC))) {
		switch (token_type) {
			case T_INLINE_HTML:
				zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
				break;
			case T_WHITESPACE: {
					token.type = 0;
					/* eat whitespace, emit newlines */
					for (i=0; i<LANG_SCNG(yy_leng); i++) {
						emit_whitespace[(unsigned char) LANG_SCNG(yy_text)[i]]++;
					}
					continue;
				}
				break;
			case '"':
				in_string = !in_string;
				/* break missing intentionally */
			default:
				if (token.type==0) {
					/* keyword */
					switch (token_type) {
						case ',':
							ZEND_PUTS(", ");
							goto dflt_printout;
							break;
						case '{':
							nest_level++;
							if (emit_whitespace['\n']>0) {
								ZEND_PUTS(" {\n");
								memset(emit_whitespace, 0, sizeof(int)*256);
							} else {
								ZEND_PUTS("{");
							}
							break;
						case '}':
							nest_level--;
							if (emit_whitespace['\n']==0) {
								ZEND_PUTS("\n");
							}
							for (i=0; i<nest_level; i++) {
								ZEND_PUTS("    ");
							}
							goto dflt_printout;
							break;
dflt_printout:
						default:
							if (emit_whitespace['\n']>0) {
								for (i=0; i<emit_whitespace['\n']; i++) {
									ZEND_PUTS("\n");
								}
								memset(emit_whitespace, 0, sizeof(int)*256);
								for (i=0; i<nest_level; i++) {
									ZEND_PUTS("    ");
								}
							} else {
								handle_whitespace(emit_whitespace);
							}
							zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
							break;
					}
				} else {
					handle_whitespace(emit_whitespace);
					if (in_string) {
						zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
						/* a part of a string */
					} else {
						zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
					}
				}
				break;
		}
		if (token.type == IS_STRING) {
			switch (token_type) {
			case T_OPEN_TAG:
			case T_CLOSE_TAG:
			case T_WHITESPACE:
				break;
			default:
				efree(token.value.str.val);
				break;
			}
		}
		token.type = 0;
	}
}