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