]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | #include <lua.h> | |
23 | #include "frrlua.h" | |
24 | ||
25 | #ifdef __cplusplus | |
26 | extern "C" { | |
27 | #endif | |
28 | ||
29 | #define FRRSCRIPT_PATH "/etc/frr/scripts" | |
30 | ||
31 | typedef void (*encoder_func)(lua_State *, const void *); | |
32 | typedef void *(*decoder_func)(lua_State *, int); | |
33 | ||
34 | struct frrscript_codec { | |
35 | const char *typename; | |
36 | encoder_func encoder; | |
37 | decoder_func decoder; | |
38 | }; | |
39 | ||
40 | struct frrscript { | |
41 | /* Script name */ | |
42 | char *name; | |
43 | ||
44 | /* Lua state */ | |
45 | struct lua_State *L; | |
46 | }; | |
47 | ||
48 | struct frrscript_env { | |
49 | /* Value type */ | |
50 | const char *typename; | |
51 | ||
52 | /* Binding name */ | |
53 | const char *name; | |
54 | ||
55 | /* Value */ | |
56 | const void *val; | |
57 | }; | |
58 | ||
59 | /* | |
60 | * Create new FRR script. | |
61 | */ | |
62 | struct frrscript *frrscript_load(const char *name, | |
63 | int (*load_cb)(struct frrscript *)); | |
64 | ||
65 | /* | |
66 | * Destroy FRR script. | |
67 | */ | |
68 | void frrscript_unload(struct frrscript *fs); | |
69 | ||
70 | /* | |
71 | * Register a Lua codec for a type. | |
72 | * | |
73 | * tname | |
74 | * Name of type; e.g., "peer", "ospf_interface", etc. Chosen at will. | |
75 | * | |
76 | * codec(s) | |
77 | * Function pointer to codec struct. Encoder function should push a Lua | |
78 | * table representing the passed argument - which will have the C type | |
79 | * associated with the chosen 'tname' to the provided stack. The decoder | |
80 | * function should pop a value from the top of the stack and return a heap | |
81 | * chunk containing that value. Allocations should be made with MTYPE_TMP. | |
82 | * | |
83 | * If using the plural function variant, pass a NULL-terminated array. | |
84 | * | |
85 | */ | |
86 | void frrscript_register_type_codec(struct frrscript_codec *codec); | |
87 | void frrscript_register_type_codecs(struct frrscript_codec *codecs); | |
88 | ||
89 | /* | |
90 | * Initialize scripting subsystem. Call this before anything else. | |
91 | */ | |
92 | void frrscript_init(void); | |
93 | ||
94 | ||
95 | /* | |
96 | * Call script. | |
97 | * | |
98 | * fs | |
99 | * The script to call; this is obtained from frrscript_load(). | |
100 | * | |
101 | * env | |
102 | * The script's environment. Specify this as an array of frrscript_env. | |
103 | * | |
104 | * Returns: | |
105 | * 0 if the script ran successfully, nonzero otherwise. | |
106 | */ | |
107 | int frrscript_call(struct frrscript *fs, struct frrscript_env *env); | |
108 | ||
109 | ||
110 | /* | |
111 | * Get result from finished script. | |
112 | * | |
113 | * fs | |
114 | * The script. This script must have been run already. | |
115 | * | |
116 | * result | |
117 | * The result to extract from the script. | |
118 | * This reuses the frrscript_env type, but only the typename and name fields | |
119 | * need to be set. The value is returned directly. | |
120 | * | |
121 | * Returns: | |
122 | * The script result of the specified name and type, or NULL. | |
123 | */ | |
124 | void *frrscript_get_result(struct frrscript *fs, | |
125 | const struct frrscript_env *result); | |
126 | ||
127 | #ifdef __cplusplus | |
128 | } | |
129 | #endif /* __cplusplus */ | |
130 | ||
131 | #endif /* __FRRSCRIPT_H__ */ |