aboutsummaryrefslogtreecommitdiffstats
path: root/tools/convert-proto-tree-new.awk
blob: f4ee3a531be63796d8e040e6a1a85a12f5c771df (plain)
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
#!/usr/bin/gawk -f

function again(i)
{
 # shift remaining arguments up
 for (i = ARGC; i > ARGIND; i--)
     ARGV[i] = ARGV[i-1]

 # make sure gawk knows to keep going
 ARGC++

 # make current file next to get done
 ARGV[ARGIND+1] = FILENAME
}

BEGIN {
	while (getline x) {
		if (x ~ /^static\s*(int|gint)\s*hf_(.*)=\s*-1/) {
			hf = gensub(/^static\s*(int|gint)\s*(\S*).*/, "\\2", "g", x)

			HFS[hf] = ""
		}

		if (x ~ /\{\s*&hf_(.*)/) {
			hf = gensub(/\s*\{\s*\&(.*),(.*)/, "\\1", "g", x)

			if (hf in HFS) {
				hf_descr = gensub(/\s*\{\s*\&(.*),(.*)/, "\\2", "g", x)

				do { 
					getline x
					hf_descr = hf_descr "\n" x
					# XXX, below regex should check if we have { hf description }},
				} while (!(hf_descr ~ /[^{}]*}[^{}]*}[^{}]*,/))

				# get rid of one }
				hf_descr = gensub(/}\S*},/, "}", "g", hf_descr);

				HFS[hf] = hf_descr
			}
		}
	}

	print "#define NEW_PROTO_TREE_API"
	print "converted " length(HFS) > "/dev/stderr"

	again()
	TWOPASS = 1
}

TWOPASS {
	x = $0
	do {
		if (x ~ /^static\s*(int|gint)\s*hf_(.*)=\s*-1/) {
			hf = gensub(/^static\s*(int|gint)\s*(\S*).*/, "\\2", "g", x)
			## XXX, it can have some comment or smth, copy?

			if (hf in HFS && HFS[hf] != "") {
				print "static header_field_info " gensub("^hf_", "hfi_", "g", hf) " THIS_HF_INIT =" HFS[hf] ";"
				print ""
			} else
				print x
		}

		else if (x ~ /\{\s*&hf_(.*)/) {
			hf = gensub(/\s*\{\s*\&(.*),(.*)/, "\\1", "g", x)

			if (hf in HFS) {
				## keep indent
				new_x = gensub(/(\s*)\{\s*\&hf_(.*),(.*)/, "\\1\\&" "hfi_" "\\2" ",", "g", x)

				hf_descr = gensub(/\s*\{\s*\&(.*),(.*)/, "\\2", "g", x)

				do {
					getline x
					hf_descr = hf_descr "\n" x
				} while (!(hf_descr ~ /}/))

				print new_x

			} else
				print x
		} else
			print gensub("hf_", "\\&hfi_", "g", x)

	} while (getline x);
}