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