Commit efb20a1faf56548baf3ccbdad14af42b5c106dbd

Thomas de Grivel 2024-07-16T23:13:45

wip ekc3, make kc3_dlopen silent on success

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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
diff --git a/Makefile b/Makefile
index 962d3a2..92f2d6f 100644
--- a/Makefile
+++ b/Makefile
@@ -17,6 +17,7 @@ all:
 	${MAKE} -C libkc3 all
 	${MAKE} -C ikc3 all
 	${MAKE} -C kc3s all
+	${MAKE} -C ekc3 all
 	${MAKE} -C test all
 	${MAKE} -C libkc3_window all
 
@@ -26,6 +27,7 @@ asan:
 	${MAKE} -C libkc3 asan
 	${MAKE} -C ikc3 asan
 	${MAKE} -C kc3s asan
+	${MAKE} -C ekc3 asan
 	${MAKE} -C test asan
 	${MAKE} -C libkc3_window asan
 
@@ -36,6 +38,7 @@ build:
 	${MAKE} -C libkc3 build
 	${MAKE} -C ikc3 build
 	${MAKE} -C kc3s build
+	${MAKE} -C ekc3 build
 	${MAKE} -C test build
 	${MAKE} -C libkc3_window build
 
@@ -45,6 +48,7 @@ clean:
 	${MAKE} -C libkc3 clean
 	${MAKE} -C ikc3 clean
 	${MAKE} -C kc3s clean
+	${MAKE} -C ekc3 clean
 	${MAKE} -C test clean
 	${MAKE} -C libkc3_window clean
 
@@ -53,6 +57,7 @@ clean_cov:
 	${MAKE} -C libkc3 clean_cov
 	${MAKE} -C ikc3 clean_cov
 	${MAKE} -C kc3s clean_cov
+	${MAKE} -C ekc3 clean_cov
 	${MAKE} -C test clean_cov
 	${MAKE} -C libkc3_window clean_cov
 
@@ -62,6 +67,7 @@ cov:
 	${MAKE} -C libkc3 cov
 	${MAKE} -C ikc3 cov
 	${MAKE} -C kc3s cov
+	${MAKE} -C ekc3 cov
 	${MAKE} -C test cov
 	${MAKE} -C libkc3_window cov
 
@@ -71,6 +77,7 @@ debug:
 	${MAKE} -C libkc3 debug
 	${MAKE} -C ikc3 debug
 	${MAKE} -C kc3s debug
+	${MAKE} -C ekc3 debug
 	${MAKE} -C test debug
 	${MAKE} -C libkc3_window debug
 
@@ -116,6 +123,7 @@ distclean:
 	${MAKE} -C libkc3 distclean
 	${MAKE} -C ikc3 distclean
 	${MAKE} -C kc3s distclean
+	${MAKE} -C ekc3 distclean
 	${MAKE} -C test distclean
 	${MAKE} -C libkc3_window distclean
 
@@ -123,6 +131,7 @@ gcovr:
 	${MAKE} -C libkc3 gcovr
 	${MAKE} -C ikc3 gcovr
 	${MAKE} -C kc3s gcovr
+	${MAKE} -C ekc3 gcovr
 	${MAKE} -C test gcovr
 	${MAKE} -C libkc3_window gcovr
 	if [ -d "$$HOME/Downloads/kc3_gcovr" ]; then bin/gcovr-to-downloads; fi
@@ -166,6 +175,7 @@ install:
 	${MAKE} -C libkc3 install
 	${MAKE} -C ikc3 install
 	${MAKE} -C kc3s install
+	${MAKE} -C ekc3 install
 	${MAKE} -C libkc3_window install
 
 libkc3_gcovr:
@@ -198,6 +208,8 @@ test:
 	${MAKE} -C libtommath build
 	${MAKE} -C libkc3 build
 	${MAKE} -C ikc3 build
+	${MAKE} -C kc3s build
+	${MAKE} -C ekc3 build
 	${MAKE} -C test test
 	if ${HAVE_ASAN}; then ${MAKE} test_asan; fi
 
@@ -205,18 +217,24 @@ test_asan:
 	${MAKE} -C libtommath asan
 	${MAKE} -C libkc3 asan
 	${MAKE} -C ikc3 asan
+	${MAKE} -C kc3s asan
+	${MAKE} -C ekc3 asan
 	${MAKE} -C test test_asan
 
 test_cov: cov clean_cov
 	${MAKE} -C libtommath cov clean_cov
 	${MAKE} -C libkc3 cov clean_cov
 	${MAKE} -C ikc3 cov clean_cov
+	${MAKE} -C kc3s cov clean_cov
+	${MAKE} -C ekc3 cov clean_cov
 	${MAKE} -C test test_cov
 
 test_debug:
 	${MAKE} -C libtommath debug
 	${MAKE} -C libkc3 debug
 	${MAKE} -C ikc3 debug
+	${MAKE} -C kc3s debug
+	${MAKE} -C ekc3 debug
 	${MAKE} -C test test_debug
 
 test_ekc3: build
diff --git a/ekc3/configure b/ekc3/configure
index 1403402..f74c320 100755
--- a/ekc3/configure
+++ b/ekc3/configure
@@ -18,10 +18,10 @@ CONFIG_H_PREFIX=LIBKC3_WEB_
 
 . ../config.subr
 
-LIB=libkc3_web.la
-LIB_ASAN=libkc3_web_asan.la
-LIB_COV=libkc3_web_cov.la
-LIB_DEBUG=libkc3_web_debug.la
+LIB=libekc3.la
+LIB_ASAN=libekc3_asan.la
+LIB_COV=libekc3_cov.la
+LIB_DEBUG=libekc3_debug.la
 
 echo "LIB = $LIB" >> ${CONFIG_MK}
 echo "LIB_ASAN = $LIB_ASAN" >> ${CONFIG_MK}
diff --git a/ekc3/ekc3.c b/ekc3/ekc3.c
index 09ef6e9..64bc8ea 100644
--- a/ekc3/ekc3.c
+++ b/ekc3/ekc3.c
@@ -13,9 +13,9 @@
 #include <libkc3/kc3.h>
 #include "ekc3.h"
 
-#define EC3_BUF_PARSE_STR_BUF_SIZE (1024 * 1024)
+#define EKC3_BUF_PARSE_STR_BUF_SIZE (1024 * 1024)
 
-s_list ** ec3_append_and_empty_buf (s_list **tail, s_buf *buf)
+s_list ** ekc3_append_and_empty_buf (s_list **tail, s_buf *buf)
 {
   sw r;
   s_str str;
@@ -40,7 +40,7 @@ s_list ** ec3_append_and_empty_buf (s_list **tail, s_buf *buf)
   return tail;
 }
 
-s_list ** ec3_append_block (s_list **tail, s_block *block)
+s_list ** ekc3_append_block (s_list **tail, s_block *block)
 {
   assert(tail);
   assert(! *tail);
@@ -53,19 +53,19 @@ s_list ** ec3_append_block (s_list **tail, s_block *block)
   return tail;
 }
 
-s_list ** ec3_append_silent_block (s_list **tail, s_block *block)
+s_list ** ekc3_append_silent_block (s_list **tail, s_block *block)
 {
   assert(tail);
   assert(! *tail);
   assert(block);
-  if (! ec3_append_sym(tail, sym_1("silent")))
+  if (! ekc3_append_sym(tail, sym_1("silent")))
     return NULL;
-  if (! ec3_append_block(tail, block))
+  if (! ekc3_append_block(tail, block))
     return NULL;
   return tail;
 }
 
-s_list ** ec3_append_str (s_list **tail, s_str *str)
+s_list ** ekc3_append_str (s_list **tail, s_str *str)
 {
   assert(tail);
   assert(! *tail);
@@ -78,7 +78,7 @@ s_list ** ec3_append_str (s_list **tail, s_str *str)
   return tail;
 }
 
-s_list ** ec3_append_sym (s_list **tail, const s_sym *sym)
+s_list ** ekc3_append_sym (s_list **tail, const s_sym *sym)
 {
   assert(tail);
   assert(! *tail);
@@ -91,7 +91,7 @@ s_list ** ec3_append_sym (s_list **tail, const s_sym *sym)
   return tail;
 }
 
-sw ec3_buf_parse (s_buf *buf, p_ec3 *dest)
+sw ekc3_buf_parse (s_buf *buf, p_ekc3 *dest)
 {
   s_block block;
   character c;
@@ -100,12 +100,12 @@ sw ec3_buf_parse (s_buf *buf, p_ec3 *dest)
   s_buf_save save;
   s_buf str_buf;
   s_list **tail;
-  p_ec3 tmp;
+  p_ekc3 tmp;
   assert(dest);
   assert(buf);
-  if (! buf_init_alloc(&str_buf, EC3_BUF_PARSE_STR_BUF_SIZE))
+  if (! buf_init_alloc(&str_buf, EKC3_BUF_PARSE_STR_BUF_SIZE))
     return -1;
-  ec3_init(&tmp);
+  ekc3_init(&tmp);
   tail = &tmp;
   buf_save_init(buf, &save);
   while (1) {
@@ -114,12 +114,12 @@ sw ec3_buf_parse (s_buf *buf, p_ec3 *dest)
       goto restore;
     if (r > 0) {
       result += r;
-      if (! ec3_append_and_empty_buf(tail, &str_buf))
+      if (! ekc3_append_and_empty_buf(tail, &str_buf))
         goto ko;
-      r = ec3_buf_parse_c3_block(buf, &block);
+      r = ekc3_buf_parse_kc3_block(buf, &block);
       if (r <= 0)
         goto restore;
-      if (! ec3_append_block(tail, &block))
+      if (! ekc3_append_block(tail, &block))
         goto ko;
       continue;
     }
@@ -128,12 +128,12 @@ sw ec3_buf_parse (s_buf *buf, p_ec3 *dest)
       goto restore;
     if (r > 0) {
       result += r;
-      if (! ec3_append_and_empty_buf(tail, &str_buf))
+      if (! ekc3_append_and_empty_buf(tail, &str_buf))
         goto ko;
-      r = ec3_buf_parse_c3_block(buf, &block);
+      r = ekc3_buf_parse_kc3_block(buf, &block);
       if (r <= 0)
         goto restore;
-      if (! ec3_append_block(tail, &block))
+      if (! ekc3_append_block(tail, &block))
         goto ko;
       continue;
     }
@@ -148,8 +148,8 @@ sw ec3_buf_parse (s_buf *buf, p_ec3 *dest)
     result += r;
     r = buf_write_character_utf8(&str_buf, c);
     if (r <= 0) {
-      err_puts("ec3_buf_parse: buf_write_character_utf8 str_buf");
-      assert(! "ec3_buf_parse: buf_write_character_utf8 str_buf");
+      err_puts("ekc3_buf_parse: buf_write_character_utf8 str_buf");
+      assert(! "ekc3_buf_parse: buf_write_character_utf8 str_buf");
       goto restore;
     }
   }
@@ -157,63 +157,139 @@ sw ec3_buf_parse (s_buf *buf, p_ec3 *dest)
   r = -1;
  restore:
   buf_save_restore_rpos(buf, &save);
-  ec3_clean(&tmp);
+  ekc3_clean(&tmp);
  clean:
   buf_clean(&str_buf);
   buf_save_clean(buf, &save);
   return r;
 }
 
-sw ec3_buf_parse_c3_block (s_buf *buf, s_block *dest)
+sw ekc3_buf_parse_kc3_block (s_buf *buf, s_block *dest)
 {
   sw r;
   sw result = 0;
   s_buf_save save;
+  s_list *list;
+  s_list **tail;
+  s_block tmp;
   assert(buf);
   assert(dest);
   buf_save_init(buf, &save);
-  r = buf_ignore_spaces(buf);
-  if (r < 0)
-    goto clean;
-  if (! r) {
-    r = -1;
-    goto clean;
+  list = NULL;
+  tail = &list;
+  while (1) {
+    r = buf_ignore_spaces(buf);
+    if (r < 0)
+      goto clean;
+    if (! r) {
+      r = -1;
+      goto clean;
+    }
+    r = buf_read_1(buf, "%>");
+    if (r > 0) {
+      result += r;
+      if (! block_init_from_list(&tmp, (const s_list * const*) &list)) {
+        list_delete_all(list);
+        r = -1;
+        goto clean;
+      }
+      *dest = tmp;
+      r = result;
+      goto clean;
+    }
+    *tail = list_new(NULL);
+    r = buf_parse_tag(buf, &(*tail)->tag);
+    if (r <= 0) {
+      list_delete_all(list);
+      goto clean;
+    }
+    result += r;
+    tail = &(*tail)->next.data.list;
   }
-  result += r;
-  
  clean:
   buf_save_clean(buf, &save);
   return r;
 }
 
-void ec3_clean (p_ec3 *ec3)
+void ekc3_clean (p_ekc3 *ekc3)
 {
-  list_delete_all(*ec3);
+  list_delete_all(*ekc3);
 }
 
-p_ec3 * ec3_init (p_ec3 *ec3)
+p_ekc3 * ekc3_init (p_ekc3 *ekc3)
 {
-  p_ec3 tmp = {0};
-  *ec3 = tmp;
-  return ec3;
+  p_ekc3 tmp = {0};
+  *ekc3 = tmp;
+  return ekc3;
 }
 
-sw ec3_render (const p_ec3 *ec3, s_buf *buf, s_map *map)
+sw ekc3_render (const p_ekc3 *ekc3)
 {
-  assert(ec3);
-  assert(buf);
-  assert(map);
-  (void) ec3;
-  (void) buf;
-  (void) map;
-  return -42;
+  const s_list *l;
+  sw r;
+  sw result = 0;
+  assert(ekc3);
+  l = *ekc3;
+  while (l) {
+    if (! (r = ekc3_render_tag(&l->tag)))
+      return r;
+    result += r;
+    l = list_next(l);
+  }
+  return result;
+}
+
+sw ekc3_render_buf (s_buf *in)
+{
+  p_ekc3 ekc3;
+  sw r;
+  assert(in);
+  ekc3_init(&ekc3);
+  r = ekc3_buf_parse(in, &ekc3);
+  if (r < 0)
+    return r;
+  r = ekc3_render(&ekc3);
+  ekc3_clean(&ekc3);
+  return r;
+}
+
+sw ekc3_render_file (const s_str *path)
+{
+  s_tag *file_dir;
+  s_tag  file_dir_save;
+  s_tag *file_path;
+  s_tag  file_path_save;
+  FILE *fp;
+  s_buf in;
+  char  in_data[BUF_SIZE];
+  sw r;
+  buf_init(&in, false, BUF_SIZE, in_data);
+  fp = file_open(path->ptr.pchar, "rb");
+  if (! fp)
+    return -1;
+  if (! buf_file_open_r(&in, fp)) {
+    fclose(fp);
+    return -1;
+  }
+  file_dir = frame_get_w(&g_kc3_env.global_frame, &g_sym___DIR__);
+  file_dir_save = *file_dir;
+  file_dirname(path, &file_dir->data.str);
+  file_path = frame_get_w(&g_kc3_env.global_frame, &g_sym___FILE__);
+  file_path_save = *file_path;
+  file_path->data.str = *path;
+  r = ekc3_render_buf(&in);
+  *file_dir = file_dir_save;
+  *file_path = file_path_save;
+  buf_file_close(&in);
+  fclose(fp);
+  return r;
 }
 
-s_fn * ec3_to_render_fn (const p_ec3 *ec3, s_fn *dest)
+s_fn * ekc3_to_render_fn (const p_ekc3 *ekc3, s_fn *dest)
 {
-  assert(ec3);
+  assert(ekc3);
   assert(dest);
-  (void) ec3;
+  (void) ekc3;
   (void) dest;
   return NULL;
 }
diff --git a/ekc3/ekc3.h b/ekc3/ekc3.h
index 740d8bd..9a90fdf 100644
--- a/ekc3/ekc3.h
+++ b/ekc3/ekc3.h
@@ -26,10 +26,12 @@ s_list ** ekc3_append_silent_block (s_list **tail, s_block *block);
 s_list ** ekc3_append_str (s_list **tail, s_str *str);
 s_list ** ekc3_append_sym (s_list **tail, const s_sym *sym);
 sw        ekc3_buf_parse (s_buf *buf, p_ekc3 *dest);
-sw        ekc3_buf_parse_c3_block (s_buf *buf, s_block *dest);
+sw        ekc3_buf_parse_kc3_block (s_buf *buf, s_block *dest);
 
 /* Observers. */
-sw     ekc3_render (const p_ekc3 *ekc3, s_buf *buf, s_map *map);
-s_fn * ekc3_to_render_fn (const p_ekc3 *ekc3, s_fn *dest);
+sw ekc3_render (const p_ekc3 *ekc3);
+sw ekc3_render_buf (s_buf *in);
+sw ekc3_render_file (const s_str *path);
+sw ekc3_render_tag (const s_tag *tag);
 
 #endif /* EKC3_H */
diff --git a/env b/env
index 9a086eb..de90e5d 100644
--- a/env
+++ b/env
@@ -1 +1 @@
-export LD_LIBRARY_PATH="$PWD/libkc3/.libs:$LD_LIBRARY_PATH"
+export LD_LIBRARY_PATH="$PWD/libkc3/.libs:$PWD/ekc3/.libs:$PWD/lib/kc3/0.1:$LD_LIBRARY_PATH"
diff --git a/ikc3/ikc3.c b/ikc3/ikc3.c
index 6e9be7b..aae0ddb 100644
--- a/ikc3/ikc3.c
+++ b/ikc3/ikc3.c
@@ -113,7 +113,7 @@ int main (int argc, char **argv)
         r = 1;
         goto clean;
       }
-      fp = fopen(argv[1], "rb");
+      fp = file_open(argv[1], "rb");
       if (! fp) {
         e = errno;
         err_write_1("ikc3: ");
diff --git a/kc3s/kc3s.c b/kc3s/kc3s.c
index aea9fbd..92f59ec 100644
--- a/kc3s/kc3s.c
+++ b/kc3s/kc3s.c
@@ -69,16 +69,20 @@ sw kc3s_run (void)
         tag_clean(&input);
         continue;
       }
+      /*
       if (buf_inspect_tag(&g_kc3_env.out, &result) < 0) {
 	tag_clean(&input);
 	tag_clean(&result);
         break;
       }
+      */
       tag_clean(&input);
       tag_clean(&result);
+      /*
       buf_write_u8(&g_kc3_env.out, '\n');
       if ((r = buf_flush(&g_kc3_env.out)) < 0)
         break;
+      */
     }
     if (r < 0 ||
         (r == 0 &&
diff --git a/lib/kc3/0.1/ekc3.kc3 b/lib/kc3/0.1/ekc3.kc3
index 59342b6..b9d79b9 100644
--- a/lib/kc3/0.1/ekc3.kc3
+++ b/lib/kc3/0.1/ekc3.kc3
@@ -1,6 +1,6 @@
 defmodule EKC3 do
 
-  dlopen(__DIR__ + "/libekc3.so")
+  dlopen(__DIR__ + "/ekc3/libekc3.so")
 
   def render_file = cfn Str "ekc3_render_file" (Str)
 
diff --git a/libkc3/block.c b/libkc3/block.c
index 64459de..c9307c0 100644
--- a/libkc3/block.c
+++ b/libkc3/block.c
@@ -18,6 +18,7 @@
 #include "buf_inspect.h"
 #include "buf_parse.h"
 #include "io.h"
+#include "list.h"
 #include "tag.h"
 
 void block_clean (s_block *block)
@@ -104,6 +105,29 @@ s_block * block_init_copy (s_block *block, const s_block *src)
   return block;
 }
 
+s_block * block_init_from_list (s_block *block,
+                                const s_list * const *list)
+{
+  uw i;
+  const s_list *l;
+  uw len;
+  s_block tmp;
+  assert(block);
+  assert(list);
+  l = *list;
+  len = list_length(l);
+  block_init(&tmp, len);
+  i = 0;
+  while (l) {
+    if (! tag_init_copy(tmp.tag + i, &l->tag))
+      return NULL;
+    i++;
+    l = list_next(l);
+  }
+  *block = tmp;
+  return block;
+}
+
 s_str * block_inspect (const s_block *x, s_str *dest)
 {
   s_buf buf;
diff --git a/libkc3/block.h b/libkc3/block.h
index 093623d..55a9457 100644
--- a/libkc3/block.h
+++ b/libkc3/block.h
@@ -27,6 +27,8 @@ s_block * block_init (s_block *block, uw count);
 s_block * block_init_1 (s_block *block, const char *p);
 s_block * block_init_cast (s_block *block, const s_tag *tag);
 s_block * block_init_copy (s_block *block, const s_block *src);
+s_block * block_init_from_list (s_block *block,
+                                const s_list * const *list);
 void      block_clean (s_block *block);
 
 /* Constructors, call block_delete after use */
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index 019a96c..f8e6d8c 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -108,9 +108,11 @@ void ** kc3_dlopen (const s_str *path, void **dest)
 {
   assert(path);
   assert(dest);
+#if DEBUG_KC3_DLOPEN
   err_write_1("kc3_dlopen: ");
   err_inspect_str(path);
   err_write_1("\n");
+#endif
   *dest = dlopen(path->ptr.pchar, RTLD_GLOBAL);
   if (! *dest) {
     err_write_1("kc3_dlopen: ");
diff --git a/libkc3/kc3.h b/libkc3/kc3.h
index 0d6242e..07cc81e 100644
--- a/libkc3/kc3.h
+++ b/libkc3/kc3.h
@@ -16,6 +16,7 @@
 #include "arg.h"
 #include "array.h"
 #include "assert.h"
+#include "block.h"
 #include "bool.h"
 #include "buf.h"
 #include "buf_file.h"