xref: /imagick/util/fixup_arginfo.php (revision 4c0cccda)
1<?php
2
3declare(strict_types = 1);
4
5
6if ($argc !== 2) {
7    fwrite(STDERR, "usage php fixup_arginfo.php \$arginfo_filename\n");
8    exit(-1);
9}
10
11$filename = $argv[1];
12
13$fixup_note = "file has been fixedup for different versions";
14
15echo "Fixing $filename\n";
16
17$input_lines = file($filename);
18
19foreach ($input_lines as $input_line) {
20    if (strpos($input_line, $fixup_note) !== false) {
21        echo "File has already been fixedup.\n";
22        exit(0);
23    }
24}
25
26$output_lines = [];
27
28$search = [];
29$replace = [];
30
31$search[] = "#.*Stub hash: (.*) .*/#iu";
32$replace[] = "* Stub hash: regen with 'sh regen_arginfo.sh' \n* $fixup_note */";
33
34$search[] = "#ZEND_ARG_OBJ_INFO\(0, (\w*), IMAGICK_QUANTUM_TYPE, 0\)#iu";
35$replace[] = "
36#if MAGICKCORE_HDRI_ENABLE
37	ZEND_ARG_TYPE_INFO(0, $1, IS_DOUBLE, 0)
38#else
39	ZEND_ARG_TYPE_INFO(0, $1, IS_LONG, 0)
40#endif
41";
42
43
44// ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null)
45// ZEND_ARG_INFO(pass_by_ref, name)
46
47$search[] = "#ZEND_ARG_TYPE_INFO\((\w*), (\w*), (\w*), (\w*)\)#iu";
48$replace[] = "
49#if PHP_VERSION_ID >= 80000
50    ZEND_ARG_TYPE_INFO($1, $2, $3, $4)
51#else
52    ZEND_ARG_INFO($1, $2)
53#endif";
54
55$search[] = "#ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX\((\w*), 0, (\w*), IMAGICK_QUANTUM_TYPE, 0\)#iu";
56$replace[] = "
57#if MAGICKCORE_HDRI_ENABLE
58ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX($1, 0, $2, IS_DOUBLE, 0)
59#else
60ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX($1, 0, $2, IS_LONG, 0)
61#endif
62";
63
64
65//ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null)
66#define ZEND_BEGIN_ARG_INFO_EX(name, _unused, return_reference, required_num_args)
67$search[] = "#ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX\((\w*), (\w*), (\w*), (\w*), (\w*)\)#iu";
68$replace[] = "
69#if PHP_VERSION_ID >= 80000
70ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX($1, $2, $3, $4, $5)
71#else
72ZEND_BEGIN_ARG_INFO_EX($1, 0, $2, $3)
73#endif
74";
75
76//#define ZEND_ARG_TYPE_MASK(pass_by_ref, name, type_mask, default_value) \
77$search[] = "#.*ZEND_ARG_TYPE_MASK\(([\w|\|]*), ([\w|\|]*), ([\w|\|]*), ([\w\|\"]*)\)#iu";
78$replace[] = "
79#if PHP_VERSION_ID >= 80000
80    ZEND_ARG_TYPE_MASK($1, $2, $3, $4)
81#else
82    ZEND_ARG_INFO($1, $2)
83#endif
84";
85
86//ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, required_num_args, type)
87//ZEND_BEGIN_ARG_INFO_EX(name, _unused, return_reference, required_num_args)
88
89$search[] = "#.*ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX\(([\w|\|]*), ([\w|\|]*), ([\w|\|]*), ([\w|\|]*)\)#iu";
90$replace[] = "
91#if PHP_VERSION_ID >= 80000
92    ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX($1, $2, $3, $4)
93#else
94    ZEND_BEGIN_ARG_INFO_EX($1, 0, $2, $3)
95#endif
96";
97
98//ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null)
99$search[] = "#ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX\((\w*), (\w*), (\w*), (\w*), (\w*)\)#iu";
100$replace[] = "
101#if PHP_VERSION_ID >= 80000
102ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX($1, $2, $3, $4, $5)
103#else
104ZEND_BEGIN_ARG_INFO_EX($1, 0, $2, $3)
105#endif
106";
107
108//ZEND_ARG_OBJ_INFO(pass_by_ref, name, classname, allow_null) \
109$search[] = "#.*ZEND_ARG_OBJ_INFO\((\w*), (\w*), resource, (\w*)\)#iu";
110$replace[] = "
111#if PHP_VERSION_ID >= 80000
112\tZEND_ARG_OBJ_INFO($1, $2, resource, $3)
113#else
114\tZEND_ARG_INFO($1, $2)
115#endif
116";
117
118// ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filename, IS_STRING, 1, "null")
119$search[] = "#.*ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE\((\w*), (\w*), (\w*), (\w*), ([\w\"]*)\)#iu";
120$replace[] = "
121#if PHP_VERSION_ID >= 80000
122    ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE($1, $2, $3, $4, $5)
123#else
124    ZEND_ARG_INFO($1, $2)
125#endif
126";
127
128
129//#if PHP_VERSION_ID >= 80000
130//ZEND_ARG_TYPE_MASK(0, files, MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_NULL, NULL)
131//#else
132//    ZEND_ARG_INFO(0, files)
133//#endif
134
135
136
137foreach ($input_lines as $input_line) {
138    $input_line = rtrim($input_line);
139    $input_line = preg_replace($search, $replace, $input_line);
140
141    $output_lines[] = $input_line;
142}
143
144file_put_contents($filename, implode("\n", $output_lines));
145echo "File has now been fixedup.\n";
146