Skip to content

Commit

Permalink
- Fixed incorrect code generated when all parameters are optional.
Browse files Browse the repository at this point in the history
- Fixed handling of grouped optional parameters.
- Added an option to generate xml documentation.
- Added an option not to be nice and helpful and create all kinds
  of comments and testing functions.
- Added on option to create function stubs only.
- Added options --assing-params and --string-lens that change
  the generated code.
- Updated documentation.
  • Loading branch information
Jouni Ahto committed Jun 15, 2000
1 parent 9ded807 commit 495a957
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 150 deletions.
97 changes: 79 additions & 18 deletions README.EXT_SKEL
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ HOW TO USE IT

Very simple. First, cd do directory ext/ in PHP 4 sources. If you just need
the basic framework and will be writing all the code in your functions
yourself, you can now do './ext_skel your_module_name' and everything you
need is placed in directory your_module_name. In fact, if you don't need to
test the existence of any external header files, libraries or functions in
them, the module is already almost ready to be compiled in PHP. Just remove
3 comments in your_module_name/config.m4, cd back up to PHP sources top
directory, and do './buildconf; ./configure --enable-your_module_name; make'.
yourself, you can now do

./ext_skel --extname=module_name

and everything you need is placed in directory module_name. In fact, if you
don't need to test the existence of any external header files, libraries or
functions in them, the module is already almost ready to be compiled in PHP.
Just remove 3 comments in your_module_name/config.m4, cd back up to PHP
sources top directory, and do

./buildconf; ./configure --enable-module_name; make

But if you already have planned the overall scheme of your module, what
functions it will contain, their return types and the arguments they take
(a very good idea) and don't want to bother yourself with creating function
definitions and handling arguments passed yourself, it's time to create a
function definitions file, which you will give as the second argument to
ext_skel.
function definitions file, which you will give as an argument to ext_skel
with option

--proto=filename.

FORMAT OF FUNCTION DEFINITIONS FILE

Expand All @@ -36,7 +43,7 @@ FORMAT OF FUNCTION DEFINITIONS FILE
Arguments are given in parenthesis after the function name, and are of
the form 'argument_type argument_name'. Arguments are separated from each
other with a comma and optional space. Argument_type can be one of int,
double, string, array, object or mixed.
bool, double, float, string, array, object or mixed.

An optional argument is separated from the previous by an optional space,
then '[' and of course comma and optional space, like all the other
Expand All @@ -59,17 +66,71 @@ FORMAT OF FUNCTION DEFINITIONS FILE
The file must contain nothing else but function definitions, no comments or
empty lines.

CURRENT LIMITATIONS AND BUGS
OTHER OPTIONS

--no-help

By default, ext_skel creates both comments in the source code and a test
function to help first time module writers to get started and testing
configuring and compiling their module. This option turns off all such things
which may just annoy experienced PHP module coders. Especially useful with

--stubs=file

which will leave out also all module specific stuff and write just function
stubs with function value declarations and passed argument handling, and
function entries and definitions at the end of the file, for copying and
pasting into an already existing module.

--assign-params
--string-lens

By default, function proto 'void foo(string bar)' creates the following:
...
zval **bar;
... (zend_get_parameters_ex() called in the middle...)
convert_to_string_ex(bar);

Only arguments of types int, float, string and array are handled. For other
types you must write the code yourself. And for type mixed, it wouldn't even
be possible to write anything, because only you know what to expect.
Specifying both of these options changes the generated code to:
...
zval **bar_arg;
int bar_len;
char *bar = NULL;
... (zend_get_parameters_ex() called in the middle...)
convert_to_string_ex(bar_arg);
bar = Z_STRVAL_PP(bar_arg);
bar_len = Z_STRLEN_PP(bar_arg);

You shouldn't have to ask what happens if you leave --string-lens out. If you
have to, it's questionable whether you should be reading this document.

--with-xml[=file]

Creates the basics for phpdoc .xml file.

--full-xml

Not implemented yet. When or if there will ever be created a framework for
self-contained extensions to use phpdoc system for their documentation, this
option enables it on the created xml file.

CURRENT LIMITATIONS, BUGS AND OTHER ODDITIES

Only arguments of types int, bool, double, float, string and array are
handled. For other types you must write the code yourself. And for type
mixed, it wouldn't even be possible to write anything, because only you
know what to expect.

It doesn't yet handle correctly grouped optional arguments, ie. it thinks
'type function(type arg1 [, type arg2, type arg3]' to actually be
'type function(type arg1 [, type arg2 [, type arg3]]', so you have to
manually correct the switch construct created. But it's nothing more than
deleting a few 'case ?:' lines and fixing PHP in-source documentation proto.
It can't handle correctly, and probably never will, variable list of
of arguments. (void foo(int bar [, ...])

Don't trust too much the generated code. It tries to be useful in most of
the situations you might encounter, but automatic code generating will never
beat a programmer who knows the real situation at hand. axt_skel is generally
best suited for quickly generating a wrapper for c-library functions you
might want to have available in PHP too.

This program doesn't have a --help option. It has --no-help instead.

EXAMPLE

Expand Down
152 changes: 121 additions & 31 deletions ext/ext_skel
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
#!/bin/sh

extname="$1"
EXTNAME=`echo $1|tr a-z A-Z`
if [ ! -z $2 -a -r $2 ]; then
functions=$2
echo=$2
fi

givup() {
echo $*
exit 1
}

if test "$extname" = ""; then
givup "usage: $0 extension-name [function-list]"
usage() {
echo "$0 --extname=module [--proto=file] [--stubs=file] [--xml[=file]]"
echo " [--full-xml] [--no-help] [--assign-params [--string-lens]]"
echo ""
echo " --extname=module module is the name of your extension"
echo " --proto=file file contains prototypes of functions to create"
echo " --stubs=file generate only function stubs in file"
echo " --xml generate xml documentation to be added to phpdoc-cvs"
echo " --full-xml generate xml documentation for a self-contained extension"
echo " (not yet implemented)"
echo " --no-help don't try to be nice and create comments in the code"
echo " and helper functions to test if the module compiled"
echo " --assign-params"
echo " --string-lens"
exit 1
}

if test $# -eq 0; then
usage
fi

while test $# -gt 0; do
case "$1" in
-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac

case $1 in
--extname=?*)
extname=$optarg
EXTNAME=`echo $extname | tr a-z A-Z`
;;
--proto=?*)
proto=$optarg
;;
--stubs=*)
stubs=yes
stubfile=$optarg
;;
--xml)
xml="yes"
;;
--xml=?*)
xml=$optarg
;;
--full-xml)
full_xml="yes"
;;
--no-help)
no_help="yes"
;;
--assign-params)
assign_params="yes"
;;
--string-lens)
string_lens="yes"
;;
*)
usage
;;
esac
shift
done

if [ -z "$assign_params" -a ! -z "$string_lens" ]; then
usage
fi

if test -d "$extname" ; then
Expand All @@ -32,15 +89,18 @@ else
ECHO_C='\c'
fi

echo "Creating directory"
if [ -z $stubs ]; then
echo "Creating directory $extname"
stubfile=$extname"/function_stubs"

mkdir $extname || givup "Cannot create directory $extname"
fi

if [ ! -z $functions ]; then
echo $functions
cat $functions | awk -v extname=$extname -f ./skeleton/create_stubs
if [ ! -z $proto ]; then
cat $proto | awk -v extname=$extname -v stubs=$stubs -v stubfile=$stubfile -v xml=$xml -v full_xml=$full_xml -v i_know_what_to_do_shut_up_i_dont_need_your_help_mode=$no_help -v assign_params=$assign_params -v string_lens=$string_lens -f ./skeleton/create_stubs
fi

if [ -z $stubs ]; then
cd $extname
chmod 755 .

Expand Down Expand Up @@ -102,32 +162,59 @@ libs.mk
eof

$ECHO_N " $extname.c$ECHO_C"
cat ../skeleton/skeleton.c | sed \
-e "s/extname/$extname/g" \
-e "s/EXTNAME/$EXTNAME/g" \
-e '/__function_entries_here__/r function_entries' \
-e '/__function_stubs_here__/r function_stubs' \
-e '/__function_entries_here__/D' \
-e '/__function_stubs_here__/D' \
> $extname.c
echo "s/extname/$extname/g" > sedscript
echo "s/EXTNAME/$EXTNAME/g" >> sedscript
echo '/__function_entries_here__/r function_entries' >> sedscript
echo '/__function_stubs_here__/r function_stubs' >> sedscript
echo '/__header_here__/r ../../header' >> sedscript
echo '/__footer_here__/r ../../footer' >> sedscript
echo '/__function_entries_here__/D' >> sedscript
echo '/__function_stubs_here__/D' >> sedscript
echo '/__header_here__/D' >> sedscript
echo '/__footer_here__/D' >> sedscript
if [ ! -z $no_help ]; then
echo "/confirm_$extname_compiled/D" >> sedscript
echo '/Remove the following/,/^\*\//D' >> sedscript
echo 's/[[:space:]]\/\*.\+\*\///' >> sedscript
echo 's/^\/\*.*\*\/$//' >> sedscript
echo '/^[[:space:]]*\/\*/,/^[[:space:]]*\*\//D' >> sedscript
fi

cat ../skeleton/skeleton.c | sed -f sedscript > $extname.c


$ECHO_N " php_$extname.h$ECHO_C"
cat ../skeleton/php_skeleton.h | sed \
-e "s/extname/$extname/g" \
-e "s/EXTNAME/$EXTNAME/g" \
-e '/__function_declarations_here__/r function_declarations' \
-e '/__function_declarations_here__/D' \
> php_$extname.h
echo "s/extname/$extname/g" > sedscript
echo "s/EXTNAME/$EXTNAME/g" >> sedscript
echo '/__function_declarations_here__/r function_declarations' >> sedscript
echo '/__header_here__/r ../../header' >> sedscript
echo '/__footer_here__/r ../../footer' >> sedscript
echo '/__function_declarations_here__/D' >> sedscript
echo '/__header_here__/D' >> sedscript
echo '/__footer_here__/D' >> sedscript
if [ ! -z $no_help ]; then
echo "/confirm_$extname_compiled/D" >> sedscript
echo 's/[[:space:]]\/\*.\+\*\///' >> sedscript
echo 's/^\/\*.*\*\/$//' >> sedscript
echo '/^[[:space:]]*\/\*/,/^[[:space:]]*\*\//D' >> sedscript
fi
cat ../skeleton/php_skeleton.h | sed -f sedscript > php_$extname.h

rm sedscript

if [ -z "$stubs" -a -z "$no_help" ]; then
$ECHO_N " $extname.php$ECHO_C"
cat ../skeleton/skeleton.php | sed \
-e "s/extname/$extname/g" \
> $extname.php
fi

if [ ! -z $functions ]; then
rm function_entries
rm function_declarations
rm function_stubs
if [ ! -z $proto ]; then
if [ -z $stubs ]; then
rm function_entries
rm function_declarations
rm function_stubs
fi
if [ -f function_warning ]; then
rm function_warning
warning="
Expand All @@ -139,9 +226,11 @@ in the instructions above.
fi

chmod 644 *
fi

echo " [done]."

if [ -z "$no_help" -a -z "$stubs" ]; then
cat <<eof
To use your new extension, you will have to execute the following steps:
Expand All @@ -160,3 +249,4 @@ step 6 confirms that your module is compiled in PHP. Then, start writing
code and repeat the last two steps as often as necessary.
$warning
eof
fi
Loading

0 comments on commit 495a957

Please sign in to comment.