plugins.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, plg_cqd;
  39. struct plugin *plugins[] = {
  40. &plg_hello, &plg_robot, &plg_m4, &plg_script, &plg_hist, &plg_cqd,
  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. return plg_remove(p);
  99. }
  100. int do_cmd_pins(int argc, char **argv) {
  101. struct plugin *p;
  102. char name[MSG_SIZE];
  103. char *s;
  104. switch (argc) {
  105. case 1:
  106. fprintf(stderr, _("plugin: "));
  107. scanf("%s", name);
  108. s = name;
  109. break;
  110. case 2:
  111. s = argv[1];
  112. break;
  113. default:
  114. fprintf(stderr, _("usage: -pins [plugin-name]\n"));
  115. return 1;
  116. }
  117. if (!(p = find_plg(s))) {
  118. fprintf(stderr, _("*** plugin not found. \n"));
  119. return 1;
  120. }
  121. plg_insert(p);
  122. if (p->init)
  123. p->init();
  124. return 0;
  125. }