xref: /OK3568_Linux_fs/kernel/Documentation/sphinx/kernel_include.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/usr/bin/env python3
2*4882a593Smuzhiyun# -*- coding: utf-8; mode: python -*-
3*4882a593Smuzhiyun# pylint: disable=R0903, C0330, R0914, R0912, E0401
4*4882a593Smuzhiyun
5*4882a593Smuzhiyunu"""
6*4882a593Smuzhiyun    kernel-include
7*4882a593Smuzhiyun    ~~~~~~~~~~~~~~
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun    Implementation of the ``kernel-include`` reST-directive.
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun    :copyright:  Copyright (C) 2016  Markus Heiser
12*4882a593Smuzhiyun    :license:    GPL Version 2, June 1991 see linux/COPYING for details.
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun    The ``kernel-include`` reST-directive is a replacement for the ``include``
15*4882a593Smuzhiyun    directive. The ``kernel-include`` directive expand environment variables in
16*4882a593Smuzhiyun    the path name and allows to include files from arbitrary locations.
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun    .. hint::
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun      Including files from arbitrary locations (e.g. from ``/etc``) is a
21*4882a593Smuzhiyun      security risk for builders. This is why the ``include`` directive from
22*4882a593Smuzhiyun      docutils *prohibit* pathnames pointing to locations *above* the filesystem
23*4882a593Smuzhiyun      tree where the reST document with the include directive is placed.
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun    Substrings of the form $name or ${name} are replaced by the value of
26*4882a593Smuzhiyun    environment variable name. Malformed variable names and references to
27*4882a593Smuzhiyun    non-existing variables are left unchanged.
28*4882a593Smuzhiyun"""
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun# ==============================================================================
31*4882a593Smuzhiyun# imports
32*4882a593Smuzhiyun# ==============================================================================
33*4882a593Smuzhiyun
34*4882a593Smuzhiyunimport os.path
35*4882a593Smuzhiyun
36*4882a593Smuzhiyunfrom docutils import io, nodes, statemachine
37*4882a593Smuzhiyunfrom docutils.utils.error_reporting import SafeString, ErrorString
38*4882a593Smuzhiyunfrom docutils.parsers.rst import directives
39*4882a593Smuzhiyunfrom docutils.parsers.rst.directives.body import CodeBlock, NumberLines
40*4882a593Smuzhiyunfrom docutils.parsers.rst.directives.misc import Include
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun__version__  = '1.0'
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun# ==============================================================================
45*4882a593Smuzhiyundef setup(app):
46*4882a593Smuzhiyun# ==============================================================================
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun    app.add_directive("kernel-include", KernelInclude)
49*4882a593Smuzhiyun    return dict(
50*4882a593Smuzhiyun        version = __version__,
51*4882a593Smuzhiyun        parallel_read_safe = True,
52*4882a593Smuzhiyun        parallel_write_safe = True
53*4882a593Smuzhiyun    )
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun# ==============================================================================
56*4882a593Smuzhiyunclass KernelInclude(Include):
57*4882a593Smuzhiyun# ==============================================================================
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun    u"""KernelInclude (``kernel-include``) directive"""
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun    def run(self):
62*4882a593Smuzhiyun        path = os.path.realpath(
63*4882a593Smuzhiyun            os.path.expandvars(self.arguments[0]))
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun        # to get a bit security back, prohibit /etc:
66*4882a593Smuzhiyun        if path.startswith(os.sep + "etc"):
67*4882a593Smuzhiyun            raise self.severe(
68*4882a593Smuzhiyun                'Problems with "%s" directive, prohibited path: %s'
69*4882a593Smuzhiyun                % (self.name, path))
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun        self.arguments[0] = path
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun        #return super(KernelInclude, self).run() # won't work, see HINTs in _run()
74*4882a593Smuzhiyun        return self._run()
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun    def _run(self):
77*4882a593Smuzhiyun        """Include a file as part of the content of this reST file."""
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun        # HINT: I had to copy&paste the whole Include.run method. I'am not happy
80*4882a593Smuzhiyun        # with this, but due to security reasons, the Include.run method does
81*4882a593Smuzhiyun        # not allow absolute or relative pathnames pointing to locations *above*
82*4882a593Smuzhiyun        # the filesystem tree where the reST document is placed.
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun        if not self.state.document.settings.file_insertion_enabled:
85*4882a593Smuzhiyun            raise self.warning('"%s" directive disabled.' % self.name)
86*4882a593Smuzhiyun        source = self.state_machine.input_lines.source(
87*4882a593Smuzhiyun            self.lineno - self.state_machine.input_offset - 1)
88*4882a593Smuzhiyun        source_dir = os.path.dirname(os.path.abspath(source))
89*4882a593Smuzhiyun        path = directives.path(self.arguments[0])
90*4882a593Smuzhiyun        if path.startswith('<') and path.endswith('>'):
91*4882a593Smuzhiyun            path = os.path.join(self.standard_include_path, path[1:-1])
92*4882a593Smuzhiyun        path = os.path.normpath(os.path.join(source_dir, path))
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun        # HINT: this is the only line I had to change / commented out:
95*4882a593Smuzhiyun        #path = utils.relative_path(None, path)
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun        path = nodes.reprunicode(path)
98*4882a593Smuzhiyun        encoding = self.options.get(
99*4882a593Smuzhiyun            'encoding', self.state.document.settings.input_encoding)
100*4882a593Smuzhiyun        e_handler=self.state.document.settings.input_encoding_error_handler
101*4882a593Smuzhiyun        tab_width = self.options.get(
102*4882a593Smuzhiyun            'tab-width', self.state.document.settings.tab_width)
103*4882a593Smuzhiyun        try:
104*4882a593Smuzhiyun            self.state.document.settings.record_dependencies.add(path)
105*4882a593Smuzhiyun            include_file = io.FileInput(source_path=path,
106*4882a593Smuzhiyun                                        encoding=encoding,
107*4882a593Smuzhiyun                                        error_handler=e_handler)
108*4882a593Smuzhiyun        except UnicodeEncodeError as error:
109*4882a593Smuzhiyun            raise self.severe('Problems with "%s" directive path:\n'
110*4882a593Smuzhiyun                              'Cannot encode input file path "%s" '
111*4882a593Smuzhiyun                              '(wrong locale?).' %
112*4882a593Smuzhiyun                              (self.name, SafeString(path)))
113*4882a593Smuzhiyun        except IOError as error:
114*4882a593Smuzhiyun            raise self.severe('Problems with "%s" directive path:\n%s.' %
115*4882a593Smuzhiyun                      (self.name, ErrorString(error)))
116*4882a593Smuzhiyun        startline = self.options.get('start-line', None)
117*4882a593Smuzhiyun        endline = self.options.get('end-line', None)
118*4882a593Smuzhiyun        try:
119*4882a593Smuzhiyun            if startline or (endline is not None):
120*4882a593Smuzhiyun                lines = include_file.readlines()
121*4882a593Smuzhiyun                rawtext = ''.join(lines[startline:endline])
122*4882a593Smuzhiyun            else:
123*4882a593Smuzhiyun                rawtext = include_file.read()
124*4882a593Smuzhiyun        except UnicodeError as error:
125*4882a593Smuzhiyun            raise self.severe('Problem with "%s" directive:\n%s' %
126*4882a593Smuzhiyun                              (self.name, ErrorString(error)))
127*4882a593Smuzhiyun        # start-after/end-before: no restrictions on newlines in match-text,
128*4882a593Smuzhiyun        # and no restrictions on matching inside lines vs. line boundaries
129*4882a593Smuzhiyun        after_text = self.options.get('start-after', None)
130*4882a593Smuzhiyun        if after_text:
131*4882a593Smuzhiyun            # skip content in rawtext before *and incl.* a matching text
132*4882a593Smuzhiyun            after_index = rawtext.find(after_text)
133*4882a593Smuzhiyun            if after_index < 0:
134*4882a593Smuzhiyun                raise self.severe('Problem with "start-after" option of "%s" '
135*4882a593Smuzhiyun                                  'directive:\nText not found.' % self.name)
136*4882a593Smuzhiyun            rawtext = rawtext[after_index + len(after_text):]
137*4882a593Smuzhiyun        before_text = self.options.get('end-before', None)
138*4882a593Smuzhiyun        if before_text:
139*4882a593Smuzhiyun            # skip content in rawtext after *and incl.* a matching text
140*4882a593Smuzhiyun            before_index = rawtext.find(before_text)
141*4882a593Smuzhiyun            if before_index < 0:
142*4882a593Smuzhiyun                raise self.severe('Problem with "end-before" option of "%s" '
143*4882a593Smuzhiyun                                  'directive:\nText not found.' % self.name)
144*4882a593Smuzhiyun            rawtext = rawtext[:before_index]
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun        include_lines = statemachine.string2lines(rawtext, tab_width,
147*4882a593Smuzhiyun                                                  convert_whitespace=True)
148*4882a593Smuzhiyun        if 'literal' in self.options:
149*4882a593Smuzhiyun            # Convert tabs to spaces, if `tab_width` is positive.
150*4882a593Smuzhiyun            if tab_width >= 0:
151*4882a593Smuzhiyun                text = rawtext.expandtabs(tab_width)
152*4882a593Smuzhiyun            else:
153*4882a593Smuzhiyun                text = rawtext
154*4882a593Smuzhiyun            literal_block = nodes.literal_block(rawtext, source=path,
155*4882a593Smuzhiyun                                    classes=self.options.get('class', []))
156*4882a593Smuzhiyun            literal_block.line = 1
157*4882a593Smuzhiyun            self.add_name(literal_block)
158*4882a593Smuzhiyun            if 'number-lines' in self.options:
159*4882a593Smuzhiyun                try:
160*4882a593Smuzhiyun                    startline = int(self.options['number-lines'] or 1)
161*4882a593Smuzhiyun                except ValueError:
162*4882a593Smuzhiyun                    raise self.error(':number-lines: with non-integer '
163*4882a593Smuzhiyun                                     'start value')
164*4882a593Smuzhiyun                endline = startline + len(include_lines)
165*4882a593Smuzhiyun                if text.endswith('\n'):
166*4882a593Smuzhiyun                    text = text[:-1]
167*4882a593Smuzhiyun                tokens = NumberLines([([], text)], startline, endline)
168*4882a593Smuzhiyun                for classes, value in tokens:
169*4882a593Smuzhiyun                    if classes:
170*4882a593Smuzhiyun                        literal_block += nodes.inline(value, value,
171*4882a593Smuzhiyun                                                      classes=classes)
172*4882a593Smuzhiyun                    else:
173*4882a593Smuzhiyun                        literal_block += nodes.Text(value, value)
174*4882a593Smuzhiyun            else:
175*4882a593Smuzhiyun                literal_block += nodes.Text(text, text)
176*4882a593Smuzhiyun            return [literal_block]
177*4882a593Smuzhiyun        if 'code' in self.options:
178*4882a593Smuzhiyun            self.options['source'] = path
179*4882a593Smuzhiyun            codeblock = CodeBlock(self.name,
180*4882a593Smuzhiyun                                  [self.options.pop('code')], # arguments
181*4882a593Smuzhiyun                                  self.options,
182*4882a593Smuzhiyun                                  include_lines, # content
183*4882a593Smuzhiyun                                  self.lineno,
184*4882a593Smuzhiyun                                  self.content_offset,
185*4882a593Smuzhiyun                                  self.block_text,
186*4882a593Smuzhiyun                                  self.state,
187*4882a593Smuzhiyun                                  self.state_machine)
188*4882a593Smuzhiyun            return codeblock.run()
189*4882a593Smuzhiyun        self.state_machine.insert_input(include_lines, path)
190*4882a593Smuzhiyun        return []
191