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