MIT krb5-1.6 HACK patch
[metze/wireshark/wip.git] / tools / convert-proto-tree-new.awk
1 #!/usr/bin/gawk -f
2
3 # $Id$
4
5 function again(i)
6 {
7  # shift remaining arguments up
8  for (i = ARGC; i > ARGIND; i--)
9      ARGV[i] = ARGV[i-1]
10
11  # make sure gawk knows to keep going
12  ARGC++
13
14  # make current file next to get done
15  ARGV[ARGIND+1] = FILENAME
16 }
17
18 BEGIN {
19         while (getline x) {
20                 if (x ~ /^static\s*(int|gint)\s*hf_(.*)=\s*-1/) {
21                         hf = gensub(/^static\s*(int|gint)\s*(\S*).*/, "\\2", "g", x)
22
23                         HFS[hf] = ""
24                 }
25
26                 if (x ~ /\{\s*&hf_(.*)/) {
27                         hf = gensub(/\s*\{\s*\&(.*),(.*)/, "\\1", "g", x)
28
29                         if (hf in HFS) {
30                                 hf_descr = gensub(/\s*\{\s*\&(.*),(.*)/, "\\2", "g", x)
31
32                                 do { 
33                                         getline x
34                                         hf_descr = hf_descr "\n" x
35                                         # XXX, below regex should check if we have { hf description }},
36                                 } while (!(hf_descr ~ /[^{}]*}[^{}]*}[^{}]*,/))
37
38                                 # get rid of one }
39                                 hf_descr = gensub(/}\S*},/, "}", "g", hf_descr);
40
41                                 HFS[hf] = hf_descr
42                         }
43                 }
44         }
45
46         print "#define NEW_PROTO_TREE_API"
47         print "converted " length(HFS) > "/dev/stderr"
48
49         again()
50         TWOPASS = 1
51 }
52
53 TWOPASS {
54         x = $0
55         do {
56                 if (x ~ /^static\s*(int|gint)\s*hf_(.*)=\s*-1/) {
57                         hf = gensub(/^static\s*(int|gint)\s*(\S*).*/, "\\2", "g", x)
58                         ## XXX, it can have some comment or smth, copy?
59
60                         if (hf in HFS && HFS[hf] != "") {
61                                 print "static header_field_info " gensub("^hf_", "hfi_", "g", hf) " THIS_HF_INIT =" HFS[hf] ";"
62                                 print ""
63                         } else
64                                 print x
65                 }
66
67                 else if (x ~ /\{\s*&hf_(.*)/) {
68                         hf = gensub(/\s*\{\s*\&(.*),(.*)/, "\\1", "g", x)
69
70                         if (hf in HFS) {
71                                 ## keep indent
72                                 new_x = gensub(/(\s*)\{\s*\&hf_(.*),(.*)/, "\\1\\&" "hfi_" "\\2" ",", "g", x)
73
74                                 hf_descr = gensub(/\s*\{\s*\&(.*),(.*)/, "\\2", "g", x)
75
76                                 do {
77                                         getline x
78                                         hf_descr = hf_descr "\n" x
79                                 } while (!(hf_descr ~ /}/))
80
81                                 print new_x
82
83                         } else
84                                 print x
85                 } else
86                         print gensub("hf_", "\\&hfi_", "g", x)
87
88         } while (getline x);
89 }