plugins.c 3.7 KB

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