annotate scripts/miscellaneous/popen2.m @ 2313:5ca126254d15

[project @ 1996-07-11 21:25:22 by jwe]
author jwe
date Thu, 11 Jul 1996 21:25:22 +0000
parents 2b5788792cad
children 949ab8eba8bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
1 ## Copyright (C) 1996 John W. Eaton
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## under the terms of the GNU General Public License as published by
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ## any later version.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## You should have received a copy of the GNU General Public License
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
18 ## 02111-1307, USA.
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
19
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
20 ## usage: [IN, OUT, PID] = popen2 (COMMAND, ARGS)
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
21 ##
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
22 ## Start a subprocess with two-way communication. COMMAND specifies
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
23 ## the name of the command to start. ARGS is an array of strings
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
24 ## containing options for COMMAND. IN and out are the file ids of the
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
25 ## input and streams for the subprocess, and PID is the process id of
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
26 ## the subprocess, or -1 if COMMAND could not be executed.
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
27 ##
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
28 ## Example:
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
29 ##
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
30 ## [in, out, pid] = popen2 ("sort", "-nr");
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
31 ## fputs (in, "these\n");
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
32 ## fputs (in, "are\n");
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
33 ## fputs (in, "some\n");
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
34 ## fputs (in, "strings\n");
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
35 ## fclose (in);
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
36 ## while (isstr (s = fgets (out)))
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
37 ## fputs (stdout, s);
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
38 ## endwhile
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
39 ## fclose (out);
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
40
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
41 function [in, out, pid] = popen2 (command, args)
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
42
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
43 in = -1;
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
44 out = -1;
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
45 pid = -1;
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
46
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
47 if (nargin == 1 || nargin == 2)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
48
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
49 if (nargin == 1)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
50 args = "";
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
51 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
52
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
53 if (isstr (command))
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
54
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
55 [stdin_pipe, stdin_status] = pipe ();
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
56 [stdout_pipe, stdout_status] = pipe ();
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
57
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
58 if (stdin_status == 0 && stdout_status == 0)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
59
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
60 pid = fork ();
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
61
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
62 if (pid == 0)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
63
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
64 fclose (stdin_pipe (2));
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
65 fclose (stdout_pipe (1));
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
66
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
67 dup2 (stdin_pipe (1), stdin);
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
68 fclose (stdin_pipe (1));
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
69
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
70 dup2 (stdout_pipe (2), stdout);
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
71 fclose (stdout_pipe (2));
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
72
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
73 if (exec (command, args) < 0)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
74 error ("popen2: unable to start process `%s'", command);
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
75 exit (0);
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
76 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
77
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
78 elseif (pid)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
79
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
80 fclose (stdin_pipe (1));
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
81 fclose (stdout_pipe (2));
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
82
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
83 if (fcntl (stdout_pipe (1), __F_SETFL__, __O_NONBLOCK__) < 0)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
84 error ("popen2: error setting file mode");
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
85 else
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
86 in = stdin_pipe (2);
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
87 out = stdout_pipe (1);
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
88 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
89
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
90 elseif (pid < 0)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
91 error ("popen2: fork failed -- unable to create child process");
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
92 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
93 else
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
94 error ("popen2: pipe creation failed");
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
95 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
96 else
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
97 error ("popen2: file name must be a string");
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
98 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
99 else
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
100 usage ("[in, out, pid] = popen2 (command, args)");
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
101 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
102
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
103 endfunction