-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathphp_jsmin.c
175 lines (148 loc) · 4.61 KB
/
php_jsmin.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Michael Squires <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/info.h"
#include "jsmin.h"
#include "php_jsmin.h"
ZEND_DECLARE_MODULE_GLOBALS(jsmin)
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_jsmin, 0, 0, 1)
ZEND_ARG_INFO(0, javascript)
ZEND_ARG_INFO(1, return_code)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_jsmin_last_error, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_jsmin_last_error_msg, 0)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ jsmin_functions[] */
const zend_function_entry jsmin_functions[] = {
PHP_FE(jsmin, arginfo_jsmin)
PHP_FE(jsmin_last_error, arginfo_jsmin_last_error)
PHP_FE(jsmin_last_error_msg, arginfo_jsmin_last_error_msg)
PHP_FE_END
};
/* }}} */
/* {{{ MINIT */
static PHP_MINIT_FUNCTION(jsmin)
{
REGISTER_LONG_CONSTANT("JSMIN_ERROR_NONE", PHP_JSMIN_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSMIN_ERROR_UNTERMINATED_COMMENT", PHP_JSMIN_ERROR_UNTERMINATED_COMMENT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSMIN_ERROR_UNTERMINATED_STRING", PHP_JSMIN_ERROR_UNTERMINATED_STRING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSMIN_ERROR_UNTERMINATED_REGEX", PHP_JSMIN_ERROR_UNTERMINATED_REGEX, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(jsmin)
{
jsmin_globals->error_code = 0;
}
/* }}} */
/* {{{ jsmin_module_entry
*/
zend_module_entry jsmin_module_entry = {
STANDARD_MODULE_HEADER,
"jsmin",
jsmin_functions,
PHP_MINIT(jsmin),
NULL,
NULL,
NULL,
PHP_MINFO(jsmin),
PHP_JSMIN_VERSION,
PHP_MODULE_GLOBALS(jsmin),
PHP_GINIT(jsmin),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_JSMIN
ZEND_GET_MODULE(jsmin)
#endif
/* {{{ proto string jsmin(string javascript)
Minifies JavaScript */
PHP_FUNCTION(jsmin)
{
char *javascript;
size_t javascript_len;
jsmin_obj *jmo;
zval *ret_code = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z/", &javascript, &javascript_len, &ret_code) == FAILURE) {
RETURN_FALSE;
}
jmo = jsmin(javascript TSRMLS_CC);
if (ret_code) {
zval_dtor(ret_code);
ZVAL_LONG(ret_code, jmo->errorCode);
}
JSMIN_G(error_code) = jmo->errorCode;
if (jmo->errorCode) {
ZVAL_BOOL(return_value, 0);
} else {
ZVAL_STRINGL(return_value, jmo->buffer.c, jmo->buffer.len);
}
free_jsmin_obj(jmo TSRMLS_CC);
}
/* }}} */
/* {{{ proto int jsmin_last_error()
Returns error code of last call to jsmin() */
PHP_FUNCTION(jsmin_last_error)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_LONG(JSMIN_G(error_code));
}
/* }}} */
/* {{{ proto string jsmin_last_error_msg()
Returns error message of last call to jsmin() */
PHP_FUNCTION(jsmin_last_error_msg)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
switch (JSMIN_G(error_code)) {
case PHP_JSMIN_ERROR_UNTERMINATED_COMMENT:
RETURN_STRING("Unterminated comment");
case PHP_JSMIN_ERROR_UNTERMINATED_STRING:
RETURN_STRING("Unterminated string literal");
case PHP_JSMIN_ERROR_UNTERMINATED_REGEX:
RETURN_STRING("Unterminated set in Regular Expression literal");
default:
RETURN_STRING("No error");
}
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(jsmin)
{
php_info_print_table_start();
php_info_print_table_header(2, "jsmin support", "enabled");
php_info_print_table_row(2, "jsmin version", PHP_JSMIN_VERSION);
php_info_print_table_row(2, "jsmin notes", "Port of Douglas Crockford's JSMin");
php_info_print_table_end();
}
/* }}} */