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
|
<?php
$overwrite = false;
$zend_include_dir = "../../php-src/Zend";
$zend_include_files = array(
"zend.h",
"zend_API.h",
"zend_builtin_functions.h",
"zend_compile.h",
"zend_constants.h",
"zend_exceptions.h",
"zend_execute.h",
"zend_hash.h",
"zend_highlight.h",
"zend_interfaces.h",
"zend_ini.h",
"zend_list.h",
"zend_modules.h",
"zend_objects.h",
"zend_object_handlers.h",
"zend_objects_API.h",
"zend_qsort.h",
"zend_stream.h",
"zend_strtod.h",
"zend_unicode.h",
"zend_variables.h",
"../TSRM/TSRM.h",
"../TSRM/tsrm_virtual_cwd.h",
);
$api_dir = array("ZEND"=>"../en/internals/zendapi",
"TSRM"=>"../en/internals/tsrm",
"CWD" =>"../en/internals/tsrm",);
$functions = array();
$wrappers = array();
foreach ($zend_include_files as $infile) {
echo "processing $zend_include_dir/$infile\n";
$in = fopen("$zend_include_dir/$infile", "r");
if (!$in) {
die("can't open $zend_include_dir/$infile");
}
// loop over all lines in the file
while (!feof($in)) {
// TODO a prototype may span more than one line?
$line = trim(fgets($in));
if (substr($line, -1) == "\\") { // multiline macro, read one more line
$line = substr($line, 0, -1) . trim(fgets($in));
}
// first we look for prototypes marked with ZEND_API
if (preg_match('/^\s*(ZEND|TSRM|CWD)_API\s+(\S+)\s+(\S+)\((.*)\)/U', $line, $matches)) {
// parse prototypes, step #1
// extract return type and function name
$api_type = $matches[1];
$return_type = $matches[2];
$function = $matches[3];
$param_list = $matches[4];
// the pointer '*' is usually next to the function name, not the type
// TODO what if there is whitespace on both sides of the '*'?
while ($function[0] == '*') {
$return_type.= "*";
$function = substr($function, 1);
}
// the parameters are spearated by commas
// TODO find a better way to handle TSRMLS_D and TSRMLS_DC
// TODO handle ...
$params = array();
foreach (explode(",", $param_list) as $param) {
$new_param = array();
$tokens = preg_split("/\s+/", trim($param));
$name = array_pop($tokens);
if (preg_match("|_DC$|", $name)) {
$magic = $name;
$name = array_pop($tokens);
} else {
$magic = "";
}
$type = implode(" ", $tokens);
if (empty($name)) {
$new_param['type'] = "magic";
$new_param['name'] = $type;
} else {
while ($name[0] == '*') {
$type.= "*";
$name = substr($name, 1);
}
$new_param['type'] = $type;
$new_param['name'] = $name;
}
$params[$name] = $new_param;
if ($magic) {
$params[$magic] = array("type"=>"magic", "name"=>$magic);
}
}
$functions[$function] = array("name" => $function,
"return_type" => $return_type,
"params" => $params,
"api_type" => $api_type,
"infile" => $infile
);
}
// next we look for macros that seem to be just wrappers around existing functions
// TODO catch multiline definitions
if (preg_match('|^#define\s+(\w+)\((.*)\)\s+(\w+)\(|U', $line, $matches)) {
$wrapper = $matches[1];
$param_list = $matches[2];
$function = $matches[3];
$wrappers[$wrapper] = array("function" => $function,
"wrapper" => $wrapper,
"param_list" => $param_list,
"infile" => $infile
);
}
}
}
do {
$additions = 0;
foreach ($wrappers as $name => $wrapper) {
if (isset($functions[$wrapper["function"]])) {
$function = $functions[$wrapper["function"]];
$params = array();
foreach (explode(",", $wrapper["param_list"]) as $param) {
$param = preg_replace('|\s*_*(\w+)\s*|', '${1}', $param); // trim and strip leading _s
if (isset($function["params"][$param])) {
$param_type = $function["params"][$param]["type"];
} else {
$param_type = "...";
}
$params[$param] = array("type"=>$param_type, "name"=>$param);
}
$functions[$name] = array("name" => $name,
"return_type" => $function["return_type"],
"params" => $params,
"api_type" => $function["api_type"],
"infile" => $wrapper["infile"]
);
unset($wrappers[$name]);
$additions++;
}
}
} while ($additions > 0);
foreach ($functions as $name => $function) {
create_page($name, $function["return_type"], $function["params"], $function["api_type"], $function["infile"]);
}
function create_page($function, $return_type, $params, $api_type, $infile)
{
global $overwrite, $api_dir;
// now generate the doc filename for this function
$functype = (strtolower($function) == $function) ? "function" : "macro";
$filename = $api_dir[$api_type]."/".$functype."s/".$function.".xml";
// only proceed it fhe file doesn't exist yet (no overwrites)
// and do not expose functions staring with '_'
if (($function[0] != '_') && ($overwrite || !file_exists($filename))) {
// now write the template file to phpdoc/en/internals/zendapi/functions
echo "writing $filename\n";
ob_start();
echo '<?xml version="1.0" encoding="iso-8859-1"?>'."\n";
// take revision from existing file
if (!$overwrite || !file_exists($filename)) {
echo "<!-- $"."Revision: 1.1 $ -->\n";
} else {
foreach (file($filename) as $line) {
if (strstr($line, 'Revision: ')) {
echo $line;
break;
}
}
}
?>
<refentry id="zend-api.<?php echo str_replace("_","-",$function); ?>">
<refnamediv>
<refname><?php echo $function; ?></refname>
<refpurpose>...</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<literallayout>#include <<?php echo basename($infile); ?>></literallayout>
<methodsynopsis>
<?php
if ($return_type == "void") {
echo " <void/>";
} else {
echo " <type>$return_type</type>";
}
echo "<methodname>$function</methodname>";
if (count($params)) {
echo "\n";
foreach($params as $param) {
echo " <methodparam><type>$param[type]</type><parameter>$param[name]</parameter></methodparam>\n";
}
} else {
echo "<void/>\n";
}
?>
</methodsynopsis>
<para>
...
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<?php
foreach($params as $param) {
?>
<varlistentry>
<term><parameter><?php echo $param["name"]; ?></parameter></term>
<listitem>
<para>
...
</para>
</listitem>
</varlistentry>
<?php
}
?>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
...
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
<?php
file_put_contents($filename, ob_get_clean());
}
}
?>
|