read.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 
00003 #include "intersci-n.h"
00004 
00005 /**********************************************************
00006  *Reading the intersci description file 
00007  **********************************************************/
00008   
00009 int ReadFunction(f)
00010      FILE *f;
00011 {
00012   int i, j, l, type, ftype;
00013   char s[MAXLINE];
00014   char *words[MAXLINE];
00015   char *optwords[MAXLINE];
00016   IVAR ivar;
00017   int nwords, line1, inbas, fline1, infor, nopt, out1;
00018   nVariable = 0;
00019   icre=1;
00020   basfun->maxOpt = 0;
00021   basfun->NewMaxOpt = 0;
00022   line1 = 1;
00023   inbas = 0;
00024   fline1 = 0;
00025   infor = 0;
00026   out1 = 0;
00027   while (fgets(s,MAXLINE,f)) 
00028     {
00029       /* ignoring comments */
00030       if (s[0] == '/' && s[1] == '/' ) continue;
00031       
00032       /* analysis of one line */
00033       if (line1 != 1) 
00034         nwords = ParseLine(s,words);
00035       else 
00036         nwords = ParseScilabLine(s,words);
00037       /* empty definition at end of file */
00038       if (line1 == 1 && nwords == 0) 
00039         {
00040           return 0;
00041         }
00042       /* end of description */
00043       if (words[0][0] == '*') return(1);
00044       if (line1 == 1) 
00045         {
00046           /* SCILAB function description */
00047           if ((int)strlen(words[0]) > 24) 
00048             {
00049               printf("SCILAB function name too long: \"%s\"\n",words[0]);
00050               exit(1);
00051             }
00052           basfun->name = (char *)malloc((unsigned)(strlen(words[0])+1));
00053           strcpy(basfun->name,words[0]);
00054           printf("**************************\n");
00055           printf("processing SCILAB function \"%s\"\n",words[0]);
00056           funNames[nFun] = basfun->name;
00057           i = nwords - 1;
00058           if (i > MAXARG) 
00059             {
00060               printf("too may input arguments for SCILAB function\"%s\"\n",
00061                      words[0]);
00062               printf("  augment constant \"MAXARG\" and recompile intersci\n");
00063               exit(1);
00064             }
00065           basfun->nin = i;
00066           for (i = 0; i < basfun->nin ; i++) 
00067             {
00068               if (words[i+1][0] == '{') 
00069                 {
00070                   basfun->maxOpt++;
00071                   nopt = ParseLine(words[i+1]+1,optwords);
00072                   if (nopt != 2) {
00073                     printf("Bad syntax for optional argument. Two variables needed\n");
00074                     exit(1);
00075                   }
00076                   ivar = GetVar(optwords[0],1);
00077                   basfun->in[i] = ivar;
00078                   variables[ivar-1]->opt_type = NAME;
00079                   variables[ivar-1]->opt_name =
00080                     (char *)malloc((unsigned)(strlen(optwords[1])+1));
00081                   variables[ivar-1]->stack_position = icre++;
00082                   strcpy(variables[ivar-1]->opt_name,optwords[1]);
00083                   variables[ivar-1]->is_sciarg = 1;
00084                 }
00085               else if (words[i+1][0] == '[') 
00086                 {
00087                   basfun->maxOpt++;
00088                   nopt = ParseLine(words[i+1]+1,optwords);
00089                   if (nopt != 2) 
00090                     {
00091                       printf("Bad syntax for optional argument. Two variables needed\n");
00092                       exit(1);
00093                     }
00094                   ivar = GetVar(optwords[0],1);
00095                   basfun->in[i] = ivar;
00096                   variables[ivar-1]->opt_type = VALUE;
00097                   variables[ivar-1]->opt_name =
00098                     (char *)malloc((unsigned)(strlen(optwords[1])+1));
00099                   strcpy(variables[ivar-1]->opt_name,optwords[1]);
00100                   variables[ivar-1]->stack_position = icre++;
00101                   variables[ivar-1]->is_sciarg = 1;
00102                 }
00103               else 
00104                 {
00105                   basfun->in[i] = GetVar(words[i+1],1);
00106                   variables[basfun->in[i]-1]->stack_position = icre++;
00107                   variables[basfun->in[i]-1]->is_sciarg = 1;
00108                 }
00109             }
00110           line1 = 0;
00111           inbas = 1;
00112         } 
00113       else if (inbas == 1) 
00114         {
00115           if (nwords == 0) 
00116             {
00117               /* end of SCILAB variable description */
00118               inbas = 0;
00119               fline1 = 1;
00120             } 
00121           else 
00122             {
00123               /* SCILAB variable description */
00124               ivar = GetVar(words[0],1);
00125               i = ivar - 1;
00126               if ( variables[i]->is_sciarg == 0) 
00127                 {
00129                   variables[i]->stack_position = icre++;
00130                 }
00131               if (nwords == 1) 
00132                 {
00133                   printf("type missing for variable \"%s\"\n",words[0]);
00134                   exit(1);
00135                 }
00136               type = GetBasType(words[1]);
00137               variables[i]->type = type;
00138               switch (type) 
00139                 {
00140                 case SCALAR:
00141                 case ANY:
00142                 case SCIMPOINTER:
00143                 case SCISMPOINTER:
00144                 case SCILPOINTER:
00145                 case SCIBPOINTER:
00146                 case SCIOPOINTER:
00147                   break;
00148                 case COLUMN:
00149                 case ROW:
00150                 case STRING:
00151                 case WORK:
00152                 case VECTOR:
00153                   if (nwords != 3) 
00154                     {
00155                       printf("bad type specification for variable \"%s\" \n", words[0]);
00156                       printf("only %d argument given and %d are expected\n", nwords,3);
00157                       exit(1);
00158                     }
00159                   variables[i]->el[0] = GetVar(words[2],1);
00160                   variables[i]->length++;
00161                   break;
00162                 case LIST:
00163                 case TLIST:
00164                   if (nwords != 3) 
00165                     {
00166                       printf("bad type specification for variable \"%s\"\n", words[0]);
00167                       printf("only %d argument given and %d are expected\n", nwords,3);
00168                       exit(1);
00169                     }
00170                   ReadListFile(words[2],words[0],i,
00171                                variables[i]->stack_position);
00172                   break;
00173                 case POLYNOM:
00174                 case MATRIX:
00175                 case BMATRIX:
00176                 case STRINGMAT:
00177                   if (nwords != 4) 
00178                     {
00179                       printf("bad type specification for variable \"%s\"\n",words[0]);
00180                       printf("%d argument given and %d are expected\n", nwords,4);
00181                       exit(1);
00182                     }     
00183                   variables[i]->el[0] = GetVar(words[2],1);
00184                   variables[i]->el[1] = GetVar(words[3],1);
00185                   variables[i]->length = 2;
00186                   break;
00187                 case IMATRIX:
00188                   if (nwords != 5) 
00189                     {
00190                       printf("bad type specification for variable \"%s\"\n",words[0]);
00191                       printf("%d argument given and %d are expected\n", nwords,4);
00192                       exit(1);
00193                     }     
00194                   variables[i]->el[0] = GetVar(words[2],1);
00195                   variables[i]->el[1] = GetVar(words[3],1);
00196                   variables[i]->el[2] = GetVar(words[4],1);
00197                   variables[i]->length = 3;
00198                   break;
00199                 case SPARSE:
00200                   if (nwords != 6)
00201                     {
00202                       printf("bad type specification for variable \"%s\"\n",words[0]);
00203                       printf("%d argument given and %d are expected\n", nwords,6);
00204                       printf("name sparse m n nel it\n");
00205                       exit(1);
00206                     }     
00207                   variables[i]->el[0] = GetVar(words[2],1);
00208                   variables[i]->el[1] = GetVar(words[3],1);
00209                   variables[i]->el[2] = GetVar(words[4],1);
00210                   variables[i]->el[3] = GetVar(words[5],1);
00211                   variables[i]->length = 4;
00212                   break;
00213                 case SEQUENCE:
00214                   printf("variable \"%s\" cannot have type \"SEQUENCE\"\n",
00215                          words[0]);
00216                   exit(1);
00217                   break;
00218                 case EMPTY:
00219                   printf("variable \"%s\" cannot have type \"EMPTY\"\n",
00220                          words[0]);
00221                   exit(1);
00222                   break;
00223                 }
00224             }
00225         } 
00226       else if (fline1 == 1) 
00227         {
00228           /* FORTRAN subroutine description */
00229           forsub->name = (char *)malloc((unsigned)(strlen(words[0])+1));
00230           strcpy(forsub->name,words[0]);
00231           i = nwords - 1;
00232           if (i > MAXARG) 
00233             {
00234               printf("too many argument for FORTRAN subroutine \"%s\"\n",
00235                      words[0]);
00236               printf("  augment constant \"MAXARG\" and recompile intersci\n");
00237               exit(1);
00238             }
00239           forsub->narg = i;
00240           for (i = 0; i < nwords - 1; i++) 
00241             {
00242               forsub->arg[i] = GetExistVar(words[i+1]);
00243             }
00244           fline1 = 0;
00245           infor = 1;
00246         } 
00247       else if (infor == 1) 
00248         {
00249           if (nwords == 0) 
00250             {
00251               /* end of FORTRAN subroutine description */
00252               infor = 0;
00253               out1 = 1;
00254             }
00255           else 
00256             {
00257               /* FORTRAN variable description */
00258               if (nwords == 1) 
00259                 {
00260                   printf("type missing for FORTRAN argument \"%s\"\n",
00261                          words[0]);
00262                   exit(1);
00263                 }
00264               ivar = GetExistVar(words[0]);
00265               ftype = GetForType(words[1]);
00266               variables[ivar-1]->for_type = ftype;
00267               if (ftype == EXTERNAL) 
00268                 {
00269                   strcpy((char *)(variables[ivar-1]->fexternal),words[1]);
00270                   switch (variables[ivar-1]->type) 
00271                     {
00272                     case LIST :
00273                     case TLIST :
00274                     case SCALAR :
00275                     case SEQUENCE :
00276                     case WORK:
00277                     case EMPTY :
00278                     case ANY:
00279                     case SCIMPOINTER :
00280                     case SCISMPOINTER :
00281                     case SCILPOINTER :
00282                     case SCIBPOINTER :
00283                     case SCIOPOINTER :
00284                       printf("FORTRAN argument \"%s\" with external type \"%s\"\n",
00285                              variables[ivar-1]->name,words[1]);
00286                       printf("  cannot have a variable type of \"%s\" \n",SGetSciType(variables[ivar-1]->type));
00287                       exit(1);
00288                       break;
00289                     }
00290                 }
00291             }
00292         } 
00293       else if (out1 == 1) 
00294         {
00295           /* output variable description */
00296           i = ivar - 1;
00297           if (nwords == 1) 
00298             {
00299               printf("type missing for output variable \"out\"\n");
00300               exit(1);
00301             }
00302           ivar = GetOutVar(words[0]);
00303           basfun->out = ivar;
00304           i = ivar - 1;
00305           type = GetBasType(words[1]);
00306           variables[i]->type = type;
00307           switch (type) 
00308             {
00309             case LIST:
00310             case TLIST:
00311             case SEQUENCE:
00312               l = nwords - 2;
00313               if (l > MAXEL) 
00314                 {
00315                   printf("list or sequence too long for output variable \"out\"\n");
00316                   printf("  augment constant \"MAXEL\" and recompile intersci\n");
00317                   exit(1);
00318                 }
00319               for (j = 0; j < l; j++) 
00320                 {
00321                   int k = GetExistVar(words[j+2]);
00322                   variables[i]->el[j] = k;
00323                   variables[k-1]->out_position = j+1;
00324                 }
00325               variables[i]->length = l;
00326               break;
00327             case EMPTY:
00328               break;
00329             default:
00330               printf("output variable \"out\" of SCILAB function\n");
00331               printf("  must have type \"LIST\", \"TLIST\", \"SEQUENCE\" or\n");
00332               printf("  \"EMPTY\"\n");
00333               exit(1);
00334               break;
00335             }
00336           out1 = 0;
00337         }
00338       else 
00339         {
00340           /* possibly equal variables */
00341           ivar = GetExistVar(words[0]);
00342           i = ivar -1 ;
00343           variables[i]->equal = GetExistVar(words[1]);
00344         }
00345     }
00346   /* end of description file */
00347   return(0);
00348 }
00349 
00350 /***********************************************************************
00351  *  put the words of SCILAB function description line "s" in "words" and 
00352  * return the number of words with checking syntax of optional variables:
00353  * "{g  the_g }" => 1 word "{g  the_g \n"
00354  * "[f v]" => 1 word "[f v\n" 
00355  **************************************************************************/
00356 
00357 int ParseScilabLine(s,words)
00358      char *s, *words[];
00359 {
00360   char w[MAXNAM];
00361   int nwords = 0;
00362   int inword = 1;
00363   int inopt1 = 0; /* {  } */
00364   int inopt2 = 0; /* [  ] */
00365   int i = 0;
00366   if (*s == ' ' || *s == '\t') inword = 0;
00367   if (*s == '{') inopt1 = 1;
00368   if (*s == '[') inopt2 = 1;
00369   while (*s) {
00370     if (inopt1) {
00371       w[i++] = *s++;
00372       if (*s == '{' || *s == '[' || *s == ']' || *s == '\n') {
00373         printf("Bad syntax for optional argument. No matching \"}\"\n");
00374         exit(1);
00375       }
00376       else if (*s == '}') {
00377         w[i++] = '\n';
00378         w[i] = '\0';
00379         words[nwords] = (char *)malloc((unsigned)(i+1));
00380         strcpy(words[nwords],w);
00381         nwords++;
00382         inopt1 = 0;
00383         inword = 0;
00384       }
00385     }
00386     else if (inopt2) {
00387       w[i++] = *s++;
00388       if (*s == '[' || *s == '{' || *s == '}' || *s == '\n') {
00389         printf("Bad syntax for optional argument. No matching \"]\"\n");
00390         exit(1);
00391       }
00392       else if (*s == ']') {
00393         w[i++] = '\n';
00394         w[i] = '\0';
00395         words[nwords] = (char *)malloc((unsigned)(i+1));
00396         strcpy(words[nwords],w);
00397         nwords++;
00398         inopt2 = 0;
00399         inword = 0;
00400       }
00401     }
00402     else if (inword) {
00403       w[i++] = *s++;
00404       if (*s == ' ' || *s == '\t' || *s == '\n') {
00405         w[i] = '\0';
00406         words[nwords] = (char *)malloc((unsigned)(i+1));
00407         strcpy(words[nwords],w);
00408         nwords++;
00409         inword = 0;
00410       }
00411     }
00412     else {
00413       s++; /* *s++; */
00414       if (*s != ' ' && *s != '\t') {
00415         /* beginning of a word */
00416         i = 0;
00417         inword = 1;
00418         if (*s == '{') inopt1 = 1;
00419         if (*s == '[') inopt2 = 1;
00420       }
00421     }
00422   }
00423   return(nwords);
00424 }
00425 
00426 /* put the words of line "s" in "words" and return the number of words */
00427 
00428 int ParseLine(s,words)
00429      char *s, *words[];
00430 {
00431   char w[MAXNAM];
00432   int nwords = 0;
00433   int inword = 1;
00434   int i = 0;
00435   if(*s == ' ' || *s == '\t') inword = 0;
00436   while (*s) {
00437     if (inword) {
00438       w[i++] = *s++;
00439       if (*s == ' ' || *s == '\t' || *s == '\n') {
00440         w[i] = '\0';
00441         words[nwords] = (char *)malloc((unsigned)(i+1));
00442         strcpy(words[nwords],w);
00443         nwords++;
00444         inword = 0;
00445       }
00446     }
00447     else {
00448       s++; /* *s++; */
00449       if (*s != ' ' && *s != '\t') {
00450         i = 0;
00451         inword = 1;
00452       }
00453     }
00454   }
00455   return(nwords);
00456 }
00457 
00458 /***********************************************************************
00459  * Read a List description file 
00460  **************************************************************************/
00461 
00462 
00463 void ReadListFile(listname,varlistname,ivar,stack_position)
00464      char *listname;
00465      char *varlistname;
00466      IVAR ivar;
00467      int stack_position ;
00468 {
00469   FILE *fin;
00470   char filin[MAXNAM];
00471   int nel;
00472   
00473   sprintf(filin,"%s.list",listname);
00474   fin = fopen(filin,"r");
00475   if (fin == 0) 
00476     {
00477       printf("description file for list or tlist \"%s\" does not exist\n",
00478              filin);
00479       exit(1);
00480     }
00481   printf("reading description file for list or tlist \"%s\"\n", listname);
00482   
00483   nel = 0;
00484   while(ReadListElement(fin,varlistname,ivar,nel,stack_position)) 
00485     {
00486       nel++;
00487     }
00488   
00489   fclose(fin);
00490 }
00491 
00492 int ReadListElement(f,varlistname,iivar,nel,stack_position)
00493      FILE *f;
00494      char *varlistname;
00495      int nel;
00496      IVAR iivar;
00497      int stack_position;
00498 {
00499   char s[MAXLINE];
00500   char *words[MAXLINE];
00501   int i, nline, nwords, type;
00502   IVAR ivar;
00503   char str[MAXNAM];
00504   nline = 0;
00505   while (fgets(s,MAXLINE,f) != NULL) 
00506     {
00507       /* analyse of one line */
00508       nline++;
00509       switch (nline) 
00510         {
00511         case 1:
00512           break;
00513         case 2:
00514           /* SCILAB variable description */
00515           nwords = ParseLine(s,words);
00516           sprintf(str,"%s(%s)",words[0],varlistname);
00517           ivar = GetVar(str,0);
00518           i = ivar - 1;
00519           variables[ivar-1]->stack_position =stack_position;
00520           if (nwords == 1) 
00521             {
00522               printf("type missing for variable \"%s\"\n",words[0]);
00523               exit(1);
00524             }
00525           type = GetBasType(words[1]);
00526           variables[i]->type = type;
00527           variables[i]->list_name = (char *)malloc((unsigned)(strlen(varlistname)+1));
00528           strcpy(variables[i]->list_name,varlistname);
00529           variables[i]->list_el = nel+1;
00530           switch (type) 
00531             {
00532             case SCALAR:
00533             case ANY:
00534               break;
00535             case COLUMN:
00536             case ROW:
00537             case STRING:
00538             case VECTOR:
00539               if (nwords != 3) 
00540                 {
00541                   printf("bad type for variable \"%s\"\n",
00542                          words[0]);
00543                   exit(1);
00544                 }
00545               if (isdigit(words[2][0]))
00546                 {
00547                   variables[i]->el[0] = GetVar(words[2],0);
00548                   variables[i]->length = 1;
00549                 }
00550               else 
00551                 {
00552                   sprintf(str,"%s(%s)",words[2],varlistname);
00553                   variables[i]->el[0] = GetVar(str,0);
00554                   variables[i]->length = 1;
00555                 }
00556               break;
00557             case POLYNOM:
00558             case MATRIX:
00559             case BMATRIX:
00560             case STRINGMAT:
00561               if (nwords != 4) 
00562                 {
00563                   printf("bad type for variable \"%s\"\n",
00564                          words[0]);
00565                   exit(1);
00566                 }         
00567               if (isdigit(words[2][0]))
00568                 {
00569                   variables[i]->el[0] = GetVar(words[2],0);
00570                   variables[i]->length = 1;
00571                 } 
00572               else
00573                 {
00574                   sprintf(str,"%s(%s)",words[2],varlistname);   
00575                   variables[i]->el[0] = GetVar(str,0);
00576                   variables[i]->length = 1;
00577                 }
00578               if (isdigit(words[3][0]))
00579                 {
00580                   variables[i]->el[1] = GetVar(words[3],0); 
00581                   variables[i]->length = 2;
00582                 }
00583               else 
00584                 {
00585                   sprintf(str,"%s(%s)",words[3],varlistname);   
00586                   variables[i]->el[1] = GetVar(str,0);
00587                   variables[i]->length = 2;
00588                 }
00589               break;
00590             case IMATRIX:
00591               if (nwords != 5) 
00592                 {
00593                   printf("bad type for variable \"%s\"\n",
00594                          words[0]);
00595                   exit(1);
00596                 }         
00597               if (isdigit(words[2][0]))
00598                 {
00599                   variables[i]->el[0] = GetVar(words[2],0);
00600                   variables[i]->length = 1;
00601                 } 
00602               else
00603                 {
00604                   sprintf(str,"%s(%s)",words[2],varlistname);   
00605                   variables[i]->el[0] = GetVar(str,0);
00606                   variables[i]->length = 1;
00607                 }
00608               if (isdigit(words[3][0]))
00609                 {
00610                   variables[i]->el[1] = GetVar(words[3],0); 
00611                   variables[i]->length = 2;
00612                 }
00613               else 
00614                 {
00615                   sprintf(str,"%s(%s)",words[3],varlistname);   
00616                   variables[i]->el[1] = GetVar(str,0);
00617                   variables[i]->length = 2;
00618                 }
00619               sprintf(str,"%s(%s)",words[4],varlistname);       
00620               variables[i]->el[2] = GetVar(str,0);
00621               variables[i]->length = 3;
00622               break;
00623             case SPARSE:
00624               if (nwords != 6) 
00625                 {
00626                   printf("bad type for variable \"%s\"\n",
00627                          words[0]);
00628                   exit(1);
00629                 }         
00630               if (isdigit(words[2][0]))
00631                 {
00632                   variables[i]->el[0] = GetVar(words[2],0);
00633                   variables[i]->length = 1;
00634                 } 
00635               else
00636                 {
00637                   sprintf(str,"%s(%s)",words[2],varlistname);   
00638                   variables[i]->el[0] = GetVar(str,0);
00639                   variables[i]->length = 1;
00640                 }
00641               if (isdigit(words[3][0]))
00642                 {
00643                   variables[i]->el[1] = GetVar(words[3],0); 
00644                   variables[i]->length = 2;
00645                 }
00646               else 
00647                 {
00648                   sprintf(str,"%s(%s)",words[3],varlistname);   
00649                   variables[i]->el[1] = GetVar(str,0);
00650                   variables[i]->length = 2;
00651                 }
00652               if (isdigit(words[4][0]))
00653                 {
00654                   variables[i]->el[2] = GetVar(words[4],0); 
00655                   variables[i]->length = 3;
00656                 }
00657               else 
00658                 {
00659                   sprintf(str,"%s(%s)",words[4],varlistname);   
00660                   variables[i]->el[2] = GetVar(str,0);
00661                   variables[i]->length = 3;
00662                 }
00663               sprintf(str,"%s(%s)",words[5],varlistname);       
00664               variables[i]->el[3] = GetVar(str,0);
00665               variables[i]->length = 4;
00666               break;
00667             case WORK:
00668             case SEQUENCE:
00669             case EMPTY:
00670             case LIST:
00671             case TLIST:
00672               printf("variable \"%s\" cannot have type \"%s\"\n",
00673                      words[0],SGetSciType(type));
00674               exit(1);
00675             default:
00676               printf("variable \"%s\" has unknown type \"%s\"\n",
00677                      words[0],SGetSciType(type));
00678             }
00679           break;
00680         default:
00681           /* end of description */
00682           if (s[0] == '*') 
00683             {
00684               return(1);
00685             }
00686           else
00687             {
00688               printf("bad description file for list or tlist \"%s\"\n",
00689                      varlistname);
00690               exit(1);
00691             }
00692           break;
00693         }
00694     }
00695   return(0);
00696 }
00697 

Generated on Sun Mar 4 15:03:57 2007 for Scilab [trunk] by  doxygen 1.5.1