aboutsummaryrefslogtreecommitdiff
path: root/contrib/lyaml/ext/yaml/emitter.c
blob: b71fd14f4c1dfa43498cbbafbf08828305646d1f (plain) (blame)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * emitter.c, LibYAML emitter binding for Lua
 * Written by Gary V. Vaughan, 2013
 *
 * Copyright (C) 2013-2022 Gary V. Vaughan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <assert.h>

#include "lyaml.h"


typedef struct {
   yaml_emitter_t   emitter;

   /* output accumulator */
   lua_State	   *outputL;
   luaL_Buffer	    yamlbuff;

   /* error handling */
   lua_State	   *errL;
   luaL_Buffer	    errbuff;
   int		    error;
} lyaml_emitter;


/* Emit a STREAM_START event. */
static int
emit_STREAM_START (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_encoding_t yaml_encoding;
   const char *encoding = NULL;

   RAWGET_STRDUP (encoding); lua_pop (L, 1);

#define MENTRY(_s) (STREQ (encoding, #_s)) { yaml_encoding = YAML_##_s##_ENCODING; }
   if (encoding == NULL) { yaml_encoding = YAML_ANY_ENCODING; } else
   if MENTRY( UTF8	) else
   if MENTRY( UTF16LE	) else
   if MENTRY( UTF16BE	) else
   {
      emitter->error++;
      luaL_addsize (&emitter->errbuff,
                    sprintf (luaL_prepbuffer (&emitter->errbuff),
                             "invalid stream encoding '%s'", encoding));
   }
#undef MENTRY

   if (encoding) free ((void *) encoding);

   if (emitter->error != 0)
     return 0;

   yaml_stream_start_event_initialize (&event, yaml_encoding);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a STREAM_END event. */
static int
emit_STREAM_END (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_stream_end_event_initialize (&event);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a DOCUMENT_START event. */
static int
emit_DOCUMENT_START (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_version_directive_t version_directive, *Pversion_directive = NULL;
   yaml_tag_directive_t *tag_directives_start = NULL, *tag_directives_end = NULL;
   int implicit = 0;

   RAWGET_PUSHTABLE ("version_directive");
   if (lua_type (L, -1) == LUA_TTABLE)
   {
      RAWGETS_INTEGER (version_directive.major, "major");
      ERROR_IFNIL ("version_directive missing key 'major'");
      if (emitter->error == 0)
      {
         RAWGETS_INTEGER (version_directive.minor, "minor");
         ERROR_IFNIL ("version_directive missing key 'minor'");
      }
      Pversion_directive = &version_directive;
   }
   lua_pop (L, 1);		/* pop version_directive rawget */

   RAWGET_PUSHTABLE ("tag_directives");
   if (lua_type (L, -1) == LUA_TTABLE)
   {
      size_t bytes = lua_objlen (L, -1) * sizeof (yaml_tag_directive_t);

      tag_directives_start = (yaml_tag_directive_t *) malloc (bytes);
      tag_directives_end = tag_directives_start;

      lua_pushnil (L);	/* first key */
      while (lua_next (L, -2) != 0)
      {
         RAWGETS_STRDUP (tag_directives_end->handle, "handle");
         ERROR_IFNIL ("tag_directives item missing key 'handle'");
         lua_pop (L, 1);	/* pop handle */

         RAWGETS_STRDUP (tag_directives_end->prefix, "prefix");
         ERROR_IFNIL ("tag_directives item missing key 'prefix'");
         lua_pop (L, 1);	/* pop prefix */

         tag_directives_end += 1;

         /* pop tag_directives list elewent, leave key for next iteration */
         lua_pop (L, 1);
      }
   }
   lua_pop (L, 1);	/* pop lua_rawget "tag_directives" result */

   RAWGET_BOOLEAN (implicit); lua_pop (L, 1);

   if (emitter->error != 0)
      return 0;

   yaml_document_start_event_initialize (&event, Pversion_directive,
      tag_directives_start, tag_directives_end, implicit);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a DOCUMENT_END event. */
static int
emit_DOCUMENT_END (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   int implicit = 0;

   RAWGET_BOOLEAN (implicit);

   yaml_document_end_event_initialize (&event, implicit);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a MAPPING_START event. */
static int
emit_MAPPING_START (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_mapping_style_t yaml_style;
   yaml_char_t *anchor = NULL, *tag = NULL;
   int implicit = 1;
   const char *style = NULL;

   RAWGET_STRDUP (style);  lua_pop (L, 1);

#define MENTRY(_s) (STREQ (style, #_s)) { yaml_style = YAML_##_s##_MAPPING_STYLE; }
   if (style == NULL) { yaml_style = YAML_ANY_MAPPING_STYLE; } else
   if MENTRY( BLOCK	) else
   if MENTRY( FLOW	) else
   {
      emitter->error++;
      luaL_addsize (&emitter->errbuff,
                    sprintf (luaL_prepbuffer (&emitter->errbuff),
                             "invalid mapping style '%s'", style));
   }
#undef MENTRY

   if (style) free ((void *) style);

   RAWGET_YAML_CHARP (anchor); lua_pop (L, 1);
   RAWGET_YAML_CHARP (tag);    lua_pop (L, 1);
   RAWGET_BOOLEAN (implicit);  lua_pop (L, 1);

   yaml_mapping_start_event_initialize (&event, anchor, tag, implicit, yaml_style);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a MAPPING_END event. */
static int
emit_MAPPING_END (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_mapping_end_event_initialize (&event);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a SEQUENCE_START event. */
static int
emit_SEQUENCE_START (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_sequence_style_t yaml_style;
   yaml_char_t *anchor = NULL, *tag = NULL;
   int implicit = 1;
   const char *style = NULL;

   RAWGET_STRDUP (style);  lua_pop (L, 1);

#define MENTRY(_s) (STREQ (style, #_s)) { yaml_style = YAML_##_s##_SEQUENCE_STYLE; }
   if (style == NULL) { yaml_style = YAML_ANY_SEQUENCE_STYLE; } else
   if MENTRY( BLOCK	) else
   if MENTRY( FLOW	) else
   {
      emitter->error++;
      luaL_addsize (&emitter->errbuff,
                    sprintf (luaL_prepbuffer (&emitter->errbuff),
                             "invalid sequence style '%s'", style));
   }
#undef MENTRY

   if (style) free ((void *) style);

   RAWGET_YAML_CHARP (anchor); lua_pop (L, 1);
   RAWGET_YAML_CHARP (tag);    lua_pop (L, 1);
   RAWGET_BOOLEAN (implicit);  lua_pop (L, 1);

   yaml_sequence_start_event_initialize (&event, anchor, tag, implicit, yaml_style);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a SEQUENCE_END event. */
static int
emit_SEQUENCE_END (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_sequence_end_event_initialize (&event);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit a SCALAR event. */
static int
emit_SCALAR (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_scalar_style_t yaml_style;
   yaml_char_t *anchor = NULL, *tag = NULL, *value;
   int length = 0, plain_implicit = 1, quoted_implicit = 1;
   const char *style = NULL;

   RAWGET_STRDUP (style);  lua_pop (L, 1);

#define MENTRY(_s) (STREQ (style, #_s)) { yaml_style = YAML_##_s##_SCALAR_STYLE; }
   if (style == NULL) { yaml_style = YAML_ANY_SCALAR_STYLE; } else
   if MENTRY( PLAIN		) else
   if MENTRY( SINGLE_QUOTED	) else
   if MENTRY( DOUBLE_QUOTED	) else
   if MENTRY( LITERAL		) else
   if MENTRY( FOLDED		) else
   {
      emitter->error++;
      luaL_addsize (&emitter->errbuff,
                    sprintf (luaL_prepbuffer (&emitter->errbuff),
                             "invalid scalar style '%s'", style));
   }
#undef MENTRY

   if (style) free ((void *) style);

   RAWGET_YAML_CHARP (anchor); lua_pop (L, 1);
   RAWGET_YAML_CHARP (tag);    lua_pop (L, 1);
   RAWGET_YAML_CHARP (value);  length = lua_objlen (L, -1); lua_pop (L, 1);
   RAWGET_BOOLEAN (plain_implicit);
   RAWGET_BOOLEAN (quoted_implicit);

   yaml_scalar_event_initialize (&event, anchor, tag, value, length,
      plain_implicit, quoted_implicit, yaml_style);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


/* Emit an ALIAS event. */
static int
emit_ALIAS (lua_State *L, lyaml_emitter *emitter)
{
   yaml_event_t event;
   yaml_char_t *anchor;

   RAWGET_YAML_CHARP (anchor);

   yaml_alias_event_initialize (&event, anchor);
   return yaml_emitter_emit (&emitter->emitter, &event);
}


static int
emit (lua_State *L)
{
   lyaml_emitter *emitter;
   int yaml_ok = 0;
   int finalize = 0;

   luaL_argcheck (L, lua_istable (L, 1), 1, "expected table");

   emitter = (lyaml_emitter *) lua_touserdata (L, lua_upvalueindex (1));

   {
     const char *type;

     RAWGET_STRDUP (type); lua_pop (L, 1);

     if (type == NULL)
     {
        emitter->error++;
        luaL_addstring (&emitter->errbuff, "no type field in event table");
     }
#define MENTRY(_s) (STREQ (type, #_s)) { yaml_ok = emit_##_s (L, emitter); }
     /* Minimize comparisons by putting more common types earlier. */
     else if MENTRY( SCALAR		)
     else if MENTRY( MAPPING_START	)
     else if MENTRY( MAPPING_END	)
     else if MENTRY( SEQUENCE_START	)
     else if MENTRY( SEQUENCE_END	)
     else if MENTRY( DOCUMENT_START	)
     else if MENTRY( DOCUMENT_END	)
     else if MENTRY( STREAM_START	)
     else if MENTRY( STREAM_END		)
     else if MENTRY( ALIAS		)
#undef MENTRY
     else
     {
        emitter->error++;
        luaL_addsize (&emitter->errbuff,
                      sprintf (luaL_prepbuffer (&emitter->errbuff),
                               "invalid event type '%s'", type));
     }

     /* If the stream has finished, finalize the YAML output. */
     if (type && STREQ (type, "STREAM_END"))
       finalize = 1;

     if (type) free ((void *) type);
   }

   /* Copy any yaml_emitter_t errors into the error buffer. */
   if (!emitter->error && !yaml_ok)
   {
      if (emitter->emitter.problem)
        luaL_addstring (&emitter->errbuff, emitter->emitter.problem);
      else
        luaL_addstring (&emitter->errbuff, "LibYAML call failed");
      emitter->error++;
   }

   /* Report errors back to the caller as `false, "error message"`. */
   if (emitter->error != 0)
   {
      assert (emitter->error == 1);  /* bail on uncaught additional errors */
      lua_pushboolean (L, 0);
      luaL_pushresult (&emitter->errbuff);
      lua_xmove (emitter->errL, L, 1);
      return 2;
   }

   /* Return `true, "YAML string"` after accepting a STREAM_END event. */
   if (finalize)
   {
      lua_pushboolean (L, 1);
      luaL_pushresult (&emitter->yamlbuff);
      lua_xmove (emitter->outputL, L, 1);
      return 2;
   }

   /* Otherwise, just report success to the caller as `true`. */
   lua_pushboolean (L, 1);
   return 1;
}


static int
append_output (void *arg, unsigned char *buff, size_t len)
{
   lyaml_emitter *emitter = (lyaml_emitter *) arg;
   luaL_addlstring (&emitter->yamlbuff, (char *) buff, len);
   return 1;
}


static int
emitter_gc (lua_State *L)
{
   lyaml_emitter *emitter = (lyaml_emitter *) lua_touserdata (L, 1);

   if (emitter)
      yaml_emitter_delete (&emitter->emitter);

   return 0;
}


int
Pemitter (lua_State *L)
{
   lyaml_emitter *emitter;

   lua_newtable (L);	/* object table */

   /* Create a user datum to store the emitter. */
   emitter = (lyaml_emitter *) lua_newuserdata (L, sizeof (*emitter));
   emitter->error = 0;

   /* Initialize the emitter. */
   if (!yaml_emitter_initialize (&emitter->emitter))
   {
      if (!emitter->emitter.problem)
         emitter->emitter.problem = "cannot initialize emitter";
      return luaL_error (L, "%s", emitter->emitter.problem);
   }
   yaml_emitter_set_unicode (&emitter->emitter, 1);
   yaml_emitter_set_width   (&emitter->emitter, 2);
   yaml_emitter_set_output  (&emitter->emitter, &append_output, emitter);

   /* Set it's metatable, and ensure it is garbage collected properly. */
   luaL_newmetatable (L, "lyaml.emitter");
   lua_pushcfunction (L, emitter_gc);
   lua_setfield      (L, -2, "__gc");
   lua_setmetatable  (L, -2);

   /* Set the emit method of object as a closure over the user datum, and
      return the whole object. */
   lua_pushcclosure (L, emit, 1);
   lua_setfield (L, -2, "emit");

   /* Set up a separate thread to collect error messages; save the thread
      in the returned table so that it's not garbage collected when the
      function call stack for Pemitter is cleaned up.  */
   emitter->errL = lua_newthread (L);
   luaL_buffinit (emitter->errL, &emitter->errbuff);
   lua_setfield (L, -2, "errthread");

   /* Create a thread for the YAML buffer. */
   emitter->outputL = lua_newthread (L);
   luaL_buffinit (emitter->outputL, &emitter->yamlbuff);
   lua_setfield (L, -2, "outputthread");

   return 1;
}