]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrscript.c
9b27ca745943b3a4f2b526a861153c56a5799cb3
2 * Copyright (C) 2020 NVIDIA Corporation
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)
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
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
22 #include "frrscript.h"
33 int (*encoder
)(struct lua_State
*, const void *);
36 struct hash
*encoder_hash
;
38 static unsigned int encoder_hash_key(const void *data
)
40 const struct encoder
*e
= data
;
42 return string_hash_make(e
->typename
);
45 static bool encoder_hash_cmp(const void *d1
, const void *d2
)
47 return !strcmp(d1
, d2
);
50 static void *encoder_alloc(void *arg
)
52 struct encoder
*tmp
= arg
;
54 struct encoder
*e
= XCALLOC(MTYPE_TMP
, sizeof(struct encoder
));
55 e
->typename
= XSTRDUP(MTYPE_TMP
, tmp
->typename
);
61 static void encoder_free(struct encoder
*e
)
63 XFREE(MTYPE_TMP
, e
->typename
);
68 /* Generic script APIs */
70 int frrscript_lua_call(struct frrscript
*fs
, ...)
72 /* Process arguments according to argspec in fs */
75 int ret
= lua_pcall(fs
->L
, 0, 0, 0);
77 /* Process stack result according to argspec in fs */
80 /* LUA_OK is 0, so we can just return lua_pcall's result directly */
84 void frrscript_register_type_encoder(const char *typename
,
85 int (*encoder
)(lua_State
*L
, void *))
88 .typename
= (char *) typename
,
92 if (hash_lookup(encoder_hash
, &e
)) {
93 zlog_backtrace(LOG_ERR
);
94 assert(!"Type encoder double-registered.");
97 hash_get(encoder_hash
, &e
, encoder_alloc
);
101 struct frrscript
*frrscript_load(const char *name
,
102 int (*load_cb
)(struct frrscript
*))
104 struct frrscript
*fs
= XCALLOC(MTYPE_TMP
, sizeof(struct frrscript
));
106 fs
->name
= XSTRDUP(MTYPE_TMP
, name
);
107 fs
->L
= luaL_newstate();
109 char fname
[MAXPATHLEN
];
110 snprintf(fname
, sizeof(fname
), FRRSCRIPT_PATH
"/%s.lua", fs
->name
);
112 if (luaL_loadfile(fs
->L
, fname
) != LUA_OK
)
115 if (load_cb
&& (*load_cb
)(fs
) != 0)
120 frrscript_unload(fs
);
124 void frrscript_unload(struct frrscript
*fs
)
126 XFREE(MTYPE_TMP
, fs
->name
);
127 XFREE(MTYPE_TMP
, fs
);
130 void frrscript_init()
132 encoder_hash
= hash_create(encoder_hash_key
, encoder_hash_cmp
, "Lua type encoders");