Search code examples
regexbashaidl

Extract methods from AIDL file with Bash (regex?)


I would like to extract all the methods including their argument and return data types. (One per line)

Here is an example file: IPackageManager.aidl

I have tried it like this:

wget -qO - "https://raw.githubusercontent.com/LineageOS/android_frameworks_base/7fc95f204527ee079c5891d56c969668f0b35a0b/core/java/android/content/pm/IPackageManager.aidl" | \
sed -e '1,/interface/d' \
-e '/^$/d' \
-e 's/^[[:space:]]*//' \
-e '/^[^a-zA-Z]/d' \
-e '/^[^;]*$/{$!N}' \
-e '$d' \
-e 's/\(^\|\n\)[[:space:]]*\|\([[:space:]]\)\{2,\}/\2/g'

but unfortunately it still doesn't handle all cases. For example it fails to put queryIntentActivityOptions in one line (Same for setPackagesSuspendedAsUser).

Here is some example output:

ParceledListSlice queryIntentActivities(in Intent intent,String resolvedType, int flags, int userId);
ParceledListSlice queryIntentActivityOptions(in ComponentName caller, in Intent[] specifics,
in String[] specificTypes, in Intent intent,String resolvedType, int flags, int userId);
ParceledListSlice queryIntentReceivers(in Intent intent,String resolvedType, int flags, int userId);

This should have been 3 lines, not 4 because there are only 3 methods.

Any ideas?


Solution

  • To perform the sed command again to remove the \n on lines that end without ;.

    wget -qO - "https://raw.githubusercontent.com/LineageOS/android_frameworks_base/7fc95f204527ee079c5891d56c969668f0b35a0b/core/java/android/content/pm/IPackageManager.aidl" | \
    sed -e '1,/interface/d' \
    -e '/^$/d' \
    -e 's/^[[:space:]]*//' \
    -e '/^[^a-zA-Z]/d' \
    -e '/^[^;]*$/{$!N}' \
    -e '$d' \
    -e 's/\(^\|\n\)[[:space:]]*\|\([[:space:]]\)\{2,\}/\2/g' | \
    sed -e ':x;N;s/\([^;]\)\n/\1/;bx'