1*6a074c93Sab#!/usr/bin/perl
2*6a074c93Sab
3*6a074c93Sab#
4*6a074c93Sab# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
5*6a074c93Sab# Use is subject to license terms.
6*6a074c93Sab#
7*6a074c93Sab# CDDL HEADER START
8*6a074c93Sab#
9*6a074c93Sab# The contents of this file are subject to the terms of the
10*6a074c93Sab# Common Development and Distribution License (the "License").
11*6a074c93Sab# You may not use this file except in compliance with the License.
12*6a074c93Sab#
13*6a074c93Sab# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
14*6a074c93Sab# or http://www.opensolaris.org/os/licensing.
15*6a074c93Sab# See the License for the specific language governing permissions
16*6a074c93Sab# and limitations under the License.
17*6a074c93Sab#
18*6a074c93Sab# When distributing Covered Code, include this CDDL HEADER in each
19*6a074c93Sab# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
20*6a074c93Sab# If applicable, add the following below this CDDL HEADER, with the
21*6a074c93Sab# fields enclosed by brackets "[]" replaced with your own identifying
22*6a074c93Sab# information: Portions Copyright [yyyy] [name of copyright owner]
23*6a074c93Sab#
24*6a074c93Sab# CDDL HEADER END
25*6a074c93Sab#
26*6a074c93Sab#ident	"%Z%%M%	%I%	%E% SMI"
27*6a074c93Sab
28*6a074c93Sabuse warnings;
29*6a074c93Sabuse strict;
30*6a074c93Sab
31*6a074c93Sabuse vars qw($script $limit $i);
32*6a074c93Sab
33*6a074c93Sab$script = "libconv_mk_report_bufsize";
34*6a074c93Sab
35*6a074c93Sab
36*6a074c93Sab# This perl script is used by the sgs/libconv makefile to generate an
37*6a074c93Sab# include file named report_bufsize.h. That include file is used to
38*6a074c93Sab# generate an error message that tells the programmer the required
39*6a074c93Sab# size for a libconv buffer type.
40*6a074c93Sab#
41*6a074c93Sab# For details of how that generated file is supposed to be used, please
42*6a074c93Sab# read the comment that is placed at the top of that file, the text of
43*6a074c93Sab# which is found further down in this script.
44*6a074c93Sab
45*6a074c93Sab
46*6a074c93Sab# Highest value to test for
47*6a074c93Sab((scalar(@ARGV) == 1) && (($limit = int($ARGV[0])) > 0)) ||
48*6a074c93Sab	die "usage: $script toplimit\n";
49*6a074c93Sab
50*6a074c93Sabopen(OFILE, ">report_bufsize.h") ||
51*6a074c93Sab	die "$script: Unable to create report_bufsize.h";
52*6a074c93Sab
53*6a074c93Sab
54*6a074c93Sabprint OFILE <<TEXT;
55*6a074c93Sab/*
56*6a074c93Sab * This file was generated by $script,
57*6a074c93Sab * from the libconv Makefile.
58*6a074c93Sab *
59*6a074c93Sab * Many of the buffer types defined in sgs/include/conv.h are defined
60*6a074c93Sab * as a fixed integer rather than using the actual size calculation that
61*6a074c93Sab * would take the lengths of all the possible strings into consideration.
62*6a074c93Sab * The code that implements the conversion function does the proper
63*6a074c93Sab * calculation. It is important that these two values be the same.
64*6a074c93Sab *
65*6a074c93Sab * It is done this way because:
66*6a074c93Sab *
67*6a074c93Sab *	(1) The size calculation uses message length values only available
68*6a074c93Sab *		within the file that implements the conversion function.
69*6a074c93Sab *	(2) Separating the size calculation from the code that uses it
70*6a074c93Sab *		would increase the odds of not updating both, and is
71*6a074c93Sab *		therefore more error prone.
72*6a074c93Sab *
73*6a074c93Sab * If the public size in conv.h and the real size computed by the
74*6a074c93Sab * implementing module are not the same, memory corruption or wasted
75*6a074c93Sab * space will result. Therefore, the code is supposed to contain a
76*6a074c93Sab * preprocessor check that refuses to compile if they don't. This
77*6a074c93Sab * is easy to do, but when the sizes do not match, it can be tedious
78*6a074c93Sab * to determine the right size to set the conv.h value to. This file
79*6a074c93Sab * is used to help with that.
80*6a074c93Sab *
81*6a074c93Sab * Example: Suppose the external size declared in conv.h is
82*6a074c93Sab * CONV_DYN_FLAG_BUFSIZE, and the internal computed value based
83*6a074c93Sab * on the actual strings is FLAGSZ. The module would use the following
84*6a074c93Sab * to ensure they match:
85*6a074c93Sab *
86*6a074c93Sab *	#if CONV_DYN_FLAG_BUFSIZE != FLAGSZ
87*6a074c93Sab *	#define REPORT_BUFSIZE FLAGSZ
88*6a074c93Sab *	#include "report_bufsize.h"
89*6a074c93Sab *	#error "CONV_DYN_FLAG_BUFSIZE does not match FLAGSZ"
90*6a074c93Sab *	#endif
91*6a074c93Sab *
92*6a074c93Sab * In the error case, report_bufsize.h is included. It will use a #warning
93*6a074c93Sab * preprocessor directive to show the current value of the REPORT_BUFSIZE
94*6a074c93Sab * macro. The programmer will therefore see a message giving the correct
95*6a074c93Sab * size of the value that should be defined in conv.h, followed by a
96*6a074c93Sab * #error line telling them which constant is at fault.
97*6a074c93Sab */
98*6a074c93Sab
99*6a074c93Sab#ifndef REPORT_BUFSIZE
100*6a074c93Sab#error "REPORT_BUFSIZE must be defined before including report_bufsize.h"
101*6a074c93SabTEXT
102*6a074c93Sab
103*6a074c93Sabfor ($i = 1; $i <= $limit; $i++) {
104*6a074c93Sab	print OFILE "#elif REPORT_BUFSIZE == $i\n";
105*6a074c93Sab	print OFILE "#warning \"The correct buffer size is $i\"\n";
106*6a074c93Sab}
107*6a074c93Sab
108*6a074c93Sabprint OFILE <<TEXT;
109*6a074c93Sab#else
110*6a074c93Sab#warning "report_bufsize.h encountered value larger than $limit. Please raise the limit and rerun"
111*6a074c93Sab#endif
112*6a074c93SabTEXT
113*6a074c93Sab
114*6a074c93Sabclose OFILE;
115