| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /* vcc/plugins.c
- *
- * This file is part of vcc.
- *
- * vcc is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * vcc is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with vcc. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include <ctype.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <poll.h>
- #include <signal.h>
- #include <klist.h>
- #include <vcc/vcc.h>
- #include <vcc/plugin.h>
- #include <vcc/version.h>
- #include <vcc/pretty.h>
- extern struct plugin plg_hello, plg_robot, plg_m4;
- hookfn_t msg_hook, recv_hook;
- struct plugin *plugins[] = {
- &plg_hello, &plg_robot, &plg_m4,
- (struct plugin *) NULL
- };
- int do_cmd_pls(char *s) {
- int i;
- struct plugin *p;
- (void) s;
- for (i = 0; (p = plugins[i]); i++)
- pretty_cmd_help(p->name, p->descr);
-
- return 0;
- }
- struct plugin *find_plg(char *s) {
- int i;
- struct plugin *p;
- for (i = 0; (p = plugins[i]); i++)
- if (!strcmp(s, p->name))
- return p;
- return NULL;
- }
- int plg_insert(struct plugin *plg) {
- plg->state = PLG_INSERTED;
- return 0;
- }
- int plg_remove(struct plugin *plg) {
- plg->state = 0;
- return 0;
- }
- int do_cmd_prem(int argc, char **argv) {
- struct plugin *p;
- char name[MSG_SIZE];
- char *s;
- switch (argc) {
- case 1:
- fprintf(stderr, "plugin: ");
- scanf("%s", name);
- s = name;
- break;
- case 2:
- s = argv[1];
- break;
- default:
- fprintf(stderr, "usage: -prem [plugin-name]\n");
- break;
- }
- if (!(p = find_plg(s))) {
- fprintf(stderr, "*** plugin not found. \n");
- return 1;
- }
- if (p->state != PLG_INSERTED) {
- fprintf(stderr, "*** plugin didn't insert. \n");
- return 1;
- }
- if (p->exit)
- p->exit();
- unregister_msghook(p->msg_hook, p);
- unregister_recvhook(p->recv_hook, p);
-
- return plg_remove(p);
- }
- int do_cmd_pins(int argc, char **argv) {
- struct plugin *p;
- char name[MSG_SIZE];
- char *s;
- switch (argc) {
- case 1:
- fprintf(stderr, "plugin: ");
- scanf("%s", name);
- s = name;
- break;
- case 2:
- s = argv[1];
- break;
- default:
- fprintf(stderr, "usage: -pins [plugin-name]\n");
- return 1;
- }
- if (!(p = find_plg(s))) {
- fprintf(stderr, "*** plugin not found. \n");
- return 1;
- }
-
- plg_insert(p);
-
- if (p->init)
- p->init();
- return 0;
- }
- int register_msghook(hookfn_t hook, struct plugin *plg) {
- /* unlike other things, we can have only one hook at the same moment. */
- if (msg_hook)
- fprintf(stderr, "warning: the old hook removed. \n");
-
- plg->msg_hook = msg_hook = hook;
- return 0;
- }
- int register_recvhook(hookfn_t hook, struct plugin *plg) {
- if (recv_hook)
- fprintf(stderr, "warning: the old hook removed. \n");
-
- plg->recv_hook = recv_hook = hook;
- return 0;
-
- }
- int unregister_msghook(hookfn_t hook, struct plugin *plg) {
- if (msg_hook != hook)
- return 0;
- plg->msg_hook = msg_hook = NULL;
- return 0;
- }
- int unregister_recvhook(hookfn_t hook, struct plugin *plg) {
- if (recv_hook != hook)
- return 0;
- plg->recv_hook = recv_hook = NULL;
- return 0;
- }
|