e42173b5bb5e92abebed89f9c368483be881c5e1
[scilab.git] / scilab / modules / completion / src / c / completion.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - 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 #include <string.h>
16 #include <stdlib.h>
17 #include "completion.h"
18 #include "sci_malloc.h"
19 #include "getvariablesname.h"
20 #include "commandwords.h"
21 #include "getfunctionsname.h"
22 #include "getmacrosname.h"
23 #include "completion_generic.h"
24 #include "getfilesdictionary.h"
25 #include "getfieldsdictionary.h"
26 #include "getDictionarySetProperties.h"
27 #include "getDictionaryGetProperties.h"
28 #include "toolsdictionary.h"
29 #include "os_string.h"
30 #include "stdio.h"
31 /*--------------------------------------------------------------------------*/
32 char **completionOnDictionary(char **dictionary, int sizedictionary, const char *somechars, int *sizearrayreturned);
33 /*--------------------------------------------------------------------------*/
34 char **completion(const char *somechars, int *sizeArrayReturned)
35 {
36     char **ListWords = NULL;
37
38     char **dictionary = NULL;
39     int sizedictionary = 0;
40
41     int sizecompletionfunctions = 0;
42     char **completionfunctions = completionOnFunctions(somechars, &sizecompletionfunctions);
43
44     int sizecompletioncommandwords = 0;
45     char **completioncommandwords = completionOnCommandWords(somechars, &sizecompletioncommandwords);
46
47     int sizecompletionmacros = 0;
48     char **completionmacros =  completionOnMacros(somechars, &sizecompletionmacros);
49
50     int sizecompletionvariables = 0;
51     char **completionvariables = completionOnVariables(somechars, &sizecompletionvariables);
52
53     int sizecompletionhandlegraphicsproperties = 0;
54     char **completionhandlegraphicsproperties = completionOnHandleGraphicsProperties(somechars, &sizecompletionhandlegraphicsproperties);
55
56     int sizecompletionfiles = 0;
57     char **completionfiles = completionOnFiles(somechars, &sizecompletionfiles);
58
59     *sizeArrayReturned = 0;
60
61     sizedictionary = sizecompletionfunctions + sizecompletioncommandwords + sizecompletionmacros
62                      + sizecompletionvariables + sizecompletionhandlegraphicsproperties + sizecompletionfiles;
63
64
65     if ( (completionfiles) && (sizedictionary == sizecompletionfiles) )
66     {
67         ListWords = completionfiles;
68         *sizeArrayReturned = sizecompletionfiles;
69     }
70     else
71     {
72         if (sizedictionary > 0)
73         {
74             dictionary = (char**)MALLOC(sizeof(char*) * sizedictionary);
75         }
76
77         if (dictionary)
78         {
79             int i = 0;
80             appendDictionary(&dictionary, &i, &completionfunctions, &sizecompletionfunctions);
81             appendDictionary(&dictionary, &i, &completioncommandwords, &sizecompletioncommandwords);
82             appendDictionary(&dictionary, &i, &completionmacros, &sizecompletionmacros);
83             appendDictionary(&dictionary, &i, &completionvariables, &sizecompletionvariables);
84             appendDictionary(&dictionary, &i, &completionhandlegraphicsproperties, &sizecompletionhandlegraphicsproperties);
85             appendDictionary(&dictionary, &i, &completionfiles, &sizecompletionfiles);
86
87             dictionary = SortDictionary(dictionary, i);
88             dictionary = RemoveDuplicateDictionary(dictionary, &i);
89
90             sizedictionary = i;
91         }
92         ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
93         freePointerDictionary(dictionary, sizedictionary);
94     }
95
96     return ListWords;
97 }
98 /*--------------------------------------------------------------------------*/
99 char **completionOnFunctions(const char *somechars, int *sizeArrayReturned)
100 {
101     char **ListWords = NULL;
102     char **dictionary = NULL;
103     int sizedictionary = 0;
104
105     dictionary = getFunctionsName(&sizedictionary);
106
107     if (dictionary)
108     {
109         ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
110         freePointerDictionary(dictionary, sizedictionary);
111     }
112     else
113     {
114         *sizeArrayReturned = 0;
115     }
116     return ListWords;
117 }
118 /*--------------------------------------------------------------------------*/
119 char **completionOnCommandWords(const char *somechars, int *sizeArrayReturned)
120 {
121     char **ListWords = NULL;
122     char **dictionary = NULL;
123     int sizedictionary = 0;
124
125     dictionary = getcommandkeywords(&sizedictionary);
126
127     if (dictionary)
128     {
129         dictionary = SortDictionary(dictionary, sizedictionary);
130         ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
131         freePointerDictionary(dictionary, sizedictionary);
132     }
133     else
134     {
135         *sizeArrayReturned = 0;
136     }
137     return ListWords;
138 }
139 /*--------------------------------------------------------------------------*/
140 char **completionOnMacros(const char *somechars, int *sizeArrayReturned)
141 {
142     char **ListWords = NULL;
143     char **dictionary = NULL;
144     int sizedictionary = 0;
145
146     dictionary = getMacrosName(&sizedictionary);
147
148     if (dictionary)
149     {
150         ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
151         freePointerDictionary(dictionary, sizedictionary);
152     }
153     else
154     {
155         *sizeArrayReturned = 0;
156     }
157     return ListWords;
158 }
159 /*--------------------------------------------------------------------------*/
160 char **completionOnVariables(const char *somechars, int *sizeArrayReturned)
161 {
162     int i = 0;
163     char **ListWords = NULL;
164     char **dictionary = NULL;
165     int sizedictionary = 0;
166     dictionary = getVariablesName(&sizedictionary, TRUE);
167     ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
168     freePointerDictionary(dictionary, sizedictionary);
169
170     return ListWords;
171 }
172 /*--------------------------------------------------------------------------*/
173 char **completionOnVariablesWithoutMacros(const char *somechars, int *sizeArrayReturned)
174 {
175     int i = 0;
176     int j = 0;
177     int nbWordsAlreadyInMacros = 0;
178
179     char **ListWords = NULL;
180     int sizeListWords = 0;
181
182     char **dictionaryVariables = NULL;
183     int sizedictionaryVariables = 0;
184
185     dictionaryVariables = completionOnVariables(somechars, &sizedictionaryVariables);
186
187     if (sizedictionaryVariables)
188     {
189         char **dictionaryMacros = NULL;
190         int sizedictionaryMacros = 0;
191
192         dictionaryMacros = getMacrosName(&sizedictionaryMacros);
193
194         /* Search if we have more than one definition */
195         for ( i = 0; i < sizedictionaryVariables; i++)
196         {
197             for ( j = 0; j < sizedictionaryMacros; j++)
198             {
199                 if ( strcmp(dictionaryVariables[i], dictionaryMacros[j]) == 0 )
200                 {
201                     nbWordsAlreadyInMacros++;
202                 }
203             }
204         }
205
206         if (nbWordsAlreadyInMacros)
207         {
208             sizeListWords = sizedictionaryVariables - nbWordsAlreadyInMacros;
209             if (sizeListWords > 0)
210             {
211                 char **ListWordsTmp = (char**)MALLOC(sizeof(char*) * sizedictionaryVariables);
212                 if (ListWordsTmp)
213                 {
214                     int k = 0;
215
216                     /* do a copy of dictionary of Variables */
217                     for ( i = 0; i < sizedictionaryVariables; i++)
218                     {
219                         ListWordsTmp[i] = os_strdup(dictionaryVariables[i]);
220                     }
221
222                     for ( i = 0; i < sizedictionaryVariables; i++)
223                     {
224                         for ( j = 0; j < sizedictionaryMacros; j++)
225                         {
226                             if ( strcmp(dictionaryVariables[i], dictionaryMacros[j]) == 0 )
227                             {
228                                 FREE(ListWordsTmp[i]);
229                                 ListWordsTmp[i] = NULL;
230                             }
231                         }
232                     }
233
234                     ListWords = (char**)MALLOC(sizeof(char*) * (sizeListWords + 1));
235                     if (ListWords)
236                     {
237                         for ( i = 0; i < sizedictionaryVariables; i++)
238                         {
239                             if (ListWordsTmp[i])
240                             {
241                                 ListWords[k] = os_strdup(ListWordsTmp[i]);
242                                 if (k <= sizeListWords)
243                                 {
244                                     k++;
245                                 }
246                             }
247                         }
248                         /* Add a NULL element at the end (to get number of items from JNI) */
249                         ListWords[sizeListWords] = NULL;
250
251                         *sizeArrayReturned = sizeListWords;
252                     }
253                     else
254                     {
255                         ListWords = NULL;
256                         *sizeArrayReturned = 0;
257                     }
258                 }
259                 else
260                 {
261                     ListWords = NULL;
262                     *sizeArrayReturned = 0;
263                 }
264             }
265             else
266             {
267                 ListWords = NULL;
268                 *sizeArrayReturned = 0;
269             }
270         }
271         else
272         {
273             ListWords = dictionaryVariables;
274             *sizeArrayReturned = sizedictionaryVariables;
275         }
276
277         freePointerDictionary(dictionaryMacros, sizedictionaryMacros);
278     }
279     else
280     {
281         ListWords = NULL;
282         *sizeArrayReturned = 0;
283     }
284
285     return ListWords;
286 }
287 /*--------------------------------------------------------------------------*/
288 char **completionOnFiles(const char *somechars, int *sizeArrayReturned)
289 {
290     char **ListWords = NULL;
291     char **dictionary = NULL;
292     int sizedictionary = 0;
293
294     dictionary = getfilesdictionary(somechars, &sizedictionary, FALSE);
295
296     if (dictionary)
297     {
298         ListWords = dictionary;
299         *sizeArrayReturned = sizedictionary;
300     }
301     else
302     {
303         *sizeArrayReturned = 0;
304     }
305     return ListWords;
306 }
307 /*--------------------------------------------------------------------------*/
308 char **completionOnFields(char *lineBeforeCaret, char *pattern, int *sizeArrayReturned)
309 {
310     char **ListWords = NULL;
311     char **dictionary = NULL;
312     int sizedictionary = 0;
313
314     if (lineBeforeCaret && pattern)
315     {
316         dictionary = getfieldsdictionary(lineBeforeCaret, pattern, &sizedictionary);
317     }
318
319     if (dictionary)
320     {
321         ListWords = dictionary;
322         *sizeArrayReturned = sizedictionary;
323     }
324     else
325     {
326         *sizeArrayReturned = 0;
327     }
328     return ListWords;
329 }
330 /*--------------------------------------------------------------------------*/
331 char **completionOnHandleGraphicsProperties(const char *somechars, int *sizeArrayReturned)
332 {
333     char **ListWords = NULL;
334     char **dictionary = NULL;
335     int sizedictionary = 0;
336
337     int sizeHandleGraphicsGetPropertiesDictionary = 0;
338     char **HandleGraphicsGetPropertiesDictionary = getDictionaryGetProperties(&sizeHandleGraphicsGetPropertiesDictionary);
339
340     int sizeHandleGraphicsSetPropertiesDictionary = 0;
341     char **HandleGraphicsSetPropertiesDictionary = getDictionarySetProperties(&sizeHandleGraphicsSetPropertiesDictionary);
342
343     *sizeArrayReturned = 0;
344
345     sizedictionary = sizeHandleGraphicsGetPropertiesDictionary + sizeHandleGraphicsSetPropertiesDictionary;
346
347     if (sizedictionary > 0)
348     {
349         dictionary = (char**)MALLOC(sizeof(char*) * sizedictionary);
350
351         if (dictionary)
352         {
353             int i = 0;
354
355             appendDictionary(&dictionary, &i, &HandleGraphicsGetPropertiesDictionary, &sizeHandleGraphicsGetPropertiesDictionary);
356             appendDictionary(&dictionary, &i, &HandleGraphicsSetPropertiesDictionary, &sizeHandleGraphicsSetPropertiesDictionary);
357             sizedictionary = i;
358         }
359
360         if (dictionary)
361         {
362             dictionary = SortDictionary(dictionary, sizedictionary);
363             dictionary = RemoveDuplicateDictionary(dictionary, &sizedictionary);
364             ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
365             freePointerDictionary(dictionary, sizedictionary);
366         }
367         else
368         {
369             *sizeArrayReturned = 0;
370         }
371     }
372     else
373     {
374         *sizeArrayReturned = 0;
375     }
376     return ListWords;
377 }
378 /*--------------------------------------------------------------------------*/
379 char **completionOnDictionary(char **dictionary, int sizedictionary, const char *somechars, int *sizearrayreturned)
380 {
381     char **ListWords = NULL;
382
383     if (dictionary)
384     {
385         ListWords = completion_generic(dictionary, sizedictionary, somechars, sizearrayreturned);
386         if (ListWords == NULL)
387         {
388             *sizearrayreturned = 0;
389         }
390     }
391     else
392     {
393         *sizearrayreturned = 0;
394     }
395     return ListWords;
396 }
397 /*--------------------------------------------------------------------------*/