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