summaryrefslogtreecommitdiff
path: root/modules/io/macros/unix_w.sci
blob: 60116a4d8ffc419fd0c932baf314d308632de80c (plain)
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
// 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 unix_w(cmd)
    //unix_w - shell command execution results redirected in main scilab window
    //%Syntax
    // unix_w(cmd)
    //%Parameters
    // cmd - a character string
    //%Description
    // cmd instruction (sh syntax) is passed to shell, the standard output
    // is redirected  to main scilab window
    //%Examples
    // unix_w("ls")
    //%See also
    // host unix_x unix_s unix_g
    //!

    [lhs,rhs] = argn(0);

    if rhs <> 1 then
        error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected.\n"),"unix_w",1));
    end

    if type(cmd) <> 10 then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: String expected.\n"),"unix_w",1));
    end

    if size(cmd,"*") <> 1 then
        error(msprintf(gettext("%s: Wrong size for input argument #%d: A string expected.\n"),"unix_w",1));
    end

    if getos() == "Windows" then
        [rep,stat]=dos(cmd,"-echo");
        if (~stat) then
            error(msprintf(gettext("%s: error during ""%s"" execution"),"unix_w",cmd));
        end
    else
        tmp=TMPDIR+"/unix.out";
        cmd1="("+cmd+")>"+ tmp +" 2>"+TMPDIR+"/unix.err;";
        stat=host(cmd1);

        select stat
        case 0 then
            write(%io(2),read(tmp,-1,1,"(a)"));
        case -1 then // host failed
            error(msprintf(gettext("%s: The system interpreter does not answer..."),"unix_w"));
        else
            msg=read(TMPDIR+"/unix.err",-1,1,"(a)");
            if (size(msg,"*") == 0) then // If the program does not return anything
                msg=""
            end
            errmsg = msprintf(gettext("%s: The command failed with the error code ""%s"" and the following message:\n"),"unix_w",string(stat));
            error(msprintf("%s\n"+strcat(msg, "\n"), errmsg));
        end
        deletefile(tmp);
    end
endfunction