1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/* unix-lib.c - make the newlisp shared newlisp library
Copyright (C) 2016 Lutz Mueller
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "newlisp.h"
#include "protos.h"
extern int bigEndian;
extern char preLoad[];
extern CELL * sysEvalString(char * str, SYMBOL * context, CELL * proc, int mode);
extern void setupAllSignals(void);
extern int evalSilent;
extern int opsys;
extern SYMBOL * mainArgsSymbol;
extern char linkOffset[];
extern FILE * IOchannel;
extern int newlispLibConsoleFlag;
int libInitialized = 0;
#ifdef MAC_OSX
#ifdef EMSCRIPTEN
#define LIBNAME "newlisp-js-lib.js"
#else
#define LIBNAME "newlisp.dylib"
#endif
#else
#define LIBNAME "newlisp.so"
#endif
void initializeMain(void)
{
#ifndef EMSCRIPTEN
char name[MAX_LINE];
char * initFile;
#endif
opsys += 64;
#ifdef SUPPORT_UTF8
opsys += 128;
#endif
#ifdef NEWLISP64
opsys += 256;
#endif
#ifdef FFI
opsys += 1024;
initFFI();
#endif
#ifdef EMSCRIPTEN
opsys += 2048;
#endif
bigEndian = (*((char *)&bigEndian) == 0);
initLocale();
initStacks();
initialize();
mainArgsSymbol->contents = (UINT)makeCell(CELL_EXPRESSION, (UINT)stuffString(LIBNAME));
/* setupAllSignals(); taken out for LIBRARY in 10.5.7 */
sysEvalString(preLoad, mainContext, nilCell, EVAL_STRING);
IOchannel = stdin;
#ifndef EMSCRIPTEN
initDefaultInAddr();
initFile = getenv("NEWLISPLIB_INIT");
if(initFile)
{
strncpy(name, initFile, MAX_LINE);
name[MAX_LINE - 1] = 0;
loadFile(name, 0, 0, mainContext);
}
#endif
libInitialized = 1;
reset();
}
extern STREAM errorStream;
STREAM libStrStream = {NULL, NULL, 0, 0, 0};
/* ---- imported and called from a client using newlisp.so ---- */
char * newlispEvalStr(char * cmd)
{
if(!libInitialized) initializeMain();
if(setjmp(errorJump))
{
/* setupAllSignals(); taken out for LIBRARY in 10.5.7 */
reset();
initStacks();
if(errorReg)
{
executeSymbol(errorEvent, NULL, NULL);
return(libStrStream.buffer);
}
else
return(errorStream.buffer);
}
openStrStream(&libStrStream, MAX_STRING, 1);
executeCommandLine(cmd, (UINT)&libStrStream, NULL);
if(evalSilent) evalSilent = 0;
return(libStrStream.buffer);
}
/* don't let stdout be included in return string */
int newlispLibConsole(int flag)
{
newlispLibConsoleFlag = flag;
return(flag);
}
#ifdef EMSCRIPTEN
/* for callbacks 'eval-string-js' is used see p_evalStringJS in newlisp.c */
#else
/* callbacks from newlisp library into caller
currently only tested with newLISP as caller:
(import "newlisp.dylib" "newlispEvalStr")
(import "newlisp.dylib" "newlispCallback")
(define (callme p1 p2 p3) (println "p1 => " p1 " p2 => " p2 " p3 => " p3) "hello world")
(newlispCallback "callme" (callback 0 'callme) "cdecl")
(get-string (newlispEvalStr {(get-string (callme 123 456 789))})) ; for string return
;(get-string (newlispEvalStr {(callme 123 456 789)})) ; for number return
*/
long newlispCallback(char * funcName, long funcAddr, char * callType)
{
CELL * pCell;
SYMBOL * symbol;
if(!libInitialized) initializeMain();
if(callType != NULL && strcmp(callType, "stdcall") == 0)
pCell = getCell(CELL_IMPORT_DLL);
else
pCell = getCell(CELL_IMPORT_CDECL);
symbol = translateCreateSymbol(funcName, pCell->type, currentContext, TRUE);
if(isProtected(symbol->flags))
{
errorProcExt2(ERR_SYMBOL_PROTECTED, stuffSymbol(symbol));
return(-1);
}
deleteList((CELL *)symbol->contents);
symbol->contents = (UINT)pCell;
pCell->contents = (UINT)funcAddr;
pCell->aux = (UINT)symbol->name;
return(funcAddr);
}
#endif
/* eof */
|