From c4d89540574dfb6a7418ed8cdd34e3cb2df585e7 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 29 Oct 2018 00:50:38 +0200 Subject: Initial commit --- gen-stubs.bash | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 gen-stubs.bash (limited to 'gen-stubs.bash') 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 -- cgit v1.2.3