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 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
|
#!/usr/bin/env python3
#
# Copyright (c) 2010-2016 Varnish Software
# All rights reserved.
#
# Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# syntax version history:
# 1.0: initial
# 2.0: added cname (nm2) after argument name
"""
Read the first existing file from arguments or vmod.vcc and produce:
${prefix}.h -- Prototypes for the implementation
${prefix}.c -- Magic glue & datastructures to make things a VMOD.
vmod_${name}.rst -- Extracted documentation
vmod_${name}.man.rst -- Extracted documentation (rst2man input)
prefix can set via -o and defaults to vcc_if
"""
import copy
import glob
import hashlib
import json
import optparse
import os
import re
import subprocess
import sys
import time
AMBOILERPLATE = '''\
## Generated by vmodtool.py --boilerplate.
vmod_XXX_vcc ?= $(srcdir)/VCC
vmod_vcc_files += $(vmod_XXX_vcc)
vmod_LTLIBRARIES += libvmod_XXX.la
libvmod_XXX_la_SOURCES = \\
\tSRC
libvmod_XXX_la_CFLAGS =
vmodtoolargs_XXX ?= --strict --boilerplate -o PFX
vmod_XXX_symbols_regex ?= Vmod_XXX_Data
libvmod_XXX_la_LDFLAGS = \\
\t-export-symbols-regex $(vmod_XXX_symbols_regex) \\
\t$(AM_LDFLAGS) \\
\t$(VMOD_LDFLAGS)
nodist_libvmod_XXX_la_SOURCES = PFX.c PFX.h
EXTRA_libvmod_XXX_la_DEPENDENCIES = $(nodist_libvmod_XXX_la_SOURCES)
EXTRA_DIST += automake_boilerplate_XXX.am
$(libvmod_XXX_la_OBJECTS): PFX.h
PFX.h vmod_XXX.rst vmod_XXX.man.rst: PFX.c
# A doc-change will not update mtime on the .h and .c files, so a
# touch(1) is necessary to signal that vmodtool was in fact run.
PFX.c: $(VMODTOOL) $(srcdir)/VCC
\t@PYTHON@ $(VMODTOOL) $(vmodtoolargs_XXX) $(srcdir)/VCC
\ttouch PFX.c
clean-local: clean-vmod-XXX
clean-vmod-XXX:
rm -f $(nodist_libvmod_XXX_la_SOURCES)
rm -f vmod_XXX.rst vmod_XXX.man.rst
'''
PRIVS = {
'PRIV_CALL': "struct vmod_priv *",
'PRIV_VCL': "struct vmod_priv *",
'PRIV_TASK': "struct vmod_priv *",
'PRIV_TOP': "struct vmod_priv *",
}
CTYPES = {
'ACL': "VCL_ACL",
'BACKEND': "VCL_BACKEND",
'BLOB': "VCL_BLOB",
'BODY': "VCL_BODY",
'BOOL': "VCL_BOOL",
'BYTES': "VCL_BYTES",
'DURATION': "VCL_DURATION",
'ENUM': "VCL_ENUM",
'HEADER': "VCL_HEADER",
'HTTP': "VCL_HTTP",
'INT': "VCL_INT",
'IP': "VCL_IP",
'PROBE': "VCL_PROBE",
'REAL': "VCL_REAL",
'REGEX': "VCL_REGEX",
'STEVEDORE': "VCL_STEVEDORE",
'STRANDS': "VCL_STRANDS",
'STRING': "VCL_STRING",
'SUB': "VCL_SUB",
'TIME': "VCL_TIME",
'VOID': "VCL_VOID",
}
CTYPES.update(PRIVS)
DEPRECATED = {}
#######################################################################
def deprecated(key, txt):
'''
Be annoying about features which are going away
'''
if DEPRECATED.get(key):
return
sys.stderr.write('#' * 72 + '\n')
sys.stderr.write(txt + '\n')
sys.stderr.write('#' * 72 + '\n')
time.sleep(3)
DEPRECATED[key] = True
#######################################################################
def is_quoted(txt):
return len(txt) > 2 and txt[0] == txt[-1] and txt[0] in ('"', "'")
def unquote(txt):
assert is_quoted(txt)
return txt[1:-1]
def fmt_cstruct(fo, a, b):
''' Output line in vmod struct '''
t = '\t%s' % a
if len(t.expandtabs()) > 40:
t += '\n\t\t\t\t\t'
else:
t += '\t'
while len(t.expandtabs()) < 40:
t += '\t'
fo.write('%s%s\n' % (t, b))
#######################################################################
def write_file_warning(fo, a, b, c, s):
fo.write(a + "\n")
fo.write(b + " NB: This file is machine generated, DO NOT EDIT!\n")
fo.write(b + "\n")
fo.write(b + " Edit " + s + " and run make instead\n")
fo.write(c + "\n\n")
def write_c_file_warning(fo, s):
write_file_warning(fo, "/*", " *", " */", s)
def write_rst_file_warning(fo, s):
write_file_warning(fo, "..", "..", "..", s)
def write_rst_hdr(fo, s, below="-", above=None):
fo.write('\n')
if above:
fo.write(above * len(s) + "\n")
fo.write(s + "\n")
if below:
fo.write(below * len(s) + "\n")
#######################################################################
def lwrap(s, width=64):
"""
Wrap a C-prototype like string into a number of lines.
"""
ll = []
p = ""
while len(s) > width:
y = s[:width].rfind(',')
if y == -1:
y = s[:width].rfind('(')
if y == -1:
break
ll.append(p + s[:y + 1])
s = s[y + 1:].lstrip()
p = " "
if s:
ll.append(p + s)
return "\n".join(ll) + "\n"
#######################################################################
inputline = None
def err(txt, warn=True):
if inputline is not None:
print("While parsing line:\n\t", inputline)
if opts.strict or not warn:
print("ERROR: " + txt, file=sys.stderr)
exit(1)
else:
print("WARNING: " + txt, file=sys.stderr)
#######################################################################
class CType():
def __init__(self, wl, enums):
self.nm = None
self.defval = None
self.spec = None
self.opt = False
self.vt = wl.pop(0)
self.ct = CTYPES.get(self.vt)
if self.ct is None:
err("Expected type got '%s'" % self.vt, warn=False)
if wl and wl[0] == "{":
if self.vt != "ENUM":
err("Only ENUMs take {...} specs", warn=False)
self.add_spec(wl, enums)
def __str__(self):
s = "<" + self.vt
if self.nm is not None:
s += " " + self.nm
if self.defval is not None:
s += " VAL=" + self.defval
if self.spec is not None:
s += " SPEC=" + str(self.spec)
return s + ">"
def add_spec(self, wl, enums):
assert self.vt == "ENUM"
assert wl.pop(0) == "{"
self.spec = []
while True:
x = wl.pop(0)
if is_quoted(x):
x = unquote(x)
assert x
self.spec.append(x)
enums[x] = True
w = wl.pop(0)
if w == "}":
break
assert w == ","
def vcl(self, terse=False):
if self.vt == "STRANDS":
return "STRING"
if terse:
return self.vt
if self.spec is None:
return self.vt
return self.vt + " {" + ", ".join(self.spec) + "}"
def jsonproto(self, jl):
jl.append([self.vt])
while jl[-1][-1] is None:
jl[-1].pop(-1)
#######################################################################
class arg(CType):
''' Parse front of word list into argument '''
def __init__(self, wl, argnames, enums, end):
super(arg, self).__init__(wl, enums)
if wl[0] == end:
return
x = wl.pop(0)
if x in argnames:
err("Duplicate argument name '%s'" % x, warn=False)
argnames[x] = True
self.nm = x
if wl[0] == end:
return
x = wl.pop(0)
if x != "=":
err("Expected '=' got '%s'" % x, warn=False)
x = wl.pop(0)
if self.vt == "ENUM":
if is_quoted(x):
x = unquote(x)
self.defval = x
def jsonproto(self, jl):
jl.append([self.vt, self.nm, self.nm2, self.defval, self.spec])
if self.opt:
jl[-1].append(True)
while jl[-1][-1] is None:
jl[-1].pop(-1)
#######################################################################
class ProtoType():
def __init__(self, st, retval=True, prefix=""):
self.st = st
self.obj = None
self.args = []
self.argstruct = False
wl = self.st.toks[1:]
if retval:
self.retval = CType(wl, st.vcc.enums)
else:
self.retval = CType(['VOID'], st.vcc.enums)
self.bname = wl.pop(0)
if not re.match(r'^[a-zA-Z.][a-zA-Z0-9_]*$', self.bname):
err("%s(): Illegal name\n" % self.bname, warn=False)
self.name = prefix + self.bname
if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', self.cname()):
err("%s(): Illegal C-name\n" % self.cname(), warn=False)
if len(wl) == 2 and wl[0] == '(' and wl[1] == ')':
return
if wl[0] != "(":
err("Syntax error: Expected '(', got '%s'" % wl[0], warn=False)
wl[0] = ','
if wl[-1] != ")":
err("Syntax error: Expected ')', got '%s'" % wl[-1], warn=False)
wl[-1] = ','
names = {}
n = 0
while wl:
n += 1
x = wl.pop(0)
if x != ',':
err("Expected ',' found '%s'" % x, warn=False)
if not wl:
break
if wl[0] == '[':
wl.pop(0)
t = arg(wl, names, st.vcc.enums, ']')
if t.nm is None:
err("Optional arguments must have names", warn=False)
t.opt = True
x = wl.pop(0)
if x != ']':
err("Expected ']' found '%s'" % x, warn=False)
self.argstruct = True
else:
t = arg(wl, names, st.vcc.enums, ',')
if t.vt == 'VOID':
err("arguments cannot be of type '%s'" % t.vt, warn=False)
if t.nm is None:
t.nm2 = "arg%d" % n
elif ':' in t.nm:
[t.nm, t.nm2] = t.nm.split(':')
else:
t.nm2 = t.nm
self.args.append(t)
def vcl_proto(self, terse, pfx=""):
if isinstance(self.st, MethodStanza):
pfx += pfx
s = pfx
if isinstance(self.st, ObjectStanza):
s += "new " + self.obj + " = "
elif self.retval is not None:
s += self.retval.vcl() + " "
if isinstance(self.st, ObjectStanza):
s += self.st.vcc.modname + "." + self.name + "("
elif isinstance(self.st, MethodStanza):
s += self.obj + self.bname + "("
else:
s += self.name + "("
ll = []
for i in self.args:
t = i.vcl(terse)
if t in PRIVS:
continue
if i.nm is not None:
t += " " + i.nm
if not terse:
if i.defval is not None:
t += "=" + i.defval
if i.opt:
t = "[" + t + "]"
ll.append(t)
t = ",@".join(ll)
if len(s + t) > 68 and not terse:
s += "\n" + pfx + pfx
s += t.replace("@", "\n" + pfx + pfx)
s += "\n" + pfx + ")"
else:
s += t.replace("@", " ") + ")"
return s
def rst_proto(self, fo, sep='-'):
s = self.vcl_proto(False)
if len(s) < 60:
write_rst_hdr(fo, s, sep)
else:
s = self.vcl_proto(True)
write_rst_hdr(fo, s, sep)
fo.write("\n::\n\n" + self.vcl_proto(False, pfx=" ") + "\n")
def cname(self, pfx=False):
r = self.name.replace(".", "_")
if pfx:
return self.st.vcc.sympfx + r
return r
def proto(self, args, name):
s = self.retval.ct + " " + name + '('
ll = args
if self.argstruct:
ll.append(self.argstructname() + "*")
else:
for i in self.args:
ll.append(i.ct)
s += ", ".join(ll)
return s + ');'
def typedef_name(self):
return 'td_' + self.st.vcc.sympfx + \
self.st.vcc.modname + '_' + self.cname()
def typedef(self, args):
return "typedef " + self.proto(args, name=self.typedef_name())
def argstructname(self):
return "struct VARGS(%s)" % self.cname(False)
def argstructure(self):
s = "\n" + self.argstructname() + " {\n"
for i in self.args:
if i.opt:
assert i.nm2 is not None
s += "\tchar\t\t\tvalid_%s;\n" % i.nm2
for i in self.args:
s += "\t" + i.ct
if len(i.ct) < 8:
s += "\t"
if len(i.ct) < 16:
s += "\t"
s += "\t" + i.nm2 + ";\n"
s += "};\n"
return s
def cproto(self, eargs, where):
''' Produce C language prototype '''
s = ""
if where == 'h':
if self.argstruct:
s += self.argstructure()
s += lwrap(self.proto(eargs, self.cname(True)))
elif where == 'c':
s += lwrap(self.typedef(eargs))
elif where == 'o':
if self.argstruct:
s += self.argstructure()
s += lwrap(self.typedef(eargs))
else:
assert False
return s
def jsonproto(self, jl, cfunc):
''' Produce VCL prototype as JSON '''
ll = []
self.retval.jsonproto(ll)
ll.append('%s.f_%s' % (self.st.vcc.csn, cfunc))
if self.argstruct:
# We cannot use VARGS() here, we are after the #undef
ll.append('struct arg_%s%s_%s' %
(self.st.vcc.sympfx, self.st.vcc.modname, self.cname(False)))
else:
ll.append("")
for i in self.args:
i.jsonproto(ll)
jl.append(ll)
#######################################################################
class Stanza():
''' Base class for all $-Stanzas '''
def __init__(self, vcc, toks, doc):
self.toks = toks
doc = doc.split('\n')
while doc and not doc[0].strip():
doc.pop(0)
while doc and not doc[-1].strip():
doc.pop(-1)
self.doc = doc
self.vcc = vcc
self.rstlbl = None
self.null_ok = False
self.methods = None
self.proto = None
self.parse()
def parse(self):
assert "subclass should have defined" == "parse method"
def syntax(self):
err("Syntax error.\n" +
"\tShould be: " + self.__doc__.strip() + "\n" +
"\tIs: " + " ".join(self.toks) + "\n",
warn=False)
def rstfile(self, fo, man):
self.rsthead(fo, man)
self.rstdoc(fo, man)
def rsthead(self, fo, unused_man):
''' Emit the systematic part of the documentation '''
if self.rstlbl:
fo.write('\n.. _' + self.rstlbl + ':\n')
if self.proto:
self.proto.rst_proto(fo)
fo.write("\n")
def rstdoc(self, fo, unused_man):
''' Emit the explanatory part of the documentation '''
fo.write("\n".join(self.doc) + "\n")
def synopsis(self, fo, man):
if man and self.proto:
fo.write(self.proto.vcl_proto(True, pfx=" ") + '\n \n')
elif self.proto and self.rstlbl:
fo.write(' :ref:`%s`\n \n' % self.rstlbl)
def cstuff(self, unused_fo, unused_where):
return
def fmt_cstruct_proto(self, fo, proto, define):
if define:
fmt_cstruct(
fo,
proto.typedef_name(),
'*f_' + proto.cname() + ';'
)
else:
fmt_cstruct(
fo,
'.f_' + proto.cname() + ' =',
self.vcc.sympfx + proto.cname() + ','
)
def cstruct(self, unused_fo, unused_define):
return
def json(self, unused_jl):
''' Add to the json we hand VCC '''
return
#######################################################################
class ModuleStanza(Stanza):
''' $Module modname man_section description ... '''
def parse(self):
if len(self.toks) < 4:
self.syntax()
self.vcc.modname = self.toks[1]
self.vcc.mansection = self.toks[2]
if len(self.toks) == 4 and is_quoted(self.toks[3]):
self.vcc.moddesc = unquote(self.toks[3])
else:
print("\nNOTICE: Please put $Module description in quotes.\n")
self.vcc.moddesc = " ".join(self.toks[3:])
self.rstlbl = "vmod_%s(%d)" % (self.vcc.modname, 3)
self.vcc.contents.append(self)
def rsthead(self, fo, man):
if man:
write_rst_hdr(fo, "vmod_" + self.vcc.modname, "=", "=")
write_rst_hdr(fo, self.vcc.moddesc, "-", "-")
fo.write("\n")
fo.write(":Manual section: " + self.vcc.mansection + "\n")
else:
if self.rstlbl:
fo.write('\n.. _' + self.rstlbl + ':\n')
write_rst_hdr(fo,
"VMOD " + self.vcc.modname +
' - ' + self.vcc.moddesc,
"=", "=")
if self.vcc.auto_synopsis:
write_rst_hdr(fo, "SYNOPSIS", "=")
fo.write("\n")
fo.write(".. parsed-literal::\n\n")
fo.write(' import %s [as name] [from "path"]\n' % self.vcc.modname)
fo.write(" \n")
for c in self.vcc.contents:
c.synopsis(fo, man)
class ABIStanza(Stanza):
''' $ABI [strict|vrt] '''
def parse(self):
if len(self.toks) != 2:
self.syntax()
self.vcc.strict_abi = {
'strict': True,
'vrt': False,
}.get(self.toks[1])
if self.vcc.strict_abi is None:
err("Valid ABI types are 'strict' or 'vrt', got '%s'\n" %
self.toks[1])
self.vcc.contents.append(self)
if not self.vcc.strict_abi:
self.vcc.vrt_major = "VRT_MAJOR_VERSION"
self.vcc.vrt_minor = "VRT_MINOR_VERSION"
class PrefixStanza(Stanza):
''' $Prefix symbol '''
def parse(self):
if len(self.toks) != 2:
self.syntax()
self.vcc.sympfx = self.toks[1] + "_"
self.vcc.contents.append(self)
class SynopsisStanza(Stanza):
''' $Synopsis [auto|manual] '''
def parse(self):
if len(self.toks) != 2:
self.syntax()
valid = {
'auto': True,
'manual': False,
}
self.vcc.auto_synopsis = valid.get(self.toks[1])
if self.vcc.auto_synopsis is None:
err("Valid Synopsis values are 'auto' or 'manual', got '%s'\n" %
self.toks[1])
self.vcc.contents.append(self)
class EventStanza(Stanza):
''' $Event function_name '''
def parse(self):
if len(self.toks) != 2:
self.syntax()
self.event_func = self.toks[1]
self.vcc.contents.append(self)
def rstfile(self, fo, man):
if self.doc:
err("Not emitting .RST for $Event %s\n" %
self.event_func)
def cstuff(self, fo, where):
if where == 'h':
fo.write("vmod_event_f VPFX(%s);\n" % self.event_func)
def cstruct(self, fo, define):
if define:
fmt_cstruct(fo, "vmod_event_f", "*_event;")
else:
fmt_cstruct(fo,
"._event =",
self.vcc.sympfx + self.event_func + ',')
def json(self, jl):
jl.append(["$EVENT", "%s._event" % self.vcc.csn])
class FunctionMethodStanzaBase(Stanza):
''' Base class for $Function and Method '''
def cstruct(self, fo, define):
self.fmt_cstruct_proto(fo, self.proto, define)
def rstdoc(self, fo, unused_man):
super().rstdoc(fo,unused_man)
if (self.restrict is not None):
fo.write("\nRestricted to: ``%s``.\n\n" %
'``, ``'.join(self.restrict.restrict_toks))
self.restrict.rstdoc(fo, unused_man)
def json(self, jl):
self.proto.jsonproto(jl[-1], self.proto.cname())
if (self.restrict is not None):
self.restrict.json(jl)
class FunctionStanza(FunctionMethodStanzaBase):
''' $Function TYPE name ( ARGUMENTS ) '''
def parse(self):
self.proto = ProtoType(self)
self.rstlbl = '%s.%s()' % (self.vcc.modname, self.proto.name)
self.vcc.contents.append(self)
self.restrict = None
def cstuff(self, fo, where):
fo.write(self.proto.cproto(['VRT_CTX'], where))
def json(self, jl):
jl.append(["$FUNC", "%s" % self.proto.name])
super().json(jl)
class ObjectStanza(Stanza):
''' $Object TYPE class ( ARGUMENTS ) '''
def parse(self):
if self.toks[1] == "NULL_OK":
self.toks.pop(1)
self.null_ok = True
self.proto = ProtoType(self, retval=False)
self.proto.obj = "x" + self.proto.name
self.init = copy.copy(self.proto)
self.init.name += '__init'
self.fini = copy.copy(self.proto)
self.fini.name += '__fini'
self.fini.argstruct = False
self.fini.args = []
self.rstlbl = '%s.%s()' % (self.vcc.modname, self.proto.name)
self.vcc.contents.append(self)
self.methods = []
def rsthead(self, fo, man):
if self.rstlbl:
fo.write('\n.. _' + self.rstlbl + ':\n')
self.proto.rst_proto(fo)
fo.write("\n" + "\n".join(self.doc) + "\n")
for i in self.methods:
i.rstfile(fo, man)
def rstdoc(self, unused_fo, unused_man):
return
def synopsis(self, fo, man):
if man and self.proto:
fo.write(self.proto.vcl_proto(True, pfx=" ") + '\n \n')
for i in self.methods:
if i.proto:
fo.write(i.proto.vcl_proto(True, pfx=" ") + '\n \n')
elif self.proto and self.rstlbl:
fo.write(' :ref:`%s`\n \n' % self.rstlbl)
for i in self.methods:
if i.proto and i.rstlbl:
fo.write(' :ref:`%s`\n \n' % i.rstlbl)
def cstuff(self, fo, w):
sn = 'VPFX(' + self.vcc.modname + '_' + self.proto.name + ')'
fo.write("struct %s;\n" % sn)
fo.write(self.init.cproto(
['VRT_CTX', 'struct %s **' % sn, 'const char *'], w))
fo.write(self.fini.cproto(['struct %s **' % sn], w))
for i in self.methods:
fo.write(i.proto.cproto(['VRT_CTX', 'struct %s *' % sn], w))
fo.write("\n")
def cstruct(self, fo, define):
self.fmt_cstruct_proto(fo, self.init, define)
self.fmt_cstruct_proto(fo, self.fini, define)
for i in self.methods:
i.cstruct(fo, define)
fo.write("\n")
def json(self, jl):
ll = [
"$OBJ",
self.proto.name,
{"NULL_OK": self.null_ok},
"struct %s%s_%s" %
(self.vcc.sympfx, self.vcc.modname, self.proto.name),
]
l2 = ["$INIT"]
ll.append(l2)
self.init.jsonproto(l2, self.init.name)
l2 = ["$FINI"]
ll.append(l2)
self.fini.jsonproto(l2, self.fini.name)
for i in self.methods:
i.json(ll)
jl.append(ll)
#######################################################################
class MethodStanza(FunctionMethodStanzaBase):
''' $Method TYPE . method ( ARGUMENTS ) '''
def parse(self):
p = self.vcc.contents[-1]
assert isinstance(p, ObjectStanza)
self.pfx = p.proto.name
self.proto = ProtoType(self, prefix=self.pfx)
if not self.proto.bname.startswith("."):
err("$Method %s: Method names need to start with . (dot)"
% self.proto.bname, warn=False)
self.proto.obj = "x" + self.pfx
self.rstlbl = 'x%s()' % self.proto.name
p.methods.append(self)
self.restrict = None
def json(self, jl):
jl.append(["$METHOD", self.proto.name[len(self.pfx)+1:]])
super().json(jl)
class RestrictStanza(Stanza):
''' $Restrict scope1 [scope2 ..] '''
def parse(self):
if len(self.toks) < 2:
self.syntax()
p = self.vcc.contents[-1]
if (isinstance(p, ObjectStanza)):
if(p.methods):
p.methods[-1].restrict = self
else:
err("$Restrict should be after $Method or $Function", False)
elif (isinstance(p, FunctionStanza)):
p.restrict = self
else :
err("$Restrict should be after $Method or $Function", False)
self.restrict_toks = self.toks[1:]
def json(self, jl):
tab = ["$RESTRICT"]
tab.append(self.restrict_toks)
jl.append(tab)
class AliasStanza(Stanza):
''' $Alias ALIAS SYMBOL '''
def find_symbol(self, tbl, name):
for sym in tbl:
if sym.proto is None:
continue
if sym.proto.name == name:
return sym
err("Symbol '%s' not found\n" % name, warn=False)
def parse(self):
if len(self.toks) != 3:
err("Syntax error, expected: $Alias <alias> <symbol>\n", warn=False)
if not re.match(r'^\.?[a-zA-Z_][a-zA-Z0-9_]*$', self.toks[1]):
err("%s(): Illegal C-name\n" % self.toks[1], warn=False)
if self.toks[1][0] == '.':
if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*\.[a-zA-Z_][a-zA-Z0-9_]*$',
self.toks[2]):
err("Syntax error, expected: $Alias <.alias> <obj.method>\n",
warn=False)
obj_name = self.toks[2].split('.')[0]
obj = self.find_symbol(self.vcc.contents, obj_name)
self.find_symbol(obj.methods, self.toks[2])
self.sym_alias = "%s%s" % (obj_name, self.toks[1])
self.doc_alias = "x%s" % self.sym_alias
self.sym_name = self.toks[2]
self.doc_name = "x%s" % self.toks[2]
else:
self.find_symbol(self.vcc.contents, self.toks[2])
self.sym_alias = self.toks[1]
self.doc_alias = self.toks[1]
self.sym_name = self.toks[2]
self.doc_name = self.toks[2]
self.vcc.contents.append(self)
def rsthead(self, fo, unused_man):
write_rst_hdr(fo, "ALIAS %s()" % self.doc_alias, "-")
fo.write("\nDeprecated alias for ``%s()``." % self.doc_name)
if len(self.doc) > 0:
fo.write("\n\n")
def json(self, jl):
jl.append(["$ALIAS", self.sym_alias, self.sym_name])
class VersionStanza(Stanza):
''' $Version version ... '''
def parse(self):
if len(self.toks) < 2:
self.syntax()
self.vcc.pkgstr = " ".join(self.toks[1:])
#######################################################################
DISPATCH = {
"Module": ModuleStanza,
"Prefix": PrefixStanza,
"ABI": ABIStanza,
"Event": EventStanza,
"Function": FunctionStanza,
"Object": ObjectStanza,
"Method": MethodStanza,
"Synopsis": SynopsisStanza,
"Alias": AliasStanza,
"Restrict": RestrictStanza,
"Version": VersionStanza
}
class vcc():
''' Processing context for a single .vcc file '''
def __init__(self, inputvcc, rstdir, outputprefix):
self.inputfile = inputvcc
self.rstdir = rstdir
self.pfx = outputprefix
self.sympfx = "vmod_"
self.contents = []
self.commit_files = []
self.copyright = ""
self.enums = {}
self.strict_abi = True
self.vrt_major = "0"
self.vrt_minor = "0"
self.auto_synopsis = True
self.modname = None
self.csn = None
self.pkgstr = None
def openfile(self, fn):
self.commit_files.append(fn)
return open(fn + ".tmp", "w")
def commit(self):
for i in self.commit_files:
try:
before = open(i, "rb").read()
except:
before = None
after = open(i + ".tmp", "rb").read()
if before != after:
os.rename(i + ".tmp", i)
else:
os.remove(i + ".tmp")
def parse(self):
global inputline
b = open(self.inputfile, "rb").read()
a = "\n" + b.decode("utf-8")
h = hashlib.sha256()
s = a.split("\n$")
self.copyright = s.pop(0).strip()
while s:
ss = re.split('\n([^\t ])', s.pop(0), maxsplit=1)
toks = self.tokenize(ss[0])
inputline = '$' + ' '.join(toks)
h.update((inputline + '\n').encode('utf-8'))
docstr = "".join(ss[1:])
stanzaclass = DISPATCH.get(toks[0])
if stanzaclass is None:
err("Unknown stanza $%s" % toks[0], warn=False)
stanzaclass(self, toks, docstr)
inputline = None
self.csn = "Vmod_%s%s_Func" % (self.sympfx, self.modname)
self.file_id = h.hexdigest()
def tokenize(self, txt, seps=None, quotes=None):
if seps is None:
seps = "[](){},="
if quotes is None:
quotes = '"' + "'"
quote = None
out = []
i = 0
inside = False
while i < len(txt):
c = txt[i]
# print("T", [c], quote, inside, i)
i += 1
if quote is not None and c == quote:
inside = False
quote = None
out[-1] += c
elif quote is not None:
out[-1] += c
elif c.isspace():
inside = False
elif seps.find(c) >= 0:
inside = False
out.append(c)
elif quotes.find(c) >= 0:
quote = c
out.append(c)
elif inside:
out[-1] += c
else:
out.append(c)
inside = True
#print("TOK", [str])
#for i in out:
# print("\t", [i])
return out
def rstfile(self, man=False):
''' Produce rst documentation '''
fn = os.path.join(self.rstdir, "vmod_" + self.modname)
if man:
fn += ".man"
fn += ".rst"
fo = self.openfile(fn)
write_rst_file_warning(fo, self.inputfile)
if man:
fo.write(".. role:: ref(emphasis)\n")
else:
fo.write('\n:tocdepth: 1\n')
for i in self.contents:
i.rstfile(fo, man)
if self.copyright:
write_rst_hdr(fo, "COPYRIGHT", "=")
fo.write("\n::\n\n")
a = self.copyright
a = a.replace("\n#", "\n ")
if a[:2] == "#\n":
a = a[2:]
if a[:3] == "#-\n":
a = a[3:]
fo.write(a + "\n")
fo.close()
def amboilerplate(self):
''' Produce boilerplate for autocrap tools '''
vcc = os.path.basename(self.inputfile)
src = glob.glob("vmod_" + self.modname + "*.[ch]")
src.sort()
fn = "automake_boilerplate_" + self.modname + ".am"
fo = self.openfile(fn)
fo.write(AMBOILERPLATE.replace("XXX", self.modname)
.replace("VCC", vcc)
.replace("PFX", self.pfx)
.replace("SRC", " \\\n\t".join(src)))
fo.close()
def mkdefs(self, fo):
fo.write('#define VPFX(a) %s##a\n' % self.sympfx)
fo.write('#define VARGS(a) arg_%s%s_##a\n' %
(self.sympfx, self.modname))
fo.write('#define VENUM(a) enum_%s%s_##a\n' %
(self.sympfx, self.modname))
for a in ('VPFX', 'VARGS', 'VENUM'):
for b in (755, 767):
fo.write('//lint -esym(%d, %s)\n' % (b, a))
fo.write('//lint -esym(755, VARGS)\n')
fo.write('//lint -esym(755, VENUM)\n')
fo.write('\n')
def mkhfile(self):
''' Produce vcc_if.h file '''
fn = self.pfx + ".h"
fo = self.openfile(fn)
write_c_file_warning(fo, self.inputfile)
fo.write("#ifndef VDEF_H_INCLUDED\n")
fo.write('# error "Include vdef.h first"\n')
fo.write("#endif\n")
fo.write("#ifndef VRT_H_INCLUDED\n")
fo.write('# error "Include vrt.h first"\n')
fo.write("#endif\n")
fo.write("\n")
self.mkdefs(fo)
for j in sorted(self.enums):
fo.write("extern VCL_ENUM VENUM(%s);\n" % j)
fo.write("\n")
for j in sorted(self.enums):
fo.write("//lint -esym(14, enum_%s%s_%s)\n" %
(self.sympfx, self.modname, j))
fo.write("//lint -esym(759, enum_%s%s_%s)\n" %
(self.sympfx, self.modname, j))
fo.write("//lint -esym(765, enum_%s%s_%s)\n" %
(self.sympfx, self.modname, j))
fo.write("\n")
for j in self.contents:
j.cstuff(fo, 'h')
fo.close()
def cstruct(self, fo):
fo.write("\nstruct %s {\n" % self.csn)
for j in self.contents:
j.cstruct(fo, True)
for j in sorted(self.enums):
fmt_cstruct(fo, 'VCL_ENUM', '*enum_%s;' % j)
fo.write("};\n")
def cstruct_init(self, fo):
fo.write("\nstatic const struct %s %s = {\n" % (self.csn, self.csn))
for j in self.contents:
j.cstruct(fo, False)
fo.write("\n")
for j in sorted(self.enums):
fmt_cstruct(fo, '.enum_%s =' % j, '&VENUM(%s),' % j)
fo.write("};\n")
def iter_json(self, fnx):
jl = [["$VMOD", "2.0", self.modname, self.csn, self.file_id]]
jl.append(["$CPROTO"])
for i in open(fnx):
jl[-1].append(i.rstrip())
jl[-1].append("static struct %s %s;" % (self.csn, self.csn))
for j in self.contents:
j.json(jl)
for i in json.dumps(jl, indent=2, separators=(",", ": ")).splitlines():
yield i + " "
def json(self, fo, fnx):
fo.write('#define STRINGIFY3(arg) #arg\n')
fo.write('#define STRINGIFY2(arg) STRINGIFY3(#arg)\n')
fo.write('#define STRINGIFY1(arg) STRINGIFY2(arg)\n')
fo.write("\nstatic const char Vmod_Json[] = {\n")
fo.write('\t"VMOD_JSON_SPEC\x02"\n')
for n, i in enumerate(self.iter_json(fnx)):
fo.write('\t"')
for j in i:
if j in '"\\':
fo.write('\\')
fo.write(j)
if n == 6:
# Hand-munge the JSON to insert stuff only known by
# the C-compiler at compile-time.
fo.write(',"\n\t" \\"" VMOD_ABI_Version "\\", "\n')
fo.write('\t STRINGIFY1(%s) ", "\n' % self.vrt_major)
fo.write('\t STRINGIFY1(%s)\n' % self.vrt_minor)
else:
fo.write('"\n')
fo.write('\t\"\\n\\x03\"\n};\n')
fo.write('#undef STRINGIFY\n')
# parts from varnish-cache include/generate.py
def version(self):
srcdir = os.path.dirname(self.inputfile)
pkgstr = "NOVERSION"
if self.pkgstr is not None:
pkgstr = self.pkgstr
else:
for d in [srcdir, "."]:
f = os.path.join(d, "Makefile")
if not os.path.exists(f):
continue
for pkgstr in open(f):
if pkgstr[:14] == "PACKAGE_STRING":
pkgstr = pkgstr.split("=")[1].strip()
break
break
return pkgstr
# parts from varnish-cache include/generate.py
def vcs(self):
srcdir = os.path.dirname(self.inputfile)
gitver = subprocess.check_output([
"git -C %s rev-parse HEAD 2>/dev/null || echo NOGIT" %
srcdir], shell=True, universal_newlines=True).strip()
gitfile = "vmod_vcs_version.txt"
if gitver == "NOGIT":
for d in [".", srcdir]:
f = os.path.join(d, gitfile)
if not os.path.exists(f):
continue
fh = open(f, "r")
if not fh:
continue
gitver = fh.read()
fh.close()
break;
else:
fh = open(gitfile, "w")
fh.write(gitver)
fh.close()
return gitver
def vmod_data(self, fo):
version = json.dumps(self.version())
vcs = json.dumps(self.vcs())
vmd = "Vmod_%s_Data" % self.modname
fo.write('\n')
fo.write('static const char vmod_vcs[] ')
fo.write('v_used_(section(".vmod_vcs")) = %s;\n' % vcs)
for i in (714, 759, 765):
fo.write("/*lint -esym(%d, %s) */\n" % (i, vmd))
fo.write("\nextern const struct vmod_data %s;\n" % vmd)
fo.write("\nconst struct vmod_data %s = {\n" % vmd)
fo.write("\t.vrt_major =\t%s,\n" % self.vrt_major)
fo.write("\t.vrt_minor =\t%s,\n" % self.vrt_minor)
fo.write("\t.file_id =\t\"%s\",\n" % self.file_id)
fo.write('\t.name =\t\t"%s",\n' % self.modname)
fo.write('\t.func_name =\t"%s",\n' % self.csn)
fo.write('\t.func =\t\t&%s,\n' % self.csn)
fo.write('\t.func_len =\tsizeof(%s),\n' % self.csn)
fo.write('\t.json =\t\tVmod_Json,\n')
fo.write('\t.abi =\t\tVMOD_ABI_Version,\n')
fo.write('\t.version =\t%s,\n' % version)
fo.write('\t.vcs =\tvmod_vcs,\n')
fo.write("};\n")
def mkcfile(self):
''' Produce vcc_if.c file '''
fno = self.pfx + ".c"
fo = self.openfile(fno)
fnx = fno + ".tmp2"
fx = open(fnx, "w")
write_c_file_warning(fo, self.inputfile)
self.mkdefs(fx)
fo.write('#include "config.h"\n')
for i in ["vdef", "vrt", self.pfx, "vmod_abi"]:
fo.write('#include "%s.h"\n' % i)
fo.write("\n")
for j in sorted(self.enums):
fo.write('VCL_ENUM VENUM(%s) = "%s";\n' % (j, j))
fo.write("\n")
for i in self.contents:
if isinstance(i, ObjectStanza):
i.cstuff(fo, 'c')
i.cstuff(fx, 'o')
fx.write("/* Functions */\n")
for i in self.contents:
if isinstance(i, FunctionStanza):
i.cstuff(fo, 'c')
i.cstuff(fx, 'o')
self.cstruct(fo)
self.cstruct(fx)
fo.write("\n/*lint -esym(754, " + self.csn + "::*) */\n")
self.cstruct_init(fo)
fx.write('#undef VPFX\n')
fx.write('#undef VARGS\n')
fx.write('#undef VENUM\n')
fx.close()
self.json(fo, fnx)
self.vmod_data(fo)
os.remove(fnx)
fo.close()
#######################################################################
def runmain(inputvcc, rstdir, outputprefix):
v = vcc(inputvcc, rstdir, outputprefix)
v.parse()
v.rstfile(man=False)
v.rstfile(man=True)
v.mkhfile()
v.mkcfile()
if opts.boilerplate:
v.amboilerplate()
v.commit()
if __name__ == "__main__":
usagetext = "Usage: %prog [options] <vmod.vcc>"
oparser = optparse.OptionParser(usage=usagetext)
oparser.add_option('-b', '--boilerplate', action='store_true',
default=False,
help="Create automake_boilerplate.am")
oparser.add_option('-N', '--strict', action='store_true', default=False,
help="Be strict when parsing the input file")
oparser.add_option('-o', '--output', metavar="prefix", default='vcc_if',
help='Output file prefix (default: "vcc_if")')
oparser.add_option('-w', '--rstdir', metavar="directory", default='.',
help='Where to save the generated RST files ' +
'(default: ".")')
(opts, args) = oparser.parse_args()
i_vcc = None
for f in args:
if os.path.exists(f):
i_vcc = f
break
if i_vcc is None and os.path.exists("vmod.vcc"):
i_vcc = "vmod.vcc"
if i_vcc is None:
print("ERROR: No vmod.vcc file supplied or found.", file=sys.stderr)
oparser.print_help()
exit(-1)
runmain(i_vcc, opts.rstdir, opts.output)
|