<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) XXXX-2008 - INRIA
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:lang="en" xml:id="execstr">
    <refnamediv>
        <refname>execstr</refname>
        <refpurpose>execute Scilab code in strings</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            execstr(instr)
            ierr = execstr(instr,'errcatch' [,msg])
            [ierr, errormsg] = execstr(instr,'errcatch' [,msg])
            [ierr, errormsg, callstack] = execstr(instr,'errcatch' [,msg])
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>instr</term>
                <listitem>
                    <para>a vector of character strings, Scilab instruction to be executed.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>ierr</term>
                <listitem>
                    <para>an integer, 0 or error number.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>msg</term>
                <listitem>
                    <para>
                        a character string with values <literal>'m'</literal> or <literal>'n'</literal>.  Default value is <literal>'n'</literal>.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>errormsg</term>
                <listitem>
                    <para>error message in case of error and with <literal>'errcatch'</literal> flag.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>callstack</term>
                <listitem>
                    <para>
                        callstack in case of error and with <literal>'errcatch'</literal> flag.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>Executes the Scilab instructions given in argument
            <varname>instr</varname>.
        </para>
        <note>
            Note that <varname>instr</varname> should not make use of continuation marks (..)
        </note>
        <para>
            If the <literal>'errcatch'</literal> flag is not present, error handling works as usual.
        </para>
        <para>
            If the <literal>'errcatch'</literal> flag is set, and an error is encountered while
            executing the instructions defined in <varname>instr</varname>, <function>execstr</function>
            issues no error message, but aborts execution of the <varname>instr</varname>
            instructions (at the point where the error occurred), and resumes with
            <varname>ierr</varname> equal to the error number.  In this case the display of the
            error message is controlled by the <varname>msg</varname> option:
        </para>
        <variablelist>
            <varlistentry>
                <term>"m"</term>
                <listitem>
                    <para>error message is displayed and recorded.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>"n"</term>
                <listitem>
                    <para>
                        no error message is displayed, but the error message is recorded (see <link linkend="lasterror">lasterror</link>).  This is the default.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
        <para>
            <code>ierr = execstr(instr, 'errcatch')</code> can handle syntactical errors. This is
            useful for evaluation of instruction obtained by a query to the user.
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
execstr('a=1') // sets a=1.
execstr('1+1') // does nothing (while evstr('1+1') returns 2)

execstr(['if %t then';
         '  a=1';
         '  b=a+1';
         'else'
         ' b=0'
         'end'])

execstr('a=zzzzzzz','errcatch')
execstr('a=zzzzzzz','errcatch','m')

//syntax errors
execstr('a=1?02','errcatch')
lasterror(%t)

execstr('a=[1 2 3)','errcatch')
lasterror(%t)

// variable1 does not exist
if execstr('variable1;','errcatch')<>0 then
   disp("Trigger an error");
else
   disp("execstr is happy");
end

// variable2 exists ... no error is triggered by execstr
variable2=[2,3];
if execstr('variable2;','errcatch')<>0 then
   disp("Trigger an error");
else
   disp("execstr is happy");
end

//get errormsg if case of error
[_ b] = execstr("acosd(""a"");", "errcatch")
 ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="evstr">evstr</link>
            </member>
            <member>
                <link linkend="lasterror">lasterror</link>
            </member>
            <member>
                <link linkend="error">error</link>
            </member>
            <member>
                <link linkend="try">try</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
