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