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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This module implements the routine to parse the configuration file.
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <alloca.h>
#include <limits.h>
#include <sys/utsname.h>
#include <sys/systeminfo.h>
#include <sys/types.h>
#include <libintl.h>
#include <syslog.h>
#include <locale.h>
#include <picl.h>
#include <picltree.h>
#include "picld_pluginutil.h"
#include "picld_pluginutil_impl.h"
/* error codes returned from syntax checking */
#define EC_SYNTAX_OK 0
#define EC_INSUFFICIENT_TOKEN 1
#define EC_SYNTAX_ERR 2
#define EC_UNSUPPORTED 3
#define EC_PATH_ERR 4
#define EC_NODE_MISMATCH 5
#define EC_FAILURE 6
#define EC_PICL_ERR 7
#define EC_TABLE_MISMATCH 8
#define EC_ROW_MISMATCH 9
#define EC_ROW_EMPTY 10
/*
* Error message texts
*/
static char *err_msg[] = {
"%s: Syntax OK", /* 0 */
"%s::%s[line %d]: Insufficient token\n", /* 1 */
"%s::%s[line %d]: Syntax error\n", /* 2 */
"%s::%s[line %d]: Unsupported or missing version\n", /* 3 */
"%s::%s[line %d]: Illegal use of nodepath or namepath\n", /* 4 */
"%s::%s[line %d]: Node and endnode mismatch\n", /* 5 */
"%s::%s[line %d]: General system failure\n", /* 6 */
"%s: PICL error code %d\n", /* 7 */
"%s::%s[line %d]: Table and endtable mismatch\n", /* 8 */
"%s::%s[line %d]: Row and endrow mismatch\n", /* 9 */
"%s::%s[line %d]: Row has no entries \n" /* 10 */
};
/* token per directive */
#define TOK_CLASSPATH 0
#define TOK_NAMEPATH 1
#define TOK_NODE 2
#define TOK_ENDNODE 3
#define TOK_PROP 4
#define TOK_REFPROP 5
#define TOK_VERSION 6
#define TOK_REFNODE 7
#define TOK_VERBOSE 8
#define TOK_TABLE 9
#define TOK_ENDTABLE 10
#define TOK_ROW 11
#define TOK_ENDROW 12
static const char *tokens[] = {
"_class", /* _CLASS:<classpath> */
"name", /* NAME:<namepath> */
"node", /* NODE <name> <class> */
"endnode", /* ENDNODE */
"prop", /* PROP <name> <type> <access_mode> <size> <value> */
"refprop", /* REFPROP <prop> <destnode> */
"version", /* VERSION <version_number> */
"refnode", /* REFNODE <node> <class> WITH <destnode> */
"verbose", /* VERBOSE <level> */
"table", /* TABLE <table_prop_name> */
"endtable", /* ENDTABLE */
"row", /* ROW */
"endrow" /* ENDROW */
};
#define BUF_SIZE_MAX 1024
/*
* print error message
*/
/*VARARGS2*/
static void
verbose_log(int pri, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsyslog(pri, fmt, ap);
va_end(ap);
}
/*
* Undo the commands which have created valid node/prop handle
* The undo order is from last command to the first command.
*/
static void
undo_commands(cmdbuf_t *cmds, int last_cmd_index)
{
int i;
command_t *com = cmds->commands;
for (i = last_cmd_index; i >= 0; i--) {
switch (com[i].type) {
case TOK_NODE:
if (com[i].nodecmd_nodeh == 0)
break;
(void) ptree_delete_node(com[i].nodecmd_nodeh);
(void) ptree_destroy_node(com[i].nodecmd_nodeh);
break;
case TOK_REFNODE:
if (com[i].refnodecmd_nodeh == 0)
break;
(void) ptree_delete_node(com[i].refnodecmd_nodeh);
(void) ptree_destroy_node(com[i].refnodecmd_nodeh);
break;
case TOK_PROP:
if (com[i].propcmd_proph == 0)
break;
(void) ptree_delete_prop(com[i].propcmd_proph);
(void) ptree_destroy_prop(com[i].propcmd_proph);
break;
case TOK_REFPROP:
if (com[i].refpropcmd_proph == 0)
break;
(void) ptree_delete_prop(com[i].refpropcmd_proph);
(void) ptree_destroy_prop(com[i].refpropcmd_proph);
break;
case TOK_TABLE:
if ((com[i].tablecmd_tblh == 0) ||
(com[i].tablecmd_newtbl == 0))
break;
(void) ptree_delete_prop(com[i].tablecmd_tblh);
(void) ptree_destroy_prop(com[i].tablecmd_tblh);
break;
case TOK_ENDTABLE:
/*FALLTHROUGH*/
case TOK_ROW:
/*FALLTHROUGH*/
case TOK_ENDROW:
/*FALLTHROUGH*/
case TOK_NAMEPATH:
/*FALLTHROUGH*/
case TOK_CLASSPATH:
/*FALLTHROUGH*/
case TOK_ENDNODE:
/*FALLTHROUGH*/
case TOK_VERBOSE:
/*FALLTHROUGH*/
default:
break;
}
}
}
/*
* Get the token index from the tokens table
*/
static int
get_token_id(char *t)
{
int i;
for (i = 0; i < sizeof (tokens)/ sizeof (char *); ++i)
if (strcasecmp(tokens[i], t) == 0)
return (i);
return (-1);
}
/*
* Check the version syntax and set the version_no
*
* VERSION <version_num> -- specify the configuration version
*/
static int
parse_version(cmdbuf_t *cmds, char *line)
{
char *tok;
char *vertok;
char *last;
char *endptr;
/* get the VERSION directive */
tok = strtok_r(line, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the version number */
vertok = strtok_r(last, WHITESPACE, &last);
if (vertok == NULL)
return (EC_INSUFFICIENT_TOKEN);
cmds->version_no = (float)strtod(vertok, &endptr);
if (endptr != (vertok + strlen(vertok)))
return (EC_UNSUPPORTED);
if (cmds->version_no > (float)SUPPORTED_VERSION_NUM)
return (EC_UNSUPPORTED);
/* check if more tokens */
tok = strtok_r(last, WHITESPACE, &last);
if (tok != NULL)
return (EC_SYNTAX_ERR);
return (EC_SYNTAX_OK);
}
/*
* free path_cmd_t
*/
static void
free_path(command_t *command)
{
free(command->pathcmd_name);
}
/*
* Check the path syntax
* NAMEPATH:<namepath> -- gives the anchor node
* or
* CLASSPATH:<classpath> -- gives the anchor node
*/
static int
parse_path(char *line, command_t *command)
{
char *tok;
char *pathtok;
char *last;
pathtok = strtok_r(line, WHITESPACE, &last);
if (pathtok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* check if more tokens */
tok = strtok_r(last, WHITESPACE, &last);
if (tok != NULL)
return (EC_SYNTAX_ERR);
command->pathcmd_name = strdup(pathtok);
if (command->pathcmd_name == NULL)
return (EC_FAILURE);
return (EC_SYNTAX_OK);
}
/*
* Process the path command and return PICL node handle
*/
static int
process_path(command_t *command, picl_nodehdl_t *nodeh)
{
int err;
err = ptree_get_node_by_path(command->pathcmd_name, nodeh);
return (err);
}
/*
* free node_cmd_t
*/
static void
free_node(command_t *command)
{
free(command->nodecmd_nodename);
free(command->nodecmd_classname);
}
/*
* Check the NODE syntax
* NODE <name> <class>
*/
static int
parse_node(char *line, command_t *command)
{
char *tok;
char *nametok;
char *classtok;
char *last;
/* get the NODE directive */
tok = strtok_r(line, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get name */
nametok = strtok_r(last, WHITESPACE, &last);
if (nametok == NULL)
return (EC_INSUFFICIENT_TOKEN);
classtok = strtok_r(last, WHITESPACE, &last);
if (classtok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* check if more tokens */
tok = strtok_r(last, WHITESPACE, &last);
if (tok != NULL)
return (EC_SYNTAX_ERR);
command->nodecmd_nodename = strdup(nametok);
command->nodecmd_classname = strdup(classtok);
command->nodecmd_nodeh = 0;
if ((command->nodecmd_nodename == NULL) ||
(command->nodecmd_classname == NULL))
return (EC_FAILURE);
return (EC_SYNTAX_OK);
}
/*
* Process the NODE command and return PICL node handle
*/
static int
process_node(command_t *command, picl_nodehdl_t parh, picl_nodehdl_t *nodeh)
{
int err;
err = ptree_create_and_add_node(parh, command->nodecmd_nodename,
command->nodecmd_classname, nodeh);
if (err == PICL_SUCCESS)
command->nodecmd_nodeh = *nodeh;
return (err);
}
/*
* get the PICL property type
*/
static int
getpicltype(char *type)
{
if (strcasecmp(type, KEYWORD_INT_TYPE) == 0)
return (PICL_PTYPE_INT);
else if (strcasecmp(type, KEYWORD_UINT_TYPE) == 0)
return (PICL_PTYPE_UNSIGNED_INT);
else if (strcasecmp(type, KEYWORD_FLOAT_TYPE) == 0)
return (PICL_PTYPE_FLOAT);
else if (strcasecmp(type, KEYWORD_STRING_TYPE) == 0)
return (PICL_PTYPE_CHARSTRING);
else if (strcasecmp(type, KEYWORD_VOID_TYPE) == 0)
return (PICL_PTYPE_VOID);
else
return (-1);
}
/*
* get the PICL accessmode mode
*/
static int
getpiclmode(char *mode)
{
if (strcasecmp(mode, KEYWORD_READ_MODE) == 0)
return (PICL_READ);
else if (strcasecmp(mode, KEYWORD_WRITE_MODE) == 0)
return (PICL_WRITE);
else if (strcasecmp(mode, KEYWORD_READWRITE_MODE) == 0)
return (PICL_READ|PICL_WRITE);
else
return (-1);
}
/*
* check if the size and value are valid given by the prop type
*/
static int
validate_size_and_cvt_val(void *outbuf, size_t size, int type, char *val)
{
int64_t llval;
int32_t intval;
int16_t sval;
int8_t cval;
uint64_t ullval;
uint32_t uintval;
uint16_t usval;
uint8_t ucval;
float fval;
double dval;
char *endptr;
switch (type) {
case PICL_PTYPE_CHARSTRING:
break;
case PICL_PTYPE_INT:
switch (size) {
case sizeof (int64_t):
llval = strtoll(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &llval, size);
break;
case sizeof (int32_t):
intval = strtol(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &intval, size);
break;
case sizeof (int16_t):
sval = (int16_t)strtol(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &sval, size);
break;
case sizeof (int8_t):
cval = (int8_t)strtol(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &cval, size);
break;
default: /* invalid size */
return (EC_SYNTAX_ERR);
}
break;
case PICL_PTYPE_UNSIGNED_INT:
switch (size) {
case sizeof (uint64_t):
ullval = strtoull(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &ullval, size);
break;
case sizeof (uint32_t):
uintval = strtoul(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &uintval, size);
break;
case sizeof (uint16_t):
usval = (uint16_t)strtoul(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &usval, size);
break;
case sizeof (uint8_t):
ucval = (uint8_t)strtoul(val, &endptr, 0);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &ucval, size);
break;
default: /* invalid size */
return (EC_SYNTAX_ERR);
}
break;
case PICL_PTYPE_FLOAT:
switch (size) {
case sizeof (double):
dval = strtod(val, &endptr);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &dval, size);
break;
case sizeof (float):
fval = (float)strtod(val, &endptr);
if (endptr != (val + strlen(val)))
return (EC_SYNTAX_ERR);
(void) memcpy(outbuf, &fval, size);
break;
default: /* invalid size */
return (EC_SYNTAX_ERR);
}
break;
default: /* not supported type */
return (EC_SYNTAX_ERR);
}
return (EC_SYNTAX_OK);
}
/*
* free prop_cmd_t
*/
static void
free_prop(command_t *command)
{
free(command->propcmd_pname);
if (command->propcmd_type != PICL_PTYPE_VOID)
free(command->propcmd_valbuf);
}
/*
* return the string token in two double quotes
* The current version won't support multiple-line string
*/
static int
get_string_token(char *line, char **valtok)
{
char *optr; /* ptr to the open quote */
char *cptr; /* ptr to the close quote */
char *ptr;
char *tmpbuf;
if (line == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* skipping leading white spaces */
optr = line;
while ((*optr == ' ') || (*optr == '\t') || (*optr == '\n'))
optr++;
/* reach end of string */
if (*optr == '\0')
return (EC_INSUFFICIENT_TOKEN);
/* it's not an open double quote */
if (*optr != '"')
return (EC_SYNTAX_ERR);
/* skipping ending white spaces */
cptr = line + strlen(line) - 1;
while ((*cptr == ' ') || (*cptr == '\t') || (*cptr == '\n'))
cptr--;
/* it's not an close double quote */
if (*cptr != '"')
return (EC_SYNTAX_ERR);
/* close double quote is missing */
if (cptr == optr)
return (EC_SYNTAX_ERR);
/* replace close qoute by null to make a string */
*cptr = '\0';
/* move the begin pointer to the first char of string */
optr++;
tmpbuf = malloc(strlen(optr) + 1);
if (tmpbuf == NULL)
return (EC_FAILURE);
for (ptr = tmpbuf; *optr != '\0'; ptr++, optr++) {
/* if escape character, go to next character */
if (*optr == '\\') {
optr++;
if (*optr == '\0') { /* for exampe, "xxx\" */
free(tmpbuf);
return (EC_SYNTAX_ERR);
}
}
*ptr = *optr;
}
*ptr = '\0';
*valtok = tmpbuf;
return (EC_SYNTAX_OK);
}
/*
* Check the PROP syntax
* PROP <name> <type> <access_mode> [<size> <value>]
* supported prop types: void, int, uint, float, string
* supported prop access_modes: r, w, rw
* For void prop, <size> and <value> are not needed
* For string prop, <size> will be set the actual string size if <size>
* is 0
*/
static int
parse_prop(char *line, command_t *command)
{
char *tok;
char *pnametok;
int typetok;
size_t sizetok;
int modetok;
char *valtok;
char *last;
char *endptr;
int err;
/* get the PROP directive */
tok = strtok_r(line, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the property name */
pnametok = strtok_r(last, WHITESPACE, &last);
if (pnametok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the type */
tok = strtok_r(last, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
if ((typetok = getpicltype(tok)) < 0)
return (EC_SYNTAX_ERR);
/* get mode */
tok = strtok_r(last, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
if ((modetok = getpiclmode(tok)) < 0)
return (EC_SYNTAX_ERR);
if (typetok == PICL_PTYPE_VOID) {
/* ignore the rest of arguments */
command->propcmd_valbuf = NULL;
command->propcmd_pname = strdup(pnametok);
if (command->propcmd_pname == NULL)
return (EC_FAILURE);
command->propcmd_type = typetok;
command->propcmd_accessmode = modetok;
command->propcmd_size = 0;
command->propcmd_proph = 0;
return (EC_SYNTAX_OK);
}
/* get size */
tok = strtok_r(last, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
sizetok = (size_t)strtol(tok, &endptr, 0);
if (endptr != (tok + strlen(tok)))
return (EC_SYNTAX_ERR);
/* get val */
if (typetok == PICL_PTYPE_CHARSTRING) {
err = get_string_token(last, &valtok);
if (err != EC_SYNTAX_OK)
return (err);
if (sizetok == 0)
sizetok = strlen(valtok) + 1;
command->propcmd_valbuf = valtok;
} else {
valtok = strtok_r(last, WHITESPACE, &last);
if (valtok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* check if more tokens */
tok = strtok_r(last, WHITESPACE, &last);
if (tok != NULL)
return (EC_SYNTAX_ERR);
command->propcmd_valbuf = malloc(sizetok);
if (command->propcmd_valbuf == NULL)
return (EC_FAILURE);
err = validate_size_and_cvt_val(command->propcmd_valbuf,
sizetok, typetok, valtok);
if (err != EC_SYNTAX_OK) {
free(command->propcmd_valbuf);
return (err);
}
}
command->propcmd_pname = strdup(pnametok);
if (command->propcmd_pname == NULL)
return (EC_FAILURE);
command->propcmd_type = typetok;
command->propcmd_accessmode = modetok;
command->propcmd_size = sizetok;
command->propcmd_proph = 0;
return (EC_SYNTAX_OK);
}
/*
* Add a property to the row, the row gets added to the node at endrow
*/
static int
add_proph_to_row(command_t *command, picl_prophdl_t proph)
{
if (command->rowcmd_index >= command->rowcmd_nproph)
return (PICL_FAILURE);
command->rowcmd_prophs[command->rowcmd_index] = proph;
command->rowcmd_index++;
return (PICL_SUCCESS);
}
/*
* Process the PROP command and add the specified property under the given
* node handle
*/
static int
process_prop(cmdbuf_t *cmds, command_t *command, picl_nodehdl_t nodeh)
{
ptree_propinfo_t propinfo;
picl_prophdl_t proph;
int err;
/* prop in discarded row */
if (cmds->inside_row_block &&
cmds->commands[cmds->current_row].rowcmd_nproph == 0)
return (PICL_SUCCESS);
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
command->propcmd_type, command->propcmd_accessmode,
command->propcmd_size, command->propcmd_pname, NULL,
NULL);
if (err != PICL_SUCCESS)
return (err);
err = ptree_create_prop(&propinfo, command->propcmd_valbuf, &proph);
if (err != PICL_SUCCESS)
return (err);
command->propcmd_proph = proph;
if (cmds->inside_row_block) {
err = add_proph_to_row(&cmds->commands[cmds->current_row],
proph);
} else {
err = ptree_add_prop(nodeh, proph);
}
return (err);
}
/*
* free refnode_cmd_t
*/
static void
free_refnode(command_t *command)
{
free(command->refnodecmd_name);
free(command->refnodecmd_class);
free(command->refnodecmd_dstnode);
}
/*
* Check the REFNODE syntax
*
* REFNODE <name> <class> with <destnode> -- if <destnode> exists,
* create node with nodename <name> and piclclass <class>
*/
static int
parse_refnode(char *line, command_t *command)
{
char *tok;
char *dsttok;
char *classnm;
char *nodenm;
char *last;
/* get the directive */
tok = strtok_r(line, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the nodename */
nodenm = strtok_r(last, WHITESPACE, &last);
if (nodenm == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the class */
classnm = strtok_r(last, WHITESPACE, &last);
if (classnm == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the WITH keyword */
tok = strtok_r(last, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
if (strcasecmp(tok, KEYWORD_WITH_STR) != 0)
return (EC_SYNTAX_ERR);
/* get the dst node */
dsttok = strtok_r(last, WHITESPACE, &last);
if (dsttok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* check if more tokens */
tok = strtok_r(last, WHITESPACE, &last);
if (tok != NULL)
return (EC_SYNTAX_ERR);
command->refnodecmd_name = strdup(nodenm);
command->refnodecmd_class = strdup(classnm);
command->refnodecmd_dstnode = strdup(dsttok);
command->refnodecmd_nodeh = 0;
if ((command->refnodecmd_name == NULL) ||
(command->refnodecmd_class == NULL) ||
(command->refnodecmd_dstnode == NULL))
return (EC_FAILURE);
return (EC_SYNTAX_OK);
}
/*
* Process the REFNODE command
*/
static int
process_refnode(command_t *command, picl_nodehdl_t parh)
{
picl_nodehdl_t dsth;
picl_nodehdl_t nodeh;
int err;
if ((ptree_get_node_by_path(command->refnodecmd_dstnode,
&dsth) == PICL_SUCCESS)) {
err = ptree_create_and_add_node(parh, command->refnodecmd_name,
command->refnodecmd_class, &nodeh);
if (err == PICL_SUCCESS)
command->refnodecmd_nodeh = nodeh;
return (err);
}
return (PICL_SUCCESS);
}
/*
* free refprop_cmd_t
*/
static void
free_refprop(command_t *command)
{
free(command->refpropcmd_pname);
free(command->refpropcmd_dstnode);
}
/*
* Check the REFPROP syntax
*
* REFPROP <prop> <destnode> -- creates a reference property to <destnode>
*/
static int
parse_refprop(char *line, command_t *command)
{
char *tok;
char *pnametok;
char *dsttok;
char *last;
/* get the REFPROP directive */
tok = strtok_r(line, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the propname */
pnametok = strtok_r(last, WHITESPACE, &last);
if (pnametok == NULL)
return (EC_INSUFFICIENT_TOKEN);
dsttok = strtok_r(last, WHITESPACE, &last);
if (dsttok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* check if more tokens */
tok = strtok_r(last, WHITESPACE, &last);
if (tok != NULL)
return (EC_SYNTAX_ERR);
command->refpropcmd_pname = strdup(pnametok);
command->refpropcmd_dstnode = strdup(dsttok);
command->refpropcmd_proph = 0;
if ((command->refpropcmd_pname == NULL) ||
(command->refpropcmd_dstnode == NULL))
return (EC_FAILURE);
return (EC_SYNTAX_OK);
}
/*
* Process the REFPROP command
*/
static int
process_refprop(cmdbuf_t *cmds, command_t *command, picl_nodehdl_t nodeh)
{
int err;
picl_nodehdl_t dsth;
picl_prophdl_t proph;
ptree_propinfo_t propinfo;
/* refprop in discarded row */
if (cmds->inside_row_block &&
cmds->commands[cmds->current_row].rowcmd_nproph == 0)
return (PICL_SUCCESS);
/* try finding the refprop's dstnode */
err = ptree_get_node_by_path(command->refpropcmd_dstnode, &dsth);
/* dstnode doesn't exist, return */
if (err != PICL_SUCCESS)
return (err);
/* dstnode exists, try adding the refprop to nodeh */
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_REFERENCE, PICL_READ, sizeof (picl_nodehdl_t),
command->refpropcmd_pname, NULL, NULL);
if (err != PICL_SUCCESS)
return (err);
err = ptree_create_prop(&propinfo, &dsth, &proph);
if (err != PICL_SUCCESS)
return (err);
command->refpropcmd_proph = proph;
if (cmds->inside_row_block) {
err = add_proph_to_row(&cmds->commands[cmds->current_row],
proph);
} else {
err = ptree_add_prop(nodeh, proph);
}
return (err);
}
/*
* free table_cmd_t
*/
static void
free_table(command_t *command)
{
if (command->tablecmd_tname)
free(command->tablecmd_tname);
}
/*
* Check the TABLE syntax
* TABLE <table_prop_name>
*
*/
static int
parse_table(char *line, command_t *command)
{
char *tok = NULL;
char *tnametok = NULL;
char *last = NULL;
/* get the TABLE directive */
tok = strtok_r(line, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get the property name */
tnametok = strtok_r(last, WHITESPACE, &last);
if (tnametok == NULL)
return (EC_INSUFFICIENT_TOKEN);
command->tablecmd_tname = strdup(tnametok);
if (command->tablecmd_tname == NULL)
return (EC_FAILURE);
command->tablecmd_newtbl = 0;
command->tablecmd_tblh = 0;
return (EC_SYNTAX_OK);
}
/*
* Process the TABLE command and add the specified property under the given
* node handle
*/
static int
process_table(command_t *command, picl_nodehdl_t nodeh)
{
int err;
picl_prophdl_t tblh;
picl_prophdl_t proph;
ptree_propinfo_t propinfo;
/* find if table already exists */
err = ptree_get_prop_by_name(nodeh, command->tablecmd_tname, &tblh);
if (err == PICL_SUCCESS) {
err = ptree_get_propinfo(tblh, &propinfo);
if (err != PICL_SUCCESS)
return (err);
/* prop with the same name as table? */
if (propinfo.piclinfo.type != PICL_PTYPE_TABLE)
return (EC_SYNTAX_ERR);
command->tablecmd_newtbl = 0;
command->tablecmd_tblh = tblh;
return (PICL_SUCCESS);
}
/* init and create a new table */
err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
PICL_PTYPE_TABLE, PICL_READ|PICL_WRITE,
sizeof (picl_prophdl_t), command->tablecmd_tname, NULL, NULL);
if (err != PICL_SUCCESS)
return (err);
err = ptree_create_table(&tblh);
if (err != PICL_SUCCESS)
return (err);
command->tablecmd_newtbl = 1;
command->tablecmd_tblh = tblh;
err = ptree_create_prop(&propinfo, &tblh, &proph);
if (err != PICL_SUCCESS)
return (err);
err = ptree_add_prop(nodeh, proph);
return (err);
}
/*
* Process the ROW command by alloc'ing space to store the prop handles for
* the whole row. The number of props in the row gets known while parsing.
*/
static int
process_row(command_t *command)
{
command->rowcmd_index = 0;
command->rowcmd_prophs =
malloc(command->rowcmd_nproph * sizeof (picl_prophdl_t));
if (command->rowcmd_prophs == NULL)
return (PICL_FAILURE);
return (PICL_SUCCESS);
}
/*
* Process the ENDROW command. If a valid row, add the row to the ptree.
*/
static int
process_endrow(cmdbuf_t *cmds)
{
int err;
int i;
command_t *curr_row;
curr_row = &cmds->commands[cmds->current_row];
/* if nproph == 0, some row prop had problems, don't add */
if (curr_row->rowcmd_nproph == 0) {
for (i = 0; i < curr_row->rowcmd_index; i++) {
(void) ptree_delete_prop(curr_row->rowcmd_prophs[i]);
(void) ptree_destroy_prop(curr_row->rowcmd_prophs[i]);
}
err = PICL_SUCCESS;
} else
err = ptree_add_row_to_table(
cmds->commands[cmds->current_tbl].tablecmd_tblh,
curr_row->rowcmd_nproph,
curr_row->rowcmd_prophs);
/* let go the space alloc'd in process_row */
free(curr_row->rowcmd_prophs);
curr_row->rowcmd_prophs = NULL;
return (err);
}
/*
* Check the VERBOSE syntax
* VERBOSE <level>
*/
static int
parse_verbose(cmdbuf_t *cmds, char *line, command_t *command)
{
char *tok;
char *level;
char *last;
char *endptr;
int verbose_level;
/* get the VERBOSE directive */
tok = strtok_r(line, WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
/* get verbose level */
level = strtok_r(last, WHITESPACE, &last);
if (level == NULL)
return (EC_INSUFFICIENT_TOKEN);
verbose_level = strtol(level, &endptr, 0);
if (endptr != (level + strlen(level)))
return (EC_SYNTAX_ERR);
/* check if more tokens */
tok = strtok_r(last, WHITESPACE, &last);
if (tok != NULL)
return (EC_SYNTAX_ERR);
cmds->verbose = verbose_level;
command->verbosecmd_level = verbose_level;
return (EC_SYNTAX_OK);
}
/*
* Process the VERBOSE command to set the verbose level
*/
static int
process_verbose(cmdbuf_t *cmds, command_t *command)
{
cmds->verbose = command->verbosecmd_level;
return (PICL_SUCCESS);
}
/*
* parse and tokenize the line
*/
static int
parse_and_tokenize_line(cmdbuf_t *cmds, char *buf, command_t *command)
{
char rec[RECORD_SIZE_MAX];
char *tok;
int err;
char *last;
int id;
(void) strcpy(rec, buf);
tok = strtok_r(rec, RECORD_WHITESPACE, &last);
if (tok == NULL)
return (EC_INSUFFICIENT_TOKEN);
id = get_token_id(tok);
(void) strcpy(rec, buf);
switch (id) {
case TOK_VERSION:
err = parse_version(cmds, rec);
break;
case TOK_CLASSPATH:
case TOK_NAMEPATH:
if (cmds->inside_node_block != 0)
return (EC_PATH_ERR);
err = parse_path(rec, command);
if (err != EC_SYNTAX_OK)
return (err);
break;
case TOK_NODE:
/* Check for NODE outside of TABLE, ROW */
if ((cmds->inside_table_block != 0) ||
(cmds->inside_row_block != 0))
return (EC_SYNTAX_ERR);
err = parse_node(rec, command);
if (err != EC_SYNTAX_OK)
return (err);
cmds->inside_node_block++;
break;
case TOK_ENDNODE:
/* Check for ENDNODE outside of TABLE, ROW */
if ((cmds->inside_table_block != 0) ||
(cmds->inside_row_block != 0))
return (EC_SYNTAX_ERR);
cmds->inside_node_block--;
err = EC_SYNTAX_OK;
break;
case TOK_PROP:
/* Check if inside TABLE, but not in ROW */
if ((cmds->inside_table_block != 0) &&
(cmds->inside_row_block == 0))
return (EC_SYNTAX_ERR);
err = parse_prop(rec, command);
if (err != EC_SYNTAX_OK)
return (err);
if (cmds->inside_row_block) {
cmds->commands[cmds->current_row].rowcmd_nproph++;
}
break;
case TOK_REFNODE:
err = parse_refnode(rec, command);
if (err != EC_SYNTAX_OK)
return (err);
break;
case TOK_REFPROP:
/* Check if inside TABLE, but not in ROW */
if ((cmds->inside_table_block != 0) &&
(cmds->inside_row_block == 0))
return (EC_SYNTAX_ERR);
err = parse_refprop(rec, command);
if (err != EC_SYNTAX_OK)
return (err);
if (cmds->inside_row_block) {
cmds->commands[cmds->current_row].rowcmd_nproph++;
}
break;
case TOK_TABLE:
/* Table/Row supported in version 1.1 and above */
if (cmds->version_no < (float)SUPPORTED_VERSION_NUM)
return (EC_UNSUPPORTED);
if (cmds->inside_table_block != 0)
return (EC_SYNTAX_ERR);
err = parse_table(rec, command);
if (err != EC_SYNTAX_OK)
return (err);
cmds->inside_table_block = 1;
break;
case TOK_ENDTABLE:
/* Check for ENDTABLE before TABLE */
if (cmds->inside_table_block == 0)
return (EC_SYNTAX_ERR);
cmds->inside_table_block = 0;
break;
case TOK_ROW:
/* Check for ROW outside of TABLE, ROW inside ROW */
if ((cmds->inside_table_block == 0) ||
(cmds->inside_row_block != 0))
return (EC_SYNTAX_ERR);
cmds->inside_row_block = 1;
break;
case TOK_ENDROW:
/* Check for ENDROW outside of TABLE, ENDROW before ROW */
if ((cmds->inside_table_block == 0) ||
(cmds->inside_row_block == 0))
return (EC_SYNTAX_ERR);
else
err = EC_SYNTAX_OK;
cmds->inside_row_block = 0;
/* error if row is empty */
if (cmds->commands[cmds->current_row].rowcmd_nproph <= 0)
return (EC_ROW_EMPTY);
break;
case TOK_VERBOSE:
err = parse_verbose(cmds, rec, command);
if (err != EC_SYNTAX_OK)
return (err);
break;
default: /* unsupported command */
return (EC_SYNTAX_ERR);
}
command->type = id;
return (EC_SYNTAX_OK);
}
/*
* Check the syntax and save the tokens in the commands buffer
*/
static int
check_line_syntax(cmdbuf_t *cmds, char *buf)
{
int err;
command_t command;
(void) memset(&command, 0, sizeof (command_t));
err = parse_and_tokenize_line(cmds, buf, &command);
if (err != EC_SYNTAX_OK)
return (err);
/*
* don't add and count version command in the command buffer
*/
if (command.type == TOK_VERSION)
return (EC_SYNTAX_OK);
/*
* check if the commands buffer has been filled
* If it is full, reallocate the buffer.
*/
if (cmds->count == cmds->allocated) {
cmds->commands = realloc(cmds->commands,
sizeof (command_t) * (cmds->allocated + PER_ALLOC_COUNT));
if (cmds->commands == NULL)
return (EC_FAILURE);
cmds->allocated += PER_ALLOC_COUNT;
}
cmds->commands[cmds->count] = command; /* copy */
/*
* make a note of the row/endrow command, to keep track of # of props
*/
if (command.type == TOK_ROW)
cmds->current_row = cmds->count;
if (command.type == TOK_ENDROW)
cmds->current_row = 0;
cmds->count++;
return (EC_SYNTAX_OK);
}
/*
* get the line control information
* return 1 if it's the line control information, else return 0
*/
static int
get_line_control_info(char *buf, uint32_t *linenum, char *filename)
{
char *ptr;
char *last;
uint32_t num;
char *fname;
char *endptr;
/* skip # and get next string */
ptr = strtok_r(buf + 1, WHITESPACE, &last);
if (ptr == NULL) {
return (0);
}
num = strtoul(ptr, &endptr, 0);
/*
* It's not the line control information
*/
if (endptr != (ptr + strlen(ptr))) {
return (0);
}
/*
* get the filename
*/
/* get the beginning double quote */
last = strchr(last, '"');
if (last == NULL)
return (0);
last++;
/* get the ending double quote */
fname = strtok_r(last, DOUBLE_QUOTE, &last);
if (fname == NULL)
return (0);
*linenum = num;
(void) strlcpy(filename, fname, PATH_MAX);
return (1);
}
/*
* check the syntax of the configuration file
*/
static int
check_conffile_syntax(cmdbuf_t *cmds, FILE *fp)
{
char lbuf[RECORD_SIZE_MAX];
char buf[RECORD_SIZE_MAX];
uint32_t linenum;
char cppfile[PATH_MAX] = "";
int err = EC_SYNTAX_OK;
linenum = 0;
while (fgets(buf, sizeof (buf), fp) != NULL) {
/*
* get cpp line control information, if any
*/
if (buf[0] == '#') {
if (!get_line_control_info(buf, &linenum, cppfile))
++linenum;
continue;
}
++linenum;
/*
* skip line whose first char is a newline char
*/
if (buf[0] == '\n') {
continue;
}
if (err == EC_SYNTAX_OK)
(void) strlcpy(lbuf, buf, RECORD_SIZE_MAX);
else if (strlcat(lbuf, buf, RECORD_SIZE_MAX) >=
RECORD_SIZE_MAX) { /* buffer overflow */
err = EC_FAILURE;
break;
}
err = check_line_syntax(cmds, lbuf);
if ((err != EC_INSUFFICIENT_TOKEN) && (err != EC_SYNTAX_OK))
break;
}
if (err != EC_SYNTAX_OK) {
if (cmds->verbose) {
verbose_log(LOG_ERR, err_msg[err],
cmds->fname, cppfile, linenum);
}
return (err);
}
/*
* check if the version has been set
*/
if (cmds->version_no > (float)SUPPORTED_VERSION_NUM) {
if (cmds->verbose) {
verbose_log(LOG_ERR, err_msg[EC_UNSUPPORTED],
cmds->fname, cppfile, linenum);
}
return (EC_UNSUPPORTED);
}
/*
* check if node and endnode command mismatch
*/
if (cmds->inside_node_block != 0) {
if (cmds->verbose) {
verbose_log(LOG_ERR, err_msg[EC_NODE_MISMATCH],
cmds->fname, cppfile, linenum);
}
return (EC_NODE_MISMATCH);
}
/*
* check if row and endrow command mismatch
*/
if (cmds->inside_row_block != 0) {
if (cmds->verbose) {
verbose_log(LOG_ERR, err_msg[EC_ROW_MISMATCH],
cmds->fname, cppfile, linenum);
}
return (EC_ROW_MISMATCH);
}
/*
* check if table and endtable command mismatch
*/
if (cmds->inside_table_block != 0) {
if (cmds->verbose) {
verbose_log(LOG_ERR, err_msg[EC_TABLE_MISMATCH],
cmds->fname, cppfile, linenum);
}
return (EC_TABLE_MISMATCH);
}
return (EC_SYNTAX_OK);
}
/*
* If classpath/namepath given is not found in the picl tree,
* skip the whole blocks until next valid classpath or namepath
*/
static void
skip_to_next_valid_path(cmdbuf_t *cmds, int starting_index,
picl_nodehdl_t *parent, int *last_processed_index)
{
int err;
int index;
for (index = starting_index; index < cmds->count; ++index) {
switch (cmds->commands[index].type) {
case TOK_CLASSPATH:
case TOK_NAMEPATH:
err = process_path(&cmds->commands[index], parent);
if (err == PICL_SUCCESS) {
*last_processed_index = index;
return;
}
default:
/* skipped this line */
break;
}
}
/* reach last command */
*last_processed_index = cmds->count - 1;
}
/*
* Process the command buffer and return last command index and the new head of
* the handle list
*/
static int
process_commands(cmdbuf_t *cmds, int starting_index, picl_nodehdl_t parent,
int *last_processed_index)
{
int err;
int index;
picl_nodehdl_t rooth;
picl_nodehdl_t nodeh;
command_t *commands = cmds->commands;
for (index = starting_index; index < cmds->count; ++index) {
switch (commands[index].type) {
case TOK_CLASSPATH:
case TOK_NAMEPATH:
err = process_path(&commands[index], &rooth);
if (err != PICL_SUCCESS) {
index++;
(void) skip_to_next_valid_path(cmds, index,
&rooth, &index);
}
parent = rooth;
continue;
case TOK_NODE:
err = process_node(&commands[index], parent, &nodeh);
if (err == PICL_SUCCESS) {
index++;
err = process_commands(cmds, index, nodeh,
&index);
}
break;
case TOK_ENDNODE:
*last_processed_index = index;
return (PICL_SUCCESS);
case TOK_PROP:
err = process_prop(cmds, &commands[index], parent);
break;
case TOK_REFPROP:
err = process_refprop(cmds, &commands[index], parent);
/* no reference node */
if (err == PICL_NOTNODE) {
err = PICL_SUCCESS; /* discard prop */
/* discard row by setting nproph = 0 */
if (cmds->inside_row_block)
cmds->commands[cmds->current_row]
.rowcmd_nproph = 0;
}
break;
case TOK_REFNODE:
err = process_refnode(&commands[index], parent);
break;
case TOK_TABLE:
cmds->inside_table_block = 1;
err = process_table(&commands[index], parent);
cmds->current_tbl = index;
break;
case TOK_ENDTABLE:
cmds->inside_table_block = 0;
cmds->current_tbl = 0;
break;
case TOK_ROW:
cmds->inside_row_block = 1;
err = process_row(&commands[index]);
cmds->current_row = index;
break;
case TOK_ENDROW:
err = process_endrow(cmds);
cmds->inside_row_block = 0;
cmds->current_row = 0;
break;
case TOK_VERBOSE:
err = process_verbose(cmds, &commands[index]);
break;
default: /* won't reach here */
err = PICL_FAILURE;
break;
}
if ((err != PICL_SUCCESS) && (err != PICL_PROPEXISTS)) {
*last_processed_index = index;
return (err);
}
}
/* reach last command */
*last_processed_index = cmds->count - 1;
return (PICL_SUCCESS);
}
/*
* clean up the commands buffer
*/
static void
clean_up(cmdbuf_t *cmds)
{
int cmd_index;
for (cmd_index = 0; cmd_index < cmds->count; cmd_index++) {
switch (cmds->commands[cmd_index].type) {
case TOK_CLASSPATH:
case TOK_NAMEPATH:
free_path(&cmds->commands[cmd_index]);
break;
case TOK_NODE:
free_node(&cmds->commands[cmd_index]);
break;
case TOK_PROP:
free_prop(&cmds->commands[cmd_index]);
break;
case TOK_REFPROP:
free_refprop(&cmds->commands[cmd_index]);
break;
case TOK_REFNODE:
free_refnode(&cmds->commands[cmd_index]);
break;
case TOK_TABLE:
free_table(&cmds->commands[cmd_index]);
break;
case TOK_ENDTABLE:
case TOK_ROW:
case TOK_ENDROW:
case TOK_ENDNODE:
case TOK_VERBOSE:
default:
break;
}
}
if (cmds->commands)
free(cmds->commands);
}
/*
* Parse the configuration file and create nodes/properties under nh
*
* It checks the syntax first. If there is any syntax error,
* it returns 1 and won't continue processing the file to add nodes or props.
*
* If any error happens during command processing, all nodes
* and properties just created will be deleted, i.e. undo
* commands which have been processed. It returns 1.
*
* If success, return 0.
*/
int
picld_pluginutil_parse_config_file(picl_nodehdl_t nh, const char *filename)
{
FILE *ifp;
int last_processed_index;
int err;
cmdbuf_t *cmds;
/* set correct locale for use inside pluginutil */
setlocale(LC_ALL, "C");
/*
* Initialize the command buffer
*/
cmds = malloc(sizeof (*cmds));
if (cmds == NULL) {
setlocale(LC_ALL, "");
return (1);
}
memset(cmds, 0, sizeof (cmdbuf_t));
cmds->fname = filename;
ifp = fopen(filename, "r");
if (ifp == NULL) {
setlocale(LC_ALL, "");
free(cmds);
return (1);
}
/*
* check the syntax of the configuration file
*/
err = check_conffile_syntax(cmds, ifp);
(void) fclose(ifp);
if (err != EC_SYNTAX_OK) {
clean_up(cmds);
free(cmds);
setlocale(LC_ALL, "");
return (1);
}
/*
* Process the commands
*/
err = process_commands(cmds, STARTING_INDEX, nh, &last_processed_index);
/*
* If any PICL error, remove the newly created node/prop
* handles from the PICL tree.
*/
if (err != PICL_SUCCESS) {
undo_commands(cmds, last_processed_index);
if (cmds->verbose)
verbose_log(LOG_ERR, err_msg[EC_PICL_ERR], filename,
err);
}
clean_up(cmds);
free(cmds);
/* reset the locale */
setlocale(LC_ALL, "");
return ((err == PICL_SUCCESS) ? 0 : 1);
}
|