summaryrefslogtreecommitdiff
path: root/ext/skeleton/create_stubs
blob: ad9bc81ab0765ef798012acaf620c0e2f2946e65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/awk -f

function gobble(s, x)
{
	sub(/^ /, "", line)
	match(line, "^" "(" s ")")
	x = substr(line, 1, RLENGTH)
	line = substr(line, RLENGTH+1)
	return x 
}

function convert(i, j, t)
{
	type = argtypes[i,j]
	name = argnames[i,j]
	opt  = optionals[i,j]
	tabs = x = ""

	for (i = 0; i < t; i++) { tabs = tabs "\t" }

	if (type == "int" || type == "long") {
		longs = longs "\tlong " name ";\n"
	} else if (type == "bool") {
		bools = bools "\tzend_bool " name ";\n"
	} else if (type == "double" || type == "float") {
		doubles = doubles "\tdouble " name ";\n"
	} else if (type == "string") {
		strings = strings "\tchar *" name " = NULL;\n"
		ints = ints "\tint " name "_len;\n"
	} else if (type == "array" || type == "object" || type == "mixed") {
		zvals = zvals "\tzval *" name " = NULL;\n"
	} else if (type == "resource") {
		zvals = zvals "\tzval *" name " = NULL;\n"
		resources = resources "\tif (" name ") {\n" \
			"\t\tZEND_FETCH_RESOURCE(???, ???, " name ", " name "_id, \"???\", ???_rsrc_id);\n\t}\n"
		ints = ints "\tint " name "_id = -1;\n"
	}
}

function comment(s)
{
	if (i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
		return
	} else {
		return s
	}
}

BEGIN {
	name = "[_A-Za-z][_A-Za-z0-9]*"
	type = "int|long|double|float|string|bool|array|object|resource|mixed|void"
	spec = "l|l|d|d|s|b|a|o|r|z|"
	num_funcs = 0

# create a map from type name to the spec
	split(type, type_array, "\|")
	split(spec, spec_array, "\|")
	for (i in type_array) {
		spec_map[type_array[i]] = spec_array[i]
	}

	if (xml && xml != "yes") {
		xmldoc = xml
	} else {
		xmldoc = extname "/" extname ".xml"
	}
			

	xmlhead = " <reference id=\"ref." extname "\">\n" \
		"  <title> functions</title>\n" \
		"  <titleabbrev></titleabbrev>\n\n" \
		"  <partintro>\n" \
    		"   <para>\n" \
    		"   </para\n" \
    		"  </partintro>\n\n";

	xmlfoot = " </reference>\n\n" \
		"<!-- Keep this comment at the end of the file\n" \
		"Local variables:\n" \
		"mode: sgml\n" \
		"sgml-omittag:t\n" \
		"sgml-shorttag:t\n" \
		"sgml-minimize-attributes:nil\n" \
		"sgml-always-quote-attributes:t\n" \
		"sgml-indent-step:1\n" \
		"sgml-indent-data:t\n" \
		"sgml-parent-document:nil\n" \
		"sgml-default-dtd-file:\"../../manual.ced\"\n" \
		"sgml-exposed-tags:nil\n" \
		"sgml-local-catalogs:nil\n" \
		"sgml-local-ecat-files:nil\n" \
		"End:\n" \
		"-->\n"
}

{
	args_max = args_min = optional = i = spec_opt = 0
	line = $0
	spec_str = "\""

	func_type = gobble(type);
	func_name = gobble(name);

	if (gobble("\\(")) {
		if (gobble("\\[")) optional = 1
		while (arg_type = gobble(type)) {
			arg_name = gobble(name)
			if(arg_type == "void") {
				args_max = 0;
				args_min = 0;
				break;
			} else {
				argtypes[num_funcs,args_max] = arg_type
				argnames[num_funcs,args_max] = arg_name

				args_max++
				spec_str = spec_str spec_map[arg_type]
				if (optional) {
					optionals[num_funcs,i] = optional
					if (arg_type != "resource") {
						useswitch[num_funcs] = 1
					}
				} else {
						args_min++
				}

				if (x = gobble("\\[")) {
					if (!spec_opt) {
						spec_str = spec_str "|"
						spec_opt = 1
					}
					optional++
				}

				y = gobble(",")
				if (!x && y && optional) {
					grouped_optional_param[num_funcs,i] = 1
				}
				i++
			}
		}
	}

#	if (x = gobble("\\)")) {
		gobble("\\]* *)")
		sub(/^[ \t]+/, "", line)
		fcomments[num_funcs] = line
#	}

	spec_str = spec_str "\""

	funcs[num_funcs]   = func_name
	types[num_funcs]   = func_type
	maxargs[num_funcs] = args_max
	minargs[num_funcs] = args_min
	specs[num_funcs]   = spec_str

	num_funcs++
}

END {
	if (xml) print xmlhead > xmldoc
	for (i = 0; i < num_funcs; i++) {
		compareargc = maxargs[i] - minargs[i]
		closefetch = fetchargs = zvals = xmlparams = funcvals = resources = handleargs = closeopts = ""
		ints = longs = doubles = strings = bools = zvals = ""

		proto = "/* {{{ proto " types[i] " " funcs[i] "("

	 	refid = funcs[i]
	 	gsub(/_/, "-", refid)
	 	xmlstr = "  <refentry id=\"function." refid "\">\n" \
			"   <refnamediv>\n" \
			"    <refname>" funcs[i] "</refname>\n" \
			"    <refpurpose>" fcomments[i] "</refpurpose>\n" \
			"   </refnamediv>\n" \
			"   <refsect1>\n" \
			"    <title>Description</title>\n" \
			"    <funcsynopsis>\n" \
			"     <funcprototype>\n" \
			"      <funcdef>" types[i] " <function>" funcs[i] "</function></funcdef>\n"

		fetchargs = "\tif (zend_parse_parameters(ZEND_NUM_ARGS(), " specs[i] ", "

		for (j = 0; j < maxargs[i]; j++) {

			if (j) {
				fetchargs = fetchargs ", "
			}

			fetchargs = fetchargs "&" argnames[i,j]
			if (argtypes[i,j] == "string") {
				fetchargs = fetchargs ", &" argnames[i,j] "_len"
			}

			xmlparams = xmlparams "     <paramdef>" argtypes[i,j]
			if (j > minargs[i]-1) {
				if (!grouped_optional_param[i,j-1]) {
					if (j > 0) proto = proto " "
					proto = proto "["
					closeopts = closeopts "]"
				}
				xmlparams = xmlparams "\n      <parameter><optional>" \
					argnames[i,j] \
					"</optional></parameter>\n     </paramdef>\n"
			} else {
				xmlparams = xmlparams \
					" <parameter>" \
					argnames[i,j] \
					"</parameter></paramdef>\n"
			}

			if (j > 0) proto = proto ", "
			proto = proto argtypes[i,j] " " argnames[i,j]

			convert(i, j, 1)
		}

		proto = proto closeopts ")\n   " fcomments[i] " */\nPHP_FUNCTION(" funcs[i] ")\n{"
		fetchargs = fetchargs ") == FAILURE)" closefetch " \n\t\treturn;\n"
		funcvals = strings ints longs doubles bools zvals
		xmlstr = xmlstr xmlparams \
			"     </funcprototype>\n" \
			"    </funcsynopsis>\n" \
			"    <para>\n" \
			"    </para>\n" \
			"   </refsect1>\n" \
			"  </refentry>\n"

		print proto > stubfile
 		if (funcvals) print funcvals > stubfile
		if (fetchargs) print fetchargs > stubfile
		if (resources) {
				print resources > stubfile
				if (!stubs) print "" > extname "/function_warning"
		}
		if (!i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
			print "\tphp_error(E_WARNING, \"" funcs[i] ": not yet implemented\");" > stubfile
		}
		print "}\n/* }}} */\n" > stubfile

		if (stubs) {
			h_stubs = h_stubs "PHP_FUNCTION(" funcs[i] ");\n"
			c_stubs = c_stubs "\tPHP_FE(" funcs[i] ",\tNULL)\n"
		} else {
			print "PHP_FUNCTION(" funcs[i] ");" > extname "/function_declarations"
			print "\tPHP_FE(" funcs[i] ",\tNULL)" > extname "/function_entries"
		}

		if (xml) print xmlstr > xmldoc
	}

	if (stubs) {
		print "\n/* ----------------------------------------------------------- */\n" > stubfile
		print c_stubs > stubfile
		print "\n/* ----------------------------------------------------------- */\n" > stubfile
		print h_stubs > stubfile
	}

	if (xml) print xmlfoot > xmldoc
}

#
# Local variables:
# tab-width: 2
# c-basic-offset: 2
# End: