xref: /illumos-gate/usr/src/lib/libc/port/gen/errlist.awk (revision 1da57d55)
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 (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
22# Use is subject to license terms.
23#
24# Create two files from a list of input strings;
25# new_list.c contains an array of characters indexed into by perror and
26# strerror;
27# errlst.c contains an array of pointers to strings for compatibility
28# with existing user programs that reference it directly;
29# errlst.c references the strings in new_list.c indirectly using a library
30# private symbol, __sys_errs[], in order to get relative relocations.
31#
32# Since the 64 bit ABI doesn't define the old symbols, the second file
33# should be left out 64 bit libraries.
34#
35# WARNING!
36#        Do NOT add entries to this list such that it grows the list
37#        beyond the last entry:
38#              151     Stale NFS file handle
39#        Growing this list may damage programs because this array is
40#        copied into a reserved array at runtime.  See bug 4097669.
41#
42#        If you need to add an entry please use one of the empty
43#        slots.
44#        The arrays _sys_errs[], accessible via perror(3C) and strerror(3C)
45#        interfaces, and sys_errlist[] are created from this list.
46#        It is the direct referencing of sys_errlist[] that is the problem.
47#        Your code should only use perror() or strerror().
48
49
50BEGIN	{
51		FS = "\t"
52		hi = 0
53
54		newfile = "new_list.c"
55		oldfile = "errlst.c"
56
57		print "#pragma weak _sys_errlist = sys_errlist\n" >oldfile
58		print "#include \"lint.h\"\n" >oldfile
59		# We need to include the errors strings proper in the
60		# C source for gettext; the macro C allows us to embed
61		# them as comment.
62		print "#define\tC(x)\n" >oldfile
63		print "extern const char __sys_errs[];\n" >oldfile
64		print "const char *sys_errlist[] = {" >oldfile
65
66		print "#include \"lint.h\"" >newfile
67		print "#include <sys/isa_defs.h>\n" >newfile
68		print "#pragma weak __sys_errs = _sys_errs\n" >newfile
69	}
70
71/^[0-9]+/ {
72		if ($1 > hi)
73			hi = $1
74		astr[$1] = $2
75	}
76
77END	{
78		print "const int _sys_index[] =\n{" >newfile
79		k = 0
80		mx = 151	# max number of entries for sys_errlist[]
81		if (hi > mx)
82		{
83			printf "awk: ERROR! sys_errlist[] > %d entries\n", mx
84			printf "Please read comments in"
85			printf " usr/src/lib/libc/port/gen/errlist\n"
86			exit 1
87		}
88		for (j = 0; j <= hi; ++j)
89		{
90			if (astr[j] == "")
91				astr[j] = sprintf("Error %d", j)
92			printf "\t%d,\n", k >newfile
93			printf "\t&__sys_errs[%d], C(\"%s\")\n", k, astr[j] \
94				>oldfile
95			k += length(astr[j]) + 1
96		}
97		print "};\n" >newfile
98
99		print "/* This is one long string */" >newfile
100		printf "const char _sys_errs[%d] =\n", k >newfile
101		for (j = 0; j <= hi; ++j)
102		{
103			printf "\t\"%s\\0\"\n", astr[j] >newfile
104		}
105		print ";\n" >newfile
106		print "};\n" >oldfile
107
108		print "const int _sys_num_err = " hi + 1 ";\n" >newfile
109		print "#undef sys_nerr" >newfile
110		print "#ifndef _LP64" >newfile
111		print "#pragma weak _sys_nerr = _sys_num_err" >newfile
112		print "#pragma weak sys_nerr = _sys_num_err" >newfile
113		print "#endif /* _LP64 */" >newfile
114	}
115