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