]> git.proxmox.com Git - mirror_frr.git/blame - lib/frrscript.h
lib: better load-time error handling for scripts
[mirror_frr.git] / lib / frrscript.h
CommitLineData
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
22#include "frrlua.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#define FRRSCRIPT_PATH "/etc/frr/scripts"
29
f944ec67
QY
30typedef int (*encoder_func)(struct lua_State *, const void *);
31
5f98c815
QY
32struct frrscript {
33 /* Script name */
34 char *name;
35
36 /* Lua state */
37 struct lua_State *L;
38};
39
40
41/*
42 * Create new FRR script.
43 */
44struct frrscript *frrscript_load(const char *name,
45 int (*load_cb)(struct frrscript *));
46
47/*
48 * Destroy FRR script.
49 */
50void frrscript_unload(struct frrscript *fs);
51
52/*
53 * Register a Lua encoder for a type.
54 *
55 * tname
56 * Name of type; e.g., "peer", "ospf_interface", etc. Chosen at will.
57 *
58 * encoder
59 * Function pointer to encoder function. Encoder function should push a Lua
60 * table representing the passed argument - which will have the C type
61 * associated with the chosen 'tname' to the provided stack.
62 *
63 */
f944ec67 64void frrscript_register_type_encoder(const char *tname, encoder_func encoder);
5f98c815
QY
65
66/*
67 * Initialize scripting subsystem. Call this before anything else.
68 */
69void frrscript_init(void);
70
71/*
72 * Forward decl for frrscript_lua_call
73 */
74int frrscript_lua_call(struct frrscript *fs, ...);
75
76/*
77 * Call FRR script.
3b002f19
QY
78 *
79 * Call it like this:
80 *
81 * frrscript_call(fs, FRRSCRIPT_ARGS("cool_prefix", "prefix", p),
82 * FRRSCRIPT_RESULTS("result1", "result2"))
5f98c815
QY
83 */
84#define frrscript_call(fs, ...) frrscript_lua_call((fs), __VA_ARGS__)
85
3b002f19
QY
86/*
87 * Macro that defines the arguments to a script.
88 *
89 * For each argument you want to pass to a script, pass *three* arguments to
90 * this function. The first should be name of the variable to bind the argument
91 * to in the script's environment. The second should be the type, as registered
92 * by frrscript_register_type_encoder(). The third should be the argument
93 * itself.
94 *
95 * This macro itself should be used as the second argument to frrscript_call().
96 */
97#define FRRSCRIPT_ARGS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__
98
99/*
100 * Macro that defines the results from a script.
101 *
102 * Similar to FRRSCRIPT_ARGS, except this defines the results from a script.
103 *
104 * The first argument should be the name to bind the first result to and will
105 * be used after the script finishes to get that particular result value.
106 *
107 * This macro itself should be used as the third argument to frrscript_call().
108 * It may not be omitted.
109 */
110#define FRRSCRIPT_RESULTS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__
111
5f98c815
QY
112#ifdef __cplusplus
113}
114#endif /* __cplusplus */
115
116#endif /* __FRRSCRIPT_H__ */