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
//! XCompose helpers and types.
//!
//! The entry point to this module is the [`ComposeTable`].
pub use table::{ComposeTable, FeedResult, Iter, MatchRule, MatchStep, State};
#[expect(unused_imports)]
use {crate::xkb::ContextBuilder, secure_execution::requires_secure_execution};
use {
crate::xkb::{
Context,
code::Code,
code_map::CodeMap,
code_slice::CodeSlice,
compose::{
lexer::Lexer,
parser::{Line, parse_line},
},
diagnostic::{DiagnosticHandler, DiagnosticKind, DiagnosticSink},
interner::Interner,
meaning::MeaningCache,
span::SpanExt,
},
bstr::ByteSlice,
hashbrown::HashSet,
kbvm_proc::ad_hoc_display,
std::{
fs::File,
io::{BufRead, BufReader},
mem,
path::{Path, PathBuf},
sync::Arc,
},
thiserror::Error,
};
#[macro_use]
mod macros;
mod lexer;
mod parser;
mod table;
#[cfg(test)]
mod tests;
mod token;
#[derive(Clone)]
enum TopLevel {
Path(PathBuf),
Buffer(Vec<u8>),
}
/// A builder for a compose table.
///
/// This type is created using [`Context::compose_table_builder`].
#[derive(Clone)]
pub struct ComposeTableBuilder<'a> {
context: &'a Context,
locale: Option<String>,
top_level: Option<TopLevel>,
}
impl Context {
/// Creates a builder for a compose table.
///
/// This function is only available when the `compose` feature is enabled.
pub fn compose_table_builder(&self) -> ComposeTableBuilder<'_> {
ComposeTableBuilder {
context: self,
locale: None,
top_level: None,
}
}
}
impl ComposeTableBuilder<'_> {
/// Sets the locale to use for the compose file.
///
/// If this function is not called, the locale of the context is used. See
/// [`Context::locale`].
pub fn locale(&mut self, locale: &str) {
self.locale = Some(locale.to_string());
}
/// Sets the buffer that contains the compose table.
///
/// This overwrites any buffer or file previously set for this builder.
///
/// # Security
///
/// Compose files can contain arbitrary include paths. If you are executing code in a
/// set-user-ID binary, care must be taken so that this does not lead to information
/// disclosure. The [`requires_secure_execution`] function might be useful for this.
pub fn buffer(&mut self, buffer: impl AsRef<[u8]>) {
self.top_level = Some(TopLevel::Buffer(buffer.as_ref().to_vec()));
}
/// Sets the path to the file that contains the compose table.
///
/// This overwrites any buffer or file previously set for this builder.
///
/// # Security
///
/// Compose files can contain arbitrary include paths. If you are executing code in a
/// set-user-ID binary, care must be taken so that this does not lead to information
/// disclosure. The [`requires_secure_execution`] function might be useful for this.
pub fn file(&mut self, path: impl AsRef<Path>) {
self.top_level = Some(TopLevel::Path(path.as_ref().to_path_buf()));
}
/// Builds the compose table.
///
/// Building a compose table requires access to the user's locale. If the [`Self::locale`]
/// function was not called, the [locale of the context](Context::locale) is used.
///
/// If [`Self::buffer`] or [`Self::file`] were called, that information is used to find the
/// compose table.
///
/// Otherwise, first the `<xlocaledir>` is determined:
///
/// - If environment variables are [enabled](ContextBuilder::enable_environment) and
/// `$XLOCALEDIR` is set, that is the value of `<xlocaledir>`.
/// - Otherwise, the value of `<xlocaledir>` is `/usr/share/X11/locale`.
///
/// Then, the path to the compose table is determined as follows:
///
/// - If environment variables are enabled, the first readable file from the following list is
/// used:
///
/// - `$XCOMPOSEFILE` if it is set.
/// - `$XDG_CONFIG_HOME/XCompose` if `$XDG_CONFIG_HOME` is set.
/// - `$HOME/.config/XCompose` if `$XDG_CONFIG_HOME` is not set and `$HOME` is set.
/// - `$HOME/.XCompose` if `$HOME` is set.
///
/// - Otherwise, if the environment is disabled or none of the files exist, the following file
/// is used if it is readable:
///
/// - `<xlocaledir>/??`. The value of `??` is described below.
///
/// - Otherwise, this function returns `None`.
///
/// The value of `??` above is determined as follows:
///
/// - First, the locale is resolved to the canonical locale name by reading
/// `<xlocaledir>/locale.alias`.
/// - Then, the value of `??` determined by reading `<xlocaledir>/compose.dir`.
///
/// If this fails, this function returns `None`.
pub fn build(self, mut diagnostics: impl DiagnosticHandler) -> Option<ComposeTable> {
self.build_(&mut DiagnosticSink::new(&mut diagnostics))
}
fn build_(self, diagnostics: &mut DiagnosticSink<'_, '_>) -> Option<ComposeTable> {
let locale = self.locale.as_deref().unwrap_or(&self.context.env.locale);
let locale = self.resolve_locale(locale);
let compose_file_path = self.get_compose_file(&locale);
let mut toplevel_path = None;
let toplevel = match self.top_level {
Some(t) => t,
None => 'toplevel: {
enum Candidate {
Xcomposefile,
XdgConfigHome,
Home,
ComposeFilePath,
}
use Candidate::*;
for candidate in [Xcomposefile, XdgConfigHome, Home, ComposeFilePath] {
let env = &self.context.env;
let path = match candidate {
Xcomposefile => match env.xcomposefile.as_deref() {
Some(f) => f.to_string(),
_ => continue,
},
XdgConfigHome => match env.xdg_config_home.as_deref() {
Some(f) => format!("{f}/XCompose"),
_ => match env.home.as_deref() {
Some(f) => format!("{f}/.config/XCompose"),
_ => continue,
},
},
Home => match env.home.as_deref() {
Some(f) => format!("{f}/.XCompose"),
_ => continue,
},
ComposeFilePath => match compose_file_path.as_ref() {
Some(f) => f.clone(),
None => continue,
},
};
if let Ok(c) = std::fs::read(&path) {
toplevel_path = Some(PathBuf::from(path));
break 'toplevel TopLevel::Buffer(c);
}
}
return None;
}
};
let buffer = match toplevel {
TopLevel::Buffer(b) => b,
TopLevel::Path(p) => match std::fs::read(&p).ok() {
Some(b) => {
toplevel_path = Some(p);
b
}
_ => return None,
},
};
// println!("{}", buffer.as_bstr());
let mut map = CodeMap::default();
let mut interner = Interner::default();
let path = toplevel_path.map(Arc::new);
let code = Code::new(&Arc::new(buffer));
let span = map.add(path.as_ref(), None, &code);
let lexer = Lexer::new(&code, span.lo);
let mut tokens = vec![];
let mut meaning_cache = MeaningCache::default();
let mut productions = vec![];
let mut lexers = vec![(None, lexer)];
let mut processed_includes = 0;
let mut current_includes = HashSet::new();
let mut processed_statements = 0;
macro_rules! pop {
() => {
if let Some((Some(path), _)) = lexers.pop() {
current_includes.remove(&path);
}
};
}
while let Some((_, lexer)) = lexers.last_mut() {
tokens.clear();
let res = lexer.lex_line(&mut map, diagnostics, &mut interner, &mut tokens);
if let Err(e) = res {
pop!();
diagnostics.push(&mut map, e.val.diagnostic_kind(), e);
continue;
}
if tokens.is_empty() {
pop!();
continue;
}
let line = parse_line(
&mut map,
diagnostics,
&mut interner,
&mut meaning_cache,
&tokens,
&self.context.env,
compose_file_path.as_deref(),
);
let line = match line {
Ok(Some(l)) => l,
Ok(None) => continue,
Err(e) => {
diagnostics.push(&mut map, e.val.diagnostic_kind(), e);
continue;
}
};
if processed_statements >= self.context.max_runtime {
diagnostics.push(
&mut map,
DiagnosticKind::MaxRuntimeReached,
ad_hoc_display!("maximum number of statements reached").spanned2(line.span),
);
break;
}
processed_statements += 1;
let include = match line.val {
Line::Production(p) => {
if productions.len() as u64 >= self.context.max_compose_rules {
diagnostics.push(
&mut map,
DiagnosticKind::MaxComposeRulesReached,
ad_hoc_display!("maximum number of compose rules reached")
.spanned2(line.span),
);
break;
}
productions.push(p.spanned2(line.span));
continue;
}
Line::Include(i) => i,
};
if processed_includes >= self.context.max_includes {
diagnostics.push(
&mut map,
DiagnosticKind::MaxIncludesReached,
ad_hoc_display!("maximum number of includes reached").spanned2(line.span),
);
continue;
}
processed_includes += 1;
if lexers.len() as u64 > self.context.max_include_depth {
diagnostics.push(
&mut map,
DiagnosticKind::MaxIncludeDepthReached,
ad_hoc_display!("maximum include depth ({}) reached", self.context.max_include_depth => u64).spanned2(line.span),
);
continue;
}
if !current_includes.insert(include.to_owned()) {
diagnostics.push(
&mut map,
DiagnosticKind::RecursiveInclude,
ad_hoc_display!("ignoring recursive include").spanned2(line.span),
);
continue;
}
let Ok(path) = include.as_bstr().to_str() else {
diagnostics.push(
&mut map,
DiagnosticKind::NonUTF8Path,
ad_hoc_display!("ignoring non-UTF-8 path").spanned2(line.span),
);
continue;
};
let buffer = match std::fs::read(path) {
Ok(b) => b,
Err(e) => {
#[derive(Debug, Error)]
#[error("could not read file `{}`", .0.as_bstr())]
struct E(CodeSlice<'static>, #[source] std::io::Error);
diagnostics.push(
&mut map,
DiagnosticKind::FileReadFailed,
E(include, e).spanned2(line.span),
);
continue;
}
};
let path = Arc::new(Path::new(path).to_path_buf());
let code = Code::new(&Arc::new(buffer));
let span = map.add(Some(&path), Some(line.span), &code);
let lexer = Lexer::new(&code, span.lo);
lexers.push((Some(include), lexer));
}
let table = ComposeTable::new(&mut map, diagnostics, &productions);
Some(table)
}
fn get_compose_file(&self, mut resolved_locale: &str) -> Option<String> {
if resolved_locale == "C" {
resolved_locale = "en_US.UTF-8";
}
let mut ll = self.read_locale_lines("compose.dir")?;
while let Some((l, r)) = ll.next() {
if r == resolved_locale.as_bytes() {
return Some(format!("{}/{}", self.context.env.xlocaledir, l.as_bstr()));
}
}
None
}
fn resolve_locale(&self, locale: &str) -> String {
let Some(mut ll) = self.read_locale_lines("locale.alias") else {
return locale.to_string();
};
while let Some((l, r)) = ll.next() {
if l == locale.as_bytes() {
return r.as_bstr().to_string();
}
}
locale.to_string()
}
fn read_locale_lines(&self, filename: &str) -> Option<LocaleLines> {
let path = Path::new(&self.context.env.xlocaledir).join(filename);
let file = File::open(&path).ok()?;
Some(LocaleLines {
buf: Vec::new(),
reader: BufReader::new(file),
})
}
}
struct LocaleLines {
buf: Vec<u8>,
reader: BufReader<File>,
}
impl LocaleLines {
fn next<'a>(&'a mut self) -> Option<(&'a [u8], &'a [u8])> {
loop {
self.buf.clear();
if self.reader.read_until(b'\n', &mut self.buf).ok()? == 0 {
return None;
}
let mut buf = self.buf.as_bytes();
if let Some((l, _)) = buf.split_once_str(b"#") {
buf = l;
}
let (l, r) = if let Some((l, r)) = buf.split_once_str(b":") {
(l, r)
} else if let Some(idx) = buf.find_byteset([b' ', b'\t']) {
buf.split_at(idx)
} else {
continue;
};
let ret = Some((l.trim_ascii(), r.trim_ascii()));
unsafe {
type Ret<'b> = Option<(&'b [u8], &'b [u8])>;
// SAFETY: This is a compiler bug that will never be fixed.
return mem::transmute::<Ret<'_>, Ret<'a>>(ret);
}
}
}
}