plugins.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. extern struct plugin plg_hello, plg_robot, plg_m4, plg_script;
  38. hookfn_t msg_hook, recv_hook;
  39. struct plugin *plugins[] = {
  40. &plg_hello, &plg_robot, &plg_m4, &plg_script,
  41. (struct plugin *) NULL
  42. };
  43. int do_cmd_pls(int argc, char **argv) {
  44. int i;
  45. struct plugin *p;
  46. (void) argv;
  47. if (argc != 1) {
  48. fprintf(stderr, "usage: -pls\n");
  49. return 1;
  50. }
  51. for (i = 0; (p = plugins[i]); i++)
  52. pretty_cmd_help(p->name, p->descr);
  53. return 0;
  54. }
  55. struct plugin *find_plg(char *s) {
  56. int i;
  57. struct plugin *p;
  58. for (i = 0; (p = plugins[i]); i++)
  59. if (!strcmp(s, p->name))
  60. return p;
  61. return NULL;
  62. }
  63. int plg_insert(struct plugin *plg) {
  64. plg->state = PLG_INSERTED;
  65. return 0;
  66. }
  67. int plg_remove(struct plugin *plg) {
  68. plg->state = 0;
  69. return 0;
  70. }
  71. int do_cmd_prem(int argc, char **argv) {
  72. struct plugin *p;
  73. char name[MSG_SIZE];
  74. char *s;
  75. switch (argc) {
  76. case 1:
  77. fprintf(stderr, "plugin: ");
  78. scanf("%s", name);
  79. s = name;
  80. break;
  81. case 2:
  82. s = argv[1];
  83. break;
  84. default:
  85. fprintf(stderr, "usage: -prem [plugin-name]\n");
  86. break;
  87. }
  88. if (!(p = find_plg(s))) {
  89. fprintf(stderr, "*** plugin not found. \n");
  90. return 1;
  91. }
  92. if (p->state != PLG_INSERTED) {
  93. fprintf(stderr, "*** plugin didn't insert. \n");
  94. return 1;
  95. }
  96. if (p->exit)
  97. p->exit();
  98. unregister_msghook(p->msg_hook, p);
  99. unregister_recvhook(p->recv_hook, p);
  100. return plg_remove(p);
  101. }
  102. int do_cmd_pins(int argc, char **argv) {
  103. struct plugin *p;
  104. char name[MSG_SIZE];
  105. char *s;
  106. switch (argc) {
  107. case 1:
  108. fprintf(stderr, "plugin: ");
  109. scanf("%s", name);
  110. s = name;
  111. break;
  112. case 2:
  113. s = argv[1];
  114. break;
  115. default:
  116. fprintf(stderr, "usage: -pins [plugin-name]\n");
  117. return 1;
  118. }
  119. if (!(p = find_plg(s))) {
  120. fprintf(stderr, "*** plugin not found. \n");
  121. return 1;
  122. }
  123. plg_insert(p);
  124. if (p->init)
  125. p->init();
  126. return 0;
  127. }
  128. int register_msghook(hookfn_t hook, struct plugin *plg) {
  129. /* unlike other things, we can have only one hook at the same moment. */
  130. if (msg_hook)
  131. fprintf(stderr, "warning: the old hook removed. \n");
  132. plg->msg_hook = msg_hook = hook;
  133. return 0;
  134. }
  135. int register_recvhook(hookfn_t hook, struct plugin *plg) {
  136. if (recv_hook)
  137. fprintf(stderr, "warning: the old hook removed. \n");
  138. plg->recv_hook = recv_hook = hook;
  139. return 0;
  140. }
  141. int unregister_msghook(hookfn_t hook, struct plugin *plg) {
  142. if (msg_hook != hook)
  143. return 0;
  144. plg->msg_hook = msg_hook = NULL;
  145. return 0;
  146. }
  147. int unregister_recvhook(hookfn_t hook, struct plugin *plg) {
  148. if (recv_hook != hook)
  149. return 0;
  150. plg->recv_hook = recv_hook = NULL;
  151. return 0;
  152. }