fix build
[m6w6/ext-http] / scripts / gen_curlinfo.php
1 #!/usr/bin/env php
2 <?php
3
4 error_reporting(0);
5
6 function failure() {
7 // this is why error_get_last() should return a stdClass object
8 $error = error_get_last();
9 fprintf(STDERR, "FAILURE: %s\n", $error["message"]);
10 exit(-1);
11 }
12
13 function file_re($file, $pattern, $all = true) {
14 static $path;
15
16 $path or $path = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1].'/include/curl/' : "/usr/local/include/curl/";
17
18 if ($content = file_get_contents($path . $file)) {
19 if ($all) {
20 if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
21 return $matches;
22 }
23 } else {
24 if (preg_match($pattern, $content, $matches)) {
25 return $matches;
26 }
27 }
28 trigger_error("no match in $file for $pattern");
29 }
30 failure();
31 }
32
33 $ifdefs = array(
34 'PRIMARY_IP' => 'PHP_HTTP_CURL_VERSION(7,19,0)',
35 'APPCONNECT_TIME' => 'PHP_HTTP_CURL_VERSION(7,19,0)',
36 'CONDITION_UNMET' => 'PHP_HTTP_CURL_VERSION(7,19,4)',
37 'PRIMARY_PORT' => 'PHP_HTTP_CURL_VERSION(7,21,0)',
38 'LOCAL_PORT' => 'PHP_HTTP_CURL_VERSION(7,21,0)',
39 'LOCAL_IP' => 'PHP_HTTP_CURL_VERSION(7,21,0)',
40 );
41 $exclude = array(
42 'PRIVATE', 'LASTSOCKET', 'FTP_ENTRY_PATH', 'CERTINFO', 'TLS_SESSION',
43 'RTSP_SESSION_ID', 'RTSP_CLIENT_CSEQ', 'RTSP_SERVER_CSEQ', 'RTSP_CSEQ_RECV',
44 'COOKIELIST'
45 );
46
47 $translate = array(
48 'HTTP_CONNECTCODE' => "connect_code",
49 'COOKIELIST' => 'cookies',
50 );
51
52 $templates = array(
53 'STRING' =>
54 ' if (CURLE_OK == curl_easy_getinfo(ch, %s, &c)) {
55 ZVAL_STRING(&tmp, STR_PTR(c));
56 zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
57 }
58 ',
59 'DOUBLE' =>
60 ' if (CURLE_OK == curl_easy_getinfo(ch, %s, &d)) {
61 ZVAL_DOUBLE(&tmp, d);
62 zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
63 }
64 ',
65 'LONG' =>
66 ' if (CURLE_OK == curl_easy_getinfo(ch, %s, &l)) {
67 ZVAL_LONG(&tmp, l);
68 zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
69 }
70 ',
71 'SLIST' =>
72 ' if (CURLE_OK == curl_easy_getinfo(ch, %s, &s)) {
73 array_init(&tmp);
74 for (p = s; p; p = p->next) {
75 if (p->data) {
76 add_next_index_string(&tmp, p->data);
77 }
78 }
79 zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
80 curl_slist_free_all(s);
81 }
82 ',
83 );
84
85 $infos = file_re('curl.h', '/^\s*(CURLINFO_(\w+))\s*=\s*CURLINFO_(STRING|LONG|DOUBLE|SLIST)\s*\+\s*\d+\s*,?\s*$/m');
86
87 ob_start();
88 foreach ($infos as $info) {
89 list(, $full, $short, $type) = $info;
90 if (in_array($short, $exclude)) continue;
91 if (isset($ifdefs[$short])) printf("#if %s\n", $ifdefs[$short]);
92 printf($templates[$type], $full, strtolower((isset($translate[$short])) ? $translate[$short] : $short));
93 if (isset($ifdefs[$short])) printf("#endif\n");
94 }
95
96 file_put_contents("php_http_client_curl.c",
97 preg_replace('/(\/\* BEGIN::CURLINFO \*\/\n).*(\n\s*\/\* END::CURLINFO \*\/)/s', '$1'. ob_get_contents() .'$2',
98 file_get_contents("php_http_client_curl.c")));
99
100 ?>