forked from libgit2/php-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.php
224 lines (188 loc) · 6.33 KB
/
gen.php
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
<?php
// slipshod template generator.
//
// flag:
// 0 => generate header
// 1 => generate source
//
// filter:
// - prefix: don't show matched keywords
// e.g ) -cred
// will ignore codes which have `cred` keyword
// default: show matched keywords only
// e.g ) cred
// will show codes which have `cred` keyword
//
// php gen.php libgit2/include/git2/<target>.h (0|1) [filter] > target.h or target.c
$data = file_get_contents($_SERVER['argv'][1]);
$table = array();
$buffer = "";
if (preg_match_all("/GIT_EXTERN\((.+?)\)\s*([a-zA-Z0-9_-]+)\((.+?)\);/s", $data, $match)) {
for ($i = 0; $i < count($match[0]); $i++) {
$tmp = array(
"args" => array(),
);
$list = array_map("trim", explode(",", $match[3][$i]));
$tmp['name'] = $match[2][$i];
$tmp['retval'] = $match[1][$i];
$d = count($list);
if ((preg_match("/(\*\*|out)/", $list[0]) || preg_match("/(write|create|new)/", $match[2][$i]))) {
$d--;
}
if (isset($_SERVER['argv'][3])) {
if ($_SERVER['argv'][3][0] == "-") {
$_SERVER['argv'][3] = substr($_SERVER['argv'][3][0], 1);
$flag = true;
} else {
$flag = false;
}
if (preg_match("/{$_SERVER['argv'][3]}/", $match[2][$i]) == $flag) {
continue;
}
}
$match[3][$i] = trim(preg_replace("/\r?\n/", "", $match[3][$i]));
$match[3][$i] = trim(preg_replace("/\t/", " ", $match[3][$i]));
if ($_SERVER['argv'][2] == "0") {
$buffer .= "ZEND_BEGIN_ARG_INFO_EX(arginfo_{$match[2][$i]}, 0, 0, $d)\n";
}
$o = 0;
foreach ($list as $l) {
$b = array_map(function($l){return trim($l, " *");}, explode(" ", $l));
$type = substr($l, 0, strrpos($l, " "));
$n = array_pop($b);
$b = array_pop($b);
if ($o == 0 && (preg_match("/(\*\*|out)/", $l) || preg_match("/(write|create|new)/", $match[2][$i]))) {
$w = 1;
$tmp['retval'] = "resource";
}else {
$w = 0;
}
if (!$w) {
$tmp['args'][] = array(
"write" => $w,
"name" => $n,
"type" => $type,
);
if ($_SERVER['argv'][2] == "0") {
$buffer .= "\tZEND_ARG_INFO(0, $n)\n";
}
}
$o++;
}
if ($_SERVER['argv'][2] == "0") {
$buffer .= "ZEND_END_ARG_INFO()\n";
$buffer .= "\n";
}
$table[] = $tmp;
}
}
if ($_SERVER['argv'][2] == "1") {
$buffer .= '#include "php_git2.h"' . "\n";
$buffer .= '#include "php_git2_priv.h"' . "\n";
$buffer .= '#include ".h"' . "\n";
}
foreach ($table as $func) {
$ret = getReturnType($func['retval']);
$t = array();
foreach ($func['args'] as $a) {
$t[] = $a['name'];
}
$sig = join(", ", $t);
$buffer .= "/* {{{ proto $ret {$func['name']}($sig)\n";
$buffer .= "*/\n";
if ($_SERVER['argv'][2] == "0") {
$buffer .= "PHP_FUNCTION({$func['name']});\n";
$buffer .= "\n";
} else {
$buffer .= "PHP_FUNCTION({$func['name']})\n";
$buffer .= "{\n";
$buffer .= getDeclarations($func);
$buffer .= "\t/* TODO(chobie): implement this */\n";
$buffer .= "\tphp_error_docref(NULL TSRMLS_CC, E_WARNING, " . '"' . "{$func['name']} not implemented yet" . '"' . ");\n";
$buffer .= "\treturn;\n\n";
$buffer .= "\tif (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,\n";
$buffer .= "\t\t" . sprintf('"%s", %s) == FAILURE) {%s', getParseStr($func), getParseStr2($func), "\n");
$buffer .= "\t\treturn;\n";
$buffer .= "\t}\n";
if (hasResource($func)) {
$t = $func['args'][0];
$buffer .= "\tZEND_FETCH_RESOURCE(_{$t['name']}, php_git2_t*, &{$t['name']}, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);\n";
}
$buffer .= "}\n";
$buffer .= "\n";
}
}
echo $buffer;
function hasResource($func)
{
$result = false;
foreach ($func['args'] as $arg) {
if (preg_match("/char/", $arg['type'])) {
} else if (preg_match("/git_oid/", $arg['type'])) {
} else if (preg_match("/git_/", $arg['type'])) {
$result = true;
}
}
return $result;
}
function getDeclarations($func)
{
$result = array();
foreach ($func['args'] as $arg) {
if (preg_match("/char/", $arg['type'])) {
$result[] = "\tchar *" . $arg['name'] . " = {0};";
$result[] = "\tint " . $arg['name'] . "_len;";
} else if (preg_match("/int/", $arg['type'])) {
$result[] = "\tlong " . $arg['name'] . ";";
} else if (preg_match("/git_oid/", $arg['type'])) {
$result[] = "\tchar *" . $arg['name'] . " = {0};";
$result[] = "\tint " . $arg['name'] . "_len;";
} else if (preg_match("/git_/", $arg['type'])) {
$result[] = "\tzval *" . $arg['name'] . ";";
$result[] = "\tphp_git2_t *_" . $arg['name'] . ";";
}
}
$result[] = "";
$result[] = "";
return join("\n", $result);
}
function getParseStr($func)
{
$result = array();
foreach ($func['args'] as $arg) {
if (preg_match("/char/", $arg['type'])) {
$result[] = "s";
} else if (preg_match("/git_oid/", $arg['type'])) {
$result[] = "s";
} else if (preg_match("/int/", $arg['type'])) {
$result[] = "l";
} else if (preg_match("/git_/", $arg['type'])) {
$result[] = "r";
}
}
return join("", $result);
}
function getParseStr2($func)
{
$result = array();
foreach ($func['args'] as $arg) {
$result[] = "&" . $arg['name'];
if (preg_match("/char/", $arg['type'])) {
$result[] = "&" . $arg['name'] . "_len";
} else if (preg_match("/git_oid/", $arg['type'])) {
$result[] = "&" . $arg['name'] . "_len";
}
}
return join(", ", $result);
}
function getReturnType($name)
{
switch($name) {
case "int":
return "long";
case "void":
return "void";
default:
return "resource";
}
}