lclient.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. void print_usage(void) {
  28. fprintf(stderr, "usage: lclient [-s, --sock SOCKET] [--] [LISP_ARGUMENTS]");
  29. }
  30. int main(int argc, char **argv) {
  31. //set_dispatch_functions(isatty(0) ? fgets_from_stdin : fread_from_stdin);
  32. init_readline();
  33. set_dispatch_functions();
  34. int i = 1;
  35. int lisp_argv_start = argc;
  36. char *socket_name = NULL;
  37. while (i < argc) {
  38. if (strcmp(argv[i], "--") == 0) {
  39. lisp_argv_start = i + 1;
  40. break;
  41. } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
  42. print_usage();
  43. return EXIT_SUCCESS;
  44. } else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--socket") == 0) {
  45. if (i + 1 == argc || strcmp(argv[i+1], "--") == 0) {
  46. fprintf(stderr, "Socket name missing after %s.", argv[i]);
  47. print_usage();
  48. return EXIT_FAILURE;
  49. }
  50. socket_name = argv[i + 1];
  51. i += 2;
  52. } else {
  53. fprintf(stderr, "Unknown option: %s.", argv[i]);
  54. print_usage();
  55. return EXIT_FAILURE;
  56. }
  57. }
  58. if (!socket_name) socket_name = getenv("LSERVER_SOCKET");
  59. if (!socket_name) {
  60. fprintf(stderr, "Socket needed.\n");
  61. return EXIT_FAILURE;
  62. }
  63. struct session s = {0};
  64. s.cont = 1;
  65. s.argv0 = argv[0];
  66. s.lisp_argc = argc - lisp_argv_start;
  67. s.lisp_argv = argv + lisp_argv_start;
  68. struct sockaddr_un servaddr;
  69. s.fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  70. bzero(&servaddr, sizeof(servaddr));
  71. servaddr.sun_family = AF_LOCAL;
  72. strcpy(servaddr.sun_path, socket_name);
  73. connect(s.fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
  74. while (s.cont) {
  75. if (cmd_from_lisp(&s)) return EXIT_FAILURE;
  76. if (s.cmd >= NUM_COMMANDS || s.cmd < 0) {
  77. fprintf(stderr, "Unknown command code %d.\n", (int) s.cmd);
  78. return EXIT_FAILURE;
  79. }
  80. //printf("cmd %d\n", s.cmd);
  81. if ((*dispatch_functions[s.cmd])(&s)) return EXIT_FAILURE;
  82. }
  83. return (int) s.i;
  84. }