run-lisp-trans.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Run a clisp process with stdin and stdout open.
  2. Copyright (C) 2008 Free Software Foundation, Inc.
  3. Written by FlÃvio Cruz <flaviocruz@gmail.com>
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <paths.h>
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <error.h>
  24. /* lisp interpreter absolute location */
  25. static char *LISP_PATH = "/usr/bin/clisp";
  26. static int
  27. fd_is_open (const int fd)
  28. {
  29. static struct stat buf;
  30. return (fstat (fd, &buf) == 0);
  31. }
  32. static void
  33. run_lisp (int argc, char **argv)
  34. {
  35. int stdin_handle, stdout_handle;
  36. char *new_argv[argc + 1];
  37. int i;
  38. if (!fd_is_open (STDIN_FILENO))
  39. {
  40. stdin_handle = open (_PATH_DEVNULL, O_RDONLY);
  41. if (stdin_handle == -1)
  42. {
  43. error (EXIT_FAILURE, errno, "Could not open file %s",
  44. _PATH_DEVNULL);
  45. }
  46. }
  47. if (!fd_is_open (STDOUT_FILENO))
  48. {
  49. stdout_handle = open (_PATH_DEVNULL, O_WRONLY);
  50. if (stdout_handle == -1)
  51. {
  52. error (EXIT_FAILURE, errno, "Could not open file %s",
  53. _PATH_DEVNULL);
  54. }
  55. }
  56. new_argv[0] = LISP_PATH;
  57. for (i = 1; i <= argc; ++i)
  58. {
  59. new_argv[i] = argv[i];
  60. }
  61. execvp (new_argv[0], new_argv);
  62. /* we should not get here ... */
  63. error (EXIT_FAILURE, errno, "Could not launch lisp %s", LISP_PATH);
  64. }
  65. int
  66. main (int argc, char **argv)
  67. {
  68. run_lisp (argc, argv);
  69. return EXIT_SUCCESS;
  70. }