plugins.c 3.1 KB

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