This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
/
command1.c
1580 lines (1439 loc) · 44.4 KB
/
command1.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
#include "ts/co.h"
#include "command1.h"
#include "libtsctl.h"
#include "NetTsctl.h"
#include "Mode.h"
#include "shell.h"
/*
reply parsing works as follows.
every reply is prefixed with an class#:cmd# header
our current output mode consists of an encoding and a base. we call a
function corresponding to our current encoding and pass a base formatting
function corresponding to our class and command.
our reply handling functions are continuations, so that if we don't have
enough data to finish the output the continutation will pick up where it
left off when more data arrives.
Mode bases are:
HEX=16
DEC=10
OCT=8
ESC=-1 is escaped binary mode
BIN=0 is binary mode
note: any base accepted by WriteInt is valid
Mode encodings are:
JSON is "name": value, entries
ASSIGN is name=value pairs
NL is values separated by newlines
RAW is unseparated values, probably only useful for binary mode
In JSON and ASSIGN modes the name is appended by a number indicating how
many times that particular function has been called. This is tracked in
the count variable.
We also have the global variable cmdname which has Arrays of command names
for each command for each class.
Upon input to the encoding function, the class#/cmd# has already been read
off the input and we are left with the actual data returned.
We must handle ESC and BIN mode differently from other bases, because our
WriteInt function can't handle these.
in binary output mode, all we really need to know if the length of the
data so we can copy it directly to the output.
int WriteArrayInt8AsBin(coParm,Stream *out,Stream *in,int base) {
StreamCopy(out,in,1);
}
int WriteArrayInt16AsBin(coParm,Stream *out,Stream *in,int base) {
WriteInt16LE(out,ReadInt16LE(in));
}
For escaped binary output, we must know the length of the data as well,
but instead of copying it directly to the output, we must filter it
through an escaping function which converts all special characters into
escape sequences.
*/
//int (****binarybasefunc)(coParm,Stream *,Stream *,int,int);
//int (****escapebasefunc)(coParm,Stream *,Stream *,int,int);
/*
JSON : encodefunc = WriteJSONVal
Assign : encodefunc = WriteAssignVal
NoAssign : encodefunc = WriteNLVal
Hex : base=16
Dec : base=10
Oct : base=8
AStr : ?
AHex : ?
ADec : ?
AOct : ?
*/
#if 0
typedef struct {
int base;
int abase;
int (*encodefunc)(coParm,Stream *,Stream *,int,int,int **,
int (**)(coParm,Stream *,Stream *,int,int),
int,int);
} modeinfo;
int WriteJSONVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase);
int WriteAssignVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase);
int WriteNLVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase);
int WriteRawVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase);
#if 0
// generate output for as many commands in input as are available
// yields 0 if in the middle of a command but needs more input
// returns 1 when finished processing and no more input available
// UNUSED
int tsctlOutputReply(coParm,Stream *out,Stream *in,int** count,modeinfo *mode){
coBeginContext;
void *context;
int16 class;
int8 cmd;
int (**basefunc)(coParm,Stream *,Stream *,int,int);
int (**abasefunc)(coParm,Stream *,Stream *,int,int);
coEndContext;
coBegin;
co(context) = 0;
while (InputReady(in) > 0) {
while (InputReady(in) < 3) coYield(0);
co(class) = ReadInt16LE(in);
co(cmd) = ReadInt8LE(in);
if (co(class) == NetModeClass) {
switch (co(cmd)) {
case NetMode_JSON: mode->encodefunc = WriteJSONVal; break;
case NetMode_Assign: mode->encodefunc = WriteAssignVal; break;
case NetMode_NoAssign: mode->encodefunc = WriteNLVal; break;
case NetMode_Hex: mode->base = 16; break;
case NetMode_Dec: mode->base = 10; break;
case NetMode_Oct: mode->base = 8; break;
case NetMode_AStr: mode->abase = 0; break;
case NetMode_AHex: mode->abase = 16; break;
case NetMode_ADec: mode->abase = 10; break;
case NetMode_AOct: mode->abase = 8; break;
}
continue;
}
if (mode->base > 1) {
co(basefunc) = (int (**)(coParm,Stream *,Stream *,int,int))
textbasefunc[co(class)][co(cmd)];
} else if (mode->base == 0) {
co(basefunc) = (int (**)(coParm,Stream *,Stream *,int,int))
binarybasefunc[co(class)][co(cmd)];
} else if (mode->base == -1) {
co(basefunc) = (int (**)(coParm,Stream *,Stream *,int,int))
escapebasefunc[co(class)][co(cmd)];
}
if (mode->abase > 1) {
co(abasefunc) = (int (**)(coParm,Stream *,Stream *,int,int))
textbasefunc[co(class)][co(cmd)];
} else if (mode->abase == 0) {
co(abasefunc) = (int (**)(coParm,Stream *,Stream *,int,int))
binarybasefunc[co(class)][co(cmd)];
} else if (mode->abase == -1) {
co(abasefunc) = (int (**)(coParm,Stream *,Stream *,int,int))
escapebasefunc[co(class)][co(cmd)];
}
while (!mode->encodefunc(&co(context),out,in,co(class),co(cmd),count,
co(basefunc),mode->base,mode->abase)) {
coYield(0);
}
}
coReturn(1);
}
#endif
int WriteJSONVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase) {
coBeginContext;
void *context;
int i;
coEndContext;
coBegin;
co(context) = 0;
for (co(i)=0;co(i) < ArrayLength(basefunc);co(i)++) {
WriteChar(out,'"');
WriteASCIIZ(out,cmdname[obj][cmd]);
WriteInt(out,count[obj][cmd]++,10,0,0);
WriteChar(out,'"');
WriteChar(out,',');
while (!basefunc[co(i)](&co(context),out,in,base,abase)) coYield(0);
WriteChar(out,',');
WriteChar(out,'\n');
}
coReturn(1);
}
int WriteAssignVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase) {
coBeginContext;
void *context;
int i;
coEndContext;
coBegin;
co(context) = 0;
for (co(i)=0;co(i) < ArrayLength(basefunc);co(i)++) {
WriteASCIIZ(out,cmdname[obj][cmd]);
WriteChar(out,'_');
WriteInt(out,count[obj][cmd]++,10,0,0);
WriteChar(out,'=');
while (!basefunc[co(i)](&co(context),out,in,base,abase)) coYield(0);
WriteChar(out,'\n');
}
coReturn(1);
}
int WriteNLVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase) {
coBeginContext;
void *context;
int i;
coEndContext;
coBegin;
for (co(i)=0;co(i) < ArrayLength(basefunc);co(i)++) {
co(context) = 0;
while (!basefunc[co(i)](&co(context),out,in,base,abase)) coYield(0);
WriteChar(out,'\n');
}
coReturn(1);
}
int WriteRawVal(coParm,Stream *out,Stream *in,int obj,int cmd,int** count,
int (**basefunc)(coParm,Stream *,Stream *,int,int),int base,
int abase) {
coBeginContext;
void *context;
int i;
coEndContext;
coBegin;
co(context) = 0;
for (co(i)=0;co(i) < ArrayLength(basefunc);co(i)++) {
while (!basefunc[co(i)](&co(context),out,in,base,abase)) coYield(0);
}
coReturn(1);
}
#endif
/*
What functions need to be implemented for Bin and Esc?
WriteArrayInt8*
WriteArrayUInt8*
WriteArrayInt16*
WriteArrayInt32*
WriteArrayUInt32*
WriteArrayReal32*
WriteVoid*
WriteInt8*
WriteInt16*
WriteInt32*
WriteUInt8*
WriteUInt16*
WriteUInt32*
what is missing?
ArrayUInt16, Real32
*/
int WriteArrayInt8Bin(coParm,Stream *out,Stream *in,int base,int abase) {
WriteInt8LE(out,ReadInt8LE(in));
}
int WriteArrayUInt8Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayInt16Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayInt32Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayUInt32Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayReal32Bin(coParm,Stream *out,Stream *in,int base,int abase) {
// TO DO
}
int WriteVoidBin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteInt8Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteInt16Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteInt32Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteUInt8Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteUInt16Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteUInt32Bin(coParm,Stream *out,Stream *in,int base,int abase) {
}
// =================================
int WriteArrayInt8Esc(coParm,Stream *out,Stream *in,int base,int abase) {
int8 ch = ReadInt8LE(in);
if (ch < 32 || ch > 126) {
out->WriteChar(out,'\\');
out->WriteChar(out,'x');
WriteInt(out,ch,8,3,'0');
} else {
WriteInt8LE(out,ch);
}
}
int WriteArrayUInt8Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayInt16Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayInt32Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayUInt32Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteArrayReal32Esc(coParm,Stream *out,Stream *in,int base,int abase) {
// TO DO
}
int WriteVoidEsc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteInt8Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteInt16Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteInt32Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteUInt8Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteUInt16Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteUInt32Esc(coParm,Stream *out,Stream *in,int base,int abase) {
}
// =================================
int WriteArrayInt8As(coParm,Stream *out,Stream *in,int base,int abase) {
coBeginContext;
unsigned length;
coEndContext;
int i;
coBegin;
while (in->InputReady(in) < 4) coYield(0);
co(length) = ReadInt32LE(in);
while (in->InputReady(in) < co(length)) coYield(0);
for (i=0;i<co(length);i++) {
WriteInt(out,ReadInt8LE(in),base,0,0);
if (i+1<co(length)) out->WriteChar(out,':');
}
coReturn(1);
}
int WriteArrayUInt8As(coParm,Stream *out,Stream *in,int base,int abase) {
coBeginContext;
unsigned length;
coEndContext;
int i;
coBegin;
while (in->InputReady(in) < 4) coYield(0);
co(length) = ReadInt32LE(in);
while (in->InputReady(in) < co(length)) coYield(0);
for (i=0;i<co(length);i++) {
WriteUInt(out,ReadUInt8LE(in),base,0,0);
if (i+1<co(length)) out->WriteChar(out,':');
}
coReturn(1);
}
int WriteArrayInt16As(coParm,Stream *out,Stream *in,int base,int abase) {
coBeginContext;
unsigned length;
coEndContext;
int i;
coBegin;
while (in->InputReady(in) < 4) coYield(0);
co(length) = ReadInt32LE(in);
while (in->InputReady(in) < co(length)) coYield(0);
for (i=0;i<co(length);i++) {
WriteInt(out,ReadInt16LE(in),base,0,0);
if (i+1<co(length)) out->WriteChar(out,':');
}
coReturn(1);
}
int WriteArrayInt32As(coParm,Stream *out,Stream *in,int base,int abase) {
coBeginContext;
unsigned length;
coEndContext;
int i;
coBegin;
while (in->InputReady(in) < 4) coYield(0);
co(length) = ReadInt32LE(in);
while (in->InputReady(in) < co(length)) coYield(0);
for (i=0;i<co(length);i++) {
WriteInt(out,ReadInt32LE(in),base,0,0);
if (i+1<co(length)) out->WriteChar(out,':');
}
coReturn(1);
}
int WriteArrayUInt32As(coParm,Stream *out,Stream *in,int base,int abase) {
coBeginContext;
unsigned length;
coEndContext;
int i;
coBegin;
while (in->InputReady(in) < 4) coYield(0);
co(length) = ReadInt32LE(in);
while (in->InputReady(in) < co(length)) coYield(0);
for (i=0;i<co(length);i++) {
WriteInt(out,ReadUInt32LE(in),base,0,0);
if (i+1<co(length)) out->WriteChar(out,':');
}
coReturn(1);
}
int WriteArrayReal32As(coParm,Stream *out,Stream *in,int base,int abase) {
// TO DO
}
int WriteVoidAs(coParm,Stream *out,Stream *in,int base,int abase) {
}
int WriteInt8As(coParm,Stream *out,Stream *in,int base,int abase) {
if (in->InputReady(in) < 1) return 0;
WriteInt(out,ReadInt8LE(in),base,0,0);
return 1;
}
int WriteInt16As(coParm,Stream *out,Stream *in,int base,int abase) {
if (in->InputReady(in) < 1) return 0;
WriteInt(out,ReadInt16LE(in),base,0,0);
return 1;
}
int WriteInt32As(coParm,Stream *out,Stream *in,int base,int abase) {
if (in->InputReady(in) < 1) return 0;
WriteInt(out,ReadInt32LE(in),base,0,0);
return 1;
}
int WriteUInt8As(coParm,Stream *out,Stream *in,int base,int abase) {
if (in->InputReady(in) < 1) return 0;
WriteInt(out,ReadInt8LE(in),base,0,0);
return 1;
}
int WriteUInt16As(coParm,Stream *out,Stream *in,int base,int abase) {
if (in->InputReady(in) < 1) return 0;
WriteUInt(out,ReadInt16LE(in),base,0,0);
return 1;
}
int WriteUInt32As(coParm,Stream *out,Stream *in,int base,int abase) {
if (in->InputReady(in) < 1) return 0;
WriteUInt(out,ReadInt32LE(in),base,0,0);
return 1;
}
//ArrayAuto(char*,invalidInstStr,ARR("invalid instance"));
extern void**** docmd;
extern char** classname;
extern char*** cmdname;
extern char**** ArgNames;
extern int (*tsctlCommand[])(char *);
extern void**** Args;
extern char**** RetNames;
//=================================
int tsctlDoCommand(Stream *out,Stream *in) {
int class,inst,cmd;
void (*cmdfunc)(void *,Stream *,Stream *);
void *ob = 0;
while (!in->isEOF(in)) {
class = ReadInt16LE(in);
inst = ReadInt8LE(in);
cmd = ReadInt8LE(in);
switch (class) {
case NetSystemClass: ob = SystemInit(inst); break;
case NetBusClass: ob = BusInit(inst); break;
case NetTimeClass: ob = TimeInit(inst); break;
case NetPinClass: ob = PinInit(inst); break;
case NetDIORawClass: ob = DIORawInit(inst); break;
case NetDIOClass: ob = DIOInit(inst); break;
case NetTWIClass: ob = TWIInit(inst); break;
case NetCANClass: ob = CANInit(inst); break;
case NetSPIClass: ob = SPIInit(inst); break;
case NetAIOClass: ob = AIOInit(inst); break;
case NetEDIOClass: ob = EDIOInit(inst); break;
case NetModeClass:
if (cmd >= NetMode_JSON && cmd < XMode_APICount) {
WriteInt16LE(out,class);
WriteInt8LE(out,cmd);
out->Flush(out);
//WriteInt8LE(out,0x80); // end
continue;
}
default: return -1;
}
if (class >= ArrayLength(docmd)) return -2;
if (cmd >= ArrayLength(docmd[class])) return -3;
//WriteInt16LE(out,class);
//WriteInt8LE(out,cmd);
if (ob == 0) {
return tsctlException(out,8,ASCIIZ("invalid instance"),0);
}
cmdfunc = (void (*)(void *,Stream *,Stream *))docmd[class][cmd];
cmdfunc(ob,out,in);
}
return 1;
}
/*
exceptions
class = System
instance = ?
cmd = NetSystem_Note
int = exception number or error code
char* = error string
*/
int tsctlException(Stream *out,int num,char* str,int request) {
WriteUInt16LE(out,NetSystemClass);
if (request) WriteUInt8LE(out,0);
WriteUInt8LE(out,NetSystem_Note);
//if (!request) WriteUInt8LE(out,0x13);
//WriteUInt32LE(out,num);
if (!request) WriteUInt8LE(out,0x50);
WriteInt32LE(out,ArrayLength(str));
WriteArray(out,str);
//if (request) WriteUInt32LE(out,ArrayLength(str));
if (!request) WriteUInt8LE(out,0x80);
ArrayFree(str);
return -num;
}
static void ListClassesHelper(Stream *st) {
int i;
WriteASCIIZ(st,"Class is one of the following:\n");
for (i=0;i<ArrayLength(classname);i++) {
WriteASCIIZ(st,classname[i]);
st->WriteChar(st,' ');
}
st->WriteChar(st,'\n');
}
char* ObjNotSpecifiedStr() {
char* ret = ArrayAlloc(0,1);
Stream *st = StringStreamInit(0,&ret);
ListClassesHelper(st);
st->Fini(st);
return ret;
}
char* ListClasses() {
char* ret = ArrayAlloc(0,1);
Stream *st = StringStreamInit(0,&ret);
WriteASCIIZ(st,"\
tsctl expects commands separated by semi-colons or newlines.\n\
The format of each command is: [<@Host>] <Class> <Function> <Args...>\n\n\
Host is optional and must be preceded by the '@' character. If the\n\
Host is not specified, direct access to the hardware is performed unless\n\
tsctl server is already running, in which case the host 'localhost' is used.\n\n\
If you type a partial command, that command will be added to the\n\
'command stack', which is automatically prefixed to all subsequent commands.\n\
To pop the top element from the stack, enter an empty line.\n\n");
ListClassesHelper(st);
WriteASCIIZ(st,"\n\
To see a list of functions available for a given class, use the command:\n\
<Class> ?\n\
\n\
For a list of command-line options to tsctl, use the --help option.\n");
st->Fini(st);
return ret;
}
char* ObjNotFoundStr(char* class) {
char* ret = ArrayAlloc(0,1);
Stream *st = StringStreamInit(0,&ret);
int i;
WriteF(st,"Invalid class %s. Classes available:\n",class);
for (i=0;i<ArrayLength(classname);i++) {
WriteASCIIZ(st,classname[i]);
st->WriteChar(st,' ');
}
st->WriteChar(st,'\n');
st->Fini(st);
return ret;
}
static void ListFunctionsHelper(Stream *st,int cl) {
int i;
WriteF(st,"Functions available for class %s:\n",classname[cl]);
for (i=0;i<ArrayLength(cmdname[cl]);i++) {
WriteASCIIZ(st,cmdname[cl][i]);
st->WriteChar(st,' ');
}
WriteF(st,"\n\
To see the arguments expected for a given function, use the command:\n\
<%s> <Function> ?",classname[cl]);
}
char* CmdNotSpecifiedStr(char* class,int cl) {
char* ret = ArrayAlloc(0,1);
Stream *st = StringStreamInit(0,&ret);
ListFunctionsHelper(st,cl);
st->Fini(st);
return ret;
}
char* CmdInvalidStr(char* class,int cl,char* cmd) {
char* ret = ArrayAlloc(0,1);
Stream *st = StringStreamInit(0,&ret);
WriteF(st,"Invalid function %s.\n",
cmd,class);
ListFunctionsHelper(st,cl);
st->Fini(st);
return ret;
}
static void ListArgsHelper(Stream *st,int cl,int cmdnum) {
int i;
WriteF(st,"Usage: %s %s ",classname[cl],cmdname[cl][cmdnum]);
for (i=0;i<ArrayLength(ArgNames[cl][cmdnum]);i++) {
WriteF(st,"<%s>",ArgNames[cl][cmdnum][i]);
if (i+1<ArrayLength(ArgNames[cl][cmdnum])) st->WriteChar(st,' ');
}
}
char* CmdTooManyArgs(int cl,int cmdnum,char* cmd) {
char* ret = ArrayAlloc(0,1);
Stream *st = StringStreamInit(0,&ret);
WriteF(st,"Too many arguments to command %s.\n",cmd);
ListArgsHelper(st,cl,cmdnum);
st->Fini(st);
return ret;
}
char* ListArgs(int cl,int cmdnum) {
char* ret = ArrayAlloc(0,1);
Stream *st = StringStreamInit(0,&ret);
ListArgsHelper(st,cl,cmdnum);
st->Fini(st);
return ret;
}
// -1 = class not specified
// -2 = class not found
// -3 = command not specified
// -4 = command not found
// -5 = Init/Fini cannot be called directly
// -6 = insufficient arguments
// -7 = too many arguments
// -8 = instance not found
int tsctlCommandParse(Stream* out,LookupRef* *lu,char** args) {
int class,inst=0,cmd,i;
char *colon;
if (ArrayLength(args) < 1) return tsctlException(out,1,ObjNotSpecifiedStr(),1);
colon = strchr(args[0],':');
if (colon) {
inst = strtol(colon+1,0,0);
//*colon = 0;
}
if (args[0][0] == '?') {
return tsctlException(out,0,ListClasses(),1);
}
class = tsctlClassTextToBin(args[0]);
if (class == -1) return tsctlException(out,2,ObjNotFoundStr(args[0]),1);
if (ArrayLength(args) < 2) {
return tsctlException(out,3,CmdNotSpecifiedStr(args[0],class),1);
}
if (args[1][0] == '?') {
return tsctlException(out,0,CmdNotSpecifiedStr(args[0],class),1);
}
cmd = tsctlCommand[class](args[1]);
if (cmd == -1) {
return tsctlException(out,4,CmdInvalidStr(args[0],class,args[1]),1);
}
if (cmd < 2) return tsctlException(out,5,CmdInvalidStr(args[0],class,args[1]),1);
cmd -= 2;
if (ArrayLength(args) - 2 < ArrayLength(Args[class][cmd])) {
if (args[ArrayLength(args)-1][0] == '?') {
return tsctlException(out,0,ListArgs(class,cmd),1);
} else return -6; // insufficient arguments
}
if (ArrayLength(args) - 2 > ArrayLength(Args[class][cmd])) {
// too many arguments
return tsctlException(out,7,CmdTooManyArgs(class,cmd,args[1]),1);
}
if (args[ArrayLength(args)-1][0] == '?') {
return tsctlException(out,0,ListArgs(class,cmd),1);
}
// TO DO: experimenting with invalid class, instance, and command
WriteInt16LE(out,class);
WriteInt8LE(out,inst);
WriteInt8LE(out,cmd);
for (i=2;i<ArrayLength(args);i++) {
int (*func)(Stream *,LookupRef **,char *) = Args[class][cmd][i-2];
func(out,lu,args[i]);
}
return 1;
// convert each arguments from text to stream
// to do this we need:
// 1. a way to map type numbers to conversion functions
// 2. a conversion to stream function for each type
/* So let's look at a concrete example of this. Take the
command DIO SetAsync GREEN_LED HIGH
a this point we will already know that we have class=5
inst=0
cmd=7
and what we have left is "GREEN_LED", "HIGH"
our autogenerated code for class 5, command 7
must parse 2 arguments of type int (DIOState enum is an int)
But, whenever we have an integer argument, we must accept
a (non-numeric) string as well. For that we put 0 in as the value,
but create an entry in the lookup section.
does the code really have to be auto-generated? probably not.
in fact, we just need a table to look up the type of each argument,
which returns the function to call. The function will take the
string argument and write out its value to the output stream,
and possibly add a lookup to the table.
*/
}
// tsctlArgParseInt8 replaces Input_8
int tsctlArgParseInt8(Stream *out,LookupRef* *lu,char *arg) {
int offset = out->outputOffset;
char *endptr;
int val = strtol(arg,&endptr,0);
WriteInt8LE(out,val);
if (*endptr || (!val && arg[0] != '0')) {
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
}
int tsctlArgParseInt16(Stream *out,LookupRef* *lu,char *arg) {
int offset = out->outputOffset;
char *endptr;
int val = strtol(arg,&endptr,0);
WriteInt16LE(out,val);
if (*endptr || (!val && arg[0] != '0')) {
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
}
int tsctlArgParseInt32(Stream *out,LookupRef* *lu,char *arg) {
int offset = out->outputOffset;
char *endptr;
int val = strtol(arg,&endptr,0);
WriteInt32LE(out,val);
if (*endptr || (!val && arg[0] != '0')) {
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
}
int tsctlArgParseUInt8(Stream *out,LookupRef* *lu,char *arg) {
int offset = out->outputOffset;
char *endptr;
int val = strtoul(arg,&endptr,0);
WriteInt8LE(out,val);
if (*endptr || (!val && arg[0] != '0')) {
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
}
int tsctlArgParseUInt16(Stream *out,LookupRef* *lu,char *arg) {
int offset = out->outputOffset;
char *endptr;
int val = strtoul(arg,&endptr,0);
WriteInt16LE(out,val);
if (*endptr || (!val && arg[0] != '0')) {
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
}
int tsctlArgParseUInt32(Stream *out,LookupRef* *lu,char *arg) {
int offset = out->outputOffset;
char *endptr;
int val = strtoul(arg,&endptr,0);
WriteInt32LE(out,val);
if (*endptr || (!val && arg[0] != '0')) {
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
}
int tsctlArgParseArrayInt8(Stream *out,LookupRef* *lu,char *arg) {
char** n = split(arg,':');
int i;
if (ArrayLength(n) > 1) {
WriteInt32LE(out,ArrayLength(n));
for (i=0;i<ArrayLength(n);i++) {
WriteInt8LE(out,strtol(n[i],0,0));
}
} else {
WriteInt32LE(out,ArrayLength(arg));
WriteArray(out,arg);
}
ArrayFreeFree(n);
}
int tsctlArgParseArrayInt16(Stream *out,LookupRef* *lu,char *arg) {
char** n = split(arg,':');
int i;
char *endptr;
short val;
if (ArrayLength(n) > 1) {
WriteInt32LE(out,ArrayLength(n));
for (i=0;i<ArrayLength(n);i++) {
val = strtol(n[i],&endptr,0);
if (*endptr || (!val && n[i][0] != '0')) {
int offset = out->outputOffset;
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
WriteInt16LE(out,val);
}
} else {
if (ArrayLength(arg) == 0) {
WriteInt32LE(out,0);
} else {
WriteInt32LE(out,1);
val = strtol(arg,&endptr,0);
if (*endptr || (!val && n[i][0] != '0')) {
int offset = out->outputOffset;
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
WriteInt16LE(out,val);
}
}
ArrayFreeFree(n);
}
int tsctlArgParseArrayInt32(Stream *out,LookupRef* *lu,char *arg) {
char** n = split(arg,':');
char *endptr;
int i,val;
if (ArrayLength(n) > 1) {
WriteInt32LE(out,ArrayLength(n));
for (i=0;i<ArrayLength(n);i++) {
val = strtol(n[i],&endptr,0);
if (*endptr || (!val && n[i][0] != '0')) {
int offset = out->outputOffset;
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
WriteInt32LE(out,val);
}
} else {
if (ArrayLength(arg) == 0) {
WriteInt32LE(out,0);
} else {
WriteInt32LE(out,1);
val = strtol(arg,&endptr,0);
if (*endptr || (!val && n[i][0] != '0')) {
int offset = out->outputOffset;
LookupRef *entry = LookupRefName(lu,arg);
entry->offset = ArrayQueue(entry->offset,A(int,offset));
}
WriteInt32LE(out,val);
}
}
ArrayFree(n);
}
int tsctlArgParseArrayUInt8(Stream *out,LookupRef* *lu,char *arg) {
char** n = split(arg,':');
int i;
if (ArrayLength(n) > 1) {
WriteInt32LE(out,ArrayLength(n));
for (i=0;i<ArrayLength(n);i++) {
WriteInt8LE(out,strtoul(n[i],0,0));
}
} else {
WriteInt32LE(out,ArrayLength(arg));
WriteArray(out,arg);
}
ArrayFree(n);
}
int tsctlArgParseReal32(Stream *out,LookupRef* *lu,char *arg) {
WriteReal32LE(out,strtof(arg,0));
}
int tsctlArgParseArrayReal32(Stream *out,LookupRef* *lu,char *arg) {
char** n = split(arg,':');
float f;
int i;
if (ArrayLength(n) > 1) {
WriteInt32LE(out,ArrayLength(n));
for (i=0;i<ArrayLength(n);i++) {
WriteReal32LE(out,strtof(n[i],0));
}
} else {
if (ArrayLength(arg) == 0) {
WriteInt32LE(out,0);
} else {
WriteInt32LE(out,1);
WriteReal32LE(out,strtof(arg,0));
}
}
ArrayFree(n);
}
// IDEA FOR TAGGED DATA
// 1. header
// byte 0-1 = class
// byte 2 = function
// 2. fields
// one entry for each field. terminated by a void field
// byte 0
// bit 7:6 = type
// 00 = number
// 01 = array of numbers
// 10 = structure
// 11 = void
// bit 5 = signed (number or array only)
// bit 4:0 = element width or structure length (1-32)
// byte 1-4 = array length (array only)
// TAGGED DATA
// consists of
// Array/NotArray Signed/Unsigned ElementWidth [ElementCount]
// byte 0 = Flags
// bit 7 = Array
// bit 6 = Signed
// bit 5 = Struct
// bit 4 = void (no element width follows)
// bit 3 = Enum
// byte 1 = ElementWidth 1-256 (0 means 256)
// if Struct bit is set, means the number of elements
// if Enum bit is set, means the specified enum follows (32-bit)
// byte 2-5 present only if Array bit set, contains little-endian ElementCount
// if bit 5 is set, then bit 7 is implied (but should/must still be set)
// because data returned in structs must always be done via an Array, even
// however, if the array length is 1, then is this REALLY needed???
// if it is a fixed length 1 array.
// if bit 5 is set, each element is itself treated as tagged data
// fixme: if the struct is also an Array, then what?
// whether or not protocol data from the server is tagged should be a switch
// in the server itself.
// how does our auto-generated code tag data?
// idea: before
// WriteInt32LE(out,ArrayLength(arg3));
// we put:
// WriteInt8LE(out,0x40); // signed
// WriteInt8LE(out,4); // 32 bits
#define WAITBYTES(n) while (in->InputReady(in) < n) coYield(0);
// the mode, if needed, should define these functions such that
// as needed, it tracks the current position in the output
// (using Start/End Array/Struct)
// and uses that to pick the correct tag to output for each value
//
// call mode->InitState() to initialize the state pointer before calling
// coWriteTagged, and free it when done.
/*
mode functions for:
1. JSON
2. Assign
3. NL
4. Raw
*/
mode ModeRaw = {
.InitState = 0,
.StartArray = 0,
.EndArray = 0,
.StartNonArray = 0,
.EndNonArray = 0,
.StartStruct = 0,
.EndStruct = 0,
.StartValue = 0,
.EndValue = 0,
.StartEnum = 0,
.EndEnum = 0,
.Separator = 0