home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / execute-unix-command.el < prev    next >
Encoding:
Text File  |  1991-06-19  |  3.7 KB  |  99 lines

  1. ; Path: dg-rtp!uunet!uunet!cis.ohio-state.edu!tut.cis.ohio-state.edu!unreplyable!garbage
  2. ; From: darrylo@HPNMXX.SR.HP.COM (Darryl Okahata)
  3. ; Newsgroups: gnu.emacs.help,comp.emacs
  4. ; Subject: Re: process exit status
  5. ; Date: 4 Jun 91 17:34:10 GMT
  6. ; References: <WALT.91Jun4105619@arrisun3.utarl.edu>
  7. ; Organization: Gatewayed from the GNU Project mailing list help-gnu-emacs@prep.ai.mit.edu
  8. ; In message <WALT.91Jun4105619@arrisun3.utarl.edu> you write:
  9. ; > Is there a way to obtain the exit status besides resorting to
  10. ; > 'start-process' and using a process-sentinal?  The function I'm
  11. ; > writing shouldn't require such hair, but I *must* have the exit
  12. ; > status.
  13. ;      I wrote the following function eons ago.  It will execute a unix
  14. ; command, wait for it to finish, and return the exit status.  Note,
  15. ; however, that this is not the world's best written function; if I had to
  16. ; re-write it, I'd do it slightly differently.  Also note that the
  17. ; function documentation says that a SIGINT will be sent if ^G is pressed.
  18. ; This will only work on those machines that do NOT support SIGIO.  I wrote
  19. ; this function when HP-UX did not support SIGIO; it does now, and
  20. ; pressing ^G to interrupt the subprocess no longer works.  Pressing ^G
  21. ; will interrupt Emacs, but the subprocess will continue to grind away in
  22. ; the background (this can be easily fixed using `unwind-protect', but I
  23. ; don't have the time to modify and test it).
  24. ;      -- Darryl Okahata
  25. ;     Internet: darrylo@sr.hp.com
  26. ; DISCLAIMER: this message is the author's personal opinion and does not
  27. ; constitute the support, opinion or policy of Hewlett-Packard or of the
  28. ; little green men that have been following him all day.
  29. ; ============================================================================
  30. ;; LCD Archive Entry:
  31. ;; execute-unix-command|Darryl Okahata|darrylo@sr.hp.com
  32. ;; |Execute a command, wait for finish, and return exit status
  33. ;; |91-06-04||~/functions/execute-unix-command.el.Z
  34.  
  35. (defun execute-unix-command (buffer program &rest args)
  36.   "Execute a unix command, wait for it for finish, and return the exit status.
  37. This function creates a process, using BUFFER for output, and waits
  38. for the process to exit.  The name of the program to run is given by
  39. PROGRAM, and any remaining arguments, ARGS, are passed as command line
  40. arguments to the program.  All arguments are strings.
  41.  
  42. Note that there are no provisions for giving input to the program.  If
  43. you must give input to the program, create a file containing the input
  44. for the process and pass the name of this file to a shell script that
  45. starts up the program with standard input redirected to the given
  46. file.
  47.  
  48. If BUFFER is nil, any output from the program will be throw away.
  49.  
  50. If a ^G is pressed while the program is running, the program will be
  51. killed via a SIGINT, and this function will return nil.  All other
  52. keypresses will be flushed.
  53.  
  54. Upon normal completion of the program, the numeric exit status of the
  55. program will be returned.  Note that this function returns either nil
  56. or a numeric value.
  57. "
  58.   (let (prg (process nil) status (return-status nil))
  59.     (catch 'exit-process
  60.       (progn
  61.     (setq prg (list 'start-process "unix-process" buffer program))
  62.     (if args
  63.       (if (listp args)
  64.         (setq prg (append prg args))
  65.         (setq prg (append prg (list args)))
  66.       )
  67.     )
  68.     (setq process (eval prg))
  69.     (if (processp process)
  70.       (progn
  71.         (process-send-eof process)
  72.         (while (or (equal (setq status (process-status process))
  73.                   'run)
  74.                (equal status 'stop))
  75.           (sit-for 1)
  76.           (if (input-pending-p)
  77.         (if (equal (read-char) ?\^G)
  78.           (progn
  79.             (interrupt-process process t)
  80.             (throw 'exit-process nil)
  81.           )
  82.         )
  83.           )
  84.         )
  85.         (setq return-status (process-exit-status process))
  86.       )
  87.     )
  88.       )
  89.     )
  90.     return-status
  91.   )
  92. )
  93.