blob: 592d84ffd2415eca5c93dd534ddcfd3299b24bb3 [file] [log] [blame]
Vadim Bendebury56797522015-05-20 10:32:25 -07001#!/usr/bin/python
2
3import sys
4import re
5
6header = '''// This file was extracted from the TCG Published
7// Trusted Platform Module Library
8// Part 3: Commands
9// Family "2.0"
10// Level 00 Revision 01.16
11// October 30, 2014
12
13'''
14
15head_spaces = re.compile('^\s*[0-9]+\s{0,4}')
16source_lines = open(sys.argv[1], 'r').read().splitlines()
17
18def strip_line_num(line):
19 line = head_spaces.sub('', line)
20 return line
21
22def postprocess_lines(buffer):
23 # get rid of heading line numbers and spaces.
24 buffer = [head_spaces.sub('', x) for x in buffer]
25
26 # Drop the file level conditional compilation statement.
27 for i in range(len(buffer)):
28 if buffer[i].startswith('#include'):
29 continue
30 if buffer[i].startswith(
31 '#ifdef TPM_CC') and buffer[-1].startswith(
32 '#endif // CC_'):
33 buffer = buffer[:i] + buffer[i + 1:-1]
34 break
35 return header + '\n'.join(buffer) + '\n'
36
37text = []
38for line in source_lines:
39 text.append(line)
40 if line == '' and text[-2].startswith(' ') and text[-5] == '':
41 text = text[:-5]
42
43func_file = None
44func_name = ''
45prev_num = 0
46line_buffer = []
47output_buffer = []
48for line in text:
49 f = re.match('^\s*[0-9]+\.[0-9]+\s+(\S+)$', line)
50 if f:
51 func_name = re.sub('^TPM2_', '', f.groups(0)[0])
52
53 num = re.match('^\s*([0-9]+)[$ ]', line + ' ')
54 if num:
55 line_num = int(num.groups(0)[0])
56 if line_num == 1:
57 # this is the first line of a file
58 if func_file:
59 func_file.write(postprocess_lines(output_buffer))
60 func_file.close()
61 func_file = open('%s.c' % func_name, 'w')
62 output_buffer = [line,]
63 prev_num = 1
64 line_buffer = []
65 continue
66 if line_num == prev_num + 1:
67 if line_buffer:
68 output_buffer.append('\n'.join(line_buffer))
69 line_buffer = []
70 output_buffer.append(line)
71 prev_num = line_num
72 continue
73 line_buffer.append('//' + line)
74
75func_file.write(postprocess_lines(output_buffer))
76func_file.close()