1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) XXXX-2008 - INRIA
// Copyright (C) XXXX-2008 - INRIA - Allan CORNET
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution. The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
function varargout = unix_g(cmd)
//unix_g - shell command execution
//%Syntax
//rep=unix_g(cmd)
//%Parameters
// cmd - a character string
// rep - a column vector of character strings
//%Description
// cmd instruction (sh syntax) is passed to shell, the standard output
// is redirected to scilab variable rep.
//%Examples
// unix_g("ls")
//%See also
// host unix_x unix_s
//!
[lhs,rhs] = argn(0);
if rhs <> 1 then
error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected.\n"),"unix_g",1));
end
if type(cmd) <> 10 then
error(msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"),"unix_g",1));
end
if size(cmd,"*") <> 1 then
error(msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"),"unix_g",1));
end
if lhs > 3 then
error(msprintf(gettext("%s: Wrong number of output argument(s).\n"),"unix_g"));
end
// initialize variables
stderr = emptystr();
stat = 1;
rep = emptystr();
if getos() == "Windows" then
[rep,stat] = dos(cmd);
if (stat == %t) then
stat = 0;
else
if lhs == 3 then
stderr = rep;
else
for i=1:size(rep,"*") do write(%io(2)," "+rep(i));end
end
stat = 1;
rep = emptystr();
end
else
tmp = TMPDIR+"/unix.out";
cmd1 = "("+cmd+")>"+ tmp +" 2>"+TMPDIR+"/unix.err;";
stat = host(cmd1);
select stat
case 0 then
rep = mgetl(tmp);
if (size(rep,"*")==0) | (length(rep)==0) then
rep = [];
end;
case 1 then
rep = mgetl(tmp);
case -1 then
// host failed
if lhs == 3 then
stderr = msprintf(gettext("%s: The system interpreter does not answer..."),"unix_g");
else
disp(msprintf(gettext("%s: The system interpreter does not answer..."),"unix_g"));
end
rep = emptystr();
else
msg = mgetl(TMPDIR+"/unix.err");
if lhs == 3 then
stderr = msg;
else
disp(msg(1));
end
rep = emptystr();
end
mdelete(tmp);
end
// output arguments
varargout(1) = rep;
if lhs >= 2 then
varargout(2) = stat;
end
if lhs >= 3 then
varargout(3) = stderr;
end
endfunction
|