forked from alliedtelesis/apteryx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua.c
879 lines (797 loc) · 21.4 KB
/
lua.c
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/**
* @file lua.c
* Lua bindings for Apteryx.
*
* Copyright 2014, Allied Telesis Labs New Zealand, Ltd
*
* Basic API Example:
* > apteryx = require("apteryx")
* > apteryx.set("/interfaces/eth0/state", "up")
* > apteryx.set("/interfaces/eth1/state", "down")
* > print(apteryx.get("/interfaces/eth0/state"))
* up
* > paths = apteryx.search("/interfaces/")
* > print(unpack(paths))
* /interfaces/eth0 /interfaces/eth1
*
* Schema based API example:
* > api = require("apteryx").api("/etc/apteryx")
* > api.interfaces("eth0").state = "up"
* > api.interfaces("eth1").state = "down"
* > print(api.interaces("eth0").state)
* up
* > paths = api.interaces()
* > print(unpack(paths))
* eth0 eth1
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#ifdef HAVE_LUA
#include <sys/poll.h>
#include <lua.h>
#include <lauxlib.h>
#include "apteryx.h"
#include "internal.h"
#define lua_absindex(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
lua_gettop(L) + (i) + 1)
static lua_State *g_L = NULL;
static void
lua_apteryx_error (lua_State *ls, int res)
{
switch (res)
{
case LUA_ERRRUN:
ERROR ("LUA: %s\n", lua_tostring (ls, -1));
break;
case LUA_ERRSYNTAX:
ERROR ("LUA: %s\n", lua_tostring (ls, -1));
break;
case LUA_ERRMEM:
ERROR ("LUA: Memory allocation error\n");
break;
case LUA_ERRERR:
ERROR ("LUA: Error handler error\n");
break;
case LUA_ERRFILE:
ERROR ("LUA: Couldn't open file\n");
break;
default:
ERROR ("LUA: Unknown error\n");
break;
}
}
static const char *
lua_apteryx_tostring (lua_State *L, int i)
{
const char *ret = NULL;
int abs_index = lua_absindex (L, i);
switch (lua_type (L, i))
{
case LUA_TNIL:
return NULL;
case LUA_TBOOLEAN:
return lua_toboolean (L, i) ? "1" : "0";
default:
lua_getglobal (L, "tostring");
lua_pushvalue (L, abs_index);
lua_call (L, 1, 1);
ret = lua_tostring (L, -1);
lua_pop (L, 1);
}
return ret;
}
static void
_lua_apteryx_tree2dict (lua_State *L, GNode *this)
{
GNode *child = NULL;
if (!(this->children))
{
/* Something is wrong, either a value has been stored on a trunk
* or get_tree was called on a leaf node. Both we do not support */
return;
}
lua_pushstring (L, APTERYX_NAME (this));
/* is this a leaf? */
if (APTERYX_HAS_VALUE (this))
{
lua_pushstring (L, APTERYX_VALUE (this));
}
else
{
lua_newtable (L);
for (child = g_node_first_child (this); child; child = g_node_next_sibling (child))
{
_lua_apteryx_tree2dict (L, child);
}
}
lua_settable (L, -3);
}
static inline void
lua_apteryx_tree2dict (lua_State *L, GNode *this)
{
GNode *child = NULL;
lua_newtable (L);
if (this)
{
for (child = g_node_first_child (this); child; child = g_node_next_sibling (child))
{
_lua_apteryx_tree2dict (L, child);
}
}
}
static bool
_lua_apteryx_dict2tree (lua_State *L, GNode *n, bool leaves)
{
bool ret = false;
GNode *c = NULL;
const char *value = NULL;
lua_pushnil (L);
while (lua_next (L, -2))
{
if (lua_type (L, -1) == LUA_TTABLE)
{
lua_pushvalue (L, -2);
c = APTERYX_NODE (n, strdup ((char *) lua_tostring (L, -1)));
lua_pop (L, 1);
if ((_lua_apteryx_dict2tree (L, c, leaves)) || !leaves)
{
ret = true;
}
else
{ /* destroy leafless sub-trees */
apteryx_free_tree (c);
}
}
else
{
value = lua_apteryx_tostring (L, -1);
if (value)
{
lua_pushvalue (L, -2);
APTERYX_LEAF (n, strdup ((char *) lua_tostring (L, -1)), strdup (value));
lua_pop (L, 1);
ret = true;
}
}
lua_pop (L, 1);
}
return ret;
}
static inline GNode *
lua_apteryx_dict2tree (lua_State *L)
{
GNode *root = NULL;
root = APTERYX_NODE (NULL, (char *) strdup (lua_tostring (L, 1)));
if (!_lua_apteryx_dict2tree (L, root, true))
{
apteryx_free_tree (root);
root = NULL;
}
return root;
}
static inline GNode *
lua_apteryx_dict2tree_paths (lua_State *L)
{
GNode *root = g_node_new (strdup ("/"));
if (!_lua_apteryx_dict2tree (L, root, false))
{
apteryx_free_tree (root);
root = NULL;
}
return root;
}
static int
lua_apteryx_debug (lua_State *L)
{
if (lua_gettop (L) < 1 || !lua_isboolean (L, 1))
{
luaL_error (L, "Invalid arguments: requires boolean");
return 0;
}
apteryx_debug = lua_toboolean (L, 1);
return 0;
}
static int
lua_apteryx_set (lua_State *L)
{
if (lua_gettop (L) < 1 || !lua_isstring (L, 1))
{
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
if (lua_gettop (L) < 2)
{
lua_pushnil (L);
}
lua_pushboolean (L, apteryx_set (lua_tostring (L, 1), lua_apteryx_tostring (L, 2)));
return 1;
}
static int
lua_apteryx_get (lua_State *L)
{
char *value = NULL;
if (lua_gettop (L) != 1 || !lua_isstring (L, 1))
{
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
value = apteryx_get (lua_tostring (L, 1));
if (value)
{
lua_pushstring (L, value);
free (value);
return 1;
}
return 0;
}
static int
lua_apteryx_search (lua_State *L)
{
GList *paths;
int num;
if (lua_gettop (L) != 1 || !lua_isstring (L, 1))
{
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
paths = apteryx_search (lua_tostring (L, 1));
num = g_list_length (paths);
GList *_iter = paths;
lua_createtable (L, num, 0);
for (int i = 1; i <= num; i++)
{
const char *path = (char *) _iter->data;
lua_pushstring (L, path);
lua_rawseti (L, -2, i);
_iter = _iter->next;
}
g_list_free_full (paths, free);
return 1;
}
static int
lua_apteryx_prune (lua_State *L)
{
if (lua_gettop (L) != 1 || !lua_isstring (L, 1))
{
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
lua_pushboolean (L, apteryx_prune (lua_tostring (L, 1)));
return 1;
}
static int
lua_apteryx_set_tree (lua_State *L)
{
GNode *root = NULL;
if (lua_gettop (L) < 1 || !lua_isstring (L, 1))
{
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
if (lua_gettop (L) < 2 || !lua_istable (L, 2))
{
luaL_error (L, "Invalid arguments: requires table");
return 0;
}
root = lua_apteryx_dict2tree (L);
if (root)
{
lua_pushboolean (L, apteryx_set_tree (root));
apteryx_free_tree (root);
}
return 1;
}
static int
lua_apteryx_get_tree (lua_State *L)
{
GNode *tree = NULL;
if (lua_gettop (L) != 1 || !lua_isstring (L, 1))
{
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
tree = apteryx_get_tree (lua_tostring (L, 1));
lua_apteryx_tree2dict (L, tree);
apteryx_free_tree (tree);
return 1;
}
static int
lua_apteryx_query (lua_State *L)
{
GNode *root = NULL;
GNode *out_root = NULL;
if (lua_gettop (L) != 1 || !lua_istable (L, 1))
{
luaL_error (L, "Invalid arguments: requires table");
return 0;
}
root = lua_apteryx_dict2tree_paths (L);
if (root)
{
out_root = apteryx_query (root);
lua_apteryx_tree2dict (L, out_root);
apteryx_free_tree (root);
apteryx_free_tree (out_root);
}
else
{
luaL_error (L, "Invalid arguments: no query paths specified");
return 0;
}
return 1;
}
#define APTERYX_CB_TABLE_REGISTRY_INDEX "apteryx_cb_table"
static int
find_callback (lua_State *L, int index)
{
int ref = 0;
luaL_checktype (L, -1, LUA_TTABLE);
lua_pushvalue (L, -1);
lua_pushnil (L);
while (lua_next (L, -2))
{
lua_pushvalue (L, -2);
if (lua_rawequal (L, index, -2))
{
ref = lua_tonumber (L, -1);
lua_pop (L, 2);
break;
}
lua_pop(L, 2);
}
lua_pop(L, 1);
return ref;
}
static int
ref_callback (lua_State* L, int index)
{
luaL_checktype (L, index, LUA_TFUNCTION);
lua_pushlightuserdata (L, APTERYX_CB_TABLE_REGISTRY_INDEX);
lua_gettable (L, LUA_REGISTRYINDEX);
if (lua_isnil (L, -1))
{
lua_pop (L, 1); /* pop nil */
lua_pushlightuserdata (L, APTERYX_CB_TABLE_REGISTRY_INDEX);
lua_newtable (L);
lua_settable (L, LUA_REGISTRYINDEX);
lua_pushlightuserdata (L, APTERYX_CB_TABLE_REGISTRY_INDEX);
lua_gettable (L, LUA_REGISTRYINDEX);
}
int ref = find_callback (L, index);
if (ref == 0)
{
lua_pushvalue (L, index);
ref = luaL_ref (L, -2);
}
lua_pop (L, 1); /* pop table */
return ref;
}
static bool
push_callback (lua_State* L, size_t ref)
{
if (L == NULL)
return false;
lua_pushlightuserdata (L, APTERYX_CB_TABLE_REGISTRY_INDEX);
lua_gettable (L, LUA_REGISTRYINDEX);
if (lua_isnil (L, -1))
{
ERROR ("LUA: Callback not found\n");
lua_pop (L, 1); /* pop nil */
return false;
}
lua_rawgeti (L, -1, ref);
if (!lua_isfunction (L, -1))
{
ERROR ("LUA: Callback not a function\n");
lua_pop (L, 1);
return false;
}
return true;
}
static GList*
lua_do_index (const char *path, size_t ref)
{
int res = 0;
lua_State* L = g_L;
ASSERT (L, return NULL, "Index: LUA is not multi-threaded (use apteryx.process)\n");
int ssize = lua_gettop (L);
GList *paths = NULL;
if (!push_callback (L, ref))
return NULL;
lua_pushstring (L, path);
res = lua_pcall (L, 1, 1, 0);
if (res != 0)
lua_apteryx_error (L, res);
if (lua_gettop (L))
{
if (lua_istable (L, -1))
{
lua_pushnil (L);
while (lua_next(L, -2) != 0)
{
paths = g_list_append (paths, strdup (lua_tostring (L, -1)));
lua_pop (L, 1);
}
lua_pop (L, 1);
}
else
{
ERROR ("LUA: Index did not return a table\n");
lua_pop (L, 1);
}
}
lua_pop (L, 1); /* pop fn */
ASSERT (lua_gettop (L) == ssize, return paths, "Index: Stack changed\n");
return paths;
}
static int
lua_apteryx_index (lua_State *L)
{
luaL_checktype (L, 1, LUA_TSTRING);
luaL_checktype (L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!add_callback (APTERYX_INDEXERS_PATH, path, (void *)lua_do_index, false, (void *) ref, 0))
{
luaL_error (L, "Failed to register callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_apteryx_unindex (lua_State *L)
{
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!delete_callback (APTERYX_INDEXERS_PATH, path, (void *)lua_do_index, (void *) ref))
{
luaL_error (L, "Failed to unregister callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static bool
lua_do_watch (const char *path, const char *value, size_t ref)
{
int res = 0;
lua_State* L = g_L;
ASSERT (L, return false, "Watch: LUA is not multi-threaded (use apteryx.process)\n");
int ssize = lua_gettop (L);
if (!push_callback (L, ref))
return false;
lua_pushstring (L, path);
lua_pushstring (L, value);
res = lua_pcall (L, 2, 0, 0);
if (res != 0)
lua_apteryx_error (L, res);
lua_pop (L, 1); /* pop fn */
ASSERT (lua_gettop (L) == ssize, return true, "Watch: Stack changed\n");
return true;
}
static int
lua_apteryx_watch (lua_State *L)
{
luaL_checktype (L, 1, LUA_TSTRING);
luaL_checktype (L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!add_callback (APTERYX_WATCHERS_PATH, path, (void *)lua_do_watch, true, (void *) ref, 0))
{
luaL_error (L, "Failed to register watch\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_apteryx_unwatch (lua_State *L)
{
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!delete_callback (APTERYX_WATCHERS_PATH, path, (void *)lua_do_watch, (void *) ref))
{
luaL_error (L, "Failed to unregister callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_do_refresh (const char *path, size_t ref)
{
int res = 0;
lua_State* L = g_L;
ASSERT (L, return 0, "Refresh: LUA is not multi-threaded (use apteryx.process)\n");
int ssize = lua_gettop (L);
int timeout = 0;
if (!push_callback (L, ref))
return 0;
lua_pushstring (L, path);
res = lua_pcall (L, 1, 1, 0);
if (res != 0)
lua_apteryx_error (L, res);
if (lua_gettop (L))
{
timeout = lua_tonumber (L, -1);
lua_pop (L, 1);
}
lua_pop (L, 1); /* pop fn */
ASSERT (lua_gettop (L) == ssize, return timeout, "Refresh: Stack changed\n");
return timeout;
}
static int
lua_apteryx_refresh (lua_State *L)
{
luaL_checktype (L, 1, LUA_TSTRING);
luaL_checktype (L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!add_callback (APTERYX_REFRESHERS_PATH, path, (void *)lua_do_refresh, false, (void *) ref, 0))
{
luaL_error (L, "Failed to register refresh\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_apteryx_unrefresh (lua_State *L)
{
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!delete_callback (APTERYX_REFRESHERS_PATH, path, (void *)lua_do_refresh, (void *) ref))
{
luaL_error (L, "Failed to unregister callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_do_validate (const char *path, const char *value, size_t ref)
{
int res = 0;
lua_State* L = g_L;
ASSERT (L, return -1, "Validate: LUA is not multi-threaded (use apteryx.process)\n");
int ssize = lua_gettop (L);
int rc = 0;
if (!push_callback (L, ref))
return -1;
lua_pushstring (L, path);
lua_pushstring (L, value);
res = lua_pcall (L, 2, 1, 0);
if (res != 0)
lua_apteryx_error (L, res);
if (lua_gettop (L))
{
rc = lua_tonumber (L, -1);
lua_pop (L, 1);
}
lua_pop (L, 1); /* pop fn */
ASSERT (lua_gettop (L) == ssize, return rc, "Validate: Stack changed\n");
return rc;
}
static int
lua_apteryx_validate (lua_State *L)
{
luaL_checktype (L, 1, LUA_TSTRING);
luaL_checktype (L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!add_callback (APTERYX_VALIDATORS_PATH, path, (void *)lua_do_validate, true, (void *) ref, 0))
{
luaL_error (L, "Failed to register callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_apteryx_unvalidate (lua_State *L)
{
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!delete_callback (APTERYX_VALIDATORS_PATH, path, (void *)lua_do_validate, (void *) ref))
{
luaL_error (L, "Failed to unregister callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static char*
lua_do_provide (const char *path, size_t ref)
{
int res = 0;
lua_State* L = g_L;
ASSERT (L, return NULL, "Provide: LUA is not multi-threaded (use apteryx.process)\n");
int ssize = lua_gettop (L);
char *value = NULL;
if (!push_callback (L, ref))
return NULL;
lua_pushstring (L, path);
res = lua_pcall (L, 1, 1, 0);
if (res != 0)
lua_apteryx_error (L, res);
if (lua_gettop (L))
{
value = strdup (lua_apteryx_tostring (L, -1));
lua_pop (L, 1);
}
lua_pop (L, 1); /* pop fn */
ASSERT (lua_gettop (L) == ssize, return value, "Provide: Stack changed\n");
return value;
}
static int
lua_apteryx_provide (lua_State *L)
{
luaL_checktype (L, 1, LUA_TSTRING);
luaL_checktype (L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!add_callback (APTERYX_PROVIDERS_PATH, path, (void *)lua_do_provide, false, (void *) ref, 0))
{
luaL_error (L, "Failed to register callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_apteryx_unprovide (lua_State *L)
{
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TFUNCTION);
const char *path = lua_tostring (L, 1);
size_t ref = ref_callback (L, 2);
if (!delete_callback (APTERYX_PROVIDERS_PATH, path, (void *)lua_do_provide, (void *) ref))
{
luaL_error (L, "Failed to unregister callback\n");
lua_pushboolean (L, false);
return 1;
}
lua_pushboolean (L, true);
return 1;
}
static int
lua_apteryx_process (lua_State *L)
{
if (lua_gettop (L) == 1 && !lua_isboolean (L, 1))
{
luaL_error (L, "Invalid arguments: process(bool poll)");
return 0;
}
bool poll = lua_gettop (L) == 1 ? lua_toboolean (L, 1) : true;
g_L = L;
int fd = apteryx_process (poll);
lua_pushnumber (L, fd);
g_L = NULL;
return 1;
}
static bool running = false;
static void
termination_handler (void)
{
running = false;
}
static int
lua_apteryx_timestamp (lua_State *L)
{
uint64_t ret;
if (lua_gettop (L) != 1 || !lua_isstring (L, 1))
{
luaL_error (L, "Invalid arguments: requires path");
return 0;
}
ret = apteryx_timestamp (lua_tostring (L, 1));
lua_pop (L, 1);
lua_pushinteger (L, ret);
return 1;
}
static int
lua_apteryx_mainloop (lua_State *L)
{
int fd = 0;
struct pollfd pfd;
uint8_t dummy = 0;
if (lua_gettop (L) > 1 ||
(lua_gettop (L) == 1 && !lua_isboolean (L, 1)))
{
luaL_error (L, "Invalid arguments: mainloop(bool catch_sig)");
return 0;
}
if (lua_gettop (L) == 1 && lua_toboolean (L, 1))
{
signal (SIGTERM, (__sighandler_t) termination_handler);
signal (SIGINT, (__sighandler_t) termination_handler);
}
running = true;
while (running && fd >= 0)
{
g_L = L;
fd = apteryx_process (true);
g_L = NULL;
pfd.fd = fd;
pfd.events = POLLIN;
poll (&pfd, 1, -1);
if (running && (!(pfd.revents & POLLIN) || read (fd, &dummy, 1) == 0))
{
luaL_error (L, "Poll/Read error: %s\n", strerror (errno));
}
}
return 0;
}
int
luaopen_libapteryx (lua_State *L)
{
/* Standard functions */
static const luaL_Reg _apteryx_fns[] = {
{ "debug", lua_apteryx_debug },
{ "set", lua_apteryx_set },
{ "get", lua_apteryx_get },
{ "search", lua_apteryx_search },
{ "prune", lua_apteryx_prune },
{ "get_tree", lua_apteryx_get_tree },
{ "set_tree", lua_apteryx_set_tree },
{ "query", lua_apteryx_query },
{ "timestamp", lua_apteryx_timestamp },
{ "index", lua_apteryx_index },
{ "unindex", lua_apteryx_unindex },
{ "watch", lua_apteryx_watch },
{ "unwatch", lua_apteryx_unwatch },
{ "refresh", lua_apteryx_refresh },
{ "unrefresh", lua_apteryx_unrefresh },
{ "validate", lua_apteryx_validate },
{ "unvalidate", lua_apteryx_unvalidate },
{ "provide", lua_apteryx_provide },
{ "unprovide", lua_apteryx_unprovide },
{ "process", lua_apteryx_process },
{ "mainloop", lua_apteryx_mainloop },
{ NULL, NULL }
};
/* Initialise Apteryx */
if (!apteryx_init (false))
{
return 0;
}
/* Return the Apteryx object on the stack */
luaL_newmetatable (L, "apteryx");
luaL_setfuncs (L, _apteryx_fns, 0);
return 1;
}
int
luaopen_apteryx (lua_State *L)
{
return luaopen_libapteryx (L);
}
#endif /* HAVE_LUA */