summaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rwxr-xr-xutil/getdefs27
-rwxr-xr-xutil/getenum21
-rwxr-xr-xutil/make-enum45
-rwxr-xr-xutil/make-mask55
-rwxr-xr-xutil/make-multiextractor25
-rwxr-xr-xutil/make-sym-extractor25
-rwxr-xr-xutil/what-architecture-am-i-using22
7 files changed, 220 insertions, 0 deletions
diff --git a/util/getdefs b/util/getdefs
new file mode 100755
index 0000000..869dff6
--- /dev/null
+++ b/util/getdefs
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+set -e
+
+text="$(cat)"
+
+expr='\(0x[0-9a-fA-F]\+\|[0-9]\+\)[Ll]*[Uu]*[Ll]*'
+expr='\(\*\|\/\|[+~-]\|<<\|>>\)\?\s*'"$expr"
+expr='\('"$expr"'\|(\|)\)'
+expr='\s*\('"$expr"'\s*\)\+'
+
+filter () {
+ sed -n 's/^\s*#\s*define\s\+\('"$1"'\)\s\+\('"$expr"'\)\s*\(\/.*\)\?$/\1 \2/p'
+}
+
+if test $# = 0; then
+ printf '%s\n' "$text" | filter '[A-Z0-9_]\+'
+else
+ for prefix; do
+ printf '%s\n' "$text" | filter "$prefix"'_[A-Z0-9_]\+'
+ done
+fi | \
+while read name value; do
+ value="$(printf '%s\n' "$value" | tr -d ULul)"
+ value=$(printf '%s\n' "$(( $value ))" | cut -d . -f 1)
+ printf '%s %s\n' "$name" "$value"
+done
diff --git a/util/getenum b/util/getenum
new file mode 100755
index 0000000..84cb73e
--- /dev/null
+++ b/util/getenum
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+set -e
+
+sed '1,/^\s*enum\s\+'"$1"'\b/d' | \
+tr '\n' '\0' | cut -d \; -f 1 | tr '\0' '\n' | \
+sed -n 's/^\s*\([A-Z0-9_]\+\)\(\s*=[^,]*\)\?\s*,\?\s*\(\/.*\)\?/\1\2/p' | \
+sed 's/=/ /g' | \
+(
+ prev=-1
+ while read name value; do
+ if test -z "$value"; then
+ value=$(( prev + 1 ))
+ else
+ value="$(printf '%s\n' "$value" | tr -d ULul)"
+ value=$(printf '%s\n' "$(( $value ))" | cut -d . -f 1)
+ fi
+ printf '%s %s\n' "$name" "$value"
+ prev=$value
+ done
+)
diff --git a/util/make-enum b/util/make-enum
new file mode 100755
index 0000000..7b63e3c
--- /dev/null
+++ b/util/make-enum
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+set -e
+
+test $# -ge 1
+
+lowercase () { printf '%s\n' "$*" | tr '[A-Z]' '[a-z]'; }
+
+symbols="$(lowercase "$1")"
+shift 1
+
+validate () {
+ if (( -$1 > 0x7FFF )) || (( +$1 > 0x7FFF )); then
+ printf 'Invalid enum value: %s = %x (beyond 16-bit signed integer)\n' $2 $1 >&2
+ return 1
+ else
+ return 0
+ fi
+}
+
+printf 'static const char *\n'
+printf 'extract_symbol_%s(struct libsyscalls_symbol_printer_data *data, ' "$symbols"
+printf 'unsigned long long int *valuep, char *fallback_out) /* 1 */\n'
+printf '{\n'
+printf ' const char *ret;\n'
+printf ' switch ((signed long long int)*valuep) {\n'
+while read name value; do
+validate $value $name
+printf ' case %i: ret = "%s"; break;\n' $value $name
+done
+printf ' default:\n'
+if test $# -ge 1; then
+ for use; do
+printf ' if ((ret = extract_symbol_%s(data, valuep, fallback_out))) return ret;\n' "$(lowercase "$use")"
+ done
+fi
+printf ' return NULL;\n'
+printf ' }\n'
+printf ' *valuep = 0;\n'
+if test $# = 0; then
+printf ' (void) data;\n'
+printf ' (void) fallback_out;\n'
+fi
+printf ' return ret;\n'
+printf '}\n'
diff --git a/util/make-mask b/util/make-mask
new file mode 100755
index 0000000..1285f52
--- /dev/null
+++ b/util/make-mask
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+set -e
+
+test $# -ge 1
+
+lowercase () { printf '%s\n' "$*" | tr '[A-Z]' '[a-z]'; }
+
+symbols="$(lowercase "$1")"
+shift 1
+
+trailing_zeroes () {
+ r=0
+ x=$1
+ while (( (x & 1) == 0 )); do
+ : $(( r++ ))
+ x=$(( x >> 1 ))
+ done
+ if (( x > 1 )); then
+ printf 'Invalid bitmask value: %s = %#x: multiple bits set\n' $2 $1 >&2
+ exit 1
+ fi
+ printf '%i\n' $r
+}
+
+ifzero=
+printf 'static const char *\n'
+printf 'extract_symbol_%s(struct libsyscalls_symbol_printer_data *data, ' "$symbols"
+printf 'unsigned long long int *valuep, char *fallback_out) /* 1 */\n'
+printf '{\n'
+printf ' const char *ret;\n'
+printf ' signed char bit = trailing_zeroes(*valuep);\n'
+printf ' *fallback_out = %s;\n' "'x'"
+printf ' switch (bit) {\n'
+while read name value; do
+ if test $value = 0; then ifzero="$name"; continue; fi
+printf ' case %i: ret = "%s"; break;\n' $(trailing_zeroes $value $name) $name
+done
+if test -n "$ifzero"; then
+printf ' case -1: return "%s";\n' $ifzero
+fi
+printf ' default:\n'
+if test $# -ge 1; then
+ for use; do
+printf ' if ((ret = extract_symbol_%s(data, valuep, fallback_out))) return ret;\n' "$(lowercase "$use")"
+ done
+fi
+printf ' return NULL;\n'
+printf ' }\n'
+printf ' *valuep ^= 1ULL << (unsigned)bit;\n'
+if test $# = 0; then
+printf ' (void) data;\n'
+fi
+printf ' return ret;\n'
+printf '}\n'
diff --git a/util/make-multiextractor b/util/make-multiextractor
new file mode 100755
index 0000000..487162e
--- /dev/null
+++ b/util/make-multiextractor
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+set -e
+
+test $# -ge 3
+
+lowercase () { printf '%s\n' "$*" | tr '[A-Z]' '[a-z]'; }
+
+symbols="$(lowercase "$1")"
+shift 1
+
+printf 'static const char *\n'
+printf 'extract_symbol_%s(struct libsyscalls_symbol_printer_data *data, ' "$symbols"
+printf 'unsigned long long int *valuep, char *fallback_out) /* %i */\n' $#
+printf '{\n'
+printf ' switch (data->nr) {\n'
+i=0
+for use; do
+printf ' case %i:\n' $(( i++ ))
+printf ' return extract_symbol_%s(data, valuep, fallback_out);\n' "$(lowercase "$use")"
+done
+printf ' default:\n'
+printf ' abort();\n'
+printf ' }\n'
+printf '}\n'
diff --git a/util/make-sym-extractor b/util/make-sym-extractor
new file mode 100755
index 0000000..7fec6b8
--- /dev/null
+++ b/util/make-sym-extractor
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+set -e
+
+test $# = 2
+
+symbols="$1"
+how=""
+deps=""
+set $(printf '%s\n' "$2" | tr ',|' ' ')
+
+for dep; do
+ if test "$dep" = mask || test "$dep" = enum; then
+ test -z "$how"
+ how="$dep"
+ else
+ deps="$deps $dep"
+ fi
+done
+
+test -n "$how"
+
+dir="$(dirname -- "$0")"
+
+$dir/make-$how "$symbols" $deps
diff --git a/util/what-architecture-am-i-using b/util/what-architecture-am-i-using
new file mode 100755
index 0000000..e64d255
--- /dev/null
+++ b/util/what-architecture-am-i-using
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+dir="$(dirname -- "$0")"
+
+for cmd in "$dir"/../*/what-architecture-am-i-using; do
+ if printf '%s\n' "$cmd" | grep '/util/what-architecture-am-i-using$' > /dev/null; then
+ continue
+ fi
+ "$cmd" "$@"
+ status=$?
+ if test $status = 0; then
+ exit 0
+ elif test $status = 1; then
+ printf '%s\n' 'Architecture not recognised' >&2
+ printf 'UNRECOGNISED\n'
+ exit 1
+ fi
+done
+
+printf '%s\n' 'Operating system not recognised' >&2
+printf 'UNRECOGNISED\n'
+exit 2