summaryrefslogtreecommitdiff
path: root/modules/tclsci/src/c/ScilabEval.c
blob: 56bed0a1e0468ea5507be46d5797fe9b1ab779da (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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2005-2008 - INRIA - Allan CORNET
 * Copyright (C) 2008-2008 - INRIA - Bruno JOFRET
 *
 * 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
 *
 */
#include <string.h>
#include "machine.h"
#include "TCL_Global.h"
#include "ScilabEval.h"
#include "sciprint.h"
#include "sciprint_full.h"
#include "Scierror.h"
#include "localization.h"
#include "syncexec.h"
#include "storeCommand.h"
#ifdef _MSC_VER
#include "strdup_windows.h"
#endif
/*--------------------------------------------------------------------------*/
/* what's the max number of commands in the queue ??*/
#define arbitrary_max_queued_callbacks 20
#define AddCharacters 4
/*--------------------------------------------------------------------------*/
/* Used by tksynchro
 * static int c_n1 = -1;
 */
/*--------------------------------------------------------------------------*/
int TCL_EvalScilabCmd(ClientData clientData, Tcl_Interp * theinterp, int objc, CONST char ** argv)
{
    int ierr = 0, seq = 0;
    char *command;

    char *comm[arbitrary_max_queued_callbacks];
    int   seqf[arbitrary_max_queued_callbacks];
    int nc, ncomm = -1;

    if (C2F(iop).ddt == -1)
    {
        /* trace for debugging */
        int argc = 1;
        char *msg = _("TCL_EvalScilabCmd %s");

        sciprint_full(msg, argv[1]);

        while (argv[++argc])
        {
            sciprint(" %s", argv[argc]);
        }
        sciprint("\n");

    }

    if (argv[1] != (char *)0)
    {
        command = strdup(argv[1]);
        if (command == (char *) 0)
        {
            sciprint(_("%s: No more memory.\n"), "TCL_EvalScilabCmd");
            return TCL_ERROR;
        }


        if ( (argv[2] != (char *)0) && (strncmp(argv[2], "sync", 4) == 0) )
        {
            /* sync or sync seq
             * TODO : Scilab is supposed to be busy there. Add mutex lock...
             * C2F(tksynchro)(&c_n1);
             * set sciprompt to -1 (scilab busy)
             */
            seq = ( (argv[3] != (char *)0) && (strncmp(argv[3], "seq", 3) == 0) );

            if (C2F(iop).ddt == -1)
            {
                char *msg = _("Execution starts for %s");
                sciprint_full(msg, command);
                sciprint("\n");
            }

            /*
            int ns=(int)strlen(command);
            Was : syncexec(command,&ns,&ierr,&seq,ns);
            So far as Tcl has it's own thread now mixing global values
            and threads within parse makes Scilab crash often.
            */
            StorePrioritaryCommandWithFlag(command, seq);
            ierr = 0;

            if (C2F(iop).ddt == -1)
            {
                char *msg = _("Execution ends for %s");
                sciprint_full(msg, command);
                sciprint("\n");
            }
            // TODO : Scilab is supposed to be busy there. Add mutex lock...
            // C2F(tksynchro)(&C2F(recu).paus);
            if (ierr != 0)
            {
                return TCL_ERROR;
            }
        }
        else if (strncmp(command, "flush", 5) == 0)
        {
            /* flush */
            if (C2F(iop).ddt == -1)
            {
                sciprint(_(" Flushing starts for queued commands.\n"));
            }
            while (ismenu() && ncomm < arbitrary_max_queued_callbacks - 1)
            {
                ncomm++;
                comm[ncomm] = (char *) MALLOC (bsiz + 1);
                if (comm[ncomm] == (char *) 0)
                {
                    sciprint(_("%s: No more memory.\n"), "TCL_EvalScilabCmd");
                    FREE(command);
                    return TCL_ERROR;
                }
                seqf[ncomm] = GetCommand (comm[ncomm]);
            }
            if (ismenu())
            {
                sciprint(_("Warning: Too many callbacks in queue!\n"));
            }
            for (nc = 0 ; nc <= ncomm ; nc++ )
            {
                // TODO : Scilab is supposed to be busy there. Add mutex lock...
                // C2F(tksynchro)(&c_n1);  // set sciprompt to -1 (scilab busy)
                if (C2F(iop).ddt == -1)
                {
                    if (seqf[nc] == 0)
                    {
                        char *msg = _("Flushed execution starts for %s - No option");
                        sciprint_full(msg, comm[nc]);
                        sciprint("\n");
                    }
                    else
                    {
                        char *msg = _("Flushed execution starts for %s - seq");
                        sciprint_full(msg, comm[nc]);
                        sciprint("\n");
                    }
                }
                /*
                Was : syncexec(comm[nc],&ns,&ierr,&(seqf[nc]),ns);
                So far as Tcl has it's own thread now mixing global values
                and threads within parse makes Scilab crash often.
                */
                StorePrioritaryCommandWithFlag(comm[nc], seqf[nc]);
                if (C2F(iop).ddt == -1)
                {
                    char *msg = _("Flushed execution ends for %s");
                    sciprint_full(msg, comm[nc]);
                    sciprint("\n");
                }
                FREE(comm[nc]);
                // TODO : Scilab is supposed to be busy there. Add mutex lock...
                // C2F(tksynchro)(&C2F(recu).paus);
                if (ierr != 0)
                {
                    return TCL_ERROR;
                }
            }
            if (C2F(iop).ddt == -1)
            {
                sciprint(_("Flushing ends\n"));
            }
        }
        else
        {
            if ( (argv[2] != (char *)0) && (strncmp(argv[2], "seq", 3) == 0) )
            {
                /* seq */
                StoreCommandWithFlag(command, 1);
            }
            else
            {
                /* no option or unknown option (TODO: no error for this latter case?) */
                StoreCommand(command);
                Tcl_SetResult(theinterp, NULL, NULL);
            }
        }
        FREE(command);

    }
    else
    {
        /* ScilabEval called without argument */
        Scierror(999, _("%s: Wrong number of input argument(s): at least one expected.\n"), "TCL_EvalScilabCmd");
    }

    return TCL_OK;
}