2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) ????-2008 - INRIA
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 #include "intersci-n.h"
23 static int removeEOL(char *_str)
27 int len = (int)strlen(_str);
30 if (_str[len - 1] == CR)
39 /**********************************************************
40 *Reading the intersci description file
41 **********************************************************/
43 int ReadFunction(FILE *f)
45 int i = 0, j = 0, l = 0, type = 0, ftype = 0;
48 char *optwords[MAXLINE];
50 int nwords = 0, line1 = 0, inbas = 0, fline1 = 0, infor = 0, nopt = 0, out1 = 0;
55 basfun->NewMaxOpt = 0;
64 while (fgets(s, MAXLINE, f) != NULL)
67 /* ignoring comments */
68 if (s[0] == '/' && s[1] == '/' ) continue;
70 /* analysis of one line */
72 nwords = ParseLine(s,words);
74 nwords = ParseScilabLine(s,words);
75 /* empty definition at end of file */
76 if (line1 == 1 && nwords == 0)
80 /* end of description */
81 if (words[0][0] == '*') return(1);
84 /* SCILAB function description */
85 if ((int)strlen(words[0]) > nlgh)
87 printf("SCILAB function name too long: \"%s\"\n",words[0]);
90 basfun->name = (char *)malloc((unsigned)(strlen(words[0])+1));
91 strcpy(basfun->name,words[0]);
92 printf("**************************\n");
93 printf("processing SCILAB function \"%s\"\n",words[0]);
94 funNames[nFun] = basfun->name;
98 printf("too may input arguments for SCILAB function\"%s\"\n",
100 printf(" augment constant \"MAXARG\" and recompile intersci\n");
104 for (i = 0; i < basfun->nin ; i++)
106 if (words[i+1][0] == '{')
109 nopt = ParseLine(words[i+1]+1,optwords);
111 printf("Bad syntax for optional argument. Two variables needed\n");
114 ivar = GetVar(optwords[0],1);
115 basfun->in[i] = ivar;
116 variables[ivar-1]->opt_type = NAME;
117 variables[ivar-1]->opt_name =
118 (char *)malloc((unsigned)(strlen(optwords[1])+1));
119 variables[ivar-1]->stack_position = icre++;
120 strcpy(variables[ivar-1]->opt_name,optwords[1]);
121 variables[ivar-1]->is_sciarg = 1;
123 else if (words[i+1][0] == '[')
126 nopt = ParseLine(words[i+1]+1,optwords);
129 printf("Bad syntax for optional argument. Two variables needed\n");
132 ivar = GetVar(optwords[0],1);
133 basfun->in[i] = ivar;
134 variables[ivar-1]->opt_type = VALUE;
135 variables[ivar-1]->opt_name =
136 (char *)malloc((unsigned)(strlen(optwords[1])+1));
137 strcpy(variables[ivar-1]->opt_name,optwords[1]);
138 variables[ivar-1]->stack_position = icre++;
139 variables[ivar-1]->is_sciarg = 1;
143 basfun->in[i] = GetVar(words[i+1],1);
144 variables[basfun->in[i]-1]->stack_position = icre++;
145 variables[basfun->in[i]-1]->is_sciarg = 1;
155 /* end of SCILAB variable description */
161 /* SCILAB variable description */
162 ivar = GetVar(words[0],1);
164 if ( variables[i]->is_sciarg == 0)
166 /** we only fix stack_position for remaining arguments**/
167 variables[i]->stack_position = icre++;
171 printf("type missing for variable \"%s\"\n",words[0]);
174 type = GetBasType(words[1]);
175 variables[i]->type = type;
193 printf("bad type specification for variable \"%s\"\n", words[0]);
194 printf("only %d argument given and %d are expected\n", nwords,3);
197 variables[i]->el[0] = GetVar(words[2],1);
198 variables[i]->length++;
204 printf("bad type specification for variable \"%s\"\n", words[0]);
205 printf("only %d argument given and %d are expected\n", nwords,3);
208 ReadListFile(words[2],words[0],i,
209 variables[i]->stack_position);
217 printf("bad type specification for variable \"%s\"\n",words[0]);
218 printf("%d argument given and %d are expected\n", nwords,4);
221 variables[i]->el[0] = GetVar(words[2],1);
222 variables[i]->el[1] = GetVar(words[3],1);
223 variables[i]->length = 2;
228 printf("bad type specification for variable \"%s\"\n",words[0]);
229 printf("%d argument given and %d are expected\n", nwords,4);
232 variables[i]->el[0] = GetVar(words[2],1);
233 variables[i]->el[1] = GetVar(words[3],1);
234 variables[i]->el[2] = GetVar(words[4],1);
235 variables[i]->length = 3;
240 printf("bad type specification for variable \"%s\"\n",words[0]);
241 printf("%d argument given and %d are expected\n", nwords,6);
242 printf("name sparse m n nel it\n");
245 variables[i]->el[0] = GetVar(words[2],1);
246 variables[i]->el[1] = GetVar(words[3],1);
247 variables[i]->el[2] = GetVar(words[4],1);
248 variables[i]->el[3] = GetVar(words[5],1);
249 variables[i]->length = 4;
252 printf("variable \"%s\" cannot have type \"SEQUENCE\"\n",
257 printf("variable \"%s\" cannot have type \"EMPTY\"\n",
264 else if (fline1 == 1)
266 /* FORTRAN subroutine description */
267 forsub->name = (char *)malloc((unsigned)(strlen(words[0])+1));
268 strcpy(forsub->name,words[0]);
272 printf("too many argument for FORTRAN subroutine \"%s\"\n",
274 printf(" augment constant \"MAXARG\" and recompile intersci\n");
278 for (i = 0; i < nwords - 1; i++)
280 forsub->arg[i] = GetExistVar(words[i+1]);
289 /* end of FORTRAN subroutine description */
295 /* FORTRAN variable description */
298 printf("type missing for FORTRAN argument \"%s\"\n",
302 ivar = GetExistVar(words[0]);
303 ftype = GetForType(words[1]);
304 variables[ivar-1]->for_type = ftype;
305 if (ftype == EXTERNAL)
307 strcpy((char *)(variables[ivar-1]->fexternal),words[1]);
308 switch (variables[ivar-1]->type)
322 printf("FORTRAN argument \"%s\" with external type \"%s\"\n",
323 variables[ivar-1]->name,words[1]);
324 printf(" cannot have a variable type of \"%s\"\n",SGetSciType(variables[ivar-1]->type));
333 /* output variable description */
337 printf("type missing for output variable \"out\"\n");
340 ivar = GetOutVar(words[0]);
343 type = GetBasType(words[1]);
344 variables[i]->type = type;
353 printf("list or sequence too long for output variable \"out\"\n");
354 printf(" augment constant \"MAXEL\" and recompile intersci\n");
357 for (j = 0; j < l; j++)
359 int k = GetExistVar(words[j+2]);
360 variables[i]->el[j] = k;
361 variables[k-1]->out_position = j+1;
363 variables[i]->length = l;
368 printf("output variable \"out\" of SCILAB function\n");
369 printf(" must have type \"LIST\", \"TLIST\", \"SEQUENCE\" or\n");
370 printf(" \"EMPTY\"\n");
378 /* possibly equal variables */
379 ivar = GetExistVar(words[0]);
381 variables[i]->equal = GetExistVar(words[1]);
385 /* end of description file */
389 /***********************************************************************
390 * put the words of SCILAB function description line "s" in "words" and
391 * return the number of words with checking syntax of optional variables:
392 * "{g the_g }" => 1 word "{g the_g\n"
393 * "[f v]" => 1 word "[f v\n"
394 **************************************************************************/
396 int ParseScilabLine(char *s,char *words[])
401 int inopt1 = 0; /* { } */
402 int inopt2 = 0; /* [ ] */
404 if (*s == ' ' || *s == '\t') inword = 0;
405 if (*s == '{') inopt1 = 1;
406 if (*s == '[') inopt2 = 1;
410 if (*s == '{' || *s == '[' || *s == ']' || *s == '\n') {
411 printf("Bad syntax for optional argument. No matching \"}\"\n");
414 else if (*s == '}') {
417 words[nwords] = (char *)malloc((unsigned)(i+1));
418 strcpy(words[nwords],w);
426 if (*s == '[' || *s == '{' || *s == '}' || *s == '\n') {
427 printf("Bad syntax for optional argument. No matching \"]\"\n");
430 else if (*s == ']') {
433 words[nwords] = (char *)malloc((unsigned)(i+1));
434 strcpy(words[nwords],w);
442 if (*s == ' ' || *s == '\t' || *s == '\n') {
444 words[nwords] = (char *)malloc((unsigned)(i+1));
445 strcpy(words[nwords],w);
452 if (*s != ' ' && *s != '\t') {
453 /* beginning of a word */
456 if (*s == '{') inopt1 = 1;
457 if (*s == '[') inopt2 = 1;
464 /* put the words of line "s" in "words" and return the number of words */
466 int ParseLine(char *s,char *words[])
472 if(*s == ' ' || *s == '\t') inword = 0;
478 if (*s == ' ' || *s == '\t' || *s == '\n')
481 words[nwords] = (char *)malloc((unsigned)(i+1));
482 strcpy(words[nwords],w);
490 if (*s != ' ' && *s != '\t')
498 /* bug 7599 fixed: if the last line end with eof, not eol then one word missed */
502 words[nwords] = (char *)malloc((unsigned)(i+1));
503 strcpy(words[nwords], w);
510 /***********************************************************************
511 * Read a List description file
512 **************************************************************************/
515 void ReadListFile(char *listname,char *varlistname,IVAR ivar,int stack_position)
521 sprintf(filin,"%s.list",listname);
522 fin = fopen(filin,"rt");
525 printf("description file for list or tlist \"%s\" does not exist\n",
529 printf("reading description file for list or tlist \"%s\"\n", listname);
532 while(ReadListElement(fin,varlistname,ivar,nel,stack_position))
540 int ReadListElement(FILE *f,char *varlistname,IVAR iivar,int nel,int stack_position)
543 char *words[MAXLINE];
544 int i = 0, nline = 0, nwords = 0, type = 0;
550 while (fgets(s, MAXLINE, f) != NULL)
553 /* analyse of one line */
560 /* SCILAB variable description */
561 nwords = ParseLine(s,words);
562 sprintf(str,"%s(%s)",words[0],varlistname);
563 ivar = GetVar(str,0);
565 variables[ivar-1]->stack_position =stack_position;
568 printf("type missing for variable \"%s\"\n",words[0]);
571 type = GetBasType(words[1]);
572 variables[i]->type = type;
573 variables[i]->list_name = (char *)malloc((unsigned)(strlen(varlistname)+1));
574 strcpy(variables[i]->list_name,varlistname);
575 variables[i]->list_el = nel+1;
587 printf("bad type for variable \"%s\"\n",
591 if (isdigit(words[2][0]))
593 variables[i]->el[0] = GetVar(words[2],0);
594 variables[i]->length = 1;
598 sprintf(str,"%s(%s)",words[2],varlistname);
599 variables[i]->el[0] = GetVar(str,0);
600 variables[i]->length = 1;
609 printf("bad type for variable \"%s\"\n",
613 if (isdigit(words[2][0]))
615 variables[i]->el[0] = GetVar(words[2],0);
616 variables[i]->length = 1;
620 sprintf(str,"%s(%s)",words[2],varlistname);
621 variables[i]->el[0] = GetVar(str,0);
622 variables[i]->length = 1;
624 if (isdigit(words[3][0]))
626 variables[i]->el[1] = GetVar(words[3],0);
627 variables[i]->length = 2;
631 sprintf(str,"%s(%s)",words[3],varlistname);
632 variables[i]->el[1] = GetVar(str,0);
633 variables[i]->length = 2;
639 printf("bad type for variable \"%s\"\n",
643 if (isdigit(words[2][0]))
645 variables[i]->el[0] = GetVar(words[2],0);
646 variables[i]->length = 1;
650 sprintf(str,"%s(%s)",words[2],varlistname);
651 variables[i]->el[0] = GetVar(str,0);
652 variables[i]->length = 1;
654 if (isdigit(words[3][0]))
656 variables[i]->el[1] = GetVar(words[3],0);
657 variables[i]->length = 2;
661 sprintf(str,"%s(%s)",words[3],varlistname);
662 variables[i]->el[1] = GetVar(str,0);
663 variables[i]->length = 2;
665 sprintf(str,"%s(%s)",words[4],varlistname);
666 variables[i]->el[2] = GetVar(str,0);
667 variables[i]->length = 3;
672 printf("bad type for variable \"%s\"\n",
676 if (isdigit(words[2][0]))
678 variables[i]->el[0] = GetVar(words[2],0);
679 variables[i]->length = 1;
683 sprintf(str,"%s(%s)",words[2],varlistname);
684 variables[i]->el[0] = GetVar(str,0);
685 variables[i]->length = 1;
687 if (isdigit(words[3][0]))
689 variables[i]->el[1] = GetVar(words[3],0);
690 variables[i]->length = 2;
694 sprintf(str,"%s(%s)",words[3],varlistname);
695 variables[i]->el[1] = GetVar(str,0);
696 variables[i]->length = 2;
698 if (isdigit(words[4][0]))
700 variables[i]->el[2] = GetVar(words[4],0);
701 variables[i]->length = 3;
705 sprintf(str,"%s(%s)",words[4],varlistname);
706 variables[i]->el[2] = GetVar(str,0);
707 variables[i]->length = 3;
709 sprintf(str,"%s(%s)",words[5],varlistname);
710 variables[i]->el[3] = GetVar(str,0);
711 variables[i]->length = 4;
718 printf("variable \"%s\" cannot have type \"%s\"\n",
719 words[0],SGetSciType(type));
722 printf("variable \"%s\" has unknown type \"%s\"\n",
723 words[0],SGetSciType(type));
727 /* end of description */
734 printf("bad description file for list or tlist \"%s\"\n",