run_emitter_test_suite/
run-emitter-test-suite.rs

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
#![allow(missing_docs)]
#![warn(clippy::pedantic)]
#![allow(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss,
    clippy::items_after_statements,
    clippy::let_underscore_untyped,
    clippy::missing_errors_doc,
    clippy::missing_safety_doc,
    clippy::ptr_as_ptr,
    clippy::single_match_else,
    clippy::too_many_lines,
    clippy::uninlined_format_args,
    clippy::unreadable_literal
)]

mod cstr;

use self::cstr::CStr;
use core::fmt;
pub(crate) use core::primitive::u8 as yaml_char_t;
use libyml::api::ScalarEventData;
use libyml::document::{
    yaml_document_end_event_initialize,
    yaml_document_start_event_initialize,
};
use libyml::{
    yaml_alias_event_initialize, yaml_emitter_delete,
    yaml_emitter_emit, yaml_emitter_initialize,
    yaml_emitter_set_canonical, yaml_emitter_set_output,
    yaml_emitter_set_unicode, yaml_mapping_end_event_initialize,
    yaml_mapping_start_event_initialize, yaml_scalar_event_initialize,
    yaml_sequence_end_event_initialize,
    yaml_sequence_start_event_initialize,
    yaml_stream_end_event_initialize,
    yaml_stream_start_event_initialize, YamlAnyScalarStyle,
    YamlBlockMappingStyle, YamlBlockSequenceStyle,
    YamlDoubleQuotedScalarStyle, YamlEmitterError, YamlEmitterT,
    YamlEventT, YamlFoldedScalarStyle, YamlLiteralScalarStyle,
    YamlMemoryError, YamlPlainScalarStyle, YamlScalarStyleT,
    YamlSingleQuotedScalarStyle, YamlTagDirectiveT, YamlUtf8Encoding,
    YamlVersionDirectiveT, YamlWriterError,
};
use std::env;
use std::error::Error;
use std::ffi::c_void;
use std::fs::File;
use std::io::{self, Read, Write};
use std::mem::MaybeUninit;
use std::process::{self, ExitCode};
use std::ptr::{self, addr_of_mut};

#[derive(Debug)]
pub(crate) enum EmitterError {
    InitializationError(String),
    MemoryError(String),
    UnknownEvent(String),
    EmittingError(String),
    InternalError,
}

impl fmt::Display for EmitterError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EmitterError::InitializationError(msg)
            | EmitterError::MemoryError(msg)
            | EmitterError::UnknownEvent(msg)
            | EmitterError::EmittingError(msg) => {
                write!(f, "{}", msg)
            }
            EmitterError::InternalError => write!(f, "Internal error"),
        }
    }
}

impl Error for EmitterError {}

pub(crate) unsafe fn unsafe_main(
    stdin: &mut dyn Read,
    mut stdout: &mut dyn Write,
) -> Result<(), EmitterError> {
    let mut emitter = MaybeUninit::<YamlEmitterT>::uninit();
    let emitter = emitter.as_mut_ptr();
    if yaml_emitter_initialize(emitter).fail {
        return Err(EmitterError::InitializationError(
            "Could not initialize the emitter object".into(),
        ));
    }

    unsafe fn write_to_stdio(
        data: *mut c_void,
        buffer: *mut u8,
        size: u64,
    ) -> i32 {
        let stdout: *mut &mut dyn Write = data as _;
        let bytes = std::slice::from_raw_parts(buffer, size as usize);
        match (*stdout).write(bytes) {
            Ok(n) => n as i32,
            Err(_) => 0,
        }
    }

    yaml_emitter_set_output(
        emitter,
        write_to_stdio,
        addr_of_mut!(stdout).cast(),
    );
    yaml_emitter_set_canonical(emitter, false);
    yaml_emitter_set_unicode(emitter, false);

    let mut buf = ReadBuf::new();
    let mut event = MaybeUninit::<YamlEventT>::uninit();
    let event = event.as_mut_ptr();
    let result = loop {
        let line = match buf.get_line(stdin) {
            Some(line) => line,
            None => break Ok(()),
        };

        let mut anchor = [0u8; 256];
        let mut tag = [0u8; 256];
        let result = if line.starts_with(b"+STR") {
            yaml_stream_start_event_initialize(event, YamlUtf8Encoding)
        } else if line.starts_with(b"-STR") {
            yaml_stream_end_event_initialize(event)
        } else if line.starts_with(b"+DOC") {
            let implicit = !line[4..].starts_with(b" ---");
            yaml_document_start_event_initialize(
                event,
                ptr::null_mut::<YamlVersionDirectiveT>(),
                ptr::null_mut::<YamlTagDirectiveT>(),
                ptr::null_mut::<YamlTagDirectiveT>(),
                implicit,
            )
        } else if line.starts_with(b"-DOC") {
            let implicit = !line[4..].starts_with(b" ...");
            yaml_document_end_event_initialize(event, implicit)
        } else if line.starts_with(b"+MAP") {
            yaml_mapping_start_event_initialize(
                event,
                get_anchor(b'&', line, anchor.as_mut_ptr()),
                get_tag(line, tag.as_mut_ptr()),
                false,
                YamlBlockMappingStyle,
            )
        } else if line.starts_with(b"-MAP") {
            yaml_mapping_end_event_initialize(event)
        } else if line.starts_with(b"+SEQ") {
            yaml_sequence_start_event_initialize(
                event,
                get_anchor(b'&', line, anchor.as_mut_ptr()),
                get_tag(line, tag.as_mut_ptr()),
                false,
                YamlBlockSequenceStyle,
            )
        } else if line.starts_with(b"-SEQ") {
            yaml_sequence_end_event_initialize(event)
        } else if line.starts_with(b"=VAL") {
            let mut value = [0i8; 1024];
            let mut style = YamlAnyScalarStyle;
            get_value(line, value.as_mut_ptr(), &mut style);
            let implicit = get_tag(line, tag.as_mut_ptr()).is_null();
            let scalar_event_data = ScalarEventData {
                anchor: get_anchor(b'&', line, anchor.as_mut_ptr()),
                tag: get_tag(line, tag.as_mut_ptr()),
                value: value.as_mut_ptr() as *const yaml_char_t,
                length: -1,
                plain_implicit: implicit,
                quoted_implicit: implicit,
                style,
                _marker: core::marker::PhantomData,
            };

            yaml_scalar_event_initialize(event, scalar_event_data)
        } else if line.starts_with(b"=ALI") {
            yaml_alias_event_initialize(
                event,
                get_anchor(b'*', line, anchor.as_mut_ptr()),
            )
        } else {
            let line = line.as_mut_ptr() as *mut i8;
            break Err(EmitterError::UnknownEvent(format!(
                "Unknown event: '{}'",
                CStr::from_ptr(line)
            )));
        };

        if result.fail {
            break Err(EmitterError::MemoryError(
                "Memory error: Not enough memory for creating an event"
                    .into(),
            ));
        }
        if yaml_emitter_emit(emitter, event).fail {
            break Err(match (*emitter).error {
                YamlMemoryError => EmitterError::MemoryError(
                    "Memory error: Not enough memory for emitting"
                        .into(),
                ),
                YamlWriterError => {
                    EmitterError::EmittingError(format!(
                        "Writer error: {}",
                        CStr::from_ptr((*emitter).problem)
                    ))
                }
                YamlEmitterError => {
                    EmitterError::EmittingError(format!(
                        "Emitter error: {}",
                        CStr::from_ptr((*emitter).problem)
                    ))
                }
                _ => EmitterError::InternalError,
            });
        }
    };

    yaml_emitter_delete(emitter);
    result.map_err(Into::into)
}

struct ReadBuf {
    buf: [u8; 1024],
    offset: usize,
    filled: usize,
}

impl ReadBuf {
    fn new() -> Self {
        ReadBuf {
            buf: [0; 1024],
            offset: 0,
            filled: 0,
        }
    }

    fn get_line(&mut self, input: &mut dyn Read) -> Option<&mut [u8]> {
        loop {
            for i in self.offset..self.offset + self.filled {
                if self.buf[i] == b'\n' {
                    self.buf[i] = b'\0';
                    let line = &mut self.buf[self.offset..=i];
                    self.offset = i + 1;
                    self.filled -= line.len();
                    return Some(line);
                }
            }
            let mut remainder =
                &mut self.buf[self.offset + self.filled..];
            if remainder.is_empty() {
                if self.offset == 0 {
                    let _ = writeln!(
                        io::stderr(),
                        "Line too long: '{}'",
                        String::from_utf8_lossy(&self.buf),
                    );
                    process::abort();
                }
                self.buf.copy_within(self.offset.., 0);
                self.offset = 0;
                remainder = &mut self.buf;
            }
            let n = input.read(remainder).ok()?;
            self.filled += n;
            if n == 0 {
                return None;
            }
        }
    }
}

unsafe fn get_anchor(
    sigil: u8,
    line: &[u8],
    anchor: *mut u8,
) -> *mut u8 {
    let start = match line.iter().position(|ch| *ch == sigil) {
        Some(offset) => offset + 1,
        None => return ptr::null_mut(),
    };
    let end = match line[start..].iter().position(|ch| *ch == b' ') {
        Some(offset) => start + offset,
        None => line.len(),
    };
    anchor.copy_from_nonoverlapping(
        line[start..end].as_ptr(),
        end - start,
    );
    *anchor.add(end - start) = b'\0';
    anchor
}

unsafe fn get_tag(line: &[u8], tag: *mut u8) -> *mut u8 {
    let start = match line.iter().position(|ch| *ch == b'<') {
        Some(offset) => offset + 1,
        None => return ptr::null_mut(),
    };
    let end = match line[start..].iter().position(|ch| *ch == b'>') {
        Some(offset) => start + offset,
        None => return ptr::null_mut(),
    };
    tag.copy_from_nonoverlapping(
        line[start..end].as_ptr(),
        end - start,
    );
    *tag.add(end - start) = b'\0';
    tag
}

unsafe fn get_value(
    line: &[u8],
    value: *mut i8,
    style: *mut YamlScalarStyleT,
) {
    let line_len = line.len();
    let line = line.as_ptr() as *mut i8;
    let mut start = ptr::null_mut::<i8>();
    let end = line.add(line_len);
    let mut c = line.offset(4);
    while c < end {
        if *c as u8 == b' ' {
            start = c.offset(1);
            *style = match *start as u8 {
                b':' => YamlPlainScalarStyle,
                b'\'' => YamlSingleQuotedScalarStyle,
                b'"' => YamlDoubleQuotedScalarStyle,
                b'|' => YamlLiteralScalarStyle,
                b'>' => YamlFoldedScalarStyle,
                _ => {
                    start = ptr::null_mut::<i8>();
                    c = c.offset(1);
                    continue;
                }
            };
            start = start.offset(1);
            break;
        }
        c = c.offset(1);
    }
    if start.is_null() {
        process::abort();
    }

    let mut i = 0;
    c = start;
    while c < end {
        *value.offset(i) = if *c as u8 == b'\\' {
            c = c.offset(1);
            match *c as u8 {
                b'\\' => b'\\' as i8,
                b'0' => b'\0' as i8,
                b'b' => b'\x08' as i8,
                b'n' => b'\n' as i8,
                b'r' => b'\r' as i8,
                b't' => b'\t' as i8,
                _ => process::abort(),
            }
        } else {
            *c
        };
        i += 1;
        c = c.offset(1);
    }
    *value.offset(i) = b'\0' as i8;
}

fn main() -> ExitCode {
    let args = env::args_os().skip(1);
    if args.len() == 0 {
        let _ = writeln!(
            io::stderr(),
            "Usage: run-emitter-test-suite <test.event>...",
        );
        return ExitCode::FAILURE;
    }
    for arg in args {
        let mut stdin = File::open(arg).unwrap();
        let mut stdout = io::stdout();
        let result = unsafe { unsafe_main(&mut stdin, &mut stdout) };
        if let Err(err) = result {
            let _ = writeln!(io::stderr(), "{}", err);
            return ExitCode::FAILURE;
        }
    }
    ExitCode::SUCCESS
}