xref: /illumos-gate/usr/src/lib/libfru/libfru/Parser.cc (revision 1da57d55)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <pthread.h>
28 #include <string.h>
29 #include <stdlib.h>
30 
31 #include "Parser.h"
32 
33 // yacc symbols
34 int fruparse(void);
35 extern int frudebug;
36 
37 // global data to/from the lexer
38 pthread_mutex_t gParserLock;
39 fru_errno_t gParserErrno = FRU_SUCCESS;
40 char *gParserString = NULL;
41 Ancestor *gParserAnts   = NULL;
42 PathDef *gParserHead   = NULL;
43 int *gParserAbs    = NULL;
44 
45 // returns a NULL terminated list of PathDef objects.
46 // and a NULL terminated list of ancestor objects this path exists in
47 // NOTE: ancestors may be NULL if no tags contain a valid path.
48 fru_errno_t
49 fru_field_parser(const char *path, Ancestor **ancestors,
50 	int *absolute, PathDef **pathDef)
51 {
52 	// lock up the globals for the parser...
53 	pthread_mutex_lock(&gParserLock);
54 
55 	// get out a string for the parser to play with.
56 	gParserString = strdup(path);
57 	if (gParserString == NULL) {
58 		pthread_mutex_unlock(&gParserLock);
59 		return (FRU_FAILURE);
60 	}
61 	// save the head pointer for delete.
62 	char *delPtr = gParserString;
63 
64 	// frudebug = 1;
65 
66 	// set up for return data from lexer.
67 	gParserHead = NULL;
68 	gParserAnts = NULL;
69 	gParserErrno = FRU_SUCCESS;
70 	gParserAbs = absolute;
71 	*gParserAbs = 0;
72 
73 	int rc = fruparse();
74 
75 	// clean up the string we used for yacc.
76 	free(delPtr);
77 	gParserString = NULL;
78 
79 	// frudebug = 0;
80 	if (rc != 0) {
81 		delete gParserHead;
82 		delete gParserAnts;
83 		fru_errno_t err = gParserErrno;
84 		pthread_mutex_unlock(&gParserLock);
85 		return (err);
86 	}
87 
88 	/* if ((gParserHead == NULL) || (gParserAnts == NULL)) { */
89 	/* allow ancestors to be NULL */
90 	/* some elements don't have tagged ancestors */
91 	if (gParserHead == NULL) {
92 		delete gParserAnts;
93 		pthread_mutex_unlock(&gParserLock);
94 		return (FRU_FAILURE);
95 	}
96 
97 	*pathDef = gParserHead;
98 	*ancestors = gParserAnts;
99 
100 	pthread_mutex_unlock(&gParserLock);
101 	return (FRU_SUCCESS);
102 }
103