xref: /illumos-gate/usr/src/lib/libfru/libfru/Parser.cc (revision 7eae07ab)
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 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 int fruparse(void);
38 extern int frudebug;
39 #ifdef	__cplusplus
40 }
41 #endif
42 
43 // global data to/from the lexer
44 pthread_mutex_t gParserLock;
45 fru_errno_t gParserErrno = FRU_SUCCESS;
46 char *gParserString = NULL;
47 Ancestor *gParserAnts   = NULL;
48 PathDef *gParserHead   = NULL;
49 int *gParserAbs    = NULL;
50 
51 // returns a NULL terminated list of PathDef objects.
52 // and a NULL terminated list of ancestor objects this path exists in
53 // NOTE: ancestors may be NULL if no tags contain a valid path.
54 fru_errno_t
fru_field_parser(const char * path,Ancestor ** ancestors,int * absolute,PathDef ** pathDef)55 fru_field_parser(const char *path, Ancestor **ancestors,
56 	int *absolute, PathDef **pathDef)
57 {
58 	// lock up the globals for the parser...
59 	pthread_mutex_lock(&gParserLock);
60 
61 	// get out a string for the parser to play with.
62 	gParserString = strdup(path);
63 	if (gParserString == NULL) {
64 		pthread_mutex_unlock(&gParserLock);
65 		return (FRU_FAILURE);
66 	}
67 	// save the head pointer for delete.
68 	char *delPtr = gParserString;
69 
70 	// frudebug = 1;
71 
72 	// set up for return data from lexer.
73 	gParserHead = NULL;
74 	gParserAnts = NULL;
75 	gParserErrno = FRU_SUCCESS;
76 	gParserAbs = absolute;
77 	*gParserAbs = 0;
78 
79 	int rc = fruparse();
80 
81 	// clean up the string we used for yacc.
82 	free(delPtr);
83 	gParserString = NULL;
84 
85 	// frudebug = 0;
86 	if (rc != 0) {
87 		delete gParserHead;
88 		delete gParserAnts;
89 		fru_errno_t err = gParserErrno;
90 		pthread_mutex_unlock(&gParserLock);
91 		return (err);
92 	}
93 
94 	/* if ((gParserHead == NULL) || (gParserAnts == NULL)) { */
95 	/* allow ancestors to be NULL */
96 	/* some elements don't have tagged ancestors */
97 	if (gParserHead == NULL) {
98 		delete gParserAnts;
99 		pthread_mutex_unlock(&gParserLock);
100 		return (FRU_FAILURE);
101 	}
102 
103 	*pathDef = gParserHead;
104 	*ancestors = gParserAnts;
105 
106 	pthread_mutex_unlock(&gParserLock);
107 	return (FRU_SUCCESS);
108 }
109