[go: up one dir, main page]

File: cmds.c

package info (click to toggle)
scli 0.3.1-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,872 kB
  • ctags: 8,627
  • sloc: ansic: 32,657; sh: 7,388; makefile: 321
file content (1848 lines) | stat: -rw-r--r-- 49,054 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
/* 
 * cmds.c -- basic commands for the scli command interpreter
 *
 * Copyright (C) 2001 Juergen Schoenwaelder
 *
 * 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 * @(#) $Id: cmds.c 2111 2007-01-02 23:46:40Z schoenw $
 */

#include "scli.h"

#include <ctype.h>
#include <readline/history.h>
#include <arpa/inet.h>



static GNetSnmpEnum const scli_regex_table[] = {
    { REG_EXTENDED,      "extended" },
    { REG_ICASE,	 "case-insensitive" },
    { 0, 0 }
};



static gint
alias_compare(gconstpointer a, gconstpointer b)
{
    scli_alias_t *alias_a = (scli_alias_t *) a;
    scli_alias_t *alias_b = (scli_alias_t *) b;

    return strcmp(alias_a->name, alias_b->name);
}



static int
cmd_scli_exit(scli_interp_t *interp, int argc, char **argv)
{
    g_return_val_if_fail(interp, SCLI_ERROR);
        
    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    return SCLI_EXIT;
}



static void
fmt_cmd_tree(GString *s, GNode *node, char *prefix)
{
    scli_cmd_t *cmd = (scli_cmd_t *) node->data;
    size_t len;
    const int width = 24;

    len = strlen(prefix);
    
    if (cmd) {
	g_string_sprintfa(s, "%s- ", prefix);
	g_string_sprintfa(s, "%-*s %s %s\n", (width-len > 0) ? width-len : 0,
			  cmd->name, cmd->path ? cmd->path : "",
			  cmd->options ? cmd->options : "");
    }

    for (node = g_node_first_child(node);
	 node; node = g_node_next_sibling(node)) {
	char *new_prefix;
	new_prefix = g_malloc(len + 4);
	strcpy(new_prefix, prefix);
	if (new_prefix[len-1] == '`') {
	    new_prefix[len-1] = ' ';
	}
	if (g_node_next_sibling(node)) {
	    strcat(new_prefix, len ? "  |" : " ");
	} else {
	    strcat(new_prefix, len ? "  `" : " ");
	}
	fmt_cmd_tree(s, node, new_prefix);
	g_free(new_prefix);
    }
}



static int
show_scli_command_tree(scli_interp_t *interp, int argc, char **argv)
{
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (interp->cmd_root) {
	fmt_cmd_tree(interp->result, interp->cmd_root, "");
    }
    return SCLI_OK;
}



struct show_scli_command_data {
    scli_interp_t *interp;
    regex_t *regex_cmd;
    int width;
    int cnt;
};



static void
fmt_cmd_info(GString *s, scli_cmd_t *cmd, int width)
{
    g_string_sprintfa(s, " %c%c%c  ",
		      cmd->flags & SCLI_CMD_FLAG_XML ? 'X' : '-',
		      cmd->flags & SCLI_CMD_FLAG_NEED_PEER ? 'S' : '-',
		      cmd->flags & SCLI_CMD_FLAG_NORECURSE ? '-' : 'R');
    g_string_sprintfa(s, "%-*s %s\n", width, cmd->path, cmd->options ? cmd->options : "");
}



static gboolean
show_scli_command_info_pass1(GNode *node, gpointer data)
{
    scli_cmd_t *cmd = (scli_cmd_t *) node->data;
    struct show_scli_command_data *x = (struct show_scli_command_data *) data;

    if (! x->regex_cmd || regexec(x->regex_cmd, cmd->path, (size_t) 0, NULL, 0) == 0) {
	if (strlen(cmd->path) > x->width) {
	    x->width = strlen(cmd->path);
	}
    }

    return FALSE;
}



static gboolean
show_scli_command_info_pass2(GNode *node, gpointer data)
{
    scli_cmd_t *cmd = (scli_cmd_t *) node->data;
    struct show_scli_command_data *x = (struct show_scli_command_data *) data;
    
    if (! x->regex_cmd || regexec(x->regex_cmd, cmd->path, (size_t) 0, NULL, 0) == 0) {
	fmt_cmd_info(x->interp->result, cmd, x->width);
    }

    return FALSE;
}



static int
show_scli_command_info(scli_interp_t *interp, int argc, char **argv)
{
    regex_t _regex_cmd, *regex_cmd = NULL;

    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (argc == 2) {
	regex_cmd = &_regex_cmd;
	if (regcomp(regex_cmd, argv[1], interp->regex_flags) != 0) {
	    g_string_assign(interp->result, argv[1]);
	    return SCLI_SYNTAX_REGEXP;
	}
    }

    if (interp->cmd_root) {
	struct show_scli_command_data x;
	x.interp = interp;
	x.width = 8;
	x.regex_cmd = regex_cmd;
	g_node_traverse(interp->cmd_root, G_IN_ORDER, G_TRAVERSE_LEAFS, -1,
			show_scli_command_info_pass1, &x);
	g_string_sprintfa(interp->header, "FLAGS %-*s ARGUMENTS", x.width, "COMMAND");
	g_node_traverse(interp->cmd_root, G_IN_ORDER, G_TRAVERSE_LEAFS, -1,
			show_scli_command_info_pass2, &x);
    }

    if (regex_cmd) regfree(regex_cmd);

    return SCLI_OK;
}



static void
fmt_cmd_details(GString *s, scli_cmd_t *cmd)
{
    const int indent = 14;
    g_string_sprintfa(s, "%-*s%s\n", indent, "Command:", cmd->path);
    if (cmd->options) {
	g_string_sprintfa(s, "%-*s%s\n", indent, "Arguments:", cmd->options);
    }
    g_string_sprintfa(s, "%-*sscli%s\n", indent, "Formats:",
		      cmd->flags & SCLI_CMD_FLAG_XML ? ", xml" : "");
    if (cmd->desc) {
	fmt_indent_string(s, indent, "Description:", strlen(cmd->desc), cmd->desc);
    }
    if (cmd->xsd) {
	fmt_indent_string(s, indent, "Schema:", strlen(cmd->xsd), cmd->xsd);
    }
}



static gboolean
show_scli_command_details_pass1(GNode *node, gpointer data)
{
    scli_cmd_t *cmd = (scli_cmd_t *) node->data;
    struct show_scli_command_data *x = (struct show_scli_command_data *) data;
    
    if (! x->regex_cmd || regexec(x->regex_cmd, cmd->path, (size_t) 0, NULL, 0) == 0) {
	if (x->cnt) {
	    g_string_append(x->interp->result, "\n");
	}
	fmt_cmd_details(x->interp->result, cmd);
	x->cnt++;
    }

    return FALSE;
}



static int
show_scli_command_details(scli_interp_t *interp, int argc, char **argv)
{
    regex_t _regex_cmd, *regex_cmd = NULL;

    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (argc == 2) {
	regex_cmd = &_regex_cmd;
	if (regcomp(regex_cmd, argv[1], interp->regex_flags) != 0) {
	    g_string_assign(interp->result, argv[1]);
	    return SCLI_SYNTAX_REGEXP;
	}
    }

    if (interp->cmd_root) {
	struct show_scli_command_data x;
	x.interp = interp;
	x.width = 8;
	x.regex_cmd = regex_cmd;
	x.cnt = 0;
	g_node_traverse(interp->cmd_root, G_IN_ORDER, G_TRAVERSE_LEAFS, -1,
			show_scli_command_details_pass1, &x);
    }

    if (regex_cmd) regfree(regex_cmd);

    return SCLI_OK;
}



static int
show_scli_modes(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_mode_t *mode;
    char *s;
    int i, c;
    regex_t _regex_mode, *regex_mode = NULL;

    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (argc == 2) {
	regex_mode = &_regex_mode;
	if (regcomp(regex_mode, argv[1], interp->regex_flags) != 0) {
	    g_string_assign(interp->result, argv[1]);
	    return SCLI_SYNTAX_REGEXP;
	}
    }
    
    for (c = 0, elem = interp->mode_list; elem; elem = g_slist_next(elem)) {
	mode = (scli_mode_t *) elem->data;
	if (regex_mode) {
	    if (regexec(regex_mode, mode->name, (size_t) 0, NULL, 0) != 0) {
		continue;
	    }
	}
	if (c) {
	    g_string_append(interp->result, " \n");
	}
	s = g_strdup_printf("%s MODE", mode->name);
	g_strup(s);
	g_string_sprintfa(interp->result, "%s\n\n%s\n \n", s, mode->desc);
	g_free(s);
	for (i = 0; mode->cmds[i].path; i++) {
	    g_string_sprintfa(interp->result, "    %s %s\n", 
			      mode->cmds[i].path,
			      mode->cmds[i].options ? mode->cmds[i].options : "");
	}
	for (i = 0; mode->cmds[i].path; i++) {
	    g_string_sprintfa(interp->result, "\n%s\n", mode->cmds[i].desc);
	}
	c++;
    }

    if (regex_mode) regfree(regex_mode); 
    
    return SCLI_OK;
}



static int
show_scli_schema(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_mode_t *mode;
    int i, c;
    regex_t _regex_mode, *regex_mode = NULL;

    if (argc > 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (argc == 2) {
	regex_mode = &_regex_mode;
	if (regcomp(regex_mode, argv[1], interp->regex_flags) != 0) {
	    g_string_assign(interp->result, argv[1]);
	    return SCLI_SYNTAX_REGEXP;
	}
    }

    g_string_append(interp->result,
	    "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n"
	    "\n"
	    "<xsd:annotation>\n"
	    "  <xsd:documentation xml:lang=\"en\">\n"
	    "    XML schema definition for the output produced by scli version " VERSION ".\n"
	    "  </xsd:documentation>\n"
	    "</xsd:annotation>\n"
	    "\n"
	    "<xsd:element name=\"scli\" type=\"ScliType\"/>\n"
	    "\n"
	    "<xsd:complexType name=\"ScliType\">\n"
	    "  <xsd:sequence>\n"
	    "    <xsd:element name=\"system\" type=\"SystemType\" minOccurs=\"0\"/>\n"		    
	    "    <xsd:element name=\"devices\" type=\"DevicesType\" minOccurs=\"0\"/>\n"		    
	    "  </xsd:sequence>\n"
	    "  <xsd:attribute name=\"version\" type=\"xsd:NMTOKEN\"/>\n"
	    "  <xsd:attribute name=\"peer\" type=\"xsd:NMTOKEN\"/>\n"
	    "  <xsd:attribute name=\"date\" type=\"xsd:string\"/>\n"
	    "</xsd:complexType>\n"
	    "\n");

    for (elem = interp->mode_list; elem; elem = g_slist_next(elem)) {
	mode = (scli_mode_t *) elem->data;
	if (regex_mode) {
	    if (regexec(regex_mode, mode->name, (size_t) 0, NULL, 0) != 0) {
		continue;
	    }
	}

	g_string_sprintfa(interp->result, "<!-- %s mode -->\n\n", mode->name);
	
	for (i = 0, c = 0; mode->cmds[i].path; i++) {
	    if (mode->cmds[i].xsd) {
		if (c) g_string_append(interp->result, "\n");
		g_string_sprintfa(interp->result, "%s\n", mode->cmds[i].xsd);
		c++;
	    }
	}
    }

    g_string_append(interp->result, "\n</xsd:schema>\n");
    
    if (regex_mode) regfree(regex_mode); 
    
    return SCLI_OK;
}



static int
help(scli_interp_t *interp, int argc, char **argv)
{
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    g_string_sprintfa(interp->result,
      "Scli is a command interpreter which can be used to browse,\n"
      "monitor and configure SNMP enabled devices. All scli commands\n"
      "are organized in a hierarchy. The top-level commands are:\n"
      "\n"
      " - open         Establish an association to a remote SNMP agent.\n"
      " - close        Close the association to a remote SNMP agent.\n"
      " - exit         Exit the scli command interpreter.\n"
      " - help         Show this help information.\n"
      " - history      Show the history of the last scli commands.\n"
      " - create       Create object instances on the remote SNMP agent.\n"
      " - delete       Delete object instances from the remote SNMP agent.\n"
      " - set          Modify object instances on the remote SNMP agent.\n"
      " - show         Show information provided by the remote SNMP agent.\n"
      " - monitor      Monitor information provided by the remote SNMP agent.\n"
      " - dump         Dump scli command sequences to restore configurations.\n"
      "\n"
      "Use the \"show scli command tree\" command to browse the complete\n"
      "scli command tree and the \"show scli modes\" command to obtain\n"
      "a detailed description of the various scli commands.\n");
    
    return SCLI_OK;
}



static int
history(scli_interp_t *interp, int argc, char **argv)
{
    HIST_ENTRY **the_list;
    int i;
    
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    the_list = history_list();
    if (the_list) {
	for (i = 0; the_list[i]; i++) {
	    g_string_sprintfa(interp->result, "%4d %s\n",
			      i + history_base, the_list[i]->line);
	}
    }

    return SCLI_OK;
}



static int
scli_cmd_open(scli_interp_t *interp, int argc, char **argv)
{
    gchar *host;
    gint ipv6, port = 161;
    gchar *community = NULL;
    GInetAddr *addr;
    int code;
    
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc < 2 || argc > 4) {
        return SCLI_SYNTAX_NUMARGS;
    }

    host = argv[1];
    if (argc == 3) {
	community = argv[2];
    }
    if (argc == 4) {
        port = atoi(argv[2]);
	community = argv[3];
    }

    addr = gnet_inetaddr_new(host, port);
    if (! addr) {
	return SCLI_SYNTAX_VALUE;
    }

    if (interp->taddress) gnet_inetaddr_delete(interp->taddress);
    interp->taddress = addr;
    ipv6 = gnet_inetaddr_is_ipv6(interp->taddress);

    /*
     * We prefer to use TCP so try TCP first and fall back to UDP if
     * this fails for whatever reason.
     */
#if 1
    interp->tdomain
	    = ipv6 ? GNET_SNMP_TDOMAIN_TCP_IPV6 : GNET_SNMP_TDOMAIN_TCP_IPV4;
    
    code = scli_open_community(interp, interp->tdomain,
			       interp->taddress, community);
    if (code == SCLI_OK) {
	return code;
    }
#endif
    interp->tdomain
	    = ipv6 ? GNET_SNMP_TDOMAIN_UDP_IPV6 : GNET_SNMP_TDOMAIN_UDP_IPV4;
    code = scli_open_community(interp, interp->tdomain,
			       interp->taddress, community);
    return code;
}



static int
cmd_scli_close(scli_interp_t *interp, int argc, char **argv)
{
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    scli_close(interp);

    return SCLI_OK;
}



static int
cmd_scli_load(scli_interp_t *interp, int argc, char **argv)
{
    GModule *module;
    gpointer pointer;
    gchar *path;
    GString *symbol;
    
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    path = g_module_build_path(SCLI_PLUGIN_PATH, argv[1]);
    module = g_module_open(path, 0);
    if (! module) {
	g_string_sprintfa(interp->result, "%s", g_module_error());
	g_free(path);
	return SCLI_ERROR;
    }
    g_free(path);

    symbol = g_string_new(NULL);
    g_string_sprintfa(symbol, "scli_init_%s_mode", argv[1]);
    if (! g_module_symbol(module, symbol->str, &pointer)) {
	g_string_sprintfa(interp->result, "%s", g_module_error());
	g_module_close(module);
	g_string_free(symbol, 1);
	return SCLI_ERROR;
    }
    g_string_free(symbol, 1);

    /*
     * Call the init function and save the module pointer,
     * lookup the new mode data structure and save the module
     * pointer in there. xxx
     */

    return SCLI_OK;
}



static int
cmd_scli_unload(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_mode_t *mode = NULL;
    
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    for (elem = interp->mode_list; elem; elem = g_slist_next(elem)) {
	mode = (scli_mode_t *) elem->data;
	if (strcmp(mode->name, argv[1]) == 0 && mode->module) {
	    break;
	}
    }

    if (elem && mode) {
	if (! g_module_close(mode->module)) {
	    g_string_sprintfa(interp->result, "%s", g_module_error());
	    return SCLI_ERROR;
	}
    }

    return SCLI_OK;
}



static int
create_scli_alias(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_alias_t *alias = NULL;
    
    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 3) {
	return SCLI_SYNTAX_NUMARGS;
    }

    for (elem = interp->alias_list; elem; elem = g_slist_next(elem)) {
	alias = (scli_alias_t *) elem->data;
	if (strcmp(alias->name, argv[1]) == 0) break;
    }

    if (elem) {
	g_free(alias->value);
	alias->value = g_strdup_printf(argv[2]);
    } else {
	alias = g_new0(scli_alias_t, 1);
	alias->name = g_strdup(argv[1]);
	alias->value = g_strdup(argv[2]);
	interp->alias_list = g_slist_insert_sorted(interp->alias_list,
						   alias, alias_compare);
    }
    
    return SCLI_OK;
}



static int
delete_scli_alias(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_alias_t *alias = NULL;
    regex_t _regex_name, *regex_name = NULL;
    
    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    regex_name = &_regex_name;
    if (regcomp(regex_name, argv[1], interp->regex_flags) != 0) {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_REGEXP;
    }
    
 again:
    for (elem = interp->alias_list; elem; elem = g_slist_next(elem)) {
	alias = (scli_alias_t *) elem->data;
	if (regexec(regex_name, alias->name, (size_t) 0, NULL, 0) == 0) {
	    interp->alias_list = g_slist_remove(interp->alias_list,
						elem->data);
	    g_free(alias->name);
	    g_free(alias->value);
	    g_free(alias);
	    goto again;
	}
    }

    if (regex_name) regfree(regex_name);
    
    return SCLI_OK;
}



static int
show_scli_aliases(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_alias_t *alias;
    int name_width = 16;
    int value_width = 16;

    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (interp->alias_list) {
	for (elem = interp->alias_list; elem; elem = g_slist_next(elem)) {
	    alias = (scli_alias_t *) elem->data;
	    if (strlen(alias->name) > name_width) {
		name_width = strlen(alias->name);
	    }
	    if (strlen(alias->value) > value_width) {
		value_width = strlen(alias->value);
	    }
	}
	g_string_sprintfa(interp->header, "%-*s %-*s",
			  name_width, "ALIAS NAME",
			  value_width, "ALIAS VALUE");
	for (elem = interp->alias_list; elem; elem = g_slist_next(elem)) {
	    alias = (scli_alias_t *) elem->data;
	    g_string_sprintfa(interp->result, "%-*s %-*s\n",
			      name_width, alias->name,
			      value_width, alias->value);
	}
    }

    return SCLI_OK;
}



static int
scli_interp_eval(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_interp_t *slave = NULL;
    
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc < 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    for (elem = interp->slave_list; elem; elem = g_slist_next(elem)) {
	slave = (scli_interp_t *) elem->data;
	if (strcmp(slave->name, argv[0]) == 0) {
	    break;
	}
    }

    if (! elem || !slave) {
	return SCLI_ERROR;
    }

    return scli_eval_argc_argv(slave, argc-1, argv+1);
}



static int
create_scli_interp(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_interp_t *slave = NULL;
    scli_cmd_t *cmd;
    
    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    for (elem = interp->slave_list; elem; elem = g_slist_next(elem)) {
	slave = (scli_interp_t *) elem->data;
	if (strcmp(slave->name, argv[1]) == 0) {
	    g_string_sprintfa(interp->result,
			      "interp `%s' already exists", argv[1]);
	    return SCLI_ERROR;
	}
    }

    slave = scli_interp_create(argv[1]);
    scli_interp_init(slave);
    if (scli_interp_proto(interp)) {
	slave->flags |= SCLI_INTERP_FLAG_PROTO;
    }
    slave->master = interp;
    interp->slave_list = g_slist_append(interp->slave_list, slave);

    cmd = g_new0(scli_cmd_t, 1);
    cmd->path = g_strdup(argv[1]);
    cmd->options = "command arg1 arg2 ...";
    cmd->flags = SCLI_CMD_FLAG_DYNAMIC;
    cmd->desc = g_strdup_printf("The `%s' command is used to invoke commands\n"
				"in the scli interpreter named %s.",
				argv[1], argv[1]);
    cmd->func = scli_interp_eval;
    scli_create_command(interp, cmd);

    return SCLI_OK;
}



static int
delete_scli_interp(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_interp_t *slave = NULL;
    regex_t _regex_name, *regex_name = NULL;
    
    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    regex_name = &_regex_name;
    if (regcomp(regex_name, argv[1], interp->regex_flags) != 0) {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_REGEXP;
    }
    
 again:
    for (elem = interp->slave_list; elem; elem = g_slist_next(elem)) {
	slave = (scli_interp_t *) elem->data;
	if (regexec(regex_name, slave->name, (size_t) 0, NULL, 0) == 0) {
	    interp->slave_list = g_slist_remove(interp->slave_list,
						elem->data);
	    scli_interp_delete(slave);
	    goto again;
	}
    }

    if (regex_name) regfree(regex_name);
    
    return SCLI_OK;
}



static int
show_scli_info(scli_interp_t *interp, int argc, char **argv)
{
    int const indent = 18;
    int rows, cols, c;
    GNetSnmpEnum const *dft;
    char const *label;
    char const *fmt;
    GSList *elem;

    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    g_string_sprintfa(interp->result, "%-*s %s\n", indent, "Version:",
		      VERSION);
    
    g_string_sprintfa(interp->result, "%-*s %lu\n", indent, "Epoch:",
		      interp->epoch);

    fmt = "scli";
    if (scli_interp_xml(interp)) {
	fmt = interp->flags & SCLI_INTERP_FLAG_FMT ? "fxml" : "xml";
    }
    g_string_sprintfa(interp->result, "%-*s %s\n", indent, "Format:", fmt);

    g_string_sprintfa(interp->result, "%-*s %s\n", indent, "Interactive:",
		      scli_interp_interactive(interp) ? "true" : "false");

    g_string_sprintfa(interp->result, "%-*s %d seconds\n", indent, "Delay:",
		      interp->delay / 1000);

    g_string_sprintfa(interp->result, "%-*s ", indent, "Regex:");
    for (dft = scli_regex_table, c = 0; dft && dft->label; dft++) {
	if (interp->regex_flags & dft->number) {
	    g_string_sprintfa(interp->result, "%s%s", c ? "|" : "", dft->label);
	    c++;
	}
    }
    g_string_append(interp->result, "\n");

    g_string_sprintfa(interp->result, "%-*s ", indent, "Debugging:");
    for (dft = gnet_snmp_enum_debug_table, c = 0; dft && dft->label; dft++) {
	if (gnet_snmp_debug_flags & dft->number) {
	    g_string_sprintfa(interp->result, "%s%s", c ? "|" : "", dft->label);
	    c++;
	}
    }
    g_string_append(interp->result, "\n");

    scli_get_screen(&rows, &cols);
    g_string_sprintfa(interp->result, "%-*s %d\n", indent,
		      "Rows:", rows);
    g_string_sprintfa(interp->result, "%-*s %d\n", indent,
		      "Columns:", cols);
    g_string_sprintfa(interp->result, "%-*s %s\n", indent,
		      "Pager:", interp->pager ? interp->pager : "scli");

    if (interp->peer) {
        GURI *uri;
	uri = gnet_snmp_get_uri(interp->peer);
	if (uri) {
	    gchar *name = gnet_uri_get_string(uri);
	    g_string_sprintfa(interp->result, "%-*s %s\n",
			      indent, "URI:", name);
	    g_free(name);
	}
	g_string_sprintfa(interp->result, "%-*s %u\n", indent,
			  "Retries:", gnet_snmp_get_retries(interp->peer));
	g_string_sprintfa(interp->result, "%-*s %u ms\n", indent,
			  "Timeout:", gnet_snmp_get_timeout(interp->peer));
	g_string_sprintfa(interp->result, "%-*s %s\n", indent,
			  "Community:", gnet_snmp_get_community(interp->peer));
	label = gnet_snmp_enum_get_label(gnet_snmp_enum_version_table,
				 (gint32) gnet_snmp_get_version(interp->peer));
	if (label) {
	    g_string_sprintfa(interp->result, "%-*s %s\n", indent,
			      "Protocol:", label);
	} else {
	    g_string_sprintfa(interp->result, "%-*s %d\n", indent,
			      "Protocol:", gnet_snmp_get_version(interp->peer));
	}
    }

    if (interp->slave_list) {
	g_string_sprintfa(interp->result, "%-*s", indent, "Interpreters:");
	for (elem = interp->slave_list; elem; elem = g_slist_next(elem)) {
	    scli_interp_t *slave = (scli_interp_t *) elem->data;
	    g_string_sprintfa(interp->result, " %s", slave->name);
	}
	g_string_sprintfa(interp->result, "\n");
    }

    return SCLI_OK;
}



static void
fmt_alarm_info(GString *s, scli_alarm_t *alarm)
{	
    struct tm *tm;

    tm = localtime(&alarm->detected);
    g_string_sprintfa(s, "%04d-%02d-%02d %02d:%02d:%02d ",
		      tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
		      tm->tm_hour, tm->tm_min, tm->tm_sec);
    g_string_sprintfa(s, "%s\n", alarm->desc);
}



static int
show_scli_alarm_info(scli_interp_t *interp, int argc, char **argv)
{
    GSList *elem;
    scli_alarm_t *alarm;
    
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (interp->alarm_list) {
	g_string_sprintfa(interp->header, "DETECTED            VERIFIED            DESCRIPTION");
	for (elem = interp->alarm_list; elem; elem = g_slist_next(elem)) {
	    alarm = (scli_alarm_t *) elem->data;
	    fmt_alarm_info(interp->result, alarm);
	}
    }

    return SCLI_OK;
}



static int
set_scli_regex(scli_interp_t *interp, int argc, char **argv)
{
    int flags = 0;
    GNetSnmpEnum const *dft;
    regex_t _regex_flags, *regex_flags = NULL;

    if (argc < 1 || argc > 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (argc == 1) {
	interp->regex_flags = 0;
	return SCLI_OK;
    }

    regex_flags = &_regex_flags;
    if (regcomp(regex_flags, argv[1], interp->regex_flags) != 0) {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_REGEXP;
    }

    for (dft = scli_regex_table; dft && dft->label; dft++) {
	if (regexec(regex_flags, dft->label, (size_t) 0, NULL, 0) == 0) {
	    flags |= dft->number;
	}
    }
    interp->regex_flags = flags;
    
    if (regex_flags) regfree(regex_flags);

    return SCLI_OK;
}



static int
set_scli_debugging(scli_interp_t *interp, int argc, char **argv)
{
    GNetSnmpDebugFlags flags = 0;
    GNetSnmpEnum const *dft;
    regex_t _regex_flags, *regex_flags = NULL;
    
    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc < 1 || argc > 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (argc == 1) {
	gnet_snmp_debug_flags = 0;
	return SCLI_OK;
    }
    
    regex_flags = &_regex_flags;
    if (regcomp(regex_flags, argv[1], interp->regex_flags) != 0) {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_REGEXP;
    }
    
    for (dft = gnet_snmp_enum_debug_table; dft && dft->label; dft++) {
	if (regexec(regex_flags, dft->label, (size_t) 0, NULL, 0) == 0) {
	    flags |= dft->number;
	}
    }
    gnet_snmp_debug_flags = flags;

    if (regex_flags) regfree(regex_flags);

    return SCLI_OK;
}



static int
set_scli_pager(scli_interp_t *interp, int argc, char **argv)
{
    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (scli_set_pager(interp, argv[1]) < 0) {
	g_string_sprintfa(interp->result,
			  "pager `%s' contains unsafe characters",
			  argv[1]);
	return SCLI_ERROR;	
    }

    return SCLI_OK;
}



static int
set_scli_retries(scli_interp_t *interp, int argc, char **argv)
{
    char *end;
    guint32 retries; 

    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    retries = strtoul(argv[1], &end, 0);
    if (*end || retries < 0) {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_NUMBER;
    }

    if (scli_set_retries(interp, retries) < 0) {
	g_string_sprintfa(interp->result,
			  "illegal number of retries `%u'", retries);
	return SCLI_ERROR;
    }

    return SCLI_OK;
}



static int
set_scli_timeout(scli_interp_t *interp, int argc, char **argv)
{
    char *end;
    guint32 timeout; 

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    timeout = strtoul(argv[1], &end, 0);
    if (*end || timeout < 0) {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_NUMBER;
    }

    if (scli_set_timeout(interp, timeout) < 0) {
	g_string_sprintfa(interp->result,
			  "illegal timeout `%u'", timeout);
	return SCLI_ERROR;
    }

    return SCLI_OK;
}



static int
set_scli_format(scli_interp_t *interp, int argc, char **argv)
{
    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (strcmp(argv[1], "xml") == 0) {
	interp->flags |= SCLI_INTERP_FLAG_XML;
	interp->flags &= ~SCLI_INTERP_FLAG_FMT;
    } else if (strcmp(argv[1], "fxml") == 0) {
	interp->flags |= SCLI_INTERP_FLAG_XML;
	interp->flags |= SCLI_INTERP_FLAG_FMT;
    } else if (strcmp(argv[1], "scli") == 0) {
	interp->flags &= ~SCLI_INTERP_FLAG_XML;
	interp->flags &= ~SCLI_INTERP_FLAG_FMT;
    } else {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_VALUE;	
    }

    return SCLI_OK;
}


static int
set_scli_mode(scli_interp_t *interp, int argc, char **argv)
{
    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (strcmp(argv[1], "protocol") == 0) {
	interp->flags |= SCLI_INTERP_FLAG_PROTO;
    } else if (strcmp(argv[1], "normal") == 0) {
	interp->flags &= ~SCLI_INTERP_FLAG_PROTO;
    } else {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_VALUE;	
    }

    return SCLI_OK;
}



static void
fmt_walk(GString *s, GNetSnmpVarBind *vb)
{
    int i, printable;
    const char *t;
    
    for (i = 0; i < vb->oid_len; i++) {
	g_string_sprintfa(s, "%s%d",
			  i ? "." : "", vb->oid[i]);
    }
    t = gnet_snmp_enum_get_label(gnet_snmp_enum_type_table, vb->type);
    g_string_append(s, " = ");
    if (t) {
        g_string_sprintfa(s, "[%s] ", t);
    }
    switch (vb->type) {
    case GNET_SNMP_VARBIND_TYPE_INTEGER32:
	g_string_sprintfa(s, "%d", vb->value.i32);
	break;
    case GNET_SNMP_VARBIND_TYPE_UNSIGNED32:
    case GNET_SNMP_VARBIND_TYPE_COUNTER32:
    case GNET_SNMP_VARBIND_TYPE_TIMETICKS:
	g_string_sprintfa(s, "%u", vb->value.ui32);
	break;
    case GNET_SNMP_VARBIND_TYPE_COUNTER64:
	g_string_sprintfa(s, "%llu", vb->value.ui64);
	break;
    case GNET_SNMP_VARBIND_TYPE_IPADDRESS:
	if (vb->value_len == 4) {
	    g_string_sprintfa(s, "%d.%d.%d.%d",
			      vb->value.ui8v[0],
			      vb->value.ui8v[1],
			      vb->value.ui8v[2],
			      vb->value.ui8v[3]);
	}
	break;
    case GNET_SNMP_VARBIND_TYPE_OPAQUE:
	for (i = 0; i < vb->value_len; i++) {
	    g_string_sprintfa(s, "%s%02x", i ? ":" : "", vb->value.ui8v[i]);
	}
	break;
    case GNET_SNMP_VARBIND_TYPE_OCTETSTRING:
	for (i = 0, printable = 1; i < vb->value_len; i++) {
	    printable = printable && isprint((int) vb->value.ui8v[i]);
	}
	if (printable && vb->value_len) {
	    g_string_append(s, " \"");
	    for (i = 0; i < vb->value_len; i++) {
		g_string_append_c(s, vb->value.ui8v[i]);
	    }
	    g_string_append(s, "\"");
	} else {
	    for (i = 0; i < vb->value_len; i++) {
		g_string_sprintfa(s, "%s%02x", i ? ":" : "", vb->value.ui8v[i]);
	    }
	}
	break;
    case GNET_SNMP_VARBIND_TYPE_OBJECTID:
	for (i = 0; i < vb->value_len; i++) {
	    g_string_sprintfa(s, "%s%d",
			      i ? "." : "", vb->value.ui32v[i]);
	}
	break;
    default:
	break;
    }
    g_string_append_c(s, '\n');
}



static int
cmd_scli_walk(scli_interp_t *interp, int argc, char **argv)
{
    GList *in = NULL, *out = NULL;
    guint32 base[GNET_SNMP_SIZE_OBJECTID];
    GList *elem;
    int i, len;
    char *p;
    GNetSnmpVarBind *vb;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc < 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    for (i = 1; i < argc; i++) {
	for (len = 0, p = argv[i]; p && *p && len < GNET_SNMP_SIZE_OBJECTID; len++) {
	    base[len] = strtoul(p, &p, 0);
	    if (p && *p) {
		if (*p == '.') {
		    p++;
		} else {
	            g_list_foreach(in, (GFunc) gnet_snmp_varbind_delete, NULL);
		    g_list_free(in);
		    g_string_assign(interp->result, argv[i]);
		    return SCLI_SYNTAX_VALUE;
		}
	    }
	}
	if ((p && *p) || len < 2 || base[0] > 2 || base[1] > 40) {
	    g_list_foreach(in, (GFunc) gnet_snmp_varbind_delete, NULL);
	    g_list_free(in);
	    g_string_assign(interp->result, argv[i]);
	    return SCLI_SYNTAX_VALUE;
	}
	vb = gnet_snmp_varbind_new(base, len,
				   GNET_SNMP_VARBIND_TYPE_NULL, NULL, 0);
	in = g_list_append(in, vb);
    }

    out = gnet_snmp_sync_walk(interp->peer, in);
    g_list_foreach(in, (GFunc) gnet_snmp_varbind_delete, NULL);
    g_list_free(in);
    if (out) {
	for (elem = out; elem; elem = g_list_next(elem)) {
	    fmt_walk(interp->result, (GNetSnmpVarBind *) elem->data);
	}
    }
    g_list_foreach(out, (GFunc) gnet_snmp_varbind_delete, NULL);
    g_list_free(out);
            
    return SCLI_OK;
}



struct scan_elem {
    GNetSnmpTDomain tdomain;
    GInetAddr *taddress;	/* xxx - not very efficient */
    int descr_len;
    char *descr;
};

static void
scan_elem_free(gpointer data, gpointer user_data)
{
    struct scan_elem *p = data;
    if (p) {
	if (p->taddress) gnet_inetaddr_delete(p->taddress);
	if (p->descr) g_free(p->descr);
	g_free(p);
    }
}

struct scanmagic {
    GMainLoop *loop;
    guint count;
    GList *scan_list;
};

static void
append_flat_string(GString *s, int len, char *string)
{
    int i;
    char last = 0;
    
    /* Remove leading and trailing white-space characters first. */

    while (len && isspace((int) string[0])) {
	string++, len--;
    }
    while (len && isspace((int) string[len-1])) {
	len--;
    }

    /* Turn \r and \n into white space characters (but only if there
       are no other white space characters. Display non-printable
       characters as special sequences such as \245. */

    for (i = 0; i < len; i++) {
	if (string[i] == '\r' || string[i] == '\n') {
  	    if (isspace(last) || isspace(string[i+1])) {
		continue;
	    } else {
	        g_string_append_c(s, ' ');
	    }
	} else {
	    if (isprint((int) string[i])) {
		g_string_append_c(s, string[i]);
	    } else {
		g_string_sprintfa(s, "\\%o", string[i]);
	    }
	}
    }
}

static gint
scan_cmp(gconstpointer a, gconstpointer b)
{
    struct scan_elem *n_a = (struct scan_elem *) a;
    struct scan_elem *n_b = (struct scan_elem *) b;
    gchar ip_a[GNET_INETADDR_MAX_LEN];
    gchar ip_b[GNET_INETADDR_MAX_LEN];
    gint len_a, len_b;

    len_a = gnet_inetaddr_get_length(n_a->taddress);
    len_b = gnet_inetaddr_get_length(n_b->taddress);

    gnet_inetaddr_get_bytes(n_a->taddress, ip_a);
    gnet_inetaddr_get_bytes(n_b->taddress, ip_b);

    if (len_a == len_b) {
	return memcmp(ip_a, ip_b, len_a);
    }

    return (len_a < len_b) ? -1 : 1;
}

static void
scan_print(gpointer data, gpointer user_data)
{
    struct scan_elem *p = (struct scan_elem *) data;
    scli_interp_t *interp = (scli_interp_t *) user_data;
    gchar *name = NULL;

    name = gnet_inetaddr_get_canonical_name(p->taddress);
    g_string_sprintfa(interp->result, "%-16s ", name);
    if (p->descr_len && p->descr) {
	append_flat_string(interp->result, p->descr_len, p->descr);
    }
    g_string_append(interp->result, "\n");
    g_free(name);
}

static gboolean
scan_one_done(GNetSnmp *s, GNetSnmpPdu *pdu, GList *vbl, gpointer magic)
{
    struct scanmagic *sm = (struct scanmagic *) magic;
    GNetSnmpVarBind *vb = NULL;
    
    sm->count--;
    if (sm->count <= 0) g_main_quit(sm->loop);
    if (s->error_status == GNET_SNMP_PDU_ERR_NOERROR && vbl) {
	struct scan_elem *p;
	p = g_new0(struct scan_elem, 1);
	p->tdomain = s->tdomain;
	p->taddress = gnet_inetaddr_clone(s->taddress);
	vb = vbl->data;
	if (vb->type == GNET_SNMP_VARBIND_TYPE_OCTETSTRING) {
	    p->descr_len = vb->value_len;
	    p->descr = vb->value.ui8v;
	}
	sm->scan_list = g_list_insert_sorted(sm->scan_list, p, scan_cmp);
    }
    gnet_snmp_delete(s);
    return TRUE;
}

static void
scan_one_time(GNetSnmp *s, void *magic)
{
    struct scanmagic *sm = (struct scanmagic *) magic;
    sm->count--;
    if (sm->count <= 0) g_main_quit(sm->loop);
    gnet_snmp_delete(s);
}

static void
scan_one(char *host, int port, char *community, gpointer magic)
{
    GInetAddr *taddress;
    GNetSnmpTDomain tdomain;
    GNetSnmp *s;
    static GList *in = NULL;
    static guint32 base[] = {1, 3, 6, 1, 2, 1, 1, 1, 0};
    GNetSnmpVarBind *vb;

    taddress = gnet_inetaddr_new(host, port); /* xxx where to release? */
    if (! taddress) {
        g_warning("failed to convert address");
	return;
    }
    tdomain = gnet_inetaddr_is_ipv6(taddress)
	    ? GNET_SNMP_TDOMAIN_UDP_IPV6 : GNET_SNMP_TDOMAIN_UDP_IPV4;
     
    s = gnet_snmp_new();
    gnet_snmp_set_transport(s, tdomain, taddress);
    gnet_snmp_set_community(s, community ? community : "public");
    gnet_snmp_set_version(s, GNET_SNMP_V1);
    if (! s) {
	 g_warning("failed to create session: %s\n", host);
	 return;
    }

    if (! in) {
	 vb = gnet_snmp_varbind_new(base, G_N_ELEMENTS(base),
				    GNET_SNMP_VARBIND_TYPE_NULL, NULL, 0);
	 in = g_list_append(in, vb);
    }

    s->done_callback = scan_one_done;
    s->time_callback = scan_one_time;
    s->magic = magic;

    if (gnet_snmp_async_get(s, in) == NULL) {
        g_warning("failed to send async request");
	return;
    }
    ((struct scanmagic *) magic)->count++;
}


static int
cmd_scli_scan(scli_interp_t *interp, int argc, char **argv)
{
    int prefix;
    GInetAddr *addr = NULL;
    struct scanmagic *magic;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc < 2 || argc > 3) {
	return SCLI_SYNTAX_NUMARGS;
    }

    /* xxx accept port numbers? accept an optional community name? version? */

    /*
     * Check the network argument which should be of the format
     * a.b.c.d/e where the lower case letters are decimal numbers
     * in a suitable range.
     */

    {
	char *p, *end;
	
	p = strchr(argv[1], '/');
	if (! p)  {
	    return SCLI_SYNTAX_VALUE;
	}
	*p = '\0', p++;
	prefix = strtol(p, &end, 0);
	if (*end || prefix < 0 || prefix > 31) {
	    g_string_assign(interp->result, p);
	    return SCLI_SYNTAX_VALUE;
	}
	
	addr = gnet_inetaddr_new(argv[1], 161);
	if (! addr) {
	    g_string_assign(interp->result, argv[1]);
	    return SCLI_SYNTAX_VALUE;
	}
    }

    if (scli_interp_dry(interp)) {
	gnet_inetaddr_delete(addr);
	return SCLI_OK;
    }

    magic = g_new0(struct scanmagic, 1);
    magic->loop = g_main_new(TRUE);

    /* put the sending loop also into the main loop so that we already
     * process responses while we are still sending requests - in fact,
     * we should do some nice traffic shaping like scotty did */
    
    {
	 guint32 ip, start, end;
	 struct in_addr ipaddr;
	 char buf[INET_ADDRSTRLEN];
	 gchar bin_addr[GNET_INETADDR_MAX_LEN];
	 gchar bin_addr_len = 0;

	 bin_addr_len = gnet_inetaddr_get_length(addr);
	 gnet_inetaddr_get_bytes(addr, bin_addr);

	 /* xxx this ipv4 specific and should be fixed */

	 start = (ntohl(* (guint32 *) bin_addr) >> (32-prefix)) << (32-prefix);
	 end = start + (1 << (32-prefix));
	 for (ip = start + 1; ip < end; ip++) {
	      ipaddr.s_addr = htonl(ip);
	      inet_ntop(AF_INET, &ipaddr, buf, sizeof(buf));
	      scan_one(buf, 161, argc == 3 ? argv[2] : "public", magic);
	 }
    }

    while(g_main_is_running(magic->loop)) {
	g_main_run(magic->loop);
    }
    g_main_destroy(magic->loop);
    g_list_foreach(magic->scan_list, scan_print, (gpointer) interp);
    g_list_foreach(magic->scan_list, scan_elem_free, NULL);
    g_free(magic);

    gnet_inetaddr_delete(addr);

    if (interp->result->len) {
	g_string_sprintfa(interp->header,
			  "ADDRESS          DESCRIPTION");
    }

    return SCLI_OK;
}



void
scli_init_scli_mode(scli_interp_t *interp)
{
    static scli_cmd_t cmds[] = {

	{ "open", "<nodename> [<community>]",
	  "The `open' command establishes an association to a remote SNMP\n"
	  "agent. The <nodename> argument is the DNS name or the IP\n"
	  "address of the remote node. Scli will try to talk to the SNMP\n"
	  "agent on this node by using the default port number (usually 161)\n"
	  "and the default transport mapping (usually SNMP over UDP).\n"
	  "The optional <community> argument is the community string\n"
	  "needed to communicate with the remote SNMP agent. The default\n"
	  "community string is \"public\". Opening an association while\n"
	  "an association is already established is not considered an\n"
	  "error. The existing established association will be closed\n"
	  "automatically before an attempt to create a new association\n"
	  "is started.",
	  SCLI_CMD_FLAG_XML,
	  "", NULL,
	  scli_cmd_open },

	{ "close", NULL,
	  "The `close' command closes an established association to a remote\n"
	  "SNMP agent. Invoking the close command when no association is\n"
	  "established is not considered an error and will do just nothing.",
	  SCLI_CMD_FLAG_XML,
	  "", NULL,
	  cmd_scli_close },

	{ "run scli walk", "<oid> [<oid> ...]",
	  "The `run scli walk' command is a debugging utility which simply\n"
	  "performs a MIB walk. Note that scli does not have general MIB\n"
	  "knowledge and hence the output requires some post-processing.",
	  SCLI_CMD_FLAG_NEED_PEER,
	  NULL, NULL,
	  cmd_scli_walk },

	{ "run scli scan", "<network> [community]",
	  "The `run scli scan' command is a utility which scans an IPv4\n"
	  "address space identified by the <network> argument. The <network>\n"
	  "must be specified in the format <ipv4address>/<prefix>.\n"
	  "The optional <community> argument is the community string\n"
	  "needed to communicate with the remote SNMP agent. The default\n"
	  "community string is \"public\".",
	  0,
	  NULL, NULL,
	  cmd_scli_scan },

	{ "create scli plugin", "<module>",
	  "The `create scli plugin' command dynamically loads an scli mode\n"
	  "into a running scli process. This can be used to dynamically\n"
	  "extend scli with modules coming from other sources. Dynamic\n"
	  "loadable modules also simplify the development and management\n"
	  "of site-specific modules.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  cmd_scli_load },

	{ "delete scli plugin", "<module>",
	  "The `delete scli plugin' command removes a previously loaded\n"
	  "modules from a running scli process.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  cmd_scli_unload },

	{ "exit", NULL,
	  "The `exit' command terminates the scli interpreter. An end of file\n"
	  "in the standard input stream will also terminate the the scli\n"
	  "interpreter.",
	  SCLI_CMD_FLAG_XML,
	  "", NULL,
	  cmd_scli_exit },

	{ "help", NULL,
	  "The `help' command displays some help information including a list\n"
	  "of all top-level scli commands.",
	  0,
	  NULL, NULL,
	  help },

	{ "history", NULL,
	  "The `history' command displays the scli command history list with\n"
	  "line numbers.",
	  0,
	  NULL, NULL,
	  history },

        { "create scli alias", "<name> <value>",
	  "The `create scli alias' command creates the alias <name> for the\n"
	  "scli command (fragment) <value>. If the alias <name> already\n"
	  "exists, then the new <value> will be assigned to the existing\n"
	  "alias.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  create_scli_alias },
	
	{ "delete scli alias", "<regexp>",
	  "The `delete scli alias' command removes previously defined\n"
	  "aliases from the scli interpreter. The regular expression\n"
	  "<regexp> is matched against all alias names in order to\n"
	  "select the aliases that are deleted.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  delete_scli_alias },
	
        { "create scli interp", "<name>",
	  "The `create scli interp' command creates a new internal scli\n"
	  "interpreter with the name <name>.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  create_scli_interp },
	
	{ "delete scli interp", "<regexp>",
	  "The `delete scli interp' command deletes previously defined\n"
	  "scli interpreters from the main scli interpreter. The regular\n"
	  "expression <regexp> is matched against all alias names in order\n"
	  "to select the interpreter(s) to be removed.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  delete_scli_interp },
	
	{ "set scli regex", "[<regexp>]",
	  "The `set scli regex' command controls how scli matches regular\n"
	  "expressions. The optional regular expression <regexp> is\n"
	  "matched against the regular expression options. A successful\n"
	  "match turns a regular expression option on while an unsuccessful\n"
          "match turns a regular expression option off. Invoking the command\n"
	  "without the <regexp> argument will turn all regular expression\n"
	  "options off. The currently defined regular expression options\n"
	  "are \"extended\" for POSIX extended regular expressions and\n"
	  "\"case-insensitive\" for case insensitive matches.",
	  SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_NORECURSE,
	  "", NULL,
	  set_scli_regex },
	
	{ "set scli debugging", "[<regexp>]",
	  "The `set scli debugging' command sets the debugging level of\n"
	  "the SNMP engine. The optional regular expression <regexp> is\n"
	  "matched against the debugging levels. A successful match turns\n"
	  "a debugging level on while an unsuccessful match turns a\n"
	  "debugging level off. Invoking the command without the <regexp>\n"
	  "argument will turn all debugging levels off. The currently\n"
	  "defined debugging levels are \"session\" for the SNMP session\n"
	  "layer, \"request\" for the SNMP request handling layer, \n"
	  "\"transport\" for the SNMP transport layer, \"packet\" for\n"
	  "the SNMP packet layer, and \"asn1\" for the ASN.1 coding layer.",
	  SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_NORECURSE,
	  "", NULL,
	  set_scli_debugging },
	
	{ "set scli pager", "<pager>",
	  "The `set scli pager' command defines the shell command which is\n"
	  "used as a pager if the output produced by an scli command does\n"
	  "not fit on a single screen. The output is passed to the <pager>\n"
	  "shell command via its standard input stream.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  set_scli_pager },

	{ "set scli retries", "<retries>",
	  "The `set scli retries' command defines the number of SNMP\n"
	  "request retries before giving up requesting a certain object.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  set_scli_retries },

	{ "set scli timeout", "<milliseconds>",
	  "The `set scli timeout' command defines the number milliseconds\n"
	  "between subsequent SNMP request retries.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  set_scli_timeout },
	
	{ "set scli format", "<fmt>",
	  "The `set scli format' command defines the output format used by\n"
	  "subsequent scli commands. The currently supported formats are\n"
	  "\"scli\" and \"xml\". The \"scli\" format is the default output\n"
	  "format and described in this documentation. The \"xml\" output\n"
	  "format is experimental and therefore not described here.",
	  SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_NORECURSE,
	  "", NULL,
	  set_scli_format },
	
	{ "set scli mode", "<mode>",
	  "The `set scli mode' command defines the scli mode used by\n"
	  "subsequent scli commands. Setting the mode to \"protocol\"\n"
	  "will force scli to work in protocol mode. Setting the mode to\n"
	  "\"normal\" causes scli to work in normal mode where certain\n"
	  "status messages are suppressed.",
	  SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_NORECURSE,
	  "", NULL,
	  set_scli_mode },
	
	{ "show scli info", NULL,
	  "The `show scli info' command displays the current status of the\n"
	  "scli interpreter.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  show_scli_info },

	{ "show scli command info", "[<regex]",
	  "The `show scli command info' command displays summary information\n"
	  "about scli commands. The optional regular expression <regexp> is\n"
	  "matched against the command names to select the scli commands.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  show_scli_command_info },

	{ "show scli command details", "[<regex]",
	  "The `show scli command details' command displays detailed information\n"
	  "about scli commands. The optional regular expression <regexp> is\n"
	  "matched against the command names to select the scli commands.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  show_scli_command_details },

	{ "show scli command tree", NULL,
	  "The `show scli command tree' command displays the scli command\n"
	  "tree. The full command syntax is displayed for each leaf node.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  show_scli_command_tree },

	{ "show scli aliases", NULL,
	  "The `show scli aliases' command lists all scli command aliases.\n"
	  "The first column in the generated table lists the aliase names\n"
	  "while the second column shows the alias values.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  show_scli_aliases },

	{ "show scli modes", "[<regex>]",
	  "The `show scli modes' command shows information about the scli\n"
	  "modes. An scli mode is a logical grouping of related commands\n"
	  "(e.g., all commands that deal with printers). The optional\n"
	  "regular expression <regex> can be use to select a subset of\n"
	  "the available scli modes.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  show_scli_modes },

	{ "show scli schema", "[<regex>]",
	  "The `show scli schema' command produces xml schema definitions for\n"
	  "the selected scli modes. An scli mode is a logical grouping of\n"
	  "related commands (e.g., all commands that deal with printers).\n"
	  "The optional regular expression <regex> can be use to select a\n"
	  "subset of the available scli modes.",
	  SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  show_scli_schema },

	{ "show scli alarm info", NULL,
	  "The `show scli alarm info' command displays summary information\n"
	  "about all known alarms.",
	  0,
	  NULL, NULL,
	  show_scli_alarm_info },

	{ NULL, NULL, NULL, 0, NULL, NULL, NULL }
    };
    
    static scli_mode_t scli_mode = {
	"scli",
	"The scli mode provides commands that can be used to display and\n"
	"manipulate the internal state of the scli interpreter.",
	cmds
    };

    scli_register_mode(interp, &scli_mode);
}