forked from Hamlib/Hamlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrigctl_parse.c
2905 lines (2474 loc) · 71.5 KB
/
rigctl_parse.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
/*
* rigctl_parse.c - (C) Stephane Fillod 2000-2011
* (C) Nate Bargmann 2003,2006,2008,2010,2011,2012,2013
* (C) Terry Embry 2008-2009
* (C) The Hamlib Group 2002,2006,2007,2008,2009,2010,2011
*
* This program tests/controls a radio using Hamlib.
* It takes commands in interactive mode as well as
* from command line options.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#ifdef HAVE_LIBREADLINE
# if defined(HAVE_READLINE_READLINE_H)
# include <readline/readline.h>
# elif defined(HAVE_READLINE_H) /* !defined(HAVE_READLINE_READLINE_H) */
# include <readline.h>
# else /* !defined(HAVE_READLINE_H) */
extern char *readline ();
# endif /* HAVE_READLINE_H */
#else
/* no readline */
#endif /* HAVE_LIBREADLINE */
#ifdef HAVE_READLINE_HISTORY
# if defined(HAVE_READLINE_HISTORY_H)
# include <readline/history.h>
# elif defined(HAVE_HISTORY_H)
# include <history.h>
# else /* !defined(HAVE_HISTORY_H) */
extern void add_history ();
extern int write_history ();
extern int read_history ();
# endif /* defined(HAVE_READLINE_HISTORY_H) */
/* no history */
#endif /* HAVE_READLINE_HISTORY */
#include <hamlib/rig.h>
#include "misc.h"
#include "iofunc.h"
#include "serial.h"
#include "sprintflst.h"
/* HAVE_SSLEEP is defined when Windows Sleep is found
* HAVE_SLEEP is defined when POSIX sleep is found
* _WIN32 is defined when compiling with MinGW
*
* When cross-compiling from POSIX to Windows using MinGW, HAVE_SLEEP
* will often be defined by configure although it is not supported by
* MinGW. So substitute the sleep definition below in such a case and
* when compiling on Windows using MinGW where HAVE_SLEEP will be
* undefined.
*
* FIXME: Needs better handling for all versions of MinGW.
*
*/
#if (defined(HAVE_SSLEEP) || defined(_WIN32)) && (!defined(HAVE_SLEEP))
#include "hl_sleep.h"
#endif
#include "rigctl_parse.h"
/* Hash table implementation See: http://uthash.sourceforge.net/ */
#include "uthash.h"
#ifdef HAVE_PTHREAD
#include <pthread.h>
static pthread_mutex_t rig_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#define STR1(S) #S
#define STR(S) STR1(S)
#define MAXNAMSIZ 32
#define MAXNBOPT 100 /* max number of different options */
#define MAXARGSZ 127
#define ARG_IN1 0x01
#define ARG_OUT1 0x02
#define ARG_IN2 0x04
#define ARG_OUT2 0x08
#define ARG_IN3 0x10
#define ARG_OUT3 0x20
#define ARG_IN4 0x40
#define ARG_OUT4 0x80
#define ARG_IN_LINE 0x4000
#define ARG_NOVFO 0x8000
#define ARG_IN (ARG_IN1|ARG_IN2|ARG_IN3|ARG_IN4)
#define ARG_OUT (ARG_OUT1|ARG_OUT2|ARG_OUT3|ARG_OUT4)
/* variables for readline support */
#ifdef HAVE_LIBREADLINE
static char *input_line = (char *)NULL;
static char *result = (char *)NULL;
static char *parsed_input[sizeof(char) * 5];
static const int have_rl = 1;
#ifdef HAVE_READLINE_HISTORY
static char *rp_hist_buf = (char *)NULL;
#endif
#else /* no readline */
static const int have_rl = 0;
#endif
struct test_table {
unsigned char cmd;
const char *name;
int (*rig_routine)(RIG*, FILE*, FILE*, int, const struct test_table*, vfo_t,
const char*, const char*, const char*);
int flags;
const char *arg1;
const char *arg2;
const char *arg3;
const char *arg4;
};
#define CHKSCN1ARG(a) if ((a) != 1) return -RIG_EINVAL; else do {} while(0)
#define ACTION(f) rigctl_##f
#define declare_proto_rig(f) static int (ACTION(f))(RIG *rig, FILE *fout, FILE *fin, int interactive, \
const struct test_table *cmd, vfo_t vfo, const char *arg1, \
const char *arg2, const char *arg3)
declare_proto_rig(set_freq);
declare_proto_rig(get_freq);
declare_proto_rig(set_rit);
declare_proto_rig(get_rit);
declare_proto_rig(set_xit);
declare_proto_rig(get_xit);
declare_proto_rig(set_mode);
declare_proto_rig(get_mode);
declare_proto_rig(set_vfo);
declare_proto_rig(get_vfo);
declare_proto_rig(set_ptt);
declare_proto_rig(get_ptt);
declare_proto_rig(get_ptt);
declare_proto_rig(get_dcd);
declare_proto_rig(set_rptr_shift);
declare_proto_rig(get_rptr_shift);
declare_proto_rig(set_rptr_offs);
declare_proto_rig(get_rptr_offs);
declare_proto_rig(set_ctcss_tone);
declare_proto_rig(get_ctcss_tone);
declare_proto_rig(set_dcs_code);
declare_proto_rig(get_dcs_code);
declare_proto_rig(set_ctcss_sql);
declare_proto_rig(get_ctcss_sql);
declare_proto_rig(set_dcs_sql);
declare_proto_rig(get_dcs_sql);
declare_proto_rig(set_split_freq);
declare_proto_rig(get_split_freq);
declare_proto_rig(set_split_mode);
declare_proto_rig(get_split_mode);
declare_proto_rig(set_split_vfo);
declare_proto_rig(get_split_vfo);
declare_proto_rig(set_ts);
declare_proto_rig(get_ts);
declare_proto_rig(power2mW);
declare_proto_rig(mW2power);
declare_proto_rig(set_level);
declare_proto_rig(get_level);
declare_proto_rig(set_func);
declare_proto_rig(get_func);
declare_proto_rig(set_parm);
declare_proto_rig(get_parm);
declare_proto_rig(set_bank);
declare_proto_rig(set_mem);
declare_proto_rig(get_mem);
declare_proto_rig(vfo_op);
declare_proto_rig(scan);
declare_proto_rig(set_channel);
declare_proto_rig(get_channel);
declare_proto_rig(set_trn);
declare_proto_rig(get_trn);
declare_proto_rig(get_info);
declare_proto_rig(dump_caps);
declare_proto_rig(dump_conf);
declare_proto_rig(dump_state);
declare_proto_rig(set_ant);
declare_proto_rig(get_ant);
declare_proto_rig(reset);
declare_proto_rig(send_morse);
declare_proto_rig(send_cmd);
declare_proto_rig(set_powerstat);
declare_proto_rig(get_powerstat);
declare_proto_rig(send_dtmf);
declare_proto_rig(recv_dtmf);
declare_proto_rig(chk_vfo);
declare_proto_rig(halt);
declare_proto_rig(pause);
/*
* convention: upper case cmd is set, lowercase is get
*
* TODO: add missing rig_set_/rig_get_: sql, dcd, etc.
* NB: 'q' 'Q' '?' are reserved by interactive mode interface
* do NOT use -W since it's reserved by POSIX.
*
* Available alphabetic letters: -.--------K-----*-----W-Y-
*/
static struct test_table test_list[] = {
{ 'F', "set_freq", ACTION(set_freq), ARG_IN, "Frequency" },
{ 'f', "get_freq", ACTION(get_freq), ARG_OUT, "Frequency" },
{ 'M', "set_mode", ACTION(set_mode), ARG_IN, "Mode", "Passband" },
{ 'm', "get_mode", ACTION(get_mode), ARG_OUT, "Mode", "Passband" },
{ 'I', "set_split_freq", ACTION(set_split_freq), ARG_IN, "TX Frequency" },
{ 'i', "get_split_freq", ACTION(get_split_freq), ARG_OUT, "TX Frequency" },
{ 'X', "set_split_mode", ACTION(set_split_mode), ARG_IN, "TX Mode", "TX Passband" },
{ 'x', "get_split_mode", ACTION(get_split_mode), ARG_OUT, "TX Mode", "TX Passband" },
{ 'S', "set_split_vfo", ACTION(set_split_vfo), ARG_IN, "Split", "TX VFO" },
{ 's', "get_split_vfo", ACTION(get_split_vfo), ARG_OUT, "Split", "TX VFO" },
{ 'N', "set_ts", ACTION(set_ts), ARG_IN, "Tuning Step" },
{ 'n', "get_ts", ACTION(get_ts), ARG_OUT, "Tuning Step" },
{ 'L', "set_level", ACTION(set_level), ARG_IN, "Level", "Level Value" },
{ 'l', "get_level", ACTION(get_level), ARG_IN1|ARG_OUT2, "Level", "Level Value" },
{ 'U', "set_func", ACTION(set_func), ARG_IN, "Func", "Func Status" },
{ 'u', "get_func", ACTION(get_func), ARG_IN1|ARG_OUT2, "Func", "Func Status" },
{ 'P', "set_parm", ACTION(set_parm), ARG_IN|ARG_NOVFO, "Parm", "Parm Value" },
{ 'p', "get_parm", ACTION(get_parm), ARG_IN1|ARG_OUT2|ARG_NOVFO, "Parm", "Parm Value" },
{ 'G', "vfo_op", ACTION(vfo_op), ARG_IN, "Mem/VFO Op" },
{ 'g', "scan", ACTION(scan), ARG_IN, "Scan Fct", "Scan Channel" },
{ 'A', "set_trn", ACTION(set_trn), ARG_IN|ARG_NOVFO, "Transceive" },
{ 'a', "get_trn", ACTION(get_trn), ARG_OUT|ARG_NOVFO, "Transceive" },
{ 'R', "set_rptr_shift", ACTION(set_rptr_shift), ARG_IN, "Rptr Shift" },
{ 'r', "get_rptr_shift", ACTION(get_rptr_shift), ARG_OUT, "Rptr Shift" },
{ 'O', "set_rptr_offs", ACTION(set_rptr_offs), ARG_IN, "Rptr Offset" },
{ 'o', "get_rptr_offs", ACTION(get_rptr_offs), ARG_OUT, "Rptr Offset" },
{ 'C', "set_ctcss_tone", ACTION(set_ctcss_tone), ARG_IN, "CTCSS Tone" },
{ 'c', "get_ctcss_tone", ACTION(get_ctcss_tone), ARG_OUT, "CTCSS Tone" },
{ 'D', "set_dcs_code", ACTION(set_dcs_code), ARG_IN, "DCS Code" },
{ 'd', "get_dcs_code", ACTION(get_dcs_code), ARG_OUT, "DCS Code" },
{ 0x90, "set_ctcss_sql", ACTION(set_ctcss_sql), ARG_IN, "CTCSS Sql" },
{ 0x91, "get_ctcss_sql", ACTION(get_ctcss_sql), ARG_OUT, "CTCSS Sql" },
{ 0x92, "set_dcs_sql", ACTION(set_dcs_sql), ARG_IN, "DCS Sql" },
{ 0x93, "get_dcs_sql", ACTION(get_dcs_sql), ARG_OUT, "DCS Sql" },
{ 'V', "set_vfo", ACTION(set_vfo), ARG_IN|ARG_NOVFO, "VFO" },
{ 'v', "get_vfo", ACTION(get_vfo), ARG_OUT, "VFO" },
{ 'T', "set_ptt", ACTION(set_ptt), ARG_IN, "PTT" },
{ 't', "get_ptt", ACTION(get_ptt), ARG_OUT, "PTT" },
{ 'E', "set_mem", ACTION(set_mem), ARG_IN, "Memory#" },
{ 'e', "get_mem", ACTION(get_mem), ARG_OUT, "Memory#" },
{ 'H', "set_channel", ACTION(set_channel), ARG_IN|ARG_NOVFO, "Channel" },
{ 'h', "get_channel", ACTION(get_channel), ARG_IN|ARG_NOVFO, "Channel" },
{ 'B', "set_bank", ACTION(set_bank), ARG_IN, "Bank" },
{ '_', "get_info", ACTION(get_info), ARG_OUT|ARG_NOVFO, "Info" },
{ 'J', "set_rit", ACTION(set_rit), ARG_IN, "RIT" },
{ 'j', "get_rit", ACTION(get_rit), ARG_OUT, "RIT" },
{ 'Z', "set_xit", ACTION(set_xit), ARG_IN, "XIT" },
{ 'z', "get_xit", ACTION(get_xit), ARG_OUT, "XIT" },
{ 'Y', "set_ant", ACTION(set_ant), ARG_IN, "Antenna" },
{ 'y', "get_ant", ACTION(get_ant), ARG_OUT, "Antenna" },
{ 0x87, "set_powerstat", ACTION(set_powerstat), ARG_IN|ARG_NOVFO, "Power Status" },
{ 0x88, "get_powerstat", ACTION(get_powerstat), ARG_OUT|ARG_NOVFO, "Power Status" },
{ 0x89, "send_dtmf", ACTION(send_dtmf), ARG_IN, "Digits" },
{ 0x8a, "recv_dtmf", ACTION(recv_dtmf), ARG_OUT, "Digits" },
{ '*', "reset", ACTION(reset), ARG_IN, "Reset" },
{ 'w', "send_cmd", ACTION(send_cmd), ARG_IN1|ARG_IN_LINE|ARG_OUT2|ARG_NOVFO, "Cmd", "Reply" },
{ 'b', "send_morse", ACTION(send_morse), ARG_IN|ARG_IN_LINE, "Morse" },
{ 0x8b, "get_dcd", ACTION(get_dcd), ARG_OUT, "DCD" },
{ '2', "power2mW", ACTION(power2mW), ARG_IN1|ARG_IN2|ARG_IN3|ARG_OUT1|ARG_NOVFO, "Power [0.0..1.0]", "Frequency", "Mode", "Power mW" },
{ '4', "mW2power", ACTION(mW2power), ARG_IN1|ARG_IN2|ARG_IN3|ARG_OUT1|ARG_NOVFO, "Power mW", "Frequency", "Mode", "Power [0.0..1.0]" },
{ '1', "dump_caps", ACTION(dump_caps), ARG_NOVFO },
{ '3', "dump_conf", ACTION(dump_conf), ARG_NOVFO },
{ 0x8f,"dump_state", ACTION(dump_state), ARG_OUT|ARG_NOVFO },
{ 0xf0,"chk_vfo", ACTION(chk_vfo), ARG_NOVFO }, /* rigctld only--check for VFO mode */
{ 0xf1,"halt", ACTION(halt), ARG_NOVFO }, /* rigctld only--halt the daemon */
{ 0x8c, "pause", ACTION(pause), ARG_IN, "Seconds" },
{ 0x00, "", NULL },
};
static struct test_table *find_cmd_entry(int cmd)
{
int i;
for (i = 0; i < MAXNBOPT && test_list[i].cmd != 0x00; i++)
if (test_list[i].cmd == cmd)
break;
if (i >= MAXNBOPT || test_list[i].cmd == 0x00)
return NULL;
return &test_list[i];
}
/* Structure for hash table provided by uthash.h
*
* Structure and hash funtions patterned after/copied from example.c
* distributed with the uthash package. See: http://uthash.sourceforge.net/
*/
struct mod_lst
{
int id; /* caps->rig_model This is the hash key */
char mfg_name[32]; /* caps->mfg_name */
char model_name[32]; /* caps->model_name */
char version[32]; /* caps->version */
char status[32]; /* caps->status */
UT_hash_handle hh; /* makes this structure hashable */
};
/* Hash declaration. Must be initialized to NULL */
struct mod_lst *models = NULL;
/* Add model information to the hash */
void hash_add_model(int id, const char *mfg_name, const char *model_name,
const char *version, const char *status)
{
struct mod_lst *s;
s = (struct mod_lst*)malloc(sizeof(struct mod_lst));
s->id = id;
snprintf(s->mfg_name, sizeof(s->mfg_name), "%s", mfg_name);
snprintf(s->model_name, sizeof(s->model_name), "%s", model_name);
snprintf(s->version, sizeof(s->version), "%s", version);
snprintf(s->status, sizeof(s->status), "%s", status);
HASH_ADD_INT(models, id, s); /* id: name of key field */
}
/* Hash sorting functions */
int hash_model_id_sort(struct mod_lst *a, struct mod_lst *b)
{
return (a->id - b->id);
}
void hash_sort_by_model_id()
{
HASH_SORT(models, hash_model_id_sort);
}
/* Delete hash */
void hash_delete_all() {
struct mod_lst *current_model, *tmp;
HASH_ITER(hh, models, current_model, tmp) {
HASH_DEL(models, current_model); /* delete it (models advances to next) */
free(current_model); /* free it */
}
}
#ifdef HAVE_LIBREADLINE
/* Frees allocated memory and sets pointers to NULL before calling readline
* and then parses the input into space separated tokens.
*/
static void rp_getline(const char *s)
{
int i;
/* free allocated memory and set pointers to NULL */
if (input_line) {
free(input_line);
input_line = (char *)NULL;
}
if (result) {
result = (char *)NULL;
}
for (i = 0; i < 5; i++)
parsed_input[i] = NULL;
/* Action! Returns typed line with newline stripped. */
input_line = readline(s);
}
#endif
/*
* TODO: use Lex?
*/
static char parse_arg(const char *arg)
{
int i;
for (i = 0; i < MAXNBOPT && test_list[i].cmd != 0; i++)
if (!strncmp(arg, test_list[i].name, MAXNAMSIZ))
return test_list[i].cmd;
return 0;
}
/*
* This scanf works even in presence of signals (timer, SIGIO, ..)
*/
static int scanfc(FILE *fin, const char *format, void *p)
{
int ret;
do {
ret = fscanf(fin, format, p);
if (ret < 0) {
if (errno == EINTR)
continue;
rig_debug(RIG_DEBUG_ERR, "fscanf: %s\n", strerror(errno));
}
return ret;
} while(1);
}
/*
* function to get the next word from the command line or from stdin
* until stdin exhausted. stdin is read if the special token '-' is
* found on the command line.
*
* returns EOF when words exhausted
* returns <0 is error number
* returns >=0 when successful
*/
static int next_word (char *buffer, int argc, char *argv[], int newline)
{
int ret;
char c;
static int reading_stdin;
if (!reading_stdin)
{
if (optind >= argc) return EOF;
else if ('-' == argv[optind][0])
{
++optind;
reading_stdin = 1;
}
}
if (reading_stdin)
{
do
{
do ret = scanf (" %c%" STR(MAXARGSZ) "[^ \t\n#]", &c, &buffer[1]); while (EINTR == ret);
if (ret > 0 && '#' == c)
{
do ret = scanf ("%*[^\n]"); while (EINTR == ret); /* consume comments */
ret = 0;
}
}
while (!ret);
if (EOF == ret) reading_stdin = 0;
else if (ret < 0)
{
rig_debug (RIG_DEBUG_ERR, "scanf: %s\n", strerror (errno));
reading_stdin = 0;
}
else
{
buffer[0] = c;
buffer[1 == ret ? 1 : MAXARGSZ] = '\0';
if (newline) putchar ('\n');
fputs (buffer, stdout);
putchar (' ');
}
}
if (!reading_stdin)
{
if (optind < argc)
{
strncpy (buffer, argv[optind++], MAXARGSZ);
buffer[MAXARGSZ] = '\0';
ret = 1;
}
else ret = EOF;
}
return ret;
}
#define fprintf_flush(f, a...) \
({ int __ret; \
__ret = fprintf((f), a); \
fflush((f)); \
__ret; \
})
extern int interactive;
extern int prompt;
extern int vfo_mode;
extern char send_cmd_term;
int ext_resp = 0;
unsigned char resp_sep = '\n'; /* Default response separator */
int rigctl_parse(RIG *my_rig, FILE *fin, FILE *fout, char *argv[], int argc)
{
int retcode; /* generic return code from functions */
unsigned char cmd;
struct test_table *cmd_entry = NULL;
char command[MAXARGSZ+1];
char arg1[MAXARGSZ+1], *p1 = NULL;
char arg2[MAXARGSZ+1], *p2 = NULL;
char arg3[MAXARGSZ+1], *p3 = NULL;
static int last_was_ret = 1;
vfo_t vfo = RIG_VFO_CURR;
/* cmd, internal, rigctld */
if (!(interactive && prompt && have_rl)) {
if (interactive) {
if (prompt)
fprintf_flush(fout, "\nRig command: ");
do {
if (scanfc(fin, "%c", &cmd) < 1)
return -1;
/* Extended response protocol requested with leading '+' on command
* string--rigctld only!
*/
if (cmd == '+' && !prompt) {
ext_resp = 1;
if (scanfc(fin, "%c", &cmd) < 1)
return -1;
} else if (cmd == '+' && prompt) {
return 0;
}
if (cmd != '\\' && cmd != '_' && cmd != '#' && ispunct(cmd) && !prompt) {
ext_resp = 1;
resp_sep = cmd;
if (scanfc(fin, "%c", &cmd) < 1)
return -1;
} else if (cmd != '\\' && cmd != '?' && cmd != '_' && cmd != '#' && ispunct(cmd) && prompt) {
return 0;
}
/* command by name */
if (cmd == '\\') {
unsigned char cmd_name[MAXNAMSIZ], *pcmd = cmd_name;
int c_len = MAXNAMSIZ;
if (scanfc(fin, "%c", pcmd) < 1)
return -1;
while(c_len-- && (isalnum(*pcmd) || *pcmd == '_' ))
if (scanfc(fin, "%c", ++pcmd) < 1)
return -1;
*pcmd = '\0';
cmd = parse_arg((char *)cmd_name);
break;
}
if (cmd == 0x0a || cmd == 0x0d) {
if (last_was_ret) {
if (prompt) {
fprintf(fout, "? for help, q to quit.\n");
fprintf_flush(fout, "\nRig command: ");
}
return 0;
}
last_was_ret = 1;
}
} while (cmd == 0x0a || cmd == 0x0d);
last_was_ret = 0;
/* comment line */
if (cmd == '#') {
while( cmd != '\n' && cmd != '\r')
if (scanfc(fin, "%c", &cmd) < 1)
return -1;
return 0;
}
if (cmd == 'Q' || cmd == 'q')
return 1;
if (cmd == '?') {
usage_rig(fout);
fflush(fout);
return 0;
}
} else {
/* parse rest of command line */
retcode = next_word (command, argc, argv, 1);
if (EOF == retcode) return 1;
else if (retcode < 0) return retcode;
else if ('\0' == command[1]) {
cmd = command[0];
} else {
cmd = parse_arg (command);
}
}
cmd_entry = find_cmd_entry(cmd);
if (!cmd_entry) {
fprintf(stderr, "Command '%c' not found!\n", cmd);
return 0;
}
if (!(cmd_entry->flags & ARG_NOVFO) && vfo_mode) {
if (interactive) {
if (prompt)
fprintf_flush(fout, "VFO: ");
if (scanfc(fin, "%s", arg1) < 1)
return -1;
vfo = rig_parse_vfo(arg1);
} else {
retcode = next_word (arg1, argc, argv, 0);
if (EOF == retcode) {
fprintf(stderr, "Invalid arg for command '%s'\n",
cmd_entry->name);
}
else if (retcode < 0) return retcode;
vfo = rig_parse_vfo(arg1);
}
}
if ((cmd_entry->flags & ARG_IN_LINE) &&
(cmd_entry->flags & ARG_IN1) && cmd_entry->arg1) {
if (interactive) {
char *nl;
if (prompt)
fprintf_flush(fout, "%s: ", cmd_entry->arg1);
if (fgets(arg1, MAXARGSZ, fin) == NULL)
return -1;
if (arg1[0] == 0xa)
if (fgets(arg1, MAXARGSZ, fin) == NULL)
return -1;
nl = strchr(arg1, 0xa);
if (nl) *nl = '\0'; /* chomp */
p1 = arg1[0] == ' ' ? arg1 + 1 : arg1;
} else {
retcode = next_word (arg1, argc, argv, 0);
if (EOF == retcode) {
fprintf(stderr, "Invalid arg for command '%s'\n",
cmd_entry->name);
return 1;
}
else if (retcode < 0) return retcode;
p1 = arg1;
}
} else
if ((cmd_entry->flags & ARG_IN1) && cmd_entry->arg1) {
if (interactive) {
if (prompt)
fprintf_flush(fout, "%s: ", cmd_entry->arg1);
if (scanfc(fin, "%s", arg1) < 1)
return -1;
p1 = arg1;
} else {
retcode = next_word (arg1, argc, argv, 0);
if (EOF == retcode) {
fprintf(stderr, "Invalid arg for command '%s'\n",
cmd_entry->name);
return 1;
}
else if (retcode < 0) return retcode;
p1 = arg1;
}
}
if (p1 && p1[0] != '?' && (cmd_entry->flags & ARG_IN2) && cmd_entry->arg2) {
if (interactive) {
if (prompt)
fprintf_flush(fout, "%s: ", cmd_entry->arg2);
if (scanfc(fin, "%s", arg2) < 1)
return -1;
p2 = arg2;
} else {
retcode = next_word (arg2, argc, argv, 0);
if (EOF == retcode) {
fprintf(stderr, "Invalid arg for command '%s'\n",
cmd_entry->name);
return 1;
}
else if (retcode < 0) return retcode;
p2 = arg2;
}
}
if (p1 && p1[0] != '?' && (cmd_entry->flags & ARG_IN3) && cmd_entry->arg3) {
if (interactive) {
if (prompt)
fprintf_flush(fout, "%s: ", cmd_entry->arg3);
if (scanfc(fin, "%s", arg3) < 1)
return -1;
p3 = arg3;
} else {
retcode = next_word (arg3, argc, argv, 0);
if (EOF == retcode) {
fprintf(stderr, "Invalid arg for command '%s'\n",
cmd_entry->name);
return 1;
}
else if (retcode < 0) return retcode;
p3 = arg3;
}
}
}
#ifdef HAVE_LIBREADLINE
if (interactive && prompt && have_rl) {
int j, x;
#ifdef HAVE_READLINE_HISTORY
/* Minimum space for 32+1+32+1+128+1+128+1+128+1 = 453 chars, so
* allocate 512 chars cleared to zero for safety.
*/
rp_hist_buf = (char *)calloc(512, sizeof(char));
#endif
rl_instream = fin;
rl_outstream = fout;
rp_getline("\nRig command: ");
/* EOF (Ctl-D) received on empty input line, bail out gracefully. */
if (!input_line) {
fprintf_flush(fout, "\n");
return 1;
}
/* Q or q to quit */
if (!(strncasecmp(input_line, "q", 1)))
return 1;
/* '?' for help */
if (!(strncmp(input_line, "?", 1))) {
usage_rig(fout);
fflush(fout);
return 0;
}
/* '#' for comment */
if (!(strncmp(input_line, "#", 1)))
return 0;
/* Blank line entered */
if (!(strcmp(input_line, ""))) {
fprintf(fout, "? for help, q to quit.\n");
fflush(fout);
return 0;
}
rig_debug(RIG_DEBUG_BUG, "%s: input_line: %s\n", __func__, input_line);
/* Split input_line on any number of spaces to get the command token
* Tabs are intercepted by readline for completion and a newline
* causes readline to return the typed text. If more than one
* argument is given, it will be parsed out later.
*/
result = strtok(input_line, " ");
/* parsed_input stores pointers into input_line where the token strings
* start.
*/
if (result) {
parsed_input[0] = result;
} else {
/* Oops! Invoke GDB!! */
fprintf_flush(fout, "\n");
return 1;
}
/* At this point parsed_input contains the typed text of the command
* with surrounding space characters removed. If Readline History is
* available, copy the command string into a history buffer.
*/
/* Single character command */
if ((strlen(parsed_input[0]) == 1) && (*parsed_input[0] != '\\')) {
cmd = *parsed_input[0];
#ifdef HAVE_READLINE_HISTORY
/* Store what is typed, not validated, for history. */
if (rp_hist_buf)
strncpy(rp_hist_buf, parsed_input[0], 1);
#endif
}
/* Test the command token, parsed_input[0] */
else if ((*parsed_input[0] == '\\') && (strlen(parsed_input[0]) > 1)) {
char cmd_name[MAXNAMSIZ];
/* if there is no terminating '\0' character in the source string,
* srncpy() doesn't add one even if the supplied length is less
* than the destination array. Truncate the source string here.
*/
if (strlen(parsed_input[0] + 1) >= MAXNAMSIZ)
*(parsed_input[0] + MAXNAMSIZ) = '\0';
#ifdef HAVE_READLINE_HISTORY
if (rp_hist_buf)
strncpy(rp_hist_buf, parsed_input[0], MAXNAMSIZ);
#endif
/* The starting position of the source string is the first
* character past the initial '\'. Using MAXNAMSIZ for the
* length leaves enough space for the '\0' string terminator in the
* cmd_name array.
*/
strncpy(cmd_name, parsed_input[0] + 1, MAXNAMSIZ);
/* Sanity check as valid multiple character commands consist of
* alpha-numeric characters and the underscore ('_') character.
*/
for (j = 0; cmd_name[j] != '\0'; j++) {
if (!(isalnum((int)cmd_name[j]) || cmd_name[j] == '_')) {
fprintf(stderr, "Valid multiple character command names contain alpha-numeric characters plus '_'\n");
return 0;
}
}
cmd = parse_arg(cmd_name);
}
/* Single '\' entered, prompt again */
else if ((*parsed_input[0] == '\\') && (strlen(parsed_input[0]) == 1)) {
return 0;
}
/* Multiple characters but no leading '\' */
else {
fprintf(stderr, "Precede multiple character command names with '\\'\n");
return 0;
}
cmd_entry = find_cmd_entry(cmd);
if (!cmd_entry) {
if (cmd == '\0')
fprintf(stderr, "Command '%s' not found!\n", parsed_input[0]);
else
fprintf(stderr, "Command '%c' not found!\n", cmd);
return 0;
}
/* If vfo_mode is enabled (-o|--vfo) check if already given
* or prompt for it.
*/
if (!(cmd_entry->flags & ARG_NOVFO) && vfo_mode) {
/* Check if VFO was given with command. */
result = strtok(NULL, " ");
if (result) {
x = 1;
parsed_input[x] = result;
}
/* Need to prompt if a VFO string was not given. */
else {
x = 0;
rp_getline("VFO: ");
if (!input_line) {
fprintf_flush(fout, "\n");
return 1;
}
/* Blank line entered */
if (!(strcmp(input_line, ""))) {
fprintf(fout, "? for help, q to quit.\n");
fflush(fout);
return 0;
}
/* Get the first token of input, the rest, if any, will be
* used later.
*/
result = strtok(input_line, " ");
if (result) {
parsed_input[x] = result;
} else {
fprintf_flush(fout, "\n");
return 1;
}
}
/* VFO name tokens are presently quite short. Truncate excessively
* long strings.
*/
if (strlen(parsed_input[x]) >= MAXNAMSIZ)
*(parsed_input[x] + (MAXNAMSIZ - 1)) = '\0';
#ifdef HAVE_READLINE_HISTORY
if (rp_hist_buf) {
strncat(rp_hist_buf, " ", 1);
strncat(rp_hist_buf, parsed_input[x], MAXNAMSIZ);
}
#endif
/* Sanity check, VFO names are alpha only. */
for (j = 0; j < MAXNAMSIZ && parsed_input[x][j] != '\0'; j++) {
if (!(isalpha((int)parsed_input[x][j]))) {
parsed_input[x][j] = '\0';
break;
}
}
vfo = rig_parse_vfo(parsed_input[x]);
if (vfo == RIG_VFO_NONE) {
fprintf(stderr, "Warning: VFO '%s' unrecognized, using 'currVFO' instead.\n",
parsed_input[x]);
vfo = RIG_VFO_CURR;
}
}
/* \send_cmd, \send_morse */
if ((cmd_entry->flags & ARG_IN_LINE) &&
(cmd_entry->flags & ARG_IN1) && cmd_entry->arg1) {
/* Check for a non-existent delimiter so as to not break up
* remaining line into separate tokens (spaces OK).
*/
result = strtok(NULL, "\0");
if (vfo_mode && result) {
x = 2;
parsed_input[x] = result;
} else if (result) {
x = 1;
parsed_input[x] = result;
} else {
x = 0;
char pmptstr[(strlen(cmd_entry->arg1) + 3)];
strcpy(pmptstr, cmd_entry->arg1);
strcat(pmptstr, ": ");
rp_getline(pmptstr);
/* Blank line entered */
if (!(strcmp(input_line, ""))) {
fprintf(fout, "? for help, q to quit.\n");
fflush(fout);
return 0;
}
if (input_line)
parsed_input[x] = input_line;
else {
fprintf_flush(fout, "\n");
return 1;
}
}
/* The arg1 array size is MAXARGSZ + 1 so truncate it to fit if larger. */
if (strlen(parsed_input[x]) > MAXARGSZ)
parsed_input[x][MAXARGSZ] = '\0';
#ifdef HAVE_READLINE_HISTORY
if (rp_hist_buf) {
strncat(rp_hist_buf, " ", 1);
strncat(rp_hist_buf, parsed_input[x], MAXARGSZ);
}
#endif
strcpy(arg1, parsed_input[x]);
p1 = arg1;
}
/* Normal argument parsing. */
else if ((cmd_entry->flags & ARG_IN1) && cmd_entry->arg1) {
result = strtok(NULL, " ");
if (vfo_mode && result) {
x = 2;
parsed_input[x] = result;
} else if (result) {
x = 1;
parsed_input[x] = result;
} else {
x = 0;
char pmptstr[(strlen(cmd_entry->arg1) + 3)];
strcpy(pmptstr, cmd_entry->arg1);
strcat(pmptstr, ": ");
rp_getline(pmptstr);
if (!(strcmp(input_line, ""))) {
fprintf(fout, "? for help, q to quit.\n");
fflush(fout);
return 0;
}
result = strtok(input_line, " ");