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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
|
## 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 developers of ntop to create various .inc files
# used by xmldumpPlugin.c. It should be run after any changes to globals.h or
# ntop.h
# Note that it IS perfectly safe to run, since you should NEVER be editing
# the generated .inc files by hand.
# Usage: awk -f xmldump.awk xxx.h
# This script is designed to extract structured comments from ntop .h files
# to create c code in the .inc files which are the xml (gdome) statements
# for xmldumpPlugin.c to build the DOM structure.
# Beware - between this routine and the convenience functions in xmldumpPlugin.c,
# A LOT of complexity is hidden.
# The "structured comment language":
#/*XMLNOTE ...whatever... */
# --Is a comment, but one that we are indicating is related to the xml stuff.
#
#/*XMLSTRUCT structurename [additional vars] */
# --Causes both a forward declaration and normal code block to be generated
#/*XMLDEFINEBEGIN filename */
#/*XMLDEFINE name value */
#/*XMLDEFINEEND */
# --Creates a file with #define lines
#/*XMLSECTIONBEGIN filename parent prefix extrabufs */
# --Causes new file to be created, and to start creating xml statements under
# the node [el]parent, pulling data from prefix.whatever
#
# So the typical /*XMLSECTIONBEGIN xmlmyglobals.inc Globals myGlobals */
# starts writing to xmlmyglobals.inc, using elGlobals as the base node and
# prefixing variables by myGlobals.
#
#/*XMLSECTIONEND [name] */
# --Ends echo to section...
#
#/*XMLPARENT parent */
# --Causes new parent to be used... keeping name and prefix.
#
#/*XMLPREFIX prefix */
# --Causes new prefix to be used... keeping name and parent.
# The processing statement is:
#
#/*XML type[:format] item[:itemname] childof[:result] "description" */
#
# Types:
# b noyes
# h hex (0x....)
# e empty
# n[:fmt] numeric
# nc:[fmt] numeric constant (e.g. #define) ... NO PREFIX!
# s string
# sc string constant (e.g. #define) ... NO PREFIX!
# si string index (special form of string)
# x ignore
# * special (inline) code
#
#/*XMLEXISTS nodename ... */
# --Tells script that nodename exists ... loads it into nodes[]
#
#/*XMLFOR var initial test [operand] */
# 2 3 4 5... NF
#
# XMLFOR i 0 "a[i] != NULL" -> for (i=0; a[i] != NULL; i++)
# XMLFOR i 0 1 -> for (i=0; i<1; i++)
# XMLFOR i 0 1 <= -> for (i=0; i<=1; i++)
#
#/*XMLROF */
# --creates closing } for XMLFOR
#
#/*XMLIF test */
#/*XMLELSE */
#/*XMLFI */
# -- becomes if (test) { ...
# }
#
#/*XMLSWITCH value */
#/*XMLCASE value */
#/*XMLHCTIWS */
# -- becomes switch (value) {
# case value: ...; break;
# }
#
#/*XMLINLINE name
# ... source ... with $x variables substituted ...
#XML*/
#
#/*XMLLINKEDLIST type name base link parent
# ... follows a linked list creating entries of 'type'
#
function insert_nodes(value, tag) {
if (debug != "") printf(" /*XMLNOTE insert_node(%s)=%s\n", value, tag)
if (value != "") {
nodes[value] = tag
}
}
function process_parent() {
if (parent == "root") {
parentnodename=parent
} else if (parent == "parent") {
parentnodename=parent
} else {
parentnodename="el" toupper(substr(parent, 1, 1)) substr(parent, 2)
}
if (parent in nodes) {
} else {
if (debug != "") printf(" /*XMLNOTE Generating node %s, child of root\n", parent)
printf("/* *via XMLPARENT********************************Assume root* */\n" \
"%s%s = newxmlna(root, \"%s\");\n\n",
indent,
parentnodename,
parent) >>outputname
insert_nodes(parent, "XMLPARENT")
}
}
function process_prefix() {
if (substr(prefix, length(prefix)-1) == "->") {
connect=""
stripedprefix=substr(prefix, 1, length(prefix)-2)
} else if (substr(prefix, length(prefix)) == ".") {
connect=""
stripedprefix=substr(prefix, 1, length(prefix)-1)
sub(/\[i.*\]/, "", stripedprefix);
} else {
if (prefix != "myGlobals") {
connect="->"
} else {
connect="."
}
stripedprefix=prefix
}
}
BEGIN {
# export debug=anything to turn on debug
debug=ENVIRON["XMLDUMPDEBUG"]
# Start off dumping to the bit bucket.
outputname="/dev/null"
print "xmldump.awk running..."
lines=0
parent="root"
nodes["root"] = "Default"
nodes["parent"] = "Default"
workNodePrefix="elWork"
#
# Preset the stuff we "know"...
#
type_macroname_conversion["b"] = "newxml_simplenoyes"
type_macroname_conversion["h"] = "newxml_simplehex"
type_macroname_conversion["e"] = "newxml_empty"
type_macroname_conversion["n"] = "newxml_simplenumeric"
type_macroname_conversion["nc"] = "newxml_namednumeric"
type_macroname_conversion["f"] = "newxml_simplefloat"
type_macroname_conversion["s"] = "newxml_smartstring"
type_macroname_conversion["sc"] = "newxml_namedstring"
type_macroname_conversion["*"] = "special-inline-code"
type_macroname_conversion["SPECIAL1"] = "special-output-coding"
#
# Special conversions...
#
type_macroname_conversion["tcp_seq"] = "newxml_namednumeric"
macroformat["tcp_seq"] = "u"
# Skip the (item, value) pair on the call, also
# Force an empty :result to itemname
type_nonvalue_node["e"] = "yes"
# Don't prepend the "prefix" from XMLSECTIONBEGIN / XMLPREFIX on these
# Generate <name constantname=value> instead of the <item= value= >
type_constant_item["nc"] = "yes"
type_constant_item["sc"] = "yes"
# These append and (index, value) set to the parameters (value is the index to append)
##type_appendindex["hosttraffic"] ="j"
##type_appendindex["si"] ="i"
#
# Load a list of stuff defined in xmldumpPlugin.c
#
system("grep '^ *#define *newxml_' xmldumpPlugin.c >/tmp/xmldump1")
while (getline < "/tmp/xmldump1" > 0) {
i=index($2, "(")
macro=substr($2, 1, i-1)
gsub(/newxml_/, "", macro)
type_macroname_conversion[macro] = "newxml_" macro
}
system("grep '^GdomeElement \\* newxml_' xmldumpPlugin.c > /tmp/xmldump2")
while (getline < "/tmp/xmldump2" > 0) {
i=index($3, "(")
functionname=substr($3, 1, i-1)
gsub(/newxml_/, "", functionname)
type_macroname_conversion[functionname] = "newxml_" functionname
}
system("rm -f /tmp/xmldump[12]")
printf("/* Created by xmldump.awk\n" \
" *\n" \
" * part of and licensed the same as ntop, http://www.ntop.org\n" \
" *\n" \
" * WARNING: Changes made here will be lost the next time this\n" \
" * file is recreated, which can happen automatically during\n" \
" * a 'make'.\n" \
" *\n" \
" */\n\n\n") > "xml_s_simple_forward.inc"
printf("/* Created by xmldump.awk\n" \
" *\n" \
" * part of and licensed the same as ntop, http://www.ntop.org\n" \
" *\n" \
" * WARNING: Changes made here will be lost the next time this\n" \
" * file is recreated, which can happen automatically during\n" \
" * a 'make'.\n" \
" *\n" \
" */\n\n\n") > "xml_s_simple.inc"
indent = " "
inlineline = 0
}
# Count input lines...
{
lines++
}
$1 == "/*XMLNOTE" {
#/*XMLNOTE ... whatever ... */
# This is just a note to ourselves ... ignore it...
if (debug != "") print " " $0
next
}
$1 == "/*XMLSTRUCT" {
# --Causes both a forward declaration and normal code block to be generated
if (debug != "") print " " $0
structure = $2
sname = tolower($2)
printf("GdomeElement * newxml_%s(GdomeElement * parent,\n" \
" char * nodename,\n" \
" %s * input,\n" \
" char * description);\n\n",
sname,
structure) >> "xml_s_simple_forward.inc"
additionalvars = ""
for (i=3; i<NF; i++) {
additionalvars = additionalvars ", *el" $i
}
printf("GdomeElement * newxml_%s(GdomeElement * parent,\n" \
" char * nodename,\n" \
" %s * input,\n" \
" char * description) {\n\n" \
" GdomeElement *elWork%s;\n" \
" GdomeException exc;\n\n" \
"#if (XMLDUMP_DEBUG >= 3)\n" \
" traceEvent(CONST_TRACE_INFO, \"XMLDUMP_DEBUG: Starting newxml_%s\");\n" \
"#endif\n\n" \
" /* Insert the generated block of code */\n" \
" #include \"xml_s_%s.inc\"\n\n" \
"#if (XMLDUMP_DEBUG >= 3)\n" \
" traceEvent(CONST_TRACE_INFO, \"XMLDUMP_DEBUG: Ending newxml_%s\");\n" \
"#endif\n\n" \
" return elWork;\n" \
"}\n\n",
sname,
structure,
additionalvars,
sname,
sname,
sname) >> "xml_s_simple.inc"
type_macroname_conversion[sname] = "newxml_" sname
next
}
$1 == "/*XMLDEFINEBEGIN" {
# Handle (close) prior section...
if ( (outputname != "") && (outputname != "/dev/null") ) {
if (debug != "") printf(" /*XMLNOTE Closing %s */\n", outputname)
print "" >>outputname
close(outputname)
}
sectionhead=""
outputname = $2
if (outputname in sections) {
if (debug != "") print " /*XMLNOTE Resuming " outputname " */"
} else {
sectionhead="y"
if (debug != "") print " /*XMLNOTE Begining " outputname " */"
sections[outputname]="Yes"
printf("/* Created by xmldump.awk\n" \
" *\n" \
" * part of and licensed the same as ntop, http://www.ntop.org\n" \
" *\n" \
" * WARNING: Changes made here will be lost the next time this\n" \
" * file is recreated, which can happen automatically during\n" \
" * a 'make'. Y'all been warned, now!\n" \
" *\n" \
" */\n\n\n") >outputname
}
next
}
$1 == "/*XMLDEFINE" {
print "#define " $2 " " $3 >>outputname
next
}
$1 == "/*XMLDEFINEEND" {
if (outputname != "") {
if (debug != "") print " /*XMLNODE Suspending " outputname " */"
print "" >>outputname
close(outputname)
}
outputname = "/dev/null"
next
}
$1 == "/*XMLSECTIONBEGIN" {
# Handle (close) prior section...
if ( (outputname != "") && (outputname != "/dev/null") ) {
if (debug != "") printf(" /*XMLNOTE Closing %s */\n", outputname)
print "" >>outputname
close(outputname)
}
sectionhead=""
outputname = $2
if (outputname in sections) {
if (debug != "") print " /*XMLNOTE Resuming " outputname " */"
} else {
sectionhead="y"
if (debug != "") print " /*XMLNOTE Begining " outputname " */"
sections[outputname]="Yes"
printf("/* Created by xmldump.awk\n" \
" *\n" \
" * part of and licensed the same as ntop, http://www.ntop.org\n" \
" *\n" \
" * WARNING: Changes made here will be lost the next time this\n" \
" * file is recreated, which can happen automatically during\n" \
" * a 'make'. Y'all been warned, now!\n" \
" *\n" \
" */\n\n\n") >outputname
}
parent=$3
process_parent()
prefix = $4
process_prefix()
printf("char buf[LEN_GENERAL_WORK_BUFFER]") >>outputname
for(i=5; i<NF; i++) {
printf(",\n %s[LEN_GENERAL_WORK_BUFFER]", $i) >>outputname
}
printf(";\n\n") >>outputname
if (sectionhead == "y") {
if (stripedprefix != "myGlobals") {
printf(" if (%s == NULL) { return NULL; };\n", stripedprefix) >>outputname
}
printf(" if (%s == NULL) { return NULL; };\n\n\n", parentnodename) >>outputname
}
printf(" memset(&buf, 0, sizeof(buf));\n") >>outputname
for(i=5; i<NF; i++) {
printf(" memset(&%s, 0, sizeof(%s));\n", $i, $i) >>outputname
}
printf("\n") >>outputname
next
}
$1 == "/*XMLPARENT" {
parent=$2
process_parent()
next
}
$1 == "/*XMLPREFIX" {
prefix = $2
process_prefix()
next
}
$1 == "/*XMLSECTIONEND" {
if (outputname != "") {
if (debug != "") print " /*XMLNODE Suspending " outputname " */"
print "" >>outputname
close(outputname)
}
outputname = "/dev/null"
next
}
$1 == "/*XMLEXISTS" {
for (i=2; i<NF; i++) {
if ($i == "*/") { break }
if ( ($i != "") && ($i != "root") && ($1 != "parent") ) {
if (debug != "") print " /*XMLNOTE XMLEXISTS - marking node " $i " */"
insert_nodes($i, "XMLEXISTS")
}
}
nodename=""
next
}
$1 == "/*XMLINLINE" {
inlinename = $2
inlinecode_start[inlinename] = inlineline
while (getline > 0) {
if ($1 == "XML*/") { break }
inlinecode[inlineline++] = $0
}
inlinecode_end[inlinename] = inlineline
}
$1 == "/*XMLLINKEDLIST" {
type=$2
name=$3
base=$4
link=$5
parent=$6
printf("%s{ GdomeElement *llWork;\n" \
"%s int indexLL = 0;\n" \
"%s %s *ptr = myGlobals.%s;\n" \
"%s while (ptr != NULL) {\n" \
"%s safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), \"%%d\", indexLL++);\n" \
"%s llWork = newxml(el%s, \"index\", \"value\", buf, \"\");\n" \
"%s newxml_%s(llWork, \"%s\", ptr, \"\");\n" \
"%s ptr = ptr->%s;\n" \
"%s} }\n",
indent,
indent,
indent, type, base,
indent,
indent,
indent, parent,
indent, tolower(type), type,
indent, link,
indent) >>outputname
next
}
# EXPERIMENTAL
$1 == "/*XMLPTR" {
next
}
$1 == "/*XMLSET" {
next
}
$1 == "/*XMLWHILE" {
if (substr($2, 1, 1) == "\"") {
compare = $2
for (i=3; i<NF; i++) {
compare = compare " " $i
}
gsub(/\"/, "", compare)
} else if ($3 == "*/") {
compare = $2 "<" $3
} else {
compare = $2 " " $3 " " $4
}
printf("%swhile (%s) {\n", indent, compare) >>outputname
indent = indent "\t"
next
}
(($1 == "/*XMLELIHE") || ($1 == "/*XMLELIHE*/")) {
indent = substr(indent, 1, length(indent)-1)
printf("%s} }\n\n", indent) >>outputname
next
}
#END EXPERIMENTAL
$1 == "/*XMLFOR" {
var = $2
init = $2 "=" $3
if (substr($4, 1, 1) == "\"") {
compare = $4
for (i=5; i<NF; i++) {
compare = compare " " $i
}
gsub(/\"/, "", compare)
} else if ($5 == "*/") {
compare = $2 "<" $4
} else {
compare = $2 " " $5 " " $4
}
increment = $2 "++"
printf("%s{ int %s;\n%s for (%s; %s; %s) {\n", indent, var, indent, init, compare, increment) >>outputname
indent = indent "\t"
next
}
(($1 == "/*XMLROF") || ($1 == "/*XMLROF*/")) {
indent = substr(indent, 1, length(indent)-1)
printf("%s} }\n\n", indent) >>outputname
next
}
$1 == "/*XMLIF" {
test = $2
for (i=3; i<=NF; i++) {
if ($i == "*/") {
i=NF+1
} else {
test = test " " $i
}
}
printf("%sif (%s) {\n", indent, test) >>outputname
indent = indent "\t"
next
}
(($1 == "/*XMLELSE") || ($1 == "/*XMLELSE*/")) {
printf("%s} else {\n\n", indent) >>outputname
next
}
(($1 == "/*XMLFI") || ($1 == "/*XMLFI*/")) {
indent = substr(indent, 1, length(indent)-1)
printf("%s}\n\n", indent) >>outputname
next
}
$1 == "/*XMLSWITCH" {
printf("%sswitch (%s) {\n", indent, $2) >>outputname
indent = indent "\t"
forceBreak="n"
next
}
(($1 == "/*XMLCASE") || ($1 == "/*XMLCASE*/")) {
if(forceBreak == "y") {
printf("%s break;\n", indent) >>outputname
}
if(tolower($2) == "default") {
printf("%s default:\n", indent) >>outputname
} else {
printf("%s case %s:\n", indent, $2) >>outputname
}
if(tolower($3) == "withnext") {
forceBreak="n"
} else {
forceBreak="y"
}
next
}
(($1 == "/*XMLHCTIWS") || ($1 == "/*XMLHCTIWS*/")) {
indent = substr(indent, 1, length(indent)-1)
printf("%s}\n\n", indent) >>outputname
inCase="n"
next
}
substr($1, 1, 3) == "#if" {
print "\n" $0 >>outputname
next
}
substr($1, 1, 5) == "#else" {
print $0 >>outputname
next
}
substr($1, 1, 5) == "#elif" {
print $0 >>outputname
next
}
substr($1, 1, 6) == "#endif" {
print $0 "\n" >>outputname
next
}
$1 == "/*XML" {
#/*XML type[:format] item[!]itemname childof[:result] "description" */
#$1 2 3 4 5...
# Presets...
gdomenode=""
# Process fields...
if ( (NF < 5) && ($2 != "x") && ($2 != "*") ) {
printf("\n\n ERROR -- missing fields on following line:\n%5d. %s\n\n", lines, $0)
next
}
if ((i=index($2, ":")) > 0) {
typeflag = substr($2, 1, i-1)
format = substr($2, i+1)
} else {
typeflag = $2
format = ""
}
if ((i=index($3, "!")) > 0) {
item = substr($3, 1, i-1)
itemname= substr($3, i+1)
} else {
item = $3
itemname= item
}
if ((i=index($4, ":")) > 0) {
childof = substr($4, 1, i-1)
result = substr($4, i+1)
} else {
childof = $4
result = ""
}
description=$5
for (i=6; i<NF; i++) {
if ($i != "*/") {
description = description " " $i
}
}
# All parms read...
f1 = typeflag (format == "" ? "" : ":") format
f2 = item (item == itemname ? "" : "!" itemname)
f3 = childof (result == "" ? "" : ":") result
processedline = sprintf("/*XML %-15s %-20s %-15s %s */",
f1,
f2,
f3,
description)
print indent processedline >>outputname
if (debug > "1") { printf(" /*XMLNOTE input line is '%s' */\n", $0)
printf(" /*XMLNOTE processed as '%s' */\n", processedline)
printf("\n /*XMLNOTE RAW values: */\n")
printf(" /*XMLNOTE childof.........'%s' */\n", childof)
printf(" /*XMLNOTE description.....'%s' */\n", description)
printf(" /*XMLNOTE format..........'%s' */\n", format)
printf(" /*XMLNOTE item............'%s' */\n", item)
printf(" /*XMLNOTE itemname........'%s' */\n", itemname)
printf(" /*XMLNOTE result..........'%s' */\n", result)
printf(" /*XMLNOTE typeflag........'%s' */\n", typeflag)
}
# Errors and other reasons to ignore this...
if (typeflag == "x") {
if (debug != "") printf(" /*XMLNOTE Ignored: %s */\n", item)
next
}
if (result == "root") {
if (debug != "") printf(" /*XMLNOTE ERROR resetting root node */\n")
next
} else if (result == "parent") {
if (debug != "") printf(" /*XMLNOTE ERROR resetting parent node */\n")
next
}
# Convenience conversions...
if (typeflag == "char*") {
typeflag="s"
}
# Cleanup, Tests and priority items...
typeprefix=""
if (typeflag == "*") {
} else {
while (substr(typeflag, 1, 1) == "*") {
typeprefix=typeprefix "* "
typeflag = substr(typeflag, 2)
}
if (substr(typeflag, 1, 1) == "&") {
typeprefix=typeprefix "&"
typeflag = substr(typeflag, 2)
}
}
if (debug > "1") { printf("\n /*XMLNOTE PROCESSED values: */\n")
printf(" /*XMLNOTE typeflag........'%s' */\n", typeflag)
printf(" /*XMLNOTE typeprefix......'%s' */\n", typeprefix)
}
if (item == ".") {
item = ""
if (debug > "1") { printf(" /*XMLNOTE item............'%s' */\n", item) }
}
if (childof == ".") {
childof = ""
if (debug > "1") { printf(" /*XMLNOTE childof.........'%s' */\n", childof) }
}
if (description == "*/") {
description = ""
if (debug > "1") { printf(" /*XMLNOTE description.....'%s' */\n", description) }
}
# Derived stuff...
# What about the "item"? Just to clarify...
# item is the way the user named it (e.g. xyz) -- we don't use that past here
# itemname is how to refer to it in xml (xyz)
# Setting those is handled above, like all the other field splits
# itemref is how to refer to it in c (myGlobals.xyz)
# fieldname is the stripped version of itemname
# Cleanup itemname, stripping indexes, pointers, etc.
while ((i=index(itemname, ".")) > 0) {
itemname=substr(itemname, i+1)
}
while ((i=index(itemname, "->")) > 0) {
itemname=substr(itemname, i+2)
}
if ((i=index(itemname, "_")) > 0) {
itemname=substr(itemname, i+1)
}
if ((i=index(itemname, "[")) > 0) {
itemname=substr(itemname, 1, i-1)
}
if ((i=index(itemname, "(")) > 0) {
itemname=substr(itemname, 1, i-1)
}
if (debug > "1") { printf(" /*XMLNOTE itemname........'%s' (xml) */\n", itemname) }
fieldname=itemname
# Cleanup fieldname, stripping indexes, pointers, etc.
while ((i=index(fieldname, ".")) > 0) {
fieldname=substr(fieldname, i+1)
}
while ((i=index(fieldname, "->")) > 0) {
fieldname=substr(fieldname, i+2)
}
while ((i=index(fieldname, "&")) > 0) {
fieldname=substr(fieldname, i+1)
}
while ((i=index(fieldname, "_")) > 0) {
fieldname=substr(fieldname, i+1)
}
if ((i=index(fieldname, "[")) > 0) {
fieldname=substr(fieldname, 1, i-1)
}
if (debug > "1") { printf(" /*XMLNOTE fieldname.......'%s' (stripped) */\n", fieldname) }
# Set itemref
if (typeflag in type_constant_item) {
itemref=item
} else {
if ( (substr(item, 1, 1) == ".") ||
(substr(item, 1, 2) == "->") ) {
itemref= prefix item
} else if (substr(item, 1, 1) == "&") {
item=substr(item, 2)
itemref= "&" prefix connect item
# } else if (prefix == "myGlobals") {
# itemref= "&" prefix connect item
} else if ( (index(item, ".") > 0) ||
(index(item, "->") > 0) ) {
itemref=item
} else if ( (index(item, "(") > 0) &&
(index(item, ")") > 0) ) {
itemref=item
} else {
itemref= prefix connect item
}
}
if (debug > "1") { printf(" /*XMLNOTE itemref.........'%s' (c) */\n", itemref) }
if (childof == "root") {
childnodename="root"
} else if (childof == "parent") {
childnodename="parent"
} else if (childof != "") {
childnodename="el" toupper(substr(childof, 1, 1)) substr(childof, 2)
}
if (debug > "1") { printf(" /*XMLNOTE childnodename...'%s' */\n", childnodename) }
makechildnode="N"
if (substr(childnodename, 1, length(workNodePrefix)) != workNodePrefix) {
# Child, not elWorkxxx, test if the child node exists...
if (childnodename in nodes) {
} else {
# no result and childof doesn't exist ... force it to be set...
makechildnode="Y"
}
}
if (debug > "1") { printf(" /*XMLNOTE makechildnode...'%s' */\n", makechildnode) }
if ( (typeflag in type_nonvalue_node) && (result == "") ) {
result=fieldname
if (debug > "1") { printf(" /*XMLNOTE result..........'%s' - FORCED (empty) */\n", result) }
}
if (result != "") {
result="el" toupper(substr(result, 1, 1)) substr(result, 2)
if (debug > "1") { printf(" /*XMLNOTE result..........'%s' */\n", result) }
}
if ((i=index(item, "[")) > 0) {
j=index(item, "]")
indexname=substr(item, i+1, j-i-1)
} else {
indexname=""
}
if (debug > "1") { printf(" /*XMLNOTE indexname.......'%s' */\n", indexname) }
if (typeflag in type_macroname_conversion) {
macro=type_macroname_conversion[typeflag]
if (typeflag in macroappend) {
macroappendvalue=macroappend[typeflag]
} else {
macroappendvalue=""
}
if (typeflag in macroformat) {
format=macroformat[typeflag]
}
} else if (typeflag in inlinecode_start) {
} else {
printf("\n\n ERROR -- type flag '%s' is unknown on following line:\n%5d. %s\n\n",
typeflag,
lines,
$0)
next
}
if ( ( (typeflag == "n") || (typeflag == "nc") ) && (format == "") ) {
format="d"
if (debug > "1") printf(" /*XMLNOTE format set to %s */\n", format)
}
if ( (typeflag == "f") && (format == "") ) {
format="f"
if (debug > "1") printf(" /*XMLNOTE format set to %s */\n", format)
}
if (debug > "1") { printf(" /*XMLNOTE macro...........'%s' */\n", macro)
printf(" /*XMLNOTE ..appendvalue...'%s' */\n", macroappendvalue)
printf(" /*XMLNOTE format..........'%s' */\n", format) }
# To set the result AND use simplenumeric we can't just use the xmldumpPlugin.c macro...
# Since this is dependent on the xmldumpPlugin.c macros, we hardcode their names here...
buftext=""
if ( (macro=="newxml_simplenumeric") && (result != "") ) {
if (debug != "") printf(" /*XMLNOTE numeric+result - macroname was %s, set to newxml_smartstring */\n", macro)
macro="newxml_smartstring"
buftext=sprintf("safe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), \"%%%s\", %s);",
format, itemref)
format=""
itemref="buf"
typeflag="s"
} else if ( (macro=="newxml_namednumeric") && (result != "") ) {
if (debug != "") printf(" /*XMLNOTE numeric+result - macroname was %s, set to newxml_smartstring */\n", macro)
typeflag="SPECIAL1"
}
if (debug > "1") { printf(" /*XMLNOTE macro...........'%s' */\n", macro)
printf(" /*XMLNOTE itemref.........'%s' */\n", itemref)
printf(" /*XMLNOTE buftext.........'%s' */\n", buftext)
printf(" /*XMLNOTE typeflag........'%s' */\n", typeflag)
printf(" /*XMLNOTE format..........'%s' */\n", format) }
# Process...
# 1. Create the child node if necessary...
if (makechildnode == "Y") {
if (debug != "") printf(" /*XMLNOTE (auto) generating node %s, child of %s */\n",
childnodename, parentnodename)
printf("%s/* *****************************************Auto create node** */\n" \
"%s%s = newxmlna(%s, \"%s\");\n",
indent,
indent,
childnodename,
parentnodename,
childof) >>outputname
insert_nodes(childnodename, "ChildAutoCreate")
}
# 2. Handle inline code...
# Just copy all lines until XML*/ found
if (typeflag == "*") {
if (debug != "") printf(" /*XMLNOTE Processing (inline) %s, %s */\n", item, description)
printf("\n%s/* %scopied from %s at line %d */\n",
indent,
description == "\"\"" ? "" : description " ",
ARGV[1],
lines) >>outputname
while (getline > 0) {
if ($1 == "XML*/") { break }
print indent $0 >>outputname
}
printf("%s/* end copy from %s */\n\n", indent, ARGV[1]) >>outputname
next
}
# 2a. Handle XMLINLINE field
if (typeflag in inlinecode_start) {
if (debug != "") printf(" /*XMLNOTE Processing (xmlinline) %s, %s */\n", item, description)
printf("\n%s/*i %sgenerated from %s at line %d */\n",
indent,
description == "\"\"" ? "" : description " ",
ARGV[1],
lines) >>outputname
for (i=inlinecode_start[typeflag]; i<inlinecode_end[typeflag]; i++) {
otext = inlinecode[i]
j=index(otext, "$")
while (j>0) {
f=substr(otext, j+1, 1)
stext = ""
if (f == "P") {
stext = stripedprefix connect
} else if (f == "X") {
if (description != "\"\"") {
stext = ",\n" indent "/*i*/ \"description\", " description " "
} else {
stext = ""
}
} else if (f == "2") {
stext = typeflag
} else if (f == "3") {
stext = item
} else if (f == "4") {
stext = childnodename
} else if (f == "5") {
stext = description
}
otext = substr(otext, 1, j-1) stext substr(otext, j+2)
j=index(otext, "$")
}
if (substr(otext, 1, 1) == "#") {
print otext >>outputname
} else {
print indent "/*i*/ " otext >>outputname
}
}
next
}
#
# OUTPUT OUTPUT OUTPUT
#
# 3. Let's output the code...
# 3a. SPECIAL1 --
if (typeflag == "SPECIAL1") {
# Special case for simplenumeric with result
if (debug != "") printf(" /*XMLNOTE SPECIAL1 */\n")
printf("%ssafe_snprintf(__FILE__, __LINE__, buf, sizeof(buf), \"%%%s\", %s);\n",
indent, format, itemref) >> outputname
printf("%s%s = newxml(%s, \"%s\", \n" \
"%s \"%s\", buf,\n" \
"%s \"description\", %s);\n\n\n",
indent, result, childnodename, itemname,
indent, fieldname,
indent, description) >>outputname
insert_nodes(result, "special1set")
next
}
# 3z. normal --
# NULL checks on pointers...
if ( (typeflag != "e") && (typeprefix ~ /\*/) ) {
itemrefroot=itemref
i=index(itemrefroot, "[")
if (i > 0) {
itemrefroot=substr(itemrefroot, 1, i-1)
}
if (debug > "1") { printf(" /*XMLNOTE itemrefroot.....'%s' */\n", itemrefroot) }
workprefix=typeprefix
testprefix=""
while (workprefix != "") {
if (debug > "1") { printf(" /*XMLNOTE workprefix......'%s' */\n", workprefix)
printf(" /*XMLNOTE testprefix......'%s' */\n", testprefix) }
printf("%sif (%s%s != NULL) {\n", indent, testprefix, itemrefroot) >>outputname
workprefix=substr(workprefix, 3)
testprefix=testprefix "* "
indent=indent " "
if (debug > "1") { printf(" /*XMLNOTE indent..........'%s' */\n", indent) }
}
}
# Normal case, build it up, piece by piece... (Note no \n 's in most pieces)
# If there is a sprintf to buf...
if (buftext != "") {
printf("%s%s\n", indent, buftext) >>outputname
}
# Start the macro/function... handling it if there is a result
# indent...
printf("%s", indent) >>outputname
# A result?
if (result != "") {
printf("%s = ", result) >>outputname
}
# Now the function
if((macro == "newxml_smartstring") && (format == "u")) {
macro = "newxml_smartstring_u"
format=""
}
printf("%s(%s%s,\n",
macro, (gdomenode != "" ? gdomenode ", " : ""), childnodename) >>outputname
printf("%s %s%s%s,\n",
indent,
fieldname == "nodename" ? "" : "\"",
fieldname,
fieldname == "nodename" ? "" : "\"") >>outputname
# Skip the "value" piece for non-valued nodes
if (typeflag in type_nonvalue_node) {
if (debug != "") { printf(" /*XMLNOTE typeflag '%s' in type_nonvalue_node */\n", typeflag) }
} else {
if ( (typeprefix == "") || (typeprefix == "&") ) {
workprefix = typeprefix
} else {
workprefix = substr(typeprefix, 3)
}
if (debug > "1") { printf(" /*XMLNOTE workprefix......'%s' */\n", workprefix) }
printf("%s %s%s%s%s,\n",
indent,
workprefix,
workprefix == "" ? "" : "(",
itemref,
workprefix == "" ? "" : ")" ) >>outputname
}
# Description
printf("%s %s",
indent, description) >>outputname
# Post description parameters (constants, formats, index)
if (type_macroname_conversion[typeflag] == "newxml_namedstring") {
printf(",\n%s \"%s\"",
indent, itemname) >>outputname
} else if (type_macroname_conversion[typeflag] == "newxml_namednumeric") {
printf(",\n%s \"%%%s\"",
indent, format != "" ? format : "d") >>outputname
printf(",\n%s \"%s\"",
indent, itemname) >>outputname
} else if (format != "" ) {
printf(",\n%s \"%%%s\"",
indent, format) >>outputname
##} else if (typeflag in type_appendindex) {
## printf(",\n%s %s",
## indent, type_appendindex[typeflag]) >>outputname
}
# Finish it
printf(");\n\n") >>outputname
# NULL checks on pointers...
if ( (typeflag != "e") && (typeprefix ~ /\*/) ) {
workprefix=typeprefix
while (workprefix != "") {
indent=substr(indent, 1, length(indent) - 2)
workprefix=substr(workprefix, 3)
printf("%s}\n", indent) >>outputname
}
}
# Remember
insert_nodes(result, "set")
next
}
{ next }
END {
if (outputname != "") {
print "" >>outputname
close(outputname)
}
if (debug != "") {
printf("/*XMLNOTE recap\n")
for (i in nodes) {
printf(" * %-30s %s\n", i, nodes[i])
}
printf(" */\n")
printf("/*XMLNOTE type_macroname_conversion\n")
for (i in type_macroname_conversion) {
printf(" * %-30s %s\n", i, type_macroname_conversion[i])
}
printf(" */\n")
printf("/*XMLNOTE inline\n")
for (i in inlinecode_start) {
printf(" * %-30s\n", i)
for (j=inlinecode_start[i]; j<inlinecode_end[i]; j++) {
printf(" * %3d %s\n", j-inlinecode_start[i], inlinecode[j])
}
printf(" *\n")
}
printf(" */\n")
}
print "xmldump.awk finished!"
}
|