xref: /illumos-gate/usr/src/cmd/abi/spectrans/spec2trace/db.c (revision 61dfa5098dc8576d9a5e277deba6df647bb70c06)
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) 1997-1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*
28  * db.c -- the tiny database for trace.  Only stores
29  *	global things: see symtab for per-function data.
30  *
31  */
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <libgen.h>
36 #include <limits.h>
37 #include <sys/param.h>
38 #include "parser.h"
39 #include "trace.h"
40 #include "util.h"
41 #include "errlog.h"
42 #include "db.h"
43 
44 static int curr_print_type;
45 
46 static struct {
47 	char	Current_Library[PATH_MAX];
48 	char	Current_File[PATH_MAX];
49 	char	Output_File[PATH_MAX];
50 	char	Current_Interface[PATH_MAX];
51 	char	Source_Directory[PATH_MAX];
52 	char	Target_Directory[PATH_MAX];
53 	int	NFiles;
54 	int	Verbosity;
55 	char	Library_List[PATH_MAX];
56 	char	Translator[MAXNAMELEN];
57 	char	Test_Type[MAXNAMELEN];
58 	char	Kludge[PATH_MAX];
59 	int	Flags;
60 	char const *Arch;
61 	table_t	*Print_Types;
62 	table_t	*File;
63 	table_t	*Exclusions;
64 
65 } Database;
66 
67 
68 /* Generated by m4 -- character string values */
69 void
70 db_set_current_library(char const *p)
71 {
72 	errlog(BEGIN, "db_set_current_library() {");
73 	(void) strncpy(Database.Current_Library, p,
74 	    sizeof (Database.Current_Library));
75 	Database.Current_Library[sizeof (Database.Current_Library) - 1] = '\0';
76 	errlog(END, "}");
77 }
78 
79 char *
80 db_get_current_library(void)
81 {
82 	errlog(BEGIN, "db_get_current_library() {"); errlog(END, "}");
83 	return (Database.Current_Library);
84 }
85 
86 void
87 db_set_current_interface(char const *p)
88 {
89 	errlog(BEGIN, "db_set_current_interface() {");
90 	(void) strncpy(Database.Current_Interface, p,
91 	    sizeof (Database.Current_Interface));
92 	Database.Current_Interface[
93 		sizeof (Database.Current_Interface) - 1] = '\0';
94 	errlog(END, "}");
95 }
96 
97 char *
98 db_get_current_interface(void)
99 {
100 	errlog(BEGIN, "db_get_current_interface() {"); errlog(END, "}");
101 	return (Database.Current_Interface);
102 }
103 
104 
105 void
106 db_set_source_directory(char const *p)
107 {
108 	errlog(BEGIN, "db_set_source_directory() {");
109 	(void) strncpy(Database.Source_Directory, p,
110 	    sizeof (Database.Source_Directory));
111 	Database.Source_Directory[sizeof (Database.Source_Directory) - 1] =
112 	    '\0';
113 	errlog(END, "}");
114 }
115 
116 char *
117 db_get_source_directory(void)
118 {
119 	errlog(BEGIN, "db_get_source_directory() {"); errlog(END, "}");
120 	return (Database.Source_Directory);
121 }
122 
123 
124 void
125 db_set_target_directory(char const *p)
126 {
127 	errlog(BEGIN, "db_set_target_directory() {");
128 	(void) strncpy(Database.Target_Directory, p,
129 	    sizeof (Database.Target_Directory));
130 	Database.Target_Directory[sizeof (Database.Target_Directory) - 1] =
131 	    '\0';
132 	errlog(END, "}");
133 }
134 
135 char *
136 db_get_target_directory(void)
137 {
138 	errlog(BEGIN, "db_get_target_directory() {"); errlog(END, "}");
139 	return (Database.Target_Directory);
140 }
141 
142 void
143 db_set_current_file(char const *p)
144 {
145 	(void) strncpy(Database.Current_File, p,
146 		sizeof (Database.Current_File));
147 	Database.Current_File[sizeof (Database.Current_File) - 1] = '\0';
148 }
149 
150 char *
151 db_get_current_file(void)
152 {
153 	return (Database.Current_File);
154 }
155 
156 
157 /*
158  * Output File -- set from either -o option or file name.
159  */
160 void
161 db_set_output_file(char const *p)
162 {
163 	char	*q;
164 
165 	errlog(BEGIN, "db_set_output_file() {");
166 	if (p == NULL) {
167 		errlog(END, "}");
168 		return;
169 	}
170 
171 	(void) strncpy(Database.Output_File, p, sizeof (Database.Output_File));
172 	if ((q = strrchr(Database.Output_File, '.')) != NULL) {
173 		*q = '\0';
174 	} else {
175 		Database.Output_File[sizeof (Database.Output_File) - 1] = '\0';
176 	}
177 	errlog(VERBOSE, "output file = '%s'\n", Database.Output_File);
178 	errlog(END, "}");
179 }
180 
181 char *
182 db_get_output_file(void)
183 {
184 	static char buffer[MAXLINE];
185 	char	*p, *q;
186 
187 	errlog(BEGIN, "db_get_output_file() {");
188 	if (*Database.Output_File != '\0') {
189 		/* It was set with the -o option */
190 		errlog(VERBOSE, "output file from -o = '%s'\n",
191 			Database.Output_File);
192 		errlog(END, "}");
193 		return (Database.Output_File);
194 	} else {
195 		/* We generate it from the current input file. */
196 		(void) strncpy(buffer, Database.Current_File, sizeof (buffer));
197 		p = basename(buffer);
198 		if ((q = strrchr(p, '.')) != NULL) {
199 			*q = '\0';
200 		}
201 		errlog(VERBOSE, "output file from input = '%s'\n", p);
202 		errlog(END, "}");
203 		return (p);
204 	}
205 }
206 
207 /*
208  * Manually written table code.
209  */
210 
211 /*
212  * add_print_types -- add legal print types.  Check for void
213  *	moved here out of collect, as collect isn't good enough
214  *	quality of parser to have a single code path for
215  *	types. (Shudder)  Subsequently changed to use special-purpose
216  *	test for membership.  Also shudder!
217  */
218 
219 void
220 db_add_print_types(char *print_type, char *c_type)
221 {
222 	char    buffer[MAXLINE];
223 
224 	errlog(BEGIN, "db_add_print_types() {");
225 
226 	(void) snprintf(buffer, sizeof (buffer), "%s, %s", print_type, c_type);
227 	if (Database.Print_Types == NULL) {
228 		Database.Print_Types = create_string_table(50);
229 	}
230 	if (in_string_table(Database.Print_Types, print_type) == NO) {
231 		Database.Print_Types = add_string_table(Database.Print_Types,
232 		    &buffer[0]);
233 	}
234 
235 	errlog(END, "}");
236 }
237 
238 char *
239 db_get_first_print_type(void)
240 {
241 	curr_print_type = 1;
242 	return (get_string_table(Database.Print_Types, 0));
243 }
244 
245 char *
246 db_get_next_print_type(void)
247 {
248 
249 	return (get_string_table(Database.Print_Types, curr_print_type++));
250 }
251 
252 
253 void
254 db_sort_print_types(void)
255 {
256 	errlog(BEGIN, "db_sort_print_types() {");
257 	sort_string_table(Database.Print_Types);
258 	errlog(END, "}");
259 }
260 
261 void
262 db_set_arch(char const *arch)
263 {
264 	Database.Arch = arch;
265 }
266 
267 char const *
268 db_get_arch(void)
269 {
270 	return (Database.Arch);
271 }
272