File: processstruct.awk

package info (click to toggle)
ntop 3%3A5.0.1%2Bdfsg1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 15,720 kB
  • ctags: 11,480
  • sloc: ansic: 79,804; sh: 21,658; python: 1,948; awk: 1,504; perl: 971; makefile: 745; php: 123; xml: 71; sql: 13; sed: 11
file content (457 lines) | stat: -rwxr-xr-x 15,299 bytes parent folder | download | duplicates (5)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# ntop xml output generator -- tools

# Written by and copyright (c) 2002, Burton M. Strauss III

# Distributed as part of ntop, http://www.ntop.org

 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software Foundation,
 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 # 

# This tool is part of the ntop xml output system, but it is in fact
# not normally executed by persons compiling ntop.

# It is designed for the developer of ntop to create a skeleton of 
# certain files used by xmldump.c

#  Usage:   awk -f processstruct.awk xxx.h

# This program processes a c language .h file and attempts to extract
# the structure definitions (typedef struct) so that it can create
# (skeleton) subroutines for insertion into xmldump.c

# It also creates /*XML lines for xmldump.awk to process to create
# the c language code that populates the DOM mode in xmldump.awk

# Understand the limitations - this is not a c parser, it's just dumb
# token matching.  The output is probably pretty good, but it's only
# a start.  If there are inteligent groupings of the fields, that's
# not in the struct, well, it won't figure them out.

# However, if you add a bunch of stuff to a structure, or add a new
# structure, this should save you from manually creating everything.

#  Good Luck!

#     -----Burton

#
# Append - stripping trailing blanks and comments.
#    This is used to build up a single awk string that contains an entire C statement,
#    even if it crosses line boundaries.
#
function specialappend(inputline, append) {
    outputline = inputline " " append
    if ( (i=index(outputline, "/*")) > 0) {
         if ( (j = index(outputline, "*/")) > 0) {
            if (j < i) {
                printf("/*XMLNOTE mis-matched comments, may fail */\n")
            } else {
                outputline=substr(outputline, 1, i-1) " " substr(outputline, j+2)
            }
        }
    }
    gsub(/ +$/, "", outputline)
    return outputline
}

BEGIN {
    programname="processstruct.awk"

    datestamp = strftime("%Y-%m-%dT%H:%M:%S", systime() )

    # export debug=anything to turn on debug
    debug=ENVIRON["XMLDUMPDEBUG"]

    outputfile=ARGV[1]
    if ((i=index(outputfile, ".")) > 0) { outputfile = substr(outputfile, 1, i-1) }
    outputfile=outputfile ".skeleton"

    if (tolower(substr(ARGV[1], 1, 1)) == "globals-structtypes.h") {
        xmlprefix="s"
    } else {
        xmlprefix=tolower(substr(ARGV[1], 1, 1))
    }

    printf("/*XMLNOTE %s %s running */\n", datestamp, programname)
    printf("/*XMLNOTE     Processing %s -> %s */\n", ARGV[1], outputfile)

    printf("/*XMLNOTE %s created on %s from %s */\n\n", outputfile, datestamp, ARGV[1]) >outputfile

    lines=0
    found_typedef="N"
    typedef_name=""
    filename=ARGV[1]

    xmlprefix=tolower(substr(ARGV[1], 1, 1))

#
# This is the translation from c types to /*XML typeflag values
#   e.g. a float is n:f (numeric, format %f)
#
    typewords["float"]="n:f"
    typewords["int"]="n"
    typewords["short"]="n"
    typewords["long"]="n:l"
    typewords["unsigned"]="n:u"
    typewords["u_char"]="n:u"
    typewords["u_int"]="n:u"
    typewords["u_short"]="n:u"
    typewords["char"]="s"
    typewords["volatile"]=""
    typewords["u_int16_t"]="n:u"
    typewords["u_int32_t"]="n:u"
    typewords["u_int"]="n:u"
    typewords["u_long"]="n:lu"

#
# This is used for modifiers (e.g. unsigned int) so that we can merge them.
#
    priority_typewords["unsigned"]="u"

#
# These are used to list know typeflag values, so we don't output them (optionally, debug)
# at the end
#
    codewords["n"]="y"
    codewords["s"]="y"
    codewords["b"]="y"
    codewords["n:u"]="y"
    codewords["n:lu"]="y"
    codewords["n:llu"]="y"

#
# These are hard-coded opaque types which we studiously ignore
#
    ignore_types["pthread_mutex_t"]="yes"
    ignore_types["pthread_cond_t"]="yes"
    ignore_types["gdbm_file"]="yes"
    ignore_types["file"]="yes"
    ignore_types["ssl"]="yes"
    ignore_types["mycode"]="yes"

#
# Current .inc output count
#       Since the name is only valid at the end, we store the generated lines...
#
    ocount=0

    skip="N"
}

#
# Line counter and compress out tabs
#
{
    lines++
    gsub(/\t/, " ")
}

#
# Skip cpp directives... unless we're in the middle, then just echo...
#
substr($1, 1, 1) == "#" { 
    if ( (found_typedef == "Y") && 
         ( ($1 == "#if")        || 
           ($1 == "#ifdef")     || 
           ($1 == "#ifndef")    || 
           ($1 == "#elif")      ||
           ($1 == "#else")      ||
           ($1 == "#endif") ) ) {
        outputecho[ocount] = $0
        ocount++
    }
    next 
}

# Skip directives
$1 == "/*XMLSKIPBEGIN" {
    skip="Y"
    next
}

$1 == "/*XMLSKIPEND" {
    skip="N"
    next
}

skip == "Y" { next }

# If all we have is a comment or a blank line, ignore it
$1 == "/*" && $NF == "*/" { next }
$0 == ""                  { next }
/^[ \t]*$/                { next }

# Comment opening ... skip until the end ... yeah, this is crude...
$1 == "/*" {
     while (getline > 0) {
         lines++
         if (index($0, "*/") > 0) {
             next
         }
     }
     next
}

#
# Stuff we care about begins:
#   typedef struct [optionalcomment] {
#
$1 == "typedef" {
    # Lets skip the simplest ones .... typedef xyz int;
    if (index($0, ";") > 0) { next }
    if ($2 != "struct")     { next }

    found_typedef="Y"

    printf("  /*XMLNOTE Starting typedef at line %d */\n", lines) 
    printf("/*XMLNOTE automatically created from %s starting at line %d */\n", 
           filename, lines) >>outputfile

    next
}

#
# Ends:
#   } name;
#
$1 == "}" {

    found_typedef="N"

    # Create working names... (stripping the ;)
    #    fname is the name, lfname is lower case and pfname is 1st upper, rest lower
    fname=$2
    gsub(/ *;$/, "", fname)
    lfname = tolower(fname)
    pfname = toupper(substr(lfname, 1, 1)) substr(lfname, 2)
    typewords[fname]=lfname

    # Output the header lines
    printf("    /*XMLNOTE Creating %s.inc */\n", lfname)
    printf("/*XMLSECTIONBEGIN xml/%s_%s.inc parent input */\n", xmlprefix, lfname) >>outputfile
    printf("  /*XML e      %-30s %-20s   \"\" */\n", fname, "parent:Work") >>outputfile

    # Output the stored lines
    for (i=0; i<ocount; i++) {
        if (i in outputecho) {
            printf("%s\n", outputecho[i]) >>outputfile
            delete outputecho[i]
            continue
        }
        otype=tolower(outputtype[i])
        gsub(/\*/, "", otype)
        if (i in outputindex) {
            printf("  /*XMLFOR i 0 %s */\n", outputindex[i]) >>outputfile
            indent="    "
        } else {
            indent=""
        }
        if (otype != outputfield[i]) {
            printf("%s  /*XML%s %-6s %-30s %-20s   \"\" */\n", 
                indent,
                otype in ignore_types ? "NOTE - IGNORE " : "",
                otype,
                outputfield[i] (i in outputindex ? "[i]" : ""),
                "Work") >>outputfile
        }
        if (i in outputindex) {
            printf("  /*XMLROF */\n") >>outputfile
            delete outputindex[i]
        }
        delete outputfield[i]
    }
    printf("/*XMLSECTIONEND */\n\n\n") >>outputfile

    printf("/* ********************************** */\n") >>outputfile
    printf("/* *Generated skeleton for xmldump.c* */\n") >>outputfile
    printf("/* * created from %-17s * */\n", filename) >>outputfile
    printf("/* *      at line %4d              * */\n", lines) >>outputfile
    printf("/* *      at %-22s * */\n", datestamp) >>outputfile
    printf("/* ********************************** */\n\n") >>outputfile
    printf("GdomeElement * newxml_%s(GdomeElement * parent,\n", lfname) >>outputfile
    printf("                       char * nodename,\n") >>outputfile
    printf("                       %s * input,\n", fname) >>outputfile
    printf("                       char * description);\n\n") >>outputfile
    printf("GdomeElement * newxml_%s(GdomeElement * parent,\n", lfname) >>outputfile
    printf("                       char * nodename,\n") >>outputfile
    printf("                       %s * input,\n", fname) >>outputfile
    printf("                       char * description) {\n\n") >>outputfile
    printf("    GdomeElement *elWork;\n") >>outputfile
    printf("    GdomeException exc;\n\n") >>outputfile
    printf("#if (XMLDUMP_DEBUG >= 3)\n") >>outputfile
    printf("        traceEvent(CONST_TRACE_INFO, \"XMLDUMP_DEBUG: Starting newxml_%s\\n\");\n", lfname) >>outputfile
    printf("#endif\n\n") >>outputfile
    printf("    /* Insert the generated block of code */\n") >>outputfile
    printf("        #include \"xml/%s_%s.inc\"\n\n", xmlprefix, lfname) >>outputfile
    printf("#if (XMLDUMP_DEBUG >= 3)\n") >>outputfile
    printf("        traceEvent(CONST_TRACE_INFO, \"XMLDUMP_DEBUG: Ending newxml_%s\\n\");\n", lfname) >>outputfile
    printf("#endif\n\n") >>outputfile
    printf("    return elWork;\n") >>outputfile
    printf("}\n\n") >>outputfile
    printf("/* ********************************** */\n") >>outputfile
    printf("/* ********************************** */\n\n") >>outputfile

    # Store the name of the type for later, reset the counter and continue on...
    typelist[lfname]="y"
    ocount=0
    next
}

#
# If we are NOT in the middle of a typedef, then we skip the line...
#
found_typedef == "N" { next }

#
# Otherwise, it's a definition in our struct and we process it...
#
{
    # Process a line...
    #    First, read up to the ; dropping comments

    inputline=""
    inputline = specialappend(inputline, $0)
    gsub(/ +$/, "", inputline)
    while (substr(inputline, length(inputline), 1) != ";") {
        if (getline > 0) {
            lines++
            inputline = specialappend(inputline, $0)
        } else if (substr($1, 1, 1) == "#") {
            continue
        } else {
            printf("/*XMLNOTE no closing ; - assuming, may fail '%s' */\n", inputline)
            inputline=inputline ";"
            break
        }
        gsub(/ +$/, "", inputline)
    }
    while ( (i=index(inputline, "/*")) > 0) {
        j = index(inputline, "*/")
        if (j < i) {
            printf("/*XMLNOTE mis-matched comments, may fail */\n")
            break
        }
        inputline=substr(inputline, 1, i-1) " " substr(inputline, j+2)
    }
    gsub(/ *; *$/, "", inputline)
    if (debug != "") printf("DEBUG: inputline='%s', NF=%d\n", inputline, NF)

    # This gives us an entire C declaration statement.  We'll set $0 to it so we can
    #  use the automatic split and $n variables.
    $0=inputline

    # Process the first field(s) to grab the type.
    #  typetype: We look up the type in the table above until we find the 
    #            first conversion to /*XML typeflag
    #  prioritytypetype is the "u" suffix if needed, again from table above.
    #   This allows us to convert "unsigned int" to n:u while "int" is just "n"
    #  ActualTypeName is just a concat of the words (unsigned long long -> unsignedlonglong)
    #      Used below to see if this is a char xx[n] that we should treat as a string.
    ActualTypeName=""
    typetype=""
    prioritytypetype=""
    for (i=1; i<=NF; i++) {
      typeword=$i
      gsub(/\*$/, "", typeword)
      if (typeword == "struct") { 
          ActualTypeName=ActualTypeName $i
          continue
      } else if (typeword in typewords) {
          ActualTypeName=ActualTypeName $i
          if ( (typetype == "") && (typeword in typewords) && (typewords[typeword] != "") ) {
              typetype = typewords[typeword]
          }
          if ( (prioritytypetype == "") && (typeword in prioritytypewords) ) {
              prioritytypetype = prioritytypewords[typeword]
          }
      } else {
          # Not a type word? Must be the field... stop mucking with types.
          break
      }
    }
    if (typetype == "" ) { typetype = $i }
    if (prioritytypetype != "") { 
        if (index(typetype, ":") == 0) { typetype = typetype ":" }
        typetype = typetype prioritytypetype
    }
    if (debug != "") printf("DEBUG: typetype='%s'\n", typetype)

    # Process the remaining field(s) as variables of the associated type...
    if (debug != "") printf("DEBUG: i=%d, NF=%d\n", i, NF)
    for (; i<=NF; i++) {
        if (debug != "") printf("DEBUG: processing field '%s'\n", $i)
        # Convert **x to x[] 
        if (substr($i, 1, 2) == "**") { 
            $i = substr($i, 3) "[?]" 
            ActualTypeName="forcedarray"
        }
        # If it's got an index value, we need to know for the XMLFOR later on...
        j=index($i, "[")
        if (j>0) {
            field=substr($i, 1, j-1)
            findex=substr($i, j+1)
            gsub(/\]/, "", findex)
        } else {
            field=$i
            findex=""
        }
        # Strip off bit masks, leading *'s, trailing commas
        gsub(/^:[0-9]*/, "", field)
        gsub(/^\*+/, "", field)
        gsub(/\*+$/, "", field)
        gsub(/,$/, "", field)
        if (debug != "") printf("DEBUG: Field %s, type %s\n", field, typetype)

        if (tolower(field) in ignore_types) {
        } else if (field != "") {
            # Store the output data for when we find the }
            outputtype[ocount] = typetype
            outputfield[ocount]= field
            if (findex != "") { 
                if (ActualTypeName ~ /char/) {
                    # char xxx[size]  -- treat as string
                } else {
                    outputindex[ocount] = findex 
                }
            }
            ocount++
        }
    }
    next
}

END {
    datestamp = strftime("%Y-%m-%dT%H:%M:%S", systime() )
##    # Note we explicitly APPEND to the list, in case we have multiple files to process...
##    print "# processstruct.list created " datestamp >>"processstruct.list"
##    print "#    by processstruct.awk "              >>"processstruct.list"
##    print "#    from " ARGV[1]                      >>"processstruct.list"
##    print "#"                                       >>"processstruct.list"

    # Output a list of all the unknown types we've found for xmldump.awk
##    for (i in typelist) {
##        if (i in typewords) {
##            printf("# typewords   %s\n", i) >>"processstruct.list"
##        } else if (i in codewords) {
##            printf("# codewords   %s\n", i) >>"processstruct.list"
##        } else {
##            printf("struct        %s\n", i) >>"processstruct.list"
##        }
##    }
    printf("\n\n/*XMLNOTE %s %s finished */\n\n\n", datestamp, programname)
}