hack PHP_5_4 compatibility into v1
[m6w6/ext-http] / scripts / gen_curlinfo.php
1 #!/usr/bin/env php
2 <?php
3 // $Id$
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 'COOKIELIST' => 'HTTP_CURL_VERSION(7,14,1)',
36 'PRIMARY_IP' => 'HTTP_CURL_VERSION(7,19,0)',
37 'APPCONNECT_TIME' => 'HTTP_CURL_VERSION(7,19,0)',
38 'REDIRECT_URL' => 'HTTP_CURL_VERSION(7,18,2)',
39 'CONDITION_UNMET' => 'HTTP_CURL_VERSION(7,19,4)',
40 'PRIMARY_PORT' => 'HTTP_CURL_VERSION(7,21,0)',
41 'LOCAL_PORT' => 'HTTP_CURL_VERSION(7,21,0)',
42 'LOCAL_IP' => 'HTTP_CURL_VERSION(7,21,0)',
43 );
44 $exclude = array(
45 'PRIVATE', 'LASTSOCKET', 'FTP_ENTRY_PATH', 'CERTINFO',
46 'RTSP_SESSION_ID', 'RTSP_CLIENT_CSEQ', 'RTSP_SERVER_CSEQ', 'RTSP_CSEQ_RECV'
47 );
48
49 $translate = array(
50 'HTTP_CONNECTCODE' => "connect_code",
51 'COOKIELIST' => 'cookies',
52 );
53
54 $templates = array(
55 'STRING' =>
56 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &c)) {
57 add_assoc_string_ex(&array, "%s", sizeof("%2$s"), c ? c : "", 1);
58 }
59 ',
60 'DOUBLE' =>
61 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &d)) {
62 add_assoc_double_ex(&array, "%s", sizeof("%2$s"), d);
63 }
64 ',
65 'LONG' =>
66 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &l)) {
67 add_assoc_long_ex(&array, "%s", sizeof("%2$s"), l);
68 }
69 ',
70 'SLIST' =>
71 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &s)) {
72 MAKE_STD_ZVAL(subarray);
73 array_init(subarray);
74 for (p = s; p; p = p->next) {
75 if (p->data) {
76 add_next_index_string(subarray, p->data, 1);
77 }
78 }
79 add_assoc_zval_ex(&array, "%s", sizeof("%2$s"), subarray);
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("http_request_info.c",
97 preg_replace('/(\/\* BEGIN \*\/\n).*(\/\* END \*\/)/s', '$1'. ob_get_contents() .'$2',
98 file_get_contents("http_request_info.c")));
99
100 ?>