#!/bin/bash ################################################################################ ################################################################################ ################################################################################ ################################################################################ #######This file is an unholy mess. It's being replaced as you read this.####### ################################################################################ ################################################################################ ################################################################################ ################################################################################ if [[ ! -f "/opt/zentific/include/zentific-server.zxdl" ]]; then echo "--------------------------------------------------------------" echo -e "\n" echo " Error: please install the minimum zentific core required" echo " from zrpc with:" echo " ./configure --enable-zrpc=no && make && make install" echo -e "\n" echo "--------------------------------------------------------------" exit 1 fi TMP=`mktemp` ZXDLTMP=`mktemp` cat "/opt/zentific/include/zentific-server.zxdl" > $ZXDLTMP sed -i 's#//.*$##' $ZXDLTMP cat $ZXDLTMP | ../comment-remove.sed.sh > $TMP mv $TMP $ZXDLTMP # delete trailing whitespace (spaces, tabs) from end of each line sed -i 's/[ \t]*$//' $ZXDLTMP # delete BOTH leading and trailing whitespace from each line sed -i 's/^[ \t]*//;s/[ \t]*$//' $ZXDLTMP #delete blank lines sed -i '/^$/d' $ZXDLTMP #convert all multiple spaces to single tr -s ' ' < $ZXDLTMP > $TMP mv "${TMP}" "${ZXDLTMP}" cp "${ZXDLTMP}" /tmp/zxdl-processing #list related functions function listfuncs() { FUNCTIONS=$( for i in /opt/zentific/include/function*.h; do [[ `echo $i | grep 'functions-debug'` ]] && continue; egrep '^[A-Za-z0-9_]' $i | egrep '\s*_' ; done | grep -v static | grep '^GSList' | grep -v '_getConsoleLockVmsBySession' | sed -e 's#\*##' -e 's#(# #' | awk '{print $2}') for i in $FUNCTIONS; do SIGNATURE= TYPE=$(python ../zxdl-parse.py $i); SIGNATURE=`egrep "${i}\b" /opt/zentific/include/function*.h | sed -e 's#(# (#' | sed -e 's#^.*:# #' -e 's#;##' -e 's#'"${i}"'##' -e 's#'"${TYPE}"'##' -e 's#GSList##' -e 's#'"*"'##g' -e 's#[ ]*(#(#'` echo NAME="'${i}'" TYPE="'${TYPE}'" SIGNATURE="'${SIGNATURE}'"; done SIGNATURE= } function nonlistfuncs() { for i in /opt/zentific/include/function*.h; do [[ `echo $i | grep 'functions-debug'` ]] && continue; NAMES=`egrep '^[A-Za-z0-9_]' $i | egrep '\s*_' | sort -u | egrep -v '(make_|inline|zxr|zentific_session|static)' | grep -v '^GSList' | sed -e 's#(# (#' | sed -e 's#\*##' | awk '{print $2}'` for n in $NAMES; do SIGNATURE= TYPE=`egrep "$n " /opt/zentific/include/function*.h | sed -e 's#(# (#' | sed -e 's#\*##' -e 's#:# #' | awk '{print $2}'` SIGNATURE=`egrep "$n " /opt/zentific/include/function*.h | sed -e 's#(# (#' | sed -e 's#^.*:# #' -e 's#;##' -e 's#'"${n}"'##' -e 's#'"${TYPE}"'##' -e 's#[ ]*(#(#' -e 's#'"*"'##g'` echo NAME="'$n'" TYPE="'$TYPE'" SIGNATURE="'${SIGNATURE}'" done SIGNATURE= done } FTMP=`mktemp` nonlistfuncs > $FTMP listfuncs >> $FTMP #get rid of ^M characters coming from stay files edited by someone (??) in windows sed -ie 's# ##g' $FTMP #clear it :>jobmodule-callbacks.c :>jobmodule-callback-defs.c :>../modules/zpy-generated.c cat >> ../modules/zpy-generated.c <\n" >> ../modules/zpy-generated.c /bin/echo -e "#include \"zpy-generated.h\"\n" >> ../modules/zpy-generated.c /bin/echo -e "#include \"zpy.h\"\n" >> ../modules/zpy-generated.c while read n t s; do eval $n eval $t eval $s #strip leading underscore from function name ORIGNAME="${NAME}" NAME=$(echo "$NAME"|sed -e 's#^_##') #strip leading spaces, __UNUSED__ const, collapse multiple spaces SIGNATURE=$(echo ${SIGNATURE} | sed -e 's#[ ]*(#(#' -e 's#__UNUSED__##g' -e 's#const##g' -e 's#( #(#' | tr -s ' ' ) FPTR=$(egrep "( |\*)(${NAME}|${ORIGNAME}) " /opt/zentific/include/* | sed -e 's#.*:##' | sed -r 's#[_]?'"${NAME}"' #(*'"${NAME}"')#') if [[ "${ORIGNAME}" != "${ORIGNAME/_/}" ]]; then HOOK="ret->${NAME} = ${ORIGNAME};" elif [[ "${NAME}" == "${ORIGNAME}" ]]; then HOOK="ret->${NAME} = ${ORIGNAME};" else HOOK="ret->${NAME} = _${ORIGNAME};" fi echo -e "\t${HOOK}" >> jobmodule-callbacks.c echo -e "\t${FPTR}" >> jobmodule-callback-defs.c #will we accept input? if no, use __UNUSED__ in funct signature ARGS_ATTR= if [[ "${SIGNATURE}" == "(GError error)" ]]; then ARGS_ATTR=" __UNUSED__" fi #output basic function start cat >> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <${NAME}) { ERR( "Python module: %s - callback function invalid", __func__); goto err; } EOFgen #remove all but commas, and count them. # wc returns an extra for the terminal char, but that's actually our desired number SIGNATURE=$(echo $SIGNATURE | sed -e 's#[()]##g') NUMPARAMS=$(echo $SIGNATURE | sed -e 's#[A-Za-z_ ]##g' | wc -c) PARSEFORMAT= PARSEPARAMS= POSTPARSEARGS= PARSEVARS= PARSEARGS= CFUNCARGS= LISTTYPE= FOREACHFREE= POSTEXECFREE= for p in `seq 1 $NUMPARAMS`; do ERRAMP= PARAM=`echo $SIGNATURE | awk 'BEGIN { FS = "," } ; { print $'${p}' }' |tr -s ' ' | sed -e 's#^[ ]*##' ` PARAMT=`echo $PARAM | awk '{print $1}'` PARAMN=`echo $PARAM | awk '{print $2}'` if [[ "$PARAMT" == "int" ]]; then PARSEFORMAT="${PARSEFORMAT}i" PARSEARGS="${PARSEARGS}, &${PARAMN}" PARSEVARS="${PARSEVARS}\n\tint ${PARAMN}\t= -1;" elif [[ "$PARAMT" == "char" ]]; then PARSEFORMAT="${PARSEFORMAT}s" PARSEVARS="${PARSEVARS}\n\tchar * ${PARAMN}\t= NULL;" PARSEARGS="${PARSEARGS}, &${PARAMN}" elif [[ "$PARAMT" == "GError" ]]; then ERRAMP="&" elif [[ "$PARAMT" == "gboolean" ]]; then PARSEFORMAT="${PARSEFORMAT}i" PARSEVARS="${PARSEVARS}\n\tgboolean ${PARAMN}\t= FALSE;" PARSEARGS="${PARSEARGS}, &${PARAMN}" elif [[ "$PARAMT" == "GSList" ]]; then LISTTYPE=$(echo "${TYPE}" | sed -e 's#[<> ]##g' -e 's#array##') LISTTYPE=$(python ../zxdl-parse.py "${NAME}" "${PARAMN}" | awk '{print $2}'); #handle special cases (e.g. functions in the functions-*.h headers, # but never called from the zxdl if [[ "x${LISTTYPE}" == "x" && "${NAME}" != "${NAME/Disk/}" ]]; then LISTTYPE="DISK" elif [[ "x${LISTTYPE}" == "x" && "${NAME}" != "${NAME/NetIf/}" ]]; then LISTTYPE="NETIF" fi PARSEFORMAT="${PARSEFORMAT}O" PARSEVARS="${PARSEVARS}\n\tGSList * ${PARAMN}\t= NULL;" PARSEVARS="${PARSEVARS}\n\tPyObject * py${PARAMN}\t= NULL;" PARSEARGS="${PARSEARGS}, &py${PARAMN}" if [[ "${LISTTYPE}" == "int" ]]; then POSTPARSEARGS="if(!(${POSTPARSEARGS}\n\t${PARAMN} = PyList_to_GSList(py${PARAMN}, (dictconv)NULL, (intconv)PyInt_toGSListEntry))) {\n\t\tgoto err;\n\t}" elif [[ "${LISTTYPE}" == "string" ]]; then POSTPARSEARGS="if(!(${POSTPARSEARGS}\n\t${PARAMN} = PyList_to_GSList(py${PARAMN}, (dictconv)_to_string, (intconv)NULL))) {\n\t\tgoto err;\n\t}" else POSTPARSEARGS="if(!(${POSTPARSEARGS}\n\t${PARAMN} = PyList_to_GSList(py${PARAMN}, (dictconv)dict_to_${LISTTYPE}, (intconv)NULL))) {\n\t\tgoto err;\n\t}" fi else #object PARSEFORMAT="${PARSEFORMAT}O&" PARSEVARS="${PARSEVARS}\n\t${PARAMT} *${PARAMN}\t= NULL;" PARSEARGS="${PARSEARGS}, dict_arg_to_${PARAMT}, &${PARAMN}" POSTEXECFREE="${PARAMT}_free(${PARAMN});" fi if [[ "${CFUNCARGS}x" == "x" ]]; then CFUNCARGS="${ERRAMP}${PARAMN}" else CFUNCARGS="${CFUNCARGS}, ${ERRAMP}${PARAMN}" fi done cat >> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <${NAME}(${CFUNCARGS}); Py_END_ALLOW_THREADS `/bin/echo -e "${POSTEXECFREE}"` if(error){ goto err; } EOFgen #HANDLE OUTPUT if [[ "${TYPE}" != "${TYPE/array/}" ]]; then convfunc= FOREACHFREE= arraytype=$(echo ${TYPE} | sed -e 's#array<##' -e 's#>##') if [[ "${arraytype}" == "string" ]]; then convfunc="PyString_FromString" FOREACHFREE="g_slist_foreach(head, (GFunc)free, NULL);" elif [[ "${arraytype}" == "int" ]]; then convfunc="PyInt_FromGSListEntry" FOREACHFREE="head = c_ret;\n\tg_slist_free(head);" else convfunc="${arraytype}_to_dict" FOREACHFREE="head = c_ret;\n\tg_slist_foreach(head, (GFunc)${arraytype}_free, NULL);\n\tg_slist_free(head);" fi cat >> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <> ../modules/zpy-generated.c <message); g_clear_error(&error); } return NULL; } EOFgen done < $FTMP #clear and regenerate headers cat > ../modules/zpy-generated.h < #include "zentific.h" #include "defines.h" #include "jobcommon.h" #include "zpy-typewrappers.h" extern zModCallbacks *zmcb; EOFgen grep '^[A-Za-z]' ../modules/zpy-generated.c | grep -v 'err:' | sed -e 's#{#;#' >> ../modules/zpy-generated.h ## FUNCTION wrapper exposures via PyMethodDef : > ../modules/zpy-wrapper-defs.c for f in $(grep ' py_.*{' ../modules/zpy-generated.c | sed -e 's#\*# #' -e 's#(# #' | awk '{print $2}'); do trimmed=${f/py_/} echo "{\"${trimmed}\", ${f}, METH_VARARGS, \"Function wrapper around C _${trimmed}\"}," >> ../modules/zpy-wrapper-defs.c done ## EXCEPTIONS : declaration, initialization, and conversion : > ../modules/zpy-exc-defs.c : > ../modules/zpy-exc-init.c : > ../modules/zpy-exc-conv.c exceptions=`mktemp` exceptionconv=`mktemp` exceptioninit=`mktemp` for err in $(grep _XMLRPC_ERROR /opt/zentific/include/Common.h | awk '{print $1}' | sed -e 's#_XMLRPC_ERROR_##'); do #camelcase ugliness # almost equivalent : sed -E "s/(^|_)([a-z])/\u\2/g" string=$(echo $err | tr A-Z a-z | sed -e 's#_# #g'); string=($string); string=${string[@]^}; string=$(echo $string | sed -e 's# ##g'); #exception declaration echo "static PyObject * PyExc_${string};" >> $exceptions #exception initialization echo >> $exceptioninit echo -e "\tPyExc_${string} = PyErr_NewException(\"zpymod.${string}\", NULL, NULL);" >> $exceptioninit echo -e "\tPy_INCREF(PyExc_${string});" >> $exceptioninit echo -e "\tPyModule_AddObject(m, \"${string}\", PyExc_${string});" >> $exceptioninit echo >> $exceptioninit #exception conversion echo -e "\tcase ZERR(${err}):" >> $exceptionconv echo -e "\t\texc = PyExc_${string};" >> $exceptionconv echo -e "\t\tbreak;" >> $exceptionconv done mv $exceptions ../modules/zpy-exc-defs.c mv $exceptioninit ../modules/zpy-exc-init.c mv $exceptionconv ../modules/zpy-exc-conv.c