plugins.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* vcc/plugins.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <poll.h>
  31. #include <signal.h>
  32. #include <klist.h>
  33. #include <vcc/vcc.h>
  34. #include <vcc/plugin.h>
  35. #include <vcc/version.h>
  36. #include <vcc/pretty.h>
  37. #include <vcc/interfaces.h>
  38. extern struct plugin plg_hello, plg_robot, plg_m4, plg_script;
  39. hookfn_t msg_hook, recv_hook;
  40. struct plugin *plugins[] = {
  41. &plg_hello, &plg_robot, &plg_m4, &plg_script,
  42. (struct plugin *) NULL
  43. };
  44. int do_cmd_pls(int argc, char **argv) {
  45. int i;
  46. struct plugin *p;
  47. (void) argv;
  48. if (argc != 1) {
  49. fprintf(stderr, _("usage: -pls\n"));
  50. return 1;
  51. }
  52. for (i = 0; (p = plugins[i]); i++)
  53. pretty_cmd_help(p->name, p->descr);
  54. return 0;
  55. }
  56. struct plugin *find_plg(char *s) {
  57. int i;
  58. struct plugin *p;
  59. for (i = 0; (p = plugins[i]); i++)
  60. if (!strcmp(s, p->name))
  61. return p;
  62. return NULL;
  63. }
  64. int plg_insert(struct plugin *plg) {
  65. plg->state = PLG_INSERTED;
  66. return 0;
  67. }
  68. int plg_remove(struct plugin *plg) {
  69. plg->state = 0;
  70. return 0;
  71. }
  72. int do_cmd_prem(int argc, char **argv) {
  73. struct plugin *p;
  74. char name[MSG_SIZE];
  75. char *s;
  76. switch (argc) {
  77. case 1:
  78. fprintf(stderr, _("plugin: "));
  79. scanf("%s", name);
  80. s = name;
  81. break;
  82. case 2:
  83. s = argv[1];
  84. break;
  85. default:
  86. fprintf(stderr, _("usage: -prem [plugin-name]\n"));
  87. break;
  88. }
  89. if (!(p = find_plg(s))) {
  90. fprintf(stderr, _("*** plugin not found. \n"));
  91. return 1;
  92. }
  93. if (p->state != PLG_INSERTED) {
  94. fprintf(stderr, _("*** plugin didn't insert. \n"));
  95. return 1;
  96. }
  97. if (p->exit)
  98. p->exit();
  99. unregister_msghook(p->msg_hook, p);
  100. unregister_recvhook(p->recv_hook, p);
  101. return plg_remove(p);
  102. }
  103. int do_cmd_pins(int argc, char **argv) {
  104. struct plugin *p;
  105. char name[MSG_SIZE];
  106. char *s;
  107. switch (argc) {
  108. case 1:
  109. fprintf(stderr, _("plugin: "));
  110. scanf("%s", name);
  111. s = name;
  112. break;
  113. case 2:
  114. s = argv[1];
  115. break;
  116. default:
  117. fprintf(stderr, _("usage: -pins [plugin-name]\n"));
  118. return 1;
  119. }
  120. if (!(p = find_plg(s))) {
  121. fprintf(stderr, _("*** plugin not found. \n"));
  122. return 1;
  123. }
  124. plg_insert(p);
  125. if (p->init)
  126. p->init();
  127. return 0;
  128. }
  129. int register_msghook(hookfn_t hook, struct plugin *plg) {
  130. /* unlike other things, we can have only one hook at the same moment. */
  131. if (msg_hook)
  132. fprintf(stderr, _("warning: the old hook removed. \n"));
  133. plg->msg_hook = msg_hook = hook;
  134. return 0;
  135. }
  136. int register_recvhook(hookfn_t hook, struct plugin *plg) {
  137. if (recv_hook)
  138. fprintf(stderr, _("warning: the old hook removed. \n"));
  139. plg->recv_hook = recv_hook = hook;
  140. return 0;
  141. }
  142. int unregister_msghook(hookfn_t hook, struct plugin *plg) {
  143. if (msg_hook != hook)
  144. return 0;
  145. plg->msg_hook = msg_hook = NULL;
  146. return 0;
  147. }
  148. int unregister_recvhook(hookfn_t hook, struct plugin *plg) {
  149. if (recv_hook != hook)
  150. return 0;
  151. plg->recv_hook = recv_hook = NULL;
  152. return 0;
  153. }