f7c6cdb53d55ad0dfc5f3b124256495c1cb57fbc
[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' => '7,14,1'
36 );
37 $exclude = array(
38 'PRIVATE', 'LASTSOCKET', 'FTP_ENTRY_PATH'
39 );
40 $translate = array(
41 'HTTP_CONNECTCODE' => "connect_code",
42 'COOKIELIST' => 'cookies',
43 );
44
45 $templates = array(
46 'STRING' =>
47 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &c)) {
48 add_assoc_string_ex(&array, "%s", sizeof("%2$s"), c ? c : "", 1);
49 }
50 ',
51 'DOUBLE' =>
52 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &d)) {
53 add_assoc_double_ex(&array, "%s", sizeof("%2$s"), d);
54 }
55 ',
56 'LONG' =>
57 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &l)) {
58 add_assoc_long_ex(&array, "%s", sizeof("%2$s"), l);
59 }
60 ',
61 'SLIST' =>
62 ' if (CURLE_OK == curl_easy_getinfo(request->ch, %s, &s)) {
63 MAKE_STD_ZVAL(subarray);
64 array_init(subarray);
65 for (p = s; p; p = p->next) {
66 add_next_index_string(subarray, p->data, 1);
67 }
68 add_assoc_zval_ex(&array, "%s", sizeof("%2$s"), subarray);
69 curl_slist_free_all(s);
70 }
71 '
72 );
73
74 $infos = file_re('curl.h', '/^\s*(CURLINFO_(\w+))\s*=\s*CURLINFO_(STRING|LONG|DOUBLE|SLIST)\s*\+\s*\d+\s*,?\s*$/m');
75
76 ob_start();
77 foreach ($infos as $info) {
78 list(, $full, $short, $type) = $info;
79 if (in_array($short, $exclude)) continue;
80 if (isset($ifdefs[$short])) printf("#if HTTP_CURL_VERSION(%s)\n", $ifdefs[$short]);
81 printf($templates[$type], $full, strtolower((isset($translate[$short])) ? $translate[$short] : $short));
82 if (isset($ifdefs[$short])) printf("#endif\n");
83 }
84
85 file_put_contents("http_request_info.c",
86 preg_replace('/(\/\* BEGIN \*\/\n).*(\/\* END \*\/)/s', '$1'. ob_get_contents() .'$2',
87 file_get_contents("http_request_info.c")));
88
89 ?>