17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include "parser.h"
327c478bd9Sstevel@tonic-gate #include "trace.h"
337c478bd9Sstevel@tonic-gate #include "util.h"
347c478bd9Sstevel@tonic-gate #include "db.h"
357c478bd9Sstevel@tonic-gate #include "symtab.h"
367c478bd9Sstevel@tonic-gate #include "io.h"
377c478bd9Sstevel@tonic-gate #include "printfuncs.h"
387c478bd9Sstevel@tonic-gate #include "errlog.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static int	 prepare_printf_part(ENTRY *, char *, char *, int);
417c478bd9Sstevel@tonic-gate static char	 *space_to_uscore(char const *);
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static char	arglist[_POSIX_ARG_MAX];
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * generate_printf -- make the cleanest possible printf for the
477c478bd9Sstevel@tonic-gate  *	parameters, in a relatively terse apptrace/dbx-like format,
487c478bd9Sstevel@tonic-gate  *	ending in ") = ", or ) if its a void function we're doing.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate void
generate_printf(ENTRY * f)517c478bd9Sstevel@tonic-gate generate_printf(ENTRY *f)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	ENTRY	*e;
547c478bd9Sstevel@tonic-gate 	char	*p, *name;
557c478bd9Sstevel@tonic-gate 	int	l, n;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "generate_printf");
587c478bd9Sstevel@tonic-gate 	(void) fprintf(Bodyfp, "    fprintf(ABISTREAM, \"");
597c478bd9Sstevel@tonic-gate 	p = &arglist[0];
607c478bd9Sstevel@tonic-gate 	l = (int)sizeof (arglist);
61*07c94cbfSToomas Soome 	*p = '\0';
627c478bd9Sstevel@tonic-gate 	for (e = symtab_get_first_arg(); e != NULL; e = symtab_get_next_arg()) {
637c478bd9Sstevel@tonic-gate 		errlog(TRACING, "arglist = '%s'", arglist);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 		if (is_void(e)) {
667c478bd9Sstevel@tonic-gate 			/* This placeholder means there are no real args. */
677c478bd9Sstevel@tonic-gate 			break;
687c478bd9Sstevel@tonic-gate 		}
697c478bd9Sstevel@tonic-gate 		/* Insert punctuation. */
707c478bd9Sstevel@tonic-gate 		if (p != &arglist[0]) {
717c478bd9Sstevel@tonic-gate 			(void) fprintf(Bodyfp, ", ");
727c478bd9Sstevel@tonic-gate 		}
73*07c94cbfSToomas Soome 		if (*(name =  name_of(e)) == '\0') {
747c478bd9Sstevel@tonic-gate 			/* It's a varargs indicator instead */
757c478bd9Sstevel@tonic-gate 			(void) fprintf(Bodyfp, "...");
767c478bd9Sstevel@tonic-gate 		} else {
777c478bd9Sstevel@tonic-gate 			(void) fprintf(Bodyfp, "%s = ", name);
787c478bd9Sstevel@tonic-gate 			n = prepare_printf_part(e, name, p, l);
797c478bd9Sstevel@tonic-gate 			l -= n;
807c478bd9Sstevel@tonic-gate 			p += n;
81*07c94cbfSToomas Soome 			*(p+1) = '\0';
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (is_void(f) || symtab_get_nonreturn() == YES) {
867c478bd9Sstevel@tonic-gate 		/* It is a function returning void, or a function */
877c478bd9Sstevel@tonic-gate 		/* which doesn't return. Close off args. */
887c478bd9Sstevel@tonic-gate 		(void) fprintf(Bodyfp, ")\"");
897c478bd9Sstevel@tonic-gate 	} else {
907c478bd9Sstevel@tonic-gate 		/* Make some more printf for the return type. */
917c478bd9Sstevel@tonic-gate 		(void) fprintf(Bodyfp, ") = ");
927c478bd9Sstevel@tonic-gate 		(void) prepare_printf_part(f, "_return", p, l);
937c478bd9Sstevel@tonic-gate 		(void) fprintf(Bodyfp, "\"");
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	(void) fprintf(Bodyfp, "%s);\n", arglist);
977c478bd9Sstevel@tonic-gate 	errlog(END, "}");
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * prepare_printf_part -- do one element of a printf/argument string,
1037c478bd9Sstevel@tonic-gate  *	for printing non-verbose parameter lists
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static int
prepare_printf_part(ENTRY * e,char * name,char * place,int size)1067c478bd9Sstevel@tonic-gate prepare_printf_part(ENTRY *e, char *name, char *place, int size)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	char	*bt;
1097c478bd9Sstevel@tonic-gate 	int	li;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "prepare_printf_part() {");
1127c478bd9Sstevel@tonic-gate 	errlog(TRACING, "name = '%s'", name);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	bt = basetype_of(e);
1157c478bd9Sstevel@tonic-gate 	li = levels_of(e);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (li == 1 && (strcmp(bt, "char") == 0)) {
1187c478bd9Sstevel@tonic-gate 		/* It's a string, print the beginning of it. */
1197c478bd9Sstevel@tonic-gate 		(void) fputs("\\\"%.*s\\\"", Bodyfp);
1207c478bd9Sstevel@tonic-gate 		size = snprintf(place, size,
1217c478bd9Sstevel@tonic-gate 		    /*CSTYLED*/
1227c478bd9Sstevel@tonic-gate 		    ",\n\tabi_strpsz, (%s) ? %s : nilstr",
1237c478bd9Sstevel@tonic-gate 		    name, name);
1247c478bd9Sstevel@tonic-gate 	} else {
1257c478bd9Sstevel@tonic-gate 		/* Just print a hex value */
1267c478bd9Sstevel@tonic-gate 		(void) fprintf(Bodyfp, "%s", "0x%p");
1277c478bd9Sstevel@tonic-gate 		size = snprintf(place, size, ", \n\t%s", name);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	errlog(TRACING, "place='%s'\n", place);
1317c478bd9Sstevel@tonic-gate 	errlog(END, "}");
1327c478bd9Sstevel@tonic-gate 	return (size);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * generate_printfunc_calls -- generate print commands for primitive types
1397c478bd9Sstevel@tonic-gate  *	and calls to print functions for composite types, cleanly.
1407c478bd9Sstevel@tonic-gate  *	Needs to know about base types of primitives, difference
1417c478bd9Sstevel@tonic-gate  *	between primitives and composite types: TBD.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate void
generate_printfunc_calls(ENTRY * f)1447c478bd9Sstevel@tonic-gate generate_printfunc_calls(ENTRY *f)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	ENTRY	*e;
1477c478bd9Sstevel@tonic-gate 	char	*name;
1487c478bd9Sstevel@tonic-gate 	char	*pf_str_name;
1497c478bd9Sstevel@tonic-gate 	int	li;
1507c478bd9Sstevel@tonic-gate 	char	*format;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "generate_printfunc_calls() {");
1537c478bd9Sstevel@tonic-gate 	for (e = symtab_get_first_arg(); e != NULL; e = symtab_get_next_arg()) {
1547c478bd9Sstevel@tonic-gate 		if (is_void(e)) {
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 		}
157*07c94cbfSToomas Soome 		if (*(name = name_of(e)) == '\0') {
1587c478bd9Sstevel@tonic-gate 			(void) fprintf(Bodyfp, "        fputs(\"  ...\\n\", "
1597c478bd9Sstevel@tonic-gate 				"ABISTREAM);\n");
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		errlog(TRACING, "name = '%s'\n", name);
1627c478bd9Sstevel@tonic-gate 		(void) fprintf(Bodyfp,
1637c478bd9Sstevel@tonic-gate 		    "        fprintf(ABISTREAM, \"  %s = \");\n",
1647c478bd9Sstevel@tonic-gate 		    name);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		pf_str_name = space_to_uscore(basetype_of(e));
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		/*
1697c478bd9Sstevel@tonic-gate 		 * If we're dealing with a scalar (non-pointer) then
1707c478bd9Sstevel@tonic-gate 		 * we need to call the printer with a &
1717c478bd9Sstevel@tonic-gate 		 */
1727c478bd9Sstevel@tonic-gate 		li = levels_of(e);
1737c478bd9Sstevel@tonic-gate 		if (li)
1747c478bd9Sstevel@tonic-gate 			format = "\tspf_prtype(ABISTREAM, pf_%s_str, %d, "
1757c478bd9Sstevel@tonic-gate 			    "(void const *)%s);\n";
1767c478bd9Sstevel@tonic-gate 		else
1777c478bd9Sstevel@tonic-gate 			format = "\tspf_prtype(ABISTREAM, pf_%s_str, %d, "
1787c478bd9Sstevel@tonic-gate 			    "(void const *)&%s);\n";
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		(void) fprintf(Bodyfp, format, pf_str_name, li, name);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		free(pf_str_name);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (is_void(f)) {
1867c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
1877c478bd9Sstevel@tonic-gate 	} else {
1887c478bd9Sstevel@tonic-gate 		pf_str_name = space_to_uscore(basetype_of(f));
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		li = levels_of(f);
1917c478bd9Sstevel@tonic-gate 		if (li)
1927c478bd9Sstevel@tonic-gate 			format = "\tspf_prtype(ABISTREAM, pf_%s_str, %d, "
1937c478bd9Sstevel@tonic-gate 			    "(void const *)_return);\n";
1947c478bd9Sstevel@tonic-gate 		else
1957c478bd9Sstevel@tonic-gate 			format = "\tspf_prtype(ABISTREAM, pf_%s_str, %d, "
1967c478bd9Sstevel@tonic-gate 			    "(void const *)&_return);\n";
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		(void) fputs("        fputs(retstr, ABISTREAM);\n", Bodyfp);
1997c478bd9Sstevel@tonic-gate 		(void) fprintf(Bodyfp, format, pf_str_name, li);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		free(pf_str_name);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	errlog(END, "}");
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Print Function Pointers -- definition, declaration and initialization.
2107c478bd9Sstevel@tonic-gate  *	Use is above...
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * generate_print_definitions -- generate variable definitions and
2157c478bd9Sstevel@tonic-gate  *	initialize them to NULL.
2167c478bd9Sstevel@tonic-gate  *      These will be set non-null by a lazy evaluation in the
2177c478bd9Sstevel@tonic-gate  *	main.c file if and only if the print function will be used.
2187c478bd9Sstevel@tonic-gate  *	All print functions which can be called must be defined.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate void
generate_print_definitions(FILE * fp)2217c478bd9Sstevel@tonic-gate generate_print_definitions(FILE *fp)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	char	*print_type,
2247c478bd9Sstevel@tonic-gate 		*c_type,
2257c478bd9Sstevel@tonic-gate 		*pf_str_name;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "generate_print_definitions() {");
2287c478bd9Sstevel@tonic-gate 	for (print_type = db_get_first_print_type();
2297c478bd9Sstevel@tonic-gate 		print_type != NULL;
2307c478bd9Sstevel@tonic-gate 			print_type = db_get_next_print_type()) {
2317c478bd9Sstevel@tonic-gate 		c_type = strchr(print_type, ','); /* Safe by construction. */
232*07c94cbfSToomas Soome 		*c_type++ = '\0';
2337c478bd9Sstevel@tonic-gate 		errlog(TRACING,  "print_type=%s\n", print_type);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		pf_str_name = space_to_uscore(print_type);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
2387c478bd9Sstevel@tonic-gate 		    "char const *pf_%s_str = \"%s\";\n",
2397c478bd9Sstevel@tonic-gate 		    pf_str_name, print_type);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		free(pf_str_name);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		*--c_type = ',';
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	errlog(END, "}");
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * generate_print_declarations -- generate variable declarations
2517c478bd9Sstevel@tonic-gate  *	for the strings that'll be used as arguments to the type
2527c478bd9Sstevel@tonic-gate  *	printing function.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate void
generate_print_declarations(FILE * fp)2557c478bd9Sstevel@tonic-gate generate_print_declarations(FILE *fp)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	char	*print_type,
2587c478bd9Sstevel@tonic-gate 		*c_type,
2597c478bd9Sstevel@tonic-gate 		*pf_str_name;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	errlog(BEGIN, "generate_print_declarations() {");
2627c478bd9Sstevel@tonic-gate 	for (print_type = symtab_get_first_print_type();
2637c478bd9Sstevel@tonic-gate 	    print_type != NULL;
2647c478bd9Sstevel@tonic-gate 	    print_type = symtab_get_next_print_type()) {
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		errlog(TRACING,  "print_type, c_type=%s\n", print_type);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		c_type = strchr(print_type, ','); /* Safe by construction. */
269*07c94cbfSToomas Soome 		*c_type++ = '\0';
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		pf_str_name = space_to_uscore(print_type);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "extern char const *pf_%s_str;\n",
2747c478bd9Sstevel@tonic-gate 		    pf_str_name);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		free(pf_str_name);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		*--c_type = ',';
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	errlog(END, "}");
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * is_void -- see if a type is void.
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate int
is_void(ENTRY * e)2887c478bd9Sstevel@tonic-gate is_void(ENTRY *e)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	if ((e != NULL) &&
2917c478bd9Sstevel@tonic-gate 	    levels_of(e) == 0 && (strcmp(basetype_of(e), "void") == 0))
2927c478bd9Sstevel@tonic-gate 		return (1);
2937c478bd9Sstevel@tonic-gate 	else
2947c478bd9Sstevel@tonic-gate 		return (0);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static char *
space_to_uscore(char const * str)2987c478bd9Sstevel@tonic-gate space_to_uscore(char const *str)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	char *strp, *p;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	strp = strdup(str);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	assert(strp != NULL, "strdup failed");
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	for (p = strp; *p != '\0'; p++)
3077c478bd9Sstevel@tonic-gate 		if (*p == ' ')
3087c478bd9Sstevel@tonic-gate 			*p = '_';
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	return (strp);
3117c478bd9Sstevel@tonic-gate }
312