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
|
#!/bin/sh
#
# This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
# project.
#
# Copyright (C) 1998-2018 OpenLink Software
#
# This project is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; only version 2 of the License, dated June 1991.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#
# Exit script on error
#
#set -e
#
# Parse args
#
list=$1
[ -z "$list" ] && list="default.h"
#
# Basename of plugin
#
outname=`echo "$list" | sed 's/.h//g'`
#
# Make sure temp work directory is empty
#
[ -d tmp ] || mkdir tmp
rm -f tmp/*
#
# Name of generated files
#
importh="import_$outname.h"
exportc="export_$outname.c"
importc="import_$outname.c"
#
# ======================================================================
# Extract list of all include files needed
# ======================================================================
#
grep '^#include[ \t]*"[A-Za-z0-9./_-]*"' < $list > tmp/includes.txt
#
# ======================================================================
# Generate helper script to extract EXE_EXPORT lines from include files
# ======================================================================
#
cat <<- "EOF" > tmp/process.sh
gen_gate_4_file()
{
grep 'EXE_EXPORT' < "$1" >> tmp/exports.txt
}
EOF
sed 's/^#include\([ \t]*\)"\([A-Za-z0-9./_-]*\)"/gen_gate_4_file \2 #/g' < tmp/includes.txt >> tmp/process.sh
chmod a+x tmp/process.sh
#
# Run helper script
#
./tmp/process.sh
#
# ======================================================================
# Extract list of names
# ======================================================================
#
#
# Extract EXE_EXPORT declarations
#
grep '^[ \t]*EXE_EXPORT[ \t]*([^,)]*,[ ]*[A-Za-z][A-Za-z0-9_]*[ \t]*,' < tmp/exports.txt > tmp/decls.txt
#
# Extract export names from declarations
#
sed 's/^\([ \t]*\)EXE_EXPORT\([ \t]*\)(\([^,)]*\),\([ ]*\)\([A-Za-z][A-Za-z0-9_]*\)\([ \t]*\),\(.*\)$/\5@typeof__\5/g' < tmp/decls.txt > tmp/export_names_raw.txt
#
# Extract EXE_EXPORT_TYPED type declarations
#
grep '^[ \t]*EXE_EXPORT_TYPED[ \t]*([ ]*[A-Za-z][A-Za-z0-9_]*[ \t]*,[ ]*[A-Za-z][A-Za-z0-9_]*[ \t]*)' < tmp/exports.txt > tmp/decls_t.txt
#
# Extract export names from type declarations
#
sed 's/^\([ \t]*\)EXE_EXPORT_TYPED\([ \t]*\)(\([ ]*\)\([A-Za-z][A-Za-z0-9_]*\)\([ \t]*\),\([ ]*\)\([A-Za-z][A-Za-z0-9_]*\)\([ \t]*\))\(.*\)$/\7@\4/g' < tmp/decls_t.txt >> tmp/export_names_raw.txt
#
# Generate sorted list of uniq export names
#
sort -u < tmp/export_names_raw.txt > tmp/export_names.txt
#
# ======================================================================
# Generate defines and types
# ======================================================================
#
sed 's/^\(.*\)$/#define \1 (_gate._\1._ptr)/g' < tmp/export_names.txt > tmp/gate_use.txt
sed 's/^\(.*\)$/ struct { typeof__\1 *_ptr; const char *_name; } _\1;/g' < tmp/export_names.txt > tmp/gate_decl.txt
sed 's/^\(.*\)$/ { NULL, "\1" },/g' < tmp/export_names.txt > tmp/gate_idef.txt
sed 's/^\(.*\)$/ { \&\1, "\1" },/g' < tmp/export_names.txt > tmp/gate_edef.txt
sed 's/^\([^@]*\)@\(.*\)$/#define \1 (_gate._\1._ptr)/g' < tmp/export_names.txt > tmp/gate_use.txt
sed 's/^\([^@]*\)@\(.*\)$/ struct { \2 *_ptr; const char *_name; } _\1;/g' < tmp/export_names.txt > tmp/gate_decl.txt
sed 's/^\([^@]*\)@\(.*\)$/ { NULL, "\1" },/g' < tmp/export_names.txt > tmp/gate_idef.txt
sed 's/^\([^@]*\)@\(.*\)$/ { \&\1, "\1" },/g' < tmp/export_names.txt > tmp/gate_edef.txt
#
# ======================================================================
# Generate import gate header
# ======================================================================
#
cat <<- EOF > $importh.tmp
/* This file is automatically generated by plugin/gen_gate.sh */
#ifndef __gate_import_h_
#define __gate_import_h_
/* First we should include all imported header files to define data types of arguments and return values */
EOF
cat < tmp/includes.txt >> $importh.tmp
cat <<- EOF >> $importh.tmp
/* Now we should declare dictionary structure with one member per one imported function. */
/* At connection time, executable will fill an instance of this structure with actual pointers to functions. */
struct _gate_s {
EOF
cat < tmp/gate_decl.txt >> $importh.tmp
cat <<- EOF >> $importh.tmp
struct { void *_ptr; const char *_name; } _gate_end;
};
/* Only one instance of _gate_s will exist, and macro definitions will be used to access functions of main executable */
/* via members of this instance. */
extern struct _gate_s _gate;
EOF
cat < tmp/gate_use.txt >> $importh.tmp
cat <<- EOF >> $importh.tmp
#endif
EOF
#
# ======================================================================
# Generate import gate source
# ======================================================================
#
cat <<- EOF >> $importc.tmp
/* This file is automatically generated by plugin/gen_gate.sh */
#include <stdlib.h>
#include "$importh"
struct _gate_s _gate = {
EOF
cat < tmp/gate_idef.txt >> $importc.tmp
cat <<- EOF >> $importc.tmp
{ NULL, "." }
};
EOF
#
# ======================================================================
# Generate export gate source
# ======================================================================
#
cat <<- EOF > $exportc.tmp
/* This file is automatically generated by plugin/gen_gate.sh */
#define EXPORT_GATE
#include "exe_export.h"
#include <string.h>
/* First we should include all imported header files to declare names of all exported functions */
EOF
cat < tmp/includes.txt >> $exportc.tmp
cat <<- EOF >> $exportc.tmp
/* Now we should declare dictionary array with one item per one exported function. */
/* At connection time, executable will fill _gate structures of plugins with data from this table. */
struct _gate_export_item_s { void *_ptr; const char *_name; };
typedef struct _gate_export_item_s _gate_export_item_t;
extern _gate_export_item_t _gate_export_data[];
int _gate_export (_gate_export_item_t *tgt)
{
int err = 0;
_gate_export_item_t *src = _gate_export_data;
for (/* no init */; '.' != tgt->_name[0]; tgt++)
{
err = -1;
for (/* no init */; '.' != src->_name[0]; src++)
{
if (strcmp (src->_name, tgt->_name))
continue;
tgt->_ptr = src->_ptr;
err = 0;
break;
}
if (err)
break;
}
return err;
}
_gate_export_item_t _gate_export_data[] = {
EOF
cat < tmp/gate_edef.txt >> $exportc.tmp
cat <<- EOF >> $exportc.tmp
{ NULL, "." }
};
EOF
#
# ======================================================================
# Compare newly generated version against current version
# to reduce unnecessary rebuilds
# ======================================================================
#
for i in $importh $exportc $importc
do
cmp $i.tmp $i 2>/dev/null >/dev/null || mv $i.tmp $i
rm -f $i.tmp
done
exit 0
|