simpos.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2003, 2004, 2009,
  2. * 2010, 2012, 2013, 2014 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <errno.h>
  23. #include <signal.h> /* for SIG constants */
  24. #include <stdlib.h> /* for getenv */
  25. #include <stdio.h>
  26. #include "libguile/_scm.h"
  27. #include "libguile/scmsigs.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/simpos.h"
  31. #include "libguile/dynwind.h"
  32. #ifdef HAVE_STRING_H
  33. #include <string.h>
  34. #endif
  35. #include <unistd.h>
  36. #if HAVE_SYS_WAIT_H
  37. # include <sys/wait.h>
  38. #endif
  39. #ifdef __MINGW32__
  40. # include <process.h> /* for spawnvp and friends */
  41. #endif
  42. #include "posix.h"
  43. extern int system();
  44. #ifdef HAVE_SYSTEM
  45. SCM_DEFINE (scm_system, "system", 0, 1, 0,
  46. (SCM cmd),
  47. "Execute @var{cmd} using the operating system's \"command\n"
  48. "processor\". Under Unix this is usually the default shell\n"
  49. "@code{sh}. The value returned is @var{cmd}'s exit status as\n"
  50. "returned by @code{waitpid}, which can be interpreted using\n"
  51. "@code{status:exit-val} and friends.\n"
  52. "\n"
  53. "If @code{system} is called without arguments, return a boolean\n"
  54. "indicating whether the command processor is available.")
  55. #define FUNC_NAME s_scm_system
  56. {
  57. int rv, eno;
  58. char *c_cmd;
  59. if (SCM_UNBNDP (cmd))
  60. {
  61. rv = system (NULL);
  62. return scm_from_bool(rv);
  63. }
  64. SCM_VALIDATE_STRING (1, cmd);
  65. errno = 0;
  66. c_cmd = scm_to_locale_string (cmd);
  67. rv = system (c_cmd);
  68. eno = errno; free (c_cmd); errno = eno;
  69. if (rv == -1 || (rv == 127 && errno != 0))
  70. SCM_SYSERROR;
  71. return scm_from_int (rv);
  72. }
  73. #undef FUNC_NAME
  74. #endif /* HAVE_SYSTEM */
  75. #ifdef HAVE_SYSTEM
  76. SCM_DEFINE (scm_system_star, "system*", 0, 0, 1,
  77. (SCM args),
  78. "Execute the command indicated by @var{args}. The first element must\n"
  79. "be a string indicating the command to be executed, and the remaining\n"
  80. "items must be strings representing each of the arguments to that\n"
  81. "command.\n"
  82. "\n"
  83. "This function returns the exit status of the command as provided by\n"
  84. "@code{waitpid}. This value can be handled with @code{status:exit-val}\n"
  85. "and the related functions.\n"
  86. "\n"
  87. "@code{system*} is similar to @code{system}, but accepts only one\n"
  88. "string per-argument, and performs no shell interpretation. The\n"
  89. "command is executed using fork and execlp. Accordingly this function\n"
  90. "may be safer than @code{system} in situations where shell\n"
  91. "interpretation is not required.\n"
  92. "\n"
  93. "Example: (system* \"echo\" \"foo\" \"bar\")")
  94. #define FUNC_NAME s_scm_system_star
  95. {
  96. if (scm_is_null (args))
  97. SCM_WRONG_NUM_ARGS ();
  98. if (scm_is_pair (args))
  99. {
  100. SCM oldint;
  101. SCM sig_ign;
  102. SCM sigint;
  103. /* SIGQUIT is undefined on MS-Windows. */
  104. #ifdef SIGQUIT
  105. SCM oldquit;
  106. SCM sigquit;
  107. #endif
  108. #ifdef HAVE_FORK
  109. int pid;
  110. #else
  111. int status;
  112. #endif
  113. char **execargv;
  114. /* allocate before fork */
  115. execargv = scm_i_allocate_string_pointers (args);
  116. /* make sure the child can't kill us (as per normal system call) */
  117. sig_ign = scm_from_ulong ((unsigned long) SIG_IGN);
  118. sigint = scm_from_int (SIGINT);
  119. oldint = scm_sigaction (sigint, sig_ign, SCM_UNDEFINED);
  120. #ifdef SIGQUIT
  121. sigquit = scm_from_int (SIGQUIT);
  122. oldquit = scm_sigaction (sigquit, sig_ign, SCM_UNDEFINED);
  123. #endif
  124. #ifdef HAVE_FORK
  125. pid = fork ();
  126. if (pid == 0)
  127. {
  128. /* child */
  129. execvp (execargv[0], execargv);
  130. /* Something went wrong. */
  131. fprintf (stderr, "In execvp of %s: %s\n",
  132. execargv[0], strerror (errno));
  133. /* Exit directly instead of throwing, because otherwise this
  134. process may keep on running. Use exit status 127, like
  135. shells in this case, as per POSIX
  136. <http://pubs.opengroup.org/onlinepubs/007904875/utilities/xcu_chap02.html#tag_02_09_01_01>. */
  137. _exit (127);
  138. }
  139. else
  140. {
  141. /* parent */
  142. int wait_result, status;
  143. if (pid == -1)
  144. SCM_SYSERROR;
  145. SCM_SYSCALL (wait_result = waitpid (pid, &status, 0));
  146. if (wait_result == -1)
  147. SCM_SYSERROR;
  148. scm_sigaction (sigint, SCM_CAR (oldint), SCM_CDR (oldint));
  149. scm_sigaction (sigquit, SCM_CAR (oldquit), SCM_CDR (oldquit));
  150. return scm_from_int (status);
  151. }
  152. #else /* !HAVE_FORK */
  153. status = spawnvp (P_WAIT, execargv[0], (const char * const *)execargv);
  154. scm_sigaction (sigint, SCM_CAR (oldint), SCM_CDR (oldint));
  155. #ifdef SIGQUIT
  156. scm_sigaction (sigquit, SCM_CAR (oldquit), SCM_CDR (oldquit));
  157. #endif
  158. return scm_from_int (status);
  159. #endif /* !HAVE_FORK */
  160. }
  161. else
  162. SCM_WRONG_TYPE_ARG (1, args);
  163. }
  164. #undef FUNC_NAME
  165. #endif /* HAVE_SYSTEM */
  166. SCM_DEFINE (scm_getenv, "getenv", 1, 0, 0,
  167. (SCM nam),
  168. "Looks up the string @var{nam} in the current environment. The return\n"
  169. "value is @code{#f} unless a string of the form @code{NAME=VALUE} is\n"
  170. "found, in which case the string @code{VALUE} is returned.")
  171. #define FUNC_NAME s_scm_getenv
  172. {
  173. char *val;
  174. char *var = scm_to_locale_string (nam);
  175. val = getenv (var);
  176. free (var);
  177. return val ? scm_from_locale_string (val) : SCM_BOOL_F;
  178. }
  179. #undef FUNC_NAME
  180. /* Get an integer from an environment variable. */
  181. int
  182. scm_getenv_int (const char *var, int def)
  183. {
  184. char *end = 0;
  185. char *val = getenv (var);
  186. long res = def;
  187. if (!val)
  188. return def;
  189. res = strtol (val, &end, 10);
  190. if (end == val)
  191. return def;
  192. return res;
  193. }
  194. /* simple exit, without unwinding the scheme stack or flushing ports. */
  195. SCM_DEFINE (scm_primitive_exit, "primitive-exit", 0, 1, 0,
  196. (SCM status),
  197. "Terminate the current process without unwinding the Scheme\n"
  198. "stack. The exit status is @var{status} if supplied, otherwise\n"
  199. "zero.")
  200. #define FUNC_NAME s_scm_primitive_exit
  201. {
  202. int cstatus = 0;
  203. if (!SCM_UNBNDP (status))
  204. cstatus = scm_to_int (status);
  205. exit (cstatus);
  206. }
  207. #undef FUNC_NAME
  208. SCM_DEFINE (scm_primitive__exit, "primitive-_exit", 0, 1, 0,
  209. (SCM status),
  210. "Terminate the current process using the _exit() system call and\n"
  211. "without unwinding the Scheme stack. The exit status is\n"
  212. "@var{status} if supplied, otherwise zero.\n"
  213. "\n"
  214. "This function is typically useful after a fork, to ensure no\n"
  215. "Scheme cleanups or @code{atexit} handlers are run (those\n"
  216. "usually belonging in the parent rather than the child).")
  217. #define FUNC_NAME s_scm_primitive__exit
  218. {
  219. int cstatus = 0;
  220. if (!SCM_UNBNDP (status))
  221. cstatus = scm_to_int (status);
  222. _exit (cstatus);
  223. }
  224. #undef FUNC_NAME
  225. void
  226. scm_init_simpos ()
  227. {
  228. #include "libguile/simpos.x"
  229. }
  230. /*
  231. Local Variables:
  232. c-file-style: "gnu"
  233. End:
  234. */