Home | History | Annotate | Download | only in generator
      1 #!/bin/sh
      2 #
      3 # Copyright (C) 2014 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 
     18 cat $1 |
     19   # Mark descriptions and actions in the body.
     20   sed 's/^[0-9. ]*Command and Response$/_COMMAND_SECTION/' |
     21   sed 's/^[0-9. ]*Detailed Actions$/_ACTIONS_SECTION/' |
     22   # Keep only command and response sections.
     23   awk '/^_COMMAND_SECTION$/,/^_ACTIONS_SECTION$/ { print $0; }' |
     24   sed 's/^_COMMAND_SECTION$//' |
     25   sed 's/^_ACTIONS_SECTION$//' |
     26   # Remove headers and footers.
     27   sed 's/^.*Trusted Platform Module Library.*$//' |
     28   sed 's/^.*Part 3: Commands.*$//' |
     29   sed 's/^.*Family .2.0..*$//' |
     30   sed 's/^.*Level 00 Revision.*$//' |
     31   sed 's/^.*Published.*$//' |
     32   sed 's/^.*Copyright.*$//' |
     33   sed 's/^.*Page [0-9].*$//' |
     34   sed 's/^.*October 31, 2013.*$//' |
     35   # Remove table headers.
     36   sed 's/^Type$//' | sed 's/^Name$//' | sed 's/^Description$//' |
     37   # Remove leading spaces.
     38   sed 's/^[ ][ ]*//' |
     39   # Remove empty lines.
     40   sed '/^$/d' |
     41   # Mark begin and end.
     42   awk '
     43     BEGIN { print "_BEGIN"; }
     44     { print $0; }
     45     END { print "_END"; }
     46   ' |
     47   # Mark command / response tables.
     48   sed 's/^Table [0-9]* . \(.*\) Command$/_INPUT_START \1/' |
     49   sed 's/^Table [0-9]* . \(.*\) Response$/_OUTPUT_START \1/' |
     50   # Mark argument types.
     51   sed 's/^\(TPM[_A-Z0-9+]*\)$/_TYPE \1/' |
     52   sed 's/^\(U*INT[0-9]*\)$/_TYPE \1/' |
     53   # Filter out a few special cases that look like types but are not.
     54   sed 's/^_TYPE TPM_ST_NO_SESSIONS$/TPM_ST_NO_SESSIONS/' |
     55   sed 's/^_TYPE TPM_ALG_NULL$/TPM_ALG_NULL/' |
     56   sed 's/^_TYPE TPM_CC_HMAC$/TPM_CC_HMAC/' |
     57   sed 's/^_TYPE TPM_GENERATED_VALUE$/TPM_GENERATED_VALUE/' |
     58   sed 's/^_TYPE \(TPM_RH_[A-Z+]*\)$/\1/' |
     59   # Mark argument names.
     60   awk '
     61     BEGIN { last_line_was_type = 0; }
     62     /^_.*$/      { print $0; }
     63     /^_TYPE .*$/ { last_line_was_type = 1; }
     64     /^[^_].*$/   { if (last_line_was_type) {
     65                      last_line_was_type = 0;
     66                      print "_NAME " $0;
     67                      if ($0 !~ /^[@a-zA-Z0-9]*$/) {
     68                        print "_ERROR: Invalid name."; } }
     69                    else { print $0; } }
     70   ' |
     71   # Consolidate comments to a single line and mark.
     72   awk '
     73     BEGIN { comment = ""; }
     74     /^_.*$/    { if (comment != "") { print "_COMMENT " comment; comment = ""; }
     75                  print $0; }
     76     /^[^_].*$/ { if (comment != "") { comment = comment " " $0; }
     77                  else { comment = $0 } }
     78   ' |
     79   # Fix typos.
     80   sed 's/_COMMENT TPM_CC_PolicyNVWritten/_COMMENT TPM_CC_PolicyNvWritten/' |
     81   # Strip off modifiers like TYPE+ and @name.
     82   sed 's/^\(_TYPE .*\)[+]$/\1/' |
     83   sed 's/^_NAME @\(.*\)$/_NAME \1/' |
     84   # Sanity check.  The format should now follow this grammar:
     85   # Format:Begin||CommandBlock*||End
     86   # Begin:_BEGIN\n
     87   # End:_END\n
     88   # CommandBlock:InputTag||Argument*||OutputTag||Argument*
     89   # InputTag:_INPUT_START <command>\n
     90   # OutputTag:_OUTPUT_START <command>\n
     91   # Argument:Type||Name[||Comment]
     92   # Type:_TYPE <type>\n
     93   # Name:_NAME <name>\n
     94   # Comment:_COMMENT <comment>\n
     95   awk '
     96     BEGIN { RS = ""; }
     97     $0 !~ /_BEGIN\n(_INPUT_START[^\n]*\n(_TYPE[^\n]*\n_NAME[^\n]*\n(_COMMENT[^\n]*\n)*)*_OUTPUT_START[^\n]*\n(_TYPE[^\n]*\n_NAME[^\n]*\n(_COMMENT[^\n]*\n)*)*)*_END/ {
     98       print "_ERROR: Format check failed."; }
     99     { print $0; }
    100   '
    101 exit 0
    102