Home | History | Annotate | Download | only in awk
      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 package name from an application's
     16 # manifest (i.e. AndroidManifest.xml). Usage is:
     17 #
     18 #   awk -f <this-script> AndroidManifest.xml
     19 #
     20 # The name itself is the value of the 'package' attribute in the
     21 # 'manifest' element.
     22 #
     23 
     24 BEGIN {
     25     PACKAGE="";
     26     while (xml_event()) {
     27         # Simply extract the value of the 'name' attribute from
     28         # the top-level <manifest> element.
     29         if ( XML_TYPE == "BEGIN" && XML_RPATH == "MANIFEST/" ) {
     30             PACKAGE = XML_ATTR["package"];
     31             break;
     32         }
     33     }
     34     if (!PACKAGE)
     35         PACKAGE = "<none>";
     36 
     37     print PACKAGE;
     38 }
     39 
     40 #
     41 # the following is copied directly from xml.awk - see this file for
     42 # usage and implementation details.
     43 #
     44 function xml_event () {
     45     RS=">";
     46     XML_TAG=XML_TYPE="";
     47     split("", XML_ATTR);
     48     while ( 1 ) {
     49         if (_xml_closing) { # delayed direct tag closure
     50             XML_TAG = _xml_closing;
     51             XML_TYPE = "END";
     52             _xml_closing = "";
     53             _xml_exit(XML_TAG);
     54             return 1;
     55         }
     56         if (getline <= 0) return 0; # read new input line
     57         _xml_p = index($0, "<"); # get start marker
     58         if (_xml_p == 0) return 0; # end of file (or malformed input)
     59         $0 = substr($0, _xml_p) # remove anything before '<'
     60         # ignore CData / Comments / Processing instructions / Declarations
     61         if (_xml_in_section("<!\\[[Cc][Dd][Aa][Tt][Aa]\\[", "]]") ||
     62             _xml_in_section("<!--", "--") ||
     63             _xml_in_section("<\\?", "\\?") ||
     64             _xml_in_section("<!", "")) {
     65             continue;
     66         }
     67         if (substr($0, 1, 2) == "</") { # is it a closing tag ?
     68             XML_TYPE = "END";
     69             $0 = substr($0, 3);
     70         } else { # nope, it's an opening one
     71             XML_TYPE = "BEGIN";
     72             $0 = substr($0, 2);
     73         }
     74         XML_TAG = $0
     75         sub("[ \n\t/].*$", "", XML_TAG);  # extract tag name
     76         XML_TAG = toupper(XML_TAG);       # uppercase it
     77         if ( XML_TAG !~ /^[A-Z][-+_.:0-9A-Z]*$/ )  # validate it
     78             _xml_panic("Invalid tag name: " XML_TAG);
     79         if (XML_TYPE == "BEGIN") {  # update reverse path
     80             _xml_enter(XML_TAG);
     81         } else {
     82             _xml_exit(XML_TAG);
     83         }
     84         sub("[^ \n\t]*[ \n\t]*", "", $0); # get rid of tag and spaces
     85         while ($0) { # process attributes
     86             if ($0 == "/") {  # deal with direct closing tag, e.g. </foo>
     87                 _xml_closing = XML_TAG; # record delayed tag closure.
     88                 break
     89             }
     90             _xml_attrib = $0;
     91             sub(/=.*$/,"",_xml_attrib);  # extract attribute name
     92             sub(/^[^=]*/,"",$0);         # remove it from record
     93             _xml_attrib = tolower(_xml_attrib);
     94             if ( _xml_attrib !~ /^[a-z][-+_0-9a-z:]*$/ ) # validate it
     95                 _xml_panic("Invalid attribute name: " _xml_attrib);
     96             if (substr($0,1,2) == "=\"") { # value is ="something"
     97                 _xml_value = substr($0,3);
     98                 sub(/".*$/,"",_xml_value);
     99                 sub(/^="[^"]*"/,"",$0);
    100             } else if (substr($0,1,2) == "='") { # value is ='something'
    101                 _xml_value = substr($0,3);
    102                 sub(/'.*$/,"",_xml_value);
    103                 sub(/^='[^']*'/,"",$0);
    104             } else {
    105                 _xml_panic("Invalid attribute value syntax for " _xml_attrib ": " $0);
    106             }
    107             XML_ATTR[_xml_attrib] = _xml_value;  # store attribute name/value
    108             sub(/^[ \t\n]*/,"",$0); # get rid of remaining leading spaces
    109         }
    110         return 1; # now return, XML_TYPE/TAG/ATTR/RPATH are set
    111     }
    112 }
    113 
    114 function _xml_panic (msg) {
    115     print msg > "/dev/stderr"
    116     exit(1)
    117 }
    118 
    119 function _xml_in_section (sec_begin, sec_end) {
    120     if (!match( $0, "^" sec_begin )) return 0;
    121     while (!match($0, sec_end "$")) {
    122         if (getline <= 0) _xml_panic("Unexpected EOF: " ERRNO);
    123     }
    124     return 1;
    125 }
    126 
    127 function _xml_enter (tag) {
    128     XML_RPATH = tag "/" XML_RPATH;
    129 }
    130 
    131 function _xml_exit (tag) {
    132     _xml_p = index(XML_RPATH, "/");
    133     _xml_expected = substr(XML_RPATH, 1, _xml_p-1);
    134     if (_xml_expected != XML_TAG)
    135         _xml_panic("Unexpected close tag: " XML_TAG ", expecting " _xml_expected);
    136     XML_RPATH = substr(XML_RPATH, _xml_p+1);
    137 }
    138