xref: /onnv-gate/usr/src/cmd/abi/spectrans/spec2trace/trace.c (revision 2775:892d346f56a9)
1*2775Sraf /*
2*2775Sraf  * CDDL HEADER START
3*2775Sraf  *
4*2775Sraf  * The contents of this file are subject to the terms of the
5*2775Sraf  * Common Development and Distribution License, Version 1.0 only
6*2775Sraf  * (the "License").  You may not use this file except in compliance
7*2775Sraf  * with the License.
8*2775Sraf  *
9*2775Sraf  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*2775Sraf  * or http://www.opensolaris.org/os/licensing.
11*2775Sraf  * See the License for the specific language governing permissions
12*2775Sraf  * and limitations under the License.
13*2775Sraf  *
14*2775Sraf  * When distributing Covered Code, include this CDDL HEADER in each
15*2775Sraf  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*2775Sraf  * If applicable, add the following below this CDDL HEADER, with the
17*2775Sraf  * fields enclosed by brackets "[]" replaced with your own identifying
18*2775Sraf  * information: Portions Copyright [yyyy] [name of copyright owner]
19*2775Sraf  *
20*2775Sraf  * CDDL HEADER END
21*2775Sraf  */
22*2775Sraf /*
23*2775Sraf  * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
24*2775Sraf  * All rights reserved.
25*2775Sraf  */
26*2775Sraf 
27*2775Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*2775Sraf 
29*2775Sraf /*
30*2775Sraf  *
31*2775Sraf  * trace.c -- a  simple translator from spec source to c source for
32*2775Sraf  *	a apptrace interposer library.  This file implements the
33*2775Sraf  *	(interface to) the front end. Other files implement the middle
34*2775Sraf  *	and databases, and generate.c implements the back end.
35*2775Sraf  *
36*2775Sraf  */
37*2775Sraf 
38*2775Sraf #include <stdio.h>
39*2775Sraf #include <errno.h>
40*2775Sraf #include <stdlib.h>
41*2775Sraf #include <sys/types.h>
42*2775Sraf #include <time.h>
43*2775Sraf #include <string.h>
44*2775Sraf 
45*2775Sraf #include "parser.h"
46*2775Sraf #include "trace.h"
47*2775Sraf 
48*2775Sraf #include "util.h"
49*2775Sraf #include "db.h"
50*2775Sraf #include "symtab.h"
51*2775Sraf #include "io.h"
52*2775Sraf #include "printfuncs.h"
53*2775Sraf #include "errlog.h"
54*2775Sraf #include "parseproto.h"
55*2775Sraf 
56*2775Sraf static int  Verbose;
57*2775Sraf 
58*2775Sraf /* File globals. This would be better as a class. */
59*2775Sraf /* The first four (commented out) of these enums are defined in parser.h */
60*2775Sraf enum {
61*2775Sraf 	/* XLATOR_KW_NOTFOUND = 0, */
62*2775Sraf 	/* XLATOR_KW_FUNC, */
63*2775Sraf 	/* XLATOR_KW_DATA */
64*2775Sraf 	/* XLATOR_KW_END */
65*2775Sraf 	XLATOR_KW_EXCP = 4,
66*2775Sraf 	XLATOR_KW_DECL,
67*2775Sraf 	XLATOR_KW_INCL,
68*2775Sraf 	XLATOR_KW_ERRNO,
69*2775Sraf 	XLATOR_KW_ERRVAL,
70*2775Sraf 	XLATOR_KW_ARCH,
71*2775Sraf 	XLATOR_KW_WEAK
72*2775Sraf };
73*2775Sraf #define	FIRST_TOKEN 4	/* Must match the first token in the above enum */
74*2775Sraf 
75*2775Sraf static xlator_keyword_t Keywords[] = {
76*2775Sraf 	{ "exception", XLATOR_KW_EXCP },
77*2775Sraf 	{ "declaration", XLATOR_KW_DECL },
78*2775Sraf 	{ "include", XLATOR_KW_INCL },
79*2775Sraf 	{ "errno", XLATOR_KW_ERRNO },
80*2775Sraf 	{ "errval", XLATOR_KW_ERRVAL},
81*2775Sraf 	{ "arch", XLATOR_KW_ARCH},
82*2775Sraf 	{ "weak", XLATOR_KW_WEAK},
83*2775Sraf 	{ "weakfor", XLATOR_KW_WEAK},
84*2775Sraf 	{ "alias", XLATOR_KW_WEAK},
85*2775Sraf 	{ NULL, XLATOR_KW_NOTFOUND }
86*2775Sraf };
87*2775Sraf 
88*2775Sraf static struct stats_t {
89*2775Sraf 	int	libraries,
90*2775Sraf 		files,
91*2775Sraf 		interfaces,
92*2775Sraf 		lines;
93*2775Sraf 	int	errors,
94*2775Sraf 		warnings,
95*2775Sraf 		skips;
96*2775Sraf 	time_t	start,
97*2775Sraf 		end;
98*2775Sraf } Statistics;
99*2775Sraf 
100*2775Sraf #define	LINE	(m.mi_line_number-(m.mi_nlines-1))
101*2775Sraf 
102*2775Sraf static void stats_init(void);
103*2775Sraf static void stats_report(void);
104*2775Sraf 
105*2775Sraf static int collect_binding(int const, char *, int);
106*2775Sraf static int collect_prototype(char *, int, int);
107*2775Sraf static int collect_include(char *, int);
108*2775Sraf static int collect_errval(char *, int);
109*2775Sraf static int collect_arch(char *);
110*2775Sraf 
111*2775Sraf static void generate_includes(void);
112*2775Sraf static void generate_init(void);
113*2775Sraf static void generate_interface(void);
114*2775Sraf static void generate_closedown(void);
115*2775Sraf static int generate_aux_file();
116*2775Sraf 
117*2775Sraf /* Local (static) parsing functions. */
118*2775Sraf static char *to_actual();
119*2775Sraf static int to_basetype(char *);
120*2775Sraf static char *de_const(char *);
121*2775Sraf static char *strpqcpy(char *, char *, char *);
122*2775Sraf 
123*2775Sraf /*
124*2775Sraf  * xlator_init -- initialize translator, called at startup-time
125*2775Sraf  *	with a struct translator_info of information the translator
126*2775Sraf  *	might need, returning a list of ``interesting'' spec keywords
127*2775Sraf  *	for the front end to select and pass to the back end translator.
128*2775Sraf  *
129*2775Sraf  */
130*2775Sraf xlator_keyword_t *
xlator_init(const Translator_info * t_info)131*2775Sraf xlator_init(const Translator_info *t_info)
132*2775Sraf {
133*2775Sraf 	int	i;
134*2775Sraf 
135*2775Sraf 	errlog(BEGIN, "xlator_init() {");
136*2775Sraf 
137*2775Sraf 	/* Save interesting parameters. */
138*2775Sraf 	stats_init();
139*2775Sraf 	db_set_source_directory(".");
140*2775Sraf 	db_set_target_directory(".");
141*2775Sraf 	Verbose = t_info->ti_verbosity;
142*2775Sraf 	seterrseverity(Verbose); /* Ditto. */
143*2775Sraf 	db_set_output_file(t_info->ti_output_file);
144*2775Sraf 	db_set_arch(t_info->ti_arch);
145*2775Sraf 
146*2775Sraf 	/* Display passed argument and return value. */
147*2775Sraf 	errlog(VERBOSE, "Keywords[] = {");
148*2775Sraf 	for (i = 0; Keywords[i].key != NULL; i++) {
149*2775Sraf 		errlog(VERBOSE, "    \"%s\", ", Keywords[i].key);
150*2775Sraf 	}
151*2775Sraf 	errlog(VERBOSE, "    (char *) NULL");
152*2775Sraf 	errlog(VERBOSE, "};");
153*2775Sraf 
154*2775Sraf 	errlog(END, "}");
155*2775Sraf 	return (Keywords);
156*2775Sraf }
157*2775Sraf 
158*2775Sraf /*
159*2775Sraf  * xlator_startlib -- called on starting a new library, so back end
160*2775Sraf  *	translator can decide to change output file/directory if desired.
161*2775Sraf  */
162*2775Sraf int
xlator_startlib(char const * libname)163*2775Sraf xlator_startlib(char const *libname)
164*2775Sraf {
165*2775Sraf 	errlog(BEGIN, "xlator_startlib() ");
166*2775Sraf 
167*2775Sraf 	Statistics.libraries++;
168*2775Sraf 	db_set_current_library(libname);
169*2775Sraf 	errlog(VERBOSE, "now in library \"%s\"", libname);
170*2775Sraf 	errlog(END, "}");
171*2775Sraf 	return (SUCCESS_RC);
172*2775Sraf }
173*2775Sraf 
174*2775Sraf /*
175*2775Sraf  * xlator_startfile -- ditto, called on starting each new spec file in the
176*2775Sraf  *	specified library.
177*2775Sraf  */
178*2775Sraf int
xlator_startfile(char const * filename)179*2775Sraf xlator_startfile(char const *filename)
180*2775Sraf {
181*2775Sraf 	int	rc = SUCCESS_RC;
182*2775Sraf 	char	infile[MAXLINE],
183*2775Sraf 		outfile[MAXLINE],
184*2775Sraf 		*lib = db_get_current_library();
185*2775Sraf 
186*2775Sraf 	seterrline(0, filename, "", "");
187*2775Sraf 	errlog(BEGIN, "xlator_startfile() {");
188*2775Sraf 	Statistics.files++;
189*2775Sraf 	db_set_current_file(filename);
190*2775Sraf 	errlog(TRACING, "now in file \"%s\" in lib \"%s\"",
191*2775Sraf 		filename, lib);
192*2775Sraf 
193*2775Sraf 	/* Generate filenames. */
194*2775Sraf 	(void) snprintf(infile, sizeof (infile), "%s", filename);
195*2775Sraf 	(void) snprintf(outfile, sizeof (outfile), "%s.c",
196*2775Sraf 		db_get_output_file());
197*2775Sraf 
198*2775Sraf 	/* Open .c file. */
199*2775Sraf 	if (open_code_file() == NO) {
200*2775Sraf 		rc = ERROR_RC;
201*2775Sraf 	}
202*2775Sraf 
203*2775Sraf 	generate_init(); /* Write stuff to the c file. */
204*2775Sraf 	symtab_clear_includes(); /* Clear out the per-file data. */
205*2775Sraf 	errlog(END, "}");
206*2775Sraf 	return (rc);
207*2775Sraf }
208*2775Sraf 
209*2775Sraf /*
210*2775Sraf  * xlator_start_if -- tritto, called on starting each new
211*2775Sraf  *	interface in the spec file.
212*2775Sraf  */
213*2775Sraf int
xlator_start_if(const Meta_info m,int const token,char * value)214*2775Sraf xlator_start_if(const Meta_info m, int const token, char *value)
215*2775Sraf {
216*2775Sraf 	char ifname[BUFSIZ];
217*2775Sraf 	char *kw;
218*2775Sraf 
219*2775Sraf 	switch (token) {
220*2775Sraf 	case XLATOR_KW_FUNC:
221*2775Sraf 		kw = "Function";
222*2775Sraf 		break;
223*2775Sraf 	case XLATOR_KW_DATA:
224*2775Sraf 		kw = "Data";
225*2775Sraf 		break;
226*2775Sraf 	default:
227*2775Sraf 		/* This should never happen */
228*2775Sraf 		errlog(ERROR,
229*2775Sraf 		    "\"%s\", line %d: Implementation error! "
230*2775Sraf 		    "Please file a bug\n", __FILE__, __LINE__);
231*2775Sraf 		return (XLATOR_FATAL);
232*2775Sraf 	}
233*2775Sraf 
234*2775Sraf 	seterrline(LINE, m.mi_filename, kw, value);
235*2775Sraf 	errlog(BEGIN, "xlator_start_if() {");
236*2775Sraf 
237*2775Sraf /*
238*2775Sraf  * XXX Note whether interface is function or data in some state data item.
239*2775Sraf  * We'll need it later when writing interceptors.
240*2775Sraf  */
241*2775Sraf 
242*2775Sraf 	Statistics.interfaces++;
243*2775Sraf 	(void) strpqcpy(ifname, value, nextsep2(value));
244*2775Sraf 	if (*ifname == '\0') {
245*2775Sraf 		errlog(INPUT|ERROR|FATAL,
246*2775Sraf 		    "missing argument in \"%s\" line", kw);
247*2775Sraf 	}
248*2775Sraf 	db_set_current_interface(ifname);
249*2775Sraf 	errlog(VERBOSE, "interface='%s'", value);
250*2775Sraf 	if (token == XLATOR_KW_DATA) {
251*2775Sraf 		Statistics.skips++;
252*2775Sraf 		errlog(VERBOSE, "telling front end to skip '%s'", value);
253*2775Sraf 		errlog(END, "}");
254*2775Sraf 		return (SKIP_RC); /* Tell front end to skip it for us. */
255*2775Sraf 	}
256*2775Sraf 
257*2775Sraf 	errlog(TRACING, "now in interface \"%s\"", value);
258*2775Sraf 
259*2775Sraf 	symtab_new_function(m.mi_line_number, m.mi_filename);
260*2775Sraf 		/* Also cleans junk out of symbol table. */
261*2775Sraf 	errlog(END, "}");
262*2775Sraf 	return (SUCCESS_RC);
263*2775Sraf }
264*2775Sraf 
265*2775Sraf /*
266*2775Sraf  * xlator_take_kvpair -- the primary call: collect a datum provide by the
267*2775Sraf  *	front-end wrapper.
268*2775Sraf  */
269*2775Sraf int
xlator_take_kvpair(Meta_info m,int const token,char * value)270*2775Sraf xlator_take_kvpair(Meta_info m, int const token, char *value)
271*2775Sraf {
272*2775Sraf 	int retval;
273*2775Sraf 	char *key = Keywords[token-FIRST_TOKEN].key;
274*2775Sraf 
275*2775Sraf 	int line = LINE; /* TBD */
276*2775Sraf 	symtab_set_filename(m.mi_filename);
277*2775Sraf 
278*2775Sraf 	value = strnormalize(value);
279*2775Sraf 
280*2775Sraf 	seterrline(line, m.mi_filename, key, value);
281*2775Sraf 	errlog(BEGIN, "xlator_take_kvpair() {");
282*2775Sraf 	Statistics.lines++;
283*2775Sraf 	errlog(VERBOSE, "key='%s', value='%s'",
284*2775Sraf 	    (key) ? key : "<nil>",
285*2775Sraf 	    (value) ? value : "<nil>");
286*2775Sraf 	switch (token) {
287*2775Sraf 	case XLATOR_KW_DECL:
288*2775Sraf 
289*2775Sraf 	/*
290*2775Sraf 	 * XXX Check state item to see that it is a function,
291*2775Sraf 	 * else do not emit interceptor
292*2775Sraf 	 */
293*2775Sraf 		symtab_clear_function(); /* Always use last one. */
294*2775Sraf 		errlog(END, "}");
295*2775Sraf 		retval = collect_prototype(value, line, m.mi_ext_cnt);
296*2775Sraf 		break;
297*2775Sraf 
298*2775Sraf 	case XLATOR_KW_INCL:
299*2775Sraf 		errlog(END, "}"); /* Use union of all includes. */
300*2775Sraf 		retval = collect_include(value, line);
301*2775Sraf 		if (retval == ERROR_RC) {
302*2775Sraf 			errlog(FATAL|INPUT, "Bad include line in spec file");
303*2775Sraf 		}
304*2775Sraf 		break;
305*2775Sraf 
306*2775Sraf 	case XLATOR_KW_EXCP:
307*2775Sraf 		symtab_clear_exception(); /* Always use last. */
308*2775Sraf 		retval = collect_binding(token, value, line);
309*2775Sraf 		break;
310*2775Sraf 
311*2775Sraf 	case XLATOR_KW_ERRNO:
312*2775Sraf 		symtab_clear_errval(); /* Always use last. */
313*2775Sraf 		retval = collect_errval("errno", line);
314*2775Sraf 		break;
315*2775Sraf 
316*2775Sraf 	case XLATOR_KW_ERRVAL:
317*2775Sraf 		symtab_clear_errval(); /* Always use last. */
318*2775Sraf 		retval =  collect_errval(value, line);
319*2775Sraf 		break;
320*2775Sraf 
321*2775Sraf 	case XLATOR_KW_ARCH:
322*2775Sraf 		retval = collect_arch(value);
323*2775Sraf 		break;
324*2775Sraf 
325*2775Sraf 	case XLATOR_KW_WEAK:
326*2775Sraf 		if (m.mi_extended == 1) {
327*2775Sraf 			errlog(ERROR, "\"%s\", line %d: "
328*2775Sraf 			    "Warning: Cannot use extends with a weak "
329*2775Sraf 			    "interface",
330*2775Sraf 			    m.mi_filename,
331*2775Sraf 			    m.mi_line_number);
332*2775Sraf 		}
333*2775Sraf 		retval = SUCCESS_RC;
334*2775Sraf 		break;
335*2775Sraf 	default:
336*2775Sraf 		retval = ERROR_RC;
337*2775Sraf 	}
338*2775Sraf 
339*2775Sraf 	errlog(END, "}");
340*2775Sraf 
341*2775Sraf 	return (retval);
342*2775Sraf }
343*2775Sraf 
344*2775Sraf /*
345*2775Sraf  * xlator_end_if -- called at the end of the interface, to trigger
346*2775Sraf  *	per-interface processing now entire thing has been seen.
347*2775Sraf  */
348*2775Sraf /*ARGSUSED*/
349*2775Sraf int
xlator_end_if(const Meta_info m,char const * value)350*2775Sraf xlator_end_if(const Meta_info m, char const *value)
351*2775Sraf {
352*2775Sraf 	seterrline(LINE, m.mi_filename, "end", value);
353*2775Sraf 	errlog(BEGIN, "xlator_end_if() {");
354*2775Sraf 	if (symtab_get_skip() == YES) {
355*2775Sraf 		symtab_set_skip(NO);
356*2775Sraf 		Statistics.skips++;
357*2775Sraf 	} else {
358*2775Sraf 		generate_interface();
359*2775Sraf 	}
360*2775Sraf 	errlog(END, "}");
361*2775Sraf 	return (SUCCESS_RC);
362*2775Sraf }
363*2775Sraf 
364*2775Sraf /*
365*2775Sraf  * xlator_endfile -- called at the end of the file, to trigger per-file
366*2775Sraf  * processing.
367*2775Sraf  */
368*2775Sraf int
xlator_endfile(void)369*2775Sraf xlator_endfile(void)
370*2775Sraf {
371*2775Sraf 	errlog(BEGIN, "xlator_endfile() {");
372*2775Sraf 
373*2775Sraf 	generate_closedown();
374*2775Sraf 	errlog(END, "}");
375*2775Sraf 	return ((commit_code_file() == YES)? SUCCESS_RC: ERROR_RC);
376*2775Sraf }
377*2775Sraf 
378*2775Sraf /*
379*2775Sraf  * xlator_endlib -- ditto, at the end of the library.
380*2775Sraf  */
381*2775Sraf int
xlator_endlib(void)382*2775Sraf xlator_endlib(void)
383*2775Sraf {
384*2775Sraf 	errlog(BEGIN, "xlator_endlib() {");
385*2775Sraf 	errlog(END, "}");
386*2775Sraf 	return (SUCCESS_RC);
387*2775Sraf }
388*2775Sraf 
389*2775Sraf /*
390*2775Sraf  * xlator_end -- the end of the processing, called so translator
391*2775Sraf  *	can do cleanup, write makefiles, etc.
392*2775Sraf  */
393*2775Sraf int
xlator_end(void)394*2775Sraf xlator_end(void)
395*2775Sraf {
396*2775Sraf 	int	rc = SUCCESS_RC;
397*2775Sraf 
398*2775Sraf 	errlog(BEGIN, "xlator_end() {");
399*2775Sraf 	rc += !generate_aux_file();
400*2775Sraf 	stats_report();
401*2775Sraf 	errlog(END, "}");
402*2775Sraf 	return (rc);
403*2775Sraf }
404*2775Sraf 
405*2775Sraf 
406*2775Sraf /*
407*2775Sraf ** utilities for this layer/phase only.
408*2775Sraf */
409*2775Sraf 
410*2775Sraf /*
411*2775Sraf  * stats_init -- note what time it is...
412*2775Sraf  */
413*2775Sraf static void
stats_init(void)414*2775Sraf stats_init(void)
415*2775Sraf {
416*2775Sraf 	Statistics.start = time(NULL);
417*2775Sraf }
418*2775Sraf 
419*2775Sraf /*
420*2775Sraf  * stats_report -- say how much we just did
421*2775Sraf  */
422*2775Sraf #define	max(a, b) (a > b)? a: b
423*2775Sraf 
424*2775Sraf static void
stats_report(void)425*2775Sraf stats_report(void)
426*2775Sraf {
427*2775Sraf 	double	seconds;
428*2775Sraf 
429*2775Sraf 	Statistics.end = time(NULL);
430*2775Sraf 	seconds = difftime(Statistics.end, Statistics.start);
431*2775Sraf 
432*2775Sraf 	switch (Verbose) {
433*2775Sraf 	default:
434*2775Sraf 		/*FALLTHROUGH*/
435*2775Sraf 	case 1:
436*2775Sraf 		(void) fprintf(stderr, "Statistics:\n"
437*2775Sraf 		    "    %d libraries\n    %d files\n"
438*2775Sraf 		    "    %d interfaces\n    %d lines\n"
439*2775Sraf 		    "    %d errors\n    %d warnings\n"
440*2775Sraf 		    "    %d skips\n"
441*2775Sraf 		    "in %.0f seconds, at %.1f lines/minute.\n",
442*2775Sraf 		    Statistics.libraries, Statistics.files,
443*2775Sraf 		    Statistics.interfaces, Statistics.lines,
444*2775Sraf 		    Statistics.errors, Statistics.warnings,
445*2775Sraf 		    Statistics.skips,
446*2775Sraf 		    seconds, Statistics.lines*60.0/seconds);
447*2775Sraf 		break;
448*2775Sraf 	case 0:
449*2775Sraf 		if (Statistics.errors != 0 || Statistics.warnings != 0) {
450*2775Sraf 			(void) fprintf(stderr,
451*2775Sraf 			    "spec2trace: %d errors %d warnings.\n",
452*2775Sraf 			    Statistics.errors, Statistics.warnings);
453*2775Sraf 		}
454*2775Sraf 		break;
455*2775Sraf 	}
456*2775Sraf }
457*2775Sraf 
458*2775Sraf 
459*2775Sraf /*
460*2775Sraf  * Tiny stats class...
461*2775Sraf  */
462*2775Sraf void
stats_add_warning(void)463*2775Sraf stats_add_warning(void)
464*2775Sraf {
465*2775Sraf 	Statistics.warnings++;
466*2775Sraf }
467*2775Sraf 
468*2775Sraf void
stats_add_error(void)469*2775Sraf stats_add_error(void)
470*2775Sraf {
471*2775Sraf 	Statistics.errors++;
472*2775Sraf }
473*2775Sraf 
474*2775Sraf /*
475*2775Sraf  * collect_includes -- collect a global list of include files,
476*2775Sraf  *	converting the comma- or space-separated input list into a
477*2775Sraf  *	structure for the database to store.
478*2775Sraf  *	As this can cause problems will ill-structured
479*2775Sraf  *	files, there is a mechanism to allow exclusion of
480*2775Sraf  *	certain files, (or certain combinations).  At
481*2775Sraf  *	the moment, the mechanism is TBD, as is the second arg.
482*2775Sraf  */
483*2775Sraf /*ARGSUSED1*/
484*2775Sraf int
collect_include(char * p,int line)485*2775Sraf collect_include(char *p, int line)
486*2775Sraf {
487*2775Sraf 	char	*include;
488*2775Sraf 	int	len;
489*2775Sraf 
490*2775Sraf 	errlog(BEGIN, "collect_include() {");
491*2775Sraf 	if ((include = strtok(p, ", ")) != NULL) {
492*2775Sraf 		for (; include != NULL; include = strtok(NULL, ", ")) {
493*2775Sraf 			include  = skipb(include);
494*2775Sraf 
495*2775Sraf 			/*
496*2775Sraf 			 * Make sure the include file's name
497*2775Sraf 			 * has legitimate C syntax - i.e. it's in double
498*2775Sraf 			 * quotes or angle brackets.
499*2775Sraf 			 */
500*2775Sraf 			if (*include != '"' && *include != '<')
501*2775Sraf 				return (ERROR_RC);
502*2775Sraf 
503*2775Sraf 			len = strlen(include);
504*2775Sraf 
505*2775Sraf 			if (include[len-1] != '"' && include[len-1] != '>')
506*2775Sraf 				return (ERROR_RC);
507*2775Sraf 
508*2775Sraf 			/*
509*2775Sraf 			 * If include filename syntax is OK, add it to
510*2775Sraf 			 * the list
511*2775Sraf 			 */
512*2775Sraf 			symtab_add_includes(include);
513*2775Sraf 		}
514*2775Sraf 	}
515*2775Sraf 	errlog(END, "}");
516*2775Sraf 	return (SUCCESS_RC);
517*2775Sraf }
518*2775Sraf 
519*2775Sraf /*
520*2775Sraf  * collect_binding -- take a binding and stuff it into the database
521*2775Sraf  *	in canonical form (with the word return in it).
522*2775Sraf  */
523*2775Sraf int
collect_binding(int const token,char * value,int line)524*2775Sraf collect_binding(int const token, char *value, int line)
525*2775Sraf {
526*2775Sraf 	char	*file = db_get_current_file();
527*2775Sraf 
528*2775Sraf 	errlog(BEGIN, "collect_binding() {");
529*2775Sraf 	errlog(VERBOSE, "name=\"%s\", value=\"%s\", line=%d\n",
530*2775Sraf 	    Keywords[token-FIRST_TOKEN].key, value, line);
531*2775Sraf 
532*2775Sraf 	if (token == XLATOR_KW_EXCP) {
533*2775Sraf 		symtab_set_exception(value, line, file);
534*2775Sraf 	} else {
535*2775Sraf 		errlog(FATAL|INPUT, "programmer error: impossible binding.");
536*2775Sraf 	}
537*2775Sraf 	errlog(END, "}");
538*2775Sraf 	return (SUCCESS_RC);
539*2775Sraf }
540*2775Sraf 
541*2775Sraf /*
542*2775Sraf  * collect_errval -- collect the error variable name (only)
543*2775Sraf  *	from the line.  This is expected to be the first
544*2775Sraf  *	or only thing in a space- or comma-separated list.
545*2775Sraf  *	Collecting errno/errval possible value is left TBD.
546*2775Sraf  */
547*2775Sraf int
collect_errval(char * p,int line)548*2775Sraf collect_errval(char *p, int line)
549*2775Sraf {
550*2775Sraf 	char	*name;
551*2775Sraf 
552*2775Sraf 	errlog(BEGIN, "collect_errval() {");
553*2775Sraf 	name = strtok(p, " \t\n\r");
554*2775Sraf 	symtab_set_errval(name, line, db_get_current_file(), "int", "int", 0);
555*2775Sraf 	errlog(END, "}");
556*2775Sraf 	return (SUCCESS_RC);
557*2775Sraf }
558*2775Sraf 
559*2775Sraf /*
560*2775Sraf  * collect_arch -- collect architecture.
561*2775Sraf  */
562*2775Sraf int
collect_arch(char * value)563*2775Sraf collect_arch(char *value)
564*2775Sraf {
565*2775Sraf 	char const	*arch = db_get_arch();
566*2775Sraf 	char	*buf, *p;
567*2775Sraf 	char	*t;
568*2775Sraf 
569*2775Sraf 	errlog(BEGIN, "collect_arch() {");
570*2775Sraf 	if (value == 0 || *value == '\0')
571*2775Sraf 		errlog(FATAL|INPUT, "No architectures defined in ARCH line");
572*2775Sraf 
573*2775Sraf 	if ((buf = strdup(value)) == NULL)
574*2775Sraf 		errlog(FATAL, "Could not allocate memory in ARCH directive");
575*2775Sraf 
576*2775Sraf 	t = buf;
577*2775Sraf 	while ((p = strtok(t, " \r\t\n")) != NULL) {
578*2775Sraf 		if (strcmp(p, arch) == 0 || strcmp(p, "all") == 0)
579*2775Sraf 			goto cleanup;
580*2775Sraf 		t = NULL;
581*2775Sraf 	}
582*2775Sraf 	symtab_set_skip(YES);
583*2775Sraf 
584*2775Sraf cleanup:
585*2775Sraf 	free(buf);
586*2775Sraf 	return (SUCCESS_RC);
587*2775Sraf }
588*2775Sraf 
589*2775Sraf /*
590*2775Sraf  * de_const -- get rid of const meta-types. This is actually a
591*2775Sraf  *	dodge to avoid writing a base-type function early in the
592*2775Sraf  *	process. This may turn into to_basetype() or to_primitivetype().
593*2775Sraf  */
594*2775Sraf static char *
de_const(char * type)595*2775Sraf de_const(char *type)
596*2775Sraf {
597*2775Sraf 	char *p, *q;
598*2775Sraf 	int i;
599*2775Sraf 
600*2775Sraf 	p = skipb(type);
601*2775Sraf 
602*2775Sraf 	q = strstr(type, "const");
603*2775Sraf 	if (q > p) {
604*2775Sraf 		for (i = 0; i < 5; i++) {
605*2775Sraf 			*q++ = '\0';
606*2775Sraf 		}
607*2775Sraf 		(void) sprintf(type, "%s%s", strnormalize(p), q);
608*2775Sraf 		return (type);
609*2775Sraf 	} else if (p == q) {
610*2775Sraf 		return (skipb(nextsep(p)));
611*2775Sraf 	} else {
612*2775Sraf 		return (type);
613*2775Sraf 	}
614*2775Sraf 
615*2775Sraf }
616*2775Sraf 
617*2775Sraf /*
618*2775Sraf  * to_basetype -- convert a C type declaration into its base type and return
619*2775Sraf  * 	the number of levels of indirection.
620*2775Sraf  *	Destructive and eats ``const''.
621*2775Sraf  */
622*2775Sraf static int
to_basetype(char * str)623*2775Sraf to_basetype(char *str)
624*2775Sraf {
625*2775Sraf 	char	*p = str,
626*2775Sraf 		buffer[MAXLINE+1],
627*2775Sraf 		*q = &buffer[0];
628*2775Sraf 	int	levels = 0;
629*2775Sraf 
630*2775Sraf 	assert(strlen(str) < MAXLINE, "string exceeded MAXLINE");
631*2775Sraf 	buffer[0] = NULL;
632*2775Sraf 	for (; *p != NULL; p++) {
633*2775Sraf 		switch (*p) {
634*2775Sraf 		case ' ': /* Convert spaces to single ' '. */
635*2775Sraf 			if (*(q-1) != ' ')
636*2775Sraf 				*q++ = ' ';
637*2775Sraf 			break;
638*2775Sraf 		case '*': /* Convert * to _P. */
639*2775Sraf 			if (*(q-1) != ' ')
640*2775Sraf 				*q++ = ' ';
641*2775Sraf 			levels++;
642*2775Sraf 			break;
643*2775Sraf 		case 'c': /* This might be a const */
644*2775Sraf 			if (strncmp(p, "const", 5) == 0) {
645*2775Sraf 				p += 4;
646*2775Sraf 			} else {
647*2775Sraf 				*q++ = *p;
648*2775Sraf 			}
649*2775Sraf 			break;
650*2775Sraf 		default:
651*2775Sraf 			/* Otherwise just copy. */
652*2775Sraf 			*q++ = *p;
653*2775Sraf 			break;
654*2775Sraf 		}
655*2775Sraf 		*q = NULL;
656*2775Sraf 	}
657*2775Sraf 	assert(q < &buffer[MAXLINE], "q fell off end of buffer");
658*2775Sraf 	q--;
659*2775Sraf 	while (*q == ' ') {
660*2775Sraf 		*q-- = NULL;
661*2775Sraf 	}
662*2775Sraf 	assert(strlen(buffer) < MAXLINE, "buffer length exceeded MAXLINE");
663*2775Sraf 	(void) strcpy(str, buffer);
664*2775Sraf 	return (levels);
665*2775Sraf }
666*2775Sraf 
667*2775Sraf /*
668*2775Sraf  * to_actual -- create an actual-argument list for use
669*2775Sraf  *	when calling the function.
670*2775Sraf  */
671*2775Sraf static char *
to_actual(void)672*2775Sraf to_actual(void)
673*2775Sraf {
674*2775Sraf 	ENTRY	*p;
675*2775Sraf 	static char buffer[MAXLINE+1];
676*2775Sraf 	int	n;
677*2775Sraf 
678*2775Sraf 	*buffer = NULL;
679*2775Sraf 	if ((p = symtab_get_first_arg()) != NULL) {
680*2775Sraf 		n = MAXLINE - snprintf(buffer, MAXLINE, "%s", name_of(p));
681*2775Sraf 		for (p = symtab_get_next_arg(); p != NULL;
682*2775Sraf 						p = symtab_get_next_arg()) {
683*2775Sraf 			if (*name_of(p) != NULL)
684*2775Sraf 				n -= snprintf(strend(buffer), n,
685*2775Sraf 					", %s", name_of(p));
686*2775Sraf 		}
687*2775Sraf 	}
688*2775Sraf 	return (buffer);
689*2775Sraf }
690*2775Sraf 
691*2775Sraf /*
692*2775Sraf  * strpqcpy -- string copy that takes whatever begins with p and ends
693*2775Sraf  *	just before q.
694*2775Sraf  */
695*2775Sraf static char *
strpqcpy(char * target,char * p,char * q)696*2775Sraf strpqcpy(char *target, char *p, char *q)
697*2775Sraf {
698*2775Sraf 	char	saved;
699*2775Sraf 
700*2775Sraf 	saved = *q;
701*2775Sraf 	*q = NULL;
702*2775Sraf 	(void) strcpy(target, p);
703*2775Sraf 	*q = saved;
704*2775Sraf 	return (target);
705*2775Sraf }
706*2775Sraf 
707*2775Sraf #ifndef lint
708*2775Sraf int
breakpoint(void)709*2775Sraf breakpoint(void)
710*2775Sraf {
711*2775Sraf 	return (0);
712*2775Sraf }
713*2775Sraf #endif
714*2775Sraf 
715*2775Sraf 
716*2775Sraf int
collect_prototype(char * p,int line,int extcnt)717*2775Sraf collect_prototype(char *p, int line, int extcnt)
718*2775Sraf {
719*2775Sraf 	char	f_type[BUFSIZ];	/* The function. */
720*2775Sraf 	char	f_basetype[BUFSIZ];
721*2775Sraf 	char	f_name[BUFSIZ];
722*2775Sraf 	char	a_name[BUFSIZ];	/* The arguments. */
723*2775Sraf 	char	a_basetype[BUFSIZ];
724*2775Sraf 	char	a_type[BUFSIZ];
725*2775Sraf 	char	*file = db_get_current_file();
726*2775Sraf 	char	*interface = db_get_current_interface();
727*2775Sraf 	char	*q;
728*2775Sraf 	char const *parse_err;
729*2775Sraf 	char	tmp_proto[BUFSIZ], buf[BUFSIZ];
730*2775Sraf 	decl_t	*pp, *funargs;
731*2775Sraf 	type_t	*tp;
732*2775Sraf 	int	levels, a_levels;
733*2775Sraf 
734*2775Sraf 	tmp_proto[BUFSIZ-1] = 0;
735*2775Sraf 	errlog(BEGIN, "collect_prototype() {");
736*2775Sraf 	if (p[strlen(p)-1] != ';')
737*2775Sraf 		(void) snprintf(tmp_proto, BUFSIZ, "%s;", p);
738*2775Sraf 	else
739*2775Sraf 		(void) snprintf(tmp_proto, BUFSIZ, "%s", p);
740*2775Sraf 
741*2775Sraf 	/* save prototype in symbol table */
742*2775Sraf 	symtab_set_prototype(p);
743*2775Sraf 
744*2775Sraf 	errlog(VERBOSE, "parsing prototype: %s\n", tmp_proto);
745*2775Sraf 
746*2775Sraf 	/* Parse Prototype */
747*2775Sraf 	if ((parse_err = decl_Parse(tmp_proto, &pp)) != NULL) {
748*2775Sraf 		errlog(FATAL|INPUT, "bad prototype: %s\n\t%s\n", parse_err, p);
749*2775Sraf 	}
750*2775Sraf 
751*2775Sraf 	if (extcnt == 0) {
752*2775Sraf 		char *dname = decl_GetName(pp);
753*2775Sraf 		if (strcmp(interface, dname) != 0)
754*2775Sraf 			errlog(FATAL|INPUT, "function and declaration"
755*2775Sraf 			    " name mismatch\nfunction name = %s,"
756*2775Sraf 			    " declaration name = %s\n", interface,
757*2775Sraf 			    dname);
758*2775Sraf 	}
759*2775Sraf 
760*2775Sraf 	tp = decl_GetType(pp);
761*2775Sraf 
762*2775Sraf 	if (type_IsPtrFun(tp)) {
763*2775Sraf 		errlog(FATAL|INPUT, "function %s is declared as a data item"
764*2775Sraf 		    " (pointer to function)\n", interface);
765*2775Sraf 	} else if (!type_IsFunction(tp)) {
766*2775Sraf 		errlog(FATAL|INPUT, "function %s is declared as a data item",
767*2775Sraf 		    interface);
768*2775Sraf 	}
769*2775Sraf 
770*2775Sraf 	if (type_IsVarargs(tp)) {
771*2775Sraf 		symtab_set_skip(YES);
772*2775Sraf 		decl_Destroy(pp);
773*2775Sraf 		return (SUCCESS_RC);
774*2775Sraf 	}
775*2775Sraf 
776*2775Sraf 	decl_GetTraceInfo(pp, f_type, f_basetype, &funargs);
777*2775Sraf 	(void) sprintf(buf, "%s", strnormalize(f_type));
778*2775Sraf 	(void) strcpy(f_type, buf);
779*2775Sraf 	(void) sprintf(buf, "%s", strnormalize(f_basetype));
780*2775Sraf 	(void) strcpy(f_basetype, buf);
781*2775Sraf 	levels = to_basetype(f_basetype);
782*2775Sraf 
783*2775Sraf 	/* get interface name from 'Begin' line */
784*2775Sraf 	(void) strpqcpy(f_name, interface, nextsep(interface));
785*2775Sraf 	(void) decl_SetName(pp, f_name);
786*2775Sraf 
787*2775Sraf 	errlog(VERBOSE, "f_name=%s, f_basetype=%s, f_type=%s\n",
788*2775Sraf 		f_name, f_basetype, f_type);
789*2775Sraf 
790*2775Sraf 	symtab_set_function(f_name, line, file, f_type, f_basetype, levels);
791*2775Sraf 
792*2775Sraf 	db_add_print_types(f_basetype,
793*2775Sraf 	    (q = de_const(type_of(symtab_get_function()))));
794*2775Sraf 
795*2775Sraf 	symtab_add_print_types(f_basetype, q);
796*2775Sraf 
797*2775Sraf 	/* args list */
798*2775Sraf 	while (funargs) {
799*2775Sraf 		(void) snprintf(a_type, BUFSIZ, "%s ",
800*2775Sraf 			strnormalize(declspec_ToString(buf, funargs->d_ds)));
801*2775Sraf 		(void) snprintf(a_basetype, BUFSIZ, "%s",
802*2775Sraf 			strnormalize(de_const(declspec_ToString(buf,
803*2775Sraf 			funargs->d_ds))));
804*2775Sraf 
805*2775Sraf 		tp = funargs->d_type;
806*2775Sraf 
807*2775Sraf 		for (a_levels = 0; tp; ) {
808*2775Sraf 			if (tp->t_dt == DD_PTR || tp->t_dt == DD_ARY) {
809*2775Sraf 				(void) strcat(a_type, "*");
810*2775Sraf 				a_levels++;
811*2775Sraf 			}
812*2775Sraf 			tp = tp->t_next;
813*2775Sraf 		}
814*2775Sraf 
815*2775Sraf 		/*
816*2775Sraf 		 * XXX: This is a hack to work around bug in yacc parser
817*2775Sraf 		 *  "int foo(void)" prototypes get interpreted as having 1
818*2775Sraf 		 *  argument with the d_name of the argument being NULL.
819*2775Sraf 		 */
820*2775Sraf 		if (funargs->d_name) {
821*2775Sraf 			(void) snprintf(a_name, 20, "%s", funargs->d_name);
822*2775Sraf 
823*2775Sraf 			errlog(VERBOSE,
824*2775Sraf 			    "a_name = %s, a_basetype = %s, a_type = %s\n",
825*2775Sraf 			    a_name, a_basetype, a_type);
826*2775Sraf 
827*2775Sraf 			symtab_add_args(a_name, line, file,
828*2775Sraf 			    a_type, a_basetype, a_levels);
829*2775Sraf 			db_add_print_types(a_basetype,
830*2775Sraf 			    q = de_const(type_of(symtab_get_last_arg())));
831*2775Sraf 			symtab_add_print_types(a_basetype, q);
832*2775Sraf 		}
833*2775Sraf 
834*2775Sraf 		funargs = funargs->d_next;
835*2775Sraf 	}
836*2775Sraf 	symtab_set_formals(decl_ToFormal(pp));
837*2775Sraf 	symtab_set_actuals(to_actual());
838*2775Sraf 
839*2775Sraf 	symtab_set_cast(decl_ToString(buf, DTS_CAST, pp, NULL));
840*2775Sraf 
841*2775Sraf 	decl_Destroy(pp);
842*2775Sraf 
843*2775Sraf 	errlog(END, "}");
844*2775Sraf 	return (SUCCESS_RC);
845*2775Sraf }
846*2775Sraf 
847*2775Sraf 
848*2775Sraf /*
849*2775Sraf  * generators
850*2775Sraf  */
851*2775Sraf 
852*2775Sraf /*
853*2775Sraf  * generate_init -- prime the code generator as required.
854*2775Sraf  */
855*2775Sraf static void
generate_init(void)856*2775Sraf generate_init(void)
857*2775Sraf {
858*2775Sraf 	errlog(BEGIN, "generate_init() {");
859*2775Sraf 
860*2775Sraf 	(void) fprintf(Headfp,
861*2775Sraf 	    "/*\n"
862*2775Sraf 	    " * Generated by spec2trace %s: do not edit this file.\n */\n\n",
863*2775Sraf 	    TRACE_VERSION);
864*2775Sraf 
865*2775Sraf 	(void) fprintf(Headfp,
866*2775Sraf 	    "#ifndef true\n"
867*2775Sraf 	    "#define\ttrue 1\n"
868*2775Sraf 	    "#define\tfalse 0\n"
869*2775Sraf 	    "#endif\n\n"
870*2775Sraf 	    "static char const *oparen = \"(\";\n"
871*2775Sraf 	    "static char const *retstr = \"  return = \";\n"
872*2775Sraf 	    "static char const *errnostr = \" errno = \";\n"
873*2775Sraf 	    "static char const *nilstr = \"<nil>\";\n"
874*2775Sraf 	    "\n");
875*2775Sraf 
876*2775Sraf 	errlog(END, "}");
877*2775Sraf }
878*2775Sraf 
879*2775Sraf 
880*2775Sraf /*
881*2775Sraf  * generate_interface -- call the two main parts of the per-interface
882*2775Sraf  *	code generation.
883*2775Sraf  */
884*2775Sraf static void
generate_interface(void)885*2775Sraf generate_interface(void)
886*2775Sraf {
887*2775Sraf 	ENTRY	*function = symtab_get_function();
888*2775Sraf 
889*2775Sraf 	errlog(BEGIN, "generate_interface() {");
890*2775Sraf 	/* Check for required information. */
891*2775Sraf 	if (validity_of(function) == NO) {
892*2775Sraf 		symtab_set_skip(YES);
893*2775Sraf 		errlog(WARNING|INPUT, "no prototype for interface "
894*2775Sraf 			"it will be skipped");
895*2775Sraf 		errlog(END, "}");
896*2775Sraf 		return;
897*2775Sraf 	}
898*2775Sraf 
899*2775Sraf 	/* Generate the current interface 's print-functions declarations. */
900*2775Sraf 	generate_print_declarations(Bodyfp);
901*2775Sraf 
902*2775Sraf 	/* Generate the linkage part (a function and a struct */
903*2775Sraf 	generate_linkage(function);
904*2775Sraf 
905*2775Sraf 	/* Generate the actual interceptor. */
906*2775Sraf 	generate_interceptor(function);
907*2775Sraf 	errlog(END, "}");
908*2775Sraf }
909*2775Sraf 
910*2775Sraf 
911*2775Sraf /*
912*2775Sraf  * generate_closedown -- produce includes.
913*2775Sraf  */
914*2775Sraf static void
generate_closedown(void)915*2775Sraf generate_closedown(void)
916*2775Sraf {
917*2775Sraf 	errlog(BEGIN, "generate_closedown() {");
918*2775Sraf 
919*2775Sraf 	/* Print includes to primary file. */
920*2775Sraf 	generate_includes();
921*2775Sraf 	(void) putc('\n', Headfp);
922*2775Sraf 	errlog(END, "}");
923*2775Sraf }
924*2775Sraf 
925*2775Sraf /*
926*2775Sraf  * generate_aux_file -- generate one additional .pf file with
927*2775Sraf  *	print-function pointers.
928*2775Sraf  */
929*2775Sraf static int
generate_aux_file(void)930*2775Sraf generate_aux_file(void)
931*2775Sraf {
932*2775Sraf 	FILE	*fp;
933*2775Sraf 	char	pathname[MAXLINE];
934*2775Sraf 
935*2775Sraf 	errlog(BEGIN, "generate_aux_file() {");
936*2775Sraf 	/* Open file */
937*2775Sraf 	(void) snprintf(pathname, sizeof (pathname), "%s.pf",
938*2775Sraf 		db_get_output_file());
939*2775Sraf 	errlog(TRACING,  "output file = '%s'", pathname);
940*2775Sraf 	if ((fp = fopen(pathname, "w")) == NULL) {
941*2775Sraf 		errlog(FATAL, "%s: %s", pathname, strerror(errno));
942*2775Sraf 	}
943*2775Sraf 
944*2775Sraf 	/*
945*2775Sraf 	 * Declare and initialize all print function pointers to null.
946*2775Sraf 	 * Some spec files result in nothing being put into the .pf
947*2775Sraf 	 * file.  We must create the file since make(1) does not cope
948*2775Sraf 	 * well with absent files that it expects to have built.  So
949*2775Sraf 	 * now the build gets empty compilation unit warnings...  So
950*2775Sraf 	 * we unconditionally create a static pointer.
951*2775Sraf 	 */
952*2775Sraf 	(void) fprintf(fp,
953*2775Sraf 	    "/* Do not edit this file: it is a generated one. */\n\n"
954*2775Sraf 	    "static char const *__abi_place_holder;\n\n");
955*2775Sraf 
956*2775Sraf 	generate_print_definitions(fp);
957*2775Sraf 
958*2775Sraf 	/* Close file */
959*2775Sraf 	if (fclose(fp) != 0) {
960*2775Sraf 		errlog(FATAL, "fclose %s: %s", pathname, strerror(errno));
961*2775Sraf 	}
962*2775Sraf 	errlog(END, "}");
963*2775Sraf 	return (YES);
964*2775Sraf }
965*2775Sraf 
966*2775Sraf 
967*2775Sraf 
968*2775Sraf /*
969*2775Sraf  * generate_includes -- generate #includes to Headfp
970*2775Sraf  */
971*2775Sraf static void
generate_includes(void)972*2775Sraf generate_includes(void)
973*2775Sraf {
974*2775Sraf 	char	*include;
975*2775Sraf 
976*2775Sraf 	errlog(BEGIN, "generate_includes() {");
977*2775Sraf 	errlog(TRACING,  "includes=");
978*2775Sraf 	for (include = symtab_get_first_include(); include != NULL;
979*2775Sraf 	    include = symtab_get_next_include())
980*2775Sraf 		(void) fprintf(Headfp, "#include %s\n", include);
981*2775Sraf 
982*2775Sraf 	(void) fprintf(Headfp, "\n#include <stdio.h>\n"
983*2775Sraf 	    "#include <dlfcn.h>\n"
984*2775Sraf 	    "#include <apptrace.h>\n\n");
985*2775Sraf 
986*2775Sraf 	errlog(TRACING,  "\n");
987*2775Sraf 	errlog(END, "}");
988*2775Sraf }
989