forked from ChatScript/ChatScript
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmainSystem.cpp
2965 lines (2764 loc) · 96.3 KB
/
mainSystem.cpp
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
#include "common.h"
#include "evserver.h"
char* version = "8.2";
char sourceInput[200];
FILE* userInitFile;
int externalTagger = 0;
char defaultbot[100];
bool loadingUser = false;
char traceuser[500];
int traceUniversal;
PRINTER printer = printf;
unsigned int idetrace = -1;
int outputlevel = 0;
char* outputCode[MAX_GLOBAL];
static bool argumentsSeen = false;
char* configFile = "cs_init.txt"; // can set config params
char language[40]; // indicate current language used
char livedata[500]; // where is the livedata folder
char languageFolder[500]; // where is the livedata language folder
char systemFolder[500]; // where is the livedata system folder
bool noboot = false;
static char* erasename = "csuser_erase";
bool build0Requested = false;
char websocketParam[1000];
bool build1Requested = false;
bool servertrace = false;
bool pendingRestart = false;
bool pendingUserReset = false;
bool rebooting = false;
bool assignedLogin = false;
int forkcount = 1;
int outputchoice = -1;
char apikey[100];
DEBUGAPI debugInput = NULL;
DEBUGAPI debugOutput = NULL;
DEBUGAPI debugEndTurn = NULL;
DEBUGLOOPAPI debugCall = NULL;
DEBUGVARAPI debugVar = NULL;
DEBUGVARAPI debugMark = NULL;
DEBUGAPI debugMessage = NULL;
DEBUGAPI debugAction = NULL;
#define MAX_RETRIES 20
uint64 startTimeInfo; // start time of current volley
char revertBuffer[INPUT_BUFFER_SIZE]; // copy of user input so can :revert if desired
int argc;
char ** argv;
char postProcessing = 0; // copy of output generated during MAIN control. Postprocessing can prepend to it
unsigned int tokenCount; // for document performc
bool callback = false; // when input is callback,alarm,loopback, dont want to enable :retry for that...
int timerLimit = 0; // limit time per volley
int timerCheckRate = 0; // how often to check calls for time
uint64 volleyStartTime = 0;
char forkCount[10];
int timerCheckInstance = 0;
static char* privateParams = NULL;
char treetaggerParams[200];
static char encryptParams[300];
static char decryptParams[300];
char hostname[100];
bool nosuchbotrestart = false; // restart if no such bot
char users[100];
char logs[100];
char topic[100];
char tmp[100];
char buildfiles[100];
char* derivationSentence[MAX_SENTENCE_LENGTH];
int derivationLength;
char authorizations[200]; // for allowing debug commands
char ourMainInputBuffer[INPUT_BUFFER_SIZE]; // user input buffer - ptr to primaryInputBuffer
char* mainInputBuffer; // user input buffer - ptr to primaryInputBuffer
char* ourMainOutputBuffer; // main user output buffer
char* mainOutputBuffer; //
char* readBuffer; // main scratch reading buffer (files, etc)
bool readingDocument = false;
bool redo = false; // retry backwards any amount
bool oobExists = false;
bool docstats = false;
unsigned int docSentenceCount = 0;
char rawSentenceCopy[INPUT_BUFFER_SIZE]; // current raw sentence
char *evsrv_arg = NULL;
unsigned short int derivationIndex[256];
bool overrideAuthorization = false;
clock_t startSystem; // time chatscript started
unsigned int choiceCount = 0;
int always = 1; // just something to stop the complaint about a loop based on a constant
// support for callbacks
uint64 callBackTime = 0; // one-shot pending time - forces callback with "[callback]" when output was [callback=xxx] when no user submitted yet
uint64 callBackDelay = 0; // one-shot pending time - forces callback with "[callback]" when output was [callback=xxx] when no user submitted yet
uint64 loopBackTime = 0; // ongoing callback forces callback with "[loopback]" when output was "[loopback=xxx]"
uint64 loopBackDelay = 0; // ongoing callback forces callback with "[loopback]" when output was "[loopback=xxx]"
uint64 alarmTime = 0;
uint64 alarmDelay = 0; // one-shot pending time - forces callback with "[alarm]" when output was [callback=xxx] when no user submitted yet
unsigned int outputLength = 100000; // max output before breaking.
char* extraTopicData = 0; // supplemental topic set by :extratopic
// server data
#ifdef DISCARDSERVER
bool server = false;
#else
std::string interfaceKind = "0.0.0.0";
#ifdef WIN32
bool server = false; // default is standalone on Windows
#elif IOS
bool server = true; // default is server on LINUX
#else
bool server = true; // default is server on LINUX
#endif
#endif
unsigned int port = 1024; // server port
bool commandLineCompile = false;
PrepareMode tmpPrepareMode = NO_MODE; // controls what processing is done in preparation NO_MODE, POS_MODE, PREPARE_MODE
PrepareMode prepareMode = NO_MODE; // controls what processing is done in preparation NO_MODE, POS_MODE, PREPARE_MODE
bool noReact = false;
// :source:document data
bool documentMode = false; // read input as a document not as chat
FILE* sourceFile = NULL; // file to use for :source
bool multiuser = false;
EchoSource echoSource = NO_SOURCE_ECHO; // for :source, echo that input to nowhere, user, or log
uint64 sourceStart = 0; // beginning time of source file
unsigned int sourceTokens = 0;
unsigned int sourceLines = 0;
// status of user input
unsigned int volleyCount = 0; // which user volley is this
bool moreToComeQuestion = false; // is there a ? in later sentences
bool moreToCome = false; // are there more sentences pending
uint64 tokenControl = 0; // results from tokenization and prepare processing
unsigned int responseControl = ALL_RESPONSES; // std output behaviors
char* nextInput; // ptr to rest of users input after current sentence
static char oldInputBuffer[INPUT_BUFFER_SIZE]; // copy of the sentence we are processing
char currentInput[INPUT_BUFFER_SIZE]; // the sentence we are processing BUG why both
// general display and flow controls
bool quitting = false; // intending to exit chatbot
int systemReset = 0; // intending to reload system - 1 (mild reset) 2 (full user reset)
bool autonumber = false; // display number of volley to user
bool showWhy = false; // show user rules generating his output
bool showTopic = false; // show resulting topic on output
bool showTopics = false; // show all relevant topics
bool showInput = false; // Log internal input additions
bool showReject = false; // Log internal repeat rejections additions
bool all = false; // generate all possible answers to input
int regression = NO_REGRESSION; // regression testing in progress
unsigned int trace = 0; // current tracing flags
char debugEntry[1000];
char bootcmd[200]; // current boot tracing flags
unsigned int timing = 0; // current timing flags
bool shortPos = false; // display pos results as you go
int inputRetryRejoinderTopic = NO_REJOINDER;
int inputRetryRejoinderRuleID = NO_REJOINDER;
int sentenceRetryRejoinderTopic = NO_REJOINDER;
int sentenceRetryRejoinderRuleID = NO_REJOINDER;
char oktest[MAX_WORD_SIZE]; // auto response test
char activeTopic[200];
char respondLevel = 0; // rejoinder level of a random choice block
static bool oobPossible = false; // 1st thing could be oob
int inputCounter = 0; // protecting ^input from cycling
int totalCounter = 0; // protecting ^input from cycling
char userPrefix[MAX_WORD_SIZE]; // label prefix for user input
char botPrefix[MAX_WORD_SIZE]; // label prefix for bot output
bool unusedRejoinder; // inputRejoinder has been executed, blocking further calls to ^Rejoinder
char inputCopy[INPUT_BUFFER_SIZE]; // the original input we were given, we will work on this
// outputs generated
RESPONSE responseData[MAX_RESPONSE_SENTENCES+1];
unsigned char responseOrder[MAX_RESPONSE_SENTENCES+1];
int responseIndex;
int inputSentenceCount; // which sentence of volley is this
static void HandleBoot(WORDP boot,bool reboot);
///////////////////////////////////////////////
/// SYSTEM STARTUP AND SHUTDOWN
///////////////////////////////////////////////
void InitStandalone()
{
startSystem = clock() / CLOCKS_PER_SEC;
*currentFilename = 0; // no files are open (if logging bugs)
tokenControl = 0;
outputlevel = 0;
*computerID = 0; // default bot
}
static void SetBotVariable(char* word)
{
char* eq = strchr(word, '=');
if (eq)
{
*eq = 0;
ReturnToAfterLayer(currentBeforeLayer-1, true);
*word = USERVAR_PREFIX;
SetUserVariable(word, eq + 1);
if (server) Log(SERVERLOG, (char*)"botvariable: %s = %s\r\n", word, eq + 1);
else (*printer)((char*)"botvariable: %s = %s\r\n", word, eq + 1);
NoteBotVariables(); // these go into level 1
LockLayer(false);
}
}
static void HandleBoot(WORDP boot, bool reboot)
{
if (reboot) rebooting = true;
if (boot && !noboot) // run script on startup of system. data it generates will also be layer 1 data
{
int oldtrace = trace;
if (*bootcmd)
{
char* at = bootcmd;
if (*bootcmd == '"')
{
++at;
if (at[strlen(at) - 1] == '"') at[strlen(at) - 1] = 0;
}
DoCommand(at, NULL, false);
}
if (!reboot) UnlockLayer(LAYER_BOOT); // unlock it to add stuff
FACT* F = factFree;
Callback(boot, (char*)"()", true, true); // do before world is locked
*ourMainOutputBuffer = 0; // remove any boot message
myBot = 0; // restore fact owner to generic all
if (!rebooting)
{
while (++F <= factFree)
{
if (F->flags & FACTTRANSIENT) F->flags |= FACTDEAD;
else F->flags |= FACTBUILD1; // convert these to level 1
}
NoteBotVariables(); // convert user variables read into bot variables in boot layer
LockLayer(false); // rewrite level 2 start data with augmented from script data
}
else
{
ReturnToAfterLayer(2, false); // dict/fact/strings reverted and any extra topic loaded info (but CSBoot process NOT lost)
}
trace = (modifiedTrace) ? modifiedTraceVal : oldtrace;
stackFree = stackStart; // drop any possible stack used
}
rebooting = false;
}
int CountWordsInBuckets(int& unused, unsigned int* depthcount,int limit)
{
memset(depthcount, 0, sizeof(int)*1000);
unused = 0;
int words = 0;
for (unsigned long i = 0; i <= maxHashBuckets; ++i)
{
int n = 0;
if (!hashbuckets[i]) ++unused;
else
{
WORDP D = Index2Word(hashbuckets[i]);
while (D != dictionaryBase)
{
++n;
D = Index2Word(GETNEXTNODE(D));
}
}
words += n;
if (n < limit) depthcount[n]++;
}
return words;
}
void CreateSystem()
{
overrideAuthorization = false; // always start safe
loading = true;
char* os;
mystart("createsystem");
#ifdef WIN32
os = "Windows";
#elif IOS
os = "IOS";
#elif __MACH__
os = "MACH";
#elif FREEBSD
os = "FreeBSD";
#else
os = "LINUX";
#endif
if (!buffers) // restart asking for new memory allocations
{
maxBufferSize = (maxBufferSize + 63);
maxBufferSize &= 0xffffffC0; // force 64 bit align
unsigned int total = maxBufferLimit * maxBufferSize;
buffers = (char*) malloc(total); // have it around already for messages
if (!buffers)
{
(*printer)((char*)"%s",(char*)"cannot allocate buffer space");
exit(1);
}
bufferIndex = 0;
readBuffer = AllocateBuffer(); // technically if variables are huge, readuserdata would overflow this
baseBufferIndex = bufferIndex;
}
char data[MAX_WORD_SIZE];
char* kind;
int pid = 0;
#ifdef EVSERVER
kind = "EVSERVER";
pid = getpid();
#elif DEBUG
kind = "Debug";
#else
kind = "Release";
#endif
sprintf(data,(char*)"ChatScript %s Version %s pid: %d %ld bit %s compiled %s",kind,version,pid,(long int)(sizeof(char*) * 8),os,compileDate);
strcat(data,(char*)" host=");
strcat(data,hostname);
if (server) Log(SERVERLOG,(char*)"Server %s\r\n",data);
strcat(data,(char*)"\r\n");
(*printer)((char*)"%s",data);
int oldtrace = trace; // in case trace turned on by default
trace = 0;
*oktest = 0;
sprintf(data,(char*)"Params: dict:%ld fact:%ld text:%ldkb hash:%ld \r\n",(long int)maxDictEntries,(long int)maxFacts,(long int)(maxHeapBytes/1000),(long int)maxHashBuckets);
if (server) Log(SERVERLOG,(char*)"%s",data);
else (*printer)((char*)"%s",data);
sprintf(data,(char*)" buffer:%dx%dkb cache:%dx%dkb userfacts:%d outputlimit:%d loglimit:%d\r\n",(int)maxBufferLimit,(int)(maxBufferSize/1000),(int)userCacheCount,(int)(userCacheSize/1000),(int)userFactCount,
outputsize,logsize);
if (server) Log(SERVERLOG,(char*)"%s",data);
else (*printer)((char*)"%s",data);
InitScriptSystem();
InitVariableSystem();
ReloadSystem(); // builds base facts and dictionary (from wordnet)
LoadTopicSystem(); // dictionary reverts to wordnet zone
*currentFilename = 0;
*debugEntry = 0;
computerID[0] = 0;
if (!assignedLogin) loginName[0] = loginID[0] = 0;
*botPrefix = *userPrefix = 0;
char word[MAX_WORD_SIZE];
char buffer[MAX_WORD_SIZE];
FILE* in = FopenReadOnly(configFile);
if (in)
{
while (ReadALine(buffer, in, MAX_WORD_SIZE) >= 0)
{
ReadCompiledWord(buffer, word);
if (*word == 'V') SetBotVariable(word); // these are level 1 values
}
fclose(in);
}
for (int i = 1; i < argc; ++i)
{
strcpy(word,argv[i]);
if (*word == '"' && word[1] == 'V')
{
memmove(word,word+1,strlen(word));
size_t len = strlen(word);
if (word[len-1] == '"') word[len-1] = 0;
}
if (*word == 'V') SetBotVariable(word); // predefined bot variable in level 1
}
kernelVariableThreadList = botVariableThreadList;
botVariableThreadList = 0;
UnlockLayer(LAYER_BOOT); // unlock it to add stuff
HandleBoot(FindWord((char*)"^csboot"),false);// run script on startup of system. data it generates will also be layer 1 data
LockLayer(true);
InitSpellCheck(); // after boot vocabulary added
unsigned int factUsedMemKB = ( factFree-factBase) * sizeof(FACT) / 1000;
unsigned int factFreeMemKB = ( factEnd-factFree) * sizeof(FACT) / 1000;
unsigned int dictUsedMemKB = ( dictionaryFree-dictionaryBase) * sizeof(WORDENTRY) / 1000;
// dictfree shares text space
unsigned int textUsedMemKB = ( heapBase-heapFree) / 1000;
unsigned int textFreeMemKB = ( heapFree- heapEnd) / 1000;
unsigned int bufferMemKB = (maxBufferLimit * maxBufferSize) / 1000;
unsigned int used = factUsedMemKB + dictUsedMemKB + textUsedMemKB + bufferMemKB;
used += (userTopicStoreSize + userTableSize) /1000;
unsigned int free = factFreeMemKB + textFreeMemKB;
char route[MAX_WORD_SIZE];
unsigned int bytes = (tagRuleCount * MAX_TAG_FIELDS * sizeof(uint64)) / 1000;
used += bytes;
char buf2[MAX_WORD_SIZE];
char buf1[MAX_WORD_SIZE];
char buf[MAX_WORD_SIZE];
strcpy(buf,StdIntOutput(factFree-factBase));
strcpy(buf2,StdIntOutput(textFreeMemKB));
unsigned int depthcount[1000];
int unused;
int words = CountWordsInBuckets(unused, depthcount,1000);
char depthmsg[1000];
int x = 1000;
while (!depthcount[--x]); // find first used slot
char* at = depthmsg;
*at = 0;
for (int i = 0; i <= x; ++i)
{
sprintf(at, "%d.", depthcount[i]);
at += strlen(at);
}
sprintf(data, (char*)"Used %dMB: dict %s (%dkb) hashdepths %s words %d unusedbuckets %d fact %s (%dkb) heap %dkb\r\n",
used / 1000,
StdIntOutput(dictionaryFree - dictionaryBase - 1),
dictUsedMemKB, depthmsg,words,unused,
buf,
factUsedMemKB,
textUsedMemKB);
if (server) Log(SERVERLOG,(char*)"%s",data);
else (*printer)((char*)"%s",data);
sprintf(data,(char*)" buffer (%dkb) cache (%dkb)\r\n",
bufferMemKB,
(userTopicStoreSize + userTableSize)/1000);
if(server) Log(SERVERLOG,(char*)"%s",data);
else (*printer)((char*)"%s",data);
strcpy(buf,StdIntOutput(factEnd-factFree)); // unused facts
strcpy(buf1,StdIntOutput(textFreeMemKB)); // unused text
strcpy(buf2,StdIntOutput(free/1000));
sprintf(data,(char*)"Free %sMB: dict %s fact %s stack/heap %sKB \r\n\r\n",
buf2,StdIntOutput(((unsigned int)maxDictEntries)-(dictionaryFree-dictionaryBase)),buf,buf1);
if (server) Log(SERVERLOG,(char*)"%s",data);
else (*printer)((char*)"%s",data);
trace = oldtrace;
#ifdef DISCARDSERVER
(*printer)((char*)" Server disabled.\r\n");
#endif
#ifdef DISCARDSCRIPTCOMPILER
if(server) Log(SERVERLOG,(char*)" Script compiler disabled.\r\n");
else (*printer)((char*)" Script compiler disabled.\r\n");
#endif
#ifdef DISCARDTESTING
if(server) Log(SERVERLOG,(char*)" Testing disabled.\r\n");
else (*printer)((char*)" Testing disabled.\r\n");
#else
*callerIP = 0;
if (!assignedLogin) *loginID = 0;
if (server && VerifyAuthorization(FopenReadOnly((char*)"authorizedIP.txt"))) // authorizedIP
{
Log(SERVERLOG,(char*)" *** Server WIDE OPEN to :command use.\r\n");
}
#endif
#ifdef DISCARDJSONOPEN
sprintf(route, (char*)"%s", (char*)" JSONOpen access disabled.\r\n");
if (server) Log(SERVERLOG, route);
else (*printer)(route);
#endif
#ifdef TREETAGGER
sprintf(route, (char*)" TreeTagger access enabled: %s\r\n",language);
if (server) Log(SERVERLOG, route);
else (*printer)(route);
#endif
#ifndef DISCARDMONGO
if (*mongodbparams) sprintf(route," Mongo enabled. FileSystem routed to %s\r\n",mongodbparams);
else sprintf(route," Mongo enabled.\r\n");
if (server) Log(SERVERLOG,route);
else (*printer)(route);
#endif
#ifndef DISCARDPOSTGRES
if (*postgresparams) sprintf(route, " Postgres enabled. FileSystem routed to %s\r\n", postgresparams);
else sprintf(route, " Postgres enabled.\r\n");
if (server) Log(SERVERLOG, route);
else (*printer)(route);
#endif
(*printer)((char*)"%s",(char*)"\r\n");
loading = false;
}
void ReloadSystem()
{// reset the basic system through wordnet but before topics
InitFacts(); // malloc space
InitStackHeap(); // malloc space
InitDictionary(); // malloc space
InitCache(); // malloc space
// make sets for the part of speech data
LoadDictionary();
InitFunctionSystem();
#ifndef DISCARDTESTING
InitCommandSystem();
#endif
ExtendDictionary(); // store labels of concepts onto variables.
DefineSystemVariables();
ClearUserVariables();
AcquirePosMeanings(false); // do not build pos facts (will be in binary) but make vocab available
if (!ReadBinaryFacts(FopenStaticReadOnly(UseDictionaryFile((char*)"facts.bin")))) // DICT
{
AcquirePosMeanings(true);
WORDP safeDict = dictionaryFree;
ReadFacts(UseDictionaryFile((char*)"facts.txt"),NULL,0);
if ( safeDict != dictionaryFree) myexit((char*)"dict changed on read of facts");
WriteBinaryFacts(FopenBinaryWrite(UseDictionaryFile((char*)"facts.bin")),factBase);
}
char name[MAX_WORD_SIZE];
sprintf(name,(char*)"%s/%s/systemfacts.txt",livedata,language);
ReadFacts(name,NULL,0); // part of wordnet, not level 0 build
ReadLiveData(); // considered part of basic system before a build
InitTextUtilities1(); // also part of basic system before a build
#ifdef TREETAGGER
// We will be reserving some working space for treetagger on the heap
// so make sure it is tucked away where it cannot be touched
InitTreeTagger(treetaggerParams);
#endif
WordnetLockDictionary();
}
static void ProcessArgument(char* arg)
{
if (!argumentsSeen)
{
char path[MAX_WORD_SIZE];
*path = 0;
GetCurrentDir(path, MAX_WORD_SIZE);
(*printer)("CommandLine: %s\r\n",path);
argumentsSeen = true;
}
(*printer)(" %s\r\n",arg);
if (!stricmp(arg,(char*)"trace")) trace = (unsigned int) -1;
else if (!strnicmp(arg,(char*)"language=",9))
{
strcpy(language,arg+9);
MakeUpperCase(language);
}
else if (!strnicmp(arg, (char*)"debug=",6)) strcpy(arg+6,debugEntry);
else if (!strnicmp(arg, "erasename=", 10)) erasename = arg + 10;
else if (!stricmp(arg,"userencrypt")) userEncrypt = true;
else if (!stricmp(arg,"ltmencrypt")) ltmEncrypt = true;
else if (!strnicmp(arg, "defaultbot=", 11)) strcpy(defaultbot, arg + 11);
else if (!strnicmp(arg, "traceuser=", 10)) strcpy(traceuser, arg + 10);
else if (!stricmp(arg,"noboot")) noboot = true;
else if (!stricmp(arg, "servertrace")) servertrace = true;
else if (!strnicmp(arg,(char*)"apikey=",7)) strcpy(apikey,arg+7);
else if (!strnicmp(arg,(char*)"logsize=",8)) logsize = atoi(arg+8); // bytes avail for log buffer
else if (!strnicmp(arg, (char*)"loglimit=", 9)) loglimit = atoi(arg + 9); // max mb of log file before change files
else if (!strnicmp(arg,(char*)"outputsize=",11)) outputsize = atoi(arg+11); // bytes avail for log buffer
else if (!stricmp(arg, (char*)"time")) timing = (unsigned int)-1 ^ TIME_ALWAYS;
else if (!strnicmp(arg,(char*)"bootcmd=",8)) strcpy(bootcmd,arg+8);
else if (!strnicmp(arg,(char*)"dir=",4))
{
#ifdef WIN32
if (!SetCurrentDirectory(arg+4)) (*printer)((char*)"unable to change to %s\r\n",arg+4);
#else
chdir(arg+5);
#endif
}
else if (!strnicmp(arg,(char*)"source=",7))
{
if (arg[7] == '"')
{
strcpy(sourceInput,arg+8);
size_t len = strlen(sourceInput);
if (sourceInput[len-1] == '"') sourceInput[len-1] = 0;
}
else strcpy(sourceInput,arg+7);
}
else if (!strnicmp(arg,(char*)"login=",6))
{
assignedLogin = true;
strcpy(loginID,arg+6);
}
else if (!strnicmp(arg,(char*)"output=",7)) outputLength = atoi(arg+7);
else if (!strnicmp(arg,(char*)"save=",5))
{
volleyLimit = atoi(arg+5);
if (volleyLimit > 255) volleyLimit = 255; // cant store higher
}
// memory sizings
else if (!strnicmp(arg,(char*)"hash=",5))
{
maxHashBuckets = atoi(arg+5); // size of hash
setMaxHashBuckets = true;
}
else if (!strnicmp(arg,(char*)"dict=",5)) maxDictEntries = atoi(arg+5); // how many dict words allowed
else if (!strnicmp(arg,(char*)"fact=",5)) maxFacts = atoi(arg+5); // fact entries
else if (!strnicmp(arg,(char*)"text=",5)) maxHeapBytes = atoi(arg+5) * 1000; // stack and heap bytes in pages
else if (!strnicmp(arg,(char*)"cache=",6)) // value of 10x0 means never save user data
{
userCacheSize = atoi(arg+6) * 1000;
char* number = strchr(arg+6,'x');
if (number) userCacheCount = atoi(number+1);
}
else if (!strnicmp(arg,(char*)"userfacts=",10)) userFactCount = atoi(arg+10); // how many user facts allowed
else if (!stricmp(arg,(char*)"redo")) redo = true; // enable redo
else if (!strnicmp(arg,(char*)"authorize=",10)) strcpy(authorizations,arg+10); // whitelist debug commands
else if (!stricmp(arg,(char*)"nodebug")) *authorizations = '1';
else if (!strnicmp(arg,(char*)"users=",6 )) strcpy(users,arg+6);
else if (!strnicmp(arg,(char*)"logs=",5 )) strcpy(logs,arg+5);
else if (!strnicmp(arg,(char*)"topic=",6 )) strcpy(topic,arg+6);
else if (!strnicmp(arg,(char*)"tmp=",4 )) strcpy(tmp,arg+4);
else if (!strnicmp(arg, (char*)"buildfiles=", 11)) strcpy(buildfiles, arg + 11);
else if (!strnicmp(arg,(char*)"private=",8)) privateParams = arg+8;
else if (!strnicmp(arg, (char*)"treetagger", 10))
{
if (arg[10] == '=') strcpy(treetaggerParams, arg + 11);
else strcpy(treetaggerParams, "1");
}
else if (!strnicmp(arg,(char*)"encrypt=",8)) strcpy(encryptParams,arg+8);
else if (!strnicmp(arg,(char*)"decrypt=",8)) strcpy(decryptParams,arg+8);
else if (!strnicmp(arg,(char*)"livedata=",9) )
{
strcpy(livedata,arg+9);
sprintf(systemFolder,(char*)"%s/SYSTEM",arg+9);
sprintf(languageFolder,(char*)"%s/%s",arg+9,language);
}
else if (!strnicmp(arg,(char*)"nosuchbotrestart=",17) )
{
if (!stricmp(arg+17,"true")) nosuchbotrestart = true;
else nosuchbotrestart = false;
}
else if (!strnicmp(arg,(char*)"system=",7) ) strcpy(systemFolder,arg+7);
else if (!strnicmp(arg,(char*)"english=",8) ) strcpy(languageFolder,arg+8);
#ifndef DISCARDPOSTGRES
else if (!strnicmp(arg,(char*)"pguser=",7) ) strcpy(postgresparams, arg+7);
// Postgres Override SQL
else if (!strnicmp(arg,(char*)"pguserread=",11) )
{
strcpy(postgresuserread, arg+11);
pguserread = postgresuserread;
}
else if (!strnicmp(arg,(char*)"pguserinsert=",13) )
{
strcpy(postgresuserinsert, arg+13);
pguserinsert = postgresuserinsert;
}
else if (!strnicmp(arg,(char*)"pguserupdate=",13) )
{
strcpy(postgresuserupdate, arg+13);
pguserupdate = postgresuserupdate;
}
#endif
#ifndef DISCARDMYSQL
else if (!strnicmp(arg,(char*)"mysqlhost=",10) )
{
mysqlconf = true;
strcpy(mysqlhost, arg+10);
}
else if (!strnicmp(arg,(char*)"mysqlport=",10) )
{
mysqlconf = true;
unsigned int port = atoi(arg+10);
mysqlport = port;
}
else if (!strnicmp(arg,(char*)"mysqldb=",8) )
{
mysqlconf = true;
strcpy(mysqldb, arg+8);
}
else if (!strnicmp(arg,(char*)"mysqluser=",10) )
{
mysqlconf = true;
strcpy(mysqluser, arg+10);
}
else if (!strnicmp(arg,(char*)"mysqlpasswd=",12) )
{
mysqlconf = true;
strcpy(mysqlpasswd, arg+12);
}
// MySQL Query Override SQL
else if (!strnicmp(arg,(char*)"mysqluserread=",14) )
{
strcpy(my_userread_sql, arg+14);
mysql_userread = my_userread_sql;
}
else if (!strnicmp(arg,(char*)"mysqluserinsert=",16) )
{
strcpy(my_userinsert_sql, arg+16);
mysql_userinsert = my_userinsert_sql;
}
else if (!strnicmp(arg,(char*)"mysqluserupdate=",16) )
{
strcpy(my_userupdate_sql, arg+16);
mysql_userupdate = my_userupdate_sql;
}
#endif
#ifndef DISCARDMONGO
else if (!strnicmp(arg,(char*)"mongo=",6) ) strcpy(mongodbparams,arg+6);
#endif
#ifndef DISCARDCLIENT
else if (!strnicmp(arg,(char*)"client=",7)) // client=1.2.3.4:1024 or client=localhost:1024
{
server = false;
char buffer[MAX_WORD_SIZE];
strcpy(serverIP,arg+7);
char* portVal = strchr(serverIP,':');
if ( portVal)
{
*portVal = 0;
port = atoi(portVal+1);
}
if (!*loginID)
{
(*printer)((char*)"%s",(char*)"\r\nEnter client user name: ");
ReadALine(buffer,stdin);
(*printer)((char*)"%s",(char*)"\r\n");
Client(buffer);
}
else Client(loginID);
myexit((char*)"client ended");
}
#endif
else if (!stricmp(arg,(char*)"userlog")) userLog = true;
else if (!stricmp(arg,(char*)"nouserlog")) userLog = false;
else if (!strnicmp(arg, (char*)"hidefromlog=", 12))
{
char* at = arg + 12;
if (*at == '"') ++at;
strcpy(hide, at);
size_t len = strlen(hide);
if (hide[len - 1] == '"') hide[len - 1] = 0;
}
#ifndef DISCARDSERVER
else if (!stricmp(arg,(char*)"serverretry")) serverRetryOK = true;
else if (!stricmp(arg,(char*)"local")) server = false; // local standalone
else if (!stricmp(arg,(char*)"noserverlog")) serverLog = false;
else if (!stricmp(arg,(char*)"serverlog")) serverLog = true;
else if (!stricmp(arg,(char*)"noserverprelog")) serverPreLog = false;
else if (!stricmp(arg,(char*)"serverctrlz")) serverctrlz = 1;
else if (!strnicmp(arg, (char*)"websocket=",10)) strcpy(websocketParam,arg + 10);
else if (!strnicmp(arg,(char*)"port=",5)) // be a server
{
port = atoi(arg+5); // accept a port=
server = true;
#ifdef LINUX
if (forkcount > 1) {
sprintf(serverLogfileName, (char*)"%s/serverlog%d-%d.txt", logs, port,getpid());
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d-%d.txt", logs, port, getpid());
}
else {
sprintf(serverLogfileName, (char*)"%s/serverlog%d.txt", logs, port);
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d.txt", logs, port);
}
#else
sprintf(serverLogfileName,(char*)"%s/serverlog%d.txt",logs,port); // DEFAULT LOG
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d.txt", logs, port);
#endif
}
#ifdef EVSERVER
else if (!strnicmp(arg, "fork=", 5))
{
forkcount = atoi(arg + 5);
sprintf(forkCount,(char*)"evsrv:fork=%d", forkcount);
evsrv_arg = forkCount;
#ifdef LINUX
if (forkcount > 1) {
sprintf(serverLogfileName, (char*)"%s/serverlog%d-%d.txt", logs, port, getpid());
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d-%d.txt", logs, port, getpid());
}
else {
sprintf(serverLogfileName, (char*)"%s/serverlog%d.txt", logs, port);
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d.txt", logs, port);
}
#else
sprintf(serverLogfileName, (char*)"%s/serverlog%d.txt", logs, port); // DEFAULT LOG
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d.txt", logs, port);
#endif
}
#endif
else if (!strnicmp(arg,(char*)"interface=",10)) interfaceKind = string(arg+10); // specify interface
#endif
}
void ProcessArguments(int argc, char* argv[])
{
for (int i = 1; i < argc; ++i) ProcessArgument(argv[i]);
}
static void ReadConfig()
{
char buffer[MAX_WORD_SIZE];
FILE* in = FopenReadOnly(configFile);
if (!in) return;
while (ReadALine(buffer,in,MAX_WORD_SIZE) >= 0) ProcessArgument(TrimSpaces(buffer,true));
fclose(in);
}
unsigned int InitSystem(int argcx, char * argvx[],char* unchangedPath, char* readablePath, char* writeablePath, USERFILESYSTEM* userfiles, DEBUGAPI infn, DEBUGAPI outfn)
{ // this work mostly only happens on first startup, not on a restart
*traceuser = 0;
*hide = 0;
*botheader = 0;
FILE* in = FopenStaticReadOnly((char*)"SRC/dictionarySystem.h"); // SRC/dictionarySystem.h
if (!in) // if we are not at top level, try going up a level
{
#ifdef WIN32
if (!SetCurrentDirectory((char*)"..")) // move from BINARIES to top level
myexit((char*)"unable to change up\r\n");
#else
chdir((char*)"..");
#endif
}
else FClose(in);
*defaultbot = 0;
strcpy(hostname,(char*)"local");
*sourceInput = 0;
*buildfiles = 0;
*apikey = 0;
*websocketParam = 0;
*bootcmd = 0;
#ifndef DISCARDMONGO
*mongodbparams = 0;
#endif
#ifndef DISCARDPOSTGRES
*postgresparams = 0;
#endif
#ifdef TREETAGGER
*treetaggerParams = 0;
#endif
*authorizations = 0;
debugInput = infn;
debugOutput = outfn;
argc = argcx;
argv = argvx;
InitFileSystem(unchangedPath,readablePath,writeablePath);
if (userfiles) memcpy((void*)&userFileSystem,userfiles,sizeof(userFileSystem));
for (unsigned int i = 0; i <= MAX_WILDCARDS; ++i)
{
*wildcardOriginalText[i] = *wildcardCanonicalText[i] = 0;
wildcardPosition[i] = 0;
}
strcpy(users,(char*)"USERS");
strcpy(logs,(char*)"LOGS");
strcpy(topic,(char*)"TOPIC");
strcpy(tmp,(char*)"TMP");
strcpy(language,(char*)"ENGLISH");
*loginID = 0;
for (int i = 1; i < argc; ++i) // essentials
{
if (!strnicmp(argv[i],(char*)"buffer=",7)) // number of large buffers available 8x80000
{
maxBufferLimit = atoi(argv[i]+7);
char* size = strchr(argv[i]+7,'x');
if (size) maxBufferSize = atoi(size+1) *1000;
if (maxBufferSize < OUTPUT_BUFFER_SIZE)
{
(*printer)((char*)"Buffer cannot be less than OUTPUT_BUFFER_SIZE of %d\r\n",OUTPUT_BUFFER_SIZE);
myexit((char*)"buffer size less than output buffer size");
}
}
if (!strnicmp(argv[i],(char*)"config=",7)) configFile = argv[i]+7;
}
currentRuleOutputBase = currentOutputBase = ourMainOutputBuffer = (char*)malloc(outputsize);
currentOutputLimit = outputsize;
// need buffers for things that run ahead like servers and such.
maxBufferSize = (maxBufferSize + 63);
maxBufferSize &= 0xffffffc0; // force 64 bit align on size
unsigned int total = maxBufferLimit * maxBufferSize;
buffers = (char*) malloc(total); // have it around already for messages
if (!buffers)
{
(*printer)((char*)"%s",(char*)"cannot allocate buffer space");
return 1;
}
bufferIndex = 0;
readBuffer = AllocateBuffer();
baseBufferIndex = bufferIndex;
quitting = false;
echo = true;
ReadConfig();
ProcessArguments(argc,argv);
MakeDirectory(tmp);
if (argumentsSeen) (*printer)("\r\n");
argumentsSeen = false;
InitTextUtilities();
sprintf(logFilename,(char*)"%s/log%d.txt",logs,port); // DEFAULT LOG
#ifdef LINUX
if (forkcount > 1) {
sprintf(serverLogfileName, (char*)"%s/serverlog%d-%d.txt", logs, port,getpid());
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d-%d.txt", logs, port, getpid());
}
else {
sprintf(serverLogfileName, (char*)"%s/serverlog%d.txt", logs, port);
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d.txt", logs, port);
}
#else
sprintf(serverLogfileName,(char*)"%s/serverlog%d.txt",logs,port); // DEFAULT LOG
sprintf(dbTimeLogfileName, (char*)"%s/dbtimelog%d.txt", logs, port);
#endif
strcpy(livedata,(char*)"LIVEDATA"); // default directory for dynamic stuff
strcpy(systemFolder,(char*)"LIVEDATA/SYSTEM"); // default directory for dynamic stuff
sprintf(languageFolder,"LIVEDATA/%s",language); // default directory for dynamic stuff
if (redo) autonumber = true;
// defaults where not specified
if (userLog == LOGGING_NOT_SET) userLog = 1; // default ON for user if unspecified
if (serverLog == LOGGING_NOT_SET) serverLog = 1; // default ON for server if unspecified
int oldserverlog = serverLog;
serverLog = true;
#ifndef DISCARDSERVER
if (server)
{
#ifndef EVSERVER
GrabPort();
#else
(*printer)("evcalled pid: %d\r\n",getpid());
if (evsrv_init(interfaceKind, port, evsrv_arg) < 0) exit(4); // additional params will apply to each child and they will load data each
#endif
#ifdef WIN32
PrepareServer();
#endif
}
#endif
if (volleyLimit == -1) volleyLimit = DEFAULT_VOLLEY_LIMIT;
for (int i = 1; i < argc; ++i)
{
if (!strnicmp(argv[i], (char*)"build0=", 7)) build0Requested = true;
if (!strnicmp(argv[i], (char*)"build1=", 7)) build1Requested = true;
}
CreateSystem();
for (int i = 1; i < argc; ++i)
{
#ifndef DISCARDSCRIPTCOMPILER
if (!strnicmp(argv[i],(char*)"build0=",7))
{
sprintf(logFilename,(char*)"%s/build0_log.txt",users);
FILE* in = FopenUTF8Write(logFilename);
FClose(in);
commandLineCompile = true;
int result = ReadTopicFiles(argv[i]+7,BUILD0,NO_SPELL);
myexit((char*)"build0 complete",result);
}
if (!strnicmp(argv[i],(char*)"build1=",7))
{
sprintf(logFilename,(char*)"%s/build1_log.txt",users);
FILE* in = FopenUTF8Write(logFilename);
FClose(in);
commandLineCompile = true;
int result = ReadTopicFiles(argv[i]+7,BUILD1,NO_SPELL);
myexit((char*)"build1 complete",result);
}
#endif
if (!strnicmp(argv[i],(char*)"timer=",6))
{
char* x = strchr(argv[i]+6,'x'); // 15000x10
if (x)
{
*x = 0;
timerCheckRate = atoi(x+1);
}
timerLimit = atoi(argv[i]+6);
}
#ifndef DISCARDTESTING
if (!strnicmp(argv[i],(char*)"debug=",6))
{
char* ptr = SkipWhitespace(argv[i]+6);
commandLineCompile = true;
if (*ptr == ':') DoCommand(ptr,mainOutputBuffer);
myexit((char*)"test complete");
}
#endif
if (!stricmp(argv[i],(char*)"trace")) trace = (unsigned int) -1; // make trace work on login
if (!stricmp(argv[i], (char*)"time")) timing = (unsigned int)-1 ^ TIME_ALWAYS; // make timing work on login
}
if (server)
{
struct tm ptm;
#ifndef EVSERVER
Log(SERVERLOG, "\r\n\r\n======== Began server %s compiled %s on host %s port %d at %s serverlog:%d userlog: %d\r\n",version,compileDate,hostname,port,GetTimeInfo(&ptm,true),oldserverlog,userLog);
(*printer)((char*)"\r\n\r\n======== Began server %s compiled %s on host %s port %d at %s serverlog:%d userlog: %d\r\n",version,compileDate,hostname,port,GetTimeInfo(&ptm,true),oldserverlog,userLog);
#else
Log(SERVERLOG, "\r\n\r\n======== Began EV server pid %d %s compiled %s on host %s port %d at %s serverlog:%d userlog: %d\r\n",getpid(),version,compileDate,hostname,port,GetTimeInfo(&ptm,true),oldserverlog,userLog);
(*printer)((char*)"\r\n\r\n======== Began EV server pid %d %s compiled %s on host %s port %d at %s serverlog:%d userlog: %d\r\n",getpid(),version,compileDate,hostname,port,GetTimeInfo(&ptm,true),oldserverlog,userLog);
#endif