This repository was archived by the owner on Feb 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
4340 lines (3577 loc) · 116 KB
/
Copy pathparser.c
File metadata and controls
4340 lines (3577 loc) · 116 KB
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
/* START LIBRARY DESCRIPTION *********************************************
PARSER.LIB
Copyright (c) 1997, Avtech Electrosystems Ltd.
DESCRIPTION:
Parser functions.
SUPPORT LIB'S:
END DESCRIPTION **********************************************************/
#include "error_utils.h"
#include "parser.h"
#include "flash.h"
#include "nicutils.h"
#include "version.h"
#include "i2c.h"
#include "lcd.h"
#include "menus.h"
#include "gpib.h"
#include <glib/gprintf.h>
extern void send_message(gchar* message);
//STATICS
static int query_int (gchar** response, int n);
static int query_min_max_float (gchar** response, char *parameter, float min_val, float max_val);
static int query_min_max_int (gchar** response, char *parameter, int min_val, int max_val);
static int query_string (gchar** response, char *in);
static int query_float (gchar** response, float value);
static int process_float_param (char *parameter, float *value, float min_val, float max_val, int zero_mode);
static int process_int_param (char *parameter, int *value, int item_count, int *valid_nums, int allow_on_off);
static int process_int_range (char *parameter, int *value, int min_val, int max_val);
static int process_on_off (char *parameter, int *value);
static int process_two_ints (char *parameter, int *value, int min, int max);
static int check_channel_ok (int channel, int enabled_channels, char chankey);
static int Parser_id_word(char *id_me, int *channel, int *with_id_code);
static int Parser_find_commands(int commands[], int command_depth);
static gchar* filter_input (gchar *raw_in);
static int Parser_get_unit(char **parameter, char **units);
static int Parser_channel (int *channel,int with_id_code,int routine_num);
static int Handle_Units(float *mult,char *units, char *base);
static int Is_Min_Command(char *text);
static int Is_Max_Command(char *text);
static int Go_syst_err_11(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_syst_errcnt66(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_Str_eprom_47(gchar** response, int channel, char *loc_string,char *store_string,int command_type);
static int Go_int_eprom_48(gchar** response, int channel, char *loc_string,char *store_string,int command_type);
static int Go_Float_eprom51(gchar** response, int channel, char *loc_string,char *store_string,int command_type);
static int Go_char_eprom_70(gchar** response, int channel, char *loc_string,char *store_string,int command_type);
static int Go_eprom_siz_86(gchar** response, int channel, int command_type);
static int Go_sys_net_91(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_ampl_26(gchar** response, int channel, char *parameter,char *units,int command_type,char *mode);
static int Go_pw_36(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_duty_37(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_offset_29(gchar** response, int channel, char *parameter,char *units,int command_type,char *mode);
static int Go_idn_5(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_freq_32_33(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_period_35(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_delay_39(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_dbl_pulse_40(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_func_34(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_polarity_42(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_hold_38(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_output_55(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_trig_source46(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_gate_type_56(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_gate_level_67(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_delay_test58(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_wai_10(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_opc_5(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_ese_2(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_esr_3(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_cls_1(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_sre_7(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_stb_8(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_tst_9(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_syst_ver_12(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_event_13(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_oper_enable15(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_ques_enable18(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_preset_19(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_zout_20(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_prot_trip_21(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_gpib_addr_59(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_ser_baud_60(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_ser_rts_64(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_load_68(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_meas_ampl_69(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_rst_6(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_calib_amp_72(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_calib_mon_73(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_mon_step_74(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_outputtype_75(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_calib_os_76(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_calib_pw_77(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_routeclose_78(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Parse_chan_list(int channel,char *parameter,int *primary_selected, int *secondary_selected,gchar** response);
static int Go_dly_shift_82(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_amp_pnt_83(gchar** response, int channel, char *parameter,char *units,int command_type,int cal_type);
static int Go_puls_count_88(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_puls_sep_89(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_sys_pwd_92(gchar** response, int channel, char *parameter,int command_type);
static int Go_eprom_sus_93(gchar** response, int channel, char *parameter,int command_type);
static int Go_rise_time_94(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_rcl_53(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_sav_54(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_soft_current_limit_96(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_curr_slew_98(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_cal_100(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_cal_interval_101(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_eprom_reset_102(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_atten_103(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_eprom_resize_105(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_eprom_resize_ampl_106(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_eprom_resize_offset_107(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Go_cal_checking_disable_108(gchar** response, int channel, char *parameter,char *units,int command_type);
static int Parser_id_word(char *id_me, int *channel, int *with_id_code)
{
int id_code;
int set_new_channel;
set_new_channel=0;
/* detect channel suffixes in the range of 0-99 */
if (isdigit((int) id_me[strlen(id_me)-1])) {
set_new_channel=1;
*channel=atoi(id_me+strlen(id_me)-1);
if (isdigit((int) id_me[strlen(id_me)-2])) {
*channel=atoi(id_me+strlen(id_me)-2);
id_me[strlen(id_me)-2]=0;
} else {
id_me[strlen(id_me)-1]=0;
}
}
if (!strcmp(id_me,"") ) {
id_code = 0;
} else if (!strcmp(id_me,"*cls") ) {
id_code = 1;
} else if (!strcmp(id_me,"*ese") ) {
id_code = 2;
} else if (!strcmp(id_me,"*esr") ) {
id_code = 4;
} else if (!strcmp(id_me,"*idn") ) {
id_code = 5;
} else if (!strcmp(id_me,"*opc") ) {
id_code = 6;
} else if (!strcmp(id_me,"*rst") ) {
id_code = 8;
} else if (!strcmp(id_me,"*sre") ) {
id_code = 9;
} else if (!strcmp(id_me,"*stb") ) {
id_code = 10;
} else if (!strcmp(id_me,"*tst") ) {
id_code = 11;
} else if (!strcmp(id_me,"*wai") ) {
id_code = 12;
} else if (!strcmp(id_me,"system") || !strcmp(id_me,"syst")) {
id_code = 13;
} else if (!strcmp(id_me,"error") || !strcmp(id_me,"err")) {
id_code = 14;
} else if (!strcmp(id_me,"version") || !strcmp(id_me,"vers")) {
id_code = 15;
} else if (!strcmp(id_me,"status") || !strcmp(id_me,"stat") || !strcmp(id_me,"state")) {
id_code = 16;
} else if (!strcmp(id_me,"operation") || !strcmp(id_me,"oper")) {
id_code = 17;
} else if (!strcmp(id_me,"event") || !strcmp(id_me,"even")) {
id_code = 18;
} else if (!strcmp(id_me,"condition") || !strcmp(id_me,"cond")) {
id_code = 19;
} else if (!strcmp(id_me,"enable") || !strcmp(id_me,"enab")) {
id_code = 20;
} else if (!strcmp(id_me,"questionable") || !strcmp(id_me,"ques")) {
id_code = 21;
} else if (!strcmp(id_me,"preset") || !strcmp(id_me,"pres")) {
id_code = 22;
} else if (!strcmp(id_me,"diagnostic") || !strcmp(id_me,"diag")) {
id_code = 23;
} else if (!strcmp(id_me,"output") || !strcmp(id_me,"outp")) {
id_code = 24;
} else if (!strcmp(id_me,"impedance") || !strcmp(id_me,"imp")) {
id_code = 25;
} else if (!strcmp(id_me,"protection") || !strcmp(id_me,"prot")) {
id_code = 26;
} else if (!strcmp(id_me,"tripped") || !strcmp(id_me,"trip")) {
id_code = 27;
} else if (!strcmp(id_me,"source") || !strcmp(id_me,"sour")) {
id_code = 28;
} else if (!strcmp(id_me,"current") || !strcmp(id_me,"curr")) {
id_code = 29;
} else if (!strcmp(id_me,"level") || !strcmp(id_me,"lev")) {
id_code = 30;
} else if (!strcmp(id_me,"immediate") || !strcmp(id_me,"imm")) {
id_code = 31;
} else if (!strcmp(id_me,"amplitude") || !strcmp(id_me,"ampl")) {
id_code = 32;
} else if (!strcmp(id_me,"offset") || !strcmp(id_me,"offs")) {
id_code = 33;
} else if (!strcmp(id_me,"high") ) {
id_code = 34;
} else if (!strcmp(id_me,"low") ) {
id_code = 35;
} else if (!strcmp(id_me,"voltage") || !strcmp(id_me,"volt")) {
id_code = 36;
} else if (!strcmp(id_me,"frequency") || !strcmp(id_me,"freq")) {
id_code = 37;
} else if (!strcmp(id_me,"function") || !strcmp(id_me,"func")) {
id_code = 38;
} else if (!strcmp(id_me,"shape") || !strcmp(id_me,"shap")) {
id_code = 39;
} else if (!strcmp(id_me,"pulse") || !strcmp(id_me,"puls")) {
id_code = 40;
} else if (!strcmp(id_me,"period") || !strcmp(id_me,"per")) {
id_code = 41;
} else if (!strcmp(id_me,"width") || !strcmp(id_me,"widt")) {
id_code = 42;
} else if (!strcmp(id_me,"dcycle") || !strcmp(id_me,"dcyc")) {
id_code = 43;
} else if (!strcmp(id_me,"hold") ) {
id_code = 44;
} else if (!strcmp(id_me,"delay") || !strcmp(id_me,"del")) {
id_code = 45;
} else if (!strcmp(id_me,"double") || !strcmp(id_me,"doub")) {
id_code = 46;
} else if (!strcmp(id_me,"polarity") || !strcmp(id_me,"pol")) {
id_code = 47;
} else if (!strcmp(id_me,"beeper") || !strcmp(id_me,"beep")) {
id_code = 48;
} else if (!strcmp(id_me,"cw") ) {
id_code = 49;
} else if (!strcmp(id_me,"fixed") || !strcmp(id_me,"fix")) {
id_code = 50;
} else if (!strcmp(id_me,"abort") || !strcmp(id_me,"abor")) {
id_code = 51;
} else if (!strcmp(id_me,"initiate") || !strcmp(id_me,"init")) {
id_code = 52;
} else if (!strcmp(id_me,"continuous")) {
id_code = 53;
} else if (!strcmp(id_me,"trigger") || !strcmp(id_me,"trig")) {
id_code = 54;
} else if (!strcmp(id_me,"integer") || !strcmp(id_me,"int")) {
id_code = 55;
} else if (!strcmp(id_me,"float") ) {
id_code = 56;
} else if (!strcmp(id_me,"eprom") ) {
id_code = 57;
} else if (!strcmp(id_me,"string") ) {
id_code = 58;
} else if (!strcmp(id_me,"bit") ) {
id_code = 59;
} else if (!strcmp(id_me,"set") ) {
id_code = 60;
} else if (!strcmp(id_me,"clear") ) {
id_code = 61;
} else if (!strcmp(id_me,"shiftreg") ) {
id_code = 62;
} else if (!strcmp(id_me,"*rcl") ) {
id_code = 63;
} else if (!strcmp(id_me,"*sav") ) {
id_code = 64;
} else if (!strcmp(id_me,"gate") ) {
id_code = 65;
} else if (!strcmp(id_me,"local") || !strcmp(id_me,"logout") || !strcmp(id_me,"exit") || !strcmp(id_me,"quit") ) {
id_code = 66;
} else if (!strcmp(id_me,"test") ) {
id_code = 67;
} else if (!strcmp(id_me,"communicate") || !strcmp(id_me,"comm")) {
id_code = 68;
} else if (!strcmp(id_me,"address") || !strcmp(id_me,"addr")) {
id_code = 69;
} else if (!strcmp(id_me,"gpib") ) {
id_code = 70;
} else if (!strcmp(id_me,"serial") || !strcmp(id_me,"ser")) {
id_code = 71;
} else if (!strcmp(id_me,"receive") || !strcmp(id_me,"rec")) {
id_code = 72;
} else if (!strcmp(id_me,"baud") ) {
id_code = 73;
} else if (!strcmp(id_me,"parity") || !strcmp(id_me,"par")) {
id_code = 74;
} else if (!strcmp(id_me,"type") ) {
id_code = 75;
} else if (!strcmp(id_me,"bits") ) {
id_code = 76;
} else if (!strcmp(id_me,"sbits") || !strcmp(id_me,"sbit")) {
id_code = 77;
} else if (!strcmp(id_me,"echo") ) {
id_code = 78;
} else if (!strcmp(id_me,"rts") ) {
id_code = 79;
} else if (!strcmp(id_me,"control") || !strcmp(id_me,"cont")) {
id_code = 80;
} else if (!strcmp(id_me,"next") ) {
id_code = 81;
} else if (!strcmp(id_me,"count") || !strcmp(id_me,"coun")) {
id_code = 82;
} else if (!strcmp(id_me,"display") || !strcmp(id_me,"disp")) {
id_code = 83;
} else if (!strcmp(id_me,"brightness") || !strcmp(id_me,"brig")) {
id_code = 84;
} else if (!strcmp(id_me,"load") ) {
id_code = 85;
} else if (!strcmp(id_me,"measure") || !strcmp(id_me,"meas")) {
id_code = 86;
} else if (!strcmp(id_me,"char") ) {
id_code = 87;
} else if (!strcmp(id_me,"calibration") || !strcmp(id_me,"cal")) {
id_code = 88;
} else if (!strcmp(id_me,"monitor") || !strcmp(id_me,"mon")) {
id_code = 89;
} else if (!strcmp(id_me,"step") ) {
id_code = 90;
} else if (!strcmp(id_me,"route") || !strcmp(id_me,"rout")) {
id_code = 91;
} else if (!strcmp(id_me,"close") || !strcmp(id_me,"clos")) {
id_code = 92;
} else if (!strcmp(id_me,"point") ) {
id_code = 93;
} else if (!strcmp(id_me,"shift") ) {
id_code = 94;
} else if (!strcmp(id_me,"scale") ) {
id_code = 95;
} else if (!strcmp(id_me,"null") ) {
id_code = 96;
} else if (!strcmp(id_me,"size") ) {
id_code = 97;
} else if (!strcmp(id_me,"separation") || !strcmp(id_me,"sep")) {
id_code = 98;
} else if (!strcmp(id_me,"network") || !strcmp(id_me,"net")) {
id_code = 99;
} else if (!strcmp(id_me,"password") || !strcmp(id_me,"pass")) {
id_code = 100;
} else if (!strcmp(id_me,"new") ) {
id_code = 101;
} else if (!strcmp(id_me,"suspend") ) {
id_code = 102;
} else if (!strcmp(id_me,"transition") || !strcmp(id_me,"tran")) {
id_code = 103;
} else if (!strcmp(id_me,"leading") || !strcmp(id_me,"lead")) {
id_code = 104;
} else if (!strcmp(id_me,"limit") || !strcmp(id_me,"lim")) {
id_code = 105;
} else if (!strcmp(id_me,"slew") ) {
id_code = 106;
} else if (!strcmp(id_me,"all") ) {
id_code = 107;
} else if (!strcmp(id_me,"reset") || !strcmp(id_me,"res")) {
id_code = 108;
} else if (!strcmp(id_me,"attenuator") || !strcmp(id_me,"att")) {
id_code = 109;
} else if (!strcmp(id_me,"distort")) {
id_code = 110;
} else if (!strcmp(id_me,"resize")) {
id_code = 111;
} else if (!strcmp(id_me,"check")) {
id_code = 112;
} else if (!strcmp(id_me,"disable")) {
id_code = 113;
} else {
id_code = 9999;
}
if (set_new_channel) {
*with_id_code=id_code;
}
return id_code;
}
static int Parser_find_commands(int commands[], int command_depth)
{
#define optional 16384 /* set bit high if optional */
#define opt_mask 16383 /* default bit mask */
#define max_tokens_in_sentence 7
#define max_no_of_sentences 110
/* Valid command -sequences-. The -individual- commands are defined in id_word. */
static const int sentences[max_no_of_sentences][max_tokens_in_sentence] = {
{0}, /* null - error */
{1}, /* cls - sentence 1 */
{2}, /* ese - sentence 2 */
{4}, /* esr - sentence 3 */
{5}, /* idn - sentence 4 */
{6}, /* opc - sentence 5 */
{8}, /* rst - sentence 6 */
{9}, /* sre - sentence 7 */
{10}, /* stb - sentence 8 */
{11}, /* tst - sentence 9 */
{12}, /* wai - sentence 10 */
{13,14,81|optional}, /* syst:err:next - 11 */
{13,15}, /* syst:vers - 12 */
{16,17,18|optional}, /* stat:oper:event - 13 */
{16,17,19}, /* stat:oper:cond - 14 */
{16,17,20}, /* stat:oper:enable - 15 */
{16,21,18|optional}, /* stat:ques:event - 16 */
{16,21,19}, /* stat:ques:cond - 17 */
{16,21,20}, /* stat:ques:enable - 18 */
{16,22}, /* stat:preset - 19 */
{24,25}, /* output:impedance - 20 */
{24,26,27}, /* output:prot:tripped - 21 */
{28|optional,29,30|optional,31|optional,32|optional}, /* sour:curr:lev:imm:ampl - 22 */
{28|optional,29,30|optional,31|optional,33}, /* sour:curr:lev:imm:offset - 23 - not used */
{28|optional,29,30|optional,31|optional,34}, /* sour:curr:lev:imm:high - 24 - not used */
{28|optional,29,30|optional,31|optional,35}, /* sour:curr:lev:imm:low - 25 */
{28|optional,36,30|optional,31|optional,32|optional}, /* sour:volt:lev:imm:ampl - 26 */
{28|optional,36,30|optional,31|optional,33}, /* sour:volt:lev:imm:offset - 27 - not used */
{28|optional,36,30|optional,31|optional,34}, /* sour:volt:lev:imm:high - 28 - not used */
{28|optional,36,30|optional,31|optional,35}, /* sour:volt:lev:imm:low - 29 */
{28|optional,29,26,27}, /* sour:curr:prot:tripped? - 30 */
{28|optional,36,26,27}, /* sour:volt:prot:tripped? - 31 */
{28|optional,37,49|optional}, /* sour:freq:cw - 32 */
{28|optional,37,50|optional}, /* sour:freq:fixed - 33 */
{28|optional,38,39|optional}, /* sour:func:shape - 34 */
{28|optional,40,41}, /* sour:puls:per - 35 */
{28|optional,40,42}, /* sour:puls:width - 36 */
{28|optional,40,43}, /* sour:puls:dcyc - 37 */
{28|optional,40,44}, /* sour:puls:hold - 38 */
{28|optional,40,45}, /* sour:puls:delay - 39 */
{28|optional,40,46,16|optional}, /* sour:puls:doub:state - 40 */
{28|optional,40,46,45}, /* sour:puls:doub:delay - 41 */
{28|optional,40,47}, /* sour:puls:pol - 42 */
{13,48,31|optional}, /* syst:beep:imm - 43 - not used */
{52,31|optional,53}, /* init:imm:cont - 44 - not used */
{51}, /* abort - 45 - not used */
{54,31|optional,28}, /* trig:imm:sour - 46 */
{23,57,58}, /* diag:eprom:string - 47 */
{23,57,55}, /* diag:eprom:int - 48 */
{23,57,59,60}, /* diag:eprom:bit:set - 49 - not used */
{23,57,59,61}, /* diag:eprom:bit:clear - 50 - not used */
{23,57,56}, /* diag:eprom:float - 51 */
{23,62}, /* diag:shiftreg - 52 */
{63}, /* *rcl - 53 */
{64}, /* *sav - 54 */
{24,16|optional}, /* output:state - 55 */
{28|optional,40,65,75}, /* sour:puls:gate:type - 56 */
{66}, /* local (RS232 mode only) - 57 */
{23,67,45}, /* diag:test:delay - 58 */
{13,68,70,69}, /* syst:comm:gpib:addr - 59 */
{13,68,71,72|optional,73}, /* syst:comm:ser:rec:baud - 60 */
{13,68,71,72|optional,74,75|optional}, /* syst:comm:ser:rec:parity - 61 - not used */
{13,68,71,72|optional,76}, /* syst:comm:ser:rec:bits - 62 - not used */
{13,68,71,72|optional,77}, /* syst:comm:ser:rec:sbits - 63 - not used */
{13,68,71,80,79}, /* syst:comm:ser:control:rts - 64 */
{13,68,71,72|optional,78}, /* syst:comm:ser:rec:echo - 65 - not used */
{13,14,82}, /* syst:err:count - 66 */
{28|optional,40,65,30}, /* sour:puls:gate:level - 67 */
{24,85}, /* output:load - 68 */
{86,32}, /* meas:ampl? - 69 */
{23,57,87}, /* diag:eprom:char - 70 */
{23,88}, /* diag:calib - 71 */
{23,32,88,95|optional}, /* diag:ampl:calib:scale - 72 */
{23,89,88,95|optional}, /* diag:mon:calib:scale - 73 */
{23,89,90}, /* diag:mon:step - 74 */
{24,75}, /* output:type - 75 */
{23,33,88,95|optional}, /* diag:offset:calib:scale - 76 */
{23,40,42,88,94}, /* diag:pulse:width:calib:shift - 77 */
{91,92}, /* route:close - 78 */
{23,40,42,88,93}, /* diag:pulse:width:calib:point - 79 */
{23,40,45,88,93}, /* diag:pulse:delay:calib:point - 80 */
{23,40,41,88,93}, /* diag:pulse:period:calib:point - 81 */
{23,40,45,88,94}, /* diag:pulse:delay:calib:shift - 82 */
{23,32,88,93}, /* diag:ampl:calib:point - 83 */
{23,33,88,93}, /* diag:offset:calib:point - 84 */
{23,33,96,93}, /* diag:offset:null:point - 85 */
{23,57,97}, /* diag:eprom:size - 86 */
{83,84}, /* display:brightness - 87 */
{28|optional,40,82}, /* sour:puls:count - 88 */
{28|optional,40,98}, /* sour:puls:separation - 89 */
{23,40,98,88,93}, /* diag:pulse:separation:calib:point - 90 */
{13,68,99}, /* system:comm:network - 91 */
{13,100,101}, /* system:password:new - 92 */
{23,57,102}, /* diag:eprom:suspend - 93 */
{28|optional,40,103,104|optional}, /* sour:puls:transition:leading - 94 */
{23,40,103,88,93}, /* diag:pulse:transition:calib:point - 95 */
{28|optional,29,105,32|optional}, /* sour:curr:limit:ampl - 96 */
{23,24,85,88,93}, /* diag:output:load:calib:point - 97 */
{28|optional,29,106}, /* sour:curr:slew - 98 */
{23,106,88,93}, /* diag:slew:calib:point - 99 */
{88,107|optional}, /* calibration:all - 100 */
{88,37}, /* calibration:frequency - 101 */
{23,57,108}, /* diag:eprom:reset - 102 */
{23,109,16|optional}, /* diag:attenuator:state - 103 */
{23,32,110,88,93}, /* diag:ampl:distort:calib:point - 104 */
{23,57,111}, /* diag:eprom:resize - 105 */
{23,57,111,32}, /* diag:eprom:resize:ampl - 106 */
{23,57,111,33}, /* diag:eprom:resize:offset - 107 */
{23,88,112,113} /* diag:cal:check:disable - 108 */
};
int token_num; /* which token in the command[] list is being considered */
int sentence_num; /* which sentence is being considered as a match */
int sentence_found; /* matching sentence found yet? */
int sent_pos; /* position in current sentence */
int invalid_match; /* flag if definitely the wrong match */
int local_match_pos; /* position (or pos+1, depends) of most-recently-matched token in sentence */
int local_match; /* match for current token found yet? */
int prev_match; /* were there previous local matches? */
int i; /* a counter */
sentence_num = 1;
sent_pos = 0;
/* check each "sentence" (corresponding to a different routine or function)
until a match is found, or until their are no possible sentences left */
for (sentence_found = 0; (!sentence_found && sentence_num < max_no_of_sentences); ++sentence_num) {
local_match_pos=0;
invalid_match=0;
prev_match=0;
/* start with the first input token, until all have been matched, or one
is unmatchable */
for (token_num = 0; (token_num < command_depth) && !invalid_match; ++token_num) {
/* start after most recent matching token */
sent_pos=local_match_pos;
/* compare current token to the sentence components until a matching component
is found, or until there are none left */
for (local_match=0; (!local_match) && (sent_pos<max_tokens_in_sentence); ++sent_pos) {
/* compare here */
if (commands[token_num]==((sentences[sentence_num][sent_pos]) & opt_mask)) {
/* flag a local token match (not a whole sentence match) */
local_match=1;
}
}
if (local_match) {
/* if any of the components in the sentence were skipped between the last two
matches, make sure that they were optional ones */
if (!prev_match) {
local_match_pos=-1;
}
/* if there was no local match previously, then the initial value
of local_match_pos is misleading - there was no match at 0. */
for (i=(local_match_pos+1); i<(sent_pos-1); ++i) {
/* if they weren't optional, the match is not valid */
if (!(sentences[sentence_num][i] & optional)) {
invalid_match=1;
}
}
/* counteract the last "++i" due to the for loop */
local_match_pos = sent_pos - 1;
if (local_match && !invalid_match) {
prev_match=1;
}
}
/* if no match for this one token existed in this sentence, mark this sentence as invalid */
if (!local_match) {
invalid_match = 1;
}
}
/* if no invalid token match was found for any of the tokens in this sentence,
then it's an overall sentence match! */
if (!invalid_match) {
sentence_found=1;
}
}
--sentence_num;
if (!sentence_found) {
sentence_num = 0;
}
/* 0 is the no-match value */
return sentence_num;
}
static int Parser_get_unit(char **parameter, char **units)
{
/* this function takes a parameter like "1e+6 ms" and breaks it into parameter="1e+6" and
units="ms" */
if (*parameter == NULL) {
*parameter = g_strdup ("");
}
g_assert (*units == NULL);
gchar *end_ptr;
g_strtod (*parameter, &end_ptr);
if (end_ptr == *parameter) {
*units = g_strdup ("");
} else {
*units = g_strdup (end_ptr);
end_ptr[0] = 0; // truncates parameter
}
g_strstrip (*units);
g_strstrip (*parameter);
int units_present = (*units[0] != 0);
return units_present;
}
static gchar* regex_replace (gchar* in_string, gchar* regex_string, gchar* replace_with)
{
return conditional_regex_replace (TRUE, in_string, regex_string, replace_with);
}
static gchar* filter_input (gchar *raw_in)
{
g_strstrip (raw_in);
gchar *step0 = g_strdup (raw_in);
// replace weird characters with a space
gchar *step1 = regex_replace (step0, "[^\x20-\x7E]+", " ");
g_free (step0);
// replace multiple whitespace with single space
gchar *step2 = regex_replace (step1, "\\s+", " ");
g_free (step1);
// remove spaces before and after semicolons in compound messages
gchar *step3 = regex_replace (step2, "\\s*;\\s*", ";");
g_free (step2);
// remove semicolon at end
gchar *step4 = regex_replace (step3, ";$", "");
g_free (step3);
// remove leading spaces, colons, semicolons
gchar *step5 = regex_replace (step4, "^[\\s:;]*", "");
g_free (step4);
// last step may leave hanging whitespace at end
g_strstrip (step5);
// add single space to terminate
gchar *filt = g_strdup_printf ("%s ", step5);
g_free (step5);
return filt;
}
void Parser_main (char *raw_in, int interactive_terminal, void(*cbfunc)(gpointer, gchar *), gpointer user_data)
{
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
g_static_mutex_lock (&mutex);
gchar **v;
v = g_strsplit_set (raw_in, "\r\n", STDBUFSIZE);
int str_chunk;
for (str_chunk=0; v[str_chunk] > 0; ++str_chunk) {
gchar *in = filter_input (v[str_chunk]);
int in_pos; /* this identifies the single character of in being processed */
int command_depth; /* how many command words have been parsed */
int old_command_depth; /* save this if in case of a compound message */
int commands[max_commands_in_input]; /* list of identified command tokens */
int old_commands[max_commands_in_input]; /* list of identified command tokens from */
/* last non-common, non-colon-started command*/
int space_found; /* if true, a space was found. parameter may follow */
int parameter_found; /* if true, there is a parameter at the end of the command string */
int semicolon_found; /* to break up lines */
int units_found; /* text found after parameter must be units */
int i; /* counter */
int routine_num; /* routine to execute, based on command */
int dont_use_this_header; /* the old head is saved in case of compound message, unless this is set */
int compound_message; /* this indicates that at least one semicolon has been found, so */
/* the message is a compound one */
int is_query;
int command_type; /* combination of is_query, parameter_found, and units_found */
int channel;
int with_id_code;
in_pos = 0; /* start at the beginning of the input string */
semicolon_found = 0;
compound_message = 0;
command_depth=0;
old_command_depth=0;
dont_use_this_header=NO;
/* examine each letter in the string until EOS */
while (in[in_pos] != 0) {
gchar *units = NULL;
gchar *response = NULL;
gchar *error_response = NULL;
gchar *parameter = NULL;
channel=-1;
with_id_code=0;
space_found = 0;
parameter_found = 0; /* no numeric parameters yet */
units_found = 0;
is_query = 0;
GString *current_word = g_string_new ("");
if (semicolon_found && !dont_use_this_header) {
old_command_depth=command_depth-2;
for (i=0; i<old_command_depth; ++i) {
old_commands[i]=commands[i];
}
}
command_depth=old_command_depth;
for (i=0; i<command_depth; ++i) {
commands[i]=old_commands[i];
}
semicolon_found=0;
dont_use_this_header=NO;
/* break it up if semicolons are present */
while (!semicolon_found && in[in_pos] != 0) {
in[in_pos]=tolower(in[in_pos]);
/* everything to lowercase */
/* words are separated by white space or colons or semicolons */
if ( isspace(in[in_pos]) || in[in_pos] == ':' || in[in_pos] == ';')
{
if (in[in_pos] == ';') {
++semicolon_found;
}
++compound_message;
/* if it is a separator, ignore it if it is duplicated */
if ( !isspace(in[in_pos-1]) && (in[in_pos-1] != ':') && (in[in_pos-1] != ';')) {
/* valid separator, so terminate current command word string */
if (space_found) {
/* Just end things if it is a semicolon */
if (in[in_pos]!=';') {
g_string_append_c (current_word, in[in_pos]);
}
++parameter_found;
// the parameter may have multiple words,
// so this branch may be called repeatedly
g_free (parameter);
parameter = g_strdup (current_word->str);
} else if (!space_found) {
/* end of a regular command word - use it */
commands[command_depth]=Parser_id_word( current_word->str,
&channel,
&with_id_code);
/* get ready for the next token */
g_string_free (current_word, 1);
current_word = g_string_new ("");
if ( commands[command_depth]<13
|| commands[command_depth]==63
|| commands[command_depth]==64) {
dont_use_this_header=YES;
commands[0]=commands[command_depth];
command_depth=0;
}
++command_depth; /* indicate that a new command word is ready */
/* indicate if the separator was a space - next bit parameter then */
if (in[in_pos]==' ') {
++space_found;
}
}
} /* non-repeated separator */
else if (compound_message && in[in_pos-1]==';' && in[in_pos]==':') {
dont_use_this_header=YES;
command_depth=0;
}
} /* a separator */
else if (in[in_pos]!='?') {
/* if not white space or separator, add character to current command word */
g_string_append_c (current_word, in[in_pos]);
} /* not a separator */
else {
++is_query;
/* question mark found */
}
++in_pos; /* move pointer to next letter */
} /* not a semi-colon */
commands[command_depth]=0;
++command_depth;
/* add end of tokens marker (0) */
int error_num=OK;
units_found = Parser_get_unit(¶meter,&units);
if (!globals.Sys.startup_complete) {
error_num=Startup_Not_Finished;
}
if (!error_num) {
command_type=(is_query<<2) | (parameter_found?2:0) | units_found;
if (commands[0]!=0) {
routine_num = Parser_find_commands(commands,command_depth);
/* check for valid channel number, based on position in command */
if ( (error_num=Parser_channel(&channel,with_id_code,routine_num)) )
/* not a valid command due to improper channel suffix */
{
routine_num=9999;
}
} else {
routine_num=9999;
/* ignore empty commands lists, generated by a white space string */
}
switch (routine_num) {
case 0:
error_num=Unrecognized;
break;
case 1:
error_num=Go_cls_1(&response,channel,parameter,units,command_type);
break;
case 2:
error_num=Go_ese_2(&response,channel,parameter,units,command_type);
break;
case 3:
error_num=Go_esr_3(&response,channel,parameter,units,command_type);
break;
case 4:
error_num=Go_idn_5(&response,channel,parameter,units,command_type);
break;
case 5:
error_num=Go_opc_5(&response,channel,parameter,units,command_type);
break;
case 6:
error_num=Go_rst_6(&response,channel,parameter,units,command_type);
break;
case 7:
error_num=Go_sre_7(&response,channel,parameter,units,command_type);
break;
case 8:
error_num=Go_stb_8(&response,channel,parameter,units,command_type);
break;
case 9:
error_num=Go_tst_9(&response,channel,parameter,units,command_type);
break;
case 10:
error_num=Go_wai_10(&response,channel,parameter,units,command_type);
break;
case 11:
error_num=Go_syst_err_11(&response,channel,parameter,units,command_type);
break;
case 12:
error_num=Go_syst_ver_12(&response,channel,parameter,units,command_type);
break;
case 13:
case 14:
case 16:
case 17:
error_num=Go_event_13(&response,channel,parameter,units,command_type);
break;
case 15:
error_num=Go_oper_enable15(&response,channel,parameter,units,command_type);
break;
case 18:
error_num=Go_ques_enable18(&response,channel,parameter,units,command_type);
break;
case 19:
error_num=Go_preset_19(&response,channel,parameter,units,command_type);
break;
case 20:
error_num=Go_zout_20(&response,channel,parameter,units,command_type);
break;
case 21:
case 30:
case 31:
error_num=Go_prot_trip_21(&response,channel,parameter,units,command_type);
break;
case 22:
error_num=Go_ampl_26(&response,channel,parameter,units,command_type,"a");
break;
case 25:
error_num=Go_offset_29(&response,channel,parameter,units,command_type,"a");
break;
case 26:
error_num=Go_ampl_26(&response,channel,parameter,units,command_type,"v");
break;
case 29:
error_num=Go_offset_29(&response,channel,parameter,units,command_type,"v");
break;
case 32:
case 33:
error_num=Go_freq_32_33(&response,channel,parameter,units,command_type);
break;
case 34:
error_num=Go_func_34(&response,channel,parameter,units,command_type);
break;
case 35:
error_num=Go_period_35(&response,channel,parameter,units,command_type);
break;
case 36:
error_num=Go_pw_36(&response,channel,parameter,units,command_type);
break;
case 37:
error_num=Go_duty_37(&response,channel,parameter,units,command_type);
break;
case 38:
error_num=Go_hold_38(&response,channel,parameter,units,command_type);
break;
case 39:
case 41:
error_num=Go_delay_39(&response,channel,parameter,units,command_type);
break;
case 40:
error_num=Go_dbl_pulse_40(&response,channel,parameter,units,command_type);
break;
case 42:
error_num=Go_polarity_42(&response,channel,parameter,units,command_type);
break;
case 46:
error_num=Go_trig_source46(&response,channel,parameter,units,command_type);
break;
case 47:
error_num=Go_Str_eprom_47(&response,channel,parameter,units,command_type);
break;
case 48:
error_num=Go_int_eprom_48(&response,channel,parameter,units,command_type);
break;
case 51:
error_num=Go_Float_eprom51(&response,channel,parameter,units,command_type);
break;
case 53:
error_num=Go_rcl_53(&response,channel,parameter,units,command_type);
break;
case 54:
error_num=Go_sav_54(&response,channel,parameter,units,command_type);
break;
case 55:
error_num=Go_output_55(&response,channel,parameter,units,command_type);
break;
case 56:
error_num=Go_gate_type_56(&response,channel,parameter,units,command_type);
break;
case 58:
error_num=Go_delay_test58(&response,channel,parameter,units,command_type);
break;
case 59:
error_num=Go_gpib_addr_59(&response,channel,parameter,units,command_type);
break;
case 60:
error_num=Go_ser_baud_60(&response,channel,parameter,units,command_type);
break;
case 64:
error_num=Go_ser_rts_64(&response,channel,parameter,units,command_type);
break;
case 66:
error_num=Go_syst_errcnt66(&response,channel,parameter,units,command_type);
break;
case 67:
error_num=Go_gate_level_67(&response,channel,parameter,units,command_type);
break;
case 68:
error_num=Go_load_68(&response,channel,parameter,units,command_type);
break;
case 69:
error_num=Go_meas_ampl_69(&response,channel,parameter,units,command_type);
break;
case 70:
error_num=Go_char_eprom_70(&response,channel,parameter,units,command_type);
break;
case 72:
error_num=Go_calib_amp_72(&response,channel,parameter,units,command_type);
break;
case 73:
error_num=Go_calib_mon_73(&response,channel,parameter,units,command_type);
break;
case 74:
error_num=Go_mon_step_74(&response,channel,parameter,units,command_type);
break;