hist.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* vcc/plugins/hist/hist.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 <string.h>
  23. #include <limits.h>
  24. #include <sys/types.h>
  25. #include <sys/times.h>
  26. #include <vcc/vcc.h>
  27. #include <vcc/pretty.h>
  28. #include <vcc/plugin.h>
  29. #include <vcc/version.h>
  30. #include <readconf/readconf.h>
  31. int hist_init(void);
  32. int hist_fd;
  33. struct cmdtable hist_cmdtab[] = {
  34. { NULL, NULL, NULL }
  35. };
  36. struct plugin plg_hist = {
  37. .state = 0,
  38. .name = "hist",
  39. .descr = "Show history. ",
  40. .init = hist_init,
  41. .cmdtab = hist_cmdtab
  42. };
  43. #define this_plg (&plg_hist)
  44. #define DEFAULT_HISTFILE ".vcc_history"
  45. char *hist_recvhook(char *s, char *usr) {
  46. dprintf(hist_fd, "[%ld] %s: %s", times(NULL), usr, s);
  47. return strdup(s);
  48. }
  49. int open_hist_file(void) {
  50. struct readconf_strconf *conf;
  51. char *p;
  52. char buf[PATH_MAX];
  53. if (!(conf = readconf_find_strname("histfile"))) {
  54. p = getenv("HOME");
  55. sprintf(buf, "%s/" DEFAULT_HISTFILE, p);
  56. p = buf;
  57. }
  58. else
  59. p = conf->conf_value;
  60. printf("using history file %s\n", p);
  61. if (unlikely((hist_fd = open(p, O_WRONLY | O_APPEND | O_CREAT)) < 0)) {
  62. perror("open()");
  63. return -1;
  64. }
  65. return hist_fd;
  66. }
  67. int hist_init(void) {
  68. if (unlikely((hist_fd = open_hist_file()) < 0)) {
  69. fprintf(stderr, "*** unable to open history file. \n");
  70. return 1;
  71. }
  72. return register_recvhook(hist_recvhook, this_plg);
  73. }