summaryrefslogtreecommitdiff
path: root/gen-stubs.bash
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2018-10-29 00:50:38 +0200
committerJari Vetoniemi <mailroxas@gmail.com>2018-10-29 00:50:38 +0200
commitc4d89540574dfb6a7418ed8cdd34e3cb2df585e7 (patch)
treed4dbb844ddb68e64a7dd8edb91c662c82417b6b8 /gen-stubs.bash
Initial commit
Diffstat (limited to 'gen-stubs.bash')
-rw-r--r--gen-stubs.bash79
1 files changed, 79 insertions, 0 deletions
diff --git a/gen-stubs.bash b/gen-stubs.bash
new file mode 100644
index 0000000..734dc2e
--- /dev/null
+++ b/gen-stubs.bash
@@ -0,0 +1,79 @@
+#!/bin/bash
+
+filter_classifiers()
+{
+ sed 's/static//;s/__inline__//;s/^[\t ]*//'
+}
+
+ret_type_for()
+{
+ filter_classifiers <<<"$1" | sed 's/^const[\t ]*//' | grep -Eo '^(struct|unsigned)? *[a-z_]+[\t \*]*'
+}
+
+fun_name_for()
+{
+ filter_classifiers <<<"$1" | sed 's/struct//;s/const//;s/unsigned//;s/^[\t ]*//;s/[a-z_]\+[\t \*]*//' | grep -Eo '^[a-z_]+'
+}
+
+should_error()
+{
+ # stubs that should return error value
+ grep -Fqs "$(cat << EOF
+snd_card_
+snd_config_
+EOF
+)" <<<"$1"
+}
+
+return_for()
+{
+ ret="$(ret_type_for "$1" | xargs)"
+ case "$ret" in
+ *\*|snd_config_iterator_t)
+ printf 'return NULL;'
+ ;;
+ void)
+ printf ''
+ ;;
+ snd_pcm_sync_id_t|snd_htimestamp_t)
+ printf 'return (%s){0};' "$ret"
+ ;;
+ 'unsigned char'|int|'unsigned int'|long|'unsigned long'|size_t|ssize_t|pid_t|snd_*)
+ if should_error "$2"; then
+ printf 'return -1;'
+ else
+ printf 'return 0;'
+ fi
+ ;;
+ *)
+ printf 'unhandled return type: %s\n' "$ret" 1>&2
+ exit 1
+ ;;
+ esac
+}
+
+blacklist()
+{
+ grep -Fvs "$(cat << EOF
+return
+snd_dlsym_link
+snd_pcm_hw_strategy
+EOF
+)"
+}
+
+match()
+{
+ blacklist | grep -Eh '^(static|__inline__|const|struct|unsigned| )*[a-z_]+[\t \*]*[a-z_]+\(.*\);'
+}
+
+preprocess()
+{
+ # trim leading whitespace and turn function definitions oneline and remove attributes
+ sed 's/^[\t ]*//;s/[\t ]*__attribute__((.*))[\t ]*//g' | sed -z 's/,[\t ]*\n/, /g'
+}
+
+cat include/alsa/*.h | preprocess | match | tr -d ';' | while read -r fun; do
+ name="$(fun_name_for "$fun")"
+ grep -Fqs "$name(" libasound.c || printf '%s { WARNX1("stub"); %s }\n' "$fun" "$(return_for "$fun" "$name")"
+done