2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Allan CORNET
4 * Copyright (C) 2011 - DIGITEO - Allan CORNET
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
20 #include "localization.h"
23 #include "call_scilab.h"
24 #include "isScilabFunction.h"
25 #include "api_scilab.h"
27 /*--------------------------------------------------------------------------*/
28 static int clearAllVariablesOnStack(void);
29 /*--------------------------------------------------------------------------*/
30 static int sci_clear_no_rhs(const char *fname);
31 static int sci_clear_n_rhs(const char *fname);
32 /*--------------------------------------------------------------------------*/
33 extern int C2F(stackp)(int *, int *); /* fortran subroutine */
34 /*--------------------------------------------------------------------------*/
35 int C2F(sci_clear)(char *fname,unsigned long fname_len)
39 sci_clear_no_rhs(fname);
43 sci_clear_n_rhs(fname);
47 /*--------------------------------------------------------------------------*/
48 static int sci_clear_no_rhs(const char *fname)
50 if (C2F(recu).macr != 0 || C2F(recu).paus != 0)
53 if (C2F(recu).rstk[C2F(recu).pt - 3] == 909)
55 /* clear within an exec'd macro */
56 return clearAllVariablesOnStack();
58 /* clear within a macro, an execstr, an exec or a pause */
59 k = C2F(iop).lpt[0] - 19;
60 if (C2F(iop).lin[k + 6] != 0 && ((int *)&C2F(stack))[C2F(iop).lin[k + 5] - 1] == 10)
65 /* clear within a macro, an exec or a pause */
67 Bot = Min(C2F(vstk).bbot, C2F(iop).lin[k + 4]);
71 return clearAllVariablesOnStack();
73 /*--------------------------------------------------------------------------*/
74 static int sci_clear_n_rhs(const char *fname)
78 int nbVariables = Rhs;
79 char **VariableNameToClear = (char **)MALLOC(sizeof(char*) * nbVariables);
81 if (!VariableNameToClear)
83 Scierror(999,_("%s: No more memory.\n"), fname);
87 for (k = 0; k < nbVariables; k++)
89 int *piAddressVar = NULL;
90 SciErr sciErr = getVarAddressFromPosition(pvApiCtx, k + 1, &piAddressVar);
93 if (isScalar(pvApiCtx, piAddressVar) && isStringType(pvApiCtx, piAddressVar))
95 char *variablename = NULL;
96 if (getAllocatedSingleString(pvApiCtx, piAddressVar, &variablename) == 0)
98 VariableNameToClear[i] = variablename;
103 freeAllocatedMatrixOfString(i, 1, VariableNameToClear);
104 VariableNameToClear = NULL;
105 Scierror(999,_("%s: No more memory.\n"), fname);
111 freeAllocatedMatrixOfString(i, 1, VariableNameToClear);
112 VariableNameToClear = NULL;
114 if (isScalar(pvApiCtx, piAddressVar))
116 Scierror(201, _("%s: Wrong type for argument %d: Valid variable name expected.\n"), fname, k + 1);
121 Scierror(999, _("%s: Wrong size for argument %d: Valid variable name expected.\n"), fname, k + 1);
128 for (k = 0; k < nbVariables; k++)
130 deleteNamedVariable(NULL, VariableNameToClear[k]);
133 if (VariableNameToClear)
135 freeAllocatedMatrixOfString(nbVariables, 1, VariableNameToClear);
136 VariableNameToClear = NULL;
141 /*--------------------------------------------------------------------------*/
142 int clearAllVariablesOnStack(void)
144 /* clear all variables */
148 Bot = C2F(vstk).bbot;
150 //create a null matrix a the Top of the stack
152 il = iadr(*Lstk(Top));
154 *Lstk(Top + 1) = *Lstk(Top) + 1;