Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
varnish-cache
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
varnishcache
varnish-cache
Commits
046d8d1c
Commit
046d8d1c
authored
Jun 02, 2014
by
Poul-Henning Kamp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some more style polishing of vmodtool
parent
9c3a747d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
64 deletions
+60
-64
vmodtool.py
lib/libvcc/vmodtool.py
+60
-64
No files found.
lib/libvcc/vmodtool.py
View file @
046d8d1c
...
...
@@ -25,17 +25,13 @@
# 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.
#
# Read the vmod.vcc file (inputvcc) and produce the vmod.h and vmod.c files.
#
# vmod.h contains the prototypes for the published functions, the module
# C-code should include this file to ensure type-consistency.
#
# vmod.c contains the symbols which VCC and varnishd will use to access
# the module: A structure of properly typed function pointers, the
# size of this structure in bytes, and the definition of the structure
# as a string, suitable for inclusion in the C-source of the compile VCL
# program.
"""
Read the vmod.vcc file (inputvcc) and produce:
vmod_if.h -- Prototypes for the implementation
vmod_if.c -- Magic glue & datastructures to make things a VMOD.
vmod_${name}.rst -- Extracted documentation
"""
# This script should work with both Python 2 and Python 3.
from
__future__
import
print_function
...
...
@@ -45,7 +41,7 @@ import re
import
optparse
import
unittest
from
os
import
unlink
from
os.path
import
dirname
,
basename
,
realpath
,
exists
from
os.path
import
dirname
,
realpath
,
exists
from
pprint
import
pprint
,
pformat
ctypes
=
{
...
...
@@ -88,7 +84,7 @@ def lwrap(s, w=72):
Wrap a c-prototype like string into a number of lines
"""
l
=
[]
p
=
""
p
=
""
while
len
(
s
)
>
w
:
y
=
s
[:
w
]
.
rfind
(
','
)
if
y
==
-
1
:
...
...
@@ -101,7 +97,7 @@ def lwrap(s, w=72):
if
len
(
s
)
>
0
:
l
.
append
(
p
+
s
)
return
l
#######################################################################
def
is_c_name
(
s
):
...
...
@@ -125,7 +121,7 @@ class FormatError(Exception):
#######################################################################
class
t
oken
(
object
):
class
T
oken
(
object
):
def
__init__
(
self
,
ln
,
ch
,
tokstr
):
self
.
ln
=
ln
self
.
ch
=
ch
...
...
@@ -136,7 +132,7 @@ class token(object):
#######################################################################
class
v
mod
(
object
):
class
V
mod
(
object
):
def
__init__
(
self
,
nam
,
dnam
,
sec
):
if
not
is_c_name
(
nam
):
raise
ParseError
(
"Module name '
%
s' is illegal"
,
nam
)
...
...
@@ -160,9 +156,9 @@ class vmod(object):
self
.
funcs
.
append
(
fn
)
self
.
doc_order
.
append
(
fn
)
def
add_obj
(
self
,
o
bj
):
self
.
objs
.
append
(
o
bj
)
self
.
doc_order
.
append
(
o
bj
)
def
add_obj
(
self
,
o
):
self
.
objs
.
append
(
o
)
self
.
doc_order
.
append
(
o
)
def
c_proto
(
self
,
fo
):
for
o
in
self
.
objs
:
...
...
@@ -332,7 +328,7 @@ class vmod(object):
#######################################################################
class
f
unc
(
object
):
class
F
unc
(
object
):
def
__init__
(
self
,
nam
,
retval
,
al
):
#if not is_c_name(nam):
# raise Exception("Func name '%s' is illegal" % nam)
...
...
@@ -399,7 +395,7 @@ class func(object):
def
c_initializer
(
self
):
return
"
\t
vmod_"
+
self
.
cnam
+
",
\n
"
def
c_strspec
(
self
,
modnam
,
pfx
=
"
\t
"
):
def
c_strspec
(
self
,
modnam
,
pfx
=
"
\t
"
):
s
=
pfx
+
'"'
+
modnam
+
"."
+
self
.
nam
+
'
\\
0"
\n
'
s
+=
pfx
+
'"'
s
+=
"Vmod_"
+
modnam
+
"_Func."
+
self
.
cnam
+
'
\\
0"
\n
'
...
...
@@ -448,7 +444,7 @@ class func(object):
#######################################################################
class
o
bj
(
object
):
class
O
bj
(
object
):
def
__init__
(
self
,
nam
):
self
.
nam
=
nam
self
.
init
=
None
...
...
@@ -467,7 +463,7 @@ class obj(object):
def
set_init
(
self
,
f
):
self
.
init
=
f
self
.
fini
=
f
unc
(
f
.
nam
,
"VOID"
,
[])
self
.
fini
=
F
unc
(
f
.
nam
,
"VOID"
,
[])
self
.
init
.
cnam
+=
"__init"
self
.
fini
.
cnam
+=
"__fini"
...
...
@@ -549,7 +545,7 @@ class obj(object):
#######################################################################
class
a
rg
(
object
):
class
A
rg
(
object
):
def
__init__
(
self
,
typ
,
nam
=
None
,
det
=
None
):
self
.
nam
=
nam
self
.
typ
=
typ
...
...
@@ -589,7 +585,7 @@ def parse_enum2(tl):
raise
ParseError
(
"Expected
\"
}
\"
or
\"
,
\"
not
\"
%
s
\"
"
%
t
.
str
)
s
+=
"
\\
0"
return
a
rg
(
"ENUM"
,
det
=
s
)
return
A
rg
(
"ENUM"
,
det
=
s
)
#######################################################################
#
...
...
@@ -602,13 +598,13 @@ def parse_module(tl):
while
len
(
tl
.
tl
)
>
0
:
s
+=
" "
+
tl
.
get_token
()
.
str
dnm
=
s
[
1
:]
return
v
mod
(
nm
,
dnm
,
sec
)
return
V
mod
(
nm
,
dnm
,
sec
)
#######################################################################
#
#
def
parse_func
(
tl
,
rt_type
=
None
,
obj
=
None
):
def
parse_func
(
tl
,
rt_type
=
None
,
p
obj
=
None
):
al
=
list
()
if
rt_type
==
None
:
t
=
tl
.
get_token
()
...
...
@@ -619,8 +615,8 @@ def parse_func(tl, rt_type=None, obj=None):
t
=
tl
.
get_token
()
fname
=
t
.
str
if
obj
!=
None
and
fname
[
0
]
==
"."
and
is_c_name
(
fname
[
1
:]):
fname
=
obj
+
fname
if
p
obj
!=
None
and
fname
[
0
]
==
"."
and
is_c_name
(
fname
[
1
:]):
fname
=
p
obj
+
fname
elif
not
is_c_name
(
fname
):
raise
ParseError
(
"Function name '
%
s' is illegal"
,
fname
)
...
...
@@ -637,7 +633,7 @@ def parse_func(tl, rt_type=None, obj=None):
if
t
.
str
==
"ENUM"
:
al
.
append
(
parse_enum2
(
tl
))
elif
t
.
str
in
ctypes
:
al
.
append
(
a
rg
(
t
.
str
))
al
.
append
(
A
rg
(
t
.
str
))
elif
t
.
str
==
")"
:
break
else
:
...
...
@@ -655,7 +651,7 @@ def parse_func(tl, rt_type=None, obj=None):
"Expected
\"
)
\"
or
\"
,
\"
not
\"
%
s
\"
"
%
t
.
str
)
if
t
.
str
!=
")"
:
raise
ParseError
(
"End Of Input looking for ')'"
)
f
=
f
unc
(
fname
,
rt_type
,
al
)
f
=
F
unc
(
fname
,
rt_type
,
al
)
return
f
...
...
@@ -665,7 +661,7 @@ def parse_func(tl, rt_type=None, obj=None):
def
parse_obj
(
tl
):
f
=
parse_func
(
tl
,
"VOID"
)
o
=
o
bj
(
f
.
nam
)
o
=
O
bj
(
f
.
nam
)
o
.
set_init
(
f
)
return
o
...
...
@@ -673,7 +669,7 @@ def parse_obj(tl):
#######################################################################
# A section of the inputvcc, starting at a keyword
class
file_s
ection
(
object
):
class
FileS
ection
(
object
):
def
__init__
(
self
):
self
.
l
=
[]
self
.
tl
=
[]
...
...
@@ -701,7 +697,7 @@ class file_section(object):
if
l
==
""
:
return
for
j
in
l
.
split
():
self
.
tl
.
append
(
t
oken
(
ln
,
0
,
j
))
self
.
tl
.
append
(
T
oken
(
ln
,
0
,
j
))
def
parse
(
self
,
vx
):
t
=
self
.
get_token
()
...
...
@@ -729,7 +725,7 @@ class file_section(object):
elif
t
.
str
==
"$Method"
:
if
len
(
vx
)
!=
2
:
raise
FormatError
(
"$Method outside $Object"
,
""
)
o
=
parse_func
(
self
,
obj
=
vx
[
1
]
.
nam
)
o
=
parse_func
(
self
,
p
obj
=
vx
[
1
]
.
nam
)
vx
[
1
]
.
add_method
(
o
)
else
:
raise
FormatError
(
"Unknown keyword:
%
s"
%
t
.
str
,
""
)
...
...
@@ -792,20 +788,20 @@ def runmain(inputvcc, outputname="vcc_if"):
#######################################################################
# First collect the copyright: All initial lines starting with '#'
copyright
=
[]
copy
_
right
=
[]
while
len
(
lines
[
0
])
>
0
and
lines
[
0
][
0
]
==
"#"
:
ln
+=
1
copyright
.
append
(
lines
.
pop
(
0
))
copy
_
right
.
append
(
lines
.
pop
(
0
))
if
len
(
copyright
)
>
0
:
if
copyright
[
0
]
==
"#-"
:
copyright
=
[]
if
len
(
copy
_
right
)
>
0
:
if
copy
_
right
[
0
]
==
"#-"
:
copy
_
right
=
[]
else
:
while
polish
(
copyright
):
while
polish
(
copy
_
right
):
continue
if
False
:
for
i
in
copyright
:
for
i
in
copy
_
right
:
print
(
"(C)
\t
"
,
i
)
#######################################################################
...
...
@@ -820,14 +816,14 @@ def runmain(inputvcc, outputname="vcc_if"):
}
sl
=
[]
sc
=
file_s
ection
()
sc
=
FileS
ection
()
sl
.
append
(
sc
)
while
len
(
lines
)
>
0
:
ln
+=
1
l
=
lines
.
pop
(
0
)
j
=
l
.
split
()
if
len
(
j
)
>
0
and
j
[
0
]
in
keywords
:
sc
=
file_s
ection
()
sc
=
FileS
ection
()
sl
.
append
(
sc
)
sc
.
add_line
(
ln
,
l
)
...
...
@@ -844,7 +840,8 @@ def runmain(inputvcc, outputname="vcc_if"):
pprint
(
str
(
e
))
exit
(
-
1
)
except
FormatError
as
e
:
print
(
"ERROR: Format error reading
\"
%
s
\"
:
%
s"
%
(
inputvcc
,
pformat
(
e
.
msg
)))
print
(
"ERROR: Format error reading
\"
%
s
\"
:
%
s"
%
(
inputvcc
,
pformat
(
e
.
msg
)))
print
(
e
.
details
)
exit
(
-
2
)
...
...
@@ -865,14 +862,11 @@ def runmain(inputvcc, outputname="vcc_if"):
vx
[
0
]
.
c_proto
(
fh
)
fc
.
write
(
"""#include "config.h"
#include "vrt.h"
#include "vcc_if.h"
#include "vmod_abi.h"
"""
)
fc
.
write
(
'#include "config.h"
\n
'
)
fc
.
write
(
'#include "vrt.h"
\n
'
)
fc
.
write
(
'#include "vcc_if.h"
\n
'
)
fc
.
write
(
'#include "vmod_abi.h"
\n
'
)
fc
.
write
(
'
\n
'
)
vx
[
0
]
.
c_typedefs
(
fc
)
vx
[
0
]
.
c_vmod
(
fc
)
...
...
@@ -886,12 +880,12 @@ def runmain(inputvcc, outputname="vcc_if"):
vx
[
0
]
.
doc_dump
(
fp
,
suf
)
if
len
(
copyright
)
>
0
:
if
len
(
copy
_
right
)
>
0
:
fp
.
write
(
"
\n
"
)
fp
.
write
(
"COPYRIGHT
\n
"
)
fp
.
write
(
"=========
\n
"
)
fp
.
write
(
"
\n
::
\n\n
"
)
for
i
in
copyright
:
for
i
in
copy
_
right
:
fp
.
write
(
"
%
s
\n
"
%
i
)
fp
.
write
(
"
\n
"
)
...
...
@@ -901,25 +895,27 @@ if __name__ == "__main__":
oparser
=
optparse
.
OptionParser
(
usage
=
usagetext
)
oparser
.
add_option
(
'-N'
,
'--strict'
,
action
=
'store_true'
,
default
=
False
,
help
=
"Be strict when parsing input file. (vmod.vcc)"
)
help
=
"Be strict when parsing input file. (vmod.vcc)"
)
oparser
.
add_option
(
''
,
'--runtests'
,
action
=
'store_true'
,
default
=
False
,
dest
=
"runtests"
,
help
=
optparse
.
SUPPRESS_HELP
)
dest
=
"runtests"
,
help
=
optparse
.
SUPPRESS_HELP
)
(
opts
,
args
)
=
oparser
.
parse_args
()
if
opts
.
runtests
:
del
sys
.
argv
[
1
]
# Pop off --runtests, pass remaining to unittest.
# Pop off --runtests, pass remaining to unittest.
del
sys
.
argv
[
1
]
unittest
.
main
()
exit
()
i
nput
vcc
=
None
i
_
vcc
=
None
if
len
(
args
)
==
1
and
exists
(
args
[
0
]):
i
nput
vcc
=
args
[
0
]
i
_
vcc
=
args
[
0
]
elif
exists
(
"vmod.vcc"
):
if
not
i
nput
vcc
:
input
vcc
=
"vmod.vcc"
if
not
i
_
vcc
:
i_
vcc
=
"vmod.vcc"
else
:
print
(
"ERROR: No vmod.vcc file supplied or found."
,
file
=
sys
.
stderr
)
print
(
"ERROR: No vmod.vcc file supplied or found."
,
file
=
sys
.
stderr
)
oparser
.
print_help
()
exit
(
-
1
)
runmain
(
i
nput
vcc
)
runmain
(
i
_
vcc
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment