xref: /netbsd-src/sys/compat/common/gen_errno_tables.awk (revision 95e1ffb15694e54f29f8baaa4232152b703c2a5a)
1*95e1ffb1Schristos# $NetBSD: gen_errno_tables.awk,v 1.3 2005/12/11 12:19:56 christos Exp $
2f12f7955Scgd
3f12f7955Scgd# Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
4f12f7955Scgd#
5f12f7955Scgd# Redistribution and use in source and binary forms, with or without
6f12f7955Scgd# modification, are permitted provided that the following conditions
7f12f7955Scgd# are met:
8f12f7955Scgd# 1. Redistributions of source code must retain the above copyright
9f12f7955Scgd#    notice, this list of conditions and the following disclaimer.
10f12f7955Scgd# 2. Redistributions in binary form must reproduce the above copyright
11f12f7955Scgd#    notice, this list of conditions and the following disclaimer in the
12f12f7955Scgd#    documentation and/or other materials provided with the distribution.
13f12f7955Scgd# 3. All advertising materials mentioning features or use of this software
14f12f7955Scgd#    must display the following acknowledgement:
15f12f7955Scgd#      This product includes software developed by Christopher G. Demetriou
16f12f7955Scgd#      for the NetBSD Project.
17f12f7955Scgd# 4. The name of the author may not be used to endorse or promote products
18f12f7955Scgd#    derived from this software without specific prior written permission
19f12f7955Scgd#
20f12f7955Scgd# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21f12f7955Scgd# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22f12f7955Scgd# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23f12f7955Scgd# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24f12f7955Scgd# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25f12f7955Scgd# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26f12f7955Scgd# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27f12f7955Scgd# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28f12f7955Scgd# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29f12f7955Scgd# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30f12f7955Scgd
31f12f7955Scgd# Currently this script works with either gawk or nawk.
32f12f7955Scgd#
33f12f7955Scgd# Use it like:
34f12f7955Scgd#
35f12f7955Scgd#	awk -f gen_errno_tables.awk -v PREFIX=OSF1 netbsd_errno_hdr \
36f12f7955Scgd#	    osf1_errno_hdr
37f12f7955Scgd#
38f12f7955Scgd# It puts results into 'c' and 'h' in current directory, which can then be
39f12f7955Scgd# incorporated into emulation header files.
40f12f7955Scgd#
41f12f7955Scgd# Note that this script is not meant to generate perfectly pretty output.
42f12f7955Scgd# Wanna be a formatting weenie, hand-edit the output (or make the script
43f12f7955Scgd# output perfectly pretty lists).
44f12f7955Scgd
45f12f7955ScgdBEGIN {
46f12f7955Scgd	nr_offset = 0;
47f12f7955Scgd	idx = 0;
48f12f7955Scgd
49f12f7955Scgd	printf "" > "c"
50f12f7955Scgd	printf "" > "h"
51f12f7955Scgd}
52f12f7955Scgd
53f12f7955ScgdNR != (FNR + nr_offset) {
54f12f7955Scgd	printf("file switch\n");
55f12f7955Scgd	if (idx != 0) {
56f12f7955Scgd		exit 1
57f12f7955Scgd	}
58f12f7955Scgd	nr_offset = (NR - FNR)
59f12f7955Scgd	idx = 1;
60f12f7955Scgd}
61f12f7955Scgd
62f12f7955Scgd/^#[ \t]*define[ \t]+E[A-Z0-9]*[ \t]+[0-9]+/ {
63f12f7955Scgd	if ($1 == "#define") {
64f12f7955Scgd		name=$2
65f12f7955Scgd		val=$3
66f12f7955Scgd	} else {
67f12f7955Scgd		name=$3
68f12f7955Scgd		val=$4
69f12f7955Scgd	}
70f12f7955Scgd
71f12f7955Scgd	if (val_max[idx] < val) {
72f12f7955Scgd		val_max[idx] = val;
73f12f7955Scgd	}
74f12f7955Scgd	if (mappings[idx, "val", val] == "") {
75f12f7955Scgd		mappings[idx, "name", name] = val
76f12f7955Scgd		mappings[idx, "val", val] = name
77f12f7955Scgd	}
78f12f7955Scgd}
79f12f7955Scgd
80f12f7955ScgdEND {
81f12f7955Scgd	if (idx != 1) {
82f12f7955Scgd		exit 1
83f12f7955Scgd	}
84f12f7955Scgd
85f12f7955Scgd	printf("    0,\n") >> "c"
86f12f7955Scgd	for (i = 1; i <= val_max[0]; i++) {
87f12f7955Scgd		nb_name = mappings[0, "val", i]
88f12f7955Scgd		if (nb_name != "") {
89f12f7955Scgd			otheros_val = mappings[1, "name", nb_name]
90f12f7955Scgd			if (otheros_val != "") {
91f12f7955Scgd				printf("    %s_%s,\t\t/* %s (%d) -> %d */\n",
92f12f7955Scgd				    PREFIX, nb_name, nb_name, i,
93f12f7955Scgd				    otheros_val) >> "c"
94f12f7955Scgd			} else {
95f12f7955Scgd				printf("    %s_%s,\t\t/* %s (%d) has no equivalent */\n",
96f12f7955Scgd				    PREFIX, "ENOSYS", nb_name, i) >> "c"
97f12f7955Scgd			}
98f12f7955Scgd		}
99f12f7955Scgd	}
100f12f7955Scgd
101f12f7955Scgd	for (i = 1; i <= val_max[1]; i++) {
102f12f7955Scgd		if (mappings[1, "val", i] != "") {
103f12f7955Scgd			printf("#define %s_%s\t\t%d\n",
104f12f7955Scgd			    PREFIX, mappings[1, "val", i], i) >> "h"
105f12f7955Scgd		}
106f12f7955Scgd	}
107f12f7955Scgd}
108