home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / misc / popen2.m < prev    next >
Text File  |  1999-04-29  |  3KB  |  122 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: [IN, OUT, PID] = popen2 (COMMAND, ARGS)
  21. ##
  22. ## Start a subprocess with two-way communication.  COMMAND specifies
  23. ## the name of the command to start.  ARGS is an array of strings
  24. ## containing options for COMMAND.  IN and OUT are the file ids of the
  25. ## input and output streams for the subprocess, and PID is the process id of
  26. ## the subprocess, or -1 if COMMAND could not be executed.
  27. ##
  28. ## Example:
  29. ##
  30. ##  [in, out, pid] = popen2 ("sort", "-nr");
  31. ##  fputs (in, "these\n");
  32. ##  fputs (in, "are\n");
  33. ##  fputs (in, "some\n");
  34. ##  fputs (in, "strings\n");
  35. ##  fclose (in);
  36. ##  while (isstr (s = fgets (out)))
  37. ##    fputs (stdout, s);
  38. ##  endwhile
  39. ##  fclose (out);
  40.  
  41. ## Author: Klaus Gebhardt, 1997
  42.  
  43. function [in, out, pid] = popen2 (command, args)
  44.  
  45.   in = -1;
  46.   out = -1;
  47.   pid = -1;
  48.  
  49.   if (nargin == 1 || nargin == 2)
  50.  
  51.     if (isstr (command))
  52.  
  53.       [r_pipe,  r_status]  = _pipe ();
  54.       [w_pipe,  w_status]  = _pipe ();
  55.  
  56.       if ((r_status != -1) && (w_status != -1))
  57.  
  58.     r_parent_end = r_pipe(1);
  59.     r_child_end  = r_pipe(2);
  60.     _fcntl (r_parent_end, F_SETFD, 1);
  61.     r_org = _dup (1);
  62.  
  63.     w_parent_end = w_pipe(2);
  64.     w_child_end  = w_pipe(1);
  65.     _fcntl (w_parent_end, F_SETFD, 1);
  66.     w_org = _dup (0);
  67.  
  68.     if ((r_org != -1) && (w_org != -1))
  69.  
  70.       _dup2 (r_child_end, 1);  _close (r_child_end);  r_child_end = -1;
  71.       _dup2 (w_child_end, 0);  _close (w_child_end);  w_child_end = -1;
  72.  
  73.       if (nargin == 1)
  74.         pid = spawn ("nowait", command);
  75.       else
  76.         pid = spawn ("nowait", command, args);
  77.       endif
  78.  
  79.       _dup2 (r_org, 1);  _close (r_org);
  80.       _dup2 (w_org, 0);  _close (w_org);
  81.     endif
  82.  
  83.     if (r_child_end != -1)
  84.       _close (r_child_end);
  85.       endif
  86.  
  87.     if (w_child_end != -1)
  88.       _close (w_child_end);
  89.     endif
  90.  
  91.     if (pid < 0)
  92.       _close (r_parent_end);
  93.       _close (w_parent_end);
  94.       error ("popen2: unable to start process `%s'", command);
  95.     else
  96.       out = _fdopen (r_parent_end, "r");
  97.       in  = _fdopen (w_parent_end, "w");
  98.     endif
  99.  
  100.       else
  101.  
  102.     if (r_status != -1)
  103.       _close (r_pipe(1));
  104.       _close (r_pipe(2));
  105.       endif
  106.  
  107.     if (wr_status != -1)
  108.       _close (w_pipe(1));
  109.       _close (w_pipe(2));
  110.     endif
  111.  
  112.     error ("popen2: pipe creation failed");
  113.       endif
  114.     else
  115.       error ("popen2: file name must be a string");
  116.     endif
  117.   else
  118.     usage ("[in, out, pid] = popen2 (command, args)");
  119.   endif
  120.  
  121. endfunction
  122.