lcli.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include <sys/select.h>
  22. #include <sys/socket.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <sys/un.h>
  26. #include "interpreter.h"
  27. int main(int argc, char **argv) {
  28. //set_dispatch_functions(isatty(0) ? fgets_from_stdin : fread_from_stdin);
  29. init_readline();
  30. set_dispatch_functions();
  31. char socket_name[1024];
  32. {
  33. char *s = getenv("LSERVER_HOME");
  34. if (s) {
  35. if (strlen(s) > 1000) {
  36. fprintf(stderr, "Too long LSERVER_HOME");
  37. return EXIT_FAILURE;
  38. }
  39. strncpy(socket_name, s, 1024);
  40. } else {
  41. s = getenv("HOME");
  42. if (strlen(s) > 1000) {
  43. fprintf(stderr, "Too long homedir (?!)");
  44. return EXIT_FAILURE;
  45. }
  46. strncpy(socket_name, s, 1024);
  47. strcat(socket_name, "/.lserver");
  48. }
  49. s = getenv("LSERVER_SOCKET");
  50. if (s == NULL) s = "default";
  51. if (strlen(socket_name) + strlen("/tmp/") + strlen(s) > 1023) {
  52. fprintf(stderr, "Too long everything (?!)");
  53. return EXIT_FAILURE;
  54. }
  55. strcat(socket_name, "/tmp/");
  56. strcat(socket_name, s);
  57. }
  58. struct session s = {0};
  59. s.cont = 1;
  60. s.argv0 = argv[0];
  61. s.lisp_argc = argc - 1;
  62. s.lisp_argv = argv + 1;
  63. struct sockaddr_un servaddr;
  64. s.fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  65. bzero(&servaddr, sizeof(servaddr));
  66. servaddr.sun_family = AF_LOCAL;
  67. strcpy(servaddr.sun_path, socket_name);
  68. if (connect(s.fd, (struct sockaddr *) &servaddr, sizeof(servaddr))) {
  69. fprintf(stderr, "Cannot connect to server on socket %s\n", socket_name);
  70. perror("connect");
  71. return EXIT_FAILURE;
  72. }
  73. while (s.cont) {
  74. if (cmd_from_lisp(&s)) return EXIT_FAILURE;
  75. if (s.cmd >= NUM_COMMANDS || s.cmd < 0) {
  76. fprintf(stderr, "Unknown command code %d.\n", (int) s.cmd);
  77. return EXIT_FAILURE;
  78. }
  79. //printf("cmd %d\n", s.cmd);
  80. if ((*dispatch_functions[s.cmd])(&s)) return EXIT_FAILURE;
  81. }
  82. return (int) s.i;
  83. }