blob: 451e68dd6620772a6110b60f48f478afd17f186d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2 class for handling configuration data files
3
4 Reads a .conf file and obtains its metadata
5
6"""
7
8# Copyright (C) 2003, 2004 Chris Larson
9# Copyright (C) 2003, 2004 Phil Blundell
10#
Brad Bishopc342db32019-05-15 21:57:59 -040011# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013
14import errno
15import re
16import os
17import bb.utils
18from bb.parse import ParseError, resolve_file, ast, logger, handle
19
20__config_regexp__ = re.compile( r"""
21 ^
Brad Bishopd7bf8c12018-02-25 22:55:05 -050022 (?P<exp>export\s+)?
Andrew Geissler5f350902021-07-23 13:09:54 -040023 (?P<var>[a-zA-Z0-9\-_+.${}/~:]+?)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024 (\[(?P<flag>[a-zA-Z0-9\-_+.]+)\])?
25
26 \s* (
27 (?P<colon>:=) |
28 (?P<lazyques>\?\?=) |
29 (?P<ques>\?=) |
30 (?P<append>\+=) |
31 (?P<prepend>=\+) |
32 (?P<predot>=\.) |
33 (?P<postdot>\.=) |
34 =
35 ) \s*
36
37 (?!'[^']*'[^']*'$)
38 (?!\"[^\"]*\"[^\"]*\"$)
39 (?P<apo>['\"])
40 (?P<value>.*)
41 (?P=apo)
42 $
43 """, re.X)
44__include_regexp__ = re.compile( r"include\s+(.+)" )
45__require_regexp__ = re.compile( r"require\s+(.+)" )
Brad Bishop6e60e8b2018-02-01 10:27:11 -050046__export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
47__unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
48__unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.]+)\]$" )
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049
50def init(data):
Andrew Geissler595f6302022-01-24 19:11:47 +000051 return
Patrick Williamsc124f4f2015-09-15 14:41:29 -050052
53def supports(fn, d):
54 return fn[-5:] == ".conf"
55
Brad Bishopd7bf8c12018-02-25 22:55:05 -050056def include(parentfn, fns, lineno, data, error_out):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 """
58 error_out: A string indicating the verb (e.g. "include", "inherit") to be
59 used in a ParseError that will be raised if the file to be included could
60 not be included. Specify False to avoid raising an error in this case.
61 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 fns = data.expand(fns)
63 parentfn = data.expand(parentfn)
64
65 # "include" or "require" accept zero to n space-separated file names to include.
66 for fn in fns.split():
67 include_single_file(parentfn, fn, lineno, data, error_out)
68
69def include_single_file(parentfn, fn, lineno, data, error_out):
70 """
71 Helper function for include() which does not expand or split its parameters.
72 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 if parentfn == fn: # prevent infinite recursion
74 return None
75
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 if not os.path.isabs(fn):
77 dname = os.path.dirname(parentfn)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050078 bbpath = "%s:%s" % (dname, data.getVar("BBPATH"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 abs_fn, attempts = bb.utils.which(bbpath, fn, history=True)
80 if abs_fn and bb.parse.check_dependency(data, abs_fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081 logger.warning("Duplicate inclusion for %s in %s" % (abs_fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082 for af in attempts:
83 bb.parse.mark_dependency(data, af)
84 if abs_fn:
85 fn = abs_fn
86 elif bb.parse.check_dependency(data, fn):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050087 logger.warning("Duplicate inclusion for %s in %s" % (fn, data.getVar('FILE')))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088
89 try:
90 bb.parse.handle(fn, data, True)
91 except (IOError, OSError) as exc:
92 if exc.errno == errno.ENOENT:
93 if error_out:
94 raise ParseError("Could not %s file %s" % (error_out, fn), parentfn, lineno)
Andrew Geisslerd1e89492021-02-12 15:35:20 -060095 logger.debug2("CONF file '%s' not found", fn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096 else:
97 if error_out:
98 raise ParseError("Could not %s file %s: %s" % (error_out, fn, exc.strerror), parentfn, lineno)
99 else:
100 raise ParseError("Error parsing %s: %s" % (fn, exc.strerror), parentfn, lineno)
101
102# We have an issue where a UI might want to enforce particular settings such as
103# an empty DISTRO variable. If configuration files do something like assigning
104# a weak default, it turns out to be very difficult to filter out these changes,
105# particularly when the weak default might appear half way though parsing a chain
106# of configuration files. We therefore let the UIs hook into configuration file
107# parsing. This turns out to be a hard problem to solve any other way.
108confFilters = []
109
110def handle(fn, data, include):
111 init(data)
112
113 if include == 0:
114 oldfile = None
115 else:
116 oldfile = data.getVar('FILE', False)
117
118 abs_fn = resolve_file(fn, data)
Brad Bishop64c979e2019-11-04 13:55:29 -0500119 with open(abs_fn, 'r') as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120
Brad Bishop64c979e2019-11-04 13:55:29 -0500121 statements = ast.StatementGroup()
122 lineno = 0
123 while True:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 lineno = lineno + 1
Brad Bishop64c979e2019-11-04 13:55:29 -0500125 s = f.readline()
126 if not s:
127 break
Andrew Geissler615f2f12022-07-15 14:00:58 -0500128 origlineno = lineno
129 origline = s
Brad Bishop64c979e2019-11-04 13:55:29 -0500130 w = s.strip()
131 # skip empty lines
132 if not w:
133 continue
134 s = s.rstrip()
135 while s[-1] == '\\':
Andrew Geissler615f2f12022-07-15 14:00:58 -0500136 line = f.readline()
137 origline += line
138 s2 = line.rstrip()
Brad Bishop64c979e2019-11-04 13:55:29 -0500139 lineno = lineno + 1
140 if (not s2 or s2 and s2[0] != "#") and s[0] == "#" :
Andrew Geissler615f2f12022-07-15 14:00:58 -0500141 bb.fatal("There is a confusing multiline, partially commented expression starting on line %s of file %s:\n%s\nPlease clarify whether this is all a comment or should be parsed." % (origlineno, fn, origline))
142
Brad Bishop64c979e2019-11-04 13:55:29 -0500143 s = s[:-1] + s2
144 # skip comments
145 if s[0] == '#':
146 continue
147 feeder(lineno, s, abs_fn, statements)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148
149 # DONE WITH PARSING... time to evaluate
150 data.setVar('FILE', abs_fn)
151 statements.eval(data)
152 if oldfile:
153 data.setVar('FILE', oldfile)
154
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 for f in confFilters:
156 f(fn, data)
157
158 return data
159
160def feeder(lineno, s, fn, statements):
161 m = __config_regexp__.match(s)
162 if m:
163 groupd = m.groupdict()
164 ast.handleData(statements, fn, lineno, groupd)
165 return
166
167 m = __include_regexp__.match(s)
168 if m:
169 ast.handleInclude(statements, fn, lineno, m, False)
170 return
171
172 m = __require_regexp__.match(s)
173 if m:
174 ast.handleInclude(statements, fn, lineno, m, True)
175 return
176
177 m = __export_regexp__.match(s)
178 if m:
179 ast.handleExport(statements, fn, lineno, m)
180 return
181
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600182 m = __unset_regexp__.match(s)
183 if m:
184 ast.handleUnset(statements, fn, lineno, m)
185 return
186
187 m = __unset_flag_regexp__.match(s)
188 if m:
189 ast.handleUnsetFlag(statements, fn, lineno, m)
190 return
191
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500192 raise ParseError("unparsed line: '%s'" % s, fn, lineno);
193
194# Add us to the handlers list
195from bb.parse import handlers
196handlers.append({'supports': supports, 'handle': handle, 'init': init})
197del handlers