summary refs log tree commit diff
path: root/Src/makepro.awk
blob: f6966053170405b31782bed5dd4755595a32a281 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#
# makepro.awk - generate prototype lists
#

BEGIN {
    aborting = 0

    # arg 1 is the name of the file to process
    # arg 2 is the name of the subdirectory it is in
    if(ARGC != 3) {
	aborting = 1
	exit 1
    }
    name = ARGV[1]
    gsub(/^.*\//, "", name)
    gsub(/\.c$/, "", name)
    name = ARGV[2] "_" name
    gsub(/\//, "_", name)
    ARGC--

    printf "E#ifndef have_%s_globals\n", name
    printf "E#define have_%s_globals\n", name
    printf "E\n"
}

# all relevant declarations are preceded by "/**/" on a line by itself

/^\/\*\*\/$/ {
    # The declaration is on following lines.  The interesting part might
    # be terminated by a `{' (`int foo(void) { }' or `int bar[] = {')
    # or `;' (`int x;').
    line = ""
    isfunc = 0
    while(1) {
	if(getline <= 0) {
	    aborting = 1
	    exit 1
	}
	if (line == "" && $0 ~ /^[ \t]*#/) {
            # Directly after the /**/ was a preprocessor line.
            # Spit it out and re-start the outer loop.
	    printf "E%s\n", $0
	    printf "L%s\n", $0
	    next
	}
	gsub(/\t/, " ")
	line = line " " $0
	gsub(/\/\*([^*]|\*+[^*\/])*\*+\//, " ", line)
	if(line ~ /\/\*/)
	    continue
	# If it is a function definition, note so.
	if(line ~ /\) *(VA_DCL )*[{].*$/) #}
	    isfunc = 1
	if(sub(/ *[{;].*$/, "", line)) #}
	    break
    }
    if (!match(line, /VA_ALIST/)) {
	# Put spaces around each identifier.
	while(match(line, /[^_0-9A-Za-z ][_0-9A-Za-z]/) ||
	      match(line, /[_0-9A-Za-z][^_0-9A-Za-z ]/))
	    line = substr(line, 1, RSTART) " " substr(line, RSTART+1)
    }
    # Separate declarations into a type and a list of declarators.
    # In each declarator, "@{" and "@}" are used in place of parens to
    # mark function parameter lists, and "@!" is used in place of commas
    # in parameter lists.  "@<" and "@>" are used in place of
    # non-parameter list parens.
    gsub(/ _ +/, " _ ", line)
    while(1) {
	if(isfunc && match(line, /\([^()]*\)$/))
	    line = substr(line, 1, RSTART-1) " _ (" substr(line, RSTART) ")"
	else if(match(line, / _ \(\([^,()]*,/))
	    line = substr(line, 1, RSTART+RLENGTH-2) "@!" substr(line, RSTART+RLENGTH)
	else if(match(line, / _ \(\([^,()]*\)\)/))
	    line = substr(line, 1, RSTART-1) "@{" substr(line, RSTART+5, RLENGTH-7) "@}" substr(line, RSTART+RLENGTH)
	else if(match(line, /\([^,()]*\)/))
	    line = substr(line, 1, RSTART-1) "@<" substr(line, RSTART+1, RLENGTH-2) "@>" substr(line, RSTART+RLENGTH)
	else
	    break
    }
    sub(/^ */, "", line)
    match(line, /^((const|enum|mod_export|static|struct|union|volatile) +)*([_0-9A-Za-z]+ +|((char|double|float|int|long|short|unsigned|void) +)+)((const|static) +)*/)
    dtype = substr(line, 1, RLENGTH)
    sub(/ *$/, "", dtype)
    if(" " dtype " " ~ / static /)
	locality = "L"
    else
	locality = "E"
    exported = " " dtype " " ~ / mod_export /
    line = substr(line, RLENGTH+1) ","
    # Handle each declarator.
    if (match(line, /VA_ALIST/)) {
	# Already has VARARGS handling.

	# Put parens etc. back
	gsub(/@[{]/, "((", line)
	gsub(/@}/, "))", line)
	gsub(/@</, "(", line)
	gsub(/@>/, ")", line)
	gsub(/@!/, ",", line)
	sub(/,$/, ";", line)
	gsub(/mod_export/, "mod_import_function", dtype)
	gsub(/VA_ALIST/, "VA_ALIST_PROTO", line)
	sub(/ VA_DCL/, "", line)

	if(locality ~ /E/)
	    dtype = "extern " dtype

	if (match(line, /[_0-9A-Za-z]+\(VA_ALIST/))
	  dnam = substr(line, RSTART, RLENGTH-9)

	# If this is exported, add it to the exported symbol list.
	if (exported)
	    printf "X%s\n", dnam

	printf "%s%s %s\n", locality, dtype, line
    } else {
	while(match(line, /^[^,]*,/)) {
		# Separate out the name from the declarator.  Use "@+" and "@-"
		# to bracket the name within the declarator.  Strip off any
		# initialiser.
		dcltor = substr(line, 1, RLENGTH-1)
		line = substr(line, RLENGTH+1)
		sub(/=.*$/, "", dcltor)
		match(dcltor, /^([^_0-9A-Za-z]| const )*/)
		dcltor = substr(dcltor, 1, RLENGTH) "@+" substr(dcltor, RLENGTH+1)
		match(dcltor, /^.*@\+[_0-9A-Za-z]+/)
		dcltor = substr(dcltor, 1, RLENGTH) "@-" substr(dcltor, RLENGTH+1)
		dnam = dcltor
		sub(/^.*@\+/, "", dnam)
		sub(/@-.*$/, "", dnam)

		# Put parens etc. back
		gsub(/@[{]/, " _((", dcltor)
		gsub(/@}/, "))", dcltor)
		gsub(/@</, "(", dcltor)
		gsub(/@>/, ")", dcltor)
		gsub(/@!/, ",", dcltor)

		# If this is exported, add it to the exported symbol list.
		if(exported)
		    printf "X%s\n", dnam

		# Format the declaration for output
		dcl = dtype " " dcltor ";"
		if(locality ~ /E/)
		    dcl = "extern " dcl
		if(isfunc)
		    gsub(/ mod_export /, " mod_import_function ", dcl)
		else
		    gsub(/ mod_export /, " mod_import_variable ", dcl)
		gsub(/@[+-]/, "", dcl)
		gsub(/ +/, " ", dcl)
		while(match(dcl, /[^_0-9A-Za-z] ./) || match(dcl, /. [^_0-9A-Za-z]/))
		    dcl = substr(dcl, 1, RSTART) substr(dcl, RSTART+2)
		printf "%s%s\n", locality, dcl
	}
    }
}

END {
    if(aborting)
	exit 1
    printf "E\n"
    printf "E#endif /* !have_%s_globals */\n", name
}