]>
Commit | Line | Data |
---|---|---|
5f98c815 QY |
1 | /* Scripting foo |
2 | * Copyright (C) 2020 NVIDIA Corporation | |
3 | * Quentin Young | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | #ifndef __FRRSCRIPT_H__ | |
20 | #define __FRRSCRIPT_H__ | |
21 | ||
9e47ee98 | 22 | #include <lua.h> |
5f98c815 QY |
23 | #include "frrlua.h" |
24 | ||
25 | #ifdef __cplusplus | |
26 | extern "C" { | |
27 | #endif | |
28 | ||
29 | #define FRRSCRIPT_PATH "/etc/frr/scripts" | |
30 | ||
47dd8736 | 31 | typedef void (*encoder_func)(lua_State *, const void *); |
f944ec67 | 32 | |
5f98c815 QY |
33 | struct frrscript { |
34 | /* Script name */ | |
35 | char *name; | |
36 | ||
37 | /* Lua state */ | |
38 | struct lua_State *L; | |
39 | }; | |
40 | ||
41 | ||
42 | /* | |
43 | * Create new FRR script. | |
44 | */ | |
45 | struct frrscript *frrscript_load(const char *name, | |
46 | int (*load_cb)(struct frrscript *)); | |
47 | ||
48 | /* | |
49 | * Destroy FRR script. | |
50 | */ | |
51 | void frrscript_unload(struct frrscript *fs); | |
52 | ||
53 | /* | |
54 | * Register a Lua encoder for a type. | |
55 | * | |
56 | * tname | |
57 | * Name of type; e.g., "peer", "ospf_interface", etc. Chosen at will. | |
58 | * | |
59 | * encoder | |
60 | * Function pointer to encoder function. Encoder function should push a Lua | |
61 | * table representing the passed argument - which will have the C type | |
62 | * associated with the chosen 'tname' to the provided stack. | |
63 | * | |
64 | */ | |
f944ec67 | 65 | void frrscript_register_type_encoder(const char *tname, encoder_func encoder); |
5f98c815 QY |
66 | |
67 | /* | |
68 | * Initialize scripting subsystem. Call this before anything else. | |
69 | */ | |
70 | void frrscript_init(void); | |
71 | ||
72 | /* | |
73 | * Forward decl for frrscript_lua_call | |
74 | */ | |
75 | int frrscript_lua_call(struct frrscript *fs, ...); | |
76 | ||
77 | /* | |
78 | * Call FRR script. | |
3b002f19 QY |
79 | * |
80 | * Call it like this: | |
81 | * | |
82 | * frrscript_call(fs, FRRSCRIPT_ARGS("cool_prefix", "prefix", p), | |
83 | * FRRSCRIPT_RESULTS("result1", "result2")) | |
5f98c815 QY |
84 | */ |
85 | #define frrscript_call(fs, ...) frrscript_lua_call((fs), __VA_ARGS__) | |
86 | ||
3b002f19 QY |
87 | /* |
88 | * Macro that defines the arguments to a script. | |
89 | * | |
90 | * For each argument you want to pass to a script, pass *three* arguments to | |
91 | * this function. The first should be name of the variable to bind the argument | |
92 | * to in the script's environment. The second should be the type, as registered | |
93 | * by frrscript_register_type_encoder(). The third should be the argument | |
94 | * itself. | |
95 | * | |
96 | * This macro itself should be used as the second argument to frrscript_call(). | |
97 | */ | |
98 | #define FRRSCRIPT_ARGS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ | |
99 | ||
100 | /* | |
101 | * Macro that defines the results from a script. | |
102 | * | |
103 | * Similar to FRRSCRIPT_ARGS, except this defines the results from a script. | |
104 | * | |
105 | * The first argument should be the name to bind the first result to and will | |
106 | * be used after the script finishes to get that particular result value. | |
107 | * | |
108 | * This macro itself should be used as the third argument to frrscript_call(). | |
109 | * It may not be omitted. | |
110 | */ | |
111 | #define FRRSCRIPT_RESULTS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ | |
112 | ||
5f98c815 QY |
113 | #ifdef __cplusplus |
114 | } | |
115 | #endif /* __cplusplus */ | |
116 | ||
117 | #endif /* __FRRSCRIPT_H__ */ |