1# Copyright (C) 2010 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15# A nawk/gawk script used to extract the list of launchable activities 16# from an application's manifest (i.e. AndroidManifest.xml). Usage: 17# 18# awk -f <this-script> AndroidManifest.xml 19# 20 21# 22# Explanation: 23# 24# A given application can have several activities, and each activity 25# can have several intent filters. We want to only list, in the final 26# output, the activities which have a intent-filter that contains the 27# following elements: 28# 29# <action android:name="android.intent.action.MAIN" /> 30# <category android:name="android.intent.category.LAUNCHER" /> 31# 32# To do this, we need hooks called when entering and exiting <activity> 33# and <intent-filter> elements. 34# 35 36BEGIN { 37 while ( xml_event() ) { 38 # concat xml event type and tag for simpler comparisons 39 event = XML_TYPE "-" XML_TAG; 40 # When entering a new <activity>, extract its name and set 41 # the 'launchable' flag to false. 42 if ( event == "BEGIN-ACTIVITY" && 43 XML_RPATH == "ACTIVITY/APPLICATION/MANIFEST/" ) { 44 name = XML_ATTR["android:name"]; 45 launchable = 0; 46 } 47 # When exiting an <activity>, check that it has a name and 48 # is launchable. If so, print its name to the output 49 else if ( event == "END-ACTIVITY" && 50 XML_RPATH == "APPLICATION/MANIFEST/" ) { 51 if ( name && launchable ) { 52 # If the name doesn't contain any dot, we consider 53 # that it is just missing the initial one. 54 if (index(name, ".") == 0) { 55 name = "." name 56 } 57 print name; 58 } 59 } 60 # When entering an <intent-filter> inside an <activity>, clear 61 # the 'action' and 'category' variables. They are updated when 62 # we enter the corresponding elements within the intent-filter. 63 else if ( event == "BEGIN-INTENT-FILTER" && 64 XML_RPATH == "INTENT-FILTER/ACTIVITY/APPLICATION/MANIFEST/" ) { 65 action = "" 66 category = "" 67 } 68 # When exiting an <intent-filter>, set the 'launchable' flag to true 69 # for the current activity if both 'action' and 'category' have the 70 # correct name. 71 else if ( event == "END-INTENT-FILTER" && 72 XML_RPATH == "ACTIVITY/APPLICATION/MANIFEST/" ) { 73 if ( action == "android.intent.action.MAIN" && 74 category == "android.intent.category.LAUNCHER" ) { 75 launchable = 1; 76 } 77 } 78 # When entering an <action> element inside an <intent-filter>, record 79 # its name. 80 else if ( event == "BEGIN-ACTION" && 81 XML_RPATH == "ACTION/INTENT-FILTER/ACTIVITY/APPLICATION/MANIFEST/" ) { 82 action = XML_ATTR["android:name"]; 83 } 84 # When entering a <category> element inside an <intent-filter>, record 85 # its name. 86 else if ( event == "BEGIN-CATEGORY" && 87 XML_RPATH == "CATEGORY/INTENT-FILTER/ACTIVITY/APPLICATION/MANIFEST/" ) { 88 category = XML_ATTR["android:name"]; 89 } 90 } 91} 92 93 94# 95# the following is copied directly from xml.awk - see this file for 96# usage and implementation details. 97# 98function xml_event () { 99 RS=">"; 100 XML_TAG=XML_TYPE=""; 101 split("", XML_ATTR); 102 while ( 1 ) { 103 if (_xml_closing) { # delayed direct tag closure 104 XML_TAG = _xml_closing; 105 XML_TYPE = "END"; 106 _xml_closing = ""; 107 _xml_exit(XML_TAG); 108 return 1; 109 } 110 if (getline <= 0) return 0; # read new input line 111 _xml_p = index($0, "<"); # get start marker 112 if (_xml_p == 0) return 0; # end of file (or malformed input) 113 $0 = substr($0, _xml_p) # remove anything before '<' 114 # ignore CData / Comments / Processing instructions / Declarations 115 if (_xml_in_section("<!\\[[Cc][Dd][Aa][Tt][Aa]\\[", "]]") || 116 _xml_in_section("<!--", "--") || 117 _xml_in_section("<\\?", "\\?") || 118 _xml_in_section("<!", "")) { 119 continue; 120 } 121 if (substr($0, 1, 2) == "</") { # is it a closing tag ? 122 XML_TYPE = "END"; 123 $0 = substr($0, 3); 124 } else { # nope, it's an opening one 125 XML_TYPE = "BEGIN"; 126 $0 = substr($0, 2); 127 } 128 XML_TAG = $0 129 sub("[ \n\t/].*$", "", XML_TAG); # extract tag name 130 XML_TAG = toupper(XML_TAG); # uppercase it 131 if ( XML_TAG !~ /^[A-Z][-+_.:0-9A-Z]*$/ ) # validate it 132 _xml_panic("Invalid tag name: " XML_TAG); 133 if (XML_TYPE == "BEGIN") { # update reverse path 134 _xml_enter(XML_TAG); 135 } else { 136 _xml_exit(XML_TAG); 137 } 138 sub("[^ \n\t]*[ \n\t]*", "", $0); # get rid of tag and spaces 139 while ($0) { # process attributes 140 if ($0 == "/") { # deal with direct closing tag, e.g. </foo> 141 _xml_closing = XML_TAG; # record delayed tag closure. 142 break 143 } 144 _xml_attrib = $0; 145 sub(/=.*$/,"",_xml_attrib); # extract attribute name 146 sub(/^[^=]*/,"",$0); # remove it from record 147 _xml_attrib = tolower(_xml_attrib); 148 if ( _xml_attrib !~ /^[a-z][-+_0-9a-z:]*$/ ) # validate it 149 _xml_panic("Invalid attribute name: " _xml_attrib); 150 if (substr($0,1,2) == "=\"") { # value is ="something" 151 _xml_value = substr($0,3); 152 sub(/".*$/,"",_xml_value); 153 sub(/^="[^"]*"/,"",$0); 154 } else if (substr($0,1,2) == "='") { # value is ='something' 155 _xml_value = substr($0,3); 156 sub(/'.*$/,"",_xml_value); 157 sub(/^='[^']*'/,"",$0); 158 } else { 159 _xml_panic("Invalid attribute value syntax for " _xml_attrib ": " $0); 160 } 161 XML_ATTR[_xml_attrib] = _xml_value; # store attribute name/value 162 sub(/^[ \t\n]*/,"",$0); # get rid of remaining leading spaces 163 } 164 return 1; # now return, XML_TYPE/TAG/ATTR/RPATH are set 165 } 166} 167 168function _xml_panic (msg) { 169 print msg > "/dev/stderr" 170 exit(1) 171} 172 173function _xml_in_section (sec_begin, sec_end) { 174 if (!match( $0, "^" sec_begin )) return 0; 175 while (!match($0, sec_end "$")) { 176 if (getline <= 0) _xml_panic("Unexpected EOF: " ERRNO); 177 } 178 return 1; 179} 180 181function _xml_enter (tag) { 182 XML_RPATH = tag "/" XML_RPATH; 183} 184 185function _xml_exit (tag) { 186 _xml_p = index(XML_RPATH, "/"); 187 _xml_expected = substr(XML_RPATH, 1, _xml_p-1); 188 if (_xml_expected != XML_TAG) 189 _xml_panic("Unexpected close tag: " XML_TAG ", expecting " _xml_expected); 190 XML_RPATH = substr(XML_RPATH, _xml_p+1); 191} 192