Coverity: completion module resource leaks fixed
[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     freePointerDictionary(completionvariables, sizecompletionvariables);
96     freePointerDictionary(completionmacros, sizecompletionmacros);
97     freePointerDictionary(completionhandlegraphicsproperties, sizecompletionhandlegraphicsproperties);
98     freePointerDictionary(completionfunctions, sizecompletionfunctions);
99
100     return ListWords;
101 }
102 /*--------------------------------------------------------------------------*/
103 char **completionOnFunctions(const char *somechars, int *sizeArrayReturned)
104 {
105     char **ListWords = NULL;
106     char **dictionary = NULL;
107     int sizedictionary = 0;
108
109     dictionary = getFunctionsName(&sizedictionary);
110
111     if (dictionary)
112     {
113         ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
114         freePointerDictionary(dictionary, sizedictionary);
115     }
116     else
117     {
118         *sizeArrayReturned = 0;
119     }
120     return ListWords;
121 }
122 /*--------------------------------------------------------------------------*/
123 char **completionOnCommandWords(const char *somechars, int *sizeArrayReturned)
124 {
125     char **ListWords = NULL;
126     char **dictionary = NULL;
127     int sizedictionary = 0;
128
129     dictionary = getcommandkeywords(&sizedictionary);
130
131     if (dictionary)
132     {
133         dictionary = SortDictionary(dictionary, sizedictionary);
134         ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
135         freePointerDictionary(dictionary, sizedictionary);
136     }
137     else
138     {
139         *sizeArrayReturned = 0;
140     }
141     return ListWords;
142 }
143 /*--------------------------------------------------------------------------*/
144 char **completionOnMacros(const char *somechars, int *sizeArrayReturned)
145 {
146     char **ListWords = NULL;
147     char **dictionary = NULL;
148     int sizedictionary = 0;
149
150     dictionary = getMacrosName(&sizedictionary);
151
152     if (dictionary)
153     {
154         ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
155         freePointerDictionary(dictionary, sizedictionary);
156     }
157     else
158     {
159         *sizeArrayReturned = 0;
160     }
161     return ListWords;
162 }
163 /*--------------------------------------------------------------------------*/
164 char **completionOnVariables(const char *somechars, int *sizeArrayReturned)
165 {
166     int i = 0;
167     char **ListWords = NULL;
168     char **dictionary = NULL;
169     int sizedictionary = 0;
170     dictionary = getVariablesName(&sizedictionary, TRUE);
171     ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
172     freePointerDictionary(dictionary, sizedictionary);
173
174     return ListWords;
175 }
176 /*--------------------------------------------------------------------------*/
177 char **completionOnVariablesWithoutMacros(const char *somechars, int *sizeArrayReturned)
178 {
179     int i = 0;
180     int j = 0;
181     int nbWordsAlreadyInMacros = 0;
182
183     char **ListWords = NULL;
184     int sizeListWords = 0;
185
186     char **dictionaryVariables = NULL;
187     int sizedictionaryVariables = 0;
188
189     dictionaryVariables = completionOnVariables(somechars, &sizedictionaryVariables);
190
191     if (sizedictionaryVariables)
192     {
193         char **dictionaryMacros = NULL;
194         int sizedictionaryMacros = 0;
195
196         dictionaryMacros = getMacrosName(&sizedictionaryMacros);
197
198         /* Search if we have more than one definition */
199         for ( i = 0; i < sizedictionaryVariables; i++)
200         {
201             for ( j = 0; j < sizedictionaryMacros; j++)
202             {
203                 if ( strcmp(dictionaryVariables[i], dictionaryMacros[j]) == 0 )
204                 {
205                     nbWordsAlreadyInMacros++;
206                 }
207             }
208         }
209
210         if (nbWordsAlreadyInMacros)
211         {
212             sizeListWords = sizedictionaryVariables - nbWordsAlreadyInMacros;
213             if (sizeListWords > 0)
214             {
215                 char **ListWordsTmp = (char**)MALLOC(sizeof(char*) * sizedictionaryVariables);
216                 if (ListWordsTmp)
217                 {
218                     int k = 0;
219
220                     /* do a copy of dictionary of Variables */
221                     for ( i = 0; i < sizedictionaryVariables; i++)
222                     {
223                         ListWordsTmp[i] = os_strdup(dictionaryVariables[i]);
224                     }
225
226                     for ( i = 0; i < sizedictionaryVariables; i++)
227                     {
228                         for ( j = 0; j < sizedictionaryMacros; j++)
229                         {
230                             if ( strcmp(dictionaryVariables[i], dictionaryMacros[j]) == 0 )
231                             {
232                                 FREE(ListWordsTmp[i]);
233                                 ListWordsTmp[i] = NULL;
234                             }
235                         }
236                     }
237
238                     ListWords = (char**)MALLOC(sizeof(char*) * (sizeListWords + 1));
239                     if (ListWords)
240                     {
241                         for ( i = 0; i < sizedictionaryVariables; i++)
242                         {
243                             if (ListWordsTmp[i])
244                             {
245                                 ListWords[k] = os_strdup(ListWordsTmp[i]);
246                                 if (k <= sizeListWords)
247                                 {
248                                     k++;
249                                 }
250                             }
251                         }
252                         /* Add a NULL element at the end (to get number of items from JNI) */
253                         ListWords[sizeListWords] = NULL;
254
255                         *sizeArrayReturned = sizeListWords;
256                     }
257                     else
258                     {
259                         ListWords = NULL;
260                         *sizeArrayReturned = 0;
261                     }
262                 }
263                 else
264                 {
265                     ListWords = NULL;
266                     *sizeArrayReturned = 0;
267                 }
268             }
269             else
270             {
271                 ListWords = NULL;
272                 *sizeArrayReturned = 0;
273             }
274         }
275         else
276         {
277             ListWords = dictionaryVariables;
278             *sizeArrayReturned = sizedictionaryVariables;
279         }
280
281         freePointerDictionary(dictionaryMacros, sizedictionaryMacros);
282     }
283     else
284     {
285         ListWords = NULL;
286         *sizeArrayReturned = 0;
287     }
288     freePointerDictionary(dictionaryVariables, sizedictionaryVariables);
289
290     return ListWords;
291 }
292 /*--------------------------------------------------------------------------*/
293 char **completionOnFiles(const char *somechars, int *sizeArrayReturned)
294 {
295     char **ListWords = NULL;
296     char **dictionary = NULL;
297     int sizedictionary = 0;
298
299     dictionary = getfilesdictionary(somechars, &sizedictionary, FALSE);
300
301     if (dictionary)
302     {
303         ListWords = dictionary;
304         *sizeArrayReturned = sizedictionary;
305     }
306     else
307     {
308         *sizeArrayReturned = 0;
309     }
310     return ListWords;
311 }
312 /*--------------------------------------------------------------------------*/
313 char **completionOnFields(char *lineBeforeCaret, char *pattern, int *sizeArrayReturned)
314 {
315     char **ListWords = NULL;
316     char **dictionary = NULL;
317     int sizedictionary = 0;
318
319     if (lineBeforeCaret && pattern)
320     {
321         dictionary = getfieldsdictionary(lineBeforeCaret, pattern, &sizedictionary);
322     }
323
324     if (dictionary)
325     {
326         ListWords = dictionary;
327         *sizeArrayReturned = sizedictionary;
328     }
329     else
330     {
331         *sizeArrayReturned = 0;
332     }
333     return ListWords;
334 }
335 /*--------------------------------------------------------------------------*/
336 char **completionOnHandleGraphicsProperties(const char *somechars, int *sizeArrayReturned)
337 {
338     char **ListWords = NULL;
339     char **dictionary = NULL;
340     int sizedictionary = 0;
341
342     int sizeHandleGraphicsGetPropertiesDictionary = 0;
343     char **HandleGraphicsGetPropertiesDictionary = getDictionaryGetProperties(&sizeHandleGraphicsGetPropertiesDictionary);
344
345     int sizeHandleGraphicsSetPropertiesDictionary = 0;
346     char **HandleGraphicsSetPropertiesDictionary = getDictionarySetProperties(&sizeHandleGraphicsSetPropertiesDictionary);
347
348     *sizeArrayReturned = 0;
349
350     sizedictionary = sizeHandleGraphicsGetPropertiesDictionary + sizeHandleGraphicsSetPropertiesDictionary;
351
352     if (sizedictionary > 0)
353     {
354         dictionary = (char**)MALLOC(sizeof(char*) * sizedictionary);
355
356         if (dictionary)
357         {
358             int i = 0;
359
360             appendDictionary(&dictionary, &i, &HandleGraphicsGetPropertiesDictionary, &sizeHandleGraphicsGetPropertiesDictionary);
361             appendDictionary(&dictionary, &i, &HandleGraphicsSetPropertiesDictionary, &sizeHandleGraphicsSetPropertiesDictionary);
362             sizedictionary = i;
363         }
364
365         if (dictionary)
366         {
367             dictionary = SortDictionary(dictionary, sizedictionary);
368             dictionary = RemoveDuplicateDictionary(dictionary, &sizedictionary);
369             ListWords = completionOnDictionary(dictionary, sizedictionary, somechars, sizeArrayReturned);
370             freePointerDictionary(dictionary, sizedictionary);
371         }
372         else
373         {
374             *sizeArrayReturned = 0;
375         }
376     }
377     else
378     {
379         *sizeArrayReturned = 0;
380     }
381     freePointerDictionary(HandleGraphicsSetPropertiesDictionary, sizeHandleGraphicsSetPropertiesDictionary);
382     freePointerDictionary(HandleGraphicsGetPropertiesDictionary, sizeHandleGraphicsGetPropertiesDictionary);
383     return ListWords;
384 }
385 /*--------------------------------------------------------------------------*/
386 char **completionOnDictionary(char **dictionary, int sizedictionary, const char *somechars, int *sizearrayreturned)
387 {
388     char **ListWords = NULL;
389
390     if (dictionary)
391     {
392         ListWords = completion_generic(dictionary, sizedictionary, somechars, sizearrayreturned);
393         if (ListWords == NULL)
394         {
395             *sizearrayreturned = 0;
396         }
397     }
398     else
399     {
400         *sizearrayreturned = 0;
401     }
402     return ListWords;
403 }
404 /*--------------------------------------------------------------------------*/