aboutsummaryrefslogtreecommitdiff
path: root/scripts/coccinelle/ctrl-reply.cocci
blob: d6e9aeedd700e4878d7a74c14f44aa054ad354e2 (plain)
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
// Script to edit control_*.c for refactored control reply output functions

@ initialize:python @
@@
import re
from coccilib.report import *

# reply strings "NNN-foo", "NNN+foo", "NNN foo", etc.
r = re.compile(r'^"(\d+)([ +-])(.*)\\r\\n"$')

# Generate name of function to call based on which separator character
# comes between the numeric code and the text
def idname(sep, base):
    if sep == '+':
        return base + "datareply"
    elif sep == '-':
        return base + "midreply"
    else:
        return base + "endreply"

# Generate the actual replacements used by the rules
def gen(s, base, p):
    pos = p[0]
    print_report(pos, "%s %s" % (base, s))
    m = r.match(s)
    if m is None:
        # String not correct format, so fail match
        cocci.include_match(False)
        print_report(pos, "BAD STRING %s" % s)
        return

    code, sep, s1 = m.groups()

    if r'\r\n' in s1:
        # Extra CRLF in string, so fail match
        cocci.include_match(False)
        print_report(pos, "extra CRLF in string %s" % s)
        return

    coccinelle.code = code
    # Need a string that is a single C token, because Coccinelle only allows
    # "identifiers" to be output from Python scripts?
    coccinelle.body = '"%s"' % s1
    coccinelle.id = idname(sep, base)
    return

@ match @
identifier f;
position p;
expression E;
constant s;
@@
(
 connection_printf_to_buf@f@p(E, s, ...)
|
 connection_write_str_to_buf@f@p(s, E)
)

@ script:python sc1 @
s << match.s;
p << match.p;
f << match.f;
id;
body;
code;
@@
if f == 'connection_printf_to_buf':
    gen(s, 'control_printf_', p)
elif f == 'connection_write_str_to_buf':
    gen(s, 'control_write_', p)
else:
    raise(ValueError("%s: %s" % (f, s)))

@ replace @
constant match.s;
expression match.E;
identifier match.f;
identifier sc1.body, sc1.id, sc1.code;
@@
(
-connection_write_str_to_buf@f(s, E)
+id(E, code, body)
|
-connection_printf_to_buf@f(E, s
+id(E, code, body
 , ...)
)