~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to utils/xmldump.awk

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## ntop xml output generator -- tools
 
2
 
 
3
# Written by and copyright (c) 2002, Burton M. Strauss III
 
4
 
 
5
# Distributed as part of ntop, http://www.ntop.org
 
6
 
 
7
 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
8
 #
 
9
 # This program is free software; you can redistribute it and/or modify
 
10
 # it under the terms of the GNU General Public License as published by
 
11
 # the Free Software Foundation; either version 2 of the License, or
 
12
 # (at your option) any later version.
 
13
 #
 
14
 # This program is distributed in the hope that it will be useful,
 
15
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 # GNU General Public License for more details.
 
18
 #
 
19
 # You should have received a copy of the GNU General Public License
 
20
 # along with this program; if not, write to the Free Software Foundation,
 
21
 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
22
 # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
23
 #
 
24
 
 
25
# This tool is part of the ntop xml output system, but it is in fact
 
26
# not normally executed by persons compiling ntop.
 
27
 
 
28
# It is designed for the developers of ntop to create various .inc files
 
29
# used by xmldumpPlugin.c.  It should be run after any changes to globals.h or
 
30
# ntop.h
 
31
 
 
32
# Note that it IS perfectly safe to run, since you should NEVER be editing
 
33
# the generated .inc files by hand.
 
34
 
 
35
#  Usage:   awk -f xmldump.awk xxx.h
 
36
 
 
37
#  This script is designed to extract structured comments from ntop .h files 
 
38
#  to create c code in the .inc files which are the xml (gdome) statements 
 
39
#  for xmldumpPlugin.c to build the DOM structure.
 
40
 
 
41
#  Beware - between this routine and the convenience functions in xmldumpPlugin.c, 
 
42
#  A LOT of complexity is hidden.
 
43
 
 
44
# The "structured comment language":
 
45
 
 
46
#/*XMLNOTE ...whatever... */
 
47
#  --Is a comment, but one that we are indicating is related to the xml stuff.
 
48
#
 
49
#/*XMLSECTIONBEGIN filename parent prefix */
 
50
#  --Causes new file to be created, and to start creating xml statements under
 
51
#    the node [el]parent, pulling data from prefix.whatever
 
52
#
 
53
#    So the typical  /*XMLSECTIONBEGIN xmlmyglobals.inc Globals myGlobals */
 
54
#    starts writing to xmlmyglobals.inc, using elGlobals as the base node and
 
55
#    prefixing variables by myGlobals.   
 
56
#
 
57
#/*XMLSECTIONEND [name] */
 
58
#  --Ends echo to section...
 
59
#
 
60
#/*XMLPARENT parent */
 
61
#  --Causes new parent to be used... keeping name and prefix.
 
62
#
 
63
#/*XMLPREFIX prefix */
 
64
#  --Causes new prefix to be used... keeping name and parent.
 
65
 
 
66
# The processing statement is:
 
67
#
 
68
#/*XML type[:format] item[:itemname] childof[:result] "description" */
 
69
#
 
70
#   Types:
 
71
#           b        noyes
 
72
#           h        hex (0x....)
 
73
#           e        empty
 
74
#           n[:fmt]  numeric
 
75
#           nc:[fmt] numeric constant (e.g. #define) ... NO PREFIX!
 
76
#           s        string
 
77
#           sc       string constant (e.g. #define) ... NO PREFIX!
 
78
#           si       string index (special form of string)
 
79
#           x        ignore
 
80
#           *        special (inline) code
 
81
#
 
82
#/*XMLEXISTS nodename ... */
 
83
#  --Tells script that nodename exists ... loads it into nodes[]
 
84
#
 
85
#/*XMLFOR var initial test [operand] */
 
86
#         2   3       4    5...      NF
 
87
#      
 
88
#       XMLFOR i 0 "a[i] != NULL" ->  for (i=0; a[i] != NULL; i++)
 
89
#       XMLFOR i 0 1              ->  for (i=0; i<1; i++)
 
90
#       XMLFOR i 0 1 <=           ->  for (i=0; i<=1; i++)
 
91
#
 
92
#/*XMLROF */
 
93
#  --creates closing } for XMLFOR
 
94
#
 
95
#/*XMLIF test */
 
96
#/*XMLELSE */
 
97
#/*XMLFI */
 
98
#  -- becomes if (test) { ...
 
99
#             }
 
100
#
 
101
#/*XMLINLINE name   
 
102
#   ... source ... with $x variables substituted ...
 
103
#XML*/
 
104
#
 
105
 
 
106
function insert_nodes(value, tag) {
 
107
     if (debug != "") printf("    /*XMLNOTE insert_node(%s)=%s\n", value, tag)
 
108
     if (value != "") {
 
109
         nodes[value] = tag
 
110
     }
 
111
}
 
112
 
 
113
function process_parent() {
 
114
     if (parent == "root") {
 
115
         parentnodename=parent
 
116
     } else if (parent == "parent") {
 
117
         parentnodename=parent
 
118
     } else {
 
119
         parentnodename="el" toupper(substr(parent, 1, 1)) substr(parent, 2)
 
120
     }
 
121
     if (parent in nodes) {
 
122
     } else {
 
123
         if (debug != "") printf("    /*XMLNOTE Generating node %s, child of root\n", parent)
 
124
         printf("/* *via XMLPARENT********************************Assume root* */\n" \
 
125
                "%s%s = newxmlna(GDOME_ELEMENT_NODE, root, \"%s\");\n\n",
 
126
                indent,
 
127
                parentnodename,
 
128
                parent) >>outputname
 
129
         insert_nodes(parent, "XMLPARENT")
 
130
     }
 
131
}
 
132
 
 
133
function process_prefix() {
 
134
     if (substr(prefix, length(prefix)-1) == "->") {
 
135
         connect=""
 
136
         stripedprefix=substr(prefix, 1, length(prefix)-2)
 
137
     } else if (substr(prefix, length(prefix)) == ".") {
 
138
         connect=""
 
139
         stripedprefix=substr(prefix, 1, length(prefix)-1)
 
140
         sub(/\[i.*\]/, "", stripedprefix);
 
141
     } else {
 
142
         if (prefix != "myGlobals") {
 
143
             connect="->"
 
144
         } else {
 
145
             connect="."
 
146
         }
 
147
         stripedprefix=prefix
 
148
     }
 
149
}
 
150
 
 
151
BEGIN {
 
152
    # export debug=anything to turn on debug
 
153
    debug=ENVIRON["XMLDUMPDEBUG"]
 
154
 
 
155
    # Start off dumping to the bit bucket.
 
156
    outputname="/dev/null"
 
157
 
 
158
    print "xmldump.awk running..."
 
159
    lines=0
 
160
 
 
161
    parent="root"
 
162
 
 
163
    nodes["root"]    = "Default"
 
164
    nodes["parent"]  = "Default"
 
165
 
 
166
    workNodePrefix="elWork"
 
167
 
 
168
    #
 
169
    # Preset the stuff we "know"...
 
170
    #
 
171
    type_macroname_conversion["b"]              = "newxml_simplenoyes"
 
172
    type_macroname_conversion["h"]              = "newxml_simplehex"
 
173
    type_macroname_conversion["e"]              = "newxml_empty"
 
174
    type_macroname_conversion["n"]              = "newxml_simplenumeric"
 
175
    type_macroname_conversion["nc"]             = "newxml_namednumeric"
 
176
    type_macroname_conversion["s"]              = "newxml_simplestring"
 
177
    type_macroname_conversion["sc"]             = "newxml_namedstring"
 
178
    type_macroname_conversion["si"]             = "newxml_simplestringindex"
 
179
    type_macroname_conversion["*"]              = "special-inline-code"
 
180
    type_macroname_conversion["SPECIAL1"]       = "special-output-coding"
 
181
 
 
182
    #
 
183
    # Special conversions...
 
184
    #
 
185
    type_macroname_conversion["tcp_seq"]        = "newxml_namednumeric"
 
186
    macroformat["tcp_seq"]                      = "u"
 
187
 
 
188
    # Skip the (item, value) pair on the call, also 
 
189
    #   Force an empty :result to itemname
 
190
    type_nonvalue_node["e"]                     = "yes"
 
191
 
 
192
    # Don't prepend the "prefix" from XMLSECTIONBEGIN / XMLPREFIX on these 
 
193
    # Generate <name constantname=value> instead of the <item= value= >
 
194
    type_constant_item["nc"]                    = "yes"
 
195
    type_constant_item["sc"]                    = "yes"
 
196
 
 
197
    # These append and (index, value) set to the parameters (value is the index to append)
 
198
    ##type_appendindex["hosttraffic"]             ="j"
 
199
    ##type_appendindex["si"]                      ="i"
 
200
 
 
201
    #
 
202
    # Load a list of stuff defined in xmldumpPlugin.c
 
203
    #
 
204
    system("grep '^ *#define *newxml_' xmldumpPlugin.c >/tmp/xmldump1")
 
205
    while (getline < "/tmp/xmldump1" > 0) {
 
206
        i=index($2, "(")
 
207
        macro=substr($2, 1, i-1)
 
208
        gsub(/newxml_/, "", macro)
 
209
        type_macroname_conversion[macro]    = "newxml_" macro
 
210
    }
 
211
    system("grep '^GdomeElement \\* newxml_' xmldumpPlugin.c > /tmp/xmldump2")
 
212
    while (getline < "/tmp/xmldump2" > 0) {
 
213
        i=index($3, "(")
 
214
        functionname=substr($3, 1, i-1)
 
215
        gsub(/newxml_/, "", functionname)
 
216
        type_macroname_conversion[functionname]    = "newxml_" functionname
 
217
    }
 
218
 
 
219
    system("rm -f /tmp/xmldump[12]")
 
220
 
 
221
    indent = "    "
 
222
    inlineline = 0
 
223
}
 
224
 
 
225
# Count input lines...
 
226
{
 
227
    lines++
 
228
}
 
229
 
 
230
$1 == "/*XMLNOTE" {
 
231
#/*XMLNOTE ... whatever ... */
 
232
     # This is just a note to ourselves ... ignore it...
 
233
     if (debug != "") print "    " $0
 
234
     next
 
235
}
 
236
 
 
237
$1 == "/*XMLSECTIONBEGIN" {
 
238
     # Handle (close) prior section...
 
239
     if ( (outputname != "") && (outputname != "/dev/null") ) { 
 
240
         if (debug != "") printf("    /*XMLNOTE Closing %s */\n", outputname)
 
241
         print "" >>outputname
 
242
         close(outputname) 
 
243
     }
 
244
 
 
245
 
 
246
     sectionhead=""
 
247
     outputname = $2
 
248
     if (outputname in sections) {
 
249
         if (debug != "") print "    /*XMLNOTE Resuming " outputname " */"
 
250
     } else {
 
251
         sectionhead="y"
 
252
         if (debug != "") print "    /*XMLNOTE Begining " outputname " */"
 
253
         sections[outputname]="Yes"
 
254
         printf("/* Created by xmldump.awk\n" \
 
255
                " *\n" \
 
256
                " * part of and licensed the same as ntop, http://www.ntop.org\n" \
 
257
                " *\n" \
 
258
                " * WARNING: Changes made here will be lost the next time this\n" \
 
259
                " * file is recreated, which can happen automatically during\n" \
 
260
                " * a 'make'.  Y'all been warned, now!\n" \
 
261
                " *\n" \
 
262
                " */\n\n\n") >outputname
 
263
     }
 
264
 
 
265
     parent=$3
 
266
     process_parent()
 
267
 
 
268
     prefix     = $4
 
269
     process_prefix()
 
270
 
 
271
     if (sectionhead == "y") {
 
272
         if (stripedprefix != "myGlobals") {
 
273
             printf("    if (%s == NULL) { return NULL; };\n", stripedprefix) >>outputname
 
274
         }
 
275
         printf("    if (%s == NULL) { return NULL; };\n\n\n", parentnodename) >>outputname
 
276
     }
 
277
 
 
278
     next
 
279
}
 
280
 
 
281
$1 == "/*XMLPARENT" {
 
282
 
 
283
     parent=$2
 
284
     process_parent()
 
285
     next
 
286
}
 
287
 
 
288
$1 == "/*XMLPREFIX" {
 
289
     prefix     = $2
 
290
     process_prefix()
 
291
     next
 
292
}
 
293
 
 
294
$1 == "/*XMLSECTIONEND" {
 
295
     if (outputname != "") { 
 
296
         if (debug != "") print "    /*XMLNODE Suspending " outputname " */"
 
297
         print "" >>outputname
 
298
         close(outputname) 
 
299
     }
 
300
     outputname = "/dev/null"
 
301
     next
 
302
}
 
303
 
 
304
$1 == "/*XMLEXISTS" {
 
305
     for (i=2; i<NF; i++) {
 
306
         if ($i == "*/") { break }
 
307
         if ( ($i != "") && ($i != "root") && ($1 != "parent") ) {
 
308
             if (debug != "") print "    /*XMLNOTE XMLEXISTS - marking node " $i " */"
 
309
              insert_nodes($i, "XMLEXISTS")
 
310
         }
 
311
     }
 
312
     nodename=""
 
313
     next
 
314
}
 
315
 
 
316
$1 == "/*XMLINLINE" {
 
317
     inlinename = $2
 
318
     inlinecode_start[inlinename] = inlineline
 
319
     while (getline > 0) {
 
320
         if ($1 == "XML*/") { break }
 
321
         inlinecode[inlineline++] = $0
 
322
     }
 
323
     inlinecode_end[inlinename] = inlineline
 
324
}
 
325
 
 
326
$1 == "/*XMLFOR" {
 
327
 
 
328
     var  = $2
 
329
 
 
330
     init = $2 "=" $3
 
331
 
 
332
     if (substr($4, 1, 1) == "\"") {
 
333
         compare = $4
 
334
         for (i=5; i<NF; i++) {
 
335
             compare = compare " " $i
 
336
         }
 
337
         gsub(/\"/, "", compare)
 
338
     } else if ($5 == "*/") {
 
339
         compare = $2 "<" $4
 
340
     } else {
 
341
         compare = $2 " " $5 " " $4
 
342
     }
 
343
 
 
344
     increment = $2 "++"
 
345
 
 
346
     printf("%s{ int %s;\n%s  for (%s; %s; %s) {\n", indent, var, indent, init, compare, increment) >>outputname
 
347
     indent = indent "\t"
 
348
     next
 
349
}
 
350
 
 
351
(($1 == "/*XMLROF") || ($1 == "/*XMLROF*/")) {
 
352
     indent = substr(indent, 1, length(indent)-1)
 
353
     printf("%s} }\n\n", indent) >>outputname
 
354
     next
 
355
}
 
356
 
 
357
$1 == "/*XMLIF" {
 
358
     test = $2
 
359
     for (i=3; i<=NF; i++) {
 
360
         if ($i == "*/") { 
 
361
             i=NF+1
 
362
         } else {
 
363
             test = test " " $i
 
364
         }
 
365
     }
 
366
 
 
367
     printf("%sif (%s) {\n", indent, test) >>outputname
 
368
     indent = indent "\t"
 
369
     next
 
370
}
 
371
 
 
372
(($1 == "/*XMLELSE") || ($1 == "/*XMLELSE*/")) {
 
373
     printf("%s} else {\n\n", indent) >>outputname
 
374
     next
 
375
}
 
376
 
 
377
(($1 == "/*XMLFI") || ($1 == "/*XMLFI*/")) {
 
378
     indent = substr(indent, 1, length(indent)-1)
 
379
     printf("%s}\n\n", indent) >>outputname
 
380
     next
 
381
}
 
382
 
 
383
substr($1, 1, 3) == "#if" {
 
384
     print "\n" $0 >>outputname
 
385
     next
 
386
}
 
387
 
 
388
substr($1, 1, 5) == "#else" {
 
389
     print $0 >>outputname
 
390
     next
 
391
}
 
392
 
 
393
substr($1, 1, 5) == "#elif" {
 
394
     print $0 >>outputname
 
395
     next
 
396
}
 
397
 
 
398
substr($1, 1, 6) == "#endif" {
 
399
     print $0 "\n" >>outputname
 
400
     next
 
401
}
 
402
 
 
403
$1 == "/*XML" {
 
404
#/*XML type[:format] item[!]itemname  childof[:result] "description" */
 
405
#$1    2             3                4                5...
 
406
     # Presets...
 
407
     gdomenode=""
 
408
 
 
409
     # Process fields...
 
410
     if ( (NF < 5) && ($2 != "x") && ($2 != "*") ) {
 
411
         printf("\n\n ERROR -- missing fields on following line:\n%5d. %s\n\n", lines, $0)
 
412
         next
 
413
     }
 
414
 
 
415
     if ((i=index($2, ":")) > 0) {
 
416
         typeflag = substr($2, 1, i-1)
 
417
         format   = substr($2, i+1)
 
418
     } else {
 
419
         typeflag = $2
 
420
         format   = ""
 
421
     }
 
422
 
 
423
     if ((i=index($3, "!")) > 0) {
 
424
         item    = substr($3, 1, i-1)
 
425
         itemname= substr($3, i+1)
 
426
     } else {
 
427
         item    = $3
 
428
         itemname= item
 
429
     }
 
430
 
 
431
     if ((i=index($4, ":")) > 0) {
 
432
         childof = substr($4, 1, i-1)
 
433
         result  = substr($4, i+1)
 
434
     } else {
 
435
         childof = $4
 
436
         result  = ""
 
437
     }
 
438
 
 
439
     description=$5
 
440
     for (i=6; i<NF; i++) {
 
441
         if ($i != "*/") {
 
442
             description = description " " $i
 
443
         }
 
444
     }
 
445
 
 
446
 # All parms read...
 
447
     f1 = typeflag (format == "" ? "" : ":") format 
 
448
     f2 = item (item == itemname ? "" : "!" itemname)
 
449
     f3 = childof  (result == "" ? "" : ":") result 
 
450
     processedline = sprintf("/*XML %-15s %-20s %-15s %s */", 
 
451
                                    f1, 
 
452
                                          f2,
 
453
                                                f3,
 
454
                                                      description)
 
455
     print indent processedline >>outputname
 
456
     if (debug > "1") { printf("    /*XMLNOTE input line is '%s' */\n", $0)
 
457
                        printf("    /*XMLNOTE processed as  '%s' */\n", processedline)
 
458
                        printf("\n    /*XMLNOTE RAW values: */\n")
 
459
                        printf("        /*XMLNOTE childof.........'%s' */\n", childof)
 
460
                        printf("        /*XMLNOTE description.....'%s' */\n", description)
 
461
                        printf("        /*XMLNOTE format..........'%s' */\n", format)
 
462
                        printf("        /*XMLNOTE item............'%s' */\n", item)
 
463
                        printf("        /*XMLNOTE itemname........'%s' */\n", itemname)
 
464
                        printf("        /*XMLNOTE result..........'%s' */\n", result)
 
465
                        printf("        /*XMLNOTE typeflag........'%s' */\n", typeflag)
 
466
     }
 
467
 
 
468
 # Errors and other reasons to ignore this...
 
469
     if (typeflag == "x") { 
 
470
         if (debug != "") printf("    /*XMLNOTE Ignored: %s */\n", item)
 
471
         next
 
472
     }
 
473
     if (result == "root") {
 
474
         if (debug != "") printf("    /*XMLNOTE ERROR resetting root node */\n")
 
475
         next
 
476
     } else if (result == "parent") {
 
477
         if (debug != "") printf("    /*XMLNOTE ERROR resetting parent node */\n")
 
478
         next
 
479
     }
 
480
 
 
481
 # Convenience conversions...
 
482
     if (typeflag == "char*") {
 
483
         typeflag="s"
 
484
     }
 
485
 
 
486
 # Cleanup, Tests and priority items...
 
487
     typeprefix=""
 
488
     if (typeflag == "*") {
 
489
     } else {
 
490
         while (substr(typeflag, 1, 1) == "*") {
 
491
             typeprefix=typeprefix "* "
 
492
             typeflag = substr(typeflag, 2)
 
493
         }
 
494
         if (substr(typeflag, 1, 1) == "&") {
 
495
             typeprefix=typeprefix "&"
 
496
             typeflag = substr(typeflag, 2)
 
497
         }
 
498
     }
 
499
     if (debug > "1") { printf("\n    /*XMLNOTE PROCESSED values: */\n")
 
500
                        printf("        /*XMLNOTE typeflag........'%s' */\n", typeflag)
 
501
                        printf("        /*XMLNOTE typeprefix......'%s' */\n", typeprefix)
 
502
     }
 
503
     if (item == ".") { 
 
504
         item = ""
 
505
         if (debug > "1") { printf("        /*XMLNOTE item............'%s' */\n", item) }
 
506
     }
 
507
     if (childof  == ".") { 
 
508
         childof  = ""
 
509
         if (debug > "1") { printf("        /*XMLNOTE childof.........'%s' */\n", childof) }
 
510
     }
 
511
     if (description == "*/") {
 
512
         description = ""
 
513
         if (debug > "1") { printf("        /*XMLNOTE description.....'%s' */\n", description) }
 
514
     }
 
515
 
 
516
 # Derived stuff...
 
517
 
 
518
     # What about the "item"? Just to clarify...
 
519
     #   item is the way the user named it (e.g. xyz) -- we don't use that past here
 
520
     #   itemname is how to refer to it in xml (xyz)
 
521
     #      Setting those is handled above, like all the other field splits
 
522
     #   itemref is how to refer to it in c (myGlobals.xyz)
 
523
     #   fieldname is the stripped version of itemname
 
524
 
 
525
     #   Cleanup itemname, stripping indexes, pointers, etc.
 
526
     while ((i=index(itemname, ".")) > 0) {
 
527
         itemname=substr(itemname, i+1)
 
528
     }
 
529
     while ((i=index(itemname, "->")) > 0) {
 
530
         itemname=substr(itemname, i+2)
 
531
     }
 
532
     if ((i=index(itemname, "[")) > 0) {
 
533
         itemname=substr(itemname, 1, i-1)
 
534
     }
 
535
     if (debug > "1") { printf("        /*XMLNOTE itemname........'%s' (xml) */\n", itemname) }
 
536
 
 
537
     fieldname=itemname
 
538
     #   Cleanup fieldname, stripping indexes, pointers, etc.
 
539
     while ((i=index(fieldname, ".")) > 0) {
 
540
         fieldname=substr(fieldname, i+1)
 
541
     }
 
542
     while ((i=index(fieldname, "->")) > 0) {
 
543
         fieldname=substr(fieldname, i+2)
 
544
     }
 
545
     while ((i=index(fieldname, "&")) > 0) {
 
546
         fieldname=substr(fieldname, i+1)
 
547
     }
 
548
     if ((i=index(fieldname, "[")) > 0) {
 
549
         fieldname=substr(fieldname, 1, i-1)
 
550
     }
 
551
     if (debug > "1") { printf("        /*XMLNOTE fieldname.......'%s' (stripped) */\n", fieldname) }
 
552
 
 
553
     #   Set itemref
 
554
     if (typeflag in type_constant_item) {
 
555
         itemref=item
 
556
     } else {
 
557
         if ( (substr(item, 1, 1) == ".") || 
 
558
              (substr(item, 1, 2) == "->") ) {
 
559
             itemref= prefix item
 
560
         } else if (substr(item, 1, 1) == "&") {
 
561
             item=substr(item, 2)
 
562
             itemref= "&" prefix connect item
 
563
#         } else if (prefix == "myGlobals") {
 
564
#             itemref= "&" prefix connect item
 
565
         } else {
 
566
             itemref= prefix connect item
 
567
         }
 
568
     }
 
569
     if (debug > "1") { printf("        /*XMLNOTE itemref.........'%s' (c) */\n", itemref) }
 
570
 
 
571
     if (childof == "root") {
 
572
         childnodename="root"
 
573
     } else if (childof == "parent") {
 
574
         childnodename="parent"
 
575
     } else if (childof != "") {
 
576
         childnodename="el" toupper(substr(childof, 1, 1)) substr(childof, 2)
 
577
     }
 
578
     if (debug > "1") { printf("        /*XMLNOTE childnodename...'%s' */\n", childnodename) }
 
579
 
 
580
     makechildnode="N"
 
581
     if (substr(childnodename, 1, length(workNodePrefix)) != workNodePrefix) {
 
582
         # Child, not elWorkxxx, test if the child node exists...
 
583
         if (childnodename in nodes) {
 
584
         } else {
 
585
             # no result and childof doesn't exist ... force it to be set...
 
586
             makechildnode="Y"
 
587
         }
 
588
     }
 
589
     if (debug > "1") { printf("        /*XMLNOTE makechildnode...'%s' */\n", makechildnode) }
 
590
 
 
591
     if ( (typeflag in type_nonvalue_node) && (result == "") ) {
 
592
         result=fieldname
 
593
         if (debug > "1") { printf("        /*XMLNOTE result..........'%s' - FORCED (empty) */\n", result) }
 
594
     }
 
595
     if (result != "") {
 
596
         result="el" toupper(substr(result, 1, 1)) substr(result, 2)
 
597
         if (debug > "1") { printf("        /*XMLNOTE result..........'%s' */\n", result) }
 
598
     }
 
599
 
 
600
     if ((i=index(item, "[")) > 0) {
 
601
         j=index(item, "]")
 
602
         indexname=substr(item, i+1, j-i-1)
 
603
     } else {
 
604
         indexname=""
 
605
     }
 
606
     if (debug > "1") { printf("        /*XMLNOTE indexname.......'%s' */\n", indexname) }
 
607
 
 
608
     if (typeflag in type_macroname_conversion) { 
 
609
         macro=type_macroname_conversion[typeflag]
 
610
         if (typeflag in macroappend) {
 
611
             macroappendvalue=macroappend[typeflag]
 
612
         } else {
 
613
             macroappendvalue=""
 
614
         }
 
615
         if (typeflag in macroformat) {
 
616
             format=macroformat[typeflag]
 
617
         }
 
618
     } else if (typeflag in inlinecode_start) {
 
619
     } else {
 
620
         printf("\n\n ERROR -- type flag '%s' is unknown on following line:\n%5d. %s\n\n", 
 
621
                typeflag, 
 
622
                lines, 
 
623
                $0)
 
624
         next
 
625
     }
 
626
 
 
627
     if ( ( (typeflag == "n") || (typeflag == "nc") ) && (format == "") ) {
 
628
         format="d"
 
629
         if (debug > "1") printf("      /*XMLNOTE format set to %s */\n", format)
 
630
     }
 
631
     if (debug > "1") { printf("        /*XMLNOTE macro...........'%s' */\n", macro)
 
632
                        printf("        /*XMLNOTE ..appendvalue...'%s' */\n", macroappendvalue)
 
633
                        printf("        /*XMLNOTE format..........'%s' */\n", format) }
 
634
 
 
635
     # To set the result AND use simplenumeric we can't just use the xmldumpPlugin.c macro...
 
636
         # Since this is dependent on the xmldumpPlugin.c macros, we hardcode their names here...
 
637
     buftext=""
 
638
     if ( (macro=="newxml_simplenumeric") && (result != "") ) {
 
639
         if (debug != "") printf("      /*XMLNOTE numeric+result - macroname was %s, set to newxml_simplestring */\n", macro)
 
640
         macro="newxml_simplestring"
 
641
         buftext=sprintf("if (snprintf(buf, sizeof(buf), \"%%%s\", %s) < 0) BufferTooShort();", 
 
642
                         format, itemref)
 
643
         format=""
 
644
         itemref="buf"
 
645
         typeflag="s"
 
646
     } else if ( (macro=="newxml_namednumeric") && (result != "") ) {
 
647
         if (debug != "") printf("      /*XMLNOTE numeric+result - macroname was %s, set to newxml_simplestring */\n", macro)
 
648
         typeflag="SPECIAL1"
 
649
     }
 
650
     if (debug > "1") { printf("        /*XMLNOTE macro...........'%s' */\n", macro)
 
651
                        printf("        /*XMLNOTE itemref.........'%s' */\n", itemref)
 
652
                        printf("        /*XMLNOTE buftext.........'%s' */\n", buftext)
 
653
                        printf("        /*XMLNOTE typeflag........'%s' */\n", typeflag)
 
654
                        printf("        /*XMLNOTE format..........'%s' */\n", format) }
 
655
 
 
656
 # Process...
 
657
 
 
658
     #  1. Create the child node if necessary...
 
659
     if (makechildnode == "Y") {
 
660
         if (debug != "") printf("    /*XMLNOTE (auto) generating node %s, child of %s */\n",
 
661
                                 childnodename, parentnodename)
 
662
         printf("%s/* *****************************************Auto create node** */\n" \
 
663
                "%s%s = newxmlna(GDOME_ELEMENT_NODE, %s, \"%s\");\n", 
 
664
                indent,
 
665
                indent, 
 
666
                childnodename, 
 
667
                parentnodename, 
 
668
                childof) >>outputname
 
669
         insert_nodes(childnodename, "ChildAutoCreate")
 
670
     }
 
671
 
 
672
     # 2. Handle inline code...
 
673
              #  Just copy all lines until XML*/ found
 
674
     if (typeflag == "*") {
 
675
         if (debug != "") printf("    /*XMLNOTE Processing (inline) %s, %s */\n", item, description)
 
676
         printf("\n%s/* %scopied from %s at line %d */\n", 
 
677
                indent, 
 
678
                description == "\"\"" ? "" : description " ", 
 
679
                ARGV[1],
 
680
                lines) >>outputname
 
681
         while (getline > 0) {
 
682
             if ($1 == "XML*/") { break }
 
683
             print indent $0 >>outputname
 
684
         }
 
685
         printf("%s/* end copy from %s */\n\n", indent, ARGV[1]) >>outputname
 
686
         next
 
687
     }
 
688
 
 
689
     # 2a. Handle XMLINLINE field
 
690
     if (typeflag in inlinecode_start) {
 
691
         if (debug != "") printf("    /*XMLNOTE Processing (xmlinline) %s, %s */\n", item, description)
 
692
         printf("\n%s/*i %sgenerated from %s at line %d */\n", 
 
693
                indent, 
 
694
                description == "\"\"" ? "" : description " ", 
 
695
                ARGV[1],
 
696
                lines) >>outputname
 
697
         for (i=inlinecode_start[typeflag]; i<inlinecode_end[typeflag]; i++) {
 
698
             otext = inlinecode[i]
 
699
             j=index(otext, "$")
 
700
             while (j>0) {
 
701
                 f=substr(otext, j+1, 1)
 
702
                 stext = ""
 
703
                 if (f == "P") {
 
704
                   stext = stripedprefix connect
 
705
                 } else if (f == "X") {
 
706
                     if (description != "\"\"") {
 
707
                         stext = ",\n" indent "/*i*/            \"description\", " description " " 
 
708
                     } else {
 
709
                         stext = ""
 
710
                     }
 
711
                 } else if (f == "2") {
 
712
                   stext = typeflag
 
713
                 } else if (f == "3") {
 
714
                   stext = item
 
715
                 } else if (f == "4") {
 
716
                   stext = childnodename
 
717
                 } else if (f == "5") {
 
718
                   stext = description
 
719
                 }
 
720
                 otext = substr(otext, 1, j-1) stext substr(otext, j+2)
 
721
                 j=index(otext, "$")
 
722
             }
 
723
             if (substr(otext, 1, 1) == "#") {
 
724
                 print otext >>outputname
 
725
             } else {
 
726
                 print indent "/*i*/ " otext >>outputname
 
727
             }
 
728
         }
 
729
         next
 
730
     }
 
731
 
 
732
#
 
733
#                                       OUTPUT OUTPUT OUTPUT
 
734
#
 
735
 
 
736
     # 3. Let's output the code...
 
737
     # 3a. SPECIAL1 --
 
738
     if (typeflag == "SPECIAL1") {
 
739
         # Special case for simplenumeric with result
 
740
         if (debug != "") printf("    /*XMLNOTE SPECIAL1 */\n")
 
741
         printf("%sif (snprintf(buf, sizeof(buf), \"%%%s\", %s) < 0) BufferTooShort();\n",
 
742
                        indent, format, itemref) >> outputname
 
743
         printf("%s%s = newxml(GDOME_ELEMENT_NODE, %s, \"%s\", \n" \
 
744
                "%s                \"%s\", buf,\n" \
 
745
                "%s                \"description\", %s);\n\n\n",
 
746
                indent, result, childnodename, itemname,
 
747
                indent, fieldname,
 
748
                indent, description) >>outputname
 
749
         insert_nodes(result, "special1set")
 
750
         next
 
751
     }
 
752
 
 
753
     # 3z. normal --
 
754
 
 
755
     # NULL checks on pointers...
 
756
     if ( (typeflag != "e") && (typeprefix ~ /\*/) ) {
 
757
         itemrefroot=itemref
 
758
         i=index(itemrefroot, "[")
 
759
         if (i > 0) {
 
760
             itemrefroot=substr(itemrefroot, 1, i-1)
 
761
         }
 
762
         if (debug > "1") { printf("        /*XMLNOTE itemrefroot.....'%s' */\n", itemrefroot) }
 
763
 
 
764
         workprefix=typeprefix
 
765
         testprefix=""
 
766
 
 
767
         while (workprefix != "") {
 
768
             if (debug > "1") { printf("        /*XMLNOTE workprefix......'%s' */\n", workprefix)
 
769
                                printf("        /*XMLNOTE testprefix......'%s' */\n", testprefix) }
 
770
             printf("%sif (%s%s != NULL) {\n", indent, testprefix, itemrefroot) >>outputname
 
771
             workprefix=substr(workprefix, 3)
 
772
             testprefix=testprefix "* "
 
773
             indent=indent "  "
 
774
             if (debug > "1") { printf("        /*XMLNOTE indent..........'%s' */\n", indent) }
 
775
         }
 
776
     }
 
777
   # Normal case, build it up, piece by piece... (Note no \n 's in most pieces)
 
778
   #  If there is a sprintf to buf...
 
779
     if (buftext != "") {
 
780
         printf("%s%s\n", indent, buftext) >>outputname
 
781
     }
 
782
   #  Start the macro/function... handling it if there is a result
 
783
     # indent...
 
784
       printf("%s", indent) >>outputname
 
785
     # A result?
 
786
       if (result != "") {
 
787
           printf("%s = ", result) >>outputname
 
788
       }
 
789
     # Now the function
 
790
       printf("%s(%s%s,\n",
 
791
              macro, (gdomenode != "" ? gdomenode ", " : ""), childnodename) >>outputname
 
792
       printf("%s                        %s%s%s,\n",
 
793
              indent, 
 
794
              fieldname == "nodename" ? "" : "\"",
 
795
              fieldname,
 
796
              fieldname == "nodename" ? "" : "\"") >>outputname
 
797
 
 
798
     # Skip the "value" piece for non-valued nodes
 
799
        if (typeflag in type_nonvalue_node) {
 
800
             if (debug != "") { printf("        /*XMLNOTE typeflag '%s' in type_nonvalue_node */\n", typeflag) }
 
801
        } else {
 
802
            if ( (typeprefix == "") || (typeprefix == "&") ) {
 
803
                workprefix = typeprefix
 
804
            } else {
 
805
                workprefix = substr(typeprefix, 3)
 
806
            }
 
807
            if (debug > "1") { printf("        /*XMLNOTE workprefix......'%s' */\n", workprefix) }
 
808
            printf("%s                        %s%s%s%s,\n",
 
809
                   indent, 
 
810
                   workprefix,
 
811
                   workprefix == "" ? "" : "(",
 
812
                   itemref,
 
813
                   workprefix == "" ? "" : ")" ) >>outputname
 
814
        }
 
815
 
 
816
     # Description
 
817
       printf("%s                        %s",
 
818
              indent, description) >>outputname
 
819
 
 
820
     #  Post description parameters (constants, formats, index)
 
821
       if (type_macroname_conversion[typeflag] == "newxml_namedstring") {
 
822
            printf(",\n%s                        \"%s\"", 
 
823
                   indent, itemname) >>outputname
 
824
       } else if (type_macroname_conversion[typeflag] == "newxml_namednumeric") {
 
825
            printf(",\n%s                        \"%%%s\"",
 
826
                   indent, format != "" ? format : "d") >>outputname
 
827
            printf(",\n%s                        \"%s\"", 
 
828
                   indent, itemname) >>outputname
 
829
       } else if (format != "" ) {
 
830
            printf(",\n%s                        \"%%%s\"",
 
831
                   indent, format) >>outputname
 
832
       ##} else if (typeflag in type_appendindex) {
 
833
       ##     printf(",\n%s                        %s", 
 
834
       ##            indent, type_appendindex[typeflag]) >>outputname
 
835
       }
 
836
 
 
837
     # Finish it
 
838
       printf(");\n\n") >>outputname
 
839
 
 
840
   # NULL checks on pointers...
 
841
     if ( (typeflag != "e") && (typeprefix ~ /\*/) ) {
 
842
         workprefix=typeprefix
 
843
         while (workprefix != "") {
 
844
             indent=substr(indent, 1, length(indent) - 2)
 
845
             workprefix=substr(workprefix, 3)
 
846
             printf("%s}\n", indent) >>outputname
 
847
         }
 
848
     }
 
849
 
 
850
     # Remember
 
851
     insert_nodes(result, "set")
 
852
 
 
853
     next
 
854
}
 
855
 
 
856
{ next }
 
857
 
 
858
END {
 
859
    if (outputname != "") { 
 
860
        print "" >>outputname
 
861
        close(outputname) 
 
862
    }
 
863
    if (debug != "") {
 
864
        printf("/*XMLNOTE recap\n")
 
865
        for (i in nodes) {
 
866
            printf(" *  %-30s %s\n", i, nodes[i])
 
867
        }
 
868
        printf(" */\n")
 
869
 
 
870
        printf("/*XMLNOTE type_macroname_conversion\n")
 
871
        for (i in type_macroname_conversion) {
 
872
            printf(" *  %-30s %s\n", i, type_macroname_conversion[i])
 
873
        }
 
874
        printf(" */\n")
 
875
 
 
876
        printf("/*XMLNOTE inline\n")
 
877
        for (i in inlinecode_start) {
 
878
            printf(" *  %-30s\n", i)
 
879
            for (j=inlinecode_start[i]; j<inlinecode_end[i]; j++) {
 
880
                printf(" *      %3d %s\n", j-inlinecode_start[i], inlinecode[j])
 
881
            }
 
882
            printf(" *\n")
 
883
        }
 
884
        printf(" */\n")
 
885
    }
 
886
    print "xmldump.awk finished!"
 
887
}