stndlone.c 4.5 KB
Newer Older
1 2 3 4 5
/* 

FILE:   stndlone.c
HEADER: stndlone.h

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Common MIT License - Jan 31, 2018

ScriptBasic Copyright (c) 2018 Peter Verhas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

TO_HEADER:

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "getopt.h"
#include "scriba.h"

#include "basext.h"

#if BCC32
char *_pgmptr;
#endif


static int libscriba_LoadBinaryProgram(pSbProgram pProgram
  ){
/*noverbatim
Before calling this function the function R<scriba_SetFileName()> should have been called specifying the
file name.
CUT*/
  pBuildObject pBuild;

  extern unsigned long ulGlobalVariables;
  extern unsigned long ulNodeCounter;
  extern unsigned long ulStartNode;
  extern unsigned long ulStringTableSize;
  extern unsigned char szCommandArray[];
  extern char szStringTable[];

  pProgram->pBUILD = alloc_Alloc( sizeof(BuildObject) , pProgram->pMEM);
  if( pProgram->pBUILD == NULL )return 1;

  pProgram->pBUILD->memory_allocating_function = pProgram->maf;
  pProgram->pBUILD->memory_releasing_function  = pProgram->mrf;
  pProgram->pBUILD->iErrorCounter = 0;
  pProgram->pBUILD->reportptr = pProgram->pReportPointer;
  pProgram->pBUILD->report   = pProgram->fpReportFunction;
  pProgram->pBUILD->fErrorFlags = pProgram->fErrorFlags;
  pBuild = pProgram->pBUILD;

  pBuild->pMemorySegment = alloc_InitSegment(pBuild->memory_allocating_function,
                                             pBuild->memory_releasing_function);
  if( pBuild->pMemorySegment == NULL ){
    return BU_ERROR_MEMORY_LOW;
    }

  pBuild->cGlobalVariables = ulGlobalVariables;
  pBuild->NodeCounter      = ulNodeCounter;
  pBuild->StartNode        = ulStartNode;
  pBuild->CommandArray     = (struct _cNODE *)szCommandArray;
  pBuild->cbStringTable    = ulStringTableSize;
  pBuild->StringTable      = szStringTable;
  pBuild->cbFTable         = 0;
  pBuild->FTable           = NULL;
  pBuild->cbVTable         = 0;
  pBuild->VTable           = NULL;
  return pProgram->pBUILD->iErrorCounter;
  }


int GetC(void *f){ return getc((FILE *)f); }

#ifdef _DEBUG
#define ERREXIT do{getchar();scriba_destroy(pProgram);exit(iError);}while(0)
#else
#define ERREXIT exit(iError)
#endif

#ifdef WIN32
stndlone(int argc, char *argv[]){
#else
  char **_environ;
stndlone(int argc, char *argv[], char *env[]){
#endif
  int iError;
  int iErrorCounter;
  pSbProgram pProgram;
  unsigned long fErrorFlags;
#define FULL_PATH_BUFFER_LENGTH 1024
  char CmdLinBuffer[FULL_PATH_BUFFER_LENGTH];
  extern int GlobalDebugDisplayFlag;
  int i;
  
  CmdLinBuffer[0] = (char)0;
  for( i=1 ; i < argc ; i++ ){
    strcat(CmdLinBuffer,argv[i]);
    if( i < argc-1 )strcat(CmdLinBuffer," ");
    }

/*pSbData p,psb;
SbData RetVale;*/
#ifndef WIN32
  _environ = env;
#endif

#if BCC32
  _pgmptr = argv[0];
#endif

#ifdef _DEBUG
#define malloc testa_Alloc
#define free testa_Free
  testa_InitSegment();
#endif
  /* default values for command line options */
#define pszForcedConfigurationFileName NULL
  GlobalDebugDisplayFlag = 0;

  pProgram = scriba_new(malloc,free);
  if( pProgram == NULL ){
    iError = COMMAND_ERROR_MEMORY_LOW;
    ERREXIT;
    }

  scriba_LoadConfiguration(pProgram,pszForcedConfigurationFileName);

  scriba_SetFileName(pProgram,argv[0]);

  if( (iError = libscriba_LoadBinaryProgram(pProgram)) != 0 ){
    ERREXIT;
    }
  if( iError=scriba_Run(pProgram,CmdLinBuffer) ){
    if( iError > 0 )
      report_report(stderr,"",0,iError,REPORT_ERROR,&iErrorCounter,NULL,&fErrorFlags);
    else
      iError = -iError;
    ERREXIT;
    }

  scriba_destroy(pProgram);
  exit(iError);
  }

MODLIST StaticallyLinkedModules[] ={
  { NULL, NULL },
  };