Example #1
0
main(int argc, char *argv[]){
#else
char **_environ;
main(int argc, char *argv[], char *env[]){
#endif
  extern unsigned char szCommandArray[] ;
  extern unsigned long ulStartNode,
                       ulNodeCounter,
                       ulStringTableSize,
                       ulGlobalVariables;
  void *pMEM;
  extern unsigned char szStringTable[];

  tConfigTree MyCONF;
  ExecuteObject MyEXE;
  int iError;
#define FULL_PATH_BUFFER_LENGTH 256
  char CmdLinBuffer[FULL_PATH_BUFFER_LENGTH],*s;
#ifndef WIN32
  _environ = env;
#endif

  pMEM = alloc_InitSegment(malloc,free);
  if( pMEM == NULL ){
    fprintf(stderr,"No memory\n");
    exit(1);
    }
  cft_start(&MyCONF,alloc_Alloc,alloc_Free,pMEM,
#ifdef WIN32
            "Software\\ScriptBasic\\config",
            "SCRIBA.INI",
#else
            "SCRIBACONF",
            "/etc/scriba/basic.conf",
#endif
            NULL);

  MyEXE.memory_allocating_function = malloc;
  MyEXE.memory_releasing_function = free;
  MyEXE.reportptr = (void *)stderr;
  MyEXE.report   = report_report;
  MyEXE.fErrorFlags = 0;

  MyEXE.pConfig = &MyCONF;
  build_MagicCode(&(MyEXE.Ver));

  build_MagicCode(&(MyEXE.Ver));
  MyEXE.fpStdinFunction = NULL;
  MyEXE.fpStdouFunction = NULL;
  MyEXE.fpEnvirFunction = NULL;
  MyEXE.CmdLineArgument = NULL;

  s = cft_GetString(MyEXE.pConfig,"maxstep");
  MyEXE.GlobalStepLimit = s ? atol(s) : 0;
  s = cft_GetString(MyEXE.pConfig,"maxlocalstep");
  MyEXE.LocalStepLimit  = s ? atol(s) : 0;
  s = cft_GetString(MyEXE.pConfig,"maxlevel");
  MyEXE.FunctionLevelLimit = s ? atol(s) : 0;

  MyEXE.CommandArray = (pcNODE)szCommandArray ;
  MyEXE.StartNode = ulStartNode;
  MyEXE.CommandArraySize = ulNodeCounter;
  MyEXE.StringTable = szStringTable;
  MyEXE.cbStringTable = ulStringTableSize;
  MyEXE.cGlobalVariables = ulGlobalVariables;
  MyEXE.lGlobalStepCounter = 0L;

  MyEXE.pCommandFunction = CommandFunction;
  MyEXE.CSymbolList = COMMANDSYMBOLS;
  MyEXE.fThreadedCommandTable = 0;
  MyEXE.pFunctionResult = NULL;

  MyEXE.pST = NULL;
  MyEXE.modules = NULL;

  MyEXE.pMemorySegment = alloc_InitSegment(MyEXE.memory_allocating_function,MyEXE.memory_releasing_function);

  if( MyEXE.pMemorySegment == NULL )return 1;


  MyEXE.pMo = alloc_Alloc(sizeof(MemoryObject),MyEXE.pMemorySegment);
  if( MyEXE.pMo == NULL )return 1;
  MyEXE.pMo->memory_allocating_function = MyEXE.memory_allocating_function;
  MyEXE.pMo->memory_releasing_function = MyEXE.memory_releasing_function;
  MyEXE.cbStringTable = 0L;
  MyEXE.OptionsTable = NULL;
  memory_InitStructure(MyEXE.pMo);

  s = cft_GetString(MyEXE.pConfig,"maxmem");
  if( s )alloc_SegmentLimit(MyEXE.pMo->pMemorySegment,atol(s));


  memory_RegisterTypes(MyEXE.pMo);
  if( hook_Init(&MyEXE, &(MyEXE.pHookers)) )return 1;
  if( modu_Preload(&MyEXE) )return 1;

/*
  We could alter the standard input, standard output, the environment
  function and command arguments here . We do only the command
  arguments here in this variation.
*/
  MyEXE.CmdLineArgument = CmdLinBuffer;  

  execute_Execute(&MyEXE,&iError);
  alloc_FinishSegment(MyEXE.pMo->pMemorySegment);
  alloc_FinishSegment(MyEXE.pMemorySegment);
  alloc_FinishSegment(MyCONF.pMemorySegment);
#ifdef _DEBUG
  printf("Press any key ...\n");
  getchar();
#endif
  exit(0);
  }
Example #2
0
/*FUNCTION*/
int ipreproc_LoadInternalPreprocessor(pPreprocObject pPre,
                                      char *pszPreprocessorName
  ){
/*noverbatim
The first argument is the pointer to the ScriptBasic preprocessor object to access the
configuration information and the list of loaded preprocessors to put the actual one
on the list.

The second argument is the name of the preprocessor as named in the configuration file, for example

=verbatim
preproc (
  internal (
    sample "C:\\ScriptBasic\\bin\\samplepreprocessor.dll"
    )
=noverbatim

The return value is zero or the error code.

CUT*/
#define FNLEN 1024
  char szBuffer[FNLEN];
  char *s;
  void *pDllHandle = NULL,*pFunction=NULL;
  int (*preproc)(void *,long *,void *);
  pSbProgram pProgram;
  int iError;
  pPreprocessor pThisPre;
  long lCommand;
  int bFirst;
#define PREFLEN 17
  char *pszDllExtension;
  unsigned int cbDllExtension;
  CFT_NODE Node;

  pProgram = pPre->pSB;

  pszDllExtension = cft_GetString(pProgram->pCONF,"dll");
  if( pszDllExtension == NULL ){
#ifdef WIN32
    pszDllExtension = ".dll";
#elif defined(__DARWIN__)
    pszDllExtension = ".dylib";
#elif defined(__MACOS__)
    pszDllExtension = "";
#else
    pszDllExtension = ".so";
#endif
    }
  cbDllExtension = strlen(pszDllExtension);

  /* check that the preprocessor was not loaded yet */
  for( pThisPre = pPre->pFirst ; pThisPre ; pThisPre = pThisPre->next )
    if( !strcmp(pThisPre->pszPreprocessorName,pszPreprocessorName) )return COMMAND_ERROR_SUCCESS;

  strcpy(szBuffer,"preproc.internal.");
  if( strlen(pszPreprocessorName) > FNLEN - PREFLEN )return READER_ERROR_PREPROC_LONG;
  strcpy(szBuffer+PREFLEN,pszPreprocessorName);
  s = szBuffer+PREFLEN;
  while( *s && ! isspace(*s) )s++; /* chop off optional parameters and/or NL from the end of line */
  *s = (char)0;
  s = cft_GetString(pProgram->pCONF,szBuffer);
  /* if the internal preprocessor was not configured then it still can be used if the DLL or SO file
     is copied into the modules library. */
  if( NULL == s ){
      if( ! cft_GetEx(pProgram->pCONF,"module",&Node,&s,NULL,NULL,NULL) ){
        while( 1 ){
          if( cft_GetEx(pProgram->pCONF,NULL,&Node,&s,NULL,NULL,NULL) ){
            /* if there are no more directories in the configuration */
            break;
            }
          if( ! strcmp(cft_GetKey(pProgram->pCONF,Node),"module") ){
            if( strlen(s) + strlen(pszPreprocessorName) > FNLEN )return READER_ERROR_PREPROC_LONG;
            strcpy(szBuffer,s);
            strcat(szBuffer,pszPreprocessorName);
            if( strlen(szBuffer) + cbDllExtension > FNLEN )return READER_ERROR_PREPROC_LONG;
            strcat(szBuffer,pszDllExtension);
            pDllHandle = dynlolib_LoadLibrary( szBuffer );
            if( pDllHandle != NULL )break;
            }
          Node = cft_EnumNext(pProgram->pCONF,Node);
          }
        }
    }else{
    /* if the preprocessor was configured in the config file
       not only copied into one of the module directories */
    pDllHandle = dynlolib_LoadLibrary(s);
    }
  if( pDllHandle == NULL )return READER_ERROR_PREPROC_NOTAVA;

  pFunction = dynlolib_GetFunctionByName(pDllHandle,"preproc");
  if( pFunction == NULL )return READER_ERROR_PREPROC_NOTVAL;

  bFirst = (pPre->pFirst == NULL);
  pThisPre = ipreproc_InsertPreprocessor(pPre);
  if( pThisPre == NULL  )return COMMAND_ERROR_MEMORY_LOW;
  pThisPre->pszPreprocessorName = alloc_Alloc(strlen(pszPreprocessorName)+1,pPre->pMemorySegment);
  if( pThisPre->pszPreprocessorName == NULL )return COMMAND_ERROR_MEMORY_LOW;

  strcpy(pThisPre->pszPreprocessorName,pszPreprocessorName);
  pThisPre->pDllHandle = pDllHandle;
  pThisPre->pFunction = pFunction;
  pThisPre->pEXT.lVersion = IP_INTERFACE_VERSION;
  pThisPre->pEXT.pPointer = NULL;
  pThisPre->pEXT.pMemorySegment = alloc_InitSegment(pPre->pSB->maf,pPre->pSB->mrf);
  if( pThisPre->pEXT.pMemorySegment == NULL )return COMMAND_ERROR_MEMORY_LOW;

  /* if this is the first preprocessor loaded then init
     the support function table*/
  if( bFirst ){
    pPre->EXE.pMemorySegment = pPre->pMemorySegment;
    modu_Init(&(pPre->EXE),0);
    pPre->EXE.pST->pEo = &(pPre->EXE);
    pThisPre->pEXT.pST = pPre->EXE.pST;
    }

  preproc = pFunction;
  lCommand = PreprocessorLoad;
  iError = preproc(&(pThisPre->pEXT),&lCommand,NULL);
  if( lCommand == PreprocessorUnload ){
    /* unload the current preprocessor */
    pDllHandle = pThisPre->pDllHandle;
    ipreproc_DeletePreprocessor(pPre,pThisPre);
    /* this may happen if the preprocessor is statically linked */
    if( pDllHandle )
      dynlolib_FreeLibrary(pDllHandle);
    }
  return iError;
  }