File: icom.c

package info (click to toggle)
icom 20040912-1.1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 744 kB
  • ctags: 501
  • sloc: ansic: 3,086; csh: 65; makefile: 62; awk: 61
file content (2315 lines) | stat: -rw-r--r-- 48,602 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
/*
 * Program to control ICOM radios
 *
 * Main program
 */
#include "icom.h"

#ifndef MSDOS			/* include for Unix */
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#endif /* MSDOS */
#ifdef AUDIO			/* include for Sun audio */
#include <sys/audioio.h>
#endif /* AUDIO */

/*
 * Module definitions
 */
#define ARGMAX 20		/* maximum number of command args */

/*
 * External functions
 */
extern FILE *fopen();
#if (__GLIBC__ < 2)
extern char *strtok(), *strcpy();
#endif
extern char *optarg;
extern int optind, opterr;

/*
 * Local function prototypes
 */
static int scanarg(struct icom *, char *, struct cmdtable *, int);
static int mygetline(char *);
static int argchan(struct icom *, struct chan *, char *);
static int argbank(struct icom *, struct chan *, char *);
static int loadsub(struct icom *, struct chan *, int);
static int setswitch(struct icom *, char *, struct cmdtable *, int);
static int setctrl(struct icom *, char *, int);
static int sw_keypad(void);
static int sw_keybd(void);
static void reset(struct icom *);
static void update(struct icom *, struct chan *);
char *modetoa(int, struct cmdtable *);
double fabs(double);

/*
 * Local variables
 */
int flags;			/* radio flags */
int pflags;			/* program flags */
static char *argv[ARGMAX];	/* command line args */
static int argn;		/* number of command line args */
static FILE *fp_cmd;		/* command input file */
static FILE *fp_in;		/* data input file */
static FILE *fp_out;		/* data output file */
double logtab[] = {		/* tuning rate table */
	1., 2., 5.,		/* 0.000000 */
	10., 25., 50.,		/* 0.00000 */
	1e2, 2.5e2, 5e2,	/* 0.0000 */
	1e3, 2.5e3, 5e3,	/* 0.000 */
	1e4, 2.5e4, 5e4,	/* 0.00 */
	1e5, 2.5e5, 5e5,	/* 0.0 */
	1e6, 2.5e6, 5e6		/* 0. */
};
int sigtab[] = {		/* significant digits table */
	6, 6, 6,		/* 0.000000 */
	5, 6, 5,		/* 0.00000 */
	4, 5, 4,		/* 0.0000 */
	3, 4, 3,		/* 0.000 */
	2, 3, 2,		/* 0.00 */
	1, 2, 1,		/* 0.0 */
	0, 0, 0			/* 0. */
};
struct metertab {
	int smeter;		/* S meter reading */
	char pip[NAMMAX];	/* meter translation */
};
struct metertab mtab[] = {
	{0,	"S0"},		/* 0.4-1.3 uv */
	{12,	"S1"},
	{32,	"S2"},
	{46,	"S3"},		/* 0.79-2.0 uV */
	{64,	"S4"},
	{78,	"S5"},		/* 3.2 uV */
	{94,	"S6"}, 
	{110,	"S7"},		/* 13 uV */
	{126,	"S8"},
	{140,	"S9"},		/* 50 uV */
	{156,	"S9+10"},
	{174,	"S9+20"},	/* 0.5 mv */
	{190,	"S9+30"},
	{208,	"S9+40"},	/* 5 mv */
	{230,	"S9+50"},
	{999,	"S9+60"}	/* 50 mV */
};

#ifndef MSDOS
static int fd;			/* terminal control file descriptor */
static struct termios terma, termb; /* Unix terminal interface */
extern int errno;
#endif /* MSDOS */
#ifdef AUDIO
static struct audio_device device; /* audio device ident */
static struct audio_info info;	/* audio device info */
static int ctl_fd;		/* audio control file descriptor */
#endif /* AUDIO */

/*
 * Main program
 */
int
main(
	int	argc,		/* number of arguments */
	char	**argcv		/* vector of argument pointers */
	)
{
	char	s1[VALMAX], s2[VALMAX]; /* parameter strings */
	char	chr, *ptr;	/* char temps */
	int	i, temp, sw;	/* int temps */
	double	freq, step, dtemp; /* double temps */
	int	par1, par2;	/* integer parameters */
	struct icom *rp;	/* radio pointer */
	struct icom *rp1;	/* temporary radio structure pointer */
	struct chan *cp;	/* memory channel pointer */
	struct chan *vp;	/* vfo channel pointer */

	u_char cmdwrite[] = {V_WRITE, FI};
	u_char cmdclear[] = {V_CLEAR, FI};
	u_char cmdsig[] = {V_RDCTL, S_RDSG, FI};
	u_char cmdsqu[] = {V_RDCTL, S_RDSQ, FI};
	struct metermsg rspsig;
	struct modelmsg rspsqu;
	int	ident;

	/*
	 * Initialize
	 */
	flags = 0;
	rp = NULL;
	init();

#ifndef MSDOS
	/*
	 * Process command-line arguments
	 */
	while ((temp = getopt(argc, argcv, "ac:df:g:km:o:r:")) != -1) {
		switch (temp) {

		/*
		 * -d - debug trace
		 */
		case 'd':
			pflags |= P_TRACE | P_ERMSG;
			continue;

		/*
		 * -a - open audio device
		 */
		case 'a':
#ifdef AUDIO
			if ((ctl_fd = open("/dev/audioctl", O_RDWR)) <
			    0)
				printf("*** audio not available\n");
			continue;
#else /* AUDIO */
			printf("*** audio not configured\n");
			continue;
#endif /* AUDIO */

		/*
		 * -f <file> - open command file
		 */
		case 'f':
			if ((fp_cmd = fopen(optarg, "r")) == NULL) {
				printf("*** file not found\n");
				exit(1);
			}
			continue;

		/*
		 * -k - specify keypad mode
		 */
		case 'k':
			pflags |= P_PAD;
			continue;

		/*
		 * -r <radio> - select radio
		 */
		case 'r':
			temp = argsx(optarg, identab);
			if (temp == R_ERR)
				exit(1);

			rp = select_radio(temp);
			if (rp == NULL) {
				printf("*** initialization failure\n");
				exit(1);
			}
			continue;
		}

		/*
		 * The remaining options are valid only after a valid
		 * radio has been selected. If any are present, the
		 * program exits after executing the command line
		 * options.
		 */
		if (rp == NULL) {
			printf("*** radio not specified\n");
			exit(1);
		}
		ident = rp->ident;
		vp = &rp->vfo;
		switch (temp) {

		/*
		 * -c <chan> - set bank, channel
		 */
		case 'c':
			pflags |= P_EXIT;
			if (argchan(rp, vp, optarg) == R_ERR)
				continue;

			setchan(ident, vp->bank, vp->mchan);
			if (readchan(ident, cp) == R_ERR)
				printf("*** empty channel\n");
			continue;

		/*
		 * -g <freq> - set frequency
		 */
		case 'g':
			pflags |= P_EXIT;
			if (sscanf(optarg, "%lf", &freq) != 1) {
				printf("*** bad frequency format\n");
				continue;
			}
			if (freq > 1000)
				freq /= 1000;
			if (loadfreq(ident, freq) == R_ERR)
				printf("*** bad frequency\n");
			continue;

		/*
		 * -m <mode> - set mode. Note, this option must precede
		 * the -f option for older radios.
		 */
		case 'm':
			pflags |= P_EXIT;
			temp = argsx(optarg, rp->modetab);
			if (temp == R_ERR)
				continue;

			loadmode(ident, temp);
			continue;

		/*
		 * -o <offset> - set duplex offset
		 */
		case 'o':
			pflags |= P_EXIT;
			if (sscanf(optarg, "%lf", &freq) != 1) {
				printf("*** bad offset format\n");
				continue;
			}
			if (loadoffset(ident, freq / 1000) == R_ERR)
				printf("*** bad offset\n");
			continue;
		}
	}

	/*
	 * If a radio was found, initialize it. If its settings were
	 * changed and a command file is not open, assume this is run
	 * from a script and nothing more needs to be done.
	 */
	if (rp != NULL)
		reset(rp);
	if (pflags & P_EXIT)
		exit(1);
	if (pflags & P_PAD) {
		if (sw_keypad())
			pflags &= ~P_PAD;
	}
#endif /* MSDOS */

	/*
	 * Main loop
	 */
	while (1) {
		pflags &= ~(P_DISP | P_KEYP | P_ESC);
		if (pflags & P_PAD) {

			/*
			 * Keypad mode. Keypad commands begin with a
			 * sequence of digits and '+', '-', '.' and end
			 * with a single letter or ANSI escape sequence,
			 * which specifies the command, or \n, which is
			 * mapped to 'f'. Escape sequences consist of
			 * ESC followed by '[' and either a letter or
			 * sequence of digits followed by '~', which is
			 * not part of the command. Help '?' displays a
			 * list of command names and descriptions.
			 */
			printf(">");
			ptr = s1;
			for (i = 0; i < 6; i++)
				*ptr++ = ' ';
			while (1) {
				chr = (char)getchar();
				if (chr == ESC) {
					pflags |= P_ESC;
					*ptr = '\0';
					ptr = s1;
					continue;
				}
				if (pflags & P_ESC) {
					if (chr == '~')
						break;

					*ptr++ = chr;
					if (isalpha(chr) || chr == '\n')
						break;
					continue;
				}
				if (isdigit(chr) || chr == '.' ||
				    chr == '+' || chr == '-') {
					pflags |= P_KEYP;
					*ptr++ = chr;
 					putchar(chr);
					continue;

				} else if (chr != '\n') {
					pflags |= P_KEYP;
					putchar(chr);
				}
				if (pflags & P_KEYP)
					putchar('\n');
				*ptr = '\0';
				*s1 = chr;
				break;
			}
			if ((argn = mygetline(s1)) == 0)
				sw = C_FREQ;
			else
				sw = argsx(argv[0], key);
		} else {

			/*
			 * Keyboard mode. Get the next command, convert
			 * to lower case and parse the tokens. Select
			 * the command based on the first token. Ignore
			 * '#' and the rest of the line. This is for
			 * command scripts.
			 */
			if (fp_cmd != NULL) {
				if (fgets(s1, VALMAX, fp_cmd) == NULL)
					exit(0);

				printf("%s", s1);
			} else {
				printf("icom>");
				if (gets(s1) == NULL)
					exit(0);
			}
			if (*s1 == '#')
				continue;

			if ((argn = mygetline(s1)) == 0)
				sw = C_FREQ;
			else
				sw = argsx(argv[0], cmd);
		}
		switch (sw) {

		/*
		 * radio [ <name> ]
		 *
		 * Select the <name> radio for further commands and
		 * display its description and band limits. If name is
		 * missing and the radio has not been previously
		 * defined, the buss is probed for all known radios,
		 * which takes some time. If previouslh defined, its
		 * description and band limits of the are displayed.
		 */
		case C_RADIO:
			if (argn < 2) {
				if (rp != NULL) {
					printf("radio %s\n",
					    getcap("radio", rp->cap));
					continue;
				}
				for (i = 0; name[i].name[0] != '\0';
				    i++) {
					rp1 =
					    select_radio(name[i].ident);
					if (rp1 != NULL) {
						rp = rp1;
						reset(rp);
						printf("radio %s\n",
						    getcap("radio",
						    rp->cap));
					}
				}
				continue;
			}
			temp = argsx(argv[1], identab);
			if (temp == R_ERR)
				continue;

			rp = select_radio(temp);
			if (rp == NULL) {
				printf("*** radio not found\n");
				continue;
			}
			reset(rp);
			printf("radio %s\n", getcap("radio", rp->cap));
			break;
 
		/*
		 * quit
		 *
		 * Quit the dance
		 */
		case C_QUIT:
			exit(0);
 
		/*
		 * verbose off | on
		 *
		 * Set verbose mode
		 */
		case C_VERB:
			if (argn < 2) {
				printf("*** missing argument\n");
				continue;
			}
			temp = argsx(argv[1], verbx);
			if (temp != R_ERR)
				pflags = (pflags & ~P_VERB) | temp;
			continue;

		/*
		 * trace [ all | none | bus | pkt ]
		 *
		 * Set debug flags
		 */
		case C_DEBUG:
			if (argn < 2) {
				printf("*** missing argument\n");
				continue;
			}
			temp = argsx(argv[1], dbx);
			if (temp != R_ERR)
				pflags = (pflags & ~(P_TRACE |
				    P_ERMSG)) | temp;
			continue;

		/*
		 * pad
		 *
		 * Switch to keypad mode.
		 */
		case C_KEYPAD:
			if (!sw_keypad())
				pflags |= P_PAD;
			continue;

		/*
		 * # (keypad mode)
		 *
		 * Erase input
		 */
		case R_ERR:
		case C_ERASE:
			continue;

		/*
		 * q (keypad mode)
		 *
		 * Switch to keyboard mode.
		 */
		case C_KEYBD:
			if (!sw_keybd())
				pflags &= ~P_PAD;
			continue;
#ifdef AUDIO
		/*
		 * gain [ <gain> ]
		 *
		 * Adjust record gain in 16 levels.
		 */
		case C_GAIN:
			if (ctl_fd <= 0) {
				printf("*** audio not available\n");
			} else if (argn < 2) {
				printf("audio gain %d\n",
				    info.record.gain);
			} else if (sscanf(argv[1], "%d", &temp) != 1) {
				printf("*** bad gain format\n");
			} else {
				if (temp > 0)
					temp = (temp << 4) - 1;
				if (temp > AUDIO_MAX_GAIN)
					temp = AUDIO_MAX_GAIN;
				else if (temp < AUDIO_MIN_GAIN)
					temp = AUDIO_MIN_GAIN;
				ioctl(ctl_fd, (int)AUDIO_GETINFO,
				    &info);
				info.record.gain = temp;
				ioctl(ctl_fd, (int)AUDIO_SETINFO,
				    &info);
			}
			continue;

		/*
		 * mute
		 *
		 * Mute output (toggle)
		 */
		case C_MUTE:
			if (ctl_fd <= 0) {
				printf("*** audio not available\n");
				continue;
			}
			ioctl(ctl_fd, (int)AUDIO_GETINFO, &info);
			if (info.output_muted) {
				info.output_muted = 0;
			} else {
				info.output_muted = 1;
				printf("audio muted\n");
			}
			ioctl(ctl_fd, (int)AUDIO_SETINFO, &info);
			continue;

		/*
		 * port <port>
		 *
		 * Select input port (1 = mike, 2 = line in)
		 */
		case C_PORT:
			if (ctl_fd <= 0) {
				printf("*** audio not available\n");
			} else if (argn < 2) {
				printf("audio port %d\n",
				    info.record.port);
			} else if (sscanf(argv[1], "%d", &temp) != 1) {
				printf("*** bad port format\n");
			} else if (temp < 1 || temp > 2) {
				printf("*** invalid port number\n");
			} else {
				ioctl(ctl_fd, (int)AUDIO_GETINFO,
				    &info);
				info.record.port = temp;
				ioctl(ctl_fd, (int)AUDIO_SETINFO,
				    &info);
			}
			continue;
#endif /* AUDIO */
		}

		/*
		 * The remaining commands are valid only after a radio
		 * has been selected.
		 */
		if (rp == NULL) {
			printf("*** radio not specified\n");
			continue;
		}
		ident = rp->ident;
		vp = &rp->vfo;
		cp = &rp->chan;
		switch (sw) {

		/*
		 * cap [ <args> ]
		 */
		case C_PROBE:
			if (argn < 2) {
				(void)getcap("?", rp->cap);
				break;
			}
			printf("%s %s\n", argv[1], getcap(argv[1],
			    rp->cap));
			break;

		/*
		 * restore file [ first ] [ last ]
		 *
		 * Restore memory channels from from a file to a range
		 * of channels numbered first through last in sequence.
		 * The default is to restore them all to the original
		 * channels.
		 */
		case C_RESTORE:
			if (argn < 2) {
				printf(
				"usage: restore <file> [first] [last]\n");
				break;
			}
			if ((fp_in = fopen(argv[1], "r")) == NULL) {
				printf("*** input file error\n");
				break;
			}
			if (argn > 3) {
				if (argchan(rp, vp, argv[3]) == R_ERR)
					break;

				par1 = vp->bank;
				par2 = vp->mchan;
				if (argchan(rp, vp, argv[2]) == R_ERR)
					break;

			} else if (argn == 3) {
				if (argchan(rp, vp, argv[2]) == R_ERR)
					break;

				par1 = vp->bank;
				par2 = vp->mchan;
			} else {
				vp->bank = 0;
				vp->mchan = rp->minch;
				par1 = rp->maxbk;
				par2 = rp->maxch;
			}
			while (1) {
				if (fgets(s1, VALMAX, fp_in) == NULL)
					break;

				printf("%s", s1);
				argn = mygetline(s1);
				if (argn == 0)
					continue;

				if (argchan(rp, vp, argv[0]) == R_ERR)
					continue;

				if (argn == 1)
					continue;

				if (sscanf(argv[1], "%lf", &freq) != 1)
				    {
					printf("*** bad frequency format\n");
					continue;
				}
				if (freq > 1000.)
					freq /= 1000.;
				vp->freq = freq;
				if (argn == 3) {
					temp = argsx(argv[2],
					    rp->modetab);
					if (temp == R_ERR)
						continue;

					vp->mode = temp;
				}
				if (argn > 3) {
					if (loadsub(rp, vp, 2) == R_ERR)
						continue;
				}
				if (flags & F_BANK) {
					write_chan(ident, vp);
				} else {
					setchan(ident, vp->bank,
					    vp->mchan);
					if (loadmode(ident, vp->mode) ==
					    R_ERR)
						printf("*** bad mode\n");
					if (loadfreq(ident, vp->freq) ==
					    R_ERR)
						printf("*** bad frequency\n");;
					setcmd(ident, V_WRITE, FI);
				}
				if (vp->bank == par1 && vp->mchan ==
				    par2)
					break;

			}
			close(fp_in);
			break;

		/*
		 * save file [ first ] [ last ]
		  Save memory channels to a file from a range of
		 * channels numbered first through last in sequence,
		 * skipping empty channels. The default is to save them
		 * all.
		 */
		case C_SAVE:
			if (argn < 2) {
				printf(
				"usage: save <file> [first] [last]\n");
				break;
			}
			if ((fp_out = fopen(argv[1], "w")) == NULL) {
				printf(
				"*** output file error\n");
				break;
			}
			if (argn > 3) {
				if (argchan(rp, vp, argv[3]) == R_ERR)
					break;

				par1 = vp->bank;
				par2 = vp->mchan;
				if (argchan(rp, vp, argv[2]) == R_ERR)
					break;

			} else if (argn > 2) {
				if (argchan(rp, vp, argv[2]) == R_ERR)
					break;

				par1 = vp->bank;
				par2 = vp->mchan;
			} else {
				vp->bank = 0;
				vp->mchan = rp->minch;
				par1 = rp->maxbk;
				par2 = rp->maxch;
			}
			while (1) {
				setchan(ident, vp->bank, vp->mchan);
				if (readchan(ident, vp) == R_ERR) {
					printf("%d.%d empty channel\n",
					    vp->bank, vp->mchan);
				} else if (flags & F_OFFSET) {
 					sprintf(s1,
					    "%2d.%-2d %10.*lf %s %.0lf",
					    vp->bank, vp->mchan,
					    sigtab[rp->rate], vp->freq,
					    modetoa(vp->mode,
					    rp->modetab), vp->duplex);
					fprintf(fp_out, "%s\n", s1);
					puts(s1);
				} else {
					sprintf(s1,
					    "%2d.%-2d %10.*lf %s",
					    vp->bank, vp->mchan,
					    sigtab[rp->rate], vp->freq,
					    modetoa(vp->mode,
					    rp->modetab));
					fprintf(fp_out, "%s\n", s1);
					puts(s1);
				}
				if (vp->bank == par1 && vp->mchan ==
				    par2)
					break;

				if (argchan(rp, vp, "+") == R_ERR)
					break;
			}
			fclose(fp_out);
			break;

		/*
		 * bank [ bank ] [ name ] [...]
		 *
		 * Read/write bank name (R8500).
		 */
		case C_BANK:
			if (argn < 2) {
				temp = cp->bank;
			} else if (sscanf(argv[1], "%d", &temp) != 1) {
				printf("*** bad bank number\n");
				break;
			}
			if (loadsub(rp, cp, 2) == R_ERR)
				break;

			if (argn > 2)
				loadbank(ident, temp, cp->name);
			if (readbank(ident, temp, s1) == R_ERR) {
				printf("*** radio %s can't do that\n",
				    rp->name);
				break;
			}
			cp->bank = temp;
			printf("bank %d %s\n", temp, s1);
			break;

		/*
		 * chan [ chan ]
		 *
		 * Read frequency, mode and other data from a memory
		 * channel.
		 */
		case C_CHAN:
			if (argn < 2) {
				pflags |= P_DISP | P_DSPCH;
				break;

			}
			if (argchan(rp, vp, argv[1]) == R_ERR)
				break;

			setchan(ident, vp->bank, vp->mchan);
			if (readchan(ident, vp) == R_ERR) {
				printf("*** empty channel\n");
				break;
			}
			
			pflags |= P_DISP | P_DSPCH;
			break;

		/*
		 * write [ chan ]
		 *
		 * Write frequency, mode and other data to a memory
		 * channel.
		 */
		case C_WRITE:
			if (argn > 1) {
				if (argchan(rp, vp, argv[1]) == R_ERR)
					break;
			}
			if (vp->freq == 0) {
				printf("*** empty channel\n");
				break;
			}
			if (setcmda(ident, cmdwrite, s1) == R_ERR) {
				printf("*** radio %s can't do that\n",
				    rp->name);
				break;
			}
			break;

		/*
		 * clear [ chan ] [ chan ]
		 *
		 * Clear memory channel. If no arguments, clear current
		 * channel; if one argument, clear that; if two
		 * argments, clear the block.
		 */
		case C_CLEAR:
			if (argn > 2) {
				if (argchan(rp, vp, argv[2]) == R_ERR)
					break;

				par1 = vp->bank;
				par2 = vp->mchan;
				if (argchan(rp, vp, argv[1]) == R_ERR)
					break;

			} else if (argn > 1) {
				if (argchan(rp, vp, argv[1]) == R_ERR)
					break;

				par1 = vp->bank;
				par2 = vp->mchan;
			} else {
				par1 = vp->bank;
				par2 = vp->mchan;
			}
			while (1) {
				setchan(ident, vp->bank, vp->mchan);
				if (setcmda(ident, cmdclear, s1) ==
				    R_ERR) {
					printf("*** radio %s can't do that\n",
				 	   rp->name);
					break;
				}
				if (vp->bank == par1 && vp->mchan ==
				    par2)
					break;

				if (argchan(rp, vp, "+") == R_ERR)
					break;
			}
			vp->freq = 0;
			break;

		/*
		 * freq [ freq ] [ mode ]
		 *
		 * Set current operating frequency (kHz or MHz) and
		 * optional mode. The default is to display the current
		 * frequency and mode. Note that the mode has to be set
		 * first, since some radios shift only the BFO and don't
		 * compensate the local oscillator. Watch out for
		 * ham/general-coverage switch; some radios won't let
		 * you set the frequency outside the ham bands if the
		 * switch is in the ham position. This holds also when
		 * reading a memory channel, which can be stored either
		 * way. 
		 */
		case C_FREQ:
			if (argn < 2) {
				pflags |= P_DISP;
				break;
			}
			if (argn > 2) {
				temp = argsx(argv[2], rp->modetab);
				if (temp == R_ERR)
					break;

				if (loadmode(ident, temp) == R_ERR) {
					printf("*** bad mode\n");
					break;
				}
				vp->mode = temp;
			}
			if (sscanf(argv[1], "%lf", &freq) != 1) {
				printf("*** bad frequency format\n");
				break;
			}
			if (freq > 1000.)
				freq /= 1000.;
			if (loadfreq(ident, freq) == R_ERR) {
				printf("*** bad frequency\n");
				break;
			}	
			vp->freq = freq;
			pflags |= P_DISP;
			break;

		/*
		 * load chan freq [...]
		 *
		 * Read/write channel data (R8500).
		 *
		 * This command loads the memory channel directly
		 * without affecting the working registers. Unless
		 * overriden, the name, mode, dial tuning step and
		 * attenuator apply to all subsequent loads. These
		 * functions can be set in the [...] list and will apply
		 * to the current and subsequent loads.
		 *
		 * The [...] options are in pairs
		 *
		 * name     up to eight characters     
		 * mode     as in the mode command
		 * dial     as in the dial command
		 * atten    as in the atten command
		 * duplex   as in the duplex command
		 */
		case C_LOAD:
			if (argn < 2) {
				printf("*** argument required\n");
				break;
			}
			if (argchan(rp, cp, argv[1]) == R_ERR)
				break;

			if (argn < 3) {
				if (read_chan(ident, cp) == R_ERR)
					printf("*** radio %s can't do that\n",
					    rp->name);
				break;
			}
			if (sscanf(argv[2], "%lf", &freq) != 1) {
				printf("*** bad frequency format\n");
				break;
			}
			if (freq > 1000)
				freq /= 1000;
			cp->freq = freq;
			if (loadsub(rp, cp, 3) == R_ERR)
				break;

			if (cp->pstep < .5)
				cp->pstep = .5;
			if (write_chan(ident, cp) == R_ERR) {
				printf("*** radio %s can't do that\n",
				    rp->name);
				break;
			}
			if (pflags & P_VERB) {
				printf("bank %d chan %d", cp->bank,
				    cp->mchan);
				printf(" freq %lf MHz mode %04x",
			   	    cp->freq, cp->mode);
				printf(" step %02x pstep %.1lf atten %02x scan %02x name %s\n",
			    	    cp->step, cp->pstep, cp->atten,
				    cp->scan, cp->name);
			}
			break;

		/*
		 * (command not found)
		 *
		 * We get here if the first argument matches no valid
		 * command name. If it has valid floating point format,
		 * set the frequency as given. If so and there is an
		 * additional argument, set the mode as given.
		 */
		case C_FREQX:
			if (argn > 1) {
				temp = argsx(argv[1], rp->modetab);
				if (temp == R_ERR)
					break;

				if (loadmode(ident, temp) == R_ERR)
					break;

				vp->mode = temp;
			}
			if (sscanf(argv[0], "%lf", &freq) != 1) {
				printf("*** bad frequency format\n");
				break;
			}
			if (freq > 1000.)
				freq /= 1000;
			if (loadfreq(ident, freq) == R_ERR) {
				printf("*** bad frequency\n");
				break;
			}
			vp->freq = freq;
			pflags |= P_DISP;
			break;

		/*
		 * offset [ <offset> ]
		 *
		 * Set VFO frequency offset.
		 */
		case C_OFFSET:
			if (argn < 2) {
				printf("VFO offset %.0lf Hz\n",
				    rp->offset);
				break;
			}
			if (sscanf(argv[1], "%lf", &freq) != 1) {
				printf("*** bad offset format\n");
				break;
			}
			rp->offset = freq;
			break;

		/*
		 * comp [ <offset> ]
		 *
		 * Compensate VFO frequency offset.
		 */
		case C_VCOMP:
			if (argn < 2) {
				printf("VFO compensation %.3lf PPM\n",
				    rp->freq_comp * 1e6);
				break;
			}
			if (sscanf(argv[1], "%lf", &freq) != 1) {
				printf("*** bad compensation format\n");
				break;
			}
			rp->freq_comp = freq / 1e6;
			break;

		/*
		 * band [ <low> ] [ <high> ]
		 *
		 * Set band scan limits.
		 */
		case C_BAND:
			if (argn < 2) {
				printf("band %s\n", getcap("band",
				    rp->cap));
				break;
			}
			if (argn < 3) {
				printf("*** two arguments required\n");
				break;
			}
			if (sscanf(argv[1], "%lf", &freq) != 1) {
				printf("*** bad format arg 1\n");
				break;
			}
			if (sscanf(argv[2], "%lf", &step) != 1) {
				printf("*** bad format arg 2\n");
				break;
			}
			if (freq > step) {
				dtemp = freq;
				freq = step;
				step = dtemp;
			}
			if (freq < rp->lband)
				freq = rp->lband;
			rp->lstep = freq;
			if (step > rp->uband)
				step = rp->uband;
			rp->ustep = step;
			break;

		/*
		 * Some radios synthesize BFO frequencies separately for
		 * each mode. The BFO offset command is used to correct
		 * for the intrinsic frequency errors (Hz) in each mode.
		 * Other radios synthesize all frequencies from a single
		 * crystal, so the BFO offsets should all be zero. The
		 * VFO compensation command is used to correct for the
		 * intrinsic frequency error (PPM) of the main
		 * synthesizer.
		 *
		 * mode [ mode ] [ offset ]
		 *
		 * Set current operating mode and BFO offset.
		 */
		case C_MODE:
			if (argn < 2) {
				freq = rp->bfo[vp->mode & 0x7];
				printf("mode %s BFO offset %.0lf Hz\n",
				    modetoa(vp->mode, rp->modetab),
				    freq);
				break;
			}
			temp = argsx(argv[1], rp->modetab);
			if (temp == R_ERR)
				break;

			if (loadmode(ident, temp) == R_ERR)
				break;

			vp->mode = temp;
			if (flags & F_RELD)
				loadfreq(ident, vp->freq);
			if (argn > 2) {
				if (sscanf(argv[2], "%lf", &freq) != 1)
				    {
					printf(
					    "*** bad offset format\n");
					break;
				}
				rp->bfo[vp->mode & 0x7] = freq;
			}
			pflags |= P_DISP;
			break;

		/*
		 * Set the operating mode.
		 */
		case C_MODEG:
			temp = argsx(argv[0], rp->modetab);
			if (temp == R_ERR)
				break;

			if (loadmode(ident, temp) == R_ERR)
				break;

			vp->mode = temp;
			if (flags & F_RELD)
				loadfreq(ident, vp->freq);
			pflags |= P_DISP;
			break;

		/*
		 * The following commands require the transmit duplex
		 * offset feature in most VHF/UHV transceivers.
		 *
		 * duplex [ duplex ]
		 *
		 * Set transmit offset for FM duplex.
		 */
		case C_DUPLEX:
			if (!(flags & F_OFFSET)) {
				printf("*** radio %s can't do that\n",
				    rp->name);
				break;
			}
			if (argn < 2) {
				printf("duplex %+.0lf kHz\n",
				    vp->duplex);
				break;
			}
			flags &= ~F_SMPLX;
			if (sscanf(argv[1], "%lf", &freq) != 1) {
				printf("*** bad duplex format\n");
				break;
			}
			if (loadoffset(ident, freq))
				break;

			vp->duplex = freq;
			if (pflags & P_VERB)
				pflags |= P_DISP;
			break;

		/*
		 * simplex (toggle)
		 *
		 * Receive on transmit frequency.
		 */
		case C_SMPLX:
			if (!(flags & F_OFFSET)) {
				printf("*** radio %s can't do that\n",
				    rp->name);
				break;
			}
			if (flags & F_SMPLX) {
				freq = (vp->freq -
				    rp->oldplex) * 1000.;
				if (loadoffset(ident, freq))
					break;

				if (loadfreq(ident, rp->oldplex))
					break;

				flags &= ~F_SMPLX;
				vp->duplex = freq;
			} else {
				freq = vp->freq +
				    vp->duplex / 1000.;
				rp->oldplex = vp->freq;
				if (loadoffset(ident, 0))
					break;

				if (loadfreq(ident, freq))
					break;

				vp->duplex = freq;
				flags |= F_SMPLX;
			}
			if (pflags & P_VERB)
				pflags |= P_DISP;
			break;

		/*
		 * The following commands should work in all receivers
		 * and transceivers.
		 *
		 * Tuning rate commands. These adjust the tuning steps
		 * in 1-2.5-5 sequence from the minimum step usable with
		 * the radio to 5 MHz per step(!). Each time the tuning
		 * step is changed, the vfo frequency is aligned to the
		 * current least significant digit padded by zeros.
		 *
		 * rate [ <rate> ]
		 *
		 * Set tuning rate. The values of <rate> from 0 through
		 * 20 select the rate values in a 1-2.5-5-10 sequence.
		 */
		case C_RATE:
			if (argn > 1) {
				if (sscanf(argv[1], "%d", &temp) != 1) {
					printf("*** bad rate format\n");
					break;
				}
				if (temp > 20)
					temp = 20;
				else if (temp < rp->minstep)
					temp = rp->minstep;
				rp->rate = temp;
				rp->step = logtab[rp->rate];
			}
			printf("rate %d step %.0lf Hz\n", rp->rate,
			    rp->step);
			break;

		/*
		 * rate up (keypad)
		 *
		 * Set tuning rate up one notch.
		 */
		case C_RUP:
			if (rp->rate < 20)
				rp->rate++;
			rp->step = logtab[rp->rate];
			step = modf(vp->freq / (rp->step * 1e-6),
			    &freq);
			freq *= rp->step * 1e-6;
			if (loadfreq(ident, freq) != R_ERR)
				printf("step %.0lf Hz\n", rp->step);
			break;

		/*
		 * rate down (keypad)
		 *
		 * Set tuning rate down one notch.
		 */
		case C_RDOWN:
			if (rp->rate > rp->minstep)
				rp->rate--;
			rp->step = logtab[rp->rate];
			step = modf(vp->freq / (rp->step * 1e-6),
			    &freq);
			freq *= rp->step * 1e-6;
			if (loadfreq(ident, freq) != R_ERR)
				printf("step %.0lf Hz\n", rp->step);
			break;

		/*
		 * Tuning step commands. The step command sets the
		 * tuning step directly to an arbitrary value. The up
		 * and down commands shift the frequency up or down by
		 * the value of the tuning step.
		 *
		 * step [ <step> ]
		 *
		 * Set tuning step directly in kHz. This is useful when
		 * scanning odd channel spacings, such as aviation and
		 * marine radio channels. Note that the tuning rate is
		 * set to minimum here, since otherwise the rounding
		 * process would violate the principle of least
		 * astonishment.
		 */
		case C_STEP:
			if (argn < 2) {
				printf("rate %d step %.0lf Hz\n",
				    rp->rate, rp->step);
				break;
			} else {
				if (sscanf(argv[1], "%lf", &step) != 1)
				    {
					printf("*** bad step format\n");
					break;
				}
			}
			rp->rate = rp->minstep;
			if (step < logtab[rp->minstep])
				step = logtab[rp->minstep];
			rp->step = step;
			break;

		/*
		 * up (keypad)
		 *
		 * Tune up one step.
		 */
		case C_UP:
			freq = vp->freq + rp->step / 1e6;
			if (freq > rp->ustep + rp->step / 2e6)
				freq = rp->lstep;
			if (loadfreq(ident, freq) != R_ERR)
				vp->freq = freq;
			if (pflags & P_VERB)
				pflags |= P_DISP;
			break;

		/*
		 * down (keypad)
		 *
		 * Tune down one step.
		 */
		case C_DOWN:
			freq = vp->freq - rp->step / 1e6;
			if (freq < rp->lstep - rp->step / 2e6)
				freq = rp->ustep;
			if (loadfreq(ident, freq) != R_ERR)
				vp->freq = freq;
			if (pflags & P_VERB)
				pflags |= P_DISP;
			break;

		/*
		 * The following commands are known to work in the 775
		 * transceiver and probably the 781.
		 *
		 * vfo [ <command> ]
		 *
		 * Set vfo (V_VFO) subcommands.
		 */
		case C_VFO:
			if (argn < 2) {
				printf("vfo %s\n",
				    getcap("vfo", rp->cap));
				break;
			}
			temp = scanarg(rp, argv[1], vfo, V_SVFO);
			if (temp == R_ERR)
				break;

			setcap("vfo", rp->cap, argv[1]);
			switch (temp) {

			case S_XCHNG:
				freq = rp->sub;
				rp->sub = vp->freq;
				vp->freq = freq;
				break;

			case S_EQUAL:
				rp->sub = vp->freq;
				break;
			}
			break;

		/*
		 * The following commands require the transmit split
		 * feature in some HF transceivers.
		 *
		 * split [ <offset> ]
		 *
		 * Set the transmit frequency relative to the receive
		 * frequency and turn the split on. If no arguments,
		 * just toggle the split on and off.
		 */
		case C_SPLIT:
			if (argn < 2 && rp->sub != 0) {
				if (flags & F_SPLIT) {
					setcmd(ident, V_SPLIT, S_OFF);
					flags &=~F_SPLIT;
				} else {
					setcmd(ident, V_SPLIT, S_ON);
					flags |= F_SPLIT;
				}
				pflags |= P_DISP;
				break;
			}
			setcmd(ident, V_SPLIT, S_ON);
			if (argn < 2) {
				step = 0;
			} else {
				if (sscanf(argv[1], "%lf", &step) != 1)
				    {
					printf("*** bad format\n");
					break;
				}
			}
			if (step == 0 || *argv[1] == '+' || *argv[1] ==
			    '-')
				step = vp->freq + step / 1000.;
			else if (step > 1000.)
				step /= 1000.;
			freq = vp->freq;
			setcmd(ident, V_SVFO, S_EQUAL);
			loadfreq(ident, step);
			setcmd(ident, V_SVFO, S_XCHNG);
			vp->freq = freq;
			rp->sub = step;
			flags |= F_SPLIT;
			if (pflags & P_VERB)
				pflags |= P_DISP;
			break;

		/*
		 * change (toggle)
		 *
		 * Swap main and sub VFOs.
		 */
		case C_CHANGE:
			if (!(flags & F_SPLIT)) {
				printf(
				    "*** transmit split must be on\n");
				break;
			}
			if (setcmd(ident, V_SVFO, S_XCHNG) == R_ERR) {
				printf("*** radio %s can't do that",
				    rp->name);
				break;
			}
			freq = vp->freq;
			vp->freq = rp->sub;
			rp->sub = freq;
			if (pflags & P_VERB)
				pflags |= P_DISP;
			break;

		/*
		 * key <string>
		 *
		 * Transmit quoted ASCII string as CW (775)
		 */
		case C_KEY:
			ptr = getcap("key", rp->cap);
			if (argn < 2) {
				if (*ptr == '\0') {
					printf("key undefined\n");
					break;
				}
			} else {
				ptr = argv[1];
			}
			if (sendcw(ident, ptr) == R_ERR)
				printf("*** radio %s can't do that\n",
				    rp->name);
			else
				setcap("key", rp->cap, ptr);
			break;

		/*
		 * dial [ <step> ]
		 *
		 * Set dial tuning step. This command works with both
		 * the 775 and R8500; however, the allowable arguments
		 * are different. Note that in the R8500 the allowable
		 * steps are constrained to multiples of 0.5 kHz.
		 */
		case C_DIAL:
			ptr = getcap("dial", rp->cap);
			if (argn < 2) {
				if (*ptr == '\0')
					printf("dial undefined\n");
				else
					printf("dial %s\n", ptr);
				break;
			}
			if (flags & F_BANK)
				temp = argsx(argv[1], diala);
			else
				temp = argsx(argv[1], dialb);
			if (temp == R_ERR) {
				break;

			} else if (sscanf(argv[1], "%lf", &dtemp) !=
			    1) {
				printf("*** invalid format\n");
			} else if (loaddial(ident, temp, dtemp) ==
			    R_ERR) {
				printf("*** radio %s can't do that\n",
				    rp->name);
			} else {
				cp->step = temp;
				if (step == 13)
					cp->pstep = dtemp;
				setcap("dial", rp->cap, argv[1]);
			}
			break;

		/*
		 * The control commands read or write a internal
		 * register associated with a front panel control. The
		 * 706MKIIG can read these registers but cannot write
		 * them. The R8500 can write them but not read them. For
		 * the R8500 a write command disables the front panel
		 * control until the control is set to zero, then the
		 * control is reenabled.
		 */
		/*
		 * meter
		 *
		 * Read S meter and squelch. Note that the S meter is
		 * decoded in S units and dB above S9.
		 */
		case C_METER:
			temp = setcmda(ident, cmdsig,
			    (u_char *)&rspsig);
			if (temp == R_ERR) {
				printf("*** radio %s can't do that\n",
			  	    rp->name);
				break;
			}
			temp = rspsig.meter[0] & 0xf;
			temp = temp * 10 + (rspsig.meter[1] >> 4);
			temp = temp * 10 + (rspsig.meter[1] & 0xf);
			for (i = 0; temp > mtab[i].smeter; i++);
			setcmda(ident, cmdsqu, (u_char *)&rspsqu);
			if (rspsqu.id == 1)
				printf("meter %s squelch open\n",
				    mtab[i].pip);
			else
				printf("meter %s squelch closed\n",
				    mtab[i].pip);
			break;
		/*
		 * volume [ value ]
		 *
		 * Set volume control range 0 to 255.
		 */
		case C_VOLUME:
			setctrl(rp, "vol", S_WRAF);
			break;

		/*
		 * squelch [ value ]
		 *
		 * Set squelch control range 0 to 255.
		 */
		case C_SQUELCH:
			setctrl(rp, "squelch", S_WRSQ);
			break;

		/*
		 * shift [ value ]
		 *
		 * Set IF shift control range -128 to 127.
		 */
		case C_SHIFT:
			setctrl(rp, "shift", S_WRSH);
			break;

		/*
		 * peak [ value ]
		 *
		 * Set audio peak filter (APF) control range -128 to
		 * 127. The APF function is operative only if enabled by
		 * the apf command.
		 */
		case C_APFC:
			setctrl(rp, "peak", S_WRAP);
			break;

		/*
		 * The switch commands activate and deactivate functions
		 * associated with front panel radio buttons. A button
		 * toggles an associated function on or off, while a
		 * command explicitly sets the function on or off.
		 * 
		 * ant [ 1 | 2 ]
		 *
		 * Select antenna 1/2.
		 */
		case C_ANT:
			setswitch(rp, "ant", ant, V_SANT);
			break;

		/*
		 * power [ off | on ]
		 *
		 * Set power on/off. The radio will be powered off after
		 * the sleep interval, but it will still listen for a
		 * power on command.
		 */
		case C_POWER:
			setswitch(rp, "power", power, V_POWER);
			break;

		/*
		 * agc [ slow | fast ]
		 *
		 * Set AGC time constant slow/fast.
		 */
		case C_AGC:
			if (flags & F_BANK)
				setswitch(rp, "agc", agc, V_TOGL);
			else
				setswitch(rp, "agc", agc1, V_TOGL);
			break;

		/*
		 * nb [ off | on ]
		 *
		 * Set noise blanker off/on.
		 */
		case C_NB:
			if (flags & F_BANK)
				setswitch(rp, "nb", nb, V_TOGL);
			else
				setswitch(rp, "nb", nb1, V_TOGL);
			break;

		/*
		 * apf [ off | on ]
		 *
		 * Set audio peak filter (APF) off/on. Note that there
		 * are two modes for the APF, narrow and wide. The mode
		 * alternates when the APF button is held down for one
		 * second, but cannot be controlled by the program.
		 */
		case C_APF:
			setswitch(rp, "apf", apf, V_TOGL);
			break;

		/*
		 * atten [ 0 | 10 | 20 | 30 ]
		 *
		 * Select attenuator 0/10/20/30 dB.
		 */
		case C_ATTEN:
			temp = setswitch(rp, "atten",
			    atten, V_ATTEN);
			if (temp != R_ERR)
				rp->atten = temp;
			break;

		/*
		 * say [ off | on ]
		 *
		 * Set announce control off/on. This requires the UT-102
		 * Voice Synthesizer Unit.
		 */
		case C_ANNC:
			setswitch(rp, "say", annc, V_ANNC);
			break;

		/*
		 * preamp [ off | on ]
		 *
		 * Preamp off/on.
		 */
		case C_PAMP:
			setswitch(rp, "preamp", preamp, V_TOGL);
			break;

		/*
		 * tone [ off | on | nosquelch | squelch ]
		 *
		 * Repeater tone off/on.
		 */
		case C_TONE:
			setswitch(rp, "tone", tone, V_TOGL);
			break;

		/*
		 * comp [ off | on ]
		 *
		 * Speech compressor  off/on.
		 */
		case C_COMP:
			setswitch(rp, "comp", comp, V_TOGL);
			break;

		/*
		 * vox [ off | on ]
		 *
		 * Voice control off/on.
		 */
		case C_VOX:
			setswitch(rp, "vox", vox, V_TOGL);
			break;

		/*
		 * qsk [ off | on ]
		 *
		 * QSK break in off/on.
		 */
		case C_QSK:
			setswitch(rp, "qsk", qsk, V_TOGL);
			break;

		/*
		 * scan [ <command> ]
		 *
		 * Scan control (V_SCAN) subcommands.
		 */
		case C_SCAN:
			setswitch(rp, "scan", scan, V_SCAN);
			if (strcasecmp(argv[1], "stop") == 0) {
				readfreq(ident, &vp->freq);
				pflags |= P_DISP;
			}
			break;

		/*
		 * Miscellaneous control (S_CTRL) subcommands.
		 */
		case C_MISC:
			setswitch(rp, "misc", misc, V_CTRL);
			break;
		}
		update(rp, vp);
	}
	return (R_OK);
}


/*
 * reset(radio) - initialize radio
 */
static void
reset(
	struct icom *rp		/* radio structure */
	)
{
	struct cmd2msg cmdmodel = {V_RDID, 0, FI};
	struct cmdvec *table;
	char	s1[VALMAX];
	u_char	rsp[CMDMAX];
	int	temp, i;

	/*
	 * Initialize radio capabilities
	 */
	table = rp->probe;
	sprintf(s1, "%s (%02x) %g-%g MHz %d chan %d bank",
	    getcap(rp->name, identab), rp->ident, rp->lband, rp->uband,
	    rp->maxch + 1, rp->maxbk + 1);
	setcap("radio", rp->cap, s1);
	update(rp, &rp->vfo);

	/*
	 * Scan through capabilities list.
	 */
	if (table == NULL)
		return;

	for (i = 0; table[i].name[0] != '\0'; i++) {
		temp = setcmda(rp->ident, table[i].cmd, rsp);
		if (temp != R_ERR)
			setcap(table[i].name, rp->cap, table[i].descr);
	}
}


/*
 * update(radio, chan) - update capabilities
 */
static void
update(
	struct icom *rp,	/* radio structure */
	struct chan *vp		/* channel structure */
	)
{
	char	s1[VALMAX];
	int	temp;

	/*
	 * Update chan, freq, mode, duplex, split capabilities.
	 */
	temp = sigtab[rp->rate];
	sprintf(s1, "%.*lf MHz %s", temp, vp->freq, modetoa(vp->mode,
	    rp->modetab));
	setcap("chan", rp->cap, s1);
	sprintf(s1,"%.*lf-%.*lf MHz step %.0lf Hz", temp, rp->lstep,
	    temp, rp->ustep, rp->step);
	setcap("band", rp->cap, s1);
	if (flags & F_SPLIT) {
 		sprintf(s1, "%.*lf MHz", temp, rp->sub);
		setcap("split", rp->cap, s1);
	}
	if (flags & F_OFFSET && vp->duplex != 0) {
		sprintf(s1, "%+.0lf kHz", vp->duplex);
		setcap("duplex", rp->cap, s1);
	}
	if (!(pflags & P_DISP))
		return;

	if (pflags & P_DSPCH)
		printf("chan %d.%d ", vp->bank, vp->mchan);
	printf("%.*lf %s", temp, vp->freq, modetoa(vp->mode,
	    rp->modetab));
	if (pflags & F_SPLIT)
		printf(" %.*lf", temp, rp->sub);
	if (vp->duplex != 0)
		printf(" %+.0lf", vp->duplex);
	if (flags & F_SMPLX)
		printf(" S");
	printf("\n");
	pflags &= ~(P_DISP | P_DSPCH); 
}


/*
 * modetoa(ident, table) - returns capability name
 */
char *				/* capability name, "" (not found) */
modetoa(
	int	ident,		/* capability key */
	struct cmdtable *table	/* capability table */
	)
{
	int i;

	for (i = 0; table[i].name[0] != '\0'; i++) {
		if (table[i].ident == ident)
			return (table[i].name);
	}
	return ("");
}


/*
 * argsx(name, table) - returns capability key
 */
int				/* capability key, -1 (not found) */
argsx(
	char	*name,		/* capability name */
	struct cmdtable *table	/* capability table */
	)
{
	int i, temp;

	if (*name == '?') {
		for (i = 0; table[i].name[0] != '\0'; i++)
			printf("%10s %s\n", table[i].name,
			    table[i].descr);
		return (R_ERR);
	}
	for (i = 0; table[i].name[0] != '\0'; i++) {
		if (strcasecmp(name, table[i].name) == 0 ||
		    *table[i].name == '*')
			break;
	}
	if (table[i].ident == R_ERR)
		printf("*** %s\n", table[i].descr);
	return (table[i].ident);
}


/*
 * getcap(name, table) - return pointer to capability string
 */
char *				/* capability string, "" (not found") */
getcap(
	char	*name,		/* capability name */
	struct cmdtable *table	/* capability table */
	)
{
	int i;

	if (*name == '?') {
		for (i = 0; table[i].name[0] != '\0'; i++)
			printf("%10s %s\n", table[i].name,
			    table[i].descr);
		return ("");
	}
	for (i = 0; table[i].name[0] != '\0'; i++) {
		if (strcasecmp(name, table[i].name) == 0)
			break;
	}
	return (table[i].descr);
}


/*
 * setcap(name, table, string) - insert capability string
 */
void
setcap(
	char *name,		/* capability name */
	struct cmdtable *table,	/* capability table */
	char *string		/* capability string */
	)
{
	int i;

	for (i = 0; table[i].name[0] != '\0'; i++) {
		if (strcasecmp(name, table[i].name) == 0) {
			strcpy(table[i].descr, string);
			return;
		}
	}
	strcpy(table[i].name, name);
	strcpy(table[i].descr, string);
	table[i + 1].name[0]  = '\0';
	table[i + 1].ident = R_ERR;
	table[i + 1].descr[0] = '\0';
}


/*
 * scanarg(radio, arg, table, sub) - execute capability
 */
static int			/* opcode, -1 (error) */
scanarg(
	struct icom *rp,	/* radio structure */
	char	*name,		/* subcommand */
	struct cmdtable *table, /* subcommand table */
	int	cmd		/* command */
	)
{
	int	temp;		/* temp */

	temp = argsx(name, table);
	if (temp != R_ERR) {
		if (setcmd(rp->ident, cmd, temp) == R_ERR) {
			printf("*** radio %s can't do that\n",
			    rp->name);
			temp = R_ERR;
		}
	}
	return (temp);
}


/*
 * setswitch(radio, name, op) - set switches.
 */
static int			/* opcode, -1 (error) */
setswitch(
	struct icom *rp,	/* radio structure pointer */
	char	*name,		/* command name */
	struct cmdtable *cmmd,	/* command pointer */
	int	op		/* operation code */
	)
{
	int	temp, i;
	char	*s1;
	u_char	cmd[] = {op, FI, FI};
	u_char	rsp[CMDMAX];
	int	rval;

	if (argn < 2) {
		if (op & 0x8000)
			cmd[1] = cmmd->ident & 0x7f;
		rval = setcmda(rp->ident, cmd, rsp);
		if (rval == R_ERR) {
			printf("*** radio %s can't do that\n",
			    rp->name);
			return (rval);;
		}
		temp = rsp[1];
		if (op & 0x8000)
			temp |= 0x8000 | (rsp[2] << 8);
		for (i = 0; cmmd[i].ident != R_ERR; i++) {
			if (temp == cmmd[i].ident)
				break;
		}
		if (cmmd[i].ident == R_ERR)
			printf("*** radio %s can't do that\n",
			    rp->name);
		else
			printf("%s %s\n", name, cmmd[i].name);
		return (rval);
	}
	return (scanarg(rp, argv[1], cmmd, op));
}


/*
 * setctrl(radio, name, op) - twirl controls.
 *
 * +	current value plus 8
 * -	current value minus 8
 * num	specified value 
 *
 * Argument is clamped at low and high limits of valid range.
 */
static int			/* opcode, -1 (error) */
setctrl(
	struct icom *rp,	/* radio structure */
	char	*name,		/* command name */
	int	op		/* operation code */
	)
{
	struct metermsg msgctl = {V_WRCTL, 0, 0, 0, FI};
	u_char	ctlmsg[] = {V_RDCTL, 0, FI};
	u_char	rsp[NAMMAX];
	char	*s1, s2[VALMAX];
	int	temp, incr;

	if (argn < 2) {

		/*
		 * Known radios can't read anything but S meter and
		 * squelch. This is a half-baked effort until some radio
		 * is found that can do more than that.
		 */
		ctlmsg[1] = op;
		temp = setcmda(rp->ident, ctlmsg, (u_char *)&rsp);
		if (temp == R_ERR) {
			printf("*** radio %s can't do that\n",
		  	    rp->name);
		}
		return (temp);
	}
	sscanf(s1, "%d", &temp);
	incr = 0;
	if (*argv[1] == '+') {
		incr = 8;
	} else if (*argv[1] == '-') {
		incr = -8;
	} else if (sscanf(argv[1], "%d", &temp) != 1) {
		printf("*** invalid argument\n");
		return (R_ERR);
	}
	if (op == S_WRSH || op == S_WRAP)
		temp += 128;
	temp += incr;
	if (temp < 0)
		temp = 0;
	else if (temp > 255)
		temp = 255;
	sprintf(s1, "%04d", temp);
	msgctl.subcmd = op;
	msgctl.meter[0] = ((s1[0] & 0x0f) << 4) | (s1[1] &
	   0x0f);
	msgctl.meter[1] = ((s1[2] & 0x0f) << 4) | (s1[3] &
	   0x0f);
	incr = setcmda(rp->ident, (u_char *)&msgctl, rsp);
	if (incr == R_ERR) {
		printf("*** radio %s can't do that\n", rp->name);
		return (R_ERR);
	}
	if (op == S_WRSH || op == S_WRAP)
		temp -= 128;
	sprintf(s2, "%d", temp);
	return (temp);
}


/*
 * Getline(str) - process input line and extract tokens
 *
 * Blank lines and comments beginning with '#' are ignored and the
 * string converted to lower case. The resulting tokens are saved in the
 * *argv[] array. The number of tokens found is returned to the caller.
 */
static int			/* number of tokens */
mygetline(
	char	*str		/* pointer to input string */
	)
{
	char	*ptr;
	char	xbreak[] = " ,\t\n\0";
	char	sbreak[] = "\"\n\0";

	int i, j, temp;

	ptr = strchr(str, '\r');
	if (ptr != NULL)
		*ptr = '\0';
	ptr = strchr(str, '#');
	if (ptr != NULL)
		*ptr = '\0';
	ptr = str;
	for (i = 0; i < ARGMAX;) {
		temp = strspn(ptr, xbreak);
		ptr += temp;
		if (*ptr == '\0')
			break;

		if (*ptr == '"') {
			argv[i++] = ++ptr;
			temp = strcspn(ptr, sbreak);
		} else {
			argv[i++] = ptr;
			temp = strcspn(ptr, xbreak);
		}
		ptr += temp;
		if (*ptr == '\0')
			break;

		*ptr++ = '\0';
	}
	return (i);
}


/*
 * argchan(radio, chan, sptr) - decode channel argument
 *
 * NULL	current bank/channel
 * $	highest bank/channel
 * +	current bank/channel plus 1
 * -	current bank/channel minus 1
 * bank	specified bank
 * chan	specified channel
 *
 * Channel argument formats are in the form of a single '$', '+' or '-'
 * character, a single number (channel) or two numbers (bank.channel)
 * separated by '.'. Channel increments wrap around the valid channel
 * range and increment/decrement bank number, which wraps around its
 * range. Values specified are checked for format and range.
 */
static int			/* > 0 (ok), -1 (error) */
argchan(
	struct icom *rp,	/* radio structure */
	struct chan *cp,	/* channel structure */
	char	*sptr		/* ascii argument pointer */
	)
{
	int	bank, mchan;

	bank = cp->bank;
	mchan = cp->mchan;
	if (sptr == NULL) {
		return (R_OK);

	} else if (*sptr == '$') {
		bank = rp->maxbk;
		mchan = rp->maxch;
	} else if (*sptr == '+') {
		mchan++;
		if (mchan > rp->maxch) {
			mchan = rp->minch;
			bank++;
			if (bank > rp->maxbk)
				bank = 0;
		}
	} else if (*sptr == '-') {
		mchan--;
		if (mchan < rp->minch) {
			mchan = rp->maxch;
			bank--;
			if (bank < 0)
				bank = rp->maxbk;
		}
	} else if (sscanf(sptr, "%d.%d", &bank, &mchan) == 2) {
		if (bank < 0 || bank > rp->maxbk || mchan < rp->minch ||
		    mchan > rp->maxch) {
			printf("*** bad channel number\n");
			return (R_ERR);
		}
	} else if (sscanf(sptr, "%d", &mchan) == 1) {
		bank = cp->bank;
		if (mchan < rp->minch || mchan > rp->maxch) {
			printf("*** bad channel number\n");
			return (R_ERR);
		}
	} else {
		printf("*** bad channel format\n");
		return (R_ERR);
	}
	cp->bank = bank;
	cp->mchan = mchan;
	return (R_OK);
}


/*
 * loadsub() - called by the bank, restore and load commands
 */
int
loadsub(
	struct icom *rp,	/* radio structure */
	struct chan *cp,	/* channel pointer */
	int	index		/* args index */
	)
{
	double	dtemp;
	int	i, temp;

	i = index;
	temp= R_OK;
	for (i += 1; i < argn; i += 2) {
		temp = argsx(argv[i - 1], loadtab);
		switch (temp) {

		/*
		 * name
		 */
		case D_NAME:
			strncpy(cp->name, argv[i], 8);
			break;

		/*
		 * mode
		 */
		case D_MODE:
			temp = argsx(argv[i], rp->modetab);
			if (temp != R_ERR)
				cp->mode = temp;
			break;

		/*
		 * dial
		 */
		case D_DIAL:
			temp = argsx(argv[i], diala);
			if (temp == R_ERR)
				break;

			if (temp == 13) {
				if (sscanf(argv[i], "%lf", &dtemp) ==
				    1) {
					cp->step = temp;
					cp->pstep = dtemp;
				} else {
					printf("*** invalid format\n");
					temp = R_ERR;
				}
			} else {
				cp->step = temp;
			}
			break;

		/*
		 * atten
		 */
		case D_ATTEN:
			temp = argsx(argv[i], atten);
			if (temp != R_ERR)
				cp->atten = temp;
			break;

		/*
		 * duplex
		 */
		case D_DUPLEX:
			if (sscanf(argv[i], "%lf", &dtemp) != 1) {
				printf("*** invalid format\n");
				temp = R_ERR;
			} else {
				cp->duplex = dtemp;
			}
			break;

		/*
		 * unknown
		 */
		default:
			printf("*** radio %s can't do that\n",
			    rp->name);
		}
		if (temp == R_ERR)
			break;
	}
	return (temp);
}


/*
 * sw_keypad() - switch to keypad mode
 */
static int			/* 0 (ok), -1 (system error) */
sw_keypad()
{
	fd = open("/dev/tty", O_RDONLY);
	if (fd < 0) {
		printf("*** open error %d\n", errno);
		return (R_ERR);
	}
	if (tcgetattr(fd, &terma) < 0) {
		printf("*** tcgetattr error %d\n", errno);
		return (R_ERR);
	}
	tcgetattr(fd, &termb);
	termb.c_lflag &= ~(ICANON | ECHO);
	termb.c_cc[VMIN] = 1;
	termb.c_cc[VTIME] = 0;
	if (tcsetattr(fd, TCSADRAIN, &termb) < 0) {
		printf("*** tcsetattr error %d\n", errno);
		return (R_ERR);
	}
	return (R_OK);
}


/*
 * sw_keybd() - switch to keyboard mode
 */
static int			/* 0 (ok), -1 (system error) */
sw_keybd()
{
	if (tcsetattr(fd, TCSADRAIN, &terma) < 0) {
		printf("*** tcsetattr error %d\n", errno);
		return (R_ERR);
	}
	return (R_OK);
}

/* end program */