1#!/bin/bash
2#
3# Copyright (C) 2007 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# opcode-gen <file>
18#
19# This script uses the file bytecodes.txt (in this directory) to
20# generate code inside the given <file>, based on the directives found
21# in that file. Refer to those files to understand what's being
22# generated. (Look for comments that say "BEGIN(name)" where "name" is
23# one of the "emission" directives defined in opcode-gen.awk in this
24# directory.)
25
26file="$1"
27tmpfile="/tmp/$$.txt"
28
29echo "processing `basename $1`" 1>&2
30
31if [ "x$1" = "x" ]; then
32    echo "must specify a file" 1>&2
33    exit 1
34fi
35
36# Set up prog to be the path of this script, including following symlinks,
37# and set up progdir to be the fully-qualified pathname of its directory.
38prog="$0"
39while [ -h "${prog}" ]; do
40    newProg=`/bin/ls -ld "${prog}"`
41    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
42    if expr "x${newProg}" : 'x/' >/dev/null; then
43        prog="${newProg}"
44    else
45        progdir=`dirname "${prog}"`
46        prog="${progdir}/${newProg}"
47    fi
48done
49oldwd=`pwd`
50progdir=`dirname "${prog}"`
51cd "${progdir}"
52progdir=`pwd`
53prog="${progdir}"/`basename "${prog}"`
54cd "${oldwd}"
55
56bytecodeFile="$progdir/bytecode.txt"
57
58awk -v "bytecodeFile=$bytecodeFile" -f "$progdir/opcode-gen.awk" \
59    "$file" > "$tmpfile"
60
61if [ "$?" = "0" ]; then
62    cp "$tmpfile" "$file"
63    rm "$tmpfile"
64else
65    echo "error running awk" 1>&2
66    exit 1
67fi
68