forked from ChatScript/ChatScript
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathoutputSystem.cpp
1207 lines (1136 loc) · 41.4 KB
/
outputSystem.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"
unsigned int maxOutputUsed = 0;
unsigned int currentOutputLimit = MAX_BUFFER_SIZE; // max size of current output base
char* currentOutputBase = NULL; // current base of buffer which must not overflow
char* currentRuleOutputBase = NULL; // the partial buffer within outputbase started for current rule, whose output can be canceled.
#define MAX_OUTPUT_NEST 50
static char* oldOutputBase[MAX_OUTPUT_NEST];
static char* oldOutputRuleBase[MAX_OUTPUT_NEST];
static unsigned int oldOutputLimit[MAX_OUTPUT_NEST];
int oldOutputIndex = 0;
unsigned int outputNest = 0;
static char* ProcessChoice(char* ptr,char* buffer,FunctionResult &result,int controls) ;
static char* Output_Function(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result,bool once);
static char* Output_Dollar(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result,bool once,bool nojson);
#ifdef JUNK
Special strings:
When you put ^"xxxx" as a string in a normal output field, it is a format string. By definition you didnt need it compileable.
It will be treated...
When you use it as an argument to a function (top level), it is compiled.
When you put ^"xxxx" as a string in a table, it will be compiled and be either a pattern compile or an output compile.
Internally that becomes "^xxx" which is fully executable.
#endif
bool SafeCopy(char* output, char* word, bool space)
{
size_t len = strlen(word);
if (((output - currentOutputBase) + len ) > (currentOutputLimit - 200))
{
ReportBug((char*)"output buffer too big for copy %s\r\n",output) // buffer overflow
return false;
}
if (space) {*output++ = ' '; *output = 0;}
strcpy(output,word);
return true;
}
void ResetOutput()
{
outputNest = 0;
}
void PushOutputBuffers()
{
oldOutputBase[oldOutputIndex] = currentOutputBase;
oldOutputRuleBase[oldOutputIndex] = currentRuleOutputBase;
oldOutputLimit[oldOutputIndex] = currentOutputLimit;
++oldOutputIndex;
if (oldOutputIndex == MAX_OUTPUT_NEST)
{
ReportBug("PushOutputBuffer limit reached");
--oldOutputIndex; // just fail it
}
}
void PopOutputBuffers()
{
--oldOutputIndex;
if (oldOutputIndex < 0) ++oldOutputIndex;
currentOutputBase = oldOutputBase[oldOutputIndex];
currentRuleOutputBase = oldOutputRuleBase[oldOutputIndex];
currentOutputLimit = oldOutputLimit[oldOutputIndex];
}
void AllocateOutputBuffer()
{
PushOutputBuffers();
currentRuleOutputBase = currentOutputBase = AllocateBuffer(); // cant use stack- others may allocate on it from output and we cant free them
currentOutputLimit = maxBufferSize;
*currentOutputBase = 0;
}
void FreeOutputBuffer()
{
FreeBuffer(); // presumed the current buffer allocated via AllocateOutputBuffer
PopOutputBuffers();
}
static bool IsAssignmentOperator(char* word)
{
if ((*word == '<' || *word == '>') && word[1] == *word && word[2] == '=') return true; // shift operators
if (*word == '|' && word[1] == '^' && word[2] == '=') return true;
return ((*word == '=' && word[1] != '=' && word[1] != '>') || (*word && *word != '!' && *word != '\\' && *word != '=' && word[1] == '=' )); // x = y, x *= y
}
char* GetCommandArg(char* ptr, char* buffer,FunctionResult& result,unsigned int control)
{
int oldImpliedSet = impliedSet; // so @0object will decode
if (!(control & ASSIGNMENT)) impliedSet = ALREADY_HANDLED;
if (control == 0) control |= OUTPUT_KEEPSET | OUTPUT_ONCE | OUTPUT_NOCOMMANUMBER;
else control |= OUTPUT_ONCE | OUTPUT_NOCOMMANUMBER;
unsigned int size = MAX_BUFFER_SIZE - (buffer - currentOutputBase); // how much used
if (size > MAX_BUFFER_SIZE) size = MAX_WORD_SIZE * 4; // arbitrary assumption
ptr = FreshOutput(ptr, buffer, result, control, size);
if (!(control & ASSIGNMENT)) impliedSet = oldImpliedSet; // assignment of @0 = ^querytopics needs to be allowed to change to alreadyhandled
return ptr;
}
char* ReadShortCommandArg(char* ptr, char* buffer,FunctionResult& result,unsigned int control) // always word size or less
{
int oldImpliedSet = impliedSet; // so @0object will decode
if (!(control & ASSIGNMENT)) impliedSet = ALREADY_HANDLED;
if (control == 0) control |= OUTPUT_KEEPSET | OUTPUT_NOTREALBUFFER | OUTPUT_ONCE | OUTPUT_NOCOMMANUMBER;
else control |= OUTPUT_ONCE | OUTPUT_NOCOMMANUMBER| OUTPUT_NOTREALBUFFER ;
char* answer = FreshOutput(ptr,buffer,result,control,MAX_WORD_SIZE);
if (!(control & ASSIGNMENT)) impliedSet = oldImpliedSet; // assignment of @0 = ^querytopics needs to be allowed to change to alreadyhandled
return answer;
}
static char* FixFormatOutput(char* output,unsigned int controls) // revises output in place
{
size_t len = strlen(output);
if (*output == '"' && output[len-1] == '"' && controls & OUTPUT_NOQUOTES) // strip quotes
{
output[--len] = 0; // remove trailing
memmove(output,output+1,len--);
}
if (controls & OUTPUT_NOUNDERSCORE)
{
char* at = output;
while (( at = strchr(at,'_'))) *at = ' ';
}
return output + len;
}
bool LegalVarChar(char at)
{
return (IsAlphaUTF8OrDigit(at) || at == '_' || at == '-' );
}
static char* ReadUserVariable(char* input, char* var)
{
char* at = input++; // skip $ and below either $ or _ if one exists or first legal char
while (LegalVarChar(*++input) || *input == '.' || *input == '[' || *input == ']' || (*input == '$' && *(input-1) == '.'))
{
if (*input == '.' || *input == '[' || *input == ']')
{
if (!LegalVarChar(input[1]) && input[1] != '$' && input[1] != '[') break; // not a var dot, just an ordinary one
}
}
strncpy(var,at,input-at);
var[input-at] = 0;
return input;
}
static char* ReadMatchVariable(char* input, char* var)
{
char* at = input;
while (IsDigit(*++input)){;}
strncpy(var,at,input-at);
var[input-at] = 0;
return input;
}
void ReformatString(char starter, char* input,char*& output, FunctionResult& result,unsigned int controls, char* space) // take ^"xxx" format string and perform substitutions on variables within it
{
*output = 0;
controls |= OUTPUT_NOCOMMANUMBER; // never reformat a number from here
size_t len = strlen(input);
if (!len)
return;
--len;
char c = input[len];
char* original = input;
input[len] = 0; // remove closing "
if (*input == ':') // has been compiled by script compiler. safe to execute fully. actual string is "^:xxxxx"
{
++input;
Output(input,output,result,controls); // directly execute the content but no leading space
input[len] = c;
return;
}
bool str = false;
char* start = output;
*output = 0;
char mainValue[3];
mainValue[1] = 0;
char var[200]; // no variable name should be this big
char prior = 0;
while (input && *input)
{
if (*input == '"' && prior != '\\') str = !str; // toggle string status
prior = *input;
if (prior == '^' && input[1] == USERVAR_PREFIX && (IsAlphaUTF8(input[2]) || input[2] == '$')) // ^ user variable
{
var[0] = '^';
input = ReadUserVariable(input+1,var+1); // end up after the var
Output(var,output,result,controls);
output = FixFormatOutput(output,controls);
}
else if (prior == '^' && input[1] == '_' && IsDigit(input[2])) // ^ canonical match variable
{
var[0] = '^';
input = ReadMatchVariable(input+1,var+1); // end up after the var
Output(var,output,result,controls);
output = FixFormatOutput(output,controls);
}
else if (prior == USERVAR_PREFIX && (IsAlphaUTF8(input[1]) || input[1] == TRANSIENTVAR_PREFIX || input[1] == LOCALVAR_PREFIX)) // user variable
{
input = ReadUserVariable(input,var); // end up after the var
Output(var,output,result,controls);
output = FixFormatOutput(output,controls);
}
else if (prior == SYSVAR_PREFIX && IsAlphaUTF8(input[1]))
{
input = ReadCompiledWord(input,var,false,true);
char* at = var;
while (*++at && IsAlphaUTF8(*at)){;}
input -= strlen(at);
*at = 0;
char* value = SystemVariable(var,NULL);
if (*value)
{
strcpy(output,value);
output = FixFormatOutput(output,controls);
}
else if (!FindWord(var))
{
strcpy(output,var);
output = FixFormatOutput(output,controls); // not a system variable
}
}
else if (prior == '_' && IsDigit(input[1]) && *(input-1) != '@') // canonical match variable
{
input = ReadMatchVariable(input,var); // end up after the var
Output(var,output,result,controls);
output = FixFormatOutput(output,controls);
}
else if (prior == '\'' && input[1] == '_' && IsDigit(input[2])) // quoted match variable
{
var[0] = '\'';
input = ReadMatchVariable(input+1,var+1); // end up after the var
Output(var,output,result,controls);
output = FixFormatOutput(output,controls);
}
else if (prior == '^' && input[1] == '\'' && input[2] == '_' && IsDigit(input[3])) // ^ quoted match variable
{
var[0] = '^';
var[1] = '\'';
input = ReadMatchVariable(input+2,var+2); // end up after the var
Output(var,output,result,controls);
output = FixFormatOutput(output,controls);
}
else if (prior == '@' && IsDigit(input[1])) // factset
{
input = ReadCompiledWord(input,var,false,true);
// go get value of reference
GetCommandArg(var,output,result,0);
output = FixFormatOutput(output,controls);
if (result & ENDCODES)
{
output = start + 1; // null return
break;
}
}
else if (prior == '^' && IsDigit(input[1])) // function variable
{
char* base = input;
while (*++input && IsDigit(*input)){;} // find end of function variable name
char* tmp = FNVAR(base+1);
// if tmp turns out to be $var or _var %var, need to recurse to get it
if (*tmp == LCLVARDATA_PREFIX && tmp[1] == LCLVARDATA_PREFIX)
{
strcpy(output,tmp+2);
output = FixFormatOutput(output,controls); // is already evaled
}
else if (*tmp == USERVAR_PREFIX && !IsDigit(tmp[1])) // user variable (could be json object ref)
{
strcpy(output,GetUserVariable(tmp));
output = FixFormatOutput(output,controls);
}
else if (*tmp == '_' && IsDigit(tmp[1])) // canonical match variable
{
char* wildbase = tmp++;
if (IsDigit(*tmp)) ++tmp; // 2nd digit
strcpy(output,GetwildcardText(GetWildcardID(wildbase),true));
output = FixFormatOutput(output,controls);
}
else if (*tmp == '\'' && tmp[1] == '_' && IsDigit(tmp[2])) // quoted match variable
{
char* wildbase = ++tmp;
++tmp;
if (IsDigit(*tmp)) ++tmp; // 2nd digit
strcpy(output,GetwildcardText(GetWildcardID(wildbase),false));
output = FixFormatOutput(output,controls);
}
else if (*tmp == SYSVAR_PREFIX && IsAlphaUTF8(tmp[1])) // system variable
{
char* value = SystemVariable(tmp,NULL);
if (*value)
{
strcpy(output,value);
output = FixFormatOutput(output,controls);
}
else if (!FindWord(tmp))
{
strcpy(output,tmp);
output = FixFormatOutput( output,controls); // not a system variable
}
}
else if (*tmp == FUNCTIONSTRING && (tmp[1] == '"' || tmp[1] == '\''))
{
ReformatString(tmp[1],tmp+2,output,result,controls);
output += strlen(output);
}
else if (!stricmp(tmp,(char*)"null")) {;} // value is to be ignored
else
{
strcpy(output,tmp);
output = FixFormatOutput(output,controls);
}
}
else if (prior == '^' && (IsAlphaUTF8(input[1]) ))
{
char* at = var;
*at++ = *input++;
while (IsLegalNameCharacter(*input) ) *at++ = *input++;
*at = 0;
if (output != start && *(output-1) == ' ') --output; // no space before
input = Output_Function(var, input, NULL, output, controls,result,false);
output += strlen(output);
if (result & ENDCODES) return;
}
else if (prior == '\\') // protected special character
{
++input;
if (starter == '"' && *input == 'n')
{
strcpy(output,"\\n");
output += 2;
}
else if (starter == '"' && *input == 't')
{
strcpy(output,"\\t");
output += 2;
}
else if (starter == '"' && *input == 'r')
{
strcpy(output,"\\r");
output += 2;
}
else if (starter == '"') *output++ = *input; // just pass along the protected char in ^"xxx" strings
else // is ^'xxxx' string - other than our special ' we need, leave all other escapes alone as legal json
{
if (str) *output++ = *input; // copy inside a string
else if (*input == '\'' ) *output++ = *input; // cs required the \, not in final output
else {*output++ = '\\'; *output++ = *input;} // json can escape anything, particularly doublequote
}
++input; // skip over the specialed character
}
else // ordinary character
{
*mainValue = *input++;
strcpy(output,mainValue);
output = FixFormatOutput(output,controls);
}
*output = 0;
}
original[len] = c;
*output = 0; // when failures, return the null string
if (trace & TRACE_OUTPUT) Log(STDTRACELOG,(char*)" %s",start);
}
void StdNumber(char* word,char*& buffer,int controls) // text numbers may have sign and decimal
{
size_t len = strlen(word);
char* ptr = word;
if ( IsAlphaUTF8(*ptr) || !IsDigitWord(word, AMERICAN_NUMBERS) || strchr(word,':')) // either its not a number or its a time - leave unchanged
{
strcpy(buffer,word);
if (controls & OUTPUT_NOUNDERSCORE)
{
char* at = buffer;
while (( at = strchr(at,'_'))) *at = ' ';
}
return;
}
// capture any percentage symbol
char* end = word + len;
bool percent = false;
if (*(end - 1) == '%')
{
*--end = 0;
percent = true;
}
int useNumberStyle = numberStyle;
if (controls & OUTPUT_NOCOMMANUMBER || len < 5) useNumberStyle = NOSTYLE_NUMBERS;
if (IsFloat(word,end))
{
if (!fullfloat) // insure is not full
{
char c = word[len];
word[len] = 0;
WriteFloat(buffer, atof(word), useNumberStyle);
word[len] = c;
}
else // write out what we have, don't redo the formatting
{
FormatFloat(word, buffer, useNumberStyle);
}
}
else
{
// add commas between number triples, unless not needed
// except india uses doubles until final triple
WriteInteger(word, buffer, useNumberStyle);
}
if (percent) strcat(buffer, "%");
}
char* StdIntOutput(int n)
{
char buffer[50];
static char answer[50];
*answer = 0;
#ifdef WIN32
sprintf(buffer,(char*)"%I64d",(long long int) n);
#else
sprintf(buffer,(char*)"%lld",(long long int) n);
#endif
char* ptr = answer;
StdNumber(buffer,ptr,0);
return answer;
}
static char* ProcessChoice(char* ptr,char* buffer,FunctionResult &result,int controls) // given block of [xx] [yy] [zz] randomly pick one
{
char* choice[CHOICE_LIMIT];
char** choiceset = choice;
int count = 0;
char* endptr = 0;
// gather choices
while (*ptr && *ptr == '[') // get next choice for block may not nest choice blocks...
{
// find closing ]
endptr = ptr-1;
while (ALWAYS)
{
endptr = strchr(endptr+1,']'); // find a closing ]
if (!endptr) // failed
{
respondLevel = 0;
return 0;
}
if (*(endptr-1) != '\\') break; // ignore literal \[
}
// choice can be simple: [ xxx ] or conditional [ $var1 xxx] or conditional [!$var1 xx] but avoid assignment [ $var1 = 10 xxx ]
char word[MAX_WORD_SIZE];
char* base = ptr + 1; // start of 1st token within choice
char* simpleOutput = ReadCompiledWord(base,word);
char* var = word;
bool notted = false;
if (*word == '!')
{
notted = true;
++var;
}
if (*var == USERVAR_PREFIX && (IsAlphaUTF8(var[1]) || var[1] == TRANSIENTVAR_PREFIX || var[1] == LOCALVAR_PREFIX)) // user variable given
{
ReadCompiledWord(simpleOutput,tmpWord);
if (*tmpWord == '=' || tmpWord[1] == '=') choiceset[count++] = base; // some kind of assignment, it's all simple output
else if (!notted && !*GetUserVariable(var)) {;} // user variable test fails
else if (notted && *GetUserVariable(var)) {;} // user variable test fails
else choiceset[count++] = simpleOutput;
}
else choiceset[count++] = base;
ptr = SkipWhitespace(endptr + 1); // past end of choice
}
// pick a choice randomly - a choice starts past the [ but has visible the closing ]
respondLevel = 0;
while (count > 0)
{
int r = random(count);
if (outputchoice >= 0 && outputchoice < count) r = outputchoice; // forced by user
char* ptr = SkipWhitespace(choiceset[r]);
if (*ptr == ']') break; // choice does nothing by intention
char level = 0;
if (IsAlphaUTF8(*ptr) && ptr[1] == ':' && ptr[2] == ' ')
{
level = *ptr;
ptr += 3; // skip special rejoinder
}
Output(ptr,buffer,result,controls);
if (result & ENDCODES) break; // declared done
// is choice a repeat of something already said... if so try again
if (*buffer && HasAlreadySaid(buffer))
{
if (trace & TRACE_OUTPUT) Log(STDTRACELOG,(char*)"Choice %s already said\r\n",buffer);
*buffer = 0;
choiceset[r] = choiceset[--count];
}
else
{
if (level) respondLevel = level;
break; // if choice didnt fail, it has completed, even if it generates no output
}
}
return (endptr[1]) ? (endptr+2) : (endptr+1); // skip to end of rand past the ] and space
}
char* FreshOutput(char* ptr,char* buffer,FunctionResult &result,int controls,unsigned int limit)
{ // used to get isolated values not normally part of real output stream
++outputNest;
if (limit != maxBufferSize) AllocateOutputBuffer(); // where he wants to put it is SMALL and we're not ready for that. allocate a big bufer can copy later
else
{
PushOutputBuffers();
currentRuleOutputBase = currentOutputBase = buffer; // is a normal buffer
}
ptr = Output(ptr,currentOutputBase,result,controls); // has no content before it
if (limit != maxBufferSize) // someone's small local buffer
{
size_t olen = strlen(currentRuleOutputBase);
if (olen >= limit)
{
strncpy(buffer,currentRuleOutputBase,limit-1);
buffer[limit-1] = 0;
ReportBug((char*)"FreshOutput of %d exceeded caller limit of %d. Truncated: %s\r\n",olen,limit,currentRuleOutputBase);
}
else strcpy(buffer,currentRuleOutputBase);
FreeOutputBuffer();
}
else PopOutputBuffers();
--outputNest;
return ptr;
}
#ifdef INFORMATION
There are two kinds of output streams. The ONCE only stream expects to read an item and return.
If a fail code is hit when processing an item, then if the stream is ONCE only, it will be done
and return a ptr to the rest. If a general stream hits an error, it has to flush all the remaining
tokens and return a ptr to the end.
#endif
static char* Output_Percent(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result,bool once)
{
// Handles system variables: %date
// Handles any other % item - %
if (IsAlphaUTF8(word[1])) // must be a system variable
{
if (!once && IsAssignmentOperator(ptr)) return PerformAssignment(word,ptr,buffer,result); // = or *= kind of construction
strcpy(word,SystemVariable(word,NULL));
}
if (*word) strcpy(buffer,word);
return ptr;
}
static char* Output_Backslash(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result)
{
// handles newline: \n
// handles backslashed strings: \"testing" means dump the rest of the token out
// handles backslashed standalone double quote - \" - means treat as even/odd pair and on 2nd one (closing) do not space before it
// handles any other backslashed item: \help means just put out the item without the backslash
if (word[1] == 'r' && !word[2])
{
strcpy(buffer,(char*)"\\r");
return ptr;
}
if (word[1] == 'n') // \n
{
if (space) --buffer; // remove space before newline
#ifdef WIN32
strcpy(buffer,(char*)"\\r\\n");
#else
strcpy(buffer,(char*)"\\n");
#endif
ptr -= strlen(word);
if (*ptr == 'n') --ptr;
ptr += 2;
}
else if (word[1] == 't') // tab
{
strcpy(buffer,(char*)"\\t");
ptr -= strlen(word);
if (*ptr == 't') --ptr;
ptr += 2;
}
else strcpy(buffer,word+1); // some other random backslashed content, including \"
return ptr;
}
static char* ResetOutputPtr(char* start,char* buffer)
{
char* at = strrchr(start,'`'); // where it ended
if (!at) return start;
size_t len = strlen(at+1);
memmove(start,at+1,len+1); // shift new data back to start
if (currentOutputBase != start && start[len-1] == ' ') --len; // output ended with a space, remove it
start[len] = 0;
return start + len; // resume back at original buffer location
}
static char* Output_Function(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result,bool once)
{
char* start = buffer;
if (IsDigit(word[1])) // function variable
{
if (!once && IsAssignmentOperator(ptr)) ptr = PerformAssignment(word,ptr,buffer,result);
else
{
char* value = FNVAR(word+1);
size_t len = strlen(value);
size_t size = (buffer - currentOutputBase);
if ((size + len) >= (currentOutputLimit-50) )
{
result = FAILRULE_BIT;
return ptr;
}
if (*value == LCLVARDATA_PREFIX && value[1] == LCLVARDATA_PREFIX)
strcpy(buffer,value+2); // already evaluated. do not reeval
else
{
strcpy(buffer,value);
if (*buffer)
{
*word = ENDUNIT; // marker for retry
word[1] = '^'; // additional marker for function variables
}
}
}
}
else if (word[1] == '"' || word[1] == '\'') ReformatString(word[1],word+2,buffer,result,0,space); // functional string, uncompiled. DO NOT USE function calls within it
else if (word[1] == USERVAR_PREFIX || word[1] == '_' || word[1] == '\'' || (word[1] == '^' && IsDigit(word[2]))) // ^$$1 = null or ^_1 = null or ^'_1 = null or ^^which = null is indirect user assignment or retrieval
{
if (!once && IsAssignmentOperator(ptr)) // we are lefthand side indirect
{
if (word[1] != '^' || !IsDigit(word[2])) // anything but ^^2
{
Output(word+1,buffer,result,controls); // no leading space - we now have the variable value from the indirection
strcpy(word,buffer);
}
*buffer = 0;
ptr = PerformAssignment(word,ptr,buffer,result,true); // = or *= kind of construction -- dont do json indirect assignment
}
else // we are right side (expression) indirect
{
Output(word+1,buffer,result,controls); // no leading space - we now have the variable value from the indirection
if (word[1] == USERVAR_PREFIX) // direct retry to avoid json issues
{
strcpy(word,GetUserVariable(word+1));
Output_Dollar(word, "", space,buffer,controls,result,false,false); // allow json processing
}
else *word = ENDUNIT; // marker for retry
}
}
else if (!strcmp(word,(char*)"^if")) ptr = HandleIf(ptr,buffer,result);
else if (!strcmp(word,(char*)"^loop")) ptr = HandleLoop(ptr,buffer,result);
else if (word[1] == '^') // if and loop
{
if (!once && IsAssignmentOperator(ptr))
ptr = PerformAssignment(word,ptr,buffer,result); // = or *= kind of construction
else if (!word[2]) strcpy(buffer,word); // "^^" exponent operator
else result = FAILRULE_BIT;
}
else // functions or ordinary words
{
if (*ptr != '(' || !word[1])
{
strcpy(buffer,word); // a non function
}
else // ordinary function
{
if (*currentRuleOutputBase && (!strcmp(word,(char*)"^gambit") || !strcmp(word,(char*)"^respond") || !strcmp(word,(char*)"^reuse") || !strcmp(word,(char*)"^retry") || !strcmp(word,(char*)"^refine") || !strcmp(word,(char*)"^print") )) // leaving current rule
{
char* end = currentRuleOutputBase;
if (*currentRuleOutputBase == '`') end = strrchr(currentRuleOutputBase,'`') + 1; // is there really something new there?
if (*end && !AddResponse(currentRuleOutputBase,responseControl)) result = FAILRULE_BIT;
if (*start == '`') buffer = ResetOutputPtr(start,buffer);
}
ptr = DoFunction(word,ptr,buffer,result);
if (space && *space != ' ' && result != ENDCALL_BIT) // we need to add a space, but not if requesting a call return ^return
{
memmove(buffer+1,buffer,strlen(buffer) + 1);
*buffer = ' ';
}
}
}
return ptr;
}
static char* Output_AttachedPunctuation(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result)
{
strcpy(buffer,word);
return ptr;
}
static char* Output_Text(char* word,char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result)
{
// handles text or script
if (*ptr != '(' || controls & OUTPUT_FACTREAD || IsDigit(*word)) StdNumber(word,buffer,controls); // SIMPLE word - paren if any is a nested fact read, or number before ( which cant be ^number
else // function call missing ^
{
memmove(word+1,word,strlen(word)+1);
*word = '^'; // supply ^
ptr = Output_Function(word, ptr,space,buffer, controls,result,false);
if (result == UNDEFINED_FUNCTION) // wasnt a function after all.
{
result = NOPROBLEM_BIT;
StdNumber(word+1,buffer,controls);
}
}
return ptr;
}
static char* Output_AtSign(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result,bool once)
{
// handles factset assignement: @3 = @2
// handles factset field: @3object
if (!once && IsAssignmentOperator(ptr)) ptr = PerformAssignment(word,ptr,buffer,result);
else if (impliedSet != ALREADY_HANDLED) strcpy(buffer,word);
else if (IsDigit(word[1]) && IsAlphaUTF8(*GetSetType(word)) && !(controls & OUTPUT_KEEPQUERYSET)) // fact set reference
{
int store = GetSetID(word);
if (store == ILLEGAL_FACTSET) return ptr;
unsigned int count = FACTSET_COUNT(store);
if (!count || count >= MAX_FIND) return ptr;
FACT* F = factSet[store][1]; // use but don't use up most recent fact
MEANING T;
uint64 flags;
char type = *GetSetType(word);
if (type == 's' )
{
T = F->subject;
flags = F->flags & FACTSUBJECT;
}
else if (type== 'v')
{
T = F->verb;
flags = F->flags & FACTVERB;
}
else if (type == 'f' )
{
T = Fact2Index(F);
flags = FACTSUBJECT;
}
else if (type == 'a' && impliedWild != ALREADY_HANDLED)
{
ARGUMENT(1) = AllocateStack(word);
result = FLR(buffer,(char*)"l");
return ptr;
}
else
{
T = F->object;
flags = F->flags & FACTOBJECT;
}
char* answer;
char buf[100];
if (flags)
{
sprintf(buf,(char*)"%d",T);
answer = buf;
}
else answer = Meaning2Word(T)->word;
strcpy(buffer,answer);
}
else strcpy(buffer,word);
return ptr;
}
static char* Output_Bracket(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result)
{
// handles normal token: [ice
// handles choice: [ this is data ]
if (word[1] || (controls & OUTPUT_NOTREALBUFFER && !(controls & OUTPUT_FNDEFINITION)) || !*ptr) StdNumber(word,buffer,controls); // normal token
else
{
while (*ptr != '[') --ptr;
ptr = ProcessChoice(ptr,buffer,result,controls);
}
return ptr;
}
static char* Output_Quote(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result)
{
// handles possessive: 's
// handles original wildcard: '_2
// handles quoted variable: '$hello
if (word[1] == 's' && !word[2]) strcpy(buffer,(char*)"'s"); // possessive
else if (word[1] == '_') // original wildcard
{
int index = GetWildcardID(word+1); // which one
char* at = wildcardOriginalText[index];
StdNumber(at,buffer,controls);
if (controls & OUTPUT_NOQUOTES && *buffer == '"') // remove quotes from variable data
{
size_t len = strlen(buffer);
if (buffer[len-1] == '"')
{
buffer[len-1] = 0;
memmove(buffer,buffer+1,len);
}
}
}
else if (word[1] == USERVAR_PREFIX || word[1] == '@' || word[1] == SYSVAR_PREFIX) // variable or factset or system variable quoted, means dont reeval its content
{
strcpy(buffer,word+1);
}
else if (word[1] == '^' && IsDigit(word[2])) // function variable quoted, means dont reeval its content
{
size_t len = strlen(FNVAR(word+2));
size_t size = (buffer - currentOutputBase);
if ((size + len) >= (currentOutputLimit-50) )
{
result = FAILRULE_BIT;
return ptr;
}
strcpy(buffer,FNVAR(word+2));
}
else StdNumber(word,buffer,controls);
return ptr;
}
static char* Output_String(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result)
{
// handles function string: (char*)"^ .... " which means go eval the contents of the string
// handles simple string: (char*)"this is a string" which means just put it out (with or without quotes depending on controls)
// handles compiled strings: "^:xxx" which means formatting has already been performed (function arguments)
size_t len;
if (controls & OUTPUT_UNTOUCHEDSTRING && word[1] != FUNCTIONSTRING) strcpy(buffer,word); //functions take untouched strings typically
else if (word[1] == FUNCTIONSTRING) // treat as format string
{
if (!dictionaryLocked) strcpy(buffer,word); // untouched - function strings passed as arguments to functions are untouched until used up. - need untouched so can create facts from tables with them in it
else ReformatString(*word,word+2,buffer,result,controls);
}
else if (controls & OUTPUT_NOQUOTES)
{
strcpy(buffer,word+1);
len = strlen(buffer);
if (buffer[len-1] == '"') buffer[len-1] = 0; // remove end quote
}
else strcpy(buffer,word); // untouched - function strings passed as arguments to functions are untouched until used up. - need untouched so can create facts from tables with them in it
return ptr;
}
static char* Output_Underscore(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result,bool once)
{
// handles wildcard assigment: _10 = hello
// handles wildcard: _19
// handles simple _ or _xxxx
if (!once && IsAssignmentOperator(ptr)) ptr = PerformAssignment(word,ptr,buffer,result);
else if (IsDigit(word[1])) // wildcard
{
int id = GetWildcardID(word);
if (id >= 0)
{
StdNumber(wildcardCanonicalText[id],buffer,controls);
if (controls & OUTPUT_NOQUOTES && *buffer == '"') // remove quotes from variable data
{
size_t len = strlen(buffer);
if (buffer[len-1] == '"')
{
buffer[len-1] = 0;
memmove(buffer,buffer+1,len);
}
}
if (*buffer == '^' && IsAlphaUTF8(buffer[1]) && *SkipWhitespace(ptr) == '(') *word = ENDUNIT; // if fn call substituted, force reeval
}
}
else strcpy(buffer,word); // stand-alone _ or some non wildcard
return ptr;
}
static char* Output_Dollar(char* word, char* ptr, char* space,char*& buffer, unsigned int controls,FunctionResult& result,bool once,bool nojson)
{
// handles user variable assignment: $myvar = 4
// handles user variables: $myvar
// handles US money: $1000.00
if ((word[1] == '$' || word[1] == '_') && !word[2]) StdNumber(word,buffer,controls); // simple $_ or $$
else if (word[1] && !IsDigit(word[1])) // variable
{
if (controls & OUTPUT_EVALCODE && !(controls & OUTPUT_KEEPVAR))
{
char* answer = GetUserVariable(word,nojson);
if (*answer == USERVAR_PREFIX && ( answer[1] == LOCALVAR_PREFIX || answer[1] == TRANSIENTVAR_PREFIX || IsAlphaUTF8(answer[1]))) strcpy(word,answer); // force nested indirect on var of a var value
}
if (!once && IsAssignmentOperator(ptr))
ptr = PerformAssignment(word,ptr,buffer,result);
else
{
char* value = GetUserVariable(word,nojson);
StdNumber(value,buffer,controls);
char* at = SkipWhitespace(buffer);
if (controls & OUTPUT_NOQUOTES && *at == '"') // remove quotes from variable data
{
size_t len = strlen(at);
if (at[len-1] == '"')
{
at[len-1] = 0;
memmove(at,at+1,len);
}
}
if (*at == '"' && at[1] == FUNCTIONSTRING) *word = ENDUNIT; // reeval the function string
}
}
else StdNumber(word,buffer,controls); // money or simple $
return ptr;
}
char* Output(char* ptr,char* buffer,FunctionResult &result,int controls)
{ // moves any special stuff to front of buffer
// an output stream consists of words, special words, [] random zones, commands, C-style script. It autoformats whitespace.
*buffer = 0;
char* start = buffer;
result = NOPROBLEM_BIT;
if (*ptr == ENDUNIT && ptr[1] == ENDUNIT) ptr += 2; // skip over a transient $_ marker
if (!*ptr) return NULL;
bool once = false;
if (controls & OUTPUT_ONCE) // do one token
{
once = true;
controls ^= OUTPUT_ONCE;
}
if (buffer < currentOutputBase || (size_t)(buffer - currentOutputBase) >= (size_t)(currentOutputLimit-200)) // output is wrong or in danger already?
{
result = FAILRULE_BIT;
return ptr;
}
char* word = AllocateBuffer("output"); // allocation from stack doesnt work for unknown reason
++outputlevel; // where we are
bool quoted = false;
char* startQuoted = NULL;
// nested depth assignments from our heap space
int paren = 0;
while (ptr)
{
if (!*start) buffer = start; // we may have built up a lot of buffer, then flushed it along the way. If so, drop back to start to continue.
char* space = NULL;
*buffer = 0;
ptr = SkipWhitespace(ptr); // functions no longer skip blank after end, in case being used by reformat and preserving the space
outputCode[outputlevel] = ptr; // where we are
#ifndef DISCARDTESTING
if (debugAction)
{
(*debugAction)(ptr);
*buffer = 0;
ptr = outputCode[outputlevel]; // can replace where we are
}
#endif
if (!*ptr || (*ptr == ENDUNIT && ptr[1] != ENDUNIT)) break; // out of data
char* priorPtr = ptr;
ptr = ReadCompiledWord(ptr,word,false,true); // stop when $var %var _var @nvar end normally- insure no ) ] } lingers on word in case it wasnt compiled
if (*word == '$' && *ptr == '[' && ptr[1] == ']') // merge $word[]
{
strcat(word, "[]");
ptr += 2;
}
if (*word == '/' && ptr[0] == '(') continue; // JA PATCH for mistaken /(OS)
char* startptr = ptr;
ptr = SkipWhitespace(ptr); // find next token to tes for assignment and the like
if (!*word && !paren ) break; // end of data or choice or body
if (!(controls & OUTPUT_RAW) && (*word == ')' || *word == ']' || *word == '}') && !paren ) break; // end of data or choice or body
if (*word == '[' && word[1]) // break apart uncompiled choice
{
ptr = SkipWhitespace(priorPtr) + 1;
word[1] = 0;
}
else if (*word == FUNCTIONSTRING && (word[1] == '"' || word[1] == '\'' ) ) {;} // function strings can all sorts of stuff in them
else if ((*word == '"') && word[1] == FUNCTIONSTRING) {;} // function strings can all sorts of stuff in them
else if (*word != '$') // separate closing brackets from token in case not run thru script compilation
{
bool quote = false;
char* at = word-1;
while (*++at )
{
if (*at == '\\')
{
++at; // skip over next character
continue;
}
if (*at == '"') quote = !quote;
if (quote) continue; // ignore inside a quote