-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewlisp.c
7483 lines (6252 loc) · 177 KB
/
newlisp.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* newlisp.c --- entry point and main functions for newLISP
Copyright (C) 2020 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"
#include "primes.h"
#ifdef WINDOWS
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
#ifdef READLINE
#include <readline/readline.h>
/* take following line out on Slackware Linux */
#include <readline/history.h>
#endif /* end READLINE */
#ifdef SUPPORT_UTF8
#include <wctype.h>
#endif
#define freeMemory free
#define INIT_FILE "init.lsp"
#ifdef WINDOWS
#define fprintf win_fprintf
#define fgets win_fgets
#define fclose win_fclose
#endif
#ifdef LIBRARY
extern STREAM libStrStream;
int newlispLibConsoleFlag = 0;
#endif
#if defined(LINUX) || defined(KFREEBSD)
#ifdef ANDROID
int opsys = 11;
#else
int opsys = 1;
#endif
#endif
#ifdef _BSD
int opsys = 2;
#endif
#ifdef MAC_OSX
#ifdef EMSCRIPTEN
int opsys = 11;
#else
int opsys = 3;
#endif
#endif
#ifdef SOLARIS
int opsys = 4;
#endif
#ifdef WINDOWS
int opsys = 6;
#endif
#ifdef OS2
int opsys = 7;
#endif
#ifdef CYGWIN
int opsys = 8;
#endif
#ifdef TRU64
int opsys = 9;
#endif
#ifdef AIX
int opsys = 10;
#endif
/* opsys = 11 taken for ANDROID; see LINUX */
int bigEndian = 1; /* gets set in main() */
int version = 10706;
char copyright[]=
"\nnewLISP v.10.7.6 Copyright (c) 2020 Lutz Mueller. All rights reserved.\n\n%s\n\n";
#ifndef NEWLISP64
#ifdef SUPPORT_UTF8
char banner[]=
"newLISP v.10.7.6 32-bit on %s IPv4/6 UTF-8%s%s\n\n";
#else
char banner[]=
"newLISP v.10.7.6 32-bit on %s IPv4/6%s%s\n\n";
#endif
#else /* NEWLISP64 */
#ifdef SUPPORT_UTF8
char banner[]=
"newLISP v.10.7.6 64-bit on %s IPv4/6 UTF-8%s%s\n\n";
#else
char banner[]=
"newLISP v.10.7.6 64-bit on %s IPv4/6%s%s\n\n";
#endif
#endif /* NEWLISP64 */
char banner2[]= ", options: newlisp -h";
void linkSource(char *, char *, char *);
char linkOffset[] = "&&&&@@@@";
char preLoad[] =
#ifdef EMSCRIPTEN
"(set (global 'module) (fn ($x) (load (append {/newlisp-js/} $x))))"
#else
"(set (global 'module) (fn ($x) (load (append (env {NEWLISPDIR}) {/modules/} $x))))"
#endif
"(context 'Tree) (constant 'Tree:Tree) (context MAIN)"
"(define (Class:Class) (cons (context) (args)))";
void printHelpText(void);
#ifdef READLINE
char ** newlisp_completion (char * text, int start, int end);
#endif
/* --------------------- globals -------------------------------------- */
/* interactive command line */
int isTTY = FALSE;
int daemonMode = 0;
int noPromptMode = 0;
int forcePromptMode = 0;
int httpMode = 0;
int httpSafe = 0;
int evalSilent = 0;
#ifdef WINDOWS
int IOchannelIsSocketStream = 0;
#endif
FILE * IOchannel;
char * IOdomain = NULL;
int IOport = 0;
int connectionTimeout = 0;
int logTraffic = 0;
#define LOG_LESS 1
#define LOG_MORE 2
/* initialization */
int MAX_CPU_STACK = 0x800;
int MAX_ENV_STACK;
int MAX_RESULT_STACK;
#define MAX_OBJECT_STACK 64
#ifndef NEWLISP64
INT MAX_CELL_COUNT = 0x10000000;
#else
INT MAX_CELL_COUNT = 0x800000000000000LL;
#endif
INT blockCount = 0;
CELL * firstFreeCell = NULL;
CELL * nilCell;
CELL * trueCell;
CELL * lastCellCopied;
CELL * countCell;
SYMBOL * nilSymbol;
SYMBOL * trueSymbol;
SYMBOL * starSymbol;
SYMBOL * plusSymbol;
SYMBOL * questionSymbol;
SYMBOL * atSymbol;
SYMBOL * currentFunc;
SYMBOL * argsSymbol;
SYMBOL * mainArgsSymbol;
SYMBOL * listIdxSymbol;
SYMBOL * itSymbol;
SYMBOL * sysxSymbol;
SYMBOL * countSymbol;
SYMBOL * beginSymbol;
SYMBOL * expandSymbol;
SYMBOL * sysSymbol[MAX_REGEX_EXP];
SYMBOL * currentContext = NULL;
SYMBOL * mainContext = NULL;
SYMBOL * errorEvent;
SYMBOL * timerEvent;
SYMBOL * promptEvent;
SYMBOL * commandEvent;
SYMBOL * transferEvent;
SYMBOL * readerEvent;
SYMBOL * symHandler[32];
int currentSignal = 0;
SYMBOL * symbolCheck = NULL;
CELL * stringCell = NULL;
void * stringIndexPtr = NULL;
jmp_buf errorJump;
char lc_decimal_point;
/* error and exception handling */
#define EXCEPTION_THROW -1
int errorReg = 0;
CELL * throwResult;
/* buffers for read-line and error reporting */
STREAM readLineStream;
STREAM errorStream;
/* compiler */
size_t cellCount = 0;
size_t symbolCount = 0;
int parStackCounter = 0;
/* expression evaluation */
static CELL * (*evalFunc)(CELL *) = NULL;
UINT * envStack = NULL;
UINT * envStackIdx;
UINT * envStackTop;
UINT * resultStack = NULL;
UINT * resultStackIdx;
UINT * resultStackTop;
UINT * lambdaStack = NULL;
UINT * lambdaStackIdx;
/* internal dummy to carry FOOP object */
SYMBOL objSymbol = {SYMBOL_GLOBAL | SYMBOL_BUILTIN,
0, "container of (self)", 0, NULL, NULL, NULL, NULL};
CELL * objCell;
extern PRIMITIVE primitive[];
/* debugger in nl-debug.c */
extern char debugPreStr[];
extern char debugPostStr[];
extern CELL * debugPrintCell;
int traceFlag = 0;
int evalCatchFlag = 0;
int recursionCount = 0;
int prettyPrintPars = 0;
int prettyPrintCurrent = 0;
int prettyPrintFlags = 0;
int prettyPrintLength = 0;
char * prettyPrintTab = " ";
char * prettyPrintFloat = "%1.16g";
#define MAX_PRETTY_PRINT_LENGTH 80
UINT prettyPrintMaxLength = MAX_PRETTY_PRINT_LENGTH;
int stringOutputRaw = TRUE;
#define pushLambda(A) (*(lambdaStackIdx++) = (UINT)(A))
int pushResultFlag = TRUE;
char startupDir[PATH_MAX]; /* start up directory, if defined via -w */
char * tempDir; /* /tmp on unix or geten("TMP") on Windows */
char logFile[PATH_MAX]; /* logFile, is define with -l, -L */
/* nl-filesys.c */
int pagesize;
/* ============================== MAIN ================================ */
#ifndef EMSCRIPTEN
/*
void setupSignalHandler(int sig, void (* handler)(int))
{
static struct sigaction sig_act;
sig_act.sa_handler = handler;
sigemptyset(&sig_act.sa_mask);
sig_act.sa_flags = SA_RESTART | SA_NOCLDSTOP;
if(sigaction(sig, &sig_act, 0) != 0)
printf("Error setting signal:%d handler\n", sig);
}
*/
void setupSignalHandler(int sig, void (* handler)(int))
{
if(signal(sig, handler) == SIG_ERR)
printf("Error setting signal:%d handler\n", sig);
}
#if defined(SOLARIS) || defined(TRU64) || defined(AIX)
void sigpipe_handler(int sig)
{
setupSignalHandler(SIGPIPE, sigpipe_handler);
}
void sigchld_handler(int sig)
{
waitpid(-1, (int *)0, WNOHANG);
}
void ctrlC_handler(int sig)
{
char chr;
setupSignalHandler(SIGINT, ctrlC_handler);
traceFlag |= TRACE_SIGINT;
printErrorMessage(ERR_SIGINT, NULL, 0);
printf("%s", "(c)ontinue, e(x)it, (r)eset:");
fflush(NULL);
chr = getchar();
if(chr == 'x') exit(1);
if(chr == 'c') traceFlag &= ~TRACE_SIGINT;
}
void sigalrm_handler(int sig)
{
setupSignalHandler(sig, sigalrm_handler);
/* check if not sitting idle */
if(recursionCount)
traceFlag |= TRACE_TIMER;
else /* if idle */
executeSymbol(timerEvent, NULL, NULL);
}
#endif /* SOLARIS, TRUE64, AIX */
void setupAllSignals(void)
{
#if defined(SOLARIS) || defined(TRU64) || defined(AIX)
setupSignalHandler(SIGINT, ctrlC_handler);
#else
setupSignalHandler(SIGINT, signal_handler);
#endif
#ifndef WINDOWS
#if defined(SOLARIS) || defined(TRU64) || defined(AIX)
setupSignalHandler(SIGALRM, sigalrm_handler);
setupSignalHandler(SIGVTALRM, sigalrm_handler);
setupSignalHandler(SIGPROF, sigalrm_handler);
setupSignalHandler(SIGPIPE, sigpipe_handler);
setupSignalHandler(SIGCHLD, sigchld_handler);
#else
setupSignalHandler(SIGALRM, signal_handler);
setupSignalHandler(SIGVTALRM, signal_handler);
setupSignalHandler(SIGPROF, signal_handler);
setupSignalHandler(SIGPIPE, signal_handler);
setupSignalHandler(SIGCHLD, signal_handler);
#endif
#endif
}
void signal_handler(int sig)
{
#ifndef WINDOWS
char chr;
#endif
if(sig > 32 || sig < 1) return;
#if defined(SOLARIS) || defined(TRU64) || defined(AIX)
switch(sig)
{
case SIGALRM:
case SIGVTALRM:
case SIGPROF:
setupSignalHandler(sig, sigalrm_handler);
break;
case SIGPIPE:
setupSignalHandler(SIGPIPE, sigpipe_handler);
break;
case SIGCHLD:
setupSignalHandler(SIGCHLD, sigchld_handler);
break;
}
#else
setupSignalHandler(sig, signal_handler);
#endif
if(symHandler[sig - 1] != nilSymbol)
{
if(recursionCount)
{
currentSignal = sig;
traceFlag |= TRACE_SIGNAL;
return;
}
else
{
executeSymbol(symHandler[sig-1], stuffInteger(sig), NULL);
return;
}
}
switch(sig)
{
case SIGINT:
printErrorMessage(ERR_SIGINT, NULL, 0);
#ifdef WINDOWS
traceFlag |= TRACE_SIGINT;
#else
printf("%s", "\n(c)ontinue, (d)ebug, e(x)it, (r)eset:");
fflush(NULL);
chr = getchar();
if(chr == 'x') exit(1);
if(chr == 'd')
{
traceFlag &= ~TRACE_SIGINT;
openTrace();
}
if(chr == 'r') traceFlag |= TRACE_SIGINT;
break;
case SIGPIPE:
break;
case SIGALRM:
case SIGVTALRM:
case SIGPROF:
/* check if not sitting idle */
if(recursionCount)
traceFlag |= TRACE_TIMER;
else /* if idle */
executeSymbol(timerEvent, NULL, NULL);
break;
case SIGCHLD:
waitpid(-1, (int *)0, WNOHANG);
#endif
break;
default:
return;
}
}
#endif /* no EMSCRIPTEN */
char * which(char * name, char * buff)
{
char *path_list, *test, *tmp, *path_parsed;
struct stat filestat;
int count = 1;
int i, len, nlen;
int found = FALSE;
path_list = getenv("PATH");
if (!path_list) path_list = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin";
len = strlen(path_list);
nlen = strlen(name);
path_parsed = alloca(len + 1);
strncpy(path_parsed, path_list, len + 1);
test = path_parsed;
while (TRUE)
{
#ifdef WINDOWS
tmp = strchr(test, ';');
#else
tmp = strchr(test, ':');
#endif
if (tmp == NULL) break;
*tmp = 0;
test = tmp + 1;
count++;
}
test = path_parsed;
for (i = 0; i < count; i++)
{
len = strlen(test);
if((len + nlen + 2) > PATH_MAX)
return(NULL);
strncpy(buff, test, len + 1);
buff[len] = '/';
memcpy(buff + len + 1, name, nlen);
buff[len + 1 + nlen] = 0;
if (stat (buff, &filestat) == 0 && filestat.st_mode & S_IXUSR)
{
found = TRUE;
break;
}
test += (len + 1);
}
if(!found) return(NULL);
errno = 0;
return(buff);
}
#ifndef LIBRARY
void loadStartup(char * name)
{
char initFile[PATH_MAX];
char * envPtr;
int len;
/* normal newLISP start up */
if(strncmp(linkOffset + 4, "@@@@", 4) == 0)
{
if(getenv("HOME"))
strncpy(initFile, getenv("HOME"), PATH_MAX - 16);
else if(getenv("USERPROFILE"))
strncpy(initFile, getenv("USERPROFILE"), PATH_MAX - 16);
else if(getenv("DOCUMENT_ROOT"))
strncpy(initFile, getenv("DOCUMENT_ROOT"), PATH_MAX - 16);
len = strlen(initFile);
memcpy(initFile + len, "/.", 2);
memcpy(initFile + len + 2, INIT_FILE, 8);
initFile[len + 2 + 8] = 0;
if(loadFile(initFile, 0, 0, mainContext) == NULL)
{
envPtr = getenv("NEWLISPDIR");
if(envPtr)
{
strncpy(initFile, envPtr, PATH_MAX - 16);
len = strlen(envPtr);
memcpy(initFile + len, "/", 1);
memcpy(initFile + len + 1, INIT_FILE, 8);
initFile[len + 1 + 8] = 0;
loadFile(initFile, 0, 0, mainContext);
}
}
}
/* load part at offset no init.lsp or .init.lsp is loaded */
else
{
#ifdef WINDOWS
name = win_getExePath(alloca(MAX_PATH));
loadFile(name, *(unsigned int *)linkOffset, 1, mainContext);
#else /* if not Win32 get full pathname of file in name */
if(strchr(name, '/') == NULL)
if((name = which(name, alloca(PATH_MAX))) == NULL)
{
printf("%s: %s\n", strerror(ENOENT), name);
exit(ENOENT);
}
loadFile(name, *(unsigned int *)linkOffset, 1, mainContext);
#endif
}
}
#endif /* LIBRARY */
#ifdef _BSD
struct lconv *localeconv(void);
char *setlocale(int, const char *);
#endif
void initLocale(void)
{
#ifndef ANDROID
struct lconv * lc;
#endif
char * locale;
#ifndef SUPPORT_UTF8
locale = setlocale(LC_ALL, "C");
#else
locale = setlocale(LC_ALL, "");
#endif
if (locale != NULL)
stringOutputRaw = (strcmp(locale, "C") == 0);
#ifdef ANDROID
lc_decimal_point = '.';
#else
lc = localeconv();
lc_decimal_point = *lc->decimal_point;
#endif
}
/* set NEWLISPDIR only if not set already */
void initNewlispDir(void)
{
#ifdef WINDOWS
char * varValue;
char * newlispDir;
int len;
if(getenv("NEWLISPDIR") == NULL)
{
newlispDir = alloca(MAX_PATH);
varValue = getenv("PROGRAMFILES");
if(varValue != NULL)
{
len = strlen(varValue);
strncpy(newlispDir, varValue, MAX_PATH - 12);
memcpy(newlispDir + len, "/newlisp", 8);
newlispDir[len + 8] = 0;
setenv("NEWLISPDIR", newlispDir, TRUE);
}
else setenv("NEWLISPDIR", "newlisp", TRUE);
}
#else
if(getenv("NEWLISPDIR") == NULL)
setenv("NEWLISPDIR", NEWLISPDIR, TRUE);
#endif
}
void initTempDir()
{
#ifdef WINDOWS
if((tempDir = getenv("TMP")) == NULL)
{
printf("Environment variable TMP not set, assuming /tmp .");
tempDir = "/tmp";
}
#else
#ifdef ANDROID
tempDir = "/data/tmp";
#else /* all UNIX */
tempDir = "/tmp";
#endif
#endif
return;
}
#ifndef LIBRARY
char * getArg(char * * arg, int argc, int * index)
{
if(strlen(arg[*index]) > 2)
return(arg[*index] + 2);
if(*index >= (argc - 1))
{
printf("missing parameter for %s\n", arg[*index]);
exit(-1);
}
*index = *index + 1;
return(arg[*index]);
}
#ifndef WINDOWS
char ** MainArgs;
#endif
CELL * getMainArgs(char * mainArgs[])
{
CELL * argList;
int idx = 0;
#ifndef WINDOWS
MainArgs = mainArgs;
#endif
argList = getCell(CELL_EXPRESSION);
while(mainArgs[idx] != NULL)
addList(argList, stuffString(mainArgs[idx++]));
return(argList);
}
char * getCommandLine(int batchMode, int * length);
int main(int argc, char * argv[])
{
char command[MAX_COMMAND_LINE];
STREAM cmdStream = {NULL, NULL, 0, 0, 0};
char * cmd;
int idx;
#ifdef WINDOWS
WSADATA WSAData;
if(WSAStartup(MAKEWORD(2,2), &WSAData) != 0)
{
printf("Winsocket initialization failed\n");
exit(-1);
}
pagesize = 4096;
/* replace '_CRT_fmode = _O_BINARY' in nl-filesys.c for 10.4.8, thanks to Kosh */
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
_setmode(_fileno(stderr), _O_BINARY);
#endif
#ifdef SUPPORT_UTF8
opsys += 128;
#endif
#ifdef NEWLISP64
opsys += 256;
#endif
#ifdef FFI
opsys += 1024;
initFFI();
#endif
#ifndef WINDOWS
#ifndef OS2
pagesize = getpagesize();
#endif
tzset();
#endif
#ifdef OS2
/* Reset the floating point coprocessor */
_fpreset();
#endif
initLocale();
initNewlispDir();
initTempDir();
IOchannel = stdin;
bigEndian = (*((char *)&bigEndian) == 0);
initStacks();
initialize();
initDefaultInAddr();
#ifdef WINDOWS
#ifdef SUPPORT_UTF8
{
/*
command line parameter is MBCS.
MBCS -> Unicode(UTF-16) -> UTF-8
*/
char **argv_utf8 = allocMemory((argc + 1)* sizeof(char *)) ;
{
for(idx = 0 ; idx<argc ; idx++)
{
WCHAR *p_argvW = ansi_mbcs_to_utf16(argv[idx]) ;
char *p_argvU = utf16_to_utf8(p_argvW) ;
argv_utf8[idx] = p_argvU ;
}
argv_utf8[idx] = NULL ;
argv = argv_utf8 ;
}
}
#endif
#endif
mainArgsSymbol->contents = (UINT)getMainArgs(argv);
if((errorReg = setjmp(errorJump)) != 0)
{
if((errorEvent != nilSymbol) || (errorReg == ERR_USER_RESET))
executeSymbol(errorEvent, NULL, NULL);
else exit(-1);
goto AFTER_ERROR_ENTRY;
}
setupAllSignals();
sysEvalString(preLoad, mainContext, nilCell, EVAL_STRING);
/* loading of init.lsp will be suppressed with -n, -x or -h as first option
but is never done when program is link.lsp'd */
if(argc < 2 || (strncmp(argv[1], "-n", 2) && strncmp(argv[1], "-h", 2)) )
{
if(!(argc >= 2 && strcmp(argv[1], "-x") == 0))
loadStartup(argv[0]);
}
errno = 0;
if(realpath(".", startupDir) == NULL)
fatalError(ERR_IO_ERROR, 0, 0);
for(idx = 1; idx < argc; idx++)
{
if(strncmp(argv[idx], "-c", 2) == 0)
{
noPromptMode = TRUE;
continue;
}
if(strncmp(argv[idx], "-C", 2) == 0)
{
forcePromptMode = TRUE;
continue;
}
if(strncmp(argv[idx], "-http", 5) == 0)
{
noPromptMode = TRUE;
httpMode = TRUE;
if(strncmp(argv[idx], "-http-safe", 10) == 0)
httpSafe = TRUE;
continue;
}
if(strncmp(argv[idx], "-s", 2) == 0)
{
MAX_CPU_STACK = atoi(getArg(argv, argc, &idx));
if(MAX_CPU_STACK < 1024) MAX_CPU_STACK = 1024;
initStacks();
continue;
}
if(strncmp(argv[idx], "-p", 2) == 0 || strncmp(argv[idx], "-d", 2) == 0 )
{
if(strncmp(argv[idx], "-d", 2) == 0)
daemonMode = TRUE;
IOdomain = getArg(argv, argc, &idx);
IOport = atoi(IOdomain);
setupServer(0);
continue;
}
if(strncmp(argv[idx], "-t", 2) == 0)
{
connectionTimeout = atoi(getArg(argv, argc, &idx));
continue;
}
if(strncmp(argv[idx], "-l", 2) == 0 || strncmp(argv[idx], "-L", 2) == 0)
{
logTraffic = (strncmp(argv[idx], "-L", 2) == 0) ? LOG_MORE : LOG_LESS;
if(realpath(getArg(argv, argc, &idx), logFile) == NULL)
close(openFile(logFile, "w", 0));
continue;
}
if(strncmp(argv[idx], "-m", 2) == 0)
{
#ifndef NEWLISP64
MAX_CELL_COUNT = abs(0x0010000 * atoi(getArg(argv, argc, &idx)));
#else
MAX_CELL_COUNT = abs(0x0008000 * atoi(getArg(argv, argc, &idx)));
#endif
continue;
}
if(strncmp(argv[idx], "-w", 2) == 0)
{
if(realpath(getArg(argv, argc, &idx), startupDir) == NULL
|| chdir(startupDir) < 0)
fatalError(ERR_WORKING_DIR, 0, 0);
continue;
}
if(strcmp(argv[idx], "-6") == 0)
{
ADDR_FAMILY = AF_INET6;
initDefaultInAddr();
continue;
}
if(strcmp(argv[idx], "-v") == 0)
{
varPrintf(OUT_CONSOLE, banner, OSTYPE, LIBFFI, ".");
exit(0);
}
if(strncmp(argv[idx], "-e", 2) == 0)
{
executeCommandLine(getArg(argv, argc, &idx), OUT_CONSOLE, &cmdStream);
exit(0);
}
if(strncmp(argv[idx], "-x", 2) == 0)
{
if(argc == 4)
linkSource(argv[0], argv[idx + 1], argv[idx + 2]);
exit(0);
}
if(strcmp(argv[idx], "-h") == 0)
{
printHelpText();
exit(0);
}
loadFile(argv[idx], 0, 0, mainContext);
}
AFTER_ERROR_ENTRY:
if(isatty(fileno(IOchannel)))
{
isTTY = TRUE;
if(!noPromptMode)
varPrintf(OUT_CONSOLE, banner, OSTYPE, LIBFFI, banner2);
}
else
{
#ifdef WINDOWS
if(!IOchannelIsSocketStream)
#endif
setbuf(IOchannel,0);
if(forcePromptMode)
varPrintf(OUT_CONSOLE, banner, OSTYPE, LIBFFI, banner2);
}
/* ======================= main entry on reset ====================== */
errorReg = setjmp(errorJump);
setupAllSignals();
reset();
initStacks();
if(errorReg && !isNil((CELL*)errorEvent->contents) )
executeSymbol(errorEvent, NULL, NULL);
#ifdef READLINE
rl_readline_name = "newlisp";
rl_attempted_completion_function = (char ** (*) (const char *, int, int))newlisp_completion;
#if defined(LINUX) || defined(_BSD) || defined(KFREEBSD)
/* in Bash .inputrc put 'set blink-matching-paren on' */
rl_set_paren_blink_timeout(300000); /* 300 ms */
#endif
#endif
while(TRUE)
{
cleanupResults(resultStack);
if(isTTY)
{
cmd = getCommandLine(FALSE, NULL);
executeCommandLine(cmd, OUT_CONSOLE, &cmdStream);
free(cmd);
continue;
}
if(IOchannel != stdin || forcePromptMode)
varPrintf(OUT_CONSOLE, "%s", prompt());
/* daemon mode timeout if nothing read after accepting connection */
if(connectionTimeout && IOchannel && daemonMode)
{
#ifdef WINDOWS
if(IOchannelIsSocketStream)
if(wait_ready(getSocket(IOchannel), connectionTimeout, 0) == 0)
#else
if(wait_ready(fileno(IOchannel), connectionTimeout, 0) == 0)
#endif
{
fclose(IOchannel);
setupServer(1);
continue;
}
}
if(IOchannel == NULL || fgets(command, MAX_COMMAND_LINE - 1, IOchannel) == NULL)
{
if(!daemonMode) exit(1);
if(IOchannel != NULL) fclose(IOchannel);
setupServer(1);
continue;
}
executeCommandLine(command, OUT_CONSOLE, &cmdStream);
}
#ifndef WINDOWS
return 0;
#endif
}
#endif /* not LIBRARY */
#ifdef READLINE
char * command_generator(char * text, int state)
{
static int list_index, len, clen;
char * name;
if (!state)
{
list_index = 0;
len = strlen (text);
}