genhash revision 1.2 1 1.1 roy #!/bin/sh
2 1.2 snj # $NetBSD: genhash,v 1.2 2010/02/03 18:49:23 snj Exp $
3 1.1 roy
4 1.1 roy # Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.1 roy #
6 1.1 roy # This code is derived from software contributed to The NetBSD Foundation
7 1.1 roy # by Roy Marples.
8 1.1 roy #
9 1.1 roy # Redistribution and use in source and binary forms, with or without
10 1.1 roy # modification, are permitted provided that the following conditions
11 1.1 roy # are met:
12 1.1 roy # 1. Redistributions of source code must retain the above copyright
13 1.1 roy # notice, this list of conditions and the following disclaimer.
14 1.1 roy # 2. Redistributions in binary form must reproduce the above copyright
15 1.1 roy # notice, this list of conditions and the following disclaimer in the
16 1.1 roy # documentation and/or other materials provided with the distribution.
17 1.1 roy #
18 1.1 roy # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 roy # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 roy # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 roy # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 roy # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 roy # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 roy # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 roy # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 roy # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 roy # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 roy
29 1.1 roy
30 1.1 roy # Generate string and hash tables for our terminfo strings in term.h
31 1.1 roy # We don't expose the hash or tables directly, but instead via functions.
32 1.2 snj # This allows us to freely change how we hash or store our string tables
33 1.1 roy # in the future.
34 1.1 roy
35 1.1 roy : ${TOOL_AWK:=awk}
36 1.1 roy : ${TOOL_NBPERF:=nbperf}
37 1.1 roy : ${TOOL_SED:=sed}
38 1.1 roy
39 1.1 roy TERMH=${1:-term.h}
40 1.1 roy
41 1.1 roy genent()
42 1.1 roy {
43 1.1 roy local name=$1 NAME=$2 len=
44 1.1 roy
45 1.1 roy # Calculate the maximum word length plus terminator
46 1.1 roy len=`$TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
47 1.1 roy -e 's/.*TICODE_\([^,]*\).*/\1X/' $TERMH | \
48 1.1 roy $TOOL_AWK 'BEGIN {L=0} {if (length($1)>L) L=length($1)} END {print L}'`
49 1.1 roy
50 1.1 roy echo
51 1.1 roy echo "static const char _ti_${name}ids[][${len}] = {"
52 1.1 roy $TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
53 1.1 roy -e 's/.*TICODE_\([^,]*\).*/ "\1",/' $TERMH
54 1.1 roy echo "};"
55 1.1 roy echo
56 1.1 roy $TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
57 1.1 roy -e 's/.*TICODE_\([^,]*\).*/\1/' $TERMH | \
58 1.1 roy $TOOL_NBPERF -sn _ti_${name}hash;
59 1.1 roy
60 1.1 roy cat <<EOF
61 1.1 roy
62 1.1 roy const char *
63 1.1 roy _ti_${name}id(ssize_t idx)
64 1.1 roy {
65 1.1 roy
66 1.1 roy if ((size_t)idx > __arraycount(_ti_${name}ids))
67 1.1 roy return NULL;
68 1.1 roy return _ti_${name}ids[idx];
69 1.1 roy }
70 1.1 roy
71 1.1 roy ssize_t
72 1.1 roy _ti_${name}index(const char *key)
73 1.1 roy {
74 1.1 roy uint32_t idx;
75 1.1 roy
76 1.1 roy idx = _ti_${name}hash((const unsigned char *)key, strlen(key));
77 1.1 roy if (idx > __arraycount(_ti_${name}ids) ||
78 1.1 roy strcmp(key, _ti_${name}ids[idx]) != 0)
79 1.1 roy return -1;
80 1.1 roy return idx;
81 1.1 roy }
82 1.1 roy EOF
83 1.1 roy }
84 1.1 roy
85 1.1 roy cat <<EOF
86 1.1 roy /* \$NetBSD\$ */
87 1.1 roy /* DO NOT EDIT
88 1.1 roy * Automatically generated from term.h */
89 1.1 roy
90 1.1 roy #include <sys/cdefs.h>
91 1.2 snj __RCSID("\$NetBSD: genhash,v 1.2 2010/02/03 18:49:23 snj Exp $");
92 1.1 roy
93 1.1 roy #include <sys/types.h>
94 1.1 roy
95 1.1 roy #include <stdlib.h>
96 1.1 roy #include <string.h>
97 1.1 roy #include <term_private.h>
98 1.1 roy #include <term.h>
99 1.1 roy EOF
100 1.1 roy
101 1.1 roy genent flag FLAG
102 1.1 roy genent num NUM
103 1.1 roy genent str STR
104