addinter.c

Go to the documentation of this file.
00001 /* Copyright ENPC */
00002 
00003 #include <string.h> 
00004 #include <stdio.h>
00005 #include "core_math.h"
00006 
00007 #include "../../../string/includes/men_Sutils.h"
00008 
00009 #include "link.h"
00010 #include "addinter.h" 
00011 #include "Os_specific.h"
00012 
00013 #include "stack-def.h"
00014 #include "MALLOC.h" /* MALLOC */
00015 #include "sciprint.h"
00016 #include "Funtab.h"
00017 #include "warningmode.h"
00018 
00019 #ifdef _MSC_VER
00020 #include "ExceptionMessage.h"
00021 #endif
00022 
00023 extern int C2F(namstr) __PARAMS((integer *id, integer *str, integer *n, integer *job));
00024 extern int C2F(error)  __PARAMS((integer *n));  
00025 extern void GetenvB __PARAMS(( char *name,char *env, int len));
00026 
00027 #include <stdlib.h>
00028 
00029 #define OK 1
00030 #define FAIL 0
00031 
00032 #define MAXINTERF 50
00033 #define INTERFSIZE 25 
00034 
00035 #define debug C2F(iop).ddt==1
00036 
00037 typedef struct 
00038 {
00039   char name[INTERFSIZE]; 
00040   void (*func)();        
00041   int Nshared; 
00042   int ok;    
00043 } Iel;
00044 
00045 Iel DynInterf[MAXINTERF];
00046 static int LastInterf=0;
00047 static void SciInterInit(void);
00048 static void DynFuntab __PARAMS((int *Scistring,int *ptrstrings,int *nstring,int k1));
00049 static void ShowInterf(void);
00050 
00051 
00052 int Use_cpp_code;
00053 char * Use_c_cpp;
00054 
00055 /************************************************
00056  * Dynamically added interface to Scilab 
00057  ************************************************/
00058 
00059 void C2F(addinter)(int *iflag,int *descla, int *ptrdescla, int *nvla, char *iname,
00060                    int *desc, int *ptrdesc, int *nv, char *c_cpp, 
00061                    int *lib_cpp, int *err)
00062      /* files (int *descla, int *ptrdescla, int *nvla) */
00063      /* interface name iname */
00064      /* functions name : desc, ptrdesc, nv */
00065      /* for cpp library: c_cpp */
00066      /* C++ compiler lib_cpp  */
00067      /* the files are given by a previously loaded libary with number iflag  */ 
00068      /* (if iflag == -1 (int *descla, int *ptrdescla, int *nvla) are used ) */
00069 {
00070   int i,rhs=2,ilib=0,inum;
00071   char **files,*names[2];
00072   *err=0;
00073   files=NULL; /* Initialisation pour Windows */
00074 
00075   Use_cpp_code=*lib_cpp;
00076   Use_c_cpp = (char *) MALLOC((strlen(c_cpp) +1) * sizeof(char));
00077   strcpy(Use_c_cpp,c_cpp);
00078 
00079   if ( *iflag == -1 ) 
00080     {
00081       ScilabMStr2CM(descla,nvla,ptrdescla,&files,err);
00082       if ( *err == 1) return;
00083     }
00084 
00085   names[0]=iname;
00086   names[1]=(char *)0;
00087 
00088   SciLinkInit();
00089   SciInterInit();
00090 
00093   for ( i = 0 ; i < LastInterf ; i++) 
00094     {
00095       if (strcmp(iname,DynInterf[i].name)==0) 
00096         {
00098           if ( LinkStatus() == 1) 
00099             {
00100               C2F(isciulink)(&DynInterf[i].Nshared);
00101             }
00102           break;
00103         }
00104     }
00105 
00107   inum=-1;
00108   for ( i = 0 ; i < LastInterf ; i++) 
00109     {
00110       if ( DynInterf[i].ok == 0 ) inum= i;
00111     }
00112   inum = ( inum == -1 ) ? LastInterf : inum ;
00113 
00116   if ( inum >=  MAXINTERF ) 
00117     {
00118       /* sciprint("Maximum number of dynamic interfaces %d\r\n",MAXINTERF);
00119          sciprint("has been reached\r\n");*/
00120       *err=1;
00121       return;
00122     }
00123 
00124   if ( *iflag == -1 ) { 
00125     /* link then search  */ 
00126     SciLink(0,&rhs,&ilib,files,names,"f");
00127     if ( ilib < 0 ) 
00128       {
00129         *err=ilib;  return;
00130       }
00131   } else {
00132     /* search in already loaded library number ilib */ 
00133     ilib = Max(*iflag,0);
00134     SciLink(1,&rhs,&ilib,files,names,"f");
00135     if ( ilib < 0 ) 
00136       {
00137         *err=ilib;  return;
00138       }
00139   }
00140 
00142   DynInterf[inum].Nshared = ilib;
00143 
00144   if ( SearchInDynLinks(names[0],&DynInterf[inum].func) < 0 ) 
00145     {
00146       /*sciprint("addinter failed for %s Not  found!\r\n",iname);*/
00147       *err=2;
00148       return;
00149     }
00150   else
00151     {
00152       strncpy(DynInterf[inum].name,iname,INTERFSIZE);
00153       DynInterf[inum].ok = 1;
00154     }
00155   if ( inum == LastInterf ) LastInterf++;
00156 
00160   DynFuntab(desc,ptrdesc,nv,inum+1);
00161 
00162   if ( *iflag == -1 ) {
00163     for (i=0;i< *nvla;i++) FREE(files[i]); FREE(files);
00164   }
00165   ShowInterf();
00166 }
00167 
00168 
00169 static void SciInterInit(void)
00170 {
00171   static int first_entry=0;
00172   if ( first_entry == 0) 
00173     {
00174       int i;
00175       for ( i= 0 ; i < MAXINTERF ; i++) 
00176         DynInterf[i].ok=0;
00177       first_entry++;
00178     }
00179 }
00180 
00181 /*********************************
00182  * unlink shared hp only 
00183  *********************************/
00184 
00185 #if (defined(hppa))
00186 
00187 extern int Call_shl_load;
00188 
00189 void hppa_sci_unlink_shared()
00190 {
00191   if ((LinkStatus() == 1) && (Call_shl_load))
00192       C2F(isciulink)(&DynInterf[0].Nshared);
00193 }
00194 
00195 #endif
00196 
00197 /*********************************
00198  * used in C2F(isciulink)(i) 
00199  *********************************/
00200 
00201 void RemoveInterf(int Nshared)
00202 {
00203   int i;
00204   for ( i = 0 ; i < LastInterf ; i++ ) 
00205     {
00206       if ( DynInterf[i].Nshared == Nshared ) 
00207         {
00208           DynInterf[i].ok = 0;
00209           break;
00210         }
00211     }
00212 }
00213 
00214 /*********************************
00215  * show the interface table 
00216  *********************************/
00217 
00218 static void ShowInterf(void)
00219 {
00220   int i;
00221   for ( i = 0 ; i < LastInterf ; i++ ) 
00222     {
00223       if ( DynInterf[i].ok == 1 ) 
00224         if (debug) sciprint("Interface %d %s\r\n",i,DynInterf[i].name);
00225     }
00226 }
00227 
00228 #define nsiz 6 
00229 
00230 /************************************************
00231  * add the set of functions associated to 
00232  *   dynamically added interface k1 
00233  *   in scilab function table with id 10000*k1+i 
00234  ************************************************/
00235 
00236 
00237 static void DynFuntab(int *Scistring, int *ptrstrings, int *nstring, int k1)
00238 {
00239   int id[nsiz],zero=0,trois=3,fptr,fptr1,quatre=4;
00240   int li=1,ni,*SciS,i;
00241   SciS= Scistring;
00242   for ( i=1 ; i < *nstring+1 ; i++) 
00243     {
00244       ni=ptrstrings[i]-li;
00245       li=ptrstrings[i];
00246       C2F(namstr)(id,SciS,&ni,&zero);
00247       fptr1= fptr= (DynInterfStart+k1)*100 +i;
00248       C2F(funtab)(id,&fptr1,&quatre,"NULL_NAME",0); /* clear previous def set fptr1 to 0*/
00249       C2F(funtab)(id,&fptr,&trois,"NULL_NAME",0);  /* reinstall */
00250       SciS += ni;
00251     }
00252 }
00253 
00254 /************************************************
00255  * Used when one want to call a function added 
00256  * with addinterf the dynamic interface number 
00257  * is given by k1=(*k/100)-1
00258  ************************************************/
00259 
00260 void C2F(userlk)(integer *k)
00261 {
00262   int k1 = *k - (DynInterfStart+1) ;
00263   int imes = 9999;
00264   if ( k1 >= LastInterf || k1 < 0 ) 
00265     {
00266       if (getWarningMode()) sciprint("Invalid interface number %d",k1);
00267       C2F(error)(&imes);
00268       return;
00269     }
00270   if ( DynInterf[k1].ok == 1 ) 
00271   {
00272         #ifdef _MSC_VER
00273           #ifndef _DEBUG
00274                 _try
00275                 {
00276                         (*DynInterf[k1].func)();
00277                 }
00278                 _except (EXCEPTION_EXECUTE_HANDLER)
00279                 {
00280                         ExceptionMessage(GetExceptionCode(),DynInterf[k1].name);
00281                 }
00282           #else
00283                 (*DynInterf[k1].func)();
00284           #endif
00285         #else
00286           (*DynInterf[k1].func)();
00287         #endif
00288   }
00289   else 
00290     {
00291       if (getWarningMode()) sciprint("Interface %s not linked\r\n",DynInterf[k1].name);
00292       C2F(error)(&imes);
00293       return;
00294     }
00295 }
00296 
00297 
00298 /******************************************************
00299  * Test for scilab library loaded when needed 
00300  * similar to addinter but we do not add the function list 
00301  * in fundef since it is already present 
00302  * sometimes we need to link several interfaces with the same code 
00303  ******************************************************/
00304 
00305 int  SciLibLoad(int num_names, char **names, char **files, int *nums, int *err)
00306 {
00307   int i,rhs=2,inum,ilib=0,j;
00308   SciLinkInit();
00309   SciInterInit();
00310   *err=0;
00311   
00312   for ( j=0 ; j < num_names ; j++) 
00313     {
00315       inum=-1;
00316       for ( i = 0 ; i < LastInterf ; i++) 
00317         {
00318           if ( DynInterf[i].ok == 0 ) inum= i;
00319         }
00320       inum = ( inum == -1 ) ? LastInterf : inum ;
00322       if ( inum >=  MAXINTERF ) 
00323         {
00324           if (getWarningMode()) sciprint("Maximum number of dynamic interfaces %d\r\n",MAXINTERF);
00325           if (getWarningMode()) sciprint("has been reached\r\n");
00326           *err=1;
00327           return -1 ;
00328         }
00329       else 
00330         nums[j]=inum;
00331       if ( inum == LastInterf ) LastInterf++;
00332     }
00333   SciLink(0,&rhs,&ilib,files,names,"f");
00334   if ( ilib < 0 ) 
00335     {
00336       *err=1;  return -1;
00337     }
00340   for ( j=0 ; j < num_names ; j++) 
00341     {
00342       DynInterf[nums[j]].Nshared = ilib;
00343       if ( SearchInDynLinks(names[0],&DynInterf[nums[j]].func) < 0 ) 
00344         {
00345           if (getWarningMode()) sciprint("addinter failed for %s Not  found!\r\n",names[j]);
00346           return -1;
00347         }
00348       else
00349         {
00350           strncpy(DynInterf[nums[j]].name,names[j],INTERFSIZE);
00351           DynInterf[nums[j]].ok = 1;
00352         }
00353     }
00354   ShowInterf();
00355   return 0;
00356 }
00357 
00358 #define MAX_ENV 256 
00359 
00360 void BuildName(char *name, char *str)
00361 {
00362   int  nc= MAX_ENV;
00363   GetenvB("SCI",name,nc);
00364   strcat(name,"/libs/");
00365   strcat(name,str);
00366 }
00367 
00368 void CallDynInterf(int *pos, int num_names, int namepos, char **names,
00369                   int *nums, char **files)
00370 {
00371   int imes = 9999;
00372   if ( *pos == -1 || DynInterf[*pos].ok == 0) 
00373     {
00375       int err=0;
00376       SciLibLoad(num_names,names,files,nums,&err);
00377       if (err != 1) *pos = nums[namepos];
00378     }
00379   if ( DynInterf[*pos].ok == 1 ) 
00380     (*DynInterf[*pos].func)();
00381   else 
00382     {
00383       if (getWarningMode()) sciprint("Interface %s not linked\r\n",DynInterf[*pos].name);
00384       C2F(error)(&imes);
00385     }
00386 }  
00387 
00388 
00389 
00390 
00391 
00392 
00393 

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