1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) DIGITEO - 2009 - Allan CORNET
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 /*--------------------------------------------------------------------------*/
16 #include "api_scilab.h"
17 #include "completeLine.h"
18 #include "localization.h"
19 #include "Scierror.h"
20 #include "BOOL.h"
21 #include "sci_malloc.h"
22 /*--------------------------------------------------------------------------*/
sci_completeline(char * fname,void * pvApiCtx)23 int sci_completeline(char *fname, void *pvApiCtx)
24 {
25     SciErr sciErr;
26     int* piAddr1 = NULL;
27     int* piAddr2 = NULL;
28     int* piAddr3 = NULL;
29     int* piAddr4 = NULL;
30     int* piAddr5 = NULL;
31     int* piAddr6 = NULL;
32 
33     char *currentline       = NULL;
34     char *stringToAdd       = NULL;
35     char *filePattern       = NULL;
36     char *defaultPattern    = NULL;
37     int stringToAddIsPath   = FALSE;
38     char *postCaretLine     = NULL;
39     char *result            = NULL;
40 
41     CheckInputArgument(pvApiCtx, 5, 6);
42     CheckOutputArgument(pvApiCtx, 0, 1);
43 
44     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
45     if (sciErr.iErr)
46     {
47         printError(&sciErr, 0);
48         return 1;
49     }
50 
51     if (getAllocatedSingleString(pvApiCtx, piAddr1, &currentline))
52     {
53         Scierror(999, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 1);
54         return 1;
55     }
56 
57     sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
58     if (sciErr.iErr)
59     {
60         printError(&sciErr, 0);
61         return 1;
62     }
63 
64     if (getAllocatedSingleString(pvApiCtx, piAddr2, &stringToAdd))
65     {
66         Scierror(999, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 2);
67         return 1;
68     }
69 
70     sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr3);
71     if (sciErr.iErr)
72     {
73         printError(&sciErr, 0);
74         return 1;
75     }
76 
77     if (getAllocatedSingleString(pvApiCtx, piAddr3, &filePattern))
78     {
79         Scierror(999, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 3);
80         return 1;
81     }
82 
83     sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddr4);
84     if (sciErr.iErr)
85     {
86         printError(&sciErr, 0);
87         return 1;
88     }
89 
90     if (getAllocatedSingleString(pvApiCtx, piAddr4, &defaultPattern))
91     {
92         Scierror(999, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 4);
93         return 1;
94     }
95 
96     sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddr5);
97     if (sciErr.iErr)
98     {
99         printError(&sciErr, 0);
100         return 1;
101     }
102 
103     if (getScalarBoolean(pvApiCtx, piAddr5, &stringToAddIsPath))
104     {
105         Scierror(999, _("%s: Wrong type for argument #%d: A boolean expected.\n"), fname, 5);
106         return 1;
107     }
108 
109     if (*getNbInputArgument(pvApiCtx) == 6)
110     {
111         sciErr = getVarAddressFromPosition(pvApiCtx, 6, &piAddr6);
112         if (sciErr.iErr)
113         {
114             printError(&sciErr, 0);
115             return 1;
116         }
117 
118         if (getAllocatedSingleString(pvApiCtx, piAddr6, &postCaretLine))
119         {
120             Scierror(999, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 6);
121             return 1;
122         }
123     }
124     else
125     {
126         postCaretLine = (char*)MALLOC(sizeof(char));
127         postCaretLine[0] = '\0';
128     }
129 
130     result = completeLine(currentline, stringToAdd, filePattern, defaultPattern, (BOOL)stringToAddIsPath, postCaretLine);
131 
132     if (result == NULL)
133     {
134         createSingleString(pvApiCtx, *getNbInputArgument(pvApiCtx) + 1, "");
135     }
136     else
137     {
138         createSingleString(pvApiCtx, *getNbInputArgument(pvApiCtx) + 1, result);
139         FREE(result);
140         result = NULL;
141     }
142 
143     freeAllocatedSingleString(currentline);
144     freeAllocatedSingleString(stringToAdd);
145     freeAllocatedSingleString(filePattern);
146     freeAllocatedSingleString(defaultPattern);
147     if (postCaretLine)
148     {
149         freeAllocatedSingleString(postCaretLine);
150     }
151 
152     AssignOutputVariable(pvApiCtx, 1) = *getNbInputArgument(pvApiCtx) + 1; // rhs + 1
153     ReturnArguments(pvApiCtx);
154 
155     return 0;
156 }
157 /*--------------------------------------------------------------------------*/
158