link.c

Go to the documentation of this file.
00001 /* Copyright INRIA/ENPC */
00002 
00003 /********************************************* 
00004  *   Scilab link functions    
00005  ******************************************/ 
00006 #ifdef sgi
00007 #define _KMEMUSER 1
00008 #endif
00009 
00010 #include <string.h> 
00011 #include <stdio.h>
00012 
00013 #include "core_math.h"
00014 #include "MALLOC.h" /* MALLOC */
00015 #include "../../../string/includes/men_Sutils.h"
00016 #include "link.h"
00017 #include "sciprint.h"
00018 
00019 #include <stdlib.h>
00020 
00021 #include "machine.h"
00022 #include "stack-c.h"
00023 #include "warningmode.h"
00024 
00025 static void Underscores __PARAMS((int isfor,char *ename,char *ename1));
00026 static int SearchFandS  __PARAMS( ( char *,int ));
00027 int LinkStatus __PARAMS((void)) ;
00028 int C2F(scilinknorhs)(void);
00029 void C2F(iislink)(char *buf, integer *irep);
00030 void ulinkall(void);
00031 void C2F(iscilink)(int *descla, int *ptrdescla, int *nvla, int *desc, int *ptrdesc, int *nv, char *strf, int *ilib, int *iflag, int *rhs);
00032 /*********************************************
00033  * Structure to keep the entry points 
00034  *********************************************/
00035 
00036 #define OK 1
00037 #define FAIL 0
00038 
00039 
00040 #define ENTRYMAX 5000
00041 #define MAXNAME  256 
00042 
00043 typedef void (*function) ();
00044 
00045 typedef char Name[MAXNAME];   /* could be changed to dynamic structure */
00046 
00047 typedef struct { 
00048   function epoint;            /* the entry point */ 
00049   Name     name;              /* entry point name */
00050   int      Nshared;           /* number of the shared file */
00051 } Epoints;
00052 
00053 #define TMPL 256
00054 
00055 typedef struct {
00056   int ok;
00057   char tmp_file[TMPL];
00058   unsigned long  shl;
00059 } Hd;
00060 
00061 static Hd  hd[ENTRYMAX]; /* shared libs handler */
00062 static int Nshared  = 0   ;
00063 static Epoints EP[ENTRYMAX];  /* entryPoints */
00064 static int NEpoints = 0   ;        /* Number of Linked names */
00065 
00066 /*-----------------------------------------------------------------------------------*/
00067 int C2F(scilinknorhs)()
00068 {
00069         int i=0;
00070         static int l1,n1,m1;
00071         char **ReturnArrayString=NULL;
00072         int j=0;
00073         m1=NEpoints;
00074         n1=1;
00075 
00076         if (NEpoints)
00077         {
00078                 ReturnArrayString = (char **) MALLOC(NEpoints*sizeof(char **));
00079 
00080                 for ( i = NEpoints-1 ; i >=0 ; i--) 
00081                 {
00082                         char *EntryName=(char *)MALLOC(strlen(EP[i].name)*sizeof(char));
00083                         if ( hd[i].ok == OK) 
00084                         {
00085                                 sprintf(EntryName,"%s",EP[i].name);
00086                                 ReturnArrayString[j]=EntryName;
00087                                 j++;
00088                         }
00089                 }
00090 
00091                 CreateVarFromPtr(Rhs+1, "S", &n1, &m1, ReturnArrayString);
00092 
00093                 LhsVar(1)=Rhs+1;
00094                 C2F(putlhsvar)();
00095 
00096                 for (i=0;i<NEpoints;i++)
00097                 {
00098                         if (ReturnArrayString[i])
00099                         {
00100                                 FREE(ReturnArrayString[i]);
00101                                 ReturnArrayString[i]=NULL;
00102                         }
00103                 }
00104                 FREE(ReturnArrayString);
00105         }
00106         else
00107         {
00108                 m1=0;
00109                 n1=0;
00110                 l1=0;
00111                 CreateVar(Rhs+1,"d",  &m1, &n1, &l1);
00112                 LhsVar(1)=Rhs+1;
00113                 C2F(putlhsvar)();
00114         }
00115 
00116 
00117         return 0;
00118 }
00119 /*-----------------------------------------------------------------------------------*/
00120 
00124 /************************************************
00125  * Link facilities 
00126  * Interface to work with Scilab 
00127  * descla,ptrdescla,nvla : string matrix if *iflag == 0 
00128  * desc,ptrdesc,nv       : string matrix ,
00129  * string  strf          : "f" OR "c" 
00130  * ilib                  : integer (out/in value )
00131  * iflag                 : 0 a string matrix is used 1 ilib is used
00132  * rhs                   : number of rhs arguments in link(...)
00133  ************************************************/
00134 
00135 void C2F(iscilink)(int *descla, int *ptrdescla, int *nvla, int *desc, int *ptrdesc, int *nv, char *strf, int *ilib, int *iflag, int *rhs)
00136                                      /* files */
00137                                      /* entry points */
00138                 
00139                                                  
00140 {
00141   int ierr,i;
00142   char **files=NULL,**en_names=NULL;
00143   ierr=0;
00144   en_names=NULL; /* Initialisation variable pour Windows */
00145 
00146   if ( *iflag == 0) 
00147     {
00148       ScilabMStr2CM(descla,nvla,ptrdescla,&files,&ierr);
00149       if ( ierr == 1) return;
00152     }
00153   if ( *rhs >= 2) 
00154     {
00155       ScilabMStr2CM(desc,nv,ptrdesc,&en_names,&ierr);
00156       if ( ierr == 1) return;
00159     }
00160 
00161   SciLinkInit();
00162   
00163   if ( *iflag== 0 &&  strncmp(files[0],"show",4)==0) 
00164     {
00165       ShowDynLinks();
00166       *ilib = LinkStatus();  /* return value for Scilab */
00167       return;
00168     }
00169 
00172   SciLink(*iflag,rhs,ilib,files,en_names,strf);
00173 
00174   if ( *rhs >= 2) 
00175     {
00176       for (i=0;i< *nv;i++) FREE(en_names[i]); FREE(en_names);
00177     }
00178 
00179   if ( *iflag == 0) 
00180     {
00181       for (i=0;i< *nvla;i++) FREE(files[i]); FREE(files);
00182     }
00183   if (*ilib >= 0) 
00184   {
00185           if (getWarningMode()) sciprint("Link done\r\n");
00186   }
00187 }
00188 
00189 
00190 #if defined(netbsd) || defined(freebsd) || defined(sun) || defined(__alpha) || defined(sgi) || (!defined(hppa_old) && defined(hppa))  || defined(__APPLE__)
00191 #include "link_SYSV.c"
00192 #else
00193 
00194 #if (defined(sun_old) ||  (defined(mips) && !defined(netbsd)) || defined(_IBMR2) || defined(hppa_old)) && !defined(linux)
00195 #ifdef SUNOSDLD 
00196 #include "link_linux.c"
00197 #else 
00198 #include "link_std.c"
00199 #endif /* end of SUNOSDLD */
00200 #else
00201 #if defined(linux)
00202 #ifdef __ELF__
00203 #include "link_SYSV.c"
00204 #else
00205 #include "link_linux.c"
00206 #endif /* end of __ELF__ */
00207 #else
00208 #if defined(_MSC_VER)
00209 #include "link_W95.c"
00210 #else
00211 
00215 int LinkStatus()
00216 {
00217   return(0);
00218 }
00219 
00220 void SciLink(iflag,rhs,ilib,files,en_names,strf)
00221      int iflag,*ilib,*rhs;
00222      char *files[],*en_names[],*strf;
00223 {
00224   if (getWarningMode()) sciprint("Sorry : Dynamic linking is not implemented  \r\n");
00225 }
00226 
00227 
00228 void C2F(isciulink)(i) 
00229      integer *i;
00230 {
00231   if (getWarningMode()) sciprint("Sorry : Unlinking is not implemented \r\n");
00232 }
00233 
00234 #endif
00235 #endif
00236 #endif 
00237 #endif 
00238 
00239 #ifdef WLU
00240 #ifndef DLDLINK
00241 #ifndef _MSC_VER
00242 #ifndef __APPLE_CC__
00243 #define WLU1 /* dld will add the leading _ itself, win32 too, dlcompat too */
00244 #endif
00245 #endif 
00246 #endif
00247 #endif 
00248 
00249 /********************************************
00250  * Underscores : deals with the trailing _ 
00251  * in entry names 
00252  ********************************************/
00253 
00254 static void Underscores(int isfor, char *ename, char *ename1)
00255 {
00256 #ifdef WLU1
00257   *ename1='_'; ename1++;
00258 #endif
00259   strcpy(ename1,ename);
00260 #ifdef WTU
00261   if (isfor==1) strcat(ename1,"_");
00262 #endif
00263   return;
00264 }
00265 
00266 /**************************************
00267  * Initialize tables 
00268  *************************************/
00269 
00270 void SciLinkInit(void)
00271 {
00272   static int first_entry = 0;
00273   int i;
00274   if ( first_entry == 0)
00275     {
00276       
00277       for ( i = 0 ; i < ENTRYMAX ; i++) 
00278         {
00279           hd[i].ok= FAIL;
00280           EP[i].Nshared = -1;
00281         }
00282       first_entry++;
00283     }
00284 }
00285 
00286 /**************************************
00287  * if *irep == -1 
00288  *    checks if buf is a loaded
00289  *    entry point 
00290  *    the result is -1 if false 
00291  *               or the number in the function table 
00292  * 
00293  *    
00294  * if *irep != -1 : 
00295  *    checks if buf is a loaded
00296  *    entry point from shared lib *irep
00297  *    the result is -1 if false 
00298  *               or the number in the function table 
00299  * 
00300  *************************************/
00301 
00302 void C2F(iislink)(char *buf, integer *irep)
00303 {
00304   void (*loc)();
00305   if ( *irep != -1 ) 
00306     *irep=SearchFandS(buf,*irep);
00307   else
00308     *irep=SearchInDynLinks(buf,&loc);
00309 }
00310 
00311 
00312 /**************************************
00313  * returns the ii functions 
00314  *************************************/
00315 
00316 void GetDynFunc(int ii, void (**realop) (/* ??? */))
00317 {
00318   if ( EP[ii].Nshared != -1 ) 
00319     *realop = EP[ii].epoint;
00320   else
00321     *realop = (function) 0;
00322 }
00323 
00324 /**************************************
00325  * Search a function in the table 
00326  * Search from end to top 
00327  *************************************/
00328 
00329 int SearchInDynLinks(char *op, void (**realop) (/* ??? */))
00330 {
00331   int i=0;
00332   for ( i = NEpoints-1 ; i >=0 ; i--) 
00333     {
00334       if ( strcmp(op,EP[i].name) == 0) 
00335         {
00336            *realop = EP[i].epoint;
00337            return(EP[i].Nshared );
00338          }
00339     }
00340   return(-1);
00341 }
00342 
00343 
00344 /**************************************
00345  * Search a (function,libid) in the table 
00346  * Search from end to top 
00347  *************************************/
00348 
00349 static int SearchFandS(char *op, int ilib)
00350 {
00351   int i=0;
00352   for ( i = NEpoints-1 ; i >=0 ; i--) 
00353     {
00354       if ( strcmp(op,EP[i].name) == 0 && EP[i].Nshared == ilib)
00355         {
00356            return(i);
00357          }
00358     }
00359   return(-1);
00360 }
00361 
00362 /**************************************
00363  * Show the linked files 
00364  *************************************/
00365 
00366 void  ShowDynLinks(void)
00367 {
00368   int i=0,count=0;
00369   if (getWarningMode()) sciprint("Number of entry points %d\r\n",NEpoints);
00370   if (getWarningMode()) sciprint("Shared libs : [");
00371   for ( i = 0 ; i < Nshared ; i++) 
00372     if ( hd[i].ok == OK) { if (getWarningMode())sciprint("%d ",i);count++;}
00373   if (getWarningMode()) sciprint("] : %d libs\r\n",count);
00374   for ( i = NEpoints-1 ; i >=0 ; i--) 
00375     {
00376       if (getWarningMode()) sciprint("Entry point %s in shared lib %d\r\n",EP[i].name,EP[i].Nshared);
00377     }
00378 }
00379 
00380 void ulinkall(void)
00381 {
00382         int i=0;
00383         
00384         for ( i = 0 ; i < Nshared ; i++)
00385         {
00386                 if ( hd[i].ok == OK) Sci_Delsym(i);
00387         }
00388     
00389 }

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